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()
103 wxList
wxImage::sm_handlers
;
107 //-----------------------------------------------------------------------------
109 #define M_IMGDATA ((wxImageRefData *)m_refData)
111 IMPLEMENT_DYNAMIC_CLASS(wxImage
, wxObject
)
113 wxImage::wxImage( int width
, int height
, bool clear
)
115 Create( width
, height
, clear
);
118 wxImage::wxImage( int width
, int height
, unsigned char* data
, bool static_data
)
120 Create( width
, height
, data
, static_data
);
123 wxImage::wxImage( const wxString
& name
, long type
, int index
)
125 LoadFile( name
, type
, index
);
128 wxImage::wxImage( const wxString
& name
, const wxString
& mimetype
, int index
)
130 LoadFile( name
, mimetype
, index
);
134 wxImage::wxImage( wxInputStream
& stream
, long type
, int index
)
136 LoadFile( stream
, type
, index
);
139 wxImage::wxImage( wxInputStream
& stream
, const wxString
& mimetype
, int index
)
141 LoadFile( stream
, mimetype
, index
);
143 #endif // wxUSE_STREAMS
145 wxImage::wxImage( const wxImage
& image
)
151 wxImage::wxImage( const wxImage
* image
)
153 if (image
) Ref(*image
);
156 bool wxImage::Create( int width
, int height
, bool clear
)
160 m_refData
= new wxImageRefData();
162 M_IMGDATA
->m_data
= (unsigned char *) malloc( width
*height
*3 );
163 if (!M_IMGDATA
->m_data
)
170 memset(M_IMGDATA
->m_data
, 0, width
*height
*3);
172 M_IMGDATA
->m_width
= width
;
173 M_IMGDATA
->m_height
= height
;
174 M_IMGDATA
->m_ok
= true;
179 bool wxImage::Create( int width
, int height
, unsigned char* data
, bool static_data
)
183 wxCHECK_MSG( data
, false, _T("NULL data in wxImage::Create") );
185 m_refData
= new wxImageRefData();
187 M_IMGDATA
->m_data
= data
;
188 M_IMGDATA
->m_width
= width
;
189 M_IMGDATA
->m_height
= height
;
190 M_IMGDATA
->m_ok
= true;
191 M_IMGDATA
->m_static
= static_data
;
196 void wxImage::Destroy()
201 wxImage
wxImage::Copy() const
205 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
207 image
.Create( M_IMGDATA
->m_width
, M_IMGDATA
->m_height
, false );
209 unsigned char *data
= image
.GetData();
211 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
213 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
214 image
.SetMask( M_IMGDATA
->m_hasMask
);
216 memcpy( data
, GetData(), M_IMGDATA
->m_width
*M_IMGDATA
->m_height
*3 );
218 // also copy the image options
219 wxImageRefData
*imgData
= (wxImageRefData
*)image
.m_refData
;
220 imgData
->m_optionNames
= M_IMGDATA
->m_optionNames
;
221 imgData
->m_optionValues
= M_IMGDATA
->m_optionValues
;
226 wxImage
wxImage::ShrinkBy( int xFactor
, int yFactor
) const
228 if( xFactor
== 1 && yFactor
== 1 )
233 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
235 // can't scale to/from 0 size
236 wxCHECK_MSG( (xFactor
> 0) && (yFactor
> 0), image
,
237 wxT("invalid new image size") );
239 long old_height
= M_IMGDATA
->m_height
,
240 old_width
= M_IMGDATA
->m_width
;
242 wxCHECK_MSG( (old_height
> 0) && (old_width
> 0), image
,
243 wxT("invalid old image size") );
245 long width
= old_width
/ xFactor
;
246 long height
= old_height
/ yFactor
;
248 image
.Create( width
, height
, false );
250 char unsigned *data
= image
.GetData();
252 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
254 bool hasMask
= false ;
255 unsigned char maskRed
= 0;
256 unsigned char maskGreen
= 0;
257 unsigned char maskBlue
=0 ;
258 if (M_IMGDATA
->m_hasMask
)
261 maskRed
= M_IMGDATA
->m_maskRed
;
262 maskGreen
= M_IMGDATA
->m_maskGreen
;
263 maskBlue
=M_IMGDATA
->m_maskBlue
;
265 image
.SetMaskColour( M_IMGDATA
->m_maskRed
,
266 M_IMGDATA
->m_maskGreen
,
267 M_IMGDATA
->m_maskBlue
);
269 char unsigned *source_data
= M_IMGDATA
->m_data
;
270 char unsigned *target_data
= data
;
272 for (long y
= 0; y
< height
; y
++)
274 for (long x
= 0; x
< width
; x
++)
276 unsigned long avgRed
= 0 ;
277 unsigned long avgGreen
= 0;
278 unsigned long avgBlue
= 0;
279 unsigned long counter
= 0 ;
281 for ( int y1
= 0 ; y1
< yFactor
; ++y1
)
283 long y_offset
= (y
* yFactor
+ y1
) * old_width
;
284 for ( int x1
= 0 ; x1
< xFactor
; ++x1
)
286 unsigned char *pixel
= source_data
+ 3 * ( y_offset
+ x
* xFactor
+ x1
) ;
287 unsigned char red
= pixel
[0] ;
288 unsigned char green
= pixel
[1] ;
289 unsigned char blue
= pixel
[2] ;
290 if ( !hasMask
|| red
!= maskRed
|| green
!= maskGreen
|| blue
!= maskBlue
)
301 *(target_data
++) = M_IMGDATA
->m_maskRed
;
302 *(target_data
++) = M_IMGDATA
->m_maskGreen
;
303 *(target_data
++) = M_IMGDATA
->m_maskBlue
;
307 *(target_data
++) = (unsigned char)(avgRed
/ counter
);
308 *(target_data
++) = (unsigned char)(avgGreen
/ counter
);
309 *(target_data
++) = (unsigned char)(avgBlue
/ counter
);
314 // In case this is a cursor, make sure the hotspot is scalled accordingly:
315 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
) )
316 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
,
317 (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X
))/xFactor
);
318 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
) )
319 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
,
320 (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y
))/yFactor
);
325 wxImage
wxImage::Scale( int width
, int height
) const
329 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
331 // can't scale to/from 0 size
332 wxCHECK_MSG( (width
> 0) && (height
> 0), image
,
333 wxT("invalid new image size") );
335 long old_height
= M_IMGDATA
->m_height
,
336 old_width
= M_IMGDATA
->m_width
;
337 wxCHECK_MSG( (old_height
> 0) && (old_width
> 0), image
,
338 wxT("invalid old image size") );
340 if ( old_width
% width
== 0 && old_width
>= width
&&
341 old_height
% height
== 0 && old_height
>= height
)
343 return ShrinkBy( old_width
/ width
, old_height
/ height
) ;
345 image
.Create( width
, height
, false );
347 unsigned char *data
= image
.GetData();
349 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
351 if (M_IMGDATA
->m_hasMask
)
353 image
.SetMaskColour( M_IMGDATA
->m_maskRed
,
354 M_IMGDATA
->m_maskGreen
,
355 M_IMGDATA
->m_maskBlue
);
358 unsigned char *source_data
= M_IMGDATA
->m_data
;
359 unsigned char *target_data
= data
;
361 long x_delta
= (old_width
<<16) / width
;
362 long y_delta
= (old_height
<<16) / height
;
364 unsigned char* dest_pixel
= target_data
;
367 for ( long j
= 0; j
< height
; j
++ )
369 unsigned char* src_line
= &source_data
[(y
>>16)*old_width
*3];
372 for ( long i
= 0; i
< width
; i
++ )
374 unsigned char* src_pixel
= &src_line
[(x
>>16)*3];
375 dest_pixel
[0] = src_pixel
[0];
376 dest_pixel
[1] = src_pixel
[1];
377 dest_pixel
[2] = src_pixel
[2];
385 // In case this is a cursor, make sure the hotspot is scalled accordingly:
386 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
) )
387 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
,
388 (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X
)*width
)/old_width
);
389 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
) )
390 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
,
391 (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y
)*height
)/old_height
);
396 wxImage
wxImage::Rotate90( bool clockwise
) const
400 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
402 image
.Create( M_IMGDATA
->m_height
, M_IMGDATA
->m_width
, false );
404 unsigned char *data
= image
.GetData();
406 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
408 if (M_IMGDATA
->m_hasMask
)
409 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
411 long height
= M_IMGDATA
->m_height
;
412 long width
= M_IMGDATA
->m_width
;
414 unsigned char *source_data
= M_IMGDATA
->m_data
;
415 unsigned char *target_data
;
417 for (long j
= 0; j
< height
; j
++)
419 for (long i
= 0; i
< width
; i
++)
422 target_data
= data
+ (((i
+1)*height
) - j
- 1)*3;
424 target_data
= data
+ ((height
*(width
-1)) + j
- (i
*height
))*3;
425 memcpy( target_data
, source_data
, 3 );
433 wxImage
wxImage::Mirror( bool horizontally
) const
437 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
439 image
.Create( M_IMGDATA
->m_width
, M_IMGDATA
->m_height
, false );
441 unsigned char *data
= image
.GetData();
443 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
445 if (M_IMGDATA
->m_hasMask
)
446 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
448 long height
= M_IMGDATA
->m_height
;
449 long width
= M_IMGDATA
->m_width
;
451 unsigned char *source_data
= M_IMGDATA
->m_data
;
452 unsigned char *target_data
;
456 for (long j
= 0; j
< height
; j
++)
459 target_data
= data
-3;
460 for (long i
= 0; i
< width
; i
++)
462 memcpy( target_data
, source_data
, 3 );
470 for (long i
= 0; i
< height
; i
++)
472 target_data
= data
+ 3*width
*(height
-1-i
);
473 memcpy( target_data
, source_data
, (size_t)3*width
);
474 source_data
+= 3*width
;
481 wxImage
wxImage::GetSubImage( const wxRect
&rect
) const
485 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
487 wxCHECK_MSG( (rect
.GetLeft()>=0) && (rect
.GetTop()>=0) && (rect
.GetRight()<=GetWidth()) && (rect
.GetBottom()<=GetHeight()),
488 image
, wxT("invalid subimage size") );
490 int subwidth
=rect
.GetWidth();
491 const int subheight
=rect
.GetHeight();
493 image
.Create( subwidth
, subheight
, false );
495 unsigned char *subdata
= image
.GetData(), *data
=GetData();
497 wxCHECK_MSG( subdata
, image
, wxT("unable to create image") );
499 if (M_IMGDATA
->m_hasMask
)
500 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
502 const int subleft
=3*rect
.GetLeft();
503 const int width
=3*GetWidth();
506 data
+=rect
.GetTop()*width
+subleft
;
508 for (long j
= 0; j
< subheight
; ++j
)
510 memcpy( subdata
, data
, subwidth
);
518 void wxImage::Paste( const wxImage
&image
, int x
, int y
)
520 wxCHECK_RET( Ok(), wxT("invalid image") );
521 wxCHECK_RET( image
.Ok(), wxT("invalid image") );
525 int width
= image
.GetWidth();
526 int height
= image
.GetHeight();
539 if ((x
+xx
)+width
> M_IMGDATA
->m_width
)
540 width
= M_IMGDATA
->m_width
- (x
+xx
);
541 if ((y
+yy
)+height
> M_IMGDATA
->m_height
)
542 height
= M_IMGDATA
->m_height
- (y
+yy
);
544 if (width
< 1) return;
545 if (height
< 1) return;
547 if ((!HasMask() && !image
.HasMask()) ||
548 ((HasMask() && image
.HasMask() &&
549 (GetMaskRed()==image
.GetMaskRed()) &&
550 (GetMaskGreen()==image
.GetMaskGreen()) &&
551 (GetMaskBlue()==image
.GetMaskBlue()))))
554 unsigned char* source_data
= image
.GetData() + xx
*3 + yy
*3*image
.GetWidth();
555 int source_step
= image
.GetWidth()*3;
557 unsigned char* target_data
= GetData() + (x
+xx
)*3 + (y
+yy
)*3*M_IMGDATA
->m_width
;
558 int target_step
= M_IMGDATA
->m_width
*3;
559 for (int j
= 0; j
< height
; j
++)
561 memcpy( target_data
, source_data
, width
);
562 source_data
+= source_step
;
563 target_data
+= target_step
;
568 if (!HasMask() && image
.HasMask())
570 unsigned char r
= image
.GetMaskRed();
571 unsigned char g
= image
.GetMaskGreen();
572 unsigned char b
= image
.GetMaskBlue();
575 unsigned char* source_data
= image
.GetData() + xx
*3 + yy
*3*image
.GetWidth();
576 int source_step
= image
.GetWidth()*3;
578 unsigned char* target_data
= GetData() + (x
+xx
)*3 + (y
+yy
)*3*M_IMGDATA
->m_width
;
579 int target_step
= M_IMGDATA
->m_width
*3;
581 for (int j
= 0; j
< height
; j
++)
583 for (int i
= 0; i
< width
; i
+=3)
585 if ((source_data
[i
] != r
) &&
586 (source_data
[i
+1] != g
) &&
587 (source_data
[i
+2] != b
))
589 memcpy( target_data
+i
, source_data
+i
, 3 );
592 source_data
+= source_step
;
593 target_data
+= target_step
;
598 void wxImage::Replace( unsigned char r1
, unsigned char g1
, unsigned char b1
,
599 unsigned char r2
, unsigned char g2
, unsigned char b2
)
601 wxCHECK_RET( Ok(), wxT("invalid image") );
603 unsigned char *data
= GetData();
605 const int w
= GetWidth();
606 const int h
= GetHeight();
608 for (int j
= 0; j
< h
; j
++)
609 for (int i
= 0; i
< w
; i
++)
611 if ((data
[0] == r1
) && (data
[1] == g1
) && (data
[2] == b1
))
621 wxImage
wxImage::ConvertToMono( unsigned char r
, unsigned char g
, unsigned char b
) const
625 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
627 image
.Create( M_IMGDATA
->m_width
, M_IMGDATA
->m_height
, false );
629 unsigned char *data
= image
.GetData();
631 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
633 if (M_IMGDATA
->m_hasMask
)
635 if (M_IMGDATA
->m_maskRed
== r
&& M_IMGDATA
->m_maskGreen
== g
&&
636 M_IMGDATA
->m_maskBlue
== b
)
637 image
.SetMaskColour( 255, 255, 255 );
639 image
.SetMaskColour( 0, 0, 0 );
642 long size
= M_IMGDATA
->m_height
* M_IMGDATA
->m_width
;
644 unsigned char *srcd
= M_IMGDATA
->m_data
;
645 unsigned char *tard
= image
.GetData();
647 for ( long i
= 0; i
< size
; i
++, srcd
+= 3, tard
+= 3 )
649 if (srcd
[0] == r
&& srcd
[1] == g
&& srcd
[2] == b
)
650 tard
[0] = tard
[1] = tard
[2] = 255;
652 tard
[0] = tard
[1] = tard
[2] = 0;
658 void wxImage::SetRGB( int x
, int y
, unsigned char r
, unsigned char g
, unsigned char b
)
660 wxCHECK_RET( Ok(), wxT("invalid image") );
662 int w
= M_IMGDATA
->m_width
;
663 int h
= M_IMGDATA
->m_height
;
665 wxCHECK_RET( (x
>=0) && (y
>=0) && (x
<w
) && (y
<h
), wxT("invalid image index") );
667 long pos
= (y
* w
+ x
) * 3;
669 M_IMGDATA
->m_data
[ pos
] = r
;
670 M_IMGDATA
->m_data
[ pos
+1 ] = g
;
671 M_IMGDATA
->m_data
[ pos
+2 ] = b
;
674 unsigned char wxImage::GetRed( int x
, int y
) const
676 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
678 int w
= M_IMGDATA
->m_width
;
679 int h
= M_IMGDATA
->m_height
;
681 wxCHECK_MSG( (x
>=0) && (y
>=0) && (x
<w
) && (y
<h
), 0, wxT("invalid image index") );
683 long pos
= (y
* w
+ x
) * 3;
685 return M_IMGDATA
->m_data
[pos
];
688 unsigned char wxImage::GetGreen( int x
, int y
) const
690 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
692 int w
= M_IMGDATA
->m_width
;
693 int h
= M_IMGDATA
->m_height
;
695 wxCHECK_MSG( (x
>=0) && (y
>=0) && (x
<w
) && (y
<h
), 0, wxT("invalid image index") );
697 long pos
= (y
* w
+ x
) * 3;
699 return M_IMGDATA
->m_data
[pos
+1];
702 unsigned char wxImage::GetBlue( int x
, int y
) const
704 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
706 int w
= M_IMGDATA
->m_width
;
707 int h
= M_IMGDATA
->m_height
;
709 wxCHECK_MSG( (x
>=0) && (y
>=0) && (x
<w
) && (y
<h
), 0, wxT("invalid image index") );
711 long pos
= (y
* w
+ x
) * 3;
713 return M_IMGDATA
->m_data
[pos
+2];
716 bool wxImage::Ok() const
718 // image of 0 width or height can't be considered ok - at least because it
719 // causes crashes in ConvertToBitmap() if we don't catch it in time
720 wxImageRefData
*data
= M_IMGDATA
;
721 return data
&& data
->m_ok
&& data
->m_width
&& data
->m_height
;
724 unsigned char *wxImage::GetData() const
726 wxCHECK_MSG( Ok(), (unsigned char *)NULL
, wxT("invalid image") );
728 return M_IMGDATA
->m_data
;
731 void wxImage::SetData( unsigned char *data
)
733 wxCHECK_RET( Ok(), wxT("invalid image") );
735 wxImageRefData
*newRefData
= new wxImageRefData();
737 newRefData
->m_width
= M_IMGDATA
->m_width
;
738 newRefData
->m_height
= M_IMGDATA
->m_height
;
739 newRefData
->m_data
= data
;
740 newRefData
->m_ok
= true;
741 newRefData
->m_maskRed
= M_IMGDATA
->m_maskRed
;
742 newRefData
->m_maskGreen
= M_IMGDATA
->m_maskGreen
;
743 newRefData
->m_maskBlue
= M_IMGDATA
->m_maskBlue
;
744 newRefData
->m_hasMask
= M_IMGDATA
->m_hasMask
;
748 m_refData
= newRefData
;
751 void wxImage::SetData( unsigned char *data
, int new_width
, int new_height
)
753 wxImageRefData
*newRefData
= new wxImageRefData();
757 newRefData
->m_width
= new_width
;
758 newRefData
->m_height
= new_height
;
759 newRefData
->m_data
= data
;
760 newRefData
->m_ok
= true;
761 newRefData
->m_maskRed
= M_IMGDATA
->m_maskRed
;
762 newRefData
->m_maskGreen
= M_IMGDATA
->m_maskGreen
;
763 newRefData
->m_maskBlue
= M_IMGDATA
->m_maskBlue
;
764 newRefData
->m_hasMask
= M_IMGDATA
->m_hasMask
;
768 newRefData
->m_width
= new_width
;
769 newRefData
->m_height
= new_height
;
770 newRefData
->m_data
= data
;
771 newRefData
->m_ok
= true;
776 m_refData
= newRefData
;
779 // ----------------------------------------------------------------------------
780 // alpha channel support
781 // ----------------------------------------------------------------------------
783 void wxImage::SetAlpha(int x
, int y
, unsigned char alpha
)
785 wxCHECK_RET( Ok() && HasAlpha(), wxT("invalid image or no alpha channel") );
787 int w
= M_IMGDATA
->m_width
,
788 h
= M_IMGDATA
->m_height
;
790 wxCHECK_RET( x
>=0 && y
>= 0 && x
< w
&& y
< h
, wxT("invalid image index") );
792 M_IMGDATA
->m_alpha
[y
*w
+ x
] = alpha
;
795 unsigned char wxImage::GetAlpha(int x
, int y
) const
797 wxCHECK_MSG( Ok() && HasAlpha(), 0, wxT("invalid image or no alpha channel") );
799 int w
= M_IMGDATA
->m_width
,
800 h
= M_IMGDATA
->m_height
;
802 wxCHECK_MSG( x
>=0 && y
>= 0 && x
< w
&& y
< h
, 0, wxT("invalid image index") );
804 return M_IMGDATA
->m_alpha
[y
*w
+ x
];
807 void wxImage::SetAlpha( unsigned char *alpha
)
809 wxCHECK_RET( Ok(), wxT("invalid image") );
813 alpha
= (unsigned char *)malloc(M_IMGDATA
->m_width
*M_IMGDATA
->m_height
);
816 free(M_IMGDATA
->m_alpha
);
817 M_IMGDATA
->m_alpha
= alpha
;
820 unsigned char *wxImage::GetAlpha() const
822 wxCHECK_MSG( Ok(), (unsigned char *)NULL
, wxT("invalid image") );
824 return M_IMGDATA
->m_alpha
;
827 // ----------------------------------------------------------------------------
829 // ----------------------------------------------------------------------------
831 void wxImage::SetMaskColour( unsigned char r
, unsigned char g
, unsigned char b
)
833 wxCHECK_RET( Ok(), wxT("invalid image") );
835 M_IMGDATA
->m_maskRed
= r
;
836 M_IMGDATA
->m_maskGreen
= g
;
837 M_IMGDATA
->m_maskBlue
= b
;
838 M_IMGDATA
->m_hasMask
= true;
841 unsigned char wxImage::GetMaskRed() const
843 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
845 return M_IMGDATA
->m_maskRed
;
848 unsigned char wxImage::GetMaskGreen() const
850 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
852 return M_IMGDATA
->m_maskGreen
;
855 unsigned char wxImage::GetMaskBlue() const
857 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
859 return M_IMGDATA
->m_maskBlue
;
862 void wxImage::SetMask( bool mask
)
864 wxCHECK_RET( Ok(), wxT("invalid image") );
866 M_IMGDATA
->m_hasMask
= mask
;
869 bool wxImage::HasMask() const
871 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
873 return M_IMGDATA
->m_hasMask
;
876 int wxImage::GetWidth() const
878 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
880 return M_IMGDATA
->m_width
;
883 int wxImage::GetHeight() const
885 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
887 return M_IMGDATA
->m_height
;
890 bool wxImage::SetMaskFromImage(const wxImage
& mask
,
891 unsigned char mr
, unsigned char mg
, unsigned char mb
)
893 // check that the images are the same size
894 if ( (M_IMGDATA
->m_height
!= mask
.GetHeight() ) || (M_IMGDATA
->m_width
!= mask
.GetWidth () ) )
896 wxLogError( _("Image and mask have different sizes.") );
900 // find unused colour
901 unsigned char r
,g
,b
;
902 if (!FindFirstUnusedColour(&r
, &g
, &b
))
904 wxLogError( _("No unused colour in image being masked.") );
908 unsigned char *imgdata
= GetData();
909 unsigned char *maskdata
= mask
.GetData();
911 const int w
= GetWidth();
912 const int h
= GetHeight();
914 for (int j
= 0; j
< h
; j
++)
916 for (int i
= 0; i
< w
; i
++)
918 if ((maskdata
[0] == mr
) && (maskdata
[1] == mg
) && (maskdata
[2] == mb
))
929 SetMaskColour(r
, g
, b
);
935 bool wxImage::ConvertAlphaToMask(unsigned char threshold
)
940 unsigned char mr
, mg
, mb
;
941 if (!FindFirstUnusedColour(&mr
, &mg
, &mb
))
943 wxLogError( _("No unused colour in image being masked.") );
948 SetMaskColour(mr
, mg
, mb
);
950 unsigned char *imgdata
= GetData();
951 unsigned char *alphadata
= GetAlpha();
956 for (int y
= 0; y
< h
; y
++)
958 for (int x
= 0; x
< w
; x
++, imgdata
+= 3, alphadata
++)
960 if (*alphadata
< threshold
)
969 free(M_IMGDATA
->m_alpha
);
970 M_IMGDATA
->m_alpha
= NULL
;
979 bool wxImage::HasPalette() const
984 return M_IMGDATA
->m_palette
.Ok();
987 const wxPalette
& wxImage::GetPalette() const
989 wxCHECK_MSG( Ok(), wxNullPalette
, wxT("invalid image") );
991 return M_IMGDATA
->m_palette
;
994 void wxImage::SetPalette(const wxPalette
& palette
)
996 wxCHECK_RET( Ok(), wxT("invalid image") );
998 M_IMGDATA
->m_palette
= palette
;
1001 #endif // wxUSE_PALETTE
1003 // Option functions (arbitrary name/value mapping)
1004 void wxImage::SetOption(const wxString
& name
, const wxString
& value
)
1006 wxCHECK_RET( Ok(), wxT("invalid image") );
1008 int idx
= M_IMGDATA
->m_optionNames
.Index(name
, false);
1009 if (idx
== wxNOT_FOUND
)
1011 M_IMGDATA
->m_optionNames
.Add(name
);
1012 M_IMGDATA
->m_optionValues
.Add(value
);
1016 M_IMGDATA
->m_optionNames
[idx
] = name
;
1017 M_IMGDATA
->m_optionValues
[idx
] = value
;
1021 void wxImage::SetOption(const wxString
& name
, int value
)
1024 valStr
.Printf(wxT("%d"), value
);
1025 SetOption(name
, valStr
);
1028 wxString
wxImage::GetOption(const wxString
& name
) const
1030 wxCHECK_MSG( Ok(), wxEmptyString
, wxT("invalid image") );
1032 int idx
= M_IMGDATA
->m_optionNames
.Index(name
, false);
1033 if (idx
== wxNOT_FOUND
)
1034 return wxEmptyString
;
1036 return M_IMGDATA
->m_optionValues
[idx
];
1039 int wxImage::GetOptionInt(const wxString
& name
) const
1041 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
1043 return wxAtoi(GetOption(name
));
1046 bool wxImage::HasOption(const wxString
& name
) const
1048 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1050 return (M_IMGDATA
->m_optionNames
.Index(name
, false) != wxNOT_FOUND
);
1053 bool wxImage::LoadFile( const wxString
& filename
, long type
, int index
)
1056 if (wxFileExists(filename
))
1058 wxFileInputStream
stream(filename
);
1059 wxBufferedInputStream
bstream( stream
);
1060 return LoadFile(bstream
, type
, index
);
1064 wxLogError( _("Can't load image from file '%s': file does not exist."), filename
.c_str() );
1068 #else // !wxUSE_STREAMS
1070 #endif // wxUSE_STREAMS
1073 bool wxImage::LoadFile( const wxString
& filename
, const wxString
& mimetype
, int index
)
1076 if (wxFileExists(filename
))
1078 wxFileInputStream
stream(filename
);
1079 wxBufferedInputStream
bstream( stream
);
1080 return LoadFile(bstream
, mimetype
, index
);
1084 wxLogError( _("Can't load image from file '%s': file does not exist."), filename
.c_str() );
1088 #else // !wxUSE_STREAMS
1090 #endif // wxUSE_STREAMS
1095 bool wxImage::SaveFile( const wxString
& filename
) const
1097 wxString ext
= filename
.AfterLast('.').Lower();
1099 wxImageHandler
* pHandler
= FindHandler(ext
, -1);
1102 SaveFile(filename
, pHandler
->GetType());
1106 wxLogError(_("Can't save image to file '%s': unknown extension."), filename
.c_str());
1111 bool wxImage::SaveFile( const wxString
& filename
, int type
) const
1114 ((wxImage
*)this)->SetOption(wxIMAGE_OPTION_FILENAME
, filename
);
1116 wxFileOutputStream
stream(filename
);
1118 if ( stream
.IsOk() )
1120 wxBufferedOutputStream
bstream( stream
);
1121 return SaveFile(bstream
, type
);
1123 #endif // wxUSE_STREAMS
1128 bool wxImage::SaveFile( const wxString
& filename
, const wxString
& mimetype
) const
1131 ((wxImage
*)this)->SetOption(wxIMAGE_OPTION_FILENAME
, filename
);
1133 wxFileOutputStream
stream(filename
);
1135 if ( stream
.IsOk() )
1137 wxBufferedOutputStream
bstream( stream
);
1138 return SaveFile(bstream
, mimetype
);
1140 #endif // wxUSE_STREAMS
1145 bool wxImage::CanRead( const wxString
&name
)
1148 wxFileInputStream
stream(name
);
1149 return CanRead(stream
);
1155 int wxImage::GetImageCount( const wxString
&name
, long type
)
1158 wxFileInputStream
stream(name
);
1160 return GetImageCount(stream
, type
);
1168 bool wxImage::CanRead( wxInputStream
&stream
)
1170 const wxList
& list
= GetHandlers();
1172 for ( wxList::compatibility_iterator node
= list
.GetFirst(); node
; node
= node
->GetNext() )
1174 wxImageHandler
*handler
=(wxImageHandler
*)node
->GetData();
1175 if (handler
->CanRead( stream
))
1182 int wxImage::GetImageCount( wxInputStream
&stream
, long type
)
1184 wxImageHandler
*handler
;
1186 if ( type
== wxBITMAP_TYPE_ANY
)
1188 wxList
&list
=GetHandlers();
1190 for (wxList::compatibility_iterator node
= list
.GetFirst(); node
; node
= node
->GetNext())
1192 handler
=(wxImageHandler
*)node
->GetData();
1193 if ( handler
->CanRead(stream
) )
1194 return handler
->GetImageCount(stream
);
1198 wxLogWarning(_("No handler found for image type."));
1202 handler
= FindHandler(type
);
1206 wxLogWarning(_("No image handler for type %d defined."), type
);
1210 if ( handler
->CanRead(stream
) )
1212 return handler
->GetImageCount(stream
);
1216 wxLogError(_("Image file is not of type %d."), type
);
1221 bool wxImage::LoadFile( wxInputStream
& stream
, long type
, int index
)
1225 m_refData
= new wxImageRefData
;
1227 wxImageHandler
*handler
;
1229 if ( type
== wxBITMAP_TYPE_ANY
)
1231 wxList
&list
=GetHandlers();
1233 for ( wxList::compatibility_iterator node
= list
.GetFirst(); node
; node
= node
->GetNext() )
1235 handler
=(wxImageHandler
*)node
->GetData();
1236 if ( handler
->CanRead(stream
) )
1237 return handler
->LoadFile(this, stream
, true/*verbose*/, index
);
1241 wxLogWarning( _("No handler found for image type.") );
1245 handler
= FindHandler(type
);
1249 wxLogWarning( _("No image handler for type %d defined."), type
);
1254 return handler
->LoadFile(this, stream
, true/*verbose*/, index
);
1257 bool wxImage::LoadFile( wxInputStream
& stream
, const wxString
& mimetype
, int index
)
1261 m_refData
= new wxImageRefData
;
1263 wxImageHandler
*handler
= FindHandlerMime(mimetype
);
1267 wxLogWarning( _("No image handler for type %s defined."), mimetype
.GetData() );
1272 return handler
->LoadFile( this, stream
, true/*verbose*/, index
);
1275 bool wxImage::SaveFile( wxOutputStream
& stream
, int type
) const
1277 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1279 wxImageHandler
*handler
= FindHandler(type
);
1283 wxLogWarning( _("No image handler for type %d defined."), type
);
1288 return handler
->SaveFile( (wxImage
*)this, stream
);
1291 bool wxImage::SaveFile( wxOutputStream
& stream
, const wxString
& mimetype
) const
1293 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1295 wxImageHandler
*handler
= FindHandlerMime(mimetype
);
1299 wxLogWarning( _("No image handler for type %s defined."), mimetype
.GetData() );
1304 return handler
->SaveFile( (wxImage
*)this, stream
);
1306 #endif // wxUSE_STREAMS
1308 void wxImage::AddHandler( wxImageHandler
*handler
)
1310 // Check for an existing handler of the type being added.
1311 if (FindHandler( handler
->GetType() ) == 0)
1313 sm_handlers
.Append( handler
);
1317 // This is not documented behaviour, merely the simplest 'fix'
1318 // for preventing duplicate additions. If someone ever has
1319 // a good reason to add and remove duplicate handlers (and they
1320 // may) we should probably refcount the duplicates.
1321 // also an issue in InsertHandler below.
1323 wxLogDebug( _T("Adding duplicate image handler for '%s'"),
1324 handler
->GetName().c_str() );
1329 void wxImage::InsertHandler( wxImageHandler
*handler
)
1331 // Check for an existing handler of the type being added.
1332 if (FindHandler( handler
->GetType() ) == 0)
1334 sm_handlers
.Insert( handler
);
1338 // see AddHandler for additional comments.
1339 wxLogDebug( _T("Inserting duplicate image handler for '%s'"),
1340 handler
->GetName().c_str() );
1345 bool wxImage::RemoveHandler( const wxString
& name
)
1347 wxImageHandler
*handler
= FindHandler(name
);
1350 sm_handlers
.DeleteObject(handler
);
1358 wxImageHandler
*wxImage::FindHandler( const wxString
& name
)
1360 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
1363 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
1364 if (handler
->GetName().Cmp(name
) == 0) return handler
;
1366 node
= node
->GetNext();
1371 wxImageHandler
*wxImage::FindHandler( const wxString
& extension
, long bitmapType
)
1373 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
1376 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
1377 if ( (handler
->GetExtension().Cmp(extension
) == 0) &&
1378 (bitmapType
== -1 || handler
->GetType() == bitmapType
) )
1380 node
= node
->GetNext();
1385 wxImageHandler
*wxImage::FindHandler( long bitmapType
)
1387 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
1390 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
1391 if (handler
->GetType() == bitmapType
) return handler
;
1392 node
= node
->GetNext();
1397 wxImageHandler
*wxImage::FindHandlerMime( const wxString
& mimetype
)
1399 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
1402 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
1403 if (handler
->GetMimeType().IsSameAs(mimetype
, false)) return handler
;
1404 node
= node
->GetNext();
1409 void wxImage::InitStandardHandlers()
1412 AddHandler(new wxBMPHandler
);
1413 #endif // wxUSE_STREAMS
1416 void wxImage::CleanUpHandlers()
1418 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
1421 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
1422 wxList::compatibility_iterator next
= node
->GetNext();
1427 sm_handlers
.Clear();
1430 wxString
wxImage::GetImageExtWildcard()
1434 wxList
& Handlers
= wxImage::GetHandlers();
1435 wxList::compatibility_iterator Node
= Handlers
.GetFirst();
1438 wxImageHandler
* Handler
= (wxImageHandler
*)Node
->GetData();
1439 fmts
+= wxT("*.") + Handler
->GetExtension();
1440 Node
= Node
->GetNext();
1441 if ( Node
) fmts
+= wxT(";");
1444 return wxT("(") + fmts
+ wxT(")|") + fmts
;
1447 //-----------------------------------------------------------------------------
1449 //-----------------------------------------------------------------------------
1451 IMPLEMENT_ABSTRACT_CLASS(wxImageHandler
,wxObject
)
1454 bool wxImageHandler::LoadFile( wxImage
*WXUNUSED(image
), wxInputStream
& WXUNUSED(stream
), bool WXUNUSED(verbose
), int WXUNUSED(index
) )
1459 bool wxImageHandler::SaveFile( wxImage
*WXUNUSED(image
), wxOutputStream
& WXUNUSED(stream
), bool WXUNUSED(verbose
) )
1464 int wxImageHandler::GetImageCount( wxInputStream
& WXUNUSED(stream
) )
1469 bool wxImageHandler::CanRead( const wxString
& name
)
1471 if (wxFileExists(name
))
1473 wxFileInputStream
stream(name
);
1474 return CanRead(stream
);
1477 wxLogError( _("Can't check image format of file '%s': file does not exist."), name
.c_str() );
1482 bool wxImageHandler::CallDoCanRead(wxInputStream
& stream
)
1484 wxFileOffset posOld
= stream
.TellI();
1485 if ( posOld
== wxInvalidOffset
)
1487 // can't test unseekable stream
1491 bool ok
= DoCanRead(stream
);
1493 // restore the old position to be able to test other formats and so on
1494 if ( stream
.SeekI(posOld
) == wxInvalidOffset
)
1496 wxLogDebug(_T("Failed to rewind the stream in wxImageHandler!"));
1498 // reading would fail anyhow as we're not at the right position
1505 #endif // wxUSE_STREAMS
1507 // ----------------------------------------------------------------------------
1508 // image histogram stuff
1509 // ----------------------------------------------------------------------------
1512 wxImageHistogram::FindFirstUnusedColour(unsigned char *r
,
1517 unsigned char g2
) const
1519 unsigned long key
= MakeKey(r2
, g2
, b2
);
1521 while ( find(key
) != end() )
1523 // color already used
1535 wxLogError(_("No unused colour in image.") );
1541 key
= MakeKey(r2
, g2
, b2
);
1555 wxImage::FindFirstUnusedColour(unsigned char *r
,
1560 unsigned char g2
) const
1562 wxImageHistogram histogram
;
1564 ComputeHistogram(histogram
);
1566 return histogram
.FindFirstUnusedColour(r
, g
, b
, r2
, g2
, b2
);
1572 // Counts and returns the number of different colours. Optionally stops
1573 // when it exceeds 'stopafter' different colours. This is useful, for
1574 // example, to see if the image can be saved as 8-bit (256 colour or
1575 // less, in this case it would be invoked as CountColours(256)). Default
1576 // value for stopafter is -1 (don't care).
1578 unsigned long wxImage::CountColours( unsigned long stopafter
) const
1582 unsigned char r
, g
, b
;
1584 unsigned long size
, nentries
, key
;
1587 size
= GetWidth() * GetHeight();
1590 for (unsigned long j
= 0; (j
< size
) && (nentries
<= stopafter
) ; j
++)
1595 key
= wxImageHistogram::MakeKey(r
, g
, b
);
1597 if (h
.Get(key
) == NULL
)
1608 unsigned long wxImage::ComputeHistogram( wxImageHistogram
&h
) const
1610 unsigned char *p
= GetData();
1611 unsigned long nentries
= 0;
1615 const unsigned long size
= GetWidth() * GetHeight();
1617 unsigned char r
, g
, b
;
1618 for ( unsigned long n
= 0; n
< size
; n
++ )
1624 wxImageHistogramEntry
& entry
= h
[wxImageHistogram::MakeKey(r
, g
, b
)];
1626 if ( entry
.value
++ == 0 )
1627 entry
.index
= nentries
++;
1634 * Rotation code by Carlos Moreno
1637 // GRG: I've removed wxRotationPoint - we already have wxRealPoint which
1638 // does exactly the same thing. And I also got rid of wxRotationPixel
1639 // bacause of potential problems in architectures where alignment
1640 // is an issue, so I had to rewrite parts of the code.
1642 static const double gs_Epsilon
= 1e-10;
1644 static inline int wxCint (double x
)
1646 return (x
> 0) ? (int) (x
+ 0.5) : (int) (x
- 0.5);
1650 // Auxiliary function to rotate a point (x,y) with respect to point p0
1651 // make it inline and use a straight return to facilitate optimization
1652 // also, the function receives the sine and cosine of the angle to avoid
1653 // repeating the time-consuming calls to these functions -- sin/cos can
1654 // be computed and stored in the calling function.
1656 inline wxRealPoint
rotated_point (const wxRealPoint
& p
, double cos_angle
, double sin_angle
, const wxRealPoint
& p0
)
1658 return wxRealPoint (p0
.x
+ (p
.x
- p0
.x
) * cos_angle
- (p
.y
- p0
.y
) * sin_angle
,
1659 p0
.y
+ (p
.y
- p0
.y
) * cos_angle
+ (p
.x
- p0
.x
) * sin_angle
);
1662 inline wxRealPoint
rotated_point (double x
, double y
, double cos_angle
, double sin_angle
, const wxRealPoint
& p0
)
1664 return rotated_point (wxRealPoint(x
,y
), cos_angle
, sin_angle
, p0
);
1667 wxImage
wxImage::Rotate(double angle
, const wxPoint
& centre_of_rotation
, bool interpolating
, wxPoint
* offset_after_rotation
) const
1670 angle
= -angle
; // screen coordinates are a mirror image of "real" coordinates
1672 // Create pointer-based array to accelerate access to wxImage's data
1673 unsigned char ** data
= new unsigned char * [GetHeight()];
1675 data
[0] = GetData();
1677 for (i
= 1; i
< GetHeight(); i
++)
1678 data
[i
] = data
[i
- 1] + (3 * GetWidth());
1680 // precompute coefficients for rotation formula
1681 // (sine and cosine of the angle)
1682 const double cos_angle
= cos(angle
);
1683 const double sin_angle
= sin(angle
);
1685 // Create new Image to store the result
1686 // First, find rectangle that covers the rotated image; to do that,
1687 // rotate the four corners
1689 const wxRealPoint
p0(centre_of_rotation
.x
, centre_of_rotation
.y
);
1691 wxRealPoint p1
= rotated_point (0, 0, cos_angle
, sin_angle
, p0
);
1692 wxRealPoint p2
= rotated_point (0, GetHeight(), cos_angle
, sin_angle
, p0
);
1693 wxRealPoint p3
= rotated_point (GetWidth(), 0, cos_angle
, sin_angle
, p0
);
1694 wxRealPoint p4
= rotated_point (GetWidth(), GetHeight(), cos_angle
, sin_angle
, p0
);
1696 int x1
= (int) floor (wxMin (wxMin(p1
.x
, p2
.x
), wxMin(p3
.x
, p4
.x
)));
1697 int y1
= (int) floor (wxMin (wxMin(p1
.y
, p2
.y
), wxMin(p3
.y
, p4
.y
)));
1698 int x2
= (int) ceil (wxMax (wxMax(p1
.x
, p2
.x
), wxMax(p3
.x
, p4
.x
)));
1699 int y2
= (int) ceil (wxMax (wxMax(p1
.y
, p2
.y
), wxMax(p3
.y
, p4
.y
)));
1701 wxImage
rotated (x2
- x1
+ 1, y2
- y1
+ 1, false);
1703 if (offset_after_rotation
!= NULL
)
1705 *offset_after_rotation
= wxPoint (x1
, y1
);
1708 // GRG: The rotated (destination) image is always accessed
1709 // sequentially, so there is no need for a pointer-based
1710 // array here (and in fact it would be slower).
1712 unsigned char * dst
= rotated
.GetData();
1714 // GRG: if the original image has a mask, use its RGB values
1715 // as the blank pixel, else, fall back to default (black).
1717 unsigned char blank_r
= 0;
1718 unsigned char blank_g
= 0;
1719 unsigned char blank_b
= 0;
1723 blank_r
= GetMaskRed();
1724 blank_g
= GetMaskGreen();
1725 blank_b
= GetMaskBlue();
1726 rotated
.SetMaskColour( blank_r
, blank_g
, blank_b
);
1729 // Now, for each point of the rotated image, find where it came from, by
1730 // performing an inverse rotation (a rotation of -angle) and getting the
1731 // pixel at those coordinates
1733 // GRG: I've taken the (interpolating) test out of the loops, so that
1734 // it is done only once, instead of repeating it for each pixel.
1739 for (int y
= 0; y
< rotated
.GetHeight(); y
++)
1741 for (x
= 0; x
< rotated
.GetWidth(); x
++)
1743 wxRealPoint src
= rotated_point (x
+ x1
, y
+ y1
, cos_angle
, -sin_angle
, p0
);
1745 if (-0.25 < src
.x
&& src
.x
< GetWidth() - 0.75 &&
1746 -0.25 < src
.y
&& src
.y
< GetHeight() - 0.75)
1748 // interpolate using the 4 enclosing grid-points. Those
1749 // points can be obtained using floor and ceiling of the
1750 // exact coordinates of the point
1751 // C.M. 2000-02-17: when the point is near the border, special care is required.
1755 if (0 < src
.x
&& src
.x
< GetWidth() - 1)
1757 x1
= wxCint(floor(src
.x
));
1758 x2
= wxCint(ceil(src
.x
));
1760 else // else means that x is near one of the borders (0 or width-1)
1762 x1
= x2
= wxCint (src
.x
);
1765 if (0 < src
.y
&& src
.y
< GetHeight() - 1)
1767 y1
= wxCint(floor(src
.y
));
1768 y2
= wxCint(ceil(src
.y
));
1772 y1
= y2
= wxCint (src
.y
);
1775 // get four points and the distances (square of the distance,
1776 // for efficiency reasons) for the interpolation formula
1778 // GRG: Do not calculate the points until they are
1779 // really needed -- this way we can calculate
1780 // just one, instead of four, if d1, d2, d3
1781 // or d4 are < gs_Epsilon
1783 const double d1
= (src
.x
- x1
) * (src
.x
- x1
) + (src
.y
- y1
) * (src
.y
- y1
);
1784 const double d2
= (src
.x
- x2
) * (src
.x
- x2
) + (src
.y
- y1
) * (src
.y
- y1
);
1785 const double d3
= (src
.x
- x2
) * (src
.x
- x2
) + (src
.y
- y2
) * (src
.y
- y2
);
1786 const double d4
= (src
.x
- x1
) * (src
.x
- x1
) + (src
.y
- y2
) * (src
.y
- y2
);
1788 // Now interpolate as a weighted average of the four surrounding
1789 // points, where the weights are the distances to each of those points
1791 // If the point is exactly at one point of the grid of the source
1792 // image, then don't interpolate -- just assign the pixel
1794 if (d1
< gs_Epsilon
) // d1,d2,d3,d4 are positive -- no need for abs()
1796 unsigned char *p
= data
[y1
] + (3 * x1
);
1801 else if (d2
< gs_Epsilon
)
1803 unsigned char *p
= data
[y1
] + (3 * x2
);
1808 else if (d3
< gs_Epsilon
)
1810 unsigned char *p
= data
[y2
] + (3 * x2
);
1815 else if (d4
< gs_Epsilon
)
1817 unsigned char *p
= data
[y2
] + (3 * x1
);
1824 // weights for the weighted average are proportional to the inverse of the distance
1825 unsigned char *v1
= data
[y1
] + (3 * x1
);
1826 unsigned char *v2
= data
[y1
] + (3 * x2
);
1827 unsigned char *v3
= data
[y2
] + (3 * x2
);
1828 unsigned char *v4
= data
[y2
] + (3 * x1
);
1830 const double w1
= 1/d1
, w2
= 1/d2
, w3
= 1/d3
, w4
= 1/d4
;
1834 *(dst
++) = (unsigned char)
1835 ( (w1
* *(v1
++) + w2
* *(v2
++) +
1836 w3
* *(v3
++) + w4
* *(v4
++)) /
1837 (w1
+ w2
+ w3
+ w4
) );
1838 *(dst
++) = (unsigned char)
1839 ( (w1
* *(v1
++) + w2
* *(v2
++) +
1840 w3
* *(v3
++) + w4
* *(v4
++)) /
1841 (w1
+ w2
+ w3
+ w4
) );
1842 *(dst
++) = (unsigned char)
1843 ( (w1
* *v1
+ w2
* *v2
+
1844 w3
* *v3
+ w4
* *v4
) /
1845 (w1
+ w2
+ w3
+ w4
) );
1857 else // not interpolating
1859 for (int y
= 0; y
< rotated
.GetHeight(); y
++)
1861 for (x
= 0; x
< rotated
.GetWidth(); x
++)
1863 wxRealPoint src
= rotated_point (x
+ x1
, y
+ y1
, cos_angle
, -sin_angle
, p0
);
1865 const int xs
= wxCint (src
.x
); // wxCint rounds to the
1866 const int ys
= wxCint (src
.y
); // closest integer
1868 if (0 <= xs
&& xs
< GetWidth() &&
1869 0 <= ys
&& ys
< GetHeight())
1871 unsigned char *p
= data
[ys
] + (3 * xs
);
1895 // A module to allow wxImage initialization/cleanup
1896 // without calling these functions from app.cpp or from
1897 // the user's application.
1899 class wxImageModule
: public wxModule
1901 DECLARE_DYNAMIC_CLASS(wxImageModule
)
1904 bool OnInit() { wxImage::InitStandardHandlers(); return true; };
1905 void OnExit() { wxImage::CleanUpHandlers(); };
1908 IMPLEMENT_DYNAMIC_CLASS(wxImageModule
, wxModule
)
1911 #endif // wxUSE_IMAGE