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