]> git.saurik.com Git - wxWidgets.git/blame - src/common/image.cpp
removed wxLANGUAGE_SERBIAN, we must explicitly use either or wxLANGUAGE_SERBIAN_CYRIL...
[wxWidgets.git] / src / common / image.cpp
CommitLineData
01111366 1/////////////////////////////////////////////////////////////////////////////
38d4b1e4 2// Name: src/common/image.cpp
01111366
RR
3// Purpose: wxImage
4// Author: Robert Roebling
5// RCS-ID: $Id$
6// Copyright: (c) Robert Roebling
65571936 7// Licence: wxWindows licence
01111366
RR
8/////////////////////////////////////////////////////////////////////////////
9
0b4f9ee3
UU
10// For compilers that support precompilation, includes "wx.h".
11#include "wx/wxprec.h"
12
13#ifdef __BORLANDC__
edccf428 14 #pragma hdrstop
0b4f9ee3
UU
15#endif
16
c96ea657
VS
17#if wxUSE_IMAGE
18
0bca0373
WS
19#include "wx/image.h"
20
8898456d
WS
21#ifndef WX_PRECOMP
22 #include "wx/log.h"
23 #include "wx/app.h"
32d4c30a 24 #include "wx/hash.h"
de6185e2 25 #include "wx/utils.h"
0bca0373 26 #include "wx/bitmap.h"
18680f86 27 #include "wx/math.h"
8898456d
WS
28#endif
29
dc86cb34 30#include "wx/filefn.h"
3d05544e 31#include "wx/wfstream.h"
b75867a6 32#include "wx/intl.h"
a91b47e8 33#include "wx/module.h"
01111366 34
cad61c3e 35#if wxUSE_XPM
32d4c30a 36 #include "wx/xpmdecod.h"
cad61c3e
JS
37#endif
38
58a8ab88
JS
39// For memcpy
40#include <string.h>
41
01111366
RR
42//-----------------------------------------------------------------------------
43// wxImage
44//-----------------------------------------------------------------------------
45
46class wxImageRefData: public wxObjectRefData
47{
fd0eed64 48public:
edccf428 49 wxImageRefData();
487659e0 50 virtual ~wxImageRefData();
c7abc967 51
dbda9e86
JS
52 int m_width;
53 int m_height;
54 unsigned char *m_data;
487659e0 55
dbda9e86
JS
56 bool m_hasMask;
57 unsigned char m_maskRed,m_maskGreen,m_maskBlue;
487659e0
VZ
58
59 // alpha channel data, may be NULL for the formats without alpha support
60 unsigned char *m_alpha;
61
dbda9e86 62 bool m_ok;
d2502f14
VZ
63
64 // if true, m_data is pointer to static data and shouldn't be freed
f6bcfd97 65 bool m_static;
487659e0 66
d2502f14
VZ
67 // same as m_static but for m_alpha
68 bool m_staticAlpha;
69
d275c7eb 70#if wxUSE_PALETTE
5e5437e0 71 wxPalette m_palette;
d275c7eb 72#endif // wxUSE_PALETTE
487659e0 73
5e5437e0
JS
74 wxArrayString m_optionNames;
75 wxArrayString m_optionValues;
22f3361e
VZ
76
77 DECLARE_NO_COPY_CLASS(wxImageRefData)
01111366
RR
78};
79
edccf428 80wxImageRefData::wxImageRefData()
01111366 81{
fd0eed64
RR
82 m_width = 0;
83 m_height = 0;
487659e0
VZ
84 m_data =
85 m_alpha = (unsigned char *) NULL;
86
fd0eed64
RR
87 m_maskRed = 0;
88 m_maskGreen = 0;
89 m_maskBlue = 0;
70cd62e9 90 m_hasMask = false;
487659e0 91
70cd62e9 92 m_ok = false;
d2502f14
VZ
93 m_static =
94 m_staticAlpha = false;
01111366
RR
95}
96
edccf428 97wxImageRefData::~wxImageRefData()
01111366 98{
d2502f14 99 if ( !m_static )
58c837a4 100 free( m_data );
d2502f14 101 if ( !m_staticAlpha )
4ea56379 102 free( m_alpha );
01111366
RR
103}
104
105wxList wxImage::sm_handlers;
106
fec19ea9
VS
107wxImage wxNullImage;
108
01111366
RR
109//-----------------------------------------------------------------------------
110
a0f81e9f 111#define M_IMGDATA wx_static_cast(wxImageRefData*, m_refData)
01111366 112
5e5437e0 113IMPLEMENT_DYNAMIC_CLASS(wxImage, wxObject)
01111366 114
ff865c13 115wxImage::wxImage( int width, int height, bool clear )
01111366 116{
ff865c13 117 Create( width, height, clear );
01111366
RR
118}
119
f6bcfd97
BP
120wxImage::wxImage( int width, int height, unsigned char* data, bool static_data )
121{
122 Create( width, height, data, static_data );
123}
124
4ea56379
RR
125wxImage::wxImage( int width, int height, unsigned char* data, unsigned char* alpha, bool static_data )
126{
127 Create( width, height, data, alpha, static_data );
128}
129
60d43ad8 130wxImage::wxImage( const wxString& name, long type, int index )
01111366 131{
60d43ad8 132 LoadFile( name, type, index );
01111366
RR
133}
134
60d43ad8 135wxImage::wxImage( const wxString& name, const wxString& mimetype, int index )
9e9ee68e 136{
60d43ad8 137 LoadFile( name, mimetype, index );
9e9ee68e
VS
138}
139
e02afc7a 140#if wxUSE_STREAMS
60d43ad8 141wxImage::wxImage( wxInputStream& stream, long type, int index )
3d05544e 142{
60d43ad8 143 LoadFile( stream, type, index );
3d05544e 144}
9e9ee68e 145
60d43ad8 146wxImage::wxImage( wxInputStream& stream, const wxString& mimetype, int index )
9e9ee68e 147{
60d43ad8 148 LoadFile( stream, mimetype, index );
9e9ee68e 149}
e02afc7a 150#endif // wxUSE_STREAMS
3d05544e 151
cad61c3e
JS
152wxImage::wxImage( const char** xpmData )
153{
154 Create(xpmData);
155}
156
157wxImage::wxImage( char** xpmData )
158{
159 Create((const char**) xpmData);
160}
161
162bool wxImage::Create( const char** xpmData )
163{
164#if wxUSE_XPM
165 UnRef();
e9b64c5e 166
cad61c3e
JS
167 wxXPMDecoder decoder;
168 (*this) = decoder.ReadData(xpmData);
169 return Ok();
170#else
171 return false;
172#endif
173}
174
aaa97828 175bool wxImage::Create( int width, int height, bool clear )
01111366 176{
aadaf841
GRG
177 UnRef();
178
fd0eed64 179 m_refData = new wxImageRefData();
c7abc967 180
fd0eed64 181 M_IMGDATA->m_data = (unsigned char *) malloc( width*height*3 );
aaa97828 182 if (!M_IMGDATA->m_data)
fd0eed64
RR
183 {
184 UnRef();
70cd62e9 185 return false;
fd0eed64 186 }
aaa97828
VZ
187
188 if (clear)
189 memset(M_IMGDATA->m_data, 0, width*height*3);
190
191 M_IMGDATA->m_width = width;
192 M_IMGDATA->m_height = height;
70cd62e9 193 M_IMGDATA->m_ok = true;
aaa97828 194
70cd62e9 195 return true;
01111366
RR
196}
197
aaa97828 198bool wxImage::Create( int width, int height, unsigned char* data, bool static_data )
f6bcfd97
BP
199{
200 UnRef();
201
70cd62e9 202 wxCHECK_MSG( data, false, _T("NULL data in wxImage::Create") );
aaa97828 203
f6bcfd97
BP
204 m_refData = new wxImageRefData();
205
206 M_IMGDATA->m_data = data;
aaa97828
VZ
207 M_IMGDATA->m_width = width;
208 M_IMGDATA->m_height = height;
70cd62e9 209 M_IMGDATA->m_ok = true;
aaa97828
VZ
210 M_IMGDATA->m_static = static_data;
211
70cd62e9 212 return true;
f6bcfd97
BP
213}
214
4ea56379
RR
215bool wxImage::Create( int width, int height, unsigned char* data, unsigned char* alpha, bool static_data )
216{
217 UnRef();
218
219 wxCHECK_MSG( data, false, _T("NULL data in wxImage::Create") );
220
221 m_refData = new wxImageRefData();
222
223 M_IMGDATA->m_data = data;
224 M_IMGDATA->m_alpha = alpha;
225 M_IMGDATA->m_width = width;
226 M_IMGDATA->m_height = height;
227 M_IMGDATA->m_ok = true;
228 M_IMGDATA->m_static = static_data;
229
230 return true;
231}
232
01111366
RR
233void wxImage::Destroy()
234{
fd0eed64 235 UnRef();
01111366
RR
236}
237
a0f81e9f 238wxObjectRefData* wxImage::CreateRefData() const
f6bcfd97 239{
a0f81e9f
PC
240 return new wxImageRefData;
241}
051924b8 242
a0f81e9f
PC
243wxObjectRefData* wxImage::CloneRefData(const wxObjectRefData* that) const
244{
245 const wxImageRefData* refData = wx_static_cast(const wxImageRefData*, that);
246 wxCHECK_MSG(refData->m_ok, NULL, wxT("invalid image") );
051924b8 247
a0f81e9f
PC
248 wxImageRefData* refData_new = new wxImageRefData;
249 refData_new->m_width = refData->m_width;
250 refData_new->m_height = refData->m_height;
251 refData_new->m_maskRed = refData->m_maskRed;
252 refData_new->m_maskGreen = refData->m_maskGreen;
253 refData_new->m_maskBlue = refData->m_maskBlue;
254 refData_new->m_hasMask = refData->m_hasMask;
255 refData_new->m_ok = true;
256 unsigned size = unsigned(refData->m_width) * unsigned(refData->m_height);
257 if (refData->m_alpha != NULL)
a1cd9564 258 {
a0f81e9f
PC
259 refData_new->m_alpha = (unsigned char*)malloc(size);
260 memcpy(refData_new->m_alpha, refData->m_alpha, size);
a1cd9564 261 }
a0f81e9f
PC
262 size *= 3;
263 refData_new->m_data = (unsigned char*)malloc(size);
264 memcpy(refData_new->m_data, refData->m_data, size);
265#if wxUSE_PALETTE
266 refData_new->m_palette = refData->m_palette;
267#endif
268 refData_new->m_optionNames = refData->m_optionNames;
269 refData_new->m_optionValues = refData->m_optionValues;
270 return refData_new;
271}
051924b8 272
a0f81e9f
PC
273wxImage wxImage::Copy() const
274{
275 wxImage image;
276
277 wxCHECK_MSG( Ok(), image, wxT("invalid image") );
278
279 image.m_refData = CloneRefData(m_refData);
d8692f2b 280
f6bcfd97
BP
281 return image;
282}
283
fd9655ad
SC
284wxImage wxImage::ShrinkBy( int xFactor , int yFactor ) const
285{
286 if( xFactor == 1 && yFactor == 1 )
a0f81e9f 287 return *this;
7beb59f3 288
fd9655ad
SC
289 wxImage image;
290
291 wxCHECK_MSG( Ok(), image, wxT("invalid image") );
292
293 // can't scale to/from 0 size
294 wxCHECK_MSG( (xFactor > 0) && (yFactor > 0), image,
295 wxT("invalid new image size") );
296
297 long old_height = M_IMGDATA->m_height,
298 old_width = M_IMGDATA->m_width;
7beb59f3 299
fd9655ad
SC
300 wxCHECK_MSG( (old_height > 0) && (old_width > 0), image,
301 wxT("invalid old image size") );
302
303 long width = old_width / xFactor ;
304 long height = old_height / yFactor ;
305
ff865c13 306 image.Create( width, height, false );
fd9655ad
SC
307
308 char unsigned *data = image.GetData();
309
310 wxCHECK_MSG( data, image, wxT("unable to create image") );
311
312 bool hasMask = false ;
313 unsigned char maskRed = 0;
314 unsigned char maskGreen = 0;
315 unsigned char maskBlue =0 ;
cd0bbd03
SC
316
317 unsigned char *source_data = M_IMGDATA->m_data;
318 unsigned char *target_data = data;
319 unsigned char *source_alpha = 0 ;
320 unsigned char *target_alpha = 0 ;
fd9655ad
SC
321 if (M_IMGDATA->m_hasMask)
322 {
323 hasMask = true ;
324 maskRed = M_IMGDATA->m_maskRed;
325 maskGreen = M_IMGDATA->m_maskGreen;
326 maskBlue =M_IMGDATA->m_maskBlue ;
7beb59f3 327
fd9655ad
SC
328 image.SetMaskColour( M_IMGDATA->m_maskRed,
329 M_IMGDATA->m_maskGreen,
330 M_IMGDATA->m_maskBlue );
331 }
cd0bbd03
SC
332 else
333 {
334 source_alpha = M_IMGDATA->m_alpha ;
335 if ( source_alpha )
336 {
337 image.SetAlpha() ;
338 target_alpha = image.GetAlpha() ;
339 }
340 }
7beb59f3 341
fd9655ad
SC
342 for (long y = 0; y < height; y++)
343 {
fd9655ad
SC
344 for (long x = 0; x < width; x++)
345 {
346 unsigned long avgRed = 0 ;
347 unsigned long avgGreen = 0;
348 unsigned long avgBlue = 0;
cd0bbd03 349 unsigned long avgAlpha = 0 ;
fd9655ad
SC
350 unsigned long counter = 0 ;
351 // determine average
352 for ( int y1 = 0 ; y1 < yFactor ; ++y1 )
353 {
354 long y_offset = (y * yFactor + y1) * old_width;
355 for ( int x1 = 0 ; x1 < xFactor ; ++x1 )
356 {
357 unsigned char *pixel = source_data + 3 * ( y_offset + x * xFactor + x1 ) ;
358 unsigned char red = pixel[0] ;
359 unsigned char green = pixel[1] ;
360 unsigned char blue = pixel[2] ;
cd0bbd03
SC
361 unsigned char alpha = 255 ;
362 if ( source_alpha )
363 alpha = *(source_alpha + y_offset + x * xFactor + x1) ;
fd9655ad
SC
364 if ( !hasMask || red != maskRed || green != maskGreen || blue != maskBlue )
365 {
cd0bbd03
SC
366 if ( alpha > 0 )
367 {
368 avgRed += red ;
369 avgGreen += green ;
370 avgBlue += blue ;
371 }
372 avgAlpha += alpha ;
fd9655ad
SC
373 counter++ ;
374 }
375 }
376 }
377 if ( counter == 0 )
378 {
379 *(target_data++) = M_IMGDATA->m_maskRed ;
380 *(target_data++) = M_IMGDATA->m_maskGreen ;
381 *(target_data++) = M_IMGDATA->m_maskBlue ;
382 }
383 else
384 {
cd0bbd03
SC
385 if ( source_alpha )
386 *(target_alpha++) = (unsigned char)(avgAlpha / counter ) ;
646c4aeb
VZ
387 *(target_data++) = (unsigned char)(avgRed / counter);
388 *(target_data++) = (unsigned char)(avgGreen / counter);
389 *(target_data++) = (unsigned char)(avgBlue / counter);
fd9655ad
SC
390 }
391 }
392 }
393
8180d40b 394 // In case this is a cursor, make sure the hotspot is scaled accordingly:
fd9655ad
SC
395 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X) )
396 image.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X,
397 (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X))/xFactor);
398 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y) )
399 image.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y,
400 (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y))/yFactor);
401
402 return image;
403}
404
ce9a75d2 405wxImage wxImage::Scale( int width, int height ) const
4bc67cc5
RR
406{
407 wxImage image;
c7abc967 408
223d09f6 409 wxCHECK_MSG( Ok(), image, wxT("invalid image") );
c7abc967 410
6721fc38
VZ
411 // can't scale to/from 0 size
412 wxCHECK_MSG( (width > 0) && (height > 0), image,
413 wxT("invalid new image size") );
414
415 long old_height = M_IMGDATA->m_height,
416 old_width = M_IMGDATA->m_width;
417 wxCHECK_MSG( (old_height > 0) && (old_width > 0), image,
418 wxT("invalid old image size") );
c7abc967 419
fd9655ad
SC
420 if ( old_width % width == 0 && old_width >= width &&
421 old_height % height == 0 && old_height >= height )
422 {
423 return ShrinkBy( old_width / width , old_height / height ) ;
424 }
ff865c13 425 image.Create( width, height, false );
c7abc967 426
487659e0 427 unsigned char *data = image.GetData();
c7abc967 428
223d09f6 429 wxCHECK_MSG( data, image, wxT("unable to create image") );
c7abc967 430
8d3b6b8a
SC
431 unsigned char *source_data = M_IMGDATA->m_data;
432 unsigned char *target_data = data;
433 unsigned char *source_alpha = 0 ;
434 unsigned char *target_alpha = 0 ;
e9b64c5e 435
4bc67cc5 436 if (M_IMGDATA->m_hasMask)
6721fc38
VZ
437 {
438 image.SetMaskColour( M_IMGDATA->m_maskRed,
439 M_IMGDATA->m_maskGreen,
440 M_IMGDATA->m_maskBlue );
441 }
8d3b6b8a
SC
442 else
443 {
444 source_alpha = M_IMGDATA->m_alpha ;
445 if ( source_alpha )
446 {
447 image.SetAlpha() ;
448 target_alpha = image.GetAlpha() ;
449 }
450 }
c7abc967 451
ff865c13
JS
452 long x_delta = (old_width<<16) / width;
453 long y_delta = (old_height<<16) / height;
c7abc967 454
ff865c13 455 unsigned char* dest_pixel = target_data;
6721fc38 456
ff865c13
JS
457 long y = 0;
458 for ( long j = 0; j < height; j++ )
459 {
460 unsigned char* src_line = &source_data[(y>>16)*old_width*3];
8d3b6b8a 461 unsigned char* src_alpha_line = source_alpha ? &source_alpha[(y>>16)*old_width] : 0 ;
e9b64c5e 462
ff865c13
JS
463 long x = 0;
464 for ( long i = 0; i < width; i++ )
eeca3a46 465 {
ff865c13 466 unsigned char* src_pixel = &src_line[(x>>16)*3];
8d3b6b8a 467 unsigned char* src_alpha_pixel = source_alpha ? &src_alpha_line[(x>>16)] : 0 ;
ff865c13
JS
468 dest_pixel[0] = src_pixel[0];
469 dest_pixel[1] = src_pixel[1];
470 dest_pixel[2] = src_pixel[2];
471 dest_pixel += 3;
8d3b6b8a
SC
472 if ( source_alpha )
473 *(target_alpha++) = *src_alpha_pixel ;
ff865c13 474 x += x_delta;
eeca3a46 475 }
ff865c13
JS
476
477 y += y_delta;
eeca3a46 478 }
36aac195 479
8180d40b 480 // In case this is a cursor, make sure the hotspot is scaled accordingly:
fd94e8aa
VS
481 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X) )
482 image.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X,
483 (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X)*width)/old_width);
484 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y) )
485 image.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y,
486 (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y)*height)/old_height);
c7abc967 487
4bc67cc5
RR
488 return image;
489}
4698648f 490
f6bcfd97
BP
491wxImage wxImage::Rotate90( bool clockwise ) const
492{
493 wxImage image;
494
495 wxCHECK_MSG( Ok(), image, wxT("invalid image") );
496
ff865c13 497 image.Create( M_IMGDATA->m_height, M_IMGDATA->m_width, false );
f6bcfd97 498
487659e0 499 unsigned char *data = image.GetData();
f6bcfd97
BP
500
501 wxCHECK_MSG( data, image, wxT("unable to create image") );
502
921c65ed
JS
503 unsigned char *source_data = M_IMGDATA->m_data;
504 unsigned char *target_data;
505 unsigned char *alpha_data = 0 ;
506 unsigned char *source_alpha = 0 ;
507 unsigned char *target_alpha = 0 ;
508
f6bcfd97 509 if (M_IMGDATA->m_hasMask)
921c65ed 510 {
f6bcfd97 511 image.SetMaskColour( M_IMGDATA->m_maskRed, M_IMGDATA->m_maskGreen, M_IMGDATA->m_maskBlue );
921c65ed
JS
512 }
513 else
514 {
515 source_alpha = M_IMGDATA->m_alpha ;
516 if ( source_alpha )
517 {
518 image.SetAlpha() ;
519 alpha_data = image.GetAlpha() ;
520 }
521 }
f6bcfd97
BP
522
523 long height = M_IMGDATA->m_height;
524 long width = M_IMGDATA->m_width;
525
f6bcfd97
BP
526 for (long j = 0; j < height; j++)
527 {
528 for (long i = 0; i < width; i++)
529 {
530 if (clockwise)
921c65ed 531 {
f6bcfd97 532 target_data = data + (((i+1)*height) - j - 1)*3;
921c65ed
JS
533 if(source_alpha)
534 target_alpha = alpha_data + (((i+1)*height) - j - 1);
535 }
f6bcfd97 536 else
921c65ed 537 {
f6bcfd97 538 target_data = data + ((height*(width-1)) + j - (i*height))*3;
921c65ed
JS
539 if(source_alpha)
540 target_alpha = alpha_data + ((height*(width-1)) + j - (i*height));
541 }
f6bcfd97
BP
542 memcpy( target_data, source_data, 3 );
543 source_data += 3;
921c65ed
JS
544
545 if(source_alpha)
546 {
547 memcpy( target_alpha, source_alpha, 1 );
548 source_alpha += 1;
549 }
f6bcfd97
BP
550 }
551 }
552
553 return image;
554}
555
556wxImage wxImage::Mirror( bool horizontally ) const
557{
558 wxImage image;
559
560 wxCHECK_MSG( Ok(), image, wxT("invalid image") );
561
ff865c13 562 image.Create( M_IMGDATA->m_width, M_IMGDATA->m_height, false );
f6bcfd97 563
487659e0 564 unsigned char *data = image.GetData();
051924b8 565 unsigned char *alpha = NULL;
f6bcfd97
BP
566
567 wxCHECK_MSG( data, image, wxT("unable to create image") );
568
051924b8
VZ
569 if (M_IMGDATA->m_alpha != NULL) {
570 image.SetAlpha();
571 alpha = image.GetAlpha();
572 wxCHECK_MSG( alpha, image, wxT("unable to create alpha channel") );
573 }
574
f6bcfd97
BP
575 if (M_IMGDATA->m_hasMask)
576 image.SetMaskColour( M_IMGDATA->m_maskRed, M_IMGDATA->m_maskGreen, M_IMGDATA->m_maskBlue );
577
578 long height = M_IMGDATA->m_height;
579 long width = M_IMGDATA->m_width;
580
487659e0
VZ
581 unsigned char *source_data = M_IMGDATA->m_data;
582 unsigned char *target_data;
f6bcfd97
BP
583
584 if (horizontally)
585 {
586 for (long j = 0; j < height; j++)
587 {
588 data += width*3;
589 target_data = data-3;
590 for (long i = 0; i < width; i++)
591 {
592 memcpy( target_data, source_data, 3 );
593 source_data += 3;
594 target_data -= 3;
595 }
596 }
051924b8
VZ
597
598 if (alpha != NULL)
599 {
600 // src_alpha starts at the first pixel and increases by 1 after each step
601 // (a step here is the copy of the alpha value of one pixel)
602 const unsigned char *src_alpha = M_IMGDATA->m_alpha;
603 // dest_alpha starts just beyond the first line, decreases before each step,
604 // and after each line is finished, increases by 2 widths (skipping the line
605 // just copied and the line that will be copied next)
606 unsigned char *dest_alpha = alpha + width;
607
608 for (long jj = 0; jj < height; ++jj)
609 {
610 for (long i = 0; i < width; ++i) {
611 *(--dest_alpha) = *(src_alpha++); // copy one pixel
612 }
613 dest_alpha += 2 * width; // advance beyond the end of the next line
614 }
615 }
f6bcfd97
BP
616 }
617 else
618 {
619 for (long i = 0; i < height; i++)
620 {
621 target_data = data + 3*width*(height-1-i);
3ca6a5f0 622 memcpy( target_data, source_data, (size_t)3*width );
f6bcfd97
BP
623 source_data += 3*width;
624 }
051924b8
VZ
625
626 if (alpha != NULL)
627 {
628 // src_alpha starts at the first pixel and increases by 1 width after each step
629 // (a step here is the copy of the alpha channel of an entire line)
630 const unsigned char *src_alpha = M_IMGDATA->m_alpha;
631 // dest_alpha starts just beyond the last line (beyond the whole image)
632 // and decreases by 1 width before each step
633 unsigned char *dest_alpha = alpha + width * height;
634
635 for (long jj = 0; jj < height; ++jj)
636 {
637 dest_alpha -= width;
638 memcpy( dest_alpha, src_alpha, (size_t)width );
639 src_alpha += width;
640 }
641 }
f6bcfd97
BP
642 }
643
644 return image;
645}
646
7b2471a0
SB
647wxImage wxImage::GetSubImage( const wxRect &rect ) const
648{
649 wxImage image;
650
223d09f6 651 wxCHECK_MSG( Ok(), image, wxT("invalid image") );
7b2471a0 652
051924b8
VZ
653 wxCHECK_MSG( (rect.GetLeft()>=0) && (rect.GetTop()>=0) &&
654 (rect.GetRight()<=GetWidth()) && (rect.GetBottom()<=GetHeight()),
58c837a4 655 image, wxT("invalid subimage size") );
7b2471a0 656
051924b8
VZ
657 const int subwidth = rect.GetWidth();
658 const int subheight = rect.GetHeight();
7b2471a0 659
ff865c13 660 image.Create( subwidth, subheight, false );
7b2471a0 661
051924b8
VZ
662 const unsigned char *src_data = GetData();
663 const unsigned char *src_alpha = M_IMGDATA->m_alpha;
664 unsigned char *subdata = image.GetData();
665 unsigned char *subalpha = NULL;
7b2471a0 666
223d09f6 667 wxCHECK_MSG( subdata, image, wxT("unable to create image") );
7b2471a0 668
051924b8
VZ
669 if (src_alpha != NULL) {
670 image.SetAlpha();
671 subalpha = image.GetAlpha();
672 wxCHECK_MSG( subalpha, image, wxT("unable to create alpha channel"));
673 }
674
7b2471a0
SB
675 if (M_IMGDATA->m_hasMask)
676 image.SetMaskColour( M_IMGDATA->m_maskRed, M_IMGDATA->m_maskGreen, M_IMGDATA->m_maskBlue );
677
051924b8
VZ
678 const int width = GetWidth();
679 const int pixsoff = rect.GetLeft() + width * rect.GetTop();
995612e2 680
051924b8
VZ
681 src_data += 3 * pixsoff;
682 src_alpha += pixsoff; // won't be used if was NULL, so this is ok
7b2471a0
SB
683
684 for (long j = 0; j < subheight; ++j)
685 {
051924b8
VZ
686 memcpy( subdata, src_data, 3 * subwidth );
687 subdata += 3 * subwidth;
688 src_data += 3 * width;
689 if (subalpha != NULL) {
690 memcpy( subalpha, src_alpha, subwidth );
691 subalpha += subwidth;
692 src_alpha += width;
693 }
7b2471a0
SB
694 }
695
696 return image;
697}
698
e9b64c5e 699wxImage wxImage::Size( const wxSize& size, const wxPoint& pos,
b737ad10
RR
700 int r_, int g_, int b_ ) const
701{
702 wxImage image;
703
704 wxCHECK_MSG( Ok(), image, wxT("invalid image") );
705 wxCHECK_MSG( (size.GetWidth() > 0) && (size.GetHeight() > 0), image, wxT("invalid size") );
706
707 int width = GetWidth(), height = GetHeight();
708 image.Create(size.GetWidth(), size.GetHeight(), false);
709
710 unsigned char r = (unsigned char)r_;
711 unsigned char g = (unsigned char)g_;
712 unsigned char b = (unsigned char)b_;
713 if ((r_ == -1) && (g_ == -1) && (b_ == -1))
714 {
715 GetOrFindMaskColour( &r, &g, &b );
716 image.SetMaskColour(r, g, b);
717 }
718
719 image.SetRGB(wxRect(), r, g, b);
720
721 wxRect subRect(pos.x, pos.y, width, height);
722 wxRect finalRect(0, 0, size.GetWidth(), size.GetHeight());
781945ea
RR
723 if (pos.x < 0)
724 finalRect.width -= pos.x;
725 if (pos.y < 0)
726 finalRect.height -= pos.y;
b737ad10
RR
727
728 subRect.Intersect(finalRect);
729
730 if (!subRect.IsEmpty())
731 {
732 if ((subRect.GetWidth() == width) && (subRect.GetHeight() == height))
733 image.Paste(*this, pos.x, pos.y);
734 else
735 image.Paste(GetSubImage(subRect), pos.x, pos.y);
736 }
737
738 return image;
739}
740
f6bcfd97
BP
741void wxImage::Paste( const wxImage &image, int x, int y )
742{
743 wxCHECK_RET( Ok(), wxT("invalid image") );
744 wxCHECK_RET( image.Ok(), wxT("invalid image") );
745
a0f81e9f
PC
746 AllocExclusive();
747
f6bcfd97
BP
748 int xx = 0;
749 int yy = 0;
750 int width = image.GetWidth();
751 int height = image.GetHeight();
752
753 if (x < 0)
754 {
755 xx = -x;
756 width += x;
757 }
758 if (y < 0)
759 {
760 yy = -y;
761 height += y;
762 }
763
764 if ((x+xx)+width > M_IMGDATA->m_width)
765 width = M_IMGDATA->m_width - (x+xx);
766 if ((y+yy)+height > M_IMGDATA->m_height)
767 height = M_IMGDATA->m_height - (y+yy);
768
769 if (width < 1) return;
770 if (height < 1) return;
771
772 if ((!HasMask() && !image.HasMask()) ||
b737ad10 773 (HasMask() && !image.HasMask()) ||
f6bcfd97
BP
774 ((HasMask() && image.HasMask() &&
775 (GetMaskRed()==image.GetMaskRed()) &&
776 (GetMaskGreen()==image.GetMaskGreen()) &&
777 (GetMaskBlue()==image.GetMaskBlue()))))
778 {
779 width *= 3;
780 unsigned char* source_data = image.GetData() + xx*3 + yy*3*image.GetWidth();
781 int source_step = image.GetWidth()*3;
782
783 unsigned char* target_data = GetData() + (x+xx)*3 + (y+yy)*3*M_IMGDATA->m_width;
784 int target_step = M_IMGDATA->m_width*3;
785 for (int j = 0; j < height; j++)
786 {
787 memcpy( target_data, source_data, width );
788 source_data += source_step;
789 target_data += target_step;
790 }
aa21b509 791 return;
f6bcfd97 792 }
33ac7e6f 793
aa21b509 794 if (!HasMask() && image.HasMask())
f6bcfd97 795 {
aa21b509
RR
796 unsigned char r = image.GetMaskRed();
797 unsigned char g = image.GetMaskGreen();
798 unsigned char b = image.GetMaskBlue();
33ac7e6f 799
aa21b509
RR
800 width *= 3;
801 unsigned char* source_data = image.GetData() + xx*3 + yy*3*image.GetWidth();
802 int source_step = image.GetWidth()*3;
803
804 unsigned char* target_data = GetData() + (x+xx)*3 + (y+yy)*3*M_IMGDATA->m_width;
805 int target_step = M_IMGDATA->m_width*3;
33ac7e6f 806
aa21b509
RR
807 for (int j = 0; j < height; j++)
808 {
809 for (int i = 0; i < width; i+=3)
810 {
33ac7e6f
KB
811 if ((source_data[i] != r) &&
812 (source_data[i+1] != g) &&
aa21b509
RR
813 (source_data[i+2] != b))
814 {
815 memcpy( target_data+i, source_data+i, 3 );
816 }
33ac7e6f 817 }
aa21b509
RR
818 source_data += source_step;
819 target_data += target_step;
820 }
f6bcfd97
BP
821 }
822}
823
be25e480
RR
824void wxImage::Replace( unsigned char r1, unsigned char g1, unsigned char b1,
825 unsigned char r2, unsigned char g2, unsigned char b2 )
826{
827 wxCHECK_RET( Ok(), wxT("invalid image") );
828
a0f81e9f
PC
829 AllocExclusive();
830
487659e0 831 unsigned char *data = GetData();
06b466c7 832
be25e480
RR
833 const int w = GetWidth();
834 const int h = GetHeight();
835
836 for (int j = 0; j < h; j++)
837 for (int i = 0; i < w; i++)
069d0f27
VZ
838 {
839 if ((data[0] == r1) && (data[1] == g1) && (data[2] == b1))
840 {
841 data[0] = r2;
842 data[1] = g2;
843 data[2] = b2;
844 }
845 data += 3;
846 }
be25e480
RR
847}
848
ec85956a
JS
849wxImage wxImage::ConvertToGreyscale( double lr, double lg, double lb ) const
850{
851 wxImage image;
852
853 wxCHECK_MSG( Ok(), image, wxT("invalid image") );
854
855 image.Create(M_IMGDATA->m_width, M_IMGDATA->m_height, false);
856
857 unsigned char *dest = image.GetData();
858
859 wxCHECK_MSG( dest, image, wxT("unable to create image") );
860
861 unsigned char *src = M_IMGDATA->m_data;
862 bool hasMask = M_IMGDATA->m_hasMask;
863 unsigned char maskRed = M_IMGDATA->m_maskRed;
864 unsigned char maskGreen = M_IMGDATA->m_maskGreen;
865 unsigned char maskBlue = M_IMGDATA->m_maskBlue;
866
867 if ( hasMask )
868 image.SetMaskColour(maskRed, maskGreen, maskBlue);
869
870 const long size = M_IMGDATA->m_width * M_IMGDATA->m_height;
871 for ( long i = 0; i < size; i++, src += 3, dest += 3 )
872 {
873 // don't modify the mask
874 if ( hasMask && src[0] == maskRed && src[1] == maskGreen && src[2] == maskBlue )
875 {
876 memcpy(dest, src, 3);
877 }
878 else
879 {
880 // calculate the luma
881 double luma = (src[0] * lr + src[1] * lg + src[2] * lb) + 0.5;
882 dest[0] = dest[1] = dest[2] = wx_static_cast(unsigned char, luma);
883 }
884 }
885
7ce30d0b
WS
886 // copy the alpha channel, if any
887 if (HasAlpha())
888 {
889 const size_t alphaSize = GetWidth() * GetHeight();
890 unsigned char *alpha = (unsigned char*)malloc(alphaSize);
891 memcpy(alpha, GetAlpha(), alphaSize);
892 image.InitAlpha();
893 image.SetAlpha(alpha);
894 }
895
ec85956a
JS
896 return image;
897}
898
f515c25a 899wxImage wxImage::ConvertToMono( unsigned char r, unsigned char g, unsigned char b ) const
fec19ea9
VS
900{
901 wxImage image;
902
903 wxCHECK_MSG( Ok(), image, wxT("invalid image") );
904
ff865c13 905 image.Create( M_IMGDATA->m_width, M_IMGDATA->m_height, false );
fec19ea9 906
487659e0 907 unsigned char *data = image.GetData();
fec19ea9
VS
908
909 wxCHECK_MSG( data, image, wxT("unable to create image") );
910
911 if (M_IMGDATA->m_hasMask)
912 {
913 if (M_IMGDATA->m_maskRed == r && M_IMGDATA->m_maskGreen == g &&
914 M_IMGDATA->m_maskBlue == b)
915 image.SetMaskColour( 255, 255, 255 );
916 else
917 image.SetMaskColour( 0, 0, 0 );
918 }
919
920 long size = M_IMGDATA->m_height * M_IMGDATA->m_width;
921
487659e0
VZ
922 unsigned char *srcd = M_IMGDATA->m_data;
923 unsigned char *tard = image.GetData();
fec19ea9
VS
924
925 for ( long i = 0; i < size; i++, srcd += 3, tard += 3 )
926 {
927 if (srcd[0] == r && srcd[1] == g && srcd[2] == b)
928 tard[0] = tard[1] = tard[2] = 255;
929 else
930 tard[0] = tard[1] = tard[2] = 0;
931 }
932
933 return image;
934}
935
21dc4be5
VZ
936int wxImage::GetWidth() const
937{
938 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
939
940 return M_IMGDATA->m_width;
941}
942
943int wxImage::GetHeight() const
944{
945 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
946
947 return M_IMGDATA->m_height;
948}
949
5644ac46 950long wxImage::XYToIndex(int x, int y) const
ef539066 951{
5644ac46
VZ
952 if ( Ok() &&
953 x >= 0 && y >= 0 &&
954 x < M_IMGDATA->m_width && y < M_IMGDATA->m_height )
955 {
956 return y*M_IMGDATA->m_width + x;
957 }
c7abc967 958
5644ac46
VZ
959 return -1;
960}
c7abc967 961
5644ac46
VZ
962void wxImage::SetRGB( int x, int y, unsigned char r, unsigned char g, unsigned char b )
963{
964 long pos = XYToIndex(x, y);
965 wxCHECK_RET( pos != -1, wxT("invalid image coordinates") );
c7abc967 966
a0f81e9f
PC
967 AllocExclusive();
968
5644ac46 969 pos *= 3;
c7abc967 970
ef539066
RR
971 M_IMGDATA->m_data[ pos ] = r;
972 M_IMGDATA->m_data[ pos+1 ] = g;
973 M_IMGDATA->m_data[ pos+2 ] = b;
974}
975
b737ad10
RR
976void wxImage::SetRGB( const wxRect& rect_, unsigned char r, unsigned char g, unsigned char b )
977{
978 wxCHECK_RET( Ok(), wxT("invalid image") );
979
a0f81e9f
PC
980 AllocExclusive();
981
b737ad10
RR
982 wxRect rect(rect_);
983 wxRect imageRect(0, 0, GetWidth(), GetHeight());
984 if ( rect == wxRect() )
985 {
986 rect = imageRect;
987 }
988 else
989 {
e9b64c5e
WS
990 wxCHECK_RET( imageRect.Inside(rect.GetTopLeft()) &&
991 imageRect.Inside(rect.GetBottomRight()),
b737ad10
RR
992 wxT("invalid bounding rectangle") );
993 }
994
995 int x1 = rect.GetLeft(),
996 y1 = rect.GetTop(),
997 x2 = rect.GetRight() + 1,
998 y2 = rect.GetBottom() + 1;
999
e9b64c5e 1000 unsigned char *data wxDUMMY_INITIALIZE(NULL);
b737ad10
RR
1001 int x, y, width = GetWidth();
1002 for (y = y1; y < y2; y++)
1003 {
1004 data = M_IMGDATA->m_data + (y*width + x1)*3;
1005 for (x = x1; x < x2; x++)
1006 {
1007 *data++ = r;
1008 *data++ = g;
1009 *data++ = b;
1010 }
1011 }
1012}
1013
f6bcfd97 1014unsigned char wxImage::GetRed( int x, int y ) const
ef539066 1015{
5644ac46
VZ
1016 long pos = XYToIndex(x, y);
1017 wxCHECK_MSG( pos != -1, 0, wxT("invalid image coordinates") );
c7abc967 1018
5644ac46 1019 pos *= 3;
c7abc967 1020
ef539066
RR
1021 return M_IMGDATA->m_data[pos];
1022}
1023
f6bcfd97 1024unsigned char wxImage::GetGreen( int x, int y ) const
ef539066 1025{
5644ac46
VZ
1026 long pos = XYToIndex(x, y);
1027 wxCHECK_MSG( pos != -1, 0, wxT("invalid image coordinates") );
c7abc967 1028
5644ac46 1029 pos *= 3;
c7abc967 1030
ef539066
RR
1031 return M_IMGDATA->m_data[pos+1];
1032}
1033
f6bcfd97 1034unsigned char wxImage::GetBlue( int x, int y ) const
ef539066 1035{
5644ac46
VZ
1036 long pos = XYToIndex(x, y);
1037 wxCHECK_MSG( pos != -1, 0, wxT("invalid image coordinates") );
c7abc967 1038
5644ac46 1039 pos *= 3;
c7abc967 1040
ef539066
RR
1041 return M_IMGDATA->m_data[pos+2];
1042}
4698648f
VZ
1043
1044bool wxImage::Ok() const
1045{
de8c48cf
VZ
1046 // image of 0 width or height can't be considered ok - at least because it
1047 // causes crashes in ConvertToBitmap() if we don't catch it in time
1048 wxImageRefData *data = M_IMGDATA;
1049 return data && data->m_ok && data->m_width && data->m_height;
01111366
RR
1050}
1051
487659e0 1052unsigned char *wxImage::GetData() const
01111366 1053{
487659e0 1054 wxCHECK_MSG( Ok(), (unsigned char *)NULL, wxT("invalid image") );
c7abc967 1055
fd0eed64 1056 return M_IMGDATA->m_data;
01111366
RR
1057}
1058
4013de12 1059void wxImage::SetData( unsigned char *data, bool static_data )
01111366 1060{
223d09f6 1061 wxCHECK_RET( Ok(), wxT("invalid image") );
58a8ab88 1062
ed58dbea
RR
1063 wxImageRefData *newRefData = new wxImageRefData();
1064
1065 newRefData->m_width = M_IMGDATA->m_width;
1066 newRefData->m_height = M_IMGDATA->m_height;
1067 newRefData->m_data = data;
70cd62e9 1068 newRefData->m_ok = true;
ed58dbea
RR
1069 newRefData->m_maskRed = M_IMGDATA->m_maskRed;
1070 newRefData->m_maskGreen = M_IMGDATA->m_maskGreen;
1071 newRefData->m_maskBlue = M_IMGDATA->m_maskBlue;
1072 newRefData->m_hasMask = M_IMGDATA->m_hasMask;
4013de12 1073 newRefData->m_static = static_data;
995612e2 1074
ed58dbea 1075 UnRef();
995612e2 1076
ed58dbea 1077 m_refData = newRefData;
01111366
RR
1078}
1079
4013de12 1080void wxImage::SetData( unsigned char *data, int new_width, int new_height, bool static_data )
f6bcfd97
BP
1081{
1082 wxImageRefData *newRefData = new wxImageRefData();
1083
1084 if (m_refData)
1085 {
1086 newRefData->m_width = new_width;
1087 newRefData->m_height = new_height;
1088 newRefData->m_data = data;
70cd62e9 1089 newRefData->m_ok = true;
f6bcfd97
BP
1090 newRefData->m_maskRed = M_IMGDATA->m_maskRed;
1091 newRefData->m_maskGreen = M_IMGDATA->m_maskGreen;
1092 newRefData->m_maskBlue = M_IMGDATA->m_maskBlue;
1093 newRefData->m_hasMask = M_IMGDATA->m_hasMask;
1094 }
1095 else
1096 {
1097 newRefData->m_width = new_width;
1098 newRefData->m_height = new_height;
1099 newRefData->m_data = data;
70cd62e9 1100 newRefData->m_ok = true;
f6bcfd97 1101 }
4013de12 1102 newRefData->m_static = static_data;
f6bcfd97
BP
1103
1104 UnRef();
1105
1106 m_refData = newRefData;
1107}
1108
487659e0
VZ
1109// ----------------------------------------------------------------------------
1110// alpha channel support
1111// ----------------------------------------------------------------------------
1112
1113void wxImage::SetAlpha(int x, int y, unsigned char alpha)
1114{
5644ac46 1115 wxCHECK_RET( HasAlpha(), wxT("no alpha channel") );
487659e0 1116
5644ac46
VZ
1117 long pos = XYToIndex(x, y);
1118 wxCHECK_RET( pos != -1, wxT("invalid image coordinates") );
487659e0 1119
a0f81e9f
PC
1120 AllocExclusive();
1121
5644ac46 1122 M_IMGDATA->m_alpha[pos] = alpha;
487659e0
VZ
1123}
1124
d30ee785 1125unsigned char wxImage::GetAlpha(int x, int y) const
487659e0 1126{
5644ac46 1127 wxCHECK_MSG( HasAlpha(), 0, wxT("no alpha channel") );
487659e0 1128
5644ac46
VZ
1129 long pos = XYToIndex(x, y);
1130 wxCHECK_MSG( pos != -1, 0, wxT("invalid image coordinates") );
487659e0 1131
5644ac46 1132 return M_IMGDATA->m_alpha[pos];
487659e0
VZ
1133}
1134
5644ac46
VZ
1135bool
1136wxImage::ConvertColourToAlpha(unsigned char r, unsigned char g, unsigned char b)
6408deed 1137{
5644ac46 1138 SetAlpha(NULL);
b713f891 1139
5644ac46
VZ
1140 const int w = M_IMGDATA->m_width;
1141 const int h = M_IMGDATA->m_height;
b713f891 1142
6408deed
RR
1143 unsigned char *alpha = GetAlpha();
1144 unsigned char *data = GetData();
b713f891 1145
5644ac46
VZ
1146 for ( int y = 0; y < h; y++ )
1147 {
1148 for ( int x = 0; x < w; x++ )
1149 {
1150 *alpha++ = *data;
1151 *data++ = r;
1152 *data++ = g;
1153 *data++ = b;
1154 }
1155 }
6408deed
RR
1156
1157 return true;
1158}
1159
4013de12 1160void wxImage::SetAlpha( unsigned char *alpha, bool static_data )
487659e0
VZ
1161{
1162 wxCHECK_RET( Ok(), wxT("invalid image") );
1163
a0f81e9f
PC
1164 AllocExclusive();
1165
487659e0
VZ
1166 if ( !alpha )
1167 {
edf8e8e0 1168 alpha = (unsigned char *)malloc(M_IMGDATA->m_width*M_IMGDATA->m_height);
487659e0
VZ
1169 }
1170
5402d21d 1171 free(M_IMGDATA->m_alpha);
487659e0 1172 M_IMGDATA->m_alpha = alpha;
d2502f14 1173 M_IMGDATA->m_staticAlpha = static_data;
487659e0
VZ
1174}
1175
1176unsigned char *wxImage::GetAlpha() const
1177{
1178 wxCHECK_MSG( Ok(), (unsigned char *)NULL, wxT("invalid image") );
1179
1180 return M_IMGDATA->m_alpha;
1181}
1182
828f0936
VZ
1183void wxImage::InitAlpha()
1184{
1185 wxCHECK_RET( !HasAlpha(), wxT("image already has an alpha channel") );
1186
1187 // initialize memory for alpha channel
1188 SetAlpha();
1189
1190 unsigned char *alpha = M_IMGDATA->m_alpha;
1191 const size_t lenAlpha = M_IMGDATA->m_width * M_IMGDATA->m_height;
1192
828f0936
VZ
1193 if ( HasMask() )
1194 {
1195 // use the mask to initialize the alpha channel.
1196 const unsigned char * const alphaEnd = alpha + lenAlpha;
1197
1198 const unsigned char mr = M_IMGDATA->m_maskRed;
1199 const unsigned char mg = M_IMGDATA->m_maskGreen;
1200 const unsigned char mb = M_IMGDATA->m_maskBlue;
1201 for ( unsigned char *src = M_IMGDATA->m_data;
1202 alpha < alphaEnd;
1203 src += 3, alpha++ )
1204 {
1205 *alpha = (src[0] == mr && src[1] == mg && src[2] == mb)
21dc4be5
VZ
1206 ? wxIMAGE_ALPHA_TRANSPARENT
1207 : wxIMAGE_ALPHA_OPAQUE;
828f0936
VZ
1208 }
1209
1210 M_IMGDATA->m_hasMask = false;
1211 }
1212 else // no mask
1213 {
1214 // make the image fully opaque
21dc4be5 1215 memset(alpha, wxIMAGE_ALPHA_OPAQUE, lenAlpha);
828f0936
VZ
1216 }
1217}
1218
487659e0
VZ
1219// ----------------------------------------------------------------------------
1220// mask support
1221// ----------------------------------------------------------------------------
1222
01111366
RR
1223void wxImage::SetMaskColour( unsigned char r, unsigned char g, unsigned char b )
1224{
223d09f6 1225 wxCHECK_RET( Ok(), wxT("invalid image") );
c7abc967 1226
a0f81e9f
PC
1227 AllocExclusive();
1228
fd0eed64
RR
1229 M_IMGDATA->m_maskRed = r;
1230 M_IMGDATA->m_maskGreen = g;
1231 M_IMGDATA->m_maskBlue = b;
70cd62e9 1232 M_IMGDATA->m_hasMask = true;
01111366
RR
1233}
1234
b737ad10
RR
1235bool wxImage::GetOrFindMaskColour( unsigned char *r, unsigned char *g, unsigned char *b ) const
1236{
1237 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1238
1239 if (M_IMGDATA->m_hasMask)
1240 {
1241 if (r) *r = M_IMGDATA->m_maskRed;
1242 if (g) *g = M_IMGDATA->m_maskGreen;
1243 if (b) *b = M_IMGDATA->m_maskBlue;
1244 return true;
1245 }
1246 else
1247 {
1248 FindFirstUnusedColour(r, g, b);
1249 return false;
1250 }
1251}
1252
01111366
RR
1253unsigned char wxImage::GetMaskRed() const
1254{
223d09f6 1255 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
c7abc967 1256
fd0eed64 1257 return M_IMGDATA->m_maskRed;
01111366
RR
1258}
1259
1260unsigned char wxImage::GetMaskGreen() const
1261{
223d09f6 1262 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
c7abc967 1263
fd0eed64 1264 return M_IMGDATA->m_maskGreen;
01111366
RR
1265}
1266
1267unsigned char wxImage::GetMaskBlue() const
1268{
223d09f6 1269 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
c7abc967 1270
fd0eed64 1271 return M_IMGDATA->m_maskBlue;
01111366 1272}
4698648f 1273
01111366
RR
1274void wxImage::SetMask( bool mask )
1275{
223d09f6 1276 wxCHECK_RET( Ok(), wxT("invalid image") );
c7abc967 1277
a0f81e9f
PC
1278 AllocExclusive();
1279
fd0eed64 1280 M_IMGDATA->m_hasMask = mask;
01111366
RR
1281}
1282
1283bool wxImage::HasMask() const
1284{
70cd62e9 1285 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
c7abc967 1286
fd0eed64 1287 return M_IMGDATA->m_hasMask;
01111366
RR
1288}
1289
21dc4be5 1290bool wxImage::IsTransparent(int x, int y, unsigned char threshold) const
4698648f 1291{
21dc4be5
VZ
1292 long pos = XYToIndex(x, y);
1293 wxCHECK_MSG( pos != -1, false, wxT("invalid image coordinates") );
c7abc967 1294
21dc4be5
VZ
1295 // check mask
1296 if ( M_IMGDATA->m_hasMask )
1297 {
1298 const unsigned char *p = M_IMGDATA->m_data + 3*pos;
1299 if ( p[0] == M_IMGDATA->m_maskRed &&
1300 p[1] == M_IMGDATA->m_maskGreen &&
1301 p[2] == M_IMGDATA->m_maskBlue )
1302 {
1303 return true;
1304 }
1305 }
01111366 1306
21dc4be5
VZ
1307 // then check alpha
1308 if ( M_IMGDATA->m_alpha )
1309 {
1310 if ( M_IMGDATA->m_alpha[pos] < threshold )
1311 {
1312 // transparent enough
1313 return true;
1314 }
1315 }
c7abc967 1316
21dc4be5
VZ
1317 // not transparent
1318 return false;
01111366
RR
1319}
1320
487659e0 1321bool wxImage::SetMaskFromImage(const wxImage& mask,
1f5b2017 1322 unsigned char mr, unsigned char mg, unsigned char mb)
52b64b0a 1323{
52b64b0a
RR
1324 // check that the images are the same size
1325 if ( (M_IMGDATA->m_height != mask.GetHeight() ) || (M_IMGDATA->m_width != mask.GetWidth () ) )
1326 {
bc88f66f 1327 wxLogError( _("Image and mask have different sizes.") );
70cd62e9 1328 return false;
52b64b0a 1329 }
487659e0 1330
52b64b0a
RR
1331 // find unused colour
1332 unsigned char r,g,b ;
1f5b2017 1333 if (!FindFirstUnusedColour(&r, &g, &b))
52b64b0a 1334 {
bc88f66f 1335 wxLogError( _("No unused colour in image being masked.") );
70cd62e9 1336 return false ;
52b64b0a 1337 }
487659e0 1338
a0f81e9f
PC
1339 AllocExclusive();
1340
487659e0
VZ
1341 unsigned char *imgdata = GetData();
1342 unsigned char *maskdata = mask.GetData();
52b64b0a
RR
1343
1344 const int w = GetWidth();
1345 const int h = GetHeight();
1346
1347 for (int j = 0; j < h; j++)
1f5b2017 1348 {
52b64b0a
RR
1349 for (int i = 0; i < w; i++)
1350 {
1f5b2017 1351 if ((maskdata[0] == mr) && (maskdata[1] == mg) && (maskdata[2] == mb))
52b64b0a
RR
1352 {
1353 imgdata[0] = r;
1354 imgdata[1] = g;
1355 imgdata[2] = b;
1356 }
1357 imgdata += 3;
1358 maskdata += 3;
1359 }
1f5b2017 1360 }
52b64b0a 1361
1f5b2017 1362 SetMaskColour(r, g, b);
70cd62e9 1363 SetMask(true);
487659e0 1364
70cd62e9 1365 return true;
52b64b0a 1366}
7beb59f3 1367
8f2b21e4 1368bool wxImage::ConvertAlphaToMask(unsigned char threshold)
ff5ad794
VS
1369{
1370 if (!HasAlpha())
1371 return true;
1372
1373 unsigned char mr, mg, mb;
1374 if (!FindFirstUnusedColour(&mr, &mg, &mb))
1375 {
1376 wxLogError( _("No unused colour in image being masked.") );
1377 return false;
1378 }
94406a49 1379
a0f81e9f
PC
1380 AllocExclusive();
1381
ff5ad794
VS
1382 SetMask(true);
1383 SetMaskColour(mr, mg, mb);
94406a49 1384
ff5ad794
VS
1385 unsigned char *imgdata = GetData();
1386 unsigned char *alphadata = GetAlpha();
1387
8f2b21e4
VS
1388 int w = GetWidth();
1389 int h = GetHeight();
ff5ad794 1390
8f2b21e4 1391 for (int y = 0; y < h; y++)
ff5ad794 1392 {
8f2b21e4 1393 for (int x = 0; x < w; x++, imgdata += 3, alphadata++)
ff5ad794 1394 {
e95f0d79 1395 if (*alphadata < threshold)
ff5ad794
VS
1396 {
1397 imgdata[0] = mr;
1398 imgdata[1] = mg;
1399 imgdata[2] = mb;
1400 }
1401 }
1402 }
1403
1404 free(M_IMGDATA->m_alpha);
1405 M_IMGDATA->m_alpha = NULL;
94406a49
DS
1406
1407 return true;
ff5ad794 1408}
52b64b0a 1409
21dc4be5 1410// ----------------------------------------------------------------------------
5e5437e0 1411// Palette functions
21dc4be5
VZ
1412// ----------------------------------------------------------------------------
1413
1414#if wxUSE_PALETTE
5e5437e0
JS
1415
1416bool wxImage::HasPalette() const
1417{
1418 if (!Ok())
70cd62e9 1419 return false;
5e5437e0
JS
1420
1421 return M_IMGDATA->m_palette.Ok();
1422}
1423
1424const wxPalette& wxImage::GetPalette() const
1425{
1426 wxCHECK_MSG( Ok(), wxNullPalette, wxT("invalid image") );
1427
1428 return M_IMGDATA->m_palette;
1429}
1430
1431void wxImage::SetPalette(const wxPalette& palette)
1432{
1433 wxCHECK_RET( Ok(), wxT("invalid image") );
1434
a0f81e9f
PC
1435 AllocExclusive();
1436
5e5437e0
JS
1437 M_IMGDATA->m_palette = palette;
1438}
1439
d275c7eb
VZ
1440#endif // wxUSE_PALETTE
1441
21dc4be5 1442// ----------------------------------------------------------------------------
5e5437e0 1443// Option functions (arbitrary name/value mapping)
21dc4be5
VZ
1444// ----------------------------------------------------------------------------
1445
5e5437e0
JS
1446void wxImage::SetOption(const wxString& name, const wxString& value)
1447{
1448 wxCHECK_RET( Ok(), wxT("invalid image") );
1449
a0f81e9f
PC
1450 AllocExclusive();
1451
70cd62e9 1452 int idx = M_IMGDATA->m_optionNames.Index(name, false);
5e5437e0
JS
1453 if (idx == wxNOT_FOUND)
1454 {
1455 M_IMGDATA->m_optionNames.Add(name);
1456 M_IMGDATA->m_optionValues.Add(value);
1457 }
1458 else
1459 {
1460 M_IMGDATA->m_optionNames[idx] = name;
1461 M_IMGDATA->m_optionValues[idx] = value;
1462 }
1463}
1464
1465void wxImage::SetOption(const wxString& name, int value)
1466{
1467 wxString valStr;
1468 valStr.Printf(wxT("%d"), value);
1469 SetOption(name, valStr);
1470}
1471
1472wxString wxImage::GetOption(const wxString& name) const
1473{
1474 wxCHECK_MSG( Ok(), wxEmptyString, wxT("invalid image") );
1475
70cd62e9 1476 int idx = M_IMGDATA->m_optionNames.Index(name, false);
5e5437e0
JS
1477 if (idx == wxNOT_FOUND)
1478 return wxEmptyString;
1479 else
1480 return M_IMGDATA->m_optionValues[idx];
1481}
1482
1483int wxImage::GetOptionInt(const wxString& name) const
1484{
5e5437e0
JS
1485 return wxAtoi(GetOption(name));
1486}
1487
1488bool wxImage::HasOption(const wxString& name) const
1489{
70cd62e9 1490 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
5e5437e0 1491
70cd62e9 1492 return (M_IMGDATA->m_optionNames.Index(name, false) != wxNOT_FOUND);
5e5437e0
JS
1493}
1494
21dc4be5
VZ
1495// ----------------------------------------------------------------------------
1496// image I/O
1497// ----------------------------------------------------------------------------
1498
9a6384ca
WS
1499bool wxImage::LoadFile( const wxString& WXUNUSED_UNLESS_STREAMS(filename),
1500 long WXUNUSED_UNLESS_STREAMS(type),
1501 int WXUNUSED_UNLESS_STREAMS(index) )
01111366 1502{
e02afc7a 1503#if wxUSE_STREAMS
d80207c3 1504 if (wxFileExists(filename))
6c28fd33 1505 {
d80207c3
KB
1506 wxFileInputStream stream(filename);
1507 wxBufferedInputStream bstream( stream );
60d43ad8 1508 return LoadFile(bstream, type, index);
6c28fd33 1509 }
d80207c3 1510 else
6c28fd33 1511 {
d80207c3
KB
1512 wxLogError( _("Can't load image from file '%s': file does not exist."), filename.c_str() );
1513
70cd62e9 1514 return false;
6c28fd33 1515 }
9e9ee68e 1516#else // !wxUSE_STREAMS
70cd62e9 1517 return false;
9e9ee68e
VS
1518#endif // wxUSE_STREAMS
1519}
1520
9a6384ca
WS
1521bool wxImage::LoadFile( const wxString& WXUNUSED_UNLESS_STREAMS(filename),
1522 const wxString& WXUNUSED_UNLESS_STREAMS(mimetype),
1523 int WXUNUSED_UNLESS_STREAMS(index) )
9e9ee68e
VS
1524{
1525#if wxUSE_STREAMS
d80207c3 1526 if (wxFileExists(filename))
6c28fd33 1527 {
d80207c3
KB
1528 wxFileInputStream stream(filename);
1529 wxBufferedInputStream bstream( stream );
60d43ad8 1530 return LoadFile(bstream, mimetype, index);
6c28fd33 1531 }
d80207c3 1532 else
6c28fd33 1533 {
d80207c3
KB
1534 wxLogError( _("Can't load image from file '%s': file does not exist."), filename.c_str() );
1535
70cd62e9 1536 return false;
6c28fd33 1537 }
e02afc7a 1538#else // !wxUSE_STREAMS
70cd62e9 1539 return false;
e02afc7a 1540#endif // wxUSE_STREAMS
1ccbb61a
VZ
1541}
1542
45647dcf
VS
1543
1544
1545bool wxImage::SaveFile( const wxString& filename ) const
1546{
1547 wxString ext = filename.AfterLast('.').Lower();
487659e0 1548
45647dcf
VS
1549 wxImageHandler * pHandler = FindHandler(ext, -1);
1550 if (pHandler)
1551 {
1552 SaveFile(filename, pHandler->GetType());
70cd62e9 1553 return true;
45647dcf
VS
1554 }
1555
1556 wxLogError(_("Can't save image to file '%s': unknown extension."), filename.c_str());
1557
70cd62e9 1558 return false;
45647dcf
VS
1559}
1560
9a6384ca
WS
1561bool wxImage::SaveFile( const wxString& WXUNUSED_UNLESS_STREAMS(filename),
1562 int WXUNUSED_UNLESS_STREAMS(type) ) const
1ccbb61a 1563{
e02afc7a 1564#if wxUSE_STREAMS
2a736739
VZ
1565 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1566
36aac195 1567 ((wxImage*)this)->SetOption(wxIMAGE_OPTION_FILENAME, filename);
fd94e8aa 1568
1ccbb61a 1569 wxFileOutputStream stream(filename);
9e9ee68e 1570
2b5f62a0 1571 if ( stream.IsOk() )
1b055864 1572 {
069d0f27 1573 wxBufferedOutputStream bstream( stream );
1b055864
RR
1574 return SaveFile(bstream, type);
1575 }
e02afc7a 1576#endif // wxUSE_STREAMS
3ca6a5f0 1577
70cd62e9 1578 return false;
3d05544e 1579}
01111366 1580
9a6384ca
WS
1581bool wxImage::SaveFile( const wxString& WXUNUSED_UNLESS_STREAMS(filename),
1582 const wxString& WXUNUSED_UNLESS_STREAMS(mimetype) ) const
9e9ee68e
VS
1583{
1584#if wxUSE_STREAMS
2a736739
VZ
1585 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1586
36aac195 1587 ((wxImage*)this)->SetOption(wxIMAGE_OPTION_FILENAME, filename);
fd94e8aa 1588
9e9ee68e 1589 wxFileOutputStream stream(filename);
c7abc967 1590
2b5f62a0 1591 if ( stream.IsOk() )
1b055864 1592 {
069d0f27 1593 wxBufferedOutputStream bstream( stream );
1b055864
RR
1594 return SaveFile(bstream, mimetype);
1595 }
9e9ee68e 1596#endif // wxUSE_STREAMS
3ca6a5f0 1597
70cd62e9 1598 return false;
9e9ee68e
VS
1599}
1600
9a6384ca 1601bool wxImage::CanRead( const wxString& WXUNUSED_UNLESS_STREAMS(name) )
87202f78
SB
1602{
1603#if wxUSE_STREAMS
9a6384ca
WS
1604 wxFileInputStream stream(name);
1605 return CanRead(stream);
87202f78 1606#else
9a6384ca 1607 return false;
87202f78
SB
1608#endif
1609}
1610
9a6384ca
WS
1611int wxImage::GetImageCount( const wxString& WXUNUSED_UNLESS_STREAMS(name),
1612 long WXUNUSED_UNLESS_STREAMS(type) )
60d43ad8
VS
1613{
1614#if wxUSE_STREAMS
9a6384ca
WS
1615 wxFileInputStream stream(name);
1616 if (stream.Ok())
1617 return GetImageCount(stream, type);
60d43ad8 1618#endif
2c0a4e08
VZ
1619
1620 return 0;
60d43ad8
VS
1621}
1622
e02afc7a 1623#if wxUSE_STREAMS
deb2fec0 1624
87202f78
SB
1625bool wxImage::CanRead( wxInputStream &stream )
1626{
79fa2374 1627 const wxList& list = GetHandlers();
004fd0c8 1628
222ed1d6 1629 for ( wxList::compatibility_iterator node = list.GetFirst(); node; node = node->GetNext() )
004fd0c8 1630 {
60d43ad8
VS
1631 wxImageHandler *handler=(wxImageHandler*)node->GetData();
1632 if (handler->CanRead( stream ))
70cd62e9 1633 return true;
87202f78
SB
1634 }
1635
70cd62e9 1636 return false;
60d43ad8
VS
1637}
1638
649d13e8 1639int wxImage::GetImageCount( wxInputStream &stream, long type )
60d43ad8
VS
1640{
1641 wxImageHandler *handler;
1642
1643 if ( type == wxBITMAP_TYPE_ANY )
1644 {
1645 wxList &list=GetHandlers();
1646
222ed1d6 1647 for (wxList::compatibility_iterator node = list.GetFirst(); node; node = node->GetNext())
60d43ad8
VS
1648 {
1649 handler=(wxImageHandler*)node->GetData();
1650 if ( handler->CanRead(stream) )
649d13e8 1651 return handler->GetImageCount(stream);
60d43ad8
VS
1652
1653 }
1654
1655 wxLogWarning(_("No handler found for image type."));
1656 return 0;
1657 }
1658
1659 handler = FindHandler(type);
1660
1661 if ( !handler )
1662 {
1663 wxLogWarning(_("No image handler for type %d defined."), type);
70cd62e9 1664 return false;
60d43ad8
VS
1665 }
1666
1667 if ( handler->CanRead(stream) )
1668 {
649d13e8 1669 return handler->GetImageCount(stream);
60d43ad8
VS
1670 }
1671 else
1672 {
1673 wxLogError(_("Image file is not of type %d."), type);
1674 return 0;
1675 }
87202f78
SB
1676}
1677
60d43ad8 1678bool wxImage::LoadFile( wxInputStream& stream, long type, int index )
3d05544e
JS
1679{
1680 UnRef();
c7abc967 1681
fd0eed64 1682 m_refData = new wxImageRefData;
c7abc967 1683
deb2fec0
SB
1684 wxImageHandler *handler;
1685
60d43ad8 1686 if ( type == wxBITMAP_TYPE_ANY )
deb2fec0 1687 {
995612e2 1688 wxList &list=GetHandlers();
deb2fec0 1689
222ed1d6 1690 for ( wxList::compatibility_iterator node = list.GetFirst(); node; node = node->GetNext() )
995612e2
VZ
1691 {
1692 handler=(wxImageHandler*)node->GetData();
60d43ad8 1693 if ( handler->CanRead(stream) )
70cd62e9 1694 return handler->LoadFile(this, stream, true/*verbose*/, index);
7b2471a0 1695
995612e2 1696 }
deb2fec0 1697
58c837a4 1698 wxLogWarning( _("No handler found for image type.") );
70cd62e9 1699 return false;
deb2fec0
SB
1700 }
1701
1702 handler = FindHandler(type);
c7abc967 1703
2b5f62a0 1704 if (handler == 0)
fd0eed64 1705 {
58c837a4 1706 wxLogWarning( _("No image handler for type %d defined."), type );
c7abc967 1707
70cd62e9 1708 return false;
fd0eed64 1709 }
c7abc967 1710
b598ec91 1711 if (stream.IsSeekable() && !handler->CanRead(stream))
dd9ea234
JS
1712 {
1713 wxLogError(_("Image file is not of type %d."), type);
1714 return false;
1715 }
1716 else
1717 return handler->LoadFile(this, stream, true/*verbose*/, index);
01111366
RR
1718}
1719
60d43ad8 1720bool wxImage::LoadFile( wxInputStream& stream, const wxString& mimetype, int index )
9e9ee68e
VS
1721{
1722 UnRef();
1723
1724 m_refData = new wxImageRefData;
1725
1726 wxImageHandler *handler = FindHandlerMime(mimetype);
1727
2b5f62a0 1728 if (handler == 0)
9e9ee68e 1729 {
58c837a4 1730 wxLogWarning( _("No image handler for type %s defined."), mimetype.GetData() );
9e9ee68e 1731
70cd62e9 1732 return false;
9e9ee68e
VS
1733 }
1734
b598ec91 1735 if (stream.IsSeekable() && !handler->CanRead(stream))
dd9ea234
JS
1736 {
1737 wxLogError(_("Image file is not of type %s."), (const wxChar*) mimetype);
1738 return false;
1739 }
1740 else
1741 return handler->LoadFile( this, stream, true/*verbose*/, index );
9e9ee68e
VS
1742}
1743
e0a76d8d 1744bool wxImage::SaveFile( wxOutputStream& stream, int type ) const
01111366 1745{
70cd62e9 1746 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
c7abc967 1747
fd0eed64 1748 wxImageHandler *handler = FindHandler(type);
2a736739 1749 if ( !handler )
fd0eed64 1750 {
58c837a4 1751 wxLogWarning( _("No image handler for type %d defined."), type );
9e9ee68e 1752
70cd62e9 1753 return false;
9e9ee68e
VS
1754 }
1755
e0a76d8d 1756 return handler->SaveFile( (wxImage*)this, stream );
9e9ee68e
VS
1757}
1758
e0a76d8d 1759bool wxImage::SaveFile( wxOutputStream& stream, const wxString& mimetype ) const
9e9ee68e 1760{
70cd62e9 1761 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
c7abc967 1762
9e9ee68e 1763 wxImageHandler *handler = FindHandlerMime(mimetype);
2a736739 1764 if ( !handler )
9e9ee68e 1765 {
58c837a4 1766 wxLogWarning( _("No image handler for type %s defined."), mimetype.GetData() );
c7abc967 1767
70cd62e9 1768 return false;
fd0eed64 1769 }
c7abc967 1770
e0a76d8d 1771 return handler->SaveFile( (wxImage*)this, stream );
01111366 1772}
e02afc7a 1773#endif // wxUSE_STREAMS
01111366 1774
21dc4be5
VZ
1775// ----------------------------------------------------------------------------
1776// image I/O handlers
1777// ----------------------------------------------------------------------------
1778
01111366
RR
1779void wxImage::AddHandler( wxImageHandler *handler )
1780{
2b5f62a0
VZ
1781 // Check for an existing handler of the type being added.
1782 if (FindHandler( handler->GetType() ) == 0)
1783 {
1784 sm_handlers.Append( handler );
1785 }
1786 else
1787 {
1788 // This is not documented behaviour, merely the simplest 'fix'
1789 // for preventing duplicate additions. If someone ever has
1790 // a good reason to add and remove duplicate handlers (and they
1791 // may) we should probably refcount the duplicates.
1792 // also an issue in InsertHandler below.
1793
1794 wxLogDebug( _T("Adding duplicate image handler for '%s'"),
1795 handler->GetName().c_str() );
1796 delete handler;
1797 }
01111366
RR
1798}
1799
1800void wxImage::InsertHandler( wxImageHandler *handler )
1801{
2b5f62a0
VZ
1802 // Check for an existing handler of the type being added.
1803 if (FindHandler( handler->GetType() ) == 0)
1804 {
1805 sm_handlers.Insert( handler );
1806 }
1807 else
1808 {
1809 // see AddHandler for additional comments.
1810 wxLogDebug( _T("Inserting duplicate image handler for '%s'"),
1811 handler->GetName().c_str() );
1812 delete handler;
1813 }
01111366
RR
1814}
1815
1816bool wxImage::RemoveHandler( const wxString& name )
1817{
fd0eed64
RR
1818 wxImageHandler *handler = FindHandler(name);
1819 if (handler)
1820 {
1821 sm_handlers.DeleteObject(handler);
222ed1d6 1822 delete handler;
70cd62e9 1823 return true;
fd0eed64
RR
1824 }
1825 else
70cd62e9 1826 return false;
01111366
RR
1827}
1828
1829wxImageHandler *wxImage::FindHandler( const wxString& name )
1830{
222ed1d6 1831 wxList::compatibility_iterator node = sm_handlers.GetFirst();
fd0eed64
RR
1832 while (node)
1833 {
b1d4dd7a 1834 wxImageHandler *handler = (wxImageHandler*)node->GetData();
ce3ed50d 1835 if (handler->GetName().Cmp(name) == 0) return handler;
c7abc967 1836
b1d4dd7a 1837 node = node->GetNext();
fd0eed64 1838 }
2b5f62a0 1839 return 0;
01111366
RR
1840}
1841
1842wxImageHandler *wxImage::FindHandler( const wxString& extension, long bitmapType )
1843{
222ed1d6 1844 wxList::compatibility_iterator node = sm_handlers.GetFirst();
fd0eed64
RR
1845 while (node)
1846 {
b1d4dd7a 1847 wxImageHandler *handler = (wxImageHandler*)node->GetData();
ce3ed50d 1848 if ( (handler->GetExtension().Cmp(extension) == 0) &&
fd0eed64 1849 (bitmapType == -1 || handler->GetType() == bitmapType) )
dbda9e86 1850 return handler;
b1d4dd7a 1851 node = node->GetNext();
fd0eed64 1852 }
2b5f62a0 1853 return 0;
01111366
RR
1854}
1855
1856wxImageHandler *wxImage::FindHandler( long bitmapType )
1857{
222ed1d6 1858 wxList::compatibility_iterator node = sm_handlers.GetFirst();
fd0eed64
RR
1859 while (node)
1860 {
b1d4dd7a 1861 wxImageHandler *handler = (wxImageHandler *)node->GetData();
fd0eed64 1862 if (handler->GetType() == bitmapType) return handler;
b1d4dd7a 1863 node = node->GetNext();
fd0eed64 1864 }
2b5f62a0 1865 return 0;
fd0eed64
RR
1866}
1867
9e9ee68e
VS
1868wxImageHandler *wxImage::FindHandlerMime( const wxString& mimetype )
1869{
222ed1d6 1870 wxList::compatibility_iterator node = sm_handlers.GetFirst();
9e9ee68e
VS
1871 while (node)
1872 {
b1d4dd7a 1873 wxImageHandler *handler = (wxImageHandler *)node->GetData();
70cd62e9 1874 if (handler->GetMimeType().IsSameAs(mimetype, false)) return handler;
b1d4dd7a 1875 node = node->GetNext();
9e9ee68e 1876 }
2b5f62a0 1877 return 0;
9e9ee68e
VS
1878}
1879
fd0eed64
RR
1880void wxImage::InitStandardHandlers()
1881{
77fac225 1882#if wxUSE_STREAMS
66e23ad2 1883 AddHandler(new wxBMPHandler);
77fac225 1884#endif // wxUSE_STREAMS
01111366
RR
1885}
1886
1887void wxImage::CleanUpHandlers()
1888{
222ed1d6 1889 wxList::compatibility_iterator node = sm_handlers.GetFirst();
fd0eed64
RR
1890 while (node)
1891 {
b1d4dd7a 1892 wxImageHandler *handler = (wxImageHandler *)node->GetData();
222ed1d6 1893 wxList::compatibility_iterator next = node->GetNext();
fd0eed64 1894 delete handler;
fd0eed64
RR
1895 node = next;
1896 }
01111366 1897
222ed1d6
MB
1898 sm_handlers.Clear();
1899}
ff865c13 1900
939fadc8
JS
1901wxString wxImage::GetImageExtWildcard()
1902{
1903 wxString fmts;
1904
1905 wxList& Handlers = wxImage::GetHandlers();
222ed1d6 1906 wxList::compatibility_iterator Node = Handlers.GetFirst();
939fadc8
JS
1907 while ( Node )
1908 {
1909 wxImageHandler* Handler = (wxImageHandler*)Node->GetData();
1910 fmts += wxT("*.") + Handler->GetExtension();
1911 Node = Node->GetNext();
1912 if ( Node ) fmts += wxT(";");
1913 }
1914
1915 return wxT("(") + fmts + wxT(")|") + fmts;
1916}
1917
978d3d36
VZ
1918wxImage::HSVValue wxImage::RGBtoHSV(const RGBValue& rgb)
1919{
978d3d36
VZ
1920 const double red = rgb.red / 255.0,
1921 green = rgb.green / 255.0,
1922 blue = rgb.blue / 255.0;
1923
c77a6796
VZ
1924 // find the min and max intensity (and remember which one was it for the
1925 // latter)
978d3d36 1926 double minimumRGB = red;
c77a6796 1927 if ( green < minimumRGB )
978d3d36 1928 minimumRGB = green;
c77a6796 1929 if ( blue < minimumRGB )
978d3d36
VZ
1930 minimumRGB = blue;
1931
c77a6796 1932 enum { RED, GREEN, BLUE } chMax = RED;
978d3d36 1933 double maximumRGB = red;
c77a6796
VZ
1934 if ( green > maximumRGB )
1935 {
1936 chMax = GREEN;
978d3d36 1937 maximumRGB = green;
c77a6796
VZ
1938 }
1939 if ( blue > maximumRGB )
1940 {
1941 chMax = BLUE;
978d3d36 1942 maximumRGB = blue;
c77a6796 1943 }
978d3d36 1944
c77a6796 1945 const double value = maximumRGB;
978d3d36 1946
38d4b1e4 1947 double hue = 0.0, saturation;
c77a6796
VZ
1948 const double deltaRGB = maximumRGB - minimumRGB;
1949 if ( wxIsNullDouble(deltaRGB) )
978d3d36
VZ
1950 {
1951 // Gray has no color
1952 hue = 0.0;
1953 saturation = 0.0;
1954 }
1955 else
1956 {
c77a6796
VZ
1957 switch ( chMax )
1958 {
1959 case RED:
1960 hue = (green - blue) / deltaRGB;
1961 break;
978d3d36 1962
c77a6796
VZ
1963 case GREEN:
1964 hue = 2.0 + (blue - red) / deltaRGB;
1965 break;
978d3d36 1966
c77a6796
VZ
1967 case BLUE:
1968 hue = 4.0 + (red - green) / deltaRGB;
1969 break;
38d4b1e4
WS
1970
1971 default:
1972 wxFAIL_MSG(wxT("hue not specified"));
1973 break;
c77a6796
VZ
1974 }
1975
1976 hue /= 6.0;
978d3d36 1977
c77a6796
VZ
1978 if ( hue < 0.0 )
1979 hue += 1.0;
978d3d36 1980
c77a6796 1981 saturation = deltaRGB / maximumRGB;
978d3d36
VZ
1982 }
1983
1984 return HSVValue(hue, saturation, value);
1985}
1986
1987wxImage::RGBValue wxImage::HSVtoRGB(const HSVValue& hsv)
1988{
1989 double red, green, blue;
1990
c77a6796 1991 if ( wxIsNullDouble(hsv.saturation) )
978d3d36 1992 {
c77a6796
VZ
1993 // Grey
1994 red = hsv.value;
978d3d36 1995 green = hsv.value;
c77a6796 1996 blue = hsv.value;
978d3d36 1997 }
c77a6796 1998 else // not grey
978d3d36
VZ
1999 {
2000 double hue = hsv.hue * 6.0; // sector 0 to 5
2001 int i = (int)floor(hue);
2002 double f = hue - i; // fractional part of h
2003 double p = hsv.value * (1.0 - hsv.saturation);
2004
2005 switch (i)
2006 {
2007 case 0:
2008 red = hsv.value;
2009 green = hsv.value * (1.0 - hsv.saturation * (1.0 - f));
2010 blue = p;
2011 break;
2012
2013 case 1:
2014 red = hsv.value * (1.0 - hsv.saturation * f);
2015 green = hsv.value;
2016 blue = p;
2017 break;
2018
2019 case 2:
2020 red = p;
2021 green = hsv.value;
2022 blue = hsv.value * (1.0 - hsv.saturation * (1.0 - f));
2023 break;
2024
2025 case 3:
2026 red = p;
2027 green = hsv.value * (1.0 - hsv.saturation * f);
2028 blue = hsv.value;
2029 break;
2030
2031 case 4:
2032 red = hsv.value * (1.0 - hsv.saturation * (1.0 - f));
2033 green = p;
2034 blue = hsv.value;
2035 break;
2036
2037 default: // case 5:
2038 red = hsv.value;
2039 green = p;
2040 blue = hsv.value * (1.0 - hsv.saturation * f);
2041 break;
2042 }
2043 }
2044
2045 return RGBValue((unsigned char)(red * 255.0),
2046 (unsigned char)(green * 255.0),
2047 (unsigned char)(blue * 255.0));
2048}
2049
2050/*
2051 * Rotates the hue of each pixel of the image. angle is a double in the range
2052 * -1.0..1.0 where -1.0 is -360 degrees and 1.0 is 360 degrees
2053 */
2054void wxImage::RotateHue(double angle)
2055{
a0f81e9f
PC
2056 AllocExclusive();
2057
978d3d36
VZ
2058 unsigned char *srcBytePtr;
2059 unsigned char *dstBytePtr;
2060 unsigned long count;
2061 wxImage::HSVValue hsv;
2062 wxImage::RGBValue rgb;
2063
6926b9c4 2064 wxASSERT (angle >= -1.0 && angle <= 1.0);
978d3d36 2065 count = M_IMGDATA->m_width * M_IMGDATA->m_height;
c77a6796 2066 if ( count > 0 && !wxIsNullDouble(angle) )
978d3d36
VZ
2067 {
2068 srcBytePtr = M_IMGDATA->m_data;
2069 dstBytePtr = srcBytePtr;
2070 do
2071 {
2072 rgb.red = *srcBytePtr++;
2073 rgb.green = *srcBytePtr++;
2074 rgb.blue = *srcBytePtr++;
2075 hsv = RGBtoHSV(rgb);
2076
2077 hsv.hue = hsv.hue + angle;
2078 if (hsv.hue > 1.0)
2079 hsv.hue = hsv.hue - 1.0;
2080 else if (hsv.hue < 0.0)
2081 hsv.hue = hsv.hue + 1.0;
2082
2083 rgb = HSVtoRGB(hsv);
2084 *dstBytePtr++ = rgb.red;
2085 *dstBytePtr++ = rgb.green;
2086 *dstBytePtr++ = rgb.blue;
2087 } while (--count != 0);
2088 }
2089}
2090
01111366
RR
2091//-----------------------------------------------------------------------------
2092// wxImageHandler
2093//-----------------------------------------------------------------------------
2094
63d963a1 2095IMPLEMENT_ABSTRACT_CLASS(wxImageHandler,wxObject)
01111366 2096
e02afc7a 2097#if wxUSE_STREAMS
700ec454 2098bool wxImageHandler::LoadFile( wxImage *WXUNUSED(image), wxInputStream& WXUNUSED(stream), bool WXUNUSED(verbose), int WXUNUSED(index) )
01111366 2099{
70cd62e9 2100 return false;
01111366
RR
2101}
2102
deb2fec0 2103bool wxImageHandler::SaveFile( wxImage *WXUNUSED(image), wxOutputStream& WXUNUSED(stream), bool WXUNUSED(verbose) )
01111366 2104{
70cd62e9 2105 return false;
01111366 2106}
0828c087 2107
649d13e8 2108int wxImageHandler::GetImageCount( wxInputStream& WXUNUSED(stream) )
700ec454
RR
2109{
2110 return 1;
2111}
2112
0828c087
VS
2113bool wxImageHandler::CanRead( const wxString& name )
2114{
0828c087
VS
2115 if (wxFileExists(name))
2116 {
2117 wxFileInputStream stream(name);
2118 return CanRead(stream);
2119 }
2120
39d16996 2121 wxLogError( _("Can't check image format of file '%s': file does not exist."), name.c_str() );
0828c087 2122
70cd62e9 2123 return false;
39d16996
VZ
2124}
2125
2126bool wxImageHandler::CallDoCanRead(wxInputStream& stream)
2127{
30984dea 2128 wxFileOffset posOld = stream.TellI();
39d16996
VZ
2129 if ( posOld == wxInvalidOffset )
2130 {
2131 // can't test unseekable stream
70cd62e9 2132 return false;
39d16996
VZ
2133 }
2134
2135 bool ok = DoCanRead(stream);
2136
2137 // restore the old position to be able to test other formats and so on
2138 if ( stream.SeekI(posOld) == wxInvalidOffset )
2139 {
2140 wxLogDebug(_T("Failed to rewind the stream in wxImageHandler!"));
2141
2142 // reading would fail anyhow as we're not at the right position
70cd62e9 2143 return false;
0828c087 2144 }
39d16996
VZ
2145
2146 return ok;
0828c087
VS
2147}
2148
e02afc7a 2149#endif // wxUSE_STREAMS
01111366 2150
487659e0
VZ
2151// ----------------------------------------------------------------------------
2152// image histogram stuff
2153// ----------------------------------------------------------------------------
2154
2155bool
2156wxImageHistogram::FindFirstUnusedColour(unsigned char *r,
2157 unsigned char *g,
2158 unsigned char *b,
2159 unsigned char r2,
2160 unsigned char b2,
2161 unsigned char g2) const
2162{
2163 unsigned long key = MakeKey(r2, g2, b2);
2164
2165 while ( find(key) != end() )
2166 {
2167 // color already used
2168 r2++;
2169 if ( r2 >= 255 )
2170 {
2171 r2 = 0;
2172 g2++;
2173 if ( g2 >= 255 )
2174 {
2175 g2 = 0;
2176 b2++;
2177 if ( b2 >= 255 )
2178 {
bc88f66f 2179 wxLogError(_("No unused colour in image.") );
70cd62e9 2180 return false;
487659e0
VZ
2181 }
2182 }
2183 }
2184
2185 key = MakeKey(r2, g2, b2);
2186 }
2187
2188 if ( r )
2189 *r = r2;
2190 if ( g )
2191 *g = g2;
2192 if ( b )
2193 *b = b2;
2194
70cd62e9 2195 return true;
487659e0
VZ
2196}
2197
2198bool
2199wxImage::FindFirstUnusedColour(unsigned char *r,
2200 unsigned char *g,
2201 unsigned char *b,
2202 unsigned char r2,
2203 unsigned char b2,
2204 unsigned char g2) const
2205{
2206 wxImageHistogram histogram;
2207
2208 ComputeHistogram(histogram);
2209
2210 return histogram.FindFirstUnusedColour(r, g, b, r2, g2, b2);
2211}
2212
2213
c9d01afd 2214
89d00456
GRG
2215// GRG, Dic/99
2216// Counts and returns the number of different colours. Optionally stops
cc9f7d79
GRG
2217// when it exceeds 'stopafter' different colours. This is useful, for
2218// example, to see if the image can be saved as 8-bit (256 colour or
2219// less, in this case it would be invoked as CountColours(256)). Default
2220// value for stopafter is -1 (don't care).
89d00456 2221//
e0a76d8d 2222unsigned long wxImage::CountColours( unsigned long stopafter ) const
89d00456
GRG
2223{
2224 wxHashTable h;
ad30de59 2225 wxObject dummy;
33ac7e6f 2226 unsigned char r, g, b;
eeca3a46 2227 unsigned char *p;
89d00456
GRG
2228 unsigned long size, nentries, key;
2229
2230 p = GetData();
2231 size = GetWidth() * GetHeight();
2232 nentries = 0;
2233
cc9f7d79 2234 for (unsigned long j = 0; (j < size) && (nentries <= stopafter) ; j++)
89d00456
GRG
2235 {
2236 r = *(p++);
2237 g = *(p++);
2238 b = *(p++);
487659e0 2239 key = wxImageHistogram::MakeKey(r, g, b);
89d00456 2240
ad30de59 2241 if (h.Get(key) == NULL)
89d00456 2242 {
ad30de59 2243 h.Put(key, &dummy);
89d00456
GRG
2244 nentries++;
2245 }
2246 }
2247
89d00456
GRG
2248 return nentries;
2249}
2250
2251
e0a76d8d 2252unsigned long wxImage::ComputeHistogram( wxImageHistogram &h ) const
c9d01afd 2253{
487659e0
VZ
2254 unsigned char *p = GetData();
2255 unsigned long nentries = 0;
952ae1e8
VS
2256
2257 h.clear();
c9d01afd 2258
487659e0 2259 const unsigned long size = GetWidth() * GetHeight();
c9d01afd 2260
487659e0
VZ
2261 unsigned char r, g, b;
2262 for ( unsigned long n = 0; n < size; n++ )
c9d01afd 2263 {
487659e0
VZ
2264 r = *p++;
2265 g = *p++;
2266 b = *p++;
2267
2268 wxImageHistogramEntry& entry = h[wxImageHistogram::MakeKey(r, g, b)];
c9d01afd 2269
952ae1e8
VS
2270 if ( entry.value++ == 0 )
2271 entry.index = nentries++;
c9d01afd
GRG
2272 }
2273
2274 return nentries;
2275}
2276
7a632f10
JS
2277/*
2278 * Rotation code by Carlos Moreno
2279 */
2280
b5c91ac6
GRG
2281// GRG: I've removed wxRotationPoint - we already have wxRealPoint which
2282// does exactly the same thing. And I also got rid of wxRotationPixel
2283// bacause of potential problems in architectures where alignment
2284// is an issue, so I had to rewrite parts of the code.
7a632f10 2285
7a632f10
JS
2286static const double gs_Epsilon = 1e-10;
2287
2288static inline int wxCint (double x)
2289{
2290 return (x > 0) ? (int) (x + 0.5) : (int) (x - 0.5);
2291}
2292
2293
2294// Auxiliary function to rotate a point (x,y) with respect to point p0
2295// make it inline and use a straight return to facilitate optimization
2296// also, the function receives the sine and cosine of the angle to avoid
2297// repeating the time-consuming calls to these functions -- sin/cos can
2298// be computed and stored in the calling function.
2299
b5c91ac6 2300inline wxRealPoint rotated_point (const wxRealPoint & p, double cos_angle, double sin_angle, const wxRealPoint & p0)
7a632f10 2301{
b5c91ac6
GRG
2302 return wxRealPoint (p0.x + (p.x - p0.x) * cos_angle - (p.y - p0.y) * sin_angle,
2303 p0.y + (p.y - p0.y) * cos_angle + (p.x - p0.x) * sin_angle);
7a632f10
JS
2304}
2305
b5c91ac6 2306inline wxRealPoint rotated_point (double x, double y, double cos_angle, double sin_angle, const wxRealPoint & p0)
7a632f10 2307{
b5c91ac6 2308 return rotated_point (wxRealPoint(x,y), cos_angle, sin_angle, p0);
7a632f10
JS
2309}
2310
2311wxImage wxImage::Rotate(double angle, const wxPoint & centre_of_rotation, bool interpolating, wxPoint * offset_after_rotation) const
2312{
7a632f10
JS
2313 int i;
2314 angle = -angle; // screen coordinates are a mirror image of "real" coordinates
b713f891 2315
6408deed 2316 bool has_alpha = HasAlpha();
7a632f10 2317
ad30de59 2318 // Create pointer-based array to accelerate access to wxImage's data
b5c91ac6 2319 unsigned char ** data = new unsigned char * [GetHeight()];
b5c91ac6 2320 data[0] = GetData();
b5c91ac6
GRG
2321 for (i = 1; i < GetHeight(); i++)
2322 data[i] = data[i - 1] + (3 * GetWidth());
7a632f10 2323
b713f891 2324 // Same for alpha channel
6408deed
RR
2325 unsigned char ** alpha = NULL;
2326 if (has_alpha)
2327 {
2328 alpha = new unsigned char * [GetHeight()];
2329 alpha[0] = GetAlpha();
2330 for (i = 1; i < GetHeight(); i++)
2331 alpha[i] = alpha[i - 1] + GetWidth();
2332 }
2333
b5c91ac6 2334 // precompute coefficients for rotation formula
ad30de59 2335 // (sine and cosine of the angle)
7a632f10
JS
2336 const double cos_angle = cos(angle);
2337 const double sin_angle = sin(angle);
2338
ad30de59
GRG
2339 // Create new Image to store the result
2340 // First, find rectangle that covers the rotated image; to do that,
2341 // rotate the four corners
7a632f10 2342
b5c91ac6 2343 const wxRealPoint p0(centre_of_rotation.x, centre_of_rotation.y);
7a632f10 2344
b5c91ac6
GRG
2345 wxRealPoint p1 = rotated_point (0, 0, cos_angle, sin_angle, p0);
2346 wxRealPoint p2 = rotated_point (0, GetHeight(), cos_angle, sin_angle, p0);
2347 wxRealPoint p3 = rotated_point (GetWidth(), 0, cos_angle, sin_angle, p0);
2348 wxRealPoint p4 = rotated_point (GetWidth(), GetHeight(), cos_angle, sin_angle, p0);
7a632f10 2349
4e115ed2
VZ
2350 int x1a = (int) floor (wxMin (wxMin(p1.x, p2.x), wxMin(p3.x, p4.x)));
2351 int y1a = (int) floor (wxMin (wxMin(p1.y, p2.y), wxMin(p3.y, p4.y)));
2352 int x2a = (int) ceil (wxMax (wxMax(p1.x, p2.x), wxMax(p3.x, p4.x)));
2353 int y2a = (int) ceil (wxMax (wxMax(p1.y, p2.y), wxMax(p3.y, p4.y)));
7a632f10 2354
6408deed 2355 // Create rotated image
4e115ed2 2356 wxImage rotated (x2a - x1a + 1, y2a - y1a + 1, false);
6408deed
RR
2357 // With alpha channel
2358 if (has_alpha)
2359 rotated.SetAlpha();
7a632f10
JS
2360
2361 if (offset_after_rotation != NULL)
2362 {
4e115ed2 2363 *offset_after_rotation = wxPoint (x1a, y1a);
7a632f10
JS
2364 }
2365
b5c91ac6
GRG
2366 // GRG: The rotated (destination) image is always accessed
2367 // sequentially, so there is no need for a pointer-based
2368 // array here (and in fact it would be slower).
2369 //
2370 unsigned char * dst = rotated.GetData();
b713f891 2371
6408deed
RR
2372 unsigned char * alpha_dst = NULL;
2373 if (has_alpha)
2374 alpha_dst = rotated.GetAlpha();
7a632f10 2375
ad30de59
GRG
2376 // GRG: if the original image has a mask, use its RGB values
2377 // as the blank pixel, else, fall back to default (black).
2378 //
b5c91ac6
GRG
2379 unsigned char blank_r = 0;
2380 unsigned char blank_g = 0;
2381 unsigned char blank_b = 0;
ad30de59
GRG
2382
2383 if (HasMask())
2384 {
b5c91ac6
GRG
2385 blank_r = GetMaskRed();
2386 blank_g = GetMaskGreen();
2387 blank_b = GetMaskBlue();
2388 rotated.SetMaskColour( blank_r, blank_g, blank_b );
ad30de59
GRG
2389 }
2390
2391 // Now, for each point of the rotated image, find where it came from, by
2392 // performing an inverse rotation (a rotation of -angle) and getting the
2393 // pixel at those coordinates
2394
b5c91ac6
GRG
2395 // GRG: I've taken the (interpolating) test out of the loops, so that
2396 // it is done only once, instead of repeating it for each pixel.
7a632f10
JS
2397
2398 int x;
b5c91ac6 2399 if (interpolating)
7a632f10
JS
2400 {
2401 for (int y = 0; y < rotated.GetHeight(); y++)
2402 {
b5c91ac6 2403 for (x = 0; x < rotated.GetWidth(); x++)
7a632f10 2404 {
4e115ed2 2405 wxRealPoint src = rotated_point (x + x1a, y + y1a, cos_angle, -sin_angle, p0);
b5c91ac6 2406
f2506310
JS
2407 if (-0.25 < src.x && src.x < GetWidth() - 0.75 &&
2408 -0.25 < src.y && src.y < GetHeight() - 0.75)
7a632f10 2409 {
ad30de59
GRG
2410 // interpolate using the 4 enclosing grid-points. Those
2411 // points can be obtained using floor and ceiling of the
2412 // exact coordinates of the point
f2506310
JS
2413 int x1, y1, x2, y2;
2414
2415 if (0 < src.x && src.x < GetWidth() - 1)
2416 {
2417 x1 = wxCint(floor(src.x));
2418 x2 = wxCint(ceil(src.x));
2419 }
2420 else // else means that x is near one of the borders (0 or width-1)
2421 {
2422 x1 = x2 = wxCint (src.x);
2423 }
2424
2425 if (0 < src.y && src.y < GetHeight() - 1)
2426 {
2427 y1 = wxCint(floor(src.y));
2428 y2 = wxCint(ceil(src.y));
2429 }
2430 else
2431 {
2432 y1 = y2 = wxCint (src.y);
2433 }
7a632f10 2434
ad30de59
GRG
2435 // get four points and the distances (square of the distance,
2436 // for efficiency reasons) for the interpolation formula
b5c91ac6
GRG
2437
2438 // GRG: Do not calculate the points until they are
2439 // really needed -- this way we can calculate
2440 // just one, instead of four, if d1, d2, d3
2441 // or d4 are < gs_Epsilon
7a632f10
JS
2442
2443 const double d1 = (src.x - x1) * (src.x - x1) + (src.y - y1) * (src.y - y1);
2444 const double d2 = (src.x - x2) * (src.x - x2) + (src.y - y1) * (src.y - y1);
2445 const double d3 = (src.x - x2) * (src.x - x2) + (src.y - y2) * (src.y - y2);
2446 const double d4 = (src.x - x1) * (src.x - x1) + (src.y - y2) * (src.y - y2);
2447
ad30de59
GRG
2448 // Now interpolate as a weighted average of the four surrounding
2449 // points, where the weights are the distances to each of those points
7a632f10 2450
ad30de59
GRG
2451 // If the point is exactly at one point of the grid of the source
2452 // image, then don't interpolate -- just assign the pixel
7a632f10 2453
06b466c7 2454 if (d1 < gs_Epsilon) // d1,d2,d3,d4 are positive -- no need for abs()
7a632f10 2455 {
b5c91ac6
GRG
2456 unsigned char *p = data[y1] + (3 * x1);
2457 *(dst++) = *(p++);
2458 *(dst++) = *(p++);
6408deed 2459 *(dst++) = *p;
b713f891 2460
6408deed 2461 if (has_alpha)
4e115ed2 2462 *(alpha_dst++) = *(alpha[y1] + x1);
7a632f10
JS
2463 }
2464 else if (d2 < gs_Epsilon)
2465 {
b5c91ac6
GRG
2466 unsigned char *p = data[y1] + (3 * x2);
2467 *(dst++) = *(p++);
2468 *(dst++) = *(p++);
6408deed 2469 *(dst++) = *p;
b713f891 2470
6408deed 2471 if (has_alpha)
4e115ed2 2472 *(alpha_dst++) = *(alpha[y1] + x2);
7a632f10
JS
2473 }
2474 else if (d3 < gs_Epsilon)
2475 {
b5c91ac6
GRG
2476 unsigned char *p = data[y2] + (3 * x2);
2477 *(dst++) = *(p++);
2478 *(dst++) = *(p++);
6408deed 2479 *(dst++) = *p;
b713f891 2480
6408deed 2481 if (has_alpha)
4e115ed2 2482 *(alpha_dst++) = *(alpha[y2] + x2);
7a632f10
JS
2483 }
2484 else if (d4 < gs_Epsilon)
2485 {
b5c91ac6
GRG
2486 unsigned char *p = data[y2] + (3 * x1);
2487 *(dst++) = *(p++);
2488 *(dst++) = *(p++);
6408deed 2489 *(dst++) = *p;
b713f891 2490
6408deed 2491 if (has_alpha)
4e115ed2 2492 *(alpha_dst++) = *(alpha[y2] + x1);
7a632f10
JS
2493 }
2494 else
2495 {
06b466c7 2496 // weights for the weighted average are proportional to the inverse of the distance
b5c91ac6
GRG
2497 unsigned char *v1 = data[y1] + (3 * x1);
2498 unsigned char *v2 = data[y1] + (3 * x2);
2499 unsigned char *v3 = data[y2] + (3 * x2);
2500 unsigned char *v4 = data[y2] + (3 * x1);
2501
06b466c7
VZ
2502 const double w1 = 1/d1, w2 = 1/d2, w3 = 1/d3, w4 = 1/d4;
2503
b5c91ac6
GRG
2504 // GRG: Unrolled.
2505
2506 *(dst++) = (unsigned char)
2507 ( (w1 * *(v1++) + w2 * *(v2++) +
2508 w3 * *(v3++) + w4 * *(v4++)) /
2509 (w1 + w2 + w3 + w4) );
2510 *(dst++) = (unsigned char)
2511 ( (w1 * *(v1++) + w2 * *(v2++) +
2512 w3 * *(v3++) + w4 * *(v4++)) /
2513 (w1 + w2 + w3 + w4) );
2514 *(dst++) = (unsigned char)
999836aa
VZ
2515 ( (w1 * *v1 + w2 * *v2 +
2516 w3 * *v3 + w4 * *v4) /
b5c91ac6 2517 (w1 + w2 + w3 + w4) );
b713f891 2518
6408deed
RR
2519 if (has_alpha)
2520 {
4e115ed2
VZ
2521 v1 = alpha[y1] + (x1);
2522 v2 = alpha[y1] + (x2);
2523 v3 = alpha[y2] + (x2);
2524 v4 = alpha[y2] + (x1);
6408deed
RR
2525
2526 *(alpha_dst++) = (unsigned char)
2527 ( (w1 * *v1 + w2 * *v2 +
2528 w3 * *v3 + w4 * *v4) /
2529 (w1 + w2 + w3 + w4) );
2530 }
7a632f10
JS
2531 }
2532 }
2533 else
2534 {
b5c91ac6
GRG
2535 *(dst++) = blank_r;
2536 *(dst++) = blank_g;
2537 *(dst++) = blank_b;
b713f891 2538
6408deed
RR
2539 if (has_alpha)
2540 *(alpha_dst++) = 0;
7a632f10
JS
2541 }
2542 }
b5c91ac6
GRG
2543 }
2544 }
2545 else // not interpolating
2546 {
2547 for (int y = 0; y < rotated.GetHeight(); y++)
2548 {
2549 for (x = 0; x < rotated.GetWidth(); x++)
7a632f10 2550 {
4e115ed2 2551 wxRealPoint src = rotated_point (x + x1a, y + y1a, cos_angle, -sin_angle, p0);
b5c91ac6
GRG
2552
2553 const int xs = wxCint (src.x); // wxCint rounds to the
457e6c54 2554 const int ys = wxCint (src.y); // closest integer
7a632f10 2555
b5c91ac6
GRG
2556 if (0 <= xs && xs < GetWidth() &&
2557 0 <= ys && ys < GetHeight())
7a632f10 2558 {
b5c91ac6
GRG
2559 unsigned char *p = data[ys] + (3 * xs);
2560 *(dst++) = *(p++);
2561 *(dst++) = *(p++);
999836aa 2562 *(dst++) = *p;
b713f891 2563
6408deed 2564 if (has_alpha)
4e115ed2 2565 *(alpha_dst++) = *(alpha[ys] + (xs));
7a632f10
JS
2566 }
2567 else
2568 {
b5c91ac6
GRG
2569 *(dst++) = blank_r;
2570 *(dst++) = blank_g;
2571 *(dst++) = blank_b;
b713f891 2572
6408deed
RR
2573 if (has_alpha)
2574 *(alpha_dst++) = 255;
7a632f10
JS
2575 }
2576 }
2577 }
2578 }
2579
4aff28fc 2580 delete [] data;
b713f891 2581
6408deed
RR
2582 if (has_alpha)
2583 delete [] alpha;
4aff28fc 2584
7a632f10
JS
2585 return rotated;
2586}
c9d01afd 2587
ef8f37e0
VS
2588
2589
2590
2591
2592// A module to allow wxImage initialization/cleanup
2593// without calling these functions from app.cpp or from
2594// the user's application.
2595
2596class wxImageModule: public wxModule
2597{
2598DECLARE_DYNAMIC_CLASS(wxImageModule)
2599public:
2600 wxImageModule() {}
70cd62e9 2601 bool OnInit() { wxImage::InitStandardHandlers(); return true; };
ef8f37e0
VS
2602 void OnExit() { wxImage::CleanUpHandlers(); };
2603};
2604
2605IMPLEMENT_DYNAMIC_CLASS(wxImageModule, wxModule)
2606
2607
c96ea657 2608#endif // wxUSE_IMAGE