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