]> git.saurik.com Git - wxWidgets.git/blob - src/common/gifdecod.cpp
Implement support for pen cap and join in wxPostScriptDC.
[wxWidgets.git] / src / common / gifdecod.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/gifdecod.cpp
3 // Purpose: wxGIFDecoder, GIF reader for wxImage and wxAnimation
4 // Author: Guillermo Rodriguez Garcia <guille@iies.es>
5 // Version: 3.04
6 // RCS-ID: $Id$
7 // Copyright: (c) Guillermo Rodriguez Garcia
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 // For compilers that support precompilation, includes "wx.h".
12 #include "wx/wxprec.h"
13
14 #ifdef __BORLANDC__
15 #pragma hdrstop
16 #endif
17
18 #if wxUSE_STREAMS && wxUSE_GIF
19
20 #ifndef WX_PRECOMP
21 #include "wx/palette.h"
22 #include "wx/intl.h"
23 #include "wx/log.h"
24 #endif
25
26 #include <stdlib.h>
27 #include <string.h>
28 #include "wx/gifdecod.h"
29 #include "wx/scopedptr.h"
30 #include "wx/scopeguard.h"
31
32 enum
33 {
34 GIF_MARKER_EXT = '!', // 0x21
35 GIF_MARKER_SEP = ',', // 0x2C
36 GIF_MARKER_ENDOFDATA = ';', // 0x3B
37
38 GIF_MARKER_EXT_GRAPHICS_CONTROL = 0xF9,
39 GIF_MARKER_EXT_COMMENT = 0xFE,
40 GIF_MARKER_EXT_APP = 0xFF
41 };
42
43 #define GetFrame(n) ((GIFImage*)m_frames[n])
44
45 //---------------------------------------------------------------------------
46 // GIFImage
47 //---------------------------------------------------------------------------
48
49 // internal class for storing GIF image data
50 class GIFImage
51 {
52 public:
53 // def ctor
54 GIFImage();
55
56 unsigned int w; // width
57 unsigned int h; // height
58 unsigned int left; // x coord (in logical screen)
59 unsigned int top; // y coord (in logical screen)
60 int transparent; // transparent color index (-1 = none)
61 wxAnimationDisposal disposal; // disposal method
62 long delay; // delay in ms (-1 = unused)
63 unsigned char *p; // bitmap
64 unsigned char *pal; // palette
65 unsigned int ncolours; // number of colours
66 wxString comment;
67
68 wxDECLARE_NO_COPY_CLASS(GIFImage);
69 };
70
71 wxDECLARE_SCOPED_PTR(GIFImage, GIFImagePtr)
72 wxDEFINE_SCOPED_PTR(GIFImage, GIFImagePtr)
73
74
75 //---------------------------------------------------------------------------
76 // GIFImage constructor
77 //---------------------------------------------------------------------------
78 GIFImage::GIFImage()
79 {
80 w = 0;
81 h = 0;
82 left = 0;
83 top = 0;
84 transparent = 0;
85 disposal = wxANIM_DONOTREMOVE;
86 delay = -1;
87 p = (unsigned char *) NULL;
88 pal = (unsigned char *) NULL;
89 ncolours = 0;
90 }
91
92 //---------------------------------------------------------------------------
93 // wxGIFDecoder constructor and destructor
94 //---------------------------------------------------------------------------
95
96 wxGIFDecoder::wxGIFDecoder()
97 {
98 }
99
100 wxGIFDecoder::~wxGIFDecoder()
101 {
102 Destroy();
103 }
104
105 void wxGIFDecoder::Destroy()
106 {
107 wxASSERT(m_nFrames==m_frames.GetCount());
108 for (unsigned int i=0; i<m_nFrames; i++)
109 {
110 GIFImage *f = (GIFImage*)m_frames[i];
111 free(f->p);
112 free(f->pal);
113 delete f;
114 }
115
116 m_frames.Clear();
117 m_nFrames = 0;
118 }
119
120
121 //---------------------------------------------------------------------------
122 // Convert this image to a wxImage object
123 //---------------------------------------------------------------------------
124
125 // This function was designed by Vaclav Slavik
126
127 bool wxGIFDecoder::ConvertToImage(unsigned int frame, wxImage *image) const
128 {
129 unsigned char *src, *dst, *pal;
130 unsigned long i;
131 int transparent;
132
133 // just in case...
134 image->Destroy();
135
136 // create the image
137 wxSize sz = GetFrameSize(frame);
138 image->Create(sz.GetWidth(), sz.GetHeight());
139 image->SetType(wxBITMAP_TYPE_GIF);
140
141 if (!image->IsOk())
142 return false;
143
144 pal = GetPalette(frame);
145 src = GetData(frame);
146 dst = image->GetData();
147 transparent = GetTransparentColourIndex(frame);
148
149 // set transparent colour mask
150 if (transparent != -1)
151 {
152 for (i = 0; i < GetNcolours(frame); i++)
153 {
154 if ((pal[3 * i + 0] == 255) &&
155 (pal[3 * i + 1] == 0) &&
156 (pal[3 * i + 2] == 255))
157 {
158 pal[3 * i + 2] = 254;
159 }
160 }
161
162 pal[3 * transparent + 0] = 255,
163 pal[3 * transparent + 1] = 0,
164 pal[3 * transparent + 2] = 255;
165
166 image->SetMaskColour(255, 0, 255);
167 }
168 else
169 image->SetMask(false);
170
171 #if wxUSE_PALETTE
172 unsigned char r[256];
173 unsigned char g[256];
174 unsigned char b[256];
175
176 for (i = 0; i < 256; i++)
177 {
178 r[i] = pal[3*i + 0];
179 g[i] = pal[3*i + 1];
180 b[i] = pal[3*i + 2];
181 }
182
183 image->SetPalette(wxPalette(GetNcolours(frame), r, g, b));
184 #endif // wxUSE_PALETTE
185
186 // copy image data
187 unsigned long npixel = sz.GetWidth() * sz.GetHeight();
188 for (i = 0; i < npixel; i++, src++)
189 {
190 *(dst++) = pal[3 * (*src) + 0];
191 *(dst++) = pal[3 * (*src) + 1];
192 *(dst++) = pal[3 * (*src) + 2];
193 }
194
195 wxString comment = GetFrame(frame)->comment;
196 if ( !comment.empty() )
197 {
198 image->SetOption(wxIMAGE_OPTION_GIF_COMMENT, comment);
199 }
200
201 return true;
202 }
203
204
205 //---------------------------------------------------------------------------
206 // Data accessors
207 //---------------------------------------------------------------------------
208
209 // Get data for current frame
210
211 wxSize wxGIFDecoder::GetFrameSize(unsigned int frame) const
212 {
213 return wxSize(GetFrame(frame)->w, GetFrame(frame)->h);
214 }
215
216 wxPoint wxGIFDecoder::GetFramePosition(unsigned int frame) const
217 {
218 return wxPoint(GetFrame(frame)->left, GetFrame(frame)->top);
219 }
220
221 wxAnimationDisposal wxGIFDecoder::GetDisposalMethod(unsigned int frame) const
222 {
223 return GetFrame(frame)->disposal;
224 }
225
226 long wxGIFDecoder::GetDelay(unsigned int frame) const
227 {
228 return GetFrame(frame)->delay;
229 }
230
231 wxColour wxGIFDecoder::GetTransparentColour(unsigned int frame) const
232 {
233 unsigned char *pal = GetFrame(frame)->pal;
234 int n = GetFrame(frame)->transparent;
235 if (n == -1)
236 return wxNullColour;
237
238 return wxColour(pal[n*3 + 0],
239 pal[n*3 + 1],
240 pal[n*3 + 2]);
241 }
242
243 unsigned char* wxGIFDecoder::GetData(unsigned int frame) const { return (GetFrame(frame)->p); }
244 unsigned char* wxGIFDecoder::GetPalette(unsigned int frame) const { return (GetFrame(frame)->pal); }
245 unsigned int wxGIFDecoder::GetNcolours(unsigned int frame) const { return (GetFrame(frame)->ncolours); }
246 int wxGIFDecoder::GetTransparentColourIndex(unsigned int frame) const { return (GetFrame(frame)->transparent); }
247
248
249
250 //---------------------------------------------------------------------------
251 // GIF reading and decoding
252 //---------------------------------------------------------------------------
253
254 // getcode:
255 // Reads the next code from the file stream, with size 'bits'
256 //
257 int wxGIFDecoder::getcode(wxInputStream& stream, int bits, int ab_fin)
258 {
259 unsigned int mask; // bit mask
260 unsigned int code; // code (result)
261
262 // get remaining bits from last byte read
263 mask = (1 << bits) - 1;
264 code = (m_lastbyte >> (8 - m_restbits)) & mask;
265
266 // keep reading new bytes while needed
267 while (bits > m_restbits)
268 {
269 // if no bytes left in this block, read the next block
270 if (m_restbyte == 0)
271 {
272 m_restbyte = stream.GetC();
273
274 /* Some encoders are a bit broken: instead of issuing
275 * an end-of-image symbol (ab_fin) they come up with
276 * a zero-length subblock!! We catch this here so
277 * that the decoder sees an ab_fin code.
278 */
279 if (m_restbyte == 0)
280 {
281 code = ab_fin;
282 break;
283 }
284
285 // prefetch data
286 stream.Read((void *) m_buffer, m_restbyte);
287 if (stream.LastRead() != m_restbyte)
288 {
289 code = ab_fin;
290 return code;
291 }
292 m_bufp = m_buffer;
293 }
294
295 // read next byte and isolate the bits we need
296 m_lastbyte = (unsigned char) (*m_bufp++);
297 mask = (1 << (bits - m_restbits)) - 1;
298 code = code + ((m_lastbyte & mask) << m_restbits);
299 m_restbyte--;
300
301 // adjust total number of bits extracted from the buffer
302 m_restbits = m_restbits + 8;
303 }
304
305 // find number of bits remaining for next code
306 m_restbits = (m_restbits - bits);
307
308 return code;
309 }
310
311
312 // dgif:
313 // GIF decoding function. The initial code size (aka root size)
314 // is 'bits'. Supports interlaced images (interl == 1).
315 // Returns wxGIF_OK (== 0) on success, or an error code if something
316 // fails (see header file for details)
317 wxGIFErrorCode
318 wxGIFDecoder::dgif(wxInputStream& stream, GIFImage *img, int interl, int bits)
319 {
320 static const int allocSize = 4096 + 1;
321 int *ab_prefix = new int[allocSize]; // alphabet (prefixes)
322 if (ab_prefix == NULL)
323 {
324 return wxGIF_MEMERR;
325 }
326
327 int *ab_tail = new int[allocSize]; // alphabet (tails)
328 if (ab_tail == NULL)
329 {
330 delete[] ab_prefix;
331 return wxGIF_MEMERR;
332 }
333
334 int *stack = new int[allocSize]; // decompression stack
335 if (stack == NULL)
336 {
337 delete[] ab_prefix;
338 delete[] ab_tail;
339 return wxGIF_MEMERR;
340 }
341
342 int ab_clr; // clear code
343 int ab_fin; // end of info code
344 int ab_bits; // actual symbol width, in bits
345 int ab_free; // first free position in alphabet
346 int ab_max; // last possible character in alphabet
347 int pass; // pass number in interlaced images
348 int pos; // index into decompresion stack
349 unsigned int x, y; // position in image buffer
350
351 int code, readcode, lastcode, abcabca;
352
353 // these won't change
354 ab_clr = (1 << bits);
355 ab_fin = (1 << bits) + 1;
356
357 // these will change through the decompression process
358 ab_bits = bits + 1;
359 ab_free = (1 << bits) + 2;
360 ab_max = (1 << ab_bits) - 1;
361 lastcode = -1;
362 abcabca = -1;
363 pass = 1;
364 pos = x = y = 0;
365
366 // reset decoder vars
367 m_restbits = 0;
368 m_restbyte = 0;
369 m_lastbyte = 0;
370
371 do
372 {
373 // get next code
374 readcode = code = getcode(stream, ab_bits, ab_fin);
375
376 // end of image?
377 if (code == ab_fin) break;
378
379 // reset alphabet?
380 if (code == ab_clr)
381 {
382 // reset main variables
383 ab_bits = bits + 1;
384 ab_free = (1 << bits) + 2;
385 ab_max = (1 << ab_bits) - 1;
386 lastcode = -1;
387 abcabca = -1;
388
389 // skip to next code
390 continue;
391 }
392
393 // unknown code: special case (like in ABCABCA)
394 if (code >= ab_free)
395 {
396 code = lastcode; // take last string
397 stack[pos++] = abcabca; // add first character
398 }
399
400 // build the string for this code in the stack
401 while (code > ab_clr)
402 {
403 stack[pos++] = ab_tail[code];
404 code = ab_prefix[code];
405
406 // Don't overflow. This shouldn't happen with normal
407 // GIF files, the allocSize of 4096+1 is enough. This
408 // will only happen with badly formed GIFs.
409 if (pos >= allocSize)
410 {
411 delete[] ab_prefix;
412 delete[] ab_tail;
413 delete[] stack;
414 return wxGIF_INVFORMAT;
415 }
416 }
417
418 if (pos >= allocSize)
419 {
420 delete[] ab_prefix;
421 delete[] ab_tail;
422 delete[] stack;
423 return wxGIF_INVFORMAT;
424 }
425
426 stack[pos] = code; // push last code into the stack
427 abcabca = code; // save for special case
428
429 // make new entry in alphabet (only if NOT just cleared)
430 if (lastcode != -1)
431 {
432 // Normally, after the alphabet is full and can't grow any
433 // further (ab_free == 4096), encoder should (must?) emit CLEAR
434 // to reset it. This checks whether we really got it, otherwise
435 // the GIF is damaged.
436 if (ab_free > ab_max)
437 {
438 delete[] ab_prefix;
439 delete[] ab_tail;
440 delete[] stack;
441 return wxGIF_INVFORMAT;
442 }
443
444 // This assert seems unnecessary since the condition above
445 // eliminates the only case in which it went false. But I really
446 // don't like being forced to ask "Who in .text could have
447 // written there?!" And I wouldn't have been forced to ask if
448 // this line had already been here.
449 wxASSERT(ab_free < allocSize);
450
451 ab_prefix[ab_free] = lastcode;
452 ab_tail[ab_free] = code;
453 ab_free++;
454
455 if ((ab_free > ab_max) && (ab_bits < 12))
456 {
457 ab_bits++;
458 ab_max = (1 << ab_bits) - 1;
459 }
460 }
461
462 // dump stack data to the image buffer
463 while (pos >= 0)
464 {
465 (img->p)[x + (y * (img->w))] = (char) stack[pos];
466 pos--;
467
468 if (++x >= (img->w))
469 {
470 x = 0;
471
472 if (interl)
473 {
474 // support for interlaced images
475 switch (pass)
476 {
477 case 1: y += 8; break;
478 case 2: y += 8; break;
479 case 3: y += 4; break;
480 case 4: y += 2; break;
481 }
482
483 /* loop until a valid y coordinate has been
484 found, Or if the maximum number of passes has
485 been reached, exit the loop, and stop image
486 decoding (At this point the image is successfully
487 decoded).
488 If we don't loop, but merely set y to some other
489 value, that new value might still be invalid depending
490 on the height of the image. This would cause out of
491 bounds writing.
492 */
493 while (y >= (img->h))
494 {
495 switch (++pass)
496 {
497 case 2: y = 4; break;
498 case 3: y = 2; break;
499 case 4: y = 1; break;
500
501 default:
502 /*
503 It's possible we arrive here. For example this
504 happens when the image is interlaced, and the
505 height is 1. Looking at the above cases, the
506 lowest possible y is 1. While the only valid
507 one would be 0 for an image of height 1. So
508 'eventually' the loop will arrive here.
509 This case makes sure this while loop is
510 exited, as well as the 2 other ones.
511 */
512
513 // Set y to a valid coordinate so the local
514 // while loop will be exited. (y = 0 always
515 // is >= img->h since if img->h == 0 the
516 // image is never decoded)
517 y = 0;
518
519 // This will exit the other outer while loop
520 pos = -1;
521
522 // This will halt image decoding.
523 code = ab_fin;
524
525 break;
526 }
527 }
528 }
529 else
530 {
531 // non-interlaced
532 y++;
533 /*
534 Normally image decoding is finished when an End of Information code is
535 encountered (code == ab_fin) however some broken encoders write wrong
536 "block byte counts" (The first byte value after the "code size" byte),
537 being one value too high. It might very well be possible other variants
538 of this problem occur as well. The only sensible solution seems to
539 be to check for clipping.
540 Example of wrong encoding:
541 (1 * 1 B/W image, raster data stream follows in hex bytes)
542
543 02 << B/W images have a code size of 2
544 02 << Block byte count
545 44 << LZW packed
546 00 << Zero byte count (terminates data stream)
547
548 Because the block byte count is 2, the zero byte count is used in the
549 decoding process, and decoding is continued after this byte. (While it
550 should signal an end of image)
551
552 It should be:
553 02
554 02
555 44
556 01 << When decoded this correctly includes the End of Information code
557 00
558
559 Or (Worse solution):
560 02
561 01
562 44
563 00
564 (The 44 doesn't include an End of Information code, but at least the
565 decoder correctly skips to 00 now after decoding, and signals this
566 as an End of Information itself)
567 */
568 if (y >= img->h)
569 {
570 code = ab_fin;
571 break;
572 }
573 }
574 }
575 }
576
577 pos = 0;
578 lastcode = readcode;
579 }
580 while (code != ab_fin);
581
582 delete [] ab_prefix ;
583 delete [] ab_tail ;
584 delete [] stack ;
585
586 return wxGIF_OK;
587 }
588
589
590 // CanRead:
591 // Returns true if the file looks like a valid GIF, false otherwise.
592 //
593 bool wxGIFDecoder::DoCanRead(wxInputStream &stream) const
594 {
595 unsigned char buf[3];
596
597 if ( !stream.Read(buf, WXSIZEOF(buf)) )
598 return false;
599
600 return memcmp(buf, "GIF", WXSIZEOF(buf)) == 0;
601 }
602
603
604 // LoadGIF:
605 // Reads and decodes one or more GIF images, depending on whether
606 // animated GIF support is enabled. Can read GIFs with any bit
607 // size (color depth), but the output images are always expanded
608 // to 8 bits per pixel. Also, the image palettes always contain
609 // 256 colors, although some of them may be unused. Returns wxGIF_OK
610 // (== 0) on success, or an error code if something fails (see
611 // header file for details)
612 //
613 wxGIFErrorCode wxGIFDecoder::LoadGIF(wxInputStream& stream)
614 {
615 unsigned int global_ncolors = 0;
616 int bits, interl, i;
617 wxAnimationDisposal disposal;
618 long size;
619 long delay;
620 unsigned char type = 0;
621 unsigned char pal[768];
622 unsigned char buf[16];
623 bool anim = true;
624
625 // check GIF signature
626 if (!CanRead(stream))
627 return wxGIF_INVFORMAT;
628
629 // check for animated GIF support (ver. >= 89a)
630
631 static const unsigned int headerSize = (3 + 3);
632 stream.Read(buf, headerSize);
633 if (stream.LastRead() != headerSize)
634 {
635 return wxGIF_INVFORMAT;
636 }
637
638 if (memcmp(buf + 3, "89a", 3) < 0)
639 {
640 anim = false;
641 }
642
643 // read logical screen descriptor block (LSDB)
644 static const unsigned int lsdbSize = (2 + 2 + 1 + 1 + 1);
645 stream.Read(buf, lsdbSize);
646 if (stream.LastRead() != lsdbSize)
647 {
648 return wxGIF_INVFORMAT;
649 }
650
651 m_szAnimation.SetWidth( buf[0] + 256 * buf[1] );
652 m_szAnimation.SetHeight( buf[2] + 256 * buf[3] );
653
654 if (anim && ((m_szAnimation.GetWidth() == 0) || (m_szAnimation.GetHeight() == 0)))
655 {
656 return wxGIF_INVFORMAT;
657 }
658
659 // load global color map if available
660 if ((buf[4] & 0x80) == 0x80)
661 {
662 int backgroundColIndex = buf[5];
663
664 global_ncolors = 2 << (buf[4] & 0x07);
665 unsigned int numBytes = 3 * global_ncolors;
666 stream.Read(pal, numBytes);
667 if (stream.LastRead() != numBytes)
668 {
669 return wxGIF_INVFORMAT;
670 }
671
672 m_background.Set(pal[backgroundColIndex*3 + 0],
673 pal[backgroundColIndex*3 + 1],
674 pal[backgroundColIndex*3 + 2]);
675 }
676
677 // transparent colour, disposal method and delay default to unused
678 int transparent = -1;
679 disposal = wxANIM_UNSPECIFIED;
680 delay = -1;
681 wxString comment;
682
683 bool done = false;
684 while (!done)
685 {
686 type = stream.GetC();
687
688 /*
689 If the end of file has been reached (or an error) and a ";"
690 (GIF_MARKER_ENDOFDATA) hasn't been encountered yet, exit the loop. (Without this
691 check the while loop would loop endlessly.) Later on, in the next while
692 loop, the file will be treated as being truncated (But still
693 be decoded as far as possible). returning wxGIF_TRUNCATED is not
694 possible here since some init code is done after this loop.
695 */
696 if (stream.Eof())// || !stream.IsOk())
697 {
698 /*
699 type is set to some bogus value, so there's no
700 need to continue evaluating it.
701 */
702 break; // Alternative : "return wxGIF_INVFORMAT;"
703 }
704
705 switch (type)
706 {
707 case GIF_MARKER_ENDOFDATA:
708 done = true;
709 break;
710 case GIF_MARKER_EXT:
711 switch (stream.GetC())
712 {
713 case GIF_MARKER_EXT_GRAPHICS_CONTROL:
714 {
715 // graphics control extension, parse it
716
717 static const unsigned int gceSize = 6;
718 stream.Read(buf, gceSize);
719 if (stream.LastRead() != gceSize)
720 {
721 Destroy();
722 return wxGIF_INVFORMAT;
723 }
724
725 // read delay and convert from 1/100 of a second to ms
726 delay = 10 * (buf[2] + 256 * buf[3]);
727
728 // read transparent colour index, if used
729 transparent = buf[1] & 0x01 ? buf[4] : -1;
730
731 // read disposal method
732 disposal = (wxAnimationDisposal)(((buf[1] & 0x1C) >> 2) - 1);
733 break;
734 }
735 case GIF_MARKER_EXT_COMMENT:
736 {
737 int len = stream.GetC();
738 while (len)
739 {
740 if ( stream.Eof() )
741 {
742 done = true;
743 break;
744 }
745
746 wxCharBuffer charbuf(len);
747 stream.Read(charbuf.data(), len);
748 if ( (int) stream.LastRead() != len )
749 {
750 done = true;
751 break;
752 }
753
754 comment += wxConvertMB2WX(charbuf.data());
755
756 len = stream.GetC();
757 }
758
759 break;
760 }
761 default:
762 // other extension, skip
763 while ((i = stream.GetC()) != 0)
764 {
765 if (stream.Eof() || (stream.LastRead() == 0) ||
766 stream.SeekI(i, wxFromCurrent) == wxInvalidOffset)
767 {
768 done = true;
769 break;
770 }
771 }
772 break;
773 }
774 break;
775 case GIF_MARKER_SEP:
776 {
777 // allocate memory for IMAGEN struct
778 GIFImagePtr pimg(new GIFImage());
779
780 wxScopeGuard guardDestroy = wxMakeObjGuard(*this, &wxGIFDecoder::Destroy);
781
782 if ( !pimg.get() )
783 return wxGIF_MEMERR;
784
785 // fill in the data
786 static const unsigned int idbSize = (2 + 2 + 2 + 2 + 1);
787 stream.Read(buf, idbSize);
788 if (stream.LastRead() != idbSize)
789 return wxGIF_INVFORMAT;
790
791 pimg->comment = comment;
792 comment.clear();
793 pimg->left = buf[0] + 256 * buf[1];
794 pimg->top = buf[2] + 256 * buf[3];
795 /*
796 pimg->left = buf[4] + 256 * buf[5];
797 pimg->top = buf[4] + 256 * buf[5];
798 */
799 pimg->w = buf[4] + 256 * buf[5];
800 pimg->h = buf[6] + 256 * buf[7];
801
802 if ( anim )
803 {
804 // some GIF images specify incorrect animation size but we can
805 // still open them if we fix up the animation size, see #9465
806 if ( m_nFrames == 0 )
807 {
808 if ( pimg->w > (unsigned)m_szAnimation.x )
809 m_szAnimation.x = pimg->w;
810 if ( pimg->h > (unsigned)m_szAnimation.y )
811 m_szAnimation.y = pimg->h;
812 }
813 else // subsequent frames
814 {
815 // check that we have valid size
816 if ( (!pimg->w || pimg->w > (unsigned)m_szAnimation.x) ||
817 (!pimg->h || pimg->h > (unsigned)m_szAnimation.y) )
818 {
819 wxLogError(_("Incorrect GIF frame size (%u, %d) for "
820 "the frame #%u"),
821 pimg->w, pimg->h, m_nFrames);
822 return wxGIF_INVFORMAT;
823 }
824 }
825 }
826
827 interl = ((buf[8] & 0x40)? 1 : 0);
828 size = pimg->w * pimg->h;
829
830 pimg->transparent = transparent;
831 pimg->disposal = disposal;
832 pimg->delay = delay;
833
834 // allocate memory for image and palette
835 pimg->p = (unsigned char *) malloc((unsigned int)size);
836 pimg->pal = (unsigned char *) malloc(768);
837
838 if ((!pimg->p) || (!pimg->pal))
839 return wxGIF_MEMERR;
840
841 // load local color map if available, else use global map
842 if ((buf[8] & 0x80) == 0x80)
843 {
844 unsigned int local_ncolors = 2 << (buf[8] & 0x07);
845 unsigned int numBytes = 3 * local_ncolors;
846 stream.Read(pimg->pal, numBytes);
847 pimg->ncolours = local_ncolors;
848 if (stream.LastRead() != numBytes)
849 return wxGIF_INVFORMAT;
850 }
851 else
852 {
853 memcpy(pimg->pal, pal, 768);
854 pimg->ncolours = global_ncolors;
855 }
856
857 // get initial code size from first byte in raster data
858 bits = stream.GetC();
859 if (bits == 0)
860 return wxGIF_INVFORMAT;
861
862 // decode image
863 wxGIFErrorCode result = dgif(stream, pimg.get(), interl, bits);
864 if (result != wxGIF_OK)
865 return result;
866
867 guardDestroy.Dismiss();
868
869 // add the image to our frame array
870 m_frames.Add(pimg.release());
871 m_nFrames++;
872
873 // if this is not an animated GIF, exit after first image
874 if (!anim)
875 done = true;
876 break;
877 }
878 }
879 }
880
881 if (m_nFrames <= 0)
882 {
883 Destroy();
884 return wxGIF_INVFORMAT;
885 }
886
887 // try to read to the end of the stream
888 while (type != GIF_MARKER_ENDOFDATA)
889 {
890 if (!stream.IsOk())
891 return wxGIF_TRUNCATED;
892
893 type = stream.GetC();
894
895 switch (type)
896 {
897 case GIF_MARKER_EXT:
898 // extension type
899 (void) stream.GetC();
900
901 // skip all data
902 while ((i = stream.GetC()) != 0)
903 {
904 if (stream.Eof() || (stream.LastRead() == 0) ||
905 stream.SeekI(i, wxFromCurrent) == wxInvalidOffset)
906 {
907 Destroy();
908 return wxGIF_INVFORMAT;
909 }
910 }
911 break;
912 case GIF_MARKER_SEP:
913 {
914 // image descriptor block
915 static const unsigned int idbSize = (2 + 2 + 2 + 2 + 1);
916 stream.Read(buf, idbSize);
917 if (stream.LastRead() != idbSize)
918 {
919 Destroy();
920 return wxGIF_INVFORMAT;
921 }
922
923 // local color map
924 if ((buf[8] & 0x80) == 0x80)
925 {
926 unsigned int local_ncolors = 2 << (buf[8] & 0x07);
927 wxFileOffset numBytes = 3 * local_ncolors;
928 if (stream.SeekI(numBytes, wxFromCurrent) == wxInvalidOffset)
929 {
930 Destroy();
931 return wxGIF_INVFORMAT;
932 }
933 }
934
935 // initial code size
936 (void) stream.GetC();
937 if (stream.Eof() || (stream.LastRead() == 0))
938 {
939 Destroy();
940 return wxGIF_INVFORMAT;
941 }
942
943 // skip all data
944 while ((i = stream.GetC()) != 0)
945 {
946 if (stream.Eof() || (stream.LastRead() == 0) ||
947 stream.SeekI(i, wxFromCurrent) == wxInvalidOffset)
948 {
949 Destroy();
950 return wxGIF_INVFORMAT;
951 }
952 }
953 break;
954 }
955 default:
956 if ((type != GIF_MARKER_ENDOFDATA) && (type != 00)) // testing
957 {
958 // images are OK, but couldn't read to the end of the stream
959 return wxGIF_TRUNCATED;
960 }
961 break;
962 }
963 }
964
965 return wxGIF_OK;
966 }
967
968 #endif // wxUSE_STREAMS && wxUSE_GIF