]> git.saurik.com Git - wxWidgets.git/blame - src/common/imagiff.cpp
Upgrade bundled zlib to 1.2.8.
[wxWidgets.git] / src / common / imagiff.cpp
CommitLineData
63f8abca 1/////////////////////////////////////////////////////////////////////////////
80fdcdb9 2// Name: src/common/imagiff.cpp
63f8abca 3// Purpose: wxImage handler for Amiga IFF images
1811af87 4// Author: Steffen Gutmann, Thomas Meyer
63f8abca 5// Copyright: (c) Steffen Gutmann, 2002
65571936 6// Licence: wxWindows licence
63f8abca
VS
7/////////////////////////////////////////////////////////////////////////////
8
1811af87
VS
9// Parts of this source are based on the iff loading algorithm found
10// in xviff.c. Permission by the original author, Thomas Meyer, and
11// by the author of xv, John Bradley for using the iff loading part
77ffb593 12// in wxWidgets has been gratefully given.
4b6b4dfc 13
4b6b4dfc
RR
14// For compilers that support precompilation, includes "wx.h".
15#include "wx/wxprec.h"
16
17#ifdef __BORLANDC__
8898456d 18 #pragma hdrstop
4b6b4dfc
RR
19#endif
20
8898456d
WS
21#if wxUSE_IMAGE && wxUSE_IFF
22
4b6b4dfc 23#ifndef WX_PRECOMP
8898456d
WS
24 #include "wx/log.h"
25 #include "wx/intl.h"
4b6b4dfc
RR
26#endif
27
28#include "wx/imagiff.h"
4b6b4dfc 29#include "wx/wfstream.h"
4b6b4dfc 30
44c2d8d0
VZ
31#if wxUSE_PALETTE
32 #include "wx/palette.h"
33#endif // wxUSE_PALETTE
34
63f8abca
VS
35#include <stdlib.h>
36#include <string.h>
37
38
39// --------------------------------------------------------------------------
40// Constants
41// --------------------------------------------------------------------------
42
43// Error codes:
44// Note that the error code wxIFF_TRUNCATED means that the image itself
45// is most probably OK, but the decoder didn't reach the end of the data
46// stream; this means that if it was not reading directly from file,
47// the stream will not be correctly positioned.
48//
49
50enum
51{
52 wxIFF_OK = 0, /* everything was OK */
53 wxIFF_INVFORMAT, /* error in iff header */
54 wxIFF_MEMERR, /* error allocating memory */
55 wxIFF_TRUNCATED /* file appears to be truncated */
56};
57
58// --------------------------------------------------------------------------
59// wxIFFDecoder class
60// --------------------------------------------------------------------------
61
62// internal class for storing IFF image data
63class IFFImage
64{
65public:
66 unsigned int w; /* width */
67 unsigned int h; /* height */
68 int transparent; /* transparent color (-1 = none) */
7beb59f3 69 int colors; /* number of colors */
63f8abca
VS
70 unsigned char *p; /* bitmap */
71 unsigned char *pal; /* palette */
72
73 IFFImage() : w(0), h(0), colors(0), p(0), pal(0) {}
74 ~IFFImage() { delete [] p; delete [] pal; }
75};
76
77class WXDLLEXPORT wxIFFDecoder
78{
79private:
7beb59f3
WS
80 IFFImage *m_image; // image data
81 wxInputStream *m_f; // input stream
19f1a09a 82 unsigned char *databuf;
63f8abca
VS
83 unsigned char *decomp_mem;
84
85 void Destroy();
86
87public:
88 // get data of current frame
89 unsigned char* GetData() const;
90 unsigned char* GetPalette() const;
91 int GetNumColors() const;
92 unsigned int GetWidth() const;
93 unsigned int GetHeight() const;
94 int GetTransparentColour() const;
95
96 // constructor, destructor, etc.
97 wxIFFDecoder(wxInputStream *s);
98 ~wxIFFDecoder() { Destroy(); }
03647350 99
8faef7cc 100 // NOTE: this function modifies the current stream position
63f8abca 101 bool CanRead();
03647350 102
63f8abca
VS
103 int ReadIFF();
104 bool ConvertToImage(wxImage *image) const;
105};
106
107
108//---------------------------------------------------------------------------
109// wxIFFDecoder constructor and destructor
110//---------------------------------------------------------------------------
111
112wxIFFDecoder::wxIFFDecoder(wxInputStream *s)
113{
114 m_f = s;
115 m_image = 0;
116 databuf = 0;
117 decomp_mem = 0;
118}
119
120void wxIFFDecoder::Destroy()
121{
5276b0a5
VZ
122 wxDELETE(m_image);
123 wxDELETEA(databuf);
124 wxDELETEA(decomp_mem);
63f8abca
VS
125}
126
127//---------------------------------------------------------------------------
128// Convert this image to a wxImage object
129//---------------------------------------------------------------------------
130
131// This function was designed by Vaclav Slavik
132
133bool wxIFFDecoder::ConvertToImage(wxImage *image) const
134{
135 // just in case...
136 image->Destroy();
137
138 // create the image
139 image->Create(GetWidth(), GetHeight());
140
a1b806b9 141 if (!image->IsOk())
7beb59f3 142 return false;
63f8abca
VS
143
144 unsigned char *pal = GetPalette();
145 unsigned char *src = GetData();
146 unsigned char *dst = image->GetData();
147 int colors = GetNumColors();
148 int transparent = GetTransparentColour();
149 long i;
150
151 // set transparent colour mask
152 if (transparent != -1)
153 {
154 for (i = 0; i < colors; i++)
155 {
156 if ((pal[3 * i + 0] == 255) &&
157 (pal[3 * i + 1] == 0) &&
158 (pal[3 * i + 2] == 255))
159 {
160 pal[3 * i + 2] = 254;
161 }
162 }
163
164 pal[3 * transparent + 0] = 255,
165 pal[3 * transparent + 1] = 0,
166 pal[3 * transparent + 2] = 255;
167
168 image->SetMaskColour(255, 0, 255);
169 }
170 else
7beb59f3 171 image->SetMask(false);
63f8abca
VS
172
173#if wxUSE_PALETTE
174 if (pal && colors > 0)
175 {
176 unsigned char* r = new unsigned char[colors];
177 unsigned char* g = new unsigned char[colors];
178 unsigned char* b = new unsigned char[colors];
179
180 for (i = 0; i < colors; i++)
181 {
182 r[i] = pal[3*i + 0];
183 g[i] = pal[3*i + 1];
184 b[i] = pal[3*i + 2];
185 }
186
187 image->SetPalette(wxPalette(colors, r, g, b));
188
189 delete [] r;
190 delete [] g;
191 delete [] b;
192 }
193#endif // wxUSE_PALETTE
194
195 // copy image data
196 for (i = 0; i < (long)(GetWidth() * GetHeight()); i++, src += 3, dst += 3)
197 {
198 dst[0] = src[0];
199 dst[1] = src[1];
200 dst[2] = src[2];
201 }
202
7beb59f3 203 return true;
63f8abca
VS
204}
205
206
207//---------------------------------------------------------------------------
208// Data accessors
209//---------------------------------------------------------------------------
210
211// Get data for current frame
212
213unsigned char* wxIFFDecoder::GetData() const { return (m_image->p); }
214unsigned char* wxIFFDecoder::GetPalette() const { return (m_image->pal); }
215int wxIFFDecoder::GetNumColors() const { return m_image->colors; }
216unsigned int wxIFFDecoder::GetWidth() const { return (m_image->w); }
217unsigned int wxIFFDecoder::GetHeight() const { return (m_image->h); }
218int wxIFFDecoder::GetTransparentColour() const { return m_image->transparent; }
219
220//---------------------------------------------------------------------------
221// IFF reading and decoding
222//---------------------------------------------------------------------------
223
224//
225// CanRead:
7beb59f3 226// Returns true if the file looks like a valid IFF, false otherwise.
63f8abca
VS
227//
228bool wxIFFDecoder::CanRead()
229{
79fa2374 230 unsigned char buf[12];
63f8abca 231
79fa2374 232 if ( !m_f->Read(buf, WXSIZEOF(buf)) )
7beb59f3 233 return false;
79fa2374 234
79fa2374 235 return (memcmp(buf, "FORM", 4) == 0) && (memcmp(buf+8, "ILBM", 4) == 0);
63f8abca
VS
236}
237
238
239// ReadIFF:
240// Based on xv source code by Thomas Meyer
77ffb593 241// Permission for use in wxWidgets has been gratefully given.
63f8abca
VS
242
243typedef unsigned char byte;
244#define IFFDEBUG 0
245
246/*************************************************************************
b81e4506 247void decomprle(source, destination, source length, buffer size)
63f8abca 248
b81e4506
FM
249Decompress run-length encoded data from source to destination. Terminates
250when source is decoded completely or destination buffer is full.
63f8abca 251
b81e4506
FM
252The decruncher is as optimized as I could make it, without risking
253safety in case of corrupt BODY chunks.
63f8abca
VS
254**************************************************************************/
255
256static void decomprle(const byte *sptr, byte *dptr, long slen, long dlen)
257{
258 byte codeByte, dataByte;
259
260 while ((slen > 0) && (dlen > 0)) {
261 // read control byte
262 codeByte = *sptr++;
263
264 if (codeByte < 0x80) {
265 codeByte++;
266 if ((slen > (long) codeByte) && (dlen >= (long) codeByte)) {
267 slen -= codeByte + 1;
268 dlen -= codeByte;
269 while (codeByte > 0) {
270 *dptr++ = *sptr++;
271 codeByte--;
272 }
273 }
274 else slen = 0;
275 }
276
277 else if (codeByte > 0x80) {
278 codeByte = 0x81 - (codeByte & 0x7f);
279 if ((slen > (long) 0) && (dlen >= (long) codeByte)) {
280 dataByte = *sptr++;
281 slen -= 2;
282 dlen -= codeByte;
283 while (codeByte > 0) {
284 *dptr++ = dataByte;
285 codeByte--;
286 }
287 }
288 else slen = 0;
289 }
290 }
291}
292
293/******************************************/
294static unsigned int iff_getword(const byte *ptr)
295{
296 unsigned int v;
297
298 v = *ptr++;
299 v = (v << 8) + *ptr;
300 return v;
301}
302
303/******************************************/
304static unsigned long iff_getlong(const byte *ptr)
305{
306 unsigned long l;
307
308 l = *ptr++;
309 l = (l << 8) + *ptr++;
310 l = (l << 8) + *ptr++;
311 l = (l << 8) + *ptr;
312 return l;
313}
314
315// Define internal ILBM types
316#define ILBM_NORMAL 0
317#define ILBM_EHB 1
318#define ILBM_HAM 2
319#define ILBM_HAM8 3
320#define ILBM_24BIT 4
321
322int wxIFFDecoder::ReadIFF()
323{
324 Destroy();
325
326 m_image = new IFFImage();
327 if (m_image == 0) {
b81e4506
FM
328 Destroy();
329 return wxIFF_MEMERR;
63f8abca
VS
330 }
331
332 // compute file length
30984dea 333 wxFileOffset currentPos = m_f->TellI();
b81e4506
FM
334 if (m_f->SeekI(0, wxFromEnd) == wxInvalidOffset) {
335 Destroy();
336 return wxIFF_MEMERR;
337 }
338
63f8abca 339 long filesize = m_f->TellI();
b81e4506
FM
340 if (m_f->SeekI(currentPos, wxFromStart) == wxInvalidOffset) {
341 Destroy();
342 return wxIFF_MEMERR;
343 }
63f8abca
VS
344
345 // allocate memory for complete file
346 if ((databuf = new byte[filesize]) == 0) {
b81e4506
FM
347 Destroy();
348 return wxIFF_MEMERR;
63f8abca
VS
349 }
350
351 m_f->Read(databuf, filesize);
352 const byte *dataend = databuf + filesize;
353
354 // initialize work pointer. used to trace the buffer for IFF chunks
355 const byte *dataptr = databuf;
356
357 // check for minmal size
358 if (dataptr + 12 > dataend) {
b81e4506
FM
359 Destroy();
360 return wxIFF_INVFORMAT;
63f8abca
VS
361 }
362
363 // check if we really got an IFF file
364 if (strncmp((char *)dataptr, "FORM", 4) != 0) {
b81e4506
FM
365 Destroy();
366 return wxIFF_INVFORMAT;
63f8abca
VS
367 }
368
369 dataptr = dataptr + 8; // skip ID and length of FORM
370
371 // check if the IFF file is an ILBM (picture) file
372 if (strncmp((char *) dataptr, "ILBM", 4) != 0) {
b81e4506
FM
373 Destroy();
374 return wxIFF_INVFORMAT;
63f8abca
VS
375 }
376
9a83f860 377 wxLogTrace(wxT("iff"), wxT("IFF ILBM file recognized"));
63f8abca
VS
378
379 dataptr = dataptr + 4; // skip ID
380
381 //
382 // main decoding loop. searches IFF chunks and handles them.
383 // terminates when BODY chunk was found or dataptr ran over end of file
384 //
74ab5f5b 385 bool BMHDok = false, CAMGok = false;
63f8abca 386 int bmhd_width = 0, bmhd_height = 0, bmhd_bitplanes = 0, bmhd_transcol = -1;
74ab5f5b 387 byte bmhd_compression = 0;
63f8abca
VS
388 long camg_viewmode = 0;
389 int colors = 0;
390 while (dataptr + 8 <= dataend) {
391 // get chunk length and make even
74ab5f5b
VZ
392 long chunkLen = (iff_getlong(dataptr + 4) + 1) & 0xfffffffe;
393 if (chunkLen < 0) { // format error?
b81e4506 394 break;
63f8abca
VS
395 }
396 bool truncated = (dataptr + 8 + chunkLen > dataend);
397
398 if (strncmp((char *)dataptr, "BMHD", 4) == 0) { // BMHD chunk?
399 if (chunkLen < 12 + 2 || truncated) {
400 break;
401 }
402 bmhd_width = iff_getword(dataptr + 8); // width of picture
403 bmhd_height= iff_getword(dataptr + 8 + 2); // height of picture
404 bmhd_bitplanes = *(dataptr + 8 + 8); // # of bitplanes
74ab5f5b 405 // bmhd_masking = *(dataptr + 8 + 9); -- unused currently
63f8abca
VS
406 bmhd_compression = *(dataptr + 8 + 10); // get compression
407 bmhd_transcol = iff_getword(dataptr + 8 + 12);
7beb59f3 408 BMHDok = true; // got BMHD
63f8abca
VS
409 dataptr += 8 + chunkLen; // to next chunk
410 }
411 else if (strncmp((char *)dataptr, "CMAP", 4) == 0) { // CMAP ?
412 if (truncated) {
413 break;
414 }
415 const byte *cmapptr = dataptr + 8;
416 colors = chunkLen / 3; // calc no of colors
417
5276b0a5 418 wxDELETE(m_image->pal);
63f8abca
VS
419 m_image->colors = colors;
420 if (colors > 0) {
421 m_image->pal = new byte[3*colors];
422 if (!m_image->pal) {
423 Destroy();
424 return wxIFF_MEMERR;
425 }
426
427 // copy colors to color map
428 for (int i=0; i < colors; i++) {
429 m_image->pal[3*i + 0] = *cmapptr++;
430 m_image->pal[3*i + 1] = *cmapptr++;
431 m_image->pal[3*i + 2] = *cmapptr++;
432 }
433 }
434
9a83f860 435 wxLogTrace(wxT("iff"), wxT("Read %d colors from IFF file."),
63f8abca
VS
436 colors);
437
63f8abca
VS
438 dataptr += 8 + chunkLen; // to next chunk
439 } else if (strncmp((char *)dataptr, "CAMG", 4) == 0) { // CAMG ?
440 if (chunkLen < 4 || truncated) {
441 break;
442 }
443 camg_viewmode = iff_getlong(dataptr + 8); // get viewmodes
7beb59f3 444 CAMGok = true; // got CAMG
63f8abca
VS
445 dataptr += 8 + chunkLen; // to next chunk
446 }
447 else if (strncmp((char *)dataptr, "BODY", 4) == 0) { // BODY ?
448 if (!BMHDok) { // BMHD found?
449 break;
450 }
451 const byte *bodyptr = dataptr + 8; // -> BODY data
452
453 if (truncated) {
454 chunkLen = dataend - dataptr;
455 }
456
457 //
458 // if BODY is compressed, allocate buffer for decrunched BODY
459 // and decompress it (run length encoding)
460 //
461 if (bmhd_compression == 1) {
462 // calc size of decrunch buffer - (size of the actual pic.
463 // decompressed in interleaved Amiga bitplane format)
464
465 size_t decomp_bufsize = (((bmhd_width + 15) >> 4) << 1)
466 * bmhd_height * bmhd_bitplanes;
467
468 if ((decomp_mem = new byte[decomp_bufsize]) == 0) {
469 Destroy();
470 return wxIFF_MEMERR;
471 }
472
473 decomprle(bodyptr, decomp_mem, chunkLen, decomp_bufsize);
474 bodyptr = decomp_mem; // -> uncompressed BODY
475 chunkLen = decomp_bufsize;
5276b0a5 476 wxDELETEA(databuf);
63f8abca
VS
477 }
478
479 // the following determines the type of the ILBM file.
480 // it's either NORMAL, EHB, HAM, HAM8 or 24BIT
481
482 int fmt = ILBM_NORMAL; // assume normal ILBM
483 if (bmhd_bitplanes == 24) {
484 fmt = ILBM_24BIT;
485 } else if (bmhd_bitplanes == 8) {
486 if (CAMGok && (camg_viewmode & 0x800)) {
487 fmt = ILBM_HAM8;
488 }
489 } else if ((bmhd_bitplanes > 5) && CAMGok) {
490 if (camg_viewmode & 0x80) {
491 fmt = ILBM_EHB;
492 } else if (camg_viewmode & 0x800) {
493 fmt = ILBM_HAM;
494 }
495 }
496
9a83f860
VZ
497 wxLogTrace(wxT("iff"),
498 wxT("LoadIFF: %s %dx%d, planes=%d (%d cols), comp=%d"),
63f8abca
VS
499 (fmt==ILBM_NORMAL) ? "Normal ILBM" :
500 (fmt==ILBM_HAM) ? "HAM ILBM" :
501 (fmt==ILBM_HAM8) ? "HAM8 ILBM" :
502 (fmt==ILBM_EHB) ? "EHB ILBM" :
503 (fmt==ILBM_24BIT) ? "24BIT ILBM" : "unknown ILBM",
504 bmhd_width, bmhd_height, bmhd_bitplanes,
505 1<<bmhd_bitplanes, bmhd_compression);
506
507 if ((fmt==ILBM_NORMAL) || (fmt==ILBM_EHB) || (fmt==ILBM_HAM)) {
9a83f860
VZ
508 wxLogTrace(wxT("iff"),
509 wxT("Converting CMAP from normal ILBM CMAP"));
63f8abca
VS
510
511 switch(fmt) {
512 case ILBM_NORMAL: colors = 1 << bmhd_bitplanes; break;
513 case ILBM_EHB: colors = 32*2; break;
514 case ILBM_HAM: colors = 16; break;
515 }
516
517 if (colors > m_image->colors) {
518 byte *pal = new byte[colors*3];
519 if (!pal) {
520 Destroy();
521 return wxIFF_MEMERR;
522 }
523 int i;
524 for (i = 0; i < m_image->colors; i++) {
525 pal[3*i + 0] = m_image->pal[3*i + 0];
526 pal[3*i + 1] = m_image->pal[3*i + 1];
527 pal[3*i + 2] = m_image->pal[3*i + 2];
528 }
529 for (; i < colors; i++) {
530 pal[3*i + 0] = 0;
531 pal[3*i + 1] = 0;
532 pal[3*i + 2] = 0;
533 }
534 delete m_image->pal;
535 m_image->pal = pal;
536 m_image->colors = colors;
537 }
538
539 for (int i=0; i < colors; i++) {
540 m_image->pal[3*i + 0] = (m_image->pal[3*i + 0] >> 4) * 17;
541 m_image->pal[3*i + 1] = (m_image->pal[3*i + 1] >> 4) * 17;
542 m_image->pal[3*i + 2] = (m_image->pal[3*i + 2] >> 4) * 17;
543 }
544 }
545
546 m_image->p = new byte[bmhd_width * bmhd_height * 3];
4cbbbd06 547 byte *picptr = m_image->p;
63f8abca
VS
548 if (!picptr) {
549 Destroy();
550 return wxIFF_MEMERR;
551 }
552
553 byte *pal = m_image->pal;
554 int lineskip = ((bmhd_width + 15) >> 4) << 1;
555 int height = chunkLen / (lineskip * bmhd_bitplanes);
556
557 if (bmhd_height < height) {
558 height = bmhd_height;
559 }
560
561 if (fmt == ILBM_HAM || fmt == ILBM_HAM8 || fmt == ILBM_24BIT) {
562 byte *pic = picptr;
563 const byte *workptr = bodyptr;
564
565 for (int i=0; i < height; i++) {
566 byte bitmsk = 0x80;
567 const byte *workptr2 = workptr;
568
569 // at start of each line, init RGB values to background
570 byte rval = pal[0];
571 byte gval = pal[1];
572 byte bval = pal[2];
573
574 for (int j=0; j < bmhd_width; j++) {
575 long col = 0;
576 long colbit = 1;
577 const byte *workptr3 = workptr2;
578 for (int k=0; k < bmhd_bitplanes; k++) {
579 if (*workptr3 & bitmsk) {
580 col += colbit;
581 }
582 workptr3 += lineskip;
583 colbit <<= 1;
584 }
585
586 if (fmt==ILBM_HAM) {
587 int c = (col & 0x0f);
588 switch (col & 0x30) {
589 case 0x00: if (c >= 0 && c < colors) {
b81e4506
FM
590 rval = pal[3*c + 0];
591 gval = pal[3*c + 1];
592 bval = pal[3*c + 2];
593 }
594 break;
63f8abca
VS
595
596 case 0x10: bval = c * 17;
b81e4506 597 break;
63f8abca
VS
598
599 case 0x20: rval = c * 17;
b81e4506 600 break;
63f8abca
VS
601
602 case 0x30: gval = c * 17;
b81e4506 603 break;
63f8abca
VS
604 }
605 } else if (fmt == ILBM_HAM8) {
606 int c = (col & 0x3f);
607 switch(col & 0xc0) {
608 case 0x00: if (c >= 0 && c < colors) {
b81e4506
FM
609 rval = pal[3*c + 0];
610 gval = pal[3*c + 1];
611 bval = pal[3*c + 2];
612 }
613 break;
63f8abca
VS
614
615 case 0x40: bval = (bval & 3) | (c << 2);
b81e4506 616 break;
63f8abca
VS
617
618 case 0x80: rval = (rval & 3) | (c << 2);
b81e4506 619 break;
63f8abca
VS
620
621 case 0xc0: gval = (rval & 3) | (c << 2);
622 }
623 } else {
624 rval = col & 0xff;
625 gval = (col >> 8) & 0xff;
626 bval = (col >> 16) & 0xff;
627 }
628
629 *pic++ = rval;
630 *pic++ = gval;
631 *pic++ = bval;
632
633 bitmsk = bitmsk >> 1;
634 if (bitmsk == 0) {
635 bitmsk = 0x80;
636 workptr2++;
637 }
638 }
639 workptr += lineskip * bmhd_bitplanes;
640 }
641 } else if ((fmt == ILBM_NORMAL) || (fmt == ILBM_EHB)) {
642 if (fmt == ILBM_EHB) {
9a83f860 643 wxLogTrace(wxT("iff"), wxT("Doubling CMAP for EHB mode"));
63f8abca
VS
644
645 for (int i=0; i<32; i++) {
646 pal[3*(i + 32) + 0] = pal[3*i + 0] >> 1;
647 pal[3*(i + 32) + 1] = pal[3*i + 1] >> 1;
648 pal[3*(i + 32) + 2] = pal[3*i + 2] >> 1;
649 }
650 }
651
652 byte *pic = picptr; // ptr to buffer
653 const byte *workptr = bodyptr; // ptr to pic, planar format
654
655 if (bmhd_height < height) {
656 height = bmhd_height;
657 }
658
659 for (int i=0; i < height; i++) {
660 byte bitmsk = 0x80; // left most bit (mask)
661 const byte *workptr2 = workptr; // work ptr to source
662 for (int j=0; j < bmhd_width; j++) {
663 long col = 0;
664 long colbit = 1;
665 const byte *workptr3 = workptr2; // 1st byte in 1st pln
666
667 for (int k=0; k < bmhd_bitplanes; k++) {
668 if (*workptr3 & bitmsk) { // if bit set in this pln
669 col = col + colbit; // add bit to chunky byte
670 }
671 workptr3 += lineskip; // go to next line
672 colbit <<= 1; // shift color bit
673 }
674
675 if (col >= 0 && col < colors) {
676 pic[0] = pal[3*col + 0];
677 pic[1] = pal[3*col + 1];
678 pic[2] = pal[3*col + 2];
679 } else {
680 pic[0] = pic[1] = pic[2] = 0;
681 }
682 pic += 3;
683 bitmsk = bitmsk >> 1; // shift mask to next bit
684 if (bitmsk == 0) { // if mask is zero
685 bitmsk = 0x80; // reset mask
686 workptr2++; // mv ptr to next byte
687 }
688 }
689
690 workptr += lineskip * bmhd_bitplanes; // to next line
691 }
692 } else {
693 break; // unknown format
694 }
695
696 m_image->w = bmhd_width;
697 m_image->h = height;
698 m_image->transparent = bmhd_transcol;
699
9a83f860 700 wxLogTrace(wxT("iff"), wxT("Loaded IFF picture %s"),
63f8abca
VS
701 truncated? "truncated" : "completely");
702
703 return (truncated? wxIFF_TRUNCATED : wxIFF_OK);
704 } else {
9a83f860 705 wxLogTrace(wxT("iff"), wxT("Skipping unknown chunk '%c%c%c%c'"),
63f8abca
VS
706 *dataptr, *(dataptr+1), *(dataptr+2), *(dataptr+3));
707
708 dataptr = dataptr + 8 + chunkLen; // skip unknown chunk
709 }
710 }
711
712 Destroy();
713 return wxIFF_INVFORMAT;
714}
715
4b6b4dfc 716
4b6b4dfc
RR
717
718//-----------------------------------------------------------------------------
719// wxIFFHandler
720//-----------------------------------------------------------------------------
721
63f8abca
VS
722IMPLEMENT_DYNAMIC_CLASS(wxIFFHandler, wxImageHandler)
723
4b6b4dfc
RR
724#if wxUSE_STREAMS
725
19f1a09a 726bool wxIFFHandler::LoadFile(wxImage *image, wxInputStream& stream,
63f8abca 727 bool verbose, int WXUNUSED(index))
4b6b4dfc
RR
728{
729 wxIFFDecoder *decod;
730 int error;
731 bool ok;
732
733 decod = new wxIFFDecoder(&stream);
734 error = decod->ReadIFF();
735
736 if ((error != wxIFF_OK) && (error != wxIFF_TRUNCATED))
737 {
738 if (verbose)
739 {
740 switch (error)
741 {
742 case wxIFF_INVFORMAT:
743 wxLogError(_("IFF: error in IFF image format."));
744 break;
745 case wxIFF_MEMERR:
746 wxLogError(_("IFF: not enough memory."));
747 break;
748 default:
749 wxLogError(_("IFF: unknown error!!!"));
750 break;
751 }
752 }
753 delete decod;
7beb59f3 754 return false;
4b6b4dfc
RR
755 }
756
757 if ((error == wxIFF_TRUNCATED) && verbose)
758 {
759 wxLogError(_("IFF: data stream seems to be truncated."));
760 /* go on; image data is OK */
761 }
762
763 ok = decod->ConvertToImage(image);
764 delete decod;
765
766 return ok;
767}
768
769bool wxIFFHandler::SaveFile(wxImage * WXUNUSED(image),
63f8abca 770 wxOutputStream& WXUNUSED(stream), bool verbose)
4b6b4dfc
RR
771{
772 if (verbose)
af588446 773 {
4b6b4dfc 774 wxLogDebug(wxT("IFF: the handler is read-only!!"));
af588446 775 }
4b6b4dfc 776
7beb59f3 777 return false;
4b6b4dfc
RR
778}
779
780bool wxIFFHandler::DoCanRead(wxInputStream& stream)
781{
39d16996 782 wxIFFDecoder decod(&stream);
4b6b4dfc 783
39d16996 784 return decod.CanRead();
8faef7cc 785 // it's ok to modify the stream position here
4b6b4dfc
RR
786}
787
63f8abca 788#endif // wxUSE_STREAMS
4b6b4dfc 789
63f8abca 790#endif // wxUSE_IFF