1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Robert Roebling
6 // Copyright: (c) Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
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
)
117 wxImage::wxImage( int width
, int height
, bool clear
)
119 Create( width
, height
, clear
);
122 wxImage::wxImage( int width
, int height
, unsigned char* data
, bool static_data
)
124 Create( width
, height
, data
, static_data
);
127 wxImage::wxImage( const wxString
& name
, long type
, int index
)
129 LoadFile( name
, type
, index
);
132 wxImage::wxImage( const wxString
& name
, const wxString
& mimetype
, int index
)
134 LoadFile( name
, mimetype
, index
);
138 wxImage::wxImage( wxInputStream
& stream
, long type
, int index
)
140 LoadFile( stream
, type
, index
);
143 wxImage::wxImage( wxInputStream
& stream
, const wxString
& mimetype
, int index
)
145 LoadFile( stream
, mimetype
, index
);
147 #endif // wxUSE_STREAMS
149 wxImage::wxImage( const wxImage
& image
)
155 wxImage::wxImage( const wxImage
* image
)
157 if (image
) Ref(*image
);
160 bool wxImage::Create( int width
, int height
, bool clear
)
164 m_refData
= new wxImageRefData();
166 M_IMGDATA
->m_data
= (unsigned char *) malloc( width
*height
*3 );
167 if (!M_IMGDATA
->m_data
)
174 memset(M_IMGDATA
->m_data
, 0, width
*height
*3);
176 M_IMGDATA
->m_width
= width
;
177 M_IMGDATA
->m_height
= height
;
178 M_IMGDATA
->m_ok
= true;
183 bool wxImage::Create( int width
, int height
, unsigned char* data
, bool static_data
)
187 wxCHECK_MSG( data
, false, _T("NULL data in wxImage::Create") );
189 m_refData
= new wxImageRefData();
191 M_IMGDATA
->m_data
= data
;
192 M_IMGDATA
->m_width
= width
;
193 M_IMGDATA
->m_height
= height
;
194 M_IMGDATA
->m_ok
= true;
195 M_IMGDATA
->m_static
= static_data
;
200 void wxImage::Destroy()
205 wxImage
wxImage::Copy() const
209 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
211 image
.Create( M_IMGDATA
->m_width
, M_IMGDATA
->m_height
, false );
213 unsigned char *data
= image
.GetData();
215 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
217 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
218 image
.SetMask( M_IMGDATA
->m_hasMask
);
220 memcpy( data
, GetData(), M_IMGDATA
->m_width
*M_IMGDATA
->m_height
*3 );
225 wxImage
wxImage::ShrinkBy( int xFactor
, int yFactor
) const
227 if( xFactor
== 1 && yFactor
== 1 )
232 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
234 // can't scale to/from 0 size
235 wxCHECK_MSG( (xFactor
> 0) && (yFactor
> 0), image
,
236 wxT("invalid new image size") );
238 long old_height
= M_IMGDATA
->m_height
,
239 old_width
= M_IMGDATA
->m_width
;
241 wxCHECK_MSG( (old_height
> 0) && (old_width
> 0), image
,
242 wxT("invalid old image size") );
244 long width
= old_width
/ xFactor
;
245 long height
= old_height
/ yFactor
;
247 image
.Create( width
, height
, false );
249 char unsigned *data
= image
.GetData();
251 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
253 bool hasMask
= false ;
254 unsigned char maskRed
= 0;
255 unsigned char maskGreen
= 0;
256 unsigned char maskBlue
=0 ;
257 if (M_IMGDATA
->m_hasMask
)
260 maskRed
= M_IMGDATA
->m_maskRed
;
261 maskGreen
= M_IMGDATA
->m_maskGreen
;
262 maskBlue
=M_IMGDATA
->m_maskBlue
;
264 image
.SetMaskColour( M_IMGDATA
->m_maskRed
,
265 M_IMGDATA
->m_maskGreen
,
266 M_IMGDATA
->m_maskBlue
);
268 char unsigned *source_data
= M_IMGDATA
->m_data
;
269 char unsigned *target_data
= data
;
271 for (long y
= 0; y
< height
; y
++)
273 for (long x
= 0; x
< width
; x
++)
275 unsigned long avgRed
= 0 ;
276 unsigned long avgGreen
= 0;
277 unsigned long avgBlue
= 0;
278 unsigned long counter
= 0 ;
280 for ( int y1
= 0 ; y1
< yFactor
; ++y1
)
282 long y_offset
= (y
* yFactor
+ y1
) * old_width
;
283 for ( int x1
= 0 ; x1
< xFactor
; ++x1
)
285 unsigned char *pixel
= source_data
+ 3 * ( y_offset
+ x
* xFactor
+ x1
) ;
286 unsigned char red
= pixel
[0] ;
287 unsigned char green
= pixel
[1] ;
288 unsigned char blue
= pixel
[2] ;
289 if ( !hasMask
|| red
!= maskRed
|| green
!= maskGreen
|| blue
!= maskBlue
)
300 *(target_data
++) = M_IMGDATA
->m_maskRed
;
301 *(target_data
++) = M_IMGDATA
->m_maskGreen
;
302 *(target_data
++) = M_IMGDATA
->m_maskBlue
;
306 *(target_data
++) = avgRed
/ counter
;
307 *(target_data
++) = avgGreen
/ counter
;
308 *(target_data
++) = avgBlue
/ counter
;
313 // In case this is a cursor, make sure the hotspot is scalled accordingly:
314 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
) )
315 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
,
316 (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X
))/xFactor
);
317 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
) )
318 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
,
319 (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y
))/yFactor
);
324 wxImage
wxImage::Scale( int width
, int height
) const
328 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
330 // can't scale to/from 0 size
331 wxCHECK_MSG( (width
> 0) && (height
> 0), image
,
332 wxT("invalid new image size") );
334 long old_height
= M_IMGDATA
->m_height
,
335 old_width
= M_IMGDATA
->m_width
;
336 wxCHECK_MSG( (old_height
> 0) && (old_width
> 0), image
,
337 wxT("invalid old image size") );
339 if ( old_width
% width
== 0 && old_width
>= width
&&
340 old_height
% height
== 0 && old_height
>= height
)
342 return ShrinkBy( old_width
/ width
, old_height
/ height
) ;
344 image
.Create( width
, height
, false );
346 unsigned char *data
= image
.GetData();
348 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
350 if (M_IMGDATA
->m_hasMask
)
352 image
.SetMaskColour( M_IMGDATA
->m_maskRed
,
353 M_IMGDATA
->m_maskGreen
,
354 M_IMGDATA
->m_maskBlue
);
357 unsigned char *source_data
= M_IMGDATA
->m_data
;
358 unsigned char *target_data
= data
;
360 long x_delta
= (old_width
<<16) / width
;
361 long y_delta
= (old_height
<<16) / height
;
363 unsigned char* dest_pixel
= target_data
;
366 for ( long j
= 0; j
< height
; j
++ )
368 unsigned char* src_line
= &source_data
[(y
>>16)*old_width
*3];
371 for ( long i
= 0; i
< width
; i
++ )
373 unsigned char* src_pixel
= &src_line
[(x
>>16)*3];
374 dest_pixel
[0] = src_pixel
[0];
375 dest_pixel
[1] = src_pixel
[1];
376 dest_pixel
[2] = src_pixel
[2];
384 // In case this is a cursor, make sure the hotspot is scalled accordingly:
385 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
) )
386 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
,
387 (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X
)*width
)/old_width
);
388 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
) )
389 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
,
390 (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y
)*height
)/old_height
);
395 wxImage
wxImage::Rotate90( bool clockwise
) const
399 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
401 image
.Create( M_IMGDATA
->m_height
, M_IMGDATA
->m_width
, false );
403 unsigned char *data
= image
.GetData();
405 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
407 if (M_IMGDATA
->m_hasMask
)
408 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
410 long height
= M_IMGDATA
->m_height
;
411 long width
= M_IMGDATA
->m_width
;
413 unsigned char *source_data
= M_IMGDATA
->m_data
;
414 unsigned char *target_data
;
416 for (long j
= 0; j
< height
; j
++)
418 for (long i
= 0; i
< width
; i
++)
421 target_data
= data
+ (((i
+1)*height
) - j
- 1)*3;
423 target_data
= data
+ ((height
*(width
-1)) + j
- (i
*height
))*3;
424 memcpy( target_data
, source_data
, 3 );
432 wxImage
wxImage::Mirror( bool horizontally
) const
436 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
438 image
.Create( M_IMGDATA
->m_width
, M_IMGDATA
->m_height
, false );
440 unsigned char *data
= image
.GetData();
442 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
444 if (M_IMGDATA
->m_hasMask
)
445 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
447 long height
= M_IMGDATA
->m_height
;
448 long width
= M_IMGDATA
->m_width
;
450 unsigned char *source_data
= M_IMGDATA
->m_data
;
451 unsigned char *target_data
;
455 for (long j
= 0; j
< height
; j
++)
458 target_data
= data
-3;
459 for (long i
= 0; i
< width
; i
++)
461 memcpy( target_data
, source_data
, 3 );
469 for (long i
= 0; i
< height
; i
++)
471 target_data
= data
+ 3*width
*(height
-1-i
);
472 memcpy( target_data
, source_data
, (size_t)3*width
);
473 source_data
+= 3*width
;
480 wxImage
wxImage::GetSubImage( const wxRect
&rect
) const
484 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
486 wxCHECK_MSG( (rect
.GetLeft()>=0) && (rect
.GetTop()>=0) && (rect
.GetRight()<=GetWidth()) && (rect
.GetBottom()<=GetHeight()),
487 image
, wxT("invalid subimage size") );
489 int subwidth
=rect
.GetWidth();
490 const int subheight
=rect
.GetHeight();
492 image
.Create( subwidth
, subheight
, false );
494 unsigned char *subdata
= image
.GetData(), *data
=GetData();
496 wxCHECK_MSG( subdata
, image
, wxT("unable to create image") );
498 if (M_IMGDATA
->m_hasMask
)
499 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
501 const int subleft
=3*rect
.GetLeft();
502 const int width
=3*GetWidth();
505 data
+=rect
.GetTop()*width
+subleft
;
507 for (long j
= 0; j
< subheight
; ++j
)
509 memcpy( subdata
, data
, subwidth
);
517 void wxImage::Paste( const wxImage
&image
, int x
, int y
)
519 wxCHECK_RET( Ok(), wxT("invalid image") );
520 wxCHECK_RET( image
.Ok(), wxT("invalid image") );
524 int width
= image
.GetWidth();
525 int height
= image
.GetHeight();
538 if ((x
+xx
)+width
> M_IMGDATA
->m_width
)
539 width
= M_IMGDATA
->m_width
- (x
+xx
);
540 if ((y
+yy
)+height
> M_IMGDATA
->m_height
)
541 height
= M_IMGDATA
->m_height
- (y
+yy
);
543 if (width
< 1) return;
544 if (height
< 1) return;
546 if ((!HasMask() && !image
.HasMask()) ||
547 ((HasMask() && image
.HasMask() &&
548 (GetMaskRed()==image
.GetMaskRed()) &&
549 (GetMaskGreen()==image
.GetMaskGreen()) &&
550 (GetMaskBlue()==image
.GetMaskBlue()))))
553 unsigned char* source_data
= image
.GetData() + xx
*3 + yy
*3*image
.GetWidth();
554 int source_step
= image
.GetWidth()*3;
556 unsigned char* target_data
= GetData() + (x
+xx
)*3 + (y
+yy
)*3*M_IMGDATA
->m_width
;
557 int target_step
= M_IMGDATA
->m_width
*3;
558 for (int j
= 0; j
< height
; j
++)
560 memcpy( target_data
, source_data
, width
);
561 source_data
+= source_step
;
562 target_data
+= target_step
;
567 if (!HasMask() && image
.HasMask())
569 unsigned char r
= image
.GetMaskRed();
570 unsigned char g
= image
.GetMaskGreen();
571 unsigned char b
= image
.GetMaskBlue();
574 unsigned char* source_data
= image
.GetData() + xx
*3 + yy
*3*image
.GetWidth();
575 int source_step
= image
.GetWidth()*3;
577 unsigned char* target_data
= GetData() + (x
+xx
)*3 + (y
+yy
)*3*M_IMGDATA
->m_width
;
578 int target_step
= M_IMGDATA
->m_width
*3;
580 for (int j
= 0; j
< height
; j
++)
582 for (int i
= 0; i
< width
; i
+=3)
584 if ((source_data
[i
] != r
) &&
585 (source_data
[i
+1] != g
) &&
586 (source_data
[i
+2] != b
))
588 memcpy( target_data
+i
, source_data
+i
, 3 );
591 source_data
+= source_step
;
592 target_data
+= target_step
;
597 void wxImage::Replace( unsigned char r1
, unsigned char g1
, unsigned char b1
,
598 unsigned char r2
, unsigned char g2
, unsigned char b2
)
600 wxCHECK_RET( Ok(), wxT("invalid image") );
602 unsigned char *data
= GetData();
604 const int w
= GetWidth();
605 const int h
= GetHeight();
607 for (int j
= 0; j
< h
; j
++)
608 for (int i
= 0; i
< w
; i
++)
610 if ((data
[0] == r1
) && (data
[1] == g1
) && (data
[2] == b1
))
620 wxImage
wxImage::ConvertToMono( unsigned char r
, unsigned char g
, unsigned char b
) const
624 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
626 image
.Create( M_IMGDATA
->m_width
, M_IMGDATA
->m_height
, false );
628 unsigned char *data
= image
.GetData();
630 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
632 if (M_IMGDATA
->m_hasMask
)
634 if (M_IMGDATA
->m_maskRed
== r
&& M_IMGDATA
->m_maskGreen
== g
&&
635 M_IMGDATA
->m_maskBlue
== b
)
636 image
.SetMaskColour( 255, 255, 255 );
638 image
.SetMaskColour( 0, 0, 0 );
641 long size
= M_IMGDATA
->m_height
* M_IMGDATA
->m_width
;
643 unsigned char *srcd
= M_IMGDATA
->m_data
;
644 unsigned char *tard
= image
.GetData();
646 for ( long i
= 0; i
< size
; i
++, srcd
+= 3, tard
+= 3 )
648 if (srcd
[0] == r
&& srcd
[1] == g
&& srcd
[2] == b
)
649 tard
[0] = tard
[1] = tard
[2] = 255;
651 tard
[0] = tard
[1] = tard
[2] = 0;
657 void wxImage::SetRGB( int x
, int y
, unsigned char r
, unsigned char g
, unsigned char b
)
659 wxCHECK_RET( Ok(), wxT("invalid image") );
661 int w
= M_IMGDATA
->m_width
;
662 int h
= M_IMGDATA
->m_height
;
664 wxCHECK_RET( (x
>=0) && (y
>=0) && (x
<w
) && (y
<h
), wxT("invalid image index") );
666 long pos
= (y
* w
+ x
) * 3;
668 M_IMGDATA
->m_data
[ pos
] = r
;
669 M_IMGDATA
->m_data
[ pos
+1 ] = g
;
670 M_IMGDATA
->m_data
[ pos
+2 ] = b
;
673 unsigned char wxImage::GetRed( int x
, int y
) const
675 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
677 int w
= M_IMGDATA
->m_width
;
678 int h
= M_IMGDATA
->m_height
;
680 wxCHECK_MSG( (x
>=0) && (y
>=0) && (x
<w
) && (y
<h
), 0, wxT("invalid image index") );
682 long pos
= (y
* w
+ x
) * 3;
684 return M_IMGDATA
->m_data
[pos
];
687 unsigned char wxImage::GetGreen( int x
, int y
) const
689 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
691 int w
= M_IMGDATA
->m_width
;
692 int h
= M_IMGDATA
->m_height
;
694 wxCHECK_MSG( (x
>=0) && (y
>=0) && (x
<w
) && (y
<h
), 0, wxT("invalid image index") );
696 long pos
= (y
* w
+ x
) * 3;
698 return M_IMGDATA
->m_data
[pos
+1];
701 unsigned char wxImage::GetBlue( int x
, int y
) const
703 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
705 int w
= M_IMGDATA
->m_width
;
706 int h
= M_IMGDATA
->m_height
;
708 wxCHECK_MSG( (x
>=0) && (y
>=0) && (x
<w
) && (y
<h
), 0, wxT("invalid image index") );
710 long pos
= (y
* w
+ x
) * 3;
712 return M_IMGDATA
->m_data
[pos
+2];
715 bool wxImage::Ok() const
717 // image of 0 width or height can't be considered ok - at least because it
718 // causes crashes in ConvertToBitmap() if we don't catch it in time
719 wxImageRefData
*data
= M_IMGDATA
;
720 return data
&& data
->m_ok
&& data
->m_width
&& data
->m_height
;
723 unsigned char *wxImage::GetData() const
725 wxCHECK_MSG( Ok(), (unsigned char *)NULL
, wxT("invalid image") );
727 return M_IMGDATA
->m_data
;
730 void wxImage::SetData( unsigned char *data
)
732 wxCHECK_RET( Ok(), wxT("invalid image") );
734 wxImageRefData
*newRefData
= new wxImageRefData();
736 newRefData
->m_width
= M_IMGDATA
->m_width
;
737 newRefData
->m_height
= M_IMGDATA
->m_height
;
738 newRefData
->m_data
= data
;
739 newRefData
->m_ok
= true;
740 newRefData
->m_maskRed
= M_IMGDATA
->m_maskRed
;
741 newRefData
->m_maskGreen
= M_IMGDATA
->m_maskGreen
;
742 newRefData
->m_maskBlue
= M_IMGDATA
->m_maskBlue
;
743 newRefData
->m_hasMask
= M_IMGDATA
->m_hasMask
;
747 m_refData
= newRefData
;
750 void wxImage::SetData( unsigned char *data
, int new_width
, int new_height
)
752 wxImageRefData
*newRefData
= new wxImageRefData();
756 newRefData
->m_width
= new_width
;
757 newRefData
->m_height
= new_height
;
758 newRefData
->m_data
= data
;
759 newRefData
->m_ok
= true;
760 newRefData
->m_maskRed
= M_IMGDATA
->m_maskRed
;
761 newRefData
->m_maskGreen
= M_IMGDATA
->m_maskGreen
;
762 newRefData
->m_maskBlue
= M_IMGDATA
->m_maskBlue
;
763 newRefData
->m_hasMask
= M_IMGDATA
->m_hasMask
;
767 newRefData
->m_width
= new_width
;
768 newRefData
->m_height
= new_height
;
769 newRefData
->m_data
= data
;
770 newRefData
->m_ok
= true;
775 m_refData
= newRefData
;
778 // ----------------------------------------------------------------------------
779 // alpha channel support
780 // ----------------------------------------------------------------------------
782 void wxImage::SetAlpha(int x
, int y
, unsigned char alpha
)
784 wxCHECK_RET( Ok() && HasAlpha(), wxT("invalid image or no alpha channel") );
786 int w
= M_IMGDATA
->m_width
,
787 h
= M_IMGDATA
->m_height
;
789 wxCHECK_RET( x
>=0 && y
>= 0 && x
< w
&& y
< h
, wxT("invalid image index") );
791 M_IMGDATA
->m_alpha
[y
*w
+ x
] = alpha
;
794 unsigned char wxImage::GetAlpha(int x
, int y
)
796 wxCHECK_MSG( Ok() && HasAlpha(), 0, wxT("invalid image or no alpha channel") );
798 int w
= M_IMGDATA
->m_width
,
799 h
= M_IMGDATA
->m_height
;
801 wxCHECK_MSG( x
>=0 && y
>= 0 && x
< w
&& y
< h
, 0, wxT("invalid image index") );
803 return M_IMGDATA
->m_alpha
[y
*w
+ x
];
806 void wxImage::SetAlpha( unsigned char *alpha
)
808 wxCHECK_RET( Ok(), wxT("invalid image") );
812 alpha
= (unsigned char *)malloc(M_IMGDATA
->m_width
*M_IMGDATA
->m_height
);
815 delete [] M_IMGDATA
->m_alpha
;
816 M_IMGDATA
->m_alpha
= alpha
;
819 unsigned char *wxImage::GetAlpha() const
821 wxCHECK_MSG( Ok(), (unsigned char *)NULL
, wxT("invalid image") );
823 return M_IMGDATA
->m_alpha
;
826 // ----------------------------------------------------------------------------
828 // ----------------------------------------------------------------------------
830 void wxImage::SetMaskColour( unsigned char r
, unsigned char g
, unsigned char b
)
832 wxCHECK_RET( Ok(), wxT("invalid image") );
834 M_IMGDATA
->m_maskRed
= r
;
835 M_IMGDATA
->m_maskGreen
= g
;
836 M_IMGDATA
->m_maskBlue
= b
;
837 M_IMGDATA
->m_hasMask
= true;
840 unsigned char wxImage::GetMaskRed() const
842 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
844 return M_IMGDATA
->m_maskRed
;
847 unsigned char wxImage::GetMaskGreen() const
849 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
851 return M_IMGDATA
->m_maskGreen
;
854 unsigned char wxImage::GetMaskBlue() const
856 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
858 return M_IMGDATA
->m_maskBlue
;
861 void wxImage::SetMask( bool mask
)
863 wxCHECK_RET( Ok(), wxT("invalid image") );
865 M_IMGDATA
->m_hasMask
= mask
;
868 bool wxImage::HasMask() const
870 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
872 return M_IMGDATA
->m_hasMask
;
875 int wxImage::GetWidth() const
877 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
879 return M_IMGDATA
->m_width
;
882 int wxImage::GetHeight() const
884 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
886 return M_IMGDATA
->m_height
;
889 bool wxImage::SetMaskFromImage(const wxImage
& mask
,
890 unsigned char mr
, unsigned char mg
, unsigned char mb
)
892 // check that the images are the same size
893 if ( (M_IMGDATA
->m_height
!= mask
.GetHeight() ) || (M_IMGDATA
->m_width
!= mask
.GetWidth () ) )
895 wxLogError( _("Image and Mask have different sizes") );
899 // find unused colour
900 unsigned char r
,g
,b
;
901 if (!FindFirstUnusedColour(&r
, &g
, &b
))
903 wxLogError( _("No Unused Color in image being masked") );
907 unsigned char *imgdata
= GetData();
908 unsigned char *maskdata
= mask
.GetData();
910 const int w
= GetWidth();
911 const int h
= GetHeight();
913 for (int j
= 0; j
< h
; j
++)
915 for (int i
= 0; i
< w
; i
++)
917 if ((maskdata
[0] == mr
) && (maskdata
[1] == mg
) && (maskdata
[2] == mb
))
928 SetMaskColour(r
, g
, b
);
938 bool wxImage::HasPalette() const
943 return M_IMGDATA
->m_palette
.Ok();
946 const wxPalette
& wxImage::GetPalette() const
948 wxCHECK_MSG( Ok(), wxNullPalette
, wxT("invalid image") );
950 return M_IMGDATA
->m_palette
;
953 void wxImage::SetPalette(const wxPalette
& palette
)
955 wxCHECK_RET( Ok(), wxT("invalid image") );
957 M_IMGDATA
->m_palette
= palette
;
960 #endif // wxUSE_PALETTE
962 // Option functions (arbitrary name/value mapping)
963 void wxImage::SetOption(const wxString
& name
, const wxString
& value
)
965 wxCHECK_RET( Ok(), wxT("invalid image") );
967 int idx
= M_IMGDATA
->m_optionNames
.Index(name
, false);
968 if (idx
== wxNOT_FOUND
)
970 M_IMGDATA
->m_optionNames
.Add(name
);
971 M_IMGDATA
->m_optionValues
.Add(value
);
975 M_IMGDATA
->m_optionNames
[idx
] = name
;
976 M_IMGDATA
->m_optionValues
[idx
] = value
;
980 void wxImage::SetOption(const wxString
& name
, int value
)
983 valStr
.Printf(wxT("%d"), value
);
984 SetOption(name
, valStr
);
987 wxString
wxImage::GetOption(const wxString
& name
) const
989 wxCHECK_MSG( Ok(), wxEmptyString
, wxT("invalid image") );
991 int idx
= M_IMGDATA
->m_optionNames
.Index(name
, false);
992 if (idx
== wxNOT_FOUND
)
993 return wxEmptyString
;
995 return M_IMGDATA
->m_optionValues
[idx
];
998 int wxImage::GetOptionInt(const wxString
& name
) const
1000 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
1002 return wxAtoi(GetOption(name
));
1005 bool wxImage::HasOption(const wxString
& name
) const
1007 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1009 return (M_IMGDATA
->m_optionNames
.Index(name
, false) != wxNOT_FOUND
);
1012 bool wxImage::LoadFile( const wxString
& filename
, long type
, int index
)
1015 if (wxFileExists(filename
))
1017 wxFileInputStream
stream(filename
);
1018 wxBufferedInputStream
bstream( stream
);
1019 return LoadFile(bstream
, type
, index
);
1023 wxLogError( _("Can't load image from file '%s': file does not exist."), filename
.c_str() );
1027 #else // !wxUSE_STREAMS
1029 #endif // wxUSE_STREAMS
1032 bool wxImage::LoadFile( const wxString
& filename
, const wxString
& mimetype
, int index
)
1035 if (wxFileExists(filename
))
1037 wxFileInputStream
stream(filename
);
1038 wxBufferedInputStream
bstream( stream
);
1039 return LoadFile(bstream
, mimetype
, index
);
1043 wxLogError( _("Can't load image from file '%s': file does not exist."), filename
.c_str() );
1047 #else // !wxUSE_STREAMS
1049 #endif // wxUSE_STREAMS
1054 bool wxImage::SaveFile( const wxString
& filename
) const
1056 wxString ext
= filename
.AfterLast('.').Lower();
1058 wxImageHandler
* pHandler
= FindHandler(ext
, -1);
1061 SaveFile(filename
, pHandler
->GetType());
1065 wxLogError(_("Can't save image to file '%s': unknown extension."), filename
.c_str());
1070 bool wxImage::SaveFile( const wxString
& filename
, int type
) const
1073 ((wxImage
*)this)->SetOption(wxIMAGE_OPTION_FILENAME
, filename
);
1075 wxFileOutputStream
stream(filename
);
1077 if ( stream
.IsOk() )
1079 wxBufferedOutputStream
bstream( stream
);
1080 return SaveFile(bstream
, type
);
1082 #endif // wxUSE_STREAMS
1087 bool wxImage::SaveFile( const wxString
& filename
, const wxString
& mimetype
) const
1090 ((wxImage
*)this)->SetOption(wxIMAGE_OPTION_FILENAME
, filename
);
1092 wxFileOutputStream
stream(filename
);
1094 if ( stream
.IsOk() )
1096 wxBufferedOutputStream
bstream( stream
);
1097 return SaveFile(bstream
, mimetype
);
1099 #endif // wxUSE_STREAMS
1104 bool wxImage::CanRead( const wxString
&name
)
1107 wxFileInputStream
stream(name
);
1108 return CanRead(stream
);
1114 int wxImage::GetImageCount( const wxString
&name
, long type
)
1117 wxFileInputStream
stream(name
);
1119 return GetImageCount(stream
, type
);
1127 bool wxImage::CanRead( wxInputStream
&stream
)
1129 const wxList
& list
= GetHandlers();
1131 for ( wxList::Node
*node
= list
.GetFirst(); node
; node
= node
->GetNext() )
1133 wxImageHandler
*handler
=(wxImageHandler
*)node
->GetData();
1134 if (handler
->CanRead( stream
))
1141 int wxImage::GetImageCount( wxInputStream
&stream
, long type
)
1143 wxImageHandler
*handler
;
1145 if ( type
== wxBITMAP_TYPE_ANY
)
1147 wxList
&list
=GetHandlers();
1149 for (wxList::Node
*node
= list
.GetFirst(); node
; node
= node
->GetNext())
1151 handler
=(wxImageHandler
*)node
->GetData();
1152 if ( handler
->CanRead(stream
) )
1153 return handler
->GetImageCount(stream
);
1157 wxLogWarning(_("No handler found for image type."));
1161 handler
= FindHandler(type
);
1165 wxLogWarning(_("No image handler for type %d defined."), type
);
1169 if ( handler
->CanRead(stream
) )
1171 return handler
->GetImageCount(stream
);
1175 wxLogError(_("Image file is not of type %d."), type
);
1180 bool wxImage::LoadFile( wxInputStream
& stream
, long type
, int index
)
1184 m_refData
= new wxImageRefData
;
1186 wxImageHandler
*handler
;
1188 if ( type
== wxBITMAP_TYPE_ANY
)
1190 wxList
&list
=GetHandlers();
1192 for ( wxList::Node
*node
= list
.GetFirst(); node
; node
= node
->GetNext() )
1194 handler
=(wxImageHandler
*)node
->GetData();
1195 if ( handler
->CanRead(stream
) )
1196 return handler
->LoadFile(this, stream
, true/*verbose*/, index
);
1200 wxLogWarning( _("No handler found for image type.") );
1204 handler
= FindHandler(type
);
1208 wxLogWarning( _("No image handler for type %d defined."), type
);
1213 return handler
->LoadFile(this, stream
, true/*verbose*/, index
);
1216 bool wxImage::LoadFile( wxInputStream
& stream
, const wxString
& mimetype
, int index
)
1220 m_refData
= new wxImageRefData
;
1222 wxImageHandler
*handler
= FindHandlerMime(mimetype
);
1226 wxLogWarning( _("No image handler for type %s defined."), mimetype
.GetData() );
1231 return handler
->LoadFile( this, stream
, true/*verbose*/, index
);
1234 bool wxImage::SaveFile( wxOutputStream
& stream
, int type
) const
1236 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1238 wxImageHandler
*handler
= FindHandler(type
);
1242 wxLogWarning( _("No image handler for type %d defined."), type
);
1247 return handler
->SaveFile( (wxImage
*)this, stream
);
1250 bool wxImage::SaveFile( wxOutputStream
& stream
, const wxString
& mimetype
) const
1252 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1254 wxImageHandler
*handler
= FindHandlerMime(mimetype
);
1258 wxLogWarning( _("No image handler for type %s defined."), mimetype
.GetData() );
1263 return handler
->SaveFile( (wxImage
*)this, stream
);
1265 #endif // wxUSE_STREAMS
1267 void wxImage::AddHandler( wxImageHandler
*handler
)
1269 // make sure that the memory will be freed at the program end
1270 sm_handlers
.DeleteContents(true);
1272 // Check for an existing handler of the type being added.
1273 if (FindHandler( handler
->GetType() ) == 0)
1275 sm_handlers
.Append( handler
);
1279 // This is not documented behaviour, merely the simplest 'fix'
1280 // for preventing duplicate additions. If someone ever has
1281 // a good reason to add and remove duplicate handlers (and they
1282 // may) we should probably refcount the duplicates.
1283 // also an issue in InsertHandler below.
1285 wxLogDebug( _T("Adding duplicate image handler for '%s'"),
1286 handler
->GetName().c_str() );
1291 void wxImage::InsertHandler( wxImageHandler
*handler
)
1293 // make sure that the memory will be freed at the program end
1294 sm_handlers
.DeleteContents(true);
1296 // Check for an existing handler of the type being added.
1297 if (FindHandler( handler
->GetType() ) == 0)
1299 sm_handlers
.Insert( handler
);
1303 // see AddHandler for additional comments.
1304 wxLogDebug( _T("Inserting duplicate image handler for '%s'"),
1305 handler
->GetName().c_str() );
1310 bool wxImage::RemoveHandler( const wxString
& name
)
1312 wxImageHandler
*handler
= FindHandler(name
);
1315 sm_handlers
.DeleteObject(handler
);
1322 wxImageHandler
*wxImage::FindHandler( const wxString
& name
)
1324 wxNode
*node
= sm_handlers
.GetFirst();
1327 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
1328 if (handler
->GetName().Cmp(name
) == 0) return handler
;
1330 node
= node
->GetNext();
1335 wxImageHandler
*wxImage::FindHandler( const wxString
& extension
, long bitmapType
)
1337 wxNode
*node
= sm_handlers
.GetFirst();
1340 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
1341 if ( (handler
->GetExtension().Cmp(extension
) == 0) &&
1342 (bitmapType
== -1 || handler
->GetType() == bitmapType
) )
1344 node
= node
->GetNext();
1349 wxImageHandler
*wxImage::FindHandler( long bitmapType
)
1351 wxNode
*node
= sm_handlers
.GetFirst();
1354 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
1355 if (handler
->GetType() == bitmapType
) return handler
;
1356 node
= node
->GetNext();
1361 wxImageHandler
*wxImage::FindHandlerMime( const wxString
& mimetype
)
1363 wxNode
*node
= sm_handlers
.GetFirst();
1366 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
1367 if (handler
->GetMimeType().IsSameAs(mimetype
, false)) return handler
;
1368 node
= node
->GetNext();
1373 void wxImage::InitStandardHandlers()
1376 AddHandler(new wxBMPHandler
);
1377 #endif // wxUSE_STREAMS
1380 void wxImage::CleanUpHandlers()
1382 wxNode
*node
= sm_handlers
.GetFirst();
1385 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
1386 wxNode
*next
= node
->GetNext();
1394 wxString
wxImage::GetImageExtWildcard()
1398 wxList
& Handlers
= wxImage::GetHandlers();
1399 wxNode
* Node
= Handlers
.GetFirst();
1402 wxImageHandler
* Handler
= (wxImageHandler
*)Node
->GetData();
1403 fmts
+= wxT("*.") + Handler
->GetExtension();
1404 Node
= Node
->GetNext();
1405 if ( Node
) fmts
+= wxT(";");
1408 return wxT("(") + fmts
+ wxT(")|") + fmts
;
1412 //-----------------------------------------------------------------------------
1414 //-----------------------------------------------------------------------------
1416 IMPLEMENT_ABSTRACT_CLASS(wxImageHandler
,wxObject
)
1419 bool wxImageHandler::LoadFile( wxImage
*WXUNUSED(image
), wxInputStream
& WXUNUSED(stream
), bool WXUNUSED(verbose
), int WXUNUSED(index
) )
1424 bool wxImageHandler::SaveFile( wxImage
*WXUNUSED(image
), wxOutputStream
& WXUNUSED(stream
), bool WXUNUSED(verbose
) )
1429 int wxImageHandler::GetImageCount( wxInputStream
& WXUNUSED(stream
) )
1434 bool wxImageHandler::CanRead( const wxString
& name
)
1436 if (wxFileExists(name
))
1438 wxFileInputStream
stream(name
);
1439 return CanRead(stream
);
1442 wxLogError( _("Can't check image format of file '%s': file does not exist."), name
.c_str() );
1447 bool wxImageHandler::CallDoCanRead(wxInputStream
& stream
)
1449 off_t posOld
= stream
.TellI();
1450 if ( posOld
== wxInvalidOffset
)
1452 // can't test unseekable stream
1456 bool ok
= DoCanRead(stream
);
1458 // restore the old position to be able to test other formats and so on
1459 if ( stream
.SeekI(posOld
) == wxInvalidOffset
)
1461 wxLogDebug(_T("Failed to rewind the stream in wxImageHandler!"));
1463 // reading would fail anyhow as we're not at the right position
1470 #endif // wxUSE_STREAMS
1472 // ----------------------------------------------------------------------------
1473 // image histogram stuff
1474 // ----------------------------------------------------------------------------
1477 wxImageHistogram::FindFirstUnusedColour(unsigned char *r
,
1482 unsigned char g2
) const
1484 unsigned long key
= MakeKey(r2
, g2
, b2
);
1486 while ( find(key
) != end() )
1488 // color already used
1500 wxLogError(_("GetUnusedColour:: No Unused Color in image ") );
1506 key
= MakeKey(r2
, g2
, b2
);
1520 wxImage::FindFirstUnusedColour(unsigned char *r
,
1525 unsigned char g2
) const
1527 wxImageHistogram histogram
;
1529 ComputeHistogram(histogram
);
1531 return histogram
.FindFirstUnusedColour(r
, g
, b
, r2
, g2
, b2
);
1537 // Counts and returns the number of different colours. Optionally stops
1538 // when it exceeds 'stopafter' different colours. This is useful, for
1539 // example, to see if the image can be saved as 8-bit (256 colour or
1540 // less, in this case it would be invoked as CountColours(256)). Default
1541 // value for stopafter is -1 (don't care).
1543 unsigned long wxImage::CountColours( unsigned long stopafter
) const
1547 unsigned char r
, g
, b
;
1549 unsigned long size
, nentries
, key
;
1552 size
= GetWidth() * GetHeight();
1555 for (unsigned long j
= 0; (j
< size
) && (nentries
<= stopafter
) ; j
++)
1560 key
= wxImageHistogram::MakeKey(r
, g
, b
);
1562 if (h
.Get(key
) == NULL
)
1573 unsigned long wxImage::ComputeHistogram( wxImageHistogram
&h
) const
1575 unsigned char *p
= GetData();
1576 unsigned long nentries
= 0;
1580 const unsigned long size
= GetWidth() * GetHeight();
1582 unsigned char r
, g
, b
;
1583 for ( unsigned long n
= 0; n
< size
; n
++ )
1589 wxImageHistogramEntry
& entry
= h
[wxImageHistogram::MakeKey(r
, g
, b
)];
1591 if ( entry
.value
++ == 0 )
1592 entry
.index
= nentries
++;
1599 * Rotation code by Carlos Moreno
1602 // GRG: I've removed wxRotationPoint - we already have wxRealPoint which
1603 // does exactly the same thing. And I also got rid of wxRotationPixel
1604 // bacause of potential problems in architectures where alignment
1605 // is an issue, so I had to rewrite parts of the code.
1607 static const double gs_Epsilon
= 1e-10;
1609 static inline int wxCint (double x
)
1611 return (x
> 0) ? (int) (x
+ 0.5) : (int) (x
- 0.5);
1615 // Auxiliary function to rotate a point (x,y) with respect to point p0
1616 // make it inline and use a straight return to facilitate optimization
1617 // also, the function receives the sine and cosine of the angle to avoid
1618 // repeating the time-consuming calls to these functions -- sin/cos can
1619 // be computed and stored in the calling function.
1621 inline wxRealPoint
rotated_point (const wxRealPoint
& p
, double cos_angle
, double sin_angle
, const wxRealPoint
& p0
)
1623 return wxRealPoint (p0
.x
+ (p
.x
- p0
.x
) * cos_angle
- (p
.y
- p0
.y
) * sin_angle
,
1624 p0
.y
+ (p
.y
- p0
.y
) * cos_angle
+ (p
.x
- p0
.x
) * sin_angle
);
1627 inline wxRealPoint
rotated_point (double x
, double y
, double cos_angle
, double sin_angle
, const wxRealPoint
& p0
)
1629 return rotated_point (wxRealPoint(x
,y
), cos_angle
, sin_angle
, p0
);
1632 wxImage
wxImage::Rotate(double angle
, const wxPoint
& centre_of_rotation
, bool interpolating
, wxPoint
* offset_after_rotation
) const
1635 angle
= -angle
; // screen coordinates are a mirror image of "real" coordinates
1637 // Create pointer-based array to accelerate access to wxImage's data
1638 unsigned char ** data
= new unsigned char * [GetHeight()];
1640 data
[0] = GetData();
1642 for (i
= 1; i
< GetHeight(); i
++)
1643 data
[i
] = data
[i
- 1] + (3 * GetWidth());
1645 // precompute coefficients for rotation formula
1646 // (sine and cosine of the angle)
1647 const double cos_angle
= cos(angle
);
1648 const double sin_angle
= sin(angle
);
1650 // Create new Image to store the result
1651 // First, find rectangle that covers the rotated image; to do that,
1652 // rotate the four corners
1654 const wxRealPoint
p0(centre_of_rotation
.x
, centre_of_rotation
.y
);
1656 wxRealPoint p1
= rotated_point (0, 0, cos_angle
, sin_angle
, p0
);
1657 wxRealPoint p2
= rotated_point (0, GetHeight(), cos_angle
, sin_angle
, p0
);
1658 wxRealPoint p3
= rotated_point (GetWidth(), 0, cos_angle
, sin_angle
, p0
);
1659 wxRealPoint p4
= rotated_point (GetWidth(), GetHeight(), cos_angle
, sin_angle
, p0
);
1661 int x1
= (int) floor (wxMin (wxMin(p1
.x
, p2
.x
), wxMin(p3
.x
, p4
.x
)));
1662 int y1
= (int) floor (wxMin (wxMin(p1
.y
, p2
.y
), wxMin(p3
.y
, p4
.y
)));
1663 int x2
= (int) ceil (wxMax (wxMax(p1
.x
, p2
.x
), wxMax(p3
.x
, p4
.x
)));
1664 int y2
= (int) ceil (wxMax (wxMax(p1
.y
, p2
.y
), wxMax(p3
.y
, p4
.y
)));
1666 wxImage
rotated (x2
- x1
+ 1, y2
- y1
+ 1, false);
1668 if (offset_after_rotation
!= NULL
)
1670 *offset_after_rotation
= wxPoint (x1
, y1
);
1673 // GRG: The rotated (destination) image is always accessed
1674 // sequentially, so there is no need for a pointer-based
1675 // array here (and in fact it would be slower).
1677 unsigned char * dst
= rotated
.GetData();
1679 // GRG: if the original image has a mask, use its RGB values
1680 // as the blank pixel, else, fall back to default (black).
1682 unsigned char blank_r
= 0;
1683 unsigned char blank_g
= 0;
1684 unsigned char blank_b
= 0;
1688 blank_r
= GetMaskRed();
1689 blank_g
= GetMaskGreen();
1690 blank_b
= GetMaskBlue();
1691 rotated
.SetMaskColour( blank_r
, blank_g
, blank_b
);
1694 // Now, for each point of the rotated image, find where it came from, by
1695 // performing an inverse rotation (a rotation of -angle) and getting the
1696 // pixel at those coordinates
1698 // GRG: I've taken the (interpolating) test out of the loops, so that
1699 // it is done only once, instead of repeating it for each pixel.
1704 for (int y
= 0; y
< rotated
.GetHeight(); y
++)
1706 for (x
= 0; x
< rotated
.GetWidth(); x
++)
1708 wxRealPoint src
= rotated_point (x
+ x1
, y
+ y1
, cos_angle
, -sin_angle
, p0
);
1710 if (-0.25 < src
.x
&& src
.x
< GetWidth() - 0.75 &&
1711 -0.25 < src
.y
&& src
.y
< GetHeight() - 0.75)
1713 // interpolate using the 4 enclosing grid-points. Those
1714 // points can be obtained using floor and ceiling of the
1715 // exact coordinates of the point
1716 // C.M. 2000-02-17: when the point is near the border, special care is required.
1720 if (0 < src
.x
&& src
.x
< GetWidth() - 1)
1722 x1
= wxCint(floor(src
.x
));
1723 x2
= wxCint(ceil(src
.x
));
1725 else // else means that x is near one of the borders (0 or width-1)
1727 x1
= x2
= wxCint (src
.x
);
1730 if (0 < src
.y
&& src
.y
< GetHeight() - 1)
1732 y1
= wxCint(floor(src
.y
));
1733 y2
= wxCint(ceil(src
.y
));
1737 y1
= y2
= wxCint (src
.y
);
1740 // get four points and the distances (square of the distance,
1741 // for efficiency reasons) for the interpolation formula
1743 // GRG: Do not calculate the points until they are
1744 // really needed -- this way we can calculate
1745 // just one, instead of four, if d1, d2, d3
1746 // or d4 are < gs_Epsilon
1748 const double d1
= (src
.x
- x1
) * (src
.x
- x1
) + (src
.y
- y1
) * (src
.y
- y1
);
1749 const double d2
= (src
.x
- x2
) * (src
.x
- x2
) + (src
.y
- y1
) * (src
.y
- y1
);
1750 const double d3
= (src
.x
- x2
) * (src
.x
- x2
) + (src
.y
- y2
) * (src
.y
- y2
);
1751 const double d4
= (src
.x
- x1
) * (src
.x
- x1
) + (src
.y
- y2
) * (src
.y
- y2
);
1753 // Now interpolate as a weighted average of the four surrounding
1754 // points, where the weights are the distances to each of those points
1756 // If the point is exactly at one point of the grid of the source
1757 // image, then don't interpolate -- just assign the pixel
1759 if (d1
< gs_Epsilon
) // d1,d2,d3,d4 are positive -- no need for abs()
1761 unsigned char *p
= data
[y1
] + (3 * x1
);
1766 else if (d2
< gs_Epsilon
)
1768 unsigned char *p
= data
[y1
] + (3 * x2
);
1773 else if (d3
< gs_Epsilon
)
1775 unsigned char *p
= data
[y2
] + (3 * x2
);
1780 else if (d4
< gs_Epsilon
)
1782 unsigned char *p
= data
[y2
] + (3 * x1
);
1789 // weights for the weighted average are proportional to the inverse of the distance
1790 unsigned char *v1
= data
[y1
] + (3 * x1
);
1791 unsigned char *v2
= data
[y1
] + (3 * x2
);
1792 unsigned char *v3
= data
[y2
] + (3 * x2
);
1793 unsigned char *v4
= data
[y2
] + (3 * x1
);
1795 const double w1
= 1/d1
, w2
= 1/d2
, w3
= 1/d3
, w4
= 1/d4
;
1799 *(dst
++) = (unsigned char)
1800 ( (w1
* *(v1
++) + w2
* *(v2
++) +
1801 w3
* *(v3
++) + w4
* *(v4
++)) /
1802 (w1
+ w2
+ w3
+ w4
) );
1803 *(dst
++) = (unsigned char)
1804 ( (w1
* *(v1
++) + w2
* *(v2
++) +
1805 w3
* *(v3
++) + w4
* *(v4
++)) /
1806 (w1
+ w2
+ w3
+ w4
) );
1807 *(dst
++) = (unsigned char)
1808 ( (w1
* *(v1
++) + w2
* *(v2
++) +
1809 w3
* *(v3
++) + w4
* *(v4
++)) /
1810 (w1
+ w2
+ w3
+ w4
) );
1822 else // not interpolating
1824 for (int y
= 0; y
< rotated
.GetHeight(); y
++)
1826 for (x
= 0; x
< rotated
.GetWidth(); x
++)
1828 wxRealPoint src
= rotated_point (x
+ x1
, y
+ y1
, cos_angle
, -sin_angle
, p0
);
1830 const int xs
= wxCint (src
.x
); // wxCint rounds to the
1831 const int ys
= wxCint (src
.y
); // closest integer
1833 if (0 <= xs
&& xs
< GetWidth() &&
1834 0 <= ys
&& ys
< GetHeight())
1836 unsigned char *p
= data
[ys
] + (3 * xs
);
1860 // A module to allow wxImage initialization/cleanup
1861 // without calling these functions from app.cpp or from
1862 // the user's application.
1864 class wxImageModule
: public wxModule
1866 DECLARE_DYNAMIC_CLASS(wxImageModule
)
1869 bool OnInit() { wxImage::InitStandardHandlers(); return true; };
1870 void OnExit() { wxImage::CleanUpHandlers(); };
1873 IMPLEMENT_DYNAMIC_CLASS(wxImageModule
, wxModule
)
1876 #endif // wxUSE_IMAGE