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