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