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