]> git.saurik.com Git - wxWidgets.git/blame - src/common/gifdecod.cpp
Added chapter on collection and container classes to contents
[wxWidgets.git] / src / common / gifdecod.cpp
CommitLineData
464122b6
JS
1/////////////////////////////////////////////////////////////////////////////
2// Name: gifdecod.cpp
3// Purpose: wxGIFDecoder, GIF reader for wxImage and wxAnimation
4// Author: Guillermo Rodriguez Garcia <guille@iies.es>
9d0e21da
GRG
5// Version: 3.04
6// RCS-ID: $Id$
464122b6
JS
7// Copyright: (c) Guillermo Rodriguez Garcia
8// Licence: wxWindows licence
9/////////////////////////////////////////////////////////////////////////////
10
11#ifdef __GNUG__
12#pragma implementation "gifdecod.h"
13#endif
14
15// For compilers that support precompilation, includes "wx.h".
16#include "wx/wxprec.h"
17
18#ifdef __BORLANDC__
19# pragma hdrstop
20#endif
21
22#ifndef WX_PRECOMP
7be110e3 23# include "wx/defs.h"
464122b6
JS
24#endif
25
7be110e3 26#if wxUSE_STREAMS && wxUSE_GIF
464122b6
JS
27
28#include <stdlib.h>
29#include <string.h>
464122b6 30#include "wx/gifdecod.h"
2ce0a6e2 31
464122b6
JS
32
33//---------------------------------------------------------------------------
34// wxGIFDecoder constructor and destructor
35//---------------------------------------------------------------------------
36
37wxGIFDecoder::wxGIFDecoder(wxInputStream *s, bool anim)
38{
9742d3cc 39 m_f = s;
464122b6
JS
40 m_anim = anim;
41
42 m_background = -1;
43 m_screenw = 0;
44 m_screenh = 0;
45
46 m_pimage = NULL;
47 m_pfirst = NULL;
48 m_plast = NULL;
49 m_image = 0;
50 m_nimages = 0;
51}
52
53wxGIFDecoder::~wxGIFDecoder()
54{
55 Destroy();
56}
57
58void wxGIFDecoder::Destroy()
59{
e4b8154a 60 GIFImage *pimg, *paux;
464122b6
JS
61
62 pimg = m_pfirst;
63
64 while (pimg != NULL)
65 {
66 paux = pimg->next;
67 free(pimg->p);
68 free(pimg->pal);
e4b8154a 69 delete pimg;
464122b6
JS
70 pimg = paux;
71 }
9742d3cc
GRG
72
73 m_pimage = NULL;
74 m_pfirst = NULL;
75 m_plast = NULL;
76 m_image = 0;
77 m_nimages = 0;
464122b6
JS
78}
79
80
81//---------------------------------------------------------------------------
82// Convert this image to a wxImage object
83//---------------------------------------------------------------------------
84
85// This function was designed by Vaclav Slavik
86
87bool wxGIFDecoder::ConvertToImage(wxImage *image) const
88{
89 unsigned char *src, *dst, *pal;
90 unsigned long i;
91 int transparent;
92
3c87527e
GRG
93 /* just in case... */
94 image->Destroy();
95
464122b6
JS
96 /* create the image */
97 image->Create(GetWidth(), GetHeight());
98
99 if (!image->Ok())
100 return FALSE;
101
102 pal = GetPalette();
103 src = GetData();
104 dst = image->GetData();
105 transparent = GetTransparentColour();
106
107 /* set transparent colour mask */
108 if (transparent != -1)
109 {
110 for (i = 0; i < 256; i++)
111 {
112 if ((pal[3 * i + 0] == 255) &&
113 (pal[3 * i + 1] == 0) &&
114 (pal[3 * i + 2] == 255))
115 {
116 pal[3 * i + 2] = 254;
117 }
118 }
119
120 pal[3 * transparent + 0] = 255,
121 pal[3 * transparent + 1] = 0,
122 pal[3 * transparent + 2] = 255;
123
124 image->SetMaskColour(255, 0, 255);
125 }
126 else
127 image->SetMask(FALSE);
128
3f4fc796
JS
129 // Set the palette
130 if (pal)
131 {
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++)
136 {
137 r[i] = pal[3*i + 0];
138 g[i] = pal[3*i + 1];
139 b[i] = pal[3*i + 2];
140 }
141 image->SetPalette(wxPalette(256, r, g, b));
142 delete[] r; delete[] g; delete[] b;
143 }
144
464122b6
JS
145 /* copy image data */
146 for (i = 0; i < (GetWidth() * GetHeight()); i++, src++)
147 {
148 *(dst++) = pal[3 * (*src) + 0];
149 *(dst++) = pal[3 * (*src) + 1];
150 *(dst++) = pal[3 * (*src) + 2];
151 }
152
153 return TRUE;
154}
155
2ce0a6e2 156
464122b6
JS
157//---------------------------------------------------------------------------
158// Data accessors
159//---------------------------------------------------------------------------
160
161// Get data for current frame
162
163int wxGIFDecoder::GetFrameIndex() const { return m_image; }
164unsigned char* wxGIFDecoder::GetData() const { return (m_pimage->p); }
165unsigned char* wxGIFDecoder::GetPalette() const { return (m_pimage->pal); }
166unsigned int wxGIFDecoder::GetWidth() const { return (m_pimage->w); }
167unsigned int wxGIFDecoder::GetHeight() const { return (m_pimage->h); }
168unsigned int wxGIFDecoder::GetTop() const { return (m_pimage->top); }
169unsigned int wxGIFDecoder::GetLeft() const { return (m_pimage->left); }
170int wxGIFDecoder::GetTransparentColour() const { return (m_pimage->transparent); }
171int wxGIFDecoder::GetDisposalMethod() const { return (m_pimage->disposal); }
172long wxGIFDecoder::GetDelay() const { return (m_pimage->delay); }
173
174// Get global data
175
176unsigned int wxGIFDecoder::GetLogicalScreenWidth() const { return m_screenw; }
177unsigned int wxGIFDecoder::GetLogicalScreenHeight() const { return m_screenh; }
178int wxGIFDecoder::GetBackgroundColour() const { return m_background; }
179int wxGIFDecoder::GetNumberOfFrames() const { return m_nimages; }
180bool wxGIFDecoder::IsAnimation() const { return (m_nimages > 1); }
181
182
183//---------------------------------------------------------------------------
184// Functions to move through the animation
185//---------------------------------------------------------------------------
186
187bool wxGIFDecoder::GoFirstFrame()
188{
189 if (!IsAnimation())
190 return FALSE;
191
192 m_image = 1;
193 m_pimage = m_pfirst;
194 return TRUE;
195}
196
197bool wxGIFDecoder::GoLastFrame()
198{
199 if (!IsAnimation())
200 return FALSE;
201
202 m_image = m_nimages;
203 m_pimage = m_plast;
204 return TRUE;
205}
206
207bool wxGIFDecoder::GoNextFrame(bool cyclic)
208{
209 if (!IsAnimation())
210 return FALSE;
211
212 if ((m_image < m_nimages) || (cyclic))
213 {
214 m_pimage = m_pimage->next;
215 m_image++;
216
217 if (!m_pimage)
218 {
219 m_image = 1;
220 m_pimage = m_pfirst;
221 }
222
223 return TRUE;
224 }
225 else
226 return FALSE;
2ce0a6e2 227}
464122b6
JS
228
229bool wxGIFDecoder::GoPrevFrame(bool cyclic)
230{
231 if (!IsAnimation())
232 return FALSE;
233
234 if ((m_image > 1) || (cyclic))
235 {
236 m_pimage = m_pimage->prev;
237 m_image--;
238
239 if (!m_pimage)
240 {
241 m_image = m_nimages;
242 m_pimage = m_plast;
243 }
244
245 return TRUE;
246 }
247 else
248 return FALSE;
249}
250
251bool wxGIFDecoder::GoFrame(int which)
252{
253 int i;
254
255 if (!IsAnimation())
256 return FALSE;
257
258 if ((which >= 1) && (which <= m_nimages))
259 {
260 m_pimage = m_pfirst;
261
262 for (i = 1; i < which; i++)
263 m_pimage = m_pimage->next;
264
265 return TRUE;
266 }
267 else
268 return FALSE;
269}
270
271
272//---------------------------------------------------------------------------
273// GIF reading and decoding
274//---------------------------------------------------------------------------
275
276// getcode:
277// Reads the next code from the file stream, with size 'bits'
278//
279int wxGIFDecoder::getcode(int bits, int ab_fin)
280{
281 unsigned int mask; /* bit mask */
282 unsigned int code; /* code (result) */
283
284
285 /* get remaining bits from last byte read */
286 mask = (1 << bits) - 1;
287 code = (m_lastbyte >> (8 - m_restbits)) & mask;
288
289 /* keep reading new bytes while needed */
290 while (bits > m_restbits)
291 {
292 /* if no bytes left in this block, read the next block */
293 if (m_restbyte == 0)
294 {
295 m_restbyte = (unsigned char)m_f->GetC();
296
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.
301 */
302 if (m_restbyte == 0)
303 {
304 code = ab_fin;
305 break;
306 }
8708a10f
GRG
307
308 /* prefetch data */
309 m_f->Read((void *) m_buffer, m_restbyte);
310 m_bufp = m_buffer;
464122b6
JS
311 }
312
313 /* read next byte and isolate the bits we need */
8708a10f 314 m_lastbyte = (unsigned char) (*m_bufp++);
464122b6
JS
315 mask = (1 << (bits - m_restbits)) - 1;
316 code = code + ((m_lastbyte & mask) << m_restbits);
317 m_restbyte--;
318
319 /* adjust total number of bits extracted from the buffer */
320 m_restbits = m_restbits + 8;
321 }
2ce0a6e2 322
464122b6
JS
323 /* find number of bits remaining for next code */
324 m_restbits = (m_restbits - bits);
325
326 return code;
327}
328
329
330// dgif:
331// GIF decoding function. The initial code size (aka root size)
332// is 'bits'. Supports interlaced images (interl == 1).
333//
e4b8154a 334int wxGIFDecoder::dgif(GIFImage *img, int interl, int bits)
464122b6 335{
7d610b90
SC
336#ifdef __WXMAC__
337 int *ab_prefix = new int[4096]; /* alphabet (prefixes) */
338 int *ab_tail = new int[4096]; /* alphabet (tails) */
339 int *stack = new int[4096]; /* decompression stack */
340#else
464122b6
JS
341 int ab_prefix[4096]; /* alphabet (prefixes) */
342 int ab_tail[4096]; /* alphabet (tails) */
343 int stack[4096]; /* decompression stack */
7d610b90 344#endif
464122b6
JS
345 int ab_clr; /* clear code */
346 int ab_fin; /* end of info code */
347 int ab_bits; /* actual symbol width, in bits */
348 int ab_free; /* first free position in alphabet */
349 int ab_max; /* last possible character in alphabet */
350 int pass; /* pass number in interlaced images */
351 int pos; /* index into decompresion stack */
352 unsigned int x, y; /* position in image buffer */
353
354 int code, readcode, lastcode, abcabca;
355
356 /* these won't change */
357 ab_clr = (1 << bits);
358 ab_fin = (1 << bits) + 1;
359
360 /* these will change through the decompression proccess */
361 ab_bits = bits + 1;
362 ab_free = (1 << bits) + 2;
363 ab_max = (1 << ab_bits) - 1;
364 lastcode = -1;
365 abcabca = -1;
366 pass = 1;
367 pos = x = y = 0;
368
8708a10f 369 /* reset decoder vars */
464122b6
JS
370 m_restbits = 0;
371 m_restbyte = 0;
372 m_lastbyte = 0;
373
374 do
375 {
376 /* get next code */
377 readcode = code = getcode(ab_bits, ab_fin);
378
379 /* end of image? */
380 if (code == ab_fin) break;
381
382 /* reset alphabet? */
383 if (code == ab_clr)
384 {
385 /* reset main variables */
386 ab_bits = bits + 1;
387 ab_free = (1 << bits) + 2;
388 ab_max = (1 << ab_bits) - 1;
389 lastcode = -1;
390 abcabca = -1;
391
392 /* skip to next code */
393 continue;
394 }
395
396 /* unknown code: special case (like in ABCABCA) */
397 if (code >= ab_free)
398 {
399 code = lastcode; /* take last string */
400 stack[pos++] = abcabca; /* add first character */
401 }
402
403 /* build the string for this code in the stack */
404 while (code > ab_clr)
405 {
406 stack[pos++] = ab_tail[code];
407 code = ab_prefix[code];
408 }
409 stack[pos] = code; /* push last code into the stack */
410 abcabca = code; /* save for special case */
411
412 /* make new entry in alphabet (only if NOT just cleared) */
413 if (lastcode != -1)
414 {
415 ab_prefix[ab_free] = lastcode;
416 ab_tail[ab_free] = code;
417 ab_free++;
418
419 if ((ab_free > ab_max) && (ab_bits < 12))
420 {
421 ab_bits++;
422 ab_max = (1 << ab_bits) - 1;
423 }
424 }
425
426 /* dump stack data to the buffer */
427 while (pos >= 0)
428 {
429 (img->p)[x + (y * (img->w))] = (char)stack[pos--];
430
431 if (++x >= (img->w))
432 {
433 x = 0;
434
435 if (interl)
436 {
437 /* support for interlaced images */
438 switch (pass)
439 {
440 case 1: y += 8; break;
441 case 2: y += 8; break;
442 case 3: y += 4; break;
443 case 4: y += 2; break;
444 }
445 if (y >= (img->h))
446 {
447 switch (++pass)
448 {
449 case 2: y = 4; break;
450 case 3: y = 2; break;
451 case 4: y = 1; break;
452 }
453 }
454 }
455 else
456 {
457 /* non-interlaced */
458 y++;
459 }
460 }
461 }
462
463 pos = 0;
464 lastcode = readcode;
465 }
466 while (code != ab_fin);
467
7d610b90
SC
468#ifdef __WXMAC__
469 delete [] ab_prefix ;
470 delete [] ab_tail ;
471 delete [] stack ;
472#endif
464122b6
JS
473 return 0;
474}
475
476
3c87527e
GRG
477// CanRead:
478// Returns TRUE if the file looks like a valid GIF, FALSE otherwise.
479//
480bool wxGIFDecoder::CanRead()
481{
482 unsigned char buf[3];
483
3c87527e 484 m_f->Read(buf, 3);
700ec454 485 m_f->SeekI(-3, wxFromCurrent);
3c87527e
GRG
486
487 return (memcmp(buf, "GIF", 3) == 0);
488}
489
490
464122b6
JS
491// ReadGIF:
492// Reads and decodes one or more GIF images, depending on whether
493// animated GIF support is enabled. Can read GIFs with any bit
494// size (color depth), but the output images are always expanded
495// to 8 bits per pixel. Also, the image palettes always contain
8141573c 496// 256 colors, although some of them may be unused. Returns wxGIF_OK
e4b8154a
GRG
497// (== 0) on success, or an error code if something fails (see
498// header file for details)
464122b6
JS
499//
500int wxGIFDecoder::ReadGIF()
501{
502 int ncolors, bits, interl, transparent, disposal, i;
503 long size;
504 long delay;
3ca6a5f0 505 unsigned char type = 0;
464122b6
JS
506 unsigned char pal[768];
507 unsigned char buf[16];
e4b8154a 508 GIFImage **ppimg, *pimg, *pprev;
464122b6 509
3c87527e
GRG
510 /* check GIF signature */
511 if (!CanRead())
e4b8154a 512 return wxGIF_INVFORMAT;
464122b6 513
8141573c 514 /* check for animated GIF support (ver. >= 89a) */
464122b6
JS
515 m_f->Read(buf, 6);
516
464122b6
JS
517 if (memcmp(buf + 3, "89a", 3) < 0)
518 m_anim = FALSE;
519
520 /* read logical screen descriptor block (LSDB) */
521 m_f->Read(buf, 7);
522 m_screenw = buf[0] + 256 * buf[1];
523 m_screenh = buf[2] + 256 * buf[3];
524
525 /* load global color map if available */
526 if ((buf[4] & 0x80) == 0x80)
527 {
528 m_background = buf[5];
529
530 ncolors = 2 << (buf[4] & 0x07);
531 m_f->Read(pal, 3 * ncolors);
532 }
533
534 /* transparent colour, disposal method and delay default to unused */
535 transparent = -1;
536 disposal = -1;
537 delay = -1;
538
539 /* read images */
540 ppimg = &m_pfirst;
541 pprev = NULL;
542 pimg = NULL;
543
8141573c
GRG
544 bool done = FALSE;
545
546 while(!done)
464122b6
JS
547 {
548 type = (unsigned char)m_f->GetC();
549
550 /* end of data? */
551 if (type == 0x3B)
8141573c
GRG
552 {
553 done = TRUE;
554 }
555 else
464122b6
JS
556 /* extension block? */
557 if (type == 0x21)
558 {
559 if (((unsigned char)m_f->GetC()) == 0xF9)
560 /* graphics control extension, parse it */
561 {
562 m_f->Read(buf, 6);
563
564 /* read delay and convert from 1/100 of a second to ms */
565 delay = 10 * (buf[2] + 256 * buf[3]);
566
567 /* read transparent colour index, if used */
568 if (buf[1] & 0x01)
569 transparent = buf[4];
570
571 /* read disposal method */
572 disposal = (buf[1] & 0x1C) - 1;
573 }
574 else
575 /* other extension, skip */
2ce0a6e2 576 {
464122b6
JS
577 while ((i = (unsigned char)m_f->GetC()) != 0)
578 {
464122b6
JS
579 m_f->SeekI(i, wxFromCurrent);
580 }
581 }
582 }
8141573c 583 else
464122b6
JS
584 /* image descriptor block? */
585 if (type == 0x2C)
586 {
587 /* allocate memory for IMAGEN struct */
e4b8154a 588 pimg = (*ppimg) = new GIFImage();
464122b6
JS
589
590 if (pimg == NULL)
591 {
592 Destroy();
e4b8154a 593 return wxGIF_MEMERR;
464122b6
JS
594 }
595
596 /* fill in the data */
597 m_f->Read(buf, 9);
598 pimg->left = buf[4] + 256 * buf[5];
599 pimg->top = buf[4] + 256 * buf[5];
600 pimg->w = buf[4] + 256 * buf[5];
601 pimg->h = buf[6] + 256 * buf[7];
602 interl = ((buf[8] & 0x40)? 1 : 0);
603 size = pimg->w * pimg->h;
604
605 pimg->transparent = transparent;
606 pimg->disposal = disposal;
607 pimg->delay = delay;
608 pimg->next = NULL;
609 pimg->prev = pprev;
610 pprev = pimg;
611 ppimg = &pimg->next;
612
613 /* allocate memory for image and palette */
13111b2a 614 pimg->p = (unsigned char *) malloc((size_t)size);
464122b6
JS
615 pimg->pal = (unsigned char *) malloc(768);
616
617 if ((!pimg->p) || (!pimg->pal))
618 {
619 Destroy();
e4b8154a 620 return wxGIF_MEMERR;
464122b6
JS
621 }
622
623 /* load local color map if available, else use global map */
624 if ((buf[8] & 0x80) == 0x80)
625 {
626 ncolors = 2 << (buf[8] & 0x07);
627 m_f->Read(pimg->pal, 3 * ncolors);
628 }
629 else
630 memcpy(pimg->pal, pal, 768);
631
632 /* get initial code size from first byte in raster data */
633 bits = (unsigned char)m_f->GetC();
634
635 /* decode image */
636 dgif(pimg, interl, bits);
464122b6 637 m_nimages++;
464122b6 638
8141573c
GRG
639 /* if this is not an animated GIF, exit after first image */
640 if (!m_anim)
641 done = TRUE;
642 }
464122b6
JS
643 }
644
8141573c 645 /* setup image pointers */
464122b6
JS
646 if (m_nimages != 0)
647 {
648 m_image = 1;
649 m_plast = pimg;
650 m_pimage = m_pfirst;
651 }
652
8141573c
GRG
653 /* try to read to the end of the stream */
654 while (type != 0x3B)
655 {
656 type = (unsigned char)m_f->GetC();
657
658 if (type == 0x21)
659 {
660 /* extension type */
661 (void) m_f->GetC();
662
663 /* skip all data */
664 while ((i = (unsigned char)m_f->GetC()) != 0)
665 {
666 m_f->SeekI(i, wxFromCurrent);
667 }
668 }
669 else if (type == 0x2C)
670 {
671 /* image descriptor block */
672 m_f->Read(buf, 9);
673
674 /* local color map */
675 if ((buf[8] & 0x80) == 0x80)
676 {
677 ncolors = 2 << (buf[8] & 0x07);
9742d3cc 678 m_f->SeekI(3 * ncolors, wxFromCurrent);
8141573c
GRG
679 }
680
681 /* initial code size */
682 (void) m_f->GetC();
683
684 /* skip all data */
685 while ((i = (unsigned char)m_f->GetC()) != 0)
686 {
687 m_f->SeekI(i, wxFromCurrent);
688 }
689 }
9742d3cc 690 else if ((type != 0x3B) && (type != 00)) /* testing */
8141573c
GRG
691 {
692 /* images are OK, but couldn't read to the end of the stream */
693 return wxGIF_TRUNCATED;
694 }
695 }
696
e4b8154a 697 return wxGIF_OK;
464122b6
JS
698}
699
7be110e3 700#endif // wxUSE_STREAMS && wxUSE_GIF