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