]> git.saurik.com Git - wxWidgets.git/blob - src/common/imagbmp.cpp
Committing in .
[wxWidgets.git] / src / common / imagbmp.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: imagbmp.cpp
3 // Purpose: wxImage BMP handler
4 // Author: Robert Roebling
5 // RCS-ID: $Id$
6 // Copyright: (c) Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 #ifdef __GNUG__
11 #pragma implementation "imagbmp.h"
12 #endif
13
14 // For compilers that support precompilation, includes "wx.h".
15 #include "wx/wxprec.h"
16
17 #ifdef __BORLANDC__
18 #pragma hdrstop
19 #endif
20
21 #include "wx/defs.h"
22
23 #if wxUSE_IMAGE
24
25 #include "wx/imagbmp.h"
26 #include "wx/bitmap.h"
27 #include "wx/debug.h"
28 #include "wx/log.h"
29 #include "wx/app.h"
30 #include "wx/filefn.h"
31 #include "wx/wfstream.h"
32 #include "wx/intl.h"
33 #include "wx/module.h"
34 #include "wx/quantize.h"
35
36 // For memcpy
37 #include <string.h>
38
39 #ifdef __SALFORDC__
40 #ifdef FAR
41 #undef FAR
42 #endif
43 #endif
44
45 #ifdef __WXMSW__
46 #include <windows.h>
47 #endif
48
49 //-----------------------------------------------------------------------------
50 // wxBMPHandler
51 //-----------------------------------------------------------------------------
52
53 IMPLEMENT_DYNAMIC_CLASS(wxBMPHandler,wxImageHandler)
54
55
56 #if wxUSE_STREAMS
57
58
59 bool wxBMPHandler::SaveFile(wxImage *image,
60 wxOutputStream& stream,
61 bool verbose)
62 {
63 wxCHECK_MSG( image, FALSE, _T("invalid pointer in wxBMPHandler::SaveFile") );
64
65 if (!image->Ok())
66 {
67 if (verbose) wxLogError(_("BMP: Couldn't save invalid image."));
68 return FALSE;
69 }
70
71 // get the format of the BMP file to save, else use 24bpp
72 unsigned format = wxBMP_24BPP;
73 if (image->HasOption(wxBMP_FORMAT))
74 format = image->GetOptionInt(wxBMP_FORMAT);
75
76 unsigned bpp; // # of bits per pixel
77 int palette_size; // # of color map entries, ie. 2^bpp colors
78
79 // set the bpp and appropriate palette_size, and do additional checks
80 if ((format == wxBMP_1BPP) || (format == wxBMP_1BPP_BW))
81 {
82 bpp = 1;
83 palette_size = 2;
84 }
85 else if (format == wxBMP_4BPP)
86 {
87 bpp = 4;
88 palette_size = 16;
89 }
90 else if ((format == wxBMP_8BPP) || (format == wxBMP_8BPP_GREY) ||
91 (format == wxBMP_8BPP_RED) || (format == wxBMP_8BPP_PALETTE))
92 {
93 // need to set a wxPalette to use this, HOW TO CHECK IF VALID, SIZE?
94 if ((format == wxBMP_8BPP_PALETTE) && !image->HasPalette())
95 {
96 if (verbose)
97 wxLogError(_("BMP: wImage doesn't have own wxPalette."));
98 return FALSE;
99 }
100 bpp = 8;
101 palette_size = 256;
102 }
103 else // you get 24bpp
104 {
105 format = wxBMP_24BPP;
106 bpp = 24;
107 palette_size = 0;
108 }
109
110 unsigned width = image->GetWidth();
111 unsigned row_padding = (4 - int(width*bpp/8.0) % 4) % 4; // # bytes to pad to dword
112 unsigned row_width = int(width * bpp/8.0) + row_padding; // # of bytes per row
113
114 struct
115 {
116 // BitmapHeader:
117 wxUint16 magic; // format magic, always 'BM'
118 wxUint32 filesize; // total file size, inc. headers
119 wxUint32 reserved; // for future use
120 wxUint32 data_offset; // image data offset in the file
121
122 // BitmapInfoHeader:
123 wxUint32 bih_size; // 2nd part's size
124 wxUint32 width, height; // bitmap's dimensions
125 wxUint16 planes; // num of planes
126 wxUint16 bpp; // bits per pixel
127 wxUint32 compression; // compression method
128 wxUint32 size_of_bmp; // size of the bitmap
129 wxUint32 h_res, v_res; // image resolution in dpi
130 wxUint32 num_clrs; // number of colors used
131 wxUint32 num_signif_clrs;// number of significant colors
132 } hdr;
133
134 wxUint32 hdr_size = 14/*BitmapHeader*/ + 40/*BitmapInfoHeader*/;
135
136 hdr.magic = wxUINT16_SWAP_ON_BE(0x4D42/*'BM'*/);
137 hdr.filesize = wxUINT32_SWAP_ON_BE( hdr_size + palette_size*4 +
138 row_width * image->GetHeight() );
139 hdr.reserved = 0;
140 hdr.data_offset = wxUINT32_SWAP_ON_BE(hdr_size + palette_size*4);
141
142 hdr.bih_size = wxUINT32_SWAP_ON_BE(hdr_size - 14);
143 hdr.width = wxUINT32_SWAP_ON_BE(image->GetWidth());
144 hdr.height = wxUINT32_SWAP_ON_BE(image->GetHeight());
145 hdr.planes = wxUINT16_SWAP_ON_BE(1); // always 1 plane
146 hdr.bpp = wxUINT16_SWAP_ON_BE(bpp);
147 hdr.compression = 0; // RGB uncompressed
148 hdr.size_of_bmp = wxUINT32_SWAP_ON_BE(row_width * image->GetHeight());
149 hdr.h_res = hdr.v_res = wxUINT32_SWAP_ON_BE(72); // 72dpi is standard
150 hdr.num_clrs = wxUINT32_SWAP_ON_BE(palette_size); // # colors in colormap
151 hdr.num_signif_clrs = 0; // all colors are significant
152
153 if (// VS: looks ugly but compilers tend to do ugly things with structs,
154 // like aligning hdr.filesize's ofset to dword :(
155 // VZ: we should add padding then...
156 !stream.Write(&hdr.magic, 2) ||
157 !stream.Write(&hdr.filesize, 4) ||
158 !stream.Write(&hdr.reserved, 4) ||
159 !stream.Write(&hdr.data_offset, 4) ||
160 !stream.Write(&hdr.bih_size, 4) ||
161 !stream.Write(&hdr.width, 4) ||
162 !stream.Write(&hdr.height, 4) ||
163 !stream.Write(&hdr.planes, 2) ||
164 !stream.Write(&hdr.bpp, 2) ||
165 !stream.Write(&hdr.compression, 4) ||
166 !stream.Write(&hdr.size_of_bmp, 4) ||
167 !stream.Write(&hdr.h_res, 4) ||
168 !stream.Write(&hdr.v_res, 4) ||
169 !stream.Write(&hdr.num_clrs, 4) ||
170 !stream.Write(&hdr.num_signif_clrs, 4)
171 )
172 {
173 if (verbose)
174 wxLogError(_("BMP: Couldn't write the file header."));
175 return FALSE;
176 }
177
178 wxPalette *palette = NULL; // entries for quantized images
179 wxUint8 *rgbquad = NULL; // for the RGBQUAD bytes for the colormap
180 wxImage *q_image = NULL; // destination for quantized image
181
182 // if <24bpp use quantization to reduce colors for *some* of the formats
183 if ( (format == wxBMP_1BPP) || (format == wxBMP_4BPP) ||
184 (format == wxBMP_8BPP) || (format == wxBMP_8BPP_PALETTE))
185 {
186 // make a new palette and quantize the image
187 if (format != wxBMP_8BPP_PALETTE)
188 {
189 q_image = new wxImage();
190
191 // I get a delete error using Quantize when desired colors > 236
192 int quantize = ((palette_size > 236) ? 236 : palette_size);
193 // fill the destination too, it gives much nicer 4bpp images
194 wxQuantize::Quantize( *image, *q_image, &palette, quantize, 0,
195 wxQUANTIZE_FILL_DESTINATION_IMAGE );
196 }
197 else
198 {
199 palette = new wxPalette(image->GetPalette());
200 }
201
202 int i;
203 unsigned char r, g, b;
204 rgbquad = new wxUint8 [palette_size*4];
205
206 for (i=0; i<palette_size; i++)
207 {
208 if (!palette->GetRGB( i, &r, &g, &b )) r = g = b = 0;
209
210 rgbquad[i*4] = b;
211 rgbquad[i*4+1] = g;
212 rgbquad[i*4+2] = r;
213 rgbquad[i*4+3] = 0;
214 }
215 }
216 // make a 256 entry greyscale colormap or 2 entry black & white
217 else if ((format == wxBMP_8BPP_GREY) || (format == wxBMP_8BPP_RED) ||
218 (format == wxBMP_1BPP_BW))
219 {
220 int i;
221 rgbquad = new wxUint8 [palette_size*4];
222
223 for (i=0; i<palette_size; i++)
224 {
225 // if 1BPP_BW then just 0 and 255 then exit
226 if (( i > 0) && (format == wxBMP_1BPP_BW)) i = 255;
227 rgbquad[i*4] = i;
228 rgbquad[i*4+1] = i;
229 rgbquad[i*4+2] = i;
230 rgbquad[i*4+3] = 0;
231 }
232 }
233
234 // if the colormap was made, then it needs to be written
235 if (rgbquad)
236 {
237 if (!stream.Write(rgbquad, palette_size*4))
238 {
239 if (verbose) wxLogError(_("BMP: Couldn't write RGB color map."));
240 delete []rgbquad;
241 if (palette) delete palette;
242 if (q_image) delete q_image;
243 return FALSE;
244 }
245 delete []rgbquad;
246 }
247
248 // pointer to the image data, use quantized if available
249 wxUint8 *data = (wxUint8*) image->GetData();
250 if (q_image) if (q_image->Ok()) data = (wxUint8*) q_image->GetData();
251
252 wxUint8 *buffer = new wxUint8[row_width];
253 memset(buffer, 0, row_width);
254 int y; unsigned x;
255 long int pixel;
256
257 for (y = image->GetHeight() -1 ; y >= 0; y--)
258 {
259 if (format == wxBMP_24BPP) // 3 bytes per pixel red,green,blue
260 {
261 for (x = 0; x < width; x++)
262 {
263 pixel = 3*(y*width + x);
264
265 buffer[3*x ] = data[pixel+2];
266 buffer[3*x + 1] = data[pixel+1];
267 buffer[3*x + 2] = data[pixel];
268 }
269 }
270 else if ((format == wxBMP_8BPP) || // 1 byte per pixel in color
271 (format == wxBMP_8BPP_PALETTE))
272 {
273 for (x = 0; x < width; x++)
274 {
275 pixel = 3*(y*width + x);
276 buffer[x] = palette->GetPixel( data[pixel],
277 data[pixel+1],
278 data[pixel+2] );
279 }
280 }
281 else if (format == wxBMP_8BPP_GREY) // 1 byte per pix, rgb ave to grey
282 {
283 for (x = 0; x < width; x++)
284 {
285 pixel = 3*(y*width + x);
286 buffer[x] = (wxUint8)(.299*data[pixel] +
287 .587*data[pixel+1] +
288 .114*data[pixel+2]);
289 }
290 }
291 else if (format == wxBMP_8BPP_RED) // 1 byte per pixel, red as greys
292 {
293 for (x = 0; x < width; x++)
294 {
295 buffer[x] = (wxUint8)data[3*(y*width + x)];
296 }
297 }
298 else if (format == wxBMP_4BPP) // 4 bpp in color
299 {
300 for (x = 0; x < width; x+=2)
301 {
302 pixel = 3*(y*width + x);
303
304 // fill buffer, ignore if > width
305 buffer[x/2] =
306 ((wxUint8)palette->GetPixel(data[pixel], data[pixel+1], data[pixel+2]) << 4) |
307 (((x+1) > width) ? 0 : ((wxUint8)palette->GetPixel(data[pixel+3], data[pixel+4], data[pixel+5]) ));
308 }
309 }
310 else if (format == wxBMP_1BPP) // 1 bpp in "color"
311 {
312 for (x = 0; x < width; x+=8)
313 {
314 pixel = 3*(y*width + x);
315
316 buffer[x/8] =
317 ((wxUint8)palette->GetPixel(data[pixel], data[pixel+1], data[pixel+2]) << 7) |
318 (((x+1) > width) ? 0 : ((wxUint8)palette->GetPixel(data[pixel+3], data[pixel+4], data[pixel+5]) << 6)) |
319 (((x+2) > width) ? 0 : ((wxUint8)palette->GetPixel(data[pixel+6], data[pixel+7], data[pixel+8]) << 5)) |
320 (((x+3) > width) ? 0 : ((wxUint8)palette->GetPixel(data[pixel+9], data[pixel+10], data[pixel+11]) << 4)) |
321 (((x+4) > width) ? 0 : ((wxUint8)palette->GetPixel(data[pixel+12], data[pixel+13], data[pixel+14]) << 3)) |
322 (((x+5) > width) ? 0 : ((wxUint8)palette->GetPixel(data[pixel+15], data[pixel+16], data[pixel+17]) << 2)) |
323 (((x+6) > width) ? 0 : ((wxUint8)palette->GetPixel(data[pixel+18], data[pixel+19], data[pixel+20]) << 1)) |
324 (((x+7) > width) ? 0 : ((wxUint8)palette->GetPixel(data[pixel+21], data[pixel+22], data[pixel+23]) ));
325 }
326 }
327 else if (format == wxBMP_1BPP_BW) // 1 bpp B&W colormap from red color ONLY
328 {
329 for (x = 0; x < width; x+=8)
330 {
331 pixel = 3*(y*width + x);
332
333 buffer[x/8] =
334 (((wxUint8)(data[pixel] /128.)) << 7) |
335 ( ((x+1) > width) ? 0 : (((wxUint8)(data[pixel+3] /128.)) << 6)) |
336 ( ((x+2) > width) ? 0 : (((wxUint8)(data[pixel+6] /128.)) << 5)) |
337 ( ((x+3) > width) ? 0 : (((wxUint8)(data[pixel+9] /128.)) << 4)) |
338 ( ((x+4) > width) ? 0 : (((wxUint8)(data[pixel+12]/128.)) << 3)) |
339 ( ((x+5) > width) ? 0 : (((wxUint8)(data[pixel+15]/128.)) << 2)) |
340 ( ((x+6) > width) ? 0 : (((wxUint8)(data[pixel+18]/128.)) << 1)) |
341 ( ((x+7) > width) ? 0 : (((wxUint8)(data[pixel+21]/128.)) ));
342 }
343 }
344
345 if (!stream.Write(buffer, row_width))
346 {
347 if (verbose)
348 wxLogError(_("BMP: Couldn't write data."));
349 delete[] buffer;
350 if (palette) delete palette;
351 if (q_image) delete q_image;
352 return FALSE;
353 }
354 }
355 delete[] buffer;
356 if (palette) delete palette;
357 if (q_image) delete q_image;
358
359 return TRUE;
360 }
361
362
363
364 #ifndef BI_RGB
365 #define BI_RGB 0
366 #define BI_RLE8 1
367 #define BI_RLE4 2
368 #endif
369
370 #ifndef BI_BITFIELDS
371 #define BI_BITFIELDS 3
372 #endif
373
374 #define poffset (line * width * 3 + column * 3)
375
376 bool wxBMPHandler::LoadFile( wxImage *image, wxInputStream& stream, bool verbose, int WXUNUSED(index) )
377 {
378 int rshift = 0, gshift = 0, bshift = 0;
379 wxUint8 aByte;
380 wxUint16 aWord;
381 wxInt32 dbuf[4];
382 wxInt32 aDword, rmask = 0, gmask = 0, bmask = 0;
383 wxInt8 bbuf[4];
384 struct _cmap {
385 unsigned char r, g, b;
386 } *cmap = NULL;
387
388 off_t start_offset = stream.TellI();
389 if (start_offset == wxInvalidOffset) start_offset = 0;
390
391 image->Destroy();
392
393 /*
394 * Read the BMP header
395 */
396
397 stream.Read( bbuf, 2 );
398 stream.Read( dbuf, 4 * 4 );
399
400 #if 0 // unused
401 wxInt32 size = wxINT32_SWAP_ON_BE( dbuf[0] );
402 #endif
403 wxInt32 offset = wxINT32_SWAP_ON_BE( dbuf[2] );
404
405 stream.Read(dbuf, 4 * 2);
406 int width = (int)wxINT32_SWAP_ON_BE( dbuf[0] );
407 int height = (int)wxINT32_SWAP_ON_BE( dbuf[1] );
408 if (width > 32767)
409 {
410 if (verbose)
411 wxLogError( _("BMP: Image width > 32767 pixels for file.") );
412 return FALSE;
413 }
414 if (height > 32767)
415 {
416 if (verbose)
417 wxLogError( _("BMP: Image height > 32767 pixels for file.") );
418 return FALSE;
419 }
420
421 stream.Read( &aWord, 2 );
422 /*
423 TODO
424 int planes = (int)wxUINT16_SWAP_ON_BE( aWord );
425 */
426 stream.Read( &aWord, 2 );
427 int bpp = (int)wxUINT16_SWAP_ON_BE( aWord );
428 if (bpp != 1 && bpp != 4 && bpp != 8 && bpp != 16 && bpp != 24 && bpp != 32)
429 {
430 if (verbose)
431 wxLogError( _("BMP: Unknown bitdepth in file.") );
432 return FALSE;
433 }
434
435 stream.Read( dbuf, 4 * 4 );
436 int comp = (int)wxINT32_SWAP_ON_BE( dbuf[0] );
437 if (comp != BI_RGB && comp != BI_RLE4 && comp != BI_RLE8 && comp != BI_BITFIELDS)
438 {
439 if (verbose)
440 wxLogError( _("BMP: Unknown encoding in file.") );
441 return FALSE;
442 }
443
444 stream.Read( dbuf, 4 * 2 );
445 int ncolors = (int)wxINT32_SWAP_ON_BE( dbuf[0] );
446 if (ncolors == 0)
447 ncolors = 1 << bpp;
448 /* some more sanity checks */
449 if (((comp == BI_RLE4) && (bpp != 4)) ||
450 ((comp == BI_RLE8) && (bpp != 8)) ||
451 ((comp == BI_BITFIELDS) && (bpp != 16 && bpp != 32)))
452 {
453 if (verbose)
454 wxLogError( _("BMP: Encoding doesn't match bitdepth.") );
455 return FALSE;
456 }
457 if (bpp < 16)
458 {
459 cmap = (struct _cmap *)malloc(sizeof(struct _cmap) * ncolors);
460 if (!cmap)
461 {
462 if (verbose)
463 wxLogError( _("BMP: Couldn't allocate memory.") );
464 return FALSE;
465 }
466 }
467 else
468 cmap = NULL;
469
470 image->Create( width, height );
471 unsigned char *ptr = image->GetData();
472 if (!ptr)
473 {
474 if (verbose)
475 wxLogError( _("BMP: Couldn't allocate memory.") );
476 if (cmap)
477 free(cmap);
478 return FALSE;
479 }
480
481 /*
482 * Reading the palette, if it exists.
483 */
484 if (bpp < 16 && ncolors != 0)
485 {
486 unsigned char* r = new unsigned char[ncolors];
487 unsigned char* g = new unsigned char[ncolors];
488 unsigned char* b = new unsigned char[ncolors];
489 for (int j = 0; j < ncolors; j++)
490 {
491 stream.Read( bbuf, 4 );
492 cmap[j].b = bbuf[0];
493 cmap[j].g = bbuf[1];
494 cmap[j].r = bbuf[2];
495
496 r[j] = cmap[j].r;
497 g[j] = cmap[j].g;
498 b[j] = cmap[j].b;
499 }
500 // Set the palette for the wxImage
501 image->SetPalette(wxPalette(ncolors, r, g, b));
502
503 delete[] r;
504 delete[] g;
505 delete[] b;
506 }
507 else if (bpp == 16 || bpp == 32)
508 {
509 if (comp == BI_BITFIELDS)
510 {
511 int bit = 0;
512 stream.Read( dbuf, 4 * 3 );
513 bmask = wxINT32_SWAP_ON_BE( dbuf[0] );
514 gmask = wxINT32_SWAP_ON_BE( dbuf[1] );
515 rmask = wxINT32_SWAP_ON_BE( dbuf[2] );
516 /* find shift amount.. ugly, but i can't think of a better way */
517 for (bit = 0; bit < bpp; bit++)
518 {
519 if (bmask & (1 << bit))
520 bshift = bit;
521 if (gmask & (1 << bit))
522 gshift = bit;
523 if (rmask & (1 << bit))
524 rshift = bit;
525 }
526 }
527 else if (bpp == 16)
528 {
529 rmask = 0x7C00;
530 gmask = 0x03E0;
531 bmask = 0x001F;
532 rshift = 10;
533 gshift = 5;
534 bshift = 0;
535 }
536 else if (bpp == 32)
537 {
538 rmask = 0x00FF0000;
539 gmask = 0x0000FF00;
540 bmask = 0x000000FF;
541 rshift = 16;
542 gshift = 8;
543 bshift = 0;
544 }
545 }
546
547 /*
548 * Reading the image data
549 */
550 stream.SeekI( start_offset + offset );
551 unsigned char *data = ptr;
552
553 /* set the whole image to the background color */
554 if (bpp < 16 && (comp == BI_RLE4 || comp == BI_RLE8))
555 {
556 for (int i = 0; i < width * height; i++)
557 {
558 *ptr++ = cmap[0].r;
559 *ptr++ = cmap[0].g;
560 *ptr++ = cmap[0].b;
561 }
562 ptr = data;
563 }
564
565 int line = 0;
566 int column = 0;
567 int linesize = ((width * bpp + 31) / 32) * 4;
568
569 /* BMPs are stored upside down */
570 for (line = (height - 1); line >= 0; line--)
571 {
572 int linepos = 0;
573 for (column = 0; column < width;)
574 {
575 if (bpp < 16)
576 {
577 int index = 0;
578 linepos++;
579 aByte = stream.GetC();
580 if (bpp == 1)
581 {
582 int bit = 0;
583 for (bit = 0; bit < 8 && column < width; bit++)
584 {
585 index = ((aByte & (0x80 >> bit)) ? 1 : 0);
586 ptr[poffset] = cmap[index].r;
587 ptr[poffset + 1] = cmap[index].g;
588 ptr[poffset + 2] = cmap[index].b;
589 column++;
590 }
591 }
592 else if (bpp == 4)
593 {
594 if (comp == BI_RLE4)
595 {
596 if (verbose)
597 wxLogError( _("BMP: Cannot deal with 4bit encoded yet.") );
598 image->Destroy();
599 free(cmap);
600 return FALSE;
601 }
602 else
603 {
604 int nibble = 0;
605 for (nibble = 0; nibble < 2 && column < width; nibble++)
606 {
607 index = ((aByte & (0xF0 >> nibble * 4)) >> (!nibble * 4));
608 if (index >= 16)
609 index = 15;
610 ptr[poffset] = cmap[index].r;
611 ptr[poffset + 1] = cmap[index].g;
612 ptr[poffset + 2] = cmap[index].b;
613 column++;
614 }
615 }
616 }
617 else if (bpp == 8)
618 {
619 if (comp == BI_RLE8)
620 {
621 unsigned char first;
622 first = aByte;
623 aByte = stream.GetC();
624 if (first == 0)
625 {
626 if (aByte == 0)
627 {
628 /* column = width; */
629 }
630 else if (aByte == 1)
631 {
632 column = width;
633 line = -1;
634 }
635 else if (aByte == 2)
636 {
637 aByte = stream.GetC();
638 column += aByte;
639 linepos = column * bpp / 8;
640 aByte = stream.GetC();
641 line += aByte;
642 }
643 else
644 {
645 int absolute = aByte;
646 for (int k = 0; k < absolute; k++)
647 {
648 linepos++;
649 aByte = stream.GetC();
650 ptr[poffset ] = cmap[aByte].r;
651 ptr[poffset + 1] = cmap[aByte].g;
652 ptr[poffset + 2] = cmap[aByte].b;
653 column++;
654 }
655 if (absolute & 0x01)
656 aByte = stream.GetC();
657 }
658 }
659 else
660 {
661 for (int l = 0; l < first && column < width; l++)
662 {
663 ptr[poffset ] = cmap[aByte].r;
664 ptr[poffset + 1] = cmap[aByte].g;
665 ptr[poffset + 2] = cmap[aByte].b;
666 column++;
667 linepos++;
668 }
669 }
670 }
671 else
672 {
673 ptr[poffset ] = cmap[aByte].r;
674 ptr[poffset + 1] = cmap[aByte].g;
675 ptr[poffset + 2] = cmap[aByte].b;
676 column++;
677 // linepos += size; seems to be wrong, RR
678 }
679 }
680 }
681 else if (bpp == 24)
682 {
683 stream.Read( bbuf, 3 );
684 linepos += 3;
685 ptr[poffset ] = (unsigned char)bbuf[2];
686 ptr[poffset + 1] = (unsigned char)bbuf[1];
687 ptr[poffset + 2] = (unsigned char)bbuf[0];
688 column++;
689 }
690 else if (bpp == 16)
691 {
692 unsigned char temp;
693 stream.Read( &aWord, 2 );
694 aWord = wxUINT16_SWAP_ON_BE( aWord );
695 linepos += 2;
696 temp = (aWord & rmask) >> rshift;
697 ptr[poffset] = temp;
698 temp = (aWord & gmask) >> gshift;
699 ptr[poffset + 1] = temp;
700 temp = (aWord & bmask) >> bshift;
701 ptr[poffset + 2] = temp;
702 column++;
703 }
704 else
705 {
706 unsigned char temp;
707 stream.Read( &aDword, 4 );
708 aDword = wxINT32_SWAP_ON_BE( aDword );
709 linepos += 4;
710 temp = (aDword & rmask) >> rshift;
711 ptr[poffset] = temp;
712 temp = (aDword & gmask) >> gshift;
713 ptr[poffset + 1] = temp;
714 temp = (aDword & bmask) >> bshift;
715 ptr[poffset + 2] = temp;
716 column++;
717 }
718 }
719 while ((linepos < linesize) && (comp != 1) && (comp != 2))
720 {
721 stream.Read( &aByte, 1 );
722 linepos += 1;
723 if (stream.LastError() != wxStream_NOERROR)
724 break;
725 }
726 }
727 if (cmap)
728 free(cmap);
729
730 image->SetMask( FALSE );
731
732 return TRUE;
733 }
734
735 bool wxBMPHandler::DoCanRead( wxInputStream& stream )
736 {
737 unsigned char hdr[2];
738
739 stream.Read(hdr, 2);
740 stream.SeekI(-2, wxFromCurrent);
741 return (hdr[0] == 'B' && hdr[1] == 'M');
742 }
743
744 #endif // wxUSE_STREAMS
745
746 #endif // wxUSE_IMAGE