forked from qdtroy/DuiLib_Ultimate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathximagif.cpp
1683 lines (1491 loc) · 49.2 KB
/
ximagif.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
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
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* File: ximagif.cpp
* Purpose: Platform Independent GIF Image Class Loader and Writer
* 07/Aug/2001 Davide Pizzolato - www.xdp.it
* CxImage version 7.0.1 07/Jan/2011
*/
#include "ximagif.h"
#if CXIMAGE_SUPPORT_GIF
#include "ximaiter.h"
#if defined (_WIN32_WCE)
#define assert(s)
#else
#include <assert.h>
#endif
////////////////////////////////////////////////////////////////////////////////
CxImageGIF::CxImageGIF(): CxImage(CXIMAGE_FORMAT_GIF)
{
buf = new uint8_t [GIFBUFTAM + 1];
stack = new uint8_t [MAX_CODES + 1];
suffix = new uint8_t [MAX_CODES + 1];
prefix = new uint16_t [MAX_CODES + 1];
htab = new int32_t [HSIZE];
codetab = new uint16_t [HSIZE];
byte_buff = new uint8_t [257];
accum = new char [256];
m_comment = new char [256];
m_loops=0;
info.dispmeth=0;
m_comment[0]='\0';
}
////////////////////////////////////////////////////////////////////////////////
CxImageGIF::~CxImageGIF()
{
delete [] buf;
delete [] stack;
delete [] suffix;
delete [] prefix;
delete [] htab;
delete [] codetab;
delete [] byte_buff;
delete [] accum;
delete [] m_comment;
}
////////////////////////////////////////////////////////////////////////////////
#if CXIMAGE_SUPPORT_DECODE
////////////////////////////////////////////////////////////////////////////////
bool CxImageGIF::Decode(CxFile *fp)
{
/* AD - for transparency */
struct_dscgif dscgif;
struct_image image;
struct_TabCol TabCol;
if (fp == NULL) return false;
fp->Read(&dscgif,/*sizeof(dscgif)*/13,1);
//if (strncmp(dscgif.header,"GIF8",3)!=0) {
if (strncmp(dscgif.header,"GIF8",4)!=0) return FALSE;
// Avoid Byte order problem with Mac <AMSN>
dscgif.scrheight = m_ntohs(dscgif.scrheight);
dscgif.scrwidth = m_ntohs(dscgif.scrwidth);
if (info.nEscape == -1) {
// Return output dimensions only
head.biWidth = dscgif.scrwidth;
head.biHeight = dscgif.scrheight;
info.dwType = CXIMAGE_FORMAT_GIF;
return true;
}
/* AD - for interlace */
TabCol.sogct = (int16_t)(1 << ((dscgif.pflds & 0x07)+1));
TabCol.colres = (int16_t)(((dscgif.pflds & 0x70) >> 4) + 1);
// assume that the image is a truecolor-gif if
// 1) no global color map found
// 2) (image.w, image.h) of the 1st image != (dscgif.scrwidth, dscgif.scrheight)
int32_t bTrueColor=0;
CxImage* imaRGB=NULL;
// Global colour map?
if (dscgif.pflds & 0x80)
fp->Read(TabCol.paleta,sizeof(struct rgb_color)*TabCol.sogct,1);
else
bTrueColor++; //first chance for a truecolor gif
int32_t first_transparent_index = 0;
int32_t iImage = 0;
info.nNumFrames=get_num_frames(fp,&TabCol,&dscgif);
if ((info.nFrame<0)||(info.nFrame>=info.nNumFrames)) return false;
//it cannot be a true color GIF with only one frame
if (info.nNumFrames == 1)
bTrueColor=0;
char ch;
bool bPreviousWasNull = true;
int32_t prevdispmeth = 0;
CxImage *previousFrame = NULL;
for (BOOL bContinue = TRUE; bContinue; )
{
if (fp->Read(&ch, sizeof(ch), 1) != 1) {break;}
if (info.nEscape > 0) return false; // <vho> - cancel decoding
if (bPreviousWasNull || ch==0)
{
switch (ch)
{
case '!': // extension
{
bContinue = DecodeExtension(fp);
break;
}
case ',': // image
{
assert(sizeof(image) == 9);
fp->Read(&image,sizeof(image),1);
//avoid byte order problems with Solaris <candan> <AMSN>
image.l = m_ntohs(image.l);
image.t = m_ntohs(image.t);
image.w = m_ntohs(image.w);
image.h = m_ntohs(image.h);
if (((image.l + image.w) > dscgif.scrwidth)||((image.t + image.h) > dscgif.scrheight))
break;
// check if it could be a truecolor gif
if ((iImage==0) && (image.w != dscgif.scrwidth) && (image.h != dscgif.scrheight))
bTrueColor++;
rgb_color locpal[256]; //Local Palette
rgb_color* pcurpal = TabCol.paleta; //Current Palette
int16_t palcount = TabCol.sogct; //Current Palette color count
// Local colour map?
if (image.pf & 0x80) {
palcount = (int16_t)(1 << ((image.pf & 0x07) +1));
assert(3 == sizeof(struct rgb_color));
fp->Read(locpal,sizeof(struct rgb_color)*palcount,1);
pcurpal = locpal;
}
int32_t bpp; //<DP> select the correct bit per pixel value
if (palcount <= 2) bpp = 1;
else if (palcount <= 16) bpp = 4;
else bpp = 8;
CxImageGIF backimage;
backimage.CopyInfo(*this);
if (iImage==0){
//first frame: build image background
backimage.Create(dscgif.scrwidth, dscgif.scrheight, bpp, CXIMAGE_FORMAT_GIF);
first_transparent_index = info.nBkgndIndex;
backimage.Clear((uint8_t)gifgce.transpcolindex);
previousFrame = new CxImage(backimage);
previousFrame->SetRetreiveAllFrames(false);
} else {
//generic frame: handle disposal method from previous one
/*Values : 0 - No disposal specified. The decoder is
not required to take any action.
1 - Do not dispose. The graphic is to be left
in place.
2 - Restore to background color. The area used by the
graphic must be restored to the background color.
3 - Restore to previous. The decoder is required to
restore the area overwritten by the graphic with
what was there prior to rendering the graphic.
*/
/* backimage.Copy(*this);
if (prevdispmeth==2){
backimage.Clear((uint8_t)first_transparent_index);
}*/
if (prevdispmeth==2){
backimage.Copy(*this,false,false,false);
backimage.Clear((uint8_t)first_transparent_index);
} else if (prevdispmeth==3) {
backimage.Copy(*this,false,false,false);
backimage.Create(previousFrame->GetWidth(),
previousFrame->GetHeight(),
previousFrame->GetBpp(),CXIMAGE_FORMAT_GIF);
memcpy(backimage.GetDIB(),previousFrame->GetDIB(),
backimage.GetSize());
//backimage.AlphaSet(*previousFrame);
} else {
backimage.Copy(*this);
}
}
//active frame
Create(image.w, image.h, bpp, CXIMAGE_FORMAT_GIF);
if ((image.pf & 0x80) || (dscgif.pflds & 0x80)) {
uint8_t r[256], g[256], b[256];
int32_t i, has_white = 0;
for (i=0; i < palcount; i++) {
r[i] = pcurpal[i].r;
g[i] = pcurpal[i].g;
b[i] = pcurpal[i].b;
if (RGB(r[i],g[i],b[i]) == 0xFFFFFF) has_white = 1;
}
// Force transparency colour white...
//if (0) if (info.nBkgndIndex >= 0)
// r[info.nBkgndIndex] = g[info.nBkgndIndex] = b[info.nBkgndIndex] = 255;
// Fill in with white // AD
if (info.nBkgndIndex >= 0) {
while (i < 256) {
has_white = 1;
r[i] = g[i] = b[i] = 255;
i++;
}
}
// Force last colour to white... // AD
//if ((info.nBkgndIndex >= 0) && !has_white) {
// r[255] = g[255] = b[255] = 255;
//}
SetPalette((info.nBkgndIndex >= 0 ? 256 : palcount), r, g, b);
}
CImageIterator* iter = new CImageIterator(this);
iter->Upset();
int32_t badcode=0;
ibf = GIFBUFTAM+1;
interlaced = image.pf & 0x40;
iheight = image.h;
istep = 8;
iypos = 0;
ipass = 0;
int32_t pos_start = fp->Tell();
//if (interlaced) log << "Interlaced" << endl;
decoder(fp, iter, image.w, badcode);
delete iter;
if (info.nEscape) return false; // <vho> - cancel decoding
if (bTrueColor<2 ){ //standard GIF: mix frame with background
backimage.IncreaseBpp(bpp);
backimage.GifMix(*this,image);
backimage.SetTransIndex(first_transparent_index);
backimage.SetPalette(GetPalette());
Transfer(backimage,false);
} else { //it's a truecolor gif!
//force full image decoding
info.nFrame=info.nNumFrames-1;
//build the RGB image
if (imaRGB==NULL) imaRGB = new CxImage(dscgif.scrwidth,dscgif.scrheight,24,CXIMAGE_FORMAT_GIF);
//copy the partial image into the full RGB image
for(int32_t y=0;y<image.h;y++){
for (int32_t x=0;x<image.w;x++){
imaRGB->SetPixelColor(x+image.l,dscgif.scrheight-1-image.t-y,GetPixelColor(x,image.h-y-1));
}
}
}
prevdispmeth = (gifgce.flags >> 2) & 0x7;
//restore the correct position in the file for the next image
if (badcode){
seek_next_image(fp,pos_start);
} else {
fp->Seek(-(ibfmax - ibf - 1), SEEK_CUR);
}
if (info.bGetAllFrames && imaRGB == NULL) {
if (iImage == 0) {
DestroyFrames();
ppFrames = new CxImage*[info.nNumFrames];
for(int32_t frameIdx = 0; frameIdx < info.nNumFrames; frameIdx++){
ppFrames[frameIdx] = NULL;
}
}
ppFrames[iImage] = new CxImage(*this);
ppFrames[iImage]->SetRetreiveAllFrames(false);
}
if (prevdispmeth <= 1) {
delete previousFrame;
previousFrame = new CxImage(*this);
previousFrame->SetRetreiveAllFrames(false);
}
if ((info.nFrame==iImage) && (info.bGetAllFrames==false)) bContinue=false; else iImage++;
break;
}
case ';': //terminator
bContinue=false;
break;
default:
bPreviousWasNull = (ch==0);
break;
}
}
}
if (bTrueColor>=2 && imaRGB){
if (gifgce.flags & 0x1){
imaRGB->SetTransColor(GetPaletteColor((uint8_t)info.nBkgndIndex));
imaRGB->SetTransIndex(0);
}
Transfer(*imaRGB);
}
delete imaRGB;
delete previousFrame;
return true;
}
////////////////////////////////////////////////////////////////////////////////
bool CxImageGIF::DecodeExtension(CxFile *fp)
{
bool bContinue;
uint8_t count;
uint8_t fc;
bContinue = (1 == fp->Read(&fc, sizeof(fc), 1));
if (bContinue) {
/* AD - for transparency */
if (fc == 0xF9) {
bContinue = (1 == fp->Read(&count, sizeof(count), 1));
if (bContinue) {
assert(sizeof(gifgce) == 4);
bContinue = (count == fp->Read(&gifgce, 1, sizeof(gifgce)));
gifgce.delaytime = m_ntohs(gifgce.delaytime); // Avoid Byte order problem with Mac <AMSN>
if (bContinue) {
info.nBkgndIndex = (gifgce.flags & 0x1) ? gifgce.transpcolindex : -1;
info.dwFrameDelay = gifgce.delaytime;
SetDisposalMethod((gifgce.flags >> 2) & 0x7);
} } }
if (fc == 0xFE) { //<DP> Comment block
bContinue = (1 == fp->Read(&count, sizeof(count), 1));
if (bContinue) {
bContinue = (1 == fp->Read(m_comment, count, 1));
m_comment[count]='\0';
} }
if (fc == 0xFF) { //<DP> Application Extension block
bContinue = (1 == fp->Read(&count, sizeof(count), 1));
if (bContinue) {
bContinue = (count==11);
if (bContinue){
char AppID[11];
bContinue = (1 == fp->Read(AppID, count, 1));
if (bContinue) {
bContinue = (1 == fp->Read(&count, sizeof(count), 1));
if (bContinue) {
uint8_t* dati = (uint8_t*)malloc(count);
bContinue = (dati!=NULL);
if (bContinue){
bContinue = (1 == fp->Read(dati, count, 1));
if (count>2){
m_loops = dati[1]+256*dati[2];
}
}
free(dati);
} } } } }
while (bContinue && fp->Read(&count, sizeof(count), 1) && count) {
//log << "Skipping " << count << " bytes" << endl;
fp->Seek(count, SEEK_CUR);
}
}
return bContinue;
}
////////////////////////////////////////////////////////////////////////////////
#endif //CXIMAGE_SUPPORT_DECODE
////////////////////////////////////////////////////////////////////////////////
// - This external (machine specific) function is expected to return
// either the next uint8_t from the GIF file, or a negative error number.
int32_t CxImageGIF::get_byte(CxFile* file)
{
if (ibf>=GIFBUFTAM){
// FW 06/02/98 >>>
ibfmax = (int32_t)file->Read( buf , 1 , GIFBUFTAM) ;
if( ibfmax < GIFBUFTAM ) buf[ ibfmax ] = 255 ;
// FW 06/02/98 <<<
ibf = 0;
}
if (ibf>=ibfmax) return -1; //<DP> avoid overflows
return buf[ibf++];
}
////////////////////////////////////////////////////////////////////////////////
/* - This function takes a full line of pixels (one uint8_t per pixel) and
* displays them (or does whatever your program wants with them...). It
* should return zero, or negative if an error or some other event occurs
* which would require aborting the decode process... Note that the length
* passed will almost always be equal to the line length passed to the
* decoder function, with the sole exception occurring when an ending code
* occurs in an odd place in the GIF file... In any case, linelen will be
* equal to the number of pixels passed...
*/
int32_t CxImageGIF::out_line(CImageIterator* iter, uint8_t *pixels, int32_t linelen)
{
if (iter == NULL || pixels == NULL)
return -1;
//<DP> for 1 & 4 bpp images, the pixels are compressed
if (head.biBitCount < 8){
for(int32_t x=0;x<head.biWidth;x++){
uint8_t pos;
uint8_t* iDst= pixels + (x*head.biBitCount >> 3);
if (head.biBitCount==4){
pos = (uint8_t)(4*(1-x%2));
*iDst &= ~(0x0F<<pos);
*iDst |= ((pixels[x] & 0x0F)<<pos);
} else if (head.biBitCount==1){
pos = (uint8_t)(7-x%8);
*iDst &= ~(0x01<<pos);
*iDst |= ((pixels[x] & 0x01)<<pos);
}
}
}
/* AD - for interlace */
if (interlaced) {
iter->SetY(iheight-iypos-1);
iter->SetRow(pixels, linelen);
if ((iypos += istep) >= iheight) {
do {
if (ipass++ > 0) istep /= 2;
iypos = istep / 2;
}
while (iypos > iheight);
}
return 0;
} else {
if (iter->ItOK()) {
iter->SetRow(pixels, linelen);
(void)iter->PrevRow();
return 0;
} else {
// puts("chafeo");
return -1;
}
}
}
////////////////////////////////////////////////////////////////////////////////
#if CXIMAGE_SUPPORT_ENCODE
////////////////////////////////////////////////////////////////////////////////
// SaveFile - writes GIF87a gif file
// Randy Spann 6/15/97
bool CxImageGIF::Encode(CxFile * fp)
{
if (EncodeSafeCheck(fp)) return false;
if(head.biBitCount > 8) {
//strcpy(info.szLastError,"GIF Images must be 8 bit or less");
//return FALSE;
return EncodeRGB(fp);
}
if ( GetNumFrames()>1 && ppFrames ) {
return Encode(fp, ppFrames, GetNumFrames() );
}
EncodeHeader(fp);
EncodeExtension(fp);
EncodeComment(fp);
EncodeBody(fp);
fp->PutC(';'); // Write the GIF file terminator
return true; // done!
}
////////////////////////////////////////////////////////////////////////////////
bool CxImageGIF::Encode(CxFile * fp, CxImage ** pImages, int32_t pagecount, bool bLocalColorMap, bool bLocalDispMeth)
{
cx_try {
if (fp==NULL) cx_throw("invalid file pointer");
if (pImages==NULL || pagecount<=0 || pImages[0]==NULL) cx_throw("multipage GIF, no images!");
int32_t i;
for (i=0; i<pagecount; i++){
if (pImages[i]==NULL)
cx_throw("Bad image pointer");
if (!(pImages[i]->IsValid()))
cx_throw("Empty image");
if (pImages[i]->GetNumColors()==0)
cx_throw("CxImageGIF::Encode cannot create animated GIFs with a true color frame. Use DecreaseBpp before");
}
CxImageGIF ghost;
//write the first image
ghost.Ghost(pImages[0]);
ghost.EncodeHeader(fp);
if (m_loops!=1){
ghost.SetLoops(max(0,m_loops-1));
ghost.EncodeLoopExtension(fp);
}
if (bLocalDispMeth) {
ghost.EncodeExtension(fp);
} else {
uint8_t dm = ghost.GetDisposalMethod();
ghost.SetDisposalMethod(GetDisposalMethod());
ghost.EncodeExtension(fp);
ghost.SetDisposalMethod(dm);
}
EncodeComment(fp);
ghost.EncodeBody(fp);
for (i=1; i<pagecount; i++){
ghost.Ghost(pImages[i]);
if (bLocalDispMeth) {
ghost.EncodeExtension(fp);
} else {
uint8_t dm = ghost.GetDisposalMethod();
ghost.SetDisposalMethod(GetDisposalMethod());
ghost.EncodeExtension(fp);
ghost.SetDisposalMethod(dm);
}
ghost.EncodeBody(fp,bLocalColorMap);
}
fp->PutC(';'); // Write the GIF file terminator
} cx_catch {
if (strcmp(message,"")) strncpy(info.szLastError,message,255);
return false;
}
return true;
}
////////////////////////////////////////////////////////////////////////////////
void CxImageGIF::EncodeHeader(CxFile *fp)
{
fp->Write("GIF89a",1,6); //GIF Header
Putword(head.biWidth,fp); //Logical screen descriptor
Putword(head.biHeight,fp);
uint8_t Flags;
if (head.biClrUsed==0){
Flags=0x11;
} else {
Flags = 0x80;
Flags |=(head.biBitCount - 1) << 5;
Flags |=(head.biBitCount - 1);
}
fp->PutC(Flags); //GIF "packed fields"
fp->PutC(0); //GIF "BackGround"
fp->PutC(0); //GIF "pixel aspect ratio"
if (head.biClrUsed!=0){
RGBQUAD* pPal = GetPalette();
for(uint32_t i=0; i<head.biClrUsed; ++i)
{
fp->PutC(pPal[i].rgbRed);
fp->PutC(pPal[i].rgbGreen);
fp->PutC(pPal[i].rgbBlue);
}
}
}
////////////////////////////////////////////////////////////////////////////////
void CxImageGIF::EncodeExtension(CxFile *fp)
{
// TRK BEGIN : transparency
fp->PutC('!');
fp->PutC(TRANSPARENCY_CODE);
gifgce.flags = 0;
gifgce.flags |= ((info.nBkgndIndex != -1) ? 1 : 0);
gifgce.flags |= ((GetDisposalMethod() & 0x7) << 2);
gifgce.delaytime = (uint16_t)info.dwFrameDelay;
gifgce.transpcolindex = (uint8_t)info.nBkgndIndex;
//Invert byte order in case we use a byte order arch, then set it back <AMSN>
gifgce.delaytime = m_ntohs(gifgce.delaytime);
fp->PutC(sizeof(gifgce));
fp->Write(&gifgce, sizeof(gifgce), 1);
gifgce.delaytime = m_ntohs(gifgce.delaytime);
fp->PutC(0);
// TRK END
}
////////////////////////////////////////////////////////////////////////////////
void CxImageGIF::EncodeLoopExtension(CxFile *fp)
{
fp->PutC('!'); //byte 1 : 33 (hex 0x21) GIF Extension code
fp->PutC(255); //byte 2 : 255 (hex 0xFF) Application Extension Label
fp->PutC(11); //byte 3 : 11 (hex (0x0B) Length of Application Block (eleven bytes of data to follow)
fp->Write("NETSCAPE2.0",11,1);
fp->PutC(3); //byte 15 : 3 (hex 0x03) Length of Data Sub-Block (three bytes of data to follow)
fp->PutC(1); //byte 16 : 1 (hex 0x01)
Putword(m_loops,fp); //bytes 17 to 18 : 0 to 65535, an unsigned integer in lo-hi byte format.
//This indicate the number of iterations the loop should be executed.
fp->PutC(0); //bytes 19 : 0 (hex 0x00) a Data Sub-block Terminator.
}
////////////////////////////////////////////////////////////////////////////////
void CxImageGIF::EncodeBody(CxFile *fp, bool bLocalColorMap)
{
curx = 0;
cury = head.biHeight - 1; //because we read the image bottom to top
CountDown = (int32_t)head.biWidth * (int32_t)head.biHeight;
fp->PutC(',');
Putword(info.xOffset,fp);
Putword(info.yOffset,fp);
Putword(head.biWidth,fp);
Putword(head.biHeight,fp);
uint8_t Flags=0x00; //non-interlaced (0x40 = interlaced) (0x80 = LocalColorMap)
if (bLocalColorMap) { Flags|=0x80; Flags|=head.biBitCount-1; }
fp->PutC(Flags);
if (bLocalColorMap){
Flags|=0x87;
RGBQUAD* pPal = GetPalette();
for(uint32_t i=0; i<head.biClrUsed; ++i)
{
fp->PutC(pPal[i].rgbRed);
fp->PutC(pPal[i].rgbGreen);
fp->PutC(pPal[i].rgbBlue);
}
}
int32_t InitCodeSize = head.biBitCount <=1 ? 2 : head.biBitCount;
// Write out the initial code size
fp->PutC((uint8_t)InitCodeSize);
// Go and actually compress the data
switch (GetCodecOption(CXIMAGE_FORMAT_GIF))
{
case 1: //uncompressed
compressNONE(InitCodeSize+1, fp);
break;
case 2: //RLE
compressRLE(InitCodeSize+1, fp);
break;
default: //LZW
compressLZW(InitCodeSize+1, fp);
}
// Write out a Zero-length packet (to end the series)
fp->PutC(0);
}
////////////////////////////////////////////////////////////////////////////////
void CxImageGIF::EncodeComment(CxFile *fp)
{
uint32_t n = (uint32_t) strlen(m_comment);
if (n>255) n=255;
if (n) {
fp->PutC('!'); //extension code:
fp->PutC(254); //comment extension
fp->PutC((uint8_t)n); //size of comment
fp->Write(m_comment,n,1);
fp->PutC(0); //block terminator
}
}
////////////////////////////////////////////////////////////////////////////////
bool CxImageGIF::EncodeRGB(CxFile *fp)
{
EncodeHeader(fp);
// EncodeLoopExtension(fp);
EncodeComment(fp);
uint32_t w,h;
w=h=0;
const int32_t cellw = 17;
const int32_t cellh = 15;
CxImageGIF tmp;
for (int32_t y=0;y<head.biHeight;y+=cellh){
for (int32_t x=0;x<head.biWidth;x+=cellw){
if ((head.biWidth -x)<cellw) w=head.biWidth -x; else w=cellw;
if ((head.biHeight-y)<cellh) h=head.biHeight-y; else h=cellh;
if (w!=tmp.GetWidth() || h!=tmp.GetHeight()) tmp.Create(w,h,8);
if (IsTransparent()){
tmp.SetTransIndex(0);
tmp.SetPaletteColor(0,GetTransColor());
}
uint8_t i;
for (uint32_t j=0;j<h;j++){
for (uint32_t k=0;k<w;k++){
i=(uint8_t)(1+k+cellw*j);
tmp.SetPaletteColor(i,GetPixelColor(x+k,head.biHeight-y-h+j));
tmp.SetPixelIndex(k,j,tmp.GetNearestIndex(tmp.GetPaletteColor(i)));
}
}
tmp.SetOffset(x,y);
tmp.EncodeExtension(fp);
tmp.EncodeBody(fp,true);
}
}
fp->PutC(';'); // Write the GIF file terminator
return true; // done!
}
////////////////////////////////////////////////////////////////////////////////
#endif // CXIMAGE_SUPPORT_ENCODE
////////////////////////////////////////////////////////////////////////////////
// Return the next pixel from the image
// <DP> fix for 1 & 4 bpp images
int32_t CxImageGIF::GifNextPixel( )
{
if( CountDown == 0 ) return EOF;
--CountDown;
int32_t r = GetPixelIndex(curx,cury);
// Bump the current X position
++curx;
if( curx == head.biWidth ){
curx = 0;
cury--; //bottom to top
}
return r;
}
////////////////////////////////////////////////////////////////////////////////
void CxImageGIF::Putword(int32_t w, CxFile *fp )
{
fp->PutC((uint8_t)(w & 0xff));
fp->PutC((uint8_t)((w >> 8) & 0xff));
}
////////////////////////////////////////////////////////////////////////////////
void CxImageGIF::compressNONE( int32_t init_bits, CxFile* outfile)
{
register int32_t c;
register int32_t ent;
// g_init_bits - initial number of bits
// g_outfile - pointer to output file
g_init_bits = init_bits;
g_outfile = outfile;
// Set up the necessary values
cur_accum = cur_bits = clear_flg = 0;
maxcode = (int16_t)MAXCODE(n_bits = g_init_bits);
code_int maxmaxcode = (code_int)1 << MAXBITSCODES;
ClearCode = (1 << (init_bits - 1));
EOFCode = ClearCode + 1;
free_ent = (int16_t)(ClearCode + 2);
a_count=0;
ent = GifNextPixel( );
output( (code_int)ClearCode );
while ( ent != EOF ) {
c = GifNextPixel();
output ( (code_int) ent );
ent = c;
if ( free_ent < maxmaxcode ) {
free_ent++;
} else {
free_ent=(int16_t)(ClearCode+2);
clear_flg=1;
output((code_int)ClearCode);
}
}
// Put out the final code.
output( (code_int) EOFCode );
}
////////////////////////////////////////////////////////////////////////////////
/***************************************************************************
*
* GIFCOMPR.C - LZW GIF Image compression routines
*
***************************************************************************/
void CxImageGIF::compressLZW( int32_t init_bits, CxFile* outfile)
{
register int32_t fcode;
register int32_t c;
register int32_t ent;
register int32_t hshift;
register int32_t disp;
register int32_t i;
// g_init_bits - initial number of bits
// g_outfile - pointer to output file
g_init_bits = init_bits;
g_outfile = outfile;
// Set up the necessary values
cur_accum = cur_bits = clear_flg = 0;
maxcode = (int16_t)MAXCODE(n_bits = g_init_bits);
code_int maxmaxcode = (code_int)1 << MAXBITSCODES;
ClearCode = (1 << (init_bits - 1));
EOFCode = ClearCode + 1;
free_ent = (int16_t)(ClearCode + 2);
a_count=0;
ent = GifNextPixel( );
hshift = 0;
for ( fcode = (int32_t) HSIZE; fcode < 65536L; fcode *= 2L ) ++hshift;
hshift = 8 - hshift; /* set hash code range bound */
cl_hash((int32_t)HSIZE); /* clear hash table */
output( (code_int)ClearCode );
while ( (c = GifNextPixel( )) != EOF ) {
fcode = (int32_t) (((int32_t) c << MAXBITSCODES) + ent);
i = (((code_int)c << hshift) ^ ent); /* xor hashing */
if ( HashTabOf (i) == fcode ) {
ent = CodeTabOf (i);
continue;
} else if ( (int32_t)HashTabOf (i) < 0 ) /* empty slot */
goto nomatch;
disp = HSIZE - i; /* secondary hash (after G. Knott) */
if ( i == 0 ) disp = 1;
probe:
if ( (i -= disp) < 0 ) i += HSIZE;
if ( HashTabOf (i) == fcode ) { ent = CodeTabOf (i); continue; }
if ( (int32_t)HashTabOf (i) > 0 ) goto probe;
nomatch:
output ( (code_int) ent );
ent = c;
if ( free_ent < maxmaxcode ) {
CodeTabOf (i) = free_ent++; /* code -> hashtable */
HashTabOf (i) = fcode;
} else {
cl_hash((int32_t)HSIZE);
free_ent=(int16_t)(ClearCode+2);
clear_flg=1;
output((code_int)ClearCode);
}
}
// Put out the final code.
output( (code_int)ent );
output( (code_int) EOFCode );
}
////////////////////////////////////////////////////////////////////////////////
static const uint32_t code_mask[] = { 0x0000, 0x0001, 0x0003, 0x0007, 0x000F,
0x001F, 0x003F, 0x007F, 0x00FF,
0x01FF, 0x03FF, 0x07FF, 0x0FFF,
0x1FFF, 0x3FFF, 0x7FFF, 0xFFFF };
////////////////////////////////////////////////////////////////////////////////
void CxImageGIF::output( code_int code)
{
cur_accum &= code_mask[ cur_bits ];
if( cur_bits > 0 )
cur_accum |= ((int32_t)code << cur_bits);
else
cur_accum = code;
cur_bits += n_bits;
while( cur_bits >= 8 ) {
char_out( (uint32_t)(cur_accum & 0xff) );
cur_accum >>= 8;
cur_bits -= 8;
}
/*
* If the next entry is going to be too big for the code size,
* then increase it, if possible.
*/
if ( free_ent > maxcode || clear_flg ) {
if( clear_flg ) {
maxcode = (int16_t)MAXCODE(n_bits = g_init_bits);
clear_flg = 0;
} else {
++n_bits;
if ( n_bits == MAXBITSCODES )
maxcode = (code_int)1 << MAXBITSCODES; /* should NEVER generate this code */
else
maxcode = (int16_t)MAXCODE(n_bits);
}
}
if( code == EOFCode ) {
// At EOF, write the rest of the buffer.
while( cur_bits > 0 ) {
char_out( (uint32_t)(cur_accum & 0xff) );
cur_accum >>= 8;
cur_bits -= 8;
}
flush_char();
g_outfile->Flush();
if(g_outfile->Error()) strcpy(info.szLastError,"Write Error in GIF file");
}
}
////////////////////////////////////////////////////////////////////////////////
void CxImageGIF::cl_hash(int32_t hsize)
{
register int32_t *htab_p = htab+hsize;
register int32_t i;
register int32_t m1 = -1L;
i = hsize - 16;
do {
*(htab_p-16)=m1;
*(htab_p-15)=m1;
*(htab_p-14)=m1;
*(htab_p-13)=m1;
*(htab_p-12)=m1;
*(htab_p-11)=m1;
*(htab_p-10)=m1;
*(htab_p-9)=m1;
*(htab_p-8)=m1;
*(htab_p-7)=m1;
*(htab_p-6)=m1;
*(htab_p-5)=m1;
*(htab_p-4)=m1;
*(htab_p-3)=m1;
*(htab_p-2)=m1;
*(htab_p-1)=m1;
htab_p-=16;
} while ((i-=16) >=0);
for (i+=16;i>0;--i)
*--htab_p=m1;
}
/*******************************************************************************
* GIF specific
*******************************************************************************/
void CxImageGIF::char_out(int32_t c)
{
accum[a_count++]=(char)c;
if (a_count >=254)
flush_char();
}
void CxImageGIF::flush_char()
{
if (a_count > 0) {
g_outfile->PutC((uint8_t)a_count);
g_outfile->Write(accum,1,a_count);
a_count=0;
}
}
/*******************************************************************************
* GIF decoder
*******************************************************************************/
/* DECODE.C - An LZW decoder for GIF
* Copyright (C) 1987, by Steven A. Bennett
* Copyright (C) 1994, C++ version by Alejandro Aguilar Sierra
*
* Permission is given by the author to freely redistribute and include
* this code in any program as int32_t as this credit is given where due.
*
* In accordance with the above, I want to credit Steve Wilhite who wrote
* the code which this is heavily inspired by...
*
* GIF and 'Graphics Interchange Format' are trademarks (tm) of
* Compuserve, Incorporated, an H&R Block Company.
*
* Release Notes: This file contains a decoder routine for GIF images