]>
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 *picptr
;
85 unsigned char *decomp_mem
;
90 // get data of current frame
91 unsigned char* GetData() const;
92 unsigned char* GetPalette() const;
93 int GetNumColors() const;
94 unsigned int GetWidth() const;
95 unsigned int GetHeight() const;
96 int GetTransparentColour() const;
98 // constructor, destructor, etc.
99 wxIFFDecoder(wxInputStream
*s
);
100 ~wxIFFDecoder() { Destroy(); }
102 // NOTE: this function modifies the current stream position
106 bool ConvertToImage(wxImage
*image
) const;
110 //---------------------------------------------------------------------------
111 // wxIFFDecoder constructor and destructor
112 //---------------------------------------------------------------------------
114 wxIFFDecoder::wxIFFDecoder(wxInputStream
*s
)
122 void wxIFFDecoder::Destroy()
126 wxDELETEA(decomp_mem
);
129 //---------------------------------------------------------------------------
130 // Convert this image to a wxImage object
131 //---------------------------------------------------------------------------
133 // This function was designed by Vaclav Slavik
135 bool wxIFFDecoder::ConvertToImage(wxImage
*image
) const
141 image
->Create(GetWidth(), GetHeight());
146 unsigned char *pal
= GetPalette();
147 unsigned char *src
= GetData();
148 unsigned char *dst
= image
->GetData();
149 int colors
= GetNumColors();
150 int transparent
= GetTransparentColour();
153 // set transparent colour mask
154 if (transparent
!= -1)
156 for (i
= 0; i
< colors
; i
++)
158 if ((pal
[3 * i
+ 0] == 255) &&
159 (pal
[3 * i
+ 1] == 0) &&
160 (pal
[3 * i
+ 2] == 255))
162 pal
[3 * i
+ 2] = 254;
166 pal
[3 * transparent
+ 0] = 255,
167 pal
[3 * transparent
+ 1] = 0,
168 pal
[3 * transparent
+ 2] = 255;
170 image
->SetMaskColour(255, 0, 255);
173 image
->SetMask(false);
176 if (pal
&& colors
> 0)
178 unsigned char* r
= new unsigned char[colors
];
179 unsigned char* g
= new unsigned char[colors
];
180 unsigned char* b
= new unsigned char[colors
];
182 for (i
= 0; i
< colors
; i
++)
189 image
->SetPalette(wxPalette(colors
, r
, g
, b
));
195 #endif // wxUSE_PALETTE
198 for (i
= 0; i
< (long)(GetWidth() * GetHeight()); i
++, src
+= 3, dst
+= 3)
209 //---------------------------------------------------------------------------
211 //---------------------------------------------------------------------------
213 // Get data for current frame
215 unsigned char* wxIFFDecoder::GetData() const { return (m_image
->p
); }
216 unsigned char* wxIFFDecoder::GetPalette() const { return (m_image
->pal
); }
217 int wxIFFDecoder::GetNumColors() const { return m_image
->colors
; }
218 unsigned int wxIFFDecoder::GetWidth() const { return (m_image
->w
); }
219 unsigned int wxIFFDecoder::GetHeight() const { return (m_image
->h
); }
220 int wxIFFDecoder::GetTransparentColour() const { return m_image
->transparent
; }
222 //---------------------------------------------------------------------------
223 // IFF reading and decoding
224 //---------------------------------------------------------------------------
228 // Returns true if the file looks like a valid IFF, false otherwise.
230 bool wxIFFDecoder::CanRead()
232 unsigned char buf
[12];
234 if ( !m_f
->Read(buf
, WXSIZEOF(buf
)) )
237 return (memcmp(buf
, "FORM", 4) == 0) && (memcmp(buf
+8, "ILBM", 4) == 0);
242 // Based on xv source code by Thomas Meyer
243 // Permission for use in wxWidgets has been gratefully given.
245 typedef unsigned char byte
;
248 /*************************************************************************
249 void decomprle(source, destination, source length, buffer size)
251 Decompress run-length encoded data from source to destination. Terminates
252 when source is decoded completely or destination buffer is full.
254 The decruncher is as optimized as I could make it, without risking
255 safety in case of corrupt BODY chunks.
256 **************************************************************************/
258 static void decomprle(const byte
*sptr
, byte
*dptr
, long slen
, long dlen
)
260 byte codeByte
, dataByte
;
262 while ((slen
> 0) && (dlen
> 0)) {
266 if (codeByte
< 0x80) {
268 if ((slen
> (long) codeByte
) && (dlen
>= (long) codeByte
)) {
269 slen
-= codeByte
+ 1;
271 while (codeByte
> 0) {
279 else if (codeByte
> 0x80) {
280 codeByte
= 0x81 - (codeByte
& 0x7f);
281 if ((slen
> (long) 0) && (dlen
>= (long) codeByte
)) {
285 while (codeByte
> 0) {
295 /******************************************/
296 static unsigned int iff_getword(const byte
*ptr
)
305 /******************************************/
306 static unsigned long iff_getlong(const byte
*ptr
)
311 l
= (l
<< 8) + *ptr
++;
312 l
= (l
<< 8) + *ptr
++;
317 // Define internal ILBM types
318 #define ILBM_NORMAL 0
324 int wxIFFDecoder::ReadIFF()
328 m_image
= new IFFImage();
334 // compute file length
335 wxFileOffset currentPos
= m_f
->TellI();
336 if (m_f
->SeekI(0, wxFromEnd
) == wxInvalidOffset
) {
341 long filesize
= m_f
->TellI();
342 if (m_f
->SeekI(currentPos
, wxFromStart
) == wxInvalidOffset
) {
347 // allocate memory for complete file
348 if ((databuf
= new byte
[filesize
]) == 0) {
353 m_f
->Read(databuf
, filesize
);
354 const byte
*dataend
= databuf
+ filesize
;
356 // initialize work pointer. used to trace the buffer for IFF chunks
357 const byte
*dataptr
= databuf
;
359 // check for minmal size
360 if (dataptr
+ 12 > dataend
) {
362 return wxIFF_INVFORMAT
;
365 // check if we really got an IFF file
366 if (strncmp((char *)dataptr
, "FORM", 4) != 0) {
368 return wxIFF_INVFORMAT
;
371 dataptr
= dataptr
+ 8; // skip ID and length of FORM
373 // check if the IFF file is an ILBM (picture) file
374 if (strncmp((char *) dataptr
, "ILBM", 4) != 0) {
376 return wxIFF_INVFORMAT
;
379 wxLogTrace(wxT("iff"), wxT("IFF ILBM file recognized"));
381 dataptr
= dataptr
+ 4; // skip ID
384 // main decoding loop. searches IFF chunks and handles them.
385 // terminates when BODY chunk was found or dataptr ran over end of file
387 bool BMHDok
= false, CAMGok
= false;
388 int bmhd_width
= 0, bmhd_height
= 0, bmhd_bitplanes
= 0, bmhd_transcol
= -1;
389 byte bmhd_compression
= 0;
390 long camg_viewmode
= 0;
392 while (dataptr
+ 8 <= dataend
) {
393 // get chunk length and make even
394 long chunkLen
= (iff_getlong(dataptr
+ 4) + 1) & 0xfffffffe;
395 if (chunkLen
< 0) { // format error?
398 bool truncated
= (dataptr
+ 8 + chunkLen
> dataend
);
400 if (strncmp((char *)dataptr
, "BMHD", 4) == 0) { // BMHD chunk?
401 if (chunkLen
< 12 + 2 || truncated
) {
404 bmhd_width
= iff_getword(dataptr
+ 8); // width of picture
405 bmhd_height
= iff_getword(dataptr
+ 8 + 2); // height of picture
406 bmhd_bitplanes
= *(dataptr
+ 8 + 8); // # of bitplanes
407 // bmhd_masking = *(dataptr + 8 + 9); -- unused currently
408 bmhd_compression
= *(dataptr
+ 8 + 10); // get compression
409 bmhd_transcol
= iff_getword(dataptr
+ 8 + 12);
410 BMHDok
= true; // got BMHD
411 dataptr
+= 8 + chunkLen
; // to next chunk
413 else if (strncmp((char *)dataptr
, "CMAP", 4) == 0) { // CMAP ?
417 const byte
*cmapptr
= dataptr
+ 8;
418 colors
= chunkLen
/ 3; // calc no of colors
420 wxDELETE(m_image
->pal
);
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(wxT("iff"), wxT("Read %d colors from IFF file."),
440 dataptr
+= 8 + chunkLen
; // to next chunk
441 } else if (strncmp((char *)dataptr
, "CAMG", 4) == 0) { // CAMG ?
442 if (chunkLen
< 4 || truncated
) {
445 camg_viewmode
= iff_getlong(dataptr
+ 8); // get viewmodes
446 CAMGok
= true; // got CAMG
447 dataptr
+= 8 + chunkLen
; // to next chunk
449 else if (strncmp((char *)dataptr
, "BODY", 4) == 0) { // BODY ?
450 if (!BMHDok
) { // BMHD found?
453 const byte
*bodyptr
= dataptr
+ 8; // -> BODY data
456 chunkLen
= dataend
- dataptr
;
460 // if BODY is compressed, allocate buffer for decrunched BODY
461 // and decompress it (run length encoding)
463 if (bmhd_compression
== 1) {
464 // calc size of decrunch buffer - (size of the actual pic.
465 // decompressed in interleaved Amiga bitplane format)
467 size_t decomp_bufsize
= (((bmhd_width
+ 15) >> 4) << 1)
468 * bmhd_height
* bmhd_bitplanes
;
470 if ((decomp_mem
= new byte
[decomp_bufsize
]) == 0) {
475 decomprle(bodyptr
, decomp_mem
, chunkLen
, decomp_bufsize
);
476 bodyptr
= decomp_mem
; // -> uncompressed BODY
477 chunkLen
= decomp_bufsize
;
481 // the following determines the type of the ILBM file.
482 // it's either NORMAL, EHB, HAM, HAM8 or 24BIT
484 int fmt
= ILBM_NORMAL
; // assume normal ILBM
485 if (bmhd_bitplanes
== 24) {
487 } else if (bmhd_bitplanes
== 8) {
488 if (CAMGok
&& (camg_viewmode
& 0x800)) {
491 } else if ((bmhd_bitplanes
> 5) && CAMGok
) {
492 if (camg_viewmode
& 0x80) {
494 } else if (camg_viewmode
& 0x800) {
499 wxLogTrace(wxT("iff"),
500 wxT("LoadIFF: %s %dx%d, planes=%d (%d cols), comp=%d"),
501 (fmt
==ILBM_NORMAL
) ? "Normal ILBM" :
502 (fmt
==ILBM_HAM
) ? "HAM ILBM" :
503 (fmt
==ILBM_HAM8
) ? "HAM8 ILBM" :
504 (fmt
==ILBM_EHB
) ? "EHB ILBM" :
505 (fmt
==ILBM_24BIT
) ? "24BIT ILBM" : "unknown ILBM",
506 bmhd_width
, bmhd_height
, bmhd_bitplanes
,
507 1<<bmhd_bitplanes
, bmhd_compression
);
509 if ((fmt
==ILBM_NORMAL
) || (fmt
==ILBM_EHB
) || (fmt
==ILBM_HAM
)) {
510 wxLogTrace(wxT("iff"),
511 wxT("Converting CMAP from normal ILBM CMAP"));
514 case ILBM_NORMAL
: colors
= 1 << bmhd_bitplanes
; break;
515 case ILBM_EHB
: colors
= 32*2; break;
516 case ILBM_HAM
: colors
= 16; break;
519 if (colors
> m_image
->colors
) {
520 byte
*pal
= new byte
[colors
*3];
526 for (i
= 0; i
< m_image
->colors
; i
++) {
527 pal
[3*i
+ 0] = m_image
->pal
[3*i
+ 0];
528 pal
[3*i
+ 1] = m_image
->pal
[3*i
+ 1];
529 pal
[3*i
+ 2] = m_image
->pal
[3*i
+ 2];
531 for (; i
< colors
; i
++) {
538 m_image
->colors
= colors
;
541 for (int i
=0; i
< colors
; i
++) {
542 m_image
->pal
[3*i
+ 0] = (m_image
->pal
[3*i
+ 0] >> 4) * 17;
543 m_image
->pal
[3*i
+ 1] = (m_image
->pal
[3*i
+ 1] >> 4) * 17;
544 m_image
->pal
[3*i
+ 2] = (m_image
->pal
[3*i
+ 2] >> 4) * 17;
548 m_image
->p
= new byte
[bmhd_width
* bmhd_height
* 3];
549 byte
*picptr
= m_image
->p
;
555 byte
*pal
= m_image
->pal
;
556 int lineskip
= ((bmhd_width
+ 15) >> 4) << 1;
557 int height
= chunkLen
/ (lineskip
* bmhd_bitplanes
);
559 if (bmhd_height
< height
) {
560 height
= bmhd_height
;
563 if (fmt
== ILBM_HAM
|| fmt
== ILBM_HAM8
|| fmt
== ILBM_24BIT
) {
565 const byte
*workptr
= bodyptr
;
567 for (int i
=0; i
< height
; i
++) {
569 const byte
*workptr2
= workptr
;
571 // at start of each line, init RGB values to background
576 for (int j
=0; j
< bmhd_width
; j
++) {
579 const byte
*workptr3
= workptr2
;
580 for (int k
=0; k
< bmhd_bitplanes
; k
++) {
581 if (*workptr3
& bitmsk
) {
584 workptr3
+= lineskip
;
589 int c
= (col
& 0x0f);
590 switch (col
& 0x30) {
591 case 0x00: if (c
>= 0 && c
< colors
) {
598 case 0x10: bval
= c
* 17;
601 case 0x20: rval
= c
* 17;
604 case 0x30: gval
= c
* 17;
607 } else if (fmt
== ILBM_HAM8
) {
608 int c
= (col
& 0x3f);
610 case 0x00: if (c
>= 0 && c
< colors
) {
617 case 0x40: bval
= (bval
& 3) | (c
<< 2);
620 case 0x80: rval
= (rval
& 3) | (c
<< 2);
623 case 0xc0: gval
= (rval
& 3) | (c
<< 2);
627 gval
= (col
>> 8) & 0xff;
628 bval
= (col
>> 16) & 0xff;
635 bitmsk
= bitmsk
>> 1;
641 workptr
+= lineskip
* bmhd_bitplanes
;
643 } else if ((fmt
== ILBM_NORMAL
) || (fmt
== ILBM_EHB
)) {
644 if (fmt
== ILBM_EHB
) {
645 wxLogTrace(wxT("iff"), wxT("Doubling CMAP for EHB mode"));
647 for (int i
=0; i
<32; i
++) {
648 pal
[3*(i
+ 32) + 0] = pal
[3*i
+ 0] >> 1;
649 pal
[3*(i
+ 32) + 1] = pal
[3*i
+ 1] >> 1;
650 pal
[3*(i
+ 32) + 2] = pal
[3*i
+ 2] >> 1;
654 byte
*pic
= picptr
; // ptr to buffer
655 const byte
*workptr
= bodyptr
; // ptr to pic, planar format
657 if (bmhd_height
< height
) {
658 height
= bmhd_height
;
661 for (int i
=0; i
< height
; i
++) {
662 byte bitmsk
= 0x80; // left most bit (mask)
663 const byte
*workptr2
= workptr
; // work ptr to source
664 for (int j
=0; j
< bmhd_width
; j
++) {
667 const byte
*workptr3
= workptr2
; // 1st byte in 1st pln
669 for (int k
=0; k
< bmhd_bitplanes
; k
++) {
670 if (*workptr3
& bitmsk
) { // if bit set in this pln
671 col
= col
+ colbit
; // add bit to chunky byte
673 workptr3
+= lineskip
; // go to next line
674 colbit
<<= 1; // shift color bit
677 if (col
>= 0 && col
< colors
) {
678 pic
[0] = pal
[3*col
+ 0];
679 pic
[1] = pal
[3*col
+ 1];
680 pic
[2] = pal
[3*col
+ 2];
682 pic
[0] = pic
[1] = pic
[2] = 0;
685 bitmsk
= bitmsk
>> 1; // shift mask to next bit
686 if (bitmsk
== 0) { // if mask is zero
687 bitmsk
= 0x80; // reset mask
688 workptr2
++; // mv ptr to next byte
692 workptr
+= lineskip
* bmhd_bitplanes
; // to next line
695 break; // unknown format
698 m_image
->w
= bmhd_width
;
700 m_image
->transparent
= bmhd_transcol
;
702 wxLogTrace(wxT("iff"), wxT("Loaded IFF picture %s"),
703 truncated
? "truncated" : "completely");
705 return (truncated
? wxIFF_TRUNCATED
: wxIFF_OK
);
707 wxLogTrace(wxT("iff"), wxT("Skipping unknown chunk '%c%c%c%c'"),
708 *dataptr
, *(dataptr
+1), *(dataptr
+2), *(dataptr
+3));
710 dataptr
= dataptr
+ 8 + chunkLen
; // skip unknown chunk
715 return wxIFF_INVFORMAT
;
720 //-----------------------------------------------------------------------------
722 //-----------------------------------------------------------------------------
724 IMPLEMENT_DYNAMIC_CLASS(wxIFFHandler
, wxImageHandler
)
728 bool wxIFFHandler::LoadFile(wxImage
*image
, wxInputStream
& stream
,
729 bool verbose
, int WXUNUSED(index
))
735 decod
= new wxIFFDecoder(&stream
);
736 error
= decod
->ReadIFF();
738 if ((error
!= wxIFF_OK
) && (error
!= wxIFF_TRUNCATED
))
744 case wxIFF_INVFORMAT
:
745 wxLogError(_("IFF: error in IFF image format."));
748 wxLogError(_("IFF: not enough memory."));
751 wxLogError(_("IFF: unknown error!!!"));
759 if ((error
== wxIFF_TRUNCATED
) && verbose
)
761 wxLogError(_("IFF: data stream seems to be truncated."));
762 /* go on; image data is OK */
765 ok
= decod
->ConvertToImage(image
);
771 bool wxIFFHandler::SaveFile(wxImage
* WXUNUSED(image
),
772 wxOutputStream
& WXUNUSED(stream
), bool verbose
)
776 wxLogDebug(wxT("IFF: the handler is read-only!!"));
782 bool wxIFFHandler::DoCanRead(wxInputStream
& stream
)
784 wxIFFDecoder
decod(&stream
);
786 return decod
.CanRead();
787 // it's ok to modify the stream position here
790 #endif // wxUSE_STREAMS