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