1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/image.cpp
4 // Author: Robert Roebling
6 // Copyright: (c) Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
22 #include "wx/bitmap.h"
26 #include "wx/filefn.h"
27 #include "wx/wfstream.h"
29 #include "wx/module.h"
35 #include "wx/xpmdecod.h"
41 //-----------------------------------------------------------------------------
43 //-----------------------------------------------------------------------------
45 class wxImageRefData
: public wxObjectRefData
49 virtual ~wxImageRefData();
53 unsigned char *m_data
;
56 unsigned char m_maskRed
,m_maskGreen
,m_maskBlue
;
58 // alpha channel data, may be NULL for the formats without alpha support
59 unsigned char *m_alpha
;
63 // if true, m_data is pointer to static data and shouldn't be freed
66 // same as m_static but for m_alpha
71 #endif // wxUSE_PALETTE
73 wxArrayString m_optionNames
;
74 wxArrayString m_optionValues
;
76 DECLARE_NO_COPY_CLASS(wxImageRefData
)
79 wxImageRefData::wxImageRefData()
84 m_alpha
= (unsigned char *) NULL
;
93 m_staticAlpha
= false;
96 wxImageRefData::~wxImageRefData()
100 if ( !m_staticAlpha
)
104 wxList
wxImage::sm_handlers
;
108 //-----------------------------------------------------------------------------
110 #define M_IMGDATA ((wxImageRefData *)m_refData)
112 IMPLEMENT_DYNAMIC_CLASS(wxImage
, wxObject
)
114 wxImage::wxImage( int width
, int height
, bool clear
)
116 Create( width
, height
, clear
);
119 wxImage::wxImage( int width
, int height
, unsigned char* data
, bool static_data
)
121 Create( width
, height
, data
, static_data
);
124 wxImage::wxImage( int width
, int height
, unsigned char* data
, unsigned char* alpha
, bool static_data
)
126 Create( width
, height
, data
, alpha
, static_data
);
129 wxImage::wxImage( const wxString
& name
, long type
, int index
)
131 LoadFile( name
, type
, index
);
134 wxImage::wxImage( const wxString
& name
, const wxString
& mimetype
, int index
)
136 LoadFile( name
, mimetype
, index
);
140 wxImage::wxImage( wxInputStream
& stream
, long type
, int index
)
142 LoadFile( stream
, type
, index
);
145 wxImage::wxImage( wxInputStream
& stream
, const wxString
& mimetype
, int index
)
147 LoadFile( stream
, mimetype
, index
);
149 #endif // wxUSE_STREAMS
151 wxImage::wxImage( const char** xpmData
)
156 wxImage::wxImage( char** xpmData
)
158 Create((const char**) xpmData
);
161 bool wxImage::Create( const char** xpmData
)
166 wxXPMDecoder decoder
;
167 (*this) = decoder
.ReadData(xpmData
);
174 bool wxImage::Create( int width
, int height
, bool clear
)
178 m_refData
= new wxImageRefData();
180 M_IMGDATA
->m_data
= (unsigned char *) malloc( width
*height
*3 );
181 if (!M_IMGDATA
->m_data
)
188 memset(M_IMGDATA
->m_data
, 0, width
*height
*3);
190 M_IMGDATA
->m_width
= width
;
191 M_IMGDATA
->m_height
= height
;
192 M_IMGDATA
->m_ok
= true;
197 bool wxImage::Create( int width
, int height
, unsigned char* data
, bool static_data
)
201 wxCHECK_MSG( data
, false, _T("NULL data in wxImage::Create") );
203 m_refData
= new wxImageRefData();
205 M_IMGDATA
->m_data
= data
;
206 M_IMGDATA
->m_width
= width
;
207 M_IMGDATA
->m_height
= height
;
208 M_IMGDATA
->m_ok
= true;
209 M_IMGDATA
->m_static
= static_data
;
214 bool wxImage::Create( int width
, int height
, unsigned char* data
, unsigned char* alpha
, bool static_data
)
218 wxCHECK_MSG( data
, false, _T("NULL data in wxImage::Create") );
220 m_refData
= new wxImageRefData();
222 M_IMGDATA
->m_data
= data
;
223 M_IMGDATA
->m_alpha
= alpha
;
224 M_IMGDATA
->m_width
= width
;
225 M_IMGDATA
->m_height
= height
;
226 M_IMGDATA
->m_ok
= true;
227 M_IMGDATA
->m_static
= static_data
;
232 void wxImage::Destroy()
237 wxImage
wxImage::Copy() const
241 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
243 image
.Create( M_IMGDATA
->m_width
, M_IMGDATA
->m_height
, false );
245 unsigned char *data
= image
.GetData();
247 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
249 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
250 image
.SetMask( M_IMGDATA
->m_hasMask
);
252 memcpy( data
, GetData(), M_IMGDATA
->m_width
*M_IMGDATA
->m_height
*3 );
254 wxImageRefData
*imgData
= (wxImageRefData
*)image
.m_refData
;
256 // also copy the alpha channel
260 unsigned char* alpha
= image
.GetAlpha();
261 memcpy( alpha
, GetAlpha(), M_IMGDATA
->m_width
*M_IMGDATA
->m_height
);
264 // also copy the image options
265 imgData
->m_optionNames
= M_IMGDATA
->m_optionNames
;
266 imgData
->m_optionValues
= M_IMGDATA
->m_optionValues
;
271 wxImage
wxImage::ShrinkBy( int xFactor
, int yFactor
) const
273 if( xFactor
== 1 && yFactor
== 1 )
278 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
280 // can't scale to/from 0 size
281 wxCHECK_MSG( (xFactor
> 0) && (yFactor
> 0), image
,
282 wxT("invalid new image size") );
284 long old_height
= M_IMGDATA
->m_height
,
285 old_width
= M_IMGDATA
->m_width
;
287 wxCHECK_MSG( (old_height
> 0) && (old_width
> 0), image
,
288 wxT("invalid old image size") );
290 long width
= old_width
/ xFactor
;
291 long height
= old_height
/ yFactor
;
293 image
.Create( width
, height
, false );
295 char unsigned *data
= image
.GetData();
297 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
299 bool hasMask
= false ;
300 unsigned char maskRed
= 0;
301 unsigned char maskGreen
= 0;
302 unsigned char maskBlue
=0 ;
304 unsigned char *source_data
= M_IMGDATA
->m_data
;
305 unsigned char *target_data
= data
;
306 unsigned char *source_alpha
= 0 ;
307 unsigned char *target_alpha
= 0 ;
308 if (M_IMGDATA
->m_hasMask
)
311 maskRed
= M_IMGDATA
->m_maskRed
;
312 maskGreen
= M_IMGDATA
->m_maskGreen
;
313 maskBlue
=M_IMGDATA
->m_maskBlue
;
315 image
.SetMaskColour( M_IMGDATA
->m_maskRed
,
316 M_IMGDATA
->m_maskGreen
,
317 M_IMGDATA
->m_maskBlue
);
321 source_alpha
= M_IMGDATA
->m_alpha
;
325 target_alpha
= image
.GetAlpha() ;
329 for (long y
= 0; y
< height
; y
++)
331 for (long x
= 0; x
< width
; x
++)
333 unsigned long avgRed
= 0 ;
334 unsigned long avgGreen
= 0;
335 unsigned long avgBlue
= 0;
336 unsigned long avgAlpha
= 0 ;
337 unsigned long counter
= 0 ;
339 for ( int y1
= 0 ; y1
< yFactor
; ++y1
)
341 long y_offset
= (y
* yFactor
+ y1
) * old_width
;
342 for ( int x1
= 0 ; x1
< xFactor
; ++x1
)
344 unsigned char *pixel
= source_data
+ 3 * ( y_offset
+ x
* xFactor
+ x1
) ;
345 unsigned char red
= pixel
[0] ;
346 unsigned char green
= pixel
[1] ;
347 unsigned char blue
= pixel
[2] ;
348 unsigned char alpha
= 255 ;
350 alpha
= *(source_alpha
+ y_offset
+ x
* xFactor
+ x1
) ;
351 if ( !hasMask
|| red
!= maskRed
|| green
!= maskGreen
|| blue
!= maskBlue
)
366 *(target_data
++) = M_IMGDATA
->m_maskRed
;
367 *(target_data
++) = M_IMGDATA
->m_maskGreen
;
368 *(target_data
++) = M_IMGDATA
->m_maskBlue
;
373 *(target_alpha
++) = (unsigned char)(avgAlpha
/ counter
) ;
374 *(target_data
++) = (unsigned char)(avgRed
/ counter
);
375 *(target_data
++) = (unsigned char)(avgGreen
/ counter
);
376 *(target_data
++) = (unsigned char)(avgBlue
/ counter
);
381 // In case this is a cursor, make sure the hotspot is scaled accordingly:
382 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
) )
383 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
,
384 (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X
))/xFactor
);
385 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
) )
386 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
,
387 (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y
))/yFactor
);
392 wxImage
wxImage::Scale( int width
, int height
) const
396 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
398 // can't scale to/from 0 size
399 wxCHECK_MSG( (width
> 0) && (height
> 0), image
,
400 wxT("invalid new image size") );
402 long old_height
= M_IMGDATA
->m_height
,
403 old_width
= M_IMGDATA
->m_width
;
404 wxCHECK_MSG( (old_height
> 0) && (old_width
> 0), image
,
405 wxT("invalid old image size") );
407 if ( old_width
% width
== 0 && old_width
>= width
&&
408 old_height
% height
== 0 && old_height
>= height
)
410 return ShrinkBy( old_width
/ width
, old_height
/ height
) ;
412 image
.Create( width
, height
, false );
414 unsigned char *data
= image
.GetData();
416 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
418 unsigned char *source_data
= M_IMGDATA
->m_data
;
419 unsigned char *target_data
= data
;
420 unsigned char *source_alpha
= 0 ;
421 unsigned char *target_alpha
= 0 ;
423 if (M_IMGDATA
->m_hasMask
)
425 image
.SetMaskColour( M_IMGDATA
->m_maskRed
,
426 M_IMGDATA
->m_maskGreen
,
427 M_IMGDATA
->m_maskBlue
);
431 source_alpha
= M_IMGDATA
->m_alpha
;
435 target_alpha
= image
.GetAlpha() ;
439 long x_delta
= (old_width
<<16) / width
;
440 long y_delta
= (old_height
<<16) / height
;
442 unsigned char* dest_pixel
= target_data
;
445 for ( long j
= 0; j
< height
; j
++ )
447 unsigned char* src_line
= &source_data
[(y
>>16)*old_width
*3];
448 unsigned char* src_alpha_line
= source_alpha
? &source_alpha
[(y
>>16)*old_width
] : 0 ;
451 for ( long i
= 0; i
< width
; i
++ )
453 unsigned char* src_pixel
= &src_line
[(x
>>16)*3];
454 unsigned char* src_alpha_pixel
= source_alpha
? &src_alpha_line
[(x
>>16)] : 0 ;
455 dest_pixel
[0] = src_pixel
[0];
456 dest_pixel
[1] = src_pixel
[1];
457 dest_pixel
[2] = src_pixel
[2];
460 *(target_alpha
++) = *src_alpha_pixel
;
467 // In case this is a cursor, make sure the hotspot is scaled accordingly:
468 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
) )
469 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
,
470 (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X
)*width
)/old_width
);
471 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
) )
472 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
,
473 (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y
)*height
)/old_height
);
478 wxImage
wxImage::Rotate90( bool clockwise
) const
482 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
484 image
.Create( M_IMGDATA
->m_height
, M_IMGDATA
->m_width
, false );
486 unsigned char *data
= image
.GetData();
488 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
490 unsigned char *source_data
= M_IMGDATA
->m_data
;
491 unsigned char *target_data
;
492 unsigned char *alpha_data
= 0 ;
493 unsigned char *source_alpha
= 0 ;
494 unsigned char *target_alpha
= 0 ;
496 if (M_IMGDATA
->m_hasMask
)
498 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
502 source_alpha
= M_IMGDATA
->m_alpha
;
506 alpha_data
= image
.GetAlpha() ;
510 long height
= M_IMGDATA
->m_height
;
511 long width
= M_IMGDATA
->m_width
;
513 for (long j
= 0; j
< height
; j
++)
515 for (long i
= 0; i
< width
; i
++)
519 target_data
= data
+ (((i
+1)*height
) - j
- 1)*3;
521 target_alpha
= alpha_data
+ (((i
+1)*height
) - j
- 1);
525 target_data
= data
+ ((height
*(width
-1)) + j
- (i
*height
))*3;
527 target_alpha
= alpha_data
+ ((height
*(width
-1)) + j
- (i
*height
));
529 memcpy( target_data
, source_data
, 3 );
534 memcpy( target_alpha
, source_alpha
, 1 );
543 wxImage
wxImage::Mirror( bool horizontally
) const
547 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
549 image
.Create( M_IMGDATA
->m_width
, M_IMGDATA
->m_height
, false );
551 unsigned char *data
= image
.GetData();
553 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
555 if (M_IMGDATA
->m_hasMask
)
556 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
558 long height
= M_IMGDATA
->m_height
;
559 long width
= M_IMGDATA
->m_width
;
561 unsigned char *source_data
= M_IMGDATA
->m_data
;
562 unsigned char *target_data
;
566 for (long j
= 0; j
< height
; j
++)
569 target_data
= data
-3;
570 for (long i
= 0; i
< width
; i
++)
572 memcpy( target_data
, source_data
, 3 );
580 for (long i
= 0; i
< height
; i
++)
582 target_data
= data
+ 3*width
*(height
-1-i
);
583 memcpy( target_data
, source_data
, (size_t)3*width
);
584 source_data
+= 3*width
;
591 wxImage
wxImage::GetSubImage( const wxRect
&rect
) const
595 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
597 wxCHECK_MSG( (rect
.GetLeft()>=0) && (rect
.GetTop()>=0) && (rect
.GetRight()<=GetWidth()) && (rect
.GetBottom()<=GetHeight()),
598 image
, wxT("invalid subimage size") );
600 int subwidth
=rect
.GetWidth();
601 const int subheight
=rect
.GetHeight();
603 image
.Create( subwidth
, subheight
, false );
605 unsigned char *subdata
= image
.GetData(), *data
=GetData();
607 wxCHECK_MSG( subdata
, image
, wxT("unable to create image") );
609 if (M_IMGDATA
->m_hasMask
)
610 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
612 const int subleft
=3*rect
.GetLeft();
613 const int width
=3*GetWidth();
616 data
+=rect
.GetTop()*width
+subleft
;
618 for (long j
= 0; j
< subheight
; ++j
)
620 memcpy( subdata
, data
, subwidth
);
628 wxImage
wxImage::Size( const wxSize
& size
, const wxPoint
& pos
,
629 int r_
, int g_
, int b_
) const
633 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
634 wxCHECK_MSG( (size
.GetWidth() > 0) && (size
.GetHeight() > 0), image
, wxT("invalid size") );
636 int width
= GetWidth(), height
= GetHeight();
637 image
.Create(size
.GetWidth(), size
.GetHeight(), false);
639 unsigned char r
= (unsigned char)r_
;
640 unsigned char g
= (unsigned char)g_
;
641 unsigned char b
= (unsigned char)b_
;
642 if ((r_
== -1) && (g_
== -1) && (b_
== -1))
644 GetOrFindMaskColour( &r
, &g
, &b
);
645 image
.SetMaskColour(r
, g
, b
);
648 image
.SetRGB(wxRect(), r
, g
, b
);
650 wxRect
subRect(pos
.x
, pos
.y
, width
, height
);
651 wxRect
finalRect(0, 0, size
.GetWidth(), size
.GetHeight());
653 subRect
.Intersect(finalRect
);
655 if (!subRect
.IsEmpty())
657 if ((subRect
.GetWidth() == width
) && (subRect
.GetHeight() == height
))
658 image
.Paste(*this, pos
.x
, pos
.y
);
660 image
.Paste(GetSubImage(subRect
), pos
.x
, pos
.y
);
666 void wxImage::Paste( const wxImage
&image
, int x
, int y
)
668 wxCHECK_RET( Ok(), wxT("invalid image") );
669 wxCHECK_RET( image
.Ok(), wxT("invalid image") );
673 int width
= image
.GetWidth();
674 int height
= image
.GetHeight();
687 if ((x
+xx
)+width
> M_IMGDATA
->m_width
)
688 width
= M_IMGDATA
->m_width
- (x
+xx
);
689 if ((y
+yy
)+height
> M_IMGDATA
->m_height
)
690 height
= M_IMGDATA
->m_height
- (y
+yy
);
692 if (width
< 1) return;
693 if (height
< 1) return;
695 if ((!HasMask() && !image
.HasMask()) ||
696 (HasMask() && !image
.HasMask()) ||
697 ((HasMask() && image
.HasMask() &&
698 (GetMaskRed()==image
.GetMaskRed()) &&
699 (GetMaskGreen()==image
.GetMaskGreen()) &&
700 (GetMaskBlue()==image
.GetMaskBlue()))))
703 unsigned char* source_data
= image
.GetData() + xx
*3 + yy
*3*image
.GetWidth();
704 int source_step
= image
.GetWidth()*3;
706 unsigned char* target_data
= GetData() + (x
+xx
)*3 + (y
+yy
)*3*M_IMGDATA
->m_width
;
707 int target_step
= M_IMGDATA
->m_width
*3;
708 for (int j
= 0; j
< height
; j
++)
710 memcpy( target_data
, source_data
, width
);
711 source_data
+= source_step
;
712 target_data
+= target_step
;
717 if (!HasMask() && image
.HasMask())
719 unsigned char r
= image
.GetMaskRed();
720 unsigned char g
= image
.GetMaskGreen();
721 unsigned char b
= image
.GetMaskBlue();
724 unsigned char* source_data
= image
.GetData() + xx
*3 + yy
*3*image
.GetWidth();
725 int source_step
= image
.GetWidth()*3;
727 unsigned char* target_data
= GetData() + (x
+xx
)*3 + (y
+yy
)*3*M_IMGDATA
->m_width
;
728 int target_step
= M_IMGDATA
->m_width
*3;
730 for (int j
= 0; j
< height
; j
++)
732 for (int i
= 0; i
< width
; i
+=3)
734 if ((source_data
[i
] != r
) &&
735 (source_data
[i
+1] != g
) &&
736 (source_data
[i
+2] != b
))
738 memcpy( target_data
+i
, source_data
+i
, 3 );
741 source_data
+= source_step
;
742 target_data
+= target_step
;
747 void wxImage::Replace( unsigned char r1
, unsigned char g1
, unsigned char b1
,
748 unsigned char r2
, unsigned char g2
, unsigned char b2
)
750 wxCHECK_RET( Ok(), wxT("invalid image") );
752 unsigned char *data
= GetData();
754 const int w
= GetWidth();
755 const int h
= GetHeight();
757 for (int j
= 0; j
< h
; j
++)
758 for (int i
= 0; i
< w
; i
++)
760 if ((data
[0] == r1
) && (data
[1] == g1
) && (data
[2] == b1
))
770 wxImage
wxImage::ConvertToGreyscale( double lr
, double lg
, double lb
) const
774 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
776 image
.Create(M_IMGDATA
->m_width
, M_IMGDATA
->m_height
, false);
778 unsigned char *dest
= image
.GetData();
780 wxCHECK_MSG( dest
, image
, wxT("unable to create image") );
782 unsigned char *src
= M_IMGDATA
->m_data
;
783 bool hasMask
= M_IMGDATA
->m_hasMask
;
784 unsigned char maskRed
= M_IMGDATA
->m_maskRed
;
785 unsigned char maskGreen
= M_IMGDATA
->m_maskGreen
;
786 unsigned char maskBlue
= M_IMGDATA
->m_maskBlue
;
789 image
.SetMaskColour(maskRed
, maskGreen
, maskBlue
);
791 const long size
= M_IMGDATA
->m_width
* M_IMGDATA
->m_height
;
792 for ( long i
= 0; i
< size
; i
++, src
+= 3, dest
+= 3 )
794 // don't modify the mask
795 if ( hasMask
&& src
[0] == maskRed
&& src
[1] == maskGreen
&& src
[2] == maskBlue
)
797 memcpy(dest
, src
, 3);
801 // calculate the luma
802 double luma
= (src
[0] * lr
+ src
[1] * lg
+ src
[2] * lb
) + 0.5;
803 dest
[0] = dest
[1] = dest
[2] = wx_static_cast(unsigned char, luma
);
810 wxImage
wxImage::ConvertToMono( unsigned char r
, unsigned char g
, unsigned char b
) const
814 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
816 image
.Create( M_IMGDATA
->m_width
, M_IMGDATA
->m_height
, false );
818 unsigned char *data
= image
.GetData();
820 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
822 if (M_IMGDATA
->m_hasMask
)
824 if (M_IMGDATA
->m_maskRed
== r
&& M_IMGDATA
->m_maskGreen
== g
&&
825 M_IMGDATA
->m_maskBlue
== b
)
826 image
.SetMaskColour( 255, 255, 255 );
828 image
.SetMaskColour( 0, 0, 0 );
831 long size
= M_IMGDATA
->m_height
* M_IMGDATA
->m_width
;
833 unsigned char *srcd
= M_IMGDATA
->m_data
;
834 unsigned char *tard
= image
.GetData();
836 for ( long i
= 0; i
< size
; i
++, srcd
+= 3, tard
+= 3 )
838 if (srcd
[0] == r
&& srcd
[1] == g
&& srcd
[2] == b
)
839 tard
[0] = tard
[1] = tard
[2] = 255;
841 tard
[0] = tard
[1] = tard
[2] = 0;
847 int wxImage::GetWidth() const
849 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
851 return M_IMGDATA
->m_width
;
854 int wxImage::GetHeight() const
856 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
858 return M_IMGDATA
->m_height
;
861 long wxImage::XYToIndex(int x
, int y
) const
865 x
< M_IMGDATA
->m_width
&& y
< M_IMGDATA
->m_height
)
867 return y
*M_IMGDATA
->m_width
+ x
;
873 void wxImage::SetRGB( int x
, int y
, unsigned char r
, unsigned char g
, unsigned char b
)
875 long pos
= XYToIndex(x
, y
);
876 wxCHECK_RET( pos
!= -1, wxT("invalid image coordinates") );
880 M_IMGDATA
->m_data
[ pos
] = r
;
881 M_IMGDATA
->m_data
[ pos
+1 ] = g
;
882 M_IMGDATA
->m_data
[ pos
+2 ] = b
;
885 void wxImage::SetRGB( const wxRect
& rect_
, unsigned char r
, unsigned char g
, unsigned char b
)
887 wxCHECK_RET( Ok(), wxT("invalid image") );
890 wxRect
imageRect(0, 0, GetWidth(), GetHeight());
891 if ( rect
== wxRect() )
897 wxCHECK_RET( imageRect
.Inside(rect
.GetTopLeft()) &&
898 imageRect
.Inside(rect
.GetBottomRight()),
899 wxT("invalid bounding rectangle") );
902 int x1
= rect
.GetLeft(),
904 x2
= rect
.GetRight() + 1,
905 y2
= rect
.GetBottom() + 1;
907 unsigned char *data
wxDUMMY_INITIALIZE(NULL
);
908 int x
, y
, width
= GetWidth();
909 for (y
= y1
; y
< y2
; y
++)
911 data
= M_IMGDATA
->m_data
+ (y
*width
+ x1
)*3;
912 for (x
= x1
; x
< x2
; x
++)
921 unsigned char wxImage::GetRed( int x
, int y
) const
923 long pos
= XYToIndex(x
, y
);
924 wxCHECK_MSG( pos
!= -1, 0, wxT("invalid image coordinates") );
928 return M_IMGDATA
->m_data
[pos
];
931 unsigned char wxImage::GetGreen( int x
, int y
) const
933 long pos
= XYToIndex(x
, y
);
934 wxCHECK_MSG( pos
!= -1, 0, wxT("invalid image coordinates") );
938 return M_IMGDATA
->m_data
[pos
+1];
941 unsigned char wxImage::GetBlue( int x
, int y
) const
943 long pos
= XYToIndex(x
, y
);
944 wxCHECK_MSG( pos
!= -1, 0, wxT("invalid image coordinates") );
948 return M_IMGDATA
->m_data
[pos
+2];
951 bool wxImage::Ok() const
953 // image of 0 width or height can't be considered ok - at least because it
954 // causes crashes in ConvertToBitmap() if we don't catch it in time
955 wxImageRefData
*data
= M_IMGDATA
;
956 return data
&& data
->m_ok
&& data
->m_width
&& data
->m_height
;
959 unsigned char *wxImage::GetData() const
961 wxCHECK_MSG( Ok(), (unsigned char *)NULL
, wxT("invalid image") );
963 return M_IMGDATA
->m_data
;
966 void wxImage::SetData( unsigned char *data
, bool static_data
)
968 wxCHECK_RET( Ok(), wxT("invalid image") );
970 wxImageRefData
*newRefData
= new wxImageRefData();
972 newRefData
->m_width
= M_IMGDATA
->m_width
;
973 newRefData
->m_height
= M_IMGDATA
->m_height
;
974 newRefData
->m_data
= data
;
975 newRefData
->m_ok
= true;
976 newRefData
->m_maskRed
= M_IMGDATA
->m_maskRed
;
977 newRefData
->m_maskGreen
= M_IMGDATA
->m_maskGreen
;
978 newRefData
->m_maskBlue
= M_IMGDATA
->m_maskBlue
;
979 newRefData
->m_hasMask
= M_IMGDATA
->m_hasMask
;
980 newRefData
->m_static
= static_data
;
984 m_refData
= newRefData
;
987 void wxImage::SetData( unsigned char *data
, int new_width
, int new_height
, bool static_data
)
989 wxImageRefData
*newRefData
= new wxImageRefData();
993 newRefData
->m_width
= new_width
;
994 newRefData
->m_height
= new_height
;
995 newRefData
->m_data
= data
;
996 newRefData
->m_ok
= true;
997 newRefData
->m_maskRed
= M_IMGDATA
->m_maskRed
;
998 newRefData
->m_maskGreen
= M_IMGDATA
->m_maskGreen
;
999 newRefData
->m_maskBlue
= M_IMGDATA
->m_maskBlue
;
1000 newRefData
->m_hasMask
= M_IMGDATA
->m_hasMask
;
1004 newRefData
->m_width
= new_width
;
1005 newRefData
->m_height
= new_height
;
1006 newRefData
->m_data
= data
;
1007 newRefData
->m_ok
= true;
1009 newRefData
->m_static
= static_data
;
1013 m_refData
= newRefData
;
1016 // ----------------------------------------------------------------------------
1017 // alpha channel support
1018 // ----------------------------------------------------------------------------
1020 void wxImage::SetAlpha(int x
, int y
, unsigned char alpha
)
1022 wxCHECK_RET( HasAlpha(), wxT("no alpha channel") );
1024 long pos
= XYToIndex(x
, y
);
1025 wxCHECK_RET( pos
!= -1, wxT("invalid image coordinates") );
1027 M_IMGDATA
->m_alpha
[pos
] = alpha
;
1030 unsigned char wxImage::GetAlpha(int x
, int y
) const
1032 wxCHECK_MSG( HasAlpha(), 0, wxT("no alpha channel") );
1034 long pos
= XYToIndex(x
, y
);
1035 wxCHECK_MSG( pos
!= -1, 0, wxT("invalid image coordinates") );
1037 return M_IMGDATA
->m_alpha
[pos
];
1041 wxImage::ConvertColourToAlpha(unsigned char r
, unsigned char g
, unsigned char b
)
1045 const int w
= M_IMGDATA
->m_width
;
1046 const int h
= M_IMGDATA
->m_height
;
1048 unsigned char *alpha
= GetAlpha();
1049 unsigned char *data
= GetData();
1051 for ( int y
= 0; y
< h
; y
++ )
1053 for ( int x
= 0; x
< w
; x
++ )
1065 void wxImage::SetAlpha( unsigned char *alpha
, bool static_data
)
1067 wxCHECK_RET( Ok(), wxT("invalid image") );
1071 alpha
= (unsigned char *)malloc(M_IMGDATA
->m_width
*M_IMGDATA
->m_height
);
1074 free(M_IMGDATA
->m_alpha
);
1075 M_IMGDATA
->m_alpha
= alpha
;
1076 M_IMGDATA
->m_staticAlpha
= static_data
;
1079 unsigned char *wxImage::GetAlpha() const
1081 wxCHECK_MSG( Ok(), (unsigned char *)NULL
, wxT("invalid image") );
1083 return M_IMGDATA
->m_alpha
;
1086 void wxImage::InitAlpha()
1088 wxCHECK_RET( !HasAlpha(), wxT("image already has an alpha channel") );
1090 // initialize memory for alpha channel
1093 unsigned char *alpha
= M_IMGDATA
->m_alpha
;
1094 const size_t lenAlpha
= M_IMGDATA
->m_width
* M_IMGDATA
->m_height
;
1098 // use the mask to initialize the alpha channel.
1099 const unsigned char * const alphaEnd
= alpha
+ lenAlpha
;
1101 const unsigned char mr
= M_IMGDATA
->m_maskRed
;
1102 const unsigned char mg
= M_IMGDATA
->m_maskGreen
;
1103 const unsigned char mb
= M_IMGDATA
->m_maskBlue
;
1104 for ( unsigned char *src
= M_IMGDATA
->m_data
;
1108 *alpha
= (src
[0] == mr
&& src
[1] == mg
&& src
[2] == mb
)
1109 ? wxIMAGE_ALPHA_TRANSPARENT
1110 : wxIMAGE_ALPHA_OPAQUE
;
1113 M_IMGDATA
->m_hasMask
= false;
1117 // make the image fully opaque
1118 memset(alpha
, wxIMAGE_ALPHA_OPAQUE
, lenAlpha
);
1122 // ----------------------------------------------------------------------------
1124 // ----------------------------------------------------------------------------
1126 void wxImage::SetMaskColour( unsigned char r
, unsigned char g
, unsigned char b
)
1128 wxCHECK_RET( Ok(), wxT("invalid image") );
1130 M_IMGDATA
->m_maskRed
= r
;
1131 M_IMGDATA
->m_maskGreen
= g
;
1132 M_IMGDATA
->m_maskBlue
= b
;
1133 M_IMGDATA
->m_hasMask
= true;
1136 bool wxImage::GetOrFindMaskColour( unsigned char *r
, unsigned char *g
, unsigned char *b
) const
1138 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1140 if (M_IMGDATA
->m_hasMask
)
1142 if (r
) *r
= M_IMGDATA
->m_maskRed
;
1143 if (g
) *g
= M_IMGDATA
->m_maskGreen
;
1144 if (b
) *b
= M_IMGDATA
->m_maskBlue
;
1149 FindFirstUnusedColour(r
, g
, b
);
1154 unsigned char wxImage::GetMaskRed() const
1156 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
1158 return M_IMGDATA
->m_maskRed
;
1161 unsigned char wxImage::GetMaskGreen() const
1163 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
1165 return M_IMGDATA
->m_maskGreen
;
1168 unsigned char wxImage::GetMaskBlue() const
1170 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
1172 return M_IMGDATA
->m_maskBlue
;
1175 void wxImage::SetMask( bool mask
)
1177 wxCHECK_RET( Ok(), wxT("invalid image") );
1179 M_IMGDATA
->m_hasMask
= mask
;
1182 bool wxImage::HasMask() const
1184 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1186 return M_IMGDATA
->m_hasMask
;
1189 bool wxImage::IsTransparent(int x
, int y
, unsigned char threshold
) const
1191 long pos
= XYToIndex(x
, y
);
1192 wxCHECK_MSG( pos
!= -1, false, wxT("invalid image coordinates") );
1195 if ( M_IMGDATA
->m_hasMask
)
1197 const unsigned char *p
= M_IMGDATA
->m_data
+ 3*pos
;
1198 if ( p
[0] == M_IMGDATA
->m_maskRed
&&
1199 p
[1] == M_IMGDATA
->m_maskGreen
&&
1200 p
[2] == M_IMGDATA
->m_maskBlue
)
1207 if ( M_IMGDATA
->m_alpha
)
1209 if ( M_IMGDATA
->m_alpha
[pos
] < threshold
)
1211 // transparent enough
1220 bool wxImage::SetMaskFromImage(const wxImage
& mask
,
1221 unsigned char mr
, unsigned char mg
, unsigned char mb
)
1223 // check that the images are the same size
1224 if ( (M_IMGDATA
->m_height
!= mask
.GetHeight() ) || (M_IMGDATA
->m_width
!= mask
.GetWidth () ) )
1226 wxLogError( _("Image and mask have different sizes.") );
1230 // find unused colour
1231 unsigned char r
,g
,b
;
1232 if (!FindFirstUnusedColour(&r
, &g
, &b
))
1234 wxLogError( _("No unused colour in image being masked.") );
1238 unsigned char *imgdata
= GetData();
1239 unsigned char *maskdata
= mask
.GetData();
1241 const int w
= GetWidth();
1242 const int h
= GetHeight();
1244 for (int j
= 0; j
< h
; j
++)
1246 for (int i
= 0; i
< w
; i
++)
1248 if ((maskdata
[0] == mr
) && (maskdata
[1] == mg
) && (maskdata
[2] == mb
))
1259 SetMaskColour(r
, g
, b
);
1265 bool wxImage::ConvertAlphaToMask(unsigned char threshold
)
1270 unsigned char mr
, mg
, mb
;
1271 if (!FindFirstUnusedColour(&mr
, &mg
, &mb
))
1273 wxLogError( _("No unused colour in image being masked.") );
1278 SetMaskColour(mr
, mg
, mb
);
1280 unsigned char *imgdata
= GetData();
1281 unsigned char *alphadata
= GetAlpha();
1284 int h
= GetHeight();
1286 for (int y
= 0; y
< h
; y
++)
1288 for (int x
= 0; x
< w
; x
++, imgdata
+= 3, alphadata
++)
1290 if (*alphadata
< threshold
)
1299 free(M_IMGDATA
->m_alpha
);
1300 M_IMGDATA
->m_alpha
= NULL
;
1305 // ----------------------------------------------------------------------------
1306 // Palette functions
1307 // ----------------------------------------------------------------------------
1311 bool wxImage::HasPalette() const
1316 return M_IMGDATA
->m_palette
.Ok();
1319 const wxPalette
& wxImage::GetPalette() const
1321 wxCHECK_MSG( Ok(), wxNullPalette
, wxT("invalid image") );
1323 return M_IMGDATA
->m_palette
;
1326 void wxImage::SetPalette(const wxPalette
& palette
)
1328 wxCHECK_RET( Ok(), wxT("invalid image") );
1330 M_IMGDATA
->m_palette
= palette
;
1333 #endif // wxUSE_PALETTE
1335 // ----------------------------------------------------------------------------
1336 // Option functions (arbitrary name/value mapping)
1337 // ----------------------------------------------------------------------------
1339 void wxImage::SetOption(const wxString
& name
, const wxString
& value
)
1341 wxCHECK_RET( Ok(), wxT("invalid image") );
1343 int idx
= M_IMGDATA
->m_optionNames
.Index(name
, false);
1344 if (idx
== wxNOT_FOUND
)
1346 M_IMGDATA
->m_optionNames
.Add(name
);
1347 M_IMGDATA
->m_optionValues
.Add(value
);
1351 M_IMGDATA
->m_optionNames
[idx
] = name
;
1352 M_IMGDATA
->m_optionValues
[idx
] = value
;
1356 void wxImage::SetOption(const wxString
& name
, int value
)
1359 valStr
.Printf(wxT("%d"), value
);
1360 SetOption(name
, valStr
);
1363 wxString
wxImage::GetOption(const wxString
& name
) const
1365 wxCHECK_MSG( Ok(), wxEmptyString
, wxT("invalid image") );
1367 int idx
= M_IMGDATA
->m_optionNames
.Index(name
, false);
1368 if (idx
== wxNOT_FOUND
)
1369 return wxEmptyString
;
1371 return M_IMGDATA
->m_optionValues
[idx
];
1374 int wxImage::GetOptionInt(const wxString
& name
) const
1376 return wxAtoi(GetOption(name
));
1379 bool wxImage::HasOption(const wxString
& name
) const
1381 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1383 return (M_IMGDATA
->m_optionNames
.Index(name
, false) != wxNOT_FOUND
);
1386 // ----------------------------------------------------------------------------
1388 // ----------------------------------------------------------------------------
1390 bool wxImage::LoadFile( const wxString
& filename
, long type
, int index
)
1393 if (wxFileExists(filename
))
1395 wxFileInputStream
stream(filename
);
1396 wxBufferedInputStream
bstream( stream
);
1397 return LoadFile(bstream
, type
, index
);
1401 wxLogError( _("Can't load image from file '%s': file does not exist."), filename
.c_str() );
1405 #else // !wxUSE_STREAMS
1407 #endif // wxUSE_STREAMS
1410 bool wxImage::LoadFile( const wxString
& filename
, const wxString
& mimetype
, int index
)
1413 if (wxFileExists(filename
))
1415 wxFileInputStream
stream(filename
);
1416 wxBufferedInputStream
bstream( stream
);
1417 return LoadFile(bstream
, mimetype
, index
);
1421 wxLogError( _("Can't load image from file '%s': file does not exist."), filename
.c_str() );
1425 #else // !wxUSE_STREAMS
1427 #endif // wxUSE_STREAMS
1432 bool wxImage::SaveFile( const wxString
& filename
) const
1434 wxString ext
= filename
.AfterLast('.').Lower();
1436 wxImageHandler
* pHandler
= FindHandler(ext
, -1);
1439 SaveFile(filename
, pHandler
->GetType());
1443 wxLogError(_("Can't save image to file '%s': unknown extension."), filename
.c_str());
1448 bool wxImage::SaveFile( const wxString
& filename
, int type
) const
1451 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1453 ((wxImage
*)this)->SetOption(wxIMAGE_OPTION_FILENAME
, filename
);
1455 wxFileOutputStream
stream(filename
);
1457 if ( stream
.IsOk() )
1459 wxBufferedOutputStream
bstream( stream
);
1460 return SaveFile(bstream
, type
);
1462 #endif // wxUSE_STREAMS
1467 bool wxImage::SaveFile( const wxString
& filename
, const wxString
& mimetype
) const
1470 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1472 ((wxImage
*)this)->SetOption(wxIMAGE_OPTION_FILENAME
, filename
);
1474 wxFileOutputStream
stream(filename
);
1476 if ( stream
.IsOk() )
1478 wxBufferedOutputStream
bstream( stream
);
1479 return SaveFile(bstream
, mimetype
);
1481 #endif // wxUSE_STREAMS
1486 bool wxImage::CanRead( const wxString
&name
)
1489 wxFileInputStream
stream(name
);
1490 return CanRead(stream
);
1496 int wxImage::GetImageCount( const wxString
&name
, long type
)
1499 wxFileInputStream
stream(name
);
1501 return GetImageCount(stream
, type
);
1509 bool wxImage::CanRead( wxInputStream
&stream
)
1511 const wxList
& list
= GetHandlers();
1513 for ( wxList::compatibility_iterator node
= list
.GetFirst(); node
; node
= node
->GetNext() )
1515 wxImageHandler
*handler
=(wxImageHandler
*)node
->GetData();
1516 if (handler
->CanRead( stream
))
1523 int wxImage::GetImageCount( wxInputStream
&stream
, long type
)
1525 wxImageHandler
*handler
;
1527 if ( type
== wxBITMAP_TYPE_ANY
)
1529 wxList
&list
=GetHandlers();
1531 for (wxList::compatibility_iterator node
= list
.GetFirst(); node
; node
= node
->GetNext())
1533 handler
=(wxImageHandler
*)node
->GetData();
1534 if ( handler
->CanRead(stream
) )
1535 return handler
->GetImageCount(stream
);
1539 wxLogWarning(_("No handler found for image type."));
1543 handler
= FindHandler(type
);
1547 wxLogWarning(_("No image handler for type %d defined."), type
);
1551 if ( handler
->CanRead(stream
) )
1553 return handler
->GetImageCount(stream
);
1557 wxLogError(_("Image file is not of type %d."), type
);
1562 bool wxImage::LoadFile( wxInputStream
& stream
, long type
, int index
)
1566 m_refData
= new wxImageRefData
;
1568 wxImageHandler
*handler
;
1570 if ( type
== wxBITMAP_TYPE_ANY
)
1572 wxList
&list
=GetHandlers();
1574 for ( wxList::compatibility_iterator node
= list
.GetFirst(); node
; node
= node
->GetNext() )
1576 handler
=(wxImageHandler
*)node
->GetData();
1577 if ( handler
->CanRead(stream
) )
1578 return handler
->LoadFile(this, stream
, true/*verbose*/, index
);
1582 wxLogWarning( _("No handler found for image type.") );
1586 handler
= FindHandler(type
);
1590 wxLogWarning( _("No image handler for type %d defined."), type
);
1595 if (stream
.IsSeekable() && !handler
->CanRead(stream
))
1597 wxLogError(_("Image file is not of type %d."), type
);
1601 return handler
->LoadFile(this, stream
, true/*verbose*/, index
);
1604 bool wxImage::LoadFile( wxInputStream
& stream
, const wxString
& mimetype
, int index
)
1608 m_refData
= new wxImageRefData
;
1610 wxImageHandler
*handler
= FindHandlerMime(mimetype
);
1614 wxLogWarning( _("No image handler for type %s defined."), mimetype
.GetData() );
1619 if (stream
.IsSeekable() && !handler
->CanRead(stream
))
1621 wxLogError(_("Image file is not of type %s."), (const wxChar
*) mimetype
);
1625 return handler
->LoadFile( this, stream
, true/*verbose*/, index
);
1628 bool wxImage::SaveFile( wxOutputStream
& stream
, int type
) const
1630 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1632 wxImageHandler
*handler
= FindHandler(type
);
1635 wxLogWarning( _("No image handler for type %d defined."), type
);
1640 return handler
->SaveFile( (wxImage
*)this, stream
);
1643 bool wxImage::SaveFile( wxOutputStream
& stream
, const wxString
& mimetype
) const
1645 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1647 wxImageHandler
*handler
= FindHandlerMime(mimetype
);
1650 wxLogWarning( _("No image handler for type %s defined."), mimetype
.GetData() );
1655 return handler
->SaveFile( (wxImage
*)this, stream
);
1657 #endif // wxUSE_STREAMS
1659 // ----------------------------------------------------------------------------
1660 // image I/O handlers
1661 // ----------------------------------------------------------------------------
1663 void wxImage::AddHandler( wxImageHandler
*handler
)
1665 // Check for an existing handler of the type being added.
1666 if (FindHandler( handler
->GetType() ) == 0)
1668 sm_handlers
.Append( handler
);
1672 // This is not documented behaviour, merely the simplest 'fix'
1673 // for preventing duplicate additions. If someone ever has
1674 // a good reason to add and remove duplicate handlers (and they
1675 // may) we should probably refcount the duplicates.
1676 // also an issue in InsertHandler below.
1678 wxLogDebug( _T("Adding duplicate image handler for '%s'"),
1679 handler
->GetName().c_str() );
1684 void wxImage::InsertHandler( wxImageHandler
*handler
)
1686 // Check for an existing handler of the type being added.
1687 if (FindHandler( handler
->GetType() ) == 0)
1689 sm_handlers
.Insert( handler
);
1693 // see AddHandler for additional comments.
1694 wxLogDebug( _T("Inserting duplicate image handler for '%s'"),
1695 handler
->GetName().c_str() );
1700 bool wxImage::RemoveHandler( const wxString
& name
)
1702 wxImageHandler
*handler
= FindHandler(name
);
1705 sm_handlers
.DeleteObject(handler
);
1713 wxImageHandler
*wxImage::FindHandler( const wxString
& name
)
1715 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
1718 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
1719 if (handler
->GetName().Cmp(name
) == 0) return handler
;
1721 node
= node
->GetNext();
1726 wxImageHandler
*wxImage::FindHandler( const wxString
& extension
, long bitmapType
)
1728 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
1731 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
1732 if ( (handler
->GetExtension().Cmp(extension
) == 0) &&
1733 (bitmapType
== -1 || handler
->GetType() == bitmapType
) )
1735 node
= node
->GetNext();
1740 wxImageHandler
*wxImage::FindHandler( long bitmapType
)
1742 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
1745 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
1746 if (handler
->GetType() == bitmapType
) return handler
;
1747 node
= node
->GetNext();
1752 wxImageHandler
*wxImage::FindHandlerMime( const wxString
& mimetype
)
1754 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
1757 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
1758 if (handler
->GetMimeType().IsSameAs(mimetype
, false)) return handler
;
1759 node
= node
->GetNext();
1764 void wxImage::InitStandardHandlers()
1767 AddHandler(new wxBMPHandler
);
1768 #endif // wxUSE_STREAMS
1771 void wxImage::CleanUpHandlers()
1773 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
1776 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
1777 wxList::compatibility_iterator next
= node
->GetNext();
1782 sm_handlers
.Clear();
1785 wxString
wxImage::GetImageExtWildcard()
1789 wxList
& Handlers
= wxImage::GetHandlers();
1790 wxList::compatibility_iterator Node
= Handlers
.GetFirst();
1793 wxImageHandler
* Handler
= (wxImageHandler
*)Node
->GetData();
1794 fmts
+= wxT("*.") + Handler
->GetExtension();
1795 Node
= Node
->GetNext();
1796 if ( Node
) fmts
+= wxT(";");
1799 return wxT("(") + fmts
+ wxT(")|") + fmts
;
1802 wxImage::HSVValue
wxImage::RGBtoHSV(const RGBValue
& rgb
)
1804 const double red
= rgb
.red
/ 255.0,
1805 green
= rgb
.green
/ 255.0,
1806 blue
= rgb
.blue
/ 255.0;
1808 // find the min and max intensity (and remember which one was it for the
1810 double minimumRGB
= red
;
1811 if ( green
< minimumRGB
)
1813 if ( blue
< minimumRGB
)
1816 enum { RED
, GREEN
, BLUE
} chMax
= RED
;
1817 double maximumRGB
= red
;
1818 if ( green
> maximumRGB
)
1823 if ( blue
> maximumRGB
)
1829 const double value
= maximumRGB
;
1831 double hue
= 0.0, saturation
;
1832 const double deltaRGB
= maximumRGB
- minimumRGB
;
1833 if ( wxIsNullDouble(deltaRGB
) )
1835 // Gray has no color
1844 hue
= (green
- blue
) / deltaRGB
;
1848 hue
= 2.0 + (blue
- red
) / deltaRGB
;
1852 hue
= 4.0 + (red
- green
) / deltaRGB
;
1856 wxFAIL_MSG(wxT("hue not specified"));
1865 saturation
= deltaRGB
/ maximumRGB
;
1868 return HSVValue(hue
, saturation
, value
);
1871 wxImage::RGBValue
wxImage::HSVtoRGB(const HSVValue
& hsv
)
1873 double red
, green
, blue
;
1875 if ( wxIsNullDouble(hsv
.saturation
) )
1884 double hue
= hsv
.hue
* 6.0; // sector 0 to 5
1885 int i
= (int)floor(hue
);
1886 double f
= hue
- i
; // fractional part of h
1887 double p
= hsv
.value
* (1.0 - hsv
.saturation
);
1893 green
= hsv
.value
* (1.0 - hsv
.saturation
* (1.0 - f
));
1898 red
= hsv
.value
* (1.0 - hsv
.saturation
* f
);
1906 blue
= hsv
.value
* (1.0 - hsv
.saturation
* (1.0 - f
));
1911 green
= hsv
.value
* (1.0 - hsv
.saturation
* f
);
1916 red
= hsv
.value
* (1.0 - hsv
.saturation
* (1.0 - f
));
1924 blue
= hsv
.value
* (1.0 - hsv
.saturation
* f
);
1929 return RGBValue((unsigned char)(red
* 255.0),
1930 (unsigned char)(green
* 255.0),
1931 (unsigned char)(blue
* 255.0));
1935 * Rotates the hue of each pixel of the image. angle is a double in the range
1936 * -1.0..1.0 where -1.0 is -360 degrees and 1.0 is 360 degrees
1938 void wxImage::RotateHue(double angle
)
1940 unsigned char *srcBytePtr
;
1941 unsigned char *dstBytePtr
;
1942 unsigned long count
;
1943 wxImage::HSVValue hsv
;
1944 wxImage::RGBValue rgb
;
1946 wxASSERT (angle
>= -1.0 && angle
<= 1.0);
1947 count
= M_IMGDATA
->m_width
* M_IMGDATA
->m_height
;
1948 if ( count
> 0 && !wxIsNullDouble(angle
) )
1950 srcBytePtr
= M_IMGDATA
->m_data
;
1951 dstBytePtr
= srcBytePtr
;
1954 rgb
.red
= *srcBytePtr
++;
1955 rgb
.green
= *srcBytePtr
++;
1956 rgb
.blue
= *srcBytePtr
++;
1957 hsv
= RGBtoHSV(rgb
);
1959 hsv
.hue
= hsv
.hue
+ angle
;
1961 hsv
.hue
= hsv
.hue
- 1.0;
1962 else if (hsv
.hue
< 0.0)
1963 hsv
.hue
= hsv
.hue
+ 1.0;
1965 rgb
= HSVtoRGB(hsv
);
1966 *dstBytePtr
++ = rgb
.red
;
1967 *dstBytePtr
++ = rgb
.green
;
1968 *dstBytePtr
++ = rgb
.blue
;
1969 } while (--count
!= 0);
1973 //-----------------------------------------------------------------------------
1975 //-----------------------------------------------------------------------------
1977 IMPLEMENT_ABSTRACT_CLASS(wxImageHandler
,wxObject
)
1980 bool wxImageHandler::LoadFile( wxImage
*WXUNUSED(image
), wxInputStream
& WXUNUSED(stream
), bool WXUNUSED(verbose
), int WXUNUSED(index
) )
1985 bool wxImageHandler::SaveFile( wxImage
*WXUNUSED(image
), wxOutputStream
& WXUNUSED(stream
), bool WXUNUSED(verbose
) )
1990 int wxImageHandler::GetImageCount( wxInputStream
& WXUNUSED(stream
) )
1995 bool wxImageHandler::CanRead( const wxString
& name
)
1997 if (wxFileExists(name
))
1999 wxFileInputStream
stream(name
);
2000 return CanRead(stream
);
2003 wxLogError( _("Can't check image format of file '%s': file does not exist."), name
.c_str() );
2008 bool wxImageHandler::CallDoCanRead(wxInputStream
& stream
)
2010 wxFileOffset posOld
= stream
.TellI();
2011 if ( posOld
== wxInvalidOffset
)
2013 // can't test unseekable stream
2017 bool ok
= DoCanRead(stream
);
2019 // restore the old position to be able to test other formats and so on
2020 if ( stream
.SeekI(posOld
) == wxInvalidOffset
)
2022 wxLogDebug(_T("Failed to rewind the stream in wxImageHandler!"));
2024 // reading would fail anyhow as we're not at the right position
2031 #endif // wxUSE_STREAMS
2033 // ----------------------------------------------------------------------------
2034 // image histogram stuff
2035 // ----------------------------------------------------------------------------
2038 wxImageHistogram::FindFirstUnusedColour(unsigned char *r
,
2043 unsigned char g2
) const
2045 unsigned long key
= MakeKey(r2
, g2
, b2
);
2047 while ( find(key
) != end() )
2049 // color already used
2061 wxLogError(_("No unused colour in image.") );
2067 key
= MakeKey(r2
, g2
, b2
);
2081 wxImage::FindFirstUnusedColour(unsigned char *r
,
2086 unsigned char g2
) const
2088 wxImageHistogram histogram
;
2090 ComputeHistogram(histogram
);
2092 return histogram
.FindFirstUnusedColour(r
, g
, b
, r2
, g2
, b2
);
2098 // Counts and returns the number of different colours. Optionally stops
2099 // when it exceeds 'stopafter' different colours. This is useful, for
2100 // example, to see if the image can be saved as 8-bit (256 colour or
2101 // less, in this case it would be invoked as CountColours(256)). Default
2102 // value for stopafter is -1 (don't care).
2104 unsigned long wxImage::CountColours( unsigned long stopafter
) const
2108 unsigned char r
, g
, b
;
2110 unsigned long size
, nentries
, key
;
2113 size
= GetWidth() * GetHeight();
2116 for (unsigned long j
= 0; (j
< size
) && (nentries
<= stopafter
) ; j
++)
2121 key
= wxImageHistogram::MakeKey(r
, g
, b
);
2123 if (h
.Get(key
) == NULL
)
2134 unsigned long wxImage::ComputeHistogram( wxImageHistogram
&h
) const
2136 unsigned char *p
= GetData();
2137 unsigned long nentries
= 0;
2141 const unsigned long size
= GetWidth() * GetHeight();
2143 unsigned char r
, g
, b
;
2144 for ( unsigned long n
= 0; n
< size
; n
++ )
2150 wxImageHistogramEntry
& entry
= h
[wxImageHistogram::MakeKey(r
, g
, b
)];
2152 if ( entry
.value
++ == 0 )
2153 entry
.index
= nentries
++;
2160 * Rotation code by Carlos Moreno
2163 // GRG: I've removed wxRotationPoint - we already have wxRealPoint which
2164 // does exactly the same thing. And I also got rid of wxRotationPixel
2165 // bacause of potential problems in architectures where alignment
2166 // is an issue, so I had to rewrite parts of the code.
2168 static const double gs_Epsilon
= 1e-10;
2170 static inline int wxCint (double x
)
2172 return (x
> 0) ? (int) (x
+ 0.5) : (int) (x
- 0.5);
2176 // Auxiliary function to rotate a point (x,y) with respect to point p0
2177 // make it inline and use a straight return to facilitate optimization
2178 // also, the function receives the sine and cosine of the angle to avoid
2179 // repeating the time-consuming calls to these functions -- sin/cos can
2180 // be computed and stored in the calling function.
2182 inline wxRealPoint
rotated_point (const wxRealPoint
& p
, double cos_angle
, double sin_angle
, const wxRealPoint
& p0
)
2184 return wxRealPoint (p0
.x
+ (p
.x
- p0
.x
) * cos_angle
- (p
.y
- p0
.y
) * sin_angle
,
2185 p0
.y
+ (p
.y
- p0
.y
) * cos_angle
+ (p
.x
- p0
.x
) * sin_angle
);
2188 inline wxRealPoint
rotated_point (double x
, double y
, double cos_angle
, double sin_angle
, const wxRealPoint
& p0
)
2190 return rotated_point (wxRealPoint(x
,y
), cos_angle
, sin_angle
, p0
);
2193 wxImage
wxImage::Rotate(double angle
, const wxPoint
& centre_of_rotation
, bool interpolating
, wxPoint
* offset_after_rotation
) const
2196 angle
= -angle
; // screen coordinates are a mirror image of "real" coordinates
2198 bool has_alpha
= HasAlpha();
2200 // Create pointer-based array to accelerate access to wxImage's data
2201 unsigned char ** data
= new unsigned char * [GetHeight()];
2202 data
[0] = GetData();
2203 for (i
= 1; i
< GetHeight(); i
++)
2204 data
[i
] = data
[i
- 1] + (3 * GetWidth());
2206 // Same for alpha channel
2207 unsigned char ** alpha
= NULL
;
2210 alpha
= new unsigned char * [GetHeight()];
2211 alpha
[0] = GetAlpha();
2212 for (i
= 1; i
< GetHeight(); i
++)
2213 alpha
[i
] = alpha
[i
- 1] + GetWidth();
2216 // precompute coefficients for rotation formula
2217 // (sine and cosine of the angle)
2218 const double cos_angle
= cos(angle
);
2219 const double sin_angle
= sin(angle
);
2221 // Create new Image to store the result
2222 // First, find rectangle that covers the rotated image; to do that,
2223 // rotate the four corners
2225 const wxRealPoint
p0(centre_of_rotation
.x
, centre_of_rotation
.y
);
2227 wxRealPoint p1
= rotated_point (0, 0, cos_angle
, sin_angle
, p0
);
2228 wxRealPoint p2
= rotated_point (0, GetHeight(), cos_angle
, sin_angle
, p0
);
2229 wxRealPoint p3
= rotated_point (GetWidth(), 0, cos_angle
, sin_angle
, p0
);
2230 wxRealPoint p4
= rotated_point (GetWidth(), GetHeight(), cos_angle
, sin_angle
, p0
);
2232 int x1a
= (int) floor (wxMin (wxMin(p1
.x
, p2
.x
), wxMin(p3
.x
, p4
.x
)));
2233 int y1a
= (int) floor (wxMin (wxMin(p1
.y
, p2
.y
), wxMin(p3
.y
, p4
.y
)));
2234 int x2a
= (int) ceil (wxMax (wxMax(p1
.x
, p2
.x
), wxMax(p3
.x
, p4
.x
)));
2235 int y2a
= (int) ceil (wxMax (wxMax(p1
.y
, p2
.y
), wxMax(p3
.y
, p4
.y
)));
2237 // Create rotated image
2238 wxImage
rotated (x2a
- x1a
+ 1, y2a
- y1a
+ 1, false);
2239 // With alpha channel
2243 if (offset_after_rotation
!= NULL
)
2245 *offset_after_rotation
= wxPoint (x1a
, y1a
);
2248 // GRG: The rotated (destination) image is always accessed
2249 // sequentially, so there is no need for a pointer-based
2250 // array here (and in fact it would be slower).
2252 unsigned char * dst
= rotated
.GetData();
2254 unsigned char * alpha_dst
= NULL
;
2256 alpha_dst
= rotated
.GetAlpha();
2258 // GRG: if the original image has a mask, use its RGB values
2259 // as the blank pixel, else, fall back to default (black).
2261 unsigned char blank_r
= 0;
2262 unsigned char blank_g
= 0;
2263 unsigned char blank_b
= 0;
2267 blank_r
= GetMaskRed();
2268 blank_g
= GetMaskGreen();
2269 blank_b
= GetMaskBlue();
2270 rotated
.SetMaskColour( blank_r
, blank_g
, blank_b
);
2273 // Now, for each point of the rotated image, find where it came from, by
2274 // performing an inverse rotation (a rotation of -angle) and getting the
2275 // pixel at those coordinates
2277 // GRG: I've taken the (interpolating) test out of the loops, so that
2278 // it is done only once, instead of repeating it for each pixel.
2283 for (int y
= 0; y
< rotated
.GetHeight(); y
++)
2285 for (x
= 0; x
< rotated
.GetWidth(); x
++)
2287 wxRealPoint src
= rotated_point (x
+ x1a
, y
+ y1a
, cos_angle
, -sin_angle
, p0
);
2289 if (-0.25 < src
.x
&& src
.x
< GetWidth() - 0.75 &&
2290 -0.25 < src
.y
&& src
.y
< GetHeight() - 0.75)
2292 // interpolate using the 4 enclosing grid-points. Those
2293 // points can be obtained using floor and ceiling of the
2294 // exact coordinates of the point
2297 if (0 < src
.x
&& src
.x
< GetWidth() - 1)
2299 x1
= wxCint(floor(src
.x
));
2300 x2
= wxCint(ceil(src
.x
));
2302 else // else means that x is near one of the borders (0 or width-1)
2304 x1
= x2
= wxCint (src
.x
);
2307 if (0 < src
.y
&& src
.y
< GetHeight() - 1)
2309 y1
= wxCint(floor(src
.y
));
2310 y2
= wxCint(ceil(src
.y
));
2314 y1
= y2
= wxCint (src
.y
);
2317 // get four points and the distances (square of the distance,
2318 // for efficiency reasons) for the interpolation formula
2320 // GRG: Do not calculate the points until they are
2321 // really needed -- this way we can calculate
2322 // just one, instead of four, if d1, d2, d3
2323 // or d4 are < gs_Epsilon
2325 const double d1
= (src
.x
- x1
) * (src
.x
- x1
) + (src
.y
- y1
) * (src
.y
- y1
);
2326 const double d2
= (src
.x
- x2
) * (src
.x
- x2
) + (src
.y
- y1
) * (src
.y
- y1
);
2327 const double d3
= (src
.x
- x2
) * (src
.x
- x2
) + (src
.y
- y2
) * (src
.y
- y2
);
2328 const double d4
= (src
.x
- x1
) * (src
.x
- x1
) + (src
.y
- y2
) * (src
.y
- y2
);
2330 // Now interpolate as a weighted average of the four surrounding
2331 // points, where the weights are the distances to each of those points
2333 // If the point is exactly at one point of the grid of the source
2334 // image, then don't interpolate -- just assign the pixel
2336 if (d1
< gs_Epsilon
) // d1,d2,d3,d4 are positive -- no need for abs()
2338 unsigned char *p
= data
[y1
] + (3 * x1
);
2344 *(alpha_dst
++) = *(alpha
[y1
] + x1
);
2346 else if (d2
< gs_Epsilon
)
2348 unsigned char *p
= data
[y1
] + (3 * x2
);
2354 *(alpha_dst
++) = *(alpha
[y1
] + x2
);
2356 else if (d3
< gs_Epsilon
)
2358 unsigned char *p
= data
[y2
] + (3 * x2
);
2364 *(alpha_dst
++) = *(alpha
[y2
] + x2
);
2366 else if (d4
< gs_Epsilon
)
2368 unsigned char *p
= data
[y2
] + (3 * x1
);
2374 *(alpha_dst
++) = *(alpha
[y2
] + x1
);
2378 // weights for the weighted average are proportional to the inverse of the distance
2379 unsigned char *v1
= data
[y1
] + (3 * x1
);
2380 unsigned char *v2
= data
[y1
] + (3 * x2
);
2381 unsigned char *v3
= data
[y2
] + (3 * x2
);
2382 unsigned char *v4
= data
[y2
] + (3 * x1
);
2384 const double w1
= 1/d1
, w2
= 1/d2
, w3
= 1/d3
, w4
= 1/d4
;
2388 *(dst
++) = (unsigned char)
2389 ( (w1
* *(v1
++) + w2
* *(v2
++) +
2390 w3
* *(v3
++) + w4
* *(v4
++)) /
2391 (w1
+ w2
+ w3
+ w4
) );
2392 *(dst
++) = (unsigned char)
2393 ( (w1
* *(v1
++) + w2
* *(v2
++) +
2394 w3
* *(v3
++) + w4
* *(v4
++)) /
2395 (w1
+ w2
+ w3
+ w4
) );
2396 *(dst
++) = (unsigned char)
2397 ( (w1
* *v1
+ w2
* *v2
+
2398 w3
* *v3
+ w4
* *v4
) /
2399 (w1
+ w2
+ w3
+ w4
) );
2403 v1
= alpha
[y1
] + (x1
);
2404 v2
= alpha
[y1
] + (x2
);
2405 v3
= alpha
[y2
] + (x2
);
2406 v4
= alpha
[y2
] + (x1
);
2408 *(alpha_dst
++) = (unsigned char)
2409 ( (w1
* *v1
+ w2
* *v2
+
2410 w3
* *v3
+ w4
* *v4
) /
2411 (w1
+ w2
+ w3
+ w4
) );
2427 else // not interpolating
2429 for (int y
= 0; y
< rotated
.GetHeight(); y
++)
2431 for (x
= 0; x
< rotated
.GetWidth(); x
++)
2433 wxRealPoint src
= rotated_point (x
+ x1a
, y
+ y1a
, cos_angle
, -sin_angle
, p0
);
2435 const int xs
= wxCint (src
.x
); // wxCint rounds to the
2436 const int ys
= wxCint (src
.y
); // closest integer
2438 if (0 <= xs
&& xs
< GetWidth() &&
2439 0 <= ys
&& ys
< GetHeight())
2441 unsigned char *p
= data
[ys
] + (3 * xs
);
2447 *(alpha_dst
++) = *(alpha
[ys
] + (xs
));
2456 *(alpha_dst
++) = 255;
2474 // A module to allow wxImage initialization/cleanup
2475 // without calling these functions from app.cpp or from
2476 // the user's application.
2478 class wxImageModule
: public wxModule
2480 DECLARE_DYNAMIC_CLASS(wxImageModule
)
2483 bool OnInit() { wxImage::InitStandardHandlers(); return true; };
2484 void OnExit() { wxImage::CleanUpHandlers(); };
2487 IMPLEMENT_DYNAMIC_CLASS(wxImageModule
, wxModule
)
2490 #endif // wxUSE_IMAGE