Added XPM ctor
[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 ((wxImage*)this)->SetOption(wxIMAGE_OPTION_FILENAME, filename);
1266
1267 wxFileOutputStream stream(filename);
1268
1269 if ( stream.IsOk() )
1270 {
1271 wxBufferedOutputStream bstream( stream );
1272 return SaveFile(bstream, type);
1273 }
1274 #endif // wxUSE_STREAMS
1275
1276 return false;
1277 }
1278
1279 bool wxImage::SaveFile( const wxString& filename, const wxString& mimetype ) const
1280 {
1281 #if wxUSE_STREAMS
1282 ((wxImage*)this)->SetOption(wxIMAGE_OPTION_FILENAME, filename);
1283
1284 wxFileOutputStream stream(filename);
1285
1286 if ( stream.IsOk() )
1287 {
1288 wxBufferedOutputStream bstream( stream );
1289 return SaveFile(bstream, mimetype);
1290 }
1291 #endif // wxUSE_STREAMS
1292
1293 return false;
1294 }
1295
1296 bool wxImage::CanRead( const wxString &name )
1297 {
1298 #if wxUSE_STREAMS
1299 wxFileInputStream stream(name);
1300 return CanRead(stream);
1301 #else
1302 return false;
1303 #endif
1304 }
1305
1306 int wxImage::GetImageCount( const wxString &name, long type )
1307 {
1308 #if wxUSE_STREAMS
1309 wxFileInputStream stream(name);
1310 if (stream.Ok())
1311 return GetImageCount(stream, type);
1312 #endif
1313
1314 return 0;
1315 }
1316
1317 #if wxUSE_STREAMS
1318
1319 bool wxImage::CanRead( wxInputStream &stream )
1320 {
1321 const wxList& list = GetHandlers();
1322
1323 for ( wxList::compatibility_iterator node = list.GetFirst(); node; node = node->GetNext() )
1324 {
1325 wxImageHandler *handler=(wxImageHandler*)node->GetData();
1326 if (handler->CanRead( stream ))
1327 return true;
1328 }
1329
1330 return false;
1331 }
1332
1333 int wxImage::GetImageCount( wxInputStream &stream, long type )
1334 {
1335 wxImageHandler *handler;
1336
1337 if ( type == wxBITMAP_TYPE_ANY )
1338 {
1339 wxList &list=GetHandlers();
1340
1341 for (wxList::compatibility_iterator node = list.GetFirst(); node; node = node->GetNext())
1342 {
1343 handler=(wxImageHandler*)node->GetData();
1344 if ( handler->CanRead(stream) )
1345 return handler->GetImageCount(stream);
1346
1347 }
1348
1349 wxLogWarning(_("No handler found for image type."));
1350 return 0;
1351 }
1352
1353 handler = FindHandler(type);
1354
1355 if ( !handler )
1356 {
1357 wxLogWarning(_("No image handler for type %d defined."), type);
1358 return false;
1359 }
1360
1361 if ( handler->CanRead(stream) )
1362 {
1363 return handler->GetImageCount(stream);
1364 }
1365 else
1366 {
1367 wxLogError(_("Image file is not of type %d."), type);
1368 return 0;
1369 }
1370 }
1371
1372 bool wxImage::LoadFile( wxInputStream& stream, long type, int index )
1373 {
1374 UnRef();
1375
1376 m_refData = new wxImageRefData;
1377
1378 wxImageHandler *handler;
1379
1380 if ( type == wxBITMAP_TYPE_ANY )
1381 {
1382 wxList &list=GetHandlers();
1383
1384 for ( wxList::compatibility_iterator node = list.GetFirst(); node; node = node->GetNext() )
1385 {
1386 handler=(wxImageHandler*)node->GetData();
1387 if ( handler->CanRead(stream) )
1388 return handler->LoadFile(this, stream, true/*verbose*/, index);
1389
1390 }
1391
1392 wxLogWarning( _("No handler found for image type.") );
1393 return false;
1394 }
1395
1396 handler = FindHandler(type);
1397
1398 if (handler == 0)
1399 {
1400 wxLogWarning( _("No image handler for type %d defined."), type );
1401
1402 return false;
1403 }
1404
1405 return handler->LoadFile(this, stream, true/*verbose*/, index);
1406 }
1407
1408 bool wxImage::LoadFile( wxInputStream& stream, const wxString& mimetype, int index )
1409 {
1410 UnRef();
1411
1412 m_refData = new wxImageRefData;
1413
1414 wxImageHandler *handler = FindHandlerMime(mimetype);
1415
1416 if (handler == 0)
1417 {
1418 wxLogWarning( _("No image handler for type %s defined."), mimetype.GetData() );
1419
1420 return false;
1421 }
1422
1423 return handler->LoadFile( this, stream, true/*verbose*/, index );
1424 }
1425
1426 bool wxImage::SaveFile( wxOutputStream& stream, int type ) const
1427 {
1428 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1429
1430 wxImageHandler *handler = FindHandler(type);
1431
1432 if (handler == 0)
1433 {
1434 wxLogWarning( _("No image handler for type %d defined."), type );
1435
1436 return false;
1437 }
1438
1439 return handler->SaveFile( (wxImage*)this, stream );
1440 }
1441
1442 bool wxImage::SaveFile( wxOutputStream& stream, const wxString& mimetype ) const
1443 {
1444 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1445
1446 wxImageHandler *handler = FindHandlerMime(mimetype);
1447
1448 if (handler == 0)
1449 {
1450 wxLogWarning( _("No image handler for type %s defined."), mimetype.GetData() );
1451
1452 return false;
1453 }
1454
1455 return handler->SaveFile( (wxImage*)this, stream );
1456 }
1457 #endif // wxUSE_STREAMS
1458
1459 void wxImage::AddHandler( wxImageHandler *handler )
1460 {
1461 // Check for an existing handler of the type being added.
1462 if (FindHandler( handler->GetType() ) == 0)
1463 {
1464 sm_handlers.Append( handler );
1465 }
1466 else
1467 {
1468 // This is not documented behaviour, merely the simplest 'fix'
1469 // for preventing duplicate additions. If someone ever has
1470 // a good reason to add and remove duplicate handlers (and they
1471 // may) we should probably refcount the duplicates.
1472 // also an issue in InsertHandler below.
1473
1474 wxLogDebug( _T("Adding duplicate image handler for '%s'"),
1475 handler->GetName().c_str() );
1476 delete handler;
1477 }
1478 }
1479
1480 void wxImage::InsertHandler( wxImageHandler *handler )
1481 {
1482 // Check for an existing handler of the type being added.
1483 if (FindHandler( handler->GetType() ) == 0)
1484 {
1485 sm_handlers.Insert( handler );
1486 }
1487 else
1488 {
1489 // see AddHandler for additional comments.
1490 wxLogDebug( _T("Inserting duplicate image handler for '%s'"),
1491 handler->GetName().c_str() );
1492 delete handler;
1493 }
1494 }
1495
1496 bool wxImage::RemoveHandler( const wxString& name )
1497 {
1498 wxImageHandler *handler = FindHandler(name);
1499 if (handler)
1500 {
1501 sm_handlers.DeleteObject(handler);
1502 delete handler;
1503 return true;
1504 }
1505 else
1506 return false;
1507 }
1508
1509 wxImageHandler *wxImage::FindHandler( const wxString& name )
1510 {
1511 wxList::compatibility_iterator node = sm_handlers.GetFirst();
1512 while (node)
1513 {
1514 wxImageHandler *handler = (wxImageHandler*)node->GetData();
1515 if (handler->GetName().Cmp(name) == 0) return handler;
1516
1517 node = node->GetNext();
1518 }
1519 return 0;
1520 }
1521
1522 wxImageHandler *wxImage::FindHandler( const wxString& extension, long bitmapType )
1523 {
1524 wxList::compatibility_iterator node = sm_handlers.GetFirst();
1525 while (node)
1526 {
1527 wxImageHandler *handler = (wxImageHandler*)node->GetData();
1528 if ( (handler->GetExtension().Cmp(extension) == 0) &&
1529 (bitmapType == -1 || handler->GetType() == bitmapType) )
1530 return handler;
1531 node = node->GetNext();
1532 }
1533 return 0;
1534 }
1535
1536 wxImageHandler *wxImage::FindHandler( long bitmapType )
1537 {
1538 wxList::compatibility_iterator node = sm_handlers.GetFirst();
1539 while (node)
1540 {
1541 wxImageHandler *handler = (wxImageHandler *)node->GetData();
1542 if (handler->GetType() == bitmapType) return handler;
1543 node = node->GetNext();
1544 }
1545 return 0;
1546 }
1547
1548 wxImageHandler *wxImage::FindHandlerMime( const wxString& mimetype )
1549 {
1550 wxList::compatibility_iterator node = sm_handlers.GetFirst();
1551 while (node)
1552 {
1553 wxImageHandler *handler = (wxImageHandler *)node->GetData();
1554 if (handler->GetMimeType().IsSameAs(mimetype, false)) return handler;
1555 node = node->GetNext();
1556 }
1557 return 0;
1558 }
1559
1560 void wxImage::InitStandardHandlers()
1561 {
1562 #if wxUSE_STREAMS
1563 AddHandler(new wxBMPHandler);
1564 #endif // wxUSE_STREAMS
1565 }
1566
1567 void wxImage::CleanUpHandlers()
1568 {
1569 wxList::compatibility_iterator node = sm_handlers.GetFirst();
1570 while (node)
1571 {
1572 wxImageHandler *handler = (wxImageHandler *)node->GetData();
1573 wxList::compatibility_iterator next = node->GetNext();
1574 delete handler;
1575 node = next;
1576 }
1577
1578 sm_handlers.Clear();
1579 }
1580
1581 wxString wxImage::GetImageExtWildcard()
1582 {
1583 wxString fmts;
1584
1585 wxList& Handlers = wxImage::GetHandlers();
1586 wxList::compatibility_iterator Node = Handlers.GetFirst();
1587 while ( Node )
1588 {
1589 wxImageHandler* Handler = (wxImageHandler*)Node->GetData();
1590 fmts += wxT("*.") + Handler->GetExtension();
1591 Node = Node->GetNext();
1592 if ( Node ) fmts += wxT(";");
1593 }
1594
1595 return wxT("(") + fmts + wxT(")|") + fmts;
1596 }
1597
1598 //-----------------------------------------------------------------------------
1599 // wxImageHandler
1600 //-----------------------------------------------------------------------------
1601
1602 IMPLEMENT_ABSTRACT_CLASS(wxImageHandler,wxObject)
1603
1604 #if wxUSE_STREAMS
1605 bool wxImageHandler::LoadFile( wxImage *WXUNUSED(image), wxInputStream& WXUNUSED(stream), bool WXUNUSED(verbose), int WXUNUSED(index) )
1606 {
1607 return false;
1608 }
1609
1610 bool wxImageHandler::SaveFile( wxImage *WXUNUSED(image), wxOutputStream& WXUNUSED(stream), bool WXUNUSED(verbose) )
1611 {
1612 return false;
1613 }
1614
1615 int wxImageHandler::GetImageCount( wxInputStream& WXUNUSED(stream) )
1616 {
1617 return 1;
1618 }
1619
1620 bool wxImageHandler::CanRead( const wxString& name )
1621 {
1622 if (wxFileExists(name))
1623 {
1624 wxFileInputStream stream(name);
1625 return CanRead(stream);
1626 }
1627
1628 wxLogError( _("Can't check image format of file '%s': file does not exist."), name.c_str() );
1629
1630 return false;
1631 }
1632
1633 bool wxImageHandler::CallDoCanRead(wxInputStream& stream)
1634 {
1635 wxFileOffset posOld = stream.TellI();
1636 if ( posOld == wxInvalidOffset )
1637 {
1638 // can't test unseekable stream
1639 return false;
1640 }
1641
1642 bool ok = DoCanRead(stream);
1643
1644 // restore the old position to be able to test other formats and so on
1645 if ( stream.SeekI(posOld) == wxInvalidOffset )
1646 {
1647 wxLogDebug(_T("Failed to rewind the stream in wxImageHandler!"));
1648
1649 // reading would fail anyhow as we're not at the right position
1650 return false;
1651 }
1652
1653 return ok;
1654 }
1655
1656 #endif // wxUSE_STREAMS
1657
1658 // ----------------------------------------------------------------------------
1659 // image histogram stuff
1660 // ----------------------------------------------------------------------------
1661
1662 bool
1663 wxImageHistogram::FindFirstUnusedColour(unsigned char *r,
1664 unsigned char *g,
1665 unsigned char *b,
1666 unsigned char r2,
1667 unsigned char b2,
1668 unsigned char g2) const
1669 {
1670 unsigned long key = MakeKey(r2, g2, b2);
1671
1672 while ( find(key) != end() )
1673 {
1674 // color already used
1675 r2++;
1676 if ( r2 >= 255 )
1677 {
1678 r2 = 0;
1679 g2++;
1680 if ( g2 >= 255 )
1681 {
1682 g2 = 0;
1683 b2++;
1684 if ( b2 >= 255 )
1685 {
1686 wxLogError(_("No unused colour in image.") );
1687 return false;
1688 }
1689 }
1690 }
1691
1692 key = MakeKey(r2, g2, b2);
1693 }
1694
1695 if ( r )
1696 *r = r2;
1697 if ( g )
1698 *g = g2;
1699 if ( b )
1700 *b = b2;
1701
1702 return true;
1703 }
1704
1705 bool
1706 wxImage::FindFirstUnusedColour(unsigned char *r,
1707 unsigned char *g,
1708 unsigned char *b,
1709 unsigned char r2,
1710 unsigned char b2,
1711 unsigned char g2) const
1712 {
1713 wxImageHistogram histogram;
1714
1715 ComputeHistogram(histogram);
1716
1717 return histogram.FindFirstUnusedColour(r, g, b, r2, g2, b2);
1718 }
1719
1720
1721
1722 // GRG, Dic/99
1723 // Counts and returns the number of different colours. Optionally stops
1724 // when it exceeds 'stopafter' different colours. This is useful, for
1725 // example, to see if the image can be saved as 8-bit (256 colour or
1726 // less, in this case it would be invoked as CountColours(256)). Default
1727 // value for stopafter is -1 (don't care).
1728 //
1729 unsigned long wxImage::CountColours( unsigned long stopafter ) const
1730 {
1731 wxHashTable h;
1732 wxObject dummy;
1733 unsigned char r, g, b;
1734 unsigned char *p;
1735 unsigned long size, nentries, key;
1736
1737 p = GetData();
1738 size = GetWidth() * GetHeight();
1739 nentries = 0;
1740
1741 for (unsigned long j = 0; (j < size) && (nentries <= stopafter) ; j++)
1742 {
1743 r = *(p++);
1744 g = *(p++);
1745 b = *(p++);
1746 key = wxImageHistogram::MakeKey(r, g, b);
1747
1748 if (h.Get(key) == NULL)
1749 {
1750 h.Put(key, &dummy);
1751 nentries++;
1752 }
1753 }
1754
1755 return nentries;
1756 }
1757
1758
1759 unsigned long wxImage::ComputeHistogram( wxImageHistogram &h ) const
1760 {
1761 unsigned char *p = GetData();
1762 unsigned long nentries = 0;
1763
1764 h.clear();
1765
1766 const unsigned long size = GetWidth() * GetHeight();
1767
1768 unsigned char r, g, b;
1769 for ( unsigned long n = 0; n < size; n++ )
1770 {
1771 r = *p++;
1772 g = *p++;
1773 b = *p++;
1774
1775 wxImageHistogramEntry& entry = h[wxImageHistogram::MakeKey(r, g, b)];
1776
1777 if ( entry.value++ == 0 )
1778 entry.index = nentries++;
1779 }
1780
1781 return nentries;
1782 }
1783
1784 /*
1785 * Rotation code by Carlos Moreno
1786 */
1787
1788 // GRG: I've removed wxRotationPoint - we already have wxRealPoint which
1789 // does exactly the same thing. And I also got rid of wxRotationPixel
1790 // bacause of potential problems in architectures where alignment
1791 // is an issue, so I had to rewrite parts of the code.
1792
1793 static const double gs_Epsilon = 1e-10;
1794
1795 static inline int wxCint (double x)
1796 {
1797 return (x > 0) ? (int) (x + 0.5) : (int) (x - 0.5);
1798 }
1799
1800
1801 // Auxiliary function to rotate a point (x,y) with respect to point p0
1802 // make it inline and use a straight return to facilitate optimization
1803 // also, the function receives the sine and cosine of the angle to avoid
1804 // repeating the time-consuming calls to these functions -- sin/cos can
1805 // be computed and stored in the calling function.
1806
1807 inline wxRealPoint rotated_point (const wxRealPoint & p, double cos_angle, double sin_angle, const wxRealPoint & p0)
1808 {
1809 return wxRealPoint (p0.x + (p.x - p0.x) * cos_angle - (p.y - p0.y) * sin_angle,
1810 p0.y + (p.y - p0.y) * cos_angle + (p.x - p0.x) * sin_angle);
1811 }
1812
1813 inline wxRealPoint rotated_point (double x, double y, double cos_angle, double sin_angle, const wxRealPoint & p0)
1814 {
1815 return rotated_point (wxRealPoint(x,y), cos_angle, sin_angle, p0);
1816 }
1817
1818 wxImage wxImage::Rotate(double angle, const wxPoint & centre_of_rotation, bool interpolating, wxPoint * offset_after_rotation) const
1819 {
1820 int i;
1821 angle = -angle; // screen coordinates are a mirror image of "real" coordinates
1822
1823 bool has_alpha = HasAlpha();
1824
1825 // Create pointer-based array to accelerate access to wxImage's data
1826 unsigned char ** data = new unsigned char * [GetHeight()];
1827 data[0] = GetData();
1828 for (i = 1; i < GetHeight(); i++)
1829 data[i] = data[i - 1] + (3 * GetWidth());
1830
1831 // Same for alpha channel
1832 unsigned char ** alpha = NULL;
1833 if (has_alpha)
1834 {
1835 alpha = new unsigned char * [GetHeight()];
1836 alpha[0] = GetAlpha();
1837 for (i = 1; i < GetHeight(); i++)
1838 alpha[i] = alpha[i - 1] + GetWidth();
1839 }
1840
1841 // precompute coefficients for rotation formula
1842 // (sine and cosine of the angle)
1843 const double cos_angle = cos(angle);
1844 const double sin_angle = sin(angle);
1845
1846 // Create new Image to store the result
1847 // First, find rectangle that covers the rotated image; to do that,
1848 // rotate the four corners
1849
1850 const wxRealPoint p0(centre_of_rotation.x, centre_of_rotation.y);
1851
1852 wxRealPoint p1 = rotated_point (0, 0, cos_angle, sin_angle, p0);
1853 wxRealPoint p2 = rotated_point (0, GetHeight(), cos_angle, sin_angle, p0);
1854 wxRealPoint p3 = rotated_point (GetWidth(), 0, cos_angle, sin_angle, p0);
1855 wxRealPoint p4 = rotated_point (GetWidth(), GetHeight(), cos_angle, sin_angle, p0);
1856
1857 int x1 = (int) floor (wxMin (wxMin(p1.x, p2.x), wxMin(p3.x, p4.x)));
1858 int y1 = (int) floor (wxMin (wxMin(p1.y, p2.y), wxMin(p3.y, p4.y)));
1859 int x2 = (int) ceil (wxMax (wxMax(p1.x, p2.x), wxMax(p3.x, p4.x)));
1860 int y2 = (int) ceil (wxMax (wxMax(p1.y, p2.y), wxMax(p3.y, p4.y)));
1861
1862 // Create rotated image
1863 wxImage rotated (x2 - x1 + 1, y2 - y1 + 1, false);
1864 // With alpha channel
1865 if (has_alpha)
1866 rotated.SetAlpha();
1867
1868 if (offset_after_rotation != NULL)
1869 {
1870 *offset_after_rotation = wxPoint (x1, y1);
1871 }
1872
1873 // GRG: The rotated (destination) image is always accessed
1874 // sequentially, so there is no need for a pointer-based
1875 // array here (and in fact it would be slower).
1876 //
1877 unsigned char * dst = rotated.GetData();
1878
1879 unsigned char * alpha_dst = NULL;
1880 if (has_alpha)
1881 alpha_dst = rotated.GetAlpha();
1882
1883 // GRG: if the original image has a mask, use its RGB values
1884 // as the blank pixel, else, fall back to default (black).
1885 //
1886 unsigned char blank_r = 0;
1887 unsigned char blank_g = 0;
1888 unsigned char blank_b = 0;
1889
1890 if (HasMask())
1891 {
1892 blank_r = GetMaskRed();
1893 blank_g = GetMaskGreen();
1894 blank_b = GetMaskBlue();
1895 rotated.SetMaskColour( blank_r, blank_g, blank_b );
1896 }
1897
1898 // Now, for each point of the rotated image, find where it came from, by
1899 // performing an inverse rotation (a rotation of -angle) and getting the
1900 // pixel at those coordinates
1901
1902 // GRG: I've taken the (interpolating) test out of the loops, so that
1903 // it is done only once, instead of repeating it for each pixel.
1904
1905 int x;
1906 if (interpolating)
1907 {
1908 for (int y = 0; y < rotated.GetHeight(); y++)
1909 {
1910 for (x = 0; x < rotated.GetWidth(); x++)
1911 {
1912 wxRealPoint src = rotated_point (x + x1, y + y1, cos_angle, -sin_angle, p0);
1913
1914 if (-0.25 < src.x && src.x < GetWidth() - 0.75 &&
1915 -0.25 < src.y && src.y < GetHeight() - 0.75)
1916 {
1917 // interpolate using the 4 enclosing grid-points. Those
1918 // points can be obtained using floor and ceiling of the
1919 // exact coordinates of the point
1920 // C.M. 2000-02-17: when the point is near the border, special care is required.
1921
1922 int x1, y1, x2, y2;
1923
1924 if (0 < src.x && src.x < GetWidth() - 1)
1925 {
1926 x1 = wxCint(floor(src.x));
1927 x2 = wxCint(ceil(src.x));
1928 }
1929 else // else means that x is near one of the borders (0 or width-1)
1930 {
1931 x1 = x2 = wxCint (src.x);
1932 }
1933
1934 if (0 < src.y && src.y < GetHeight() - 1)
1935 {
1936 y1 = wxCint(floor(src.y));
1937 y2 = wxCint(ceil(src.y));
1938 }
1939 else
1940 {
1941 y1 = y2 = wxCint (src.y);
1942 }
1943
1944 // get four points and the distances (square of the distance,
1945 // for efficiency reasons) for the interpolation formula
1946
1947 // GRG: Do not calculate the points until they are
1948 // really needed -- this way we can calculate
1949 // just one, instead of four, if d1, d2, d3
1950 // or d4 are < gs_Epsilon
1951
1952 const double d1 = (src.x - x1) * (src.x - x1) + (src.y - y1) * (src.y - y1);
1953 const double d2 = (src.x - x2) * (src.x - x2) + (src.y - y1) * (src.y - y1);
1954 const double d3 = (src.x - x2) * (src.x - x2) + (src.y - y2) * (src.y - y2);
1955 const double d4 = (src.x - x1) * (src.x - x1) + (src.y - y2) * (src.y - y2);
1956
1957 // Now interpolate as a weighted average of the four surrounding
1958 // points, where the weights are the distances to each of those points
1959
1960 // If the point is exactly at one point of the grid of the source
1961 // image, then don't interpolate -- just assign the pixel
1962
1963 if (d1 < gs_Epsilon) // d1,d2,d3,d4 are positive -- no need for abs()
1964 {
1965 unsigned char *p = data[y1] + (3 * x1);
1966 *(dst++) = *(p++);
1967 *(dst++) = *(p++);
1968 *(dst++) = *p;
1969
1970 if (has_alpha)
1971 {
1972 unsigned char *p = alpha[y1] + x1;
1973 *(alpha_dst++) = *p;
1974 }
1975 }
1976 else if (d2 < gs_Epsilon)
1977 {
1978 unsigned char *p = data[y1] + (3 * x2);
1979 *(dst++) = *(p++);
1980 *(dst++) = *(p++);
1981 *(dst++) = *p;
1982
1983 if (has_alpha)
1984 {
1985 unsigned char *p = alpha[y1] + x2;
1986 *(alpha_dst++) = *p;
1987 }
1988 }
1989 else if (d3 < gs_Epsilon)
1990 {
1991 unsigned char *p = data[y2] + (3 * x2);
1992 *(dst++) = *(p++);
1993 *(dst++) = *(p++);
1994 *(dst++) = *p;
1995
1996 if (has_alpha)
1997 {
1998 unsigned char *p = alpha[y2] + x2;
1999 *(alpha_dst++) = *p;
2000 }
2001 }
2002 else if (d4 < gs_Epsilon)
2003 {
2004 unsigned char *p = data[y2] + (3 * x1);
2005 *(dst++) = *(p++);
2006 *(dst++) = *(p++);
2007 *(dst++) = *p;
2008
2009 if (has_alpha)
2010 {
2011 unsigned char *p = alpha[y2] + x1;
2012 *(alpha_dst++) = *p;
2013 }
2014 }
2015 else
2016 {
2017 // weights for the weighted average are proportional to the inverse of the distance
2018 unsigned char *v1 = data[y1] + (3 * x1);
2019 unsigned char *v2 = data[y1] + (3 * x2);
2020 unsigned char *v3 = data[y2] + (3 * x2);
2021 unsigned char *v4 = data[y2] + (3 * x1);
2022
2023 const double w1 = 1/d1, w2 = 1/d2, w3 = 1/d3, w4 = 1/d4;
2024
2025 // GRG: Unrolled.
2026
2027 *(dst++) = (unsigned char)
2028 ( (w1 * *(v1++) + w2 * *(v2++) +
2029 w3 * *(v3++) + w4 * *(v4++)) /
2030 (w1 + w2 + w3 + w4) );
2031 *(dst++) = (unsigned char)
2032 ( (w1 * *(v1++) + w2 * *(v2++) +
2033 w3 * *(v3++) + w4 * *(v4++)) /
2034 (w1 + w2 + w3 + w4) );
2035 *(dst++) = (unsigned char)
2036 ( (w1 * *v1 + w2 * *v2 +
2037 w3 * *v3 + w4 * *v4) /
2038 (w1 + w2 + w3 + w4) );
2039
2040 if (has_alpha)
2041 {
2042 unsigned char *v1 = alpha[y1] + (x1);
2043 unsigned char *v2 = alpha[y1] + (x2);
2044 unsigned char *v3 = alpha[y2] + (x2);
2045 unsigned char *v4 = alpha[y2] + (x1);
2046
2047 *(alpha_dst++) = (unsigned char)
2048 ( (w1 * *v1 + w2 * *v2 +
2049 w3 * *v3 + w4 * *v4) /
2050 (w1 + w2 + w3 + w4) );
2051 }
2052 }
2053 }
2054 else
2055 {
2056 *(dst++) = blank_r;
2057 *(dst++) = blank_g;
2058 *(dst++) = blank_b;
2059
2060 if (has_alpha)
2061 *(alpha_dst++) = 0;
2062 }
2063 }
2064 }
2065 }
2066 else // not interpolating
2067 {
2068 for (int y = 0; y < rotated.GetHeight(); y++)
2069 {
2070 for (x = 0; x < rotated.GetWidth(); x++)
2071 {
2072 wxRealPoint src = rotated_point (x + x1, y + y1, cos_angle, -sin_angle, p0);
2073
2074 const int xs = wxCint (src.x); // wxCint rounds to the
2075 const int ys = wxCint (src.y); // closest integer
2076
2077 if (0 <= xs && xs < GetWidth() &&
2078 0 <= ys && ys < GetHeight())
2079 {
2080 unsigned char *p = data[ys] + (3 * xs);
2081 *(dst++) = *(p++);
2082 *(dst++) = *(p++);
2083 *(dst++) = *p;
2084
2085 if (has_alpha)
2086 {
2087 unsigned char *p = alpha[ys] + (xs);
2088 *(alpha_dst++) = *p;
2089 }
2090 }
2091 else
2092 {
2093 *(dst++) = blank_r;
2094 *(dst++) = blank_g;
2095 *(dst++) = blank_b;
2096
2097 if (has_alpha)
2098 *(alpha_dst++) = 255;
2099 }
2100 }
2101 }
2102 }
2103
2104 delete [] data;
2105
2106 if (has_alpha)
2107 delete [] alpha;
2108
2109 return rotated;
2110 }
2111
2112
2113
2114
2115
2116 // A module to allow wxImage initialization/cleanup
2117 // without calling these functions from app.cpp or from
2118 // the user's application.
2119
2120 class wxImageModule: public wxModule
2121 {
2122 DECLARE_DYNAMIC_CLASS(wxImageModule)
2123 public:
2124 wxImageModule() {}
2125 bool OnInit() { wxImage::InitStandardHandlers(); return true; };
2126 void OnExit() { wxImage::CleanUpHandlers(); };
2127 };
2128
2129 IMPLEMENT_DYNAMIC_CLASS(wxImageModule, wxModule)
2130
2131
2132 #endif // wxUSE_IMAGE