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"
45 //-----------------------------------------------------------------------------
47 //-----------------------------------------------------------------------------
49 class wxImageRefData
: public wxObjectRefData
53 virtual ~wxImageRefData();
57 unsigned char *m_data
;
60 unsigned char m_maskRed
,m_maskGreen
,m_maskBlue
;
62 // alpha channel data, may be NULL for the formats without alpha support
63 unsigned char *m_alpha
;
70 #endif // wxUSE_PALETTE
72 wxArrayString m_optionNames
;
73 wxArrayString m_optionValues
;
75 DECLARE_NO_COPY_CLASS(wxImageRefData
)
78 wxImageRefData::wxImageRefData()
83 m_alpha
= (unsigned char *) NULL
;
94 wxImageRefData::~wxImageRefData()
102 wxList
wxImage::sm_handlers
;
106 //-----------------------------------------------------------------------------
108 #define M_IMGDATA ((wxImageRefData *)m_refData)
110 IMPLEMENT_DYNAMIC_CLASS(wxImage
, wxObject
)
116 wxImage::wxImage( int width
, int height
)
118 Create( width
, height
);
121 wxImage::wxImage( int width
, int height
, unsigned char* data
, bool static_data
)
123 Create( width
, height
, data
, static_data
);
126 wxImage::wxImage( const wxString
& name
, long type
, int index
)
128 LoadFile( name
, type
, index
);
131 wxImage::wxImage( const wxString
& name
, const wxString
& mimetype
, int index
)
133 LoadFile( name
, mimetype
, index
);
137 wxImage::wxImage( wxInputStream
& stream
, long type
, int index
)
139 LoadFile( stream
, type
, index
);
142 wxImage::wxImage( wxInputStream
& stream
, const wxString
& mimetype
, int index
)
144 LoadFile( stream
, mimetype
, index
);
146 #endif // wxUSE_STREAMS
148 wxImage::wxImage( const wxImage
& image
)
154 wxImage::wxImage( const wxImage
* image
)
156 if (image
) Ref(*image
);
159 void wxImage::Create( int width
, int height
)
163 m_refData
= new wxImageRefData();
165 M_IMGDATA
->m_data
= (unsigned char *) malloc( width
*height
*3 );
166 if (M_IMGDATA
->m_data
)
168 for (int l
= 0; l
< width
*height
*3; l
++) M_IMGDATA
->m_data
[l
] = 0;
170 M_IMGDATA
->m_width
= width
;
171 M_IMGDATA
->m_height
= height
;
172 M_IMGDATA
->m_ok
= TRUE
;
180 void wxImage::Create( int width
, int height
, unsigned char* data
, bool static_data
)
184 m_refData
= new wxImageRefData();
186 M_IMGDATA
->m_data
= data
;
187 if (M_IMGDATA
->m_data
)
189 M_IMGDATA
->m_width
= width
;
190 M_IMGDATA
->m_height
= height
;
191 M_IMGDATA
->m_ok
= TRUE
;
192 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
);
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
);
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
);
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
;
361 // This is nonsense, RR.
363 // We do (x, y) -> (x, y)*oldSize/newSize but the valid values of x and y
364 // are from 0 to size-1, hence all decrement the sizes
365 long old_old_width
= old_width
;
370 for ( long j
= 0; j
<= height
; j
++ )
372 // don't crash for images with height == 1
373 long y_offset
= height
? (j
* old_height
/ height
)* old_old_width
: 0;
375 for ( long i
= 0; i
<= width
; i
++ )
377 long x_offset
= width
? (i
* old_width
) / width
: 0;
379 memcpy( target_data
, source_data
+ 3*(y_offset
+ x_offset
), 3 );
384 for (long j
= 0; j
< height
; j
++)
386 long y_offset
= (j
* old_height
/ height
) * old_width
;
388 for (long i
= 0; i
< width
; i
++)
391 source_data
+ 3*(y_offset
+ ((i
* old_width
)/ width
)),
398 // In case this is a cursor, make sure the hotspot is scalled accordingly:
399 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
) )
400 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
,
401 (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X
)*width
)/old_width
);
402 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
) )
403 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
,
404 (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y
)*height
)/old_height
);
409 wxImage
wxImage::Rotate90( bool clockwise
) const
413 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
415 image
.Create( M_IMGDATA
->m_height
, M_IMGDATA
->m_width
);
417 unsigned char *data
= image
.GetData();
419 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
421 if (M_IMGDATA
->m_hasMask
)
422 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
424 long height
= M_IMGDATA
->m_height
;
425 long width
= M_IMGDATA
->m_width
;
427 unsigned char *source_data
= M_IMGDATA
->m_data
;
428 unsigned char *target_data
;
430 for (long j
= 0; j
< height
; j
++)
432 for (long i
= 0; i
< width
; i
++)
435 target_data
= data
+ (((i
+1)*height
) - j
- 1)*3;
437 target_data
= data
+ ((height
*(width
-1)) + j
- (i
*height
))*3;
438 memcpy( target_data
, source_data
, 3 );
446 wxImage
wxImage::Mirror( bool horizontally
) const
450 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
452 image
.Create( M_IMGDATA
->m_width
, M_IMGDATA
->m_height
);
454 unsigned char *data
= image
.GetData();
456 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
458 if (M_IMGDATA
->m_hasMask
)
459 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
461 long height
= M_IMGDATA
->m_height
;
462 long width
= M_IMGDATA
->m_width
;
464 unsigned char *source_data
= M_IMGDATA
->m_data
;
465 unsigned char *target_data
;
469 for (long j
= 0; j
< height
; j
++)
472 target_data
= data
-3;
473 for (long i
= 0; i
< width
; i
++)
475 memcpy( target_data
, source_data
, 3 );
483 for (long i
= 0; i
< height
; i
++)
485 target_data
= data
+ 3*width
*(height
-1-i
);
486 memcpy( target_data
, source_data
, (size_t)3*width
);
487 source_data
+= 3*width
;
494 wxImage
wxImage::GetSubImage( const wxRect
&rect
) const
498 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
500 wxCHECK_MSG( (rect
.GetLeft()>=0) && (rect
.GetTop()>=0) && (rect
.GetRight()<=GetWidth()) && (rect
.GetBottom()<=GetHeight()),
501 image
, wxT("invalid subimage size") );
503 int subwidth
=rect
.GetWidth();
504 const int subheight
=rect
.GetHeight();
506 image
.Create( subwidth
, subheight
);
508 unsigned char *subdata
= image
.GetData(), *data
=GetData();
510 wxCHECK_MSG( subdata
, image
, wxT("unable to create image") );
512 if (M_IMGDATA
->m_hasMask
)
513 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
515 const int subleft
=3*rect
.GetLeft();
516 const int width
=3*GetWidth();
519 data
+=rect
.GetTop()*width
+subleft
;
521 for (long j
= 0; j
< subheight
; ++j
)
523 memcpy( subdata
, data
, subwidth
);
531 void wxImage::Paste( const wxImage
&image
, int x
, int y
)
533 wxCHECK_RET( Ok(), wxT("invalid image") );
534 wxCHECK_RET( image
.Ok(), wxT("invalid image") );
538 int width
= image
.GetWidth();
539 int height
= image
.GetHeight();
552 if ((x
+xx
)+width
> M_IMGDATA
->m_width
)
553 width
= M_IMGDATA
->m_width
- (x
+xx
);
554 if ((y
+yy
)+height
> M_IMGDATA
->m_height
)
555 height
= M_IMGDATA
->m_height
- (y
+yy
);
557 if (width
< 1) return;
558 if (height
< 1) return;
560 if ((!HasMask() && !image
.HasMask()) ||
561 ((HasMask() && image
.HasMask() &&
562 (GetMaskRed()==image
.GetMaskRed()) &&
563 (GetMaskGreen()==image
.GetMaskGreen()) &&
564 (GetMaskBlue()==image
.GetMaskBlue()))))
567 unsigned char* source_data
= image
.GetData() + xx
*3 + yy
*3*image
.GetWidth();
568 int source_step
= image
.GetWidth()*3;
570 unsigned char* target_data
= GetData() + (x
+xx
)*3 + (y
+yy
)*3*M_IMGDATA
->m_width
;
571 int target_step
= M_IMGDATA
->m_width
*3;
572 for (int j
= 0; j
< height
; j
++)
574 memcpy( target_data
, source_data
, width
);
575 source_data
+= source_step
;
576 target_data
+= target_step
;
581 if (!HasMask() && image
.HasMask())
583 unsigned char r
= image
.GetMaskRed();
584 unsigned char g
= image
.GetMaskGreen();
585 unsigned char b
= image
.GetMaskBlue();
588 unsigned char* source_data
= image
.GetData() + xx
*3 + yy
*3*image
.GetWidth();
589 int source_step
= image
.GetWidth()*3;
591 unsigned char* target_data
= GetData() + (x
+xx
)*3 + (y
+yy
)*3*M_IMGDATA
->m_width
;
592 int target_step
= M_IMGDATA
->m_width
*3;
594 for (int j
= 0; j
< height
; j
++)
596 for (int i
= 0; i
< width
; i
+=3)
598 if ((source_data
[i
] != r
) &&
599 (source_data
[i
+1] != g
) &&
600 (source_data
[i
+2] != b
))
602 memcpy( target_data
+i
, source_data
+i
, 3 );
605 source_data
+= source_step
;
606 target_data
+= target_step
;
611 void wxImage::Replace( unsigned char r1
, unsigned char g1
, unsigned char b1
,
612 unsigned char r2
, unsigned char g2
, unsigned char b2
)
614 wxCHECK_RET( Ok(), wxT("invalid image") );
616 unsigned char *data
= GetData();
618 const int w
= GetWidth();
619 const int h
= GetHeight();
621 for (int j
= 0; j
< h
; j
++)
622 for (int i
= 0; i
< w
; i
++)
624 if ((data
[0] == r1
) && (data
[1] == g1
) && (data
[2] == b1
))
634 wxImage
wxImage::ConvertToMono( unsigned char r
, unsigned char g
, unsigned char b
) const
638 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
640 image
.Create( M_IMGDATA
->m_width
, M_IMGDATA
->m_height
);
642 unsigned char *data
= image
.GetData();
644 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
646 if (M_IMGDATA
->m_hasMask
)
648 if (M_IMGDATA
->m_maskRed
== r
&& M_IMGDATA
->m_maskGreen
== g
&&
649 M_IMGDATA
->m_maskBlue
== b
)
650 image
.SetMaskColour( 255, 255, 255 );
652 image
.SetMaskColour( 0, 0, 0 );
655 long size
= M_IMGDATA
->m_height
* M_IMGDATA
->m_width
;
657 unsigned char *srcd
= M_IMGDATA
->m_data
;
658 unsigned char *tard
= image
.GetData();
660 for ( long i
= 0; i
< size
; i
++, srcd
+= 3, tard
+= 3 )
662 if (srcd
[0] == r
&& srcd
[1] == g
&& srcd
[2] == b
)
663 tard
[0] = tard
[1] = tard
[2] = 255;
665 tard
[0] = tard
[1] = tard
[2] = 0;
671 void wxImage::SetRGB( int x
, int y
, unsigned char r
, unsigned char g
, unsigned char b
)
673 wxCHECK_RET( Ok(), wxT("invalid image") );
675 int w
= M_IMGDATA
->m_width
;
676 int h
= M_IMGDATA
->m_height
;
678 wxCHECK_RET( (x
>=0) && (y
>=0) && (x
<w
) && (y
<h
), wxT("invalid image index") );
680 long pos
= (y
* w
+ x
) * 3;
682 M_IMGDATA
->m_data
[ pos
] = r
;
683 M_IMGDATA
->m_data
[ pos
+1 ] = g
;
684 M_IMGDATA
->m_data
[ pos
+2 ] = b
;
687 unsigned char wxImage::GetRed( 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
];
701 unsigned char wxImage::GetGreen( 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
+1];
715 unsigned char wxImage::GetBlue( int x
, int y
) const
717 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
719 int w
= M_IMGDATA
->m_width
;
720 int h
= M_IMGDATA
->m_height
;
722 wxCHECK_MSG( (x
>=0) && (y
>=0) && (x
<w
) && (y
<h
), 0, wxT("invalid image index") );
724 long pos
= (y
* w
+ x
) * 3;
726 return M_IMGDATA
->m_data
[pos
+2];
729 bool wxImage::Ok() const
731 // image of 0 width or height can't be considered ok - at least because it
732 // causes crashes in ConvertToBitmap() if we don't catch it in time
733 wxImageRefData
*data
= M_IMGDATA
;
734 return data
&& data
->m_ok
&& data
->m_width
&& data
->m_height
;
737 unsigned char *wxImage::GetData() const
739 wxCHECK_MSG( Ok(), (unsigned char *)NULL
, wxT("invalid image") );
741 return M_IMGDATA
->m_data
;
744 void wxImage::SetData( unsigned char *data
)
746 wxCHECK_RET( Ok(), wxT("invalid image") );
748 wxImageRefData
*newRefData
= new wxImageRefData();
750 newRefData
->m_width
= M_IMGDATA
->m_width
;
751 newRefData
->m_height
= M_IMGDATA
->m_height
;
752 newRefData
->m_data
= data
;
753 newRefData
->m_ok
= TRUE
;
754 newRefData
->m_maskRed
= M_IMGDATA
->m_maskRed
;
755 newRefData
->m_maskGreen
= M_IMGDATA
->m_maskGreen
;
756 newRefData
->m_maskBlue
= M_IMGDATA
->m_maskBlue
;
757 newRefData
->m_hasMask
= M_IMGDATA
->m_hasMask
;
761 m_refData
= newRefData
;
764 void wxImage::SetData( unsigned char *data
, int new_width
, int new_height
)
766 wxImageRefData
*newRefData
= new wxImageRefData();
770 newRefData
->m_width
= new_width
;
771 newRefData
->m_height
= new_height
;
772 newRefData
->m_data
= data
;
773 newRefData
->m_ok
= TRUE
;
774 newRefData
->m_maskRed
= M_IMGDATA
->m_maskRed
;
775 newRefData
->m_maskGreen
= M_IMGDATA
->m_maskGreen
;
776 newRefData
->m_maskBlue
= M_IMGDATA
->m_maskBlue
;
777 newRefData
->m_hasMask
= M_IMGDATA
->m_hasMask
;
781 newRefData
->m_width
= new_width
;
782 newRefData
->m_height
= new_height
;
783 newRefData
->m_data
= data
;
784 newRefData
->m_ok
= TRUE
;
789 m_refData
= newRefData
;
792 // ----------------------------------------------------------------------------
793 // alpha channel support
794 // ----------------------------------------------------------------------------
796 void wxImage::SetAlpha(int x
, int y
, unsigned char alpha
)
798 wxCHECK_RET( Ok() && HasAlpha(), wxT("invalid image or no alpha channel") );
800 int w
= M_IMGDATA
->m_width
,
801 h
= M_IMGDATA
->m_height
;
803 wxCHECK_RET( x
>=0 && y
>= 0 && x
< w
&& y
< h
, wxT("invalid image index") );
805 M_IMGDATA
->m_alpha
[y
*w
+ x
] = alpha
;
808 unsigned char wxImage::GetAlpha(int x
, int y
)
810 wxCHECK_MSG( Ok() && HasAlpha(), 0, wxT("invalid image or no alpha channel") );
812 int w
= M_IMGDATA
->m_width
,
813 h
= M_IMGDATA
->m_height
;
815 wxCHECK_MSG( x
>=0 && y
>= 0 && x
< w
&& y
< h
, 0, wxT("invalid image index") );
817 return M_IMGDATA
->m_alpha
[y
*w
+ x
];
820 void wxImage::SetAlpha( unsigned char *alpha
)
822 wxCHECK_RET( Ok(), wxT("invalid image") );
826 alpha
= (unsigned char *)
827 malloc(M_IMGDATA
->m_width
*M_IMGDATA
->m_height
*3);
830 delete [] M_IMGDATA
->m_alpha
;
831 M_IMGDATA
->m_alpha
= alpha
;
834 unsigned char *wxImage::GetAlpha() const
836 wxCHECK_MSG( Ok(), (unsigned char *)NULL
, wxT("invalid image") );
838 return M_IMGDATA
->m_alpha
;
841 // ----------------------------------------------------------------------------
843 // ----------------------------------------------------------------------------
845 void wxImage::SetMaskColour( unsigned char r
, unsigned char g
, unsigned char b
)
847 wxCHECK_RET( Ok(), wxT("invalid image") );
849 M_IMGDATA
->m_maskRed
= r
;
850 M_IMGDATA
->m_maskGreen
= g
;
851 M_IMGDATA
->m_maskBlue
= b
;
852 M_IMGDATA
->m_hasMask
= TRUE
;
855 unsigned char wxImage::GetMaskRed() const
857 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
859 return M_IMGDATA
->m_maskRed
;
862 unsigned char wxImage::GetMaskGreen() const
864 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
866 return M_IMGDATA
->m_maskGreen
;
869 unsigned char wxImage::GetMaskBlue() const
871 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
873 return M_IMGDATA
->m_maskBlue
;
876 void wxImage::SetMask( bool mask
)
878 wxCHECK_RET( Ok(), wxT("invalid image") );
880 M_IMGDATA
->m_hasMask
= mask
;
883 bool wxImage::HasMask() const
885 wxCHECK_MSG( Ok(), FALSE
, wxT("invalid image") );
887 return M_IMGDATA
->m_hasMask
;
890 int wxImage::GetWidth() const
892 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
894 return M_IMGDATA
->m_width
;
897 int wxImage::GetHeight() const
899 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
901 return M_IMGDATA
->m_height
;
904 bool wxImage::SetMaskFromImage(const wxImage
& mask
,
905 unsigned char mr
, unsigned char mg
, unsigned char mb
)
907 // check that the images are the same size
908 if ( (M_IMGDATA
->m_height
!= mask
.GetHeight() ) || (M_IMGDATA
->m_width
!= mask
.GetWidth () ) )
910 wxLogError( _("Image and Mask have different sizes") );
914 // find unused colour
915 unsigned char r
,g
,b
;
916 if (!FindFirstUnusedColour(&r
, &g
, &b
))
918 wxLogError( _("No Unused Color in image being masked") );
922 unsigned char *imgdata
= GetData();
923 unsigned char *maskdata
= mask
.GetData();
925 const int w
= GetWidth();
926 const int h
= GetHeight();
928 for (int j
= 0; j
< h
; j
++)
930 for (int i
= 0; i
< w
; i
++)
932 if ((maskdata
[0] == mr
) && (maskdata
[1] == mg
) && (maskdata
[2] == mb
))
943 SetMaskColour(r
, g
, b
);
953 bool wxImage::HasPalette() const
958 return M_IMGDATA
->m_palette
.Ok();
961 const wxPalette
& wxImage::GetPalette() const
963 wxCHECK_MSG( Ok(), wxNullPalette
, wxT("invalid image") );
965 return M_IMGDATA
->m_palette
;
968 void wxImage::SetPalette(const wxPalette
& palette
)
970 wxCHECK_RET( Ok(), wxT("invalid image") );
972 M_IMGDATA
->m_palette
= palette
;
975 #endif // wxUSE_PALETTE
977 // Option functions (arbitrary name/value mapping)
978 void wxImage::SetOption(const wxString
& name
, const wxString
& value
)
980 wxCHECK_RET( Ok(), wxT("invalid image") );
982 int idx
= M_IMGDATA
->m_optionNames
.Index(name
, FALSE
);
983 if (idx
== wxNOT_FOUND
)
985 M_IMGDATA
->m_optionNames
.Add(name
);
986 M_IMGDATA
->m_optionValues
.Add(value
);
990 M_IMGDATA
->m_optionNames
[idx
] = name
;
991 M_IMGDATA
->m_optionValues
[idx
] = value
;
995 void wxImage::SetOption(const wxString
& name
, int value
)
998 valStr
.Printf(wxT("%d"), value
);
999 SetOption(name
, valStr
);
1002 wxString
wxImage::GetOption(const wxString
& name
) const
1004 wxCHECK_MSG( Ok(), wxEmptyString
, wxT("invalid image") );
1006 int idx
= M_IMGDATA
->m_optionNames
.Index(name
, FALSE
);
1007 if (idx
== wxNOT_FOUND
)
1008 return wxEmptyString
;
1010 return M_IMGDATA
->m_optionValues
[idx
];
1013 int wxImage::GetOptionInt(const wxString
& name
) const
1015 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
1017 return wxAtoi(GetOption(name
));
1020 bool wxImage::HasOption(const wxString
& name
) const
1022 wxCHECK_MSG( Ok(), FALSE
, wxT("invalid image") );
1024 return (M_IMGDATA
->m_optionNames
.Index(name
, FALSE
) != wxNOT_FOUND
);
1027 bool wxImage::LoadFile( const wxString
& filename
, long type
, int index
)
1030 if (wxFileExists(filename
))
1032 wxFileInputStream
stream(filename
);
1033 wxBufferedInputStream
bstream( stream
);
1034 return LoadFile(bstream
, type
, index
);
1038 wxLogError( _("Can't load image from file '%s': file does not exist."), filename
.c_str() );
1042 #else // !wxUSE_STREAMS
1044 #endif // wxUSE_STREAMS
1047 bool wxImage::LoadFile( const wxString
& filename
, const wxString
& mimetype
, int index
)
1050 if (wxFileExists(filename
))
1052 wxFileInputStream
stream(filename
);
1053 wxBufferedInputStream
bstream( stream
);
1054 return LoadFile(bstream
, mimetype
, index
);
1058 wxLogError( _("Can't load image from file '%s': file does not exist."), filename
.c_str() );
1062 #else // !wxUSE_STREAMS
1064 #endif // wxUSE_STREAMS
1069 bool wxImage::SaveFile( const wxString
& filename
) const
1071 wxString ext
= filename
.AfterLast('.').Lower();
1073 wxImageHandler
* pHandler
= FindHandler(ext
, -1);
1076 SaveFile(filename
, pHandler
->GetType());
1080 wxLogError(_("Can't save image to file '%s': unknown extension."), filename
.c_str());
1085 bool wxImage::SaveFile( const wxString
& filename
, int type
) const
1088 ((wxImage
*)this)->SetOption(wxIMAGE_OPTION_FILENAME
, filename
);
1090 wxFileOutputStream
stream(filename
);
1092 if ( stream
.IsOk() )
1094 wxBufferedOutputStream
bstream( stream
);
1095 return SaveFile(bstream
, type
);
1097 #endif // wxUSE_STREAMS
1102 bool wxImage::SaveFile( const wxString
& filename
, const wxString
& mimetype
) const
1105 ((wxImage
*)this)->SetOption(wxIMAGE_OPTION_FILENAME
, filename
);
1107 wxFileOutputStream
stream(filename
);
1109 if ( stream
.IsOk() )
1111 wxBufferedOutputStream
bstream( stream
);
1112 return SaveFile(bstream
, mimetype
);
1114 #endif // wxUSE_STREAMS
1119 bool wxImage::CanRead( const wxString
&name
)
1122 wxFileInputStream
stream(name
);
1123 return CanRead(stream
);
1129 int wxImage::GetImageCount( const wxString
&name
, long type
)
1132 wxFileInputStream
stream(name
);
1134 return GetImageCount(stream
, type
);
1142 bool wxImage::CanRead( wxInputStream
&stream
)
1144 const wxList
& list
= GetHandlers();
1146 for ( wxList::Node
*node
= list
.GetFirst(); node
; node
= node
->GetNext() )
1148 wxImageHandler
*handler
=(wxImageHandler
*)node
->GetData();
1149 if (handler
->CanRead( stream
))
1156 int wxImage::GetImageCount( wxInputStream
&stream
, long type
)
1158 wxImageHandler
*handler
;
1160 if ( type
== wxBITMAP_TYPE_ANY
)
1162 wxList
&list
=GetHandlers();
1164 for (wxList::Node
*node
= list
.GetFirst(); node
; node
= node
->GetNext())
1166 handler
=(wxImageHandler
*)node
->GetData();
1167 if ( handler
->CanRead(stream
) )
1168 return handler
->GetImageCount(stream
);
1172 wxLogWarning(_("No handler found for image type."));
1176 handler
= FindHandler(type
);
1180 wxLogWarning(_("No image handler for type %d defined."), type
);
1184 if ( handler
->CanRead(stream
) )
1186 return handler
->GetImageCount(stream
);
1190 wxLogError(_("Image file is not of type %d."), type
);
1195 bool wxImage::LoadFile( wxInputStream
& stream
, long type
, int index
)
1199 m_refData
= new wxImageRefData
;
1201 wxImageHandler
*handler
;
1203 if ( type
== wxBITMAP_TYPE_ANY
)
1205 wxList
&list
=GetHandlers();
1207 for ( wxList::Node
*node
= list
.GetFirst(); node
; node
= node
->GetNext() )
1209 handler
=(wxImageHandler
*)node
->GetData();
1210 if ( handler
->CanRead(stream
) )
1211 return handler
->LoadFile(this, stream
, TRUE
/*verbose*/, index
);
1215 wxLogWarning( _("No handler found for image type.") );
1219 handler
= FindHandler(type
);
1223 wxLogWarning( _("No image handler for type %d defined."), type
);
1228 return handler
->LoadFile(this, stream
, TRUE
/*verbose*/, index
);
1231 bool wxImage::LoadFile( wxInputStream
& stream
, const wxString
& mimetype
, int index
)
1235 m_refData
= new wxImageRefData
;
1237 wxImageHandler
*handler
= FindHandlerMime(mimetype
);
1241 wxLogWarning( _("No image handler for type %s defined."), mimetype
.GetData() );
1246 return handler
->LoadFile( this, stream
, TRUE
/*verbose*/, index
);
1249 bool wxImage::SaveFile( wxOutputStream
& stream
, int type
) const
1251 wxCHECK_MSG( Ok(), FALSE
, wxT("invalid image") );
1253 wxImageHandler
*handler
= FindHandler(type
);
1257 wxLogWarning( _("No image handler for type %d defined."), type
);
1262 return handler
->SaveFile( (wxImage
*)this, stream
);
1265 bool wxImage::SaveFile( wxOutputStream
& stream
, const wxString
& mimetype
) const
1267 wxCHECK_MSG( Ok(), FALSE
, wxT("invalid image") );
1269 wxImageHandler
*handler
= FindHandlerMime(mimetype
);
1273 wxLogWarning( _("No image handler for type %s defined."), mimetype
.GetData() );
1278 return handler
->SaveFile( (wxImage
*)this, stream
);
1280 #endif // wxUSE_STREAMS
1282 void wxImage::AddHandler( wxImageHandler
*handler
)
1284 // make sure that the memory will be freed at the program end
1285 sm_handlers
.DeleteContents(TRUE
);
1287 // Check for an existing handler of the type being added.
1288 if (FindHandler( handler
->GetType() ) == 0)
1290 sm_handlers
.Append( handler
);
1294 // This is not documented behaviour, merely the simplest 'fix'
1295 // for preventing duplicate additions. If someone ever has
1296 // a good reason to add and remove duplicate handlers (and they
1297 // may) we should probably refcount the duplicates.
1298 // also an issue in InsertHandler below.
1300 wxLogDebug( _T("Adding duplicate image handler for '%s'"),
1301 handler
->GetName().c_str() );
1306 void wxImage::InsertHandler( wxImageHandler
*handler
)
1308 // make sure that the memory will be freed at the program end
1309 sm_handlers
.DeleteContents(TRUE
);
1311 // Check for an existing handler of the type being added.
1312 if (FindHandler( handler
->GetType() ) == 0)
1314 sm_handlers
.Insert( handler
);
1318 // see AddHandler for additional comments.
1319 wxLogDebug( _T("Inserting duplicate image handler for '%s'"),
1320 handler
->GetName().c_str() );
1325 bool wxImage::RemoveHandler( const wxString
& name
)
1327 wxImageHandler
*handler
= FindHandler(name
);
1330 sm_handlers
.DeleteObject(handler
);
1337 wxImageHandler
*wxImage::FindHandler( const wxString
& name
)
1339 wxNode
*node
= sm_handlers
.GetFirst();
1342 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
1343 if (handler
->GetName().Cmp(name
) == 0) return handler
;
1345 node
= node
->GetNext();
1350 wxImageHandler
*wxImage::FindHandler( const wxString
& extension
, long bitmapType
)
1352 wxNode
*node
= sm_handlers
.GetFirst();
1355 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
1356 if ( (handler
->GetExtension().Cmp(extension
) == 0) &&
1357 (bitmapType
== -1 || handler
->GetType() == bitmapType
) )
1359 node
= node
->GetNext();
1364 wxImageHandler
*wxImage::FindHandler( long bitmapType
)
1366 wxNode
*node
= sm_handlers
.GetFirst();
1369 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
1370 if (handler
->GetType() == bitmapType
) return handler
;
1371 node
= node
->GetNext();
1376 wxImageHandler
*wxImage::FindHandlerMime( const wxString
& mimetype
)
1378 wxNode
*node
= sm_handlers
.GetFirst();
1381 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
1382 if (handler
->GetMimeType().IsSameAs(mimetype
, FALSE
)) return handler
;
1383 node
= node
->GetNext();
1388 void wxImage::InitStandardHandlers()
1391 AddHandler(new wxBMPHandler
);
1392 #endif // wxUSE_STREAMS
1395 void wxImage::CleanUpHandlers()
1397 wxNode
*node
= sm_handlers
.GetFirst();
1400 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
1401 wxNode
*next
= node
->GetNext();
1408 //-----------------------------------------------------------------------------
1410 //-----------------------------------------------------------------------------
1412 IMPLEMENT_ABSTRACT_CLASS(wxImageHandler
,wxObject
)
1415 bool wxImageHandler::LoadFile( wxImage
*WXUNUSED(image
), wxInputStream
& WXUNUSED(stream
), bool WXUNUSED(verbose
), int WXUNUSED(index
) )
1420 bool wxImageHandler::SaveFile( wxImage
*WXUNUSED(image
), wxOutputStream
& WXUNUSED(stream
), bool WXUNUSED(verbose
) )
1425 int wxImageHandler::GetImageCount( wxInputStream
& WXUNUSED(stream
) )
1430 bool wxImageHandler::CanRead( const wxString
& name
)
1432 if (wxFileExists(name
))
1434 wxFileInputStream
stream(name
);
1435 return CanRead(stream
);
1438 wxLogError( _("Can't check image format of file '%s': file does not exist."), name
.c_str() );
1443 bool wxImageHandler::CallDoCanRead(wxInputStream
& stream
)
1445 off_t posOld
= stream
.TellI();
1446 if ( posOld
== wxInvalidOffset
)
1448 // can't test unseekable stream
1452 bool ok
= DoCanRead(stream
);
1454 // restore the old position to be able to test other formats and so on
1455 if ( stream
.SeekI(posOld
) == wxInvalidOffset
)
1457 wxLogDebug(_T("Failed to rewind the stream in wxImageHandler!"));
1459 // reading would fail anyhow as we're not at the right position
1466 #endif // wxUSE_STREAMS
1470 //-----------------------------------------------------------------------------
1471 // Deprecated wxBitmap conversion routines
1472 //-----------------------------------------------------------------------------
1474 #if WXWIN_COMPATIBILITY_2_2 && wxUSE_GUI
1477 wxBitmap
wxImage::ConvertToMonoBitmap( unsigned char red
, unsigned char green
, unsigned char blue
) const
1479 wxImage mono
= this->ConvertToMono( red
, green
, blue
);
1480 wxBitmap
bitmap( mono
, 1 );
1485 wxBitmap
wxImage::ConvertToBitmap() const
1487 wxBitmap
bitmap( *this );
1491 wxImage::wxImage( const wxBitmap
&bitmap
)
1493 *this = bitmap
.ConvertToImage();
1496 #endif // WXWIN_COMPATIBILITY_2_2 && wxUSE_GUI
1499 // ----------------------------------------------------------------------------
1500 // image histogram stuff
1501 // ----------------------------------------------------------------------------
1504 wxImageHistogram::FindFirstUnusedColour(unsigned char *r
,
1509 unsigned char g2
) const
1511 unsigned long key
= MakeKey(r2
, g2
, b2
);
1513 while ( find(key
) != end() )
1515 // color already used
1527 wxLogError(_("GetUnusedColour:: No Unused Color in image ") );
1533 key
= MakeKey(r2
, g2
, b2
);
1547 wxImage::FindFirstUnusedColour(unsigned char *r
,
1552 unsigned char g2
) const
1554 wxImageHistogram histogram
;
1556 ComputeHistogram(histogram
);
1558 return histogram
.FindFirstUnusedColour(r
, g
, b
, r2
, g2
, b2
);
1564 // Counts and returns the number of different colours. Optionally stops
1565 // when it exceeds 'stopafter' different colours. This is useful, for
1566 // example, to see if the image can be saved as 8-bit (256 colour or
1567 // less, in this case it would be invoked as CountColours(256)). Default
1568 // value for stopafter is -1 (don't care).
1570 unsigned long wxImage::CountColours( unsigned long stopafter
) const
1574 unsigned char r
, g
, b
;
1576 unsigned long size
, nentries
, key
;
1579 size
= GetWidth() * GetHeight();
1582 for (unsigned long j
= 0; (j
< size
) && (nentries
<= stopafter
) ; j
++)
1587 key
= wxImageHistogram::MakeKey(r
, g
, b
);
1589 if (h
.Get(key
) == NULL
)
1600 unsigned long wxImage::ComputeHistogram( wxImageHistogram
&h
) const
1602 unsigned char *p
= GetData();
1603 unsigned long nentries
= 0;
1607 const unsigned long size
= GetWidth() * GetHeight();
1609 unsigned char r
, g
, b
;
1610 for ( unsigned long n
= 0; n
< size
; n
++ )
1616 wxImageHistogramEntry
& entry
= h
[wxImageHistogram::MakeKey(r
, g
, b
)];
1618 if ( entry
.value
++ == 0 )
1619 entry
.index
= nentries
++;
1626 * Rotation code by Carlos Moreno
1629 // GRG: I've removed wxRotationPoint - we already have wxRealPoint which
1630 // does exactly the same thing. And I also got rid of wxRotationPixel
1631 // bacause of potential problems in architectures where alignment
1632 // is an issue, so I had to rewrite parts of the code.
1634 static const double gs_Epsilon
= 1e-10;
1636 static inline int wxCint (double x
)
1638 return (x
> 0) ? (int) (x
+ 0.5) : (int) (x
- 0.5);
1642 // Auxiliary function to rotate a point (x,y) with respect to point p0
1643 // make it inline and use a straight return to facilitate optimization
1644 // also, the function receives the sine and cosine of the angle to avoid
1645 // repeating the time-consuming calls to these functions -- sin/cos can
1646 // be computed and stored in the calling function.
1648 inline wxRealPoint
rotated_point (const wxRealPoint
& p
, double cos_angle
, double sin_angle
, const wxRealPoint
& p0
)
1650 return wxRealPoint (p0
.x
+ (p
.x
- p0
.x
) * cos_angle
- (p
.y
- p0
.y
) * sin_angle
,
1651 p0
.y
+ (p
.y
- p0
.y
) * cos_angle
+ (p
.x
- p0
.x
) * sin_angle
);
1654 inline wxRealPoint
rotated_point (double x
, double y
, double cos_angle
, double sin_angle
, const wxRealPoint
& p0
)
1656 return rotated_point (wxRealPoint(x
,y
), cos_angle
, sin_angle
, p0
);
1659 wxImage
wxImage::Rotate(double angle
, const wxPoint
& centre_of_rotation
, bool interpolating
, wxPoint
* offset_after_rotation
) const
1662 angle
= -angle
; // screen coordinates are a mirror image of "real" coordinates
1664 // Create pointer-based array to accelerate access to wxImage's data
1665 unsigned char ** data
= new unsigned char * [GetHeight()];
1667 data
[0] = GetData();
1669 for (i
= 1; i
< GetHeight(); i
++)
1670 data
[i
] = data
[i
- 1] + (3 * GetWidth());
1672 // precompute coefficients for rotation formula
1673 // (sine and cosine of the angle)
1674 const double cos_angle
= cos(angle
);
1675 const double sin_angle
= sin(angle
);
1677 // Create new Image to store the result
1678 // First, find rectangle that covers the rotated image; to do that,
1679 // rotate the four corners
1681 const wxRealPoint
p0(centre_of_rotation
.x
, centre_of_rotation
.y
);
1683 wxRealPoint p1
= rotated_point (0, 0, cos_angle
, sin_angle
, p0
);
1684 wxRealPoint p2
= rotated_point (0, GetHeight(), cos_angle
, sin_angle
, p0
);
1685 wxRealPoint p3
= rotated_point (GetWidth(), 0, cos_angle
, sin_angle
, p0
);
1686 wxRealPoint p4
= rotated_point (GetWidth(), GetHeight(), cos_angle
, sin_angle
, p0
);
1688 int x1
= (int) floor (wxMin (wxMin(p1
.x
, p2
.x
), wxMin(p3
.x
, p4
.x
)));
1689 int y1
= (int) floor (wxMin (wxMin(p1
.y
, p2
.y
), wxMin(p3
.y
, p4
.y
)));
1690 int x2
= (int) ceil (wxMax (wxMax(p1
.x
, p2
.x
), wxMax(p3
.x
, p4
.x
)));
1691 int y2
= (int) ceil (wxMax (wxMax(p1
.y
, p2
.y
), wxMax(p3
.y
, p4
.y
)));
1693 wxImage
rotated (x2
- x1
+ 1, y2
- y1
+ 1);
1695 if (offset_after_rotation
!= NULL
)
1697 *offset_after_rotation
= wxPoint (x1
, y1
);
1700 // GRG: The rotated (destination) image is always accessed
1701 // sequentially, so there is no need for a pointer-based
1702 // array here (and in fact it would be slower).
1704 unsigned char * dst
= rotated
.GetData();
1706 // GRG: if the original image has a mask, use its RGB values
1707 // as the blank pixel, else, fall back to default (black).
1709 unsigned char blank_r
= 0;
1710 unsigned char blank_g
= 0;
1711 unsigned char blank_b
= 0;
1715 blank_r
= GetMaskRed();
1716 blank_g
= GetMaskGreen();
1717 blank_b
= GetMaskBlue();
1718 rotated
.SetMaskColour( blank_r
, blank_g
, blank_b
);
1721 // Now, for each point of the rotated image, find where it came from, by
1722 // performing an inverse rotation (a rotation of -angle) and getting the
1723 // pixel at those coordinates
1725 // GRG: I've taken the (interpolating) test out of the loops, so that
1726 // it is done only once, instead of repeating it for each pixel.
1731 for (int y
= 0; y
< rotated
.GetHeight(); y
++)
1733 for (x
= 0; x
< rotated
.GetWidth(); x
++)
1735 wxRealPoint src
= rotated_point (x
+ x1
, y
+ y1
, cos_angle
, -sin_angle
, p0
);
1737 if (-0.25 < src
.x
&& src
.x
< GetWidth() - 0.75 &&
1738 -0.25 < src
.y
&& src
.y
< GetHeight() - 0.75)
1740 // interpolate using the 4 enclosing grid-points. Those
1741 // points can be obtained using floor and ceiling of the
1742 // exact coordinates of the point
1743 // C.M. 2000-02-17: when the point is near the border, special care is required.
1747 if (0 < src
.x
&& src
.x
< GetWidth() - 1)
1749 x1
= wxCint(floor(src
.x
));
1750 x2
= wxCint(ceil(src
.x
));
1752 else // else means that x is near one of the borders (0 or width-1)
1754 x1
= x2
= wxCint (src
.x
);
1757 if (0 < src
.y
&& src
.y
< GetHeight() - 1)
1759 y1
= wxCint(floor(src
.y
));
1760 y2
= wxCint(ceil(src
.y
));
1764 y1
= y2
= wxCint (src
.y
);
1767 // get four points and the distances (square of the distance,
1768 // for efficiency reasons) for the interpolation formula
1770 // GRG: Do not calculate the points until they are
1771 // really needed -- this way we can calculate
1772 // just one, instead of four, if d1, d2, d3
1773 // or d4 are < gs_Epsilon
1775 const double d1
= (src
.x
- x1
) * (src
.x
- x1
) + (src
.y
- y1
) * (src
.y
- y1
);
1776 const double d2
= (src
.x
- x2
) * (src
.x
- x2
) + (src
.y
- y1
) * (src
.y
- y1
);
1777 const double d3
= (src
.x
- x2
) * (src
.x
- x2
) + (src
.y
- y2
) * (src
.y
- y2
);
1778 const double d4
= (src
.x
- x1
) * (src
.x
- x1
) + (src
.y
- y2
) * (src
.y
- y2
);
1780 // Now interpolate as a weighted average of the four surrounding
1781 // points, where the weights are the distances to each of those points
1783 // If the point is exactly at one point of the grid of the source
1784 // image, then don't interpolate -- just assign the pixel
1786 if (d1
< gs_Epsilon
) // d1,d2,d3,d4 are positive -- no need for abs()
1788 unsigned char *p
= data
[y1
] + (3 * x1
);
1793 else if (d2
< gs_Epsilon
)
1795 unsigned char *p
= data
[y1
] + (3 * x2
);
1800 else if (d3
< gs_Epsilon
)
1802 unsigned char *p
= data
[y2
] + (3 * x2
);
1807 else if (d4
< gs_Epsilon
)
1809 unsigned char *p
= data
[y2
] + (3 * x1
);
1816 // weights for the weighted average are proportional to the inverse of the distance
1817 unsigned char *v1
= data
[y1
] + (3 * x1
);
1818 unsigned char *v2
= data
[y1
] + (3 * x2
);
1819 unsigned char *v3
= data
[y2
] + (3 * x2
);
1820 unsigned char *v4
= data
[y2
] + (3 * x1
);
1822 const double w1
= 1/d1
, w2
= 1/d2
, w3
= 1/d3
, w4
= 1/d4
;
1826 *(dst
++) = (unsigned char)
1827 ( (w1
* *(v1
++) + w2
* *(v2
++) +
1828 w3
* *(v3
++) + w4
* *(v4
++)) /
1829 (w1
+ w2
+ w3
+ w4
) );
1830 *(dst
++) = (unsigned char)
1831 ( (w1
* *(v1
++) + w2
* *(v2
++) +
1832 w3
* *(v3
++) + w4
* *(v4
++)) /
1833 (w1
+ w2
+ w3
+ w4
) );
1834 *(dst
++) = (unsigned char)
1835 ( (w1
* *(v1
++) + w2
* *(v2
++) +
1836 w3
* *(v3
++) + w4
* *(v4
++)) /
1837 (w1
+ w2
+ w3
+ w4
) );
1849 else // not interpolating
1851 for (int y
= 0; y
< rotated
.GetHeight(); y
++)
1853 for (x
= 0; x
< rotated
.GetWidth(); x
++)
1855 wxRealPoint src
= rotated_point (x
+ x1
, y
+ y1
, cos_angle
, -sin_angle
, p0
);
1857 const int xs
= wxCint (src
.x
); // wxCint rounds to the
1858 const int ys
= wxCint (src
.y
); // closest integer
1860 if (0 <= xs
&& xs
< GetWidth() &&
1861 0 <= ys
&& ys
< GetHeight())
1863 unsigned char *p
= data
[ys
] + (3 * xs
);
1887 // A module to allow wxImage initialization/cleanup
1888 // without calling these functions from app.cpp or from
1889 // the user's application.
1891 class wxImageModule
: public wxModule
1893 DECLARE_DYNAMIC_CLASS(wxImageModule
)
1896 bool OnInit() { wxImage::InitStandardHandlers(); return TRUE
; };
1897 void OnExit() { wxImage::CleanUpHandlers(); };
1900 IMPLEMENT_DYNAMIC_CLASS(wxImageModule
, wxModule
)
1903 #endif // wxUSE_IMAGE