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