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