]>
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
5 // Copyright: (c) Steffen Gutmann, 2002
6 // Licence: wxWindows licence
7 /////////////////////////////////////////////////////////////////////////////
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
12 // in wxWidgets has been gratefully given.
14 // For compilers that support precompilation, includes "wx.h".
15 #include "wx/wxprec.h"
21 #if wxUSE_IMAGE && wxUSE_IFF
28 #include "wx/imagiff.h"
29 #include "wx/wfstream.h"
32 #include "wx/palette.h"
33 #endif // wxUSE_PALETTE
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 *decomp_mem
;
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;
96 // constructor, destructor, etc.
97 wxIFFDecoder(wxInputStream
*s
);
98 ~wxIFFDecoder() { Destroy(); }
100 // NOTE: this function modifies the current stream position
104 bool ConvertToImage(wxImage
*image
) const;
108 //---------------------------------------------------------------------------
109 // wxIFFDecoder constructor and destructor
110 //---------------------------------------------------------------------------
112 wxIFFDecoder::wxIFFDecoder(wxInputStream
*s
)
120 void wxIFFDecoder::Destroy()
124 wxDELETEA(decomp_mem
);
127 //---------------------------------------------------------------------------
128 // Convert this image to a wxImage object
129 //---------------------------------------------------------------------------
131 // This function was designed by Vaclav Slavik
133 bool wxIFFDecoder::ConvertToImage(wxImage
*image
) const
139 image
->Create(GetWidth(), GetHeight());
144 unsigned char *pal
= GetPalette();
145 unsigned char *src
= GetData();
146 unsigned char *dst
= image
->GetData();
147 int colors
= GetNumColors();
148 int transparent
= GetTransparentColour();
151 // set transparent colour mask
152 if (transparent
!= -1)
154 for (i
= 0; i
< colors
; i
++)
156 if ((pal
[3 * i
+ 0] == 255) &&
157 (pal
[3 * i
+ 1] == 0) &&
158 (pal
[3 * i
+ 2] == 255))
160 pal
[3 * i
+ 2] = 254;
164 pal
[3 * transparent
+ 0] = 255,
165 pal
[3 * transparent
+ 1] = 0,
166 pal
[3 * transparent
+ 2] = 255;
168 image
->SetMaskColour(255, 0, 255);
171 image
->SetMask(false);
174 if (pal
&& colors
> 0)
176 unsigned char* r
= new unsigned char[colors
];
177 unsigned char* g
= new unsigned char[colors
];
178 unsigned char* b
= new unsigned char[colors
];
180 for (i
= 0; i
< colors
; i
++)
187 image
->SetPalette(wxPalette(colors
, r
, g
, b
));
193 #endif // wxUSE_PALETTE
196 for (i
= 0; i
< (long)(GetWidth() * GetHeight()); i
++, src
+= 3, dst
+= 3)
207 //---------------------------------------------------------------------------
209 //---------------------------------------------------------------------------
211 // Get data for current frame
213 unsigned char* wxIFFDecoder::GetData() const { return (m_image
->p
); }
214 unsigned char* wxIFFDecoder::GetPalette() const { return (m_image
->pal
); }
215 int wxIFFDecoder::GetNumColors() const { return m_image
->colors
; }
216 unsigned int wxIFFDecoder::GetWidth() const { return (m_image
->w
); }
217 unsigned int wxIFFDecoder::GetHeight() const { return (m_image
->h
); }
218 int wxIFFDecoder::GetTransparentColour() const { return m_image
->transparent
; }
220 //---------------------------------------------------------------------------
221 // IFF reading and decoding
222 //---------------------------------------------------------------------------
226 // Returns true if the file looks like a valid IFF, false otherwise.
228 bool wxIFFDecoder::CanRead()
230 unsigned char buf
[12];
232 if ( !m_f
->Read(buf
, WXSIZEOF(buf
)) )
235 return (memcmp(buf
, "FORM", 4) == 0) && (memcmp(buf
+8, "ILBM", 4) == 0);
240 // Based on xv source code by Thomas Meyer
241 // Permission for use in wxWidgets has been gratefully given.
243 typedef unsigned char byte
;
246 /*************************************************************************
247 void decomprle(source, destination, source length, buffer size)
249 Decompress run-length encoded data from source to destination. Terminates
250 when source is decoded completely or destination buffer is full.
252 The decruncher is as optimized as I could make it, without risking
253 safety in case of corrupt BODY chunks.
254 **************************************************************************/
256 static void decomprle(const byte
*sptr
, byte
*dptr
, long slen
, long dlen
)
258 byte codeByte
, dataByte
;
260 while ((slen
> 0) && (dlen
> 0)) {
264 if (codeByte
< 0x80) {
266 if ((slen
> (long) codeByte
) && (dlen
>= (long) codeByte
)) {
267 slen
-= codeByte
+ 1;
269 while (codeByte
> 0) {
277 else if (codeByte
> 0x80) {
278 codeByte
= 0x81 - (codeByte
& 0x7f);
279 if ((slen
> (long) 0) && (dlen
>= (long) codeByte
)) {
283 while (codeByte
> 0) {
293 /******************************************/
294 static unsigned int iff_getword(const byte
*ptr
)
303 /******************************************/
304 static unsigned long iff_getlong(const byte
*ptr
)
309 l
= (l
<< 8) + *ptr
++;
310 l
= (l
<< 8) + *ptr
++;
315 // Define internal ILBM types
316 #define ILBM_NORMAL 0
322 int wxIFFDecoder::ReadIFF()
326 m_image
= new IFFImage();
332 // compute file length
333 wxFileOffset currentPos
= m_f
->TellI();
334 if (m_f
->SeekI(0, wxFromEnd
) == wxInvalidOffset
) {
339 long filesize
= m_f
->TellI();
340 if (m_f
->SeekI(currentPos
, wxFromStart
) == wxInvalidOffset
) {
345 // allocate memory for complete file
346 if ((databuf
= new byte
[filesize
]) == 0) {
351 m_f
->Read(databuf
, filesize
);
352 const byte
*dataend
= databuf
+ filesize
;
354 // initialize work pointer. used to trace the buffer for IFF chunks
355 const byte
*dataptr
= databuf
;
357 // check for minmal size
358 if (dataptr
+ 12 > dataend
) {
360 return wxIFF_INVFORMAT
;
363 // check if we really got an IFF file
364 if (strncmp((char *)dataptr
, "FORM", 4) != 0) {
366 return wxIFF_INVFORMAT
;
369 dataptr
= dataptr
+ 8; // skip ID and length of FORM
371 // check if the IFF file is an ILBM (picture) file
372 if (strncmp((char *) dataptr
, "ILBM", 4) != 0) {
374 return wxIFF_INVFORMAT
;
377 wxLogTrace(wxT("iff"), wxT("IFF ILBM file recognized"));
379 dataptr
= dataptr
+ 4; // skip ID
382 // main decoding loop. searches IFF chunks and handles them.
383 // terminates when BODY chunk was found or dataptr ran over end of file
385 bool BMHDok
= false, CAMGok
= false;
386 int bmhd_width
= 0, bmhd_height
= 0, bmhd_bitplanes
= 0, bmhd_transcol
= -1;
387 byte bmhd_compression
= 0;
388 long camg_viewmode
= 0;
390 while (dataptr
+ 8 <= dataend
) {
391 // get chunk length and make even
392 long chunkLen
= (iff_getlong(dataptr
+ 4) + 1) & 0xfffffffe;
393 if (chunkLen
< 0) { // format error?
396 bool truncated
= (dataptr
+ 8 + chunkLen
> dataend
);
398 if (strncmp((char *)dataptr
, "BMHD", 4) == 0) { // BMHD chunk?
399 if (chunkLen
< 12 + 2 || truncated
) {
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
405 // bmhd_masking = *(dataptr + 8 + 9); -- unused currently
406 bmhd_compression
= *(dataptr
+ 8 + 10); // get compression
407 bmhd_transcol
= iff_getword(dataptr
+ 8 + 12);
408 BMHDok
= true; // got BMHD
409 dataptr
+= 8 + chunkLen
; // to next chunk
411 else if (strncmp((char *)dataptr
, "CMAP", 4) == 0) { // CMAP ?
415 const byte
*cmapptr
= dataptr
+ 8;
416 colors
= chunkLen
/ 3; // calc no of colors
418 wxDELETE(m_image
->pal
);
419 m_image
->colors
= colors
;
421 m_image
->pal
= new byte
[3*colors
];
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
++;
435 wxLogTrace(wxT("iff"), wxT("Read %d colors from IFF file."),
438 dataptr
+= 8 + chunkLen
; // to next chunk
439 } else if (strncmp((char *)dataptr
, "CAMG", 4) == 0) { // CAMG ?
440 if (chunkLen
< 4 || truncated
) {
443 camg_viewmode
= iff_getlong(dataptr
+ 8); // get viewmodes
444 CAMGok
= true; // got CAMG
445 dataptr
+= 8 + chunkLen
; // to next chunk
447 else if (strncmp((char *)dataptr
, "BODY", 4) == 0) { // BODY ?
448 if (!BMHDok
) { // BMHD found?
451 const byte
*bodyptr
= dataptr
+ 8; // -> BODY data
454 chunkLen
= dataend
- dataptr
;
458 // if BODY is compressed, allocate buffer for decrunched BODY
459 // and decompress it (run length encoding)
461 if (bmhd_compression
== 1) {
462 // calc size of decrunch buffer - (size of the actual pic.
463 // decompressed in interleaved Amiga bitplane format)
465 size_t decomp_bufsize
= (((bmhd_width
+ 15) >> 4) << 1)
466 * bmhd_height
* bmhd_bitplanes
;
468 if ((decomp_mem
= new byte
[decomp_bufsize
]) == 0) {
473 decomprle(bodyptr
, decomp_mem
, chunkLen
, decomp_bufsize
);
474 bodyptr
= decomp_mem
; // -> uncompressed BODY
475 chunkLen
= decomp_bufsize
;
479 // the following determines the type of the ILBM file.
480 // it's either NORMAL, EHB, HAM, HAM8 or 24BIT
482 int fmt
= ILBM_NORMAL
; // assume normal ILBM
483 if (bmhd_bitplanes
== 24) {
485 } else if (bmhd_bitplanes
== 8) {
486 if (CAMGok
&& (camg_viewmode
& 0x800)) {
489 } else if ((bmhd_bitplanes
> 5) && CAMGok
) {
490 if (camg_viewmode
& 0x80) {
492 } else if (camg_viewmode
& 0x800) {
497 wxLogTrace(wxT("iff"),
498 wxT("LoadIFF: %s %dx%d, planes=%d (%d cols), comp=%d"),
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
);
507 if ((fmt
==ILBM_NORMAL
) || (fmt
==ILBM_EHB
) || (fmt
==ILBM_HAM
)) {
508 wxLogTrace(wxT("iff"),
509 wxT("Converting CMAP from normal ILBM CMAP"));
512 case ILBM_NORMAL
: colors
= 1 << bmhd_bitplanes
; break;
513 case ILBM_EHB
: colors
= 32*2; break;
514 case ILBM_HAM
: colors
= 16; break;
517 if (colors
> m_image
->colors
) {
518 byte
*pal
= new byte
[colors
*3];
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];
529 for (; i
< colors
; i
++) {
536 m_image
->colors
= colors
;
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;
546 m_image
->p
= new byte
[bmhd_width
* bmhd_height
* 3];
547 byte
*picptr
= m_image
->p
;
553 byte
*pal
= m_image
->pal
;
554 int lineskip
= ((bmhd_width
+ 15) >> 4) << 1;
555 int height
= chunkLen
/ (lineskip
* bmhd_bitplanes
);
557 if (bmhd_height
< height
) {
558 height
= bmhd_height
;
561 if (fmt
== ILBM_HAM
|| fmt
== ILBM_HAM8
|| fmt
== ILBM_24BIT
) {
563 const byte
*workptr
= bodyptr
;
565 for (int i
=0; i
< height
; i
++) {
567 const byte
*workptr2
= workptr
;
569 // at start of each line, init RGB values to background
574 for (int j
=0; j
< bmhd_width
; j
++) {
577 const byte
*workptr3
= workptr2
;
578 for (int k
=0; k
< bmhd_bitplanes
; k
++) {
579 if (*workptr3
& bitmsk
) {
582 workptr3
+= lineskip
;
587 int c
= (col
& 0x0f);
588 switch (col
& 0x30) {
589 case 0x00: if (c
>= 0 && c
< colors
) {
596 case 0x10: bval
= c
* 17;
599 case 0x20: rval
= c
* 17;
602 case 0x30: gval
= c
* 17;
605 } else if (fmt
== ILBM_HAM8
) {
606 int c
= (col
& 0x3f);
608 case 0x00: if (c
>= 0 && c
< colors
) {
615 case 0x40: bval
= (bval
& 3) | (c
<< 2);
618 case 0x80: rval
= (rval
& 3) | (c
<< 2);
621 case 0xc0: gval
= (rval
& 3) | (c
<< 2);
625 gval
= (col
>> 8) & 0xff;
626 bval
= (col
>> 16) & 0xff;
633 bitmsk
= bitmsk
>> 1;
639 workptr
+= lineskip
* bmhd_bitplanes
;
641 } else if ((fmt
== ILBM_NORMAL
) || (fmt
== ILBM_EHB
)) {
642 if (fmt
== ILBM_EHB
) {
643 wxLogTrace(wxT("iff"), wxT("Doubling CMAP for EHB mode"));
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;
652 byte
*pic
= picptr
; // ptr to buffer
653 const byte
*workptr
= bodyptr
; // ptr to pic, planar format
655 if (bmhd_height
< height
) {
656 height
= bmhd_height
;
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
++) {
665 const byte
*workptr3
= workptr2
; // 1st byte in 1st pln
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
671 workptr3
+= lineskip
; // go to next line
672 colbit
<<= 1; // shift color bit
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];
680 pic
[0] = pic
[1] = pic
[2] = 0;
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
690 workptr
+= lineskip
* bmhd_bitplanes
; // to next line
693 break; // unknown format
696 m_image
->w
= bmhd_width
;
698 m_image
->transparent
= bmhd_transcol
;
700 wxLogTrace(wxT("iff"), wxT("Loaded IFF picture %s"),
701 truncated
? "truncated" : "completely");
703 return (truncated
? wxIFF_TRUNCATED
: wxIFF_OK
);
705 wxLogTrace(wxT("iff"), wxT("Skipping unknown chunk '%c%c%c%c'"),
706 *dataptr
, *(dataptr
+1), *(dataptr
+2), *(dataptr
+3));
708 dataptr
= dataptr
+ 8 + chunkLen
; // skip unknown chunk
713 return wxIFF_INVFORMAT
;
718 //-----------------------------------------------------------------------------
720 //-----------------------------------------------------------------------------
722 IMPLEMENT_DYNAMIC_CLASS(wxIFFHandler
, wxImageHandler
)
726 bool wxIFFHandler::LoadFile(wxImage
*image
, wxInputStream
& stream
,
727 bool verbose
, int WXUNUSED(index
))
733 decod
= new wxIFFDecoder(&stream
);
734 error
= decod
->ReadIFF();
736 if ((error
!= wxIFF_OK
) && (error
!= wxIFF_TRUNCATED
))
742 case wxIFF_INVFORMAT
:
743 wxLogError(_("IFF: error in IFF image format."));
746 wxLogError(_("IFF: not enough memory."));
749 wxLogError(_("IFF: unknown error!!!"));
757 if ((error
== wxIFF_TRUNCATED
) && verbose
)
759 wxLogError(_("IFF: data stream seems to be truncated."));
760 /* go on; image data is OK */
763 ok
= decod
->ConvertToImage(image
);
769 bool wxIFFHandler::SaveFile(wxImage
* WXUNUSED(image
),
770 wxOutputStream
& WXUNUSED(stream
), bool verbose
)
774 wxLogDebug(wxT("IFF: the handler is read-only!!"));
780 bool wxIFFHandler::DoCanRead(wxInputStream
& stream
)
782 wxIFFDecoder
decod(&stream
);
784 return decod
.CanRead();
785 // it's ok to modify the stream position here
788 #endif // wxUSE_STREAMS