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