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