-
Notifications
You must be signed in to change notification settings - Fork 0
/
GIF.cpp
319 lines (276 loc) · 7.39 KB
/
GIF.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
#include "GIF.h"
#include <fstream>
using std::exception;
using std::ifstream;
using std::ios_base;
using std::ios;
using std::cout;
using std::endl;
bool GIF::safe(GIFBlobData *ptr, int size, const GIFBlobData *endp)
{
if(ptr+size<endp)
return true;
return false;
}
void GIF::ignore(GIFBlobData *&ptr)
{
ptr++;
while(*ptr!=TERM_CODE) {
ptr+=(*ptr)+1;
}
ptr++;
}
GIF::GIF(const char *filename)
:GCT(NULL), GCTSize(0), sorted(false), appE(false), imgCnt(0), GIFSize(0)
{
try {
ifstream inputFile(filename, ios_base::binary);
if(!inputFile) {
cout << "Input File Open Failed" << endl;
exit(EXIT_FAILURE);
}
inputFile.seekg(0, ios::end);
int fileSize=inputFile.tellg();
inputFile.seekg(0, ios::beg);
GIFBlobData *inputStream=new GIFBlobData[fileSize];
inputFile.read((char*)inputStream, fileSize);
readGIF(inputStream, fileSize);
delete[] inputStream;
}
catch(const exception &e) {
cout << "Exception : " << e.what() << endl;
}
}
GIF::GIF(GIFBlobData *inputStream, const int fileSize)
:GCT(NULL), GCTSize(0), sorted(false), appE(false), imgCnt(0), GIFSize(0)
{
readGIF(inputStream, fileSize);
}
GIF::GIF(const list<GIF*> &GIFList)
:GCT(NULL), GCTSize(0), sorted(false), appE(false), imgCnt(0), GIFSize(0)
{
list<GIF*>::const_iterator itr = GIFList.begin();
memcpy(GIFHeader, (*itr)->getGIFHeader(), GIF_HEADER_SIZE);
memcpy(logicalScreen, (*itr)->getLogicalScreen(), LOGICAL_SCREEN_SIZE);
logicalScreen[4]&=0x70;
appE=true;
this->setMaxLogicalScreen(GIFList);
for(itr=GIFList.begin(); itr!=GIFList.end(); itr++) {
this->localImageList.assign((*itr)->getLocalImageList().begin(), (*itr)->getLocalImageList().end());
}
this->imgCnt = this->localImageList.size();
this->setLocalImageScreen();
}
GIF::~GIF()
{
if(localImageList.size()!=0) {
for(list<GIFLocalImage*>::iterator itr=localImageList.begin(); itr!=localImageList.end(); itr++) {
delete *itr;
}
}
}
void GIF::readGIF(GIFBlobData *inputStream, const int fileSize)
{
GIFBlobData *ptr = inputStream;
const GIFBlobData *endp=ptr+fileSize;
this->readGIFHeader(ptr, endp);
this->readLogicalScreen(ptr, endp);
this->readGCT(ptr, endp);
while(*ptr!=TRAILER_CODE) {
switch(*ptr) {
case EXTENSION_CODE:
this->readExtension(ptr, endp);
break;
case IMAGE_CODE:
this->localImageList.push_back(new GIFLocalImage(this->GCT, this->GCTSize, this->sorted, ptr, endp));
break;
default:
cout << "default CODE. Ignore it." << endl;
this->ignore(ptr);
break;
}
}
if(GCT != NULL) {
delete[] this->GCT;
this->GCT = NULL;
}
this->GCTSize = 0;
this->imgCnt = this->localImageList.size();
cout << "GIF File Read Done" << endl;
}
void GIF::readGIFHeader(GIFBlobData *&ptr, const GIFBlobData *endp)
{
if(this->safe(ptr, GIF_HEADER_SIZE, endp)) {
memcpy(this->GIFHeader, ptr, GIF_HEADER_SIZE);
ptr+=GIF_HEADER_SIZE;
}
else {
cout << "safe fail : GIF Header" << endl;
exit(EXIT_FAILURE);
}
if(!this->isGIF()) {
cout << "Abnormal GIF Signature Error" << endl;
exit(EXIT_FAILURE);
}
}
bool GIF::isGIF()
{
if(!(this->GIFHeader[0] == 'G' && this->GIFHeader[1] == 'I' && this->GIFHeader[2] == 'F')) {
return false;
}
if(!(this->GIFHeader[3] == '8' && (this->GIFHeader[4] == '9' || this->GIFHeader[4] == '7') && this->GIFHeader[5] == 'a')) {
return false;
}
if(this->GIFHeader[4] == '7')
this->GIFHeader[4]='9';
return true;
}
void GIF::readLogicalScreen(GIFBlobData *&ptr, const GIFBlobData *endp)
{
if(safe(ptr, LOGICAL_SCREEN_SIZE, endp)) {
memcpy(this->logicalScreen, ptr, LOGICAL_SCREEN_SIZE);
ptr+=LOGICAL_SCREEN_SIZE;
}
else {
cout << "safe fail : Logical Screen" << endl;
exit(EXIT_FAILURE);
}
}
void GIF::readGCT(GIFBlobData *&ptr, const GIFBlobData *endp)
{
if(this->logicalScreen[4]&0x80 != 0x80) {
// cout << "NO GCT" << endl;
}
else {
this->GCTSize=3*(2<<(this->logicalScreen[4]&0x07));
this->GCT=new GIFBlobData[this->GCTSize];
if(safe(ptr, this->GCTSize, endp)) {
memcpy(this->GCT, ptr, this->GCTSize);
ptr+=this->GCTSize;
}
else {
cout << "safe fail : GCT" << endl;
exit(EXIT_FAILURE);
}
}
if(this->logicalScreen[4]&0x08 == 0x08) {
this->sorted=true;
}
}
void GIF::readExtension(GIFBlobData *&ptr, const GIFBlobData *endp)
{
ptr++;
switch(*ptr) {
case APPE_CODE:
ptr++;
while(*ptr!=TERM_CODE) {
GIFBlobData *appe=new GIFBlobData[*ptr];
if(safe(ptr+1, *ptr, endp)) {
memcpy(appe, ptr+1, *ptr);
ptr+=(*ptr)+1;
if(strcmp((char*)appe, "NETSCAPE2.0")) {
this->appE=true;
}
}
else {
cout << "safe fail : Application Extension" << endl;
exit(EXIT_FAILURE);
}
}
break;
case GCE_CODE:
ptr--;
this->localImageList.push_back(new GIFLocalImage(this->GCT, this->GCTSize, this->sorted, ptr, endp));
break;
}
}
void GIF::writeBlob(GIFBlobData *outputStream, const int imgNum)
{
GIFBlobData *ptr = outputStream;
memcpy(ptr, this->GIFHeader, GIF_HEADER_SIZE);
ptr += GIF_HEADER_SIZE;
memcpy(ptr, this->logicalScreen, LOGICAL_SCREEN_SIZE);
ptr += LOGICAL_SCREEN_SIZE;
if(this->appE || imgNum == ALLIMGS) {
memcpy(ptr, APP_EXT, APPE_SIZE);
ptr += APPE_SIZE;
}
list<GIFLocalImage*>::iterator imgItr=this->localImageList.begin();
if(imgNum == ALLIMGS) {
for(; imgItr != this->localImageList.end(); imgItr++) {
(*imgItr)->writeImageBlob(ptr);
}
} else {
for(int i=0; i<imgNum; i++)
imgItr++;
(*imgItr)->writeImageBlob(ptr);
}
*ptr = TRAILER_CODE;
}
void GIF::writeFile(const char* filename, const int imgNum)
{
ofstream output(filename, ios_base::binary);
if(!output) {
cout << "Writing File Open Failed" << endl;
return;
}
output.write((char*)this->GIFHeader, GIF_HEADER_SIZE);
output.write((char*)this->logicalScreen, LOGICAL_SCREEN_SIZE);
if(this->appE || imgNum == ALLIMGS) {
output.write((char*)APP_EXT, APPE_SIZE);
}
list<GIFLocalImage*>::iterator imgItr = this->localImageList.begin();
if(imgNum == ALLIMGS) {
for(; imgItr != this->localImageList.end(); imgItr++)
(*imgItr)->writeImageFile(output);
} else {
for(int i=0; i<imgNum; i++)
imgItr++;
(*imgItr)->writeImageFile(output);
}
output.put(TRAILER_CODE);
output.close();
}
void GIF::setMaxLogicalScreen(const list<GIF*> &GIFList)
{
int maxW = 0, maxH = 0;
list<GIF*>::const_iterator itr = GIFList.begin();
for(; itr != GIFList.end(); itr++) {
if((*itr)->getLogicalWidth() > maxW) {
maxW = (*itr)->getLogicalWidth();
}
if((*itr)->getLogicalHeight() > maxH) {
maxH = (*itr)->getLogicalHeight();
}
}
this->logicalScreen[0] = maxW & 0xFF;
this->logicalScreen[1] = maxW>>8 & 0xFF;
this->logicalScreen[2] = maxH & 0xFF;
this->logicalScreen[3] = maxH>>8 & 0xFF;
}
const int GIF::getLogicalWidth() {
return this->logicalScreen[0] | this->logicalScreen[1]<<8;
}
const int GIF::getLogicalHeight() {
return this->logicalScreen[2] | this->logicalScreen[3]<<8;
}
void GIF::setLocalImageScreen()
{
list<GIFLocalImage*>::iterator itr = this->localImageList.begin();
for(; itr != this->localImageList.end(); itr++) {
(*itr)->setMaxScreenOffset(this->getLogicalWidth(), this->getLogicalHeight());
}
}
const int GIF::getGIFSize(const int imgNum)
{
int size=this->GIFSize;
list<GIFLocalImage*>::const_iterator imgItr = this->localImageList.begin();
if(imgNum == ALLIMGS) {
for(; imgItr != this->localImageList.end(); imgItr++)
size += (*imgItr)->getLocalImageSize();
} else {
for(int i=0; i<imgNum; i++)
imgItr++;
size += (*imgItr)->getLocalImageSize();
}
}