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