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