forked from CocoaBob/xee
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathXeeGIFLoader.m
504 lines (404 loc) · 10.6 KB
/
XeeGIFLoader.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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
#import "XeeGIFLoader.h"
#import "XeeView.h"
@implementation XeeGIFImage
+(NSArray *)fileTypes
{
return [NSArray arrayWithObjects:@"gif",@"'GIFf'",@"'GIF '",nil];
}
+(BOOL)canOpenFile:(NSString *)name firstBlock:(NSData *)block attributes:(NSDictionary *)attributes
{
const char *bytes=[block bytes];
if([block length]>6&&bytes[0]=='G'&&bytes[1]=='I'&&bytes[2]=='F'&&bytes[3]=='8'
&&(bytes[4]=='7'||bytes[4]=='9')&&bytes[5]=='a') return YES;
return NO;
}
-(id)init
{
if(self=[super init])
{
gif=NULL;
frames=nil;
comments=nil;
backup=NULL;
globalpal=nil;
animationtimer=nil;
}
return self;
}
-(void)dealloc
{
[frames release];
free(backup);
[globalpal release];
[animationtimer release];
[super dealloc];
}
static int XeeGIFReadData(GifFileType *gif,GifByteType *buf,int len)
{
return [(CSHandle *)gif->UserData readAtMost:len toBuffer:buf];
}
-(SEL)initLoader
{
gif=DGifOpen([self handle],XeeGIFReadData);
if(!gif) return NULL;
width=gif->SWidth;
height=gif->SHeight;
[self setDepthIndexed:1<<gif->SColorResolution];
[self setFormat:@"GIF"];
background=gif->SBackGroundColor;
currframe=0;
frametime=0;
transindex=-1;
disposal=0;
backupneeded=NO;
return @selector(startLoading);
}
-(void)deallocLoader
{
if(gif) DGifCloseFile(gif);
}
-(SEL)startLoading
{
if(![self allocWithType:XeeBitmapTypeARGB8 width:width height:height]) return NULL;
transparent=NO; // hack transparent status
frames=[[NSMutableArray array] retain];
globalpal=[[XeeGIFPalette alloc] initWithColorMap:gif->SColorMap];
return @selector(loadRecord);
}
-(SEL)loadRecord
{
GifRecordType rectype;
if(DGifGetRecordType(gif,&rectype)==GIF_ERROR) return @selector(failLoading);
if(rectype==EXTENSION_RECORD_TYPE)
{
int code;
GifByteType *ext;
if(DGifGetExtension(gif,&code,&ext)==GIF_ERROR) return @selector(failLoading);
if(ext)
{
if(code==GRAPHICS_EXT_FUNC_CODE&&ext[0]==4) // graphics control extension
{
frametime=(ext[2]&0xff)|((ext[3]&0xff)<<8);
if(frametime==0||frametime==1) frametime=10; // oh boy, broken software!
disposal=(ext[1]&0x1c)>>2;
if(disposal==4) disposal=3;
if(disposal==3) backupneeded=YES;
if(ext[1]&0x01)
{
transindex=ext[4];
if([frames count]==0) transparent=YES;
if(disposal==2&&transindex==background) transparent=YES;
if(gif->SColorMap&&transindex<gif->SColorMap->ColorCount)
[self setBackgroundColor:[NSColor colorWithCalibratedRed:(float)gif->SColorMap->Colors[transindex].Red/255.0
green:(float)gif->SColorMap->Colors[transindex].Green/255.0
blue:(float)gif->SColorMap->Colors[transindex].Blue/255.0
alpha:1]];
}
else
{
transindex=-1;
}
do { if(DGifGetExtensionNext(gif,&ext)==GIF_ERROR) return @selector(failLoading); }
while(ext);
}
else if(code==COMMENT_EXT_FUNC_CODE) // graphics control extension
{
if(!comments)
{
comments=[NSMutableArray array];
[properties addObject:[XeePropertyItem itemWithLabel:
NSLocalizedString(@"File comments",@"File comments section title")
value:comments identifier:@"common.comments"]];
}
NSMutableData *commentdata=[NSMutableData data];
do
{
[commentdata appendBytes:ext+1 length:ext[0]];
if(DGifGetExtensionNext(gif,&ext)==GIF_ERROR) return @selector(failLoading);
}
while(ext);
[comments addObject:[XeePropertyItem itemWithLabel:@""
value:[[[NSString alloc] initWithData:commentdata encoding:NSISOLatin1StringEncoding]
autorelease]]];
}
else
{
do { if(DGifGetExtensionNext(gif,&ext)==GIF_ERROR) return @selector(failLoading); }
while(ext);
}
}
}
else if(rectype==IMAGE_DESC_RECORD_TYPE)
{
if(DGifGetImageDesc(gif)==GIF_ERROR) return @selector(failLoading);
int frameleft=gif->Image.Left;
int frametop=gif->Image.Top;
int framewidth=gif->Image.Width;
int frameheight=gif->Image.Height;
int interlaced=gif->Image.Interlace;
XeeGIFPalette *pal;
if(gif->Image.ColorMap) pal=[[[XeeGIFPalette alloc] initWithColorMap:gif->Image.ColorMap] autorelease];
else pal=globalpal;
XeeGIFFrame *frame=[[[XeeGIFFrame alloc] initWithWidth:framewidth height:frameheight left:frameleft top:frametop
time:frametime transparent:transindex disposal:disposal palette:pal] autorelease];
if(!frame) return NULL;
[frames addObject:frame];
unsigned char *framedata=[frame data];
if(interlaced)
{
for(int y=0;y<frameheight;y+=8)
{
unsigned char *line=framedata+y*framewidth;
if(DGifGetLine(gif,line,framewidth)==GIF_ERROR) return @selector(failLoading);
}
for(int y=4;y<frameheight;y+=8)
{
unsigned char *line=framedata+y*framewidth;
if(DGifGetLine(gif,line,framewidth)==GIF_ERROR) return @selector(failLoading);
}
for(int y=2;y<frameheight;y+=4)
{
unsigned char *line=framedata+y*framewidth;
if(DGifGetLine(gif,line,framewidth)==GIF_ERROR) return @selector(failLoading);
}
for(int y=1;y<frameheight;y+=2)
{
unsigned char *line=framedata+y*framewidth;
if(DGifGetLine(gif,line,framewidth)==GIF_ERROR) return @selector(failLoading);
}
}
else
{
if(DGifGetLine(gif,framedata,framewidth*frameheight)==GIF_ERROR) return @selector(failLoading);
}
if([frames count]==1)
{
[self clearImage];
[(XeeGIFFrame *)[frames objectAtIndex:0] draw:self];
[self setCompleted];
}
}
else return @selector(finishLoading);
return @selector(loadRecord);
}
-(SEL)failLoading
{
if([frames count]==1)
{
[self clearImage];
[(XeeGIFFrame *)[frames objectAtIndex:0] draw:self];
[self setCompleted];
}
return NULL;
}
-(SEL)finishLoading
{
if(backupneeded)
{
backup=malloc(4*width*height);
if(!backup) return NULL;
}
if([frames count]>1)
{
int totaltime=0;
NSEnumerator *enumerator=[frames objectEnumerator];
XeeGIFFrame *frame;
while(frame=[enumerator nextObject]) totaltime+=[frame time];
[properties addObject:[XeePropertyItem subSectionItemWithLabel:
NSLocalizedString(@"GIF animation properties",@"GIF animation properties section title")
identifier:@"gif.animation"
labelsAndValues:
NSLocalizedString(@"Number of frames",@"Number of frames GIF property label"),
[NSNumber numberWithInt:[frames count]],
NSLocalizedString(@"Total playing time",@"Total playing time GIF property label"),
[NSString stringWithFormat:
NSLocalizedString(@"%.2f seconds",@"A time in seconds with two decimals"),
(float)totaltime/100.0+0.005],
nil]];
}
loaded=YES;
[self triggerPropertyChangeAction];
return NULL;
}
-(int)frames { return frames?[frames count]:0; }
-(void)setFrame:(int)frame
{
if(frame==currframe) return;
if(frame<currframe)
{
[self clearImage]; // not needed if first image paints all - should check
currframe=-1;
}
else [[frames objectAtIndex:currframe] dispose:self];
for(int i=currframe+1;i<frame;i++) [[frames objectAtIndex:i] drawAndDispose:self];
[(XeeGIFFrame *)[frames objectAtIndex:frame] draw:self];
currframe=frame;
[self invalidate];
}
-(int)frame { return currframe; }
-(BOOL)animated { return [frames count]>1; }
-(void)setAnimating:(BOOL)animating
{
if(animating)
{
animticks=0;
if(!animationtimer)
{
animationtimer=[[NSTimer scheduledTimerWithTimeInterval:1.0/100.0 target:self selector:@selector(animate:) userInfo:nil repeats:YES] retain];
}
}
else
{
if(animationtimer)
{
[animationtimer invalidate];
[animationtimer release];
animationtimer=nil;
}
}
}
-(void)setAnimatingDefault { [self setAnimating:YES]; }
-(BOOL)animating { return animationtimer?YES:NO; }
-(void)animate:(NSTimer *)timer
{
if([frames count]<=1) return;
if(![self loaded]) return;
animticks++;
if(animticks>=[[frames objectAtIndex:currframe] time])
{
int numframes=[frames count];
[self setFrame:(currframe+1)%numframes];
//[self triggerUpdateAction];
animticks=0;
}
}
-(void)clearImage
{
uint32_t *ptr=(uint32_t *)data;
int n=(bytesperrow/4)*height;
uint32_t val;
if(transindex>=0) val=0x00000000;
else val=[globalpal table][background];
while(n--) *ptr++=val;
}
-(int)background { return background; }
-(uint32_t *)backup { return backup; }
@end
@implementation XeeGIFFrame
-(id)initWithWidth:(int)framewidth height:(int)frameheight left:(int)frameleft
top:(int)frametop time:(int)frametime transparent:(int)trans disposal:(int)disp
palette:(XeeGIFPalette *)pal
{
if(self=[super init])
{
width=framewidth;
height=frameheight;
left=frameleft;
top=frametop;
time=frametime;
transparent=trans;
disposal=disp;
palette=[pal retain];
data=malloc(width*height);
if(data)
{
return self;
}
[self release];
}
return nil;
}
-(void)dealloc
{
[palette release];
free(data);
[super dealloc];
}
-(void)draw:(XeeGIFImage *)image
{
if(left<0||top<0||left+width>[image width]||top+height>[image height]) return;
uint32_t *destdata=(uint32_t *)[image data];
uint32_t *backup=(uint32_t *)[image backup];
uint32_t *ptable=[palette table];
uint8_t *src=data;
int destwidth=[image bytesPerRow]/4;
if(disposal==3&&backup)
for(int y=0;y<height;y++)
{
uint32_t *orig=destdata+(top+y)*destwidth+left;
int n=width;
while(n--) *backup++=*orig++;
}
for(int y=0;y<height;y++)
{
uint32_t *dest=destdata+(top+y)*destwidth+left;
int n=width;
while(n--)
{
int col=*src++;
if(col!=transparent) *dest=ptable[col];
dest++;
}
}
}
-(void)dispose:(XeeGIFImage *)image
{
if(left<0||top<0||left+width>[image width]||top+height>[image height]) return;
uint32_t *destdata=(uint32_t *)[image data];
int destwidth=[image width];
if(disposal==2)
{
int background=[image background];
uint32_t colour;
if(background==transparent) colour=0x00000000;
else colour=[palette table][background];
for(int y=0;y<height;y++)
{
uint32_t *dest=destdata+(top+y)*destwidth+left;
int n=width;
while(n--) *dest++=colour;
}
}
else if(disposal==3)
{
uint32_t *backup=(uint32_t *)[image backup];
if(backup)
for(int y=0;y<height;y++)
{
uint32_t *dest=destdata+(top+y)*destwidth+left;
int n=width;
while(n--) *dest++=*backup++;
}
}
}
-(void)drawAndDispose:(XeeGIFImage *)image
{
if(disposal==3) return;
else if(disposal==2) [self dispose:image];
else [self draw:image];
}
-(unsigned char *)data { return data; }
-(int)time { return time; }
@end
@implementation XeeGIFPalette
-(id)initWithColorMap:(ColorMapObject *)cmap
{
if(self=[super init])
{
if(cmap)
{
int shift=7-cmap->BitsPerPixel;
shift=0;
for(int i=0;i<cmap->ColorCount;i++)
{
int r=cmap->Colors[i].Red<<shift;
int g=cmap->Colors[i].Green<<shift;
int b=cmap->Colors[i].Blue<<shift;
table[i]=XeeMakeARGB8(0xff,r,g,b);
}
}
}
return self;
}
-(uint32_t *)table { return table; }
@end