]>
git.saurik.com Git - wxWidgets.git/blob - src/common/gifdecod.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxGIFDecoder, GIF reader for wxImage and wxAnimation
4 // Author: Guillermo Rodriguez Garcia <guille@iies.es>
7 // Copyright: (c) Guillermo Rodriguez Garcia
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
12 #pragma implementation "gifdecod.h"
15 // For compilers that support precompilation, includes "wx.h".
16 #include "wx/wxprec.h"
26 #if wxUSE_STREAMS && wxUSE_GIF
30 #include "wx/gifdecod.h"
33 //---------------------------------------------------------------------------
34 // wxGIFDecoder constructor and destructor
35 //---------------------------------------------------------------------------
37 wxGIFDecoder::wxGIFDecoder(wxInputStream
*s
, bool anim
)
53 wxGIFDecoder::~wxGIFDecoder()
58 void wxGIFDecoder::Destroy()
60 GIFImage
*pimg
, *paux
;
81 //---------------------------------------------------------------------------
82 // Convert this image to a wxImage object
83 //---------------------------------------------------------------------------
85 // This function was designed by Vaclav Slavik
87 bool wxGIFDecoder::ConvertToImage(wxImage
*image
) const
89 unsigned char *src
, *dst
, *pal
;
96 /* create the image */
97 image
->Create(GetWidth(), GetHeight());
104 dst
= image
->GetData();
105 transparent
= GetTransparentColour();
107 /* set transparent colour mask */
108 if (transparent
!= -1)
110 for (i
= 0; i
< 256; i
++)
112 if ((pal
[3 * i
+ 0] == 255) &&
113 (pal
[3 * i
+ 1] == 0) &&
114 (pal
[3 * i
+ 2] == 255))
116 pal
[3 * i
+ 2] = 254;
120 pal
[3 * transparent
+ 0] = 255,
121 pal
[3 * transparent
+ 1] = 0,
122 pal
[3 * transparent
+ 2] = 255;
124 image
->SetMaskColour(255, 0, 255);
127 image
->SetMask(FALSE
);
129 /* copy image data */
130 for (i
= 0; i
< (GetWidth() * GetHeight()); i
++, src
++)
132 *(dst
++) = pal
[3 * (*src
) + 0];
133 *(dst
++) = pal
[3 * (*src
) + 1];
134 *(dst
++) = pal
[3 * (*src
) + 2];
141 //---------------------------------------------------------------------------
143 //---------------------------------------------------------------------------
145 // Get data for current frame
147 int wxGIFDecoder::GetFrameIndex() const { return m_image
; }
148 unsigned char* wxGIFDecoder::GetData() const { return (m_pimage
->p
); }
149 unsigned char* wxGIFDecoder::GetPalette() const { return (m_pimage
->pal
); }
150 unsigned int wxGIFDecoder::GetWidth() const { return (m_pimage
->w
); }
151 unsigned int wxGIFDecoder::GetHeight() const { return (m_pimage
->h
); }
152 unsigned int wxGIFDecoder::GetTop() const { return (m_pimage
->top
); }
153 unsigned int wxGIFDecoder::GetLeft() const { return (m_pimage
->left
); }
154 int wxGIFDecoder::GetTransparentColour() const { return (m_pimage
->transparent
); }
155 int wxGIFDecoder::GetDisposalMethod() const { return (m_pimage
->disposal
); }
156 long wxGIFDecoder::GetDelay() const { return (m_pimage
->delay
); }
160 unsigned int wxGIFDecoder::GetLogicalScreenWidth() const { return m_screenw
; }
161 unsigned int wxGIFDecoder::GetLogicalScreenHeight() const { return m_screenh
; }
162 int wxGIFDecoder::GetBackgroundColour() const { return m_background
; }
163 int wxGIFDecoder::GetNumberOfFrames() const { return m_nimages
; }
164 bool wxGIFDecoder::IsAnimation() const { return (m_nimages
> 1); }
167 //---------------------------------------------------------------------------
168 // Functions to move through the animation
169 //---------------------------------------------------------------------------
171 bool wxGIFDecoder::GoFirstFrame()
181 bool wxGIFDecoder::GoLastFrame()
191 bool wxGIFDecoder::GoNextFrame(bool cyclic
)
196 if ((m_image
< m_nimages
) || (cyclic
))
198 m_pimage
= m_pimage
->next
;
213 bool wxGIFDecoder::GoPrevFrame(bool cyclic
)
218 if ((m_image
> 1) || (cyclic
))
220 m_pimage
= m_pimage
->prev
;
235 bool wxGIFDecoder::GoFrame(int which
)
242 if ((which
>= 1) && (which
<= m_nimages
))
246 for (i
= 1; i
< which
; i
++)
247 m_pimage
= m_pimage
->next
;
256 //---------------------------------------------------------------------------
257 // GIF reading and decoding
258 //---------------------------------------------------------------------------
261 // Reads the next code from the file stream, with size 'bits'
263 int wxGIFDecoder::getcode(int bits
, int ab_fin
)
265 unsigned int mask
; /* bit mask */
266 unsigned int code
; /* code (result) */
269 /* get remaining bits from last byte read */
270 mask
= (1 << bits
) - 1;
271 code
= (m_lastbyte
>> (8 - m_restbits
)) & mask
;
273 /* keep reading new bytes while needed */
274 while (bits
> m_restbits
)
276 /* if no bytes left in this block, read the next block */
279 m_restbyte
= (unsigned char)m_f
->GetC();
281 /* Some encoders are a bit broken: instead of issuing
282 * an end-of-image symbol (ab_fin) they come up with
283 * a zero-length subblock!! We catch this here so
284 * that the decoder sees an ab_fin code.
293 m_f
->Read((void *) m_buffer
, m_restbyte
);
297 /* read next byte and isolate the bits we need */
298 m_lastbyte
= (unsigned char) (*m_bufp
++);
299 mask
= (1 << (bits
- m_restbits
)) - 1;
300 code
= code
+ ((m_lastbyte
& mask
) << m_restbits
);
303 /* adjust total number of bits extracted from the buffer */
304 m_restbits
= m_restbits
+ 8;
307 /* find number of bits remaining for next code */
308 m_restbits
= (m_restbits
- bits
);
315 // GIF decoding function. The initial code size (aka root size)
316 // is 'bits'. Supports interlaced images (interl == 1).
318 int wxGIFDecoder::dgif(GIFImage
*img
, int interl
, int bits
)
320 int ab_prefix
[4096]; /* alphabet (prefixes) */
321 int ab_tail
[4096]; /* alphabet (tails) */
322 int stack
[4096]; /* decompression stack */
324 int ab_clr
; /* clear code */
325 int ab_fin
; /* end of info code */
326 int ab_bits
; /* actual symbol width, in bits */
327 int ab_free
; /* first free position in alphabet */
328 int ab_max
; /* last possible character in alphabet */
329 int pass
; /* pass number in interlaced images */
330 int pos
; /* index into decompresion stack */
331 unsigned int x
, y
; /* position in image buffer */
333 int code
, readcode
, lastcode
, abcabca
;
335 /* these won't change */
336 ab_clr
= (1 << bits
);
337 ab_fin
= (1 << bits
) + 1;
339 /* these will change through the decompression proccess */
341 ab_free
= (1 << bits
) + 2;
342 ab_max
= (1 << ab_bits
) - 1;
348 /* reset decoder vars */
356 readcode
= code
= getcode(ab_bits
, ab_fin
);
359 if (code
== ab_fin
) break;
361 /* reset alphabet? */
364 /* reset main variables */
366 ab_free
= (1 << bits
) + 2;
367 ab_max
= (1 << ab_bits
) - 1;
371 /* skip to next code */
375 /* unknown code: special case (like in ABCABCA) */
378 code
= lastcode
; /* take last string */
379 stack
[pos
++] = abcabca
; /* add first character */
382 /* build the string for this code in the stack */
383 while (code
> ab_clr
)
385 stack
[pos
++] = ab_tail
[code
];
386 code
= ab_prefix
[code
];
388 stack
[pos
] = code
; /* push last code into the stack */
389 abcabca
= code
; /* save for special case */
391 /* make new entry in alphabet (only if NOT just cleared) */
394 ab_prefix
[ab_free
] = lastcode
;
395 ab_tail
[ab_free
] = code
;
398 if ((ab_free
> ab_max
) && (ab_bits
< 12))
401 ab_max
= (1 << ab_bits
) - 1;
405 /* dump stack data to the buffer */
408 (img
->p
)[x
+ (y
* (img
->w
))] = (char)stack
[pos
--];
416 /* support for interlaced images */
419 case 1: y
+= 8; break;
420 case 2: y
+= 8; break;
421 case 3: y
+= 4; break;
422 case 4: y
+= 2; break;
428 case 2: y
= 4; break;
429 case 3: y
= 2; break;
430 case 4: y
= 1; break;
445 while (code
!= ab_fin
);
452 // Returns TRUE if the file looks like a valid GIF, FALSE otherwise.
454 bool wxGIFDecoder::CanRead()
456 unsigned char buf
[3];
459 m_f
->SeekI(-3, wxFromCurrent
);
461 return (memcmp(buf
, "GIF", 3) == 0);
466 // Reads and decodes one or more GIF images, depending on whether
467 // animated GIF support is enabled. Can read GIFs with any bit
468 // size (color depth), but the output images are always expanded
469 // to 8 bits per pixel. Also, the image palettes always contain
470 // 256 colors, although some of them may be unused. Returns wxGIF_OK
471 // (== 0) on success, or an error code if something fails (see
472 // header file for details)
474 int wxGIFDecoder::ReadGIF()
476 int ncolors
, bits
, interl
, transparent
, disposal
, i
;
480 unsigned char pal
[768];
481 unsigned char buf
[16];
482 GIFImage
**ppimg
, *pimg
, *pprev
;
484 /* check GIF signature */
486 return wxGIF_INVFORMAT
;
488 /* check for animated GIF support (ver. >= 89a) */
491 if (memcmp(buf
+ 3, "89a", 3) < 0)
494 /* read logical screen descriptor block (LSDB) */
496 m_screenw
= buf
[0] + 256 * buf
[1];
497 m_screenh
= buf
[2] + 256 * buf
[3];
499 /* load global color map if available */
500 if ((buf
[4] & 0x80) == 0x80)
502 m_background
= buf
[5];
504 ncolors
= 2 << (buf
[4] & 0x07);
505 m_f
->Read(pal
, 3 * ncolors
);
508 /* transparent colour, disposal method and delay default to unused */
522 type
= (unsigned char)m_f
->GetC();
530 /* extension block? */
533 if (((unsigned char)m_f
->GetC()) == 0xF9)
534 /* graphics control extension, parse it */
538 /* read delay and convert from 1/100 of a second to ms */
539 delay
= 10 * (buf
[2] + 256 * buf
[3]);
541 /* read transparent colour index, if used */
543 transparent
= buf
[4];
545 /* read disposal method */
546 disposal
= (buf
[1] & 0x1C) - 1;
549 /* other extension, skip */
551 while ((i
= (unsigned char)m_f
->GetC()) != 0)
553 m_f
->SeekI(i
, wxFromCurrent
);
558 /* image descriptor block? */
561 /* allocate memory for IMAGEN struct */
562 pimg
= (*ppimg
) = new GIFImage();
570 /* fill in the data */
572 pimg
->left
= buf
[4] + 256 * buf
[5];
573 pimg
->top
= buf
[4] + 256 * buf
[5];
574 pimg
->w
= buf
[4] + 256 * buf
[5];
575 pimg
->h
= buf
[6] + 256 * buf
[7];
576 interl
= ((buf
[8] & 0x40)? 1 : 0);
577 size
= pimg
->w
* pimg
->h
;
579 pimg
->transparent
= transparent
;
580 pimg
->disposal
= disposal
;
587 /* allocate memory for image and palette */
588 pimg
->p
= (unsigned char *) malloc((size_t)size
);
589 pimg
->pal
= (unsigned char *) malloc(768);
591 if ((!pimg
->p
) || (!pimg
->pal
))
597 /* load local color map if available, else use global map */
598 if ((buf
[8] & 0x80) == 0x80)
600 ncolors
= 2 << (buf
[8] & 0x07);
601 m_f
->Read(pimg
->pal
, 3 * ncolors
);
604 memcpy(pimg
->pal
, pal
, 768);
606 /* get initial code size from first byte in raster data */
607 bits
= (unsigned char)m_f
->GetC();
610 dgif(pimg
, interl
, bits
);
613 /* if this is not an animated GIF, exit after first image */
619 /* setup image pointers */
627 /* try to read to the end of the stream */
630 type
= (unsigned char)m_f
->GetC();
638 while ((i
= (unsigned char)m_f
->GetC()) != 0)
640 m_f
->SeekI(i
, wxFromCurrent
);
643 else if (type
== 0x2C)
645 /* image descriptor block */
648 /* local color map */
649 if ((buf
[8] & 0x80) == 0x80)
651 ncolors
= 2 << (buf
[8] & 0x07);
652 m_f
->SeekI(3 * ncolors
, wxFromCurrent
);
655 /* initial code size */
659 while ((i
= (unsigned char)m_f
->GetC()) != 0)
661 m_f
->SeekI(i
, wxFromCurrent
);
664 else if ((type
!= 0x3B) && (type
!= 00)) /* testing */
666 /* images are OK, but couldn't read to the end of the stream */
667 return wxGIF_TRUNCATED
;
674 #endif // wxUSE_STREAMS && wxUSE_GIF