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