]>
Commit | Line | Data |
---|---|---|
63f8abca VS |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: imagiff.h | |
3 | // Purpose: wxImage handler for Amiga IFF images | |
4 | // Author: Steffen Gutmann | |
5 | // RCS-ID: $Id$ | |
6 | // Copyright: (c) Steffen Gutmann, 2002 | |
7 | // Licence: wxWindows licence | |
8 | ///////////////////////////////////////////////////////////////////////////// | |
9 | ||
10 | // parts of the source are based on xviff by Thomas Meyer | |
11 | // Permission for use in wxWindows has been gratefully given. | |
4b6b4dfc RR |
12 | |
13 | #ifdef __GNUG__ | |
14 | #pragma implementation "imagiff.h" | |
15 | #endif | |
16 | ||
17 | // For compilers that support precompilation, includes "wx.h". | |
18 | #include "wx/wxprec.h" | |
19 | ||
20 | #ifdef __BORLANDC__ | |
21 | # pragma hdrstop | |
22 | #endif | |
23 | ||
24 | #ifndef WX_PRECOMP | |
25 | # include "wx/defs.h" | |
26 | #endif | |
27 | ||
63f8abca VS |
28 | #if wxUSE_IMAGE && wxUSE_IFF |
29 | ||
4b6b4dfc | 30 | #include "wx/imagiff.h" |
4b6b4dfc RR |
31 | #include "wx/wfstream.h" |
32 | #include "wx/log.h" | |
33 | #include "wx/intl.h" | |
34 | ||
63f8abca VS |
35 | #include <stdlib.h> |
36 | #include <string.h> | |
37 | ||
38 | ||
39 | // -------------------------------------------------------------------------- | |
40 | // Constants | |
41 | // -------------------------------------------------------------------------- | |
42 | ||
43 | // Error codes: | |
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. | |
48 | // | |
49 | ||
50 | enum | |
51 | { | |
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 */ | |
56 | }; | |
57 | ||
58 | // -------------------------------------------------------------------------- | |
59 | // wxIFFDecoder class | |
60 | // -------------------------------------------------------------------------- | |
61 | ||
62 | // internal class for storing IFF image data | |
63 | class IFFImage | |
64 | { | |
65 | public: | |
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 */ | |
72 | ||
73 | IFFImage() : w(0), h(0), colors(0), p(0), pal(0) {} | |
74 | ~IFFImage() { delete [] p; delete [] pal; } | |
75 | }; | |
76 | ||
77 | class WXDLLEXPORT wxIFFDecoder | |
78 | { | |
79 | private: | |
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; | |
85 | ||
86 | void Destroy(); | |
87 | ||
88 | public: | |
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; | |
96 | ||
97 | // constructor, destructor, etc. | |
98 | wxIFFDecoder(wxInputStream *s); | |
99 | ~wxIFFDecoder() { Destroy(); } | |
100 | bool CanRead(); | |
101 | int ReadIFF(); | |
102 | bool ConvertToImage(wxImage *image) const; | |
103 | }; | |
104 | ||
105 | ||
106 | //--------------------------------------------------------------------------- | |
107 | // wxIFFDecoder constructor and destructor | |
108 | //--------------------------------------------------------------------------- | |
109 | ||
110 | wxIFFDecoder::wxIFFDecoder(wxInputStream *s) | |
111 | { | |
112 | m_f = s; | |
113 | m_image = 0; | |
114 | databuf = 0; | |
115 | decomp_mem = 0; | |
116 | } | |
117 | ||
118 | void wxIFFDecoder::Destroy() | |
119 | { | |
120 | delete m_image; | |
121 | m_image = 0; | |
122 | delete [] databuf; | |
123 | databuf = 0; | |
124 | delete [] decomp_mem; | |
125 | decomp_mem = 0; | |
126 | } | |
127 | ||
128 | //--------------------------------------------------------------------------- | |
129 | // Convert this image to a wxImage object | |
130 | //--------------------------------------------------------------------------- | |
131 | ||
132 | // This function was designed by Vaclav Slavik | |
133 | ||
134 | bool wxIFFDecoder::ConvertToImage(wxImage *image) const | |
135 | { | |
136 | // just in case... | |
137 | image->Destroy(); | |
138 | ||
139 | // create the image | |
140 | image->Create(GetWidth(), GetHeight()); | |
141 | ||
142 | if (!image->Ok()) | |
143 | return FALSE; | |
144 | ||
145 | unsigned char *pal = GetPalette(); | |
146 | unsigned char *src = GetData(); | |
147 | unsigned char *dst = image->GetData(); | |
148 | int colors = GetNumColors(); | |
149 | int transparent = GetTransparentColour(); | |
150 | long i; | |
151 | ||
152 | // set transparent colour mask | |
153 | if (transparent != -1) | |
154 | { | |
155 | for (i = 0; i < colors; i++) | |
156 | { | |
157 | if ((pal[3 * i + 0] == 255) && | |
158 | (pal[3 * i + 1] == 0) && | |
159 | (pal[3 * i + 2] == 255)) | |
160 | { | |
161 | pal[3 * i + 2] = 254; | |
162 | } | |
163 | } | |
164 | ||
165 | pal[3 * transparent + 0] = 255, | |
166 | pal[3 * transparent + 1] = 0, | |
167 | pal[3 * transparent + 2] = 255; | |
168 | ||
169 | image->SetMaskColour(255, 0, 255); | |
170 | } | |
171 | else | |
172 | image->SetMask(FALSE); | |
173 | ||
174 | #if wxUSE_PALETTE | |
175 | if (pal && colors > 0) | |
176 | { | |
177 | unsigned char* r = new unsigned char[colors]; | |
178 | unsigned char* g = new unsigned char[colors]; | |
179 | unsigned char* b = new unsigned char[colors]; | |
180 | ||
181 | for (i = 0; i < colors; i++) | |
182 | { | |
183 | r[i] = pal[3*i + 0]; | |
184 | g[i] = pal[3*i + 1]; | |
185 | b[i] = pal[3*i + 2]; | |
186 | } | |
187 | ||
188 | image->SetPalette(wxPalette(colors, r, g, b)); | |
189 | ||
190 | delete [] r; | |
191 | delete [] g; | |
192 | delete [] b; | |
193 | } | |
194 | #endif // wxUSE_PALETTE | |
195 | ||
196 | // copy image data | |
197 | for (i = 0; i < (long)(GetWidth() * GetHeight()); i++, src += 3, dst += 3) | |
198 | { | |
199 | dst[0] = src[0]; | |
200 | dst[1] = src[1]; | |
201 | dst[2] = src[2]; | |
202 | } | |
203 | ||
204 | return TRUE; | |
205 | } | |
206 | ||
207 | ||
208 | //--------------------------------------------------------------------------- | |
209 | // Data accessors | |
210 | //--------------------------------------------------------------------------- | |
211 | ||
212 | // Get data for current frame | |
213 | ||
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; } | |
220 | ||
221 | //--------------------------------------------------------------------------- | |
222 | // IFF reading and decoding | |
223 | //--------------------------------------------------------------------------- | |
224 | ||
225 | // | |
226 | // CanRead: | |
227 | // Returns TRUE if the file looks like a valid IFF, FALSE otherwise. | |
228 | // | |
229 | bool wxIFFDecoder::CanRead() | |
230 | { | |
231 | unsigned char buf[12] = ""; | |
232 | ||
233 | m_f->Read(buf, 12); | |
234 | m_f->SeekI(-12, wxFromCurrent); | |
235 | ||
236 | return (memcmp(buf, "FORM", 4) == 0 && memcmp(buf+8, "ILBM", 4) == 0); | |
237 | } | |
238 | ||
239 | ||
240 | // ReadIFF: | |
241 | // Based on xv source code by Thomas Meyer | |
242 | // Permission for use in wxWindows has been gratefully given. | |
243 | ||
244 | typedef unsigned char byte; | |
245 | #define IFFDEBUG 0 | |
246 | ||
247 | /************************************************************************* | |
248 | void decomprle(source, destination, source length, buffer size) | |
249 | ||
250 | Decompress run-length encoded data from source to destination. Terminates | |
251 | when source is decoded completely or destination buffer is full. | |
252 | ||
253 | The decruncher is as optimized as I could make it, without risking | |
254 | safety in case of corrupt BODY chunks. | |
255 | **************************************************************************/ | |
256 | ||
257 | static void decomprle(const byte *sptr, byte *dptr, long slen, long dlen) | |
258 | { | |
259 | byte codeByte, dataByte; | |
260 | ||
261 | while ((slen > 0) && (dlen > 0)) { | |
262 | // read control byte | |
263 | codeByte = *sptr++; | |
264 | ||
265 | if (codeByte < 0x80) { | |
266 | codeByte++; | |
267 | if ((slen > (long) codeByte) && (dlen >= (long) codeByte)) { | |
268 | slen -= codeByte + 1; | |
269 | dlen -= codeByte; | |
270 | while (codeByte > 0) { | |
271 | *dptr++ = *sptr++; | |
272 | codeByte--; | |
273 | } | |
274 | } | |
275 | else slen = 0; | |
276 | } | |
277 | ||
278 | else if (codeByte > 0x80) { | |
279 | codeByte = 0x81 - (codeByte & 0x7f); | |
280 | if ((slen > (long) 0) && (dlen >= (long) codeByte)) { | |
281 | dataByte = *sptr++; | |
282 | slen -= 2; | |
283 | dlen -= codeByte; | |
284 | while (codeByte > 0) { | |
285 | *dptr++ = dataByte; | |
286 | codeByte--; | |
287 | } | |
288 | } | |
289 | else slen = 0; | |
290 | } | |
291 | } | |
292 | } | |
293 | ||
294 | /******************************************/ | |
295 | static unsigned int iff_getword(const byte *ptr) | |
296 | { | |
297 | unsigned int v; | |
298 | ||
299 | v = *ptr++; | |
300 | v = (v << 8) + *ptr; | |
301 | return v; | |
302 | } | |
303 | ||
304 | /******************************************/ | |
305 | static unsigned long iff_getlong(const byte *ptr) | |
306 | { | |
307 | unsigned long l; | |
308 | ||
309 | l = *ptr++; | |
310 | l = (l << 8) + *ptr++; | |
311 | l = (l << 8) + *ptr++; | |
312 | l = (l << 8) + *ptr; | |
313 | return l; | |
314 | } | |
315 | ||
316 | // Define internal ILBM types | |
317 | #define ILBM_NORMAL 0 | |
318 | #define ILBM_EHB 1 | |
319 | #define ILBM_HAM 2 | |
320 | #define ILBM_HAM8 3 | |
321 | #define ILBM_24BIT 4 | |
322 | ||
323 | int wxIFFDecoder::ReadIFF() | |
324 | { | |
325 | Destroy(); | |
326 | ||
327 | m_image = new IFFImage(); | |
328 | if (m_image == 0) { | |
329 | Destroy(); | |
330 | return wxIFF_MEMERR; | |
331 | } | |
332 | ||
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); | |
338 | ||
339 | // allocate memory for complete file | |
340 | if ((databuf = new byte[filesize]) == 0) { | |
341 | Destroy(); | |
342 | return wxIFF_MEMERR; | |
343 | } | |
344 | ||
345 | m_f->Read(databuf, filesize); | |
346 | const byte *dataend = databuf + filesize; | |
347 | ||
348 | // initialize work pointer. used to trace the buffer for IFF chunks | |
349 | const byte *dataptr = databuf; | |
350 | ||
351 | // check for minmal size | |
352 | if (dataptr + 12 > dataend) { | |
353 | Destroy(); | |
354 | return wxIFF_INVFORMAT; | |
355 | } | |
356 | ||
357 | // check if we really got an IFF file | |
358 | if (strncmp((char *)dataptr, "FORM", 4) != 0) { | |
359 | Destroy(); | |
360 | return wxIFF_INVFORMAT; | |
361 | } | |
362 | ||
363 | dataptr = dataptr + 8; // skip ID and length of FORM | |
364 | ||
365 | // check if the IFF file is an ILBM (picture) file | |
366 | if (strncmp((char *) dataptr, "ILBM", 4) != 0) { | |
367 | Destroy(); | |
368 | return wxIFF_INVFORMAT; | |
369 | } | |
370 | ||
371 | wxLogTrace(_T("iff"), _T("IFF ILBM file recognized")); | |
372 | ||
373 | dataptr = dataptr + 4; // skip ID | |
374 | ||
375 | // | |
376 | // main decoding loop. searches IFF chunks and handles them. | |
377 | // terminates when BODY chunk was found or dataptr ran over end of file | |
378 | // | |
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; | |
383 | int colors = 0; | |
384 | while (dataptr + 8 <= dataend) { | |
385 | // get chunk length and make even | |
386 | size_t chunkLen = (iff_getlong(dataptr + 4) + 1) & 0xfffffffe; | |
387 | #ifdef __VMS | |
388 | // Silence compiler warning | |
389 | int chunkLen_; | |
390 | chunkLen_ = chunkLen; | |
391 | if (chunkLen_ < 0) { // format error? | |
392 | #else | |
393 | if (chunkLen < 0) { // format error? | |
394 | #endif | |
395 | break; | |
396 | } | |
397 | bool truncated = (dataptr + 8 + chunkLen > dataend); | |
398 | ||
399 | if (strncmp((char *)dataptr, "BMHD", 4) == 0) { // BMHD chunk? | |
400 | if (chunkLen < 12 + 2 || truncated) { | |
401 | break; | |
402 | } | |
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 | |
411 | } | |
412 | else if (strncmp((char *)dataptr, "CMAP", 4) == 0) { // CMAP ? | |
413 | if (truncated) { | |
414 | break; | |
415 | } | |
416 | const byte *cmapptr = dataptr + 8; | |
417 | colors = chunkLen / 3; // calc no of colors | |
418 | ||
419 | delete m_image->pal; | |
420 | m_image->pal = 0; | |
421 | m_image->colors = colors; | |
422 | if (colors > 0) { | |
423 | m_image->pal = new byte[3*colors]; | |
424 | if (!m_image->pal) { | |
425 | Destroy(); | |
426 | return wxIFF_MEMERR; | |
427 | } | |
428 | ||
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++; | |
434 | } | |
435 | } | |
436 | ||
437 | wxLogTrace(_T("iff"), _T("Read %d colors from IFF file."), | |
438 | colors); | |
439 | ||
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) { | |
444 | break; | |
445 | } | |
446 | camg_viewmode = iff_getlong(dataptr + 8); // get viewmodes | |
447 | CAMGok = true; // got CAMG | |
448 | dataptr += 8 + chunkLen; // to next chunk | |
449 | } | |
450 | else if (strncmp((char *)dataptr, "BODY", 4) == 0) { // BODY ? | |
451 | if (!BMHDok) { // BMHD found? | |
452 | break; | |
453 | } | |
454 | const byte *bodyptr = dataptr + 8; // -> BODY data | |
455 | ||
456 | if (truncated) { | |
457 | chunkLen = dataend - dataptr; | |
458 | } | |
459 | ||
460 | // | |
461 | // if BODY is compressed, allocate buffer for decrunched BODY | |
462 | // and decompress it (run length encoding) | |
463 | // | |
464 | if (bmhd_compression == 1) { | |
465 | // calc size of decrunch buffer - (size of the actual pic. | |
466 | // decompressed in interleaved Amiga bitplane format) | |
467 | ||
468 | size_t decomp_bufsize = (((bmhd_width + 15) >> 4) << 1) | |
469 | * bmhd_height * bmhd_bitplanes; | |
470 | ||
471 | if ((decomp_mem = new byte[decomp_bufsize]) == 0) { | |
472 | Destroy(); | |
473 | return wxIFF_MEMERR; | |
474 | } | |
475 | ||
476 | decomprle(bodyptr, decomp_mem, chunkLen, decomp_bufsize); | |
477 | bodyptr = decomp_mem; // -> uncompressed BODY | |
478 | chunkLen = decomp_bufsize; | |
479 | delete [] databuf; | |
480 | databuf = 0; | |
481 | } | |
482 | ||
483 | // the following determines the type of the ILBM file. | |
484 | // it's either NORMAL, EHB, HAM, HAM8 or 24BIT | |
485 | ||
486 | int fmt = ILBM_NORMAL; // assume normal ILBM | |
487 | if (bmhd_bitplanes == 24) { | |
488 | fmt = ILBM_24BIT; | |
489 | } else if (bmhd_bitplanes == 8) { | |
490 | if (CAMGok && (camg_viewmode & 0x800)) { | |
491 | fmt = ILBM_HAM8; | |
492 | } | |
493 | } else if ((bmhd_bitplanes > 5) && CAMGok) { | |
494 | if (camg_viewmode & 0x80) { | |
495 | fmt = ILBM_EHB; | |
496 | } else if (camg_viewmode & 0x800) { | |
497 | fmt = ILBM_HAM; | |
498 | } | |
499 | } | |
500 | ||
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); | |
510 | ||
511 | if ((fmt==ILBM_NORMAL) || (fmt==ILBM_EHB) || (fmt==ILBM_HAM)) { | |
512 | wxLogTrace(_T("iff"), | |
513 | _T("Converting CMAP from normal ILBM CMAP")); | |
514 | ||
515 | switch(fmt) { | |
516 | case ILBM_NORMAL: colors = 1 << bmhd_bitplanes; break; | |
517 | case ILBM_EHB: colors = 32*2; break; | |
518 | case ILBM_HAM: colors = 16; break; | |
519 | } | |
520 | ||
521 | if (colors > m_image->colors) { | |
522 | byte *pal = new byte[colors*3]; | |
523 | if (!pal) { | |
524 | Destroy(); | |
525 | return wxIFF_MEMERR; | |
526 | } | |
527 | int i; | |
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]; | |
532 | } | |
533 | for (; i < colors; i++) { | |
534 | pal[3*i + 0] = 0; | |
535 | pal[3*i + 1] = 0; | |
536 | pal[3*i + 2] = 0; | |
537 | } | |
538 | delete m_image->pal; | |
539 | m_image->pal = pal; | |
540 | m_image->colors = colors; | |
541 | } | |
542 | ||
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; | |
547 | } | |
548 | } | |
549 | ||
550 | m_image->p = new byte[bmhd_width * bmhd_height * 3]; | |
551 | byte *picptr = m_image->p; | |
552 | if (!picptr) { | |
553 | Destroy(); | |
554 | return wxIFF_MEMERR; | |
555 | } | |
556 | ||
557 | byte *pal = m_image->pal; | |
558 | int lineskip = ((bmhd_width + 15) >> 4) << 1; | |
559 | int height = chunkLen / (lineskip * bmhd_bitplanes); | |
560 | ||
561 | if (bmhd_height < height) { | |
562 | height = bmhd_height; | |
563 | } | |
564 | ||
565 | if (fmt == ILBM_HAM || fmt == ILBM_HAM8 || fmt == ILBM_24BIT) { | |
566 | byte *pic = picptr; | |
567 | const byte *workptr = bodyptr; | |
568 | ||
569 | for (int i=0; i < height; i++) { | |
570 | byte bitmsk = 0x80; | |
571 | const byte *workptr2 = workptr; | |
572 | ||
573 | // at start of each line, init RGB values to background | |
574 | byte rval = pal[0]; | |
575 | byte gval = pal[1]; | |
576 | byte bval = pal[2]; | |
577 | ||
578 | for (int j=0; j < bmhd_width; j++) { | |
579 | long col = 0; | |
580 | long colbit = 1; | |
581 | const byte *workptr3 = workptr2; | |
582 | for (int k=0; k < bmhd_bitplanes; k++) { | |
583 | if (*workptr3 & bitmsk) { | |
584 | col += colbit; | |
585 | } | |
586 | workptr3 += lineskip; | |
587 | colbit <<= 1; | |
588 | } | |
589 | ||
590 | if (fmt==ILBM_HAM) { | |
591 | int c = (col & 0x0f); | |
592 | switch (col & 0x30) { | |
593 | case 0x00: if (c >= 0 && c < colors) { | |
594 | rval = pal[3*c + 0]; | |
595 | gval = pal[3*c + 1]; | |
596 | bval = pal[3*c + 2]; | |
597 | } | |
598 | break; | |
599 | ||
600 | case 0x10: bval = c * 17; | |
601 | break; | |
602 | ||
603 | case 0x20: rval = c * 17; | |
604 | break; | |
605 | ||
606 | case 0x30: gval = c * 17; | |
607 | break; | |
608 | } | |
609 | } else if (fmt == ILBM_HAM8) { | |
610 | int c = (col & 0x3f); | |
611 | switch(col & 0xc0) { | |
612 | case 0x00: if (c >= 0 && c < colors) { | |
613 | rval = pal[3*c + 0]; | |
614 | gval = pal[3*c + 1]; | |
615 | bval = pal[3*c + 2]; | |
616 | } | |
617 | break; | |
618 | ||
619 | case 0x40: bval = (bval & 3) | (c << 2); | |
620 | break; | |
621 | ||
622 | case 0x80: rval = (rval & 3) | (c << 2); | |
623 | break; | |
624 | ||
625 | case 0xc0: gval = (rval & 3) | (c << 2); | |
626 | } | |
627 | } else { | |
628 | rval = col & 0xff; | |
629 | gval = (col >> 8) & 0xff; | |
630 | bval = (col >> 16) & 0xff; | |
631 | } | |
632 | ||
633 | *pic++ = rval; | |
634 | *pic++ = gval; | |
635 | *pic++ = bval; | |
636 | ||
637 | bitmsk = bitmsk >> 1; | |
638 | if (bitmsk == 0) { | |
639 | bitmsk = 0x80; | |
640 | workptr2++; | |
641 | } | |
642 | } | |
643 | workptr += lineskip * bmhd_bitplanes; | |
644 | } | |
645 | } else if ((fmt == ILBM_NORMAL) || (fmt == ILBM_EHB)) { | |
646 | if (fmt == ILBM_EHB) { | |
647 | wxLogTrace(_T("iff"), _T("Doubling CMAP for EHB mode")); | |
648 | ||
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; | |
653 | } | |
654 | } | |
655 | ||
656 | byte *pic = picptr; // ptr to buffer | |
657 | const byte *workptr = bodyptr; // ptr to pic, planar format | |
658 | ||
659 | if (bmhd_height < height) { | |
660 | height = bmhd_height; | |
661 | } | |
662 | ||
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++) { | |
667 | long col = 0; | |
668 | long colbit = 1; | |
669 | const byte *workptr3 = workptr2; // 1st byte in 1st pln | |
670 | ||
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 | |
674 | } | |
675 | workptr3 += lineskip; // go to next line | |
676 | colbit <<= 1; // shift color bit | |
677 | } | |
678 | ||
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]; | |
683 | } else { | |
684 | pic[0] = pic[1] = pic[2] = 0; | |
685 | } | |
686 | pic += 3; | |
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 | |
691 | } | |
692 | } | |
693 | ||
694 | workptr += lineskip * bmhd_bitplanes; // to next line | |
695 | } | |
696 | } else { | |
697 | break; // unknown format | |
698 | } | |
699 | ||
700 | m_image->w = bmhd_width; | |
701 | m_image->h = height; | |
702 | m_image->transparent = bmhd_transcol; | |
703 | ||
704 | wxLogTrace(_T("iff"), _T("Loaded IFF picture %s"), | |
705 | truncated? "truncated" : "completely"); | |
706 | ||
707 | return (truncated? wxIFF_TRUNCATED : wxIFF_OK); | |
708 | } else { | |
709 | wxLogTrace(_T("iff"), _T("Skipping unknown chunk '%c%c%c%c'"), | |
710 | *dataptr, *(dataptr+1), *(dataptr+2), *(dataptr+3)); | |
711 | ||
712 | dataptr = dataptr + 8 + chunkLen; // skip unknown chunk | |
713 | } | |
714 | } | |
715 | ||
716 | Destroy(); | |
717 | return wxIFF_INVFORMAT; | |
718 | } | |
719 | ||
4b6b4dfc | 720 | |
4b6b4dfc RR |
721 | |
722 | //----------------------------------------------------------------------------- | |
723 | // wxIFFHandler | |
724 | //----------------------------------------------------------------------------- | |
725 | ||
63f8abca VS |
726 | IMPLEMENT_DYNAMIC_CLASS(wxIFFHandler, wxImageHandler) |
727 | ||
4b6b4dfc RR |
728 | #if wxUSE_STREAMS |
729 | ||
730 | bool wxIFFHandler::LoadFile(wxImage *image, wxInputStream& stream, | |
63f8abca | 731 | bool verbose, int WXUNUSED(index)) |
4b6b4dfc RR |
732 | { |
733 | wxIFFDecoder *decod; | |
734 | int error; | |
735 | bool ok; | |
736 | ||
737 | decod = new wxIFFDecoder(&stream); | |
738 | error = decod->ReadIFF(); | |
739 | ||
740 | if ((error != wxIFF_OK) && (error != wxIFF_TRUNCATED)) | |
741 | { | |
742 | if (verbose) | |
743 | { | |
744 | switch (error) | |
745 | { | |
746 | case wxIFF_INVFORMAT: | |
747 | wxLogError(_("IFF: error in IFF image format.")); | |
748 | break; | |
749 | case wxIFF_MEMERR: | |
750 | wxLogError(_("IFF: not enough memory.")); | |
751 | break; | |
752 | default: | |
753 | wxLogError(_("IFF: unknown error!!!")); | |
754 | break; | |
755 | } | |
756 | } | |
757 | delete decod; | |
758 | return FALSE; | |
759 | } | |
760 | ||
761 | if ((error == wxIFF_TRUNCATED) && verbose) | |
762 | { | |
763 | wxLogError(_("IFF: data stream seems to be truncated.")); | |
764 | /* go on; image data is OK */ | |
765 | } | |
766 | ||
767 | ok = decod->ConvertToImage(image); | |
768 | delete decod; | |
769 | ||
770 | return ok; | |
771 | } | |
772 | ||
773 | bool wxIFFHandler::SaveFile(wxImage * WXUNUSED(image), | |
63f8abca | 774 | wxOutputStream& WXUNUSED(stream), bool verbose) |
4b6b4dfc RR |
775 | { |
776 | if (verbose) | |
777 | wxLogDebug(wxT("IFF: the handler is read-only!!")); | |
778 | ||
779 | return FALSE; | |
780 | } | |
781 | ||
782 | bool wxIFFHandler::DoCanRead(wxInputStream& stream) | |
783 | { | |
784 | wxIFFDecoder *decod; | |
785 | bool ok; | |
786 | ||
787 | decod = new wxIFFDecoder(&stream); | |
788 | ok = decod->CanRead(); | |
789 | delete decod; | |
790 | ||
791 | return ok; | |
792 | } | |
793 | ||
63f8abca | 794 | #endif // wxUSE_STREAMS |
4b6b4dfc | 795 | |
63f8abca | 796 | #endif // wxUSE_IFF |