]>
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 | ||
0bca0373 WS |
19 | #include "wx/image.h" |
20 | ||
8898456d WS |
21 | #ifndef WX_PRECOMP |
22 | #include "wx/log.h" | |
32d4c30a | 23 | #include "wx/hash.h" |
de6185e2 | 24 | #include "wx/utils.h" |
18680f86 | 25 | #include "wx/math.h" |
02761f6c | 26 | #include "wx/module.h" |
5ff14574 PC |
27 | #include "wx/palette.h" |
28 | #include "wx/intl.h" | |
8898456d WS |
29 | #endif |
30 | ||
dc86cb34 | 31 | #include "wx/filefn.h" |
3d05544e | 32 | #include "wx/wfstream.h" |
452418c4 | 33 | #include "wx/xpmdecod.h" |
cad61c3e | 34 | |
58a8ab88 JS |
35 | // For memcpy |
36 | #include <string.h> | |
37 | ||
6632225c VS |
38 | // make the code compile with either wxFile*Stream or wxFFile*Stream: |
39 | #define HAS_FILE_STREAMS (wxUSE_STREAMS && (wxUSE_FILE || wxUSE_FFILE)) | |
40 | ||
41 | #if HAS_FILE_STREAMS | |
8d6c5e4f | 42 | #if wxUSE_FFILE |
6632225c VS |
43 | typedef wxFFileInputStream wxImageFileInputStream; |
44 | typedef wxFFileOutputStream wxImageFileOutputStream; | |
8d6c5e4f VS |
45 | #elif wxUSE_FILE |
46 | typedef wxFileInputStream wxImageFileInputStream; | |
47 | typedef wxFileOutputStream wxImageFileOutputStream; | |
6632225c VS |
48 | #endif // wxUSE_FILE/wxUSE_FFILE |
49 | #endif // HAS_FILE_STREAMS | |
50 | ||
6f5d7825 | 51 | #if wxUSE_VARIANT |
55ccdb93 | 52 | IMPLEMENT_VARIANT_OBJECT_EXPORTED_SHALLOWCMP(wxImage,WXDLLEXPORT) |
6f5d7825 RR |
53 | #endif |
54 | ||
01111366 RR |
55 | //----------------------------------------------------------------------------- |
56 | // wxImage | |
57 | //----------------------------------------------------------------------------- | |
58 | ||
59 | class wxImageRefData: public wxObjectRefData | |
60 | { | |
fd0eed64 | 61 | public: |
edccf428 | 62 | wxImageRefData(); |
487659e0 | 63 | virtual ~wxImageRefData(); |
c7abc967 | 64 | |
dbda9e86 JS |
65 | int m_width; |
66 | int m_height; | |
591d3fa2 | 67 | wxBitmapType m_type; |
dbda9e86 | 68 | unsigned char *m_data; |
487659e0 | 69 | |
dbda9e86 JS |
70 | bool m_hasMask; |
71 | unsigned char m_maskRed,m_maskGreen,m_maskBlue; | |
487659e0 VZ |
72 | |
73 | // alpha channel data, may be NULL for the formats without alpha support | |
74 | unsigned char *m_alpha; | |
75 | ||
dbda9e86 | 76 | bool m_ok; |
d2502f14 VZ |
77 | |
78 | // if true, m_data is pointer to static data and shouldn't be freed | |
f6bcfd97 | 79 | bool m_static; |
487659e0 | 80 | |
d2502f14 VZ |
81 | // same as m_static but for m_alpha |
82 | bool m_staticAlpha; | |
83 | ||
d275c7eb | 84 | #if wxUSE_PALETTE |
5e5437e0 | 85 | wxPalette m_palette; |
d275c7eb | 86 | #endif // wxUSE_PALETTE |
487659e0 | 87 | |
5e5437e0 JS |
88 | wxArrayString m_optionNames; |
89 | wxArrayString m_optionValues; | |
22f3361e VZ |
90 | |
91 | DECLARE_NO_COPY_CLASS(wxImageRefData) | |
01111366 RR |
92 | }; |
93 | ||
edccf428 | 94 | wxImageRefData::wxImageRefData() |
01111366 | 95 | { |
fd0eed64 RR |
96 | m_width = 0; |
97 | m_height = 0; | |
591d3fa2 | 98 | m_type = wxBITMAP_TYPE_INVALID; |
487659e0 VZ |
99 | m_data = |
100 | m_alpha = (unsigned char *) NULL; | |
101 | ||
fd0eed64 RR |
102 | m_maskRed = 0; |
103 | m_maskGreen = 0; | |
104 | m_maskBlue = 0; | |
70cd62e9 | 105 | m_hasMask = false; |
487659e0 | 106 | |
70cd62e9 | 107 | m_ok = false; |
d2502f14 VZ |
108 | m_static = |
109 | m_staticAlpha = false; | |
01111366 RR |
110 | } |
111 | ||
edccf428 | 112 | wxImageRefData::~wxImageRefData() |
01111366 | 113 | { |
d2502f14 | 114 | if ( !m_static ) |
58c837a4 | 115 | free( m_data ); |
d2502f14 | 116 | if ( !m_staticAlpha ) |
4ea56379 | 117 | free( m_alpha ); |
01111366 RR |
118 | } |
119 | ||
120 | wxList wxImage::sm_handlers; | |
121 | ||
fec19ea9 VS |
122 | wxImage wxNullImage; |
123 | ||
01111366 RR |
124 | //----------------------------------------------------------------------------- |
125 | ||
a0f81e9f | 126 | #define M_IMGDATA wx_static_cast(wxImageRefData*, m_refData) |
01111366 | 127 | |
5e5437e0 | 128 | IMPLEMENT_DYNAMIC_CLASS(wxImage, wxObject) |
01111366 | 129 | |
ff865c13 | 130 | wxImage::wxImage( int width, int height, bool clear ) |
01111366 | 131 | { |
ff865c13 | 132 | Create( width, height, clear ); |
01111366 RR |
133 | } |
134 | ||
f6bcfd97 BP |
135 | wxImage::wxImage( int width, int height, unsigned char* data, bool static_data ) |
136 | { | |
137 | Create( width, height, data, static_data ); | |
138 | } | |
139 | ||
4ea56379 RR |
140 | wxImage::wxImage( int width, int height, unsigned char* data, unsigned char* alpha, bool static_data ) |
141 | { | |
142 | Create( width, height, data, alpha, static_data ); | |
143 | } | |
144 | ||
e98e625c | 145 | wxImage::wxImage( const wxString& name, wxBitmapType type, int index ) |
01111366 | 146 | { |
60d43ad8 | 147 | LoadFile( name, type, index ); |
01111366 RR |
148 | } |
149 | ||
60d43ad8 | 150 | wxImage::wxImage( const wxString& name, const wxString& mimetype, int index ) |
9e9ee68e | 151 | { |
60d43ad8 | 152 | LoadFile( name, mimetype, index ); |
9e9ee68e VS |
153 | } |
154 | ||
e02afc7a | 155 | #if wxUSE_STREAMS |
e98e625c | 156 | wxImage::wxImage( wxInputStream& stream, wxBitmapType type, int index ) |
3d05544e | 157 | { |
60d43ad8 | 158 | LoadFile( stream, type, index ); |
3d05544e | 159 | } |
9e9ee68e | 160 | |
60d43ad8 | 161 | wxImage::wxImage( wxInputStream& stream, const wxString& mimetype, int index ) |
9e9ee68e | 162 | { |
60d43ad8 | 163 | LoadFile( stream, mimetype, index ); |
9e9ee68e | 164 | } |
e02afc7a | 165 | #endif // wxUSE_STREAMS |
3d05544e | 166 | |
452418c4 | 167 | wxImage::wxImage(const char* const* xpmData) |
cad61c3e JS |
168 | { |
169 | Create(xpmData); | |
170 | } | |
171 | ||
452418c4 | 172 | bool wxImage::Create(const char* const* xpmData) |
cad61c3e JS |
173 | { |
174 | #if wxUSE_XPM | |
175 | UnRef(); | |
e9b64c5e | 176 | |
cad61c3e JS |
177 | wxXPMDecoder decoder; |
178 | (*this) = decoder.ReadData(xpmData); | |
179 | return Ok(); | |
180 | #else | |
181 | return false; | |
182 | #endif | |
183 | } | |
184 | ||
aaa97828 | 185 | bool wxImage::Create( int width, int height, bool clear ) |
01111366 | 186 | { |
aadaf841 GRG |
187 | UnRef(); |
188 | ||
fd0eed64 | 189 | m_refData = new wxImageRefData(); |
c7abc967 | 190 | |
fd0eed64 | 191 | M_IMGDATA->m_data = (unsigned char *) malloc( width*height*3 ); |
aaa97828 | 192 | if (!M_IMGDATA->m_data) |
fd0eed64 RR |
193 | { |
194 | UnRef(); | |
70cd62e9 | 195 | return false; |
fd0eed64 | 196 | } |
aaa97828 VZ |
197 | |
198 | if (clear) | |
199 | memset(M_IMGDATA->m_data, 0, width*height*3); | |
200 | ||
201 | M_IMGDATA->m_width = width; | |
202 | M_IMGDATA->m_height = height; | |
70cd62e9 | 203 | M_IMGDATA->m_ok = true; |
aaa97828 | 204 | |
70cd62e9 | 205 | return true; |
01111366 RR |
206 | } |
207 | ||
aaa97828 | 208 | bool wxImage::Create( int width, int height, unsigned char* data, bool static_data ) |
f6bcfd97 BP |
209 | { |
210 | UnRef(); | |
211 | ||
70cd62e9 | 212 | wxCHECK_MSG( data, false, _T("NULL data in wxImage::Create") ); |
aaa97828 | 213 | |
f6bcfd97 BP |
214 | m_refData = new wxImageRefData(); |
215 | ||
216 | M_IMGDATA->m_data = data; | |
aaa97828 VZ |
217 | M_IMGDATA->m_width = width; |
218 | M_IMGDATA->m_height = height; | |
70cd62e9 | 219 | M_IMGDATA->m_ok = true; |
aaa97828 VZ |
220 | M_IMGDATA->m_static = static_data; |
221 | ||
70cd62e9 | 222 | return true; |
f6bcfd97 BP |
223 | } |
224 | ||
4ea56379 RR |
225 | bool wxImage::Create( int width, int height, unsigned char* data, unsigned char* alpha, bool static_data ) |
226 | { | |
227 | UnRef(); | |
228 | ||
229 | wxCHECK_MSG( data, false, _T("NULL data in wxImage::Create") ); | |
230 | ||
231 | m_refData = new wxImageRefData(); | |
232 | ||
233 | M_IMGDATA->m_data = data; | |
234 | M_IMGDATA->m_alpha = alpha; | |
235 | M_IMGDATA->m_width = width; | |
236 | M_IMGDATA->m_height = height; | |
237 | M_IMGDATA->m_ok = true; | |
238 | M_IMGDATA->m_static = static_data; | |
239 | ||
240 | return true; | |
241 | } | |
242 | ||
01111366 RR |
243 | void wxImage::Destroy() |
244 | { | |
fd0eed64 | 245 | UnRef(); |
01111366 RR |
246 | } |
247 | ||
a0f81e9f | 248 | wxObjectRefData* wxImage::CreateRefData() const |
f6bcfd97 | 249 | { |
a0f81e9f PC |
250 | return new wxImageRefData; |
251 | } | |
051924b8 | 252 | |
a0f81e9f PC |
253 | wxObjectRefData* wxImage::CloneRefData(const wxObjectRefData* that) const |
254 | { | |
255 | const wxImageRefData* refData = wx_static_cast(const wxImageRefData*, that); | |
256 | wxCHECK_MSG(refData->m_ok, NULL, wxT("invalid image") ); | |
051924b8 | 257 | |
a0f81e9f PC |
258 | wxImageRefData* refData_new = new wxImageRefData; |
259 | refData_new->m_width = refData->m_width; | |
260 | refData_new->m_height = refData->m_height; | |
261 | refData_new->m_maskRed = refData->m_maskRed; | |
262 | refData_new->m_maskGreen = refData->m_maskGreen; | |
263 | refData_new->m_maskBlue = refData->m_maskBlue; | |
264 | refData_new->m_hasMask = refData->m_hasMask; | |
265 | refData_new->m_ok = true; | |
266 | unsigned size = unsigned(refData->m_width) * unsigned(refData->m_height); | |
267 | if (refData->m_alpha != NULL) | |
a1cd9564 | 268 | { |
a0f81e9f PC |
269 | refData_new->m_alpha = (unsigned char*)malloc(size); |
270 | memcpy(refData_new->m_alpha, refData->m_alpha, size); | |
a1cd9564 | 271 | } |
a0f81e9f PC |
272 | size *= 3; |
273 | refData_new->m_data = (unsigned char*)malloc(size); | |
274 | memcpy(refData_new->m_data, refData->m_data, size); | |
275 | #if wxUSE_PALETTE | |
276 | refData_new->m_palette = refData->m_palette; | |
277 | #endif | |
278 | refData_new->m_optionNames = refData->m_optionNames; | |
279 | refData_new->m_optionValues = refData->m_optionValues; | |
280 | return refData_new; | |
281 | } | |
051924b8 | 282 | |
a0f81e9f PC |
283 | wxImage wxImage::Copy() const |
284 | { | |
285 | wxImage image; | |
286 | ||
287 | wxCHECK_MSG( Ok(), image, wxT("invalid image") ); | |
288 | ||
289 | image.m_refData = CloneRefData(m_refData); | |
d8692f2b | 290 | |
f6bcfd97 BP |
291 | return image; |
292 | } | |
293 | ||
fd9655ad SC |
294 | wxImage wxImage::ShrinkBy( int xFactor , int yFactor ) const |
295 | { | |
296 | if( xFactor == 1 && yFactor == 1 ) | |
a0f81e9f | 297 | return *this; |
7beb59f3 | 298 | |
fd9655ad SC |
299 | wxImage image; |
300 | ||
301 | wxCHECK_MSG( Ok(), image, wxT("invalid image") ); | |
302 | ||
303 | // can't scale to/from 0 size | |
304 | wxCHECK_MSG( (xFactor > 0) && (yFactor > 0), image, | |
305 | wxT("invalid new image size") ); | |
306 | ||
307 | long old_height = M_IMGDATA->m_height, | |
308 | old_width = M_IMGDATA->m_width; | |
7beb59f3 | 309 | |
fd9655ad SC |
310 | wxCHECK_MSG( (old_height > 0) && (old_width > 0), image, |
311 | wxT("invalid old image size") ); | |
312 | ||
313 | long width = old_width / xFactor ; | |
314 | long height = old_height / yFactor ; | |
315 | ||
ff865c13 | 316 | image.Create( width, height, false ); |
fd9655ad SC |
317 | |
318 | char unsigned *data = image.GetData(); | |
319 | ||
320 | wxCHECK_MSG( data, image, wxT("unable to create image") ); | |
321 | ||
322 | bool hasMask = false ; | |
323 | unsigned char maskRed = 0; | |
324 | unsigned char maskGreen = 0; | |
325 | unsigned char maskBlue =0 ; | |
cd0bbd03 SC |
326 | |
327 | unsigned char *source_data = M_IMGDATA->m_data; | |
328 | unsigned char *target_data = data; | |
329 | unsigned char *source_alpha = 0 ; | |
330 | unsigned char *target_alpha = 0 ; | |
fd9655ad SC |
331 | if (M_IMGDATA->m_hasMask) |
332 | { | |
333 | hasMask = true ; | |
334 | maskRed = M_IMGDATA->m_maskRed; | |
335 | maskGreen = M_IMGDATA->m_maskGreen; | |
336 | maskBlue =M_IMGDATA->m_maskBlue ; | |
7beb59f3 | 337 | |
fd9655ad SC |
338 | image.SetMaskColour( M_IMGDATA->m_maskRed, |
339 | M_IMGDATA->m_maskGreen, | |
340 | M_IMGDATA->m_maskBlue ); | |
341 | } | |
cd0bbd03 SC |
342 | else |
343 | { | |
344 | source_alpha = M_IMGDATA->m_alpha ; | |
345 | if ( source_alpha ) | |
346 | { | |
347 | image.SetAlpha() ; | |
348 | target_alpha = image.GetAlpha() ; | |
349 | } | |
350 | } | |
7beb59f3 | 351 | |
fd9655ad SC |
352 | for (long y = 0; y < height; y++) |
353 | { | |
fd9655ad SC |
354 | for (long x = 0; x < width; x++) |
355 | { | |
356 | unsigned long avgRed = 0 ; | |
357 | unsigned long avgGreen = 0; | |
358 | unsigned long avgBlue = 0; | |
cd0bbd03 | 359 | unsigned long avgAlpha = 0 ; |
fd9655ad SC |
360 | unsigned long counter = 0 ; |
361 | // determine average | |
362 | for ( int y1 = 0 ; y1 < yFactor ; ++y1 ) | |
363 | { | |
364 | long y_offset = (y * yFactor + y1) * old_width; | |
365 | for ( int x1 = 0 ; x1 < xFactor ; ++x1 ) | |
366 | { | |
367 | unsigned char *pixel = source_data + 3 * ( y_offset + x * xFactor + x1 ) ; | |
368 | unsigned char red = pixel[0] ; | |
369 | unsigned char green = pixel[1] ; | |
370 | unsigned char blue = pixel[2] ; | |
cd0bbd03 SC |
371 | unsigned char alpha = 255 ; |
372 | if ( source_alpha ) | |
373 | alpha = *(source_alpha + y_offset + x * xFactor + x1) ; | |
fd9655ad SC |
374 | if ( !hasMask || red != maskRed || green != maskGreen || blue != maskBlue ) |
375 | { | |
cd0bbd03 SC |
376 | if ( alpha > 0 ) |
377 | { | |
378 | avgRed += red ; | |
379 | avgGreen += green ; | |
380 | avgBlue += blue ; | |
381 | } | |
382 | avgAlpha += alpha ; | |
fd9655ad SC |
383 | counter++ ; |
384 | } | |
385 | } | |
386 | } | |
387 | if ( counter == 0 ) | |
388 | { | |
389 | *(target_data++) = M_IMGDATA->m_maskRed ; | |
390 | *(target_data++) = M_IMGDATA->m_maskGreen ; | |
391 | *(target_data++) = M_IMGDATA->m_maskBlue ; | |
392 | } | |
393 | else | |
394 | { | |
cd0bbd03 SC |
395 | if ( source_alpha ) |
396 | *(target_alpha++) = (unsigned char)(avgAlpha / counter ) ; | |
646c4aeb VZ |
397 | *(target_data++) = (unsigned char)(avgRed / counter); |
398 | *(target_data++) = (unsigned char)(avgGreen / counter); | |
399 | *(target_data++) = (unsigned char)(avgBlue / counter); | |
fd9655ad SC |
400 | } |
401 | } | |
402 | } | |
403 | ||
8180d40b | 404 | // In case this is a cursor, make sure the hotspot is scaled accordingly: |
fd9655ad SC |
405 | if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X) ) |
406 | image.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X, | |
407 | (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X))/xFactor); | |
408 | if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y) ) | |
409 | image.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y, | |
410 | (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y))/yFactor); | |
411 | ||
412 | return image; | |
413 | } | |
414 | ||
07aaa1a4 | 415 | wxImage wxImage::Scale( int width, int height, int quality ) const |
4bc67cc5 RR |
416 | { |
417 | wxImage image; | |
c7abc967 | 418 | |
223d09f6 | 419 | wxCHECK_MSG( Ok(), image, wxT("invalid image") ); |
c7abc967 | 420 | |
6721fc38 VZ |
421 | // can't scale to/from 0 size |
422 | wxCHECK_MSG( (width > 0) && (height > 0), image, | |
423 | wxT("invalid new image size") ); | |
424 | ||
425 | long old_height = M_IMGDATA->m_height, | |
426 | old_width = M_IMGDATA->m_width; | |
427 | wxCHECK_MSG( (old_height > 0) && (old_width > 0), image, | |
428 | wxT("invalid old image size") ); | |
c7abc967 | 429 | |
30f27c00 VZ |
430 | // If the image's new width and height are the same as the original, no |
431 | // need to waste time or CPU cycles | |
432 | if ( old_width == width && old_height == height ) | |
07aaa1a4 RR |
433 | return *this; |
434 | ||
30f27c00 VZ |
435 | // Scale the image (...or more appropriately, resample the image) using |
436 | // either the high-quality or normal method as specified | |
437 | if ( quality == wxIMAGE_QUALITY_HIGH ) | |
fd9655ad | 438 | { |
07aaa1a4 | 439 | // We need to check whether we are downsampling or upsampling the image |
30f27c00 | 440 | if ( width < old_width && height < old_height ) |
07aaa1a4 RR |
441 | { |
442 | // Downsample the image using the box averaging method for best results | |
443 | image = ResampleBox(width, height); | |
444 | } | |
445 | else | |
446 | { | |
30f27c00 VZ |
447 | // For upsampling or other random/wierd image dimensions we'll use |
448 | // a bicubic b-spline scaling method | |
07aaa1a4 RR |
449 | image = ResampleBicubic(width, height); |
450 | } | |
fd9655ad | 451 | } |
07aaa1a4 RR |
452 | else // Default scaling method == simple pixel replication |
453 | { | |
454 | if ( old_width % width == 0 && old_width >= width && | |
455 | old_height % height == 0 && old_height >= height ) | |
456 | { | |
457 | return ShrinkBy( old_width / width , old_height / height ) ; | |
458 | } | |
459 | image.Create( width, height, false ); | |
c7abc967 | 460 | |
07aaa1a4 | 461 | unsigned char *data = image.GetData(); |
c7abc967 | 462 | |
07aaa1a4 | 463 | wxCHECK_MSG( data, image, wxT("unable to create image") ); |
c7abc967 | 464 | |
07aaa1a4 RR |
465 | unsigned char *source_data = M_IMGDATA->m_data; |
466 | unsigned char *target_data = data; | |
467 | unsigned char *source_alpha = 0 ; | |
468 | unsigned char *target_alpha = 0 ; | |
e9b64c5e | 469 | |
1fc1e6af | 470 | if ( !M_IMGDATA->m_hasMask ) |
07aaa1a4 RR |
471 | { |
472 | source_alpha = M_IMGDATA->m_alpha ; | |
473 | if ( source_alpha ) | |
474 | { | |
475 | image.SetAlpha() ; | |
476 | target_alpha = image.GetAlpha() ; | |
477 | } | |
8d3b6b8a | 478 | } |
c7abc967 | 479 | |
07aaa1a4 RR |
480 | long x_delta = (old_width<<16) / width; |
481 | long y_delta = (old_height<<16) / height; | |
c7abc967 | 482 | |
07aaa1a4 | 483 | unsigned char* dest_pixel = target_data; |
6721fc38 | 484 | |
07aaa1a4 RR |
485 | long y = 0; |
486 | for ( long j = 0; j < height; j++ ) | |
ff865c13 | 487 | { |
07aaa1a4 RR |
488 | unsigned char* src_line = &source_data[(y>>16)*old_width*3]; |
489 | unsigned char* src_alpha_line = source_alpha ? &source_alpha[(y>>16)*old_width] : 0 ; | |
e9b64c5e | 490 | |
07aaa1a4 RR |
491 | long x = 0; |
492 | for ( long i = 0; i < width; i++ ) | |
493 | { | |
494 | unsigned char* src_pixel = &src_line[(x>>16)*3]; | |
495 | unsigned char* src_alpha_pixel = source_alpha ? &src_alpha_line[(x>>16)] : 0 ; | |
496 | dest_pixel[0] = src_pixel[0]; | |
497 | dest_pixel[1] = src_pixel[1]; | |
498 | dest_pixel[2] = src_pixel[2]; | |
499 | dest_pixel += 3; | |
500 | if ( source_alpha ) | |
501 | *(target_alpha++) = *src_alpha_pixel ; | |
502 | x += x_delta; | |
503 | } | |
ff865c13 | 504 | |
07aaa1a4 RR |
505 | y += y_delta; |
506 | } | |
eeca3a46 | 507 | } |
36aac195 | 508 | |
1fc1e6af | 509 | // If the original image has a mask, apply the mask to the new image |
e8a8d0dc | 510 | if (M_IMGDATA->m_hasMask) |
1fc1e6af RR |
511 | { |
512 | image.SetMaskColour( M_IMGDATA->m_maskRed, | |
513 | M_IMGDATA->m_maskGreen, | |
514 | M_IMGDATA->m_maskBlue ); | |
515 | } | |
516 | ||
8180d40b | 517 | // In case this is a cursor, make sure the hotspot is scaled accordingly: |
fd94e8aa VS |
518 | if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X) ) |
519 | image.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X, | |
520 | (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X)*width)/old_width); | |
521 | if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y) ) | |
522 | image.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y, | |
523 | (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y)*height)/old_height); | |
c7abc967 | 524 | |
4bc67cc5 RR |
525 | return image; |
526 | } | |
4698648f | 527 | |
07aaa1a4 RR |
528 | wxImage wxImage::ResampleBox(int width, int height) const |
529 | { | |
30f27c00 VZ |
530 | // This function implements a simple pre-blur/box averaging method for |
531 | // downsampling that gives reasonably smooth results To scale the image | |
532 | // down we will need to gather a grid of pixels of the size of the scale | |
533 | // factor in each direction and then do an averaging of the pixels. | |
07aaa1a4 RR |
534 | |
535 | wxImage ret_image(width, height, false); | |
536 | ||
30f27c00 VZ |
537 | const double scale_factor_x = double(M_IMGDATA->m_width) / width; |
538 | const double scale_factor_y = double(M_IMGDATA->m_height) / height; | |
539 | ||
540 | const int scale_factor_x_2 = (int)(scale_factor_x / 2); | |
541 | const int scale_factor_y_2 = (int)(scale_factor_y / 2); | |
07aaa1a4 | 542 | |
c7a284c7 VZ |
543 | unsigned char* src_data = M_IMGDATA->m_data; |
544 | unsigned char* src_alpha = M_IMGDATA->m_alpha; | |
07aaa1a4 RR |
545 | unsigned char* dst_data = ret_image.GetData(); |
546 | unsigned char* dst_alpha = NULL; | |
547 | ||
30f27c00 | 548 | if ( src_alpha ) |
07aaa1a4 RR |
549 | { |
550 | ret_image.SetAlpha(); | |
551 | dst_alpha = ret_image.GetAlpha(); | |
552 | } | |
553 | ||
30f27c00 | 554 | int averaged_pixels, src_pixel_index; |
07aaa1a4 RR |
555 | double sum_r, sum_g, sum_b, sum_a; |
556 | ||
30f27c00 | 557 | for ( int y = 0; y < height; y++ ) // Destination image - Y direction |
07aaa1a4 RR |
558 | { |
559 | // Source pixel in the Y direction | |
30f27c00 | 560 | int src_y = (int)(y * scale_factor_y); |
07aaa1a4 | 561 | |
30f27c00 | 562 | for ( int x = 0; x < width; x++ ) // Destination image - X direction |
07aaa1a4 RR |
563 | { |
564 | // Source pixel in the X direction | |
30f27c00 | 565 | int src_x = (int)(x * scale_factor_x); |
07aaa1a4 RR |
566 | |
567 | // Box of pixels to average | |
568 | averaged_pixels = 0; | |
569 | sum_r = sum_g = sum_b = sum_a = 0.0; | |
570 | ||
e8a8d0dc | 571 | for ( int j = int(src_y - scale_factor_y/2.0 + 1); |
30f27c00 VZ |
572 | j <= int(src_y + scale_factor_y_2); |
573 | j++ ) | |
07aaa1a4 RR |
574 | { |
575 | // We don't care to average pixels that don't exist (edges) | |
c7a284c7 | 576 | if ( j < 0 || j > M_IMGDATA->m_height - 1 ) |
07aaa1a4 RR |
577 | continue; |
578 | ||
e8a8d0dc | 579 | for ( int i = int(src_x - scale_factor_x/2.0 + 1); |
30f27c00 VZ |
580 | i <= src_x + scale_factor_x_2; |
581 | i++ ) | |
07aaa1a4 RR |
582 | { |
583 | // Don't average edge pixels | |
c7a284c7 | 584 | if ( i < 0 || i > M_IMGDATA->m_width - 1 ) |
07aaa1a4 RR |
585 | continue; |
586 | ||
587 | // Calculate the actual index in our source pixels | |
c7a284c7 | 588 | src_pixel_index = j * M_IMGDATA->m_width + i; |
07aaa1a4 RR |
589 | |
590 | sum_r += src_data[src_pixel_index * 3 + 0]; | |
591 | sum_g += src_data[src_pixel_index * 3 + 1]; | |
592 | sum_b += src_data[src_pixel_index * 3 + 2]; | |
30f27c00 | 593 | if ( src_alpha ) |
07aaa1a4 RR |
594 | sum_a += src_alpha[src_pixel_index]; |
595 | ||
596 | averaged_pixels++; | |
597 | } | |
598 | } | |
599 | ||
600 | // Calculate the average from the sum and number of averaged pixels | |
30f27c00 VZ |
601 | dst_data[0] = (unsigned char)(sum_r / averaged_pixels); |
602 | dst_data[1] = (unsigned char)(sum_g / averaged_pixels); | |
603 | dst_data[2] = (unsigned char)(sum_b / averaged_pixels); | |
07aaa1a4 | 604 | dst_data += 3; |
30f27c00 VZ |
605 | if ( src_alpha ) |
606 | *dst_alpha++ = (unsigned char)(sum_a / averaged_pixels); | |
07aaa1a4 RR |
607 | } |
608 | } | |
609 | ||
610 | return ret_image; | |
611 | } | |
612 | ||
30f27c00 VZ |
613 | // The following two local functions are for the B-spline weighting of the |
614 | // bicubic sampling algorithm | |
07aaa1a4 RR |
615 | static inline double spline_cube(double value) |
616 | { | |
617 | return value <= 0.0 ? 0.0 : value * value * value; | |
618 | } | |
619 | ||
620 | static inline double spline_weight(double value) | |
621 | { | |
30f27c00 VZ |
622 | return (spline_cube(value + 2) - |
623 | 4 * spline_cube(value + 1) + | |
624 | 6 * spline_cube(value) - | |
625 | 4 * spline_cube(value - 1)) / 6; | |
07aaa1a4 RR |
626 | } |
627 | ||
628 | // This is the bicubic resampling algorithm | |
629 | wxImage wxImage::ResampleBicubic(int width, int height) const | |
630 | { | |
30f27c00 VZ |
631 | // This function implements a Bicubic B-Spline algorithm for resampling. |
632 | // This method is certainly a little slower than wxImage's default pixel | |
633 | // replication method, however for most reasonably sized images not being | |
634 | // upsampled too much on a fairly average CPU this difference is hardly | |
635 | // noticeable and the results are far more pleasing to look at. | |
07aaa1a4 | 636 | // |
30f27c00 VZ |
637 | // This particular bicubic algorithm does pixel weighting according to a |
638 | // B-Spline that basically implements a Gaussian bell-like weighting | |
639 | // kernel. Because of this method the results may appear a bit blurry when | |
640 | // upsampling by large factors. This is basically because a slight | |
641 | // gaussian blur is being performed to get the smooth look of the upsampled | |
642 | // image. | |
07aaa1a4 RR |
643 | |
644 | // Edge pixels: 3-4 possible solutions | |
30f27c00 VZ |
645 | // - (Wrap/tile) Wrap the image, take the color value from the opposite |
646 | // side of the image. | |
647 | // - (Mirror) Duplicate edge pixels, so that pixel at coordinate (2, n), | |
648 | // where n is nonpositive, will have the value of (2, 1). | |
649 | // - (Ignore) Simply ignore the edge pixels and apply the kernel only to | |
650 | // pixels which do have all neighbours. | |
651 | // - (Clamp) Choose the nearest pixel along the border. This takes the | |
652 | // border pixels and extends them out to infinity. | |
07aaa1a4 | 653 | // |
30f27c00 VZ |
654 | // NOTE: below the y_offset and x_offset variables are being set for edge |
655 | // pixels using the "Mirror" method mentioned above | |
07aaa1a4 RR |
656 | |
657 | wxImage ret_image; | |
658 | ||
659 | ret_image.Create(width, height, false); | |
660 | ||
661 | unsigned char* src_data = M_IMGDATA->m_data; | |
662 | unsigned char* src_alpha = M_IMGDATA->m_alpha; | |
663 | unsigned char* dst_data = ret_image.GetData(); | |
664 | unsigned char* dst_alpha = NULL; | |
665 | ||
30f27c00 | 666 | if ( src_alpha ) |
07aaa1a4 RR |
667 | { |
668 | ret_image.SetAlpha(); | |
669 | dst_alpha = ret_image.GetAlpha(); | |
670 | } | |
671 | ||
30f27c00 | 672 | for ( int dsty = 0; dsty < height; dsty++ ) |
07aaa1a4 RR |
673 | { |
674 | // We need to calculate the source pixel to interpolate from - Y-axis | |
993f0845 | 675 | double srcpixy = double(dsty * M_IMGDATA->m_height) / height; |
30f27c00 | 676 | double dy = srcpixy - (int)srcpixy; |
07aaa1a4 | 677 | |
30f27c00 | 678 | for ( int dstx = 0; dstx < width; dstx++ ) |
07aaa1a4 RR |
679 | { |
680 | // X-axis of pixel to interpolate from | |
993f0845 | 681 | double srcpixx = double(dstx * M_IMGDATA->m_width) / width; |
30f27c00 | 682 | double dx = srcpixx - (int)srcpixx; |
07aaa1a4 | 683 | |
30f27c00 VZ |
684 | // Sums for each color channel |
685 | double sum_r = 0, sum_g = 0, sum_b = 0, sum_a = 0; | |
07aaa1a4 RR |
686 | |
687 | // Here we actually determine the RGBA values for the destination pixel | |
30f27c00 | 688 | for ( int k = -1; k <= 2; k++ ) |
07aaa1a4 RR |
689 | { |
690 | // Y offset | |
30f27c00 VZ |
691 | int y_offset = srcpixy + k < 0.0 |
692 | ? 0 | |
693 | : srcpixy + k >= M_IMGDATA->m_height | |
694 | ? M_IMGDATA->m_height - 1 | |
695 | : (int)(srcpixy + k); | |
07aaa1a4 RR |
696 | |
697 | // Loop across the X axis | |
30f27c00 | 698 | for ( int i = -1; i <= 2; i++ ) |
07aaa1a4 RR |
699 | { |
700 | // X offset | |
30f27c00 VZ |
701 | int x_offset = srcpixx + i < 0.0 |
702 | ? 0 | |
703 | : srcpixx + i >= M_IMGDATA->m_width | |
704 | ? M_IMGDATA->m_width - 1 | |
705 | : (int)(srcpixx + i); | |
706 | ||
707 | // Calculate the exact position where the source data | |
708 | // should be pulled from based on the x_offset and y_offset | |
709 | int src_pixel_index = y_offset*M_IMGDATA->m_width + x_offset; | |
710 | ||
711 | // Calculate the weight for the specified pixel according | |
712 | // to the bicubic b-spline kernel we're using for | |
713 | // interpolation | |
714 | double | |
715 | pixel_weight = spline_weight(i - dx)*spline_weight(k - dy); | |
716 | ||
717 | // Create a sum of all velues for each color channel | |
718 | // adjusted for the pixel's calculated weight | |
719 | sum_r += src_data[src_pixel_index * 3 + 0] * pixel_weight; | |
720 | sum_g += src_data[src_pixel_index * 3 + 1] * pixel_weight; | |
721 | sum_b += src_data[src_pixel_index * 3 + 2] * pixel_weight; | |
722 | if ( src_alpha ) | |
723 | sum_a += src_alpha[src_pixel_index] * pixel_weight; | |
07aaa1a4 RR |
724 | } |
725 | } | |
726 | ||
30f27c00 VZ |
727 | // Put the data into the destination image. The summed values are |
728 | // of double data type and are rounded here for accuracy | |
729 | dst_data[0] = (unsigned char)(sum_r + 0.5); | |
730 | dst_data[1] = (unsigned char)(sum_g + 0.5); | |
731 | dst_data[2] = (unsigned char)(sum_b + 0.5); | |
07aaa1a4 RR |
732 | dst_data += 3; |
733 | ||
30f27c00 VZ |
734 | if ( src_alpha ) |
735 | *dst_alpha++ = (unsigned char)sum_a; | |
07aaa1a4 RR |
736 | } |
737 | } | |
738 | ||
739 | return ret_image; | |
740 | } | |
741 | ||
742 | // Blur in the horizontal direction | |
24904055 | 743 | wxImage wxImage::BlurHorizontal(int blurRadius) const |
07aaa1a4 RR |
744 | { |
745 | wxImage ret_image; | |
746 | ret_image.Create(M_IMGDATA->m_width, M_IMGDATA->m_height, false); | |
747 | ||
748 | unsigned char* src_data = M_IMGDATA->m_data; | |
749 | unsigned char* dst_data = ret_image.GetData(); | |
750 | unsigned char* src_alpha = M_IMGDATA->m_alpha; | |
751 | unsigned char* dst_alpha = NULL; | |
752 | ||
753 | // Check for a mask or alpha | |
641ed513 VZ |
754 | if ( src_alpha ) |
755 | { | |
756 | ret_image.SetAlpha(); | |
757 | dst_alpha = ret_image.GetAlpha(); | |
758 | } | |
759 | else if ( M_IMGDATA->m_hasMask ) | |
30f27c00 VZ |
760 | { |
761 | ret_image.SetMaskColour(M_IMGDATA->m_maskRed, | |
762 | M_IMGDATA->m_maskGreen, | |
763 | M_IMGDATA->m_maskBlue); | |
764 | } | |
07aaa1a4 | 765 | |
30f27c00 VZ |
766 | // number of pixels we average over |
767 | const int blurArea = blurRadius*2 + 1; | |
07aaa1a4 | 768 | |
30f27c00 VZ |
769 | // Horizontal blurring algorithm - average all pixels in the specified blur |
770 | // radius in the X or horizontal direction | |
771 | for ( int y = 0; y < M_IMGDATA->m_height; y++ ) | |
07aaa1a4 | 772 | { |
30f27c00 VZ |
773 | // Variables used in the blurring algorithm |
774 | long sum_r = 0, | |
775 | sum_g = 0, | |
776 | sum_b = 0, | |
777 | sum_a = 0; | |
778 | ||
779 | long pixel_idx; | |
780 | const unsigned char *src; | |
781 | unsigned char *dst; | |
782 | ||
783 | // Calculate the average of all pixels in the blur radius for the first | |
784 | // pixel of the row | |
785 | for ( int kernel_x = -blurRadius; kernel_x <= blurRadius; kernel_x++ ) | |
07aaa1a4 | 786 | { |
30f27c00 VZ |
787 | // To deal with the pixels at the start of a row so it's not |
788 | // grabbing GOK values from memory at negative indices of the | |
789 | // image's data or grabbing from the previous row | |
790 | if ( kernel_x < 0 ) | |
07aaa1a4 RR |
791 | pixel_idx = y * M_IMGDATA->m_width; |
792 | else | |
793 | pixel_idx = kernel_x + y * M_IMGDATA->m_width; | |
794 | ||
30f27c00 VZ |
795 | src = src_data + pixel_idx*3; |
796 | sum_r += src[0]; | |
797 | sum_g += src[1]; | |
798 | sum_b += src[2]; | |
799 | if ( src_alpha ) | |
800 | sum_a += src_alpha[pixel_idx]; | |
07aaa1a4 | 801 | } |
30f27c00 VZ |
802 | |
803 | dst = dst_data + y * M_IMGDATA->m_width*3; | |
88835522 WS |
804 | dst[0] = (unsigned char)(sum_r / blurArea); |
805 | dst[1] = (unsigned char)(sum_g / blurArea); | |
806 | dst[2] = (unsigned char)(sum_b / blurArea); | |
30f27c00 | 807 | if ( src_alpha ) |
88835522 | 808 | dst_alpha[y * M_IMGDATA->m_width] = (unsigned char)(sum_a / blurArea); |
30f27c00 VZ |
809 | |
810 | // Now average the values of the rest of the pixels by just moving the | |
811 | // blur radius box along the row | |
812 | for ( int x = 1; x < M_IMGDATA->m_width; x++ ) | |
07aaa1a4 | 813 | { |
30f27c00 VZ |
814 | // Take care of edge pixels on the left edge by essentially |
815 | // duplicating the edge pixel | |
816 | if ( x - blurRadius - 1 < 0 ) | |
07aaa1a4 RR |
817 | pixel_idx = y * M_IMGDATA->m_width; |
818 | else | |
819 | pixel_idx = (x - blurRadius - 1) + y * M_IMGDATA->m_width; | |
820 | ||
30f27c00 VZ |
821 | // Subtract the value of the pixel at the left side of the blur |
822 | // radius box | |
823 | src = src_data + pixel_idx*3; | |
824 | sum_r -= src[0]; | |
825 | sum_g -= src[1]; | |
826 | sum_b -= src[2]; | |
827 | if ( src_alpha ) | |
828 | sum_a -= src_alpha[pixel_idx]; | |
07aaa1a4 RR |
829 | |
830 | // Take care of edge pixels on the right edge | |
30f27c00 | 831 | if ( x + blurRadius > M_IMGDATA->m_width - 1 ) |
07aaa1a4 RR |
832 | pixel_idx = M_IMGDATA->m_width - 1 + y * M_IMGDATA->m_width; |
833 | else | |
834 | pixel_idx = x + blurRadius + y * M_IMGDATA->m_width; | |
835 | ||
836 | // Add the value of the pixel being added to the end of our box | |
30f27c00 VZ |
837 | src = src_data + pixel_idx*3; |
838 | sum_r += src[0]; | |
839 | sum_g += src[1]; | |
840 | sum_b += src[2]; | |
841 | if ( src_alpha ) | |
842 | sum_a += src_alpha[pixel_idx]; | |
07aaa1a4 RR |
843 | |
844 | // Save off the averaged data | |
e8a8d0dc | 845 | dst = dst_data + x*3 + y*M_IMGDATA->m_width*3; |
88835522 WS |
846 | dst[0] = (unsigned char)(sum_r / blurArea); |
847 | dst[1] = (unsigned char)(sum_g / blurArea); | |
848 | dst[2] = (unsigned char)(sum_b / blurArea); | |
30f27c00 | 849 | if ( src_alpha ) |
88835522 | 850 | dst_alpha[x + y * M_IMGDATA->m_width] = (unsigned char)(sum_a / blurArea); |
07aaa1a4 RR |
851 | } |
852 | } | |
853 | ||
854 | return ret_image; | |
855 | } | |
856 | ||
857 | // Blur in the vertical direction | |
24904055 | 858 | wxImage wxImage::BlurVertical(int blurRadius) const |
07aaa1a4 RR |
859 | { |
860 | wxImage ret_image; | |
861 | ret_image.Create(M_IMGDATA->m_width, M_IMGDATA->m_height, false); | |
862 | ||
863 | unsigned char* src_data = M_IMGDATA->m_data; | |
864 | unsigned char* dst_data = ret_image.GetData(); | |
865 | unsigned char* src_alpha = M_IMGDATA->m_alpha; | |
866 | unsigned char* dst_alpha = NULL; | |
867 | ||
868 | // Check for a mask or alpha | |
641ed513 VZ |
869 | if ( src_alpha ) |
870 | { | |
871 | ret_image.SetAlpha(); | |
872 | dst_alpha = ret_image.GetAlpha(); | |
873 | } | |
874 | else if ( M_IMGDATA->m_hasMask ) | |
30f27c00 VZ |
875 | { |
876 | ret_image.SetMaskColour(M_IMGDATA->m_maskRed, | |
877 | M_IMGDATA->m_maskGreen, | |
878 | M_IMGDATA->m_maskBlue); | |
879 | } | |
07aaa1a4 | 880 | |
30f27c00 VZ |
881 | // number of pixels we average over |
882 | const int blurArea = blurRadius*2 + 1; | |
07aaa1a4 | 883 | |
30f27c00 VZ |
884 | // Vertical blurring algorithm - same as horizontal but switched the |
885 | // opposite direction | |
886 | for ( int x = 0; x < M_IMGDATA->m_width; x++ ) | |
07aaa1a4 | 887 | { |
30f27c00 VZ |
888 | // Variables used in the blurring algorithm |
889 | long sum_r = 0, | |
890 | sum_g = 0, | |
891 | sum_b = 0, | |
892 | sum_a = 0; | |
893 | ||
894 | long pixel_idx; | |
895 | const unsigned char *src; | |
896 | unsigned char *dst; | |
897 | ||
898 | // Calculate the average of all pixels in our blur radius box for the | |
899 | // first pixel of the column | |
900 | for ( int kernel_y = -blurRadius; kernel_y <= blurRadius; kernel_y++ ) | |
07aaa1a4 | 901 | { |
30f27c00 VZ |
902 | // To deal with the pixels at the start of a column so it's not |
903 | // grabbing GOK values from memory at negative indices of the | |
904 | // image's data or grabbing from the previous column | |
905 | if ( kernel_y < 0 ) | |
07aaa1a4 RR |
906 | pixel_idx = x; |
907 | else | |
908 | pixel_idx = x + kernel_y * M_IMGDATA->m_width; | |
909 | ||
30f27c00 VZ |
910 | src = src_data + pixel_idx*3; |
911 | sum_r += src[0]; | |
912 | sum_g += src[1]; | |
913 | sum_b += src[2]; | |
914 | if ( src_alpha ) | |
915 | sum_a += src_alpha[pixel_idx]; | |
07aaa1a4 | 916 | } |
30f27c00 VZ |
917 | |
918 | dst = dst_data + x*3; | |
88835522 WS |
919 | dst[0] = (unsigned char)(sum_r / blurArea); |
920 | dst[1] = (unsigned char)(sum_g / blurArea); | |
921 | dst[2] = (unsigned char)(sum_b / blurArea); | |
30f27c00 | 922 | if ( src_alpha ) |
88835522 | 923 | dst_alpha[x] = (unsigned char)(sum_a / blurArea); |
30f27c00 VZ |
924 | |
925 | // Now average the values of the rest of the pixels by just moving the | |
926 | // box along the column from top to bottom | |
927 | for ( int y = 1; y < M_IMGDATA->m_height; y++ ) | |
07aaa1a4 | 928 | { |
30f27c00 VZ |
929 | // Take care of pixels that would be beyond the top edge by |
930 | // duplicating the top edge pixel for the column | |
931 | if ( y - blurRadius - 1 < 0 ) | |
07aaa1a4 RR |
932 | pixel_idx = x; |
933 | else | |
934 | pixel_idx = x + (y - blurRadius - 1) * M_IMGDATA->m_width; | |
935 | ||
936 | // Subtract the value of the pixel at the top of our blur radius box | |
30f27c00 VZ |
937 | src = src_data + pixel_idx*3; |
938 | sum_r -= src[0]; | |
939 | sum_g -= src[1]; | |
940 | sum_b -= src[2]; | |
941 | if ( src_alpha ) | |
942 | sum_a -= src_alpha[pixel_idx]; | |
943 | ||
944 | // Take care of the pixels that would be beyond the bottom edge of | |
945 | // the image similar to the top edge | |
946 | if ( y + blurRadius > M_IMGDATA->m_height - 1 ) | |
07aaa1a4 RR |
947 | pixel_idx = x + (M_IMGDATA->m_height - 1) * M_IMGDATA->m_width; |
948 | else | |
949 | pixel_idx = x + (blurRadius + y) * M_IMGDATA->m_width; | |
950 | ||
951 | // Add the value of the pixel being added to the end of our box | |
30f27c00 VZ |
952 | src = src_data + pixel_idx*3; |
953 | sum_r += src[0]; | |
954 | sum_g += src[1]; | |
955 | sum_b += src[2]; | |
956 | if ( src_alpha ) | |
957 | sum_a += src_alpha[pixel_idx]; | |
07aaa1a4 RR |
958 | |
959 | // Save off the averaged data | |
30f27c00 | 960 | dst = dst_data + (x + y * M_IMGDATA->m_width) * 3; |
88835522 WS |
961 | dst[0] = (unsigned char)(sum_r / blurArea); |
962 | dst[1] = (unsigned char)(sum_g / blurArea); | |
963 | dst[2] = (unsigned char)(sum_b / blurArea); | |
30f27c00 | 964 | if ( src_alpha ) |
88835522 | 965 | dst_alpha[x + y * M_IMGDATA->m_width] = (unsigned char)(sum_a / blurArea); |
07aaa1a4 RR |
966 | } |
967 | } | |
968 | ||
969 | return ret_image; | |
970 | } | |
971 | ||
972 | // The new blur function | |
24904055 | 973 | wxImage wxImage::Blur(int blurRadius) const |
07aaa1a4 RR |
974 | { |
975 | wxImage ret_image; | |
976 | ret_image.Create(M_IMGDATA->m_width, M_IMGDATA->m_height, false); | |
977 | ||
978 | // Blur the image in each direction | |
979 | ret_image = BlurHorizontal(blurRadius); | |
980 | ret_image = ret_image.BlurVertical(blurRadius); | |
981 | ||
982 | return ret_image; | |
983 | } | |
984 | ||
f6bcfd97 BP |
985 | wxImage wxImage::Rotate90( bool clockwise ) const |
986 | { | |
987 | wxImage image; | |
988 | ||
989 | wxCHECK_MSG( Ok(), image, wxT("invalid image") ); | |
990 | ||
ff865c13 | 991 | image.Create( M_IMGDATA->m_height, M_IMGDATA->m_width, false ); |
f6bcfd97 | 992 | |
487659e0 | 993 | unsigned char *data = image.GetData(); |
f6bcfd97 BP |
994 | |
995 | wxCHECK_MSG( data, image, wxT("unable to create image") ); | |
996 | ||
921c65ed JS |
997 | unsigned char *source_data = M_IMGDATA->m_data; |
998 | unsigned char *target_data; | |
999 | unsigned char *alpha_data = 0 ; | |
1000 | unsigned char *source_alpha = 0 ; | |
1001 | unsigned char *target_alpha = 0 ; | |
1002 | ||
f6bcfd97 | 1003 | if (M_IMGDATA->m_hasMask) |
921c65ed | 1004 | { |
f6bcfd97 | 1005 | image.SetMaskColour( M_IMGDATA->m_maskRed, M_IMGDATA->m_maskGreen, M_IMGDATA->m_maskBlue ); |
921c65ed JS |
1006 | } |
1007 | else | |
1008 | { | |
1009 | source_alpha = M_IMGDATA->m_alpha ; | |
1010 | if ( source_alpha ) | |
1011 | { | |
1012 | image.SetAlpha() ; | |
1013 | alpha_data = image.GetAlpha() ; | |
1014 | } | |
1015 | } | |
f6bcfd97 BP |
1016 | |
1017 | long height = M_IMGDATA->m_height; | |
1018 | long width = M_IMGDATA->m_width; | |
1019 | ||
f6bcfd97 BP |
1020 | for (long j = 0; j < height; j++) |
1021 | { | |
1022 | for (long i = 0; i < width; i++) | |
1023 | { | |
1024 | if (clockwise) | |
921c65ed | 1025 | { |
f6bcfd97 | 1026 | target_data = data + (((i+1)*height) - j - 1)*3; |
921c65ed JS |
1027 | if(source_alpha) |
1028 | target_alpha = alpha_data + (((i+1)*height) - j - 1); | |
1029 | } | |
f6bcfd97 | 1030 | else |
921c65ed | 1031 | { |
f6bcfd97 | 1032 | target_data = data + ((height*(width-1)) + j - (i*height))*3; |
921c65ed JS |
1033 | if(source_alpha) |
1034 | target_alpha = alpha_data + ((height*(width-1)) + j - (i*height)); | |
1035 | } | |
f6bcfd97 BP |
1036 | memcpy( target_data, source_data, 3 ); |
1037 | source_data += 3; | |
921c65ed JS |
1038 | |
1039 | if(source_alpha) | |
1040 | { | |
1041 | memcpy( target_alpha, source_alpha, 1 ); | |
1042 | source_alpha += 1; | |
1043 | } | |
f6bcfd97 BP |
1044 | } |
1045 | } | |
1046 | ||
1047 | return image; | |
1048 | } | |
1049 | ||
1050 | wxImage wxImage::Mirror( bool horizontally ) const | |
1051 | { | |
1052 | wxImage image; | |
1053 | ||
1054 | wxCHECK_MSG( Ok(), image, wxT("invalid image") ); | |
1055 | ||
ff865c13 | 1056 | image.Create( M_IMGDATA->m_width, M_IMGDATA->m_height, false ); |
f6bcfd97 | 1057 | |
487659e0 | 1058 | unsigned char *data = image.GetData(); |
051924b8 | 1059 | unsigned char *alpha = NULL; |
f6bcfd97 BP |
1060 | |
1061 | wxCHECK_MSG( data, image, wxT("unable to create image") ); | |
1062 | ||
051924b8 VZ |
1063 | if (M_IMGDATA->m_alpha != NULL) { |
1064 | image.SetAlpha(); | |
1065 | alpha = image.GetAlpha(); | |
1066 | wxCHECK_MSG( alpha, image, wxT("unable to create alpha channel") ); | |
1067 | } | |
1068 | ||
f6bcfd97 BP |
1069 | if (M_IMGDATA->m_hasMask) |
1070 | image.SetMaskColour( M_IMGDATA->m_maskRed, M_IMGDATA->m_maskGreen, M_IMGDATA->m_maskBlue ); | |
1071 | ||
1072 | long height = M_IMGDATA->m_height; | |
1073 | long width = M_IMGDATA->m_width; | |
1074 | ||
487659e0 VZ |
1075 | unsigned char *source_data = M_IMGDATA->m_data; |
1076 | unsigned char *target_data; | |
f6bcfd97 BP |
1077 | |
1078 | if (horizontally) | |
1079 | { | |
1080 | for (long j = 0; j < height; j++) | |
1081 | { | |
1082 | data += width*3; | |
1083 | target_data = data-3; | |
1084 | for (long i = 0; i < width; i++) | |
1085 | { | |
1086 | memcpy( target_data, source_data, 3 ); | |
1087 | source_data += 3; | |
1088 | target_data -= 3; | |
1089 | } | |
1090 | } | |
051924b8 VZ |
1091 | |
1092 | if (alpha != NULL) | |
1093 | { | |
1094 | // src_alpha starts at the first pixel and increases by 1 after each step | |
1095 | // (a step here is the copy of the alpha value of one pixel) | |
1096 | const unsigned char *src_alpha = M_IMGDATA->m_alpha; | |
1097 | // dest_alpha starts just beyond the first line, decreases before each step, | |
1098 | // and after each line is finished, increases by 2 widths (skipping the line | |
1099 | // just copied and the line that will be copied next) | |
1100 | unsigned char *dest_alpha = alpha + width; | |
1101 | ||
1102 | for (long jj = 0; jj < height; ++jj) | |
1103 | { | |
1104 | for (long i = 0; i < width; ++i) { | |
1105 | *(--dest_alpha) = *(src_alpha++); // copy one pixel | |
1106 | } | |
1107 | dest_alpha += 2 * width; // advance beyond the end of the next line | |
1108 | } | |
1109 | } | |
f6bcfd97 BP |
1110 | } |
1111 | else | |
1112 | { | |
1113 | for (long i = 0; i < height; i++) | |
1114 | { | |
1115 | target_data = data + 3*width*(height-1-i); | |
3ca6a5f0 | 1116 | memcpy( target_data, source_data, (size_t)3*width ); |
f6bcfd97 BP |
1117 | source_data += 3*width; |
1118 | } | |
051924b8 VZ |
1119 | |
1120 | if (alpha != NULL) | |
1121 | { | |
1122 | // src_alpha starts at the first pixel and increases by 1 width after each step | |
1123 | // (a step here is the copy of the alpha channel of an entire line) | |
1124 | const unsigned char *src_alpha = M_IMGDATA->m_alpha; | |
1125 | // dest_alpha starts just beyond the last line (beyond the whole image) | |
1126 | // and decreases by 1 width before each step | |
1127 | unsigned char *dest_alpha = alpha + width * height; | |
1128 | ||
1129 | for (long jj = 0; jj < height; ++jj) | |
1130 | { | |
1131 | dest_alpha -= width; | |
1132 | memcpy( dest_alpha, src_alpha, (size_t)width ); | |
1133 | src_alpha += width; | |
1134 | } | |
1135 | } | |
f6bcfd97 BP |
1136 | } |
1137 | ||
1138 | return image; | |
1139 | } | |
1140 | ||
7b2471a0 SB |
1141 | wxImage wxImage::GetSubImage( const wxRect &rect ) const |
1142 | { | |
1143 | wxImage image; | |
1144 | ||
223d09f6 | 1145 | wxCHECK_MSG( Ok(), image, wxT("invalid image") ); |
7b2471a0 | 1146 | |
051924b8 VZ |
1147 | wxCHECK_MSG( (rect.GetLeft()>=0) && (rect.GetTop()>=0) && |
1148 | (rect.GetRight()<=GetWidth()) && (rect.GetBottom()<=GetHeight()), | |
58c837a4 | 1149 | image, wxT("invalid subimage size") ); |
7b2471a0 | 1150 | |
051924b8 VZ |
1151 | const int subwidth = rect.GetWidth(); |
1152 | const int subheight = rect.GetHeight(); | |
7b2471a0 | 1153 | |
ff865c13 | 1154 | image.Create( subwidth, subheight, false ); |
7b2471a0 | 1155 | |
051924b8 VZ |
1156 | const unsigned char *src_data = GetData(); |
1157 | const unsigned char *src_alpha = M_IMGDATA->m_alpha; | |
1158 | unsigned char *subdata = image.GetData(); | |
1159 | unsigned char *subalpha = NULL; | |
7b2471a0 | 1160 | |
223d09f6 | 1161 | wxCHECK_MSG( subdata, image, wxT("unable to create image") ); |
7b2471a0 | 1162 | |
051924b8 VZ |
1163 | if (src_alpha != NULL) { |
1164 | image.SetAlpha(); | |
1165 | subalpha = image.GetAlpha(); | |
1166 | wxCHECK_MSG( subalpha, image, wxT("unable to create alpha channel")); | |
1167 | } | |
1168 | ||
7b2471a0 SB |
1169 | if (M_IMGDATA->m_hasMask) |
1170 | image.SetMaskColour( M_IMGDATA->m_maskRed, M_IMGDATA->m_maskGreen, M_IMGDATA->m_maskBlue ); | |
1171 | ||
051924b8 VZ |
1172 | const int width = GetWidth(); |
1173 | const int pixsoff = rect.GetLeft() + width * rect.GetTop(); | |
995612e2 | 1174 | |
051924b8 VZ |
1175 | src_data += 3 * pixsoff; |
1176 | src_alpha += pixsoff; // won't be used if was NULL, so this is ok | |
7b2471a0 SB |
1177 | |
1178 | for (long j = 0; j < subheight; ++j) | |
1179 | { | |
051924b8 VZ |
1180 | memcpy( subdata, src_data, 3 * subwidth ); |
1181 | subdata += 3 * subwidth; | |
1182 | src_data += 3 * width; | |
1183 | if (subalpha != NULL) { | |
1184 | memcpy( subalpha, src_alpha, subwidth ); | |
1185 | subalpha += subwidth; | |
1186 | src_alpha += width; | |
1187 | } | |
7b2471a0 SB |
1188 | } |
1189 | ||
1190 | return image; | |
1191 | } | |
1192 | ||
e9b64c5e | 1193 | wxImage wxImage::Size( const wxSize& size, const wxPoint& pos, |
b737ad10 RR |
1194 | int r_, int g_, int b_ ) const |
1195 | { | |
1196 | wxImage image; | |
1197 | ||
1198 | wxCHECK_MSG( Ok(), image, wxT("invalid image") ); | |
1199 | wxCHECK_MSG( (size.GetWidth() > 0) && (size.GetHeight() > 0), image, wxT("invalid size") ); | |
1200 | ||
1201 | int width = GetWidth(), height = GetHeight(); | |
1202 | image.Create(size.GetWidth(), size.GetHeight(), false); | |
1203 | ||
1204 | unsigned char r = (unsigned char)r_; | |
1205 | unsigned char g = (unsigned char)g_; | |
1206 | unsigned char b = (unsigned char)b_; | |
1207 | if ((r_ == -1) && (g_ == -1) && (b_ == -1)) | |
1208 | { | |
1209 | GetOrFindMaskColour( &r, &g, &b ); | |
1210 | image.SetMaskColour(r, g, b); | |
1211 | } | |
1212 | ||
1213 | image.SetRGB(wxRect(), r, g, b); | |
1214 | ||
1215 | wxRect subRect(pos.x, pos.y, width, height); | |
1216 | wxRect finalRect(0, 0, size.GetWidth(), size.GetHeight()); | |
781945ea RR |
1217 | if (pos.x < 0) |
1218 | finalRect.width -= pos.x; | |
1219 | if (pos.y < 0) | |
1220 | finalRect.height -= pos.y; | |
b737ad10 RR |
1221 | |
1222 | subRect.Intersect(finalRect); | |
1223 | ||
1224 | if (!subRect.IsEmpty()) | |
1225 | { | |
1226 | if ((subRect.GetWidth() == width) && (subRect.GetHeight() == height)) | |
1227 | image.Paste(*this, pos.x, pos.y); | |
1228 | else | |
1229 | image.Paste(GetSubImage(subRect), pos.x, pos.y); | |
1230 | } | |
1231 | ||
1232 | return image; | |
1233 | } | |
1234 | ||
f6bcfd97 BP |
1235 | void wxImage::Paste( const wxImage &image, int x, int y ) |
1236 | { | |
1237 | wxCHECK_RET( Ok(), wxT("invalid image") ); | |
1238 | wxCHECK_RET( image.Ok(), wxT("invalid image") ); | |
1239 | ||
a0f81e9f PC |
1240 | AllocExclusive(); |
1241 | ||
f6bcfd97 BP |
1242 | int xx = 0; |
1243 | int yy = 0; | |
1244 | int width = image.GetWidth(); | |
1245 | int height = image.GetHeight(); | |
1246 | ||
1247 | if (x < 0) | |
1248 | { | |
1249 | xx = -x; | |
1250 | width += x; | |
1251 | } | |
1252 | if (y < 0) | |
1253 | { | |
1254 | yy = -y; | |
1255 | height += y; | |
1256 | } | |
1257 | ||
1258 | if ((x+xx)+width > M_IMGDATA->m_width) | |
1259 | width = M_IMGDATA->m_width - (x+xx); | |
1260 | if ((y+yy)+height > M_IMGDATA->m_height) | |
1261 | height = M_IMGDATA->m_height - (y+yy); | |
1262 | ||
1263 | if (width < 1) return; | |
1264 | if (height < 1) return; | |
1265 | ||
1266 | if ((!HasMask() && !image.HasMask()) || | |
b737ad10 | 1267 | (HasMask() && !image.HasMask()) || |
f6bcfd97 BP |
1268 | ((HasMask() && image.HasMask() && |
1269 | (GetMaskRed()==image.GetMaskRed()) && | |
1270 | (GetMaskGreen()==image.GetMaskGreen()) && | |
1271 | (GetMaskBlue()==image.GetMaskBlue())))) | |
1272 | { | |
1273 | width *= 3; | |
1274 | unsigned char* source_data = image.GetData() + xx*3 + yy*3*image.GetWidth(); | |
1275 | int source_step = image.GetWidth()*3; | |
1276 | ||
1277 | unsigned char* target_data = GetData() + (x+xx)*3 + (y+yy)*3*M_IMGDATA->m_width; | |
1278 | int target_step = M_IMGDATA->m_width*3; | |
1279 | for (int j = 0; j < height; j++) | |
1280 | { | |
1281 | memcpy( target_data, source_data, width ); | |
1282 | source_data += source_step; | |
1283 | target_data += target_step; | |
1284 | } | |
aa21b509 | 1285 | return; |
f6bcfd97 | 1286 | } |
33ac7e6f | 1287 | |
14678d6d VZ |
1288 | // Copy over the alpha channel from the original image |
1289 | if ( image.HasAlpha() ) | |
1290 | { | |
1291 | if ( !HasAlpha() ) | |
1292 | InitAlpha(); | |
1293 | ||
1294 | unsigned char* source_data = image.GetAlpha() + xx + yy*image.GetWidth(); | |
1295 | int source_step = image.GetWidth(); | |
1296 | ||
1297 | unsigned char* target_data = GetAlpha() + (x+xx) + (y+yy)*M_IMGDATA->m_width; | |
1298 | int target_step = M_IMGDATA->m_width; | |
1299 | ||
1300 | for (int j = 0; j < height; j++, | |
1301 | source_data += source_step, | |
1302 | target_data += target_step) | |
1303 | { | |
1304 | memcpy( target_data, source_data, width ); | |
1305 | } | |
1306 | } | |
1307 | ||
aa21b509 | 1308 | if (!HasMask() && image.HasMask()) |
f6bcfd97 | 1309 | { |
aa21b509 RR |
1310 | unsigned char r = image.GetMaskRed(); |
1311 | unsigned char g = image.GetMaskGreen(); | |
1312 | unsigned char b = image.GetMaskBlue(); | |
33ac7e6f | 1313 | |
aa21b509 RR |
1314 | width *= 3; |
1315 | unsigned char* source_data = image.GetData() + xx*3 + yy*3*image.GetWidth(); | |
1316 | int source_step = image.GetWidth()*3; | |
1317 | ||
1318 | unsigned char* target_data = GetData() + (x+xx)*3 + (y+yy)*3*M_IMGDATA->m_width; | |
1319 | int target_step = M_IMGDATA->m_width*3; | |
33ac7e6f | 1320 | |
aa21b509 RR |
1321 | for (int j = 0; j < height; j++) |
1322 | { | |
1323 | for (int i = 0; i < width; i+=3) | |
1324 | { | |
dcc36b34 RR |
1325 | if ((source_data[i] != r) || |
1326 | (source_data[i+1] != g) || | |
aa21b509 RR |
1327 | (source_data[i+2] != b)) |
1328 | { | |
1329 | memcpy( target_data+i, source_data+i, 3 ); | |
1330 | } | |
33ac7e6f | 1331 | } |
aa21b509 RR |
1332 | source_data += source_step; |
1333 | target_data += target_step; | |
1334 | } | |
f6bcfd97 BP |
1335 | } |
1336 | } | |
1337 | ||
be25e480 RR |
1338 | void wxImage::Replace( unsigned char r1, unsigned char g1, unsigned char b1, |
1339 | unsigned char r2, unsigned char g2, unsigned char b2 ) | |
1340 | { | |
1341 | wxCHECK_RET( Ok(), wxT("invalid image") ); | |
1342 | ||
a0f81e9f PC |
1343 | AllocExclusive(); |
1344 | ||
487659e0 | 1345 | unsigned char *data = GetData(); |
06b466c7 | 1346 | |
be25e480 RR |
1347 | const int w = GetWidth(); |
1348 | const int h = GetHeight(); | |
1349 | ||
1350 | for (int j = 0; j < h; j++) | |
1351 | for (int i = 0; i < w; i++) | |
069d0f27 VZ |
1352 | { |
1353 | if ((data[0] == r1) && (data[1] == g1) && (data[2] == b1)) | |
1354 | { | |
1355 | data[0] = r2; | |
1356 | data[1] = g2; | |
1357 | data[2] = b2; | |
1358 | } | |
1359 | data += 3; | |
1360 | } | |
be25e480 RR |
1361 | } |
1362 | ||
ec85956a JS |
1363 | wxImage wxImage::ConvertToGreyscale( double lr, double lg, double lb ) const |
1364 | { | |
1365 | wxImage image; | |
1366 | ||
1367 | wxCHECK_MSG( Ok(), image, wxT("invalid image") ); | |
1368 | ||
1369 | image.Create(M_IMGDATA->m_width, M_IMGDATA->m_height, false); | |
1370 | ||
1371 | unsigned char *dest = image.GetData(); | |
1372 | ||
1373 | wxCHECK_MSG( dest, image, wxT("unable to create image") ); | |
1374 | ||
1375 | unsigned char *src = M_IMGDATA->m_data; | |
1376 | bool hasMask = M_IMGDATA->m_hasMask; | |
1377 | unsigned char maskRed = M_IMGDATA->m_maskRed; | |
1378 | unsigned char maskGreen = M_IMGDATA->m_maskGreen; | |
1379 | unsigned char maskBlue = M_IMGDATA->m_maskBlue; | |
1380 | ||
1381 | if ( hasMask ) | |
1382 | image.SetMaskColour(maskRed, maskGreen, maskBlue); | |
1383 | ||
1384 | const long size = M_IMGDATA->m_width * M_IMGDATA->m_height; | |
1385 | for ( long i = 0; i < size; i++, src += 3, dest += 3 ) | |
1386 | { | |
1387 | // don't modify the mask | |
1388 | if ( hasMask && src[0] == maskRed && src[1] == maskGreen && src[2] == maskBlue ) | |
1389 | { | |
1390 | memcpy(dest, src, 3); | |
1391 | } | |
1392 | else | |
1393 | { | |
1394 | // calculate the luma | |
1395 | double luma = (src[0] * lr + src[1] * lg + src[2] * lb) + 0.5; | |
1396 | dest[0] = dest[1] = dest[2] = wx_static_cast(unsigned char, luma); | |
1397 | } | |
1398 | } | |
1399 | ||
7ce30d0b WS |
1400 | // copy the alpha channel, if any |
1401 | if (HasAlpha()) | |
1402 | { | |
1403 | const size_t alphaSize = GetWidth() * GetHeight(); | |
1404 | unsigned char *alpha = (unsigned char*)malloc(alphaSize); | |
1405 | memcpy(alpha, GetAlpha(), alphaSize); | |
1406 | image.InitAlpha(); | |
1407 | image.SetAlpha(alpha); | |
1408 | } | |
1409 | ||
ec85956a JS |
1410 | return image; |
1411 | } | |
1412 | ||
f515c25a | 1413 | wxImage wxImage::ConvertToMono( unsigned char r, unsigned char g, unsigned char b ) const |
fec19ea9 VS |
1414 | { |
1415 | wxImage image; | |
1416 | ||
1417 | wxCHECK_MSG( Ok(), image, wxT("invalid image") ); | |
1418 | ||
ff865c13 | 1419 | image.Create( M_IMGDATA->m_width, M_IMGDATA->m_height, false ); |
fec19ea9 | 1420 | |
487659e0 | 1421 | unsigned char *data = image.GetData(); |
fec19ea9 VS |
1422 | |
1423 | wxCHECK_MSG( data, image, wxT("unable to create image") ); | |
1424 | ||
1425 | if (M_IMGDATA->m_hasMask) | |
1426 | { | |
1427 | if (M_IMGDATA->m_maskRed == r && M_IMGDATA->m_maskGreen == g && | |
1428 | M_IMGDATA->m_maskBlue == b) | |
1429 | image.SetMaskColour( 255, 255, 255 ); | |
1430 | else | |
1431 | image.SetMaskColour( 0, 0, 0 ); | |
1432 | } | |
1433 | ||
1434 | long size = M_IMGDATA->m_height * M_IMGDATA->m_width; | |
1435 | ||
487659e0 VZ |
1436 | unsigned char *srcd = M_IMGDATA->m_data; |
1437 | unsigned char *tard = image.GetData(); | |
fec19ea9 VS |
1438 | |
1439 | for ( long i = 0; i < size; i++, srcd += 3, tard += 3 ) | |
1440 | { | |
1441 | if (srcd[0] == r && srcd[1] == g && srcd[2] == b) | |
1442 | tard[0] = tard[1] = tard[2] = 255; | |
1443 | else | |
1444 | tard[0] = tard[1] = tard[2] = 0; | |
1445 | } | |
1446 | ||
1447 | return image; | |
1448 | } | |
1449 | ||
21dc4be5 VZ |
1450 | int wxImage::GetWidth() const |
1451 | { | |
1452 | wxCHECK_MSG( Ok(), 0, wxT("invalid image") ); | |
1453 | ||
1454 | return M_IMGDATA->m_width; | |
1455 | } | |
1456 | ||
1457 | int wxImage::GetHeight() const | |
1458 | { | |
1459 | wxCHECK_MSG( Ok(), 0, wxT("invalid image") ); | |
1460 | ||
1461 | return M_IMGDATA->m_height; | |
1462 | } | |
1463 | ||
591d3fa2 VZ |
1464 | wxBitmapType wxImage::GetType() const |
1465 | { | |
1466 | wxCHECK_MSG( IsOk(), wxBITMAP_TYPE_INVALID, wxT("invalid image") ); | |
1467 | ||
1468 | return M_IMGDATA->m_type; | |
1469 | } | |
1470 | ||
5644ac46 | 1471 | long wxImage::XYToIndex(int x, int y) const |
ef539066 | 1472 | { |
5644ac46 VZ |
1473 | if ( Ok() && |
1474 | x >= 0 && y >= 0 && | |
1475 | x < M_IMGDATA->m_width && y < M_IMGDATA->m_height ) | |
1476 | { | |
1477 | return y*M_IMGDATA->m_width + x; | |
1478 | } | |
c7abc967 | 1479 | |
5644ac46 VZ |
1480 | return -1; |
1481 | } | |
c7abc967 | 1482 | |
5644ac46 VZ |
1483 | void wxImage::SetRGB( int x, int y, unsigned char r, unsigned char g, unsigned char b ) |
1484 | { | |
1485 | long pos = XYToIndex(x, y); | |
1486 | wxCHECK_RET( pos != -1, wxT("invalid image coordinates") ); | |
c7abc967 | 1487 | |
a0f81e9f PC |
1488 | AllocExclusive(); |
1489 | ||
5644ac46 | 1490 | pos *= 3; |
c7abc967 | 1491 | |
ef539066 RR |
1492 | M_IMGDATA->m_data[ pos ] = r; |
1493 | M_IMGDATA->m_data[ pos+1 ] = g; | |
1494 | M_IMGDATA->m_data[ pos+2 ] = b; | |
1495 | } | |
1496 | ||
b737ad10 RR |
1497 | void wxImage::SetRGB( const wxRect& rect_, unsigned char r, unsigned char g, unsigned char b ) |
1498 | { | |
1499 | wxCHECK_RET( Ok(), wxT("invalid image") ); | |
1500 | ||
a0f81e9f PC |
1501 | AllocExclusive(); |
1502 | ||
b737ad10 RR |
1503 | wxRect rect(rect_); |
1504 | wxRect imageRect(0, 0, GetWidth(), GetHeight()); | |
1505 | if ( rect == wxRect() ) | |
1506 | { | |
1507 | rect = imageRect; | |
1508 | } | |
1509 | else | |
1510 | { | |
22a35096 VS |
1511 | wxCHECK_RET( imageRect.Contains(rect.GetTopLeft()) && |
1512 | imageRect.Contains(rect.GetBottomRight()), | |
b737ad10 RR |
1513 | wxT("invalid bounding rectangle") ); |
1514 | } | |
1515 | ||
1516 | int x1 = rect.GetLeft(), | |
1517 | y1 = rect.GetTop(), | |
1518 | x2 = rect.GetRight() + 1, | |
1519 | y2 = rect.GetBottom() + 1; | |
1520 | ||
e9b64c5e | 1521 | unsigned char *data wxDUMMY_INITIALIZE(NULL); |
b737ad10 RR |
1522 | int x, y, width = GetWidth(); |
1523 | for (y = y1; y < y2; y++) | |
1524 | { | |
1525 | data = M_IMGDATA->m_data + (y*width + x1)*3; | |
1526 | for (x = x1; x < x2; x++) | |
1527 | { | |
1528 | *data++ = r; | |
1529 | *data++ = g; | |
1530 | *data++ = b; | |
1531 | } | |
1532 | } | |
1533 | } | |
1534 | ||
f6bcfd97 | 1535 | unsigned char wxImage::GetRed( int x, int y ) const |
ef539066 | 1536 | { |
5644ac46 VZ |
1537 | long pos = XYToIndex(x, y); |
1538 | wxCHECK_MSG( pos != -1, 0, wxT("invalid image coordinates") ); | |
c7abc967 | 1539 | |
5644ac46 | 1540 | pos *= 3; |
c7abc967 | 1541 | |
ef539066 RR |
1542 | return M_IMGDATA->m_data[pos]; |
1543 | } | |
1544 | ||
f6bcfd97 | 1545 | unsigned char wxImage::GetGreen( int x, int y ) const |
ef539066 | 1546 | { |
5644ac46 VZ |
1547 | long pos = XYToIndex(x, y); |
1548 | wxCHECK_MSG( pos != -1, 0, wxT("invalid image coordinates") ); | |
c7abc967 | 1549 | |
5644ac46 | 1550 | pos *= 3; |
c7abc967 | 1551 | |
ef539066 RR |
1552 | return M_IMGDATA->m_data[pos+1]; |
1553 | } | |
1554 | ||
f6bcfd97 | 1555 | unsigned char wxImage::GetBlue( int x, int y ) const |
ef539066 | 1556 | { |
5644ac46 VZ |
1557 | long pos = XYToIndex(x, y); |
1558 | wxCHECK_MSG( pos != -1, 0, wxT("invalid image coordinates") ); | |
c7abc967 | 1559 | |
5644ac46 | 1560 | pos *= 3; |
c7abc967 | 1561 | |
ef539066 RR |
1562 | return M_IMGDATA->m_data[pos+2]; |
1563 | } | |
4698648f | 1564 | |
b7cacb43 | 1565 | bool wxImage::IsOk() const |
4698648f | 1566 | { |
de8c48cf VZ |
1567 | // image of 0 width or height can't be considered ok - at least because it |
1568 | // causes crashes in ConvertToBitmap() if we don't catch it in time | |
1569 | wxImageRefData *data = M_IMGDATA; | |
1570 | return data && data->m_ok && data->m_width && data->m_height; | |
01111366 RR |
1571 | } |
1572 | ||
487659e0 | 1573 | unsigned char *wxImage::GetData() const |
01111366 | 1574 | { |
487659e0 | 1575 | wxCHECK_MSG( Ok(), (unsigned char *)NULL, wxT("invalid image") ); |
c7abc967 | 1576 | |
fd0eed64 | 1577 | return M_IMGDATA->m_data; |
01111366 RR |
1578 | } |
1579 | ||
4013de12 | 1580 | void wxImage::SetData( unsigned char *data, bool static_data ) |
01111366 | 1581 | { |
223d09f6 | 1582 | wxCHECK_RET( Ok(), wxT("invalid image") ); |
58a8ab88 | 1583 | |
ed58dbea RR |
1584 | wxImageRefData *newRefData = new wxImageRefData(); |
1585 | ||
1586 | newRefData->m_width = M_IMGDATA->m_width; | |
1587 | newRefData->m_height = M_IMGDATA->m_height; | |
1588 | newRefData->m_data = data; | |
70cd62e9 | 1589 | newRefData->m_ok = true; |
ed58dbea RR |
1590 | newRefData->m_maskRed = M_IMGDATA->m_maskRed; |
1591 | newRefData->m_maskGreen = M_IMGDATA->m_maskGreen; | |
1592 | newRefData->m_maskBlue = M_IMGDATA->m_maskBlue; | |
1593 | newRefData->m_hasMask = M_IMGDATA->m_hasMask; | |
4013de12 | 1594 | newRefData->m_static = static_data; |
995612e2 | 1595 | |
ed58dbea | 1596 | UnRef(); |
995612e2 | 1597 | |
ed58dbea | 1598 | m_refData = newRefData; |
01111366 RR |
1599 | } |
1600 | ||
4013de12 | 1601 | void wxImage::SetData( unsigned char *data, int new_width, int new_height, bool static_data ) |
f6bcfd97 BP |
1602 | { |
1603 | wxImageRefData *newRefData = new wxImageRefData(); | |
1604 | ||
1605 | if (m_refData) | |
1606 | { | |
1607 | newRefData->m_width = new_width; | |
1608 | newRefData->m_height = new_height; | |
1609 | newRefData->m_data = data; | |
70cd62e9 | 1610 | newRefData->m_ok = true; |
f6bcfd97 BP |
1611 | newRefData->m_maskRed = M_IMGDATA->m_maskRed; |
1612 | newRefData->m_maskGreen = M_IMGDATA->m_maskGreen; | |
1613 | newRefData->m_maskBlue = M_IMGDATA->m_maskBlue; | |
1614 | newRefData->m_hasMask = M_IMGDATA->m_hasMask; | |
1615 | } | |
1616 | else | |
1617 | { | |
1618 | newRefData->m_width = new_width; | |
1619 | newRefData->m_height = new_height; | |
1620 | newRefData->m_data = data; | |
70cd62e9 | 1621 | newRefData->m_ok = true; |
f6bcfd97 | 1622 | } |
4013de12 | 1623 | newRefData->m_static = static_data; |
f6bcfd97 BP |
1624 | |
1625 | UnRef(); | |
1626 | ||
1627 | m_refData = newRefData; | |
1628 | } | |
1629 | ||
487659e0 VZ |
1630 | // ---------------------------------------------------------------------------- |
1631 | // alpha channel support | |
1632 | // ---------------------------------------------------------------------------- | |
1633 | ||
1634 | void wxImage::SetAlpha(int x, int y, unsigned char alpha) | |
1635 | { | |
5644ac46 | 1636 | wxCHECK_RET( HasAlpha(), wxT("no alpha channel") ); |
487659e0 | 1637 | |
5644ac46 VZ |
1638 | long pos = XYToIndex(x, y); |
1639 | wxCHECK_RET( pos != -1, wxT("invalid image coordinates") ); | |
487659e0 | 1640 | |
a0f81e9f PC |
1641 | AllocExclusive(); |
1642 | ||
5644ac46 | 1643 | M_IMGDATA->m_alpha[pos] = alpha; |
487659e0 VZ |
1644 | } |
1645 | ||
d30ee785 | 1646 | unsigned char wxImage::GetAlpha(int x, int y) const |
487659e0 | 1647 | { |
5644ac46 | 1648 | wxCHECK_MSG( HasAlpha(), 0, wxT("no alpha channel") ); |
487659e0 | 1649 | |
5644ac46 VZ |
1650 | long pos = XYToIndex(x, y); |
1651 | wxCHECK_MSG( pos != -1, 0, wxT("invalid image coordinates") ); | |
487659e0 | 1652 | |
5644ac46 | 1653 | return M_IMGDATA->m_alpha[pos]; |
487659e0 VZ |
1654 | } |
1655 | ||
5644ac46 VZ |
1656 | bool |
1657 | wxImage::ConvertColourToAlpha(unsigned char r, unsigned char g, unsigned char b) | |
6408deed | 1658 | { |
5644ac46 | 1659 | SetAlpha(NULL); |
b713f891 | 1660 | |
5644ac46 VZ |
1661 | const int w = M_IMGDATA->m_width; |
1662 | const int h = M_IMGDATA->m_height; | |
b713f891 | 1663 | |
6408deed RR |
1664 | unsigned char *alpha = GetAlpha(); |
1665 | unsigned char *data = GetData(); | |
b713f891 | 1666 | |
5644ac46 VZ |
1667 | for ( int y = 0; y < h; y++ ) |
1668 | { | |
1669 | for ( int x = 0; x < w; x++ ) | |
1670 | { | |
1671 | *alpha++ = *data; | |
1672 | *data++ = r; | |
1673 | *data++ = g; | |
1674 | *data++ = b; | |
1675 | } | |
1676 | } | |
6408deed RR |
1677 | |
1678 | return true; | |
1679 | } | |
1680 | ||
4013de12 | 1681 | void wxImage::SetAlpha( unsigned char *alpha, bool static_data ) |
487659e0 VZ |
1682 | { |
1683 | wxCHECK_RET( Ok(), wxT("invalid image") ); | |
1684 | ||
a0f81e9f PC |
1685 | AllocExclusive(); |
1686 | ||
487659e0 VZ |
1687 | if ( !alpha ) |
1688 | { | |
edf8e8e0 | 1689 | alpha = (unsigned char *)malloc(M_IMGDATA->m_width*M_IMGDATA->m_height); |
487659e0 VZ |
1690 | } |
1691 | ||
eb86e775 VZ |
1692 | if( !M_IMGDATA->m_staticAlpha ) |
1693 | free(M_IMGDATA->m_alpha); | |
1694 | ||
487659e0 | 1695 | M_IMGDATA->m_alpha = alpha; |
d2502f14 | 1696 | M_IMGDATA->m_staticAlpha = static_data; |
487659e0 VZ |
1697 | } |
1698 | ||
1699 | unsigned char *wxImage::GetAlpha() const | |
1700 | { | |
1701 | wxCHECK_MSG( Ok(), (unsigned char *)NULL, wxT("invalid image") ); | |
1702 | ||
1703 | return M_IMGDATA->m_alpha; | |
1704 | } | |
1705 | ||
828f0936 VZ |
1706 | void wxImage::InitAlpha() |
1707 | { | |
1708 | wxCHECK_RET( !HasAlpha(), wxT("image already has an alpha channel") ); | |
1709 | ||
1710 | // initialize memory for alpha channel | |
1711 | SetAlpha(); | |
1712 | ||
1713 | unsigned char *alpha = M_IMGDATA->m_alpha; | |
1714 | const size_t lenAlpha = M_IMGDATA->m_width * M_IMGDATA->m_height; | |
1715 | ||
828f0936 VZ |
1716 | if ( HasMask() ) |
1717 | { | |
1718 | // use the mask to initialize the alpha channel. | |
1719 | const unsigned char * const alphaEnd = alpha + lenAlpha; | |
1720 | ||
1721 | const unsigned char mr = M_IMGDATA->m_maskRed; | |
1722 | const unsigned char mg = M_IMGDATA->m_maskGreen; | |
1723 | const unsigned char mb = M_IMGDATA->m_maskBlue; | |
1724 | for ( unsigned char *src = M_IMGDATA->m_data; | |
1725 | alpha < alphaEnd; | |
1726 | src += 3, alpha++ ) | |
1727 | { | |
1728 | *alpha = (src[0] == mr && src[1] == mg && src[2] == mb) | |
21dc4be5 VZ |
1729 | ? wxIMAGE_ALPHA_TRANSPARENT |
1730 | : wxIMAGE_ALPHA_OPAQUE; | |
828f0936 VZ |
1731 | } |
1732 | ||
1733 | M_IMGDATA->m_hasMask = false; | |
1734 | } | |
1735 | else // no mask | |
1736 | { | |
1737 | // make the image fully opaque | |
21dc4be5 | 1738 | memset(alpha, wxIMAGE_ALPHA_OPAQUE, lenAlpha); |
828f0936 VZ |
1739 | } |
1740 | } | |
1741 | ||
487659e0 VZ |
1742 | // ---------------------------------------------------------------------------- |
1743 | // mask support | |
1744 | // ---------------------------------------------------------------------------- | |
1745 | ||
01111366 RR |
1746 | void wxImage::SetMaskColour( unsigned char r, unsigned char g, unsigned char b ) |
1747 | { | |
223d09f6 | 1748 | wxCHECK_RET( Ok(), wxT("invalid image") ); |
c7abc967 | 1749 | |
a0f81e9f PC |
1750 | AllocExclusive(); |
1751 | ||
fd0eed64 RR |
1752 | M_IMGDATA->m_maskRed = r; |
1753 | M_IMGDATA->m_maskGreen = g; | |
1754 | M_IMGDATA->m_maskBlue = b; | |
70cd62e9 | 1755 | M_IMGDATA->m_hasMask = true; |
01111366 RR |
1756 | } |
1757 | ||
b737ad10 RR |
1758 | bool wxImage::GetOrFindMaskColour( unsigned char *r, unsigned char *g, unsigned char *b ) const |
1759 | { | |
1760 | wxCHECK_MSG( Ok(), false, wxT("invalid image") ); | |
1761 | ||
1762 | if (M_IMGDATA->m_hasMask) | |
1763 | { | |
1764 | if (r) *r = M_IMGDATA->m_maskRed; | |
1765 | if (g) *g = M_IMGDATA->m_maskGreen; | |
1766 | if (b) *b = M_IMGDATA->m_maskBlue; | |
1767 | return true; | |
1768 | } | |
1769 | else | |
1770 | { | |
1771 | FindFirstUnusedColour(r, g, b); | |
1772 | return false; | |
1773 | } | |
1774 | } | |
1775 | ||
01111366 RR |
1776 | unsigned char wxImage::GetMaskRed() const |
1777 | { | |
223d09f6 | 1778 | wxCHECK_MSG( Ok(), 0, wxT("invalid image") ); |
c7abc967 | 1779 | |
fd0eed64 | 1780 | return M_IMGDATA->m_maskRed; |
01111366 RR |
1781 | } |
1782 | ||
1783 | unsigned char wxImage::GetMaskGreen() const | |
1784 | { | |
223d09f6 | 1785 | wxCHECK_MSG( Ok(), 0, wxT("invalid image") ); |
c7abc967 | 1786 | |
fd0eed64 | 1787 | return M_IMGDATA->m_maskGreen; |
01111366 RR |
1788 | } |
1789 | ||
1790 | unsigned char wxImage::GetMaskBlue() const | |
1791 | { | |
223d09f6 | 1792 | wxCHECK_MSG( Ok(), 0, wxT("invalid image") ); |
c7abc967 | 1793 | |
fd0eed64 | 1794 | return M_IMGDATA->m_maskBlue; |
01111366 | 1795 | } |
4698648f | 1796 | |
01111366 RR |
1797 | void wxImage::SetMask( bool mask ) |
1798 | { | |
223d09f6 | 1799 | wxCHECK_RET( Ok(), wxT("invalid image") ); |
c7abc967 | 1800 | |
a0f81e9f PC |
1801 | AllocExclusive(); |
1802 | ||
fd0eed64 | 1803 | M_IMGDATA->m_hasMask = mask; |
01111366 RR |
1804 | } |
1805 | ||
1806 | bool wxImage::HasMask() const | |
1807 | { | |
70cd62e9 | 1808 | wxCHECK_MSG( Ok(), false, wxT("invalid image") ); |
c7abc967 | 1809 | |
fd0eed64 | 1810 | return M_IMGDATA->m_hasMask; |
01111366 RR |
1811 | } |
1812 | ||
21dc4be5 | 1813 | bool wxImage::IsTransparent(int x, int y, unsigned char threshold) const |
4698648f | 1814 | { |
21dc4be5 VZ |
1815 | long pos = XYToIndex(x, y); |
1816 | wxCHECK_MSG( pos != -1, false, wxT("invalid image coordinates") ); | |
c7abc967 | 1817 | |
21dc4be5 VZ |
1818 | // check mask |
1819 | if ( M_IMGDATA->m_hasMask ) | |
1820 | { | |
1821 | const unsigned char *p = M_IMGDATA->m_data + 3*pos; | |
1822 | if ( p[0] == M_IMGDATA->m_maskRed && | |
1823 | p[1] == M_IMGDATA->m_maskGreen && | |
1824 | p[2] == M_IMGDATA->m_maskBlue ) | |
1825 | { | |
1826 | return true; | |
1827 | } | |
1828 | } | |
01111366 | 1829 | |
21dc4be5 VZ |
1830 | // then check alpha |
1831 | if ( M_IMGDATA->m_alpha ) | |
1832 | { | |
1833 | if ( M_IMGDATA->m_alpha[pos] < threshold ) | |
1834 | { | |
1835 | // transparent enough | |
1836 | return true; | |
1837 | } | |
1838 | } | |
c7abc967 | 1839 | |
21dc4be5 VZ |
1840 | // not transparent |
1841 | return false; | |
01111366 RR |
1842 | } |
1843 | ||
487659e0 | 1844 | bool wxImage::SetMaskFromImage(const wxImage& mask, |
1f5b2017 | 1845 | unsigned char mr, unsigned char mg, unsigned char mb) |
52b64b0a | 1846 | { |
52b64b0a RR |
1847 | // check that the images are the same size |
1848 | if ( (M_IMGDATA->m_height != mask.GetHeight() ) || (M_IMGDATA->m_width != mask.GetWidth () ) ) | |
1849 | { | |
bc88f66f | 1850 | wxLogError( _("Image and mask have different sizes.") ); |
70cd62e9 | 1851 | return false; |
52b64b0a | 1852 | } |
487659e0 | 1853 | |
52b64b0a RR |
1854 | // find unused colour |
1855 | unsigned char r,g,b ; | |
1f5b2017 | 1856 | if (!FindFirstUnusedColour(&r, &g, &b)) |
52b64b0a | 1857 | { |
bc88f66f | 1858 | wxLogError( _("No unused colour in image being masked.") ); |
70cd62e9 | 1859 | return false ; |
52b64b0a | 1860 | } |
487659e0 | 1861 | |
a0f81e9f PC |
1862 | AllocExclusive(); |
1863 | ||
487659e0 VZ |
1864 | unsigned char *imgdata = GetData(); |
1865 | unsigned char *maskdata = mask.GetData(); | |
52b64b0a RR |
1866 | |
1867 | const int w = GetWidth(); | |
1868 | const int h = GetHeight(); | |
1869 | ||
1870 | for (int j = 0; j < h; j++) | |
1f5b2017 | 1871 | { |
52b64b0a RR |
1872 | for (int i = 0; i < w; i++) |
1873 | { | |
1f5b2017 | 1874 | if ((maskdata[0] == mr) && (maskdata[1] == mg) && (maskdata[2] == mb)) |
52b64b0a RR |
1875 | { |
1876 | imgdata[0] = r; | |
1877 | imgdata[1] = g; | |
1878 | imgdata[2] = b; | |
1879 | } | |
1880 | imgdata += 3; | |
1881 | maskdata += 3; | |
1882 | } | |
1f5b2017 | 1883 | } |
52b64b0a | 1884 | |
1f5b2017 | 1885 | SetMaskColour(r, g, b); |
70cd62e9 | 1886 | SetMask(true); |
487659e0 | 1887 | |
70cd62e9 | 1888 | return true; |
52b64b0a | 1889 | } |
7beb59f3 | 1890 | |
8f2b21e4 | 1891 | bool wxImage::ConvertAlphaToMask(unsigned char threshold) |
ff5ad794 VS |
1892 | { |
1893 | if (!HasAlpha()) | |
1894 | return true; | |
1895 | ||
1896 | unsigned char mr, mg, mb; | |
1897 | if (!FindFirstUnusedColour(&mr, &mg, &mb)) | |
1898 | { | |
1899 | wxLogError( _("No unused colour in image being masked.") ); | |
1900 | return false; | |
1901 | } | |
94406a49 | 1902 | |
a0f81e9f PC |
1903 | AllocExclusive(); |
1904 | ||
ff5ad794 VS |
1905 | SetMask(true); |
1906 | SetMaskColour(mr, mg, mb); | |
94406a49 | 1907 | |
ff5ad794 VS |
1908 | unsigned char *imgdata = GetData(); |
1909 | unsigned char *alphadata = GetAlpha(); | |
1910 | ||
8f2b21e4 VS |
1911 | int w = GetWidth(); |
1912 | int h = GetHeight(); | |
ff5ad794 | 1913 | |
8f2b21e4 | 1914 | for (int y = 0; y < h; y++) |
ff5ad794 | 1915 | { |
8f2b21e4 | 1916 | for (int x = 0; x < w; x++, imgdata += 3, alphadata++) |
ff5ad794 | 1917 | { |
e95f0d79 | 1918 | if (*alphadata < threshold) |
ff5ad794 VS |
1919 | { |
1920 | imgdata[0] = mr; | |
1921 | imgdata[1] = mg; | |
1922 | imgdata[2] = mb; | |
1923 | } | |
1924 | } | |
1925 | } | |
1926 | ||
eb86e775 VZ |
1927 | if( !M_IMGDATA->m_staticAlpha ) |
1928 | free(M_IMGDATA->m_alpha); | |
1929 | ||
ff5ad794 | 1930 | M_IMGDATA->m_alpha = NULL; |
eb86e775 | 1931 | M_IMGDATA->m_staticAlpha = false; |
94406a49 DS |
1932 | |
1933 | return true; | |
ff5ad794 | 1934 | } |
52b64b0a | 1935 | |
21dc4be5 | 1936 | // ---------------------------------------------------------------------------- |
5e5437e0 | 1937 | // Palette functions |
21dc4be5 VZ |
1938 | // ---------------------------------------------------------------------------- |
1939 | ||
1940 | #if wxUSE_PALETTE | |
5e5437e0 JS |
1941 | |
1942 | bool wxImage::HasPalette() const | |
1943 | { | |
1944 | if (!Ok()) | |
70cd62e9 | 1945 | return false; |
5e5437e0 JS |
1946 | |
1947 | return M_IMGDATA->m_palette.Ok(); | |
1948 | } | |
1949 | ||
1950 | const wxPalette& wxImage::GetPalette() const | |
1951 | { | |
1952 | wxCHECK_MSG( Ok(), wxNullPalette, wxT("invalid image") ); | |
1953 | ||
1954 | return M_IMGDATA->m_palette; | |
1955 | } | |
1956 | ||
1957 | void wxImage::SetPalette(const wxPalette& palette) | |
1958 | { | |
1959 | wxCHECK_RET( Ok(), wxT("invalid image") ); | |
1960 | ||
a0f81e9f PC |
1961 | AllocExclusive(); |
1962 | ||
5e5437e0 JS |
1963 | M_IMGDATA->m_palette = palette; |
1964 | } | |
1965 | ||
d275c7eb VZ |
1966 | #endif // wxUSE_PALETTE |
1967 | ||
21dc4be5 | 1968 | // ---------------------------------------------------------------------------- |
5e5437e0 | 1969 | // Option functions (arbitrary name/value mapping) |
21dc4be5 VZ |
1970 | // ---------------------------------------------------------------------------- |
1971 | ||
5e5437e0 JS |
1972 | void wxImage::SetOption(const wxString& name, const wxString& value) |
1973 | { | |
1974 | wxCHECK_RET( Ok(), wxT("invalid image") ); | |
1975 | ||
a0f81e9f PC |
1976 | AllocExclusive(); |
1977 | ||
70cd62e9 | 1978 | int idx = M_IMGDATA->m_optionNames.Index(name, false); |
5e5437e0 JS |
1979 | if (idx == wxNOT_FOUND) |
1980 | { | |
1981 | M_IMGDATA->m_optionNames.Add(name); | |
1982 | M_IMGDATA->m_optionValues.Add(value); | |
1983 | } | |
1984 | else | |
1985 | { | |
1986 | M_IMGDATA->m_optionNames[idx] = name; | |
1987 | M_IMGDATA->m_optionValues[idx] = value; | |
1988 | } | |
1989 | } | |
1990 | ||
1991 | void wxImage::SetOption(const wxString& name, int value) | |
1992 | { | |
1993 | wxString valStr; | |
1994 | valStr.Printf(wxT("%d"), value); | |
1995 | SetOption(name, valStr); | |
1996 | } | |
1997 | ||
1998 | wxString wxImage::GetOption(const wxString& name) const | |
1999 | { | |
2000 | wxCHECK_MSG( Ok(), wxEmptyString, wxT("invalid image") ); | |
2001 | ||
70cd62e9 | 2002 | int idx = M_IMGDATA->m_optionNames.Index(name, false); |
5e5437e0 JS |
2003 | if (idx == wxNOT_FOUND) |
2004 | return wxEmptyString; | |
2005 | else | |
2006 | return M_IMGDATA->m_optionValues[idx]; | |
2007 | } | |
2008 | ||
2009 | int wxImage::GetOptionInt(const wxString& name) const | |
2010 | { | |
5e5437e0 JS |
2011 | return wxAtoi(GetOption(name)); |
2012 | } | |
2013 | ||
2014 | bool wxImage::HasOption(const wxString& name) const | |
2015 | { | |
70cd62e9 | 2016 | wxCHECK_MSG( Ok(), false, wxT("invalid image") ); |
5e5437e0 | 2017 | |
70cd62e9 | 2018 | return (M_IMGDATA->m_optionNames.Index(name, false) != wxNOT_FOUND); |
5e5437e0 JS |
2019 | } |
2020 | ||
21dc4be5 VZ |
2021 | // ---------------------------------------------------------------------------- |
2022 | // image I/O | |
2023 | // ---------------------------------------------------------------------------- | |
2024 | ||
9a6384ca | 2025 | bool wxImage::LoadFile( const wxString& WXUNUSED_UNLESS_STREAMS(filename), |
e98e625c | 2026 | wxBitmapType WXUNUSED_UNLESS_STREAMS(type), |
9a6384ca | 2027 | int WXUNUSED_UNLESS_STREAMS(index) ) |
01111366 | 2028 | { |
6632225c | 2029 | #if HAS_FILE_STREAMS |
d80207c3 | 2030 | if (wxFileExists(filename)) |
6c28fd33 | 2031 | { |
6632225c | 2032 | wxImageFileInputStream stream(filename); |
d80207c3 | 2033 | wxBufferedInputStream bstream( stream ); |
60d43ad8 | 2034 | return LoadFile(bstream, type, index); |
6c28fd33 | 2035 | } |
d80207c3 | 2036 | else |
6c28fd33 | 2037 | { |
d80207c3 KB |
2038 | wxLogError( _("Can't load image from file '%s': file does not exist."), filename.c_str() ); |
2039 | ||
70cd62e9 | 2040 | return false; |
6c28fd33 | 2041 | } |
6632225c | 2042 | #else // !HAS_FILE_STREAMS |
70cd62e9 | 2043 | return false; |
6632225c | 2044 | #endif // HAS_FILE_STREAMS |
9e9ee68e VS |
2045 | } |
2046 | ||
9a6384ca WS |
2047 | bool wxImage::LoadFile( const wxString& WXUNUSED_UNLESS_STREAMS(filename), |
2048 | const wxString& WXUNUSED_UNLESS_STREAMS(mimetype), | |
2049 | int WXUNUSED_UNLESS_STREAMS(index) ) | |
9e9ee68e | 2050 | { |
6632225c | 2051 | #if HAS_FILE_STREAMS |
d80207c3 | 2052 | if (wxFileExists(filename)) |
6c28fd33 | 2053 | { |
6632225c | 2054 | wxImageFileInputStream stream(filename); |
d80207c3 | 2055 | wxBufferedInputStream bstream( stream ); |
60d43ad8 | 2056 | return LoadFile(bstream, mimetype, index); |
6c28fd33 | 2057 | } |
d80207c3 | 2058 | else |
6c28fd33 | 2059 | { |
d80207c3 KB |
2060 | wxLogError( _("Can't load image from file '%s': file does not exist."), filename.c_str() ); |
2061 | ||
70cd62e9 | 2062 | return false; |
6c28fd33 | 2063 | } |
6632225c | 2064 | #else // !HAS_FILE_STREAMS |
70cd62e9 | 2065 | return false; |
6632225c | 2066 | #endif // HAS_FILE_STREAMS |
1ccbb61a VZ |
2067 | } |
2068 | ||
45647dcf | 2069 | |
45647dcf VS |
2070 | bool wxImage::SaveFile( const wxString& filename ) const |
2071 | { | |
2072 | wxString ext = filename.AfterLast('.').Lower(); | |
487659e0 | 2073 | |
e98e625c VZ |
2074 | wxImageHandler *handler = FindHandler(ext, wxBITMAP_TYPE_ANY); |
2075 | if ( !handler) | |
45647dcf | 2076 | { |
e98e625c VZ |
2077 | wxLogError(_("Can't save image to file '%s': unknown extension."), |
2078 | filename); | |
2079 | return false; | |
45647dcf VS |
2080 | } |
2081 | ||
e98e625c | 2082 | return SaveFile(filename, handler->GetType()); |
45647dcf VS |
2083 | } |
2084 | ||
9a6384ca | 2085 | bool wxImage::SaveFile( const wxString& WXUNUSED_UNLESS_STREAMS(filename), |
e98e625c | 2086 | wxBitmapType WXUNUSED_UNLESS_STREAMS(type) ) const |
1ccbb61a | 2087 | { |
6632225c | 2088 | #if HAS_FILE_STREAMS |
2a736739 VZ |
2089 | wxCHECK_MSG( Ok(), false, wxT("invalid image") ); |
2090 | ||
36aac195 | 2091 | ((wxImage*)this)->SetOption(wxIMAGE_OPTION_FILENAME, filename); |
fd94e8aa | 2092 | |
6632225c | 2093 | wxImageFileOutputStream stream(filename); |
9e9ee68e | 2094 | |
2b5f62a0 | 2095 | if ( stream.IsOk() ) |
1b055864 | 2096 | { |
069d0f27 | 2097 | wxBufferedOutputStream bstream( stream ); |
1b055864 RR |
2098 | return SaveFile(bstream, type); |
2099 | } | |
6632225c | 2100 | #endif // HAS_FILE_STREAMS |
3ca6a5f0 | 2101 | |
70cd62e9 | 2102 | return false; |
3d05544e | 2103 | } |
01111366 | 2104 | |
9a6384ca WS |
2105 | bool wxImage::SaveFile( const wxString& WXUNUSED_UNLESS_STREAMS(filename), |
2106 | const wxString& WXUNUSED_UNLESS_STREAMS(mimetype) ) const | |
9e9ee68e | 2107 | { |
6632225c | 2108 | #if HAS_FILE_STREAMS |
2a736739 VZ |
2109 | wxCHECK_MSG( Ok(), false, wxT("invalid image") ); |
2110 | ||
36aac195 | 2111 | ((wxImage*)this)->SetOption(wxIMAGE_OPTION_FILENAME, filename); |
fd94e8aa | 2112 | |
6632225c | 2113 | wxImageFileOutputStream stream(filename); |
c7abc967 | 2114 | |
2b5f62a0 | 2115 | if ( stream.IsOk() ) |
1b055864 | 2116 | { |
069d0f27 | 2117 | wxBufferedOutputStream bstream( stream ); |
1b055864 RR |
2118 | return SaveFile(bstream, mimetype); |
2119 | } | |
6632225c | 2120 | #endif // HAS_FILE_STREAMS |
3ca6a5f0 | 2121 | |
70cd62e9 | 2122 | return false; |
9e9ee68e VS |
2123 | } |
2124 | ||
9a6384ca | 2125 | bool wxImage::CanRead( const wxString& WXUNUSED_UNLESS_STREAMS(name) ) |
87202f78 | 2126 | { |
6632225c VS |
2127 | #if HAS_FILE_STREAMS |
2128 | wxImageFileInputStream stream(name); | |
9a6384ca | 2129 | return CanRead(stream); |
87202f78 | 2130 | #else |
9a6384ca | 2131 | return false; |
87202f78 SB |
2132 | #endif |
2133 | } | |
2134 | ||
9a6384ca | 2135 | int wxImage::GetImageCount( const wxString& WXUNUSED_UNLESS_STREAMS(name), |
e98e625c | 2136 | wxBitmapType WXUNUSED_UNLESS_STREAMS(type) ) |
60d43ad8 | 2137 | { |
6632225c VS |
2138 | #if HAS_FILE_STREAMS |
2139 | wxImageFileInputStream stream(name); | |
9a6384ca WS |
2140 | if (stream.Ok()) |
2141 | return GetImageCount(stream, type); | |
60d43ad8 | 2142 | #endif |
2c0a4e08 VZ |
2143 | |
2144 | return 0; | |
60d43ad8 VS |
2145 | } |
2146 | ||
e02afc7a | 2147 | #if wxUSE_STREAMS |
deb2fec0 | 2148 | |
87202f78 SB |
2149 | bool wxImage::CanRead( wxInputStream &stream ) |
2150 | { | |
79fa2374 | 2151 | const wxList& list = GetHandlers(); |
004fd0c8 | 2152 | |
222ed1d6 | 2153 | for ( wxList::compatibility_iterator node = list.GetFirst(); node; node = node->GetNext() ) |
004fd0c8 | 2154 | { |
60d43ad8 VS |
2155 | wxImageHandler *handler=(wxImageHandler*)node->GetData(); |
2156 | if (handler->CanRead( stream )) | |
70cd62e9 | 2157 | return true; |
87202f78 SB |
2158 | } |
2159 | ||
70cd62e9 | 2160 | return false; |
60d43ad8 VS |
2161 | } |
2162 | ||
e98e625c | 2163 | int wxImage::GetImageCount( wxInputStream &stream, wxBitmapType type ) |
60d43ad8 VS |
2164 | { |
2165 | wxImageHandler *handler; | |
2166 | ||
2167 | if ( type == wxBITMAP_TYPE_ANY ) | |
2168 | { | |
f71b0c2d | 2169 | const wxList& list = GetHandlers(); |
60d43ad8 | 2170 | |
f71b0c2d VZ |
2171 | for ( wxList::compatibility_iterator node = list.GetFirst(); |
2172 | node; | |
2173 | node = node->GetNext() ) | |
60d43ad8 | 2174 | { |
f71b0c2d | 2175 | handler = (wxImageHandler*)node->GetData(); |
60d43ad8 | 2176 | if ( handler->CanRead(stream) ) |
f71b0c2d VZ |
2177 | { |
2178 | const int count = handler->GetImageCount(stream); | |
2179 | if ( count >= 0 ) | |
2180 | return count; | |
2181 | } | |
60d43ad8 VS |
2182 | |
2183 | } | |
2184 | ||
2185 | wxLogWarning(_("No handler found for image type.")); | |
2186 | return 0; | |
2187 | } | |
2188 | ||
2189 | handler = FindHandler(type); | |
2190 | ||
2191 | if ( !handler ) | |
2192 | { | |
e772e330 | 2193 | wxLogWarning(_("No image handler for type %ld defined."), type); |
70cd62e9 | 2194 | return false; |
60d43ad8 VS |
2195 | } |
2196 | ||
2197 | if ( handler->CanRead(stream) ) | |
2198 | { | |
649d13e8 | 2199 | return handler->GetImageCount(stream); |
60d43ad8 VS |
2200 | } |
2201 | else | |
2202 | { | |
e772e330 | 2203 | wxLogError(_("Image file is not of type %ld."), type); |
60d43ad8 VS |
2204 | return 0; |
2205 | } | |
87202f78 SB |
2206 | } |
2207 | ||
591d3fa2 VZ |
2208 | bool wxImage::DoLoad(wxImageHandler& handler, wxInputStream& stream, int index) |
2209 | { | |
2210 | if ( !handler.LoadFile(this, stream, true/*verbose*/, index) ) | |
2211 | return false; | |
2212 | ||
2213 | M_IMGDATA->m_type = handler.GetType(); | |
2214 | return true; | |
2215 | } | |
2216 | ||
e98e625c | 2217 | bool wxImage::LoadFile( wxInputStream& stream, wxBitmapType type, int index ) |
3d05544e JS |
2218 | { |
2219 | UnRef(); | |
c7abc967 | 2220 | |
fd0eed64 | 2221 | m_refData = new wxImageRefData; |
c7abc967 | 2222 | |
deb2fec0 SB |
2223 | wxImageHandler *handler; |
2224 | ||
60d43ad8 | 2225 | if ( type == wxBITMAP_TYPE_ANY ) |
deb2fec0 | 2226 | { |
f71b0c2d VZ |
2227 | const wxList& list = GetHandlers(); |
2228 | for ( wxList::compatibility_iterator node = list.GetFirst(); | |
2229 | node; | |
2230 | node = node->GetNext() ) | |
995612e2 | 2231 | { |
f71b0c2d | 2232 | handler = (wxImageHandler*)node->GetData(); |
591d3fa2 | 2233 | if ( handler->CanRead(stream) && DoLoad(*handler, stream, index) ) |
f71b0c2d | 2234 | return true; |
995612e2 | 2235 | } |
deb2fec0 | 2236 | |
58c837a4 | 2237 | wxLogWarning( _("No handler found for image type.") ); |
f71b0c2d | 2238 | |
70cd62e9 | 2239 | return false; |
deb2fec0 | 2240 | } |
e98e625c | 2241 | //else: have specific type |
deb2fec0 SB |
2242 | |
2243 | handler = FindHandler(type); | |
e98e625c | 2244 | if ( !handler ) |
fd0eed64 | 2245 | { |
e772e330 | 2246 | wxLogWarning( _("No image handler for type %ld defined."), type ); |
70cd62e9 | 2247 | return false; |
fd0eed64 | 2248 | } |
c7abc967 | 2249 | |
e98e625c | 2250 | if ( stream.IsSeekable() && !handler->CanRead(stream) ) |
dd9ea234 | 2251 | { |
e772e330 | 2252 | wxLogError(_("Image file is not of type %ld."), type); |
dd9ea234 JS |
2253 | return false; |
2254 | } | |
e98e625c | 2255 | |
591d3fa2 | 2256 | return DoLoad(*handler, stream, index); |
01111366 RR |
2257 | } |
2258 | ||
60d43ad8 | 2259 | bool wxImage::LoadFile( wxInputStream& stream, const wxString& mimetype, int index ) |
9e9ee68e VS |
2260 | { |
2261 | UnRef(); | |
2262 | ||
2263 | m_refData = new wxImageRefData; | |
2264 | ||
2265 | wxImageHandler *handler = FindHandlerMime(mimetype); | |
2266 | ||
e98e625c | 2267 | if ( !handler ) |
9e9ee68e | 2268 | { |
58c837a4 | 2269 | wxLogWarning( _("No image handler for type %s defined."), mimetype.GetData() ); |
70cd62e9 | 2270 | return false; |
9e9ee68e VS |
2271 | } |
2272 | ||
e98e625c | 2273 | if ( stream.IsSeekable() && !handler->CanRead(stream) ) |
dd9ea234 | 2274 | { |
86501081 | 2275 | wxLogError(_("Image file is not of type %s."), mimetype); |
dd9ea234 JS |
2276 | return false; |
2277 | } | |
e98e625c | 2278 | |
591d3fa2 VZ |
2279 | return DoLoad(*handler, stream, index); |
2280 | } | |
2281 | ||
2282 | bool wxImage::DoSave(wxImageHandler& handler, wxOutputStream& stream) const | |
2283 | { | |
2284 | wxImage * const self = wx_const_cast(wxImage *, this); | |
2285 | if ( !handler.SaveFile(self, stream) ) | |
2286 | return false; | |
2287 | ||
2288 | M_IMGDATA->m_type = handler.GetType(); | |
2289 | return true; | |
9e9ee68e VS |
2290 | } |
2291 | ||
e98e625c | 2292 | bool wxImage::SaveFile( wxOutputStream& stream, wxBitmapType type ) const |
01111366 | 2293 | { |
70cd62e9 | 2294 | wxCHECK_MSG( Ok(), false, wxT("invalid image") ); |
c7abc967 | 2295 | |
fd0eed64 | 2296 | wxImageHandler *handler = FindHandler(type); |
2a736739 | 2297 | if ( !handler ) |
fd0eed64 | 2298 | { |
58c837a4 | 2299 | wxLogWarning( _("No image handler for type %d defined."), type ); |
70cd62e9 | 2300 | return false; |
9e9ee68e VS |
2301 | } |
2302 | ||
591d3fa2 | 2303 | return DoSave(*handler, stream); |
9e9ee68e VS |
2304 | } |
2305 | ||
e0a76d8d | 2306 | bool wxImage::SaveFile( wxOutputStream& stream, const wxString& mimetype ) const |
9e9ee68e | 2307 | { |
70cd62e9 | 2308 | wxCHECK_MSG( Ok(), false, wxT("invalid image") ); |
c7abc967 | 2309 | |
9e9ee68e | 2310 | wxImageHandler *handler = FindHandlerMime(mimetype); |
2a736739 | 2311 | if ( !handler ) |
9e9ee68e | 2312 | { |
58c837a4 | 2313 | wxLogWarning( _("No image handler for type %s defined."), mimetype.GetData() ); |
fd0eed64 | 2314 | } |
c7abc967 | 2315 | |
591d3fa2 | 2316 | return DoSave(*handler, stream); |
01111366 | 2317 | } |
e98e625c | 2318 | |
e02afc7a | 2319 | #endif // wxUSE_STREAMS |
01111366 | 2320 | |
21dc4be5 VZ |
2321 | // ---------------------------------------------------------------------------- |
2322 | // image I/O handlers | |
2323 | // ---------------------------------------------------------------------------- | |
2324 | ||
01111366 RR |
2325 | void wxImage::AddHandler( wxImageHandler *handler ) |
2326 | { | |
2b5f62a0 VZ |
2327 | // Check for an existing handler of the type being added. |
2328 | if (FindHandler( handler->GetType() ) == 0) | |
2329 | { | |
2330 | sm_handlers.Append( handler ); | |
2331 | } | |
2332 | else | |
2333 | { | |
2334 | // This is not documented behaviour, merely the simplest 'fix' | |
2335 | // for preventing duplicate additions. If someone ever has | |
2336 | // a good reason to add and remove duplicate handlers (and they | |
2337 | // may) we should probably refcount the duplicates. | |
2338 | // also an issue in InsertHandler below. | |
2339 | ||
2340 | wxLogDebug( _T("Adding duplicate image handler for '%s'"), | |
2341 | handler->GetName().c_str() ); | |
2342 | delete handler; | |
2343 | } | |
01111366 RR |
2344 | } |
2345 | ||
2346 | void wxImage::InsertHandler( wxImageHandler *handler ) | |
2347 | { | |
2b5f62a0 VZ |
2348 | // Check for an existing handler of the type being added. |
2349 | if (FindHandler( handler->GetType() ) == 0) | |
2350 | { | |
2351 | sm_handlers.Insert( handler ); | |
2352 | } | |
2353 | else | |
2354 | { | |
2355 | // see AddHandler for additional comments. | |
2356 | wxLogDebug( _T("Inserting duplicate image handler for '%s'"), | |
2357 | handler->GetName().c_str() ); | |
2358 | delete handler; | |
2359 | } | |
01111366 RR |
2360 | } |
2361 | ||
2362 | bool wxImage::RemoveHandler( const wxString& name ) | |
2363 | { | |
fd0eed64 RR |
2364 | wxImageHandler *handler = FindHandler(name); |
2365 | if (handler) | |
2366 | { | |
2367 | sm_handlers.DeleteObject(handler); | |
222ed1d6 | 2368 | delete handler; |
70cd62e9 | 2369 | return true; |
fd0eed64 RR |
2370 | } |
2371 | else | |
70cd62e9 | 2372 | return false; |
01111366 RR |
2373 | } |
2374 | ||
2375 | wxImageHandler *wxImage::FindHandler( const wxString& name ) | |
2376 | { | |
222ed1d6 | 2377 | wxList::compatibility_iterator node = sm_handlers.GetFirst(); |
fd0eed64 RR |
2378 | while (node) |
2379 | { | |
b1d4dd7a | 2380 | wxImageHandler *handler = (wxImageHandler*)node->GetData(); |
ce3ed50d | 2381 | if (handler->GetName().Cmp(name) == 0) return handler; |
c7abc967 | 2382 | |
b1d4dd7a | 2383 | node = node->GetNext(); |
fd0eed64 | 2384 | } |
b4c26503 | 2385 | return NULL; |
01111366 RR |
2386 | } |
2387 | ||
e98e625c | 2388 | wxImageHandler *wxImage::FindHandler( const wxString& extension, wxBitmapType bitmapType ) |
01111366 | 2389 | { |
222ed1d6 | 2390 | wxList::compatibility_iterator node = sm_handlers.GetFirst(); |
fd0eed64 RR |
2391 | while (node) |
2392 | { | |
b1d4dd7a | 2393 | wxImageHandler *handler = (wxImageHandler*)node->GetData(); |
ce3ed50d | 2394 | if ( (handler->GetExtension().Cmp(extension) == 0) && |
e98e625c VZ |
2395 | ( (bitmapType == wxBITMAP_TYPE_ANY) || (handler->GetType() == bitmapType)) ) |
2396 | { | |
dbda9e86 | 2397 | return handler; |
e98e625c | 2398 | } |
b1d4dd7a | 2399 | node = node->GetNext(); |
fd0eed64 | 2400 | } |
b4c26503 | 2401 | return NULL; |
01111366 RR |
2402 | } |
2403 | ||
e98e625c | 2404 | wxImageHandler *wxImage::FindHandler(wxBitmapType bitmapType ) |
01111366 | 2405 | { |
222ed1d6 | 2406 | wxList::compatibility_iterator node = sm_handlers.GetFirst(); |
fd0eed64 RR |
2407 | while (node) |
2408 | { | |
b1d4dd7a | 2409 | wxImageHandler *handler = (wxImageHandler *)node->GetData(); |
fd0eed64 | 2410 | if (handler->GetType() == bitmapType) return handler; |
b1d4dd7a | 2411 | node = node->GetNext(); |
fd0eed64 | 2412 | } |
b4c26503 | 2413 | return NULL; |
fd0eed64 RR |
2414 | } |
2415 | ||
9e9ee68e VS |
2416 | wxImageHandler *wxImage::FindHandlerMime( const wxString& mimetype ) |
2417 | { | |
222ed1d6 | 2418 | wxList::compatibility_iterator node = sm_handlers.GetFirst(); |
9e9ee68e VS |
2419 | while (node) |
2420 | { | |
b1d4dd7a | 2421 | wxImageHandler *handler = (wxImageHandler *)node->GetData(); |
70cd62e9 | 2422 | if (handler->GetMimeType().IsSameAs(mimetype, false)) return handler; |
b1d4dd7a | 2423 | node = node->GetNext(); |
9e9ee68e | 2424 | } |
b4c26503 | 2425 | return NULL; |
9e9ee68e VS |
2426 | } |
2427 | ||
fd0eed64 RR |
2428 | void wxImage::InitStandardHandlers() |
2429 | { | |
77fac225 | 2430 | #if wxUSE_STREAMS |
66e23ad2 | 2431 | AddHandler(new wxBMPHandler); |
77fac225 | 2432 | #endif // wxUSE_STREAMS |
01111366 RR |
2433 | } |
2434 | ||
2435 | void wxImage::CleanUpHandlers() | |
2436 | { | |
222ed1d6 | 2437 | wxList::compatibility_iterator node = sm_handlers.GetFirst(); |
fd0eed64 RR |
2438 | while (node) |
2439 | { | |
b1d4dd7a | 2440 | wxImageHandler *handler = (wxImageHandler *)node->GetData(); |
222ed1d6 | 2441 | wxList::compatibility_iterator next = node->GetNext(); |
fd0eed64 | 2442 | delete handler; |
fd0eed64 RR |
2443 | node = next; |
2444 | } | |
01111366 | 2445 | |
222ed1d6 MB |
2446 | sm_handlers.Clear(); |
2447 | } | |
ff865c13 | 2448 | |
939fadc8 JS |
2449 | wxString wxImage::GetImageExtWildcard() |
2450 | { | |
2451 | wxString fmts; | |
2452 | ||
2453 | wxList& Handlers = wxImage::GetHandlers(); | |
222ed1d6 | 2454 | wxList::compatibility_iterator Node = Handlers.GetFirst(); |
939fadc8 JS |
2455 | while ( Node ) |
2456 | { | |
2457 | wxImageHandler* Handler = (wxImageHandler*)Node->GetData(); | |
2458 | fmts += wxT("*.") + Handler->GetExtension(); | |
2459 | Node = Node->GetNext(); | |
2460 | if ( Node ) fmts += wxT(";"); | |
2461 | } | |
2462 | ||
2463 | return wxT("(") + fmts + wxT(")|") + fmts; | |
2464 | } | |
2465 | ||
978d3d36 VZ |
2466 | wxImage::HSVValue wxImage::RGBtoHSV(const RGBValue& rgb) |
2467 | { | |
978d3d36 VZ |
2468 | const double red = rgb.red / 255.0, |
2469 | green = rgb.green / 255.0, | |
2470 | blue = rgb.blue / 255.0; | |
2471 | ||
c77a6796 VZ |
2472 | // find the min and max intensity (and remember which one was it for the |
2473 | // latter) | |
978d3d36 | 2474 | double minimumRGB = red; |
c77a6796 | 2475 | if ( green < minimumRGB ) |
978d3d36 | 2476 | minimumRGB = green; |
c77a6796 | 2477 | if ( blue < minimumRGB ) |
978d3d36 VZ |
2478 | minimumRGB = blue; |
2479 | ||
c77a6796 | 2480 | enum { RED, GREEN, BLUE } chMax = RED; |
978d3d36 | 2481 | double maximumRGB = red; |
c77a6796 VZ |
2482 | if ( green > maximumRGB ) |
2483 | { | |
2484 | chMax = GREEN; | |
978d3d36 | 2485 | maximumRGB = green; |
c77a6796 VZ |
2486 | } |
2487 | if ( blue > maximumRGB ) | |
2488 | { | |
2489 | chMax = BLUE; | |
978d3d36 | 2490 | maximumRGB = blue; |
c77a6796 | 2491 | } |
978d3d36 | 2492 | |
c77a6796 | 2493 | const double value = maximumRGB; |
978d3d36 | 2494 | |
38d4b1e4 | 2495 | double hue = 0.0, saturation; |
c77a6796 VZ |
2496 | const double deltaRGB = maximumRGB - minimumRGB; |
2497 | if ( wxIsNullDouble(deltaRGB) ) | |
978d3d36 VZ |
2498 | { |
2499 | // Gray has no color | |
2500 | hue = 0.0; | |
2501 | saturation = 0.0; | |
2502 | } | |
2503 | else | |
2504 | { | |
c77a6796 VZ |
2505 | switch ( chMax ) |
2506 | { | |
2507 | case RED: | |
2508 | hue = (green - blue) / deltaRGB; | |
2509 | break; | |
978d3d36 | 2510 | |
c77a6796 VZ |
2511 | case GREEN: |
2512 | hue = 2.0 + (blue - red) / deltaRGB; | |
2513 | break; | |
978d3d36 | 2514 | |
c77a6796 VZ |
2515 | case BLUE: |
2516 | hue = 4.0 + (red - green) / deltaRGB; | |
2517 | break; | |
38d4b1e4 WS |
2518 | |
2519 | default: | |
2520 | wxFAIL_MSG(wxT("hue not specified")); | |
2521 | break; | |
c77a6796 VZ |
2522 | } |
2523 | ||
2524 | hue /= 6.0; | |
978d3d36 | 2525 | |
c77a6796 VZ |
2526 | if ( hue < 0.0 ) |
2527 | hue += 1.0; | |
978d3d36 | 2528 | |
c77a6796 | 2529 | saturation = deltaRGB / maximumRGB; |
978d3d36 VZ |
2530 | } |
2531 | ||
2532 | return HSVValue(hue, saturation, value); | |
2533 | } | |
2534 | ||
2535 | wxImage::RGBValue wxImage::HSVtoRGB(const HSVValue& hsv) | |
2536 | { | |
2537 | double red, green, blue; | |
2538 | ||
c77a6796 | 2539 | if ( wxIsNullDouble(hsv.saturation) ) |
978d3d36 | 2540 | { |
c77a6796 VZ |
2541 | // Grey |
2542 | red = hsv.value; | |
978d3d36 | 2543 | green = hsv.value; |
c77a6796 | 2544 | blue = hsv.value; |
978d3d36 | 2545 | } |
c77a6796 | 2546 | else // not grey |
978d3d36 VZ |
2547 | { |
2548 | double hue = hsv.hue * 6.0; // sector 0 to 5 | |
2549 | int i = (int)floor(hue); | |
2550 | double f = hue - i; // fractional part of h | |
2551 | double p = hsv.value * (1.0 - hsv.saturation); | |
2552 | ||
2553 | switch (i) | |
2554 | { | |
2555 | case 0: | |
2556 | red = hsv.value; | |
2557 | green = hsv.value * (1.0 - hsv.saturation * (1.0 - f)); | |
2558 | blue = p; | |
2559 | break; | |
2560 | ||
2561 | case 1: | |
2562 | red = hsv.value * (1.0 - hsv.saturation * f); | |
2563 | green = hsv.value; | |
2564 | blue = p; | |
2565 | break; | |
2566 | ||
2567 | case 2: | |
2568 | red = p; | |
2569 | green = hsv.value; | |
2570 | blue = hsv.value * (1.0 - hsv.saturation * (1.0 - f)); | |
2571 | break; | |
2572 | ||
2573 | case 3: | |
2574 | red = p; | |
2575 | green = hsv.value * (1.0 - hsv.saturation * f); | |
2576 | blue = hsv.value; | |
2577 | break; | |
2578 | ||
2579 | case 4: | |
2580 | red = hsv.value * (1.0 - hsv.saturation * (1.0 - f)); | |
2581 | green = p; | |
2582 | blue = hsv.value; | |
2583 | break; | |
2584 | ||
2585 | default: // case 5: | |
2586 | red = hsv.value; | |
2587 | green = p; | |
2588 | blue = hsv.value * (1.0 - hsv.saturation * f); | |
2589 | break; | |
2590 | } | |
2591 | } | |
2592 | ||
2593 | return RGBValue((unsigned char)(red * 255.0), | |
2594 | (unsigned char)(green * 255.0), | |
2595 | (unsigned char)(blue * 255.0)); | |
2596 | } | |
2597 | ||
2598 | /* | |
2599 | * Rotates the hue of each pixel of the image. angle is a double in the range | |
2600 | * -1.0..1.0 where -1.0 is -360 degrees and 1.0 is 360 degrees | |
2601 | */ | |
2602 | void wxImage::RotateHue(double angle) | |
2603 | { | |
a0f81e9f PC |
2604 | AllocExclusive(); |
2605 | ||
978d3d36 VZ |
2606 | unsigned char *srcBytePtr; |
2607 | unsigned char *dstBytePtr; | |
2608 | unsigned long count; | |
2609 | wxImage::HSVValue hsv; | |
2610 | wxImage::RGBValue rgb; | |
2611 | ||
6926b9c4 | 2612 | wxASSERT (angle >= -1.0 && angle <= 1.0); |
978d3d36 | 2613 | count = M_IMGDATA->m_width * M_IMGDATA->m_height; |
c77a6796 | 2614 | if ( count > 0 && !wxIsNullDouble(angle) ) |
978d3d36 VZ |
2615 | { |
2616 | srcBytePtr = M_IMGDATA->m_data; | |
2617 | dstBytePtr = srcBytePtr; | |
2618 | do | |
2619 | { | |
2620 | rgb.red = *srcBytePtr++; | |
2621 | rgb.green = *srcBytePtr++; | |
2622 | rgb.blue = *srcBytePtr++; | |
2623 | hsv = RGBtoHSV(rgb); | |
2624 | ||
2625 | hsv.hue = hsv.hue + angle; | |
2626 | if (hsv.hue > 1.0) | |
2627 | hsv.hue = hsv.hue - 1.0; | |
2628 | else if (hsv.hue < 0.0) | |
2629 | hsv.hue = hsv.hue + 1.0; | |
2630 | ||
2631 | rgb = HSVtoRGB(hsv); | |
2632 | *dstBytePtr++ = rgb.red; | |
2633 | *dstBytePtr++ = rgb.green; | |
2634 | *dstBytePtr++ = rgb.blue; | |
2635 | } while (--count != 0); | |
2636 | } | |
2637 | } | |
2638 | ||
01111366 RR |
2639 | //----------------------------------------------------------------------------- |
2640 | // wxImageHandler | |
2641 | //----------------------------------------------------------------------------- | |
2642 | ||
63d963a1 | 2643 | IMPLEMENT_ABSTRACT_CLASS(wxImageHandler,wxObject) |
01111366 | 2644 | |
e02afc7a | 2645 | #if wxUSE_STREAMS |
700ec454 | 2646 | bool wxImageHandler::LoadFile( wxImage *WXUNUSED(image), wxInputStream& WXUNUSED(stream), bool WXUNUSED(verbose), int WXUNUSED(index) ) |
01111366 | 2647 | { |
70cd62e9 | 2648 | return false; |
01111366 RR |
2649 | } |
2650 | ||
deb2fec0 | 2651 | bool wxImageHandler::SaveFile( wxImage *WXUNUSED(image), wxOutputStream& WXUNUSED(stream), bool WXUNUSED(verbose) ) |
01111366 | 2652 | { |
70cd62e9 | 2653 | return false; |
01111366 | 2654 | } |
0828c087 | 2655 | |
649d13e8 | 2656 | int wxImageHandler::GetImageCount( wxInputStream& WXUNUSED(stream) ) |
700ec454 RR |
2657 | { |
2658 | return 1; | |
2659 | } | |
2660 | ||
0828c087 VS |
2661 | bool wxImageHandler::CanRead( const wxString& name ) |
2662 | { | |
0828c087 VS |
2663 | if (wxFileExists(name)) |
2664 | { | |
6632225c | 2665 | wxImageFileInputStream stream(name); |
0828c087 VS |
2666 | return CanRead(stream); |
2667 | } | |
2668 | ||
39d16996 | 2669 | wxLogError( _("Can't check image format of file '%s': file does not exist."), name.c_str() ); |
0828c087 | 2670 | |
70cd62e9 | 2671 | return false; |
39d16996 VZ |
2672 | } |
2673 | ||
2674 | bool wxImageHandler::CallDoCanRead(wxInputStream& stream) | |
2675 | { | |
30984dea | 2676 | wxFileOffset posOld = stream.TellI(); |
39d16996 VZ |
2677 | if ( posOld == wxInvalidOffset ) |
2678 | { | |
2679 | // can't test unseekable stream | |
70cd62e9 | 2680 | return false; |
39d16996 VZ |
2681 | } |
2682 | ||
2683 | bool ok = DoCanRead(stream); | |
2684 | ||
2685 | // restore the old position to be able to test other formats and so on | |
2686 | if ( stream.SeekI(posOld) == wxInvalidOffset ) | |
2687 | { | |
2688 | wxLogDebug(_T("Failed to rewind the stream in wxImageHandler!")); | |
2689 | ||
2690 | // reading would fail anyhow as we're not at the right position | |
70cd62e9 | 2691 | return false; |
0828c087 | 2692 | } |
39d16996 VZ |
2693 | |
2694 | return ok; | |
0828c087 VS |
2695 | } |
2696 | ||
e02afc7a | 2697 | #endif // wxUSE_STREAMS |
01111366 | 2698 | |
361f4288 VZ |
2699 | /* static */ |
2700 | wxImageResolution | |
2701 | wxImageHandler::GetResolutionFromOptions(const wxImage& image, int *x, int *y) | |
2702 | { | |
2703 | wxCHECK_MSG( x && y, wxIMAGE_RESOLUTION_NONE, _T("NULL pointer") ); | |
2704 | ||
2705 | if ( image.HasOption(wxIMAGE_OPTION_RESOLUTIONX) && | |
2706 | image.HasOption(wxIMAGE_OPTION_RESOLUTIONY) ) | |
2707 | { | |
2708 | *x = image.GetOptionInt(wxIMAGE_OPTION_RESOLUTIONX); | |
2709 | *y = image.GetOptionInt(wxIMAGE_OPTION_RESOLUTIONY); | |
2710 | } | |
2711 | else if ( image.HasOption(wxIMAGE_OPTION_RESOLUTION) ) | |
2712 | { | |
2713 | *x = | |
2714 | *y = image.GetOptionInt(wxIMAGE_OPTION_RESOLUTION); | |
2715 | } | |
2716 | else // no resolution options specified | |
2717 | { | |
2718 | *x = | |
2719 | *y = 0; | |
2720 | ||
2721 | return wxIMAGE_RESOLUTION_NONE; | |
2722 | } | |
2723 | ||
2724 | // get the resolution unit too | |
2725 | int resUnit = image.GetOptionInt(wxIMAGE_OPTION_RESOLUTIONUNIT); | |
2726 | if ( !resUnit ) | |
2727 | { | |
2728 | // this is the default | |
2729 | resUnit = wxIMAGE_RESOLUTION_INCHES; | |
2730 | } | |
2731 | ||
2732 | return (wxImageResolution)resUnit; | |
2733 | } | |
2734 | ||
487659e0 VZ |
2735 | // ---------------------------------------------------------------------------- |
2736 | // image histogram stuff | |
2737 | // ---------------------------------------------------------------------------- | |
2738 | ||
2739 | bool | |
2740 | wxImageHistogram::FindFirstUnusedColour(unsigned char *r, | |
2741 | unsigned char *g, | |
2742 | unsigned char *b, | |
2743 | unsigned char r2, | |
2744 | unsigned char b2, | |
2745 | unsigned char g2) const | |
2746 | { | |
2747 | unsigned long key = MakeKey(r2, g2, b2); | |
2748 | ||
2749 | while ( find(key) != end() ) | |
2750 | { | |
2751 | // color already used | |
2752 | r2++; | |
2753 | if ( r2 >= 255 ) | |
2754 | { | |
2755 | r2 = 0; | |
2756 | g2++; | |
2757 | if ( g2 >= 255 ) | |
2758 | { | |
2759 | g2 = 0; | |
2760 | b2++; | |
2761 | if ( b2 >= 255 ) | |
2762 | { | |
bc88f66f | 2763 | wxLogError(_("No unused colour in image.") ); |
70cd62e9 | 2764 | return false; |
487659e0 VZ |
2765 | } |
2766 | } | |
2767 | } | |
2768 | ||
2769 | key = MakeKey(r2, g2, b2); | |
2770 | } | |
2771 | ||
2772 | if ( r ) | |
2773 | *r = r2; | |
2774 | if ( g ) | |
2775 | *g = g2; | |
2776 | if ( b ) | |
2777 | *b = b2; | |
2778 | ||
70cd62e9 | 2779 | return true; |
487659e0 VZ |
2780 | } |
2781 | ||
2782 | bool | |
2783 | wxImage::FindFirstUnusedColour(unsigned char *r, | |
2784 | unsigned char *g, | |
2785 | unsigned char *b, | |
2786 | unsigned char r2, | |
2787 | unsigned char b2, | |
2788 | unsigned char g2) const | |
2789 | { | |
2790 | wxImageHistogram histogram; | |
2791 | ||
2792 | ComputeHistogram(histogram); | |
2793 | ||
2794 | return histogram.FindFirstUnusedColour(r, g, b, r2, g2, b2); | |
2795 | } | |
2796 | ||
2797 | ||
c9d01afd | 2798 | |
89d00456 GRG |
2799 | // GRG, Dic/99 |
2800 | // Counts and returns the number of different colours. Optionally stops | |
cc9f7d79 GRG |
2801 | // when it exceeds 'stopafter' different colours. This is useful, for |
2802 | // example, to see if the image can be saved as 8-bit (256 colour or | |
2803 | // less, in this case it would be invoked as CountColours(256)). Default | |
2804 | // value for stopafter is -1 (don't care). | |
89d00456 | 2805 | // |
e0a76d8d | 2806 | unsigned long wxImage::CountColours( unsigned long stopafter ) const |
89d00456 GRG |
2807 | { |
2808 | wxHashTable h; | |
ad30de59 | 2809 | wxObject dummy; |
33ac7e6f | 2810 | unsigned char r, g, b; |
eeca3a46 | 2811 | unsigned char *p; |
89d00456 GRG |
2812 | unsigned long size, nentries, key; |
2813 | ||
2814 | p = GetData(); | |
2815 | size = GetWidth() * GetHeight(); | |
2816 | nentries = 0; | |
2817 | ||
cc9f7d79 | 2818 | for (unsigned long j = 0; (j < size) && (nentries <= stopafter) ; j++) |
89d00456 GRG |
2819 | { |
2820 | r = *(p++); | |
2821 | g = *(p++); | |
2822 | b = *(p++); | |
487659e0 | 2823 | key = wxImageHistogram::MakeKey(r, g, b); |
89d00456 | 2824 | |
ad30de59 | 2825 | if (h.Get(key) == NULL) |
89d00456 | 2826 | { |
ad30de59 | 2827 | h.Put(key, &dummy); |
89d00456 GRG |
2828 | nentries++; |
2829 | } | |
2830 | } | |
2831 | ||
89d00456 GRG |
2832 | return nentries; |
2833 | } | |
2834 | ||
2835 | ||
e0a76d8d | 2836 | unsigned long wxImage::ComputeHistogram( wxImageHistogram &h ) const |
c9d01afd | 2837 | { |
487659e0 VZ |
2838 | unsigned char *p = GetData(); |
2839 | unsigned long nentries = 0; | |
952ae1e8 VS |
2840 | |
2841 | h.clear(); | |
c9d01afd | 2842 | |
487659e0 | 2843 | const unsigned long size = GetWidth() * GetHeight(); |
c9d01afd | 2844 | |
487659e0 VZ |
2845 | unsigned char r, g, b; |
2846 | for ( unsigned long n = 0; n < size; n++ ) | |
c9d01afd | 2847 | { |
487659e0 VZ |
2848 | r = *p++; |
2849 | g = *p++; | |
2850 | b = *p++; | |
2851 | ||
2852 | wxImageHistogramEntry& entry = h[wxImageHistogram::MakeKey(r, g, b)]; | |
c9d01afd | 2853 | |
952ae1e8 VS |
2854 | if ( entry.value++ == 0 ) |
2855 | entry.index = nentries++; | |
c9d01afd GRG |
2856 | } |
2857 | ||
2858 | return nentries; | |
2859 | } | |
2860 | ||
7a632f10 JS |
2861 | /* |
2862 | * Rotation code by Carlos Moreno | |
2863 | */ | |
2864 | ||
3fff3966 | 2865 | static const double wxROTATE_EPSILON = 1e-10; |
7a632f10 JS |
2866 | |
2867 | // Auxiliary function to rotate a point (x,y) with respect to point p0 | |
2868 | // make it inline and use a straight return to facilitate optimization | |
2869 | // also, the function receives the sine and cosine of the angle to avoid | |
2870 | // repeating the time-consuming calls to these functions -- sin/cos can | |
2871 | // be computed and stored in the calling function. | |
2872 | ||
3fff3966 VZ |
2873 | static inline wxRealPoint |
2874 | wxRotatePoint(const wxRealPoint& p, double cos_angle, double sin_angle, | |
2875 | const wxRealPoint& p0) | |
7a632f10 | 2876 | { |
3fff3966 VZ |
2877 | return wxRealPoint(p0.x + (p.x - p0.x) * cos_angle - (p.y - p0.y) * sin_angle, |
2878 | p0.y + (p.y - p0.y) * cos_angle + (p.x - p0.x) * sin_angle); | |
7a632f10 JS |
2879 | } |
2880 | ||
3fff3966 VZ |
2881 | static inline wxRealPoint |
2882 | wxRotatePoint(double x, double y, double cos_angle, double sin_angle, | |
2883 | const wxRealPoint & p0) | |
7a632f10 | 2884 | { |
3fff3966 | 2885 | return wxRotatePoint (wxRealPoint(x,y), cos_angle, sin_angle, p0); |
7a632f10 JS |
2886 | } |
2887 | ||
e26d5213 VZ |
2888 | wxImage wxImage::Rotate(double angle, |
2889 | const wxPoint& centre_of_rotation, | |
2890 | bool interpolating, | |
2891 | wxPoint *offset_after_rotation) const | |
7a632f10 | 2892 | { |
e26d5213 VZ |
2893 | // screen coordinates are a mirror image of "real" coordinates |
2894 | angle = -angle; | |
2895 | ||
2896 | const bool has_alpha = HasAlpha(); | |
2897 | ||
2898 | const int w = GetWidth(); | |
2899 | const int h = GetHeight(); | |
b713f891 | 2900 | |
e26d5213 | 2901 | int i; |
7a632f10 | 2902 | |
ad30de59 | 2903 | // Create pointer-based array to accelerate access to wxImage's data |
e26d5213 | 2904 | unsigned char ** data = new unsigned char * [h]; |
b5c91ac6 | 2905 | data[0] = GetData(); |
e26d5213 VZ |
2906 | for (i = 1; i < h; i++) |
2907 | data[i] = data[i - 1] + (3 * w); | |
7a632f10 | 2908 | |
b713f891 | 2909 | // Same for alpha channel |
6408deed RR |
2910 | unsigned char ** alpha = NULL; |
2911 | if (has_alpha) | |
2912 | { | |
e26d5213 | 2913 | alpha = new unsigned char * [h]; |
6408deed | 2914 | alpha[0] = GetAlpha(); |
e26d5213 VZ |
2915 | for (i = 1; i < h; i++) |
2916 | alpha[i] = alpha[i - 1] + w; | |
6408deed RR |
2917 | } |
2918 | ||
b5c91ac6 | 2919 | // precompute coefficients for rotation formula |
7a632f10 JS |
2920 | const double cos_angle = cos(angle); |
2921 | const double sin_angle = sin(angle); | |
2922 | ||
ad30de59 GRG |
2923 | // Create new Image to store the result |
2924 | // First, find rectangle that covers the rotated image; to do that, | |
2925 | // rotate the four corners | |
7a632f10 | 2926 | |
b5c91ac6 | 2927 | const wxRealPoint p0(centre_of_rotation.x, centre_of_rotation.y); |
7a632f10 | 2928 | |
3fff3966 | 2929 | wxRealPoint p1 = wxRotatePoint (0, 0, cos_angle, sin_angle, p0); |
e26d5213 VZ |
2930 | wxRealPoint p2 = wxRotatePoint (0, h, cos_angle, sin_angle, p0); |
2931 | wxRealPoint p3 = wxRotatePoint (w, 0, cos_angle, sin_angle, p0); | |
2932 | wxRealPoint p4 = wxRotatePoint (w, h, cos_angle, sin_angle, p0); | |
7a632f10 | 2933 | |
4e115ed2 VZ |
2934 | int x1a = (int) floor (wxMin (wxMin(p1.x, p2.x), wxMin(p3.x, p4.x))); |
2935 | int y1a = (int) floor (wxMin (wxMin(p1.y, p2.y), wxMin(p3.y, p4.y))); | |
2936 | int x2a = (int) ceil (wxMax (wxMax(p1.x, p2.x), wxMax(p3.x, p4.x))); | |
2937 | int y2a = (int) ceil (wxMax (wxMax(p1.y, p2.y), wxMax(p3.y, p4.y))); | |
7a632f10 | 2938 | |
6408deed | 2939 | // Create rotated image |
4e115ed2 | 2940 | wxImage rotated (x2a - x1a + 1, y2a - y1a + 1, false); |
6408deed RR |
2941 | // With alpha channel |
2942 | if (has_alpha) | |
2943 | rotated.SetAlpha(); | |
7a632f10 JS |
2944 | |
2945 | if (offset_after_rotation != NULL) | |
2946 | { | |
4e115ed2 | 2947 | *offset_after_rotation = wxPoint (x1a, y1a); |
7a632f10 JS |
2948 | } |
2949 | ||
e26d5213 VZ |
2950 | // the rotated (destination) image is always accessed sequentially via this |
2951 | // pointer, there is no need for pointer-based arrays here | |
2952 | unsigned char *dst = rotated.GetData(); | |
b713f891 | 2953 | |
e26d5213 | 2954 | unsigned char *alpha_dst = has_alpha ? rotated.GetAlpha() : NULL; |
7a632f10 | 2955 | |
e26d5213 VZ |
2956 | // if the original image has a mask, use its RGB values as the blank pixel, |
2957 | // else, fall back to default (black). | |
b5c91ac6 GRG |
2958 | unsigned char blank_r = 0; |
2959 | unsigned char blank_g = 0; | |
2960 | unsigned char blank_b = 0; | |
ad30de59 GRG |
2961 | |
2962 | if (HasMask()) | |
2963 | { | |
b5c91ac6 GRG |
2964 | blank_r = GetMaskRed(); |
2965 | blank_g = GetMaskGreen(); | |
2966 | blank_b = GetMaskBlue(); | |
2967 | rotated.SetMaskColour( blank_r, blank_g, blank_b ); | |
ad30de59 GRG |
2968 | } |
2969 | ||
2970 | // Now, for each point of the rotated image, find where it came from, by | |
2971 | // performing an inverse rotation (a rotation of -angle) and getting the | |
2972 | // pixel at those coordinates | |
2973 | ||
e26d5213 VZ |
2974 | const int rH = rotated.GetHeight(); |
2975 | const int rW = rotated.GetWidth(); | |
7a632f10 | 2976 | |
e26d5213 VZ |
2977 | // do the (interpolating) test outside of the loops, so that it is done |
2978 | // only once, instead of repeating it for each pixel. | |
b5c91ac6 | 2979 | if (interpolating) |
7a632f10 | 2980 | { |
e26d5213 | 2981 | for (int y = 0; y < rH; y++) |
7a632f10 | 2982 | { |
e26d5213 | 2983 | for (int x = 0; x < rW; x++) |
7a632f10 | 2984 | { |
3fff3966 | 2985 | wxRealPoint src = wxRotatePoint (x + x1a, y + y1a, cos_angle, -sin_angle, p0); |
b5c91ac6 | 2986 | |
e26d5213 VZ |
2987 | if (-0.25 < src.x && src.x < w - 0.75 && |
2988 | -0.25 < src.y && src.y < h - 0.75) | |
7a632f10 | 2989 | { |
ad30de59 GRG |
2990 | // interpolate using the 4 enclosing grid-points. Those |
2991 | // points can be obtained using floor and ceiling of the | |
2992 | // exact coordinates of the point | |
f2506310 JS |
2993 | int x1, y1, x2, y2; |
2994 | ||
e26d5213 | 2995 | if (0 < src.x && src.x < w - 1) |
f2506310 | 2996 | { |
3fff3966 VZ |
2997 | x1 = wxRound(floor(src.x)); |
2998 | x2 = wxRound(ceil(src.x)); | |
f2506310 JS |
2999 | } |
3000 | else // else means that x is near one of the borders (0 or width-1) | |
3001 | { | |
3fff3966 | 3002 | x1 = x2 = wxRound (src.x); |
f2506310 JS |
3003 | } |
3004 | ||
e26d5213 | 3005 | if (0 < src.y && src.y < h - 1) |
f2506310 | 3006 | { |
3fff3966 VZ |
3007 | y1 = wxRound(floor(src.y)); |
3008 | y2 = wxRound(ceil(src.y)); | |
f2506310 JS |
3009 | } |
3010 | else | |
3011 | { | |
3fff3966 | 3012 | y1 = y2 = wxRound (src.y); |
f2506310 | 3013 | } |
7a632f10 | 3014 | |
ad30de59 GRG |
3015 | // get four points and the distances (square of the distance, |
3016 | // for efficiency reasons) for the interpolation formula | |
b5c91ac6 GRG |
3017 | |
3018 | // GRG: Do not calculate the points until they are | |
3019 | // really needed -- this way we can calculate | |
3020 | // just one, instead of four, if d1, d2, d3 | |
3fff3966 | 3021 | // or d4 are < wxROTATE_EPSILON |
7a632f10 JS |
3022 | |
3023 | const double d1 = (src.x - x1) * (src.x - x1) + (src.y - y1) * (src.y - y1); | |
3024 | const double d2 = (src.x - x2) * (src.x - x2) + (src.y - y1) * (src.y - y1); | |
3025 | const double d3 = (src.x - x2) * (src.x - x2) + (src.y - y2) * (src.y - y2); | |
3026 | const double d4 = (src.x - x1) * (src.x - x1) + (src.y - y2) * (src.y - y2); | |
3027 | ||
ad30de59 GRG |
3028 | // Now interpolate as a weighted average of the four surrounding |
3029 | // points, where the weights are the distances to each of those points | |
7a632f10 | 3030 | |
ad30de59 GRG |
3031 | // If the point is exactly at one point of the grid of the source |
3032 | // image, then don't interpolate -- just assign the pixel | |
7a632f10 | 3033 | |
3fff3966 VZ |
3034 | // d1,d2,d3,d4 are positive -- no need for abs() |
3035 | if (d1 < wxROTATE_EPSILON) | |
7a632f10 | 3036 | { |
b5c91ac6 GRG |
3037 | unsigned char *p = data[y1] + (3 * x1); |
3038 | *(dst++) = *(p++); | |
3039 | *(dst++) = *(p++); | |
6408deed | 3040 | *(dst++) = *p; |
b713f891 | 3041 | |
6408deed | 3042 | if (has_alpha) |
4e115ed2 | 3043 | *(alpha_dst++) = *(alpha[y1] + x1); |
7a632f10 | 3044 | } |
3fff3966 | 3045 | else if (d2 < wxROTATE_EPSILON) |
7a632f10 | 3046 | { |
b5c91ac6 GRG |
3047 | unsigned char *p = data[y1] + (3 * x2); |
3048 | *(dst++) = *(p++); | |
3049 | *(dst++) = *(p++); | |
6408deed | 3050 | *(dst++) = *p; |
b713f891 | 3051 | |
6408deed | 3052 | if (has_alpha) |
4e115ed2 | 3053 | *(alpha_dst++) = *(alpha[y1] + x2); |
7a632f10 | 3054 | } |
3fff3966 | 3055 | else if (d3 < wxROTATE_EPSILON) |
7a632f10 | 3056 | { |
b5c91ac6 GRG |
3057 | unsigned char *p = data[y2] + (3 * x2); |
3058 | *(dst++) = *(p++); | |
3059 | *(dst++) = *(p++); | |
6408deed | 3060 | *(dst++) = *p; |
b713f891 | 3061 | |
6408deed | 3062 | if (has_alpha) |
4e115ed2 | 3063 | *(alpha_dst++) = *(alpha[y2] + x2); |
7a632f10 | 3064 | } |
3fff3966 | 3065 | else if (d4 < wxROTATE_EPSILON) |
7a632f10 | 3066 | { |
b5c91ac6 GRG |
3067 | unsigned char *p = data[y2] + (3 * x1); |
3068 | *(dst++) = *(p++); | |
3069 | *(dst++) = *(p++); | |
6408deed | 3070 | *(dst++) = *p; |
b713f891 | 3071 | |
6408deed | 3072 | if (has_alpha) |
4e115ed2 | 3073 | *(alpha_dst++) = *(alpha[y2] + x1); |
7a632f10 JS |
3074 | } |
3075 | else | |
3076 | { | |
06b466c7 | 3077 | // weights for the weighted average are proportional to the inverse of the distance |
b5c91ac6 GRG |
3078 | unsigned char *v1 = data[y1] + (3 * x1); |
3079 | unsigned char *v2 = data[y1] + (3 * x2); | |
3080 | unsigned char *v3 = data[y2] + (3 * x2); | |
3081 | unsigned char *v4 = data[y2] + (3 * x1); | |
3082 | ||
06b466c7 VZ |
3083 | const double w1 = 1/d1, w2 = 1/d2, w3 = 1/d3, w4 = 1/d4; |
3084 | ||
b5c91ac6 GRG |
3085 | // GRG: Unrolled. |
3086 | ||
3087 | *(dst++) = (unsigned char) | |
3088 | ( (w1 * *(v1++) + w2 * *(v2++) + | |
3089 | w3 * *(v3++) + w4 * *(v4++)) / | |
3090 | (w1 + w2 + w3 + w4) ); | |
3091 | *(dst++) = (unsigned char) | |
3092 | ( (w1 * *(v1++) + w2 * *(v2++) + | |
3093 | w3 * *(v3++) + w4 * *(v4++)) / | |
3094 | (w1 + w2 + w3 + w4) ); | |
3095 | *(dst++) = (unsigned char) | |
999836aa VZ |
3096 | ( (w1 * *v1 + w2 * *v2 + |
3097 | w3 * *v3 + w4 * *v4) / | |
b5c91ac6 | 3098 | (w1 + w2 + w3 + w4) ); |
b713f891 | 3099 | |
6408deed RR |
3100 | if (has_alpha) |
3101 | { | |
4e115ed2 VZ |
3102 | v1 = alpha[y1] + (x1); |
3103 | v2 = alpha[y1] + (x2); | |
3104 | v3 = alpha[y2] + (x2); | |
3105 | v4 = alpha[y2] + (x1); | |
6408deed RR |
3106 | |
3107 | *(alpha_dst++) = (unsigned char) | |
3108 | ( (w1 * *v1 + w2 * *v2 + | |
3109 | w3 * *v3 + w4 * *v4) / | |
3110 | (w1 + w2 + w3 + w4) ); | |
3111 | } | |
7a632f10 JS |
3112 | } |
3113 | } | |
3114 | else | |
3115 | { | |
b5c91ac6 GRG |
3116 | *(dst++) = blank_r; |
3117 | *(dst++) = blank_g; | |
3118 | *(dst++) = blank_b; | |
b713f891 | 3119 | |
6408deed RR |
3120 | if (has_alpha) |
3121 | *(alpha_dst++) = 0; | |
7a632f10 JS |
3122 | } |
3123 | } | |
b5c91ac6 GRG |
3124 | } |
3125 | } | |
e26d5213 | 3126 | else // not interpolating |
b5c91ac6 | 3127 | { |
e26d5213 | 3128 | for (int y = 0; y < rH; y++) |
b5c91ac6 | 3129 | { |
e26d5213 | 3130 | for (int x = 0; x < rW; x++) |
7a632f10 | 3131 | { |
3fff3966 | 3132 | wxRealPoint src = wxRotatePoint (x + x1a, y + y1a, cos_angle, -sin_angle, p0); |
b5c91ac6 | 3133 | |
3fff3966 VZ |
3134 | const int xs = wxRound (src.x); // wxRound rounds to the |
3135 | const int ys = wxRound (src.y); // closest integer | |
7a632f10 | 3136 | |
e26d5213 | 3137 | if (0 <= xs && xs < w && 0 <= ys && ys < h) |
7a632f10 | 3138 | { |
b5c91ac6 GRG |
3139 | unsigned char *p = data[ys] + (3 * xs); |
3140 | *(dst++) = *(p++); | |
3141 | *(dst++) = *(p++); | |
999836aa | 3142 | *(dst++) = *p; |
b713f891 | 3143 | |
6408deed | 3144 | if (has_alpha) |
4e115ed2 | 3145 | *(alpha_dst++) = *(alpha[ys] + (xs)); |
7a632f10 JS |
3146 | } |
3147 | else | |
3148 | { | |
b5c91ac6 GRG |
3149 | *(dst++) = blank_r; |
3150 | *(dst++) = blank_g; | |
3151 | *(dst++) = blank_b; | |
b713f891 | 3152 | |
6408deed RR |
3153 | if (has_alpha) |
3154 | *(alpha_dst++) = 255; | |
7a632f10 JS |
3155 | } |
3156 | } | |
3157 | } | |
3158 | } | |
3159 | ||
4aff28fc | 3160 | delete [] data; |
e26d5213 | 3161 | delete [] alpha; |
4aff28fc | 3162 | |
7a632f10 JS |
3163 | return rotated; |
3164 | } | |
c9d01afd | 3165 | |
ef8f37e0 VS |
3166 | |
3167 | ||
3168 | ||
3169 | ||
3170 | // A module to allow wxImage initialization/cleanup | |
3171 | // without calling these functions from app.cpp or from | |
3172 | // the user's application. | |
3173 | ||
3174 | class wxImageModule: public wxModule | |
3175 | { | |
3176 | DECLARE_DYNAMIC_CLASS(wxImageModule) | |
3177 | public: | |
3178 | wxImageModule() {} | |
47b378bd VS |
3179 | bool OnInit() { wxImage::InitStandardHandlers(); return true; } |
3180 | void OnExit() { wxImage::CleanUpHandlers(); } | |
ef8f37e0 VS |
3181 | }; |
3182 | ||
3183 | IMPLEMENT_DYNAMIC_CLASS(wxImageModule, wxModule) | |
3184 | ||
3185 | ||
c96ea657 | 3186 | #endif // wxUSE_IMAGE |