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