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