]>
Commit | Line | Data |
---|---|---|
01111366 | 1 | ///////////////////////////////////////////////////////////////////////////// |
38d4b1e4 | 2 | // Name: src/common/image.cpp |
01111366 RR |
3 | // Purpose: wxImage |
4 | // Author: Robert Roebling | |
5 | // RCS-ID: $Id$ | |
6 | // Copyright: (c) Robert Roebling | |
65571936 | 7 | // Licence: wxWindows licence |
01111366 RR |
8 | ///////////////////////////////////////////////////////////////////////////// |
9 | ||
0b4f9ee3 UU |
10 | // For compilers that support precompilation, includes "wx.h". |
11 | #include "wx/wxprec.h" | |
12 | ||
13 | #ifdef __BORLANDC__ | |
edccf428 | 14 | #pragma hdrstop |
0b4f9ee3 UU |
15 | #endif |
16 | ||
c96ea657 VS |
17 | #include "wx/defs.h" |
18 | ||
19 | #if wxUSE_IMAGE | |
20 | ||
01111366 | 21 | #include "wx/image.h" |
99c67c77 | 22 | #include "wx/bitmap.h" |
01111366 RR |
23 | #include "wx/debug.h" |
24 | #include "wx/log.h" | |
f6fcbb63 | 25 | #include "wx/app.h" |
dc86cb34 | 26 | #include "wx/filefn.h" |
3d05544e | 27 | #include "wx/wfstream.h" |
b75867a6 | 28 | #include "wx/intl.h" |
a91b47e8 | 29 | #include "wx/module.h" |
ed39ff57 | 30 | #include "wx/hash.h" |
d0ce3e52 | 31 | #include "wx/utils.h" |
b713f891 | 32 | #include "wx/math.h" |
01111366 | 33 | |
cad61c3e JS |
34 | #if wxUSE_XPM |
35 | #include "wx/xpmdecod.h" | |
36 | #endif | |
37 | ||
58a8ab88 JS |
38 | // For memcpy |
39 | #include <string.h> | |
40 | ||
01111366 RR |
41 | //----------------------------------------------------------------------------- |
42 | // wxImage | |
43 | //----------------------------------------------------------------------------- | |
44 | ||
45 | class wxImageRefData: public wxObjectRefData | |
46 | { | |
fd0eed64 | 47 | public: |
edccf428 | 48 | wxImageRefData(); |
487659e0 | 49 | virtual ~wxImageRefData(); |
c7abc967 | 50 | |
dbda9e86 JS |
51 | int m_width; |
52 | int m_height; | |
53 | unsigned char *m_data; | |
487659e0 | 54 | |
dbda9e86 JS |
55 | bool m_hasMask; |
56 | unsigned char m_maskRed,m_maskGreen,m_maskBlue; | |
487659e0 VZ |
57 | |
58 | // alpha channel data, may be NULL for the formats without alpha support | |
59 | unsigned char *m_alpha; | |
60 | ||
dbda9e86 | 61 | bool m_ok; |
d2502f14 VZ |
62 | |
63 | // if true, m_data is pointer to static data and shouldn't be freed | |
f6bcfd97 | 64 | bool m_static; |
487659e0 | 65 | |
d2502f14 VZ |
66 | // same as m_static but for m_alpha |
67 | bool m_staticAlpha; | |
68 | ||
d275c7eb | 69 | #if wxUSE_PALETTE |
5e5437e0 | 70 | wxPalette m_palette; |
d275c7eb | 71 | #endif // wxUSE_PALETTE |
487659e0 | 72 | |
5e5437e0 JS |
73 | wxArrayString m_optionNames; |
74 | wxArrayString m_optionValues; | |
22f3361e VZ |
75 | |
76 | DECLARE_NO_COPY_CLASS(wxImageRefData) | |
01111366 RR |
77 | }; |
78 | ||
edccf428 | 79 | wxImageRefData::wxImageRefData() |
01111366 | 80 | { |
fd0eed64 RR |
81 | m_width = 0; |
82 | m_height = 0; | |
487659e0 VZ |
83 | m_data = |
84 | m_alpha = (unsigned char *) NULL; | |
85 | ||
fd0eed64 RR |
86 | m_maskRed = 0; |
87 | m_maskGreen = 0; | |
88 | m_maskBlue = 0; | |
70cd62e9 | 89 | m_hasMask = false; |
487659e0 | 90 | |
70cd62e9 | 91 | m_ok = false; |
d2502f14 VZ |
92 | m_static = |
93 | m_staticAlpha = false; | |
01111366 RR |
94 | } |
95 | ||
edccf428 | 96 | wxImageRefData::~wxImageRefData() |
01111366 | 97 | { |
d2502f14 | 98 | if ( !m_static ) |
58c837a4 | 99 | free( m_data ); |
d2502f14 | 100 | if ( !m_staticAlpha ) |
4ea56379 | 101 | free( m_alpha ); |
01111366 RR |
102 | } |
103 | ||
104 | wxList wxImage::sm_handlers; | |
105 | ||
fec19ea9 VS |
106 | wxImage wxNullImage; |
107 | ||
01111366 RR |
108 | //----------------------------------------------------------------------------- |
109 | ||
110 | #define M_IMGDATA ((wxImageRefData *)m_refData) | |
111 | ||
5e5437e0 | 112 | IMPLEMENT_DYNAMIC_CLASS(wxImage, wxObject) |
01111366 | 113 | |
ff865c13 | 114 | wxImage::wxImage( int width, int height, bool clear ) |
01111366 | 115 | { |
ff865c13 | 116 | Create( width, height, clear ); |
01111366 RR |
117 | } |
118 | ||
f6bcfd97 BP |
119 | wxImage::wxImage( int width, int height, unsigned char* data, bool static_data ) |
120 | { | |
121 | Create( width, height, data, static_data ); | |
122 | } | |
123 | ||
4ea56379 RR |
124 | wxImage::wxImage( int width, int height, unsigned char* data, unsigned char* alpha, bool static_data ) |
125 | { | |
126 | Create( width, height, data, alpha, static_data ); | |
127 | } | |
128 | ||
60d43ad8 | 129 | wxImage::wxImage( const wxString& name, long type, int index ) |
01111366 | 130 | { |
60d43ad8 | 131 | LoadFile( name, type, index ); |
01111366 RR |
132 | } |
133 | ||
60d43ad8 | 134 | wxImage::wxImage( const wxString& name, const wxString& mimetype, int index ) |
9e9ee68e | 135 | { |
60d43ad8 | 136 | LoadFile( name, mimetype, index ); |
9e9ee68e VS |
137 | } |
138 | ||
e02afc7a | 139 | #if wxUSE_STREAMS |
60d43ad8 | 140 | wxImage::wxImage( wxInputStream& stream, long type, int index ) |
3d05544e | 141 | { |
60d43ad8 | 142 | LoadFile( stream, type, index ); |
3d05544e | 143 | } |
9e9ee68e | 144 | |
60d43ad8 | 145 | wxImage::wxImage( wxInputStream& stream, const wxString& mimetype, int index ) |
9e9ee68e | 146 | { |
60d43ad8 | 147 | LoadFile( stream, mimetype, index ); |
9e9ee68e | 148 | } |
e02afc7a | 149 | #endif // wxUSE_STREAMS |
3d05544e | 150 | |
4698648f | 151 | wxImage::wxImage( const wxImage& image ) |
1b0674f7 | 152 | : wxObject() |
4698648f VZ |
153 | { |
154 | Ref(image); | |
01111366 RR |
155 | } |
156 | ||
4698648f VZ |
157 | wxImage::wxImage( const wxImage* image ) |
158 | { | |
159 | if (image) Ref(*image); | |
01111366 RR |
160 | } |
161 | ||
cad61c3e JS |
162 | wxImage::wxImage( const char** xpmData ) |
163 | { | |
164 | Create(xpmData); | |
165 | } | |
166 | ||
167 | wxImage::wxImage( char** xpmData ) | |
168 | { | |
169 | Create((const char**) xpmData); | |
170 | } | |
171 | ||
172 | bool wxImage::Create( const char** xpmData ) | |
173 | { | |
174 | #if wxUSE_XPM | |
175 | UnRef(); | |
e9b64c5e | 176 | |
cad61c3e JS |
177 | wxXPMDecoder decoder; |
178 | (*this) = decoder.ReadData(xpmData); | |
179 | return Ok(); | |
180 | #else | |
181 | return false; | |
182 | #endif | |
183 | } | |
184 | ||
aaa97828 | 185 | bool wxImage::Create( int width, int height, bool clear ) |
01111366 | 186 | { |
aadaf841 GRG |
187 | UnRef(); |
188 | ||
fd0eed64 | 189 | m_refData = new wxImageRefData(); |
c7abc967 | 190 | |
fd0eed64 | 191 | M_IMGDATA->m_data = (unsigned char *) malloc( width*height*3 ); |
aaa97828 | 192 | if (!M_IMGDATA->m_data) |
fd0eed64 RR |
193 | { |
194 | UnRef(); | |
70cd62e9 | 195 | return false; |
fd0eed64 | 196 | } |
aaa97828 VZ |
197 | |
198 | if (clear) | |
199 | memset(M_IMGDATA->m_data, 0, width*height*3); | |
200 | ||
201 | M_IMGDATA->m_width = width; | |
202 | M_IMGDATA->m_height = height; | |
70cd62e9 | 203 | M_IMGDATA->m_ok = true; |
aaa97828 | 204 | |
70cd62e9 | 205 | return true; |
01111366 RR |
206 | } |
207 | ||
aaa97828 | 208 | bool wxImage::Create( int width, int height, unsigned char* data, bool static_data ) |
f6bcfd97 BP |
209 | { |
210 | UnRef(); | |
211 | ||
70cd62e9 | 212 | wxCHECK_MSG( data, false, _T("NULL data in wxImage::Create") ); |
aaa97828 | 213 | |
f6bcfd97 BP |
214 | m_refData = new wxImageRefData(); |
215 | ||
216 | M_IMGDATA->m_data = data; | |
aaa97828 VZ |
217 | M_IMGDATA->m_width = width; |
218 | M_IMGDATA->m_height = height; | |
70cd62e9 | 219 | M_IMGDATA->m_ok = true; |
aaa97828 VZ |
220 | M_IMGDATA->m_static = static_data; |
221 | ||
70cd62e9 | 222 | return true; |
f6bcfd97 BP |
223 | } |
224 | ||
4ea56379 RR |
225 | bool wxImage::Create( int width, int height, unsigned char* data, unsigned char* alpha, bool static_data ) |
226 | { | |
227 | UnRef(); | |
228 | ||
229 | wxCHECK_MSG( data, false, _T("NULL data in wxImage::Create") ); | |
230 | ||
231 | m_refData = new wxImageRefData(); | |
232 | ||
233 | M_IMGDATA->m_data = data; | |
234 | M_IMGDATA->m_alpha = alpha; | |
235 | M_IMGDATA->m_width = width; | |
236 | M_IMGDATA->m_height = height; | |
237 | M_IMGDATA->m_ok = true; | |
238 | M_IMGDATA->m_static = static_data; | |
239 | ||
240 | return true; | |
241 | } | |
242 | ||
01111366 RR |
243 | void wxImage::Destroy() |
244 | { | |
fd0eed64 | 245 | UnRef(); |
01111366 RR |
246 | } |
247 | ||
f6bcfd97 BP |
248 | wxImage wxImage::Copy() const |
249 | { | |
250 | wxImage image; | |
251 | ||
252 | wxCHECK_MSG( Ok(), image, wxT("invalid image") ); | |
253 | ||
ff865c13 | 254 | image.Create( M_IMGDATA->m_width, M_IMGDATA->m_height, false ); |
f6bcfd97 | 255 | |
487659e0 | 256 | unsigned char *data = image.GetData(); |
f6bcfd97 BP |
257 | |
258 | wxCHECK_MSG( data, image, wxT("unable to create image") ); | |
259 | ||
fdbb06ba RR |
260 | image.SetMaskColour( M_IMGDATA->m_maskRed, M_IMGDATA->m_maskGreen, M_IMGDATA->m_maskBlue ); |
261 | image.SetMask( M_IMGDATA->m_hasMask ); | |
f6bcfd97 BP |
262 | |
263 | memcpy( data, GetData(), M_IMGDATA->m_width*M_IMGDATA->m_height*3 ); | |
a1cd9564 | 264 | |
d8692f2b | 265 | wxImageRefData *imgData = (wxImageRefData *)image.m_refData; |
a1cd9564 RR |
266 | |
267 | // also copy the alpha channel | |
268 | if (HasAlpha()) | |
269 | { | |
270 | image.SetAlpha(); | |
271 | unsigned char* alpha = image.GetAlpha(); | |
272 | memcpy( alpha, GetAlpha(), M_IMGDATA->m_width*M_IMGDATA->m_height ); | |
273 | } | |
274 | ||
275 | // also copy the image options | |
d8692f2b VZ |
276 | imgData->m_optionNames = M_IMGDATA->m_optionNames; |
277 | imgData->m_optionValues = M_IMGDATA->m_optionValues; | |
278 | ||
f6bcfd97 BP |
279 | return image; |
280 | } | |
281 | ||
fd9655ad SC |
282 | wxImage wxImage::ShrinkBy( int xFactor , int yFactor ) const |
283 | { | |
284 | if( xFactor == 1 && yFactor == 1 ) | |
285 | return Copy() ; | |
7beb59f3 | 286 | |
fd9655ad SC |
287 | wxImage image; |
288 | ||
289 | wxCHECK_MSG( Ok(), image, wxT("invalid image") ); | |
290 | ||
291 | // can't scale to/from 0 size | |
292 | wxCHECK_MSG( (xFactor > 0) && (yFactor > 0), image, | |
293 | wxT("invalid new image size") ); | |
294 | ||
295 | long old_height = M_IMGDATA->m_height, | |
296 | old_width = M_IMGDATA->m_width; | |
7beb59f3 | 297 | |
fd9655ad SC |
298 | wxCHECK_MSG( (old_height > 0) && (old_width > 0), image, |
299 | wxT("invalid old image size") ); | |
300 | ||
301 | long width = old_width / xFactor ; | |
302 | long height = old_height / yFactor ; | |
303 | ||
ff865c13 | 304 | image.Create( width, height, false ); |
fd9655ad SC |
305 | |
306 | char unsigned *data = image.GetData(); | |
307 | ||
308 | wxCHECK_MSG( data, image, wxT("unable to create image") ); | |
309 | ||
310 | bool hasMask = false ; | |
311 | unsigned char maskRed = 0; | |
312 | unsigned char maskGreen = 0; | |
313 | unsigned char maskBlue =0 ; | |
cd0bbd03 SC |
314 | |
315 | unsigned char *source_data = M_IMGDATA->m_data; | |
316 | unsigned char *target_data = data; | |
317 | unsigned char *source_alpha = 0 ; | |
318 | unsigned char *target_alpha = 0 ; | |
fd9655ad SC |
319 | if (M_IMGDATA->m_hasMask) |
320 | { | |
321 | hasMask = true ; | |
322 | maskRed = M_IMGDATA->m_maskRed; | |
323 | maskGreen = M_IMGDATA->m_maskGreen; | |
324 | maskBlue =M_IMGDATA->m_maskBlue ; | |
7beb59f3 | 325 | |
fd9655ad SC |
326 | image.SetMaskColour( M_IMGDATA->m_maskRed, |
327 | M_IMGDATA->m_maskGreen, | |
328 | M_IMGDATA->m_maskBlue ); | |
329 | } | |
cd0bbd03 SC |
330 | else |
331 | { | |
332 | source_alpha = M_IMGDATA->m_alpha ; | |
333 | if ( source_alpha ) | |
334 | { | |
335 | image.SetAlpha() ; | |
336 | target_alpha = image.GetAlpha() ; | |
337 | } | |
338 | } | |
7beb59f3 | 339 | |
fd9655ad SC |
340 | for (long y = 0; y < height; y++) |
341 | { | |
fd9655ad SC |
342 | for (long x = 0; x < width; x++) |
343 | { | |
344 | unsigned long avgRed = 0 ; | |
345 | unsigned long avgGreen = 0; | |
346 | unsigned long avgBlue = 0; | |
cd0bbd03 | 347 | unsigned long avgAlpha = 0 ; |
fd9655ad SC |
348 | unsigned long counter = 0 ; |
349 | // determine average | |
350 | for ( int y1 = 0 ; y1 < yFactor ; ++y1 ) | |
351 | { | |
352 | long y_offset = (y * yFactor + y1) * old_width; | |
353 | for ( int x1 = 0 ; x1 < xFactor ; ++x1 ) | |
354 | { | |
355 | unsigned char *pixel = source_data + 3 * ( y_offset + x * xFactor + x1 ) ; | |
356 | unsigned char red = pixel[0] ; | |
357 | unsigned char green = pixel[1] ; | |
358 | unsigned char blue = pixel[2] ; | |
cd0bbd03 SC |
359 | unsigned char alpha = 255 ; |
360 | if ( source_alpha ) | |
361 | alpha = *(source_alpha + y_offset + x * xFactor + x1) ; | |
fd9655ad SC |
362 | if ( !hasMask || red != maskRed || green != maskGreen || blue != maskBlue ) |
363 | { | |
cd0bbd03 SC |
364 | if ( alpha > 0 ) |
365 | { | |
366 | avgRed += red ; | |
367 | avgGreen += green ; | |
368 | avgBlue += blue ; | |
369 | } | |
370 | avgAlpha += alpha ; | |
fd9655ad SC |
371 | counter++ ; |
372 | } | |
373 | } | |
374 | } | |
375 | if ( counter == 0 ) | |
376 | { | |
377 | *(target_data++) = M_IMGDATA->m_maskRed ; | |
378 | *(target_data++) = M_IMGDATA->m_maskGreen ; | |
379 | *(target_data++) = M_IMGDATA->m_maskBlue ; | |
380 | } | |
381 | else | |
382 | { | |
cd0bbd03 SC |
383 | if ( source_alpha ) |
384 | *(target_alpha++) = (unsigned char)(avgAlpha / counter ) ; | |
646c4aeb VZ |
385 | *(target_data++) = (unsigned char)(avgRed / counter); |
386 | *(target_data++) = (unsigned char)(avgGreen / counter); | |
387 | *(target_data++) = (unsigned char)(avgBlue / counter); | |
fd9655ad SC |
388 | } |
389 | } | |
390 | } | |
391 | ||
8180d40b | 392 | // In case this is a cursor, make sure the hotspot is scaled accordingly: |
fd9655ad SC |
393 | if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X) ) |
394 | image.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X, | |
395 | (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X))/xFactor); | |
396 | if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y) ) | |
397 | image.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y, | |
398 | (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y))/yFactor); | |
399 | ||
400 | return image; | |
401 | } | |
402 | ||
ce9a75d2 | 403 | wxImage wxImage::Scale( int width, int height ) const |
4bc67cc5 RR |
404 | { |
405 | wxImage image; | |
c7abc967 | 406 | |
223d09f6 | 407 | wxCHECK_MSG( Ok(), image, wxT("invalid image") ); |
c7abc967 | 408 | |
6721fc38 VZ |
409 | // can't scale to/from 0 size |
410 | wxCHECK_MSG( (width > 0) && (height > 0), image, | |
411 | wxT("invalid new image size") ); | |
412 | ||
413 | long old_height = M_IMGDATA->m_height, | |
414 | old_width = M_IMGDATA->m_width; | |
415 | wxCHECK_MSG( (old_height > 0) && (old_width > 0), image, | |
416 | wxT("invalid old image size") ); | |
c7abc967 | 417 | |
fd9655ad SC |
418 | if ( old_width % width == 0 && old_width >= width && |
419 | old_height % height == 0 && old_height >= height ) | |
420 | { | |
421 | return ShrinkBy( old_width / width , old_height / height ) ; | |
422 | } | |
ff865c13 | 423 | image.Create( width, height, false ); |
c7abc967 | 424 | |
487659e0 | 425 | unsigned char *data = image.GetData(); |
c7abc967 | 426 | |
223d09f6 | 427 | wxCHECK_MSG( data, image, wxT("unable to create image") ); |
c7abc967 | 428 | |
8d3b6b8a SC |
429 | unsigned char *source_data = M_IMGDATA->m_data; |
430 | unsigned char *target_data = data; | |
431 | unsigned char *source_alpha = 0 ; | |
432 | unsigned char *target_alpha = 0 ; | |
e9b64c5e | 433 | |
4bc67cc5 | 434 | if (M_IMGDATA->m_hasMask) |
6721fc38 VZ |
435 | { |
436 | image.SetMaskColour( M_IMGDATA->m_maskRed, | |
437 | M_IMGDATA->m_maskGreen, | |
438 | M_IMGDATA->m_maskBlue ); | |
439 | } | |
8d3b6b8a SC |
440 | else |
441 | { | |
442 | source_alpha = M_IMGDATA->m_alpha ; | |
443 | if ( source_alpha ) | |
444 | { | |
445 | image.SetAlpha() ; | |
446 | target_alpha = image.GetAlpha() ; | |
447 | } | |
448 | } | |
c7abc967 | 449 | |
ff865c13 JS |
450 | long x_delta = (old_width<<16) / width; |
451 | long y_delta = (old_height<<16) / height; | |
c7abc967 | 452 | |
ff865c13 | 453 | unsigned char* dest_pixel = target_data; |
6721fc38 | 454 | |
ff865c13 JS |
455 | long y = 0; |
456 | for ( long j = 0; j < height; j++ ) | |
457 | { | |
458 | unsigned char* src_line = &source_data[(y>>16)*old_width*3]; | |
8d3b6b8a | 459 | unsigned char* src_alpha_line = source_alpha ? &source_alpha[(y>>16)*old_width] : 0 ; |
e9b64c5e | 460 | |
ff865c13 JS |
461 | long x = 0; |
462 | for ( long i = 0; i < width; i++ ) | |
eeca3a46 | 463 | { |
ff865c13 | 464 | unsigned char* src_pixel = &src_line[(x>>16)*3]; |
8d3b6b8a | 465 | unsigned char* src_alpha_pixel = source_alpha ? &src_alpha_line[(x>>16)] : 0 ; |
ff865c13 JS |
466 | dest_pixel[0] = src_pixel[0]; |
467 | dest_pixel[1] = src_pixel[1]; | |
468 | dest_pixel[2] = src_pixel[2]; | |
469 | dest_pixel += 3; | |
8d3b6b8a SC |
470 | if ( source_alpha ) |
471 | *(target_alpha++) = *src_alpha_pixel ; | |
ff865c13 | 472 | x += x_delta; |
eeca3a46 | 473 | } |
ff865c13 JS |
474 | |
475 | y += y_delta; | |
eeca3a46 | 476 | } |
36aac195 | 477 | |
8180d40b | 478 | // In case this is a cursor, make sure the hotspot is scaled accordingly: |
fd94e8aa VS |
479 | if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X) ) |
480 | image.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X, | |
481 | (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X)*width)/old_width); | |
482 | if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y) ) | |
483 | image.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y, | |
484 | (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y)*height)/old_height); | |
c7abc967 | 485 | |
4bc67cc5 RR |
486 | return image; |
487 | } | |
4698648f | 488 | |
f6bcfd97 BP |
489 | wxImage wxImage::Rotate90( bool clockwise ) const |
490 | { | |
491 | wxImage image; | |
492 | ||
493 | wxCHECK_MSG( Ok(), image, wxT("invalid image") ); | |
494 | ||
ff865c13 | 495 | image.Create( M_IMGDATA->m_height, M_IMGDATA->m_width, false ); |
f6bcfd97 | 496 | |
487659e0 | 497 | unsigned char *data = image.GetData(); |
f6bcfd97 BP |
498 | |
499 | wxCHECK_MSG( data, image, wxT("unable to create image") ); | |
500 | ||
921c65ed JS |
501 | unsigned char *source_data = M_IMGDATA->m_data; |
502 | unsigned char *target_data; | |
503 | unsigned char *alpha_data = 0 ; | |
504 | unsigned char *source_alpha = 0 ; | |
505 | unsigned char *target_alpha = 0 ; | |
506 | ||
f6bcfd97 | 507 | if (M_IMGDATA->m_hasMask) |
921c65ed | 508 | { |
f6bcfd97 | 509 | image.SetMaskColour( M_IMGDATA->m_maskRed, M_IMGDATA->m_maskGreen, M_IMGDATA->m_maskBlue ); |
921c65ed JS |
510 | } |
511 | else | |
512 | { | |
513 | source_alpha = M_IMGDATA->m_alpha ; | |
514 | if ( source_alpha ) | |
515 | { | |
516 | image.SetAlpha() ; | |
517 | alpha_data = image.GetAlpha() ; | |
518 | } | |
519 | } | |
f6bcfd97 BP |
520 | |
521 | long height = M_IMGDATA->m_height; | |
522 | long width = M_IMGDATA->m_width; | |
523 | ||
f6bcfd97 BP |
524 | for (long j = 0; j < height; j++) |
525 | { | |
526 | for (long i = 0; i < width; i++) | |
527 | { | |
528 | if (clockwise) | |
921c65ed | 529 | { |
f6bcfd97 | 530 | target_data = data + (((i+1)*height) - j - 1)*3; |
921c65ed JS |
531 | if(source_alpha) |
532 | target_alpha = alpha_data + (((i+1)*height) - j - 1); | |
533 | } | |
f6bcfd97 | 534 | else |
921c65ed | 535 | { |
f6bcfd97 | 536 | target_data = data + ((height*(width-1)) + j - (i*height))*3; |
921c65ed JS |
537 | if(source_alpha) |
538 | target_alpha = alpha_data + ((height*(width-1)) + j - (i*height)); | |
539 | } | |
f6bcfd97 BP |
540 | memcpy( target_data, source_data, 3 ); |
541 | source_data += 3; | |
921c65ed JS |
542 | |
543 | if(source_alpha) | |
544 | { | |
545 | memcpy( target_alpha, source_alpha, 1 ); | |
546 | source_alpha += 1; | |
547 | } | |
f6bcfd97 BP |
548 | } |
549 | } | |
550 | ||
551 | return image; | |
552 | } | |
553 | ||
554 | wxImage wxImage::Mirror( bool horizontally ) const | |
555 | { | |
556 | wxImage image; | |
557 | ||
558 | wxCHECK_MSG( Ok(), image, wxT("invalid image") ); | |
559 | ||
ff865c13 | 560 | image.Create( M_IMGDATA->m_width, M_IMGDATA->m_height, false ); |
f6bcfd97 | 561 | |
487659e0 | 562 | unsigned char *data = image.GetData(); |
f6bcfd97 BP |
563 | |
564 | wxCHECK_MSG( data, image, wxT("unable to create image") ); | |
565 | ||
566 | if (M_IMGDATA->m_hasMask) | |
567 | image.SetMaskColour( M_IMGDATA->m_maskRed, M_IMGDATA->m_maskGreen, M_IMGDATA->m_maskBlue ); | |
568 | ||
569 | long height = M_IMGDATA->m_height; | |
570 | long width = M_IMGDATA->m_width; | |
571 | ||
487659e0 VZ |
572 | unsigned char *source_data = M_IMGDATA->m_data; |
573 | unsigned char *target_data; | |
f6bcfd97 BP |
574 | |
575 | if (horizontally) | |
576 | { | |
577 | for (long j = 0; j < height; j++) | |
578 | { | |
579 | data += width*3; | |
580 | target_data = data-3; | |
581 | for (long i = 0; i < width; i++) | |
582 | { | |
583 | memcpy( target_data, source_data, 3 ); | |
584 | source_data += 3; | |
585 | target_data -= 3; | |
586 | } | |
587 | } | |
588 | } | |
589 | else | |
590 | { | |
591 | for (long i = 0; i < height; i++) | |
592 | { | |
593 | target_data = data + 3*width*(height-1-i); | |
3ca6a5f0 | 594 | memcpy( target_data, source_data, (size_t)3*width ); |
f6bcfd97 BP |
595 | source_data += 3*width; |
596 | } | |
597 | } | |
598 | ||
599 | return image; | |
600 | } | |
601 | ||
7b2471a0 SB |
602 | wxImage wxImage::GetSubImage( const wxRect &rect ) const |
603 | { | |
604 | wxImage image; | |
605 | ||
223d09f6 | 606 | wxCHECK_MSG( Ok(), image, wxT("invalid image") ); |
7b2471a0 | 607 | |
58c837a4 RR |
608 | wxCHECK_MSG( (rect.GetLeft()>=0) && (rect.GetTop()>=0) && (rect.GetRight()<=GetWidth()) && (rect.GetBottom()<=GetHeight()), |
609 | image, wxT("invalid subimage size") ); | |
7b2471a0 SB |
610 | |
611 | int subwidth=rect.GetWidth(); | |
612 | const int subheight=rect.GetHeight(); | |
613 | ||
ff865c13 | 614 | image.Create( subwidth, subheight, false ); |
7b2471a0 | 615 | |
487659e0 | 616 | unsigned char *subdata = image.GetData(), *data=GetData(); |
7b2471a0 | 617 | |
223d09f6 | 618 | wxCHECK_MSG( subdata, image, wxT("unable to create image") ); |
7b2471a0 SB |
619 | |
620 | if (M_IMGDATA->m_hasMask) | |
621 | image.SetMaskColour( M_IMGDATA->m_maskRed, M_IMGDATA->m_maskGreen, M_IMGDATA->m_maskBlue ); | |
622 | ||
623 | const int subleft=3*rect.GetLeft(); | |
624 | const int width=3*GetWidth(); | |
625 | subwidth*=3; | |
995612e2 | 626 | |
7b2471a0 SB |
627 | data+=rect.GetTop()*width+subleft; |
628 | ||
629 | for (long j = 0; j < subheight; ++j) | |
630 | { | |
631 | memcpy( subdata, data, subwidth); | |
632 | subdata+=subwidth; | |
995612e2 | 633 | data+=width; |
7b2471a0 SB |
634 | } |
635 | ||
636 | return image; | |
637 | } | |
638 | ||
e9b64c5e | 639 | wxImage wxImage::Size( const wxSize& size, const wxPoint& pos, |
b737ad10 RR |
640 | int r_, int g_, int b_ ) const |
641 | { | |
642 | wxImage image; | |
643 | ||
644 | wxCHECK_MSG( Ok(), image, wxT("invalid image") ); | |
645 | wxCHECK_MSG( (size.GetWidth() > 0) && (size.GetHeight() > 0), image, wxT("invalid size") ); | |
646 | ||
647 | int width = GetWidth(), height = GetHeight(); | |
648 | image.Create(size.GetWidth(), size.GetHeight(), false); | |
649 | ||
650 | unsigned char r = (unsigned char)r_; | |
651 | unsigned char g = (unsigned char)g_; | |
652 | unsigned char b = (unsigned char)b_; | |
653 | if ((r_ == -1) && (g_ == -1) && (b_ == -1)) | |
654 | { | |
655 | GetOrFindMaskColour( &r, &g, &b ); | |
656 | image.SetMaskColour(r, g, b); | |
657 | } | |
658 | ||
659 | image.SetRGB(wxRect(), r, g, b); | |
660 | ||
661 | wxRect subRect(pos.x, pos.y, width, height); | |
662 | wxRect finalRect(0, 0, size.GetWidth(), size.GetHeight()); | |
663 | ||
664 | subRect.Intersect(finalRect); | |
665 | ||
666 | if (!subRect.IsEmpty()) | |
667 | { | |
668 | if ((subRect.GetWidth() == width) && (subRect.GetHeight() == height)) | |
669 | image.Paste(*this, pos.x, pos.y); | |
670 | else | |
671 | image.Paste(GetSubImage(subRect), pos.x, pos.y); | |
672 | } | |
673 | ||
674 | return image; | |
675 | } | |
676 | ||
f6bcfd97 BP |
677 | void wxImage::Paste( const wxImage &image, int x, int y ) |
678 | { | |
679 | wxCHECK_RET( Ok(), wxT("invalid image") ); | |
680 | wxCHECK_RET( image.Ok(), wxT("invalid image") ); | |
681 | ||
682 | int xx = 0; | |
683 | int yy = 0; | |
684 | int width = image.GetWidth(); | |
685 | int height = image.GetHeight(); | |
686 | ||
687 | if (x < 0) | |
688 | { | |
689 | xx = -x; | |
690 | width += x; | |
691 | } | |
692 | if (y < 0) | |
693 | { | |
694 | yy = -y; | |
695 | height += y; | |
696 | } | |
697 | ||
698 | if ((x+xx)+width > M_IMGDATA->m_width) | |
699 | width = M_IMGDATA->m_width - (x+xx); | |
700 | if ((y+yy)+height > M_IMGDATA->m_height) | |
701 | height = M_IMGDATA->m_height - (y+yy); | |
702 | ||
703 | if (width < 1) return; | |
704 | if (height < 1) return; | |
705 | ||
706 | if ((!HasMask() && !image.HasMask()) || | |
b737ad10 | 707 | (HasMask() && !image.HasMask()) || |
f6bcfd97 BP |
708 | ((HasMask() && image.HasMask() && |
709 | (GetMaskRed()==image.GetMaskRed()) && | |
710 | (GetMaskGreen()==image.GetMaskGreen()) && | |
711 | (GetMaskBlue()==image.GetMaskBlue())))) | |
712 | { | |
713 | width *= 3; | |
714 | unsigned char* source_data = image.GetData() + xx*3 + yy*3*image.GetWidth(); | |
715 | int source_step = image.GetWidth()*3; | |
716 | ||
717 | unsigned char* target_data = GetData() + (x+xx)*3 + (y+yy)*3*M_IMGDATA->m_width; | |
718 | int target_step = M_IMGDATA->m_width*3; | |
719 | for (int j = 0; j < height; j++) | |
720 | { | |
721 | memcpy( target_data, source_data, width ); | |
722 | source_data += source_step; | |
723 | target_data += target_step; | |
724 | } | |
aa21b509 | 725 | return; |
f6bcfd97 | 726 | } |
33ac7e6f | 727 | |
aa21b509 | 728 | if (!HasMask() && image.HasMask()) |
f6bcfd97 | 729 | { |
aa21b509 RR |
730 | unsigned char r = image.GetMaskRed(); |
731 | unsigned char g = image.GetMaskGreen(); | |
732 | unsigned char b = image.GetMaskBlue(); | |
33ac7e6f | 733 | |
aa21b509 RR |
734 | width *= 3; |
735 | unsigned char* source_data = image.GetData() + xx*3 + yy*3*image.GetWidth(); | |
736 | int source_step = image.GetWidth()*3; | |
737 | ||
738 | unsigned char* target_data = GetData() + (x+xx)*3 + (y+yy)*3*M_IMGDATA->m_width; | |
739 | int target_step = M_IMGDATA->m_width*3; | |
33ac7e6f | 740 | |
aa21b509 RR |
741 | for (int j = 0; j < height; j++) |
742 | { | |
743 | for (int i = 0; i < width; i+=3) | |
744 | { | |
33ac7e6f KB |
745 | if ((source_data[i] != r) && |
746 | (source_data[i+1] != g) && | |
aa21b509 RR |
747 | (source_data[i+2] != b)) |
748 | { | |
749 | memcpy( target_data+i, source_data+i, 3 ); | |
750 | } | |
33ac7e6f | 751 | } |
aa21b509 RR |
752 | source_data += source_step; |
753 | target_data += target_step; | |
754 | } | |
f6bcfd97 BP |
755 | } |
756 | } | |
757 | ||
be25e480 RR |
758 | void wxImage::Replace( unsigned char r1, unsigned char g1, unsigned char b1, |
759 | unsigned char r2, unsigned char g2, unsigned char b2 ) | |
760 | { | |
761 | wxCHECK_RET( Ok(), wxT("invalid image") ); | |
762 | ||
487659e0 | 763 | unsigned char *data = GetData(); |
06b466c7 | 764 | |
be25e480 RR |
765 | const int w = GetWidth(); |
766 | const int h = GetHeight(); | |
767 | ||
768 | for (int j = 0; j < h; j++) | |
769 | for (int i = 0; i < w; i++) | |
069d0f27 VZ |
770 | { |
771 | if ((data[0] == r1) && (data[1] == g1) && (data[2] == b1)) | |
772 | { | |
773 | data[0] = r2; | |
774 | data[1] = g2; | |
775 | data[2] = b2; | |
776 | } | |
777 | data += 3; | |
778 | } | |
be25e480 RR |
779 | } |
780 | ||
ec85956a JS |
781 | wxImage wxImage::ConvertToGreyscale( double lr, double lg, double lb ) const |
782 | { | |
783 | wxImage image; | |
784 | ||
785 | wxCHECK_MSG( Ok(), image, wxT("invalid image") ); | |
786 | ||
787 | image.Create(M_IMGDATA->m_width, M_IMGDATA->m_height, false); | |
788 | ||
789 | unsigned char *dest = image.GetData(); | |
790 | ||
791 | wxCHECK_MSG( dest, image, wxT("unable to create image") ); | |
792 | ||
793 | unsigned char *src = M_IMGDATA->m_data; | |
794 | bool hasMask = M_IMGDATA->m_hasMask; | |
795 | unsigned char maskRed = M_IMGDATA->m_maskRed; | |
796 | unsigned char maskGreen = M_IMGDATA->m_maskGreen; | |
797 | unsigned char maskBlue = M_IMGDATA->m_maskBlue; | |
798 | ||
799 | if ( hasMask ) | |
800 | image.SetMaskColour(maskRed, maskGreen, maskBlue); | |
801 | ||
802 | const long size = M_IMGDATA->m_width * M_IMGDATA->m_height; | |
803 | for ( long i = 0; i < size; i++, src += 3, dest += 3 ) | |
804 | { | |
805 | // don't modify the mask | |
806 | if ( hasMask && src[0] == maskRed && src[1] == maskGreen && src[2] == maskBlue ) | |
807 | { | |
808 | memcpy(dest, src, 3); | |
809 | } | |
810 | else | |
811 | { | |
812 | // calculate the luma | |
813 | double luma = (src[0] * lr + src[1] * lg + src[2] * lb) + 0.5; | |
814 | dest[0] = dest[1] = dest[2] = wx_static_cast(unsigned char, luma); | |
815 | } | |
816 | } | |
817 | ||
818 | return image; | |
819 | } | |
820 | ||
f515c25a | 821 | wxImage wxImage::ConvertToMono( unsigned char r, unsigned char g, unsigned char b ) const |
fec19ea9 VS |
822 | { |
823 | wxImage image; | |
824 | ||
825 | wxCHECK_MSG( Ok(), image, wxT("invalid image") ); | |
826 | ||
ff865c13 | 827 | image.Create( M_IMGDATA->m_width, M_IMGDATA->m_height, false ); |
fec19ea9 | 828 | |
487659e0 | 829 | unsigned char *data = image.GetData(); |
fec19ea9 VS |
830 | |
831 | wxCHECK_MSG( data, image, wxT("unable to create image") ); | |
832 | ||
833 | if (M_IMGDATA->m_hasMask) | |
834 | { | |
835 | if (M_IMGDATA->m_maskRed == r && M_IMGDATA->m_maskGreen == g && | |
836 | M_IMGDATA->m_maskBlue == b) | |
837 | image.SetMaskColour( 255, 255, 255 ); | |
838 | else | |
839 | image.SetMaskColour( 0, 0, 0 ); | |
840 | } | |
841 | ||
842 | long size = M_IMGDATA->m_height * M_IMGDATA->m_width; | |
843 | ||
487659e0 VZ |
844 | unsigned char *srcd = M_IMGDATA->m_data; |
845 | unsigned char *tard = image.GetData(); | |
fec19ea9 VS |
846 | |
847 | for ( long i = 0; i < size; i++, srcd += 3, tard += 3 ) | |
848 | { | |
849 | if (srcd[0] == r && srcd[1] == g && srcd[2] == b) | |
850 | tard[0] = tard[1] = tard[2] = 255; | |
851 | else | |
852 | tard[0] = tard[1] = tard[2] = 0; | |
853 | } | |
854 | ||
855 | return image; | |
856 | } | |
857 | ||
21dc4be5 VZ |
858 | int wxImage::GetWidth() const |
859 | { | |
860 | wxCHECK_MSG( Ok(), 0, wxT("invalid image") ); | |
861 | ||
862 | return M_IMGDATA->m_width; | |
863 | } | |
864 | ||
865 | int wxImage::GetHeight() const | |
866 | { | |
867 | wxCHECK_MSG( Ok(), 0, wxT("invalid image") ); | |
868 | ||
869 | return M_IMGDATA->m_height; | |
870 | } | |
871 | ||
5644ac46 | 872 | long wxImage::XYToIndex(int x, int y) const |
ef539066 | 873 | { |
5644ac46 VZ |
874 | if ( Ok() && |
875 | x >= 0 && y >= 0 && | |
876 | x < M_IMGDATA->m_width && y < M_IMGDATA->m_height ) | |
877 | { | |
878 | return y*M_IMGDATA->m_width + x; | |
879 | } | |
c7abc967 | 880 | |
5644ac46 VZ |
881 | return -1; |
882 | } | |
c7abc967 | 883 | |
5644ac46 VZ |
884 | void wxImage::SetRGB( int x, int y, unsigned char r, unsigned char g, unsigned char b ) |
885 | { | |
886 | long pos = XYToIndex(x, y); | |
887 | wxCHECK_RET( pos != -1, wxT("invalid image coordinates") ); | |
c7abc967 | 888 | |
5644ac46 | 889 | pos *= 3; |
c7abc967 | 890 | |
ef539066 RR |
891 | M_IMGDATA->m_data[ pos ] = r; |
892 | M_IMGDATA->m_data[ pos+1 ] = g; | |
893 | M_IMGDATA->m_data[ pos+2 ] = b; | |
894 | } | |
895 | ||
b737ad10 RR |
896 | void wxImage::SetRGB( const wxRect& rect_, unsigned char r, unsigned char g, unsigned char b ) |
897 | { | |
898 | wxCHECK_RET( Ok(), wxT("invalid image") ); | |
899 | ||
900 | wxRect rect(rect_); | |
901 | wxRect imageRect(0, 0, GetWidth(), GetHeight()); | |
902 | if ( rect == wxRect() ) | |
903 | { | |
904 | rect = imageRect; | |
905 | } | |
906 | else | |
907 | { | |
e9b64c5e WS |
908 | wxCHECK_RET( imageRect.Inside(rect.GetTopLeft()) && |
909 | imageRect.Inside(rect.GetBottomRight()), | |
b737ad10 RR |
910 | wxT("invalid bounding rectangle") ); |
911 | } | |
912 | ||
913 | int x1 = rect.GetLeft(), | |
914 | y1 = rect.GetTop(), | |
915 | x2 = rect.GetRight() + 1, | |
916 | y2 = rect.GetBottom() + 1; | |
917 | ||
e9b64c5e | 918 | unsigned char *data wxDUMMY_INITIALIZE(NULL); |
b737ad10 RR |
919 | int x, y, width = GetWidth(); |
920 | for (y = y1; y < y2; y++) | |
921 | { | |
922 | data = M_IMGDATA->m_data + (y*width + x1)*3; | |
923 | for (x = x1; x < x2; x++) | |
924 | { | |
925 | *data++ = r; | |
926 | *data++ = g; | |
927 | *data++ = b; | |
928 | } | |
929 | } | |
930 | } | |
931 | ||
f6bcfd97 | 932 | unsigned char wxImage::GetRed( int x, int y ) const |
ef539066 | 933 | { |
5644ac46 VZ |
934 | long pos = XYToIndex(x, y); |
935 | wxCHECK_MSG( pos != -1, 0, wxT("invalid image coordinates") ); | |
c7abc967 | 936 | |
5644ac46 | 937 | pos *= 3; |
c7abc967 | 938 | |
ef539066 RR |
939 | return M_IMGDATA->m_data[pos]; |
940 | } | |
941 | ||
f6bcfd97 | 942 | unsigned char wxImage::GetGreen( int x, int y ) const |
ef539066 | 943 | { |
5644ac46 VZ |
944 | long pos = XYToIndex(x, y); |
945 | wxCHECK_MSG( pos != -1, 0, wxT("invalid image coordinates") ); | |
c7abc967 | 946 | |
5644ac46 | 947 | pos *= 3; |
c7abc967 | 948 | |
ef539066 RR |
949 | return M_IMGDATA->m_data[pos+1]; |
950 | } | |
951 | ||
f6bcfd97 | 952 | unsigned char wxImage::GetBlue( int x, int y ) const |
ef539066 | 953 | { |
5644ac46 VZ |
954 | long pos = XYToIndex(x, y); |
955 | wxCHECK_MSG( pos != -1, 0, wxT("invalid image coordinates") ); | |
c7abc967 | 956 | |
5644ac46 | 957 | pos *= 3; |
c7abc967 | 958 | |
ef539066 RR |
959 | return M_IMGDATA->m_data[pos+2]; |
960 | } | |
4698648f VZ |
961 | |
962 | bool wxImage::Ok() const | |
963 | { | |
de8c48cf VZ |
964 | // image of 0 width or height can't be considered ok - at least because it |
965 | // causes crashes in ConvertToBitmap() if we don't catch it in time | |
966 | wxImageRefData *data = M_IMGDATA; | |
967 | return data && data->m_ok && data->m_width && data->m_height; | |
01111366 RR |
968 | } |
969 | ||
487659e0 | 970 | unsigned char *wxImage::GetData() const |
01111366 | 971 | { |
487659e0 | 972 | wxCHECK_MSG( Ok(), (unsigned char *)NULL, wxT("invalid image") ); |
c7abc967 | 973 | |
fd0eed64 | 974 | return M_IMGDATA->m_data; |
01111366 RR |
975 | } |
976 | ||
4013de12 | 977 | void wxImage::SetData( unsigned char *data, bool static_data ) |
01111366 | 978 | { |
223d09f6 | 979 | wxCHECK_RET( Ok(), wxT("invalid image") ); |
58a8ab88 | 980 | |
ed58dbea RR |
981 | wxImageRefData *newRefData = new wxImageRefData(); |
982 | ||
983 | newRefData->m_width = M_IMGDATA->m_width; | |
984 | newRefData->m_height = M_IMGDATA->m_height; | |
985 | newRefData->m_data = data; | |
70cd62e9 | 986 | newRefData->m_ok = true; |
ed58dbea RR |
987 | newRefData->m_maskRed = M_IMGDATA->m_maskRed; |
988 | newRefData->m_maskGreen = M_IMGDATA->m_maskGreen; | |
989 | newRefData->m_maskBlue = M_IMGDATA->m_maskBlue; | |
990 | newRefData->m_hasMask = M_IMGDATA->m_hasMask; | |
4013de12 | 991 | newRefData->m_static = static_data; |
995612e2 | 992 | |
ed58dbea | 993 | UnRef(); |
995612e2 | 994 | |
ed58dbea | 995 | m_refData = newRefData; |
01111366 RR |
996 | } |
997 | ||
4013de12 | 998 | void wxImage::SetData( unsigned char *data, int new_width, int new_height, bool static_data ) |
f6bcfd97 BP |
999 | { |
1000 | wxImageRefData *newRefData = new wxImageRefData(); | |
1001 | ||
1002 | if (m_refData) | |
1003 | { | |
1004 | newRefData->m_width = new_width; | |
1005 | newRefData->m_height = new_height; | |
1006 | newRefData->m_data = data; | |
70cd62e9 | 1007 | newRefData->m_ok = true; |
f6bcfd97 BP |
1008 | newRefData->m_maskRed = M_IMGDATA->m_maskRed; |
1009 | newRefData->m_maskGreen = M_IMGDATA->m_maskGreen; | |
1010 | newRefData->m_maskBlue = M_IMGDATA->m_maskBlue; | |
1011 | newRefData->m_hasMask = M_IMGDATA->m_hasMask; | |
1012 | } | |
1013 | else | |
1014 | { | |
1015 | newRefData->m_width = new_width; | |
1016 | newRefData->m_height = new_height; | |
1017 | newRefData->m_data = data; | |
70cd62e9 | 1018 | newRefData->m_ok = true; |
f6bcfd97 | 1019 | } |
4013de12 | 1020 | newRefData->m_static = static_data; |
f6bcfd97 BP |
1021 | |
1022 | UnRef(); | |
1023 | ||
1024 | m_refData = newRefData; | |
1025 | } | |
1026 | ||
487659e0 VZ |
1027 | // ---------------------------------------------------------------------------- |
1028 | // alpha channel support | |
1029 | // ---------------------------------------------------------------------------- | |
1030 | ||
1031 | void wxImage::SetAlpha(int x, int y, unsigned char alpha) | |
1032 | { | |
5644ac46 | 1033 | wxCHECK_RET( HasAlpha(), wxT("no alpha channel") ); |
487659e0 | 1034 | |
5644ac46 VZ |
1035 | long pos = XYToIndex(x, y); |
1036 | wxCHECK_RET( pos != -1, wxT("invalid image coordinates") ); | |
487659e0 | 1037 | |
5644ac46 | 1038 | M_IMGDATA->m_alpha[pos] = alpha; |
487659e0 VZ |
1039 | } |
1040 | ||
d30ee785 | 1041 | unsigned char wxImage::GetAlpha(int x, int y) const |
487659e0 | 1042 | { |
5644ac46 | 1043 | wxCHECK_MSG( HasAlpha(), 0, wxT("no alpha channel") ); |
487659e0 | 1044 | |
5644ac46 VZ |
1045 | long pos = XYToIndex(x, y); |
1046 | wxCHECK_MSG( pos != -1, 0, wxT("invalid image coordinates") ); | |
487659e0 | 1047 | |
5644ac46 | 1048 | return M_IMGDATA->m_alpha[pos]; |
487659e0 VZ |
1049 | } |
1050 | ||
5644ac46 VZ |
1051 | bool |
1052 | wxImage::ConvertColourToAlpha(unsigned char r, unsigned char g, unsigned char b) | |
6408deed | 1053 | { |
5644ac46 | 1054 | SetAlpha(NULL); |
b713f891 | 1055 | |
5644ac46 VZ |
1056 | const int w = M_IMGDATA->m_width; |
1057 | const int h = M_IMGDATA->m_height; | |
b713f891 | 1058 | |
6408deed RR |
1059 | unsigned char *alpha = GetAlpha(); |
1060 | unsigned char *data = GetData(); | |
b713f891 | 1061 | |
5644ac46 VZ |
1062 | for ( int y = 0; y < h; y++ ) |
1063 | { | |
1064 | for ( int x = 0; x < w; x++ ) | |
1065 | { | |
1066 | *alpha++ = *data; | |
1067 | *data++ = r; | |
1068 | *data++ = g; | |
1069 | *data++ = b; | |
1070 | } | |
1071 | } | |
6408deed RR |
1072 | |
1073 | return true; | |
1074 | } | |
1075 | ||
4013de12 | 1076 | void wxImage::SetAlpha( unsigned char *alpha, bool static_data ) |
487659e0 VZ |
1077 | { |
1078 | wxCHECK_RET( Ok(), wxT("invalid image") ); | |
1079 | ||
1080 | if ( !alpha ) | |
1081 | { | |
edf8e8e0 | 1082 | alpha = (unsigned char *)malloc(M_IMGDATA->m_width*M_IMGDATA->m_height); |
487659e0 VZ |
1083 | } |
1084 | ||
5402d21d | 1085 | free(M_IMGDATA->m_alpha); |
487659e0 | 1086 | M_IMGDATA->m_alpha = alpha; |
d2502f14 | 1087 | M_IMGDATA->m_staticAlpha = static_data; |
487659e0 VZ |
1088 | } |
1089 | ||
1090 | unsigned char *wxImage::GetAlpha() const | |
1091 | { | |
1092 | wxCHECK_MSG( Ok(), (unsigned char *)NULL, wxT("invalid image") ); | |
1093 | ||
1094 | return M_IMGDATA->m_alpha; | |
1095 | } | |
1096 | ||
828f0936 VZ |
1097 | void wxImage::InitAlpha() |
1098 | { | |
1099 | wxCHECK_RET( !HasAlpha(), wxT("image already has an alpha channel") ); | |
1100 | ||
1101 | // initialize memory for alpha channel | |
1102 | SetAlpha(); | |
1103 | ||
1104 | unsigned char *alpha = M_IMGDATA->m_alpha; | |
1105 | const size_t lenAlpha = M_IMGDATA->m_width * M_IMGDATA->m_height; | |
1106 | ||
828f0936 VZ |
1107 | if ( HasMask() ) |
1108 | { | |
1109 | // use the mask to initialize the alpha channel. | |
1110 | const unsigned char * const alphaEnd = alpha + lenAlpha; | |
1111 | ||
1112 | const unsigned char mr = M_IMGDATA->m_maskRed; | |
1113 | const unsigned char mg = M_IMGDATA->m_maskGreen; | |
1114 | const unsigned char mb = M_IMGDATA->m_maskBlue; | |
1115 | for ( unsigned char *src = M_IMGDATA->m_data; | |
1116 | alpha < alphaEnd; | |
1117 | src += 3, alpha++ ) | |
1118 | { | |
1119 | *alpha = (src[0] == mr && src[1] == mg && src[2] == mb) | |
21dc4be5 VZ |
1120 | ? wxIMAGE_ALPHA_TRANSPARENT |
1121 | : wxIMAGE_ALPHA_OPAQUE; | |
828f0936 VZ |
1122 | } |
1123 | ||
1124 | M_IMGDATA->m_hasMask = false; | |
1125 | } | |
1126 | else // no mask | |
1127 | { | |
1128 | // make the image fully opaque | |
21dc4be5 | 1129 | memset(alpha, wxIMAGE_ALPHA_OPAQUE, lenAlpha); |
828f0936 VZ |
1130 | } |
1131 | } | |
1132 | ||
487659e0 VZ |
1133 | // ---------------------------------------------------------------------------- |
1134 | // mask support | |
1135 | // ---------------------------------------------------------------------------- | |
1136 | ||
01111366 RR |
1137 | void wxImage::SetMaskColour( unsigned char r, unsigned char g, unsigned char b ) |
1138 | { | |
223d09f6 | 1139 | wxCHECK_RET( Ok(), wxT("invalid image") ); |
c7abc967 | 1140 | |
fd0eed64 RR |
1141 | M_IMGDATA->m_maskRed = r; |
1142 | M_IMGDATA->m_maskGreen = g; | |
1143 | M_IMGDATA->m_maskBlue = b; | |
70cd62e9 | 1144 | M_IMGDATA->m_hasMask = true; |
01111366 RR |
1145 | } |
1146 | ||
b737ad10 RR |
1147 | bool wxImage::GetOrFindMaskColour( unsigned char *r, unsigned char *g, unsigned char *b ) const |
1148 | { | |
1149 | wxCHECK_MSG( Ok(), false, wxT("invalid image") ); | |
1150 | ||
1151 | if (M_IMGDATA->m_hasMask) | |
1152 | { | |
1153 | if (r) *r = M_IMGDATA->m_maskRed; | |
1154 | if (g) *g = M_IMGDATA->m_maskGreen; | |
1155 | if (b) *b = M_IMGDATA->m_maskBlue; | |
1156 | return true; | |
1157 | } | |
1158 | else | |
1159 | { | |
1160 | FindFirstUnusedColour(r, g, b); | |
1161 | return false; | |
1162 | } | |
1163 | } | |
1164 | ||
01111366 RR |
1165 | unsigned char wxImage::GetMaskRed() const |
1166 | { | |
223d09f6 | 1167 | wxCHECK_MSG( Ok(), 0, wxT("invalid image") ); |
c7abc967 | 1168 | |
fd0eed64 | 1169 | return M_IMGDATA->m_maskRed; |
01111366 RR |
1170 | } |
1171 | ||
1172 | unsigned char wxImage::GetMaskGreen() const | |
1173 | { | |
223d09f6 | 1174 | wxCHECK_MSG( Ok(), 0, wxT("invalid image") ); |
c7abc967 | 1175 | |
fd0eed64 | 1176 | return M_IMGDATA->m_maskGreen; |
01111366 RR |
1177 | } |
1178 | ||
1179 | unsigned char wxImage::GetMaskBlue() const | |
1180 | { | |
223d09f6 | 1181 | wxCHECK_MSG( Ok(), 0, wxT("invalid image") ); |
c7abc967 | 1182 | |
fd0eed64 | 1183 | return M_IMGDATA->m_maskBlue; |
01111366 | 1184 | } |
4698648f | 1185 | |
01111366 RR |
1186 | void wxImage::SetMask( bool mask ) |
1187 | { | |
223d09f6 | 1188 | wxCHECK_RET( Ok(), wxT("invalid image") ); |
c7abc967 | 1189 | |
fd0eed64 | 1190 | M_IMGDATA->m_hasMask = mask; |
01111366 RR |
1191 | } |
1192 | ||
1193 | bool wxImage::HasMask() const | |
1194 | { | |
70cd62e9 | 1195 | wxCHECK_MSG( Ok(), false, wxT("invalid image") ); |
c7abc967 | 1196 | |
fd0eed64 | 1197 | return M_IMGDATA->m_hasMask; |
01111366 RR |
1198 | } |
1199 | ||
21dc4be5 | 1200 | bool wxImage::IsTransparent(int x, int y, unsigned char threshold) const |
4698648f | 1201 | { |
21dc4be5 VZ |
1202 | long pos = XYToIndex(x, y); |
1203 | wxCHECK_MSG( pos != -1, false, wxT("invalid image coordinates") ); | |
c7abc967 | 1204 | |
21dc4be5 VZ |
1205 | // check mask |
1206 | if ( M_IMGDATA->m_hasMask ) | |
1207 | { | |
1208 | const unsigned char *p = M_IMGDATA->m_data + 3*pos; | |
1209 | if ( p[0] == M_IMGDATA->m_maskRed && | |
1210 | p[1] == M_IMGDATA->m_maskGreen && | |
1211 | p[2] == M_IMGDATA->m_maskBlue ) | |
1212 | { | |
1213 | return true; | |
1214 | } | |
1215 | } | |
01111366 | 1216 | |
21dc4be5 VZ |
1217 | // then check alpha |
1218 | if ( M_IMGDATA->m_alpha ) | |
1219 | { | |
1220 | if ( M_IMGDATA->m_alpha[pos] < threshold ) | |
1221 | { | |
1222 | // transparent enough | |
1223 | return true; | |
1224 | } | |
1225 | } | |
c7abc967 | 1226 | |
21dc4be5 VZ |
1227 | // not transparent |
1228 | return false; | |
01111366 RR |
1229 | } |
1230 | ||
487659e0 | 1231 | bool wxImage::SetMaskFromImage(const wxImage& mask, |
1f5b2017 | 1232 | unsigned char mr, unsigned char mg, unsigned char mb) |
52b64b0a | 1233 | { |
52b64b0a RR |
1234 | // check that the images are the same size |
1235 | if ( (M_IMGDATA->m_height != mask.GetHeight() ) || (M_IMGDATA->m_width != mask.GetWidth () ) ) | |
1236 | { | |
bc88f66f | 1237 | wxLogError( _("Image and mask have different sizes.") ); |
70cd62e9 | 1238 | return false; |
52b64b0a | 1239 | } |
487659e0 | 1240 | |
52b64b0a RR |
1241 | // find unused colour |
1242 | unsigned char r,g,b ; | |
1f5b2017 | 1243 | if (!FindFirstUnusedColour(&r, &g, &b)) |
52b64b0a | 1244 | { |
bc88f66f | 1245 | wxLogError( _("No unused colour in image being masked.") ); |
70cd62e9 | 1246 | return false ; |
52b64b0a | 1247 | } |
487659e0 VZ |
1248 | |
1249 | unsigned char *imgdata = GetData(); | |
1250 | unsigned char *maskdata = mask.GetData(); | |
52b64b0a RR |
1251 | |
1252 | const int w = GetWidth(); | |
1253 | const int h = GetHeight(); | |
1254 | ||
1255 | for (int j = 0; j < h; j++) | |
1f5b2017 | 1256 | { |
52b64b0a RR |
1257 | for (int i = 0; i < w; i++) |
1258 | { | |
1f5b2017 | 1259 | if ((maskdata[0] == mr) && (maskdata[1] == mg) && (maskdata[2] == mb)) |
52b64b0a RR |
1260 | { |
1261 | imgdata[0] = r; | |
1262 | imgdata[1] = g; | |
1263 | imgdata[2] = b; | |
1264 | } | |
1265 | imgdata += 3; | |
1266 | maskdata += 3; | |
1267 | } | |
1f5b2017 | 1268 | } |
52b64b0a | 1269 | |
1f5b2017 | 1270 | SetMaskColour(r, g, b); |
70cd62e9 | 1271 | SetMask(true); |
487659e0 | 1272 | |
70cd62e9 | 1273 | return true; |
52b64b0a | 1274 | } |
7beb59f3 | 1275 | |
8f2b21e4 | 1276 | bool wxImage::ConvertAlphaToMask(unsigned char threshold) |
ff5ad794 VS |
1277 | { |
1278 | if (!HasAlpha()) | |
1279 | return true; | |
1280 | ||
1281 | unsigned char mr, mg, mb; | |
1282 | if (!FindFirstUnusedColour(&mr, &mg, &mb)) | |
1283 | { | |
1284 | wxLogError( _("No unused colour in image being masked.") ); | |
1285 | return false; | |
1286 | } | |
94406a49 | 1287 | |
ff5ad794 VS |
1288 | SetMask(true); |
1289 | SetMaskColour(mr, mg, mb); | |
94406a49 | 1290 | |
ff5ad794 VS |
1291 | unsigned char *imgdata = GetData(); |
1292 | unsigned char *alphadata = GetAlpha(); | |
1293 | ||
8f2b21e4 VS |
1294 | int w = GetWidth(); |
1295 | int h = GetHeight(); | |
ff5ad794 | 1296 | |
8f2b21e4 | 1297 | for (int y = 0; y < h; y++) |
ff5ad794 | 1298 | { |
8f2b21e4 | 1299 | for (int x = 0; x < w; x++, imgdata += 3, alphadata++) |
ff5ad794 | 1300 | { |
e95f0d79 | 1301 | if (*alphadata < threshold) |
ff5ad794 VS |
1302 | { |
1303 | imgdata[0] = mr; | |
1304 | imgdata[1] = mg; | |
1305 | imgdata[2] = mb; | |
1306 | } | |
1307 | } | |
1308 | } | |
1309 | ||
1310 | free(M_IMGDATA->m_alpha); | |
1311 | M_IMGDATA->m_alpha = NULL; | |
94406a49 DS |
1312 | |
1313 | return true; | |
ff5ad794 | 1314 | } |
52b64b0a | 1315 | |
21dc4be5 | 1316 | // ---------------------------------------------------------------------------- |
5e5437e0 | 1317 | // Palette functions |
21dc4be5 VZ |
1318 | // ---------------------------------------------------------------------------- |
1319 | ||
1320 | #if wxUSE_PALETTE | |
5e5437e0 JS |
1321 | |
1322 | bool wxImage::HasPalette() const | |
1323 | { | |
1324 | if (!Ok()) | |
70cd62e9 | 1325 | return false; |
5e5437e0 JS |
1326 | |
1327 | return M_IMGDATA->m_palette.Ok(); | |
1328 | } | |
1329 | ||
1330 | const wxPalette& wxImage::GetPalette() const | |
1331 | { | |
1332 | wxCHECK_MSG( Ok(), wxNullPalette, wxT("invalid image") ); | |
1333 | ||
1334 | return M_IMGDATA->m_palette; | |
1335 | } | |
1336 | ||
1337 | void wxImage::SetPalette(const wxPalette& palette) | |
1338 | { | |
1339 | wxCHECK_RET( Ok(), wxT("invalid image") ); | |
1340 | ||
1341 | M_IMGDATA->m_palette = palette; | |
1342 | } | |
1343 | ||
d275c7eb VZ |
1344 | #endif // wxUSE_PALETTE |
1345 | ||
21dc4be5 | 1346 | // ---------------------------------------------------------------------------- |
5e5437e0 | 1347 | // Option functions (arbitrary name/value mapping) |
21dc4be5 VZ |
1348 | // ---------------------------------------------------------------------------- |
1349 | ||
5e5437e0 JS |
1350 | void wxImage::SetOption(const wxString& name, const wxString& value) |
1351 | { | |
1352 | wxCHECK_RET( Ok(), wxT("invalid image") ); | |
1353 | ||
70cd62e9 | 1354 | int idx = M_IMGDATA->m_optionNames.Index(name, false); |
5e5437e0 JS |
1355 | if (idx == wxNOT_FOUND) |
1356 | { | |
1357 | M_IMGDATA->m_optionNames.Add(name); | |
1358 | M_IMGDATA->m_optionValues.Add(value); | |
1359 | } | |
1360 | else | |
1361 | { | |
1362 | M_IMGDATA->m_optionNames[idx] = name; | |
1363 | M_IMGDATA->m_optionValues[idx] = value; | |
1364 | } | |
1365 | } | |
1366 | ||
1367 | void wxImage::SetOption(const wxString& name, int value) | |
1368 | { | |
1369 | wxString valStr; | |
1370 | valStr.Printf(wxT("%d"), value); | |
1371 | SetOption(name, valStr); | |
1372 | } | |
1373 | ||
1374 | wxString wxImage::GetOption(const wxString& name) const | |
1375 | { | |
1376 | wxCHECK_MSG( Ok(), wxEmptyString, wxT("invalid image") ); | |
1377 | ||
70cd62e9 | 1378 | int idx = M_IMGDATA->m_optionNames.Index(name, false); |
5e5437e0 JS |
1379 | if (idx == wxNOT_FOUND) |
1380 | return wxEmptyString; | |
1381 | else | |
1382 | return M_IMGDATA->m_optionValues[idx]; | |
1383 | } | |
1384 | ||
1385 | int wxImage::GetOptionInt(const wxString& name) const | |
1386 | { | |
5e5437e0 JS |
1387 | return wxAtoi(GetOption(name)); |
1388 | } | |
1389 | ||
1390 | bool wxImage::HasOption(const wxString& name) const | |
1391 | { | |
70cd62e9 | 1392 | wxCHECK_MSG( Ok(), false, wxT("invalid image") ); |
5e5437e0 | 1393 | |
70cd62e9 | 1394 | return (M_IMGDATA->m_optionNames.Index(name, false) != wxNOT_FOUND); |
5e5437e0 JS |
1395 | } |
1396 | ||
21dc4be5 VZ |
1397 | // ---------------------------------------------------------------------------- |
1398 | // image I/O | |
1399 | // ---------------------------------------------------------------------------- | |
1400 | ||
60d43ad8 | 1401 | bool wxImage::LoadFile( const wxString& filename, long type, int index ) |
01111366 | 1402 | { |
e02afc7a | 1403 | #if wxUSE_STREAMS |
d80207c3 | 1404 | if (wxFileExists(filename)) |
6c28fd33 | 1405 | { |
d80207c3 KB |
1406 | wxFileInputStream stream(filename); |
1407 | wxBufferedInputStream bstream( stream ); | |
60d43ad8 | 1408 | return LoadFile(bstream, type, index); |
6c28fd33 | 1409 | } |
d80207c3 | 1410 | else |
6c28fd33 | 1411 | { |
d80207c3 KB |
1412 | wxLogError( _("Can't load image from file '%s': file does not exist."), filename.c_str() ); |
1413 | ||
70cd62e9 | 1414 | return false; |
6c28fd33 | 1415 | } |
9e9ee68e | 1416 | #else // !wxUSE_STREAMS |
70cd62e9 | 1417 | return false; |
9e9ee68e VS |
1418 | #endif // wxUSE_STREAMS |
1419 | } | |
1420 | ||
60d43ad8 | 1421 | bool wxImage::LoadFile( const wxString& filename, const wxString& mimetype, int index ) |
9e9ee68e VS |
1422 | { |
1423 | #if wxUSE_STREAMS | |
d80207c3 | 1424 | if (wxFileExists(filename)) |
6c28fd33 | 1425 | { |
d80207c3 KB |
1426 | wxFileInputStream stream(filename); |
1427 | wxBufferedInputStream bstream( stream ); | |
60d43ad8 | 1428 | return LoadFile(bstream, mimetype, index); |
6c28fd33 | 1429 | } |
d80207c3 | 1430 | else |
6c28fd33 | 1431 | { |
d80207c3 KB |
1432 | wxLogError( _("Can't load image from file '%s': file does not exist."), filename.c_str() ); |
1433 | ||
70cd62e9 | 1434 | return false; |
6c28fd33 | 1435 | } |
e02afc7a | 1436 | #else // !wxUSE_STREAMS |
70cd62e9 | 1437 | return false; |
e02afc7a | 1438 | #endif // wxUSE_STREAMS |
1ccbb61a VZ |
1439 | } |
1440 | ||
45647dcf VS |
1441 | |
1442 | ||
1443 | bool wxImage::SaveFile( const wxString& filename ) const | |
1444 | { | |
1445 | wxString ext = filename.AfterLast('.').Lower(); | |
487659e0 | 1446 | |
45647dcf VS |
1447 | wxImageHandler * pHandler = FindHandler(ext, -1); |
1448 | if (pHandler) | |
1449 | { | |
1450 | SaveFile(filename, pHandler->GetType()); | |
70cd62e9 | 1451 | return true; |
45647dcf VS |
1452 | } |
1453 | ||
1454 | wxLogError(_("Can't save image to file '%s': unknown extension."), filename.c_str()); | |
1455 | ||
70cd62e9 | 1456 | return false; |
45647dcf VS |
1457 | } |
1458 | ||
e0a76d8d | 1459 | bool wxImage::SaveFile( const wxString& filename, int type ) const |
1ccbb61a | 1460 | { |
e02afc7a | 1461 | #if wxUSE_STREAMS |
2a736739 VZ |
1462 | wxCHECK_MSG( Ok(), false, wxT("invalid image") ); |
1463 | ||
36aac195 | 1464 | ((wxImage*)this)->SetOption(wxIMAGE_OPTION_FILENAME, filename); |
fd94e8aa | 1465 | |
1ccbb61a | 1466 | wxFileOutputStream stream(filename); |
9e9ee68e | 1467 | |
2b5f62a0 | 1468 | if ( stream.IsOk() ) |
1b055864 | 1469 | { |
069d0f27 | 1470 | wxBufferedOutputStream bstream( stream ); |
1b055864 RR |
1471 | return SaveFile(bstream, type); |
1472 | } | |
e02afc7a | 1473 | #endif // wxUSE_STREAMS |
3ca6a5f0 | 1474 | |
70cd62e9 | 1475 | return false; |
3d05544e | 1476 | } |
01111366 | 1477 | |
e0a76d8d | 1478 | bool wxImage::SaveFile( const wxString& filename, const wxString& mimetype ) const |
9e9ee68e VS |
1479 | { |
1480 | #if wxUSE_STREAMS | |
2a736739 VZ |
1481 | wxCHECK_MSG( Ok(), false, wxT("invalid image") ); |
1482 | ||
36aac195 | 1483 | ((wxImage*)this)->SetOption(wxIMAGE_OPTION_FILENAME, filename); |
fd94e8aa | 1484 | |
9e9ee68e | 1485 | wxFileOutputStream stream(filename); |
c7abc967 | 1486 | |
2b5f62a0 | 1487 | if ( stream.IsOk() ) |
1b055864 | 1488 | { |
069d0f27 | 1489 | wxBufferedOutputStream bstream( stream ); |
1b055864 RR |
1490 | return SaveFile(bstream, mimetype); |
1491 | } | |
9e9ee68e | 1492 | #endif // wxUSE_STREAMS |
3ca6a5f0 | 1493 | |
70cd62e9 | 1494 | return false; |
9e9ee68e VS |
1495 | } |
1496 | ||
87202f78 SB |
1497 | bool wxImage::CanRead( const wxString &name ) |
1498 | { | |
1499 | #if wxUSE_STREAMS | |
1500 | wxFileInputStream stream(name); | |
1501 | return CanRead(stream); | |
1502 | #else | |
70cd62e9 | 1503 | return false; |
87202f78 SB |
1504 | #endif |
1505 | } | |
1506 | ||
649d13e8 | 1507 | int wxImage::GetImageCount( const wxString &name, long type ) |
60d43ad8 VS |
1508 | { |
1509 | #if wxUSE_STREAMS | |
1510 | wxFileInputStream stream(name); | |
2c0a4e08 | 1511 | if (stream.Ok()) |
08811762 | 1512 | return GetImageCount(stream, type); |
60d43ad8 | 1513 | #endif |
2c0a4e08 VZ |
1514 | |
1515 | return 0; | |
60d43ad8 VS |
1516 | } |
1517 | ||
e02afc7a | 1518 | #if wxUSE_STREAMS |
deb2fec0 | 1519 | |
87202f78 SB |
1520 | bool wxImage::CanRead( wxInputStream &stream ) |
1521 | { | |
79fa2374 | 1522 | const wxList& list = GetHandlers(); |
004fd0c8 | 1523 | |
222ed1d6 | 1524 | for ( wxList::compatibility_iterator node = list.GetFirst(); node; node = node->GetNext() ) |
004fd0c8 | 1525 | { |
60d43ad8 VS |
1526 | wxImageHandler *handler=(wxImageHandler*)node->GetData(); |
1527 | if (handler->CanRead( stream )) | |
70cd62e9 | 1528 | return true; |
87202f78 SB |
1529 | } |
1530 | ||
70cd62e9 | 1531 | return false; |
60d43ad8 VS |
1532 | } |
1533 | ||
649d13e8 | 1534 | int wxImage::GetImageCount( wxInputStream &stream, long type ) |
60d43ad8 VS |
1535 | { |
1536 | wxImageHandler *handler; | |
1537 | ||
1538 | if ( type == wxBITMAP_TYPE_ANY ) | |
1539 | { | |
1540 | wxList &list=GetHandlers(); | |
1541 | ||
222ed1d6 | 1542 | for (wxList::compatibility_iterator node = list.GetFirst(); node; node = node->GetNext()) |
60d43ad8 VS |
1543 | { |
1544 | handler=(wxImageHandler*)node->GetData(); | |
1545 | if ( handler->CanRead(stream) ) | |
649d13e8 | 1546 | return handler->GetImageCount(stream); |
60d43ad8 VS |
1547 | |
1548 | } | |
1549 | ||
1550 | wxLogWarning(_("No handler found for image type.")); | |
1551 | return 0; | |
1552 | } | |
1553 | ||
1554 | handler = FindHandler(type); | |
1555 | ||
1556 | if ( !handler ) | |
1557 | { | |
1558 | wxLogWarning(_("No image handler for type %d defined."), type); | |
70cd62e9 | 1559 | return false; |
60d43ad8 VS |
1560 | } |
1561 | ||
1562 | if ( handler->CanRead(stream) ) | |
1563 | { | |
649d13e8 | 1564 | return handler->GetImageCount(stream); |
60d43ad8 VS |
1565 | } |
1566 | else | |
1567 | { | |
1568 | wxLogError(_("Image file is not of type %d."), type); | |
1569 | return 0; | |
1570 | } | |
87202f78 SB |
1571 | } |
1572 | ||
60d43ad8 | 1573 | bool wxImage::LoadFile( wxInputStream& stream, long type, int index ) |
3d05544e JS |
1574 | { |
1575 | UnRef(); | |
c7abc967 | 1576 | |
fd0eed64 | 1577 | m_refData = new wxImageRefData; |
c7abc967 | 1578 | |
deb2fec0 SB |
1579 | wxImageHandler *handler; |
1580 | ||
60d43ad8 | 1581 | if ( type == wxBITMAP_TYPE_ANY ) |
deb2fec0 | 1582 | { |
995612e2 | 1583 | wxList &list=GetHandlers(); |
deb2fec0 | 1584 | |
222ed1d6 | 1585 | for ( wxList::compatibility_iterator node = list.GetFirst(); node; node = node->GetNext() ) |
995612e2 VZ |
1586 | { |
1587 | handler=(wxImageHandler*)node->GetData(); | |
60d43ad8 | 1588 | if ( handler->CanRead(stream) ) |
70cd62e9 | 1589 | return handler->LoadFile(this, stream, true/*verbose*/, index); |
7b2471a0 | 1590 | |
995612e2 | 1591 | } |
deb2fec0 | 1592 | |
58c837a4 | 1593 | wxLogWarning( _("No handler found for image type.") ); |
70cd62e9 | 1594 | return false; |
deb2fec0 SB |
1595 | } |
1596 | ||
1597 | handler = FindHandler(type); | |
c7abc967 | 1598 | |
2b5f62a0 | 1599 | if (handler == 0) |
fd0eed64 | 1600 | { |
58c837a4 | 1601 | wxLogWarning( _("No image handler for type %d defined."), type ); |
c7abc967 | 1602 | |
70cd62e9 | 1603 | return false; |
fd0eed64 | 1604 | } |
c7abc967 | 1605 | |
b598ec91 | 1606 | if (stream.IsSeekable() && !handler->CanRead(stream)) |
dd9ea234 JS |
1607 | { |
1608 | wxLogError(_("Image file is not of type %d."), type); | |
1609 | return false; | |
1610 | } | |
1611 | else | |
1612 | return handler->LoadFile(this, stream, true/*verbose*/, index); | |
01111366 RR |
1613 | } |
1614 | ||
60d43ad8 | 1615 | bool wxImage::LoadFile( wxInputStream& stream, const wxString& mimetype, int index ) |
9e9ee68e VS |
1616 | { |
1617 | UnRef(); | |
1618 | ||
1619 | m_refData = new wxImageRefData; | |
1620 | ||
1621 | wxImageHandler *handler = FindHandlerMime(mimetype); | |
1622 | ||
2b5f62a0 | 1623 | if (handler == 0) |
9e9ee68e | 1624 | { |
58c837a4 | 1625 | wxLogWarning( _("No image handler for type %s defined."), mimetype.GetData() ); |
9e9ee68e | 1626 | |
70cd62e9 | 1627 | return false; |
9e9ee68e VS |
1628 | } |
1629 | ||
b598ec91 | 1630 | if (stream.IsSeekable() && !handler->CanRead(stream)) |
dd9ea234 JS |
1631 | { |
1632 | wxLogError(_("Image file is not of type %s."), (const wxChar*) mimetype); | |
1633 | return false; | |
1634 | } | |
1635 | else | |
1636 | return handler->LoadFile( this, stream, true/*verbose*/, index ); | |
9e9ee68e VS |
1637 | } |
1638 | ||
e0a76d8d | 1639 | bool wxImage::SaveFile( wxOutputStream& stream, int type ) const |
01111366 | 1640 | { |
70cd62e9 | 1641 | wxCHECK_MSG( Ok(), false, wxT("invalid image") ); |
c7abc967 | 1642 | |
fd0eed64 | 1643 | wxImageHandler *handler = FindHandler(type); |
2a736739 | 1644 | if ( !handler ) |
fd0eed64 | 1645 | { |
58c837a4 | 1646 | wxLogWarning( _("No image handler for type %d defined."), type ); |
9e9ee68e | 1647 | |
70cd62e9 | 1648 | return false; |
9e9ee68e VS |
1649 | } |
1650 | ||
e0a76d8d | 1651 | return handler->SaveFile( (wxImage*)this, stream ); |
9e9ee68e VS |
1652 | } |
1653 | ||
e0a76d8d | 1654 | bool wxImage::SaveFile( wxOutputStream& stream, const wxString& mimetype ) const |
9e9ee68e | 1655 | { |
70cd62e9 | 1656 | wxCHECK_MSG( Ok(), false, wxT("invalid image") ); |
c7abc967 | 1657 | |
9e9ee68e | 1658 | wxImageHandler *handler = FindHandlerMime(mimetype); |
2a736739 | 1659 | if ( !handler ) |
9e9ee68e | 1660 | { |
58c837a4 | 1661 | wxLogWarning( _("No image handler for type %s defined."), mimetype.GetData() ); |
c7abc967 | 1662 | |
70cd62e9 | 1663 | return false; |
fd0eed64 | 1664 | } |
c7abc967 | 1665 | |
e0a76d8d | 1666 | return handler->SaveFile( (wxImage*)this, stream ); |
01111366 | 1667 | } |
e02afc7a | 1668 | #endif // wxUSE_STREAMS |
01111366 | 1669 | |
21dc4be5 VZ |
1670 | // ---------------------------------------------------------------------------- |
1671 | // image I/O handlers | |
1672 | // ---------------------------------------------------------------------------- | |
1673 | ||
01111366 RR |
1674 | void wxImage::AddHandler( wxImageHandler *handler ) |
1675 | { | |
2b5f62a0 VZ |
1676 | // Check for an existing handler of the type being added. |
1677 | if (FindHandler( handler->GetType() ) == 0) | |
1678 | { | |
1679 | sm_handlers.Append( handler ); | |
1680 | } | |
1681 | else | |
1682 | { | |
1683 | // This is not documented behaviour, merely the simplest 'fix' | |
1684 | // for preventing duplicate additions. If someone ever has | |
1685 | // a good reason to add and remove duplicate handlers (and they | |
1686 | // may) we should probably refcount the duplicates. | |
1687 | // also an issue in InsertHandler below. | |
1688 | ||
1689 | wxLogDebug( _T("Adding duplicate image handler for '%s'"), | |
1690 | handler->GetName().c_str() ); | |
1691 | delete handler; | |
1692 | } | |
01111366 RR |
1693 | } |
1694 | ||
1695 | void wxImage::InsertHandler( wxImageHandler *handler ) | |
1696 | { | |
2b5f62a0 VZ |
1697 | // Check for an existing handler of the type being added. |
1698 | if (FindHandler( handler->GetType() ) == 0) | |
1699 | { | |
1700 | sm_handlers.Insert( handler ); | |
1701 | } | |
1702 | else | |
1703 | { | |
1704 | // see AddHandler for additional comments. | |
1705 | wxLogDebug( _T("Inserting duplicate image handler for '%s'"), | |
1706 | handler->GetName().c_str() ); | |
1707 | delete handler; | |
1708 | } | |
01111366 RR |
1709 | } |
1710 | ||
1711 | bool wxImage::RemoveHandler( const wxString& name ) | |
1712 | { | |
fd0eed64 RR |
1713 | wxImageHandler *handler = FindHandler(name); |
1714 | if (handler) | |
1715 | { | |
1716 | sm_handlers.DeleteObject(handler); | |
222ed1d6 | 1717 | delete handler; |
70cd62e9 | 1718 | return true; |
fd0eed64 RR |
1719 | } |
1720 | else | |
70cd62e9 | 1721 | return false; |
01111366 RR |
1722 | } |
1723 | ||
1724 | wxImageHandler *wxImage::FindHandler( const wxString& name ) | |
1725 | { | |
222ed1d6 | 1726 | wxList::compatibility_iterator node = sm_handlers.GetFirst(); |
fd0eed64 RR |
1727 | while (node) |
1728 | { | |
b1d4dd7a | 1729 | wxImageHandler *handler = (wxImageHandler*)node->GetData(); |
ce3ed50d | 1730 | if (handler->GetName().Cmp(name) == 0) return handler; |
c7abc967 | 1731 | |
b1d4dd7a | 1732 | node = node->GetNext(); |
fd0eed64 | 1733 | } |
2b5f62a0 | 1734 | return 0; |
01111366 RR |
1735 | } |
1736 | ||
1737 | wxImageHandler *wxImage::FindHandler( const wxString& extension, long bitmapType ) | |
1738 | { | |
222ed1d6 | 1739 | wxList::compatibility_iterator node = sm_handlers.GetFirst(); |
fd0eed64 RR |
1740 | while (node) |
1741 | { | |
b1d4dd7a | 1742 | wxImageHandler *handler = (wxImageHandler*)node->GetData(); |
ce3ed50d | 1743 | if ( (handler->GetExtension().Cmp(extension) == 0) && |
fd0eed64 | 1744 | (bitmapType == -1 || handler->GetType() == bitmapType) ) |
dbda9e86 | 1745 | return handler; |
b1d4dd7a | 1746 | node = node->GetNext(); |
fd0eed64 | 1747 | } |
2b5f62a0 | 1748 | return 0; |
01111366 RR |
1749 | } |
1750 | ||
1751 | wxImageHandler *wxImage::FindHandler( long bitmapType ) | |
1752 | { | |
222ed1d6 | 1753 | wxList::compatibility_iterator node = sm_handlers.GetFirst(); |
fd0eed64 RR |
1754 | while (node) |
1755 | { | |
b1d4dd7a | 1756 | wxImageHandler *handler = (wxImageHandler *)node->GetData(); |
fd0eed64 | 1757 | if (handler->GetType() == bitmapType) return handler; |
b1d4dd7a | 1758 | node = node->GetNext(); |
fd0eed64 | 1759 | } |
2b5f62a0 | 1760 | return 0; |
fd0eed64 RR |
1761 | } |
1762 | ||
9e9ee68e VS |
1763 | wxImageHandler *wxImage::FindHandlerMime( const wxString& mimetype ) |
1764 | { | |
222ed1d6 | 1765 | wxList::compatibility_iterator node = sm_handlers.GetFirst(); |
9e9ee68e VS |
1766 | while (node) |
1767 | { | |
b1d4dd7a | 1768 | wxImageHandler *handler = (wxImageHandler *)node->GetData(); |
70cd62e9 | 1769 | if (handler->GetMimeType().IsSameAs(mimetype, false)) return handler; |
b1d4dd7a | 1770 | node = node->GetNext(); |
9e9ee68e | 1771 | } |
2b5f62a0 | 1772 | return 0; |
9e9ee68e VS |
1773 | } |
1774 | ||
fd0eed64 RR |
1775 | void wxImage::InitStandardHandlers() |
1776 | { | |
77fac225 | 1777 | #if wxUSE_STREAMS |
66e23ad2 | 1778 | AddHandler(new wxBMPHandler); |
77fac225 | 1779 | #endif // wxUSE_STREAMS |
01111366 RR |
1780 | } |
1781 | ||
1782 | void wxImage::CleanUpHandlers() | |
1783 | { | |
222ed1d6 | 1784 | wxList::compatibility_iterator node = sm_handlers.GetFirst(); |
fd0eed64 RR |
1785 | while (node) |
1786 | { | |
b1d4dd7a | 1787 | wxImageHandler *handler = (wxImageHandler *)node->GetData(); |
222ed1d6 | 1788 | wxList::compatibility_iterator next = node->GetNext(); |
fd0eed64 | 1789 | delete handler; |
fd0eed64 RR |
1790 | node = next; |
1791 | } | |
01111366 | 1792 | |
222ed1d6 MB |
1793 | sm_handlers.Clear(); |
1794 | } | |
ff865c13 | 1795 | |
939fadc8 JS |
1796 | wxString wxImage::GetImageExtWildcard() |
1797 | { | |
1798 | wxString fmts; | |
1799 | ||
1800 | wxList& Handlers = wxImage::GetHandlers(); | |
222ed1d6 | 1801 | wxList::compatibility_iterator Node = Handlers.GetFirst(); |
939fadc8 JS |
1802 | while ( Node ) |
1803 | { | |
1804 | wxImageHandler* Handler = (wxImageHandler*)Node->GetData(); | |
1805 | fmts += wxT("*.") + Handler->GetExtension(); | |
1806 | Node = Node->GetNext(); | |
1807 | if ( Node ) fmts += wxT(";"); | |
1808 | } | |
1809 | ||
1810 | return wxT("(") + fmts + wxT(")|") + fmts; | |
1811 | } | |
1812 | ||
978d3d36 VZ |
1813 | wxImage::HSVValue wxImage::RGBtoHSV(const RGBValue& rgb) |
1814 | { | |
978d3d36 VZ |
1815 | const double red = rgb.red / 255.0, |
1816 | green = rgb.green / 255.0, | |
1817 | blue = rgb.blue / 255.0; | |
1818 | ||
c77a6796 VZ |
1819 | // find the min and max intensity (and remember which one was it for the |
1820 | // latter) | |
978d3d36 | 1821 | double minimumRGB = red; |
c77a6796 | 1822 | if ( green < minimumRGB ) |
978d3d36 | 1823 | minimumRGB = green; |
c77a6796 | 1824 | if ( blue < minimumRGB ) |
978d3d36 VZ |
1825 | minimumRGB = blue; |
1826 | ||
c77a6796 | 1827 | enum { RED, GREEN, BLUE } chMax = RED; |
978d3d36 | 1828 | double maximumRGB = red; |
c77a6796 VZ |
1829 | if ( green > maximumRGB ) |
1830 | { | |
1831 | chMax = GREEN; | |
978d3d36 | 1832 | maximumRGB = green; |
c77a6796 VZ |
1833 | } |
1834 | if ( blue > maximumRGB ) | |
1835 | { | |
1836 | chMax = BLUE; | |
978d3d36 | 1837 | maximumRGB = blue; |
c77a6796 | 1838 | } |
978d3d36 | 1839 | |
c77a6796 | 1840 | const double value = maximumRGB; |
978d3d36 | 1841 | |
38d4b1e4 | 1842 | double hue = 0.0, saturation; |
c77a6796 VZ |
1843 | const double deltaRGB = maximumRGB - minimumRGB; |
1844 | if ( wxIsNullDouble(deltaRGB) ) | |
978d3d36 VZ |
1845 | { |
1846 | // Gray has no color | |
1847 | hue = 0.0; | |
1848 | saturation = 0.0; | |
1849 | } | |
1850 | else | |
1851 | { | |
c77a6796 VZ |
1852 | switch ( chMax ) |
1853 | { | |
1854 | case RED: | |
1855 | hue = (green - blue) / deltaRGB; | |
1856 | break; | |
978d3d36 | 1857 | |
c77a6796 VZ |
1858 | case GREEN: |
1859 | hue = 2.0 + (blue - red) / deltaRGB; | |
1860 | break; | |
978d3d36 | 1861 | |
c77a6796 VZ |
1862 | case BLUE: |
1863 | hue = 4.0 + (red - green) / deltaRGB; | |
1864 | break; | |
38d4b1e4 WS |
1865 | |
1866 | default: | |
1867 | wxFAIL_MSG(wxT("hue not specified")); | |
1868 | break; | |
c77a6796 VZ |
1869 | } |
1870 | ||
1871 | hue /= 6.0; | |
978d3d36 | 1872 | |
c77a6796 VZ |
1873 | if ( hue < 0.0 ) |
1874 | hue += 1.0; | |
978d3d36 | 1875 | |
c77a6796 | 1876 | saturation = deltaRGB / maximumRGB; |
978d3d36 VZ |
1877 | } |
1878 | ||
1879 | return HSVValue(hue, saturation, value); | |
1880 | } | |
1881 | ||
1882 | wxImage::RGBValue wxImage::HSVtoRGB(const HSVValue& hsv) | |
1883 | { | |
1884 | double red, green, blue; | |
1885 | ||
c77a6796 | 1886 | if ( wxIsNullDouble(hsv.saturation) ) |
978d3d36 | 1887 | { |
c77a6796 VZ |
1888 | // Grey |
1889 | red = hsv.value; | |
978d3d36 | 1890 | green = hsv.value; |
c77a6796 | 1891 | blue = hsv.value; |
978d3d36 | 1892 | } |
c77a6796 | 1893 | else // not grey |
978d3d36 VZ |
1894 | { |
1895 | double hue = hsv.hue * 6.0; // sector 0 to 5 | |
1896 | int i = (int)floor(hue); | |
1897 | double f = hue - i; // fractional part of h | |
1898 | double p = hsv.value * (1.0 - hsv.saturation); | |
1899 | ||
1900 | switch (i) | |
1901 | { | |
1902 | case 0: | |
1903 | red = hsv.value; | |
1904 | green = hsv.value * (1.0 - hsv.saturation * (1.0 - f)); | |
1905 | blue = p; | |
1906 | break; | |
1907 | ||
1908 | case 1: | |
1909 | red = hsv.value * (1.0 - hsv.saturation * f); | |
1910 | green = hsv.value; | |
1911 | blue = p; | |
1912 | break; | |
1913 | ||
1914 | case 2: | |
1915 | red = p; | |
1916 | green = hsv.value; | |
1917 | blue = hsv.value * (1.0 - hsv.saturation * (1.0 - f)); | |
1918 | break; | |
1919 | ||
1920 | case 3: | |
1921 | red = p; | |
1922 | green = hsv.value * (1.0 - hsv.saturation * f); | |
1923 | blue = hsv.value; | |
1924 | break; | |
1925 | ||
1926 | case 4: | |
1927 | red = hsv.value * (1.0 - hsv.saturation * (1.0 - f)); | |
1928 | green = p; | |
1929 | blue = hsv.value; | |
1930 | break; | |
1931 | ||
1932 | default: // case 5: | |
1933 | red = hsv.value; | |
1934 | green = p; | |
1935 | blue = hsv.value * (1.0 - hsv.saturation * f); | |
1936 | break; | |
1937 | } | |
1938 | } | |
1939 | ||
1940 | return RGBValue((unsigned char)(red * 255.0), | |
1941 | (unsigned char)(green * 255.0), | |
1942 | (unsigned char)(blue * 255.0)); | |
1943 | } | |
1944 | ||
1945 | /* | |
1946 | * Rotates the hue of each pixel of the image. angle is a double in the range | |
1947 | * -1.0..1.0 where -1.0 is -360 degrees and 1.0 is 360 degrees | |
1948 | */ | |
1949 | void wxImage::RotateHue(double angle) | |
1950 | { | |
1951 | unsigned char *srcBytePtr; | |
1952 | unsigned char *dstBytePtr; | |
1953 | unsigned long count; | |
1954 | wxImage::HSVValue hsv; | |
1955 | wxImage::RGBValue rgb; | |
1956 | ||
6926b9c4 | 1957 | wxASSERT (angle >= -1.0 && angle <= 1.0); |
978d3d36 | 1958 | count = M_IMGDATA->m_width * M_IMGDATA->m_height; |
c77a6796 | 1959 | if ( count > 0 && !wxIsNullDouble(angle) ) |
978d3d36 VZ |
1960 | { |
1961 | srcBytePtr = M_IMGDATA->m_data; | |
1962 | dstBytePtr = srcBytePtr; | |
1963 | do | |
1964 | { | |
1965 | rgb.red = *srcBytePtr++; | |
1966 | rgb.green = *srcBytePtr++; | |
1967 | rgb.blue = *srcBytePtr++; | |
1968 | hsv = RGBtoHSV(rgb); | |
1969 | ||
1970 | hsv.hue = hsv.hue + angle; | |
1971 | if (hsv.hue > 1.0) | |
1972 | hsv.hue = hsv.hue - 1.0; | |
1973 | else if (hsv.hue < 0.0) | |
1974 | hsv.hue = hsv.hue + 1.0; | |
1975 | ||
1976 | rgb = HSVtoRGB(hsv); | |
1977 | *dstBytePtr++ = rgb.red; | |
1978 | *dstBytePtr++ = rgb.green; | |
1979 | *dstBytePtr++ = rgb.blue; | |
1980 | } while (--count != 0); | |
1981 | } | |
1982 | } | |
1983 | ||
01111366 RR |
1984 | //----------------------------------------------------------------------------- |
1985 | // wxImageHandler | |
1986 | //----------------------------------------------------------------------------- | |
1987 | ||
63d963a1 | 1988 | IMPLEMENT_ABSTRACT_CLASS(wxImageHandler,wxObject) |
01111366 | 1989 | |
e02afc7a | 1990 | #if wxUSE_STREAMS |
700ec454 | 1991 | bool wxImageHandler::LoadFile( wxImage *WXUNUSED(image), wxInputStream& WXUNUSED(stream), bool WXUNUSED(verbose), int WXUNUSED(index) ) |
01111366 | 1992 | { |
70cd62e9 | 1993 | return false; |
01111366 RR |
1994 | } |
1995 | ||
deb2fec0 | 1996 | bool wxImageHandler::SaveFile( wxImage *WXUNUSED(image), wxOutputStream& WXUNUSED(stream), bool WXUNUSED(verbose) ) |
01111366 | 1997 | { |
70cd62e9 | 1998 | return false; |
01111366 | 1999 | } |
0828c087 | 2000 | |
649d13e8 | 2001 | int wxImageHandler::GetImageCount( wxInputStream& WXUNUSED(stream) ) |
700ec454 RR |
2002 | { |
2003 | return 1; | |
2004 | } | |
2005 | ||
0828c087 VS |
2006 | bool wxImageHandler::CanRead( const wxString& name ) |
2007 | { | |
0828c087 VS |
2008 | if (wxFileExists(name)) |
2009 | { | |
2010 | wxFileInputStream stream(name); | |
2011 | return CanRead(stream); | |
2012 | } | |
2013 | ||
39d16996 | 2014 | wxLogError( _("Can't check image format of file '%s': file does not exist."), name.c_str() ); |
0828c087 | 2015 | |
70cd62e9 | 2016 | return false; |
39d16996 VZ |
2017 | } |
2018 | ||
2019 | bool wxImageHandler::CallDoCanRead(wxInputStream& stream) | |
2020 | { | |
30984dea | 2021 | wxFileOffset posOld = stream.TellI(); |
39d16996 VZ |
2022 | if ( posOld == wxInvalidOffset ) |
2023 | { | |
2024 | // can't test unseekable stream | |
70cd62e9 | 2025 | return false; |
39d16996 VZ |
2026 | } |
2027 | ||
2028 | bool ok = DoCanRead(stream); | |
2029 | ||
2030 | // restore the old position to be able to test other formats and so on | |
2031 | if ( stream.SeekI(posOld) == wxInvalidOffset ) | |
2032 | { | |
2033 | wxLogDebug(_T("Failed to rewind the stream in wxImageHandler!")); | |
2034 | ||
2035 | // reading would fail anyhow as we're not at the right position | |
70cd62e9 | 2036 | return false; |
0828c087 | 2037 | } |
39d16996 VZ |
2038 | |
2039 | return ok; | |
0828c087 VS |
2040 | } |
2041 | ||
e02afc7a | 2042 | #endif // wxUSE_STREAMS |
01111366 | 2043 | |
487659e0 VZ |
2044 | // ---------------------------------------------------------------------------- |
2045 | // image histogram stuff | |
2046 | // ---------------------------------------------------------------------------- | |
2047 | ||
2048 | bool | |
2049 | wxImageHistogram::FindFirstUnusedColour(unsigned char *r, | |
2050 | unsigned char *g, | |
2051 | unsigned char *b, | |
2052 | unsigned char r2, | |
2053 | unsigned char b2, | |
2054 | unsigned char g2) const | |
2055 | { | |
2056 | unsigned long key = MakeKey(r2, g2, b2); | |
2057 | ||
2058 | while ( find(key) != end() ) | |
2059 | { | |
2060 | // color already used | |
2061 | r2++; | |
2062 | if ( r2 >= 255 ) | |
2063 | { | |
2064 | r2 = 0; | |
2065 | g2++; | |
2066 | if ( g2 >= 255 ) | |
2067 | { | |
2068 | g2 = 0; | |
2069 | b2++; | |
2070 | if ( b2 >= 255 ) | |
2071 | { | |
bc88f66f | 2072 | wxLogError(_("No unused colour in image.") ); |
70cd62e9 | 2073 | return false; |
487659e0 VZ |
2074 | } |
2075 | } | |
2076 | } | |
2077 | ||
2078 | key = MakeKey(r2, g2, b2); | |
2079 | } | |
2080 | ||
2081 | if ( r ) | |
2082 | *r = r2; | |
2083 | if ( g ) | |
2084 | *g = g2; | |
2085 | if ( b ) | |
2086 | *b = b2; | |
2087 | ||
70cd62e9 | 2088 | return true; |
487659e0 VZ |
2089 | } |
2090 | ||
2091 | bool | |
2092 | wxImage::FindFirstUnusedColour(unsigned char *r, | |
2093 | unsigned char *g, | |
2094 | unsigned char *b, | |
2095 | unsigned char r2, | |
2096 | unsigned char b2, | |
2097 | unsigned char g2) const | |
2098 | { | |
2099 | wxImageHistogram histogram; | |
2100 | ||
2101 | ComputeHistogram(histogram); | |
2102 | ||
2103 | return histogram.FindFirstUnusedColour(r, g, b, r2, g2, b2); | |
2104 | } | |
2105 | ||
2106 | ||
c9d01afd | 2107 | |
89d00456 GRG |
2108 | // GRG, Dic/99 |
2109 | // Counts and returns the number of different colours. Optionally stops | |
cc9f7d79 GRG |
2110 | // when it exceeds 'stopafter' different colours. This is useful, for |
2111 | // example, to see if the image can be saved as 8-bit (256 colour or | |
2112 | // less, in this case it would be invoked as CountColours(256)). Default | |
2113 | // value for stopafter is -1 (don't care). | |
89d00456 | 2114 | // |
e0a76d8d | 2115 | unsigned long wxImage::CountColours( unsigned long stopafter ) const |
89d00456 GRG |
2116 | { |
2117 | wxHashTable h; | |
ad30de59 | 2118 | wxObject dummy; |
33ac7e6f | 2119 | unsigned char r, g, b; |
eeca3a46 | 2120 | unsigned char *p; |
89d00456 GRG |
2121 | unsigned long size, nentries, key; |
2122 | ||
2123 | p = GetData(); | |
2124 | size = GetWidth() * GetHeight(); | |
2125 | nentries = 0; | |
2126 | ||
cc9f7d79 | 2127 | for (unsigned long j = 0; (j < size) && (nentries <= stopafter) ; j++) |
89d00456 GRG |
2128 | { |
2129 | r = *(p++); | |
2130 | g = *(p++); | |
2131 | b = *(p++); | |
487659e0 | 2132 | key = wxImageHistogram::MakeKey(r, g, b); |
89d00456 | 2133 | |
ad30de59 | 2134 | if (h.Get(key) == NULL) |
89d00456 | 2135 | { |
ad30de59 | 2136 | h.Put(key, &dummy); |
89d00456 GRG |
2137 | nentries++; |
2138 | } | |
2139 | } | |
2140 | ||
89d00456 GRG |
2141 | return nentries; |
2142 | } | |
2143 | ||
2144 | ||
e0a76d8d | 2145 | unsigned long wxImage::ComputeHistogram( wxImageHistogram &h ) const |
c9d01afd | 2146 | { |
487659e0 VZ |
2147 | unsigned char *p = GetData(); |
2148 | unsigned long nentries = 0; | |
952ae1e8 VS |
2149 | |
2150 | h.clear(); | |
c9d01afd | 2151 | |
487659e0 | 2152 | const unsigned long size = GetWidth() * GetHeight(); |
c9d01afd | 2153 | |
487659e0 VZ |
2154 | unsigned char r, g, b; |
2155 | for ( unsigned long n = 0; n < size; n++ ) | |
c9d01afd | 2156 | { |
487659e0 VZ |
2157 | r = *p++; |
2158 | g = *p++; | |
2159 | b = *p++; | |
2160 | ||
2161 | wxImageHistogramEntry& entry = h[wxImageHistogram::MakeKey(r, g, b)]; | |
c9d01afd | 2162 | |
952ae1e8 VS |
2163 | if ( entry.value++ == 0 ) |
2164 | entry.index = nentries++; | |
c9d01afd GRG |
2165 | } |
2166 | ||
2167 | return nentries; | |
2168 | } | |
2169 | ||
7a632f10 JS |
2170 | /* |
2171 | * Rotation code by Carlos Moreno | |
2172 | */ | |
2173 | ||
b5c91ac6 GRG |
2174 | // GRG: I've removed wxRotationPoint - we already have wxRealPoint which |
2175 | // does exactly the same thing. And I also got rid of wxRotationPixel | |
2176 | // bacause of potential problems in architectures where alignment | |
2177 | // is an issue, so I had to rewrite parts of the code. | |
7a632f10 | 2178 | |
7a632f10 JS |
2179 | static const double gs_Epsilon = 1e-10; |
2180 | ||
2181 | static inline int wxCint (double x) | |
2182 | { | |
2183 | return (x > 0) ? (int) (x + 0.5) : (int) (x - 0.5); | |
2184 | } | |
2185 | ||
2186 | ||
2187 | // Auxiliary function to rotate a point (x,y) with respect to point p0 | |
2188 | // make it inline and use a straight return to facilitate optimization | |
2189 | // also, the function receives the sine and cosine of the angle to avoid | |
2190 | // repeating the time-consuming calls to these functions -- sin/cos can | |
2191 | // be computed and stored in the calling function. | |
2192 | ||
b5c91ac6 | 2193 | inline wxRealPoint rotated_point (const wxRealPoint & p, double cos_angle, double sin_angle, const wxRealPoint & p0) |
7a632f10 | 2194 | { |
b5c91ac6 GRG |
2195 | return wxRealPoint (p0.x + (p.x - p0.x) * cos_angle - (p.y - p0.y) * sin_angle, |
2196 | p0.y + (p.y - p0.y) * cos_angle + (p.x - p0.x) * sin_angle); | |
7a632f10 JS |
2197 | } |
2198 | ||
b5c91ac6 | 2199 | inline wxRealPoint rotated_point (double x, double y, double cos_angle, double sin_angle, const wxRealPoint & p0) |
7a632f10 | 2200 | { |
b5c91ac6 | 2201 | return rotated_point (wxRealPoint(x,y), cos_angle, sin_angle, p0); |
7a632f10 JS |
2202 | } |
2203 | ||
2204 | wxImage wxImage::Rotate(double angle, const wxPoint & centre_of_rotation, bool interpolating, wxPoint * offset_after_rotation) const | |
2205 | { | |
7a632f10 JS |
2206 | int i; |
2207 | angle = -angle; // screen coordinates are a mirror image of "real" coordinates | |
b713f891 | 2208 | |
6408deed | 2209 | bool has_alpha = HasAlpha(); |
7a632f10 | 2210 | |
ad30de59 | 2211 | // Create pointer-based array to accelerate access to wxImage's data |
b5c91ac6 | 2212 | unsigned char ** data = new unsigned char * [GetHeight()]; |
b5c91ac6 | 2213 | data[0] = GetData(); |
b5c91ac6 GRG |
2214 | for (i = 1; i < GetHeight(); i++) |
2215 | data[i] = data[i - 1] + (3 * GetWidth()); | |
7a632f10 | 2216 | |
b713f891 | 2217 | // Same for alpha channel |
6408deed RR |
2218 | unsigned char ** alpha = NULL; |
2219 | if (has_alpha) | |
2220 | { | |
2221 | alpha = new unsigned char * [GetHeight()]; | |
2222 | alpha[0] = GetAlpha(); | |
2223 | for (i = 1; i < GetHeight(); i++) | |
2224 | alpha[i] = alpha[i - 1] + GetWidth(); | |
2225 | } | |
2226 | ||
b5c91ac6 | 2227 | // precompute coefficients for rotation formula |
ad30de59 | 2228 | // (sine and cosine of the angle) |
7a632f10 JS |
2229 | const double cos_angle = cos(angle); |
2230 | const double sin_angle = sin(angle); | |
2231 | ||
ad30de59 GRG |
2232 | // Create new Image to store the result |
2233 | // First, find rectangle that covers the rotated image; to do that, | |
2234 | // rotate the four corners | |
7a632f10 | 2235 | |
b5c91ac6 | 2236 | const wxRealPoint p0(centre_of_rotation.x, centre_of_rotation.y); |
7a632f10 | 2237 | |
b5c91ac6 GRG |
2238 | wxRealPoint p1 = rotated_point (0, 0, cos_angle, sin_angle, p0); |
2239 | wxRealPoint p2 = rotated_point (0, GetHeight(), cos_angle, sin_angle, p0); | |
2240 | wxRealPoint p3 = rotated_point (GetWidth(), 0, cos_angle, sin_angle, p0); | |
2241 | wxRealPoint p4 = rotated_point (GetWidth(), GetHeight(), cos_angle, sin_angle, p0); | |
7a632f10 | 2242 | |
4e115ed2 VZ |
2243 | int x1a = (int) floor (wxMin (wxMin(p1.x, p2.x), wxMin(p3.x, p4.x))); |
2244 | int y1a = (int) floor (wxMin (wxMin(p1.y, p2.y), wxMin(p3.y, p4.y))); | |
2245 | int x2a = (int) ceil (wxMax (wxMax(p1.x, p2.x), wxMax(p3.x, p4.x))); | |
2246 | int y2a = (int) ceil (wxMax (wxMax(p1.y, p2.y), wxMax(p3.y, p4.y))); | |
7a632f10 | 2247 | |
6408deed | 2248 | // Create rotated image |
4e115ed2 | 2249 | wxImage rotated (x2a - x1a + 1, y2a - y1a + 1, false); |
6408deed RR |
2250 | // With alpha channel |
2251 | if (has_alpha) | |
2252 | rotated.SetAlpha(); | |
7a632f10 JS |
2253 | |
2254 | if (offset_after_rotation != NULL) | |
2255 | { | |
4e115ed2 | 2256 | *offset_after_rotation = wxPoint (x1a, y1a); |
7a632f10 JS |
2257 | } |
2258 | ||
b5c91ac6 GRG |
2259 | // GRG: The rotated (destination) image is always accessed |
2260 | // sequentially, so there is no need for a pointer-based | |
2261 | // array here (and in fact it would be slower). | |
2262 | // | |
2263 | unsigned char * dst = rotated.GetData(); | |
b713f891 | 2264 | |
6408deed RR |
2265 | unsigned char * alpha_dst = NULL; |
2266 | if (has_alpha) | |
2267 | alpha_dst = rotated.GetAlpha(); | |
7a632f10 | 2268 | |
ad30de59 GRG |
2269 | // GRG: if the original image has a mask, use its RGB values |
2270 | // as the blank pixel, else, fall back to default (black). | |
2271 | // | |
b5c91ac6 GRG |
2272 | unsigned char blank_r = 0; |
2273 | unsigned char blank_g = 0; | |
2274 | unsigned char blank_b = 0; | |
ad30de59 GRG |
2275 | |
2276 | if (HasMask()) | |
2277 | { | |
b5c91ac6 GRG |
2278 | blank_r = GetMaskRed(); |
2279 | blank_g = GetMaskGreen(); | |
2280 | blank_b = GetMaskBlue(); | |
2281 | rotated.SetMaskColour( blank_r, blank_g, blank_b ); | |
ad30de59 GRG |
2282 | } |
2283 | ||
2284 | // Now, for each point of the rotated image, find where it came from, by | |
2285 | // performing an inverse rotation (a rotation of -angle) and getting the | |
2286 | // pixel at those coordinates | |
2287 | ||
b5c91ac6 GRG |
2288 | // GRG: I've taken the (interpolating) test out of the loops, so that |
2289 | // it is done only once, instead of repeating it for each pixel. | |
7a632f10 JS |
2290 | |
2291 | int x; | |
b5c91ac6 | 2292 | if (interpolating) |
7a632f10 JS |
2293 | { |
2294 | for (int y = 0; y < rotated.GetHeight(); y++) | |
2295 | { | |
b5c91ac6 | 2296 | for (x = 0; x < rotated.GetWidth(); x++) |
7a632f10 | 2297 | { |
4e115ed2 | 2298 | wxRealPoint src = rotated_point (x + x1a, y + y1a, cos_angle, -sin_angle, p0); |
b5c91ac6 | 2299 | |
f2506310 JS |
2300 | if (-0.25 < src.x && src.x < GetWidth() - 0.75 && |
2301 | -0.25 < src.y && src.y < GetHeight() - 0.75) | |
7a632f10 | 2302 | { |
ad30de59 GRG |
2303 | // interpolate using the 4 enclosing grid-points. Those |
2304 | // points can be obtained using floor and ceiling of the | |
2305 | // exact coordinates of the point | |
f2506310 JS |
2306 | int x1, y1, x2, y2; |
2307 | ||
2308 | if (0 < src.x && src.x < GetWidth() - 1) | |
2309 | { | |
2310 | x1 = wxCint(floor(src.x)); | |
2311 | x2 = wxCint(ceil(src.x)); | |
2312 | } | |
2313 | else // else means that x is near one of the borders (0 or width-1) | |
2314 | { | |
2315 | x1 = x2 = wxCint (src.x); | |
2316 | } | |
2317 | ||
2318 | if (0 < src.y && src.y < GetHeight() - 1) | |
2319 | { | |
2320 | y1 = wxCint(floor(src.y)); | |
2321 | y2 = wxCint(ceil(src.y)); | |
2322 | } | |
2323 | else | |
2324 | { | |
2325 | y1 = y2 = wxCint (src.y); | |
2326 | } | |
7a632f10 | 2327 | |
ad30de59 GRG |
2328 | // get four points and the distances (square of the distance, |
2329 | // for efficiency reasons) for the interpolation formula | |
b5c91ac6 GRG |
2330 | |
2331 | // GRG: Do not calculate the points until they are | |
2332 | // really needed -- this way we can calculate | |
2333 | // just one, instead of four, if d1, d2, d3 | |
2334 | // or d4 are < gs_Epsilon | |
7a632f10 JS |
2335 | |
2336 | const double d1 = (src.x - x1) * (src.x - x1) + (src.y - y1) * (src.y - y1); | |
2337 | const double d2 = (src.x - x2) * (src.x - x2) + (src.y - y1) * (src.y - y1); | |
2338 | const double d3 = (src.x - x2) * (src.x - x2) + (src.y - y2) * (src.y - y2); | |
2339 | const double d4 = (src.x - x1) * (src.x - x1) + (src.y - y2) * (src.y - y2); | |
2340 | ||
ad30de59 GRG |
2341 | // Now interpolate as a weighted average of the four surrounding |
2342 | // points, where the weights are the distances to each of those points | |
7a632f10 | 2343 | |
ad30de59 GRG |
2344 | // If the point is exactly at one point of the grid of the source |
2345 | // image, then don't interpolate -- just assign the pixel | |
7a632f10 | 2346 | |
06b466c7 | 2347 | if (d1 < gs_Epsilon) // d1,d2,d3,d4 are positive -- no need for abs() |
7a632f10 | 2348 | { |
b5c91ac6 GRG |
2349 | unsigned char *p = data[y1] + (3 * x1); |
2350 | *(dst++) = *(p++); | |
2351 | *(dst++) = *(p++); | |
6408deed | 2352 | *(dst++) = *p; |
b713f891 | 2353 | |
6408deed | 2354 | if (has_alpha) |
4e115ed2 | 2355 | *(alpha_dst++) = *(alpha[y1] + x1); |
7a632f10 JS |
2356 | } |
2357 | else if (d2 < gs_Epsilon) | |
2358 | { | |
b5c91ac6 GRG |
2359 | unsigned char *p = data[y1] + (3 * x2); |
2360 | *(dst++) = *(p++); | |
2361 | *(dst++) = *(p++); | |
6408deed | 2362 | *(dst++) = *p; |
b713f891 | 2363 | |
6408deed | 2364 | if (has_alpha) |
4e115ed2 | 2365 | *(alpha_dst++) = *(alpha[y1] + x2); |
7a632f10 JS |
2366 | } |
2367 | else if (d3 < gs_Epsilon) | |
2368 | { | |
b5c91ac6 GRG |
2369 | unsigned char *p = data[y2] + (3 * x2); |
2370 | *(dst++) = *(p++); | |
2371 | *(dst++) = *(p++); | |
6408deed | 2372 | *(dst++) = *p; |
b713f891 | 2373 | |
6408deed | 2374 | if (has_alpha) |
4e115ed2 | 2375 | *(alpha_dst++) = *(alpha[y2] + x2); |
7a632f10 JS |
2376 | } |
2377 | else if (d4 < gs_Epsilon) | |
2378 | { | |
b5c91ac6 GRG |
2379 | unsigned char *p = data[y2] + (3 * x1); |
2380 | *(dst++) = *(p++); | |
2381 | *(dst++) = *(p++); | |
6408deed | 2382 | *(dst++) = *p; |
b713f891 | 2383 | |
6408deed | 2384 | if (has_alpha) |
4e115ed2 | 2385 | *(alpha_dst++) = *(alpha[y2] + x1); |
7a632f10 JS |
2386 | } |
2387 | else | |
2388 | { | |
06b466c7 | 2389 | // weights for the weighted average are proportional to the inverse of the distance |
b5c91ac6 GRG |
2390 | unsigned char *v1 = data[y1] + (3 * x1); |
2391 | unsigned char *v2 = data[y1] + (3 * x2); | |
2392 | unsigned char *v3 = data[y2] + (3 * x2); | |
2393 | unsigned char *v4 = data[y2] + (3 * x1); | |
2394 | ||
06b466c7 VZ |
2395 | const double w1 = 1/d1, w2 = 1/d2, w3 = 1/d3, w4 = 1/d4; |
2396 | ||
b5c91ac6 GRG |
2397 | // GRG: Unrolled. |
2398 | ||
2399 | *(dst++) = (unsigned char) | |
2400 | ( (w1 * *(v1++) + w2 * *(v2++) + | |
2401 | w3 * *(v3++) + w4 * *(v4++)) / | |
2402 | (w1 + w2 + w3 + w4) ); | |
2403 | *(dst++) = (unsigned char) | |
2404 | ( (w1 * *(v1++) + w2 * *(v2++) + | |
2405 | w3 * *(v3++) + w4 * *(v4++)) / | |
2406 | (w1 + w2 + w3 + w4) ); | |
2407 | *(dst++) = (unsigned char) | |
999836aa VZ |
2408 | ( (w1 * *v1 + w2 * *v2 + |
2409 | w3 * *v3 + w4 * *v4) / | |
b5c91ac6 | 2410 | (w1 + w2 + w3 + w4) ); |
b713f891 | 2411 | |
6408deed RR |
2412 | if (has_alpha) |
2413 | { | |
4e115ed2 VZ |
2414 | v1 = alpha[y1] + (x1); |
2415 | v2 = alpha[y1] + (x2); | |
2416 | v3 = alpha[y2] + (x2); | |
2417 | v4 = alpha[y2] + (x1); | |
6408deed RR |
2418 | |
2419 | *(alpha_dst++) = (unsigned char) | |
2420 | ( (w1 * *v1 + w2 * *v2 + | |
2421 | w3 * *v3 + w4 * *v4) / | |
2422 | (w1 + w2 + w3 + w4) ); | |
2423 | } | |
7a632f10 JS |
2424 | } |
2425 | } | |
2426 | else | |
2427 | { | |
b5c91ac6 GRG |
2428 | *(dst++) = blank_r; |
2429 | *(dst++) = blank_g; | |
2430 | *(dst++) = blank_b; | |
b713f891 | 2431 | |
6408deed RR |
2432 | if (has_alpha) |
2433 | *(alpha_dst++) = 0; | |
7a632f10 JS |
2434 | } |
2435 | } | |
b5c91ac6 GRG |
2436 | } |
2437 | } | |
2438 | else // not interpolating | |
2439 | { | |
2440 | for (int y = 0; y < rotated.GetHeight(); y++) | |
2441 | { | |
2442 | for (x = 0; x < rotated.GetWidth(); x++) | |
7a632f10 | 2443 | { |
4e115ed2 | 2444 | wxRealPoint src = rotated_point (x + x1a, y + y1a, cos_angle, -sin_angle, p0); |
b5c91ac6 GRG |
2445 | |
2446 | const int xs = wxCint (src.x); // wxCint rounds to the | |
457e6c54 | 2447 | const int ys = wxCint (src.y); // closest integer |
7a632f10 | 2448 | |
b5c91ac6 GRG |
2449 | if (0 <= xs && xs < GetWidth() && |
2450 | 0 <= ys && ys < GetHeight()) | |
7a632f10 | 2451 | { |
b5c91ac6 GRG |
2452 | unsigned char *p = data[ys] + (3 * xs); |
2453 | *(dst++) = *(p++); | |
2454 | *(dst++) = *(p++); | |
999836aa | 2455 | *(dst++) = *p; |
b713f891 | 2456 | |
6408deed | 2457 | if (has_alpha) |
4e115ed2 | 2458 | *(alpha_dst++) = *(alpha[ys] + (xs)); |
7a632f10 JS |
2459 | } |
2460 | else | |
2461 | { | |
b5c91ac6 GRG |
2462 | *(dst++) = blank_r; |
2463 | *(dst++) = blank_g; | |
2464 | *(dst++) = blank_b; | |
b713f891 | 2465 | |
6408deed RR |
2466 | if (has_alpha) |
2467 | *(alpha_dst++) = 255; | |
7a632f10 JS |
2468 | } |
2469 | } | |
2470 | } | |
2471 | } | |
2472 | ||
4aff28fc | 2473 | delete [] data; |
b713f891 | 2474 | |
6408deed RR |
2475 | if (has_alpha) |
2476 | delete [] alpha; | |
4aff28fc | 2477 | |
7a632f10 JS |
2478 | return rotated; |
2479 | } | |
c9d01afd | 2480 | |
ef8f37e0 VS |
2481 | |
2482 | ||
2483 | ||
2484 | ||
2485 | // A module to allow wxImage initialization/cleanup | |
2486 | // without calling these functions from app.cpp or from | |
2487 | // the user's application. | |
2488 | ||
2489 | class wxImageModule: public wxModule | |
2490 | { | |
2491 | DECLARE_DYNAMIC_CLASS(wxImageModule) | |
2492 | public: | |
2493 | wxImageModule() {} | |
70cd62e9 | 2494 | bool OnInit() { wxImage::InitStandardHandlers(); return true; }; |
ef8f37e0 VS |
2495 | void OnExit() { wxImage::CleanUpHandlers(); }; |
2496 | }; | |
2497 | ||
2498 | IMPLEMENT_DYNAMIC_CLASS(wxImageModule, wxModule) | |
2499 | ||
2500 | ||
c96ea657 | 2501 | #endif // wxUSE_IMAGE |