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