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