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