]>
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
);
132 unsigned char* r
= new unsigned char[256];
133 unsigned char* g
= new unsigned char[256];
134 unsigned char* b
= new unsigned char[256];
135 for (i
= 0; i
< 256; i
++)
141 image
->SetPalette(wxPalette(256, r
, g
, b
));
142 delete[] r
; delete[] g
; delete[] b
;
145 /* copy image data */
146 for (i
= 0; i
< (GetWidth() * GetHeight()); i
++, src
++)
148 *(dst
++) = pal
[3 * (*src
) + 0];
149 *(dst
++) = pal
[3 * (*src
) + 1];
150 *(dst
++) = pal
[3 * (*src
) + 2];
157 //---------------------------------------------------------------------------
159 //---------------------------------------------------------------------------
161 // Get data for current frame
163 int wxGIFDecoder::GetFrameIndex() const { return m_image
; }
164 unsigned char* wxGIFDecoder::GetData() const { return (m_pimage
->p
); }
165 unsigned char* wxGIFDecoder::GetPalette() const { return (m_pimage
->pal
); }
166 unsigned int wxGIFDecoder::GetWidth() const { return (m_pimage
->w
); }
167 unsigned int wxGIFDecoder::GetHeight() const { return (m_pimage
->h
); }
168 unsigned int wxGIFDecoder::GetTop() const { return (m_pimage
->top
); }
169 unsigned int wxGIFDecoder::GetLeft() const { return (m_pimage
->left
); }
170 int wxGIFDecoder::GetTransparentColour() const { return (m_pimage
->transparent
); }
171 int wxGIFDecoder::GetDisposalMethod() const { return (m_pimage
->disposal
); }
172 long wxGIFDecoder::GetDelay() const { return (m_pimage
->delay
); }
176 unsigned int wxGIFDecoder::GetLogicalScreenWidth() const { return m_screenw
; }
177 unsigned int wxGIFDecoder::GetLogicalScreenHeight() const { return m_screenh
; }
178 int wxGIFDecoder::GetBackgroundColour() const { return m_background
; }
179 int wxGIFDecoder::GetNumberOfFrames() const { return m_nimages
; }
180 bool wxGIFDecoder::IsAnimation() const { return (m_nimages
> 1); }
183 //---------------------------------------------------------------------------
184 // Functions to move through the animation
185 //---------------------------------------------------------------------------
187 bool wxGIFDecoder::GoFirstFrame()
197 bool wxGIFDecoder::GoLastFrame()
207 bool wxGIFDecoder::GoNextFrame(bool cyclic
)
212 if ((m_image
< m_nimages
) || (cyclic
))
214 m_pimage
= m_pimage
->next
;
229 bool wxGIFDecoder::GoPrevFrame(bool cyclic
)
234 if ((m_image
> 1) || (cyclic
))
236 m_pimage
= m_pimage
->prev
;
251 bool wxGIFDecoder::GoFrame(int which
)
258 if ((which
>= 1) && (which
<= m_nimages
))
262 for (i
= 1; i
< which
; i
++)
263 m_pimage
= m_pimage
->next
;
272 //---------------------------------------------------------------------------
273 // GIF reading and decoding
274 //---------------------------------------------------------------------------
277 // Reads the next code from the file stream, with size 'bits'
279 int wxGIFDecoder::getcode(int bits
, int ab_fin
)
281 unsigned int mask
; /* bit mask */
282 unsigned int code
; /* code (result) */
285 /* get remaining bits from last byte read */
286 mask
= (1 << bits
) - 1;
287 code
= (m_lastbyte
>> (8 - m_restbits
)) & mask
;
289 /* keep reading new bytes while needed */
290 while (bits
> m_restbits
)
292 /* if no bytes left in this block, read the next block */
295 m_restbyte
= (unsigned char)m_f
->GetC();
297 /* Some encoders are a bit broken: instead of issuing
298 * an end-of-image symbol (ab_fin) they come up with
299 * a zero-length subblock!! We catch this here so
300 * that the decoder sees an ab_fin code.
309 m_f
->Read((void *) m_buffer
, m_restbyte
);
313 /* read next byte and isolate the bits we need */
314 m_lastbyte
= (unsigned char) (*m_bufp
++);
315 mask
= (1 << (bits
- m_restbits
)) - 1;
316 code
= code
+ ((m_lastbyte
& mask
) << m_restbits
);
319 /* adjust total number of bits extracted from the buffer */
320 m_restbits
= m_restbits
+ 8;
323 /* find number of bits remaining for next code */
324 m_restbits
= (m_restbits
- bits
);
331 // GIF decoding function. The initial code size (aka root size)
332 // is 'bits'. Supports interlaced images (interl == 1).
334 int wxGIFDecoder::dgif(GIFImage
*img
, int interl
, int bits
)
336 int ab_prefix
[4096]; /* alphabet (prefixes) */
337 int ab_tail
[4096]; /* alphabet (tails) */
338 int stack
[4096]; /* decompression stack */
340 int ab_clr
; /* clear code */
341 int ab_fin
; /* end of info code */
342 int ab_bits
; /* actual symbol width, in bits */
343 int ab_free
; /* first free position in alphabet */
344 int ab_max
; /* last possible character in alphabet */
345 int pass
; /* pass number in interlaced images */
346 int pos
; /* index into decompresion stack */
347 unsigned int x
, y
; /* position in image buffer */
349 int code
, readcode
, lastcode
, abcabca
;
351 /* these won't change */
352 ab_clr
= (1 << bits
);
353 ab_fin
= (1 << bits
) + 1;
355 /* these will change through the decompression proccess */
357 ab_free
= (1 << bits
) + 2;
358 ab_max
= (1 << ab_bits
) - 1;
364 /* reset decoder vars */
372 readcode
= code
= getcode(ab_bits
, ab_fin
);
375 if (code
== ab_fin
) break;
377 /* reset alphabet? */
380 /* reset main variables */
382 ab_free
= (1 << bits
) + 2;
383 ab_max
= (1 << ab_bits
) - 1;
387 /* skip to next code */
391 /* unknown code: special case (like in ABCABCA) */
394 code
= lastcode
; /* take last string */
395 stack
[pos
++] = abcabca
; /* add first character */
398 /* build the string for this code in the stack */
399 while (code
> ab_clr
)
401 stack
[pos
++] = ab_tail
[code
];
402 code
= ab_prefix
[code
];
404 stack
[pos
] = code
; /* push last code into the stack */
405 abcabca
= code
; /* save for special case */
407 /* make new entry in alphabet (only if NOT just cleared) */
410 ab_prefix
[ab_free
] = lastcode
;
411 ab_tail
[ab_free
] = code
;
414 if ((ab_free
> ab_max
) && (ab_bits
< 12))
417 ab_max
= (1 << ab_bits
) - 1;
421 /* dump stack data to the buffer */
424 (img
->p
)[x
+ (y
* (img
->w
))] = (char)stack
[pos
--];
432 /* support for interlaced images */
435 case 1: y
+= 8; break;
436 case 2: y
+= 8; break;
437 case 3: y
+= 4; break;
438 case 4: y
+= 2; break;
444 case 2: y
= 4; break;
445 case 3: y
= 2; break;
446 case 4: y
= 1; break;
461 while (code
!= ab_fin
);
468 // Returns TRUE if the file looks like a valid GIF, FALSE otherwise.
470 bool wxGIFDecoder::CanRead()
472 unsigned char buf
[3];
475 m_f
->SeekI(-3, wxFromCurrent
);
477 return (memcmp(buf
, "GIF", 3) == 0);
482 // Reads and decodes one or more GIF images, depending on whether
483 // animated GIF support is enabled. Can read GIFs with any bit
484 // size (color depth), but the output images are always expanded
485 // to 8 bits per pixel. Also, the image palettes always contain
486 // 256 colors, although some of them may be unused. Returns wxGIF_OK
487 // (== 0) on success, or an error code if something fails (see
488 // header file for details)
490 int wxGIFDecoder::ReadGIF()
492 int ncolors
, bits
, interl
, transparent
, disposal
, i
;
496 unsigned char pal
[768];
497 unsigned char buf
[16];
498 GIFImage
**ppimg
, *pimg
, *pprev
;
500 /* check GIF signature */
502 return wxGIF_INVFORMAT
;
504 /* check for animated GIF support (ver. >= 89a) */
507 if (memcmp(buf
+ 3, "89a", 3) < 0)
510 /* read logical screen descriptor block (LSDB) */
512 m_screenw
= buf
[0] + 256 * buf
[1];
513 m_screenh
= buf
[2] + 256 * buf
[3];
515 /* load global color map if available */
516 if ((buf
[4] & 0x80) == 0x80)
518 m_background
= buf
[5];
520 ncolors
= 2 << (buf
[4] & 0x07);
521 m_f
->Read(pal
, 3 * ncolors
);
524 /* transparent colour, disposal method and delay default to unused */
538 type
= (unsigned char)m_f
->GetC();
546 /* extension block? */
549 if (((unsigned char)m_f
->GetC()) == 0xF9)
550 /* graphics control extension, parse it */
554 /* read delay and convert from 1/100 of a second to ms */
555 delay
= 10 * (buf
[2] + 256 * buf
[3]);
557 /* read transparent colour index, if used */
559 transparent
= buf
[4];
561 /* read disposal method */
562 disposal
= (buf
[1] & 0x1C) - 1;
565 /* other extension, skip */
567 while ((i
= (unsigned char)m_f
->GetC()) != 0)
569 m_f
->SeekI(i
, wxFromCurrent
);
574 /* image descriptor block? */
577 /* allocate memory for IMAGEN struct */
578 pimg
= (*ppimg
) = new GIFImage();
586 /* fill in the data */
588 pimg
->left
= buf
[4] + 256 * buf
[5];
589 pimg
->top
= buf
[4] + 256 * buf
[5];
590 pimg
->w
= buf
[4] + 256 * buf
[5];
591 pimg
->h
= buf
[6] + 256 * buf
[7];
592 interl
= ((buf
[8] & 0x40)? 1 : 0);
593 size
= pimg
->w
* pimg
->h
;
595 pimg
->transparent
= transparent
;
596 pimg
->disposal
= disposal
;
603 /* allocate memory for image and palette */
604 pimg
->p
= (unsigned char *) malloc((size_t)size
);
605 pimg
->pal
= (unsigned char *) malloc(768);
607 if ((!pimg
->p
) || (!pimg
->pal
))
613 /* load local color map if available, else use global map */
614 if ((buf
[8] & 0x80) == 0x80)
616 ncolors
= 2 << (buf
[8] & 0x07);
617 m_f
->Read(pimg
->pal
, 3 * ncolors
);
620 memcpy(pimg
->pal
, pal
, 768);
622 /* get initial code size from first byte in raster data */
623 bits
= (unsigned char)m_f
->GetC();
626 dgif(pimg
, interl
, bits
);
629 /* if this is not an animated GIF, exit after first image */
635 /* setup image pointers */
643 /* try to read to the end of the stream */
646 type
= (unsigned char)m_f
->GetC();
654 while ((i
= (unsigned char)m_f
->GetC()) != 0)
656 m_f
->SeekI(i
, wxFromCurrent
);
659 else if (type
== 0x2C)
661 /* image descriptor block */
664 /* local color map */
665 if ((buf
[8] & 0x80) == 0x80)
667 ncolors
= 2 << (buf
[8] & 0x07);
668 m_f
->SeekI(3 * ncolors
, wxFromCurrent
);
671 /* initial code size */
675 while ((i
= (unsigned char)m_f
->GetC()) != 0)
677 m_f
->SeekI(i
, wxFromCurrent
);
680 else if ((type
!= 0x3B) && (type
!= 00)) /* testing */
682 /* images are OK, but couldn't read to the end of the stream */
683 return wxGIF_TRUNCATED
;
690 #endif // wxUSE_STREAMS && wxUSE_GIF