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