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"
46 //-----------------------------------------------------------------------------
48 //-----------------------------------------------------------------------------
50 class wxImageRefData
: public wxObjectRefData
54 virtual ~wxImageRefData();
58 unsigned char *m_data
;
61 unsigned char m_maskRed
,m_maskGreen
,m_maskBlue
;
63 // alpha channel data, may be NULL for the formats without alpha support
64 unsigned char *m_alpha
;
71 #endif // wxUSE_PALETTE
73 wxArrayString m_optionNames
;
74 wxArrayString m_optionValues
;
76 DECLARE_NO_COPY_CLASS(wxImageRefData
)
79 wxImageRefData::wxImageRefData()
84 m_alpha
= (unsigned char *) NULL
;
95 wxImageRefData::~wxImageRefData()
104 wxList
wxImage::sm_handlers
;
108 //-----------------------------------------------------------------------------
110 #define M_IMGDATA ((wxImageRefData *)m_refData)
112 IMPLEMENT_DYNAMIC_CLASS(wxImage
, wxObject
)
114 wxImage::wxImage( int width
, int height
, bool clear
)
116 Create( width
, height
, clear
);
119 wxImage::wxImage( int width
, int height
, unsigned char* data
, bool static_data
)
121 Create( width
, height
, data
, static_data
);
124 wxImage::wxImage( int width
, int height
, unsigned char* data
, unsigned char* alpha
, bool static_data
)
126 Create( width
, height
, data
, alpha
, static_data
);
129 wxImage::wxImage( const wxString
& name
, long type
, int index
)
131 LoadFile( name
, type
, index
);
134 wxImage::wxImage( const wxString
& name
, const wxString
& mimetype
, int index
)
136 LoadFile( name
, mimetype
, index
);
140 wxImage::wxImage( wxInputStream
& stream
, long type
, int index
)
142 LoadFile( stream
, type
, index
);
145 wxImage::wxImage( wxInputStream
& stream
, const wxString
& mimetype
, int index
)
147 LoadFile( stream
, mimetype
, index
);
149 #endif // wxUSE_STREAMS
151 wxImage::wxImage( const wxImage
& image
)
157 wxImage::wxImage( const wxImage
* image
)
159 if (image
) Ref(*image
);
162 bool wxImage::Create( int width
, int height
, bool clear
)
166 m_refData
= new wxImageRefData();
168 M_IMGDATA
->m_data
= (unsigned char *) malloc( width
*height
*3 );
169 if (!M_IMGDATA
->m_data
)
176 memset(M_IMGDATA
->m_data
, 0, width
*height
*3);
178 M_IMGDATA
->m_width
= width
;
179 M_IMGDATA
->m_height
= height
;
180 M_IMGDATA
->m_ok
= true;
185 bool wxImage::Create( int width
, int height
, unsigned char* data
, bool static_data
)
189 wxCHECK_MSG( data
, false, _T("NULL data in wxImage::Create") );
191 m_refData
= new wxImageRefData();
193 M_IMGDATA
->m_data
= data
;
194 M_IMGDATA
->m_width
= width
;
195 M_IMGDATA
->m_height
= height
;
196 M_IMGDATA
->m_ok
= true;
197 M_IMGDATA
->m_static
= static_data
;
202 bool wxImage::Create( int width
, int height
, unsigned char* data
, unsigned char* alpha
, bool static_data
)
206 wxCHECK_MSG( data
, false, _T("NULL data in wxImage::Create") );
208 m_refData
= new wxImageRefData();
210 M_IMGDATA
->m_data
= data
;
211 M_IMGDATA
->m_alpha
= alpha
;
212 M_IMGDATA
->m_width
= width
;
213 M_IMGDATA
->m_height
= height
;
214 M_IMGDATA
->m_ok
= true;
215 M_IMGDATA
->m_static
= static_data
;
220 void wxImage::Destroy()
225 wxImage
wxImage::Copy() const
229 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
231 image
.Create( M_IMGDATA
->m_width
, M_IMGDATA
->m_height
, false );
233 unsigned char *data
= image
.GetData();
235 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
237 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
238 image
.SetMask( M_IMGDATA
->m_hasMask
);
240 memcpy( data
, GetData(), M_IMGDATA
->m_width
*M_IMGDATA
->m_height
*3 );
242 // also copy the image options
243 wxImageRefData
*imgData
= (wxImageRefData
*)image
.m_refData
;
244 imgData
->m_optionNames
= M_IMGDATA
->m_optionNames
;
245 imgData
->m_optionValues
= M_IMGDATA
->m_optionValues
;
250 wxImage
wxImage::ShrinkBy( int xFactor
, int yFactor
) const
252 if( xFactor
== 1 && yFactor
== 1 )
257 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
259 // can't scale to/from 0 size
260 wxCHECK_MSG( (xFactor
> 0) && (yFactor
> 0), image
,
261 wxT("invalid new image size") );
263 long old_height
= M_IMGDATA
->m_height
,
264 old_width
= M_IMGDATA
->m_width
;
266 wxCHECK_MSG( (old_height
> 0) && (old_width
> 0), image
,
267 wxT("invalid old image size") );
269 long width
= old_width
/ xFactor
;
270 long height
= old_height
/ yFactor
;
272 image
.Create( width
, height
, false );
274 char unsigned *data
= image
.GetData();
276 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
278 bool hasMask
= false ;
279 unsigned char maskRed
= 0;
280 unsigned char maskGreen
= 0;
281 unsigned char maskBlue
=0 ;
282 if (M_IMGDATA
->m_hasMask
)
285 maskRed
= M_IMGDATA
->m_maskRed
;
286 maskGreen
= M_IMGDATA
->m_maskGreen
;
287 maskBlue
=M_IMGDATA
->m_maskBlue
;
289 image
.SetMaskColour( M_IMGDATA
->m_maskRed
,
290 M_IMGDATA
->m_maskGreen
,
291 M_IMGDATA
->m_maskBlue
);
293 char unsigned *source_data
= M_IMGDATA
->m_data
;
294 char unsigned *target_data
= data
;
296 for (long y
= 0; y
< height
; y
++)
298 for (long x
= 0; x
< width
; x
++)
300 unsigned long avgRed
= 0 ;
301 unsigned long avgGreen
= 0;
302 unsigned long avgBlue
= 0;
303 unsigned long counter
= 0 ;
305 for ( int y1
= 0 ; y1
< yFactor
; ++y1
)
307 long y_offset
= (y
* yFactor
+ y1
) * old_width
;
308 for ( int x1
= 0 ; x1
< xFactor
; ++x1
)
310 unsigned char *pixel
= source_data
+ 3 * ( y_offset
+ x
* xFactor
+ x1
) ;
311 unsigned char red
= pixel
[0] ;
312 unsigned char green
= pixel
[1] ;
313 unsigned char blue
= pixel
[2] ;
314 if ( !hasMask
|| red
!= maskRed
|| green
!= maskGreen
|| blue
!= maskBlue
)
325 *(target_data
++) = M_IMGDATA
->m_maskRed
;
326 *(target_data
++) = M_IMGDATA
->m_maskGreen
;
327 *(target_data
++) = M_IMGDATA
->m_maskBlue
;
331 *(target_data
++) = (unsigned char)(avgRed
/ counter
);
332 *(target_data
++) = (unsigned char)(avgGreen
/ counter
);
333 *(target_data
++) = (unsigned char)(avgBlue
/ counter
);
338 // In case this is a cursor, make sure the hotspot is scalled accordingly:
339 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
) )
340 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
,
341 (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X
))/xFactor
);
342 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
) )
343 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
,
344 (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y
))/yFactor
);
349 wxImage
wxImage::Scale( int width
, int height
) const
353 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
355 // can't scale to/from 0 size
356 wxCHECK_MSG( (width
> 0) && (height
> 0), image
,
357 wxT("invalid new image size") );
359 long old_height
= M_IMGDATA
->m_height
,
360 old_width
= M_IMGDATA
->m_width
;
361 wxCHECK_MSG( (old_height
> 0) && (old_width
> 0), image
,
362 wxT("invalid old image size") );
364 if ( old_width
% width
== 0 && old_width
>= width
&&
365 old_height
% height
== 0 && old_height
>= height
)
367 return ShrinkBy( old_width
/ width
, old_height
/ height
) ;
369 image
.Create( width
, height
, false );
371 unsigned char *data
= image
.GetData();
373 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
375 if (M_IMGDATA
->m_hasMask
)
377 image
.SetMaskColour( M_IMGDATA
->m_maskRed
,
378 M_IMGDATA
->m_maskGreen
,
379 M_IMGDATA
->m_maskBlue
);
382 unsigned char *source_data
= M_IMGDATA
->m_data
;
383 unsigned char *target_data
= data
;
385 long x_delta
= (old_width
<<16) / width
;
386 long y_delta
= (old_height
<<16) / height
;
388 unsigned char* dest_pixel
= target_data
;
391 for ( long j
= 0; j
< height
; j
++ )
393 unsigned char* src_line
= &source_data
[(y
>>16)*old_width
*3];
396 for ( long i
= 0; i
< width
; i
++ )
398 unsigned char* src_pixel
= &src_line
[(x
>>16)*3];
399 dest_pixel
[0] = src_pixel
[0];
400 dest_pixel
[1] = src_pixel
[1];
401 dest_pixel
[2] = src_pixel
[2];
409 // In case this is a cursor, make sure the hotspot is scalled accordingly:
410 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
) )
411 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
,
412 (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X
)*width
)/old_width
);
413 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
) )
414 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
,
415 (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y
)*height
)/old_height
);
420 wxImage
wxImage::Rotate90( bool clockwise
) const
424 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
426 image
.Create( M_IMGDATA
->m_height
, M_IMGDATA
->m_width
, false );
428 unsigned char *data
= image
.GetData();
430 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
432 if (M_IMGDATA
->m_hasMask
)
433 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
435 long height
= M_IMGDATA
->m_height
;
436 long width
= M_IMGDATA
->m_width
;
438 unsigned char *source_data
= M_IMGDATA
->m_data
;
439 unsigned char *target_data
;
441 for (long j
= 0; j
< height
; j
++)
443 for (long i
= 0; i
< width
; i
++)
446 target_data
= data
+ (((i
+1)*height
) - j
- 1)*3;
448 target_data
= data
+ ((height
*(width
-1)) + j
- (i
*height
))*3;
449 memcpy( target_data
, source_data
, 3 );
457 wxImage
wxImage::Mirror( bool horizontally
) const
461 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
463 image
.Create( M_IMGDATA
->m_width
, M_IMGDATA
->m_height
, false );
465 unsigned char *data
= image
.GetData();
467 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
469 if (M_IMGDATA
->m_hasMask
)
470 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
472 long height
= M_IMGDATA
->m_height
;
473 long width
= M_IMGDATA
->m_width
;
475 unsigned char *source_data
= M_IMGDATA
->m_data
;
476 unsigned char *target_data
;
480 for (long j
= 0; j
< height
; j
++)
483 target_data
= data
-3;
484 for (long i
= 0; i
< width
; i
++)
486 memcpy( target_data
, source_data
, 3 );
494 for (long i
= 0; i
< height
; i
++)
496 target_data
= data
+ 3*width
*(height
-1-i
);
497 memcpy( target_data
, source_data
, (size_t)3*width
);
498 source_data
+= 3*width
;
505 wxImage
wxImage::GetSubImage( const wxRect
&rect
) const
509 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
511 wxCHECK_MSG( (rect
.GetLeft()>=0) && (rect
.GetTop()>=0) && (rect
.GetRight()<=GetWidth()) && (rect
.GetBottom()<=GetHeight()),
512 image
, wxT("invalid subimage size") );
514 int subwidth
=rect
.GetWidth();
515 const int subheight
=rect
.GetHeight();
517 image
.Create( subwidth
, subheight
, false );
519 unsigned char *subdata
= image
.GetData(), *data
=GetData();
521 wxCHECK_MSG( subdata
, image
, wxT("unable to create image") );
523 if (M_IMGDATA
->m_hasMask
)
524 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
526 const int subleft
=3*rect
.GetLeft();
527 const int width
=3*GetWidth();
530 data
+=rect
.GetTop()*width
+subleft
;
532 for (long j
= 0; j
< subheight
; ++j
)
534 memcpy( subdata
, data
, subwidth
);
542 void wxImage::Paste( const wxImage
&image
, int x
, int y
)
544 wxCHECK_RET( Ok(), wxT("invalid image") );
545 wxCHECK_RET( image
.Ok(), wxT("invalid image") );
549 int width
= image
.GetWidth();
550 int height
= image
.GetHeight();
563 if ((x
+xx
)+width
> M_IMGDATA
->m_width
)
564 width
= M_IMGDATA
->m_width
- (x
+xx
);
565 if ((y
+yy
)+height
> M_IMGDATA
->m_height
)
566 height
= M_IMGDATA
->m_height
- (y
+yy
);
568 if (width
< 1) return;
569 if (height
< 1) return;
571 if ((!HasMask() && !image
.HasMask()) ||
572 ((HasMask() && image
.HasMask() &&
573 (GetMaskRed()==image
.GetMaskRed()) &&
574 (GetMaskGreen()==image
.GetMaskGreen()) &&
575 (GetMaskBlue()==image
.GetMaskBlue()))))
578 unsigned char* source_data
= image
.GetData() + xx
*3 + yy
*3*image
.GetWidth();
579 int source_step
= image
.GetWidth()*3;
581 unsigned char* target_data
= GetData() + (x
+xx
)*3 + (y
+yy
)*3*M_IMGDATA
->m_width
;
582 int target_step
= M_IMGDATA
->m_width
*3;
583 for (int j
= 0; j
< height
; j
++)
585 memcpy( target_data
, source_data
, width
);
586 source_data
+= source_step
;
587 target_data
+= target_step
;
592 if (!HasMask() && image
.HasMask())
594 unsigned char r
= image
.GetMaskRed();
595 unsigned char g
= image
.GetMaskGreen();
596 unsigned char b
= image
.GetMaskBlue();
599 unsigned char* source_data
= image
.GetData() + xx
*3 + yy
*3*image
.GetWidth();
600 int source_step
= image
.GetWidth()*3;
602 unsigned char* target_data
= GetData() + (x
+xx
)*3 + (y
+yy
)*3*M_IMGDATA
->m_width
;
603 int target_step
= M_IMGDATA
->m_width
*3;
605 for (int j
= 0; j
< height
; j
++)
607 for (int i
= 0; i
< width
; i
+=3)
609 if ((source_data
[i
] != r
) &&
610 (source_data
[i
+1] != g
) &&
611 (source_data
[i
+2] != b
))
613 memcpy( target_data
+i
, source_data
+i
, 3 );
616 source_data
+= source_step
;
617 target_data
+= target_step
;
622 void wxImage::Replace( unsigned char r1
, unsigned char g1
, unsigned char b1
,
623 unsigned char r2
, unsigned char g2
, unsigned char b2
)
625 wxCHECK_RET( Ok(), wxT("invalid image") );
627 unsigned char *data
= GetData();
629 const int w
= GetWidth();
630 const int h
= GetHeight();
632 for (int j
= 0; j
< h
; j
++)
633 for (int i
= 0; i
< w
; i
++)
635 if ((data
[0] == r1
) && (data
[1] == g1
) && (data
[2] == b1
))
645 wxImage
wxImage::ConvertToMono( unsigned char r
, unsigned char g
, unsigned char b
) const
649 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
651 image
.Create( M_IMGDATA
->m_width
, M_IMGDATA
->m_height
, false );
653 unsigned char *data
= image
.GetData();
655 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
657 if (M_IMGDATA
->m_hasMask
)
659 if (M_IMGDATA
->m_maskRed
== r
&& M_IMGDATA
->m_maskGreen
== g
&&
660 M_IMGDATA
->m_maskBlue
== b
)
661 image
.SetMaskColour( 255, 255, 255 );
663 image
.SetMaskColour( 0, 0, 0 );
666 long size
= M_IMGDATA
->m_height
* M_IMGDATA
->m_width
;
668 unsigned char *srcd
= M_IMGDATA
->m_data
;
669 unsigned char *tard
= image
.GetData();
671 for ( long i
= 0; i
< size
; i
++, srcd
+= 3, tard
+= 3 )
673 if (srcd
[0] == r
&& srcd
[1] == g
&& srcd
[2] == b
)
674 tard
[0] = tard
[1] = tard
[2] = 255;
676 tard
[0] = tard
[1] = tard
[2] = 0;
682 void wxImage::SetRGB( int x
, int y
, unsigned char r
, unsigned char g
, unsigned char b
)
684 wxCHECK_RET( Ok(), wxT("invalid image") );
686 int w
= M_IMGDATA
->m_width
;
687 int h
= M_IMGDATA
->m_height
;
689 wxCHECK_RET( (x
>=0) && (y
>=0) && (x
<w
) && (y
<h
), wxT("invalid image index") );
691 long pos
= (y
* w
+ x
) * 3;
693 M_IMGDATA
->m_data
[ pos
] = r
;
694 M_IMGDATA
->m_data
[ pos
+1 ] = g
;
695 M_IMGDATA
->m_data
[ pos
+2 ] = b
;
698 unsigned char wxImage::GetRed( int x
, int y
) const
700 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
702 int w
= M_IMGDATA
->m_width
;
703 int h
= M_IMGDATA
->m_height
;
705 wxCHECK_MSG( (x
>=0) && (y
>=0) && (x
<w
) && (y
<h
), 0, wxT("invalid image index") );
707 long pos
= (y
* w
+ x
) * 3;
709 return M_IMGDATA
->m_data
[pos
];
712 unsigned char wxImage::GetGreen( int x
, int y
) const
714 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
716 int w
= M_IMGDATA
->m_width
;
717 int h
= M_IMGDATA
->m_height
;
719 wxCHECK_MSG( (x
>=0) && (y
>=0) && (x
<w
) && (y
<h
), 0, wxT("invalid image index") );
721 long pos
= (y
* w
+ x
) * 3;
723 return M_IMGDATA
->m_data
[pos
+1];
726 unsigned char wxImage::GetBlue( int x
, int y
) const
728 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
730 int w
= M_IMGDATA
->m_width
;
731 int h
= M_IMGDATA
->m_height
;
733 wxCHECK_MSG( (x
>=0) && (y
>=0) && (x
<w
) && (y
<h
), 0, wxT("invalid image index") );
735 long pos
= (y
* w
+ x
) * 3;
737 return M_IMGDATA
->m_data
[pos
+2];
740 bool wxImage::Ok() const
742 // image of 0 width or height can't be considered ok - at least because it
743 // causes crashes in ConvertToBitmap() if we don't catch it in time
744 wxImageRefData
*data
= M_IMGDATA
;
745 return data
&& data
->m_ok
&& data
->m_width
&& data
->m_height
;
748 unsigned char *wxImage::GetData() const
750 wxCHECK_MSG( Ok(), (unsigned char *)NULL
, wxT("invalid image") );
752 return M_IMGDATA
->m_data
;
755 void wxImage::SetData( unsigned char *data
)
757 wxCHECK_RET( Ok(), wxT("invalid image") );
759 wxImageRefData
*newRefData
= new wxImageRefData();
761 newRefData
->m_width
= M_IMGDATA
->m_width
;
762 newRefData
->m_height
= M_IMGDATA
->m_height
;
763 newRefData
->m_data
= data
;
764 newRefData
->m_ok
= true;
765 newRefData
->m_maskRed
= M_IMGDATA
->m_maskRed
;
766 newRefData
->m_maskGreen
= M_IMGDATA
->m_maskGreen
;
767 newRefData
->m_maskBlue
= M_IMGDATA
->m_maskBlue
;
768 newRefData
->m_hasMask
= M_IMGDATA
->m_hasMask
;
772 m_refData
= newRefData
;
775 void wxImage::SetData( unsigned char *data
, int new_width
, int new_height
)
777 wxImageRefData
*newRefData
= new wxImageRefData();
781 newRefData
->m_width
= new_width
;
782 newRefData
->m_height
= new_height
;
783 newRefData
->m_data
= data
;
784 newRefData
->m_ok
= true;
785 newRefData
->m_maskRed
= M_IMGDATA
->m_maskRed
;
786 newRefData
->m_maskGreen
= M_IMGDATA
->m_maskGreen
;
787 newRefData
->m_maskBlue
= M_IMGDATA
->m_maskBlue
;
788 newRefData
->m_hasMask
= M_IMGDATA
->m_hasMask
;
792 newRefData
->m_width
= new_width
;
793 newRefData
->m_height
= new_height
;
794 newRefData
->m_data
= data
;
795 newRefData
->m_ok
= true;
800 m_refData
= newRefData
;
803 // ----------------------------------------------------------------------------
804 // alpha channel support
805 // ----------------------------------------------------------------------------
807 void wxImage::SetAlpha(int x
, int y
, unsigned char alpha
)
809 wxCHECK_RET( Ok() && HasAlpha(), wxT("invalid image or no alpha channel") );
811 int w
= M_IMGDATA
->m_width
,
812 h
= M_IMGDATA
->m_height
;
814 wxCHECK_RET( x
>=0 && y
>= 0 && x
< w
&& y
< h
, wxT("invalid image index") );
816 M_IMGDATA
->m_alpha
[y
*w
+ x
] = alpha
;
819 unsigned char wxImage::GetAlpha(int x
, int y
) const
821 wxCHECK_MSG( Ok() && HasAlpha(), 0, wxT("invalid image or no alpha channel") );
823 int w
= M_IMGDATA
->m_width
,
824 h
= M_IMGDATA
->m_height
;
826 wxCHECK_MSG( x
>=0 && y
>= 0 && x
< w
&& y
< h
, 0, wxT("invalid image index") );
828 return M_IMGDATA
->m_alpha
[y
*w
+ x
];
831 bool wxImage::ConvertColourToAlpha( unsigned char r
, unsigned char g
, unsigned char b
)
835 int w
= M_IMGDATA
->m_width
,
836 h
= M_IMGDATA
->m_height
;
838 unsigned char *alpha
= GetAlpha();
839 unsigned char *data
= GetData();
842 for (y
= 0; y
< h
; y
++)
843 for (x
= 0; x
< w
; x
++)
858 void wxImage::SetAlpha( unsigned char *alpha
)
860 wxCHECK_RET( Ok(), wxT("invalid image") );
864 alpha
= (unsigned char *)malloc(M_IMGDATA
->m_width
*M_IMGDATA
->m_height
);
867 free(M_IMGDATA
->m_alpha
);
868 M_IMGDATA
->m_alpha
= alpha
;
871 unsigned char *wxImage::GetAlpha() const
873 wxCHECK_MSG( Ok(), (unsigned char *)NULL
, wxT("invalid image") );
875 return M_IMGDATA
->m_alpha
;
878 // ----------------------------------------------------------------------------
880 // ----------------------------------------------------------------------------
882 void wxImage::SetMaskColour( unsigned char r
, unsigned char g
, unsigned char b
)
884 wxCHECK_RET( Ok(), wxT("invalid image") );
886 M_IMGDATA
->m_maskRed
= r
;
887 M_IMGDATA
->m_maskGreen
= g
;
888 M_IMGDATA
->m_maskBlue
= b
;
889 M_IMGDATA
->m_hasMask
= true;
892 unsigned char wxImage::GetMaskRed() const
894 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
896 return M_IMGDATA
->m_maskRed
;
899 unsigned char wxImage::GetMaskGreen() const
901 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
903 return M_IMGDATA
->m_maskGreen
;
906 unsigned char wxImage::GetMaskBlue() const
908 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
910 return M_IMGDATA
->m_maskBlue
;
913 void wxImage::SetMask( bool mask
)
915 wxCHECK_RET( Ok(), wxT("invalid image") );
917 M_IMGDATA
->m_hasMask
= mask
;
920 bool wxImage::HasMask() const
922 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
924 return M_IMGDATA
->m_hasMask
;
927 int wxImage::GetWidth() const
929 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
931 return M_IMGDATA
->m_width
;
934 int wxImage::GetHeight() const
936 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
938 return M_IMGDATA
->m_height
;
941 bool wxImage::SetMaskFromImage(const wxImage
& mask
,
942 unsigned char mr
, unsigned char mg
, unsigned char mb
)
944 // check that the images are the same size
945 if ( (M_IMGDATA
->m_height
!= mask
.GetHeight() ) || (M_IMGDATA
->m_width
!= mask
.GetWidth () ) )
947 wxLogError( _("Image and mask have different sizes.") );
951 // find unused colour
952 unsigned char r
,g
,b
;
953 if (!FindFirstUnusedColour(&r
, &g
, &b
))
955 wxLogError( _("No unused colour in image being masked.") );
959 unsigned char *imgdata
= GetData();
960 unsigned char *maskdata
= mask
.GetData();
962 const int w
= GetWidth();
963 const int h
= GetHeight();
965 for (int j
= 0; j
< h
; j
++)
967 for (int i
= 0; i
< w
; i
++)
969 if ((maskdata
[0] == mr
) && (maskdata
[1] == mg
) && (maskdata
[2] == mb
))
980 SetMaskColour(r
, g
, b
);
986 bool wxImage::ConvertAlphaToMask(unsigned char threshold
)
991 unsigned char mr
, mg
, mb
;
992 if (!FindFirstUnusedColour(&mr
, &mg
, &mb
))
994 wxLogError( _("No unused colour in image being masked.") );
999 SetMaskColour(mr
, mg
, mb
);
1001 unsigned char *imgdata
= GetData();
1002 unsigned char *alphadata
= GetAlpha();
1005 int h
= GetHeight();
1007 for (int y
= 0; y
< h
; y
++)
1009 for (int x
= 0; x
< w
; x
++, imgdata
+= 3, alphadata
++)
1011 if (*alphadata
< threshold
)
1020 free(M_IMGDATA
->m_alpha
);
1021 M_IMGDATA
->m_alpha
= NULL
;
1028 // Palette functions
1030 bool wxImage::HasPalette() const
1035 return M_IMGDATA
->m_palette
.Ok();
1038 const wxPalette
& wxImage::GetPalette() const
1040 wxCHECK_MSG( Ok(), wxNullPalette
, wxT("invalid image") );
1042 return M_IMGDATA
->m_palette
;
1045 void wxImage::SetPalette(const wxPalette
& palette
)
1047 wxCHECK_RET( Ok(), wxT("invalid image") );
1049 M_IMGDATA
->m_palette
= palette
;
1052 #endif // wxUSE_PALETTE
1054 // Option functions (arbitrary name/value mapping)
1055 void wxImage::SetOption(const wxString
& name
, const wxString
& value
)
1057 wxCHECK_RET( Ok(), wxT("invalid image") );
1059 int idx
= M_IMGDATA
->m_optionNames
.Index(name
, false);
1060 if (idx
== wxNOT_FOUND
)
1062 M_IMGDATA
->m_optionNames
.Add(name
);
1063 M_IMGDATA
->m_optionValues
.Add(value
);
1067 M_IMGDATA
->m_optionNames
[idx
] = name
;
1068 M_IMGDATA
->m_optionValues
[idx
] = value
;
1072 void wxImage::SetOption(const wxString
& name
, int value
)
1075 valStr
.Printf(wxT("%d"), value
);
1076 SetOption(name
, valStr
);
1079 wxString
wxImage::GetOption(const wxString
& name
) const
1081 wxCHECK_MSG( Ok(), wxEmptyString
, wxT("invalid image") );
1083 int idx
= M_IMGDATA
->m_optionNames
.Index(name
, false);
1084 if (idx
== wxNOT_FOUND
)
1085 return wxEmptyString
;
1087 return M_IMGDATA
->m_optionValues
[idx
];
1090 int wxImage::GetOptionInt(const wxString
& name
) const
1092 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
1094 return wxAtoi(GetOption(name
));
1097 bool wxImage::HasOption(const wxString
& name
) const
1099 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1101 return (M_IMGDATA
->m_optionNames
.Index(name
, false) != wxNOT_FOUND
);
1104 bool wxImage::LoadFile( const wxString
& filename
, long type
, int index
)
1107 if (wxFileExists(filename
))
1109 wxFileInputStream
stream(filename
);
1110 wxBufferedInputStream
bstream( stream
);
1111 return LoadFile(bstream
, type
, index
);
1115 wxLogError( _("Can't load image from file '%s': file does not exist."), filename
.c_str() );
1119 #else // !wxUSE_STREAMS
1121 #endif // wxUSE_STREAMS
1124 bool wxImage::LoadFile( const wxString
& filename
, const wxString
& mimetype
, int index
)
1127 if (wxFileExists(filename
))
1129 wxFileInputStream
stream(filename
);
1130 wxBufferedInputStream
bstream( stream
);
1131 return LoadFile(bstream
, mimetype
, index
);
1135 wxLogError( _("Can't load image from file '%s': file does not exist."), filename
.c_str() );
1139 #else // !wxUSE_STREAMS
1141 #endif // wxUSE_STREAMS
1146 bool wxImage::SaveFile( const wxString
& filename
) const
1148 wxString ext
= filename
.AfterLast('.').Lower();
1150 wxImageHandler
* pHandler
= FindHandler(ext
, -1);
1153 SaveFile(filename
, pHandler
->GetType());
1157 wxLogError(_("Can't save image to file '%s': unknown extension."), filename
.c_str());
1162 bool wxImage::SaveFile( const wxString
& filename
, int type
) const
1165 ((wxImage
*)this)->SetOption(wxIMAGE_OPTION_FILENAME
, filename
);
1167 wxFileOutputStream
stream(filename
);
1169 if ( stream
.IsOk() )
1171 wxBufferedOutputStream
bstream( stream
);
1172 return SaveFile(bstream
, type
);
1174 #endif // wxUSE_STREAMS
1179 bool wxImage::SaveFile( const wxString
& filename
, const wxString
& mimetype
) const
1182 ((wxImage
*)this)->SetOption(wxIMAGE_OPTION_FILENAME
, filename
);
1184 wxFileOutputStream
stream(filename
);
1186 if ( stream
.IsOk() )
1188 wxBufferedOutputStream
bstream( stream
);
1189 return SaveFile(bstream
, mimetype
);
1191 #endif // wxUSE_STREAMS
1196 bool wxImage::CanRead( const wxString
&name
)
1199 wxFileInputStream
stream(name
);
1200 return CanRead(stream
);
1206 int wxImage::GetImageCount( const wxString
&name
, long type
)
1209 wxFileInputStream
stream(name
);
1211 return GetImageCount(stream
, type
);
1219 bool wxImage::CanRead( wxInputStream
&stream
)
1221 const wxList
& list
= GetHandlers();
1223 for ( wxList::compatibility_iterator node
= list
.GetFirst(); node
; node
= node
->GetNext() )
1225 wxImageHandler
*handler
=(wxImageHandler
*)node
->GetData();
1226 if (handler
->CanRead( stream
))
1233 int wxImage::GetImageCount( wxInputStream
&stream
, long type
)
1235 wxImageHandler
*handler
;
1237 if ( type
== wxBITMAP_TYPE_ANY
)
1239 wxList
&list
=GetHandlers();
1241 for (wxList::compatibility_iterator node
= list
.GetFirst(); node
; node
= node
->GetNext())
1243 handler
=(wxImageHandler
*)node
->GetData();
1244 if ( handler
->CanRead(stream
) )
1245 return handler
->GetImageCount(stream
);
1249 wxLogWarning(_("No handler found for image type."));
1253 handler
= FindHandler(type
);
1257 wxLogWarning(_("No image handler for type %d defined."), type
);
1261 if ( handler
->CanRead(stream
) )
1263 return handler
->GetImageCount(stream
);
1267 wxLogError(_("Image file is not of type %d."), type
);
1272 bool wxImage::LoadFile( wxInputStream
& stream
, long type
, int index
)
1276 m_refData
= new wxImageRefData
;
1278 wxImageHandler
*handler
;
1280 if ( type
== wxBITMAP_TYPE_ANY
)
1282 wxList
&list
=GetHandlers();
1284 for ( wxList::compatibility_iterator node
= list
.GetFirst(); node
; node
= node
->GetNext() )
1286 handler
=(wxImageHandler
*)node
->GetData();
1287 if ( handler
->CanRead(stream
) )
1288 return handler
->LoadFile(this, stream
, true/*verbose*/, index
);
1292 wxLogWarning( _("No handler found for image type.") );
1296 handler
= FindHandler(type
);
1300 wxLogWarning( _("No image handler for type %d defined."), type
);
1305 return handler
->LoadFile(this, stream
, true/*verbose*/, index
);
1308 bool wxImage::LoadFile( wxInputStream
& stream
, const wxString
& mimetype
, int index
)
1312 m_refData
= new wxImageRefData
;
1314 wxImageHandler
*handler
= FindHandlerMime(mimetype
);
1318 wxLogWarning( _("No image handler for type %s defined."), mimetype
.GetData() );
1323 return handler
->LoadFile( this, stream
, true/*verbose*/, index
);
1326 bool wxImage::SaveFile( wxOutputStream
& stream
, int type
) const
1328 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1330 wxImageHandler
*handler
= FindHandler(type
);
1334 wxLogWarning( _("No image handler for type %d defined."), type
);
1339 return handler
->SaveFile( (wxImage
*)this, stream
);
1342 bool wxImage::SaveFile( wxOutputStream
& stream
, const wxString
& mimetype
) const
1344 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1346 wxImageHandler
*handler
= FindHandlerMime(mimetype
);
1350 wxLogWarning( _("No image handler for type %s defined."), mimetype
.GetData() );
1355 return handler
->SaveFile( (wxImage
*)this, stream
);
1357 #endif // wxUSE_STREAMS
1359 void wxImage::AddHandler( wxImageHandler
*handler
)
1361 // Check for an existing handler of the type being added.
1362 if (FindHandler( handler
->GetType() ) == 0)
1364 sm_handlers
.Append( handler
);
1368 // This is not documented behaviour, merely the simplest 'fix'
1369 // for preventing duplicate additions. If someone ever has
1370 // a good reason to add and remove duplicate handlers (and they
1371 // may) we should probably refcount the duplicates.
1372 // also an issue in InsertHandler below.
1374 wxLogDebug( _T("Adding duplicate image handler for '%s'"),
1375 handler
->GetName().c_str() );
1380 void wxImage::InsertHandler( wxImageHandler
*handler
)
1382 // Check for an existing handler of the type being added.
1383 if (FindHandler( handler
->GetType() ) == 0)
1385 sm_handlers
.Insert( handler
);
1389 // see AddHandler for additional comments.
1390 wxLogDebug( _T("Inserting duplicate image handler for '%s'"),
1391 handler
->GetName().c_str() );
1396 bool wxImage::RemoveHandler( const wxString
& name
)
1398 wxImageHandler
*handler
= FindHandler(name
);
1401 sm_handlers
.DeleteObject(handler
);
1409 wxImageHandler
*wxImage::FindHandler( const wxString
& name
)
1411 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
1414 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
1415 if (handler
->GetName().Cmp(name
) == 0) return handler
;
1417 node
= node
->GetNext();
1422 wxImageHandler
*wxImage::FindHandler( const wxString
& extension
, long bitmapType
)
1424 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
1427 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
1428 if ( (handler
->GetExtension().Cmp(extension
) == 0) &&
1429 (bitmapType
== -1 || handler
->GetType() == bitmapType
) )
1431 node
= node
->GetNext();
1436 wxImageHandler
*wxImage::FindHandler( long bitmapType
)
1438 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
1441 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
1442 if (handler
->GetType() == bitmapType
) return handler
;
1443 node
= node
->GetNext();
1448 wxImageHandler
*wxImage::FindHandlerMime( const wxString
& mimetype
)
1450 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
1453 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
1454 if (handler
->GetMimeType().IsSameAs(mimetype
, false)) return handler
;
1455 node
= node
->GetNext();
1460 void wxImage::InitStandardHandlers()
1463 AddHandler(new wxBMPHandler
);
1464 #endif // wxUSE_STREAMS
1467 void wxImage::CleanUpHandlers()
1469 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
1472 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
1473 wxList::compatibility_iterator next
= node
->GetNext();
1478 sm_handlers
.Clear();
1481 wxString
wxImage::GetImageExtWildcard()
1485 wxList
& Handlers
= wxImage::GetHandlers();
1486 wxList::compatibility_iterator Node
= Handlers
.GetFirst();
1489 wxImageHandler
* Handler
= (wxImageHandler
*)Node
->GetData();
1490 fmts
+= wxT("*.") + Handler
->GetExtension();
1491 Node
= Node
->GetNext();
1492 if ( Node
) fmts
+= wxT(";");
1495 return wxT("(") + fmts
+ wxT(")|") + fmts
;
1498 //-----------------------------------------------------------------------------
1500 //-----------------------------------------------------------------------------
1502 IMPLEMENT_ABSTRACT_CLASS(wxImageHandler
,wxObject
)
1505 bool wxImageHandler::LoadFile( wxImage
*WXUNUSED(image
), wxInputStream
& WXUNUSED(stream
), bool WXUNUSED(verbose
), int WXUNUSED(index
) )
1510 bool wxImageHandler::SaveFile( wxImage
*WXUNUSED(image
), wxOutputStream
& WXUNUSED(stream
), bool WXUNUSED(verbose
) )
1515 int wxImageHandler::GetImageCount( wxInputStream
& WXUNUSED(stream
) )
1520 bool wxImageHandler::CanRead( const wxString
& name
)
1522 if (wxFileExists(name
))
1524 wxFileInputStream
stream(name
);
1525 return CanRead(stream
);
1528 wxLogError( _("Can't check image format of file '%s': file does not exist."), name
.c_str() );
1533 bool wxImageHandler::CallDoCanRead(wxInputStream
& stream
)
1535 wxFileOffset posOld
= stream
.TellI();
1536 if ( posOld
== wxInvalidOffset
)
1538 // can't test unseekable stream
1542 bool ok
= DoCanRead(stream
);
1544 // restore the old position to be able to test other formats and so on
1545 if ( stream
.SeekI(posOld
) == wxInvalidOffset
)
1547 wxLogDebug(_T("Failed to rewind the stream in wxImageHandler!"));
1549 // reading would fail anyhow as we're not at the right position
1556 #endif // wxUSE_STREAMS
1558 // ----------------------------------------------------------------------------
1559 // image histogram stuff
1560 // ----------------------------------------------------------------------------
1563 wxImageHistogram::FindFirstUnusedColour(unsigned char *r
,
1568 unsigned char g2
) const
1570 unsigned long key
= MakeKey(r2
, g2
, b2
);
1572 while ( find(key
) != end() )
1574 // color already used
1586 wxLogError(_("No unused colour in image.") );
1592 key
= MakeKey(r2
, g2
, b2
);
1606 wxImage::FindFirstUnusedColour(unsigned char *r
,
1611 unsigned char g2
) const
1613 wxImageHistogram histogram
;
1615 ComputeHistogram(histogram
);
1617 return histogram
.FindFirstUnusedColour(r
, g
, b
, r2
, g2
, b2
);
1623 // Counts and returns the number of different colours. Optionally stops
1624 // when it exceeds 'stopafter' different colours. This is useful, for
1625 // example, to see if the image can be saved as 8-bit (256 colour or
1626 // less, in this case it would be invoked as CountColours(256)). Default
1627 // value for stopafter is -1 (don't care).
1629 unsigned long wxImage::CountColours( unsigned long stopafter
) const
1633 unsigned char r
, g
, b
;
1635 unsigned long size
, nentries
, key
;
1638 size
= GetWidth() * GetHeight();
1641 for (unsigned long j
= 0; (j
< size
) && (nentries
<= stopafter
) ; j
++)
1646 key
= wxImageHistogram::MakeKey(r
, g
, b
);
1648 if (h
.Get(key
) == NULL
)
1659 unsigned long wxImage::ComputeHistogram( wxImageHistogram
&h
) const
1661 unsigned char *p
= GetData();
1662 unsigned long nentries
= 0;
1666 const unsigned long size
= GetWidth() * GetHeight();
1668 unsigned char r
, g
, b
;
1669 for ( unsigned long n
= 0; n
< size
; n
++ )
1675 wxImageHistogramEntry
& entry
= h
[wxImageHistogram::MakeKey(r
, g
, b
)];
1677 if ( entry
.value
++ == 0 )
1678 entry
.index
= nentries
++;
1685 * Rotation code by Carlos Moreno
1688 // GRG: I've removed wxRotationPoint - we already have wxRealPoint which
1689 // does exactly the same thing. And I also got rid of wxRotationPixel
1690 // bacause of potential problems in architectures where alignment
1691 // is an issue, so I had to rewrite parts of the code.
1693 static const double gs_Epsilon
= 1e-10;
1695 static inline int wxCint (double x
)
1697 return (x
> 0) ? (int) (x
+ 0.5) : (int) (x
- 0.5);
1701 // Auxiliary function to rotate a point (x,y) with respect to point p0
1702 // make it inline and use a straight return to facilitate optimization
1703 // also, the function receives the sine and cosine of the angle to avoid
1704 // repeating the time-consuming calls to these functions -- sin/cos can
1705 // be computed and stored in the calling function.
1707 inline wxRealPoint
rotated_point (const wxRealPoint
& p
, double cos_angle
, double sin_angle
, const wxRealPoint
& p0
)
1709 return wxRealPoint (p0
.x
+ (p
.x
- p0
.x
) * cos_angle
- (p
.y
- p0
.y
) * sin_angle
,
1710 p0
.y
+ (p
.y
- p0
.y
) * cos_angle
+ (p
.x
- p0
.x
) * sin_angle
);
1713 inline wxRealPoint
rotated_point (double x
, double y
, double cos_angle
, double sin_angle
, const wxRealPoint
& p0
)
1715 return rotated_point (wxRealPoint(x
,y
), cos_angle
, sin_angle
, p0
);
1718 wxImage
wxImage::Rotate(double angle
, const wxPoint
& centre_of_rotation
, bool interpolating
, wxPoint
* offset_after_rotation
) const
1721 angle
= -angle
; // screen coordinates are a mirror image of "real" coordinates
1723 bool has_alpha
= HasAlpha();
1725 // Create pointer-based array to accelerate access to wxImage's data
1726 unsigned char ** data
= new unsigned char * [GetHeight()];
1727 data
[0] = GetData();
1728 for (i
= 1; i
< GetHeight(); i
++)
1729 data
[i
] = data
[i
- 1] + (3 * GetWidth());
1731 // Same for alpha channel
1732 unsigned char ** alpha
= NULL
;
1735 alpha
= new unsigned char * [GetHeight()];
1736 alpha
[0] = GetAlpha();
1737 for (i
= 1; i
< GetHeight(); i
++)
1738 alpha
[i
] = alpha
[i
- 1] + GetWidth();
1741 // precompute coefficients for rotation formula
1742 // (sine and cosine of the angle)
1743 const double cos_angle
= cos(angle
);
1744 const double sin_angle
= sin(angle
);
1746 // Create new Image to store the result
1747 // First, find rectangle that covers the rotated image; to do that,
1748 // rotate the four corners
1750 const wxRealPoint
p0(centre_of_rotation
.x
, centre_of_rotation
.y
);
1752 wxRealPoint p1
= rotated_point (0, 0, cos_angle
, sin_angle
, p0
);
1753 wxRealPoint p2
= rotated_point (0, GetHeight(), cos_angle
, sin_angle
, p0
);
1754 wxRealPoint p3
= rotated_point (GetWidth(), 0, cos_angle
, sin_angle
, p0
);
1755 wxRealPoint p4
= rotated_point (GetWidth(), GetHeight(), cos_angle
, sin_angle
, p0
);
1757 int x1
= (int) floor (wxMin (wxMin(p1
.x
, p2
.x
), wxMin(p3
.x
, p4
.x
)));
1758 int y1
= (int) floor (wxMin (wxMin(p1
.y
, p2
.y
), wxMin(p3
.y
, p4
.y
)));
1759 int x2
= (int) ceil (wxMax (wxMax(p1
.x
, p2
.x
), wxMax(p3
.x
, p4
.x
)));
1760 int y2
= (int) ceil (wxMax (wxMax(p1
.y
, p2
.y
), wxMax(p3
.y
, p4
.y
)));
1762 // Create rotated image
1763 wxImage
rotated (x2
- x1
+ 1, y2
- y1
+ 1, false);
1764 // With alpha channel
1768 if (offset_after_rotation
!= NULL
)
1770 *offset_after_rotation
= wxPoint (x1
, y1
);
1773 // GRG: The rotated (destination) image is always accessed
1774 // sequentially, so there is no need for a pointer-based
1775 // array here (and in fact it would be slower).
1777 unsigned char * dst
= rotated
.GetData();
1779 unsigned char * alpha_dst
= NULL
;
1781 alpha_dst
= rotated
.GetAlpha();
1783 // GRG: if the original image has a mask, use its RGB values
1784 // as the blank pixel, else, fall back to default (black).
1786 unsigned char blank_r
= 0;
1787 unsigned char blank_g
= 0;
1788 unsigned char blank_b
= 0;
1792 blank_r
= GetMaskRed();
1793 blank_g
= GetMaskGreen();
1794 blank_b
= GetMaskBlue();
1795 rotated
.SetMaskColour( blank_r
, blank_g
, blank_b
);
1798 // Now, for each point of the rotated image, find where it came from, by
1799 // performing an inverse rotation (a rotation of -angle) and getting the
1800 // pixel at those coordinates
1802 // GRG: I've taken the (interpolating) test out of the loops, so that
1803 // it is done only once, instead of repeating it for each pixel.
1808 for (int y
= 0; y
< rotated
.GetHeight(); y
++)
1810 for (x
= 0; x
< rotated
.GetWidth(); x
++)
1812 wxRealPoint src
= rotated_point (x
+ x1
, y
+ y1
, cos_angle
, -sin_angle
, p0
);
1814 if (-0.25 < src
.x
&& src
.x
< GetWidth() - 0.75 &&
1815 -0.25 < src
.y
&& src
.y
< GetHeight() - 0.75)
1817 // interpolate using the 4 enclosing grid-points. Those
1818 // points can be obtained using floor and ceiling of the
1819 // exact coordinates of the point
1820 // C.M. 2000-02-17: when the point is near the border, special care is required.
1824 if (0 < src
.x
&& src
.x
< GetWidth() - 1)
1826 x1
= wxCint(floor(src
.x
));
1827 x2
= wxCint(ceil(src
.x
));
1829 else // else means that x is near one of the borders (0 or width-1)
1831 x1
= x2
= wxCint (src
.x
);
1834 if (0 < src
.y
&& src
.y
< GetHeight() - 1)
1836 y1
= wxCint(floor(src
.y
));
1837 y2
= wxCint(ceil(src
.y
));
1841 y1
= y2
= wxCint (src
.y
);
1844 // get four points and the distances (square of the distance,
1845 // for efficiency reasons) for the interpolation formula
1847 // GRG: Do not calculate the points until they are
1848 // really needed -- this way we can calculate
1849 // just one, instead of four, if d1, d2, d3
1850 // or d4 are < gs_Epsilon
1852 const double d1
= (src
.x
- x1
) * (src
.x
- x1
) + (src
.y
- y1
) * (src
.y
- y1
);
1853 const double d2
= (src
.x
- x2
) * (src
.x
- x2
) + (src
.y
- y1
) * (src
.y
- y1
);
1854 const double d3
= (src
.x
- x2
) * (src
.x
- x2
) + (src
.y
- y2
) * (src
.y
- y2
);
1855 const double d4
= (src
.x
- x1
) * (src
.x
- x1
) + (src
.y
- y2
) * (src
.y
- y2
);
1857 // Now interpolate as a weighted average of the four surrounding
1858 // points, where the weights are the distances to each of those points
1860 // If the point is exactly at one point of the grid of the source
1861 // image, then don't interpolate -- just assign the pixel
1863 if (d1
< gs_Epsilon
) // d1,d2,d3,d4 are positive -- no need for abs()
1865 unsigned char *p
= data
[y1
] + (3 * x1
);
1872 unsigned char *p
= alpha
[y1
] + x1
;
1873 *(alpha_dst
++) = *p
;
1876 else if (d2
< gs_Epsilon
)
1878 unsigned char *p
= data
[y1
] + (3 * x2
);
1885 unsigned char *p
= alpha
[y1
] + x2
;
1886 *(alpha_dst
++) = *p
;
1889 else if (d3
< gs_Epsilon
)
1891 unsigned char *p
= data
[y2
] + (3 * x2
);
1898 unsigned char *p
= alpha
[y2
] + x2
;
1899 *(alpha_dst
++) = *p
;
1902 else if (d4
< gs_Epsilon
)
1904 unsigned char *p
= data
[y2
] + (3 * x1
);
1911 unsigned char *p
= alpha
[y2
] + x1
;
1912 *(alpha_dst
++) = *p
;
1917 // weights for the weighted average are proportional to the inverse of the distance
1918 unsigned char *v1
= data
[y1
] + (3 * x1
);
1919 unsigned char *v2
= data
[y1
] + (3 * x2
);
1920 unsigned char *v3
= data
[y2
] + (3 * x2
);
1921 unsigned char *v4
= data
[y2
] + (3 * x1
);
1923 const double w1
= 1/d1
, w2
= 1/d2
, w3
= 1/d3
, w4
= 1/d4
;
1927 *(dst
++) = (unsigned char)
1928 ( (w1
* *(v1
++) + w2
* *(v2
++) +
1929 w3
* *(v3
++) + w4
* *(v4
++)) /
1930 (w1
+ w2
+ w3
+ w4
) );
1931 *(dst
++) = (unsigned char)
1932 ( (w1
* *(v1
++) + w2
* *(v2
++) +
1933 w3
* *(v3
++) + w4
* *(v4
++)) /
1934 (w1
+ w2
+ w3
+ w4
) );
1935 *(dst
++) = (unsigned char)
1936 ( (w1
* *v1
+ w2
* *v2
+
1937 w3
* *v3
+ w4
* *v4
) /
1938 (w1
+ w2
+ w3
+ w4
) );
1942 unsigned char *v1
= alpha
[y1
] + (x1
);
1943 unsigned char *v2
= alpha
[y1
] + (x2
);
1944 unsigned char *v3
= alpha
[y2
] + (x2
);
1945 unsigned char *v4
= alpha
[y2
] + (x1
);
1947 *(alpha_dst
++) = (unsigned char)
1948 ( (w1
* *v1
+ w2
* *v2
+
1949 w3
* *v3
+ w4
* *v4
) /
1950 (w1
+ w2
+ w3
+ w4
) );
1966 else // not interpolating
1968 for (int y
= 0; y
< rotated
.GetHeight(); y
++)
1970 for (x
= 0; x
< rotated
.GetWidth(); x
++)
1972 wxRealPoint src
= rotated_point (x
+ x1
, y
+ y1
, cos_angle
, -sin_angle
, p0
);
1974 const int xs
= wxCint (src
.x
); // wxCint rounds to the
1975 const int ys
= wxCint (src
.y
); // closest integer
1977 if (0 <= xs
&& xs
< GetWidth() &&
1978 0 <= ys
&& ys
< GetHeight())
1980 unsigned char *p
= data
[ys
] + (3 * xs
);
1987 unsigned char *p
= alpha
[ys
] + (xs
);
1988 *(alpha_dst
++) = *p
;
1998 *(alpha_dst
++) = 255;
2016 // A module to allow wxImage initialization/cleanup
2017 // without calling these functions from app.cpp or from
2018 // the user's application.
2020 class wxImageModule
: public wxModule
2022 DECLARE_DYNAMIC_CLASS(wxImageModule
)
2025 bool OnInit() { wxImage::InitStandardHandlers(); return true; };
2026 void OnExit() { wxImage::CleanUpHandlers(); };
2029 IMPLEMENT_DYNAMIC_CLASS(wxImageModule
, wxModule
)
2032 #endif // wxUSE_IMAGE