forked from mapic91/MpcAsfTool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGifData.cpp
370 lines (332 loc) · 11.4 KB
/
GifData.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
#include "wx/log.h"
#include <cstring>
#include <cstdlib>
#include <fstream>
#include "GifData.hpp"
using namespace std;
GifData::GifData(int width, int height, int interval, bool istransparent)
{
Init();
Width = width;
Height = height;
Interval = (interval+9)/10;
isTrans = istransparent;
}
GifData::~GifData()
{
if(FirstPage != NULL)
{
GifFrame *cur, *next;
cur = FirstPage;
next = cur->next;
for(; cur != NULL;)
{
free(cur->data);
delete cur;
cur = next;
next = cur->next;
}
}
}
void GifData::Init()
{
Width = 0;
Height = 0;
Interval = 10;
isTrans = false;
FrameCounts = 0;
FirstPage = NULL;
LastPage = NULL;
memset((void*)Palette, 0, sizeof(FILOCRGBQUAD)*256);
}
void GifData::AddPage(GifFrameData* page)
{
if(page == NULL) return;
if(FrameCounts == 0)
{
FirstPage = new GifFrame();
FirstPage->data = page->data;
FirstPage->datasize = page->datasize;
FirstPage->next = NULL;
LastPage = FirstPage;
}
else
{
LastPage->next = new GifFrame();
LastPage->next->data = page->data;
LastPage->next->datasize = page->datasize;
LastPage->next->next = NULL;
LastPage = LastPage->next;
}
FrameCounts++;
}
bool GifData::Save()
{
return false;//noncomplete,to be continue...
if(OutPath.IsEmpty() || FrameCounts == 0) return false;
unsigned char temp[20];
ofstream outfile(OutPath.char_str(), ios_base::binary|ios_base::out);
if(!outfile.is_open()) return false;
outfile.write("GIF89a", 6);
temp[0] = (unsigned char)((Width&0xFF));
temp[1] = (unsigned char)((Width&0xFF00) >> 8);
temp[2] = (unsigned char)((Height&0xFF));
temp[3] = (unsigned char)((Height&0xFF00) >> 8);
outfile.write((char*)temp, 4);
temp[0] = 0;
temp[0] |= 0x80;//globle palette
temp[0] |= 0x70;//pixel size
temp[0] |= 0x7; //globle palette size
temp[1] = 0xFF; //background color index
temp[2] = 0; //pixel aspect ratio
outfile.write((char*)temp, 3);
for(int pltidx = 0; pltidx < 256; pltidx++)
{
temp[0] = Palette[pltidx].rgbRed;
temp[1] = Palette[pltidx].rgbGreen;
temp[3] = Palette[pltidx].rgbBlue;
outfile.write((char*)temp, 3);
}
//animate control
temp[0] = 0x21;
temp[1] = 0xFF;
temp[2] = 0x0B;
temp[3] = 0x4E;
temp[4] = 0x45;
temp[5] = 0x54;
temp[6] = 0x53;
temp[7] = 0x43;
temp[8] = 0x41;
temp[9] = 0x50;
temp[10]= 0x45;
temp[11]= 0x32;
temp[12]= 0x2E;
temp[13]= 0x30;
temp[14]= 0x03;
temp[15]= 0x01;
temp[16]= 0x00;
temp[17]= 0x00;
temp[18]=0x00;
outfile.write((char*)temp, 19);
unsigned char grapctl[8];
grapctl[0] = 0x21; //ExtensionIntroducer
grapctl[1] = 0xF9; //GraphicControlLabel
grapctl[2] = 0x04; //Block Size
grapctl[3] = 0x00; //Flags
grapctl[3]|= 0x08; //DisposalMethod
grapctl[3]|= 0x01; //Transparent Flag
grapctl[4] = (unsigned char)((Interval&0xFF)); // Delay Time
grapctl[5] = (unsigned char)((Interval&0xFF00) >> 8);// Delay Time
grapctl[6] = 0xFF; //TransparetnColorIndex
grapctl[7] = 0x00; //BlockTerminator
unsigned char imghed[10];
imghed[0] = 0x2C; //ImageSeperator
imghed[1] = 0x00; //ImageLeftPosition
imghed[2] = 0x00; //ImageLeftPosition
imghed[3] = 0x00; //ImageTopPosition
imghed[4] = 0x00; //ImageTopPosition
imghed[5] = (unsigned char)((Width&0xFF)); //ImageWidth
imghed[6] = (unsigned char)((Width&0xFF00) >> 8); //ImageWidth
imghed[7] = (unsigned char)((Height&0xFF)); //ImageHeight
imghed[8] = (unsigned char)((Height&0xFF00) >> 8); //ImageHeight
imghed[9] = 0x00; //Falg
for(int frmidx = 0; frmidx < FrameCounts; frmidx++)
{
outfile.write((char*)grapctl, 8);
outfile.write((char*)imghed, 10);
//to be continue...
}
}
FIMEMORY *GifData::FIPaletteLoctoGlb(FIMEMORY *instream)
{
if(instream == NULL) return NULL;
FIMEMORY *outstream = FreeImage_OpenMemory(NULL, 0);
if(outstream == NULL) return NULL;
unsigned char temp[256], gbplt[256*3];
FreeImage_SeekMemory(instream, 0, SEEK_SET);
FreeImage_ReadMemory(temp, 13, 1, instream);
if(memcmp(temp, "GIF89a", 6) != 0) return NULL;
if((temp[0x0A] & 0x80) != 0 ) return NULL;
FreeImage_WriteMemory("GIF89a", 6, 1, outstream);
temp[0] = (unsigned char)((Width&0xFF));
temp[1] = (unsigned char)((Width&0xFF00) >> 8);
temp[2] = (unsigned char)((Height&0xFF));
temp[3] = (unsigned char)((Height&0xFF00) >> 8);
FreeImage_WriteMemory(temp, 4, 1, outstream);
temp[0] = 0;
temp[0] |= 0x80;//globle palette
temp[0] |= 0x70;//pixel size
temp[0] |= 0x7; //globle palette size
temp[1] = 0xFF; //background color index
temp[2] = 0; //pixel aspect ratio
FreeImage_WriteMemory(temp, 3, 1, outstream);
FreeImage_SeekMemory(instream, 0xD, SEEK_SET);
FreeImage_ReadMemory(temp, 2, 1, instream);
for(; temp[0] != 0x2C ;)
{
if(temp[0] == 0x21)
{
if(temp[1] == 0xFF)
{
FreeImage_SeekMemory(instream, 12, SEEK_CUR);//application head
FreeImage_ReadMemory(temp, 1, 1, instream);// data size
FreeImage_SeekMemory(instream, temp[0]+1, SEEK_CUR);
}
else if(temp[1] == 0xF9)
{
FreeImage_SeekMemory(instream, 6, SEEK_CUR);//control head
}
}
FreeImage_ReadMemory(temp, 2, 1, instream);
}
FreeImage_SeekMemory(instream, 8, SEEK_CUR);//img head
FreeImage_ReadMemory(gbplt, 256*3, 1, instream);//Local palette content
FreeImage_WriteMemory(gbplt, 256*3, 1, outstream);//Global palette
unsigned char grapctl[8];
grapctl[0] = 0x21; //ExtensionIntroducer
grapctl[1] = 0xF9; //GraphicControlLabel
grapctl[2] = 0x04; //Block Size
grapctl[3] = 0x00; //Flags
grapctl[3]|= 0x08; //DisposalMethod
grapctl[3]|= 0x01; //Transparent Flag
grapctl[4] = (unsigned char)((Interval&0xFF)); // Delay Time
grapctl[5] = (unsigned char)((Interval&0xFF00) >> 8);// Delay Time
grapctl[6] = 0xFF; //TransparetnColorIndex
grapctl[7] = 0x00; //BlockTerminator
unsigned char imghed[10];
imghed[0] = 0x2C; //ImageSeperator
imghed[1] = 0x00; //ImageLeftPosition
imghed[2] = 0x00; //ImageLeftPosition
imghed[3] = 0x00; //ImageTopPosition
imghed[4] = 0x00; //ImageTopPosition
imghed[5] = (unsigned char)((Width&0xFF)); //ImageWidth
imghed[6] = (unsigned char)((Width&0xFF00) >> 8); //ImageWidth
imghed[7] = (unsigned char)((Height&0xFF)); //ImageHeight
imghed[8] = (unsigned char)((Height&0xFF00) >> 8); //ImageHeight
imghed[9] = 0x00; //Falg
FreeImage_SeekMemory(instream, 0xD, SEEK_SET);
FreeImage_ReadMemory(temp, 1, 1, instream);
for(; temp[0] != 0x3B;) //0x3B indicate file end
{
FreeImage_ReadMemory(temp+1, 1, 1, instream);
if(temp[0] == 0x21)
{
if(temp[1] == 0xFF) // Application ext,control animation
{
//Skip
FreeImage_SeekMemory(instream, 12, SEEK_CUR);//application head
FreeImage_ReadMemory(temp, 1, 1, instream);// data size
FreeImage_SeekMemory(instream, temp[0]+1, SEEK_CUR);
//Save predefined setting
temp[0] = 0x21;
temp[1] = 0xFF;
temp[2] = 0x0B;
temp[3] = 0x4E;
temp[4] = 0x45;
temp[5] = 0x54;
temp[6] = 0x53;
temp[7] = 0x43;
temp[8] = 0x41;
temp[9] = 0x50;
temp[10]= 0x45;
temp[11]= 0x32;
temp[12]= 0x2E;
temp[13]= 0x30;
temp[14]= 0x03;
temp[15]= 0x01;
temp[16]= 0x00;
temp[17]= 0x00;
temp[18]= 0x00;
FreeImage_WriteMemory(temp, 19, 1, outstream);
}
else if(temp[1] == 0xF9)//control ext
{
FreeImage_SeekMemory(instream, 6, SEEK_CUR);//control head
FreeImage_WriteMemory(grapctl, 8, 1, outstream);
}
}
else // 0x2C
{
FreeImage_WriteMemory(imghed, 10, 1, outstream);
FreeImage_SeekMemory(instream, 8+256*3, SEEK_CUR);
FreeImage_ReadMemory(temp, 1, 1, instream); //LZWMinimumCodeSize
FreeImage_WriteMemory(temp, 1, 1, outstream);
FreeImage_ReadMemory(temp, 1, 1, instream);
for(;temp[0] != 0; )//Image data
{
FreeImage_ReadMemory(temp+1, temp[0], 1, instream);
FreeImage_WriteMemory(temp, temp[0]+1, 1, outstream);
FreeImage_ReadMemory(temp, 1, 1, instream);
}
FreeImage_WriteMemory(temp, 1, 1, outstream);
}
FreeImage_ReadMemory(temp, 1, 1, instream);
}
temp[0] = 0x3B;//file end
FreeImage_WriteMemory(temp, 1, 1, outstream);
return outstream;
}
bool GifData::FilePaletteLoctoGlb(const wxString inpath, long Interval)
{
FIMULTIBITMAP *giff;
FIBITMAP *page;
unsigned width,height,filesize;
FIMEMORY *gifmem, *glbpal;
FIBOOL res;
FIBYTE *buffer;
FIDWORD bufsize;
wxString outpath = inpath;
giff = FreeImage_OpenMultiBitmap(FIF_GIF, inpath.char_str(), FALSE, TRUE);
if(!giff)
{
return false;
}
page = FreeImage_LockPage(giff, 0);
width = FreeImage_GetWidth(page);
height = FreeImage_GetHeight(page);
FreeImage_UnlockPage(giff, page, FALSE);
FreeImage_CloseMultiBitmap(giff, 0); giff = NULL;
long intv = Interval;
//wxString strintv;
//strintv = TextCtrl_interval->GetLineText(0);
//if(!strintv.ToLong(&intv))return;
GifData data((int)width, (int)height, intv);
std::ifstream ingiffile(inpath.char_str(), std::ios_base::binary|std::ios_base::in);
if(!ingiffile.is_open()) return false;
ingiffile.seekg(0, std::ios_base::end);
filesize = (unsigned)ingiffile.tellg()+1;
ingiffile.seekg(0, std::ios_base::beg);
buffer = (FIBYTE*)malloc((size_t)filesize);
if(!buffer) return false;
ingiffile.read((char*)buffer, filesize);
if(ingiffile.bad())
{free(buffer);return false;}
ingiffile.close();
gifmem = FreeImage_OpenMemory(buffer, filesize);
if(!gifmem)
{
return false;
}
glbpal = data.FIPaletteLoctoGlb(gifmem);
FreeImage_CloseMemory(gifmem); gifmem = NULL;
free(buffer);buffer = NULL;
if(!glbpal)
{
return false;
}
res = FreeImage_AcquireMemory(glbpal, &buffer, &bufsize);
if(!res)
{
FreeImage_CloseMemory(glbpal);
return false;
}
std::ofstream outfile(outpath.char_str(), std::ios_base::binary|std::ios_base::out);
if(!outfile.is_open())
{
FreeImage_CloseMemory(glbpal);
return false;
}
outfile.write((char*)buffer, bufsize);
FreeImage_CloseMemory(glbpal);
return true;
}