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