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
)
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
;
911 m_refData
= newRefData
;
914 void wxImage::SetData( unsigned char *data
, int new_width
, int new_height
)
916 wxImageRefData
*newRefData
= new wxImageRefData();
920 newRefData
->m_width
= new_width
;
921 newRefData
->m_height
= new_height
;
922 newRefData
->m_data
= data
;
923 newRefData
->m_ok
= true;
924 newRefData
->m_maskRed
= M_IMGDATA
->m_maskRed
;
925 newRefData
->m_maskGreen
= M_IMGDATA
->m_maskGreen
;
926 newRefData
->m_maskBlue
= M_IMGDATA
->m_maskBlue
;
927 newRefData
->m_hasMask
= M_IMGDATA
->m_hasMask
;
931 newRefData
->m_width
= new_width
;
932 newRefData
->m_height
= new_height
;
933 newRefData
->m_data
= data
;
934 newRefData
->m_ok
= true;
939 m_refData
= newRefData
;
942 // ----------------------------------------------------------------------------
943 // alpha channel support
944 // ----------------------------------------------------------------------------
946 void wxImage::SetAlpha(int x
, int y
, unsigned char alpha
)
948 wxCHECK_RET( Ok() && HasAlpha(), wxT("invalid image or no alpha channel") );
950 int w
= M_IMGDATA
->m_width
,
951 h
= M_IMGDATA
->m_height
;
953 wxCHECK_RET( x
>=0 && y
>= 0 && x
< w
&& y
< h
, wxT("invalid image index") );
955 M_IMGDATA
->m_alpha
[y
*w
+ x
] = alpha
;
958 unsigned char wxImage::GetAlpha(int x
, int y
) const
960 wxCHECK_MSG( Ok() && HasAlpha(), 0, wxT("invalid image or no alpha channel") );
962 int w
= M_IMGDATA
->m_width
,
963 h
= M_IMGDATA
->m_height
;
965 wxCHECK_MSG( x
>=0 && y
>= 0 && x
< w
&& y
< h
, 0, wxT("invalid image index") );
967 return M_IMGDATA
->m_alpha
[y
*w
+ x
];
970 bool wxImage::ConvertColourToAlpha( unsigned char r
, unsigned char g
, unsigned char b
)
974 int w
= M_IMGDATA
->m_width
,
975 h
= M_IMGDATA
->m_height
;
977 unsigned char *alpha
= GetAlpha();
978 unsigned char *data
= GetData();
981 for (y
= 0; y
< h
; y
++)
982 for (x
= 0; x
< w
; x
++)
997 void wxImage::SetAlpha( unsigned char *alpha
)
999 wxCHECK_RET( Ok(), wxT("invalid image") );
1003 alpha
= (unsigned char *)malloc(M_IMGDATA
->m_width
*M_IMGDATA
->m_height
);
1006 free(M_IMGDATA
->m_alpha
);
1007 M_IMGDATA
->m_alpha
= alpha
;
1010 unsigned char *wxImage::GetAlpha() const
1012 wxCHECK_MSG( Ok(), (unsigned char *)NULL
, wxT("invalid image") );
1014 return M_IMGDATA
->m_alpha
;
1017 void wxImage::InitAlpha()
1019 wxCHECK_RET( !HasAlpha(), wxT("image already has an alpha channel") );
1021 // initialize memory for alpha channel
1024 unsigned char *alpha
= M_IMGDATA
->m_alpha
;
1025 const size_t lenAlpha
= M_IMGDATA
->m_width
* M_IMGDATA
->m_height
;
1027 static const unsigned char ALPHA_TRANSPARENT
= 0;
1028 static const unsigned char ALPHA_OPAQUE
= 0xff;
1031 // use the mask to initialize the alpha channel.
1032 const unsigned char * const alphaEnd
= alpha
+ lenAlpha
;
1034 const unsigned char mr
= M_IMGDATA
->m_maskRed
;
1035 const unsigned char mg
= M_IMGDATA
->m_maskGreen
;
1036 const unsigned char mb
= M_IMGDATA
->m_maskBlue
;
1037 for ( unsigned char *src
= M_IMGDATA
->m_data
;
1041 *alpha
= (src
[0] == mr
&& src
[1] == mg
&& src
[2] == mb
)
1046 M_IMGDATA
->m_hasMask
= false;
1050 // make the image fully opaque
1051 memset(alpha
, ALPHA_OPAQUE
, lenAlpha
);
1055 // ----------------------------------------------------------------------------
1057 // ----------------------------------------------------------------------------
1059 void wxImage::SetMaskColour( unsigned char r
, unsigned char g
, unsigned char b
)
1061 wxCHECK_RET( Ok(), wxT("invalid image") );
1063 M_IMGDATA
->m_maskRed
= r
;
1064 M_IMGDATA
->m_maskGreen
= g
;
1065 M_IMGDATA
->m_maskBlue
= b
;
1066 M_IMGDATA
->m_hasMask
= true;
1069 bool wxImage::GetOrFindMaskColour( unsigned char *r
, unsigned char *g
, unsigned char *b
) const
1071 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1073 if (M_IMGDATA
->m_hasMask
)
1075 if (r
) *r
= M_IMGDATA
->m_maskRed
;
1076 if (g
) *g
= M_IMGDATA
->m_maskGreen
;
1077 if (b
) *b
= M_IMGDATA
->m_maskBlue
;
1082 FindFirstUnusedColour(r
, g
, b
);
1087 unsigned char wxImage::GetMaskRed() const
1089 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
1091 return M_IMGDATA
->m_maskRed
;
1094 unsigned char wxImage::GetMaskGreen() const
1096 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
1098 return M_IMGDATA
->m_maskGreen
;
1101 unsigned char wxImage::GetMaskBlue() const
1103 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
1105 return M_IMGDATA
->m_maskBlue
;
1108 void wxImage::SetMask( bool mask
)
1110 wxCHECK_RET( Ok(), wxT("invalid image") );
1112 M_IMGDATA
->m_hasMask
= mask
;
1115 bool wxImage::HasMask() const
1117 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1119 return M_IMGDATA
->m_hasMask
;
1122 int wxImage::GetWidth() const
1124 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
1126 return M_IMGDATA
->m_width
;
1129 int wxImage::GetHeight() const
1131 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
1133 return M_IMGDATA
->m_height
;
1136 bool wxImage::SetMaskFromImage(const wxImage
& mask
,
1137 unsigned char mr
, unsigned char mg
, unsigned char mb
)
1139 // check that the images are the same size
1140 if ( (M_IMGDATA
->m_height
!= mask
.GetHeight() ) || (M_IMGDATA
->m_width
!= mask
.GetWidth () ) )
1142 wxLogError( _("Image and mask have different sizes.") );
1146 // find unused colour
1147 unsigned char r
,g
,b
;
1148 if (!FindFirstUnusedColour(&r
, &g
, &b
))
1150 wxLogError( _("No unused colour in image being masked.") );
1154 unsigned char *imgdata
= GetData();
1155 unsigned char *maskdata
= mask
.GetData();
1157 const int w
= GetWidth();
1158 const int h
= GetHeight();
1160 for (int j
= 0; j
< h
; j
++)
1162 for (int i
= 0; i
< w
; i
++)
1164 if ((maskdata
[0] == mr
) && (maskdata
[1] == mg
) && (maskdata
[2] == mb
))
1175 SetMaskColour(r
, g
, b
);
1181 bool wxImage::ConvertAlphaToMask(unsigned char threshold
)
1186 unsigned char mr
, mg
, mb
;
1187 if (!FindFirstUnusedColour(&mr
, &mg
, &mb
))
1189 wxLogError( _("No unused colour in image being masked.") );
1194 SetMaskColour(mr
, mg
, mb
);
1196 unsigned char *imgdata
= GetData();
1197 unsigned char *alphadata
= GetAlpha();
1200 int h
= GetHeight();
1202 for (int y
= 0; y
< h
; y
++)
1204 for (int x
= 0; x
< w
; x
++, imgdata
+= 3, alphadata
++)
1206 if (*alphadata
< threshold
)
1215 free(M_IMGDATA
->m_alpha
);
1216 M_IMGDATA
->m_alpha
= NULL
;
1223 // Palette functions
1225 bool wxImage::HasPalette() const
1230 return M_IMGDATA
->m_palette
.Ok();
1233 const wxPalette
& wxImage::GetPalette() const
1235 wxCHECK_MSG( Ok(), wxNullPalette
, wxT("invalid image") );
1237 return M_IMGDATA
->m_palette
;
1240 void wxImage::SetPalette(const wxPalette
& palette
)
1242 wxCHECK_RET( Ok(), wxT("invalid image") );
1244 M_IMGDATA
->m_palette
= palette
;
1247 #endif // wxUSE_PALETTE
1249 // Option functions (arbitrary name/value mapping)
1250 void wxImage::SetOption(const wxString
& name
, const wxString
& value
)
1252 wxCHECK_RET( Ok(), wxT("invalid image") );
1254 int idx
= M_IMGDATA
->m_optionNames
.Index(name
, false);
1255 if (idx
== wxNOT_FOUND
)
1257 M_IMGDATA
->m_optionNames
.Add(name
);
1258 M_IMGDATA
->m_optionValues
.Add(value
);
1262 M_IMGDATA
->m_optionNames
[idx
] = name
;
1263 M_IMGDATA
->m_optionValues
[idx
] = value
;
1267 void wxImage::SetOption(const wxString
& name
, int value
)
1270 valStr
.Printf(wxT("%d"), value
);
1271 SetOption(name
, valStr
);
1274 wxString
wxImage::GetOption(const wxString
& name
) const
1276 wxCHECK_MSG( Ok(), wxEmptyString
, wxT("invalid image") );
1278 int idx
= M_IMGDATA
->m_optionNames
.Index(name
, false);
1279 if (idx
== wxNOT_FOUND
)
1280 return wxEmptyString
;
1282 return M_IMGDATA
->m_optionValues
[idx
];
1285 int wxImage::GetOptionInt(const wxString
& name
) const
1287 return wxAtoi(GetOption(name
));
1290 bool wxImage::HasOption(const wxString
& name
) const
1292 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1294 return (M_IMGDATA
->m_optionNames
.Index(name
, false) != wxNOT_FOUND
);
1297 bool wxImage::LoadFile( const wxString
& filename
, long type
, int index
)
1300 if (wxFileExists(filename
))
1302 wxFileInputStream
stream(filename
);
1303 wxBufferedInputStream
bstream( stream
);
1304 return LoadFile(bstream
, type
, index
);
1308 wxLogError( _("Can't load image from file '%s': file does not exist."), filename
.c_str() );
1312 #else // !wxUSE_STREAMS
1314 #endif // wxUSE_STREAMS
1317 bool wxImage::LoadFile( const wxString
& filename
, const wxString
& mimetype
, int index
)
1320 if (wxFileExists(filename
))
1322 wxFileInputStream
stream(filename
);
1323 wxBufferedInputStream
bstream( stream
);
1324 return LoadFile(bstream
, mimetype
, index
);
1328 wxLogError( _("Can't load image from file '%s': file does not exist."), filename
.c_str() );
1332 #else // !wxUSE_STREAMS
1334 #endif // wxUSE_STREAMS
1339 bool wxImage::SaveFile( const wxString
& filename
) const
1341 wxString ext
= filename
.AfterLast('.').Lower();
1343 wxImageHandler
* pHandler
= FindHandler(ext
, -1);
1346 SaveFile(filename
, pHandler
->GetType());
1350 wxLogError(_("Can't save image to file '%s': unknown extension."), filename
.c_str());
1355 bool wxImage::SaveFile( const wxString
& filename
, int type
) const
1358 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1360 ((wxImage
*)this)->SetOption(wxIMAGE_OPTION_FILENAME
, filename
);
1362 wxFileOutputStream
stream(filename
);
1364 if ( stream
.IsOk() )
1366 wxBufferedOutputStream
bstream( stream
);
1367 return SaveFile(bstream
, type
);
1369 #endif // wxUSE_STREAMS
1374 bool wxImage::SaveFile( const wxString
& filename
, const wxString
& mimetype
) const
1377 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1379 ((wxImage
*)this)->SetOption(wxIMAGE_OPTION_FILENAME
, filename
);
1381 wxFileOutputStream
stream(filename
);
1383 if ( stream
.IsOk() )
1385 wxBufferedOutputStream
bstream( stream
);
1386 return SaveFile(bstream
, mimetype
);
1388 #endif // wxUSE_STREAMS
1393 bool wxImage::CanRead( const wxString
&name
)
1396 wxFileInputStream
stream(name
);
1397 return CanRead(stream
);
1403 int wxImage::GetImageCount( const wxString
&name
, long type
)
1406 wxFileInputStream
stream(name
);
1408 return GetImageCount(stream
, type
);
1416 bool wxImage::CanRead( wxInputStream
&stream
)
1418 const wxList
& list
= GetHandlers();
1420 for ( wxList::compatibility_iterator node
= list
.GetFirst(); node
; node
= node
->GetNext() )
1422 wxImageHandler
*handler
=(wxImageHandler
*)node
->GetData();
1423 if (handler
->CanRead( stream
))
1430 int wxImage::GetImageCount( wxInputStream
&stream
, long type
)
1432 wxImageHandler
*handler
;
1434 if ( type
== wxBITMAP_TYPE_ANY
)
1436 wxList
&list
=GetHandlers();
1438 for (wxList::compatibility_iterator node
= list
.GetFirst(); node
; node
= node
->GetNext())
1440 handler
=(wxImageHandler
*)node
->GetData();
1441 if ( handler
->CanRead(stream
) )
1442 return handler
->GetImageCount(stream
);
1446 wxLogWarning(_("No handler found for image type."));
1450 handler
= FindHandler(type
);
1454 wxLogWarning(_("No image handler for type %d defined."), type
);
1458 if ( handler
->CanRead(stream
) )
1460 return handler
->GetImageCount(stream
);
1464 wxLogError(_("Image file is not of type %d."), type
);
1469 bool wxImage::LoadFile( wxInputStream
& stream
, long type
, int index
)
1473 m_refData
= new wxImageRefData
;
1475 wxImageHandler
*handler
;
1477 if ( type
== wxBITMAP_TYPE_ANY
)
1479 wxList
&list
=GetHandlers();
1481 for ( wxList::compatibility_iterator node
= list
.GetFirst(); node
; node
= node
->GetNext() )
1483 handler
=(wxImageHandler
*)node
->GetData();
1484 if ( handler
->CanRead(stream
) )
1485 return handler
->LoadFile(this, stream
, true/*verbose*/, index
);
1489 wxLogWarning( _("No handler found for image type.") );
1493 handler
= FindHandler(type
);
1497 wxLogWarning( _("No image handler for type %d defined."), type
);
1502 return handler
->LoadFile(this, stream
, true/*verbose*/, index
);
1505 bool wxImage::LoadFile( wxInputStream
& stream
, const wxString
& mimetype
, int index
)
1509 m_refData
= new wxImageRefData
;
1511 wxImageHandler
*handler
= FindHandlerMime(mimetype
);
1515 wxLogWarning( _("No image handler for type %s defined."), mimetype
.GetData() );
1520 return handler
->LoadFile( this, stream
, true/*verbose*/, index
);
1523 bool wxImage::SaveFile( wxOutputStream
& stream
, int type
) const
1525 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1527 wxImageHandler
*handler
= FindHandler(type
);
1530 wxLogWarning( _("No image handler for type %d defined."), type
);
1535 return handler
->SaveFile( (wxImage
*)this, stream
);
1538 bool wxImage::SaveFile( wxOutputStream
& stream
, const wxString
& mimetype
) const
1540 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1542 wxImageHandler
*handler
= FindHandlerMime(mimetype
);
1545 wxLogWarning( _("No image handler for type %s defined."), mimetype
.GetData() );
1550 return handler
->SaveFile( (wxImage
*)this, stream
);
1552 #endif // wxUSE_STREAMS
1554 void wxImage::AddHandler( wxImageHandler
*handler
)
1556 // Check for an existing handler of the type being added.
1557 if (FindHandler( handler
->GetType() ) == 0)
1559 sm_handlers
.Append( handler
);
1563 // This is not documented behaviour, merely the simplest 'fix'
1564 // for preventing duplicate additions. If someone ever has
1565 // a good reason to add and remove duplicate handlers (and they
1566 // may) we should probably refcount the duplicates.
1567 // also an issue in InsertHandler below.
1569 wxLogDebug( _T("Adding duplicate image handler for '%s'"),
1570 handler
->GetName().c_str() );
1575 void wxImage::InsertHandler( wxImageHandler
*handler
)
1577 // Check for an existing handler of the type being added.
1578 if (FindHandler( handler
->GetType() ) == 0)
1580 sm_handlers
.Insert( handler
);
1584 // see AddHandler for additional comments.
1585 wxLogDebug( _T("Inserting duplicate image handler for '%s'"),
1586 handler
->GetName().c_str() );
1591 bool wxImage::RemoveHandler( const wxString
& name
)
1593 wxImageHandler
*handler
= FindHandler(name
);
1596 sm_handlers
.DeleteObject(handler
);
1604 wxImageHandler
*wxImage::FindHandler( const wxString
& name
)
1606 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
1609 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
1610 if (handler
->GetName().Cmp(name
) == 0) return handler
;
1612 node
= node
->GetNext();
1617 wxImageHandler
*wxImage::FindHandler( const wxString
& extension
, long bitmapType
)
1619 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
1622 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
1623 if ( (handler
->GetExtension().Cmp(extension
) == 0) &&
1624 (bitmapType
== -1 || handler
->GetType() == bitmapType
) )
1626 node
= node
->GetNext();
1631 wxImageHandler
*wxImage::FindHandler( long bitmapType
)
1633 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
1636 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
1637 if (handler
->GetType() == bitmapType
) return handler
;
1638 node
= node
->GetNext();
1643 wxImageHandler
*wxImage::FindHandlerMime( const wxString
& mimetype
)
1645 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
1648 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
1649 if (handler
->GetMimeType().IsSameAs(mimetype
, false)) return handler
;
1650 node
= node
->GetNext();
1655 void wxImage::InitStandardHandlers()
1658 AddHandler(new wxBMPHandler
);
1659 #endif // wxUSE_STREAMS
1662 void wxImage::CleanUpHandlers()
1664 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
1667 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
1668 wxList::compatibility_iterator next
= node
->GetNext();
1673 sm_handlers
.Clear();
1676 wxString
wxImage::GetImageExtWildcard()
1680 wxList
& Handlers
= wxImage::GetHandlers();
1681 wxList::compatibility_iterator Node
= Handlers
.GetFirst();
1684 wxImageHandler
* Handler
= (wxImageHandler
*)Node
->GetData();
1685 fmts
+= wxT("*.") + Handler
->GetExtension();
1686 Node
= Node
->GetNext();
1687 if ( Node
) fmts
+= wxT(";");
1690 return wxT("(") + fmts
+ wxT(")|") + fmts
;
1693 //-----------------------------------------------------------------------------
1695 //-----------------------------------------------------------------------------
1697 IMPLEMENT_ABSTRACT_CLASS(wxImageHandler
,wxObject
)
1700 bool wxImageHandler::LoadFile( wxImage
*WXUNUSED(image
), wxInputStream
& WXUNUSED(stream
), bool WXUNUSED(verbose
), int WXUNUSED(index
) )
1705 bool wxImageHandler::SaveFile( wxImage
*WXUNUSED(image
), wxOutputStream
& WXUNUSED(stream
), bool WXUNUSED(verbose
) )
1710 int wxImageHandler::GetImageCount( wxInputStream
& WXUNUSED(stream
) )
1715 bool wxImageHandler::CanRead( const wxString
& name
)
1717 if (wxFileExists(name
))
1719 wxFileInputStream
stream(name
);
1720 return CanRead(stream
);
1723 wxLogError( _("Can't check image format of file '%s': file does not exist."), name
.c_str() );
1728 bool wxImageHandler::CallDoCanRead(wxInputStream
& stream
)
1730 wxFileOffset posOld
= stream
.TellI();
1731 if ( posOld
== wxInvalidOffset
)
1733 // can't test unseekable stream
1737 bool ok
= DoCanRead(stream
);
1739 // restore the old position to be able to test other formats and so on
1740 if ( stream
.SeekI(posOld
) == wxInvalidOffset
)
1742 wxLogDebug(_T("Failed to rewind the stream in wxImageHandler!"));
1744 // reading would fail anyhow as we're not at the right position
1751 #endif // wxUSE_STREAMS
1753 // ----------------------------------------------------------------------------
1754 // image histogram stuff
1755 // ----------------------------------------------------------------------------
1758 wxImageHistogram::FindFirstUnusedColour(unsigned char *r
,
1763 unsigned char g2
) const
1765 unsigned long key
= MakeKey(r2
, g2
, b2
);
1767 while ( find(key
) != end() )
1769 // color already used
1781 wxLogError(_("No unused colour in image.") );
1787 key
= MakeKey(r2
, g2
, b2
);
1801 wxImage::FindFirstUnusedColour(unsigned char *r
,
1806 unsigned char g2
) const
1808 wxImageHistogram histogram
;
1810 ComputeHistogram(histogram
);
1812 return histogram
.FindFirstUnusedColour(r
, g
, b
, r2
, g2
, b2
);
1818 // Counts and returns the number of different colours. Optionally stops
1819 // when it exceeds 'stopafter' different colours. This is useful, for
1820 // example, to see if the image can be saved as 8-bit (256 colour or
1821 // less, in this case it would be invoked as CountColours(256)). Default
1822 // value for stopafter is -1 (don't care).
1824 unsigned long wxImage::CountColours( unsigned long stopafter
) const
1828 unsigned char r
, g
, b
;
1830 unsigned long size
, nentries
, key
;
1833 size
= GetWidth() * GetHeight();
1836 for (unsigned long j
= 0; (j
< size
) && (nentries
<= stopafter
) ; j
++)
1841 key
= wxImageHistogram::MakeKey(r
, g
, b
);
1843 if (h
.Get(key
) == NULL
)
1854 unsigned long wxImage::ComputeHistogram( wxImageHistogram
&h
) const
1856 unsigned char *p
= GetData();
1857 unsigned long nentries
= 0;
1861 const unsigned long size
= GetWidth() * GetHeight();
1863 unsigned char r
, g
, b
;
1864 for ( unsigned long n
= 0; n
< size
; n
++ )
1870 wxImageHistogramEntry
& entry
= h
[wxImageHistogram::MakeKey(r
, g
, b
)];
1872 if ( entry
.value
++ == 0 )
1873 entry
.index
= nentries
++;
1880 * Rotation code by Carlos Moreno
1883 // GRG: I've removed wxRotationPoint - we already have wxRealPoint which
1884 // does exactly the same thing. And I also got rid of wxRotationPixel
1885 // bacause of potential problems in architectures where alignment
1886 // is an issue, so I had to rewrite parts of the code.
1888 static const double gs_Epsilon
= 1e-10;
1890 static inline int wxCint (double x
)
1892 return (x
> 0) ? (int) (x
+ 0.5) : (int) (x
- 0.5);
1896 // Auxiliary function to rotate a point (x,y) with respect to point p0
1897 // make it inline and use a straight return to facilitate optimization
1898 // also, the function receives the sine and cosine of the angle to avoid
1899 // repeating the time-consuming calls to these functions -- sin/cos can
1900 // be computed and stored in the calling function.
1902 inline wxRealPoint
rotated_point (const wxRealPoint
& p
, double cos_angle
, double sin_angle
, const wxRealPoint
& p0
)
1904 return wxRealPoint (p0
.x
+ (p
.x
- p0
.x
) * cos_angle
- (p
.y
- p0
.y
) * sin_angle
,
1905 p0
.y
+ (p
.y
- p0
.y
) * cos_angle
+ (p
.x
- p0
.x
) * sin_angle
);
1908 inline wxRealPoint
rotated_point (double x
, double y
, double cos_angle
, double sin_angle
, const wxRealPoint
& p0
)
1910 return rotated_point (wxRealPoint(x
,y
), cos_angle
, sin_angle
, p0
);
1913 wxImage
wxImage::Rotate(double angle
, const wxPoint
& centre_of_rotation
, bool interpolating
, wxPoint
* offset_after_rotation
) const
1916 angle
= -angle
; // screen coordinates are a mirror image of "real" coordinates
1918 bool has_alpha
= HasAlpha();
1920 // Create pointer-based array to accelerate access to wxImage's data
1921 unsigned char ** data
= new unsigned char * [GetHeight()];
1922 data
[0] = GetData();
1923 for (i
= 1; i
< GetHeight(); i
++)
1924 data
[i
] = data
[i
- 1] + (3 * GetWidth());
1926 // Same for alpha channel
1927 unsigned char ** alpha
= NULL
;
1930 alpha
= new unsigned char * [GetHeight()];
1931 alpha
[0] = GetAlpha();
1932 for (i
= 1; i
< GetHeight(); i
++)
1933 alpha
[i
] = alpha
[i
- 1] + GetWidth();
1936 // precompute coefficients for rotation formula
1937 // (sine and cosine of the angle)
1938 const double cos_angle
= cos(angle
);
1939 const double sin_angle
= sin(angle
);
1941 // Create new Image to store the result
1942 // First, find rectangle that covers the rotated image; to do that,
1943 // rotate the four corners
1945 const wxRealPoint
p0(centre_of_rotation
.x
, centre_of_rotation
.y
);
1947 wxRealPoint p1
= rotated_point (0, 0, cos_angle
, sin_angle
, p0
);
1948 wxRealPoint p2
= rotated_point (0, GetHeight(), cos_angle
, sin_angle
, p0
);
1949 wxRealPoint p3
= rotated_point (GetWidth(), 0, cos_angle
, sin_angle
, p0
);
1950 wxRealPoint p4
= rotated_point (GetWidth(), GetHeight(), cos_angle
, sin_angle
, p0
);
1952 int x1
= (int) floor (wxMin (wxMin(p1
.x
, p2
.x
), wxMin(p3
.x
, p4
.x
)));
1953 int y1
= (int) floor (wxMin (wxMin(p1
.y
, p2
.y
), wxMin(p3
.y
, p4
.y
)));
1954 int x2
= (int) ceil (wxMax (wxMax(p1
.x
, p2
.x
), wxMax(p3
.x
, p4
.x
)));
1955 int y2
= (int) ceil (wxMax (wxMax(p1
.y
, p2
.y
), wxMax(p3
.y
, p4
.y
)));
1957 // Create rotated image
1958 wxImage
rotated (x2
- x1
+ 1, y2
- y1
+ 1, false);
1959 // With alpha channel
1963 if (offset_after_rotation
!= NULL
)
1965 *offset_after_rotation
= wxPoint (x1
, y1
);
1968 // GRG: The rotated (destination) image is always accessed
1969 // sequentially, so there is no need for a pointer-based
1970 // array here (and in fact it would be slower).
1972 unsigned char * dst
= rotated
.GetData();
1974 unsigned char * alpha_dst
= NULL
;
1976 alpha_dst
= rotated
.GetAlpha();
1978 // GRG: if the original image has a mask, use its RGB values
1979 // as the blank pixel, else, fall back to default (black).
1981 unsigned char blank_r
= 0;
1982 unsigned char blank_g
= 0;
1983 unsigned char blank_b
= 0;
1987 blank_r
= GetMaskRed();
1988 blank_g
= GetMaskGreen();
1989 blank_b
= GetMaskBlue();
1990 rotated
.SetMaskColour( blank_r
, blank_g
, blank_b
);
1993 // Now, for each point of the rotated image, find where it came from, by
1994 // performing an inverse rotation (a rotation of -angle) and getting the
1995 // pixel at those coordinates
1997 // GRG: I've taken the (interpolating) test out of the loops, so that
1998 // it is done only once, instead of repeating it for each pixel.
2003 for (int y
= 0; y
< rotated
.GetHeight(); y
++)
2005 for (x
= 0; x
< rotated
.GetWidth(); x
++)
2007 wxRealPoint src
= rotated_point (x
+ x1
, y
+ y1
, cos_angle
, -sin_angle
, p0
);
2009 if (-0.25 < src
.x
&& src
.x
< GetWidth() - 0.75 &&
2010 -0.25 < src
.y
&& src
.y
< GetHeight() - 0.75)
2012 // interpolate using the 4 enclosing grid-points. Those
2013 // points can be obtained using floor and ceiling of the
2014 // exact coordinates of the point
2015 // C.M. 2000-02-17: when the point is near the border, special care is required.
2019 if (0 < src
.x
&& src
.x
< GetWidth() - 1)
2021 x1
= wxCint(floor(src
.x
));
2022 x2
= wxCint(ceil(src
.x
));
2024 else // else means that x is near one of the borders (0 or width-1)
2026 x1
= x2
= wxCint (src
.x
);
2029 if (0 < src
.y
&& src
.y
< GetHeight() - 1)
2031 y1
= wxCint(floor(src
.y
));
2032 y2
= wxCint(ceil(src
.y
));
2036 y1
= y2
= wxCint (src
.y
);
2039 // get four points and the distances (square of the distance,
2040 // for efficiency reasons) for the interpolation formula
2042 // GRG: Do not calculate the points until they are
2043 // really needed -- this way we can calculate
2044 // just one, instead of four, if d1, d2, d3
2045 // or d4 are < gs_Epsilon
2047 const double d1
= (src
.x
- x1
) * (src
.x
- x1
) + (src
.y
- y1
) * (src
.y
- y1
);
2048 const double d2
= (src
.x
- x2
) * (src
.x
- x2
) + (src
.y
- y1
) * (src
.y
- y1
);
2049 const double d3
= (src
.x
- x2
) * (src
.x
- x2
) + (src
.y
- y2
) * (src
.y
- y2
);
2050 const double d4
= (src
.x
- x1
) * (src
.x
- x1
) + (src
.y
- y2
) * (src
.y
- y2
);
2052 // Now interpolate as a weighted average of the four surrounding
2053 // points, where the weights are the distances to each of those points
2055 // If the point is exactly at one point of the grid of the source
2056 // image, then don't interpolate -- just assign the pixel
2058 if (d1
< gs_Epsilon
) // d1,d2,d3,d4 are positive -- no need for abs()
2060 unsigned char *p
= data
[y1
] + (3 * x1
);
2067 unsigned char *p
= alpha
[y1
] + x1
;
2068 *(alpha_dst
++) = *p
;
2071 else if (d2
< gs_Epsilon
)
2073 unsigned char *p
= data
[y1
] + (3 * x2
);
2080 unsigned char *p
= alpha
[y1
] + x2
;
2081 *(alpha_dst
++) = *p
;
2084 else if (d3
< gs_Epsilon
)
2086 unsigned char *p
= data
[y2
] + (3 * x2
);
2093 unsigned char *p
= alpha
[y2
] + x2
;
2094 *(alpha_dst
++) = *p
;
2097 else if (d4
< gs_Epsilon
)
2099 unsigned char *p
= data
[y2
] + (3 * x1
);
2106 unsigned char *p
= alpha
[y2
] + x1
;
2107 *(alpha_dst
++) = *p
;
2112 // weights for the weighted average are proportional to the inverse of the distance
2113 unsigned char *v1
= data
[y1
] + (3 * x1
);
2114 unsigned char *v2
= data
[y1
] + (3 * x2
);
2115 unsigned char *v3
= data
[y2
] + (3 * x2
);
2116 unsigned char *v4
= data
[y2
] + (3 * x1
);
2118 const double w1
= 1/d1
, w2
= 1/d2
, w3
= 1/d3
, w4
= 1/d4
;
2122 *(dst
++) = (unsigned char)
2123 ( (w1
* *(v1
++) + w2
* *(v2
++) +
2124 w3
* *(v3
++) + w4
* *(v4
++)) /
2125 (w1
+ w2
+ w3
+ w4
) );
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
) );
2137 unsigned char *v1
= alpha
[y1
] + (x1
);
2138 unsigned char *v2
= alpha
[y1
] + (x2
);
2139 unsigned char *v3
= alpha
[y2
] + (x2
);
2140 unsigned char *v4
= alpha
[y2
] + (x1
);
2142 *(alpha_dst
++) = (unsigned char)
2143 ( (w1
* *v1
+ w2
* *v2
+
2144 w3
* *v3
+ w4
* *v4
) /
2145 (w1
+ w2
+ w3
+ w4
) );
2161 else // not interpolating
2163 for (int y
= 0; y
< rotated
.GetHeight(); y
++)
2165 for (x
= 0; x
< rotated
.GetWidth(); x
++)
2167 wxRealPoint src
= rotated_point (x
+ x1
, y
+ y1
, cos_angle
, -sin_angle
, p0
);
2169 const int xs
= wxCint (src
.x
); // wxCint rounds to the
2170 const int ys
= wxCint (src
.y
); // closest integer
2172 if (0 <= xs
&& xs
< GetWidth() &&
2173 0 <= ys
&& ys
< GetHeight())
2175 unsigned char *p
= data
[ys
] + (3 * xs
);
2182 unsigned char *p
= alpha
[ys
] + (xs
);
2183 *(alpha_dst
++) = *p
;
2193 *(alpha_dst
++) = 255;
2211 // A module to allow wxImage initialization/cleanup
2212 // without calling these functions from app.cpp or from
2213 // the user's application.
2215 class wxImageModule
: public wxModule
2217 DECLARE_DYNAMIC_CLASS(wxImageModule
)
2220 bool OnInit() { wxImage::InitStandardHandlers(); return true; };
2221 void OnExit() { wxImage::CleanUpHandlers(); };
2224 IMPLEMENT_DYNAMIC_CLASS(wxImageModule
, wxModule
)
2227 #endif // wxUSE_IMAGE