]>
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"
23 # include "wx/setup.h"
31 #include "wx/gifdecod.h"
34 //---------------------------------------------------------------------------
35 // wxGIFDecoder constructor and destructor
36 //---------------------------------------------------------------------------
38 wxGIFDecoder::wxGIFDecoder(wxInputStream
*s
, bool anim
)
54 wxGIFDecoder::~wxGIFDecoder()
59 void wxGIFDecoder::Destroy()
76 //---------------------------------------------------------------------------
77 // Convert this image to a wxImage object
78 //---------------------------------------------------------------------------
80 // This function was designed by Vaclav Slavik
82 bool wxGIFDecoder::ConvertToImage(wxImage
*image
) const
84 unsigned char *src
, *dst
, *pal
;
91 /* create the image */
92 image
->Create(GetWidth(), GetHeight());
99 dst
= image
->GetData();
100 transparent
= GetTransparentColour();
102 /* set transparent colour mask */
103 if (transparent
!= -1)
105 for (i
= 0; i
< 256; i
++)
107 if ((pal
[3 * i
+ 0] == 255) &&
108 (pal
[3 * i
+ 1] == 0) &&
109 (pal
[3 * i
+ 2] == 255))
111 pal
[3 * i
+ 2] = 254;
115 pal
[3 * transparent
+ 0] = 255,
116 pal
[3 * transparent
+ 1] = 0,
117 pal
[3 * transparent
+ 2] = 255;
119 image
->SetMaskColour(255, 0, 255);
122 image
->SetMask(FALSE
);
124 /* copy image data */
125 for (i
= 0; i
< (GetWidth() * GetHeight()); i
++, src
++)
127 *(dst
++) = pal
[3 * (*src
) + 0];
128 *(dst
++) = pal
[3 * (*src
) + 1];
129 *(dst
++) = pal
[3 * (*src
) + 2];
136 //---------------------------------------------------------------------------
138 //---------------------------------------------------------------------------
140 // Get data for current frame
142 int wxGIFDecoder::GetFrameIndex() const { return m_image
; }
143 unsigned char* wxGIFDecoder::GetData() const { return (m_pimage
->p
); }
144 unsigned char* wxGIFDecoder::GetPalette() const { return (m_pimage
->pal
); }
145 unsigned int wxGIFDecoder::GetWidth() const { return (m_pimage
->w
); }
146 unsigned int wxGIFDecoder::GetHeight() const { return (m_pimage
->h
); }
147 unsigned int wxGIFDecoder::GetTop() const { return (m_pimage
->top
); }
148 unsigned int wxGIFDecoder::GetLeft() const { return (m_pimage
->left
); }
149 int wxGIFDecoder::GetTransparentColour() const { return (m_pimage
->transparent
); }
150 int wxGIFDecoder::GetDisposalMethod() const { return (m_pimage
->disposal
); }
151 long wxGIFDecoder::GetDelay() const { return (m_pimage
->delay
); }
155 unsigned int wxGIFDecoder::GetLogicalScreenWidth() const { return m_screenw
; }
156 unsigned int wxGIFDecoder::GetLogicalScreenHeight() const { return m_screenh
; }
157 int wxGIFDecoder::GetBackgroundColour() const { return m_background
; }
158 int wxGIFDecoder::GetNumberOfFrames() const { return m_nimages
; }
159 bool wxGIFDecoder::IsAnimation() const { return (m_nimages
> 1); }
162 //---------------------------------------------------------------------------
163 // Functions to move through the animation
164 //---------------------------------------------------------------------------
166 bool wxGIFDecoder::GoFirstFrame()
176 bool wxGIFDecoder::GoLastFrame()
186 bool wxGIFDecoder::GoNextFrame(bool cyclic
)
191 if ((m_image
< m_nimages
) || (cyclic
))
193 m_pimage
= m_pimage
->next
;
208 bool wxGIFDecoder::GoPrevFrame(bool cyclic
)
213 if ((m_image
> 1) || (cyclic
))
215 m_pimage
= m_pimage
->prev
;
230 bool wxGIFDecoder::GoFrame(int which
)
237 if ((which
>= 1) && (which
<= m_nimages
))
241 for (i
= 1; i
< which
; i
++)
242 m_pimage
= m_pimage
->next
;
251 //---------------------------------------------------------------------------
252 // GIF reading and decoding
253 //---------------------------------------------------------------------------
256 // Reads the next code from the file stream, with size 'bits'
258 int wxGIFDecoder::getcode(int bits
, int ab_fin
)
260 unsigned int mask
; /* bit mask */
261 unsigned int code
; /* code (result) */
264 /* get remaining bits from last byte read */
265 mask
= (1 << bits
) - 1;
266 code
= (m_lastbyte
>> (8 - m_restbits
)) & mask
;
268 /* keep reading new bytes while needed */
269 while (bits
> m_restbits
)
271 /* if no bytes left in this block, read the next block */
274 m_restbyte
= (unsigned char)m_f
->GetC();
276 /* Some encoders are a bit broken: instead of issuing
277 * an end-of-image symbol (ab_fin) they come up with
278 * a zero-length subblock!! We catch this here so
279 * that the decoder sees an ab_fin code.
288 /* read next byte and isolate the bits we need */
289 m_lastbyte
= (unsigned char)m_f
->GetC();
290 mask
= (1 << (bits
- m_restbits
)) - 1;
291 code
= code
+ ((m_lastbyte
& mask
) << m_restbits
);
294 /* adjust total number of bits extracted from the buffer */
295 m_restbits
= m_restbits
+ 8;
298 /* find number of bits remaining for next code */
299 m_restbits
= (m_restbits
- bits
);
306 // GIF decoding function. The initial code size (aka root size)
307 // is 'bits'. Supports interlaced images (interl == 1).
309 int wxGIFDecoder::dgif(IMAGEN
*img
, int interl
, int bits
)
311 int ab_prefix
[4096]; /* alphabet (prefixes) */
312 int ab_tail
[4096]; /* alphabet (tails) */
313 int stack
[4096]; /* decompression stack */
315 int ab_clr
; /* clear code */
316 int ab_fin
; /* end of info code */
317 int ab_bits
; /* actual symbol width, in bits */
318 int ab_free
; /* first free position in alphabet */
319 int ab_max
; /* last possible character in alphabet */
320 int pass
; /* pass number in interlaced images */
321 int pos
; /* index into decompresion stack */
322 unsigned int x
, y
; /* position in image buffer */
324 int code
, readcode
, lastcode
, abcabca
;
326 /* these won't change */
327 ab_clr
= (1 << bits
);
328 ab_fin
= (1 << bits
) + 1;
330 /* these will change through the decompression proccess */
332 ab_free
= (1 << bits
) + 2;
333 ab_max
= (1 << ab_bits
) - 1;
339 /* reset static globals */
347 readcode
= code
= getcode(ab_bits
, ab_fin
);
350 if (code
== ab_fin
) break;
352 /* reset alphabet? */
355 /* reset main variables */
357 ab_free
= (1 << bits
) + 2;
358 ab_max
= (1 << ab_bits
) - 1;
362 /* skip to next code */
366 /* unknown code: special case (like in ABCABCA) */
369 code
= lastcode
; /* take last string */
370 stack
[pos
++] = abcabca
; /* add first character */
373 /* build the string for this code in the stack */
374 while (code
> ab_clr
)
376 stack
[pos
++] = ab_tail
[code
];
377 code
= ab_prefix
[code
];
379 stack
[pos
] = code
; /* push last code into the stack */
380 abcabca
= code
; /* save for special case */
382 /* make new entry in alphabet (only if NOT just cleared) */
385 ab_prefix
[ab_free
] = lastcode
;
386 ab_tail
[ab_free
] = code
;
389 if ((ab_free
> ab_max
) && (ab_bits
< 12))
392 ab_max
= (1 << ab_bits
) - 1;
396 /* dump stack data to the buffer */
399 (img
->p
)[x
+ (y
* (img
->w
))] = (char)stack
[pos
--];
407 /* support for interlaced images */
410 case 1: y
+= 8; break;
411 case 2: y
+= 8; break;
412 case 3: y
+= 4; break;
413 case 4: y
+= 2; break;
419 case 2: y
= 4; break;
420 case 3: y
= 2; break;
421 case 4: y
= 1; break;
436 while (code
!= ab_fin
);
443 // Returns TRUE if the file looks like a valid GIF, FALSE otherwise.
445 bool wxGIFDecoder::CanRead()
447 unsigned char buf
[3];
451 m_f
->SeekI(0, wxFromStart
);
453 m_f
->SeekI(pos
, wxFromStart
);
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 E_OK
465 // (== 0) on success, or an error code if something fails. Error
466 // codes are E_ARCHIVO, E_FORMATO, E_MEMORIA (see header file).
468 int wxGIFDecoder::ReadGIF()
470 int ncolors
, bits
, interl
, transparent
, disposal
, i
;
474 unsigned char pal
[768];
475 unsigned char buf
[16];
476 IMAGEN
**ppimg
, *pimg
, *pprev
;
478 /* check GIF signature */
482 /* check for and animated GIF support (ver. >= 89a) */
483 m_f
->SeekI(0, wxFromStart
);
486 if (memcmp(buf
+ 3, "89a", 3) < 0)
489 /* read logical screen descriptor block (LSDB) */
491 m_screenw
= buf
[0] + 256 * buf
[1];
492 m_screenh
= buf
[2] + 256 * buf
[3];
494 /* load global color map if available */
495 if ((buf
[4] & 0x80) == 0x80)
497 m_background
= buf
[5];
499 ncolors
= 2 << (buf
[4] & 0x07);
500 m_f
->Read(pal
, 3 * ncolors
);
503 /* transparent colour, disposal method and delay default to unused */
515 type
= (unsigned char)m_f
->GetC();
521 /* extension block? */
524 if (((unsigned char)m_f
->GetC()) == 0xF9)
525 /* graphics control extension, parse it */
529 /* read delay and convert from 1/100 of a second to ms */
530 delay
= 10 * (buf
[2] + 256 * buf
[3]);
532 /* read transparent colour index, if used */
534 transparent
= buf
[4];
536 /* read disposal method */
537 disposal
= (buf
[1] & 0x1C) - 1;
540 /* other extension, skip */
542 while ((i
= (unsigned char)m_f
->GetC()) != 0)
544 /* This line should not be neccessary!
545 * Some images are not loaded correctly
546 * without it. A bug in wxStream?
549 // m_f->SeekI(m_f->TellI(), wxFromStart);
551 m_f
->SeekI(i
, wxFromCurrent
);
556 /* image descriptor block? */
559 /* allocate memory for IMAGEN struct */
560 pimg
= (*ppimg
) = (IMAGEN
*) malloc(sizeof(IMAGEN
));
568 /* fill in the data */
570 pimg
->left
= buf
[4] + 256 * buf
[5];
571 pimg
->top
= buf
[4] + 256 * buf
[5];
572 pimg
->w
= buf
[4] + 256 * buf
[5];
573 pimg
->h
= buf
[6] + 256 * buf
[7];
574 interl
= ((buf
[8] & 0x40)? 1 : 0);
575 size
= pimg
->w
* pimg
->h
;
577 pimg
->transparent
= transparent
;
578 pimg
->disposal
= disposal
;
585 /* allocate memory for image and palette */
586 pimg
->p
= (unsigned char *) malloc(size
);
587 pimg
->pal
= (unsigned char *) malloc(768);
589 if ((!pimg
->p
) || (!pimg
->pal
))
595 /* load local color map if available, else use global map */
596 if ((buf
[8] & 0x80) == 0x80)
598 ncolors
= 2 << (buf
[8] & 0x07);
599 m_f
->Read(pimg
->pal
, 3 * ncolors
);
602 memcpy(pimg
->pal
, pal
, 768);
604 /* get initial code size from first byte in raster data */
605 bits
= (unsigned char)m_f
->GetC();
608 dgif(pimg
, interl
, bits
);
613 /* if we have one image and no animated GIF support, exit */
614 if (m_nimages
== 1 && !m_anim
)
618 /* finish successfully :-) */
629 #endif // wxUSE_STREAM