]> git.saurik.com Git - wxWidgets.git/blame - src/common/image.cpp
Use the current font for the DoGetBestSize calculation
[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
4013de12 894void wxImage::SetData( unsigned char *data, bool static_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;
4013de12 908 newRefData->m_static = static_data;
995612e2 909
ed58dbea 910 UnRef();
995612e2 911
ed58dbea 912 m_refData = newRefData;
01111366
RR
913}
914
4013de12 915void wxImage::SetData( unsigned char *data, int new_width, int new_height, bool static_data )
f6bcfd97
BP
916{
917 wxImageRefData *newRefData = new wxImageRefData();
918
919 if (m_refData)
920 {
921 newRefData->m_width = new_width;
922 newRefData->m_height = new_height;
923 newRefData->m_data = data;
70cd62e9 924 newRefData->m_ok = true;
f6bcfd97
BP
925 newRefData->m_maskRed = M_IMGDATA->m_maskRed;
926 newRefData->m_maskGreen = M_IMGDATA->m_maskGreen;
927 newRefData->m_maskBlue = M_IMGDATA->m_maskBlue;
928 newRefData->m_hasMask = M_IMGDATA->m_hasMask;
929 }
930 else
931 {
932 newRefData->m_width = new_width;
933 newRefData->m_height = new_height;
934 newRefData->m_data = data;
70cd62e9 935 newRefData->m_ok = true;
f6bcfd97 936 }
4013de12 937 newRefData->m_static = static_data;
f6bcfd97
BP
938
939 UnRef();
940
941 m_refData = newRefData;
942}
943
487659e0
VZ
944// ----------------------------------------------------------------------------
945// alpha channel support
946// ----------------------------------------------------------------------------
947
948void wxImage::SetAlpha(int x, int y, unsigned char alpha)
949{
950 wxCHECK_RET( Ok() && HasAlpha(), wxT("invalid image or no alpha channel") );
951
952 int w = M_IMGDATA->m_width,
953 h = M_IMGDATA->m_height;
954
955 wxCHECK_RET( x >=0 && y >= 0 && x < w && y < h, wxT("invalid image index") );
956
957 M_IMGDATA->m_alpha[y*w + x] = alpha;
958}
959
d30ee785 960unsigned char wxImage::GetAlpha(int x, int y) const
487659e0
VZ
961{
962 wxCHECK_MSG( Ok() && HasAlpha(), 0, wxT("invalid image or no alpha channel") );
963
964 int w = M_IMGDATA->m_width,
965 h = M_IMGDATA->m_height;
966
967 wxCHECK_MSG( x >=0 && y >= 0 && x < w && y < h, 0, wxT("invalid image index") );
968
969 return M_IMGDATA->m_alpha[y*w + x];
970}
971
6408deed
RR
972bool wxImage::ConvertColourToAlpha( unsigned char r, unsigned char g, unsigned char b )
973{
974 SetAlpha( NULL );
b713f891 975
6408deed
RR
976 int w = M_IMGDATA->m_width,
977 h = M_IMGDATA->m_height;
b713f891 978
6408deed
RR
979 unsigned char *alpha = GetAlpha();
980 unsigned char *data = GetData();
b713f891 981
6408deed
RR
982 int x,y;
983 for (y = 0; y < h; y++)
984 for (x = 0; x < w; x++)
985 {
986 *alpha = *data;
987 alpha++;
988 *data = r;
989 data++;
990 *data = g;
991 data++;
992 *data = b;
993 data++;
994 }
995
996 return true;
997}
998
4013de12 999void wxImage::SetAlpha( unsigned char *alpha, bool static_data )
487659e0
VZ
1000{
1001 wxCHECK_RET( Ok(), wxT("invalid image") );
1002
1003 if ( !alpha )
1004 {
edf8e8e0 1005 alpha = (unsigned char *)malloc(M_IMGDATA->m_width*M_IMGDATA->m_height);
487659e0
VZ
1006 }
1007
5402d21d 1008 free(M_IMGDATA->m_alpha);
487659e0 1009 M_IMGDATA->m_alpha = alpha;
4013de12
RD
1010 M_IMGDATA->m_static = static_data;
1011
487659e0
VZ
1012}
1013
1014unsigned char *wxImage::GetAlpha() const
1015{
1016 wxCHECK_MSG( Ok(), (unsigned char *)NULL, wxT("invalid image") );
1017
1018 return M_IMGDATA->m_alpha;
1019}
1020
828f0936
VZ
1021void wxImage::InitAlpha()
1022{
1023 wxCHECK_RET( !HasAlpha(), wxT("image already has an alpha channel") );
1024
1025 // initialize memory for alpha channel
1026 SetAlpha();
1027
1028 unsigned char *alpha = M_IMGDATA->m_alpha;
1029 const size_t lenAlpha = M_IMGDATA->m_width * M_IMGDATA->m_height;
1030
1031 static const unsigned char ALPHA_TRANSPARENT = 0;
1032 static const unsigned char ALPHA_OPAQUE = 0xff;
1033 if ( HasMask() )
1034 {
1035 // use the mask to initialize the alpha channel.
1036 const unsigned char * const alphaEnd = alpha + lenAlpha;
1037
1038 const unsigned char mr = M_IMGDATA->m_maskRed;
1039 const unsigned char mg = M_IMGDATA->m_maskGreen;
1040 const unsigned char mb = M_IMGDATA->m_maskBlue;
1041 for ( unsigned char *src = M_IMGDATA->m_data;
1042 alpha < alphaEnd;
1043 src += 3, alpha++ )
1044 {
1045 *alpha = (src[0] == mr && src[1] == mg && src[2] == mb)
1046 ? ALPHA_TRANSPARENT
1047 : ALPHA_OPAQUE;
1048 }
1049
1050 M_IMGDATA->m_hasMask = false;
1051 }
1052 else // no mask
1053 {
1054 // make the image fully opaque
1055 memset(alpha, ALPHA_OPAQUE, lenAlpha);
1056 }
1057}
1058
487659e0
VZ
1059// ----------------------------------------------------------------------------
1060// mask support
1061// ----------------------------------------------------------------------------
1062
01111366
RR
1063void wxImage::SetMaskColour( unsigned char r, unsigned char g, unsigned char b )
1064{
223d09f6 1065 wxCHECK_RET( Ok(), wxT("invalid image") );
c7abc967 1066
fd0eed64
RR
1067 M_IMGDATA->m_maskRed = r;
1068 M_IMGDATA->m_maskGreen = g;
1069 M_IMGDATA->m_maskBlue = b;
70cd62e9 1070 M_IMGDATA->m_hasMask = true;
01111366
RR
1071}
1072
b737ad10
RR
1073bool wxImage::GetOrFindMaskColour( unsigned char *r, unsigned char *g, unsigned char *b ) const
1074{
1075 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1076
1077 if (M_IMGDATA->m_hasMask)
1078 {
1079 if (r) *r = M_IMGDATA->m_maskRed;
1080 if (g) *g = M_IMGDATA->m_maskGreen;
1081 if (b) *b = M_IMGDATA->m_maskBlue;
1082 return true;
1083 }
1084 else
1085 {
1086 FindFirstUnusedColour(r, g, b);
1087 return false;
1088 }
1089}
1090
01111366
RR
1091unsigned char wxImage::GetMaskRed() const
1092{
223d09f6 1093 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
c7abc967 1094
fd0eed64 1095 return M_IMGDATA->m_maskRed;
01111366
RR
1096}
1097
1098unsigned char wxImage::GetMaskGreen() const
1099{
223d09f6 1100 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
c7abc967 1101
fd0eed64 1102 return M_IMGDATA->m_maskGreen;
01111366
RR
1103}
1104
1105unsigned char wxImage::GetMaskBlue() const
1106{
223d09f6 1107 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
c7abc967 1108
fd0eed64 1109 return M_IMGDATA->m_maskBlue;
01111366 1110}
4698648f 1111
01111366
RR
1112void wxImage::SetMask( bool mask )
1113{
223d09f6 1114 wxCHECK_RET( Ok(), wxT("invalid image") );
c7abc967 1115
fd0eed64 1116 M_IMGDATA->m_hasMask = mask;
01111366
RR
1117}
1118
1119bool wxImage::HasMask() const
1120{
70cd62e9 1121 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
c7abc967 1122
fd0eed64 1123 return M_IMGDATA->m_hasMask;
01111366
RR
1124}
1125
4698648f
VZ
1126int wxImage::GetWidth() const
1127{
223d09f6 1128 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
c7abc967 1129
4698648f 1130 return M_IMGDATA->m_width;
01111366
RR
1131}
1132
4698648f
VZ
1133int wxImage::GetHeight() const
1134{
223d09f6 1135 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
c7abc967 1136
4698648f 1137 return M_IMGDATA->m_height;
01111366
RR
1138}
1139
487659e0 1140bool wxImage::SetMaskFromImage(const wxImage& mask,
1f5b2017 1141 unsigned char mr, unsigned char mg, unsigned char mb)
52b64b0a 1142{
52b64b0a
RR
1143 // check that the images are the same size
1144 if ( (M_IMGDATA->m_height != mask.GetHeight() ) || (M_IMGDATA->m_width != mask.GetWidth () ) )
1145 {
bc88f66f 1146 wxLogError( _("Image and mask have different sizes.") );
70cd62e9 1147 return false;
52b64b0a 1148 }
487659e0 1149
52b64b0a
RR
1150 // find unused colour
1151 unsigned char r,g,b ;
1f5b2017 1152 if (!FindFirstUnusedColour(&r, &g, &b))
52b64b0a 1153 {
bc88f66f 1154 wxLogError( _("No unused colour in image being masked.") );
70cd62e9 1155 return false ;
52b64b0a 1156 }
487659e0
VZ
1157
1158 unsigned char *imgdata = GetData();
1159 unsigned char *maskdata = mask.GetData();
52b64b0a
RR
1160
1161 const int w = GetWidth();
1162 const int h = GetHeight();
1163
1164 for (int j = 0; j < h; j++)
1f5b2017 1165 {
52b64b0a
RR
1166 for (int i = 0; i < w; i++)
1167 {
1f5b2017 1168 if ((maskdata[0] == mr) && (maskdata[1] == mg) && (maskdata[2] == mb))
52b64b0a
RR
1169 {
1170 imgdata[0] = r;
1171 imgdata[1] = g;
1172 imgdata[2] = b;
1173 }
1174 imgdata += 3;
1175 maskdata += 3;
1176 }
1f5b2017 1177 }
52b64b0a 1178
1f5b2017 1179 SetMaskColour(r, g, b);
70cd62e9 1180 SetMask(true);
487659e0 1181
70cd62e9 1182 return true;
52b64b0a 1183}
7beb59f3 1184
8f2b21e4 1185bool wxImage::ConvertAlphaToMask(unsigned char threshold)
ff5ad794
VS
1186{
1187 if (!HasAlpha())
1188 return true;
1189
1190 unsigned char mr, mg, mb;
1191 if (!FindFirstUnusedColour(&mr, &mg, &mb))
1192 {
1193 wxLogError( _("No unused colour in image being masked.") );
1194 return false;
1195 }
94406a49 1196
ff5ad794
VS
1197 SetMask(true);
1198 SetMaskColour(mr, mg, mb);
94406a49 1199
ff5ad794
VS
1200 unsigned char *imgdata = GetData();
1201 unsigned char *alphadata = GetAlpha();
1202
8f2b21e4
VS
1203 int w = GetWidth();
1204 int h = GetHeight();
ff5ad794 1205
8f2b21e4 1206 for (int y = 0; y < h; y++)
ff5ad794 1207 {
8f2b21e4 1208 for (int x = 0; x < w; x++, imgdata += 3, alphadata++)
ff5ad794 1209 {
e95f0d79 1210 if (*alphadata < threshold)
ff5ad794
VS
1211 {
1212 imgdata[0] = mr;
1213 imgdata[1] = mg;
1214 imgdata[2] = mb;
1215 }
1216 }
1217 }
1218
1219 free(M_IMGDATA->m_alpha);
1220 M_IMGDATA->m_alpha = NULL;
94406a49
DS
1221
1222 return true;
ff5ad794 1223}
52b64b0a 1224
d275c7eb
VZ
1225#if wxUSE_PALETTE
1226
5e5437e0
JS
1227// Palette functions
1228
1229bool wxImage::HasPalette() const
1230{
1231 if (!Ok())
70cd62e9 1232 return false;
5e5437e0
JS
1233
1234 return M_IMGDATA->m_palette.Ok();
1235}
1236
1237const wxPalette& wxImage::GetPalette() const
1238{
1239 wxCHECK_MSG( Ok(), wxNullPalette, wxT("invalid image") );
1240
1241 return M_IMGDATA->m_palette;
1242}
1243
1244void wxImage::SetPalette(const wxPalette& palette)
1245{
1246 wxCHECK_RET( Ok(), wxT("invalid image") );
1247
1248 M_IMGDATA->m_palette = palette;
1249}
1250
d275c7eb
VZ
1251#endif // wxUSE_PALETTE
1252
5e5437e0
JS
1253// Option functions (arbitrary name/value mapping)
1254void wxImage::SetOption(const wxString& name, const wxString& value)
1255{
1256 wxCHECK_RET( Ok(), wxT("invalid image") );
1257
70cd62e9 1258 int idx = M_IMGDATA->m_optionNames.Index(name, false);
5e5437e0
JS
1259 if (idx == wxNOT_FOUND)
1260 {
1261 M_IMGDATA->m_optionNames.Add(name);
1262 M_IMGDATA->m_optionValues.Add(value);
1263 }
1264 else
1265 {
1266 M_IMGDATA->m_optionNames[idx] = name;
1267 M_IMGDATA->m_optionValues[idx] = value;
1268 }
1269}
1270
1271void wxImage::SetOption(const wxString& name, int value)
1272{
1273 wxString valStr;
1274 valStr.Printf(wxT("%d"), value);
1275 SetOption(name, valStr);
1276}
1277
1278wxString wxImage::GetOption(const wxString& name) const
1279{
1280 wxCHECK_MSG( Ok(), wxEmptyString, wxT("invalid image") );
1281
70cd62e9 1282 int idx = M_IMGDATA->m_optionNames.Index(name, false);
5e5437e0
JS
1283 if (idx == wxNOT_FOUND)
1284 return wxEmptyString;
1285 else
1286 return M_IMGDATA->m_optionValues[idx];
1287}
1288
1289int wxImage::GetOptionInt(const wxString& name) const
1290{
5e5437e0
JS
1291 return wxAtoi(GetOption(name));
1292}
1293
1294bool wxImage::HasOption(const wxString& name) const
1295{
70cd62e9 1296 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
5e5437e0 1297
70cd62e9 1298 return (M_IMGDATA->m_optionNames.Index(name, false) != wxNOT_FOUND);
5e5437e0
JS
1299}
1300
60d43ad8 1301bool wxImage::LoadFile( const wxString& filename, long type, int index )
01111366 1302{
e02afc7a 1303#if wxUSE_STREAMS
d80207c3 1304 if (wxFileExists(filename))
6c28fd33 1305 {
d80207c3
KB
1306 wxFileInputStream stream(filename);
1307 wxBufferedInputStream bstream( stream );
60d43ad8 1308 return LoadFile(bstream, type, index);
6c28fd33 1309 }
d80207c3 1310 else
6c28fd33 1311 {
d80207c3
KB
1312 wxLogError( _("Can't load image from file '%s': file does not exist."), filename.c_str() );
1313
70cd62e9 1314 return false;
6c28fd33 1315 }
9e9ee68e 1316#else // !wxUSE_STREAMS
70cd62e9 1317 return false;
9e9ee68e
VS
1318#endif // wxUSE_STREAMS
1319}
1320
60d43ad8 1321bool wxImage::LoadFile( const wxString& filename, const wxString& mimetype, int index )
9e9ee68e
VS
1322{
1323#if wxUSE_STREAMS
d80207c3 1324 if (wxFileExists(filename))
6c28fd33 1325 {
d80207c3
KB
1326 wxFileInputStream stream(filename);
1327 wxBufferedInputStream bstream( stream );
60d43ad8 1328 return LoadFile(bstream, mimetype, index);
6c28fd33 1329 }
d80207c3 1330 else
6c28fd33 1331 {
d80207c3
KB
1332 wxLogError( _("Can't load image from file '%s': file does not exist."), filename.c_str() );
1333
70cd62e9 1334 return false;
6c28fd33 1335 }
e02afc7a 1336#else // !wxUSE_STREAMS
70cd62e9 1337 return false;
e02afc7a 1338#endif // wxUSE_STREAMS
1ccbb61a
VZ
1339}
1340
45647dcf
VS
1341
1342
1343bool wxImage::SaveFile( const wxString& filename ) const
1344{
1345 wxString ext = filename.AfterLast('.').Lower();
487659e0 1346
45647dcf
VS
1347 wxImageHandler * pHandler = FindHandler(ext, -1);
1348 if (pHandler)
1349 {
1350 SaveFile(filename, pHandler->GetType());
70cd62e9 1351 return true;
45647dcf
VS
1352 }
1353
1354 wxLogError(_("Can't save image to file '%s': unknown extension."), filename.c_str());
1355
70cd62e9 1356 return false;
45647dcf
VS
1357}
1358
e0a76d8d 1359bool wxImage::SaveFile( const wxString& filename, int type ) const
1ccbb61a 1360{
e02afc7a 1361#if wxUSE_STREAMS
2a736739
VZ
1362 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1363
36aac195 1364 ((wxImage*)this)->SetOption(wxIMAGE_OPTION_FILENAME, filename);
fd94e8aa 1365
1ccbb61a 1366 wxFileOutputStream stream(filename);
9e9ee68e 1367
2b5f62a0 1368 if ( stream.IsOk() )
1b055864 1369 {
069d0f27 1370 wxBufferedOutputStream bstream( stream );
1b055864
RR
1371 return SaveFile(bstream, type);
1372 }
e02afc7a 1373#endif // wxUSE_STREAMS
3ca6a5f0 1374
70cd62e9 1375 return false;
3d05544e 1376}
01111366 1377
e0a76d8d 1378bool wxImage::SaveFile( const wxString& filename, const wxString& mimetype ) const
9e9ee68e
VS
1379{
1380#if wxUSE_STREAMS
2a736739
VZ
1381 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1382
36aac195 1383 ((wxImage*)this)->SetOption(wxIMAGE_OPTION_FILENAME, filename);
fd94e8aa 1384
9e9ee68e 1385 wxFileOutputStream stream(filename);
c7abc967 1386
2b5f62a0 1387 if ( stream.IsOk() )
1b055864 1388 {
069d0f27 1389 wxBufferedOutputStream bstream( stream );
1b055864
RR
1390 return SaveFile(bstream, mimetype);
1391 }
9e9ee68e 1392#endif // wxUSE_STREAMS
3ca6a5f0 1393
70cd62e9 1394 return false;
9e9ee68e
VS
1395}
1396
87202f78
SB
1397bool wxImage::CanRead( const wxString &name )
1398{
1399#if wxUSE_STREAMS
1400 wxFileInputStream stream(name);
1401 return CanRead(stream);
1402#else
70cd62e9 1403 return false;
87202f78
SB
1404#endif
1405}
1406
649d13e8 1407int wxImage::GetImageCount( const wxString &name, long type )
60d43ad8
VS
1408{
1409#if wxUSE_STREAMS
1410 wxFileInputStream stream(name);
2c0a4e08 1411 if (stream.Ok())
08811762 1412 return GetImageCount(stream, type);
60d43ad8 1413#endif
2c0a4e08
VZ
1414
1415 return 0;
60d43ad8
VS
1416}
1417
e02afc7a 1418#if wxUSE_STREAMS
deb2fec0 1419
87202f78
SB
1420bool wxImage::CanRead( wxInputStream &stream )
1421{
79fa2374 1422 const wxList& list = GetHandlers();
004fd0c8 1423
222ed1d6 1424 for ( wxList::compatibility_iterator node = list.GetFirst(); node; node = node->GetNext() )
004fd0c8 1425 {
60d43ad8
VS
1426 wxImageHandler *handler=(wxImageHandler*)node->GetData();
1427 if (handler->CanRead( stream ))
70cd62e9 1428 return true;
87202f78
SB
1429 }
1430
70cd62e9 1431 return false;
60d43ad8
VS
1432}
1433
649d13e8 1434int wxImage::GetImageCount( wxInputStream &stream, long type )
60d43ad8
VS
1435{
1436 wxImageHandler *handler;
1437
1438 if ( type == wxBITMAP_TYPE_ANY )
1439 {
1440 wxList &list=GetHandlers();
1441
222ed1d6 1442 for (wxList::compatibility_iterator node = list.GetFirst(); node; node = node->GetNext())
60d43ad8
VS
1443 {
1444 handler=(wxImageHandler*)node->GetData();
1445 if ( handler->CanRead(stream) )
649d13e8 1446 return handler->GetImageCount(stream);
60d43ad8
VS
1447
1448 }
1449
1450 wxLogWarning(_("No handler found for image type."));
1451 return 0;
1452 }
1453
1454 handler = FindHandler(type);
1455
1456 if ( !handler )
1457 {
1458 wxLogWarning(_("No image handler for type %d defined."), type);
70cd62e9 1459 return false;
60d43ad8
VS
1460 }
1461
1462 if ( handler->CanRead(stream) )
1463 {
649d13e8 1464 return handler->GetImageCount(stream);
60d43ad8
VS
1465 }
1466 else
1467 {
1468 wxLogError(_("Image file is not of type %d."), type);
1469 return 0;
1470 }
87202f78
SB
1471}
1472
60d43ad8 1473bool wxImage::LoadFile( wxInputStream& stream, long type, int index )
3d05544e
JS
1474{
1475 UnRef();
c7abc967 1476
fd0eed64 1477 m_refData = new wxImageRefData;
c7abc967 1478
deb2fec0
SB
1479 wxImageHandler *handler;
1480
60d43ad8 1481 if ( type == wxBITMAP_TYPE_ANY )
deb2fec0 1482 {
995612e2 1483 wxList &list=GetHandlers();
deb2fec0 1484
222ed1d6 1485 for ( wxList::compatibility_iterator node = list.GetFirst(); node; node = node->GetNext() )
995612e2
VZ
1486 {
1487 handler=(wxImageHandler*)node->GetData();
60d43ad8 1488 if ( handler->CanRead(stream) )
70cd62e9 1489 return handler->LoadFile(this, stream, true/*verbose*/, index);
7b2471a0 1490
995612e2 1491 }
deb2fec0 1492
58c837a4 1493 wxLogWarning( _("No handler found for image type.") );
70cd62e9 1494 return false;
deb2fec0
SB
1495 }
1496
1497 handler = FindHandler(type);
c7abc967 1498
2b5f62a0 1499 if (handler == 0)
fd0eed64 1500 {
58c837a4 1501 wxLogWarning( _("No image handler for type %d defined."), type );
c7abc967 1502
70cd62e9 1503 return false;
fd0eed64 1504 }
c7abc967 1505
70cd62e9 1506 return handler->LoadFile(this, stream, true/*verbose*/, index);
01111366
RR
1507}
1508
60d43ad8 1509bool wxImage::LoadFile( wxInputStream& stream, const wxString& mimetype, int index )
9e9ee68e
VS
1510{
1511 UnRef();
1512
1513 m_refData = new wxImageRefData;
1514
1515 wxImageHandler *handler = FindHandlerMime(mimetype);
1516
2b5f62a0 1517 if (handler == 0)
9e9ee68e 1518 {
58c837a4 1519 wxLogWarning( _("No image handler for type %s defined."), mimetype.GetData() );
9e9ee68e 1520
70cd62e9 1521 return false;
9e9ee68e
VS
1522 }
1523
70cd62e9 1524 return handler->LoadFile( this, stream, true/*verbose*/, index );
9e9ee68e
VS
1525}
1526
e0a76d8d 1527bool wxImage::SaveFile( wxOutputStream& stream, int type ) const
01111366 1528{
70cd62e9 1529 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
c7abc967 1530
fd0eed64 1531 wxImageHandler *handler = FindHandler(type);
2a736739 1532 if ( !handler )
fd0eed64 1533 {
58c837a4 1534 wxLogWarning( _("No image handler for type %d defined."), type );
9e9ee68e 1535
70cd62e9 1536 return false;
9e9ee68e
VS
1537 }
1538
e0a76d8d 1539 return handler->SaveFile( (wxImage*)this, stream );
9e9ee68e
VS
1540}
1541
e0a76d8d 1542bool wxImage::SaveFile( wxOutputStream& stream, const wxString& mimetype ) const
9e9ee68e 1543{
70cd62e9 1544 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
c7abc967 1545
9e9ee68e 1546 wxImageHandler *handler = FindHandlerMime(mimetype);
2a736739 1547 if ( !handler )
9e9ee68e 1548 {
58c837a4 1549 wxLogWarning( _("No image handler for type %s defined."), mimetype.GetData() );
c7abc967 1550
70cd62e9 1551 return false;
fd0eed64 1552 }
c7abc967 1553
e0a76d8d 1554 return handler->SaveFile( (wxImage*)this, stream );
01111366 1555}
e02afc7a 1556#endif // wxUSE_STREAMS
01111366
RR
1557
1558void wxImage::AddHandler( wxImageHandler *handler )
1559{
2b5f62a0
VZ
1560 // Check for an existing handler of the type being added.
1561 if (FindHandler( handler->GetType() ) == 0)
1562 {
1563 sm_handlers.Append( handler );
1564 }
1565 else
1566 {
1567 // This is not documented behaviour, merely the simplest 'fix'
1568 // for preventing duplicate additions. If someone ever has
1569 // a good reason to add and remove duplicate handlers (and they
1570 // may) we should probably refcount the duplicates.
1571 // also an issue in InsertHandler below.
1572
1573 wxLogDebug( _T("Adding duplicate image handler for '%s'"),
1574 handler->GetName().c_str() );
1575 delete handler;
1576 }
01111366
RR
1577}
1578
1579void wxImage::InsertHandler( wxImageHandler *handler )
1580{
2b5f62a0
VZ
1581 // Check for an existing handler of the type being added.
1582 if (FindHandler( handler->GetType() ) == 0)
1583 {
1584 sm_handlers.Insert( handler );
1585 }
1586 else
1587 {
1588 // see AddHandler for additional comments.
1589 wxLogDebug( _T("Inserting duplicate image handler for '%s'"),
1590 handler->GetName().c_str() );
1591 delete handler;
1592 }
01111366
RR
1593}
1594
1595bool wxImage::RemoveHandler( const wxString& name )
1596{
fd0eed64
RR
1597 wxImageHandler *handler = FindHandler(name);
1598 if (handler)
1599 {
1600 sm_handlers.DeleteObject(handler);
222ed1d6 1601 delete handler;
70cd62e9 1602 return true;
fd0eed64
RR
1603 }
1604 else
70cd62e9 1605 return false;
01111366
RR
1606}
1607
1608wxImageHandler *wxImage::FindHandler( const wxString& name )
1609{
222ed1d6 1610 wxList::compatibility_iterator node = sm_handlers.GetFirst();
fd0eed64
RR
1611 while (node)
1612 {
b1d4dd7a 1613 wxImageHandler *handler = (wxImageHandler*)node->GetData();
ce3ed50d 1614 if (handler->GetName().Cmp(name) == 0) return handler;
c7abc967 1615
b1d4dd7a 1616 node = node->GetNext();
fd0eed64 1617 }
2b5f62a0 1618 return 0;
01111366
RR
1619}
1620
1621wxImageHandler *wxImage::FindHandler( const wxString& extension, long bitmapType )
1622{
222ed1d6 1623 wxList::compatibility_iterator node = sm_handlers.GetFirst();
fd0eed64
RR
1624 while (node)
1625 {
b1d4dd7a 1626 wxImageHandler *handler = (wxImageHandler*)node->GetData();
ce3ed50d 1627 if ( (handler->GetExtension().Cmp(extension) == 0) &&
fd0eed64 1628 (bitmapType == -1 || handler->GetType() == bitmapType) )
dbda9e86 1629 return handler;
b1d4dd7a 1630 node = node->GetNext();
fd0eed64 1631 }
2b5f62a0 1632 return 0;
01111366
RR
1633}
1634
1635wxImageHandler *wxImage::FindHandler( long bitmapType )
1636{
222ed1d6 1637 wxList::compatibility_iterator node = sm_handlers.GetFirst();
fd0eed64
RR
1638 while (node)
1639 {
b1d4dd7a 1640 wxImageHandler *handler = (wxImageHandler *)node->GetData();
fd0eed64 1641 if (handler->GetType() == bitmapType) return handler;
b1d4dd7a 1642 node = node->GetNext();
fd0eed64 1643 }
2b5f62a0 1644 return 0;
fd0eed64
RR
1645}
1646
9e9ee68e
VS
1647wxImageHandler *wxImage::FindHandlerMime( const wxString& mimetype )
1648{
222ed1d6 1649 wxList::compatibility_iterator node = sm_handlers.GetFirst();
9e9ee68e
VS
1650 while (node)
1651 {
b1d4dd7a 1652 wxImageHandler *handler = (wxImageHandler *)node->GetData();
70cd62e9 1653 if (handler->GetMimeType().IsSameAs(mimetype, false)) return handler;
b1d4dd7a 1654 node = node->GetNext();
9e9ee68e 1655 }
2b5f62a0 1656 return 0;
9e9ee68e
VS
1657}
1658
fd0eed64
RR
1659void wxImage::InitStandardHandlers()
1660{
77fac225 1661#if wxUSE_STREAMS
66e23ad2 1662 AddHandler(new wxBMPHandler);
77fac225 1663#endif // wxUSE_STREAMS
01111366
RR
1664}
1665
1666void wxImage::CleanUpHandlers()
1667{
222ed1d6 1668 wxList::compatibility_iterator node = sm_handlers.GetFirst();
fd0eed64
RR
1669 while (node)
1670 {
b1d4dd7a 1671 wxImageHandler *handler = (wxImageHandler *)node->GetData();
222ed1d6 1672 wxList::compatibility_iterator next = node->GetNext();
fd0eed64 1673 delete handler;
fd0eed64
RR
1674 node = next;
1675 }
01111366 1676
222ed1d6
MB
1677 sm_handlers.Clear();
1678}
ff865c13 1679
939fadc8
JS
1680wxString wxImage::GetImageExtWildcard()
1681{
1682 wxString fmts;
1683
1684 wxList& Handlers = wxImage::GetHandlers();
222ed1d6 1685 wxList::compatibility_iterator Node = Handlers.GetFirst();
939fadc8
JS
1686 while ( Node )
1687 {
1688 wxImageHandler* Handler = (wxImageHandler*)Node->GetData();
1689 fmts += wxT("*.") + Handler->GetExtension();
1690 Node = Node->GetNext();
1691 if ( Node ) fmts += wxT(";");
1692 }
1693
1694 return wxT("(") + fmts + wxT(")|") + fmts;
1695}
1696
01111366
RR
1697//-----------------------------------------------------------------------------
1698// wxImageHandler
1699//-----------------------------------------------------------------------------
1700
63d963a1 1701IMPLEMENT_ABSTRACT_CLASS(wxImageHandler,wxObject)
01111366 1702
e02afc7a 1703#if wxUSE_STREAMS
700ec454 1704bool wxImageHandler::LoadFile( wxImage *WXUNUSED(image), wxInputStream& WXUNUSED(stream), bool WXUNUSED(verbose), int WXUNUSED(index) )
01111366 1705{
70cd62e9 1706 return false;
01111366
RR
1707}
1708
deb2fec0 1709bool wxImageHandler::SaveFile( wxImage *WXUNUSED(image), wxOutputStream& WXUNUSED(stream), bool WXUNUSED(verbose) )
01111366 1710{
70cd62e9 1711 return false;
01111366 1712}
0828c087 1713
649d13e8 1714int wxImageHandler::GetImageCount( wxInputStream& WXUNUSED(stream) )
700ec454
RR
1715{
1716 return 1;
1717}
1718
0828c087
VS
1719bool wxImageHandler::CanRead( const wxString& name )
1720{
0828c087
VS
1721 if (wxFileExists(name))
1722 {
1723 wxFileInputStream stream(name);
1724 return CanRead(stream);
1725 }
1726
39d16996 1727 wxLogError( _("Can't check image format of file '%s': file does not exist."), name.c_str() );
0828c087 1728
70cd62e9 1729 return false;
39d16996
VZ
1730}
1731
1732bool wxImageHandler::CallDoCanRead(wxInputStream& stream)
1733{
30984dea 1734 wxFileOffset posOld = stream.TellI();
39d16996
VZ
1735 if ( posOld == wxInvalidOffset )
1736 {
1737 // can't test unseekable stream
70cd62e9 1738 return false;
39d16996
VZ
1739 }
1740
1741 bool ok = DoCanRead(stream);
1742
1743 // restore the old position to be able to test other formats and so on
1744 if ( stream.SeekI(posOld) == wxInvalidOffset )
1745 {
1746 wxLogDebug(_T("Failed to rewind the stream in wxImageHandler!"));
1747
1748 // reading would fail anyhow as we're not at the right position
70cd62e9 1749 return false;
0828c087 1750 }
39d16996
VZ
1751
1752 return ok;
0828c087
VS
1753}
1754
e02afc7a 1755#endif // wxUSE_STREAMS
01111366 1756
487659e0
VZ
1757// ----------------------------------------------------------------------------
1758// image histogram stuff
1759// ----------------------------------------------------------------------------
1760
1761bool
1762wxImageHistogram::FindFirstUnusedColour(unsigned char *r,
1763 unsigned char *g,
1764 unsigned char *b,
1765 unsigned char r2,
1766 unsigned char b2,
1767 unsigned char g2) const
1768{
1769 unsigned long key = MakeKey(r2, g2, b2);
1770
1771 while ( find(key) != end() )
1772 {
1773 // color already used
1774 r2++;
1775 if ( r2 >= 255 )
1776 {
1777 r2 = 0;
1778 g2++;
1779 if ( g2 >= 255 )
1780 {
1781 g2 = 0;
1782 b2++;
1783 if ( b2 >= 255 )
1784 {
bc88f66f 1785 wxLogError(_("No unused colour in image.") );
70cd62e9 1786 return false;
487659e0
VZ
1787 }
1788 }
1789 }
1790
1791 key = MakeKey(r2, g2, b2);
1792 }
1793
1794 if ( r )
1795 *r = r2;
1796 if ( g )
1797 *g = g2;
1798 if ( b )
1799 *b = b2;
1800
70cd62e9 1801 return true;
487659e0
VZ
1802}
1803
1804bool
1805wxImage::FindFirstUnusedColour(unsigned char *r,
1806 unsigned char *g,
1807 unsigned char *b,
1808 unsigned char r2,
1809 unsigned char b2,
1810 unsigned char g2) const
1811{
1812 wxImageHistogram histogram;
1813
1814 ComputeHistogram(histogram);
1815
1816 return histogram.FindFirstUnusedColour(r, g, b, r2, g2, b2);
1817}
1818
1819
c9d01afd 1820
89d00456
GRG
1821// GRG, Dic/99
1822// Counts and returns the number of different colours. Optionally stops
cc9f7d79
GRG
1823// when it exceeds 'stopafter' different colours. This is useful, for
1824// example, to see if the image can be saved as 8-bit (256 colour or
1825// less, in this case it would be invoked as CountColours(256)). Default
1826// value for stopafter is -1 (don't care).
89d00456 1827//
e0a76d8d 1828unsigned long wxImage::CountColours( unsigned long stopafter ) const
89d00456
GRG
1829{
1830 wxHashTable h;
ad30de59 1831 wxObject dummy;
33ac7e6f 1832 unsigned char r, g, b;
eeca3a46 1833 unsigned char *p;
89d00456
GRG
1834 unsigned long size, nentries, key;
1835
1836 p = GetData();
1837 size = GetWidth() * GetHeight();
1838 nentries = 0;
1839
cc9f7d79 1840 for (unsigned long j = 0; (j < size) && (nentries <= stopafter) ; j++)
89d00456
GRG
1841 {
1842 r = *(p++);
1843 g = *(p++);
1844 b = *(p++);
487659e0 1845 key = wxImageHistogram::MakeKey(r, g, b);
89d00456 1846
ad30de59 1847 if (h.Get(key) == NULL)
89d00456 1848 {
ad30de59 1849 h.Put(key, &dummy);
89d00456
GRG
1850 nentries++;
1851 }
1852 }
1853
89d00456
GRG
1854 return nentries;
1855}
1856
1857
e0a76d8d 1858unsigned long wxImage::ComputeHistogram( wxImageHistogram &h ) const
c9d01afd 1859{
487659e0
VZ
1860 unsigned char *p = GetData();
1861 unsigned long nentries = 0;
952ae1e8
VS
1862
1863 h.clear();
c9d01afd 1864
487659e0 1865 const unsigned long size = GetWidth() * GetHeight();
c9d01afd 1866
487659e0
VZ
1867 unsigned char r, g, b;
1868 for ( unsigned long n = 0; n < size; n++ )
c9d01afd 1869 {
487659e0
VZ
1870 r = *p++;
1871 g = *p++;
1872 b = *p++;
1873
1874 wxImageHistogramEntry& entry = h[wxImageHistogram::MakeKey(r, g, b)];
c9d01afd 1875
952ae1e8
VS
1876 if ( entry.value++ == 0 )
1877 entry.index = nentries++;
c9d01afd
GRG
1878 }
1879
1880 return nentries;
1881}
1882
7a632f10
JS
1883/*
1884 * Rotation code by Carlos Moreno
1885 */
1886
b5c91ac6
GRG
1887// GRG: I've removed wxRotationPoint - we already have wxRealPoint which
1888// does exactly the same thing. And I also got rid of wxRotationPixel
1889// bacause of potential problems in architectures where alignment
1890// is an issue, so I had to rewrite parts of the code.
7a632f10 1891
7a632f10
JS
1892static const double gs_Epsilon = 1e-10;
1893
1894static inline int wxCint (double x)
1895{
1896 return (x > 0) ? (int) (x + 0.5) : (int) (x - 0.5);
1897}
1898
1899
1900// Auxiliary function to rotate a point (x,y) with respect to point p0
1901// make it inline and use a straight return to facilitate optimization
1902// also, the function receives the sine and cosine of the angle to avoid
1903// repeating the time-consuming calls to these functions -- sin/cos can
1904// be computed and stored in the calling function.
1905
b5c91ac6 1906inline wxRealPoint rotated_point (const wxRealPoint & p, double cos_angle, double sin_angle, const wxRealPoint & p0)
7a632f10 1907{
b5c91ac6
GRG
1908 return wxRealPoint (p0.x + (p.x - p0.x) * cos_angle - (p.y - p0.y) * sin_angle,
1909 p0.y + (p.y - p0.y) * cos_angle + (p.x - p0.x) * sin_angle);
7a632f10
JS
1910}
1911
b5c91ac6 1912inline wxRealPoint rotated_point (double x, double y, double cos_angle, double sin_angle, const wxRealPoint & p0)
7a632f10 1913{
b5c91ac6 1914 return rotated_point (wxRealPoint(x,y), cos_angle, sin_angle, p0);
7a632f10
JS
1915}
1916
1917wxImage wxImage::Rotate(double angle, const wxPoint & centre_of_rotation, bool interpolating, wxPoint * offset_after_rotation) const
1918{
7a632f10
JS
1919 int i;
1920 angle = -angle; // screen coordinates are a mirror image of "real" coordinates
b713f891 1921
6408deed 1922 bool has_alpha = HasAlpha();
7a632f10 1923
ad30de59 1924 // Create pointer-based array to accelerate access to wxImage's data
b5c91ac6 1925 unsigned char ** data = new unsigned char * [GetHeight()];
b5c91ac6 1926 data[0] = GetData();
b5c91ac6
GRG
1927 for (i = 1; i < GetHeight(); i++)
1928 data[i] = data[i - 1] + (3 * GetWidth());
7a632f10 1929
b713f891 1930 // Same for alpha channel
6408deed
RR
1931 unsigned char ** alpha = NULL;
1932 if (has_alpha)
1933 {
1934 alpha = new unsigned char * [GetHeight()];
1935 alpha[0] = GetAlpha();
1936 for (i = 1; i < GetHeight(); i++)
1937 alpha[i] = alpha[i - 1] + GetWidth();
1938 }
1939
b5c91ac6 1940 // precompute coefficients for rotation formula
ad30de59 1941 // (sine and cosine of the angle)
7a632f10
JS
1942 const double cos_angle = cos(angle);
1943 const double sin_angle = sin(angle);
1944
ad30de59
GRG
1945 // Create new Image to store the result
1946 // First, find rectangle that covers the rotated image; to do that,
1947 // rotate the four corners
7a632f10 1948
b5c91ac6 1949 const wxRealPoint p0(centre_of_rotation.x, centre_of_rotation.y);
7a632f10 1950
b5c91ac6
GRG
1951 wxRealPoint p1 = rotated_point (0, 0, cos_angle, sin_angle, p0);
1952 wxRealPoint p2 = rotated_point (0, GetHeight(), cos_angle, sin_angle, p0);
1953 wxRealPoint p3 = rotated_point (GetWidth(), 0, cos_angle, sin_angle, p0);
1954 wxRealPoint p4 = rotated_point (GetWidth(), GetHeight(), cos_angle, sin_angle, p0);
7a632f10 1955
57c1c6cb
BJ
1956 int x1 = (int) floor (wxMin (wxMin(p1.x, p2.x), wxMin(p3.x, p4.x)));
1957 int y1 = (int) floor (wxMin (wxMin(p1.y, p2.y), wxMin(p3.y, p4.y)));
57c1c6cb
BJ
1958 int x2 = (int) ceil (wxMax (wxMax(p1.x, p2.x), wxMax(p3.x, p4.x)));
1959 int y2 = (int) ceil (wxMax (wxMax(p1.y, p2.y), wxMax(p3.y, p4.y)));
7a632f10 1960
6408deed 1961 // Create rotated image
ff865c13 1962 wxImage rotated (x2 - x1 + 1, y2 - y1 + 1, false);
6408deed
RR
1963 // With alpha channel
1964 if (has_alpha)
1965 rotated.SetAlpha();
7a632f10
JS
1966
1967 if (offset_after_rotation != NULL)
1968 {
06b466c7 1969 *offset_after_rotation = wxPoint (x1, y1);
7a632f10
JS
1970 }
1971
b5c91ac6
GRG
1972 // GRG: The rotated (destination) image is always accessed
1973 // sequentially, so there is no need for a pointer-based
1974 // array here (and in fact it would be slower).
1975 //
1976 unsigned char * dst = rotated.GetData();
b713f891 1977
6408deed
RR
1978 unsigned char * alpha_dst = NULL;
1979 if (has_alpha)
1980 alpha_dst = rotated.GetAlpha();
7a632f10 1981
ad30de59
GRG
1982 // GRG: if the original image has a mask, use its RGB values
1983 // as the blank pixel, else, fall back to default (black).
1984 //
b5c91ac6
GRG
1985 unsigned char blank_r = 0;
1986 unsigned char blank_g = 0;
1987 unsigned char blank_b = 0;
ad30de59
GRG
1988
1989 if (HasMask())
1990 {
b5c91ac6
GRG
1991 blank_r = GetMaskRed();
1992 blank_g = GetMaskGreen();
1993 blank_b = GetMaskBlue();
1994 rotated.SetMaskColour( blank_r, blank_g, blank_b );
ad30de59
GRG
1995 }
1996
1997 // Now, for each point of the rotated image, find where it came from, by
1998 // performing an inverse rotation (a rotation of -angle) and getting the
1999 // pixel at those coordinates
2000
b5c91ac6
GRG
2001 // GRG: I've taken the (interpolating) test out of the loops, so that
2002 // it is done only once, instead of repeating it for each pixel.
7a632f10
JS
2003
2004 int x;
b5c91ac6 2005 if (interpolating)
7a632f10
JS
2006 {
2007 for (int y = 0; y < rotated.GetHeight(); y++)
2008 {
b5c91ac6 2009 for (x = 0; x < rotated.GetWidth(); x++)
7a632f10 2010 {
b5c91ac6
GRG
2011 wxRealPoint src = rotated_point (x + x1, y + y1, cos_angle, -sin_angle, p0);
2012
f2506310
JS
2013 if (-0.25 < src.x && src.x < GetWidth() - 0.75 &&
2014 -0.25 < src.y && src.y < GetHeight() - 0.75)
7a632f10 2015 {
ad30de59
GRG
2016 // interpolate using the 4 enclosing grid-points. Those
2017 // points can be obtained using floor and ceiling of the
2018 // exact coordinates of the point
f2506310 2019 // C.M. 2000-02-17: when the point is near the border, special care is required.
7a632f10 2020
f2506310
JS
2021 int x1, y1, x2, y2;
2022
2023 if (0 < src.x && src.x < GetWidth() - 1)
2024 {
2025 x1 = wxCint(floor(src.x));
2026 x2 = wxCint(ceil(src.x));
2027 }
2028 else // else means that x is near one of the borders (0 or width-1)
2029 {
2030 x1 = x2 = wxCint (src.x);
2031 }
2032
2033 if (0 < src.y && src.y < GetHeight() - 1)
2034 {
2035 y1 = wxCint(floor(src.y));
2036 y2 = wxCint(ceil(src.y));
2037 }
2038 else
2039 {
2040 y1 = y2 = wxCint (src.y);
2041 }
7a632f10 2042
ad30de59
GRG
2043 // get four points and the distances (square of the distance,
2044 // for efficiency reasons) for the interpolation formula
b5c91ac6
GRG
2045
2046 // GRG: Do not calculate the points until they are
2047 // really needed -- this way we can calculate
2048 // just one, instead of four, if d1, d2, d3
2049 // or d4 are < gs_Epsilon
7a632f10
JS
2050
2051 const double d1 = (src.x - x1) * (src.x - x1) + (src.y - y1) * (src.y - y1);
2052 const double d2 = (src.x - x2) * (src.x - x2) + (src.y - y1) * (src.y - y1);
2053 const double d3 = (src.x - x2) * (src.x - x2) + (src.y - y2) * (src.y - y2);
2054 const double d4 = (src.x - x1) * (src.x - x1) + (src.y - y2) * (src.y - y2);
2055
ad30de59
GRG
2056 // Now interpolate as a weighted average of the four surrounding
2057 // points, where the weights are the distances to each of those points
7a632f10 2058
ad30de59
GRG
2059 // If the point is exactly at one point of the grid of the source
2060 // image, then don't interpolate -- just assign the pixel
7a632f10 2061
06b466c7 2062 if (d1 < gs_Epsilon) // d1,d2,d3,d4 are positive -- no need for abs()
7a632f10 2063 {
b5c91ac6
GRG
2064 unsigned char *p = data[y1] + (3 * x1);
2065 *(dst++) = *(p++);
2066 *(dst++) = *(p++);
6408deed 2067 *(dst++) = *p;
b713f891 2068
6408deed
RR
2069 if (has_alpha)
2070 {
2071 unsigned char *p = alpha[y1] + x1;
2072 *(alpha_dst++) = *p;
2073 }
7a632f10
JS
2074 }
2075 else if (d2 < gs_Epsilon)
2076 {
b5c91ac6
GRG
2077 unsigned char *p = data[y1] + (3 * x2);
2078 *(dst++) = *(p++);
2079 *(dst++) = *(p++);
6408deed 2080 *(dst++) = *p;
b713f891 2081
6408deed
RR
2082 if (has_alpha)
2083 {
2084 unsigned char *p = alpha[y1] + x2;
2085 *(alpha_dst++) = *p;
2086 }
7a632f10
JS
2087 }
2088 else if (d3 < gs_Epsilon)
2089 {
b5c91ac6
GRG
2090 unsigned char *p = data[y2] + (3 * x2);
2091 *(dst++) = *(p++);
2092 *(dst++) = *(p++);
6408deed 2093 *(dst++) = *p;
b713f891 2094
6408deed
RR
2095 if (has_alpha)
2096 {
2097 unsigned char *p = alpha[y2] + x2;
2098 *(alpha_dst++) = *p;
2099 }
7a632f10
JS
2100 }
2101 else if (d4 < gs_Epsilon)
2102 {
b5c91ac6
GRG
2103 unsigned char *p = data[y2] + (3 * x1);
2104 *(dst++) = *(p++);
2105 *(dst++) = *(p++);
6408deed 2106 *(dst++) = *p;
b713f891 2107
6408deed
RR
2108 if (has_alpha)
2109 {
2110 unsigned char *p = alpha[y2] + x1;
2111 *(alpha_dst++) = *p;
2112 }
7a632f10
JS
2113 }
2114 else
2115 {
06b466c7 2116 // weights for the weighted average are proportional to the inverse of the distance
b5c91ac6
GRG
2117 unsigned char *v1 = data[y1] + (3 * x1);
2118 unsigned char *v2 = data[y1] + (3 * x2);
2119 unsigned char *v3 = data[y2] + (3 * x2);
2120 unsigned char *v4 = data[y2] + (3 * x1);
2121
06b466c7
VZ
2122 const double w1 = 1/d1, w2 = 1/d2, w3 = 1/d3, w4 = 1/d4;
2123
b5c91ac6
GRG
2124 // GRG: Unrolled.
2125
2126 *(dst++) = (unsigned char)
2127 ( (w1 * *(v1++) + w2 * *(v2++) +
2128 w3 * *(v3++) + w4 * *(v4++)) /
2129 (w1 + w2 + w3 + w4) );
2130 *(dst++) = (unsigned char)
2131 ( (w1 * *(v1++) + w2 * *(v2++) +
2132 w3 * *(v3++) + w4 * *(v4++)) /
2133 (w1 + w2 + w3 + w4) );
2134 *(dst++) = (unsigned char)
999836aa
VZ
2135 ( (w1 * *v1 + w2 * *v2 +
2136 w3 * *v3 + w4 * *v4) /
b5c91ac6 2137 (w1 + w2 + w3 + w4) );
b713f891 2138
6408deed
RR
2139 if (has_alpha)
2140 {
2141 unsigned char *v1 = alpha[y1] + (x1);
2142 unsigned char *v2 = alpha[y1] + (x2);
2143 unsigned char *v3 = alpha[y2] + (x2);
2144 unsigned char *v4 = alpha[y2] + (x1);
2145
2146 *(alpha_dst++) = (unsigned char)
2147 ( (w1 * *v1 + w2 * *v2 +
2148 w3 * *v3 + w4 * *v4) /
2149 (w1 + w2 + w3 + w4) );
2150 }
7a632f10
JS
2151 }
2152 }
2153 else
2154 {
b5c91ac6
GRG
2155 *(dst++) = blank_r;
2156 *(dst++) = blank_g;
2157 *(dst++) = blank_b;
b713f891 2158
6408deed
RR
2159 if (has_alpha)
2160 *(alpha_dst++) = 0;
7a632f10
JS
2161 }
2162 }
b5c91ac6
GRG
2163 }
2164 }
2165 else // not interpolating
2166 {
2167 for (int y = 0; y < rotated.GetHeight(); y++)
2168 {
2169 for (x = 0; x < rotated.GetWidth(); x++)
7a632f10 2170 {
b5c91ac6
GRG
2171 wxRealPoint src = rotated_point (x + x1, y + y1, cos_angle, -sin_angle, p0);
2172
2173 const int xs = wxCint (src.x); // wxCint rounds to the
457e6c54 2174 const int ys = wxCint (src.y); // closest integer
7a632f10 2175
b5c91ac6
GRG
2176 if (0 <= xs && xs < GetWidth() &&
2177 0 <= ys && ys < GetHeight())
7a632f10 2178 {
b5c91ac6
GRG
2179 unsigned char *p = data[ys] + (3 * xs);
2180 *(dst++) = *(p++);
2181 *(dst++) = *(p++);
999836aa 2182 *(dst++) = *p;
b713f891 2183
6408deed
RR
2184 if (has_alpha)
2185 {
2186 unsigned char *p = alpha[ys] + (xs);
2187 *(alpha_dst++) = *p;
2188 }
7a632f10
JS
2189 }
2190 else
2191 {
b5c91ac6
GRG
2192 *(dst++) = blank_r;
2193 *(dst++) = blank_g;
2194 *(dst++) = blank_b;
b713f891 2195
6408deed
RR
2196 if (has_alpha)
2197 *(alpha_dst++) = 255;
7a632f10
JS
2198 }
2199 }
2200 }
2201 }
2202
4aff28fc 2203 delete [] data;
b713f891 2204
6408deed
RR
2205 if (has_alpha)
2206 delete [] alpha;
4aff28fc 2207
7a632f10
JS
2208 return rotated;
2209}
c9d01afd 2210
ef8f37e0
VS
2211
2212
2213
2214
2215// A module to allow wxImage initialization/cleanup
2216// without calling these functions from app.cpp or from
2217// the user's application.
2218
2219class wxImageModule: public wxModule
2220{
2221DECLARE_DYNAMIC_CLASS(wxImageModule)
2222public:
2223 wxImageModule() {}
70cd62e9 2224 bool OnInit() { wxImage::InitStandardHandlers(); return true; };
ef8f37e0
VS
2225 void OnExit() { wxImage::CleanUpHandlers(); };
2226};
2227
2228IMPLEMENT_DYNAMIC_CLASS(wxImageModule, wxModule)
2229
2230
c96ea657 2231#endif // wxUSE_IMAGE