-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGIFLocal.cpp
228 lines (201 loc) · 5.01 KB
/
GIFLocal.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
#include "GIFLocal.h"
using std::cout;
using std::endl;
bool GIFLocalImage::safe(const GIFBlobData *ptr, const int size, const GIFBlobData *endp)
{
if(ptr+size<endp)
return true;
return false;
}
void GIFLocalImage::ignore(GIFBlobData *&ptr)
{
ptr++;
while(*ptr!=TERM_CODE) {
ptr+=(*ptr)+1;
}
ptr++;
}
GIFLocalImage::GIFLocalImage(const GIFBlobData *GCT, const int GCTSize, const bool sorted, GIFBlobData *&ptr, const GIFBlobData *endp)
: GCE(NULL), LCT(NULL), LCTSize(0), dataBlock(NULL), dataBlockSize(0), localImageSize(0)
{
switch(*ptr)
{
case EXTENSION_CODE :
readExtension(ptr, endp);
break;
case IMAGE_CODE:
readImage(GCT, GCTSize, sorted, ptr, endp);
break;
default:
cout << "default Image Part. Ignore it." << endl;
ignore(ptr);
break;
}
}
GIFLocalImage::~GIFLocalImage()
{
if(GCE!=NULL) {
delete[] GCE;
GCE=NULL;
}
if(LCT!=NULL) {
delete[] LCT;
LCT=NULL;
}
if(dataBlock!=NULL) {
delete[] dataBlock;
dataBlock=NULL;
}
}
void GIFLocalImage::readExtension(GIFBlobData *&ptr, const GIFBlobData *endp)
{
ptr++;
switch(*ptr) {
case GCE_CODE :
ptr++;
if(safe(ptr+1, *ptr, endp)) {
this->GCE=new GIFBlobData[GCE_SIZE];
memcpy(this->GCE, ptr-2, GCE_SIZE);
ptr+=(*ptr)+2;
}
break;
default:
cout << "default Extension. Ignore it." << endl;
this->ignore(ptr);
break;
}
}
void GIFLocalImage::readImage(const GIFBlobData *GCT, const int GCTSize, const bool sorted, GIFBlobData *&ptr, const GIFBlobData *endp)
{
if(this->safe(ptr+1, GIF_LOCAL_IMAGE_HEADER_SIZE, endp)) {
memcpy(this->imageHeader, ptr, GIF_LOCAL_IMAGE_HEADER_SIZE);
ptr+=GIF_LOCAL_IMAGE_HEADER_SIZE;
}
else {
cout << "safe fail : imageHeader" << endl;
exit(EXIT_FAILURE);
}
if((this->imageHeader[9]&0x80)==0x80) { // LCT Exist
this->LCTSize=(3*(2<<(this->imageHeader[9]&0x07)));
if(safe(ptr, this->LCTSize, endp)) {
this->LCT=new GIFBlobData[LCTSize];
memcpy(this->LCT, ptr, this->LCTSize);
ptr+=this->LCTSize;
}
else {
cout << "safe fail : LCT" << endl;
exit(EXIT_FAILURE);
}
}
else {
this->setGCTtoLCT(GCT, GCTSize, sorted);
}
this->readDataBlock(ptr, endp);
ptr++; // Indicates Next Something's Start
this->localImageSize=(this->GCE==NULL?0:GCE_SIZE) + GIF_LOCAL_IMAGE_HEADER_SIZE + this->LCTSize + 1 + this->dataBlockSize;
}
void GIFLocalImage::setGCTtoLCT(const GIFBlobData *GCT, const int GCTSize, const bool sorted)
{
this->imageHeader[9]|=0x80;
if(sorted) {
this->imageHeader[9]|=0x20;
}
this->LCTSize=GCTSize;
int GSize=GCTSize/3;
int cnt=0;
while(GSize>2) {
cnt++;
GSize/=2;
}
imageHeader[9]|=cnt;
this->LCT=new GIFBlobData[this->LCTSize];
memcpy(this->LCT, GCT, this->LCTSize);
}
void GIFLocalImage::readDataBlock(GIFBlobData *&ptr, const GIFBlobData *endp)
{
this->LZW=*ptr;
ptr++;
int dataSize=0;
GIFBlobData *start=NULL;
start=ptr;
while(*ptr!=TERM_CODE) {
if(safe(ptr+1, *ptr, endp)) {
dataSize+=(*ptr)+1;
ptr+=(*ptr)+1;
}
else {
cout << "safe fail : dataBlock" << endl;
exit(EXIT_FAILURE);
}
}
dataSize++;
this->dataBlockSize=dataSize;
this->dataBlock=new GIFBlobData[this->dataBlockSize];
memcpy(this->dataBlock, start, this->dataBlockSize);
}
void GIFLocalImage::writeImageBlob(GIFBlobData *&output)
{
if(this->GCE == NULL) {
//cout << "NOGCE" << endl;
} else {
memcpy(output, this->GCE, GCE_SIZE);
output += GCE_SIZE;
}
memcpy(output, this->imageHeader, GIF_LOCAL_IMAGE_HEADER_SIZE);
output += GIF_LOCAL_IMAGE_HEADER_SIZE;
if(this->LCT == NULL) {
//cout << "NOLCT" << endl;
} else {
memcpy(output, this->LCT, this->LCTSize);
output += this->LCTSize;
}
*output=this->LZW;
output++;
memcpy(output, this->dataBlock, this->dataBlockSize);
output += this->dataBlockSize;
}
void GIFLocalImage::writeImageFile(ofstream &output)
{
if(this->GCE == NULL) {
//cout << "NOGCE" << endl;
} else {
output.write((char*)this->GCE, GCE_SIZE);
}
output.write((char*)this->imageHeader, GIF_LOCAL_IMAGE_HEADER_SIZE);
if(this->LCT == NULL) {
//cout << "NOLCT" << endl;
} else {
output.write((char*)this->LCT, this->LCTSize);
}
output.put((char)this->LZW);
output.write((char*)this->dataBlock, this->dataBlockSize);
}
void GIFLocalImage::setMaxScreenOffset(const int maxW, const int maxH)
{
int xOff=((maxW-this->getImageWidth())/2)+this->getImageXOffset();
int yOff=((maxH-this->getImageHeight())/2)+this->getImageYOffset();
this->setImageOffset(xOff, yOff);
}
const int GIFLocalImage::getImageWidth()
{
return this->imageHeader[5] | this->imageHeader[6]<<8;
}
const int GIFLocalImage::getImageHeight()
{
return this->imageHeader[7] | this->imageHeader[8]<<8;
}
const int GIFLocalImage::getImageXOffset()
{
return this->imageHeader[1] | this->imageHeader[2]<<8;
}
const int GIFLocalImage::getImageYOffset()
{
return this->imageHeader[3] | this->imageHeader[4]<<8;
}
void GIFLocalImage::setImageOffset(const int xOff, const int yOff)
{
this->imageHeader[1] = xOff & 0xFF;
this->imageHeader[2] = xOff>>8 & 0xFF;
this->imageHeader[3] = yOff & 0xFF;
this->imageHeader[4] = yOff>>8 & 0xFF;
}