]>
git.saurik.com Git - wxWidgets.git/blob - src/common/imagiff.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxImage handler for Amiga IFF images
4 // Author: Steffen Gutmann, Thomas Meyer
6 // Copyright: (c) Steffen Gutmann, 2002
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 // Parts of this source are based on the iff loading algorithm found
11 // in xviff.c. Permission by the original author, Thomas Meyer, and
12 // by the author of xv, John Bradley for using the iff loading part
13 // in wxWindows has been gratefully given.
16 #pragma implementation "imagiff.h"
19 // For compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
30 #if wxUSE_IMAGE && wxUSE_IFF
32 #include "wx/imagiff.h"
33 #include "wx/wfstream.h"
41 // --------------------------------------------------------------------------
43 // --------------------------------------------------------------------------
46 // Note that the error code wxIFF_TRUNCATED means that the image itself
47 // is most probably OK, but the decoder didn't reach the end of the data
48 // stream; this means that if it was not reading directly from file,
49 // the stream will not be correctly positioned.
54 wxIFF_OK
= 0, /* everything was OK */
55 wxIFF_INVFORMAT
, /* error in iff header */
56 wxIFF_MEMERR
, /* error allocating memory */
57 wxIFF_TRUNCATED
/* file appears to be truncated */
60 // --------------------------------------------------------------------------
62 // --------------------------------------------------------------------------
64 // internal class for storing IFF image data
68 unsigned int w
; /* width */
69 unsigned int h
; /* height */
70 int transparent
; /* transparent color (-1 = none) */
71 int colors
; /* number of colors */
72 unsigned char *p
; /* bitmap */
73 unsigned char *pal
; /* palette */
75 IFFImage() : w(0), h(0), colors(0), p(0), pal(0) {}
76 ~IFFImage() { delete [] p
; delete [] pal
; }
79 class WXDLLEXPORT wxIFFDecoder
82 IFFImage
*m_image
; // image data
83 wxInputStream
*m_f
; // input stream
84 unsigned char *databuf
;
85 unsigned char *picptr
;
86 unsigned char *decomp_mem
;
91 // get data of current frame
92 unsigned char* GetData() const;
93 unsigned char* GetPalette() const;
94 int GetNumColors() const;
95 unsigned int GetWidth() const;
96 unsigned int GetHeight() const;
97 int GetTransparentColour() const;
99 // constructor, destructor, etc.
100 wxIFFDecoder(wxInputStream
*s
);
101 ~wxIFFDecoder() { Destroy(); }
104 bool ConvertToImage(wxImage
*image
) const;
108 //---------------------------------------------------------------------------
109 // wxIFFDecoder constructor and destructor
110 //---------------------------------------------------------------------------
112 wxIFFDecoder::wxIFFDecoder(wxInputStream
*s
)
120 void wxIFFDecoder::Destroy()
126 delete [] decomp_mem
;
130 //---------------------------------------------------------------------------
131 // Convert this image to a wxImage object
132 //---------------------------------------------------------------------------
134 // This function was designed by Vaclav Slavik
136 bool wxIFFDecoder::ConvertToImage(wxImage
*image
) const
142 image
->Create(GetWidth(), GetHeight());
147 unsigned char *pal
= GetPalette();
148 unsigned char *src
= GetData();
149 unsigned char *dst
= image
->GetData();
150 int colors
= GetNumColors();
151 int transparent
= GetTransparentColour();
154 // set transparent colour mask
155 if (transparent
!= -1)
157 for (i
= 0; i
< colors
; i
++)
159 if ((pal
[3 * i
+ 0] == 255) &&
160 (pal
[3 * i
+ 1] == 0) &&
161 (pal
[3 * i
+ 2] == 255))
163 pal
[3 * i
+ 2] = 254;
167 pal
[3 * transparent
+ 0] = 255,
168 pal
[3 * transparent
+ 1] = 0,
169 pal
[3 * transparent
+ 2] = 255;
171 image
->SetMaskColour(255, 0, 255);
174 image
->SetMask(FALSE
);
177 if (pal
&& colors
> 0)
179 unsigned char* r
= new unsigned char[colors
];
180 unsigned char* g
= new unsigned char[colors
];
181 unsigned char* b
= new unsigned char[colors
];
183 for (i
= 0; i
< colors
; i
++)
190 image
->SetPalette(wxPalette(colors
, r
, g
, b
));
196 #endif // wxUSE_PALETTE
199 for (i
= 0; i
< (long)(GetWidth() * GetHeight()); i
++, src
+= 3, dst
+= 3)
210 //---------------------------------------------------------------------------
212 //---------------------------------------------------------------------------
214 // Get data for current frame
216 unsigned char* wxIFFDecoder::GetData() const { return (m_image
->p
); }
217 unsigned char* wxIFFDecoder::GetPalette() const { return (m_image
->pal
); }
218 int wxIFFDecoder::GetNumColors() const { return m_image
->colors
; }
219 unsigned int wxIFFDecoder::GetWidth() const { return (m_image
->w
); }
220 unsigned int wxIFFDecoder::GetHeight() const { return (m_image
->h
); }
221 int wxIFFDecoder::GetTransparentColour() const { return m_image
->transparent
; }
223 //---------------------------------------------------------------------------
224 // IFF reading and decoding
225 //---------------------------------------------------------------------------
229 // Returns TRUE if the file looks like a valid IFF, FALSE otherwise.
231 bool wxIFFDecoder::CanRead()
233 unsigned char buf
[12] = "";
236 m_f
->SeekI(-12, wxFromCurrent
);
238 return (memcmp(buf
, "FORM", 4) == 0 && memcmp(buf
+8, "ILBM", 4) == 0);
243 // Based on xv source code by Thomas Meyer
244 // Permission for use in wxWindows has been gratefully given.
246 typedef unsigned char byte
;
249 /*************************************************************************
250 void decomprle(source, destination, source length, buffer size)
252 Decompress run-length encoded data from source to destination. Terminates
253 when source is decoded completely or destination buffer is full.
255 The decruncher is as optimized as I could make it, without risking
256 safety in case of corrupt BODY chunks.
257 **************************************************************************/
259 static void decomprle(const byte
*sptr
, byte
*dptr
, long slen
, long dlen
)
261 byte codeByte
, dataByte
;
263 while ((slen
> 0) && (dlen
> 0)) {
267 if (codeByte
< 0x80) {
269 if ((slen
> (long) codeByte
) && (dlen
>= (long) codeByte
)) {
270 slen
-= codeByte
+ 1;
272 while (codeByte
> 0) {
280 else if (codeByte
> 0x80) {
281 codeByte
= 0x81 - (codeByte
& 0x7f);
282 if ((slen
> (long) 0) && (dlen
>= (long) codeByte
)) {
286 while (codeByte
> 0) {
296 /******************************************/
297 static unsigned int iff_getword(const byte
*ptr
)
306 /******************************************/
307 static unsigned long iff_getlong(const byte
*ptr
)
312 l
= (l
<< 8) + *ptr
++;
313 l
= (l
<< 8) + *ptr
++;
318 // Define internal ILBM types
319 #define ILBM_NORMAL 0
325 int wxIFFDecoder::ReadIFF()
329 m_image
= new IFFImage();
335 // compute file length
336 off_t currentPos
= m_f
->TellI();
337 m_f
->SeekI(0, wxFromEnd
);
338 long filesize
= m_f
->TellI();
339 m_f
->SeekI(currentPos
, wxFromStart
);
341 // allocate memory for complete file
342 if ((databuf
= new byte
[filesize
]) == 0) {
347 m_f
->Read(databuf
, filesize
);
348 const byte
*dataend
= databuf
+ filesize
;
350 // initialize work pointer. used to trace the buffer for IFF chunks
351 const byte
*dataptr
= databuf
;
353 // check for minmal size
354 if (dataptr
+ 12 > dataend
) {
356 return wxIFF_INVFORMAT
;
359 // check if we really got an IFF file
360 if (strncmp((char *)dataptr
, "FORM", 4) != 0) {
362 return wxIFF_INVFORMAT
;
365 dataptr
= dataptr
+ 8; // skip ID and length of FORM
367 // check if the IFF file is an ILBM (picture) file
368 if (strncmp((char *) dataptr
, "ILBM", 4) != 0) {
370 return wxIFF_INVFORMAT
;
373 wxLogTrace(_T("iff"), _T("IFF ILBM file recognized"));
375 dataptr
= dataptr
+ 4; // skip ID
378 // main decoding loop. searches IFF chunks and handles them.
379 // terminates when BODY chunk was found or dataptr ran over end of file
381 bool BMHDok
= FALSE
, CMAPok
= FALSE
, CAMGok
= FALSE
;
382 int bmhd_width
= 0, bmhd_height
= 0, bmhd_bitplanes
= 0, bmhd_transcol
= -1;
383 byte bmhd_masking
= 0, bmhd_compression
= 0;
384 long camg_viewmode
= 0;
386 while (dataptr
+ 8 <= dataend
) {
387 // get chunk length and make even
388 size_t chunkLen
= (iff_getlong(dataptr
+ 4) + 1) & 0xfffffffe;
390 // Silence compiler warning
392 chunkLen_
= chunkLen
;
393 if (chunkLen_
< 0) { // format error?
395 if (chunkLen
< 0) { // format error?
399 bool truncated
= (dataptr
+ 8 + chunkLen
> dataend
);
401 if (strncmp((char *)dataptr
, "BMHD", 4) == 0) { // BMHD chunk?
402 if (chunkLen
< 12 + 2 || truncated
) {
405 bmhd_width
= iff_getword(dataptr
+ 8); // width of picture
406 bmhd_height
= iff_getword(dataptr
+ 8 + 2); // height of picture
407 bmhd_bitplanes
= *(dataptr
+ 8 + 8); // # of bitplanes
408 bmhd_masking
= *(dataptr
+ 8 + 9);
409 bmhd_compression
= *(dataptr
+ 8 + 10); // get compression
410 bmhd_transcol
= iff_getword(dataptr
+ 8 + 12);
411 BMHDok
= TRUE
; // got BMHD
412 dataptr
+= 8 + chunkLen
; // to next chunk
414 else if (strncmp((char *)dataptr
, "CMAP", 4) == 0) { // CMAP ?
418 const byte
*cmapptr
= dataptr
+ 8;
419 colors
= chunkLen
/ 3; // calc no of colors
423 m_image
->colors
= colors
;
425 m_image
->pal
= new byte
[3*colors
];
431 // copy colors to color map
432 for (int i
=0; i
< colors
; i
++) {
433 m_image
->pal
[3*i
+ 0] = *cmapptr
++;
434 m_image
->pal
[3*i
+ 1] = *cmapptr
++;
435 m_image
->pal
[3*i
+ 2] = *cmapptr
++;
439 wxLogTrace(_T("iff"), _T("Read %d colors from IFF file."),
442 CMAPok
= TRUE
; // got CMAP
443 dataptr
+= 8 + chunkLen
; // to next chunk
444 } else if (strncmp((char *)dataptr
, "CAMG", 4) == 0) { // CAMG ?
445 if (chunkLen
< 4 || truncated
) {
448 camg_viewmode
= iff_getlong(dataptr
+ 8); // get viewmodes
449 CAMGok
= TRUE
; // got CAMG
450 dataptr
+= 8 + chunkLen
; // to next chunk
452 else if (strncmp((char *)dataptr
, "BODY", 4) == 0) { // BODY ?
453 if (!BMHDok
) { // BMHD found?
456 const byte
*bodyptr
= dataptr
+ 8; // -> BODY data
459 chunkLen
= dataend
- dataptr
;
463 // if BODY is compressed, allocate buffer for decrunched BODY
464 // and decompress it (run length encoding)
466 if (bmhd_compression
== 1) {
467 // calc size of decrunch buffer - (size of the actual pic.
468 // decompressed in interleaved Amiga bitplane format)
470 size_t decomp_bufsize
= (((bmhd_width
+ 15) >> 4) << 1)
471 * bmhd_height
* bmhd_bitplanes
;
473 if ((decomp_mem
= new byte
[decomp_bufsize
]) == 0) {
478 decomprle(bodyptr
, decomp_mem
, chunkLen
, decomp_bufsize
);
479 bodyptr
= decomp_mem
; // -> uncompressed BODY
480 chunkLen
= decomp_bufsize
;
485 // the following determines the type of the ILBM file.
486 // it's either NORMAL, EHB, HAM, HAM8 or 24BIT
488 int fmt
= ILBM_NORMAL
; // assume normal ILBM
489 if (bmhd_bitplanes
== 24) {
491 } else if (bmhd_bitplanes
== 8) {
492 if (CAMGok
&& (camg_viewmode
& 0x800)) {
495 } else if ((bmhd_bitplanes
> 5) && CAMGok
) {
496 if (camg_viewmode
& 0x80) {
498 } else if (camg_viewmode
& 0x800) {
503 wxLogTrace(_T("iff"),
504 _T("LoadIFF: %s %dx%d, planes=%d (%d cols), comp=%d"),
505 (fmt
==ILBM_NORMAL
) ? "Normal ILBM" :
506 (fmt
==ILBM_HAM
) ? "HAM ILBM" :
507 (fmt
==ILBM_HAM8
) ? "HAM8 ILBM" :
508 (fmt
==ILBM_EHB
) ? "EHB ILBM" :
509 (fmt
==ILBM_24BIT
) ? "24BIT ILBM" : "unknown ILBM",
510 bmhd_width
, bmhd_height
, bmhd_bitplanes
,
511 1<<bmhd_bitplanes
, bmhd_compression
);
513 if ((fmt
==ILBM_NORMAL
) || (fmt
==ILBM_EHB
) || (fmt
==ILBM_HAM
)) {
514 wxLogTrace(_T("iff"),
515 _T("Converting CMAP from normal ILBM CMAP"));
518 case ILBM_NORMAL
: colors
= 1 << bmhd_bitplanes
; break;
519 case ILBM_EHB
: colors
= 32*2; break;
520 case ILBM_HAM
: colors
= 16; break;
523 if (colors
> m_image
->colors
) {
524 byte
*pal
= new byte
[colors
*3];
530 for (i
= 0; i
< m_image
->colors
; i
++) {
531 pal
[3*i
+ 0] = m_image
->pal
[3*i
+ 0];
532 pal
[3*i
+ 1] = m_image
->pal
[3*i
+ 1];
533 pal
[3*i
+ 2] = m_image
->pal
[3*i
+ 2];
535 for (; i
< colors
; i
++) {
542 m_image
->colors
= colors
;
545 for (int i
=0; i
< colors
; i
++) {
546 m_image
->pal
[3*i
+ 0] = (m_image
->pal
[3*i
+ 0] >> 4) * 17;
547 m_image
->pal
[3*i
+ 1] = (m_image
->pal
[3*i
+ 1] >> 4) * 17;
548 m_image
->pal
[3*i
+ 2] = (m_image
->pal
[3*i
+ 2] >> 4) * 17;
552 m_image
->p
= new byte
[bmhd_width
* bmhd_height
* 3];
553 byte
*picptr
= m_image
->p
;
559 byte
*pal
= m_image
->pal
;
560 int lineskip
= ((bmhd_width
+ 15) >> 4) << 1;
561 int height
= chunkLen
/ (lineskip
* bmhd_bitplanes
);
563 if (bmhd_height
< height
) {
564 height
= bmhd_height
;
567 if (fmt
== ILBM_HAM
|| fmt
== ILBM_HAM8
|| fmt
== ILBM_24BIT
) {
569 const byte
*workptr
= bodyptr
;
571 for (int i
=0; i
< height
; i
++) {
573 const byte
*workptr2
= workptr
;
575 // at start of each line, init RGB values to background
580 for (int j
=0; j
< bmhd_width
; j
++) {
583 const byte
*workptr3
= workptr2
;
584 for (int k
=0; k
< bmhd_bitplanes
; k
++) {
585 if (*workptr3
& bitmsk
) {
588 workptr3
+= lineskip
;
593 int c
= (col
& 0x0f);
594 switch (col
& 0x30) {
595 case 0x00: if (c
>= 0 && c
< colors
) {
602 case 0x10: bval
= c
* 17;
605 case 0x20: rval
= c
* 17;
608 case 0x30: gval
= c
* 17;
611 } else if (fmt
== ILBM_HAM8
) {
612 int c
= (col
& 0x3f);
614 case 0x00: if (c
>= 0 && c
< colors
) {
621 case 0x40: bval
= (bval
& 3) | (c
<< 2);
624 case 0x80: rval
= (rval
& 3) | (c
<< 2);
627 case 0xc0: gval
= (rval
& 3) | (c
<< 2);
631 gval
= (col
>> 8) & 0xff;
632 bval
= (col
>> 16) & 0xff;
639 bitmsk
= bitmsk
>> 1;
645 workptr
+= lineskip
* bmhd_bitplanes
;
647 } else if ((fmt
== ILBM_NORMAL
) || (fmt
== ILBM_EHB
)) {
648 if (fmt
== ILBM_EHB
) {
649 wxLogTrace(_T("iff"), _T("Doubling CMAP for EHB mode"));
651 for (int i
=0; i
<32; i
++) {
652 pal
[3*(i
+ 32) + 0] = pal
[3*i
+ 0] >> 1;
653 pal
[3*(i
+ 32) + 1] = pal
[3*i
+ 1] >> 1;
654 pal
[3*(i
+ 32) + 2] = pal
[3*i
+ 2] >> 1;
658 byte
*pic
= picptr
; // ptr to buffer
659 const byte
*workptr
= bodyptr
; // ptr to pic, planar format
661 if (bmhd_height
< height
) {
662 height
= bmhd_height
;
665 for (int i
=0; i
< height
; i
++) {
666 byte bitmsk
= 0x80; // left most bit (mask)
667 const byte
*workptr2
= workptr
; // work ptr to source
668 for (int j
=0; j
< bmhd_width
; j
++) {
671 const byte
*workptr3
= workptr2
; // 1st byte in 1st pln
673 for (int k
=0; k
< bmhd_bitplanes
; k
++) {
674 if (*workptr3
& bitmsk
) { // if bit set in this pln
675 col
= col
+ colbit
; // add bit to chunky byte
677 workptr3
+= lineskip
; // go to next line
678 colbit
<<= 1; // shift color bit
681 if (col
>= 0 && col
< colors
) {
682 pic
[0] = pal
[3*col
+ 0];
683 pic
[1] = pal
[3*col
+ 1];
684 pic
[2] = pal
[3*col
+ 2];
686 pic
[0] = pic
[1] = pic
[2] = 0;
689 bitmsk
= bitmsk
>> 1; // shift mask to next bit
690 if (bitmsk
== 0) { // if mask is zero
691 bitmsk
= 0x80; // reset mask
692 workptr2
++; // mv ptr to next byte
696 workptr
+= lineskip
* bmhd_bitplanes
; // to next line
699 break; // unknown format
702 m_image
->w
= bmhd_width
;
704 m_image
->transparent
= bmhd_transcol
;
706 wxLogTrace(_T("iff"), _T("Loaded IFF picture %s"),
707 truncated
? "truncated" : "completely");
709 return (truncated
? wxIFF_TRUNCATED
: wxIFF_OK
);
711 wxLogTrace(_T("iff"), _T("Skipping unknown chunk '%c%c%c%c'"),
712 *dataptr
, *(dataptr
+1), *(dataptr
+2), *(dataptr
+3));
714 dataptr
= dataptr
+ 8 + chunkLen
; // skip unknown chunk
719 return wxIFF_INVFORMAT
;
724 //-----------------------------------------------------------------------------
726 //-----------------------------------------------------------------------------
728 IMPLEMENT_DYNAMIC_CLASS(wxIFFHandler
, wxImageHandler
)
732 bool wxIFFHandler::LoadFile(wxImage
*image
, wxInputStream
& stream
,
733 bool verbose
, int WXUNUSED(index
))
739 decod
= new wxIFFDecoder(&stream
);
740 error
= decod
->ReadIFF();
742 if ((error
!= wxIFF_OK
) && (error
!= wxIFF_TRUNCATED
))
748 case wxIFF_INVFORMAT
:
749 wxLogError(_("IFF: error in IFF image format."));
752 wxLogError(_("IFF: not enough memory."));
755 wxLogError(_("IFF: unknown error!!!"));
763 if ((error
== wxIFF_TRUNCATED
) && verbose
)
765 wxLogError(_("IFF: data stream seems to be truncated."));
766 /* go on; image data is OK */
769 ok
= decod
->ConvertToImage(image
);
775 bool wxIFFHandler::SaveFile(wxImage
* WXUNUSED(image
),
776 wxOutputStream
& WXUNUSED(stream
), bool verbose
)
779 wxLogDebug(wxT("IFF: the handler is read-only!!"));
784 bool wxIFFHandler::DoCanRead(wxInputStream
& stream
)
789 decod
= new wxIFFDecoder(&stream
);
790 ok
= decod
->CanRead();
796 #endif // wxUSE_STREAMS