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