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