]>
Commit | Line | Data |
---|---|---|
e65ed37a | 1 | ///////////////////////////////////////////////////////////////////////////// |
8898456d | 2 | // Name: src/common/imagbmp.cpp |
05813ada VS |
3 | // Purpose: wxImage BMP,ICO and CUR handlers |
4 | // Author: Robert Roebling, Chris Elliott | |
e65ed37a | 5 | // RCS-ID: $Id$ |
05813ada | 6 | // Copyright: (c) Robert Roebling, Chris Elliott |
65571936 | 7 | // Licence: wxWindows licence |
e65ed37a RR |
8 | ///////////////////////////////////////////////////////////////////////////// |
9 | ||
e65ed37a RR |
10 | // For compilers that support precompilation, includes "wx.h". |
11 | #include "wx/wxprec.h" | |
12 | ||
13 | #ifdef __BORLANDC__ | |
8898456d | 14 | #pragma hdrstop |
e65ed37a RR |
15 | #endif |
16 | ||
e30285ab | 17 | #if wxUSE_IMAGE |
c96ea657 | 18 | |
5ff14574 PC |
19 | #include "wx/imagbmp.h" |
20 | ||
8898456d | 21 | #ifndef WX_PRECOMP |
57bd4c60 WS |
22 | #ifdef __WXMSW__ |
23 | #include "wx/msw/wrapwin.h" | |
24 | #endif | |
8898456d WS |
25 | #include "wx/log.h" |
26 | #include "wx/app.h" | |
0bca0373 | 27 | #include "wx/bitmap.h" |
5ff14574 PC |
28 | #include "wx/palette.h" |
29 | #include "wx/intl.h" | |
37ba70a5 | 30 | #include "wx/math.h" |
8898456d WS |
31 | #endif |
32 | ||
e65ed37a RR |
33 | #include "wx/filefn.h" |
34 | #include "wx/wfstream.h" | |
1971d23c | 35 | #include "wx/quantize.h" |
ef5571c6 | 36 | #include "wx/scopeguard.h" |
72045d57 | 37 | #include "wx/anidecod.h" |
e65ed37a RR |
38 | |
39 | // For memcpy | |
40 | #include <string.h> | |
41 | ||
e65ed37a | 42 | //----------------------------------------------------------------------------- |
05813ada | 43 | // wxBMPHandler |
e65ed37a RR |
44 | //----------------------------------------------------------------------------- |
45 | ||
e65ed37a | 46 | IMPLEMENT_DYNAMIC_CLASS(wxBMPHandler,wxImageHandler) |
e65ed37a | 47 | |
e30285ab VZ |
48 | #if wxUSE_STREAMS |
49 | ||
37b83ca6 | 50 | #ifndef BI_RGB |
35b463c0 VZ |
51 | #define BI_RGB 0 |
52 | #endif | |
53 | ||
54 | #ifndef BI_RLE8 | |
37b83ca6 | 55 | #define BI_RLE8 1 |
35b463c0 VZ |
56 | #endif |
57 | ||
58 | #ifndef BI_RLE4 | |
37b83ca6 VS |
59 | #define BI_RLE4 2 |
60 | #endif | |
61 | ||
62 | #ifndef BI_BITFIELDS | |
63 | #define BI_BITFIELDS 3 | |
64 | #endif | |
65 | ||
66 | #define poffset (line * width * 3 + column * 3) | |
67 | ||
f6bcfd97 BP |
68 | bool wxBMPHandler::SaveFile(wxImage *image, |
69 | wxOutputStream& stream, | |
70 | bool verbose) | |
574c939e | 71 | { |
7beb59f3 | 72 | return SaveDib(image, stream, verbose, true/*IsBmp*/, false/*IsMask*/); |
37b83ca6 VS |
73 | } |
74 | ||
75 | bool wxBMPHandler::SaveDib(wxImage *image, | |
05813ada VS |
76 | wxOutputStream& stream, |
77 | bool verbose, | |
78 | bool IsBmp, | |
79 | bool IsMask) | |
37b83ca6 | 80 | |
f6bcfd97 | 81 | { |
7beb59f3 | 82 | wxCHECK_MSG( image, false, _T("invalid pointer in wxBMPHandler::SaveFile") ); |
f6bcfd97 | 83 | |
05813ada | 84 | if ( !image->Ok() ) |
f6bcfd97 | 85 | { |
574c939e | 86 | if ( verbose ) |
05813ada | 87 | wxLogError(_("BMP: Couldn't save invalid image.")); |
7beb59f3 | 88 | return false; |
f6bcfd97 BP |
89 | } |
90 | ||
1971d23c VZ |
91 | // get the format of the BMP file to save, else use 24bpp |
92 | unsigned format = wxBMP_24BPP; | |
fd94e8aa VS |
93 | if ( image->HasOption(wxIMAGE_OPTION_BMP_FORMAT) ) |
94 | format = image->GetOptionInt(wxIMAGE_OPTION_BMP_FORMAT); | |
1971d23c | 95 | |
574c939e | 96 | wxUint16 bpp; // # of bits per pixel |
1971d23c VZ |
97 | int palette_size; // # of color map entries, ie. 2^bpp colors |
98 | ||
99 | // set the bpp and appropriate palette_size, and do additional checks | |
05813ada | 100 | if ( (format == wxBMP_1BPP) || (format == wxBMP_1BPP_BW) ) |
1971d23c VZ |
101 | { |
102 | bpp = 1; | |
103 | palette_size = 2; | |
104 | } | |
05813ada | 105 | else if ( format == wxBMP_4BPP ) |
1971d23c VZ |
106 | { |
107 | bpp = 4; | |
108 | palette_size = 16; | |
109 | } | |
05813ada VS |
110 | else if ( (format == wxBMP_8BPP) || (format == wxBMP_8BPP_GREY) || |
111 | (format == wxBMP_8BPP_RED) || (format == wxBMP_8BPP_PALETTE) ) | |
1971d23c VZ |
112 | { |
113 | // need to set a wxPalette to use this, HOW TO CHECK IF VALID, SIZE? | |
b11e8fb6 VZ |
114 | if ((format == wxBMP_8BPP_PALETTE) |
115 | #if wxUSE_PALETTE | |
116 | && !image->HasPalette() | |
117 | #endif // wxUSE_PALETTE | |
118 | ) | |
1971d23c | 119 | { |
05813ada | 120 | if ( verbose ) |
dc9b623b | 121 | wxLogError(_("BMP: wxImage doesn't have own wxPalette.")); |
7beb59f3 | 122 | return false; |
1971d23c VZ |
123 | } |
124 | bpp = 8; | |
125 | palette_size = 256; | |
126 | } | |
127 | else // you get 24bpp | |
128 | { | |
129 | format = wxBMP_24BPP; | |
130 | bpp = 24; | |
131 | palette_size = 0; | |
132 | } | |
133 | ||
f6bcfd97 | 134 | unsigned width = image->GetWidth(); |
1971d23c VZ |
135 | unsigned row_padding = (4 - int(width*bpp/8.0) % 4) % 4; // # bytes to pad to dword |
136 | unsigned row_width = int(width * bpp/8.0) + row_padding; // # of bytes per row | |
137 | ||
f6bcfd97 BP |
138 | struct |
139 | { | |
140 | // BitmapHeader: | |
141 | wxUint16 magic; // format magic, always 'BM' | |
142 | wxUint32 filesize; // total file size, inc. headers | |
143 | wxUint32 reserved; // for future use | |
144 | wxUint32 data_offset; // image data offset in the file | |
33ac7e6f | 145 | |
f6bcfd97 BP |
146 | // BitmapInfoHeader: |
147 | wxUint32 bih_size; // 2nd part's size | |
148 | wxUint32 width, height; // bitmap's dimensions | |
149 | wxUint16 planes; // num of planes | |
150 | wxUint16 bpp; // bits per pixel | |
151 | wxUint32 compression; // compression method | |
152 | wxUint32 size_of_bmp; // size of the bitmap | |
37ba70a5 | 153 | wxUint32 h_res, v_res; // image resolution in pixels-per-meter |
f6bcfd97 BP |
154 | wxUint32 num_clrs; // number of colors used |
155 | wxUint32 num_signif_clrs;// number of significant colors | |
156 | } hdr; | |
1971d23c | 157 | |
f6bcfd97 BP |
158 | wxUint32 hdr_size = 14/*BitmapHeader*/ + 40/*BitmapInfoHeader*/; |
159 | ||
160 | hdr.magic = wxUINT16_SWAP_ON_BE(0x4D42/*'BM'*/); | |
1971d23c VZ |
161 | hdr.filesize = wxUINT32_SWAP_ON_BE( hdr_size + palette_size*4 + |
162 | row_width * image->GetHeight() ); | |
f6bcfd97 | 163 | hdr.reserved = 0; |
1971d23c | 164 | hdr.data_offset = wxUINT32_SWAP_ON_BE(hdr_size + palette_size*4); |
33ac7e6f | 165 | |
f6bcfd97 BP |
166 | hdr.bih_size = wxUINT32_SWAP_ON_BE(hdr_size - 14); |
167 | hdr.width = wxUINT32_SWAP_ON_BE(image->GetWidth()); | |
05813ada | 168 | if ( IsBmp ) |
37b83ca6 | 169 | { |
05813ada | 170 | hdr.height = wxUINT32_SWAP_ON_BE(image->GetHeight()); |
37b83ca6 VS |
171 | } |
172 | else | |
173 | { | |
174 | hdr.height = wxUINT32_SWAP_ON_BE(2 * image->GetHeight()); | |
05813ada | 175 | } |
f6bcfd97 | 176 | hdr.planes = wxUINT16_SWAP_ON_BE(1); // always 1 plane |
1971d23c | 177 | hdr.bpp = wxUINT16_SWAP_ON_BE(bpp); |
f6bcfd97 | 178 | hdr.compression = 0; // RGB uncompressed |
33ac7e6f | 179 | hdr.size_of_bmp = wxUINT32_SWAP_ON_BE(row_width * image->GetHeight()); |
37ba70a5 VZ |
180 | |
181 | // get the resolution from the image options or fall back to 72dpi standard | |
182 | // for the BMP format if not specified | |
fe7f0a59 VZ |
183 | int hres, vres; |
184 | switch ( GetResolutionFromOptions(*image, &hres, &vres) ) | |
37ba70a5 VZ |
185 | { |
186 | default: | |
187 | wxFAIL_MSG( _T("unexpected image resolution units") ); | |
188 | // fall through | |
189 | ||
190 | case wxIMAGE_RESOLUTION_NONE: | |
191 | hres = | |
192 | vres = 72; | |
193 | // fall through to convert it to correct units | |
194 | ||
195 | case wxIMAGE_RESOLUTION_INCHES: | |
196 | // convert resolution in inches to resolution in centimeters | |
fe7f0a59 VZ |
197 | hres = (int)(10*mm2inches*hres); |
198 | vres = (int)(10*mm2inches*vres); | |
37ba70a5 VZ |
199 | // fall through to convert it to resolution in meters |
200 | ||
201 | case wxIMAGE_RESOLUTION_CM: | |
202 | // convert resolution in centimeters to resolution in meters | |
203 | hres *= 100; | |
204 | vres *= 100; | |
205 | break; | |
206 | } | |
207 | ||
208 | hdr.h_res = wxUINT32_SWAP_ON_BE(hres); | |
209 | hdr.v_res = wxUINT32_SWAP_ON_BE(vres); | |
1971d23c VZ |
210 | hdr.num_clrs = wxUINT32_SWAP_ON_BE(palette_size); // # colors in colormap |
211 | hdr.num_signif_clrs = 0; // all colors are significant | |
574c939e | 212 | |
05813ada | 213 | if ( IsBmp ) |
37b83ca6 | 214 | { |
05813ada VS |
215 | if (// VS: looks ugly but compilers tend to do ugly things with structs, |
216 | // like aligning hdr.filesize's ofset to dword :( | |
217 | // VZ: we should add padding then... | |
218 | !stream.Write(&hdr.magic, 2) || | |
219 | !stream.Write(&hdr.filesize, 4) || | |
220 | !stream.Write(&hdr.reserved, 4) || | |
574c939e | 221 | !stream.Write(&hdr.data_offset, 4) |
05813ada VS |
222 | ) |
223 | { | |
224 | if (verbose) | |
225 | wxLogError(_("BMP: Couldn't write the file (Bitmap) header.")); | |
7beb59f3 | 226 | return false; |
05813ada VS |
227 | } |
228 | } | |
229 | if ( !IsMask ) | |
f6bcfd97 | 230 | { |
05813ada VS |
231 | if ( |
232 | !stream.Write(&hdr.bih_size, 4) || | |
233 | !stream.Write(&hdr.width, 4) || | |
234 | !stream.Write(&hdr.height, 4) || | |
235 | !stream.Write(&hdr.planes, 2) || | |
236 | !stream.Write(&hdr.bpp, 2) || | |
237 | !stream.Write(&hdr.compression, 4) || | |
238 | !stream.Write(&hdr.size_of_bmp, 4) || | |
239 | !stream.Write(&hdr.h_res, 4) || | |
240 | !stream.Write(&hdr.v_res, 4) || | |
241 | !stream.Write(&hdr.num_clrs, 4) || | |
242 | !stream.Write(&hdr.num_signif_clrs, 4) | |
243 | ) | |
244 | { | |
245 | if (verbose) | |
246 | wxLogError(_("BMP: Couldn't write the file (BitmapInfo) header.")); | |
7beb59f3 | 247 | return false; |
05813ada | 248 | } |
f6bcfd97 BP |
249 | } |
250 | ||
1971d23c VZ |
251 | wxPalette *palette = NULL; // entries for quantized images |
252 | wxUint8 *rgbquad = NULL; // for the RGBQUAD bytes for the colormap | |
253 | wxImage *q_image = NULL; // destination for quantized image | |
254 | ||
255 | // if <24bpp use quantization to reduce colors for *some* of the formats | |
256 | if ( (format == wxBMP_1BPP) || (format == wxBMP_4BPP) || | |
05813ada | 257 | (format == wxBMP_8BPP) || (format == wxBMP_8BPP_PALETTE) ) |
1971d23c VZ |
258 | { |
259 | // make a new palette and quantize the image | |
260 | if (format != wxBMP_8BPP_PALETTE) | |
261 | { | |
262 | q_image = new wxImage(); | |
263 | ||
264 | // I get a delete error using Quantize when desired colors > 236 | |
265 | int quantize = ((palette_size > 236) ? 236 : palette_size); | |
266 | // fill the destination too, it gives much nicer 4bpp images | |
267 | wxQuantize::Quantize( *image, *q_image, &palette, quantize, 0, | |
268 | wxQUANTIZE_FILL_DESTINATION_IMAGE ); | |
269 | } | |
270 | else | |
271 | { | |
b11e8fb6 | 272 | #if wxUSE_PALETTE |
1971d23c | 273 | palette = new wxPalette(image->GetPalette()); |
b11e8fb6 | 274 | #endif // wxUSE_PALETTE |
1971d23c VZ |
275 | } |
276 | ||
277 | int i; | |
278 | unsigned char r, g, b; | |
279 | rgbquad = new wxUint8 [palette_size*4]; | |
280 | ||
05813ada | 281 | for (i = 0; i < palette_size; i++) |
1971d23c | 282 | { |
b11e8fb6 | 283 | #if wxUSE_PALETTE |
05813ada | 284 | if ( !palette->GetRGB(i, &r, &g, &b) ) |
b11e8fb6 VZ |
285 | #endif // wxUSE_PALETTE |
286 | r = g = b = 0; | |
1971d23c VZ |
287 | |
288 | rgbquad[i*4] = b; | |
289 | rgbquad[i*4+1] = g; | |
290 | rgbquad[i*4+2] = r; | |
291 | rgbquad[i*4+3] = 0; | |
292 | } | |
293 | } | |
294 | // make a 256 entry greyscale colormap or 2 entry black & white | |
05813ada VS |
295 | else if ( (format == wxBMP_8BPP_GREY) || (format == wxBMP_8BPP_RED) || |
296 | (format == wxBMP_1BPP_BW) ) | |
1971d23c | 297 | { |
1971d23c VZ |
298 | rgbquad = new wxUint8 [palette_size*4]; |
299 | ||
0ce5a3a8 | 300 | for ( int i = 0; i < palette_size; i++ ) |
1971d23c | 301 | { |
0ce5a3a8 | 302 | // if 1BPP_BW then the value should be either 0 or 255 |
11846af3 | 303 | wxUint8 c = (wxUint8)((i > 0) && (format == wxBMP_1BPP_BW) ? 255 : i); |
0ce5a3a8 VZ |
304 | |
305 | rgbquad[i*4] = | |
306 | rgbquad[i*4+1] = | |
307 | rgbquad[i*4+2] = c; | |
1971d23c VZ |
308 | rgbquad[i*4+3] = 0; |
309 | } | |
310 | } | |
311 | ||
312 | // if the colormap was made, then it needs to be written | |
313 | if (rgbquad) | |
314 | { | |
05813ada | 315 | if ( !IsMask ) |
1971d23c | 316 | { |
05813ada VS |
317 | if ( !stream.Write(rgbquad, palette_size*4) ) |
318 | { | |
319 | if (verbose) | |
320 | wxLogError(_("BMP: Couldn't write RGB color map.")); | |
321 | delete[] rgbquad; | |
b11e8fb6 | 322 | #if wxUSE_PALETTE |
05813ada | 323 | delete palette; |
b11e8fb6 | 324 | #endif // wxUSE_PALETTE |
05813ada | 325 | delete q_image; |
7beb59f3 | 326 | return false; |
05813ada VS |
327 | } |
328 | } | |
1971d23c VZ |
329 | delete []rgbquad; |
330 | } | |
331 | ||
332 | // pointer to the image data, use quantized if available | |
f6bcfd97 | 333 | wxUint8 *data = (wxUint8*) image->GetData(); |
1971d23c VZ |
334 | if (q_image) if (q_image->Ok()) data = (wxUint8*) q_image->GetData(); |
335 | ||
f6bcfd97 | 336 | wxUint8 *buffer = new wxUint8[row_width]; |
f6bcfd97 BP |
337 | memset(buffer, 0, row_width); |
338 | int y; unsigned x; | |
1971d23c | 339 | long int pixel; |
f6bcfd97 | 340 | |
05813ada | 341 | for (y = image->GetHeight() -1; y >= 0; y--) |
f6bcfd97 | 342 | { |
05813ada | 343 | if ( format == wxBMP_24BPP ) // 3 bytes per pixel red,green,blue |
1971d23c | 344 | { |
05813ada | 345 | for ( x = 0; x < width; x++ ) |
1971d23c VZ |
346 | { |
347 | pixel = 3*(y*width + x); | |
348 | ||
349 | buffer[3*x ] = data[pixel+2]; | |
350 | buffer[3*x + 1] = data[pixel+1]; | |
351 | buffer[3*x + 2] = data[pixel]; | |
352 | } | |
353 | } | |
354 | else if ((format == wxBMP_8BPP) || // 1 byte per pixel in color | |
355 | (format == wxBMP_8BPP_PALETTE)) | |
356 | { | |
357 | for (x = 0; x < width; x++) | |
358 | { | |
359 | pixel = 3*(y*width + x); | |
b11e8fb6 | 360 | #if wxUSE_PALETTE |
9a431f4d WS |
361 | buffer[x] = (wxUint8)palette->GetPixel( data[pixel], |
362 | data[pixel+1], | |
363 | data[pixel+2] ); | |
b11e8fb6 VZ |
364 | #else |
365 | // FIXME: what should this be? use some std palette maybe? | |
366 | buffer[x] = 0; | |
367 | #endif // wxUSE_PALETTE | |
1971d23c VZ |
368 | } |
369 | } | |
05813ada | 370 | else if ( format == wxBMP_8BPP_GREY ) // 1 byte per pix, rgb ave to grey |
1971d23c VZ |
371 | { |
372 | for (x = 0; x < width; x++) | |
373 | { | |
374 | pixel = 3*(y*width + x); | |
375 | buffer[x] = (wxUint8)(.299*data[pixel] + | |
376 | .587*data[pixel+1] + | |
377 | .114*data[pixel+2]); | |
378 | } | |
379 | } | |
05813ada | 380 | else if ( format == wxBMP_8BPP_RED ) // 1 byte per pixel, red as greys |
f6bcfd97 | 381 | { |
1971d23c VZ |
382 | for (x = 0; x < width; x++) |
383 | { | |
384 | buffer[x] = (wxUint8)data[3*(y*width + x)]; | |
385 | } | |
386 | } | |
05813ada | 387 | else if ( format == wxBMP_4BPP ) // 4 bpp in color |
1971d23c VZ |
388 | { |
389 | for (x = 0; x < width; x+=2) | |
390 | { | |
391 | pixel = 3*(y*width + x); | |
392 | ||
393 | // fill buffer, ignore if > width | |
b11e8fb6 | 394 | #if wxUSE_PALETTE |
9a431f4d | 395 | buffer[x/2] = (wxUint8)( |
b11e8fb6 VZ |
396 | ((wxUint8)palette->GetPixel(data[pixel], |
397 | data[pixel+1], | |
398 | data[pixel+2]) << 4) | | |
399 | (((x+1) > width) | |
400 | ? 0 | |
401 | : ((wxUint8)palette->GetPixel(data[pixel+3], | |
402 | data[pixel+4], | |
9a431f4d | 403 | data[pixel+5]) )) ); |
b11e8fb6 VZ |
404 | #else |
405 | // FIXME: what should this be? use some std palette maybe? | |
406 | buffer[x/2] = 0; | |
407 | #endif // wxUSE_PALETTE | |
1971d23c VZ |
408 | } |
409 | } | |
05813ada | 410 | else if ( format == wxBMP_1BPP ) // 1 bpp in "color" |
1971d23c VZ |
411 | { |
412 | for (x = 0; x < width; x+=8) | |
413 | { | |
414 | pixel = 3*(y*width + x); | |
415 | ||
b11e8fb6 | 416 | #if wxUSE_PALETTE |
9a431f4d WS |
417 | buffer[x/8] = (wxUint8)( |
418 | ((wxUint8)palette->GetPixel(data[pixel], data[pixel+1], data[pixel+2]) << 7) | | |
1971d23c VZ |
419 | (((x+1) > width) ? 0 : ((wxUint8)palette->GetPixel(data[pixel+3], data[pixel+4], data[pixel+5]) << 6)) | |
420 | (((x+2) > width) ? 0 : ((wxUint8)palette->GetPixel(data[pixel+6], data[pixel+7], data[pixel+8]) << 5)) | | |
421 | (((x+3) > width) ? 0 : ((wxUint8)palette->GetPixel(data[pixel+9], data[pixel+10], data[pixel+11]) << 4)) | | |
422 | (((x+4) > width) ? 0 : ((wxUint8)palette->GetPixel(data[pixel+12], data[pixel+13], data[pixel+14]) << 3)) | | |
423 | (((x+5) > width) ? 0 : ((wxUint8)palette->GetPixel(data[pixel+15], data[pixel+16], data[pixel+17]) << 2)) | | |
424 | (((x+6) > width) ? 0 : ((wxUint8)palette->GetPixel(data[pixel+18], data[pixel+19], data[pixel+20]) << 1)) | | |
9a431f4d | 425 | (((x+7) > width) ? 0 : ((wxUint8)palette->GetPixel(data[pixel+21], data[pixel+22], data[pixel+23]) )) ); |
b11e8fb6 VZ |
426 | #else |
427 | // FIXME: what should this be? use some std palette maybe? | |
428 | buffer[x/8] = 0; | |
429 | #endif // wxUSE_PALETTE | |
1971d23c VZ |
430 | } |
431 | } | |
05813ada | 432 | else if ( format == wxBMP_1BPP_BW ) // 1 bpp B&W colormap from red color ONLY |
1971d23c VZ |
433 | { |
434 | for (x = 0; x < width; x+=8) | |
435 | { | |
436 | pixel = 3*(y*width + x); | |
437 | ||
9a431f4d | 438 | buffer[x/8] = (wxUint8)( |
05813ada VS |
439 | (((wxUint8)(data[pixel] /128.)) << 7) | |
440 | (((x+1) > width) ? 0 : (((wxUint8)(data[pixel+3] /128.)) << 6)) | | |
441 | (((x+2) > width) ? 0 : (((wxUint8)(data[pixel+6] /128.)) << 5)) | | |
442 | (((x+3) > width) ? 0 : (((wxUint8)(data[pixel+9] /128.)) << 4)) | | |
443 | (((x+4) > width) ? 0 : (((wxUint8)(data[pixel+12]/128.)) << 3)) | | |
444 | (((x+5) > width) ? 0 : (((wxUint8)(data[pixel+15]/128.)) << 2)) | | |
445 | (((x+6) > width) ? 0 : (((wxUint8)(data[pixel+18]/128.)) << 1)) | | |
9a431f4d | 446 | (((x+7) > width) ? 0 : (((wxUint8)(data[pixel+21]/128.)) )) ); |
1971d23c | 447 | } |
f6bcfd97 | 448 | } |
33ac7e6f | 449 | |
05813ada | 450 | if ( !stream.Write(buffer, row_width) ) |
f6bcfd97 BP |
451 | { |
452 | if (verbose) | |
453 | wxLogError(_("BMP: Couldn't write data.")); | |
454 | delete[] buffer; | |
b11e8fb6 VZ |
455 | #if wxUSE_PALETTE |
456 | delete palette; | |
457 | #endif // wxUSE_PALETTE | |
458 | delete q_image; | |
7beb59f3 | 459 | return false; |
f6bcfd97 BP |
460 | } |
461 | } | |
462 | delete[] buffer; | |
b11e8fb6 VZ |
463 | #if wxUSE_PALETTE |
464 | delete palette; | |
465 | #endif // wxUSE_PALETTE | |
466 | delete q_image; | |
f6bcfd97 | 467 | |
7beb59f3 | 468 | return true; |
f6bcfd97 BP |
469 | } |
470 | ||
471 | ||
ef5571c6 | 472 | struct BMPPalette |
05813ada | 473 | { |
ef5571c6 VZ |
474 | static void Free(BMPPalette* pal) { delete [] pal; } |
475 | ||
05813ada | 476 | unsigned char r, g, b; |
ef5571c6 | 477 | }; |
f6bcfd97 | 478 | |
574c939e | 479 | bool wxBMPHandler::DoLoadDib(wxImage * image, int width, int height, |
05813ada | 480 | int bpp, int ncolors, int comp, |
1b941f2d | 481 | wxFileOffset bmpOffset, wxInputStream& stream, |
05813ada VS |
482 | bool verbose, bool IsBmp, bool hasPalette) |
483 | { | |
55a35cb0 VZ |
484 | wxInt32 aDword, rmask = 0, gmask = 0, bmask = 0, amask = 0; |
485 | int rshift = 0, gshift = 0, bshift = 0, ashift = 0; | |
d8defde6 | 486 | int rbits = 0, gbits = 0, bbits = 0; |
52b64b0a RR |
487 | wxInt32 dbuf[4]; |
488 | wxInt8 bbuf[4]; | |
489 | wxUint8 aByte; | |
490 | wxUint16 aWord; | |
31528cd3 | 491 | |
05813ada | 492 | // allocate space for palette if needed: |
ef5571c6 | 493 | BMPPalette *cmap; |
31528cd3 | 494 | |
05813ada | 495 | if ( bpp < 16 ) |
e65ed37a | 496 | { |
ef5571c6 | 497 | cmap = new BMPPalette[ncolors]; |
05813ada | 498 | if ( !cmap ) |
e65ed37a | 499 | { |
58c837a4 | 500 | if (verbose) |
05813ada | 501 | wxLogError(_("BMP: Couldn't allocate memory.")); |
7beb59f3 | 502 | return false; |
e65ed37a RR |
503 | } |
504 | } | |
ef5571c6 VZ |
505 | else // no palette |
506 | { | |
e65ed37a | 507 | cmap = NULL; |
ef5571c6 VZ |
508 | } |
509 | ||
510 | wxON_BLOCK_EXIT1(&BMPPalette::Free, cmap); | |
e65ed37a | 511 | |
05813ada | 512 | // destroy existing here instead of: |
52b64b0a | 513 | image->Destroy(); |
05813ada | 514 | image->Create(width, height); |
574c939e | 515 | |
e65ed37a | 516 | unsigned char *ptr = image->GetData(); |
05813ada VS |
517 | |
518 | if ( !ptr ) | |
e65ed37a | 519 | { |
05813ada VS |
520 | if ( verbose ) |
521 | wxLogError( _("BMP: Couldn't allocate memory.") ); | |
7beb59f3 | 522 | return false; |
e65ed37a | 523 | } |
05813ada | 524 | |
55a35cb0 VZ |
525 | unsigned char *alpha; |
526 | if ( bpp == 32 ) | |
527 | { | |
528 | // tell the image to allocate an alpha buffer | |
529 | image->SetAlpha(); | |
530 | alpha = image->GetAlpha(); | |
531 | if ( !alpha ) | |
532 | { | |
533 | if ( verbose ) | |
534 | wxLogError(_("BMP: Couldn't allocate memory.")); | |
55a35cb0 VZ |
535 | return false; |
536 | } | |
537 | } | |
538 | else // no alpha | |
539 | { | |
540 | alpha = NULL; | |
541 | } | |
542 | ||
05813ada VS |
543 | // Reading the palette, if it exists: |
544 | if ( bpp < 16 && ncolors != 0 ) | |
e65ed37a | 545 | { |
3f4fc796 JS |
546 | unsigned char* r = new unsigned char[ncolors]; |
547 | unsigned char* g = new unsigned char[ncolors]; | |
548 | unsigned char* b = new unsigned char[ncolors]; | |
e65ed37a RR |
549 | for (int j = 0; j < ncolors; j++) |
550 | { | |
52b64b0a RR |
551 | if (hasPalette) |
552 | { | |
05813ada VS |
553 | stream.Read(bbuf, 4); |
554 | cmap[j].b = bbuf[0]; | |
555 | cmap[j].g = bbuf[1]; | |
556 | cmap[j].r = bbuf[2]; | |
557 | ||
558 | r[j] = cmap[j].r; | |
559 | g[j] = cmap[j].g; | |
560 | b[j] = cmap[j].b; | |
561 | } | |
52b64b0a RR |
562 | else |
563 | { | |
564 | //used in reading .ico file mask | |
9a431f4d WS |
565 | r[j] = cmap[j].r = |
566 | g[j] = cmap[j].g = | |
567 | b[j] = cmap[j].b = ( j ? 255 : 0 ); | |
52b64b0a RR |
568 | } |
569 | } | |
b11e8fb6 VZ |
570 | |
571 | #if wxUSE_PALETTE | |
3f4fc796 JS |
572 | // Set the palette for the wxImage |
573 | image->SetPalette(wxPalette(ncolors, r, g, b)); | |
b11e8fb6 | 574 | #endif // wxUSE_PALETTE |
3f4fc796 JS |
575 | |
576 | delete[] r; | |
577 | delete[] g; | |
578 | delete[] b; | |
e65ed37a | 579 | } |
05813ada | 580 | else if ( bpp == 16 || bpp == 32 ) |
e65ed37a | 581 | { |
05813ada | 582 | if ( comp == BI_BITFIELDS ) |
e65ed37a RR |
583 | { |
584 | int bit = 0; | |
05813ada | 585 | stream.Read(dbuf, 4 * 3); |
9042a017 | 586 | rmask = wxINT32_SWAP_ON_BE(dbuf[0]); |
05813ada | 587 | gmask = wxINT32_SWAP_ON_BE(dbuf[1]); |
9042a017 VZ |
588 | bmask = wxINT32_SWAP_ON_BE(dbuf[2]); |
589 | // find shift amount (Least significant bit of mask) | |
590 | for (bit = bpp-1; bit>=0; bit--) | |
e65ed37a RR |
591 | { |
592 | if (bmask & (1 << bit)) | |
593 | bshift = bit; | |
594 | if (gmask & (1 << bit)) | |
595 | gshift = bit; | |
596 | if (rmask & (1 << bit)) | |
597 | rshift = bit; | |
598 | } | |
9042a017 VZ |
599 | // Find number of bits in mask (MSB-LSB+1) |
600 | for (bit = 0; bit < bpp; bit++) | |
601 | { | |
602 | if (bmask & (1 << bit)) | |
603 | bbits = bit-bshift+1; | |
604 | if (gmask & (1 << bit)) | |
605 | gbits = bit-gshift+1; | |
606 | if (rmask & (1 << bit)) | |
607 | rbits = bit-rshift+1; | |
608 | } | |
e65ed37a | 609 | } |
05813ada | 610 | else if ( bpp == 16 ) |
e65ed37a RR |
611 | { |
612 | rmask = 0x7C00; | |
613 | gmask = 0x03E0; | |
614 | bmask = 0x001F; | |
615 | rshift = 10; | |
616 | gshift = 5; | |
617 | bshift = 0; | |
9042a017 VZ |
618 | rbits = 5; |
619 | gbits = 5; | |
620 | bbits = 5; | |
e65ed37a | 621 | } |
05813ada | 622 | else if ( bpp == 32 ) |
e65ed37a RR |
623 | { |
624 | rmask = 0x00FF0000; | |
625 | gmask = 0x0000FF00; | |
626 | bmask = 0x000000FF; | |
55a35cb0 VZ |
627 | amask = 0xFF000000; |
628 | ||
629 | ashift = 24; | |
e65ed37a RR |
630 | rshift = 16; |
631 | gshift = 8; | |
632 | bshift = 0; | |
9042a017 VZ |
633 | rbits = 8; |
634 | gbits = 8; | |
635 | bbits = 8; | |
e65ed37a RR |
636 | } |
637 | } | |
638 | ||
639 | /* | |
640 | * Reading the image data | |
641 | */ | |
05813ada VS |
642 | if ( IsBmp ) |
643 | stream.SeekI(bmpOffset); // else icon, just carry on | |
52b64b0a | 644 | |
e65ed37a RR |
645 | unsigned char *data = ptr; |
646 | ||
647 | /* set the whole image to the background color */ | |
05813ada | 648 | if ( bpp < 16 && (comp == BI_RLE4 || comp == BI_RLE8) ) |
e65ed37a RR |
649 | { |
650 | for (int i = 0; i < width * height; i++) | |
651 | { | |
652 | *ptr++ = cmap[0].r; | |
653 | *ptr++ = cmap[0].g; | |
654 | *ptr++ = cmap[0].b; | |
655 | } | |
656 | ptr = data; | |
657 | } | |
31528cd3 | 658 | |
e65ed37a RR |
659 | int linesize = ((width * bpp + 31) / 32) * 4; |
660 | ||
661 | /* BMPs are stored upside down */ | |
999836aa | 662 | for ( int line = (height - 1); line >= 0; line-- ) |
e65ed37a RR |
663 | { |
664 | int linepos = 0; | |
999836aa | 665 | for ( int column = 0; column < width ; ) |
e65ed37a | 666 | { |
05813ada | 667 | if ( bpp < 16 ) |
e65ed37a | 668 | { |
e65ed37a RR |
669 | linepos++; |
670 | aByte = stream.GetC(); | |
05813ada | 671 | if ( bpp == 1 ) |
e65ed37a | 672 | { |
999836aa | 673 | for (int bit = 0; bit < 8 && column < width; bit++) |
e65ed37a | 674 | { |
999836aa | 675 | int index = ((aByte & (0x80 >> bit)) ? 1 : 0); |
e65ed37a RR |
676 | ptr[poffset] = cmap[index].r; |
677 | ptr[poffset + 1] = cmap[index].g; | |
678 | ptr[poffset + 2] = cmap[index].b; | |
679 | column++; | |
680 | } | |
681 | } | |
05813ada | 682 | else if ( bpp == 4 ) |
e65ed37a | 683 | { |
05813ada | 684 | if ( comp == BI_RLE4 ) |
e65ed37a | 685 | { |
2b5f62a0 VZ |
686 | wxUint8 first; |
687 | first = aByte; | |
688 | aByte = stream.GetC(); | |
689 | if ( first == 0 ) | |
690 | { | |
691 | if ( aByte == 0 ) | |
692 | { | |
693 | if ( column > 0 ) | |
694 | column = width; | |
695 | } | |
696 | else if ( aByte == 1 ) | |
697 | { | |
698 | column = width; | |
699 | line = -1; | |
700 | } | |
701 | else if ( aByte == 2 ) | |
702 | { | |
703 | aByte = stream.GetC(); | |
704 | column += aByte; | |
705 | linepos = column * bpp / 4; | |
706 | aByte = stream.GetC(); | |
707 | line -= aByte; // upside down | |
708 | } | |
709 | else | |
710 | { | |
711 | int absolute = aByte; | |
712 | wxUint8 nibble[2] ; | |
713 | int readBytes = 0 ; | |
714 | for (int k = 0; k < absolute; k++) | |
715 | { | |
716 | if ( !(k % 2 ) ) | |
717 | { | |
718 | ++readBytes ; | |
719 | aByte = stream.GetC(); | |
9a431f4d WS |
720 | nibble[0] = (wxUint8)( (aByte & 0xF0) >> 4 ) ; |
721 | nibble[1] = (wxUint8)( aByte & 0x0F ) ; | |
2b5f62a0 VZ |
722 | } |
723 | ptr[poffset ] = cmap[nibble[k%2]].r; | |
724 | ptr[poffset + 1] = cmap[nibble[k%2]].g; | |
725 | ptr[poffset + 2] = cmap[nibble[k%2]].b; | |
726 | column++; | |
727 | if ( k % 2 ) | |
728 | linepos++; | |
729 | } | |
730 | if ( readBytes & 0x01 ) | |
731 | aByte = stream.GetC(); | |
732 | } | |
733 | } | |
734 | else | |
735 | { | |
736 | wxUint8 nibble[2] ; | |
9a431f4d WS |
737 | nibble[0] = (wxUint8)( (aByte & 0xF0) >> 4 ) ; |
738 | nibble[1] = (wxUint8)( aByte & 0x0F ) ; | |
2b5f62a0 VZ |
739 | |
740 | for ( int l = 0; l < first && column < width; l++ ) | |
741 | { | |
742 | ptr[poffset ] = cmap[nibble[l%2]].r; | |
743 | ptr[poffset + 1] = cmap[nibble[l%2]].g; | |
744 | ptr[poffset + 2] = cmap[nibble[l%2]].b; | |
745 | column++; | |
746 | if ( l % 2 ) | |
747 | linepos++; | |
748 | } | |
749 | } | |
e65ed37a RR |
750 | } |
751 | else | |
752 | { | |
999836aa | 753 | for (int nibble = 0; nibble < 2 && column < width; nibble++) |
e65ed37a | 754 | { |
999836aa | 755 | int index = ((aByte & (0xF0 >> nibble * 4)) >> (!nibble * 4)); |
05813ada | 756 | if ( index >= 16 ) |
e65ed37a RR |
757 | index = 15; |
758 | ptr[poffset] = cmap[index].r; | |
759 | ptr[poffset + 1] = cmap[index].g; | |
760 | ptr[poffset + 2] = cmap[index].b; | |
761 | column++; | |
762 | } | |
763 | } | |
764 | } | |
05813ada | 765 | else if ( bpp == 8 ) |
e65ed37a | 766 | { |
05813ada | 767 | if ( comp == BI_RLE8 ) |
e65ed37a RR |
768 | { |
769 | unsigned char first; | |
770 | first = aByte; | |
771 | aByte = stream.GetC(); | |
05813ada | 772 | if ( first == 0 ) |
e65ed37a | 773 | { |
05813ada | 774 | if ( aByte == 0 ) |
e65ed37a RR |
775 | { |
776 | /* column = width; */ | |
777 | } | |
05813ada | 778 | else if ( aByte == 1 ) |
e65ed37a RR |
779 | { |
780 | column = width; | |
781 | line = -1; | |
782 | } | |
05813ada | 783 | else if ( aByte == 2 ) |
e65ed37a RR |
784 | { |
785 | aByte = stream.GetC(); | |
786 | column += aByte; | |
787 | linepos = column * bpp / 8; | |
788 | aByte = stream.GetC(); | |
789 | line += aByte; | |
790 | } | |
791 | else | |
792 | { | |
793 | int absolute = aByte; | |
794 | for (int k = 0; k < absolute; k++) | |
795 | { | |
796 | linepos++; | |
797 | aByte = stream.GetC(); | |
798 | ptr[poffset ] = cmap[aByte].r; | |
799 | ptr[poffset + 1] = cmap[aByte].g; | |
800 | ptr[poffset + 2] = cmap[aByte].b; | |
801 | column++; | |
802 | } | |
05813ada | 803 | if ( absolute & 0x01 ) |
e65ed37a RR |
804 | aByte = stream.GetC(); |
805 | } | |
806 | } | |
807 | else | |
808 | { | |
05813ada | 809 | for ( int l = 0; l < first && column < width; l++ ) |
e65ed37a RR |
810 | { |
811 | ptr[poffset ] = cmap[aByte].r; | |
812 | ptr[poffset + 1] = cmap[aByte].g; | |
813 | ptr[poffset + 2] = cmap[aByte].b; | |
814 | column++; | |
815 | linepos++; | |
816 | } | |
817 | } | |
818 | } | |
819 | else | |
820 | { | |
821 | ptr[poffset ] = cmap[aByte].r; | |
822 | ptr[poffset + 1] = cmap[aByte].g; | |
823 | ptr[poffset + 2] = cmap[aByte].b; | |
824 | column++; | |
953704c1 | 825 | // linepos += size; seems to be wrong, RR |
e65ed37a RR |
826 | } |
827 | } | |
2b5f62a0 VZ |
828 | } |
829 | else if ( bpp == 24 ) | |
830 | { | |
831 | stream.Read(bbuf, 3); | |
832 | linepos += 3; | |
833 | ptr[poffset ] = (unsigned char)bbuf[2]; | |
834 | ptr[poffset + 1] = (unsigned char)bbuf[1]; | |
835 | ptr[poffset + 2] = (unsigned char)bbuf[0]; | |
836 | column++; | |
837 | } | |
838 | else if ( bpp == 16 ) | |
839 | { | |
840 | unsigned char temp; | |
841 | stream.Read(&aWord, 2); | |
842 | aWord = wxUINT16_SWAP_ON_BE(aWord); | |
843 | linepos += 2; | |
844 | /* use the masks and calculated amonut of shift | |
9042a017 VZ |
845 | to retrieve the color data out of the word. Then |
846 | shift it left by (8 - number of bits) such that | |
847 | the image has the proper dynamic range */ | |
9a431f4d | 848 | temp = (unsigned char)((aWord & rmask) >> rshift << (8-rbits)); |
2b5f62a0 | 849 | ptr[poffset] = temp; |
9a431f4d | 850 | temp = (unsigned char)((aWord & gmask) >> gshift << (8-gbits)); |
2b5f62a0 | 851 | ptr[poffset + 1] = temp; |
9a431f4d | 852 | temp = (unsigned char)((aWord & bmask) >> bshift << (8-bbits)); |
2b5f62a0 VZ |
853 | ptr[poffset + 2] = temp; |
854 | column++; | |
855 | } | |
856 | else | |
857 | { | |
858 | unsigned char temp; | |
859 | stream.Read(&aDword, 4); | |
860 | aDword = wxINT32_SWAP_ON_BE(aDword); | |
861 | linepos += 4; | |
9a431f4d | 862 | temp = (unsigned char)((aDword & rmask) >> rshift); |
2b5f62a0 | 863 | ptr[poffset] = temp; |
9a431f4d | 864 | temp = (unsigned char)((aDword & gmask) >> gshift); |
2b5f62a0 | 865 | ptr[poffset + 1] = temp; |
9a431f4d | 866 | temp = (unsigned char)((aDword & bmask) >> bshift); |
2b5f62a0 | 867 | ptr[poffset + 2] = temp; |
55a35cb0 VZ |
868 | if ( alpha ) |
869 | { | |
870 | temp = (unsigned char)((aDword & amask) >> ashift); | |
871 | alpha[line * width + column] = temp; | |
872 | } | |
2b5f62a0 VZ |
873 | column++; |
874 | } | |
875 | } | |
876 | while ( (linepos < linesize) && (comp != 1) && (comp != 2) ) | |
877 | { | |
878 | stream.Read(&aByte, 1); | |
879 | linepos += 1; | |
880 | if ( !stream ) | |
881 | break; | |
882 | } | |
883 | } | |
884 | ||
7beb59f3 | 885 | image->SetMask(false); |
2b5f62a0 VZ |
886 | |
887 | const wxStreamError err = stream.GetLastError(); | |
888 | return err == wxSTREAM_NO_ERROR || err == wxSTREAM_EOF; | |
52b64b0a RR |
889 | } |
890 | ||
574c939e | 891 | bool wxBMPHandler::LoadDib(wxImage *image, wxInputStream& stream, |
05813ada | 892 | bool verbose, bool IsBmp) |
52b64b0a | 893 | { |
52b64b0a RR |
894 | wxUint16 aWord; |
895 | wxInt32 dbuf[4]; | |
896 | wxInt8 bbuf[4]; | |
52b64b0a | 897 | |
30984dea | 898 | wxFileOffset offset = 0; // keep gcc quiet |
52b64b0a RR |
899 | if ( IsBmp ) |
900 | { | |
901 | // read the header off the .BMP format file | |
902 | ||
903 | offset = stream.TellI(); | |
67c20e13 VZ |
904 | if (offset == wxInvalidOffset) |
905 | offset = 0; | |
52b64b0a | 906 | |
05813ada VS |
907 | stream.Read(bbuf, 2); |
908 | stream.Read(dbuf, 16); | |
52b64b0a RR |
909 | } |
910 | else | |
911 | { | |
05813ada | 912 | stream.Read(dbuf, 4); |
52b64b0a RR |
913 | } |
914 | #if 0 // unused | |
05813ada | 915 | wxInt32 size = wxINT32_SWAP_ON_BE(dbuf[0]); |
52b64b0a | 916 | #endif |
05813ada | 917 | offset = offset + wxINT32_SWAP_ON_BE(dbuf[2]); |
52b64b0a RR |
918 | |
919 | stream.Read(dbuf, 4 * 2); | |
ecae80b2 WS |
920 | int width = wxINT32_SWAP_ON_BE((int)dbuf[0]); |
921 | int height = wxINT32_SWAP_ON_BE((int)dbuf[1]); | |
05813ada | 922 | if ( !IsBmp)height = height / 2; // for icons divide by 2 |
574c939e | 923 | |
05813ada | 924 | if ( width > 32767 ) |
52b64b0a RR |
925 | { |
926 | if (verbose) | |
927 | wxLogError( _("DIB Header: Image width > 32767 pixels for file.") ); | |
7beb59f3 | 928 | return false; |
52b64b0a | 929 | } |
05813ada | 930 | if ( height > 32767 ) |
52b64b0a RR |
931 | { |
932 | if (verbose) | |
933 | wxLogError( _("DIB Header: Image height > 32767 pixels for file.") ); | |
7beb59f3 | 934 | return false; |
52b64b0a RR |
935 | } |
936 | ||
05813ada | 937 | stream.Read(&aWord, 2); |
52b64b0a RR |
938 | /* |
939 | TODO | |
940 | int planes = (int)wxUINT16_SWAP_ON_BE( aWord ); | |
941 | */ | |
05813ada | 942 | stream.Read(&aWord, 2); |
ecae80b2 | 943 | int bpp = wxUINT16_SWAP_ON_BE((int)aWord); |
05813ada | 944 | if ( bpp != 1 && bpp != 4 && bpp != 8 && bpp != 16 && bpp != 24 && bpp != 32 ) |
52b64b0a RR |
945 | { |
946 | if (verbose) | |
947 | wxLogError( _("DIB Header: Unknown bitdepth in file.") ); | |
7beb59f3 | 948 | return false; |
52b64b0a RR |
949 | } |
950 | ||
05813ada | 951 | stream.Read(dbuf, 4 * 4); |
ecae80b2 | 952 | int comp = wxINT32_SWAP_ON_BE((int)dbuf[0]); |
574c939e | 953 | if ( comp != BI_RGB && comp != BI_RLE4 && comp != BI_RLE8 && |
05813ada | 954 | comp != BI_BITFIELDS ) |
52b64b0a RR |
955 | { |
956 | if (verbose) | |
957 | wxLogError( _("DIB Header: Unknown encoding in file.") ); | |
7beb59f3 | 958 | return false; |
52b64b0a RR |
959 | } |
960 | ||
05813ada | 961 | stream.Read(dbuf, 4 * 2); |
37ba70a5 | 962 | |
ecae80b2 | 963 | int ncolors = wxINT32_SWAP_ON_BE( (int)dbuf[0] ); |
52b64b0a RR |
964 | if (ncolors == 0) |
965 | ncolors = 1 << bpp; | |
966 | /* some more sanity checks */ | |
967 | if (((comp == BI_RLE4) && (bpp != 4)) || | |
968 | ((comp == BI_RLE8) && (bpp != 8)) || | |
969 | ((comp == BI_BITFIELDS) && (bpp != 16 && bpp != 32))) | |
970 | { | |
971 | if (verbose) | |
972 | wxLogError( _("DIB Header: Encoding doesn't match bitdepth.") ); | |
7beb59f3 | 973 | return false; |
52b64b0a RR |
974 | } |
975 | ||
976 | //read DIB; this is the BMP image or the XOR part of an icon image | |
05813ada | 977 | if ( !DoLoadDib(image, width, height, bpp, ncolors, comp, offset, stream, |
7beb59f3 | 978 | verbose, IsBmp, true) ) |
52b64b0a RR |
979 | { |
980 | if (verbose) | |
eab77b59 | 981 | wxLogError( _("Error in reading image DIB.") ); |
7beb59f3 | 982 | return false; |
52b64b0a RR |
983 | } |
984 | ||
985 | if ( !IsBmp ) | |
986 | { | |
987 | //read Icon mask which is monochrome | |
988 | //there is no palette, so we will create one | |
1f5b2017 | 989 | wxImage mask; |
05813ada | 990 | if ( !DoLoadDib(&mask, width, height, 1, 2, BI_RGB, offset, stream, |
7beb59f3 | 991 | verbose, IsBmp, false) ) |
52b64b0a RR |
992 | { |
993 | if (verbose) | |
994 | wxLogError( _("ICO: Error in reading mask DIB.") ); | |
7beb59f3 | 995 | return false; |
52b64b0a | 996 | } |
1f5b2017 | 997 | image->SetMaskFromImage(mask, 255, 255, 255); |
52b64b0a RR |
998 | |
999 | } | |
05813ada | 1000 | |
37ba70a5 VZ |
1001 | // the resolution in the bitmap header is in meters, convert to centimeters |
1002 | image->SetOption(wxIMAGE_OPTION_RESOLUTIONUNIT, wxIMAGE_RESOLUTION_CM); | |
1003 | image->SetOption(wxIMAGE_OPTION_RESOLUTIONX, dbuf[2]/100); | |
1004 | image->SetOption(wxIMAGE_OPTION_RESOLUTIONY, dbuf[3]/100); | |
1005 | ||
7beb59f3 | 1006 | return true; |
e65ed37a RR |
1007 | } |
1008 | ||
574c939e | 1009 | bool wxBMPHandler::LoadFile(wxImage *image, wxInputStream& stream, |
05813ada VS |
1010 | bool verbose, int WXUNUSED(index)) |
1011 | { | |
1012 | // Read a single DIB fom the file: | |
7beb59f3 | 1013 | return LoadDib(image, stream, verbose, true/*isBmp*/); |
05813ada | 1014 | } |
52b64b0a | 1015 | |
05813ada | 1016 | bool wxBMPHandler::DoCanRead(wxInputStream& stream) |
52b64b0a | 1017 | { |
05813ada VS |
1018 | unsigned char hdr[2]; |
1019 | ||
79fa2374 | 1020 | if ( !stream.Read(hdr, WXSIZEOF(hdr)) ) |
7beb59f3 | 1021 | return false; |
79fa2374 | 1022 | |
79fa2374 VZ |
1023 | // do we have the BMP file signature? |
1024 | return hdr[0] == 'B' && hdr[1] == 'M'; | |
52b64b0a RR |
1025 | } |
1026 | ||
e30285ab VZ |
1027 | #endif // wxUSE_STREAMS |
1028 | ||
52b64b0a | 1029 | |
658974ae | 1030 | #if wxUSE_ICO_CUR |
05813ada VS |
1031 | //----------------------------------------------------------------------------- |
1032 | // wxICOHandler | |
1033 | //----------------------------------------------------------------------------- | |
1034 | ||
1035 | IMPLEMENT_DYNAMIC_CLASS(wxICOHandler, wxBMPHandler) | |
1036 | ||
e30285ab VZ |
1037 | #if wxUSE_STREAMS |
1038 | ||
05813ada VS |
1039 | struct ICONDIRENTRY |
1040 | { | |
1041 | wxUint8 bWidth; // Width of the image | |
1042 | wxUint8 bHeight; // Height of the image (times 2) | |
1043 | wxUint8 bColorCount; // Number of colors in image (0 if >=8bpp) | |
1044 | wxUint8 bReserved; // Reserved | |
1045 | ||
1046 | // these two are different in icons and cursors: | |
1047 | // icon or cursor | |
1048 | wxUint16 wPlanes; // Color Planes or XHotSpot | |
1049 | wxUint16 wBitCount; // Bits per pixel or YHotSpot | |
1050 | ||
1051 | wxUint32 dwBytesInRes; // how many bytes in this resource? | |
1052 | wxUint32 dwImageOffset; // where in the file is this image | |
1053 | }; | |
1054 | ||
1055 | struct ICONDIR | |
1056 | { | |
1057 | wxUint16 idReserved; // Reserved | |
1058 | wxUint16 idType; // resource type (1 for icons, 2 for cursors) | |
1059 | wxUint16 idCount; // how many images? | |
1060 | }; | |
1061 | ||
1062 | ||
1063 | bool wxICOHandler::SaveFile(wxImage *image, | |
1064 | wxOutputStream& stream, | |
1065 | bool verbose) | |
1066 | ||
52b64b0a | 1067 | { |
05813ada VS |
1068 | //sanity check; icon must be less than 127 pixels high and 255 wide |
1069 | if ( image->GetHeight () > 127 ) | |
1070 | { | |
1071 | if ( verbose ) | |
1072 | wxLogError(_("ICO: Image too tall for an icon.")); | |
7beb59f3 | 1073 | return false; |
05813ada VS |
1074 | } |
1075 | if ( image->GetWidth () > 255 ) | |
1076 | { | |
1077 | if ( verbose ) | |
1078 | wxLogError(_("ICO: Image too wide for an icon.")); | |
7beb59f3 | 1079 | return false; |
05813ada VS |
1080 | } |
1081 | ||
1229a1e7 | 1082 | const int images = 1; // only generate one image |
05813ada | 1083 | |
574c939e | 1084 | // VS: This is a hack of sort - since ICO and CUR files are almost |
05813ada VS |
1085 | // identical, we have all the meat in wxICOHandler and check for |
1086 | // the actual (handler) type when the code has to distinguish between | |
1087 | // the two formats | |
1088 | int type = (this->GetType() == wxBITMAP_TYPE_CUR) ? 2 : 1; | |
1089 | ||
1090 | // write a header, (ICONDIR) | |
1091 | // Calculate the header size | |
1092 | wxUint32 offset = 3 * sizeof(wxUint16); | |
1093 | ||
1094 | ICONDIR IconDir; | |
1095 | IconDir.idReserved = 0; | |
ecae80b2 WS |
1096 | IconDir.idType = wxUINT16_SWAP_ON_BE((wxUint16)type); |
1097 | IconDir.idCount = wxUINT16_SWAP_ON_BE((wxUint16)images); | |
05813ada VS |
1098 | stream.Write(&IconDir.idReserved, sizeof(IconDir.idReserved)); |
1099 | stream.Write(&IconDir.idType, sizeof(IconDir.idType)); | |
1100 | stream.Write(&IconDir.idCount, sizeof(IconDir.idCount)); | |
1101 | if ( !stream.IsOk() ) | |
1102 | { | |
1103 | if ( verbose ) | |
1104 | wxLogError(_("ICO: Error writing the image file!")); | |
7beb59f3 | 1105 | return false; |
05813ada VS |
1106 | } |
1107 | ||
1108 | // for each iamage write a description ICONDIRENTRY: | |
1109 | ICONDIRENTRY icondirentry; | |
1229a1e7 | 1110 | for (int img = 0; img < images; img++) |
05813ada VS |
1111 | { |
1112 | wxImage mask; | |
1113 | ||
1114 | if ( image->HasMask() ) | |
574c939e | 1115 | { |
05813ada VS |
1116 | // make another image with black/white: |
1117 | mask = image->ConvertToMono (image->GetMaskRed(), image->GetMaskGreen(), image->GetMaskBlue() ); | |
1118 | ||
1119 | // now we need to change the masked regions to black: | |
1120 | unsigned char r = image->GetMaskRed(); | |
1121 | unsigned char g = image->GetMaskGreen(); | |
1122 | unsigned char b = image->GetMaskBlue(); | |
1123 | if ( (r != 0) || (g != 0) || (b != 0) ) | |
1124 | { | |
1125 | // Go round and apply black to the masked bits: | |
1126 | int i, j; | |
1127 | for (i = 0; i < mask.GetWidth(); i++) | |
1128 | { | |
1129 | for (j = 0; j < mask.GetHeight(); j++) | |
1130 | { | |
574c939e KB |
1131 | if ((r == mask.GetRed(i, j)) && |
1132 | (g == mask.GetGreen(i, j))&& | |
05813ada VS |
1133 | (b == mask.GetBlue(i, j)) ) |
1134 | image->SetRGB(i, j, 0, 0, 0 ); | |
1135 | } | |
1136 | } | |
1137 | } | |
1138 | } | |
1139 | else | |
1140 | { | |
1141 | // just make a black mask all over: | |
1142 | mask = image->Copy(); | |
1229a1e7 WS |
1143 | int i, j; |
1144 | for (i = 0; i < mask.GetWidth(); i++) | |
05813ada | 1145 | for (j = 0; j < mask.GetHeight(); j++) |
1229a1e7 | 1146 | mask.SetRGB(i, j, 0, 0, 0 ); |
05813ada VS |
1147 | } |
1148 | // Set the formats for image and mask | |
1149 | // (Windows never saves with more than 8 colors): | |
fd94e8aa | 1150 | image->SetOption(wxIMAGE_OPTION_BMP_FORMAT, wxBMP_8BPP); |
05813ada VS |
1151 | |
1152 | // monochome bitmap: | |
fd94e8aa | 1153 | mask.SetOption(wxIMAGE_OPTION_BMP_FORMAT, wxBMP_1BPP_BW); |
7beb59f3 WS |
1154 | bool IsBmp = false; |
1155 | bool IsMask = false; | |
05813ada VS |
1156 | |
1157 | //calculate size and offset of image and mask | |
1158 | wxCountingOutputStream cStream; | |
5cb598ae | 1159 | bool bResult = SaveDib(image, cStream, verbose, IsBmp, IsMask); |
05813ada VS |
1160 | if ( !bResult ) |
1161 | { | |
1162 | if ( verbose ) | |
1163 | wxLogError(_("ICO: Error writing the image file!")); | |
7beb59f3 | 1164 | return false; |
05813ada | 1165 | } |
7beb59f3 | 1166 | IsMask = true; |
05813ada VS |
1167 | |
1168 | bResult = SaveDib(&mask, cStream, verbose, IsBmp, IsMask); | |
1169 | if ( !bResult ) | |
1170 | { | |
1171 | if ( verbose ) | |
1172 | wxLogError(_("ICO: Error writing the image file!")); | |
7beb59f3 | 1173 | return false; |
05813ada VS |
1174 | } |
1175 | wxUint32 Size = cStream.GetSize(); | |
1176 | ||
b7cacb43 | 1177 | // wxCountingOutputStream::IsOk() always returns true for now and this |
05813ada VS |
1178 | // "if" provokes VC++ warnings in optimized build |
1179 | #if 0 | |
1180 | if ( !cStream.Ok() ) | |
1181 | { | |
1182 | if ( verbose ) | |
1183 | wxLogError(_("ICO: Error writing the image file!")); | |
7beb59f3 | 1184 | return false; |
05813ada VS |
1185 | } |
1186 | #endif // 0 | |
1187 | ||
1188 | offset = offset + sizeof(ICONDIRENTRY); | |
1189 | ||
f12a90c7 DE |
1190 | icondirentry.bWidth = (wxUint8)image->GetWidth(); |
1191 | icondirentry.bHeight = (wxUint8)(2 * image->GetHeight()); | |
05813ada VS |
1192 | icondirentry.bColorCount = 0; |
1193 | icondirentry.bReserved = 0; | |
1194 | icondirentry.wPlanes = wxUINT16_SWAP_ON_BE(1); | |
1195 | icondirentry.wBitCount = wxUINT16_SWAP_ON_BE(wxBMP_8BPP); | |
1196 | if ( type == 2 /*CUR*/) | |
1197 | { | |
fd94e8aa VS |
1198 | int hx = image->HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X) ? |
1199 | image->GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X) : | |
05813ada | 1200 | image->GetWidth() / 2; |
fd94e8aa VS |
1201 | int hy = image->HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y) ? |
1202 | image->GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y) : | |
05813ada VS |
1203 | image->GetHeight() / 2; |
1204 | ||
1205 | // actually write the values of the hot spot here: | |
1206 | icondirentry.wPlanes = wxUINT16_SWAP_ON_BE((wxUint16)hx); | |
1207 | icondirentry.wBitCount = wxUINT16_SWAP_ON_BE((wxUint16)hy); | |
1208 | } | |
1209 | icondirentry.dwBytesInRes = wxUINT32_SWAP_ON_BE(Size); | |
1210 | icondirentry.dwImageOffset = wxUINT32_SWAP_ON_BE(offset); | |
1211 | ||
1212 | // increase size to allow for the data written: | |
1213 | offset += Size; | |
1214 | ||
1215 | // write to stream: | |
1216 | stream.Write(&icondirentry.bWidth, sizeof(icondirentry.bWidth)); | |
1217 | stream.Write(&icondirentry.bHeight, sizeof(icondirentry.bHeight)); | |
1218 | stream.Write(&icondirentry.bColorCount, sizeof(icondirentry.bColorCount)); | |
1219 | stream.Write(&icondirentry.bReserved, sizeof(icondirentry.bReserved)); | |
1220 | stream.Write(&icondirentry.wPlanes, sizeof(icondirentry.wPlanes)); | |
1221 | stream.Write(&icondirentry.wBitCount, sizeof(icondirentry.wBitCount)); | |
1222 | stream.Write(&icondirentry.dwBytesInRes, sizeof(icondirentry.dwBytesInRes)); | |
1223 | stream.Write(&icondirentry.dwImageOffset, sizeof(icondirentry.dwImageOffset)); | |
1224 | if ( !stream.IsOk() ) | |
1225 | { | |
1226 | if ( verbose ) | |
1227 | wxLogError(_("ICO: Error writing the image file!")); | |
7beb59f3 | 1228 | return false; |
05813ada VS |
1229 | } |
1230 | ||
1231 | // actually save it: | |
7beb59f3 | 1232 | IsMask = false; |
05813ada VS |
1233 | bResult = SaveDib(image, stream, verbose, IsBmp, IsMask); |
1234 | if ( !bResult ) | |
1235 | { | |
1236 | if ( verbose ) | |
1237 | wxLogError(_("ICO: Error writing the image file!")); | |
7beb59f3 | 1238 | return false; |
05813ada | 1239 | } |
7beb59f3 | 1240 | IsMask = true; |
05813ada VS |
1241 | |
1242 | bResult = SaveDib(&mask, stream, verbose, IsBmp, IsMask); | |
1243 | if ( !bResult ) | |
1244 | { | |
1245 | if ( verbose ) | |
1246 | wxLogError(_("ICO: Error writing the image file!")); | |
7beb59f3 | 1247 | return false; |
05813ada VS |
1248 | } |
1249 | ||
1250 | } // end of for loop | |
1251 | ||
7beb59f3 | 1252 | return true; |
05813ada VS |
1253 | } |
1254 | ||
574c939e | 1255 | bool wxICOHandler::LoadFile(wxImage *image, wxInputStream& stream, |
05813ada | 1256 | bool verbose, int index) |
658974ae VS |
1257 | { |
1258 | stream.SeekI(0); | |
1259 | return DoLoadFile(image, stream, verbose, index); | |
1260 | } | |
1261 | ||
574c939e | 1262 | bool wxICOHandler::DoLoadFile(wxImage *image, wxInputStream& stream, |
5c235ea0 | 1263 | bool WXUNUSED(verbose), int index) |
05813ada | 1264 | { |
5cb598ae WS |
1265 | bool bResult wxDUMMY_INITIALIZE(false); |
1266 | bool IsBmp = false; | |
52b64b0a | 1267 | |
05813ada | 1268 | ICONDIR IconDir; |
658974ae | 1269 | |
1b941f2d | 1270 | wxFileOffset iPos = stream.TellI(); |
05813ada VS |
1271 | stream.Read(&IconDir, sizeof(IconDir)); |
1272 | wxUint16 nIcons = wxUINT16_SWAP_ON_BE(IconDir.idCount); | |
1273 | // nType is 1 for Icons, 2 for Cursors: | |
1274 | wxUint16 nType = wxUINT16_SWAP_ON_BE(IconDir.idType); | |
574c939e | 1275 | |
05813ada VS |
1276 | // loop round the icons and choose the best one: |
1277 | ICONDIRENTRY *pIconDirEntry = new ICONDIRENTRY[nIcons]; | |
1278 | ICONDIRENTRY *pCurrentEntry = pIconDirEntry; | |
1279 | int wMax = 0; | |
1280 | int colmax = 0; | |
1281 | int iSel = wxNOT_FOUND; | |
1282 | ||
1283 | for (int i = 0; i < nIcons; i++ ) | |
52b64b0a RR |
1284 | { |
1285 | stream.Read(pCurrentEntry, sizeof(ICONDIRENTRY)); | |
05813ada VS |
1286 | // bHeight and bColorCount are wxUint8 |
1287 | if ( pCurrentEntry->bWidth >= wMax ) | |
52b64b0a | 1288 | { |
05813ada VS |
1289 | // see if we have more colors, ==0 indicates > 8bpp: |
1290 | if ( pCurrentEntry->bColorCount == 0 ) | |
1291 | pCurrentEntry->bColorCount = 255; | |
1292 | if ( pCurrentEntry->bColorCount >= colmax ) | |
574c939e | 1293 | { |
05813ada VS |
1294 | iSel = i; |
1295 | wMax = pCurrentEntry->bWidth; | |
1296 | colmax = pCurrentEntry->bColorCount; | |
52b64b0a RR |
1297 | } |
1298 | } | |
05813ada VS |
1299 | pCurrentEntry++; |
1300 | } | |
574c939e | 1301 | |
05813ada VS |
1302 | if ( index != -1 ) |
1303 | { | |
1304 | // VS: Note that we *have* to run the loop above even if index != -1, because | |
1305 | // it reads ICONDIRENTRies. | |
1306 | iSel = index; | |
52b64b0a | 1307 | } |
574c939e | 1308 | |
05813ada | 1309 | if ( iSel == wxNOT_FOUND || iSel < 0 || iSel >= nIcons ) |
52b64b0a | 1310 | { |
05813ada | 1311 | wxLogError(_("ICO: Invalid icon index.")); |
5cb598ae | 1312 | bResult = false; |
52b64b0a RR |
1313 | } |
1314 | else | |
1315 | { | |
05813ada VS |
1316 | // seek to selected icon: |
1317 | pCurrentEntry = pIconDirEntry + iSel; | |
658974ae | 1318 | stream.SeekI(iPos + wxUINT32_SWAP_ON_BE(pCurrentEntry->dwImageOffset), wxFromStart); |
7beb59f3 | 1319 | bResult = LoadDib(image, stream, true, IsBmp); |
658974ae VS |
1320 | bool bIsCursorType = (this->GetType() == wxBITMAP_TYPE_CUR) || (this->GetType() == wxBITMAP_TYPE_ANI); |
1321 | if ( bResult && bIsCursorType && nType == 2 ) | |
05813ada VS |
1322 | { |
1323 | // it is a cursor, so let's set the hotspot: | |
fd94e8aa VS |
1324 | image->SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X, wxUINT16_SWAP_ON_BE(pCurrentEntry->wPlanes)); |
1325 | image->SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y, wxUINT16_SWAP_ON_BE(pCurrentEntry->wBitCount)); | |
05813ada | 1326 | } |
52b64b0a | 1327 | } |
05813ada VS |
1328 | delete[] pIconDirEntry; |
1329 | return bResult; | |
52b64b0a RR |
1330 | } |
1331 | ||
649d13e8 | 1332 | int wxICOHandler::GetImageCount(wxInputStream& stream) |
0828c087 | 1333 | { |
05813ada | 1334 | ICONDIR IconDir; |
1b941f2d | 1335 | wxFileOffset iPos = stream.TellI(); |
05813ada VS |
1336 | stream.SeekI(0); |
1337 | stream.Read(&IconDir, sizeof(IconDir)); | |
1338 | wxUint16 nIcons = wxUINT16_SWAP_ON_BE(IconDir.idCount); | |
658974ae | 1339 | stream.SeekI(iPos); |
05813ada | 1340 | return (int)nIcons; |
0828c087 VS |
1341 | } |
1342 | ||
05813ada | 1343 | bool wxICOHandler::DoCanRead(wxInputStream& stream) |
52b64b0a | 1344 | { |
2b5f62a0 | 1345 | stream.SeekI(0); |
52b64b0a | 1346 | unsigned char hdr[4]; |
39d16996 | 1347 | if ( !stream.Read(hdr, WXSIZEOF(hdr)) ) |
7beb59f3 | 1348 | return false; |
39d16996 VZ |
1349 | |
1350 | // hdr[2] is one for an icon and two for a cursor | |
1351 | return hdr[0] == '\0' && hdr[1] == '\0' && hdr[2] == '\1' && hdr[3] == '\0'; | |
52b64b0a RR |
1352 | } |
1353 | ||
e30285ab | 1354 | #endif // wxUSE_STREAMS |
e65ed37a | 1355 | |
05813ada VS |
1356 | |
1357 | //----------------------------------------------------------------------------- | |
1358 | // wxCURHandler | |
1359 | //----------------------------------------------------------------------------- | |
1360 | ||
1361 | IMPLEMENT_DYNAMIC_CLASS(wxCURHandler, wxICOHandler) | |
1362 | ||
e30285ab VZ |
1363 | #if wxUSE_STREAMS |
1364 | ||
05813ada VS |
1365 | bool wxCURHandler::DoCanRead(wxInputStream& stream) |
1366 | { | |
8dcfaa40 | 1367 | stream.SeekI(0); |
05813ada | 1368 | unsigned char hdr[4]; |
39d16996 | 1369 | if ( !stream.Read(hdr, WXSIZEOF(hdr)) ) |
7beb59f3 | 1370 | return false; |
39d16996 VZ |
1371 | |
1372 | // hdr[2] is one for an icon and two for a cursor | |
1373 | return hdr[0] == '\0' && hdr[1] == '\0' && hdr[2] == '\2' && hdr[3] == '\0'; | |
05813ada VS |
1374 | } |
1375 | ||
e30285ab VZ |
1376 | #endif // wxUSE_STREAMS |
1377 | ||
658974ae VS |
1378 | //----------------------------------------------------------------------------- |
1379 | // wxANIHandler | |
1380 | //----------------------------------------------------------------------------- | |
1381 | ||
1382 | IMPLEMENT_DYNAMIC_CLASS(wxANIHandler, wxCURHandler) | |
1383 | ||
e30285ab VZ |
1384 | #if wxUSE_STREAMS |
1385 | ||
574c939e | 1386 | bool wxANIHandler::LoadFile(wxImage *image, wxInputStream& stream, |
72045d57 | 1387 | bool WXUNUSED(verbose), int index) |
658974ae | 1388 | { |
72045d57 VZ |
1389 | wxANIDecoder decoder; |
1390 | if (!decoder.Load(stream)) | |
7beb59f3 | 1391 | return false; |
658974ae | 1392 | |
72045d57 | 1393 | return decoder.ConvertToImage(index != -1 ? (size_t)index : 0, image); |
658974ae VS |
1394 | } |
1395 | ||
1396 | bool wxANIHandler::DoCanRead(wxInputStream& stream) | |
1397 | { | |
72045d57 VZ |
1398 | wxANIDecoder decod; |
1399 | return decod.CanRead(stream); | |
658974ae VS |
1400 | } |
1401 | ||
1402 | int wxANIHandler::GetImageCount(wxInputStream& stream) | |
1403 | { | |
72045d57 VZ |
1404 | wxANIDecoder decoder; |
1405 | if (!decoder.Load(stream)) | |
658974ae VS |
1406 | return wxNOT_FOUND; |
1407 | ||
72045d57 | 1408 | return decoder.GetFrameCount(); |
658974ae VS |
1409 | } |
1410 | ||
e30285ab VZ |
1411 | #endif // wxUSE_STREAMS |
1412 | ||
658974ae VS |
1413 | #endif // wxUSE_ICO_CUR |
1414 | ||
e30285ab | 1415 | #endif // wxUSE_IMAGE |