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