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