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