]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/common/gifdecod.cpp
fixed indentation and warnings/bugs
[wxWidgets.git] / src / common / gifdecod.cpp
... / ...
CommitLineData
1/////////////////////////////////////////////////////////////////////////////
2// Name: gifdecod.cpp
3// Purpose: wxGIFDecoder, GIF reader for wxImage and wxAnimation
4// Author: Guillermo Rodriguez Garcia <guille@iies.es>
5// Version: 3.04
6// RCS-ID: $Id$
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
23# include "wx/defs.h"
24#endif
25
26#if wxUSE_STREAMS && wxUSE_GIF
27
28#include <stdlib.h>
29#include <string.h>
30#include "wx/gifdecod.h"
31
32
33//---------------------------------------------------------------------------
34// wxGIFDecoder constructor and destructor
35//---------------------------------------------------------------------------
36
37wxGIFDecoder::wxGIFDecoder(wxInputStream *s, bool anim)
38{
39 m_f = s;
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{
60 GIFImage *pimg, *paux;
61
62 pimg = m_pfirst;
63
64 while (pimg != NULL)
65 {
66 paux = pimg->next;
67 free(pimg->p);
68 free(pimg->pal);
69 delete pimg;
70 pimg = paux;
71 }
72
73 m_pimage = NULL;
74 m_pfirst = NULL;
75 m_plast = NULL;
76 m_image = 0;
77 m_nimages = 0;
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
93 /* just in case... */
94 image->Destroy();
95
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
129 if (pal)
130 {
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++)
135 {
136 r[i] = pal[3*i + 0];
137 g[i] = pal[3*i + 1];
138 b[i] = pal[3*i + 2];
139 }
140 image->SetPalette(wxPalette(256, r, g, b));
141 delete[] r; delete[] g; delete[] b;
142 }
143
144 /* copy image data */
145 for (i = 0; i < (GetWidth() * GetHeight()); i++, src++)
146 {
147 *(dst++) = pal[3 * (*src) + 0];
148 *(dst++) = pal[3 * (*src) + 1];
149 *(dst++) = pal[3 * (*src) + 2];
150 }
151
152 return TRUE;
153}
154
155
156//---------------------------------------------------------------------------
157// Data accessors
158//---------------------------------------------------------------------------
159
160// Get data for current frame
161
162int wxGIFDecoder::GetFrameIndex() const { return m_image; }
163unsigned char* wxGIFDecoder::GetData() const { return (m_pimage->p); }
164unsigned char* wxGIFDecoder::GetPalette() const { return (m_pimage->pal); }
165unsigned int wxGIFDecoder::GetWidth() const { return (m_pimage->w); }
166unsigned int wxGIFDecoder::GetHeight() const { return (m_pimage->h); }
167unsigned int wxGIFDecoder::GetTop() const { return (m_pimage->top); }
168unsigned int wxGIFDecoder::GetLeft() const { return (m_pimage->left); }
169int wxGIFDecoder::GetTransparentColour() const { return (m_pimage->transparent); }
170int wxGIFDecoder::GetDisposalMethod() const { return (m_pimage->disposal); }
171long wxGIFDecoder::GetDelay() const { return (m_pimage->delay); }
172
173// Get global data
174
175unsigned int wxGIFDecoder::GetLogicalScreenWidth() const { return m_screenw; }
176unsigned int wxGIFDecoder::GetLogicalScreenHeight() const { return m_screenh; }
177int wxGIFDecoder::GetBackgroundColour() const { return m_background; }
178int wxGIFDecoder::GetNumberOfFrames() const { return m_nimages; }
179bool wxGIFDecoder::IsAnimation() const { return (m_nimages > 1); }
180
181
182//---------------------------------------------------------------------------
183// Functions to move through the animation
184//---------------------------------------------------------------------------
185
186bool wxGIFDecoder::GoFirstFrame()
187{
188 if (!IsAnimation())
189 return FALSE;
190
191 m_image = 1;
192 m_pimage = m_pfirst;
193 return TRUE;
194}
195
196bool wxGIFDecoder::GoLastFrame()
197{
198 if (!IsAnimation())
199 return FALSE;
200
201 m_image = m_nimages;
202 m_pimage = m_plast;
203 return TRUE;
204}
205
206bool wxGIFDecoder::GoNextFrame(bool cyclic)
207{
208 if (!IsAnimation())
209 return FALSE;
210
211 if ((m_image < m_nimages) || (cyclic))
212 {
213 m_pimage = m_pimage->next;
214 m_image++;
215
216 if (!m_pimage)
217 {
218 m_image = 1;
219 m_pimage = m_pfirst;
220 }
221
222 return TRUE;
223 }
224 else
225 return FALSE;
226}
227
228bool wxGIFDecoder::GoPrevFrame(bool cyclic)
229{
230 if (!IsAnimation())
231 return FALSE;
232
233 if ((m_image > 1) || (cyclic))
234 {
235 m_pimage = m_pimage->prev;
236 m_image--;
237
238 if (!m_pimage)
239 {
240 m_image = m_nimages;
241 m_pimage = m_plast;
242 }
243
244 return TRUE;
245 }
246 else
247 return FALSE;
248}
249
250bool wxGIFDecoder::GoFrame(int which)
251{
252 int i;
253
254 if (!IsAnimation())
255 return FALSE;
256
257 if ((which >= 1) && (which <= m_nimages))
258 {
259 m_pimage = m_pfirst;
260
261 for (i = 1; i < which; i++)
262 m_pimage = m_pimage->next;
263
264 return TRUE;
265 }
266 else
267 return FALSE;
268}
269
270
271//---------------------------------------------------------------------------
272// GIF reading and decoding
273//---------------------------------------------------------------------------
274
275// getcode:
276// Reads the next code from the file stream, with size 'bits'
277//
278int wxGIFDecoder::getcode(int bits, int ab_fin)
279{
280 unsigned int mask; /* bit mask */
281 unsigned int code; /* code (result) */
282
283
284 /* get remaining bits from last byte read */
285 mask = (1 << bits) - 1;
286 code = (m_lastbyte >> (8 - m_restbits)) & mask;
287
288 /* keep reading new bytes while needed */
289 while (bits > m_restbits)
290 {
291 /* if no bytes left in this block, read the next block */
292 if (m_restbyte == 0)
293 {
294 m_restbyte = (unsigned char)m_f->GetC();
295
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.
300 */
301 if (m_restbyte == 0)
302 {
303 code = ab_fin;
304 break;
305 }
306
307 /* prefetch data */
308 m_f->Read((void *) m_buffer, m_restbyte);
309 m_bufp = m_buffer;
310 }
311
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);
316 m_restbyte--;
317
318 /* adjust total number of bits extracted from the buffer */
319 m_restbits = m_restbits + 8;
320 }
321
322 /* find number of bits remaining for next code */
323 m_restbits = (m_restbits - bits);
324
325 return code;
326}
327
328
329// dgif:
330// GIF decoding function. The initial code size (aka root size)
331// is 'bits'. Supports interlaced images (interl == 1).
332//
333int wxGIFDecoder::dgif(GIFImage *img, int interl, int bits)
334{
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 */
346
347 int code, readcode, lastcode, abcabca;
348
349 /* these won't change */
350 ab_clr = (1 << bits);
351 ab_fin = (1 << bits) + 1;
352
353 /* these will change through the decompression proccess */
354 ab_bits = bits + 1;
355 ab_free = (1 << bits) + 2;
356 ab_max = (1 << ab_bits) - 1;
357 lastcode = -1;
358 abcabca = -1;
359 pass = 1;
360 pos = x = y = 0;
361
362 /* reset decoder vars */
363 m_restbits = 0;
364 m_restbyte = 0;
365 m_lastbyte = 0;
366
367 do
368 {
369 /* get next code */
370 readcode = code = getcode(ab_bits, ab_fin);
371
372 /* end of image? */
373 if (code == ab_fin) break;
374
375 /* reset alphabet? */
376 if (code == ab_clr)
377 {
378 /* reset main variables */
379 ab_bits = bits + 1;
380 ab_free = (1 << bits) + 2;
381 ab_max = (1 << ab_bits) - 1;
382 lastcode = -1;
383 abcabca = -1;
384
385 /* skip to next code */
386 continue;
387 }
388
389 /* unknown code: special case (like in ABCABCA) */
390 if (code >= ab_free)
391 {
392 code = lastcode; /* take last string */
393 stack[pos++] = abcabca; /* add first character */
394 }
395
396 /* build the string for this code in the stack */
397 while (code > ab_clr)
398 {
399 stack[pos++] = ab_tail[code];
400 code = ab_prefix[code];
401 }
402 stack[pos] = code; /* push last code into the stack */
403 abcabca = code; /* save for special case */
404
405 /* make new entry in alphabet (only if NOT just cleared) */
406 if (lastcode != -1)
407 {
408 ab_prefix[ab_free] = lastcode;
409 ab_tail[ab_free] = code;
410 ab_free++;
411
412 if ((ab_free > ab_max) && (ab_bits < 12))
413 {
414 ab_bits++;
415 ab_max = (1 << ab_bits) - 1;
416 }
417 }
418
419 /* dump stack data to the buffer */
420 while (pos >= 0)
421 {
422 (img->p)[x + (y * (img->w))] = (char)stack[pos--];
423
424 if (++x >= (img->w))
425 {
426 x = 0;
427
428 if (interl)
429 {
430 /* support for interlaced images */
431 switch (pass)
432 {
433 case 1: y += 8; break;
434 case 2: y += 8; break;
435 case 3: y += 4; break;
436 case 4: y += 2; break;
437 }
438 if (y >= (img->h))
439 {
440 switch (++pass)
441 {
442 case 2: y = 4; break;
443 case 3: y = 2; break;
444 case 4: y = 1; break;
445 }
446 }
447 }
448 else
449 {
450 /* non-interlaced */
451 y++;
452 }
453 }
454 }
455
456 pos = 0;
457 lastcode = readcode;
458 }
459 while (code != ab_fin);
460
461 delete [] ab_prefix ;
462 delete [] ab_tail ;
463 delete [] stack ;
464
465 return 0;
466}
467
468
469// CanRead:
470// Returns TRUE if the file looks like a valid GIF, FALSE otherwise.
471//
472bool wxGIFDecoder::CanRead()
473{
474 unsigned char buf[3];
475
476 m_f->Read(buf, 3);
477 m_f->SeekI(-3, wxFromCurrent);
478
479 return (memcmp(buf, "GIF", 3) == 0);
480}
481
482
483// ReadGIF:
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)
491//
492int wxGIFDecoder::ReadGIF()
493{
494 int ncolors, bits, interl, transparent, disposal, i;
495 long size;
496 long delay;
497 unsigned char type = 0;
498 unsigned char pal[768];
499 unsigned char buf[16];
500 GIFImage **ppimg;
501 GIFImage *pimg, *pprev;
502
503 /* check GIF signature */
504 if (!CanRead())
505 return wxGIF_INVFORMAT;
506
507 /* check for animated GIF support (ver. >= 89a) */
508 m_f->Read(buf, 6);
509
510 if (memcmp(buf + 3, "89a", 3) < 0)
511 m_anim = FALSE;
512
513 /* read logical screen descriptor block (LSDB) */
514 m_f->Read(buf, 7);
515 m_screenw = buf[0] + 256 * buf[1];
516 m_screenh = buf[2] + 256 * buf[3];
517
518 /* load global color map if available */
519 if ((buf[4] & 0x80) == 0x80)
520 {
521 m_background = buf[5];
522
523 ncolors = 2 << (buf[4] & 0x07);
524 m_f->Read(pal, 3 * ncolors);
525 }
526
527 /* transparent colour, disposal method and delay default to unused */
528 transparent = -1;
529 disposal = -1;
530 delay = -1;
531
532 /* read images */
533 ppimg = &m_pfirst;
534 pprev = NULL;
535 pimg = NULL;
536
537 bool done = FALSE;
538
539 while(!done)
540 {
541 type = (unsigned char)m_f->GetC();
542
543 /* end of data? */
544 if (type == 0x3B)
545 {
546 done = TRUE;
547 }
548 else
549 /* extension block? */
550 if (type == 0x21)
551 {
552 if (((unsigned char)m_f->GetC()) == 0xF9)
553 /* graphics control extension, parse it */
554 {
555 m_f->Read(buf, 6);
556
557 /* read delay and convert from 1/100 of a second to ms */
558 delay = 10 * (buf[2] + 256 * buf[3]);
559
560 /* read transparent colour index, if used */
561 if (buf[1] & 0x01)
562 transparent = buf[4];
563
564 /* read disposal method */
565 disposal = (buf[1] & 0x1C) - 1;
566 }
567 else
568 /* other extension, skip */
569 {
570 while ((i = (unsigned char)m_f->GetC()) != 0)
571 {
572 m_f->SeekI(i, wxFromCurrent);
573 }
574 }
575 }
576 else
577 /* image descriptor block? */
578 if (type == 0x2C)
579 {
580 /* allocate memory for IMAGEN struct */
581 pimg = (*ppimg) = new GIFImage();
582
583 if (pimg == NULL)
584 {
585 Destroy();
586 return wxGIF_MEMERR;
587 }
588
589 /* fill in the data */
590 m_f->Read(buf, 9);
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;
597
598 pimg->transparent = transparent;
599 pimg->disposal = disposal;
600 pimg->delay = delay;
601 pimg->next = NULL;
602 pimg->prev = pprev;
603 pprev = pimg;
604 ppimg = &pimg->next;
605
606 /* allocate memory for image and palette */
607 pimg->p = (unsigned char *) malloc((size_t)size);
608 pimg->pal = (unsigned char *) malloc(768);
609
610 if ((!pimg->p) || (!pimg->pal))
611 {
612 Destroy();
613 return wxGIF_MEMERR;
614 }
615
616 /* load local color map if available, else use global map */
617 if ((buf[8] & 0x80) == 0x80)
618 {
619 ncolors = 2 << (buf[8] & 0x07);
620 m_f->Read(pimg->pal, 3 * ncolors);
621 }
622 else
623 memcpy(pimg->pal, pal, 768);
624
625 /* get initial code size from first byte in raster data */
626 bits = (unsigned char)m_f->GetC();
627
628 /* decode image */
629 dgif(pimg, interl, bits);
630 m_nimages++;
631
632 /* if this is not an animated GIF, exit after first image */
633 if (!m_anim)
634 done = TRUE;
635 }
636 }
637
638 /* setup image pointers */
639 if (m_nimages != 0)
640 {
641 m_image = 1;
642 m_plast = pimg;
643 m_pimage = m_pfirst;
644 }
645
646 /* try to read to the end of the stream */
647 while (type != 0x3B)
648 {
649 type = (unsigned char)m_f->GetC();
650
651 if (type == 0x21)
652 {
653 /* extension type */
654 (void) m_f->GetC();
655
656 /* skip all data */
657 while ((i = (unsigned char)m_f->GetC()) != 0)
658 {
659 m_f->SeekI(i, wxFromCurrent);
660 }
661 }
662 else if (type == 0x2C)
663 {
664 /* image descriptor block */
665 m_f->Read(buf, 9);
666
667 /* local color map */
668 if ((buf[8] & 0x80) == 0x80)
669 {
670 ncolors = 2 << (buf[8] & 0x07);
671 m_f->SeekI(3 * ncolors, wxFromCurrent);
672 }
673
674 /* initial code size */
675 (void) m_f->GetC();
676
677 /* skip all data */
678 while ((i = (unsigned char)m_f->GetC()) != 0)
679 {
680 m_f->SeekI(i, wxFromCurrent);
681 }
682 }
683 else if ((type != 0x3B) && (type != 00)) /* testing */
684 {
685 /* images are OK, but couldn't read to the end of the stream */
686 return wxGIF_TRUNCATED;
687 }
688 }
689
690 return wxGIF_OK;
691}
692
693#endif // wxUSE_STREAMS && wxUSE_GIF