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