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