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