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