/
GPGif.m
351 lines (288 loc) · 10.2 KB
/
GPGif.m
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
//
// GPGif.m
// TestApp
//
// Created by Dalton Cherry on 12/17/12.
// Copyright (c) 2012 Basement Krew. All rights reserved.
//
/*
http://github.com/daltoniam/GPLib-iOS
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
*/
//
#import "GPGif.h"
@implementation GPGif
@synthesize images,animationDuration;
// http://en.wikipedia.org/wiki/Graphics_Interchange_Format#Example_.gif_file
// http://en.wikipedia.org/wiki/Graphics_Interchange_Format#Animated_.gif
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
-(void)runDecoder:(NSData*)gifData
{
GIFpointer = gifData;
[GIFbuffer release];
[GIFglobal release];
[GIFscreen release];
[GIFdelays release];
[GIFframesData release];
GIFbuffer = [[NSMutableData alloc] init];
GIFglobal = [[NSMutableData alloc] init];
GIFscreen = [[NSMutableData alloc] init];
GIFframeHeader = nil;
GIFdelays = [[NSMutableArray alloc] init];
GIFframesData = [[NSMutableArray alloc] init];
// Reset file counters to 0
dataPointer = 0;
// GIF89a, throw away
int length = 6;
if ([GIFpointer length] >= dataPointer + length)
dataPointer += length;
[self GIFGetBytes: 7]; // Logical Screen Descriptor
// Deep copy
[GIFscreen setData: GIFbuffer];
// Copy the read bytes into a local buffer on the stack
// For easy byte access in the following lines.
length = [GIFbuffer length];
unsigned char aBuffer[length];
[GIFbuffer getBytes:aBuffer length:length];
if (aBuffer[4] & 0x80)
GIFcolorF = 1;
else
GIFcolorF = 0;
if (aBuffer[4] & 0x08)
GIFsorted = 1;
else
GIFsorted = 0;
GIFcolorC = (aBuffer[4] & 0x07);
GIFcolorS = 2 << GIFcolorC;
if (GIFcolorF == 1)
{
[self GIFGetBytes: (3 * GIFcolorS)];
[GIFglobal setData:GIFbuffer]; // Deep copy
}
unsigned char bBuffer[1];
while([self GIFGetBytes:1])
{
[GIFbuffer getBytes:bBuffer length:1];
if (bBuffer[0] == 0x3B) // This is the end
break;
else if (bBuffer[0] == 0x21)
[self GIFReadExtensions];
else if(bBuffer[0] == 0x2C)
[self GIFReadDescriptor];
}
NSMutableArray *array = [NSMutableArray arrayWithCapacity:[GIFframesData count]];
for (int i = 0; i < [GIFframesData count]; i++)
[array addObject: [self getFrameAsImageAtIndex:i]];
self.images = array;
double total = 0;
for (int i = 0; i < [GIFdelays count]; i++)
total += [[GIFdelays objectAtIndex:i] doubleValue];
// GIFs store the delays as 1/100th of a second,
// UIImageViews want it in seconds.
self.animationDuration = total/100;
// clean up stuff
[GIFbuffer release];
GIFbuffer = nil;
[GIFscreen release];
GIFscreen = nil;
[GIFglobal release];
GIFglobal = nil;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- (void)GIFReadExtensions
{
// 21! But we still could have an Application Extension,
// so we want to check for the full signature.
unsigned char cur[1], prev[1];
[self GIFGetBytes:1];
[GIFbuffer getBytes:cur length:1];
while (cur[0] != 0x00)
{
// TODO: Known bug, the sequence F9 04 could occur in the Application Extension, we
// should check whether this combo follows directly after the 21.
if (cur[0] == 0x04 && prev[0] == 0xF9)
{
[self GIFGetBytes:5];
unsigned char buffer[5];
[GIFbuffer getBytes:buffer length:5];
// We save the delays for easy access.
[GIFdelays addObject:[NSNumber numberWithInt:(buffer[1] | buffer[2] << 8)]];
if (GIFframeHeader == nil)
{
unsigned char board[8];
board[0] = 0x21;
board[1] = 0xF9;
board[2] = 0x04;
for(int i = 3, a = 0; a < 5; i++, a++)
{
board[i] = buffer[a];
}
GIFframeHeader = [NSMutableData dataWithBytes:board length:8];
}
break;
}
prev[0] = cur[0];
[self GIFGetBytes:1];
[GIFbuffer getBytes:cur length:1];
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- (void) GIFReadDescriptor
{
[self GIFGetBytes:9];
// Deep copy
NSMutableData *GIF_screenTmp = [NSMutableData dataWithData:GIFbuffer];
unsigned char aBuffer[9];
[GIFbuffer getBytes:aBuffer length:9];
if (aBuffer[8] & 0x80)
GIFcolorF = 1;
else
GIFcolorF = 0;
unsigned char GIF_code = GIFcolorC, GIF_sort = GIFsorted;
if (GIFcolorF == 1)
{
GIF_code = (aBuffer[8] & 0x07);
if (aBuffer[8] & 0x20)
GIF_sort = 1;
else
GIF_sort = 0;
}
int GIF_size = (2 << GIF_code);
size_t blength = [GIFscreen length];
unsigned char bBuffer[blength];
[GIFscreen getBytes:bBuffer length:blength];
bBuffer[4] = (bBuffer[4] & 0x70);
bBuffer[4] = (bBuffer[4] | 0x80);
bBuffer[4] = (bBuffer[4] | GIF_code);
if (GIF_sort)
bBuffer[4] |= 0x08;
NSMutableData *GIF_string = [NSMutableData dataWithData:[@"GIF89a" dataUsingEncoding: NSUTF8StringEncoding]];
[GIFscreen setData:[NSData dataWithBytes:bBuffer length:blength]];
[GIF_string appendData: GIFscreen];
if (GIFcolorF == 1)
{
[self GIFGetBytes:(3 * GIF_size)];
[GIF_string appendData: GIFbuffer];
}
else
[GIF_string appendData: GIFglobal];
// Add Graphic Control Extension Frame (for transparancy)
[GIF_string appendData:GIFframeHeader];
char endC = 0x2c;
[GIF_string appendBytes:&endC length:sizeof(endC)];
size_t clength = [GIF_screenTmp length];
unsigned char cBuffer[clength];
[GIF_screenTmp getBytes:cBuffer length:clength];
cBuffer[8] &= 0x40;
[GIF_screenTmp setData:[NSData dataWithBytes:cBuffer length:clength]];
[GIF_string appendData: GIF_screenTmp];
[self GIFGetBytes:1];
[GIF_string appendData: GIFbuffer];
while (true)
{
[self GIFGetBytes:1];
[GIF_string appendData: GIFbuffer];
unsigned char dBuffer[1];
[GIFbuffer getBytes:dBuffer length:1];
long u = (long) dBuffer[0];
if (u != 0x00)
{
[self GIFGetBytes:u];
[GIF_string appendData: GIFbuffer];
}
else
break;
}
endC = 0x3b;
[GIF_string appendBytes:&endC length:sizeof(endC)];
// save the frame into the array of frames
[GIFframesData addObject:[GIF_string copy]];
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/* Puts (int) length into the GIF_buffer from file, returns whether read was succesfull */
- (BOOL)GIFGetBytes:(int)length
{
[GIFbuffer release]; // Release old buffer
GIFbuffer = nil;
if ([GIFpointer length] >= dataPointer + length) // Don't read across the edge of the file..
{
GIFbuffer = [[GIFpointer subdataWithRange:NSMakeRange(dataPointer, length)] retain];
dataPointer += length;
return YES;
}
else
return NO;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- (NSMutableData*)getFrameAsDataAtIndex:(int)index
{
if (index < [GIFframesData count])
return [GIFframesData objectAtIndex:index];
return nil;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Use this to put a subframe on your GUI.
- (UIImage*) getFrameAsImageAtIndex:(int)index
{
NSData *frameData = [self getFrameAsDataAtIndex: index];
UIImage *image = nil;
if (frameData != nil)
image = [UIImage imageWithData:frameData];
return image;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- (void) dealloc
{
[GIFbuffer release];
[GIFscreen release];
[GIFglobal release];
[GIFdelays release];
[GIFframesData release];
[super dealloc];
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+(BOOL)isGif:(NSData*)data
{
return YES;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+(GPGif*)decodeGif:(NSData*)data
{
GPGif* gif = [[[GPGif alloc] init] autorelease];
[gif runDecoder:data];
return gif;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+(void)setupImageView:(UIImageView*)imgView gifObject:(GPGif*)gifObject
{
[imgView setAnimationImages:gifObject.images];
[imgView setImage:[gifObject.images objectAtIndex:0]];
[imgView setAnimationRepeatCount:0];
[imgView setAnimationDuration:gifObject.animationDuration];
[imgView startAnimating];
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+(void)imageView:(UIImageView*)imgView withData:(NSData*)gifData
{
GPGif* gif = [GPGif decodeGif:gifData];
[GPGif setupImageView:imgView gifObject:gif];
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@end