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