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