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