]>
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
);
131 unsigned char* r
= new unsigned char[256];
132 unsigned char* g
= new unsigned char[256];
133 unsigned char* b
= new unsigned char[256];
134 for (i
= 0; i
< 256; i
++)
140 image
->SetPalette(wxPalette(256, r
, g
, b
));
141 delete[] r
; delete[] g
; delete[] b
;
144 /* copy image data */
145 for (i
= 0; i
< (GetWidth() * GetHeight()); i
++, src
++)
147 *(dst
++) = pal
[3 * (*src
) + 0];
148 *(dst
++) = pal
[3 * (*src
) + 1];
149 *(dst
++) = pal
[3 * (*src
) + 2];
156 //---------------------------------------------------------------------------
158 //---------------------------------------------------------------------------
160 // Get data for current frame
162 int wxGIFDecoder::GetFrameIndex() const { return m_image
; }
163 unsigned char* wxGIFDecoder::GetData() const { return (m_pimage
->p
); }
164 unsigned char* wxGIFDecoder::GetPalette() const { return (m_pimage
->pal
); }
165 unsigned int wxGIFDecoder::GetWidth() const { return (m_pimage
->w
); }
166 unsigned int wxGIFDecoder::GetHeight() const { return (m_pimage
->h
); }
167 unsigned int wxGIFDecoder::GetTop() const { return (m_pimage
->top
); }
168 unsigned int wxGIFDecoder::GetLeft() const { return (m_pimage
->left
); }
169 int wxGIFDecoder::GetTransparentColour() const { return (m_pimage
->transparent
); }
170 int wxGIFDecoder::GetDisposalMethod() const { return (m_pimage
->disposal
); }
171 long wxGIFDecoder::GetDelay() const { return (m_pimage
->delay
); }
175 unsigned int wxGIFDecoder::GetLogicalScreenWidth() const { return m_screenw
; }
176 unsigned int wxGIFDecoder::GetLogicalScreenHeight() const { return m_screenh
; }
177 int wxGIFDecoder::GetBackgroundColour() const { return m_background
; }
178 int wxGIFDecoder::GetNumberOfFrames() const { return m_nimages
; }
179 bool wxGIFDecoder::IsAnimation() const { return (m_nimages
> 1); }
182 //---------------------------------------------------------------------------
183 // Functions to move through the animation
184 //---------------------------------------------------------------------------
186 bool wxGIFDecoder::GoFirstFrame()
196 bool wxGIFDecoder::GoLastFrame()
206 bool wxGIFDecoder::GoNextFrame(bool cyclic
)
211 if ((m_image
< m_nimages
) || (cyclic
))
213 m_pimage
= m_pimage
->next
;
228 bool wxGIFDecoder::GoPrevFrame(bool cyclic
)
233 if ((m_image
> 1) || (cyclic
))
235 m_pimage
= m_pimage
->prev
;
250 bool wxGIFDecoder::GoFrame(int which
)
257 if ((which
>= 1) && (which
<= m_nimages
))
261 for (i
= 1; i
< which
; i
++)
262 m_pimage
= m_pimage
->next
;
271 //---------------------------------------------------------------------------
272 // GIF reading and decoding
273 //---------------------------------------------------------------------------
276 // Reads the next code from the file stream, with size 'bits'
278 int wxGIFDecoder::getcode(int bits
, int ab_fin
)
280 unsigned int mask
; /* bit mask */
281 unsigned int code
; /* code (result) */
284 /* get remaining bits from last byte read */
285 mask
= (1 << bits
) - 1;
286 code
= (m_lastbyte
>> (8 - m_restbits
)) & mask
;
288 /* keep reading new bytes while needed */
289 while (bits
> m_restbits
)
291 /* if no bytes left in this block, read the next block */
294 m_restbyte
= (unsigned char)m_f
->GetC();
296 /* Some encoders are a bit broken: instead of issuing
297 * an end-of-image symbol (ab_fin) they come up with
298 * a zero-length subblock!! We catch this here so
299 * that the decoder sees an ab_fin code.
308 m_f
->Read((void *) m_buffer
, m_restbyte
);
312 /* read next byte and isolate the bits we need */
313 m_lastbyte
= (unsigned char) (*m_bufp
++);
314 mask
= (1 << (bits
- m_restbits
)) - 1;
315 code
= code
+ ((m_lastbyte
& mask
) << m_restbits
);
318 /* adjust total number of bits extracted from the buffer */
319 m_restbits
= m_restbits
+ 8;
322 /* find number of bits remaining for next code */
323 m_restbits
= (m_restbits
- bits
);
330 // GIF decoding function. The initial code size (aka root size)
331 // is 'bits'. Supports interlaced images (interl == 1).
333 int wxGIFDecoder::dgif(GIFImage
*img
, int interl
, int bits
)
335 int *ab_prefix
= new int[4096]; /* alphabet (prefixes) */
336 int *ab_tail
= new int[4096]; /* alphabet (tails) */
337 int *stack
= new int[4096]; /* decompression stack */
338 int ab_clr
; /* clear code */
339 int ab_fin
; /* end of info code */
340 int ab_bits
; /* actual symbol width, in bits */
341 int ab_free
; /* first free position in alphabet */
342 int ab_max
; /* last possible character in alphabet */
343 int pass
; /* pass number in interlaced images */
344 int pos
; /* index into decompresion stack */
345 unsigned int x
, y
; /* position in image buffer */
347 int code
, readcode
, lastcode
, abcabca
;
349 /* these won't change */
350 ab_clr
= (1 << bits
);
351 ab_fin
= (1 << bits
) + 1;
353 /* these will change through the decompression proccess */
355 ab_free
= (1 << bits
) + 2;
356 ab_max
= (1 << ab_bits
) - 1;
362 /* reset decoder vars */
370 readcode
= code
= getcode(ab_bits
, ab_fin
);
373 if (code
== ab_fin
) break;
375 /* reset alphabet? */
378 /* reset main variables */
380 ab_free
= (1 << bits
) + 2;
381 ab_max
= (1 << ab_bits
) - 1;
385 /* skip to next code */
389 /* unknown code: special case (like in ABCABCA) */
392 code
= lastcode
; /* take last string */
393 stack
[pos
++] = abcabca
; /* add first character */
396 /* build the string for this code in the stack */
397 while (code
> ab_clr
)
399 stack
[pos
++] = ab_tail
[code
];
400 code
= ab_prefix
[code
];
402 stack
[pos
] = code
; /* push last code into the stack */
403 abcabca
= code
; /* save for special case */
405 /* make new entry in alphabet (only if NOT just cleared) */
408 ab_prefix
[ab_free
] = lastcode
;
409 ab_tail
[ab_free
] = code
;
412 if ((ab_free
> ab_max
) && (ab_bits
< 12))
415 ab_max
= (1 << ab_bits
) - 1;
419 /* dump stack data to the buffer */
422 (img
->p
)[x
+ (y
* (img
->w
))] = (char)stack
[pos
--];
430 /* support for interlaced images */
433 case 1: y
+= 8; break;
434 case 2: y
+= 8; break;
435 case 3: y
+= 4; break;
436 case 4: y
+= 2; break;
442 case 2: y
= 4; break;
443 case 3: y
= 2; break;
444 case 4: y
= 1; break;
459 while (code
!= ab_fin
);
461 delete [] ab_prefix
;
470 // Returns TRUE if the file looks like a valid GIF, FALSE otherwise.
472 bool wxGIFDecoder::CanRead()
474 unsigned char buf
[3];
477 m_f
->SeekI(-3, wxFromCurrent
);
479 return (memcmp(buf
, "GIF", 3) == 0);
484 // Reads and decodes one or more GIF images, depending on whether
485 // animated GIF support is enabled. Can read GIFs with any bit
486 // size (color depth), but the output images are always expanded
487 // to 8 bits per pixel. Also, the image palettes always contain
488 // 256 colors, although some of them may be unused. Returns wxGIF_OK
489 // (== 0) on success, or an error code if something fails (see
490 // header file for details)
492 int wxGIFDecoder::ReadGIF()
494 int ncolors
, bits
, interl
, transparent
, disposal
, i
;
497 unsigned char type
= 0;
498 unsigned char pal
[768];
499 unsigned char buf
[16];
501 GIFImage
*pimg
, *pprev
;
503 /* check GIF signature */
505 return wxGIF_INVFORMAT
;
507 /* check for animated GIF support (ver. >= 89a) */
510 if (memcmp(buf
+ 3, "89a", 3) < 0)
513 /* read logical screen descriptor block (LSDB) */
515 m_screenw
= buf
[0] + 256 * buf
[1];
516 m_screenh
= buf
[2] + 256 * buf
[3];
518 /* load global color map if available */
519 if ((buf
[4] & 0x80) == 0x80)
521 m_background
= buf
[5];
523 ncolors
= 2 << (buf
[4] & 0x07);
524 m_f
->Read(pal
, 3 * ncolors
);
527 /* transparent colour, disposal method and delay default to unused */
541 type
= (unsigned char)m_f
->GetC();
549 /* extension block? */
552 if (((unsigned char)m_f
->GetC()) == 0xF9)
553 /* graphics control extension, parse it */
557 /* read delay and convert from 1/100 of a second to ms */
558 delay
= 10 * (buf
[2] + 256 * buf
[3]);
560 /* read transparent colour index, if used */
562 transparent
= buf
[4];
564 /* read disposal method */
565 disposal
= (buf
[1] & 0x1C) - 1;
568 /* other extension, skip */
570 while ((i
= (unsigned char)m_f
->GetC()) != 0)
572 m_f
->SeekI(i
, wxFromCurrent
);
577 /* image descriptor block? */
580 /* allocate memory for IMAGEN struct */
581 pimg
= (*ppimg
) = new GIFImage();
589 /* fill in the data */
591 pimg
->left
= buf
[4] + 256 * buf
[5];
592 pimg
->top
= buf
[4] + 256 * buf
[5];
593 pimg
->w
= buf
[4] + 256 * buf
[5];
594 pimg
->h
= buf
[6] + 256 * buf
[7];
595 interl
= ((buf
[8] & 0x40)? 1 : 0);
596 size
= pimg
->w
* pimg
->h
;
598 pimg
->transparent
= transparent
;
599 pimg
->disposal
= disposal
;
606 /* allocate memory for image and palette */
607 pimg
->p
= (unsigned char *) malloc((size_t)size
);
608 pimg
->pal
= (unsigned char *) malloc(768);
610 if ((!pimg
->p
) || (!pimg
->pal
))
616 /* load local color map if available, else use global map */
617 if ((buf
[8] & 0x80) == 0x80)
619 ncolors
= 2 << (buf
[8] & 0x07);
620 m_f
->Read(pimg
->pal
, 3 * ncolors
);
623 memcpy(pimg
->pal
, pal
, 768);
625 /* get initial code size from first byte in raster data */
626 bits
= (unsigned char)m_f
->GetC();
629 dgif(pimg
, interl
, bits
);
632 /* if this is not an animated GIF, exit after first image */
638 /* setup image pointers */
646 /* try to read to the end of the stream */
649 type
= (unsigned char)m_f
->GetC();
657 while ((i
= (unsigned char)m_f
->GetC()) != 0)
659 m_f
->SeekI(i
, wxFromCurrent
);
662 else if (type
== 0x2C)
664 /* image descriptor block */
667 /* local color map */
668 if ((buf
[8] & 0x80) == 0x80)
670 ncolors
= 2 << (buf
[8] & 0x07);
671 m_f
->SeekI(3 * ncolors
, wxFromCurrent
);
674 /* initial code size */
678 while ((i
= (unsigned char)m_f
->GetC()) != 0)
680 m_f
->SeekI(i
, wxFromCurrent
);
683 else if ((type
!= 0x3B) && (type
!= 00)) /* testing */
685 /* images are OK, but couldn't read to the end of the stream */
686 return wxGIF_TRUNCATED
;
693 #endif // wxUSE_STREAMS && wxUSE_GIF