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