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