]>
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
;
75 //---------------------------------------------------------------------------
76 // Convert this image to a wxImage object
77 //---------------------------------------------------------------------------
79 // This function was designed by Vaclav Slavik
81 bool wxGIFDecoder::ConvertToImage(wxImage
*image
) const
83 unsigned char *src
, *dst
, *pal
;
90 /* create the image */
91 image
->Create(GetWidth(), GetHeight());
98 dst
= image
->GetData();
99 transparent
= GetTransparentColour();
101 /* set transparent colour mask */
102 if (transparent
!= -1)
104 for (i
= 0; i
< 256; i
++)
106 if ((pal
[3 * i
+ 0] == 255) &&
107 (pal
[3 * i
+ 1] == 0) &&
108 (pal
[3 * i
+ 2] == 255))
110 pal
[3 * i
+ 2] = 254;
114 pal
[3 * transparent
+ 0] = 255,
115 pal
[3 * transparent
+ 1] = 0,
116 pal
[3 * transparent
+ 2] = 255;
118 image
->SetMaskColour(255, 0, 255);
121 image
->SetMask(FALSE
);
123 /* copy image data */
124 for (i
= 0; i
< (GetWidth() * GetHeight()); i
++, src
++)
126 *(dst
++) = pal
[3 * (*src
) + 0];
127 *(dst
++) = pal
[3 * (*src
) + 1];
128 *(dst
++) = pal
[3 * (*src
) + 2];
135 //---------------------------------------------------------------------------
137 //---------------------------------------------------------------------------
139 // Get data for current frame
141 int wxGIFDecoder::GetFrameIndex() const { return m_image
; }
142 unsigned char* wxGIFDecoder::GetData() const { return (m_pimage
->p
); }
143 unsigned char* wxGIFDecoder::GetPalette() const { return (m_pimage
->pal
); }
144 unsigned int wxGIFDecoder::GetWidth() const { return (m_pimage
->w
); }
145 unsigned int wxGIFDecoder::GetHeight() const { return (m_pimage
->h
); }
146 unsigned int wxGIFDecoder::GetTop() const { return (m_pimage
->top
); }
147 unsigned int wxGIFDecoder::GetLeft() const { return (m_pimage
->left
); }
148 int wxGIFDecoder::GetTransparentColour() const { return (m_pimage
->transparent
); }
149 int wxGIFDecoder::GetDisposalMethod() const { return (m_pimage
->disposal
); }
150 long wxGIFDecoder::GetDelay() const { return (m_pimage
->delay
); }
154 unsigned int wxGIFDecoder::GetLogicalScreenWidth() const { return m_screenw
; }
155 unsigned int wxGIFDecoder::GetLogicalScreenHeight() const { return m_screenh
; }
156 int wxGIFDecoder::GetBackgroundColour() const { return m_background
; }
157 int wxGIFDecoder::GetNumberOfFrames() const { return m_nimages
; }
158 bool wxGIFDecoder::IsAnimation() const { return (m_nimages
> 1); }
161 //---------------------------------------------------------------------------
162 // Functions to move through the animation
163 //---------------------------------------------------------------------------
165 bool wxGIFDecoder::GoFirstFrame()
175 bool wxGIFDecoder::GoLastFrame()
185 bool wxGIFDecoder::GoNextFrame(bool cyclic
)
190 if ((m_image
< m_nimages
) || (cyclic
))
192 m_pimage
= m_pimage
->next
;
207 bool wxGIFDecoder::GoPrevFrame(bool cyclic
)
212 if ((m_image
> 1) || (cyclic
))
214 m_pimage
= m_pimage
->prev
;
229 bool wxGIFDecoder::GoFrame(int which
)
236 if ((which
>= 1) && (which
<= m_nimages
))
240 for (i
= 1; i
< which
; i
++)
241 m_pimage
= m_pimage
->next
;
250 //---------------------------------------------------------------------------
251 // GIF reading and decoding
252 //---------------------------------------------------------------------------
255 // Reads the next code from the file stream, with size 'bits'
257 int wxGIFDecoder::getcode(int bits
, int ab_fin
)
259 unsigned int mask
; /* bit mask */
260 unsigned int code
; /* code (result) */
263 /* get remaining bits from last byte read */
264 mask
= (1 << bits
) - 1;
265 code
= (m_lastbyte
>> (8 - m_restbits
)) & mask
;
267 /* keep reading new bytes while needed */
268 while (bits
> m_restbits
)
270 /* if no bytes left in this block, read the next block */
273 m_restbyte
= (unsigned char)m_f
->GetC();
275 /* Some encoders are a bit broken: instead of issuing
276 * an end-of-image symbol (ab_fin) they come up with
277 * a zero-length subblock!! We catch this here so
278 * that the decoder sees an ab_fin code.
287 m_f
->Read((void *) m_buffer
, m_restbyte
);
291 /* read next byte and isolate the bits we need */
292 m_lastbyte
= (unsigned char) (*m_bufp
++);
293 mask
= (1 << (bits
- m_restbits
)) - 1;
294 code
= code
+ ((m_lastbyte
& mask
) << m_restbits
);
297 /* adjust total number of bits extracted from the buffer */
298 m_restbits
= m_restbits
+ 8;
301 /* find number of bits remaining for next code */
302 m_restbits
= (m_restbits
- bits
);
309 // GIF decoding function. The initial code size (aka root size)
310 // is 'bits'. Supports interlaced images (interl == 1).
312 int wxGIFDecoder::dgif(GIFImage
*img
, int interl
, int bits
)
314 int ab_prefix
[4096]; /* alphabet (prefixes) */
315 int ab_tail
[4096]; /* alphabet (tails) */
316 int stack
[4096]; /* decompression stack */
318 int ab_clr
; /* clear code */
319 int ab_fin
; /* end of info code */
320 int ab_bits
; /* actual symbol width, in bits */
321 int ab_free
; /* first free position in alphabet */
322 int ab_max
; /* last possible character in alphabet */
323 int pass
; /* pass number in interlaced images */
324 int pos
; /* index into decompresion stack */
325 unsigned int x
, y
; /* position in image buffer */
327 int code
, readcode
, lastcode
, abcabca
;
329 /* these won't change */
330 ab_clr
= (1 << bits
);
331 ab_fin
= (1 << bits
) + 1;
333 /* these will change through the decompression proccess */
335 ab_free
= (1 << bits
) + 2;
336 ab_max
= (1 << ab_bits
) - 1;
342 /* reset decoder vars */
350 readcode
= code
= getcode(ab_bits
, ab_fin
);
353 if (code
== ab_fin
) break;
355 /* reset alphabet? */
358 /* reset main variables */
360 ab_free
= (1 << bits
) + 2;
361 ab_max
= (1 << ab_bits
) - 1;
365 /* skip to next code */
369 /* unknown code: special case (like in ABCABCA) */
372 code
= lastcode
; /* take last string */
373 stack
[pos
++] = abcabca
; /* add first character */
376 /* build the string for this code in the stack */
377 while (code
> ab_clr
)
379 stack
[pos
++] = ab_tail
[code
];
380 code
= ab_prefix
[code
];
382 stack
[pos
] = code
; /* push last code into the stack */
383 abcabca
= code
; /* save for special case */
385 /* make new entry in alphabet (only if NOT just cleared) */
388 ab_prefix
[ab_free
] = lastcode
;
389 ab_tail
[ab_free
] = code
;
392 if ((ab_free
> ab_max
) && (ab_bits
< 12))
395 ab_max
= (1 << ab_bits
) - 1;
399 /* dump stack data to the buffer */
402 (img
->p
)[x
+ (y
* (img
->w
))] = (char)stack
[pos
--];
410 /* support for interlaced images */
413 case 1: y
+= 8; break;
414 case 2: y
+= 8; break;
415 case 3: y
+= 4; break;
416 case 4: y
+= 2; break;
422 case 2: y
= 4; break;
423 case 3: y
= 2; break;
424 case 4: y
= 1; break;
439 while (code
!= ab_fin
);
446 // Returns TRUE if the file looks like a valid GIF, FALSE otherwise.
448 bool wxGIFDecoder::CanRead()
450 unsigned char buf
[3];
453 m_f
->SeekI(-3, wxFromCurrent
);
455 return (memcmp(buf
, "GIF", 3) == 0);
460 // Reads and decodes one or more GIF images, depending on whether
461 // animated GIF support is enabled. Can read GIFs with any bit
462 // size (color depth), but the output images are always expanded
463 // to 8 bits per pixel. Also, the image palettes always contain
464 // 256 colors, although some of them may be unused. Returns GIF_OK
465 // (== 0) on success, or an error code if something fails (see
466 // header file for details)
468 int wxGIFDecoder::ReadGIF()
470 int ncolors
, bits
, interl
, transparent
, disposal
, i
;
474 unsigned char pal
[768];
475 unsigned char buf
[16];
476 GIFImage
**ppimg
, *pimg
, *pprev
;
478 /* check GIF signature */
480 return wxGIF_INVFORMAT
;
482 /* check for and animated GIF support (ver. >= 89a) */
485 if (memcmp(buf
+ 3, "89a", 3) < 0)
488 /* read logical screen descriptor block (LSDB) */
490 m_screenw
= buf
[0] + 256 * buf
[1];
491 m_screenh
= buf
[2] + 256 * buf
[3];
493 /* load global color map if available */
494 if ((buf
[4] & 0x80) == 0x80)
496 m_background
= buf
[5];
498 ncolors
= 2 << (buf
[4] & 0x07);
499 m_f
->Read(pal
, 3 * ncolors
);
502 /* transparent colour, disposal method and delay default to unused */
512 #if defined(__VISAGECPP__)
513 // VA just can't stand while(1)
520 type
= (unsigned char)m_f
->GetC();
526 /* extension block? */
529 if (((unsigned char)m_f
->GetC()) == 0xF9)
530 /* graphics control extension, parse it */
534 /* read delay and convert from 1/100 of a second to ms */
535 delay
= 10 * (buf
[2] + 256 * buf
[3]);
537 /* read transparent colour index, if used */
539 transparent
= buf
[4];
541 /* read disposal method */
542 disposal
= (buf
[1] & 0x1C) - 1;
545 /* other extension, skip */
547 while ((i
= (unsigned char)m_f
->GetC()) != 0)
549 /* This line should not be neccessary!
550 * Some images are not loaded correctly
551 * without it. A bug in wxStream?
554 // m_f->SeekI(m_f->TellI(), wxFromStart);
556 m_f
->SeekI(i
, wxFromCurrent
);
561 /* image descriptor block? */
564 /* allocate memory for IMAGEN struct */
565 pimg
= (*ppimg
) = new GIFImage();
573 /* fill in the data */
575 pimg
->left
= buf
[4] + 256 * buf
[5];
576 pimg
->top
= buf
[4] + 256 * buf
[5];
577 pimg
->w
= buf
[4] + 256 * buf
[5];
578 pimg
->h
= buf
[6] + 256 * buf
[7];
579 interl
= ((buf
[8] & 0x40)? 1 : 0);
580 size
= pimg
->w
* pimg
->h
;
582 pimg
->transparent
= transparent
;
583 pimg
->disposal
= disposal
;
590 /* allocate memory for image and palette */
591 pimg
->p
= (unsigned char *) malloc((size_t)size
);
592 pimg
->pal
= (unsigned char *) malloc(768);
594 if ((!pimg
->p
) || (!pimg
->pal
))
600 /* load local color map if available, else use global map */
601 if ((buf
[8] & 0x80) == 0x80)
603 ncolors
= 2 << (buf
[8] & 0x07);
604 m_f
->Read(pimg
->pal
, 3 * ncolors
);
607 memcpy(pimg
->pal
, pal
, 768);
609 /* get initial code size from first byte in raster data */
610 bits
= (unsigned char)m_f
->GetC();
613 dgif(pimg
, interl
, bits
);
618 /* if we have one image and no animated GIF support, exit */
619 if (m_nimages
== 1 && !m_anim
)
623 /* finish successfully :-) */
634 #endif // wxUSE_STREAMS && wxUSE_GIF