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