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