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