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