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