]>
git.saurik.com Git - wxWidgets.git/blob - src/common/imagiff.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/imagiff.cpp
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 wxWidgets has been gratefully given.
15 // For compilers that support precompilation, includes "wx.h".
16 #include "wx/wxprec.h"
22 #if wxUSE_IMAGE && wxUSE_IFF
29 #include "wx/imagiff.h"
30 #include "wx/wfstream.h"
33 #include "wx/palette.h"
34 #endif // wxUSE_PALETTE
40 // --------------------------------------------------------------------------
42 // --------------------------------------------------------------------------
45 // Note that the error code wxIFF_TRUNCATED means that the image itself
46 // is most probably OK, but the decoder didn't reach the end of the data
47 // stream; this means that if it was not reading directly from file,
48 // the stream will not be correctly positioned.
53 wxIFF_OK
= 0, /* everything was OK */
54 wxIFF_INVFORMAT
, /* error in iff header */
55 wxIFF_MEMERR
, /* error allocating memory */
56 wxIFF_TRUNCATED
/* file appears to be truncated */
59 // --------------------------------------------------------------------------
61 // --------------------------------------------------------------------------
63 // internal class for storing IFF image data
67 unsigned int w
; /* width */
68 unsigned int h
; /* height */
69 int transparent
; /* transparent color (-1 = none) */
70 int colors
; /* number of colors */
71 unsigned char *p
; /* bitmap */
72 unsigned char *pal
; /* palette */
74 IFFImage() : w(0), h(0), colors(0), p(0), pal(0) {}
75 ~IFFImage() { delete [] p
; delete [] pal
; }
78 class WXDLLEXPORT wxIFFDecoder
81 IFFImage
*m_image
; // image data
82 wxInputStream
*m_f
; // input stream
83 unsigned char *databuf
;
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(); }
101 // NOTE: this function modifies the current stream position
105 bool ConvertToImage(wxImage
*image
) const;
109 //---------------------------------------------------------------------------
110 // wxIFFDecoder constructor and destructor
111 //---------------------------------------------------------------------------
113 wxIFFDecoder::wxIFFDecoder(wxInputStream
*s
)
121 void wxIFFDecoder::Destroy()
125 wxDELETEA(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];
233 if ( !m_f
->Read(buf
, WXSIZEOF(buf
)) )
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 wxWidgets 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 wxFileOffset currentPos
= m_f
->TellI();
335 if (m_f
->SeekI(0, wxFromEnd
) == wxInvalidOffset
) {
340 long filesize
= m_f
->TellI();
341 if (m_f
->SeekI(currentPos
, wxFromStart
) == wxInvalidOffset
) {
346 // allocate memory for complete file
347 if ((databuf
= new byte
[filesize
]) == 0) {
352 m_f
->Read(databuf
, filesize
);
353 const byte
*dataend
= databuf
+ filesize
;
355 // initialize work pointer. used to trace the buffer for IFF chunks
356 const byte
*dataptr
= databuf
;
358 // check for minmal size
359 if (dataptr
+ 12 > dataend
) {
361 return wxIFF_INVFORMAT
;
364 // check if we really got an IFF file
365 if (strncmp((char *)dataptr
, "FORM", 4) != 0) {
367 return wxIFF_INVFORMAT
;
370 dataptr
= dataptr
+ 8; // skip ID and length of FORM
372 // check if the IFF file is an ILBM (picture) file
373 if (strncmp((char *) dataptr
, "ILBM", 4) != 0) {
375 return wxIFF_INVFORMAT
;
378 wxLogTrace(wxT("iff"), wxT("IFF ILBM file recognized"));
380 dataptr
= dataptr
+ 4; // skip ID
383 // main decoding loop. searches IFF chunks and handles them.
384 // terminates when BODY chunk was found or dataptr ran over end of file
386 bool BMHDok
= false, CAMGok
= false;
387 int bmhd_width
= 0, bmhd_height
= 0, bmhd_bitplanes
= 0, bmhd_transcol
= -1;
388 byte bmhd_compression
= 0;
389 long camg_viewmode
= 0;
391 while (dataptr
+ 8 <= dataend
) {
392 // get chunk length and make even
393 long chunkLen
= (iff_getlong(dataptr
+ 4) + 1) & 0xfffffffe;
394 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); -- unused currently
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
419 wxDELETE(m_image
->pal
);
420 m_image
->colors
= colors
;
422 m_image
->pal
= new byte
[3*colors
];
428 // copy colors to color map
429 for (int i
=0; i
< colors
; i
++) {
430 m_image
->pal
[3*i
+ 0] = *cmapptr
++;
431 m_image
->pal
[3*i
+ 1] = *cmapptr
++;
432 m_image
->pal
[3*i
+ 2] = *cmapptr
++;
436 wxLogTrace(wxT("iff"), wxT("Read %d colors from IFF file."),
439 dataptr
+= 8 + chunkLen
; // to next chunk
440 } else if (strncmp((char *)dataptr
, "CAMG", 4) == 0) { // CAMG ?
441 if (chunkLen
< 4 || truncated
) {
444 camg_viewmode
= iff_getlong(dataptr
+ 8); // get viewmodes
445 CAMGok
= true; // got CAMG
446 dataptr
+= 8 + chunkLen
; // to next chunk
448 else if (strncmp((char *)dataptr
, "BODY", 4) == 0) { // BODY ?
449 if (!BMHDok
) { // BMHD found?
452 const byte
*bodyptr
= dataptr
+ 8; // -> BODY data
455 chunkLen
= dataend
- dataptr
;
459 // if BODY is compressed, allocate buffer for decrunched BODY
460 // and decompress it (run length encoding)
462 if (bmhd_compression
== 1) {
463 // calc size of decrunch buffer - (size of the actual pic.
464 // decompressed in interleaved Amiga bitplane format)
466 size_t decomp_bufsize
= (((bmhd_width
+ 15) >> 4) << 1)
467 * bmhd_height
* bmhd_bitplanes
;
469 if ((decomp_mem
= new byte
[decomp_bufsize
]) == 0) {
474 decomprle(bodyptr
, decomp_mem
, chunkLen
, decomp_bufsize
);
475 bodyptr
= decomp_mem
; // -> uncompressed BODY
476 chunkLen
= decomp_bufsize
;
480 // the following determines the type of the ILBM file.
481 // it's either NORMAL, EHB, HAM, HAM8 or 24BIT
483 int fmt
= ILBM_NORMAL
; // assume normal ILBM
484 if (bmhd_bitplanes
== 24) {
486 } else if (bmhd_bitplanes
== 8) {
487 if (CAMGok
&& (camg_viewmode
& 0x800)) {
490 } else if ((bmhd_bitplanes
> 5) && CAMGok
) {
491 if (camg_viewmode
& 0x80) {
493 } else if (camg_viewmode
& 0x800) {
498 wxLogTrace(wxT("iff"),
499 wxT("LoadIFF: %s %dx%d, planes=%d (%d cols), comp=%d"),
500 (fmt
==ILBM_NORMAL
) ? "Normal ILBM" :
501 (fmt
==ILBM_HAM
) ? "HAM ILBM" :
502 (fmt
==ILBM_HAM8
) ? "HAM8 ILBM" :
503 (fmt
==ILBM_EHB
) ? "EHB ILBM" :
504 (fmt
==ILBM_24BIT
) ? "24BIT ILBM" : "unknown ILBM",
505 bmhd_width
, bmhd_height
, bmhd_bitplanes
,
506 1<<bmhd_bitplanes
, bmhd_compression
);
508 if ((fmt
==ILBM_NORMAL
) || (fmt
==ILBM_EHB
) || (fmt
==ILBM_HAM
)) {
509 wxLogTrace(wxT("iff"),
510 wxT("Converting CMAP from normal ILBM CMAP"));
513 case ILBM_NORMAL
: colors
= 1 << bmhd_bitplanes
; break;
514 case ILBM_EHB
: colors
= 32*2; break;
515 case ILBM_HAM
: colors
= 16; break;
518 if (colors
> m_image
->colors
) {
519 byte
*pal
= new byte
[colors
*3];
525 for (i
= 0; i
< m_image
->colors
; i
++) {
526 pal
[3*i
+ 0] = m_image
->pal
[3*i
+ 0];
527 pal
[3*i
+ 1] = m_image
->pal
[3*i
+ 1];
528 pal
[3*i
+ 2] = m_image
->pal
[3*i
+ 2];
530 for (; i
< colors
; i
++) {
537 m_image
->colors
= colors
;
540 for (int i
=0; i
< colors
; i
++) {
541 m_image
->pal
[3*i
+ 0] = (m_image
->pal
[3*i
+ 0] >> 4) * 17;
542 m_image
->pal
[3*i
+ 1] = (m_image
->pal
[3*i
+ 1] >> 4) * 17;
543 m_image
->pal
[3*i
+ 2] = (m_image
->pal
[3*i
+ 2] >> 4) * 17;
547 m_image
->p
= new byte
[bmhd_width
* bmhd_height
* 3];
548 byte
*picptr
= m_image
->p
;
554 byte
*pal
= m_image
->pal
;
555 int lineskip
= ((bmhd_width
+ 15) >> 4) << 1;
556 int height
= chunkLen
/ (lineskip
* bmhd_bitplanes
);
558 if (bmhd_height
< height
) {
559 height
= bmhd_height
;
562 if (fmt
== ILBM_HAM
|| fmt
== ILBM_HAM8
|| fmt
== ILBM_24BIT
) {
564 const byte
*workptr
= bodyptr
;
566 for (int i
=0; i
< height
; i
++) {
568 const byte
*workptr2
= workptr
;
570 // at start of each line, init RGB values to background
575 for (int j
=0; j
< bmhd_width
; j
++) {
578 const byte
*workptr3
= workptr2
;
579 for (int k
=0; k
< bmhd_bitplanes
; k
++) {
580 if (*workptr3
& bitmsk
) {
583 workptr3
+= lineskip
;
588 int c
= (col
& 0x0f);
589 switch (col
& 0x30) {
590 case 0x00: if (c
>= 0 && c
< colors
) {
597 case 0x10: bval
= c
* 17;
600 case 0x20: rval
= c
* 17;
603 case 0x30: gval
= c
* 17;
606 } else if (fmt
== ILBM_HAM8
) {
607 int c
= (col
& 0x3f);
609 case 0x00: if (c
>= 0 && c
< colors
) {
616 case 0x40: bval
= (bval
& 3) | (c
<< 2);
619 case 0x80: rval
= (rval
& 3) | (c
<< 2);
622 case 0xc0: gval
= (rval
& 3) | (c
<< 2);
626 gval
= (col
>> 8) & 0xff;
627 bval
= (col
>> 16) & 0xff;
634 bitmsk
= bitmsk
>> 1;
640 workptr
+= lineskip
* bmhd_bitplanes
;
642 } else if ((fmt
== ILBM_NORMAL
) || (fmt
== ILBM_EHB
)) {
643 if (fmt
== ILBM_EHB
) {
644 wxLogTrace(wxT("iff"), wxT("Doubling CMAP for EHB mode"));
646 for (int i
=0; i
<32; i
++) {
647 pal
[3*(i
+ 32) + 0] = pal
[3*i
+ 0] >> 1;
648 pal
[3*(i
+ 32) + 1] = pal
[3*i
+ 1] >> 1;
649 pal
[3*(i
+ 32) + 2] = pal
[3*i
+ 2] >> 1;
653 byte
*pic
= picptr
; // ptr to buffer
654 const byte
*workptr
= bodyptr
; // ptr to pic, planar format
656 if (bmhd_height
< height
) {
657 height
= bmhd_height
;
660 for (int i
=0; i
< height
; i
++) {
661 byte bitmsk
= 0x80; // left most bit (mask)
662 const byte
*workptr2
= workptr
; // work ptr to source
663 for (int j
=0; j
< bmhd_width
; j
++) {
666 const byte
*workptr3
= workptr2
; // 1st byte in 1st pln
668 for (int k
=0; k
< bmhd_bitplanes
; k
++) {
669 if (*workptr3
& bitmsk
) { // if bit set in this pln
670 col
= col
+ colbit
; // add bit to chunky byte
672 workptr3
+= lineskip
; // go to next line
673 colbit
<<= 1; // shift color bit
676 if (col
>= 0 && col
< colors
) {
677 pic
[0] = pal
[3*col
+ 0];
678 pic
[1] = pal
[3*col
+ 1];
679 pic
[2] = pal
[3*col
+ 2];
681 pic
[0] = pic
[1] = pic
[2] = 0;
684 bitmsk
= bitmsk
>> 1; // shift mask to next bit
685 if (bitmsk
== 0) { // if mask is zero
686 bitmsk
= 0x80; // reset mask
687 workptr2
++; // mv ptr to next byte
691 workptr
+= lineskip
* bmhd_bitplanes
; // to next line
694 break; // unknown format
697 m_image
->w
= bmhd_width
;
699 m_image
->transparent
= bmhd_transcol
;
701 wxLogTrace(wxT("iff"), wxT("Loaded IFF picture %s"),
702 truncated
? "truncated" : "completely");
704 return (truncated
? wxIFF_TRUNCATED
: wxIFF_OK
);
706 wxLogTrace(wxT("iff"), wxT("Skipping unknown chunk '%c%c%c%c'"),
707 *dataptr
, *(dataptr
+1), *(dataptr
+2), *(dataptr
+3));
709 dataptr
= dataptr
+ 8 + chunkLen
; // skip unknown chunk
714 return wxIFF_INVFORMAT
;
719 //-----------------------------------------------------------------------------
721 //-----------------------------------------------------------------------------
723 IMPLEMENT_DYNAMIC_CLASS(wxIFFHandler
, wxImageHandler
)
727 bool wxIFFHandler::LoadFile(wxImage
*image
, wxInputStream
& stream
,
728 bool verbose
, int WXUNUSED(index
))
734 decod
= new wxIFFDecoder(&stream
);
735 error
= decod
->ReadIFF();
737 if ((error
!= wxIFF_OK
) && (error
!= wxIFF_TRUNCATED
))
743 case wxIFF_INVFORMAT
:
744 wxLogError(_("IFF: error in IFF image format."));
747 wxLogError(_("IFF: not enough memory."));
750 wxLogError(_("IFF: unknown error!!!"));
758 if ((error
== wxIFF_TRUNCATED
) && verbose
)
760 wxLogError(_("IFF: data stream seems to be truncated."));
761 /* go on; image data is OK */
764 ok
= decod
->ConvertToImage(image
);
770 bool wxIFFHandler::SaveFile(wxImage
* WXUNUSED(image
),
771 wxOutputStream
& WXUNUSED(stream
), bool verbose
)
775 wxLogDebug(wxT("IFF: the handler is read-only!!"));
781 bool wxIFFHandler::DoCanRead(wxInputStream
& stream
)
783 wxIFFDecoder
decod(&stream
);
785 return decod
.CanRead();
786 // it's ok to modify the stream position here
789 #endif // wxUSE_STREAMS