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