]>
git.saurik.com Git - wxWidgets.git/blob - src/common/imagiff.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/imagiff.h
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()
128 delete [] decomp_mem
;
132 //---------------------------------------------------------------------------
133 // Convert this image to a wxImage object
134 //---------------------------------------------------------------------------
136 // This function was designed by Vaclav Slavik
138 bool wxIFFDecoder::ConvertToImage(wxImage
*image
) const
144 image
->Create(GetWidth(), GetHeight());
149 unsigned char *pal
= GetPalette();
150 unsigned char *src
= GetData();
151 unsigned char *dst
= image
->GetData();
152 int colors
= GetNumColors();
153 int transparent
= GetTransparentColour();
156 // set transparent colour mask
157 if (transparent
!= -1)
159 for (i
= 0; i
< colors
; i
++)
161 if ((pal
[3 * i
+ 0] == 255) &&
162 (pal
[3 * i
+ 1] == 0) &&
163 (pal
[3 * i
+ 2] == 255))
165 pal
[3 * i
+ 2] = 254;
169 pal
[3 * transparent
+ 0] = 255,
170 pal
[3 * transparent
+ 1] = 0,
171 pal
[3 * transparent
+ 2] = 255;
173 image
->SetMaskColour(255, 0, 255);
176 image
->SetMask(false);
179 if (pal
&& colors
> 0)
181 unsigned char* r
= new unsigned char[colors
];
182 unsigned char* g
= new unsigned char[colors
];
183 unsigned char* b
= new unsigned char[colors
];
185 for (i
= 0; i
< colors
; i
++)
192 image
->SetPalette(wxPalette(colors
, r
, g
, b
));
198 #endif // wxUSE_PALETTE
201 for (i
= 0; i
< (long)(GetWidth() * GetHeight()); i
++, src
+= 3, dst
+= 3)
212 //---------------------------------------------------------------------------
214 //---------------------------------------------------------------------------
216 // Get data for current frame
218 unsigned char* wxIFFDecoder::GetData() const { return (m_image
->p
); }
219 unsigned char* wxIFFDecoder::GetPalette() const { return (m_image
->pal
); }
220 int wxIFFDecoder::GetNumColors() const { return m_image
->colors
; }
221 unsigned int wxIFFDecoder::GetWidth() const { return (m_image
->w
); }
222 unsigned int wxIFFDecoder::GetHeight() const { return (m_image
->h
); }
223 int wxIFFDecoder::GetTransparentColour() const { return m_image
->transparent
; }
225 //---------------------------------------------------------------------------
226 // IFF reading and decoding
227 //---------------------------------------------------------------------------
231 // Returns true if the file looks like a valid IFF, false otherwise.
233 bool wxIFFDecoder::CanRead()
235 unsigned char buf
[12];
237 if ( !m_f
->Read(buf
, WXSIZEOF(buf
)) )
240 return (memcmp(buf
, "FORM", 4) == 0) && (memcmp(buf
+8, "ILBM", 4) == 0);
245 // Based on xv source code by Thomas Meyer
246 // Permission for use in wxWidgets has been gratefully given.
248 typedef unsigned char byte
;
251 /*************************************************************************
252 void decomprle(source, destination, source length, buffer size)
254 Decompress run-length encoded data from source to destination. Terminates
255 when source is decoded completely or destination buffer is full.
257 The decruncher is as optimized as I could make it, without risking
258 safety in case of corrupt BODY chunks.
259 **************************************************************************/
261 static void decomprle(const byte
*sptr
, byte
*dptr
, long slen
, long dlen
)
263 byte codeByte
, dataByte
;
265 while ((slen
> 0) && (dlen
> 0)) {
269 if (codeByte
< 0x80) {
271 if ((slen
> (long) codeByte
) && (dlen
>= (long) codeByte
)) {
272 slen
-= codeByte
+ 1;
274 while (codeByte
> 0) {
282 else if (codeByte
> 0x80) {
283 codeByte
= 0x81 - (codeByte
& 0x7f);
284 if ((slen
> (long) 0) && (dlen
>= (long) codeByte
)) {
288 while (codeByte
> 0) {
298 /******************************************/
299 static unsigned int iff_getword(const byte
*ptr
)
308 /******************************************/
309 static unsigned long iff_getlong(const byte
*ptr
)
314 l
= (l
<< 8) + *ptr
++;
315 l
= (l
<< 8) + *ptr
++;
320 // Define internal ILBM types
321 #define ILBM_NORMAL 0
327 int wxIFFDecoder::ReadIFF()
331 m_image
= new IFFImage();
337 // compute file length
338 wxFileOffset currentPos
= m_f
->TellI();
339 if (m_f
->SeekI(0, wxFromEnd
) == wxInvalidOffset
) {
344 long filesize
= m_f
->TellI();
345 if (m_f
->SeekI(currentPos
, wxFromStart
) == wxInvalidOffset
) {
350 // allocate memory for complete file
351 if ((databuf
= new byte
[filesize
]) == 0) {
356 m_f
->Read(databuf
, filesize
);
357 const byte
*dataend
= databuf
+ filesize
;
359 // initialize work pointer. used to trace the buffer for IFF chunks
360 const byte
*dataptr
= databuf
;
362 // check for minmal size
363 if (dataptr
+ 12 > dataend
) {
365 return wxIFF_INVFORMAT
;
368 // check if we really got an IFF file
369 if (strncmp((char *)dataptr
, "FORM", 4) != 0) {
371 return wxIFF_INVFORMAT
;
374 dataptr
= dataptr
+ 8; // skip ID and length of FORM
376 // check if the IFF file is an ILBM (picture) file
377 if (strncmp((char *) dataptr
, "ILBM", 4) != 0) {
379 return wxIFF_INVFORMAT
;
382 wxLogTrace(_T("iff"), _T("IFF ILBM file recognized"));
384 dataptr
= dataptr
+ 4; // skip ID
387 // main decoding loop. searches IFF chunks and handles them.
388 // terminates when BODY chunk was found or dataptr ran over end of file
390 bool BMHDok
= false, CAMGok
= false;
391 int bmhd_width
= 0, bmhd_height
= 0, bmhd_bitplanes
= 0, bmhd_transcol
= -1;
392 byte bmhd_compression
= 0;
393 long camg_viewmode
= 0;
395 while (dataptr
+ 8 <= dataend
) {
396 // get chunk length and make even
397 long chunkLen
= (iff_getlong(dataptr
+ 4) + 1) & 0xfffffffe;
398 if (chunkLen
< 0) { // format error?
401 bool truncated
= (dataptr
+ 8 + chunkLen
> dataend
);
403 if (strncmp((char *)dataptr
, "BMHD", 4) == 0) { // BMHD chunk?
404 if (chunkLen
< 12 + 2 || truncated
) {
407 bmhd_width
= iff_getword(dataptr
+ 8); // width of picture
408 bmhd_height
= iff_getword(dataptr
+ 8 + 2); // height of picture
409 bmhd_bitplanes
= *(dataptr
+ 8 + 8); // # of bitplanes
410 // bmhd_masking = *(dataptr + 8 + 9); -- unused currently
411 bmhd_compression
= *(dataptr
+ 8 + 10); // get compression
412 bmhd_transcol
= iff_getword(dataptr
+ 8 + 12);
413 BMHDok
= true; // got BMHD
414 dataptr
+= 8 + chunkLen
; // to next chunk
416 else if (strncmp((char *)dataptr
, "CMAP", 4) == 0) { // CMAP ?
420 const byte
*cmapptr
= dataptr
+ 8;
421 colors
= chunkLen
/ 3; // calc no of colors
425 m_image
->colors
= colors
;
427 m_image
->pal
= new byte
[3*colors
];
433 // copy colors to color map
434 for (int i
=0; i
< colors
; i
++) {
435 m_image
->pal
[3*i
+ 0] = *cmapptr
++;
436 m_image
->pal
[3*i
+ 1] = *cmapptr
++;
437 m_image
->pal
[3*i
+ 2] = *cmapptr
++;
441 wxLogTrace(_T("iff"), _T("Read %d colors from IFF file."),
444 dataptr
+= 8 + chunkLen
; // to next chunk
445 } else if (strncmp((char *)dataptr
, "CAMG", 4) == 0) { // CAMG ?
446 if (chunkLen
< 4 || truncated
) {
449 camg_viewmode
= iff_getlong(dataptr
+ 8); // get viewmodes
450 CAMGok
= true; // got CAMG
451 dataptr
+= 8 + chunkLen
; // to next chunk
453 else if (strncmp((char *)dataptr
, "BODY", 4) == 0) { // BODY ?
454 if (!BMHDok
) { // BMHD found?
457 const byte
*bodyptr
= dataptr
+ 8; // -> BODY data
460 chunkLen
= dataend
- dataptr
;
464 // if BODY is compressed, allocate buffer for decrunched BODY
465 // and decompress it (run length encoding)
467 if (bmhd_compression
== 1) {
468 // calc size of decrunch buffer - (size of the actual pic.
469 // decompressed in interleaved Amiga bitplane format)
471 size_t decomp_bufsize
= (((bmhd_width
+ 15) >> 4) << 1)
472 * bmhd_height
* bmhd_bitplanes
;
474 if ((decomp_mem
= new byte
[decomp_bufsize
]) == 0) {
479 decomprle(bodyptr
, decomp_mem
, chunkLen
, decomp_bufsize
);
480 bodyptr
= decomp_mem
; // -> uncompressed BODY
481 chunkLen
= decomp_bufsize
;
486 // the following determines the type of the ILBM file.
487 // it's either NORMAL, EHB, HAM, HAM8 or 24BIT
489 int fmt
= ILBM_NORMAL
; // assume normal ILBM
490 if (bmhd_bitplanes
== 24) {
492 } else if (bmhd_bitplanes
== 8) {
493 if (CAMGok
&& (camg_viewmode
& 0x800)) {
496 } else if ((bmhd_bitplanes
> 5) && CAMGok
) {
497 if (camg_viewmode
& 0x80) {
499 } else if (camg_viewmode
& 0x800) {
504 wxLogTrace(_T("iff"),
505 _T("LoadIFF: %s %dx%d, planes=%d (%d cols), comp=%d"),
506 (fmt
==ILBM_NORMAL
) ? "Normal ILBM" :
507 (fmt
==ILBM_HAM
) ? "HAM ILBM" :
508 (fmt
==ILBM_HAM8
) ? "HAM8 ILBM" :
509 (fmt
==ILBM_EHB
) ? "EHB ILBM" :
510 (fmt
==ILBM_24BIT
) ? "24BIT ILBM" : "unknown ILBM",
511 bmhd_width
, bmhd_height
, bmhd_bitplanes
,
512 1<<bmhd_bitplanes
, bmhd_compression
);
514 if ((fmt
==ILBM_NORMAL
) || (fmt
==ILBM_EHB
) || (fmt
==ILBM_HAM
)) {
515 wxLogTrace(_T("iff"),
516 _T("Converting CMAP from normal ILBM CMAP"));
519 case ILBM_NORMAL
: colors
= 1 << bmhd_bitplanes
; break;
520 case ILBM_EHB
: colors
= 32*2; break;
521 case ILBM_HAM
: colors
= 16; break;
524 if (colors
> m_image
->colors
) {
525 byte
*pal
= new byte
[colors
*3];
531 for (i
= 0; i
< m_image
->colors
; i
++) {
532 pal
[3*i
+ 0] = m_image
->pal
[3*i
+ 0];
533 pal
[3*i
+ 1] = m_image
->pal
[3*i
+ 1];
534 pal
[3*i
+ 2] = m_image
->pal
[3*i
+ 2];
536 for (; i
< colors
; i
++) {
543 m_image
->colors
= colors
;
546 for (int i
=0; i
< colors
; i
++) {
547 m_image
->pal
[3*i
+ 0] = (m_image
->pal
[3*i
+ 0] >> 4) * 17;
548 m_image
->pal
[3*i
+ 1] = (m_image
->pal
[3*i
+ 1] >> 4) * 17;
549 m_image
->pal
[3*i
+ 2] = (m_image
->pal
[3*i
+ 2] >> 4) * 17;
553 m_image
->p
= new byte
[bmhd_width
* bmhd_height
* 3];
554 byte
*picptr
= m_image
->p
;
560 byte
*pal
= m_image
->pal
;
561 int lineskip
= ((bmhd_width
+ 15) >> 4) << 1;
562 int height
= chunkLen
/ (lineskip
* bmhd_bitplanes
);
564 if (bmhd_height
< height
) {
565 height
= bmhd_height
;
568 if (fmt
== ILBM_HAM
|| fmt
== ILBM_HAM8
|| fmt
== ILBM_24BIT
) {
570 const byte
*workptr
= bodyptr
;
572 for (int i
=0; i
< height
; i
++) {
574 const byte
*workptr2
= workptr
;
576 // at start of each line, init RGB values to background
581 for (int j
=0; j
< bmhd_width
; j
++) {
584 const byte
*workptr3
= workptr2
;
585 for (int k
=0; k
< bmhd_bitplanes
; k
++) {
586 if (*workptr3
& bitmsk
) {
589 workptr3
+= lineskip
;
594 int c
= (col
& 0x0f);
595 switch (col
& 0x30) {
596 case 0x00: if (c
>= 0 && c
< colors
) {
603 case 0x10: bval
= c
* 17;
606 case 0x20: rval
= c
* 17;
609 case 0x30: gval
= c
* 17;
612 } else if (fmt
== ILBM_HAM8
) {
613 int c
= (col
& 0x3f);
615 case 0x00: if (c
>= 0 && c
< colors
) {
622 case 0x40: bval
= (bval
& 3) | (c
<< 2);
625 case 0x80: rval
= (rval
& 3) | (c
<< 2);
628 case 0xc0: gval
= (rval
& 3) | (c
<< 2);
632 gval
= (col
>> 8) & 0xff;
633 bval
= (col
>> 16) & 0xff;
640 bitmsk
= bitmsk
>> 1;
646 workptr
+= lineskip
* bmhd_bitplanes
;
648 } else if ((fmt
== ILBM_NORMAL
) || (fmt
== ILBM_EHB
)) {
649 if (fmt
== ILBM_EHB
) {
650 wxLogTrace(_T("iff"), _T("Doubling CMAP for EHB mode"));
652 for (int i
=0; i
<32; i
++) {
653 pal
[3*(i
+ 32) + 0] = pal
[3*i
+ 0] >> 1;
654 pal
[3*(i
+ 32) + 1] = pal
[3*i
+ 1] >> 1;
655 pal
[3*(i
+ 32) + 2] = pal
[3*i
+ 2] >> 1;
659 byte
*pic
= picptr
; // ptr to buffer
660 const byte
*workptr
= bodyptr
; // ptr to pic, planar format
662 if (bmhd_height
< height
) {
663 height
= bmhd_height
;
666 for (int i
=0; i
< height
; i
++) {
667 byte bitmsk
= 0x80; // left most bit (mask)
668 const byte
*workptr2
= workptr
; // work ptr to source
669 for (int j
=0; j
< bmhd_width
; j
++) {
672 const byte
*workptr3
= workptr2
; // 1st byte in 1st pln
674 for (int k
=0; k
< bmhd_bitplanes
; k
++) {
675 if (*workptr3
& bitmsk
) { // if bit set in this pln
676 col
= col
+ colbit
; // add bit to chunky byte
678 workptr3
+= lineskip
; // go to next line
679 colbit
<<= 1; // shift color bit
682 if (col
>= 0 && col
< colors
) {
683 pic
[0] = pal
[3*col
+ 0];
684 pic
[1] = pal
[3*col
+ 1];
685 pic
[2] = pal
[3*col
+ 2];
687 pic
[0] = pic
[1] = pic
[2] = 0;
690 bitmsk
= bitmsk
>> 1; // shift mask to next bit
691 if (bitmsk
== 0) { // if mask is zero
692 bitmsk
= 0x80; // reset mask
693 workptr2
++; // mv ptr to next byte
697 workptr
+= lineskip
* bmhd_bitplanes
; // to next line
700 break; // unknown format
703 m_image
->w
= bmhd_width
;
705 m_image
->transparent
= bmhd_transcol
;
707 wxLogTrace(_T("iff"), _T("Loaded IFF picture %s"),
708 truncated
? "truncated" : "completely");
710 return (truncated
? wxIFF_TRUNCATED
: wxIFF_OK
);
712 wxLogTrace(_T("iff"), _T("Skipping unknown chunk '%c%c%c%c'"),
713 *dataptr
, *(dataptr
+1), *(dataptr
+2), *(dataptr
+3));
715 dataptr
= dataptr
+ 8 + chunkLen
; // skip unknown chunk
720 return wxIFF_INVFORMAT
;
725 //-----------------------------------------------------------------------------
727 //-----------------------------------------------------------------------------
729 IMPLEMENT_DYNAMIC_CLASS(wxIFFHandler
, wxImageHandler
)
733 bool wxIFFHandler::LoadFile(wxImage
*image
, wxInputStream
& stream
,
734 bool verbose
, int WXUNUSED(index
))
740 decod
= new wxIFFDecoder(&stream
);
741 error
= decod
->ReadIFF();
743 if ((error
!= wxIFF_OK
) && (error
!= wxIFF_TRUNCATED
))
749 case wxIFF_INVFORMAT
:
750 wxLogError(_("IFF: error in IFF image format."));
753 wxLogError(_("IFF: not enough memory."));
756 wxLogError(_("IFF: unknown error!!!"));
764 if ((error
== wxIFF_TRUNCATED
) && verbose
)
766 wxLogError(_("IFF: data stream seems to be truncated."));
767 /* go on; image data is OK */
770 ok
= decod
->ConvertToImage(image
);
776 bool wxIFFHandler::SaveFile(wxImage
* WXUNUSED(image
),
777 wxOutputStream
& WXUNUSED(stream
), bool verbose
)
780 wxLogDebug(wxT("IFF: the handler is read-only!!"));
785 bool wxIFFHandler::DoCanRead(wxInputStream
& stream
)
787 wxIFFDecoder
decod(&stream
);
789 return decod
.CanRead();
790 // it's ok to modify the stream position here
793 #endif // wxUSE_STREAMS