| 1 | // tImageWEBP.h  |
| 2 | //  |
| 3 | // This knows how to load WebPs. It knows the details of the webp file format and loads the data into multiple tPixel  |
| 4 | // arrays, one for each frame (WebPs may be animated). These arrays may be 'stolen' by tPictures.  |
| 5 | //  |
| 6 | // Copyright (c) 2020 Tristan Grimmer.  |
| 7 | // Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby  |
| 8 | // granted, provided that the above copyright notice and this permission notice appear in all copies.  |
| 9 | //  |
| 10 | // THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL  |
| 11 | // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,  |
| 12 | // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN  |
| 13 | // AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR  |
| 14 | // PERFORMANCE OF THIS SOFTWARE.  |
| 15 |   |
| 16 | #pragma once  |
| 17 | #include <Foundation/tString.h>  |
| 18 | #include <Math/tColour.h>  |
| 19 | #include <Image/tPixelFormat.h>  |
| 20 | namespace tImage  |
| 21 | {  |
| 22 |   |
| 23 |   |
| 24 | class tImageWEBP  |
| 25 | {  |
| 26 | public:  |
| 27 | // Creates an invalid tImageWEBP. You must call Load manually.  |
| 28 | tImageWEBP() { }  |
| 29 | tImageWEBP(const tString& webpFile) { Load(webpFile); }  |
| 30 |   |
| 31 | virtual ~tImageWEBP() { Clear(); }  |
| 32 |   |
| 33 | // Clears the current tImageWEBP before loading. If false returned object is invalid.  |
| 34 | bool Load(const tString& webpFile);  |
| 35 |   |
| 36 | // After this call no memory will be consumed by the object and it will be invalid.  |
| 37 | void Clear();  |
| 38 | bool IsValid() const { return (GetNumFrames() >= 1); }  |
| 39 | int GetNumFrames() const { return Frames.GetNumItems(); }  |
| 40 |   |
| 41 | struct Frame : public tLink<Frame>  |
| 42 | {  |
| 43 | int Width = 0;  |
| 44 | int Height = 0;  |
| 45 | tPixel* Pixels = nullptr;  |
| 46 | float Duration = 0.0f; // Frame duration in seconds. Converted from the WebPs 1ms count.  |
| 47 | tPixelFormat SrcPixelFormat = tPixelFormat::Invalid;  |
| 48 | };  |
| 49 |   |
| 50 | // After this call you are the owner of the frame and must eventually delete it. The frame you stole will no  |
| 51 | // longer be a valid frame of the tImageWEBP, but the remaining ones will still be valid.  |
| 52 | Frame* StealFrame(int );  |
| 53 | Frame* GetFrame(int );  |
| 54 | tPixelFormat SrcPixelFormat = tPixelFormat::Invalid;  |
| 55 |   |
| 56 | private:  |
| 57 | tList<Frame> Frames;  |
| 58 | };  |
| 59 |   |
| 60 |   |
| 61 | // Implementation only below.  |
| 62 |   |
| 63 |   |
| 64 | inline tImageWEBP::Frame* tImage::tImageWEBP::StealFrame(int )  |
| 65 | {  |
| 66 | Frame* f = GetFrame(frameNum);  |
| 67 | if (!f)  |
| 68 | return nullptr;  |
| 69 |   |
| 70 | return Frames.Remove(f);  |
| 71 | }  |
| 72 |   |
| 73 |   |
| 74 | inline tImageWEBP::Frame* tImage::tImageWEBP::GetFrame(int )  |
| 75 | {  |
| 76 | if ((frameNum >= Frames.GetNumItems()) || (frameNum < 0))  |
| 77 | return nullptr;  |
| 78 |   |
| 79 | Frame* f = Frames.First();  |
| 80 | while (frameNum--)  |
| 81 | f = f->Next();  |
| 82 |   |
| 83 | return f;  |
| 84 | }  |
| 85 |   |
| 86 |   |
| 87 | inline void tImageWEBP::Clear()  |
| 88 | {  |
| 89 | while (Frame* frame = Frames.Remove())  |
| 90 | {  |
| 91 | delete[] frame->Pixels;  |
| 92 | delete frame;  |
| 93 | }  |
| 94 |   |
| 95 | SrcPixelFormat = tPixelFormat::Invalid;  |
| 96 | }  |
| 97 |   |
| 98 |   |
| 99 | }  |
| 100 | |