]>
git.saurik.com Git - wxWidgets.git/blob - src/common/imagiff.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxImage handler for Amiga IFF images
4 // Author: Steffen Gutmann
6 // Copyright: (c) Steffen Gutmann, 2002
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 // parts of the source are based on xviff by Thomas Meyer
11 // Permission for use in wxWindows has been gratefully given.
14 #pragma implementation "imagiff.h"
17 // For compilers that support precompilation, includes "wx.h".
18 #include "wx/wxprec.h"
28 #if wxUSE_IMAGE && wxUSE_IFF
30 #include "wx/imagiff.h"
31 #include "wx/wfstream.h"
39 // --------------------------------------------------------------------------
41 // --------------------------------------------------------------------------
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.
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 */
58 // --------------------------------------------------------------------------
60 // --------------------------------------------------------------------------
62 // internal class for storing IFF image data
66 unsigned int w
; /* width */
67 unsigned int h
; /* height */
68 int transparent
; /* transparent color (-1 = none) */
69 int colors
; /* number of colors */
70 unsigned char *p
; /* bitmap */
71 unsigned char *pal
; /* palette */
73 IFFImage() : w(0), h(0), colors(0), p(0), pal(0) {}
74 ~IFFImage() { delete [] p
; delete [] pal
; }
77 class WXDLLEXPORT wxIFFDecoder
80 IFFImage
*m_image
; // image data
81 wxInputStream
*m_f
; // input stream
82 unsigned char *databuf
;
83 unsigned char *picptr
;
84 unsigned char *decomp_mem
;
89 // get data of current frame
90 unsigned char* GetData() const;
91 unsigned char* GetPalette() const;
92 int GetNumColors() const;
93 unsigned int GetWidth() const;
94 unsigned int GetHeight() const;
95 int GetTransparentColour() const;
97 // constructor, destructor, etc.
98 wxIFFDecoder(wxInputStream
*s
);
99 ~wxIFFDecoder() { Destroy(); }
102 bool ConvertToImage(wxImage
*image
) const;
106 //---------------------------------------------------------------------------
107 // wxIFFDecoder constructor and destructor
108 //---------------------------------------------------------------------------
110 wxIFFDecoder::wxIFFDecoder(wxInputStream
*s
)
118 void wxIFFDecoder::Destroy()
124 delete [] decomp_mem
;
128 //---------------------------------------------------------------------------
129 // Convert this image to a wxImage object
130 //---------------------------------------------------------------------------
132 // This function was designed by Vaclav Slavik
134 bool wxIFFDecoder::ConvertToImage(wxImage
*image
) const
140 image
->Create(GetWidth(), GetHeight());
145 unsigned char *pal
= GetPalette();
146 unsigned char *src
= GetData();
147 unsigned char *dst
= image
->GetData();
148 int colors
= GetNumColors();
149 int transparent
= GetTransparentColour();
152 // set transparent colour mask
153 if (transparent
!= -1)
155 for (i
= 0; i
< colors
; i
++)
157 if ((pal
[3 * i
+ 0] == 255) &&
158 (pal
[3 * i
+ 1] == 0) &&
159 (pal
[3 * i
+ 2] == 255))
161 pal
[3 * i
+ 2] = 254;
165 pal
[3 * transparent
+ 0] = 255,
166 pal
[3 * transparent
+ 1] = 0,
167 pal
[3 * transparent
+ 2] = 255;
169 image
->SetMaskColour(255, 0, 255);
172 image
->SetMask(FALSE
);
175 if (pal
&& colors
> 0)
177 unsigned char* r
= new unsigned char[colors
];
178 unsigned char* g
= new unsigned char[colors
];
179 unsigned char* b
= new unsigned char[colors
];
181 for (i
= 0; i
< colors
; i
++)
188 image
->SetPalette(wxPalette(colors
, r
, g
, b
));
194 #endif // wxUSE_PALETTE
197 for (i
= 0; i
< (long)(GetWidth() * GetHeight()); i
++, src
+= 3, dst
+= 3)
208 //---------------------------------------------------------------------------
210 //---------------------------------------------------------------------------
212 // Get data for current frame
214 unsigned char* wxIFFDecoder::GetData() const { return (m_image
->p
); }
215 unsigned char* wxIFFDecoder::GetPalette() const { return (m_image
->pal
); }
216 int wxIFFDecoder::GetNumColors() const { return m_image
->colors
; }
217 unsigned int wxIFFDecoder::GetWidth() const { return (m_image
->w
); }
218 unsigned int wxIFFDecoder::GetHeight() const { return (m_image
->h
); }
219 int wxIFFDecoder::GetTransparentColour() const { return m_image
->transparent
; }
221 //---------------------------------------------------------------------------
222 // IFF reading and decoding
223 //---------------------------------------------------------------------------
227 // Returns TRUE if the file looks like a valid IFF, FALSE otherwise.
229 bool wxIFFDecoder::CanRead()
231 unsigned char buf
[12] = "";
234 m_f
->SeekI(-12, wxFromCurrent
);
236 return (memcmp(buf
, "FORM", 4) == 0 && memcmp(buf
+8, "ILBM", 4) == 0);
241 // Based on xv source code by Thomas Meyer
242 // Permission for use in wxWindows has been gratefully given.
244 typedef unsigned char byte
;
247 /*************************************************************************
248 void decomprle(source, destination, source length, buffer size)
250 Decompress run-length encoded data from source to destination. Terminates
251 when source is decoded completely or destination buffer is full.
253 The decruncher is as optimized as I could make it, without risking
254 safety in case of corrupt BODY chunks.
255 **************************************************************************/
257 static void decomprle(const byte
*sptr
, byte
*dptr
, long slen
, long dlen
)
259 byte codeByte
, dataByte
;
261 while ((slen
> 0) && (dlen
> 0)) {
265 if (codeByte
< 0x80) {
267 if ((slen
> (long) codeByte
) && (dlen
>= (long) codeByte
)) {
268 slen
-= codeByte
+ 1;
270 while (codeByte
> 0) {
278 else if (codeByte
> 0x80) {
279 codeByte
= 0x81 - (codeByte
& 0x7f);
280 if ((slen
> (long) 0) && (dlen
>= (long) codeByte
)) {
284 while (codeByte
> 0) {
294 /******************************************/
295 static unsigned int iff_getword(const byte
*ptr
)
304 /******************************************/
305 static unsigned long iff_getlong(const byte
*ptr
)
310 l
= (l
<< 8) + *ptr
++;
311 l
= (l
<< 8) + *ptr
++;
316 // Define internal ILBM types
317 #define ILBM_NORMAL 0
323 int wxIFFDecoder::ReadIFF()
327 m_image
= new IFFImage();
333 // compute file length
334 off_t currentPos
= m_f
->TellI();
335 m_f
->SeekI(0, wxFromEnd
);
336 long filesize
= m_f
->TellI();
337 m_f
->SeekI(currentPos
, wxFromStart
);
339 // allocate memory for complete file
340 if ((databuf
= new byte
[filesize
]) == 0) {
345 m_f
->Read(databuf
, filesize
);
346 const byte
*dataend
= databuf
+ filesize
;
348 // initialize work pointer. used to trace the buffer for IFF chunks
349 const byte
*dataptr
= databuf
;
351 // check for minmal size
352 if (dataptr
+ 12 > dataend
) {
354 return wxIFF_INVFORMAT
;
357 // check if we really got an IFF file
358 if (strncmp((char *)dataptr
, "FORM", 4) != 0) {
360 return wxIFF_INVFORMAT
;
363 dataptr
= dataptr
+ 8; // skip ID and length of FORM
365 // check if the IFF file is an ILBM (picture) file
366 if (strncmp((char *) dataptr
, "ILBM", 4) != 0) {
368 return wxIFF_INVFORMAT
;
371 wxLogTrace(_T("iff"), _T("IFF ILBM file recognized"));
373 dataptr
= dataptr
+ 4; // skip ID
376 // main decoding loop. searches IFF chunks and handles them.
377 // terminates when BODY chunk was found or dataptr ran over end of file
379 bool BMHDok
= false, CMAPok
= false, CAMGok
= false;
380 int bmhd_width
= 0, bmhd_height
= 0, bmhd_bitplanes
= 0, bmhd_transcol
= -1;
381 byte bmhd_masking
= 0, bmhd_compression
= 0;
382 long camg_viewmode
= 0;
384 while (dataptr
+ 8 <= dataend
) {
385 // get chunk length and make even
386 size_t chunkLen
= (iff_getlong(dataptr
+ 4) + 1) & 0xfffffffe;
388 // Silence compiler warning
390 chunkLen_
= chunkLen
;
391 if (chunkLen_
< 0) { // format error?
393 if (chunkLen
< 0) { // format error?
397 bool truncated
= (dataptr
+ 8 + chunkLen
> dataend
);
399 if (strncmp((char *)dataptr
, "BMHD", 4) == 0) { // BMHD chunk?
400 if (chunkLen
< 12 + 2 || truncated
) {
403 bmhd_width
= iff_getword(dataptr
+ 8); // width of picture
404 bmhd_height
= iff_getword(dataptr
+ 8 + 2); // height of picture
405 bmhd_bitplanes
= *(dataptr
+ 8 + 8); // # of bitplanes
406 bmhd_masking
= *(dataptr
+ 8 + 9);
407 bmhd_compression
= *(dataptr
+ 8 + 10); // get compression
408 bmhd_transcol
= iff_getword(dataptr
+ 8 + 12);
409 BMHDok
= true; // got BMHD
410 dataptr
+= 8 + chunkLen
; // to next chunk
412 else if (strncmp((char *)dataptr
, "CMAP", 4) == 0) { // CMAP ?
416 const byte
*cmapptr
= dataptr
+ 8;
417 colors
= chunkLen
/ 3; // calc no of colors
421 m_image
->colors
= colors
;
423 m_image
->pal
= new byte
[3*colors
];
429 // copy colors to color map
430 for (int i
=0; i
< colors
; i
++) {
431 m_image
->pal
[3*i
+ 0] = *cmapptr
++;
432 m_image
->pal
[3*i
+ 1] = *cmapptr
++;
433 m_image
->pal
[3*i
+ 2] = *cmapptr
++;
437 wxLogTrace(_T("iff"), _T("Read %d colors from IFF file."),
440 CMAPok
= true; // got CMAP
441 dataptr
+= 8 + chunkLen
; // to next chunk
442 } else if (strncmp((char *)dataptr
, "CAMG", 4) == 0) { // CAMG ?
443 if (chunkLen
< 4 || truncated
) {
446 camg_viewmode
= iff_getlong(dataptr
+ 8); // get viewmodes
447 CAMGok
= true; // got CAMG
448 dataptr
+= 8 + chunkLen
; // to next chunk
450 else if (strncmp((char *)dataptr
, "BODY", 4) == 0) { // BODY ?
451 if (!BMHDok
) { // BMHD found?
454 const byte
*bodyptr
= dataptr
+ 8; // -> BODY data
457 chunkLen
= dataend
- dataptr
;
461 // if BODY is compressed, allocate buffer for decrunched BODY
462 // and decompress it (run length encoding)
464 if (bmhd_compression
== 1) {
465 // calc size of decrunch buffer - (size of the actual pic.
466 // decompressed in interleaved Amiga bitplane format)
468 size_t decomp_bufsize
= (((bmhd_width
+ 15) >> 4) << 1)
469 * bmhd_height
* bmhd_bitplanes
;
471 if ((decomp_mem
= new byte
[decomp_bufsize
]) == 0) {
476 decomprle(bodyptr
, decomp_mem
, chunkLen
, decomp_bufsize
);
477 bodyptr
= decomp_mem
; // -> uncompressed BODY
478 chunkLen
= decomp_bufsize
;
483 // the following determines the type of the ILBM file.
484 // it's either NORMAL, EHB, HAM, HAM8 or 24BIT
486 int fmt
= ILBM_NORMAL
; // assume normal ILBM
487 if (bmhd_bitplanes
== 24) {
489 } else if (bmhd_bitplanes
== 8) {
490 if (CAMGok
&& (camg_viewmode
& 0x800)) {
493 } else if ((bmhd_bitplanes
> 5) && CAMGok
) {
494 if (camg_viewmode
& 0x80) {
496 } else if (camg_viewmode
& 0x800) {
501 wxLogTrace(_T("iff"),
502 _T("LoadIFF: %s %dx%d, planes=%d (%d cols), comp=%d"),
503 (fmt
==ILBM_NORMAL
) ? "Normal ILBM" :
504 (fmt
==ILBM_HAM
) ? "HAM ILBM" :
505 (fmt
==ILBM_HAM8
) ? "HAM8 ILBM" :
506 (fmt
==ILBM_EHB
) ? "EHB ILBM" :
507 (fmt
==ILBM_24BIT
) ? "24BIT ILBM" : "unknown ILBM",
508 bmhd_width
, bmhd_height
, bmhd_bitplanes
,
509 1<<bmhd_bitplanes
, bmhd_compression
);
511 if ((fmt
==ILBM_NORMAL
) || (fmt
==ILBM_EHB
) || (fmt
==ILBM_HAM
)) {
512 wxLogTrace(_T("iff"),
513 _T("Converting CMAP from normal ILBM CMAP"));
516 case ILBM_NORMAL
: colors
= 1 << bmhd_bitplanes
; break;
517 case ILBM_EHB
: colors
= 32*2; break;
518 case ILBM_HAM
: colors
= 16; break;
521 if (colors
> m_image
->colors
) {
522 byte
*pal
= new byte
[colors
*3];
528 for (i
= 0; i
< m_image
->colors
; i
++) {
529 pal
[3*i
+ 0] = m_image
->pal
[3*i
+ 0];
530 pal
[3*i
+ 1] = m_image
->pal
[3*i
+ 1];
531 pal
[3*i
+ 2] = m_image
->pal
[3*i
+ 2];
533 for (; i
< colors
; i
++) {
540 m_image
->colors
= colors
;
543 for (int i
=0; i
< colors
; i
++) {
544 m_image
->pal
[3*i
+ 0] = (m_image
->pal
[3*i
+ 0] >> 4) * 17;
545 m_image
->pal
[3*i
+ 1] = (m_image
->pal
[3*i
+ 1] >> 4) * 17;
546 m_image
->pal
[3*i
+ 2] = (m_image
->pal
[3*i
+ 2] >> 4) * 17;
550 m_image
->p
= new byte
[bmhd_width
* bmhd_height
* 3];
551 byte
*picptr
= m_image
->p
;
557 byte
*pal
= m_image
->pal
;
558 int lineskip
= ((bmhd_width
+ 15) >> 4) << 1;
559 int height
= chunkLen
/ (lineskip
* bmhd_bitplanes
);
561 if (bmhd_height
< height
) {
562 height
= bmhd_height
;
565 if (fmt
== ILBM_HAM
|| fmt
== ILBM_HAM8
|| fmt
== ILBM_24BIT
) {
567 const byte
*workptr
= bodyptr
;
569 for (int i
=0; i
< height
; i
++) {
571 const byte
*workptr2
= workptr
;
573 // at start of each line, init RGB values to background
578 for (int j
=0; j
< bmhd_width
; j
++) {
581 const byte
*workptr3
= workptr2
;
582 for (int k
=0; k
< bmhd_bitplanes
; k
++) {
583 if (*workptr3
& bitmsk
) {
586 workptr3
+= lineskip
;
591 int c
= (col
& 0x0f);
592 switch (col
& 0x30) {
593 case 0x00: if (c
>= 0 && c
< colors
) {
600 case 0x10: bval
= c
* 17;
603 case 0x20: rval
= c
* 17;
606 case 0x30: gval
= c
* 17;
609 } else if (fmt
== ILBM_HAM8
) {
610 int c
= (col
& 0x3f);
612 case 0x00: if (c
>= 0 && c
< colors
) {
619 case 0x40: bval
= (bval
& 3) | (c
<< 2);
622 case 0x80: rval
= (rval
& 3) | (c
<< 2);
625 case 0xc0: gval
= (rval
& 3) | (c
<< 2);
629 gval
= (col
>> 8) & 0xff;
630 bval
= (col
>> 16) & 0xff;
637 bitmsk
= bitmsk
>> 1;
643 workptr
+= lineskip
* bmhd_bitplanes
;
645 } else if ((fmt
== ILBM_NORMAL
) || (fmt
== ILBM_EHB
)) {
646 if (fmt
== ILBM_EHB
) {
647 wxLogTrace(_T("iff"), _T("Doubling CMAP for EHB mode"));
649 for (int i
=0; i
<32; i
++) {
650 pal
[3*(i
+ 32) + 0] = pal
[3*i
+ 0] >> 1;
651 pal
[3*(i
+ 32) + 1] = pal
[3*i
+ 1] >> 1;
652 pal
[3*(i
+ 32) + 2] = pal
[3*i
+ 2] >> 1;
656 byte
*pic
= picptr
; // ptr to buffer
657 const byte
*workptr
= bodyptr
; // ptr to pic, planar format
659 if (bmhd_height
< height
) {
660 height
= bmhd_height
;
663 for (int i
=0; i
< height
; i
++) {
664 byte bitmsk
= 0x80; // left most bit (mask)
665 const byte
*workptr2
= workptr
; // work ptr to source
666 for (int j
=0; j
< bmhd_width
; j
++) {
669 const byte
*workptr3
= workptr2
; // 1st byte in 1st pln
671 for (int k
=0; k
< bmhd_bitplanes
; k
++) {
672 if (*workptr3
& bitmsk
) { // if bit set in this pln
673 col
= col
+ colbit
; // add bit to chunky byte
675 workptr3
+= lineskip
; // go to next line
676 colbit
<<= 1; // shift color bit
679 if (col
>= 0 && col
< colors
) {
680 pic
[0] = pal
[3*col
+ 0];
681 pic
[1] = pal
[3*col
+ 1];
682 pic
[2] = pal
[3*col
+ 2];
684 pic
[0] = pic
[1] = pic
[2] = 0;
687 bitmsk
= bitmsk
>> 1; // shift mask to next bit
688 if (bitmsk
== 0) { // if mask is zero
689 bitmsk
= 0x80; // reset mask
690 workptr2
++; // mv ptr to next byte
694 workptr
+= lineskip
* bmhd_bitplanes
; // to next line
697 break; // unknown format
700 m_image
->w
= bmhd_width
;
702 m_image
->transparent
= bmhd_transcol
;
704 wxLogTrace(_T("iff"), _T("Loaded IFF picture %s"),
705 truncated
? "truncated" : "completely");
707 return (truncated
? wxIFF_TRUNCATED
: wxIFF_OK
);
709 wxLogTrace(_T("iff"), _T("Skipping unknown chunk '%c%c%c%c'"),
710 *dataptr
, *(dataptr
+1), *(dataptr
+2), *(dataptr
+3));
712 dataptr
= dataptr
+ 8 + chunkLen
; // skip unknown chunk
717 return wxIFF_INVFORMAT
;
722 //-----------------------------------------------------------------------------
724 //-----------------------------------------------------------------------------
726 IMPLEMENT_DYNAMIC_CLASS(wxIFFHandler
, wxImageHandler
)
730 bool wxIFFHandler::LoadFile(wxImage
*image
, wxInputStream
& stream
,
731 bool verbose
, int WXUNUSED(index
))
737 decod
= new wxIFFDecoder(&stream
);
738 error
= decod
->ReadIFF();
740 if ((error
!= wxIFF_OK
) && (error
!= wxIFF_TRUNCATED
))
746 case wxIFF_INVFORMAT
:
747 wxLogError(_("IFF: error in IFF image format."));
750 wxLogError(_("IFF: not enough memory."));
753 wxLogError(_("IFF: unknown error!!!"));
761 if ((error
== wxIFF_TRUNCATED
) && verbose
)
763 wxLogError(_("IFF: data stream seems to be truncated."));
764 /* go on; image data is OK */
767 ok
= decod
->ConvertToImage(image
);
773 bool wxIFFHandler::SaveFile(wxImage
* WXUNUSED(image
),
774 wxOutputStream
& WXUNUSED(stream
), bool verbose
)
777 wxLogDebug(wxT("IFF: the handler is read-only!!"));
782 bool wxIFFHandler::DoCanRead(wxInputStream
& stream
)
787 decod
= new wxIFFDecoder(&stream
);
788 ok
= decod
->CanRead();
794 #endif // wxUSE_STREAMS