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