1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Robert Roebling
6 // Copyright: (c) Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
11 #pragma implementation "image.h"
14 // For compilers that support precompilation, includes "wx.h".
15 #include "wx/wxprec.h"
26 #include "wx/bitmap.h"
30 #include "wx/filefn.h"
31 #include "wx/wfstream.h"
33 #include "wx/module.h"
46 //-----------------------------------------------------------------------------
48 //-----------------------------------------------------------------------------
50 class wxImageRefData
: public wxObjectRefData
54 virtual ~wxImageRefData();
58 unsigned char *m_data
;
61 unsigned char m_maskRed
,m_maskGreen
,m_maskBlue
;
63 // alpha channel data, may be NULL for the formats without alpha support
64 unsigned char *m_alpha
;
71 #endif // wxUSE_PALETTE
73 wxArrayString m_optionNames
;
74 wxArrayString m_optionValues
;
76 DECLARE_NO_COPY_CLASS(wxImageRefData
)
79 wxImageRefData::wxImageRefData()
84 m_alpha
= (unsigned char *) NULL
;
95 wxImageRefData::~wxImageRefData()
104 wxList
wxImage::sm_handlers
;
108 //-----------------------------------------------------------------------------
110 #define M_IMGDATA ((wxImageRefData *)m_refData)
112 IMPLEMENT_DYNAMIC_CLASS(wxImage
, wxObject
)
114 wxImage::wxImage( int width
, int height
, bool clear
)
116 Create( width
, height
, clear
);
119 wxImage::wxImage( int width
, int height
, unsigned char* data
, bool static_data
)
121 Create( width
, height
, data
, static_data
);
124 wxImage::wxImage( int width
, int height
, unsigned char* data
, unsigned char* alpha
, bool static_data
)
126 Create( width
, height
, data
, alpha
, static_data
);
129 wxImage::wxImage( const wxString
& name
, long type
, int index
)
131 LoadFile( name
, type
, index
);
134 wxImage::wxImage( const wxString
& name
, const wxString
& mimetype
, int index
)
136 LoadFile( name
, mimetype
, index
);
140 wxImage::wxImage( wxInputStream
& stream
, long type
, int index
)
142 LoadFile( stream
, type
, index
);
145 wxImage::wxImage( wxInputStream
& stream
, const wxString
& mimetype
, int index
)
147 LoadFile( stream
, mimetype
, index
);
149 #endif // wxUSE_STREAMS
151 wxImage::wxImage( const wxImage
& image
)
157 wxImage::wxImage( const wxImage
* image
)
159 if (image
) Ref(*image
);
162 bool wxImage::Create( int width
, int height
, bool clear
)
166 m_refData
= new wxImageRefData();
168 M_IMGDATA
->m_data
= (unsigned char *) malloc( width
*height
*3 );
169 if (!M_IMGDATA
->m_data
)
176 memset(M_IMGDATA
->m_data
, 0, width
*height
*3);
178 M_IMGDATA
->m_width
= width
;
179 M_IMGDATA
->m_height
= height
;
180 M_IMGDATA
->m_ok
= true;
185 bool wxImage::Create( int width
, int height
, unsigned char* data
, bool static_data
)
189 wxCHECK_MSG( data
, false, _T("NULL data in wxImage::Create") );
191 m_refData
= new wxImageRefData();
193 M_IMGDATA
->m_data
= data
;
194 M_IMGDATA
->m_width
= width
;
195 M_IMGDATA
->m_height
= height
;
196 M_IMGDATA
->m_ok
= true;
197 M_IMGDATA
->m_static
= static_data
;
202 bool wxImage::Create( int width
, int height
, unsigned char* data
, unsigned char* alpha
, bool static_data
)
206 wxCHECK_MSG( data
, false, _T("NULL data in wxImage::Create") );
208 m_refData
= new wxImageRefData();
210 M_IMGDATA
->m_data
= data
;
211 M_IMGDATA
->m_alpha
= alpha
;
212 M_IMGDATA
->m_width
= width
;
213 M_IMGDATA
->m_height
= height
;
214 M_IMGDATA
->m_ok
= true;
215 M_IMGDATA
->m_static
= static_data
;
220 void wxImage::Destroy()
225 wxImage
wxImage::Copy() const
229 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
231 image
.Create( M_IMGDATA
->m_width
, M_IMGDATA
->m_height
, false );
233 unsigned char *data
= image
.GetData();
235 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
237 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
238 image
.SetMask( M_IMGDATA
->m_hasMask
);
240 memcpy( data
, GetData(), M_IMGDATA
->m_width
*M_IMGDATA
->m_height
*3 );
242 // also copy the image options
243 wxImageRefData
*imgData
= (wxImageRefData
*)image
.m_refData
;
244 imgData
->m_optionNames
= M_IMGDATA
->m_optionNames
;
245 imgData
->m_optionValues
= M_IMGDATA
->m_optionValues
;
250 wxImage
wxImage::ShrinkBy( int xFactor
, int yFactor
) const
252 if( xFactor
== 1 && yFactor
== 1 )
257 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
259 // can't scale to/from 0 size
260 wxCHECK_MSG( (xFactor
> 0) && (yFactor
> 0), image
,
261 wxT("invalid new image size") );
263 long old_height
= M_IMGDATA
->m_height
,
264 old_width
= M_IMGDATA
->m_width
;
266 wxCHECK_MSG( (old_height
> 0) && (old_width
> 0), image
,
267 wxT("invalid old image size") );
269 long width
= old_width
/ xFactor
;
270 long height
= old_height
/ yFactor
;
272 image
.Create( width
, height
, false );
274 char unsigned *data
= image
.GetData();
276 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
278 bool hasMask
= false ;
279 unsigned char maskRed
= 0;
280 unsigned char maskGreen
= 0;
281 unsigned char maskBlue
=0 ;
283 unsigned char *source_data
= M_IMGDATA
->m_data
;
284 unsigned char *target_data
= data
;
285 unsigned char *source_alpha
= 0 ;
286 unsigned char *target_alpha
= 0 ;
287 if (M_IMGDATA
->m_hasMask
)
290 maskRed
= M_IMGDATA
->m_maskRed
;
291 maskGreen
= M_IMGDATA
->m_maskGreen
;
292 maskBlue
=M_IMGDATA
->m_maskBlue
;
294 image
.SetMaskColour( M_IMGDATA
->m_maskRed
,
295 M_IMGDATA
->m_maskGreen
,
296 M_IMGDATA
->m_maskBlue
);
300 source_alpha
= M_IMGDATA
->m_alpha
;
304 target_alpha
= image
.GetAlpha() ;
308 for (long y
= 0; y
< height
; y
++)
310 for (long x
= 0; x
< width
; x
++)
312 unsigned long avgRed
= 0 ;
313 unsigned long avgGreen
= 0;
314 unsigned long avgBlue
= 0;
315 unsigned long avgAlpha
= 0 ;
316 unsigned long counter
= 0 ;
318 for ( int y1
= 0 ; y1
< yFactor
; ++y1
)
320 long y_offset
= (y
* yFactor
+ y1
) * old_width
;
321 for ( int x1
= 0 ; x1
< xFactor
; ++x1
)
323 unsigned char *pixel
= source_data
+ 3 * ( y_offset
+ x
* xFactor
+ x1
) ;
324 unsigned char red
= pixel
[0] ;
325 unsigned char green
= pixel
[1] ;
326 unsigned char blue
= pixel
[2] ;
327 unsigned char alpha
= 255 ;
329 alpha
= *(source_alpha
+ y_offset
+ x
* xFactor
+ x1
) ;
330 if ( !hasMask
|| red
!= maskRed
|| green
!= maskGreen
|| blue
!= maskBlue
)
345 *(target_data
++) = M_IMGDATA
->m_maskRed
;
346 *(target_data
++) = M_IMGDATA
->m_maskGreen
;
347 *(target_data
++) = M_IMGDATA
->m_maskBlue
;
352 *(target_alpha
++) = (unsigned char)(avgAlpha
/ counter
) ;
353 *(target_data
++) = (unsigned char)(avgRed
/ counter
);
354 *(target_data
++) = (unsigned char)(avgGreen
/ counter
);
355 *(target_data
++) = (unsigned char)(avgBlue
/ counter
);
360 // In case this is a cursor, make sure the hotspot is scalled accordingly:
361 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
) )
362 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
,
363 (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X
))/xFactor
);
364 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
) )
365 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
,
366 (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y
))/yFactor
);
371 wxImage
wxImage::Scale( int width
, int height
) const
375 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
377 // can't scale to/from 0 size
378 wxCHECK_MSG( (width
> 0) && (height
> 0), image
,
379 wxT("invalid new image size") );
381 long old_height
= M_IMGDATA
->m_height
,
382 old_width
= M_IMGDATA
->m_width
;
383 wxCHECK_MSG( (old_height
> 0) && (old_width
> 0), image
,
384 wxT("invalid old image size") );
386 if ( old_width
% width
== 0 && old_width
>= width
&&
387 old_height
% height
== 0 && old_height
>= height
)
389 return ShrinkBy( old_width
/ width
, old_height
/ height
) ;
391 image
.Create( width
, height
, false );
393 unsigned char *data
= image
.GetData();
395 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
397 if (M_IMGDATA
->m_hasMask
)
399 image
.SetMaskColour( M_IMGDATA
->m_maskRed
,
400 M_IMGDATA
->m_maskGreen
,
401 M_IMGDATA
->m_maskBlue
);
404 unsigned char *source_data
= M_IMGDATA
->m_data
;
405 unsigned char *target_data
= data
;
407 long x_delta
= (old_width
<<16) / width
;
408 long y_delta
= (old_height
<<16) / height
;
410 unsigned char* dest_pixel
= target_data
;
413 for ( long j
= 0; j
< height
; j
++ )
415 unsigned char* src_line
= &source_data
[(y
>>16)*old_width
*3];
418 for ( long i
= 0; i
< width
; i
++ )
420 unsigned char* src_pixel
= &src_line
[(x
>>16)*3];
421 dest_pixel
[0] = src_pixel
[0];
422 dest_pixel
[1] = src_pixel
[1];
423 dest_pixel
[2] = src_pixel
[2];
431 // In case this is a cursor, make sure the hotspot is scalled accordingly:
432 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
) )
433 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
,
434 (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X
)*width
)/old_width
);
435 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
) )
436 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
,
437 (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y
)*height
)/old_height
);
442 wxImage
wxImage::Rotate90( bool clockwise
) const
446 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
448 image
.Create( M_IMGDATA
->m_height
, M_IMGDATA
->m_width
, false );
450 unsigned char *data
= image
.GetData();
452 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
454 if (M_IMGDATA
->m_hasMask
)
455 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
457 long height
= M_IMGDATA
->m_height
;
458 long width
= M_IMGDATA
->m_width
;
460 unsigned char *source_data
= M_IMGDATA
->m_data
;
461 unsigned char *target_data
;
463 for (long j
= 0; j
< height
; j
++)
465 for (long i
= 0; i
< width
; i
++)
468 target_data
= data
+ (((i
+1)*height
) - j
- 1)*3;
470 target_data
= data
+ ((height
*(width
-1)) + j
- (i
*height
))*3;
471 memcpy( target_data
, source_data
, 3 );
479 wxImage
wxImage::Mirror( bool horizontally
) const
483 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
485 image
.Create( M_IMGDATA
->m_width
, M_IMGDATA
->m_height
, false );
487 unsigned char *data
= image
.GetData();
489 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
491 if (M_IMGDATA
->m_hasMask
)
492 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
494 long height
= M_IMGDATA
->m_height
;
495 long width
= M_IMGDATA
->m_width
;
497 unsigned char *source_data
= M_IMGDATA
->m_data
;
498 unsigned char *target_data
;
502 for (long j
= 0; j
< height
; j
++)
505 target_data
= data
-3;
506 for (long i
= 0; i
< width
; i
++)
508 memcpy( target_data
, source_data
, 3 );
516 for (long i
= 0; i
< height
; i
++)
518 target_data
= data
+ 3*width
*(height
-1-i
);
519 memcpy( target_data
, source_data
, (size_t)3*width
);
520 source_data
+= 3*width
;
527 wxImage
wxImage::GetSubImage( const wxRect
&rect
) const
531 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
533 wxCHECK_MSG( (rect
.GetLeft()>=0) && (rect
.GetTop()>=0) && (rect
.GetRight()<=GetWidth()) && (rect
.GetBottom()<=GetHeight()),
534 image
, wxT("invalid subimage size") );
536 int subwidth
=rect
.GetWidth();
537 const int subheight
=rect
.GetHeight();
539 image
.Create( subwidth
, subheight
, false );
541 unsigned char *subdata
= image
.GetData(), *data
=GetData();
543 wxCHECK_MSG( subdata
, image
, wxT("unable to create image") );
545 if (M_IMGDATA
->m_hasMask
)
546 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
548 const int subleft
=3*rect
.GetLeft();
549 const int width
=3*GetWidth();
552 data
+=rect
.GetTop()*width
+subleft
;
554 for (long j
= 0; j
< subheight
; ++j
)
556 memcpy( subdata
, data
, subwidth
);
564 void wxImage::Paste( const wxImage
&image
, int x
, int y
)
566 wxCHECK_RET( Ok(), wxT("invalid image") );
567 wxCHECK_RET( image
.Ok(), wxT("invalid image") );
571 int width
= image
.GetWidth();
572 int height
= image
.GetHeight();
585 if ((x
+xx
)+width
> M_IMGDATA
->m_width
)
586 width
= M_IMGDATA
->m_width
- (x
+xx
);
587 if ((y
+yy
)+height
> M_IMGDATA
->m_height
)
588 height
= M_IMGDATA
->m_height
- (y
+yy
);
590 if (width
< 1) return;
591 if (height
< 1) return;
593 if ((!HasMask() && !image
.HasMask()) ||
594 ((HasMask() && image
.HasMask() &&
595 (GetMaskRed()==image
.GetMaskRed()) &&
596 (GetMaskGreen()==image
.GetMaskGreen()) &&
597 (GetMaskBlue()==image
.GetMaskBlue()))))
600 unsigned char* source_data
= image
.GetData() + xx
*3 + yy
*3*image
.GetWidth();
601 int source_step
= image
.GetWidth()*3;
603 unsigned char* target_data
= GetData() + (x
+xx
)*3 + (y
+yy
)*3*M_IMGDATA
->m_width
;
604 int target_step
= M_IMGDATA
->m_width
*3;
605 for (int j
= 0; j
< height
; j
++)
607 memcpy( target_data
, source_data
, width
);
608 source_data
+= source_step
;
609 target_data
+= target_step
;
614 if (!HasMask() && image
.HasMask())
616 unsigned char r
= image
.GetMaskRed();
617 unsigned char g
= image
.GetMaskGreen();
618 unsigned char b
= image
.GetMaskBlue();
621 unsigned char* source_data
= image
.GetData() + xx
*3 + yy
*3*image
.GetWidth();
622 int source_step
= image
.GetWidth()*3;
624 unsigned char* target_data
= GetData() + (x
+xx
)*3 + (y
+yy
)*3*M_IMGDATA
->m_width
;
625 int target_step
= M_IMGDATA
->m_width
*3;
627 for (int j
= 0; j
< height
; j
++)
629 for (int i
= 0; i
< width
; i
+=3)
631 if ((source_data
[i
] != r
) &&
632 (source_data
[i
+1] != g
) &&
633 (source_data
[i
+2] != b
))
635 memcpy( target_data
+i
, source_data
+i
, 3 );
638 source_data
+= source_step
;
639 target_data
+= target_step
;
644 void wxImage::Replace( unsigned char r1
, unsigned char g1
, unsigned char b1
,
645 unsigned char r2
, unsigned char g2
, unsigned char b2
)
647 wxCHECK_RET( Ok(), wxT("invalid image") );
649 unsigned char *data
= GetData();
651 const int w
= GetWidth();
652 const int h
= GetHeight();
654 for (int j
= 0; j
< h
; j
++)
655 for (int i
= 0; i
< w
; i
++)
657 if ((data
[0] == r1
) && (data
[1] == g1
) && (data
[2] == b1
))
667 wxImage
wxImage::ConvertToMono( unsigned char r
, unsigned char g
, unsigned char b
) const
671 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
673 image
.Create( M_IMGDATA
->m_width
, M_IMGDATA
->m_height
, false );
675 unsigned char *data
= image
.GetData();
677 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
679 if (M_IMGDATA
->m_hasMask
)
681 if (M_IMGDATA
->m_maskRed
== r
&& M_IMGDATA
->m_maskGreen
== g
&&
682 M_IMGDATA
->m_maskBlue
== b
)
683 image
.SetMaskColour( 255, 255, 255 );
685 image
.SetMaskColour( 0, 0, 0 );
688 long size
= M_IMGDATA
->m_height
* M_IMGDATA
->m_width
;
690 unsigned char *srcd
= M_IMGDATA
->m_data
;
691 unsigned char *tard
= image
.GetData();
693 for ( long i
= 0; i
< size
; i
++, srcd
+= 3, tard
+= 3 )
695 if (srcd
[0] == r
&& srcd
[1] == g
&& srcd
[2] == b
)
696 tard
[0] = tard
[1] = tard
[2] = 255;
698 tard
[0] = tard
[1] = tard
[2] = 0;
704 void wxImage::SetRGB( int x
, int y
, unsigned char r
, unsigned char g
, unsigned char b
)
706 wxCHECK_RET( Ok(), wxT("invalid image") );
708 int w
= M_IMGDATA
->m_width
;
709 int h
= M_IMGDATA
->m_height
;
711 wxCHECK_RET( (x
>=0) && (y
>=0) && (x
<w
) && (y
<h
), wxT("invalid image index") );
713 long pos
= (y
* w
+ x
) * 3;
715 M_IMGDATA
->m_data
[ pos
] = r
;
716 M_IMGDATA
->m_data
[ pos
+1 ] = g
;
717 M_IMGDATA
->m_data
[ pos
+2 ] = b
;
720 unsigned char wxImage::GetRed( int x
, int y
) const
722 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
724 int w
= M_IMGDATA
->m_width
;
725 int h
= M_IMGDATA
->m_height
;
727 wxCHECK_MSG( (x
>=0) && (y
>=0) && (x
<w
) && (y
<h
), 0, wxT("invalid image index") );
729 long pos
= (y
* w
+ x
) * 3;
731 return M_IMGDATA
->m_data
[pos
];
734 unsigned char wxImage::GetGreen( int x
, int y
) const
736 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
738 int w
= M_IMGDATA
->m_width
;
739 int h
= M_IMGDATA
->m_height
;
741 wxCHECK_MSG( (x
>=0) && (y
>=0) && (x
<w
) && (y
<h
), 0, wxT("invalid image index") );
743 long pos
= (y
* w
+ x
) * 3;
745 return M_IMGDATA
->m_data
[pos
+1];
748 unsigned char wxImage::GetBlue( int x
, int y
) const
750 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
752 int w
= M_IMGDATA
->m_width
;
753 int h
= M_IMGDATA
->m_height
;
755 wxCHECK_MSG( (x
>=0) && (y
>=0) && (x
<w
) && (y
<h
), 0, wxT("invalid image index") );
757 long pos
= (y
* w
+ x
) * 3;
759 return M_IMGDATA
->m_data
[pos
+2];
762 bool wxImage::Ok() const
764 // image of 0 width or height can't be considered ok - at least because it
765 // causes crashes in ConvertToBitmap() if we don't catch it in time
766 wxImageRefData
*data
= M_IMGDATA
;
767 return data
&& data
->m_ok
&& data
->m_width
&& data
->m_height
;
770 unsigned char *wxImage::GetData() const
772 wxCHECK_MSG( Ok(), (unsigned char *)NULL
, wxT("invalid image") );
774 return M_IMGDATA
->m_data
;
777 void wxImage::SetData( unsigned char *data
)
779 wxCHECK_RET( Ok(), wxT("invalid image") );
781 wxImageRefData
*newRefData
= new wxImageRefData();
783 newRefData
->m_width
= M_IMGDATA
->m_width
;
784 newRefData
->m_height
= M_IMGDATA
->m_height
;
785 newRefData
->m_data
= data
;
786 newRefData
->m_ok
= true;
787 newRefData
->m_maskRed
= M_IMGDATA
->m_maskRed
;
788 newRefData
->m_maskGreen
= M_IMGDATA
->m_maskGreen
;
789 newRefData
->m_maskBlue
= M_IMGDATA
->m_maskBlue
;
790 newRefData
->m_hasMask
= M_IMGDATA
->m_hasMask
;
794 m_refData
= newRefData
;
797 void wxImage::SetData( unsigned char *data
, int new_width
, int new_height
)
799 wxImageRefData
*newRefData
= new wxImageRefData();
803 newRefData
->m_width
= new_width
;
804 newRefData
->m_height
= new_height
;
805 newRefData
->m_data
= data
;
806 newRefData
->m_ok
= true;
807 newRefData
->m_maskRed
= M_IMGDATA
->m_maskRed
;
808 newRefData
->m_maskGreen
= M_IMGDATA
->m_maskGreen
;
809 newRefData
->m_maskBlue
= M_IMGDATA
->m_maskBlue
;
810 newRefData
->m_hasMask
= M_IMGDATA
->m_hasMask
;
814 newRefData
->m_width
= new_width
;
815 newRefData
->m_height
= new_height
;
816 newRefData
->m_data
= data
;
817 newRefData
->m_ok
= true;
822 m_refData
= newRefData
;
825 // ----------------------------------------------------------------------------
826 // alpha channel support
827 // ----------------------------------------------------------------------------
829 void wxImage::SetAlpha(int x
, int y
, unsigned char alpha
)
831 wxCHECK_RET( Ok() && HasAlpha(), wxT("invalid image or no alpha channel") );
833 int w
= M_IMGDATA
->m_width
,
834 h
= M_IMGDATA
->m_height
;
836 wxCHECK_RET( x
>=0 && y
>= 0 && x
< w
&& y
< h
, wxT("invalid image index") );
838 M_IMGDATA
->m_alpha
[y
*w
+ x
] = alpha
;
841 unsigned char wxImage::GetAlpha(int x
, int y
) const
843 wxCHECK_MSG( Ok() && HasAlpha(), 0, wxT("invalid image or no alpha channel") );
845 int w
= M_IMGDATA
->m_width
,
846 h
= M_IMGDATA
->m_height
;
848 wxCHECK_MSG( x
>=0 && y
>= 0 && x
< w
&& y
< h
, 0, wxT("invalid image index") );
850 return M_IMGDATA
->m_alpha
[y
*w
+ x
];
853 bool wxImage::ConvertColourToAlpha( unsigned char r
, unsigned char g
, unsigned char b
)
857 int w
= M_IMGDATA
->m_width
,
858 h
= M_IMGDATA
->m_height
;
860 unsigned char *alpha
= GetAlpha();
861 unsigned char *data
= GetData();
864 for (y
= 0; y
< h
; y
++)
865 for (x
= 0; x
< w
; x
++)
880 void wxImage::SetAlpha( unsigned char *alpha
)
882 wxCHECK_RET( Ok(), wxT("invalid image") );
886 alpha
= (unsigned char *)malloc(M_IMGDATA
->m_width
*M_IMGDATA
->m_height
);
889 free(M_IMGDATA
->m_alpha
);
890 M_IMGDATA
->m_alpha
= alpha
;
893 unsigned char *wxImage::GetAlpha() const
895 wxCHECK_MSG( Ok(), (unsigned char *)NULL
, wxT("invalid image") );
897 return M_IMGDATA
->m_alpha
;
900 // ----------------------------------------------------------------------------
902 // ----------------------------------------------------------------------------
904 void wxImage::SetMaskColour( unsigned char r
, unsigned char g
, unsigned char b
)
906 wxCHECK_RET( Ok(), wxT("invalid image") );
908 M_IMGDATA
->m_maskRed
= r
;
909 M_IMGDATA
->m_maskGreen
= g
;
910 M_IMGDATA
->m_maskBlue
= b
;
911 M_IMGDATA
->m_hasMask
= true;
914 unsigned char wxImage::GetMaskRed() const
916 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
918 return M_IMGDATA
->m_maskRed
;
921 unsigned char wxImage::GetMaskGreen() const
923 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
925 return M_IMGDATA
->m_maskGreen
;
928 unsigned char wxImage::GetMaskBlue() const
930 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
932 return M_IMGDATA
->m_maskBlue
;
935 void wxImage::SetMask( bool mask
)
937 wxCHECK_RET( Ok(), wxT("invalid image") );
939 M_IMGDATA
->m_hasMask
= mask
;
942 bool wxImage::HasMask() const
944 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
946 return M_IMGDATA
->m_hasMask
;
949 int wxImage::GetWidth() const
951 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
953 return M_IMGDATA
->m_width
;
956 int wxImage::GetHeight() const
958 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
960 return M_IMGDATA
->m_height
;
963 bool wxImage::SetMaskFromImage(const wxImage
& mask
,
964 unsigned char mr
, unsigned char mg
, unsigned char mb
)
966 // check that the images are the same size
967 if ( (M_IMGDATA
->m_height
!= mask
.GetHeight() ) || (M_IMGDATA
->m_width
!= mask
.GetWidth () ) )
969 wxLogError( _("Image and mask have different sizes.") );
973 // find unused colour
974 unsigned char r
,g
,b
;
975 if (!FindFirstUnusedColour(&r
, &g
, &b
))
977 wxLogError( _("No unused colour in image being masked.") );
981 unsigned char *imgdata
= GetData();
982 unsigned char *maskdata
= mask
.GetData();
984 const int w
= GetWidth();
985 const int h
= GetHeight();
987 for (int j
= 0; j
< h
; j
++)
989 for (int i
= 0; i
< w
; i
++)
991 if ((maskdata
[0] == mr
) && (maskdata
[1] == mg
) && (maskdata
[2] == mb
))
1002 SetMaskColour(r
, g
, b
);
1008 bool wxImage::ConvertAlphaToMask(unsigned char threshold
)
1013 unsigned char mr
, mg
, mb
;
1014 if (!FindFirstUnusedColour(&mr
, &mg
, &mb
))
1016 wxLogError( _("No unused colour in image being masked.") );
1021 SetMaskColour(mr
, mg
, mb
);
1023 unsigned char *imgdata
= GetData();
1024 unsigned char *alphadata
= GetAlpha();
1027 int h
= GetHeight();
1029 for (int y
= 0; y
< h
; y
++)
1031 for (int x
= 0; x
< w
; x
++, imgdata
+= 3, alphadata
++)
1033 if (*alphadata
< threshold
)
1042 free(M_IMGDATA
->m_alpha
);
1043 M_IMGDATA
->m_alpha
= NULL
;
1050 // Palette functions
1052 bool wxImage::HasPalette() const
1057 return M_IMGDATA
->m_palette
.Ok();
1060 const wxPalette
& wxImage::GetPalette() const
1062 wxCHECK_MSG( Ok(), wxNullPalette
, wxT("invalid image") );
1064 return M_IMGDATA
->m_palette
;
1067 void wxImage::SetPalette(const wxPalette
& palette
)
1069 wxCHECK_RET( Ok(), wxT("invalid image") );
1071 M_IMGDATA
->m_palette
= palette
;
1074 #endif // wxUSE_PALETTE
1076 // Option functions (arbitrary name/value mapping)
1077 void wxImage::SetOption(const wxString
& name
, const wxString
& value
)
1079 wxCHECK_RET( Ok(), wxT("invalid image") );
1081 int idx
= M_IMGDATA
->m_optionNames
.Index(name
, false);
1082 if (idx
== wxNOT_FOUND
)
1084 M_IMGDATA
->m_optionNames
.Add(name
);
1085 M_IMGDATA
->m_optionValues
.Add(value
);
1089 M_IMGDATA
->m_optionNames
[idx
] = name
;
1090 M_IMGDATA
->m_optionValues
[idx
] = value
;
1094 void wxImage::SetOption(const wxString
& name
, int value
)
1097 valStr
.Printf(wxT("%d"), value
);
1098 SetOption(name
, valStr
);
1101 wxString
wxImage::GetOption(const wxString
& name
) const
1103 wxCHECK_MSG( Ok(), wxEmptyString
, wxT("invalid image") );
1105 int idx
= M_IMGDATA
->m_optionNames
.Index(name
, false);
1106 if (idx
== wxNOT_FOUND
)
1107 return wxEmptyString
;
1109 return M_IMGDATA
->m_optionValues
[idx
];
1112 int wxImage::GetOptionInt(const wxString
& name
) const
1114 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
1116 return wxAtoi(GetOption(name
));
1119 bool wxImage::HasOption(const wxString
& name
) const
1121 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1123 return (M_IMGDATA
->m_optionNames
.Index(name
, false) != wxNOT_FOUND
);
1126 bool wxImage::LoadFile( const wxString
& filename
, long type
, int index
)
1129 if (wxFileExists(filename
))
1131 wxFileInputStream
stream(filename
);
1132 wxBufferedInputStream
bstream( stream
);
1133 return LoadFile(bstream
, type
, index
);
1137 wxLogError( _("Can't load image from file '%s': file does not exist."), filename
.c_str() );
1141 #else // !wxUSE_STREAMS
1143 #endif // wxUSE_STREAMS
1146 bool wxImage::LoadFile( const wxString
& filename
, const wxString
& mimetype
, int index
)
1149 if (wxFileExists(filename
))
1151 wxFileInputStream
stream(filename
);
1152 wxBufferedInputStream
bstream( stream
);
1153 return LoadFile(bstream
, mimetype
, index
);
1157 wxLogError( _("Can't load image from file '%s': file does not exist."), filename
.c_str() );
1161 #else // !wxUSE_STREAMS
1163 #endif // wxUSE_STREAMS
1168 bool wxImage::SaveFile( const wxString
& filename
) const
1170 wxString ext
= filename
.AfterLast('.').Lower();
1172 wxImageHandler
* pHandler
= FindHandler(ext
, -1);
1175 SaveFile(filename
, pHandler
->GetType());
1179 wxLogError(_("Can't save image to file '%s': unknown extension."), filename
.c_str());
1184 bool wxImage::SaveFile( const wxString
& filename
, int type
) const
1187 ((wxImage
*)this)->SetOption(wxIMAGE_OPTION_FILENAME
, filename
);
1189 wxFileOutputStream
stream(filename
);
1191 if ( stream
.IsOk() )
1193 wxBufferedOutputStream
bstream( stream
);
1194 return SaveFile(bstream
, type
);
1196 #endif // wxUSE_STREAMS
1201 bool wxImage::SaveFile( const wxString
& filename
, const wxString
& mimetype
) const
1204 ((wxImage
*)this)->SetOption(wxIMAGE_OPTION_FILENAME
, filename
);
1206 wxFileOutputStream
stream(filename
);
1208 if ( stream
.IsOk() )
1210 wxBufferedOutputStream
bstream( stream
);
1211 return SaveFile(bstream
, mimetype
);
1213 #endif // wxUSE_STREAMS
1218 bool wxImage::CanRead( const wxString
&name
)
1221 wxFileInputStream
stream(name
);
1222 return CanRead(stream
);
1228 int wxImage::GetImageCount( const wxString
&name
, long type
)
1231 wxFileInputStream
stream(name
);
1233 return GetImageCount(stream
, type
);
1241 bool wxImage::CanRead( wxInputStream
&stream
)
1243 const wxList
& list
= GetHandlers();
1245 for ( wxList::compatibility_iterator node
= list
.GetFirst(); node
; node
= node
->GetNext() )
1247 wxImageHandler
*handler
=(wxImageHandler
*)node
->GetData();
1248 if (handler
->CanRead( stream
))
1255 int wxImage::GetImageCount( wxInputStream
&stream
, long type
)
1257 wxImageHandler
*handler
;
1259 if ( type
== wxBITMAP_TYPE_ANY
)
1261 wxList
&list
=GetHandlers();
1263 for (wxList::compatibility_iterator node
= list
.GetFirst(); node
; node
= node
->GetNext())
1265 handler
=(wxImageHandler
*)node
->GetData();
1266 if ( handler
->CanRead(stream
) )
1267 return handler
->GetImageCount(stream
);
1271 wxLogWarning(_("No handler found for image type."));
1275 handler
= FindHandler(type
);
1279 wxLogWarning(_("No image handler for type %d defined."), type
);
1283 if ( handler
->CanRead(stream
) )
1285 return handler
->GetImageCount(stream
);
1289 wxLogError(_("Image file is not of type %d."), type
);
1294 bool wxImage::LoadFile( wxInputStream
& stream
, long type
, int index
)
1298 m_refData
= new wxImageRefData
;
1300 wxImageHandler
*handler
;
1302 if ( type
== wxBITMAP_TYPE_ANY
)
1304 wxList
&list
=GetHandlers();
1306 for ( wxList::compatibility_iterator node
= list
.GetFirst(); node
; node
= node
->GetNext() )
1308 handler
=(wxImageHandler
*)node
->GetData();
1309 if ( handler
->CanRead(stream
) )
1310 return handler
->LoadFile(this, stream
, true/*verbose*/, index
);
1314 wxLogWarning( _("No handler found for image type.") );
1318 handler
= FindHandler(type
);
1322 wxLogWarning( _("No image handler for type %d defined."), type
);
1327 return handler
->LoadFile(this, stream
, true/*verbose*/, index
);
1330 bool wxImage::LoadFile( wxInputStream
& stream
, const wxString
& mimetype
, int index
)
1334 m_refData
= new wxImageRefData
;
1336 wxImageHandler
*handler
= FindHandlerMime(mimetype
);
1340 wxLogWarning( _("No image handler for type %s defined."), mimetype
.GetData() );
1345 return handler
->LoadFile( this, stream
, true/*verbose*/, index
);
1348 bool wxImage::SaveFile( wxOutputStream
& stream
, int type
) const
1350 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1352 wxImageHandler
*handler
= FindHandler(type
);
1356 wxLogWarning( _("No image handler for type %d defined."), type
);
1361 return handler
->SaveFile( (wxImage
*)this, stream
);
1364 bool wxImage::SaveFile( wxOutputStream
& stream
, const wxString
& mimetype
) const
1366 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1368 wxImageHandler
*handler
= FindHandlerMime(mimetype
);
1372 wxLogWarning( _("No image handler for type %s defined."), mimetype
.GetData() );
1377 return handler
->SaveFile( (wxImage
*)this, stream
);
1379 #endif // wxUSE_STREAMS
1381 void wxImage::AddHandler( wxImageHandler
*handler
)
1383 // Check for an existing handler of the type being added.
1384 if (FindHandler( handler
->GetType() ) == 0)
1386 sm_handlers
.Append( handler
);
1390 // This is not documented behaviour, merely the simplest 'fix'
1391 // for preventing duplicate additions. If someone ever has
1392 // a good reason to add and remove duplicate handlers (and they
1393 // may) we should probably refcount the duplicates.
1394 // also an issue in InsertHandler below.
1396 wxLogDebug( _T("Adding duplicate image handler for '%s'"),
1397 handler
->GetName().c_str() );
1402 void wxImage::InsertHandler( wxImageHandler
*handler
)
1404 // Check for an existing handler of the type being added.
1405 if (FindHandler( handler
->GetType() ) == 0)
1407 sm_handlers
.Insert( handler
);
1411 // see AddHandler for additional comments.
1412 wxLogDebug( _T("Inserting duplicate image handler for '%s'"),
1413 handler
->GetName().c_str() );
1418 bool wxImage::RemoveHandler( const wxString
& name
)
1420 wxImageHandler
*handler
= FindHandler(name
);
1423 sm_handlers
.DeleteObject(handler
);
1431 wxImageHandler
*wxImage::FindHandler( const wxString
& name
)
1433 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
1436 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
1437 if (handler
->GetName().Cmp(name
) == 0) return handler
;
1439 node
= node
->GetNext();
1444 wxImageHandler
*wxImage::FindHandler( const wxString
& extension
, long bitmapType
)
1446 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
1449 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
1450 if ( (handler
->GetExtension().Cmp(extension
) == 0) &&
1451 (bitmapType
== -1 || handler
->GetType() == bitmapType
) )
1453 node
= node
->GetNext();
1458 wxImageHandler
*wxImage::FindHandler( long bitmapType
)
1460 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
1463 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
1464 if (handler
->GetType() == bitmapType
) return handler
;
1465 node
= node
->GetNext();
1470 wxImageHandler
*wxImage::FindHandlerMime( const wxString
& mimetype
)
1472 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
1475 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
1476 if (handler
->GetMimeType().IsSameAs(mimetype
, false)) return handler
;
1477 node
= node
->GetNext();
1482 void wxImage::InitStandardHandlers()
1485 AddHandler(new wxBMPHandler
);
1486 #endif // wxUSE_STREAMS
1489 void wxImage::CleanUpHandlers()
1491 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
1494 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
1495 wxList::compatibility_iterator next
= node
->GetNext();
1500 sm_handlers
.Clear();
1503 wxString
wxImage::GetImageExtWildcard()
1507 wxList
& Handlers
= wxImage::GetHandlers();
1508 wxList::compatibility_iterator Node
= Handlers
.GetFirst();
1511 wxImageHandler
* Handler
= (wxImageHandler
*)Node
->GetData();
1512 fmts
+= wxT("*.") + Handler
->GetExtension();
1513 Node
= Node
->GetNext();
1514 if ( Node
) fmts
+= wxT(";");
1517 return wxT("(") + fmts
+ wxT(")|") + fmts
;
1520 //-----------------------------------------------------------------------------
1522 //-----------------------------------------------------------------------------
1524 IMPLEMENT_ABSTRACT_CLASS(wxImageHandler
,wxObject
)
1527 bool wxImageHandler::LoadFile( wxImage
*WXUNUSED(image
), wxInputStream
& WXUNUSED(stream
), bool WXUNUSED(verbose
), int WXUNUSED(index
) )
1532 bool wxImageHandler::SaveFile( wxImage
*WXUNUSED(image
), wxOutputStream
& WXUNUSED(stream
), bool WXUNUSED(verbose
) )
1537 int wxImageHandler::GetImageCount( wxInputStream
& WXUNUSED(stream
) )
1542 bool wxImageHandler::CanRead( const wxString
& name
)
1544 if (wxFileExists(name
))
1546 wxFileInputStream
stream(name
);
1547 return CanRead(stream
);
1550 wxLogError( _("Can't check image format of file '%s': file does not exist."), name
.c_str() );
1555 bool wxImageHandler::CallDoCanRead(wxInputStream
& stream
)
1557 wxFileOffset posOld
= stream
.TellI();
1558 if ( posOld
== wxInvalidOffset
)
1560 // can't test unseekable stream
1564 bool ok
= DoCanRead(stream
);
1566 // restore the old position to be able to test other formats and so on
1567 if ( stream
.SeekI(posOld
) == wxInvalidOffset
)
1569 wxLogDebug(_T("Failed to rewind the stream in wxImageHandler!"));
1571 // reading would fail anyhow as we're not at the right position
1578 #endif // wxUSE_STREAMS
1580 // ----------------------------------------------------------------------------
1581 // image histogram stuff
1582 // ----------------------------------------------------------------------------
1585 wxImageHistogram::FindFirstUnusedColour(unsigned char *r
,
1590 unsigned char g2
) const
1592 unsigned long key
= MakeKey(r2
, g2
, b2
);
1594 while ( find(key
) != end() )
1596 // color already used
1608 wxLogError(_("No unused colour in image.") );
1614 key
= MakeKey(r2
, g2
, b2
);
1628 wxImage::FindFirstUnusedColour(unsigned char *r
,
1633 unsigned char g2
) const
1635 wxImageHistogram histogram
;
1637 ComputeHistogram(histogram
);
1639 return histogram
.FindFirstUnusedColour(r
, g
, b
, r2
, g2
, b2
);
1645 // Counts and returns the number of different colours. Optionally stops
1646 // when it exceeds 'stopafter' different colours. This is useful, for
1647 // example, to see if the image can be saved as 8-bit (256 colour or
1648 // less, in this case it would be invoked as CountColours(256)). Default
1649 // value for stopafter is -1 (don't care).
1651 unsigned long wxImage::CountColours( unsigned long stopafter
) const
1655 unsigned char r
, g
, b
;
1657 unsigned long size
, nentries
, key
;
1660 size
= GetWidth() * GetHeight();
1663 for (unsigned long j
= 0; (j
< size
) && (nentries
<= stopafter
) ; j
++)
1668 key
= wxImageHistogram::MakeKey(r
, g
, b
);
1670 if (h
.Get(key
) == NULL
)
1681 unsigned long wxImage::ComputeHistogram( wxImageHistogram
&h
) const
1683 unsigned char *p
= GetData();
1684 unsigned long nentries
= 0;
1688 const unsigned long size
= GetWidth() * GetHeight();
1690 unsigned char r
, g
, b
;
1691 for ( unsigned long n
= 0; n
< size
; n
++ )
1697 wxImageHistogramEntry
& entry
= h
[wxImageHistogram::MakeKey(r
, g
, b
)];
1699 if ( entry
.value
++ == 0 )
1700 entry
.index
= nentries
++;
1707 * Rotation code by Carlos Moreno
1710 // GRG: I've removed wxRotationPoint - we already have wxRealPoint which
1711 // does exactly the same thing. And I also got rid of wxRotationPixel
1712 // bacause of potential problems in architectures where alignment
1713 // is an issue, so I had to rewrite parts of the code.
1715 static const double gs_Epsilon
= 1e-10;
1717 static inline int wxCint (double x
)
1719 return (x
> 0) ? (int) (x
+ 0.5) : (int) (x
- 0.5);
1723 // Auxiliary function to rotate a point (x,y) with respect to point p0
1724 // make it inline and use a straight return to facilitate optimization
1725 // also, the function receives the sine and cosine of the angle to avoid
1726 // repeating the time-consuming calls to these functions -- sin/cos can
1727 // be computed and stored in the calling function.
1729 inline wxRealPoint
rotated_point (const wxRealPoint
& p
, double cos_angle
, double sin_angle
, const wxRealPoint
& p0
)
1731 return wxRealPoint (p0
.x
+ (p
.x
- p0
.x
) * cos_angle
- (p
.y
- p0
.y
) * sin_angle
,
1732 p0
.y
+ (p
.y
- p0
.y
) * cos_angle
+ (p
.x
- p0
.x
) * sin_angle
);
1735 inline wxRealPoint
rotated_point (double x
, double y
, double cos_angle
, double sin_angle
, const wxRealPoint
& p0
)
1737 return rotated_point (wxRealPoint(x
,y
), cos_angle
, sin_angle
, p0
);
1740 wxImage
wxImage::Rotate(double angle
, const wxPoint
& centre_of_rotation
, bool interpolating
, wxPoint
* offset_after_rotation
) const
1743 angle
= -angle
; // screen coordinates are a mirror image of "real" coordinates
1745 bool has_alpha
= HasAlpha();
1747 // Create pointer-based array to accelerate access to wxImage's data
1748 unsigned char ** data
= new unsigned char * [GetHeight()];
1749 data
[0] = GetData();
1750 for (i
= 1; i
< GetHeight(); i
++)
1751 data
[i
] = data
[i
- 1] + (3 * GetWidth());
1753 // Same for alpha channel
1754 unsigned char ** alpha
= NULL
;
1757 alpha
= new unsigned char * [GetHeight()];
1758 alpha
[0] = GetAlpha();
1759 for (i
= 1; i
< GetHeight(); i
++)
1760 alpha
[i
] = alpha
[i
- 1] + GetWidth();
1763 // precompute coefficients for rotation formula
1764 // (sine and cosine of the angle)
1765 const double cos_angle
= cos(angle
);
1766 const double sin_angle
= sin(angle
);
1768 // Create new Image to store the result
1769 // First, find rectangle that covers the rotated image; to do that,
1770 // rotate the four corners
1772 const wxRealPoint
p0(centre_of_rotation
.x
, centre_of_rotation
.y
);
1774 wxRealPoint p1
= rotated_point (0, 0, cos_angle
, sin_angle
, p0
);
1775 wxRealPoint p2
= rotated_point (0, GetHeight(), cos_angle
, sin_angle
, p0
);
1776 wxRealPoint p3
= rotated_point (GetWidth(), 0, cos_angle
, sin_angle
, p0
);
1777 wxRealPoint p4
= rotated_point (GetWidth(), GetHeight(), cos_angle
, sin_angle
, p0
);
1779 int x1
= (int) floor (wxMin (wxMin(p1
.x
, p2
.x
), wxMin(p3
.x
, p4
.x
)));
1780 int y1
= (int) floor (wxMin (wxMin(p1
.y
, p2
.y
), wxMin(p3
.y
, p4
.y
)));
1781 int x2
= (int) ceil (wxMax (wxMax(p1
.x
, p2
.x
), wxMax(p3
.x
, p4
.x
)));
1782 int y2
= (int) ceil (wxMax (wxMax(p1
.y
, p2
.y
), wxMax(p3
.y
, p4
.y
)));
1784 // Create rotated image
1785 wxImage
rotated (x2
- x1
+ 1, y2
- y1
+ 1, false);
1786 // With alpha channel
1790 if (offset_after_rotation
!= NULL
)
1792 *offset_after_rotation
= wxPoint (x1
, y1
);
1795 // GRG: The rotated (destination) image is always accessed
1796 // sequentially, so there is no need for a pointer-based
1797 // array here (and in fact it would be slower).
1799 unsigned char * dst
= rotated
.GetData();
1801 unsigned char * alpha_dst
= NULL
;
1803 alpha_dst
= rotated
.GetAlpha();
1805 // GRG: if the original image has a mask, use its RGB values
1806 // as the blank pixel, else, fall back to default (black).
1808 unsigned char blank_r
= 0;
1809 unsigned char blank_g
= 0;
1810 unsigned char blank_b
= 0;
1814 blank_r
= GetMaskRed();
1815 blank_g
= GetMaskGreen();
1816 blank_b
= GetMaskBlue();
1817 rotated
.SetMaskColour( blank_r
, blank_g
, blank_b
);
1820 // Now, for each point of the rotated image, find where it came from, by
1821 // performing an inverse rotation (a rotation of -angle) and getting the
1822 // pixel at those coordinates
1824 // GRG: I've taken the (interpolating) test out of the loops, so that
1825 // it is done only once, instead of repeating it for each pixel.
1830 for (int y
= 0; y
< rotated
.GetHeight(); y
++)
1832 for (x
= 0; x
< rotated
.GetWidth(); x
++)
1834 wxRealPoint src
= rotated_point (x
+ x1
, y
+ y1
, cos_angle
, -sin_angle
, p0
);
1836 if (-0.25 < src
.x
&& src
.x
< GetWidth() - 0.75 &&
1837 -0.25 < src
.y
&& src
.y
< GetHeight() - 0.75)
1839 // interpolate using the 4 enclosing grid-points. Those
1840 // points can be obtained using floor and ceiling of the
1841 // exact coordinates of the point
1842 // C.M. 2000-02-17: when the point is near the border, special care is required.
1846 if (0 < src
.x
&& src
.x
< GetWidth() - 1)
1848 x1
= wxCint(floor(src
.x
));
1849 x2
= wxCint(ceil(src
.x
));
1851 else // else means that x is near one of the borders (0 or width-1)
1853 x1
= x2
= wxCint (src
.x
);
1856 if (0 < src
.y
&& src
.y
< GetHeight() - 1)
1858 y1
= wxCint(floor(src
.y
));
1859 y2
= wxCint(ceil(src
.y
));
1863 y1
= y2
= wxCint (src
.y
);
1866 // get four points and the distances (square of the distance,
1867 // for efficiency reasons) for the interpolation formula
1869 // GRG: Do not calculate the points until they are
1870 // really needed -- this way we can calculate
1871 // just one, instead of four, if d1, d2, d3
1872 // or d4 are < gs_Epsilon
1874 const double d1
= (src
.x
- x1
) * (src
.x
- x1
) + (src
.y
- y1
) * (src
.y
- y1
);
1875 const double d2
= (src
.x
- x2
) * (src
.x
- x2
) + (src
.y
- y1
) * (src
.y
- y1
);
1876 const double d3
= (src
.x
- x2
) * (src
.x
- x2
) + (src
.y
- y2
) * (src
.y
- y2
);
1877 const double d4
= (src
.x
- x1
) * (src
.x
- x1
) + (src
.y
- y2
) * (src
.y
- y2
);
1879 // Now interpolate as a weighted average of the four surrounding
1880 // points, where the weights are the distances to each of those points
1882 // If the point is exactly at one point of the grid of the source
1883 // image, then don't interpolate -- just assign the pixel
1885 if (d1
< gs_Epsilon
) // d1,d2,d3,d4 are positive -- no need for abs()
1887 unsigned char *p
= data
[y1
] + (3 * x1
);
1894 unsigned char *p
= alpha
[y1
] + x1
;
1895 *(alpha_dst
++) = *p
;
1898 else if (d2
< gs_Epsilon
)
1900 unsigned char *p
= data
[y1
] + (3 * x2
);
1907 unsigned char *p
= alpha
[y1
] + x2
;
1908 *(alpha_dst
++) = *p
;
1911 else if (d3
< gs_Epsilon
)
1913 unsigned char *p
= data
[y2
] + (3 * x2
);
1920 unsigned char *p
= alpha
[y2
] + x2
;
1921 *(alpha_dst
++) = *p
;
1924 else if (d4
< gs_Epsilon
)
1926 unsigned char *p
= data
[y2
] + (3 * x1
);
1933 unsigned char *p
= alpha
[y2
] + x1
;
1934 *(alpha_dst
++) = *p
;
1939 // weights for the weighted average are proportional to the inverse of the distance
1940 unsigned char *v1
= data
[y1
] + (3 * x1
);
1941 unsigned char *v2
= data
[y1
] + (3 * x2
);
1942 unsigned char *v3
= data
[y2
] + (3 * x2
);
1943 unsigned char *v4
= data
[y2
] + (3 * x1
);
1945 const double w1
= 1/d1
, w2
= 1/d2
, w3
= 1/d3
, w4
= 1/d4
;
1949 *(dst
++) = (unsigned char)
1950 ( (w1
* *(v1
++) + w2
* *(v2
++) +
1951 w3
* *(v3
++) + w4
* *(v4
++)) /
1952 (w1
+ w2
+ w3
+ w4
) );
1953 *(dst
++) = (unsigned char)
1954 ( (w1
* *(v1
++) + w2
* *(v2
++) +
1955 w3
* *(v3
++) + w4
* *(v4
++)) /
1956 (w1
+ w2
+ w3
+ w4
) );
1957 *(dst
++) = (unsigned char)
1958 ( (w1
* *v1
+ w2
* *v2
+
1959 w3
* *v3
+ w4
* *v4
) /
1960 (w1
+ w2
+ w3
+ w4
) );
1964 unsigned char *v1
= alpha
[y1
] + (x1
);
1965 unsigned char *v2
= alpha
[y1
] + (x2
);
1966 unsigned char *v3
= alpha
[y2
] + (x2
);
1967 unsigned char *v4
= alpha
[y2
] + (x1
);
1969 *(alpha_dst
++) = (unsigned char)
1970 ( (w1
* *v1
+ w2
* *v2
+
1971 w3
* *v3
+ w4
* *v4
) /
1972 (w1
+ w2
+ w3
+ w4
) );
1988 else // not interpolating
1990 for (int y
= 0; y
< rotated
.GetHeight(); y
++)
1992 for (x
= 0; x
< rotated
.GetWidth(); x
++)
1994 wxRealPoint src
= rotated_point (x
+ x1
, y
+ y1
, cos_angle
, -sin_angle
, p0
);
1996 const int xs
= wxCint (src
.x
); // wxCint rounds to the
1997 const int ys
= wxCint (src
.y
); // closest integer
1999 if (0 <= xs
&& xs
< GetWidth() &&
2000 0 <= ys
&& ys
< GetHeight())
2002 unsigned char *p
= data
[ys
] + (3 * xs
);
2009 unsigned char *p
= alpha
[ys
] + (xs
);
2010 *(alpha_dst
++) = *p
;
2020 *(alpha_dst
++) = 255;
2038 // A module to allow wxImage initialization/cleanup
2039 // without calling these functions from app.cpp or from
2040 // the user's application.
2042 class wxImageModule
: public wxModule
2044 DECLARE_DYNAMIC_CLASS(wxImageModule
)
2047 bool OnInit() { wxImage::InitStandardHandlers(); return true; };
2048 void OnExit() { wxImage::CleanUpHandlers(); };
2051 IMPLEMENT_DYNAMIC_CLASS(wxImageModule
, wxModule
)
2054 #endif // wxUSE_IMAGE