1 | // tImageGIF.cpp  |
2 | //  |
3 | // This knows how to load gifs. It knows the details of the gif file format and loads the data into multiple tPixel  |
4 | // arrays, one for each frame (gifs 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 | #include <Foundation/tStandard.h>  |
17 | #include <Foundation/tString.h>  |
18 | #include <System/tFile.h>  |
19 | #include <GifLoad/gif_load.h>  |
20 | #include "Image/tImageGIF.h"  |
21 |   |
22 | using namespace tSystem;  |
23 | namespace tImage  |
24 | {  |
25 |   |
26 |   |
27 | // This callback is a essentially the example code from gif_load.  |
28 | void tImageGIF::FrameCallback(struct GIF_WHDR* whdr)  |
29 | {  |
30 | #define RGBA(i) \  |
31 | ( \  |
32 | (whdr->bptr[i] == whdr->tran) ? 0x00000000 : \  |
33 | ( \  |
34 | uint32_t(whdr->cpal[whdr->bptr[i]].B << 16) | \  |
35 | uint32_t(whdr->cpal[whdr->bptr[i]].G << 8) | \  |
36 | uint32_t(whdr->cpal[whdr->bptr[i]].R << 0) | \  |
37 | 0xFF000000 \  |
38 | ) \  |
39 | )  |
40 |   |
41 | // Is first frame?  |
42 | if (whdr->ifrm == 0)  |
43 | {  |
44 | Width = whdr->xdim;  |
45 | Height = whdr->ydim;  |
46 | FrmPict = new tPixel[Width * Height];  |
47 | FrmPrev = new tPixel[Width * Height];  |
48 | }  |
49 |   |
50 | int numPixels = Width * Height;  |
51 | tPixel* pict = FrmPict;  |
52 | tPixel* prev = nullptr;  |
53 |   |
54 | uint32 ddst = uint32(whdr->xdim * whdr->fryo + whdr->frxo);  |
55 |   |
56 | // Interlacing support.  |
57 | uint32 iter = whdr->intr ? 0 : 4;  |
58 | uint32 ifin = !iter ? 4 : 5;  |
59 |   |
60 | int y = 0;  |
61 | for (uint32 dsrc = (uint32)-1; iter < ifin; iter++)  |
62 | for (int yoff = 16U >> ((iter > 1) ? iter : 1), y = (8 >> iter) & 7; y < whdr->fryd; y += yoff)  |
63 | for (int x = 0; x < whdr->frxd; x++)  |
64 | if (whdr->tran != (long)whdr->bptr[++dsrc])  |
65 | pict[whdr->xdim * y + x + ddst].BP = RGBA(dsrc);  |
66 |   |
67 | tImageGIF::Frame* frame = new tImageGIF::Frame();  |
68 | Frames.Append(frame);  |
69 | frame->Pixels = new tPixel[numPixels];  |
70 | frame->Duration = float(whdr->time) / 100.0f;  |
71 |   |
72 | // We store rows starting from the bottom (lower left is 0,0).  |
73 | for (int row = Height-1; row >= 0; row--)  |
74 | tStd::tMemcpy(frame->Pixels + (row*Width), pict + ((Height-row-1)*Width), Width*sizeof(tPixel));  |
75 |   |
76 | if ((whdr->mode == GIF_PREV) && !FrmLast)  |
77 | {  |
78 | whdr->frxd = whdr->xdim;  |
79 | whdr->fryd = whdr->ydim;  |
80 | whdr->mode = GIF_BKGD;  |
81 | ddst = 0;  |
82 | }  |
83 | else  |
84 | {  |
85 | FrmLast = (whdr->mode == GIF_PREV) ? FrmLast : (whdr->ifrm + 1);  |
86 | pict = (whdr->mode == GIF_PREV) ? FrmPict : FrmPrev;  |
87 | prev = (whdr->mode == GIF_PREV) ? FrmPrev : FrmPict;  |
88 | for (int x = whdr->xdim * whdr->ydim; --x;  |
89 | pict[x - 1].BP = prev[x - 1].BP);  |
90 | }  |
91 |   |
92 | // Cutting a hole for the next frame.  |
93 | if (whdr->mode == GIF_BKGD)  |
94 | {  |
95 | int y = 0;  |
96 | for  |
97 | (  |
98 | whdr->bptr[0] = ((whdr->tran >= 0) ? uint8(whdr->tran) : uint8(whdr->bkgd)), y = 0, pict = FrmPict;  |
99 | y < whdr->fryd;  |
100 | y++  |
101 | )  |
102 | {  |
103 | for (int x = 0; x < whdr->frxd; x++)  |
104 | pict[whdr->xdim * y + x + ddst].BP = RGBA(0);  |
105 | }  |
106 | }  |
107 | }  |
108 |   |
109 |   |
110 | bool tImageGIF::Load(const tString& gifFile)  |
111 | {  |
112 | Clear();  |
113 |   |
114 | if (tSystem::tGetFileType(gifFile) != tSystem::tFileType::GIF)  |
115 | return false;  |
116 |   |
117 | if (!tFileExists(gifFile))  |
118 | return false;  |
119 |   |
120 | int numBytes = 0;  |
121 | uint8* gifFileInMemory = tLoadFile(gifFile, nullptr, &numBytes);  |
122 |   |
123 | // This call allocated scratchpad memory pointed to by FrmPict and FrmPrev.  |
124 | // They are set to null just in case GIF_Load fails to allocate.  |
125 | FrmPict = nullptr;  |
126 | FrmPrev = nullptr;  |
127 | int result = GIF_Load(gifFileInMemory, numBytes, FrameCallbackBridge, nullptr, (void*)this, 0);  |
128 | delete[] FrmPict;  |
129 | delete[] FrmPrev;  |
130 | delete[] gifFileInMemory;  |
131 | if (result <= 0)  |
132 | return false;  |
133 |   |
134 | SrcPixelFormat = tPixelFormat::PAL_8BIT;  |
135 | return true;  |
136 | }  |
137 |   |
138 |   |
139 | }  |
140 | |