1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Robert Roebling
6 // Copyright: (c) Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
11 #pragma implementation "image.h"
14 // For compilers that support precompilation, includes "wx.h".
15 #include "wx/wxprec.h"
26 #include "wx/bitmap.h"
30 #include "wx/filefn.h"
31 #include "wx/wfstream.h"
33 #include "wx/module.h"
39 #include "wx/xpmdecod.h"
50 //-----------------------------------------------------------------------------
52 //-----------------------------------------------------------------------------
54 class wxImageRefData
: public wxObjectRefData
58 virtual ~wxImageRefData();
62 unsigned char *m_data
;
65 unsigned char m_maskRed
,m_maskGreen
,m_maskBlue
;
67 // alpha channel data, may be NULL for the formats without alpha support
68 unsigned char *m_alpha
;
75 #endif // wxUSE_PALETTE
77 wxArrayString m_optionNames
;
78 wxArrayString m_optionValues
;
80 DECLARE_NO_COPY_CLASS(wxImageRefData
)
83 wxImageRefData::wxImageRefData()
88 m_alpha
= (unsigned char *) NULL
;
99 wxImageRefData::~wxImageRefData()
108 wxList
wxImage::sm_handlers
;
112 //-----------------------------------------------------------------------------
114 #define M_IMGDATA ((wxImageRefData *)m_refData)
116 IMPLEMENT_DYNAMIC_CLASS(wxImage
, wxObject
)
118 wxImage::wxImage( int width
, int height
, bool clear
)
120 Create( width
, height
, clear
);
123 wxImage::wxImage( int width
, int height
, unsigned char* data
, bool static_data
)
125 Create( width
, height
, data
, static_data
);
128 wxImage::wxImage( int width
, int height
, unsigned char* data
, unsigned char* alpha
, bool static_data
)
130 Create( width
, height
, data
, alpha
, static_data
);
133 wxImage::wxImage( const wxString
& name
, long type
, int index
)
135 LoadFile( name
, type
, index
);
138 wxImage::wxImage( const wxString
& name
, const wxString
& mimetype
, int index
)
140 LoadFile( name
, mimetype
, index
);
144 wxImage::wxImage( wxInputStream
& stream
, long type
, int index
)
146 LoadFile( stream
, type
, index
);
149 wxImage::wxImage( wxInputStream
& stream
, const wxString
& mimetype
, int index
)
151 LoadFile( stream
, mimetype
, index
);
153 #endif // wxUSE_STREAMS
155 wxImage::wxImage( const wxImage
& image
)
161 wxImage::wxImage( const wxImage
* image
)
163 if (image
) Ref(*image
);
166 wxImage::wxImage( const char** xpmData
)
171 wxImage::wxImage( char** xpmData
)
173 Create((const char**) xpmData
);
176 bool wxImage::Create( const char** xpmData
)
181 wxXPMDecoder decoder
;
182 (*this) = decoder
.ReadData(xpmData
);
189 bool wxImage::Create( int width
, int height
, bool clear
)
193 m_refData
= new wxImageRefData();
195 M_IMGDATA
->m_data
= (unsigned char *) malloc( width
*height
*3 );
196 if (!M_IMGDATA
->m_data
)
203 memset(M_IMGDATA
->m_data
, 0, width
*height
*3);
205 M_IMGDATA
->m_width
= width
;
206 M_IMGDATA
->m_height
= height
;
207 M_IMGDATA
->m_ok
= true;
212 bool wxImage::Create( int width
, int height
, unsigned char* data
, bool static_data
)
216 wxCHECK_MSG( data
, false, _T("NULL data in wxImage::Create") );
218 m_refData
= new wxImageRefData();
220 M_IMGDATA
->m_data
= data
;
221 M_IMGDATA
->m_width
= width
;
222 M_IMGDATA
->m_height
= height
;
223 M_IMGDATA
->m_ok
= true;
224 M_IMGDATA
->m_static
= static_data
;
229 bool wxImage::Create( int width
, int height
, unsigned char* data
, unsigned char* alpha
, bool static_data
)
233 wxCHECK_MSG( data
, false, _T("NULL data in wxImage::Create") );
235 m_refData
= new wxImageRefData();
237 M_IMGDATA
->m_data
= data
;
238 M_IMGDATA
->m_alpha
= alpha
;
239 M_IMGDATA
->m_width
= width
;
240 M_IMGDATA
->m_height
= height
;
241 M_IMGDATA
->m_ok
= true;
242 M_IMGDATA
->m_static
= static_data
;
247 void wxImage::Destroy()
252 wxImage
wxImage::Copy() const
256 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
258 image
.Create( M_IMGDATA
->m_width
, M_IMGDATA
->m_height
, false );
260 unsigned char *data
= image
.GetData();
262 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
264 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
265 image
.SetMask( M_IMGDATA
->m_hasMask
);
267 memcpy( data
, GetData(), M_IMGDATA
->m_width
*M_IMGDATA
->m_height
*3 );
269 // also copy the image options
270 wxImageRefData
*imgData
= (wxImageRefData
*)image
.m_refData
;
271 imgData
->m_optionNames
= M_IMGDATA
->m_optionNames
;
272 imgData
->m_optionValues
= M_IMGDATA
->m_optionValues
;
277 wxImage
wxImage::ShrinkBy( int xFactor
, int yFactor
) const
279 if( xFactor
== 1 && yFactor
== 1 )
284 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
286 // can't scale to/from 0 size
287 wxCHECK_MSG( (xFactor
> 0) && (yFactor
> 0), image
,
288 wxT("invalid new image size") );
290 long old_height
= M_IMGDATA
->m_height
,
291 old_width
= M_IMGDATA
->m_width
;
293 wxCHECK_MSG( (old_height
> 0) && (old_width
> 0), image
,
294 wxT("invalid old image size") );
296 long width
= old_width
/ xFactor
;
297 long height
= old_height
/ yFactor
;
299 image
.Create( width
, height
, false );
301 char unsigned *data
= image
.GetData();
303 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
305 bool hasMask
= false ;
306 unsigned char maskRed
= 0;
307 unsigned char maskGreen
= 0;
308 unsigned char maskBlue
=0 ;
310 unsigned char *source_data
= M_IMGDATA
->m_data
;
311 unsigned char *target_data
= data
;
312 unsigned char *source_alpha
= 0 ;
313 unsigned char *target_alpha
= 0 ;
314 if (M_IMGDATA
->m_hasMask
)
317 maskRed
= M_IMGDATA
->m_maskRed
;
318 maskGreen
= M_IMGDATA
->m_maskGreen
;
319 maskBlue
=M_IMGDATA
->m_maskBlue
;
321 image
.SetMaskColour( M_IMGDATA
->m_maskRed
,
322 M_IMGDATA
->m_maskGreen
,
323 M_IMGDATA
->m_maskBlue
);
327 source_alpha
= M_IMGDATA
->m_alpha
;
331 target_alpha
= image
.GetAlpha() ;
335 for (long y
= 0; y
< height
; y
++)
337 for (long x
= 0; x
< width
; x
++)
339 unsigned long avgRed
= 0 ;
340 unsigned long avgGreen
= 0;
341 unsigned long avgBlue
= 0;
342 unsigned long avgAlpha
= 0 ;
343 unsigned long counter
= 0 ;
345 for ( int y1
= 0 ; y1
< yFactor
; ++y1
)
347 long y_offset
= (y
* yFactor
+ y1
) * old_width
;
348 for ( int x1
= 0 ; x1
< xFactor
; ++x1
)
350 unsigned char *pixel
= source_data
+ 3 * ( y_offset
+ x
* xFactor
+ x1
) ;
351 unsigned char red
= pixel
[0] ;
352 unsigned char green
= pixel
[1] ;
353 unsigned char blue
= pixel
[2] ;
354 unsigned char alpha
= 255 ;
356 alpha
= *(source_alpha
+ y_offset
+ x
* xFactor
+ x1
) ;
357 if ( !hasMask
|| red
!= maskRed
|| green
!= maskGreen
|| blue
!= maskBlue
)
372 *(target_data
++) = M_IMGDATA
->m_maskRed
;
373 *(target_data
++) = M_IMGDATA
->m_maskGreen
;
374 *(target_data
++) = M_IMGDATA
->m_maskBlue
;
379 *(target_alpha
++) = (unsigned char)(avgAlpha
/ counter
) ;
380 *(target_data
++) = (unsigned char)(avgRed
/ counter
);
381 *(target_data
++) = (unsigned char)(avgGreen
/ counter
);
382 *(target_data
++) = (unsigned char)(avgBlue
/ counter
);
387 // In case this is a cursor, make sure the hotspot is scalled accordingly:
388 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
) )
389 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
,
390 (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X
))/xFactor
);
391 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
) )
392 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
,
393 (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y
))/yFactor
);
398 wxImage
wxImage::Scale( int width
, int height
) const
402 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
404 // can't scale to/from 0 size
405 wxCHECK_MSG( (width
> 0) && (height
> 0), image
,
406 wxT("invalid new image size") );
408 long old_height
= M_IMGDATA
->m_height
,
409 old_width
= M_IMGDATA
->m_width
;
410 wxCHECK_MSG( (old_height
> 0) && (old_width
> 0), image
,
411 wxT("invalid old image size") );
413 if ( old_width
% width
== 0 && old_width
>= width
&&
414 old_height
% height
== 0 && old_height
>= height
)
416 return ShrinkBy( old_width
/ width
, old_height
/ height
) ;
418 image
.Create( width
, height
, false );
420 unsigned char *data
= image
.GetData();
422 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
424 unsigned char *source_data
= M_IMGDATA
->m_data
;
425 unsigned char *target_data
= data
;
426 unsigned char *source_alpha
= 0 ;
427 unsigned char *target_alpha
= 0 ;
429 if (M_IMGDATA
->m_hasMask
)
431 image
.SetMaskColour( M_IMGDATA
->m_maskRed
,
432 M_IMGDATA
->m_maskGreen
,
433 M_IMGDATA
->m_maskBlue
);
437 source_alpha
= M_IMGDATA
->m_alpha
;
441 target_alpha
= image
.GetAlpha() ;
445 long x_delta
= (old_width
<<16) / width
;
446 long y_delta
= (old_height
<<16) / height
;
448 unsigned char* dest_pixel
= target_data
;
451 for ( long j
= 0; j
< height
; j
++ )
453 unsigned char* src_line
= &source_data
[(y
>>16)*old_width
*3];
454 unsigned char* src_alpha_line
= source_alpha
? &source_alpha
[(y
>>16)*old_width
] : 0 ;
457 for ( long i
= 0; i
< width
; i
++ )
459 unsigned char* src_pixel
= &src_line
[(x
>>16)*3];
460 unsigned char* src_alpha_pixel
= source_alpha
? &src_alpha_line
[(x
>>16)] : 0 ;
461 dest_pixel
[0] = src_pixel
[0];
462 dest_pixel
[1] = src_pixel
[1];
463 dest_pixel
[2] = src_pixel
[2];
466 *(target_alpha
++) = *src_alpha_pixel
;
473 // In case this is a cursor, make sure the hotspot is scalled accordingly:
474 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
) )
475 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
,
476 (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X
)*width
)/old_width
);
477 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
) )
478 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
,
479 (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y
)*height
)/old_height
);
484 wxImage
wxImage::Rotate90( bool clockwise
) const
488 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
490 image
.Create( M_IMGDATA
->m_height
, M_IMGDATA
->m_width
, false );
492 unsigned char *data
= image
.GetData();
494 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
496 if (M_IMGDATA
->m_hasMask
)
497 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
499 long height
= M_IMGDATA
->m_height
;
500 long width
= M_IMGDATA
->m_width
;
502 unsigned char *source_data
= M_IMGDATA
->m_data
;
503 unsigned char *target_data
;
505 for (long j
= 0; j
< height
; j
++)
507 for (long i
= 0; i
< width
; i
++)
510 target_data
= data
+ (((i
+1)*height
) - j
- 1)*3;
512 target_data
= data
+ ((height
*(width
-1)) + j
- (i
*height
))*3;
513 memcpy( target_data
, source_data
, 3 );
521 wxImage
wxImage::Mirror( bool horizontally
) const
525 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
527 image
.Create( M_IMGDATA
->m_width
, M_IMGDATA
->m_height
, false );
529 unsigned char *data
= image
.GetData();
531 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
533 if (M_IMGDATA
->m_hasMask
)
534 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
536 long height
= M_IMGDATA
->m_height
;
537 long width
= M_IMGDATA
->m_width
;
539 unsigned char *source_data
= M_IMGDATA
->m_data
;
540 unsigned char *target_data
;
544 for (long j
= 0; j
< height
; j
++)
547 target_data
= data
-3;
548 for (long i
= 0; i
< width
; i
++)
550 memcpy( target_data
, source_data
, 3 );
558 for (long i
= 0; i
< height
; i
++)
560 target_data
= data
+ 3*width
*(height
-1-i
);
561 memcpy( target_data
, source_data
, (size_t)3*width
);
562 source_data
+= 3*width
;
569 wxImage
wxImage::GetSubImage( const wxRect
&rect
) const
573 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
575 wxCHECK_MSG( (rect
.GetLeft()>=0) && (rect
.GetTop()>=0) && (rect
.GetRight()<=GetWidth()) && (rect
.GetBottom()<=GetHeight()),
576 image
, wxT("invalid subimage size") );
578 int subwidth
=rect
.GetWidth();
579 const int subheight
=rect
.GetHeight();
581 image
.Create( subwidth
, subheight
, false );
583 unsigned char *subdata
= image
.GetData(), *data
=GetData();
585 wxCHECK_MSG( subdata
, image
, wxT("unable to create image") );
587 if (M_IMGDATA
->m_hasMask
)
588 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
590 const int subleft
=3*rect
.GetLeft();
591 const int width
=3*GetWidth();
594 data
+=rect
.GetTop()*width
+subleft
;
596 for (long j
= 0; j
< subheight
; ++j
)
598 memcpy( subdata
, data
, subwidth
);
606 wxImage
wxImage::Size( const wxSize
& size
, const wxPoint
& pos
,
607 int r_
, int g_
, int b_
) const
611 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
612 wxCHECK_MSG( (size
.GetWidth() > 0) && (size
.GetHeight() > 0), image
, wxT("invalid size") );
614 int width
= GetWidth(), height
= GetHeight();
615 image
.Create(size
.GetWidth(), size
.GetHeight(), false);
617 unsigned char r
= (unsigned char)r_
;
618 unsigned char g
= (unsigned char)g_
;
619 unsigned char b
= (unsigned char)b_
;
620 if ((r_
== -1) && (g_
== -1) && (b_
== -1))
622 GetOrFindMaskColour( &r
, &g
, &b
);
623 image
.SetMaskColour(r
, g
, b
);
626 image
.SetRGB(wxRect(), r
, g
, b
);
628 wxRect
subRect(pos
.x
, pos
.y
, width
, height
);
629 wxRect
finalRect(0, 0, size
.GetWidth(), size
.GetHeight());
631 subRect
.Intersect(finalRect
);
633 if (!subRect
.IsEmpty())
635 if ((subRect
.GetWidth() == width
) && (subRect
.GetHeight() == height
))
636 image
.Paste(*this, pos
.x
, pos
.y
);
638 image
.Paste(GetSubImage(subRect
), pos
.x
, pos
.y
);
644 void wxImage::Paste( const wxImage
&image
, int x
, int y
)
646 wxCHECK_RET( Ok(), wxT("invalid image") );
647 wxCHECK_RET( image
.Ok(), wxT("invalid image") );
651 int width
= image
.GetWidth();
652 int height
= image
.GetHeight();
665 if ((x
+xx
)+width
> M_IMGDATA
->m_width
)
666 width
= M_IMGDATA
->m_width
- (x
+xx
);
667 if ((y
+yy
)+height
> M_IMGDATA
->m_height
)
668 height
= M_IMGDATA
->m_height
- (y
+yy
);
670 if (width
< 1) return;
671 if (height
< 1) return;
673 if ((!HasMask() && !image
.HasMask()) ||
674 (HasMask() && !image
.HasMask()) ||
675 ((HasMask() && image
.HasMask() &&
676 (GetMaskRed()==image
.GetMaskRed()) &&
677 (GetMaskGreen()==image
.GetMaskGreen()) &&
678 (GetMaskBlue()==image
.GetMaskBlue()))))
681 unsigned char* source_data
= image
.GetData() + xx
*3 + yy
*3*image
.GetWidth();
682 int source_step
= image
.GetWidth()*3;
684 unsigned char* target_data
= GetData() + (x
+xx
)*3 + (y
+yy
)*3*M_IMGDATA
->m_width
;
685 int target_step
= M_IMGDATA
->m_width
*3;
686 for (int j
= 0; j
< height
; j
++)
688 memcpy( target_data
, source_data
, width
);
689 source_data
+= source_step
;
690 target_data
+= target_step
;
695 if (!HasMask() && image
.HasMask())
697 unsigned char r
= image
.GetMaskRed();
698 unsigned char g
= image
.GetMaskGreen();
699 unsigned char b
= image
.GetMaskBlue();
702 unsigned char* source_data
= image
.GetData() + xx
*3 + yy
*3*image
.GetWidth();
703 int source_step
= image
.GetWidth()*3;
705 unsigned char* target_data
= GetData() + (x
+xx
)*3 + (y
+yy
)*3*M_IMGDATA
->m_width
;
706 int target_step
= M_IMGDATA
->m_width
*3;
708 for (int j
= 0; j
< height
; j
++)
710 for (int i
= 0; i
< width
; i
+=3)
712 if ((source_data
[i
] != r
) &&
713 (source_data
[i
+1] != g
) &&
714 (source_data
[i
+2] != b
))
716 memcpy( target_data
+i
, source_data
+i
, 3 );
719 source_data
+= source_step
;
720 target_data
+= target_step
;
725 void wxImage::Replace( unsigned char r1
, unsigned char g1
, unsigned char b1
,
726 unsigned char r2
, unsigned char g2
, unsigned char b2
)
728 wxCHECK_RET( Ok(), wxT("invalid image") );
730 unsigned char *data
= GetData();
732 const int w
= GetWidth();
733 const int h
= GetHeight();
735 for (int j
= 0; j
< h
; j
++)
736 for (int i
= 0; i
< w
; i
++)
738 if ((data
[0] == r1
) && (data
[1] == g1
) && (data
[2] == b1
))
748 wxImage
wxImage::ConvertToMono( unsigned char r
, unsigned char g
, unsigned char b
) const
752 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
754 image
.Create( M_IMGDATA
->m_width
, M_IMGDATA
->m_height
, false );
756 unsigned char *data
= image
.GetData();
758 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
760 if (M_IMGDATA
->m_hasMask
)
762 if (M_IMGDATA
->m_maskRed
== r
&& M_IMGDATA
->m_maskGreen
== g
&&
763 M_IMGDATA
->m_maskBlue
== b
)
764 image
.SetMaskColour( 255, 255, 255 );
766 image
.SetMaskColour( 0, 0, 0 );
769 long size
= M_IMGDATA
->m_height
* M_IMGDATA
->m_width
;
771 unsigned char *srcd
= M_IMGDATA
->m_data
;
772 unsigned char *tard
= image
.GetData();
774 for ( long i
= 0; i
< size
; i
++, srcd
+= 3, tard
+= 3 )
776 if (srcd
[0] == r
&& srcd
[1] == g
&& srcd
[2] == b
)
777 tard
[0] = tard
[1] = tard
[2] = 255;
779 tard
[0] = tard
[1] = tard
[2] = 0;
785 void wxImage::SetRGB( int x
, int y
, unsigned char r
, unsigned char g
, unsigned char b
)
787 wxCHECK_RET( Ok(), wxT("invalid image") );
789 int w
= M_IMGDATA
->m_width
;
790 int h
= M_IMGDATA
->m_height
;
792 wxCHECK_RET( (x
>=0) && (y
>=0) && (x
<w
) && (y
<h
), wxT("invalid image index") );
794 long pos
= (y
* w
+ x
) * 3;
796 M_IMGDATA
->m_data
[ pos
] = r
;
797 M_IMGDATA
->m_data
[ pos
+1 ] = g
;
798 M_IMGDATA
->m_data
[ pos
+2 ] = b
;
801 void wxImage::SetRGB( const wxRect
& rect_
, unsigned char r
, unsigned char g
, unsigned char b
)
803 wxCHECK_RET( Ok(), wxT("invalid image") );
806 wxRect
imageRect(0, 0, GetWidth(), GetHeight());
807 if ( rect
== wxRect() )
813 wxCHECK_RET( imageRect
.Inside(rect
.GetTopLeft()) &&
814 imageRect
.Inside(rect
.GetBottomRight()),
815 wxT("invalid bounding rectangle") );
818 int x1
= rect
.GetLeft(),
820 x2
= rect
.GetRight() + 1,
821 y2
= rect
.GetBottom() + 1;
823 unsigned char *data
wxDUMMY_INITIALIZE(NULL
);
824 int x
, y
, width
= GetWidth();
825 for (y
= y1
; y
< y2
; y
++)
827 data
= M_IMGDATA
->m_data
+ (y
*width
+ x1
)*3;
828 for (x
= x1
; x
< x2
; x
++)
837 unsigned char wxImage::GetRed( int x
, int y
) const
839 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
841 int w
= M_IMGDATA
->m_width
;
842 int h
= M_IMGDATA
->m_height
;
844 wxCHECK_MSG( (x
>=0) && (y
>=0) && (x
<w
) && (y
<h
), 0, wxT("invalid image index") );
846 long pos
= (y
* w
+ x
) * 3;
848 return M_IMGDATA
->m_data
[pos
];
851 unsigned char wxImage::GetGreen( int x
, int y
) const
853 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
855 int w
= M_IMGDATA
->m_width
;
856 int h
= M_IMGDATA
->m_height
;
858 wxCHECK_MSG( (x
>=0) && (y
>=0) && (x
<w
) && (y
<h
), 0, wxT("invalid image index") );
860 long pos
= (y
* w
+ x
) * 3;
862 return M_IMGDATA
->m_data
[pos
+1];
865 unsigned char wxImage::GetBlue( int x
, int y
) const
867 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
869 int w
= M_IMGDATA
->m_width
;
870 int h
= M_IMGDATA
->m_height
;
872 wxCHECK_MSG( (x
>=0) && (y
>=0) && (x
<w
) && (y
<h
), 0, wxT("invalid image index") );
874 long pos
= (y
* w
+ x
) * 3;
876 return M_IMGDATA
->m_data
[pos
+2];
879 bool wxImage::Ok() const
881 // image of 0 width or height can't be considered ok - at least because it
882 // causes crashes in ConvertToBitmap() if we don't catch it in time
883 wxImageRefData
*data
= M_IMGDATA
;
884 return data
&& data
->m_ok
&& data
->m_width
&& data
->m_height
;
887 unsigned char *wxImage::GetData() const
889 wxCHECK_MSG( Ok(), (unsigned char *)NULL
, wxT("invalid image") );
891 return M_IMGDATA
->m_data
;
894 void wxImage::SetData( unsigned char *data
, bool static_data
)
896 wxCHECK_RET( Ok(), wxT("invalid image") );
898 wxImageRefData
*newRefData
= new wxImageRefData();
900 newRefData
->m_width
= M_IMGDATA
->m_width
;
901 newRefData
->m_height
= M_IMGDATA
->m_height
;
902 newRefData
->m_data
= data
;
903 newRefData
->m_ok
= true;
904 newRefData
->m_maskRed
= M_IMGDATA
->m_maskRed
;
905 newRefData
->m_maskGreen
= M_IMGDATA
->m_maskGreen
;
906 newRefData
->m_maskBlue
= M_IMGDATA
->m_maskBlue
;
907 newRefData
->m_hasMask
= M_IMGDATA
->m_hasMask
;
908 newRefData
->m_static
= static_data
;
912 m_refData
= newRefData
;
915 void wxImage::SetData( unsigned char *data
, int new_width
, int new_height
, bool static_data
)
917 wxImageRefData
*newRefData
= new wxImageRefData();
921 newRefData
->m_width
= new_width
;
922 newRefData
->m_height
= new_height
;
923 newRefData
->m_data
= data
;
924 newRefData
->m_ok
= true;
925 newRefData
->m_maskRed
= M_IMGDATA
->m_maskRed
;
926 newRefData
->m_maskGreen
= M_IMGDATA
->m_maskGreen
;
927 newRefData
->m_maskBlue
= M_IMGDATA
->m_maskBlue
;
928 newRefData
->m_hasMask
= M_IMGDATA
->m_hasMask
;
932 newRefData
->m_width
= new_width
;
933 newRefData
->m_height
= new_height
;
934 newRefData
->m_data
= data
;
935 newRefData
->m_ok
= true;
937 newRefData
->m_static
= static_data
;
941 m_refData
= newRefData
;
944 // ----------------------------------------------------------------------------
945 // alpha channel support
946 // ----------------------------------------------------------------------------
948 void wxImage::SetAlpha(int x
, int y
, unsigned char alpha
)
950 wxCHECK_RET( Ok() && HasAlpha(), wxT("invalid image or no alpha channel") );
952 int w
= M_IMGDATA
->m_width
,
953 h
= M_IMGDATA
->m_height
;
955 wxCHECK_RET( x
>=0 && y
>= 0 && x
< w
&& y
< h
, wxT("invalid image index") );
957 M_IMGDATA
->m_alpha
[y
*w
+ x
] = alpha
;
960 unsigned char wxImage::GetAlpha(int x
, int y
) const
962 wxCHECK_MSG( Ok() && HasAlpha(), 0, wxT("invalid image or no alpha channel") );
964 int w
= M_IMGDATA
->m_width
,
965 h
= M_IMGDATA
->m_height
;
967 wxCHECK_MSG( x
>=0 && y
>= 0 && x
< w
&& y
< h
, 0, wxT("invalid image index") );
969 return M_IMGDATA
->m_alpha
[y
*w
+ x
];
972 bool wxImage::ConvertColourToAlpha( unsigned char r
, unsigned char g
, unsigned char b
)
976 int w
= M_IMGDATA
->m_width
,
977 h
= M_IMGDATA
->m_height
;
979 unsigned char *alpha
= GetAlpha();
980 unsigned char *data
= GetData();
983 for (y
= 0; y
< h
; y
++)
984 for (x
= 0; x
< w
; x
++)
999 void wxImage::SetAlpha( unsigned char *alpha
, bool static_data
)
1001 wxCHECK_RET( Ok(), wxT("invalid image") );
1005 alpha
= (unsigned char *)malloc(M_IMGDATA
->m_width
*M_IMGDATA
->m_height
);
1008 free(M_IMGDATA
->m_alpha
);
1009 M_IMGDATA
->m_alpha
= alpha
;
1010 M_IMGDATA
->m_static
= static_data
;
1014 unsigned char *wxImage::GetAlpha() const
1016 wxCHECK_MSG( Ok(), (unsigned char *)NULL
, wxT("invalid image") );
1018 return M_IMGDATA
->m_alpha
;
1021 void wxImage::InitAlpha()
1023 wxCHECK_RET( !HasAlpha(), wxT("image already has an alpha channel") );
1025 // initialize memory for alpha channel
1028 unsigned char *alpha
= M_IMGDATA
->m_alpha
;
1029 const size_t lenAlpha
= M_IMGDATA
->m_width
* M_IMGDATA
->m_height
;
1031 static const unsigned char ALPHA_TRANSPARENT
= 0;
1032 static const unsigned char ALPHA_OPAQUE
= 0xff;
1035 // use the mask to initialize the alpha channel.
1036 const unsigned char * const alphaEnd
= alpha
+ lenAlpha
;
1038 const unsigned char mr
= M_IMGDATA
->m_maskRed
;
1039 const unsigned char mg
= M_IMGDATA
->m_maskGreen
;
1040 const unsigned char mb
= M_IMGDATA
->m_maskBlue
;
1041 for ( unsigned char *src
= M_IMGDATA
->m_data
;
1045 *alpha
= (src
[0] == mr
&& src
[1] == mg
&& src
[2] == mb
)
1050 M_IMGDATA
->m_hasMask
= false;
1054 // make the image fully opaque
1055 memset(alpha
, ALPHA_OPAQUE
, lenAlpha
);
1059 // ----------------------------------------------------------------------------
1061 // ----------------------------------------------------------------------------
1063 void wxImage::SetMaskColour( unsigned char r
, unsigned char g
, unsigned char b
)
1065 wxCHECK_RET( Ok(), wxT("invalid image") );
1067 M_IMGDATA
->m_maskRed
= r
;
1068 M_IMGDATA
->m_maskGreen
= g
;
1069 M_IMGDATA
->m_maskBlue
= b
;
1070 M_IMGDATA
->m_hasMask
= true;
1073 bool wxImage::GetOrFindMaskColour( unsigned char *r
, unsigned char *g
, unsigned char *b
) const
1075 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1077 if (M_IMGDATA
->m_hasMask
)
1079 if (r
) *r
= M_IMGDATA
->m_maskRed
;
1080 if (g
) *g
= M_IMGDATA
->m_maskGreen
;
1081 if (b
) *b
= M_IMGDATA
->m_maskBlue
;
1086 FindFirstUnusedColour(r
, g
, b
);
1091 unsigned char wxImage::GetMaskRed() const
1093 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
1095 return M_IMGDATA
->m_maskRed
;
1098 unsigned char wxImage::GetMaskGreen() const
1100 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
1102 return M_IMGDATA
->m_maskGreen
;
1105 unsigned char wxImage::GetMaskBlue() const
1107 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
1109 return M_IMGDATA
->m_maskBlue
;
1112 void wxImage::SetMask( bool mask
)
1114 wxCHECK_RET( Ok(), wxT("invalid image") );
1116 M_IMGDATA
->m_hasMask
= mask
;
1119 bool wxImage::HasMask() const
1121 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1123 return M_IMGDATA
->m_hasMask
;
1126 int wxImage::GetWidth() const
1128 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
1130 return M_IMGDATA
->m_width
;
1133 int wxImage::GetHeight() const
1135 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
1137 return M_IMGDATA
->m_height
;
1140 bool wxImage::SetMaskFromImage(const wxImage
& mask
,
1141 unsigned char mr
, unsigned char mg
, unsigned char mb
)
1143 // check that the images are the same size
1144 if ( (M_IMGDATA
->m_height
!= mask
.GetHeight() ) || (M_IMGDATA
->m_width
!= mask
.GetWidth () ) )
1146 wxLogError( _("Image and mask have different sizes.") );
1150 // find unused colour
1151 unsigned char r
,g
,b
;
1152 if (!FindFirstUnusedColour(&r
, &g
, &b
))
1154 wxLogError( _("No unused colour in image being masked.") );
1158 unsigned char *imgdata
= GetData();
1159 unsigned char *maskdata
= mask
.GetData();
1161 const int w
= GetWidth();
1162 const int h
= GetHeight();
1164 for (int j
= 0; j
< h
; j
++)
1166 for (int i
= 0; i
< w
; i
++)
1168 if ((maskdata
[0] == mr
) && (maskdata
[1] == mg
) && (maskdata
[2] == mb
))
1179 SetMaskColour(r
, g
, b
);
1185 bool wxImage::ConvertAlphaToMask(unsigned char threshold
)
1190 unsigned char mr
, mg
, mb
;
1191 if (!FindFirstUnusedColour(&mr
, &mg
, &mb
))
1193 wxLogError( _("No unused colour in image being masked.") );
1198 SetMaskColour(mr
, mg
, mb
);
1200 unsigned char *imgdata
= GetData();
1201 unsigned char *alphadata
= GetAlpha();
1204 int h
= GetHeight();
1206 for (int y
= 0; y
< h
; y
++)
1208 for (int x
= 0; x
< w
; x
++, imgdata
+= 3, alphadata
++)
1210 if (*alphadata
< threshold
)
1219 free(M_IMGDATA
->m_alpha
);
1220 M_IMGDATA
->m_alpha
= NULL
;
1227 // Palette functions
1229 bool wxImage::HasPalette() const
1234 return M_IMGDATA
->m_palette
.Ok();
1237 const wxPalette
& wxImage::GetPalette() const
1239 wxCHECK_MSG( Ok(), wxNullPalette
, wxT("invalid image") );
1241 return M_IMGDATA
->m_palette
;
1244 void wxImage::SetPalette(const wxPalette
& palette
)
1246 wxCHECK_RET( Ok(), wxT("invalid image") );
1248 M_IMGDATA
->m_palette
= palette
;
1251 #endif // wxUSE_PALETTE
1253 // Option functions (arbitrary name/value mapping)
1254 void wxImage::SetOption(const wxString
& name
, const wxString
& value
)
1256 wxCHECK_RET( Ok(), wxT("invalid image") );
1258 int idx
= M_IMGDATA
->m_optionNames
.Index(name
, false);
1259 if (idx
== wxNOT_FOUND
)
1261 M_IMGDATA
->m_optionNames
.Add(name
);
1262 M_IMGDATA
->m_optionValues
.Add(value
);
1266 M_IMGDATA
->m_optionNames
[idx
] = name
;
1267 M_IMGDATA
->m_optionValues
[idx
] = value
;
1271 void wxImage::SetOption(const wxString
& name
, int value
)
1274 valStr
.Printf(wxT("%d"), value
);
1275 SetOption(name
, valStr
);
1278 wxString
wxImage::GetOption(const wxString
& name
) const
1280 wxCHECK_MSG( Ok(), wxEmptyString
, wxT("invalid image") );
1282 int idx
= M_IMGDATA
->m_optionNames
.Index(name
, false);
1283 if (idx
== wxNOT_FOUND
)
1284 return wxEmptyString
;
1286 return M_IMGDATA
->m_optionValues
[idx
];
1289 int wxImage::GetOptionInt(const wxString
& name
) const
1291 return wxAtoi(GetOption(name
));
1294 bool wxImage::HasOption(const wxString
& name
) const
1296 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1298 return (M_IMGDATA
->m_optionNames
.Index(name
, false) != wxNOT_FOUND
);
1301 bool wxImage::LoadFile( const wxString
& filename
, long type
, int index
)
1304 if (wxFileExists(filename
))
1306 wxFileInputStream
stream(filename
);
1307 wxBufferedInputStream
bstream( stream
);
1308 return LoadFile(bstream
, type
, index
);
1312 wxLogError( _("Can't load image from file '%s': file does not exist."), filename
.c_str() );
1316 #else // !wxUSE_STREAMS
1318 #endif // wxUSE_STREAMS
1321 bool wxImage::LoadFile( const wxString
& filename
, const wxString
& mimetype
, int index
)
1324 if (wxFileExists(filename
))
1326 wxFileInputStream
stream(filename
);
1327 wxBufferedInputStream
bstream( stream
);
1328 return LoadFile(bstream
, mimetype
, index
);
1332 wxLogError( _("Can't load image from file '%s': file does not exist."), filename
.c_str() );
1336 #else // !wxUSE_STREAMS
1338 #endif // wxUSE_STREAMS
1343 bool wxImage::SaveFile( const wxString
& filename
) const
1345 wxString ext
= filename
.AfterLast('.').Lower();
1347 wxImageHandler
* pHandler
= FindHandler(ext
, -1);
1350 SaveFile(filename
, pHandler
->GetType());
1354 wxLogError(_("Can't save image to file '%s': unknown extension."), filename
.c_str());
1359 bool wxImage::SaveFile( const wxString
& filename
, int type
) const
1362 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1364 ((wxImage
*)this)->SetOption(wxIMAGE_OPTION_FILENAME
, filename
);
1366 wxFileOutputStream
stream(filename
);
1368 if ( stream
.IsOk() )
1370 wxBufferedOutputStream
bstream( stream
);
1371 return SaveFile(bstream
, type
);
1373 #endif // wxUSE_STREAMS
1378 bool wxImage::SaveFile( const wxString
& filename
, const wxString
& mimetype
) const
1381 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1383 ((wxImage
*)this)->SetOption(wxIMAGE_OPTION_FILENAME
, filename
);
1385 wxFileOutputStream
stream(filename
);
1387 if ( stream
.IsOk() )
1389 wxBufferedOutputStream
bstream( stream
);
1390 return SaveFile(bstream
, mimetype
);
1392 #endif // wxUSE_STREAMS
1397 bool wxImage::CanRead( const wxString
&name
)
1400 wxFileInputStream
stream(name
);
1401 return CanRead(stream
);
1407 int wxImage::GetImageCount( const wxString
&name
, long type
)
1410 wxFileInputStream
stream(name
);
1412 return GetImageCount(stream
, type
);
1420 bool wxImage::CanRead( wxInputStream
&stream
)
1422 const wxList
& list
= GetHandlers();
1424 for ( wxList::compatibility_iterator node
= list
.GetFirst(); node
; node
= node
->GetNext() )
1426 wxImageHandler
*handler
=(wxImageHandler
*)node
->GetData();
1427 if (handler
->CanRead( stream
))
1434 int wxImage::GetImageCount( wxInputStream
&stream
, long type
)
1436 wxImageHandler
*handler
;
1438 if ( type
== wxBITMAP_TYPE_ANY
)
1440 wxList
&list
=GetHandlers();
1442 for (wxList::compatibility_iterator node
= list
.GetFirst(); node
; node
= node
->GetNext())
1444 handler
=(wxImageHandler
*)node
->GetData();
1445 if ( handler
->CanRead(stream
) )
1446 return handler
->GetImageCount(stream
);
1450 wxLogWarning(_("No handler found for image type."));
1454 handler
= FindHandler(type
);
1458 wxLogWarning(_("No image handler for type %d defined."), type
);
1462 if ( handler
->CanRead(stream
) )
1464 return handler
->GetImageCount(stream
);
1468 wxLogError(_("Image file is not of type %d."), type
);
1473 bool wxImage::LoadFile( wxInputStream
& stream
, long type
, int index
)
1477 m_refData
= new wxImageRefData
;
1479 wxImageHandler
*handler
;
1481 if ( type
== wxBITMAP_TYPE_ANY
)
1483 wxList
&list
=GetHandlers();
1485 for ( wxList::compatibility_iterator node
= list
.GetFirst(); node
; node
= node
->GetNext() )
1487 handler
=(wxImageHandler
*)node
->GetData();
1488 if ( handler
->CanRead(stream
) )
1489 return handler
->LoadFile(this, stream
, true/*verbose*/, index
);
1493 wxLogWarning( _("No handler found for image type.") );
1497 handler
= FindHandler(type
);
1501 wxLogWarning( _("No image handler for type %d defined."), type
);
1506 return handler
->LoadFile(this, stream
, true/*verbose*/, index
);
1509 bool wxImage::LoadFile( wxInputStream
& stream
, const wxString
& mimetype
, int index
)
1513 m_refData
= new wxImageRefData
;
1515 wxImageHandler
*handler
= FindHandlerMime(mimetype
);
1519 wxLogWarning( _("No image handler for type %s defined."), mimetype
.GetData() );
1524 return handler
->LoadFile( this, stream
, true/*verbose*/, index
);
1527 bool wxImage::SaveFile( wxOutputStream
& stream
, int type
) const
1529 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1531 wxImageHandler
*handler
= FindHandler(type
);
1534 wxLogWarning( _("No image handler for type %d defined."), type
);
1539 return handler
->SaveFile( (wxImage
*)this, stream
);
1542 bool wxImage::SaveFile( wxOutputStream
& stream
, const wxString
& mimetype
) const
1544 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1546 wxImageHandler
*handler
= FindHandlerMime(mimetype
);
1549 wxLogWarning( _("No image handler for type %s defined."), mimetype
.GetData() );
1554 return handler
->SaveFile( (wxImage
*)this, stream
);
1556 #endif // wxUSE_STREAMS
1558 void wxImage::AddHandler( wxImageHandler
*handler
)
1560 // Check for an existing handler of the type being added.
1561 if (FindHandler( handler
->GetType() ) == 0)
1563 sm_handlers
.Append( handler
);
1567 // This is not documented behaviour, merely the simplest 'fix'
1568 // for preventing duplicate additions. If someone ever has
1569 // a good reason to add and remove duplicate handlers (and they
1570 // may) we should probably refcount the duplicates.
1571 // also an issue in InsertHandler below.
1573 wxLogDebug( _T("Adding duplicate image handler for '%s'"),
1574 handler
->GetName().c_str() );
1579 void wxImage::InsertHandler( wxImageHandler
*handler
)
1581 // Check for an existing handler of the type being added.
1582 if (FindHandler( handler
->GetType() ) == 0)
1584 sm_handlers
.Insert( handler
);
1588 // see AddHandler for additional comments.
1589 wxLogDebug( _T("Inserting duplicate image handler for '%s'"),
1590 handler
->GetName().c_str() );
1595 bool wxImage::RemoveHandler( const wxString
& name
)
1597 wxImageHandler
*handler
= FindHandler(name
);
1600 sm_handlers
.DeleteObject(handler
);
1608 wxImageHandler
*wxImage::FindHandler( const wxString
& name
)
1610 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
1613 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
1614 if (handler
->GetName().Cmp(name
) == 0) return handler
;
1616 node
= node
->GetNext();
1621 wxImageHandler
*wxImage::FindHandler( const wxString
& extension
, long bitmapType
)
1623 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
1626 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
1627 if ( (handler
->GetExtension().Cmp(extension
) == 0) &&
1628 (bitmapType
== -1 || handler
->GetType() == bitmapType
) )
1630 node
= node
->GetNext();
1635 wxImageHandler
*wxImage::FindHandler( long bitmapType
)
1637 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
1640 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
1641 if (handler
->GetType() == bitmapType
) return handler
;
1642 node
= node
->GetNext();
1647 wxImageHandler
*wxImage::FindHandlerMime( const wxString
& mimetype
)
1649 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
1652 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
1653 if (handler
->GetMimeType().IsSameAs(mimetype
, false)) return handler
;
1654 node
= node
->GetNext();
1659 void wxImage::InitStandardHandlers()
1662 AddHandler(new wxBMPHandler
);
1663 #endif // wxUSE_STREAMS
1666 void wxImage::CleanUpHandlers()
1668 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
1671 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
1672 wxList::compatibility_iterator next
= node
->GetNext();
1677 sm_handlers
.Clear();
1680 wxString
wxImage::GetImageExtWildcard()
1684 wxList
& Handlers
= wxImage::GetHandlers();
1685 wxList::compatibility_iterator Node
= Handlers
.GetFirst();
1688 wxImageHandler
* Handler
= (wxImageHandler
*)Node
->GetData();
1689 fmts
+= wxT("*.") + Handler
->GetExtension();
1690 Node
= Node
->GetNext();
1691 if ( Node
) fmts
+= wxT(";");
1694 return wxT("(") + fmts
+ wxT(")|") + fmts
;
1697 //-----------------------------------------------------------------------------
1699 //-----------------------------------------------------------------------------
1701 IMPLEMENT_ABSTRACT_CLASS(wxImageHandler
,wxObject
)
1704 bool wxImageHandler::LoadFile( wxImage
*WXUNUSED(image
), wxInputStream
& WXUNUSED(stream
), bool WXUNUSED(verbose
), int WXUNUSED(index
) )
1709 bool wxImageHandler::SaveFile( wxImage
*WXUNUSED(image
), wxOutputStream
& WXUNUSED(stream
), bool WXUNUSED(verbose
) )
1714 int wxImageHandler::GetImageCount( wxInputStream
& WXUNUSED(stream
) )
1719 bool wxImageHandler::CanRead( const wxString
& name
)
1721 if (wxFileExists(name
))
1723 wxFileInputStream
stream(name
);
1724 return CanRead(stream
);
1727 wxLogError( _("Can't check image format of file '%s': file does not exist."), name
.c_str() );
1732 bool wxImageHandler::CallDoCanRead(wxInputStream
& stream
)
1734 wxFileOffset posOld
= stream
.TellI();
1735 if ( posOld
== wxInvalidOffset
)
1737 // can't test unseekable stream
1741 bool ok
= DoCanRead(stream
);
1743 // restore the old position to be able to test other formats and so on
1744 if ( stream
.SeekI(posOld
) == wxInvalidOffset
)
1746 wxLogDebug(_T("Failed to rewind the stream in wxImageHandler!"));
1748 // reading would fail anyhow as we're not at the right position
1755 #endif // wxUSE_STREAMS
1757 // ----------------------------------------------------------------------------
1758 // image histogram stuff
1759 // ----------------------------------------------------------------------------
1762 wxImageHistogram::FindFirstUnusedColour(unsigned char *r
,
1767 unsigned char g2
) const
1769 unsigned long key
= MakeKey(r2
, g2
, b2
);
1771 while ( find(key
) != end() )
1773 // color already used
1785 wxLogError(_("No unused colour in image.") );
1791 key
= MakeKey(r2
, g2
, b2
);
1805 wxImage::FindFirstUnusedColour(unsigned char *r
,
1810 unsigned char g2
) const
1812 wxImageHistogram histogram
;
1814 ComputeHistogram(histogram
);
1816 return histogram
.FindFirstUnusedColour(r
, g
, b
, r2
, g2
, b2
);
1822 // Counts and returns the number of different colours. Optionally stops
1823 // when it exceeds 'stopafter' different colours. This is useful, for
1824 // example, to see if the image can be saved as 8-bit (256 colour or
1825 // less, in this case it would be invoked as CountColours(256)). Default
1826 // value for stopafter is -1 (don't care).
1828 unsigned long wxImage::CountColours( unsigned long stopafter
) const
1832 unsigned char r
, g
, b
;
1834 unsigned long size
, nentries
, key
;
1837 size
= GetWidth() * GetHeight();
1840 for (unsigned long j
= 0; (j
< size
) && (nentries
<= stopafter
) ; j
++)
1845 key
= wxImageHistogram::MakeKey(r
, g
, b
);
1847 if (h
.Get(key
) == NULL
)
1858 unsigned long wxImage::ComputeHistogram( wxImageHistogram
&h
) const
1860 unsigned char *p
= GetData();
1861 unsigned long nentries
= 0;
1865 const unsigned long size
= GetWidth() * GetHeight();
1867 unsigned char r
, g
, b
;
1868 for ( unsigned long n
= 0; n
< size
; n
++ )
1874 wxImageHistogramEntry
& entry
= h
[wxImageHistogram::MakeKey(r
, g
, b
)];
1876 if ( entry
.value
++ == 0 )
1877 entry
.index
= nentries
++;
1884 * Rotation code by Carlos Moreno
1887 // GRG: I've removed wxRotationPoint - we already have wxRealPoint which
1888 // does exactly the same thing. And I also got rid of wxRotationPixel
1889 // bacause of potential problems in architectures where alignment
1890 // is an issue, so I had to rewrite parts of the code.
1892 static const double gs_Epsilon
= 1e-10;
1894 static inline int wxCint (double x
)
1896 return (x
> 0) ? (int) (x
+ 0.5) : (int) (x
- 0.5);
1900 // Auxiliary function to rotate a point (x,y) with respect to point p0
1901 // make it inline and use a straight return to facilitate optimization
1902 // also, the function receives the sine and cosine of the angle to avoid
1903 // repeating the time-consuming calls to these functions -- sin/cos can
1904 // be computed and stored in the calling function.
1906 inline wxRealPoint
rotated_point (const wxRealPoint
& p
, double cos_angle
, double sin_angle
, const wxRealPoint
& p0
)
1908 return wxRealPoint (p0
.x
+ (p
.x
- p0
.x
) * cos_angle
- (p
.y
- p0
.y
) * sin_angle
,
1909 p0
.y
+ (p
.y
- p0
.y
) * cos_angle
+ (p
.x
- p0
.x
) * sin_angle
);
1912 inline wxRealPoint
rotated_point (double x
, double y
, double cos_angle
, double sin_angle
, const wxRealPoint
& p0
)
1914 return rotated_point (wxRealPoint(x
,y
), cos_angle
, sin_angle
, p0
);
1917 wxImage
wxImage::Rotate(double angle
, const wxPoint
& centre_of_rotation
, bool interpolating
, wxPoint
* offset_after_rotation
) const
1920 angle
= -angle
; // screen coordinates are a mirror image of "real" coordinates
1922 bool has_alpha
= HasAlpha();
1924 // Create pointer-based array to accelerate access to wxImage's data
1925 unsigned char ** data
= new unsigned char * [GetHeight()];
1926 data
[0] = GetData();
1927 for (i
= 1; i
< GetHeight(); i
++)
1928 data
[i
] = data
[i
- 1] + (3 * GetWidth());
1930 // Same for alpha channel
1931 unsigned char ** alpha
= NULL
;
1934 alpha
= new unsigned char * [GetHeight()];
1935 alpha
[0] = GetAlpha();
1936 for (i
= 1; i
< GetHeight(); i
++)
1937 alpha
[i
] = alpha
[i
- 1] + GetWidth();
1940 // precompute coefficients for rotation formula
1941 // (sine and cosine of the angle)
1942 const double cos_angle
= cos(angle
);
1943 const double sin_angle
= sin(angle
);
1945 // Create new Image to store the result
1946 // First, find rectangle that covers the rotated image; to do that,
1947 // rotate the four corners
1949 const wxRealPoint
p0(centre_of_rotation
.x
, centre_of_rotation
.y
);
1951 wxRealPoint p1
= rotated_point (0, 0, cos_angle
, sin_angle
, p0
);
1952 wxRealPoint p2
= rotated_point (0, GetHeight(), cos_angle
, sin_angle
, p0
);
1953 wxRealPoint p3
= rotated_point (GetWidth(), 0, cos_angle
, sin_angle
, p0
);
1954 wxRealPoint p4
= rotated_point (GetWidth(), GetHeight(), cos_angle
, sin_angle
, p0
);
1956 int x1
= (int) floor (wxMin (wxMin(p1
.x
, p2
.x
), wxMin(p3
.x
, p4
.x
)));
1957 int y1
= (int) floor (wxMin (wxMin(p1
.y
, p2
.y
), wxMin(p3
.y
, p4
.y
)));
1958 int x2
= (int) ceil (wxMax (wxMax(p1
.x
, p2
.x
), wxMax(p3
.x
, p4
.x
)));
1959 int y2
= (int) ceil (wxMax (wxMax(p1
.y
, p2
.y
), wxMax(p3
.y
, p4
.y
)));
1961 // Create rotated image
1962 wxImage
rotated (x2
- x1
+ 1, y2
- y1
+ 1, false);
1963 // With alpha channel
1967 if (offset_after_rotation
!= NULL
)
1969 *offset_after_rotation
= wxPoint (x1
, y1
);
1972 // GRG: The rotated (destination) image is always accessed
1973 // sequentially, so there is no need for a pointer-based
1974 // array here (and in fact it would be slower).
1976 unsigned char * dst
= rotated
.GetData();
1978 unsigned char * alpha_dst
= NULL
;
1980 alpha_dst
= rotated
.GetAlpha();
1982 // GRG: if the original image has a mask, use its RGB values
1983 // as the blank pixel, else, fall back to default (black).
1985 unsigned char blank_r
= 0;
1986 unsigned char blank_g
= 0;
1987 unsigned char blank_b
= 0;
1991 blank_r
= GetMaskRed();
1992 blank_g
= GetMaskGreen();
1993 blank_b
= GetMaskBlue();
1994 rotated
.SetMaskColour( blank_r
, blank_g
, blank_b
);
1997 // Now, for each point of the rotated image, find where it came from, by
1998 // performing an inverse rotation (a rotation of -angle) and getting the
1999 // pixel at those coordinates
2001 // GRG: I've taken the (interpolating) test out of the loops, so that
2002 // it is done only once, instead of repeating it for each pixel.
2007 for (int y
= 0; y
< rotated
.GetHeight(); y
++)
2009 for (x
= 0; x
< rotated
.GetWidth(); x
++)
2011 wxRealPoint src
= rotated_point (x
+ x1
, y
+ y1
, cos_angle
, -sin_angle
, p0
);
2013 if (-0.25 < src
.x
&& src
.x
< GetWidth() - 0.75 &&
2014 -0.25 < src
.y
&& src
.y
< GetHeight() - 0.75)
2016 // interpolate using the 4 enclosing grid-points. Those
2017 // points can be obtained using floor and ceiling of the
2018 // exact coordinates of the point
2019 // C.M. 2000-02-17: when the point is near the border, special care is required.
2023 if (0 < src
.x
&& src
.x
< GetWidth() - 1)
2025 x1
= wxCint(floor(src
.x
));
2026 x2
= wxCint(ceil(src
.x
));
2028 else // else means that x is near one of the borders (0 or width-1)
2030 x1
= x2
= wxCint (src
.x
);
2033 if (0 < src
.y
&& src
.y
< GetHeight() - 1)
2035 y1
= wxCint(floor(src
.y
));
2036 y2
= wxCint(ceil(src
.y
));
2040 y1
= y2
= wxCint (src
.y
);
2043 // get four points and the distances (square of the distance,
2044 // for efficiency reasons) for the interpolation formula
2046 // GRG: Do not calculate the points until they are
2047 // really needed -- this way we can calculate
2048 // just one, instead of four, if d1, d2, d3
2049 // or d4 are < gs_Epsilon
2051 const double d1
= (src
.x
- x1
) * (src
.x
- x1
) + (src
.y
- y1
) * (src
.y
- y1
);
2052 const double d2
= (src
.x
- x2
) * (src
.x
- x2
) + (src
.y
- y1
) * (src
.y
- y1
);
2053 const double d3
= (src
.x
- x2
) * (src
.x
- x2
) + (src
.y
- y2
) * (src
.y
- y2
);
2054 const double d4
= (src
.x
- x1
) * (src
.x
- x1
) + (src
.y
- y2
) * (src
.y
- y2
);
2056 // Now interpolate as a weighted average of the four surrounding
2057 // points, where the weights are the distances to each of those points
2059 // If the point is exactly at one point of the grid of the source
2060 // image, then don't interpolate -- just assign the pixel
2062 if (d1
< gs_Epsilon
) // d1,d2,d3,d4 are positive -- no need for abs()
2064 unsigned char *p
= data
[y1
] + (3 * x1
);
2071 unsigned char *p
= alpha
[y1
] + x1
;
2072 *(alpha_dst
++) = *p
;
2075 else if (d2
< gs_Epsilon
)
2077 unsigned char *p
= data
[y1
] + (3 * x2
);
2084 unsigned char *p
= alpha
[y1
] + x2
;
2085 *(alpha_dst
++) = *p
;
2088 else if (d3
< gs_Epsilon
)
2090 unsigned char *p
= data
[y2
] + (3 * x2
);
2097 unsigned char *p
= alpha
[y2
] + x2
;
2098 *(alpha_dst
++) = *p
;
2101 else if (d4
< gs_Epsilon
)
2103 unsigned char *p
= data
[y2
] + (3 * x1
);
2110 unsigned char *p
= alpha
[y2
] + x1
;
2111 *(alpha_dst
++) = *p
;
2116 // weights for the weighted average are proportional to the inverse of the distance
2117 unsigned char *v1
= data
[y1
] + (3 * x1
);
2118 unsigned char *v2
= data
[y1
] + (3 * x2
);
2119 unsigned char *v3
= data
[y2
] + (3 * x2
);
2120 unsigned char *v4
= data
[y2
] + (3 * x1
);
2122 const double w1
= 1/d1
, w2
= 1/d2
, w3
= 1/d3
, w4
= 1/d4
;
2126 *(dst
++) = (unsigned char)
2127 ( (w1
* *(v1
++) + w2
* *(v2
++) +
2128 w3
* *(v3
++) + w4
* *(v4
++)) /
2129 (w1
+ w2
+ w3
+ w4
) );
2130 *(dst
++) = (unsigned char)
2131 ( (w1
* *(v1
++) + w2
* *(v2
++) +
2132 w3
* *(v3
++) + w4
* *(v4
++)) /
2133 (w1
+ w2
+ w3
+ w4
) );
2134 *(dst
++) = (unsigned char)
2135 ( (w1
* *v1
+ w2
* *v2
+
2136 w3
* *v3
+ w4
* *v4
) /
2137 (w1
+ w2
+ w3
+ w4
) );
2141 unsigned char *v1
= alpha
[y1
] + (x1
);
2142 unsigned char *v2
= alpha
[y1
] + (x2
);
2143 unsigned char *v3
= alpha
[y2
] + (x2
);
2144 unsigned char *v4
= alpha
[y2
] + (x1
);
2146 *(alpha_dst
++) = (unsigned char)
2147 ( (w1
* *v1
+ w2
* *v2
+
2148 w3
* *v3
+ w4
* *v4
) /
2149 (w1
+ w2
+ w3
+ w4
) );
2165 else // not interpolating
2167 for (int y
= 0; y
< rotated
.GetHeight(); y
++)
2169 for (x
= 0; x
< rotated
.GetWidth(); x
++)
2171 wxRealPoint src
= rotated_point (x
+ x1
, y
+ y1
, cos_angle
, -sin_angle
, p0
);
2173 const int xs
= wxCint (src
.x
); // wxCint rounds to the
2174 const int ys
= wxCint (src
.y
); // closest integer
2176 if (0 <= xs
&& xs
< GetWidth() &&
2177 0 <= ys
&& ys
< GetHeight())
2179 unsigned char *p
= data
[ys
] + (3 * xs
);
2186 unsigned char *p
= alpha
[ys
] + (xs
);
2187 *(alpha_dst
++) = *p
;
2197 *(alpha_dst
++) = 255;
2215 // A module to allow wxImage initialization/cleanup
2216 // without calling these functions from app.cpp or from
2217 // the user's application.
2219 class wxImageModule
: public wxModule
2221 DECLARE_DYNAMIC_CLASS(wxImageModule
)
2224 bool OnInit() { wxImage::InitStandardHandlers(); return true; };
2225 void OnExit() { wxImage::CleanUpHandlers(); };
2228 IMPLEMENT_DYNAMIC_CLASS(wxImageModule
, wxModule
)
2231 #endif // wxUSE_IMAGE