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