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