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"
39 #include "wx/xpmdecod.h"
50 //-----------------------------------------------------------------------------
52 //-----------------------------------------------------------------------------
54 class wxImageRefData
: public wxObjectRefData
58 virtual ~wxImageRefData();
62 unsigned char *m_data
;
65 unsigned char m_maskRed
,m_maskGreen
,m_maskBlue
;
67 // alpha channel data, may be NULL for the formats without alpha support
68 unsigned char *m_alpha
;
75 #endif // wxUSE_PALETTE
77 wxArrayString m_optionNames
;
78 wxArrayString m_optionValues
;
80 DECLARE_NO_COPY_CLASS(wxImageRefData
)
83 wxImageRefData::wxImageRefData()
88 m_alpha
= (unsigned char *) NULL
;
99 wxImageRefData::~wxImageRefData()
108 wxList
wxImage::sm_handlers
;
112 //-----------------------------------------------------------------------------
114 #define M_IMGDATA ((wxImageRefData *)m_refData)
116 IMPLEMENT_DYNAMIC_CLASS(wxImage
, wxObject
)
118 wxImage::wxImage( int width
, int height
, bool clear
)
120 Create( width
, height
, clear
);
123 wxImage::wxImage( int width
, int height
, unsigned char* data
, bool static_data
)
125 Create( width
, height
, data
, static_data
);
128 wxImage::wxImage( int width
, int height
, unsigned char* data
, unsigned char* alpha
, bool static_data
)
130 Create( width
, height
, data
, alpha
, static_data
);
133 wxImage::wxImage( const wxString
& name
, long type
, int index
)
135 LoadFile( name
, type
, index
);
138 wxImage::wxImage( const wxString
& name
, const wxString
& mimetype
, int index
)
140 LoadFile( name
, mimetype
, index
);
144 wxImage::wxImage( wxInputStream
& stream
, long type
, int index
)
146 LoadFile( stream
, type
, index
);
149 wxImage::wxImage( wxInputStream
& stream
, const wxString
& mimetype
, int index
)
151 LoadFile( stream
, mimetype
, index
);
153 #endif // wxUSE_STREAMS
155 wxImage::wxImage( const wxImage
& image
)
161 wxImage::wxImage( const wxImage
* image
)
163 if (image
) Ref(*image
);
166 wxImage::wxImage( const char** xpmData
)
171 wxImage::wxImage( char** xpmData
)
173 Create((const char**) xpmData
);
176 bool wxImage::Create( const char** xpmData
)
181 wxXPMDecoder decoder
;
182 (*this) = decoder
.ReadData(xpmData
);
189 bool wxImage::Create( int width
, int height
, bool clear
)
193 m_refData
= new wxImageRefData();
195 M_IMGDATA
->m_data
= (unsigned char *) malloc( width
*height
*3 );
196 if (!M_IMGDATA
->m_data
)
203 memset(M_IMGDATA
->m_data
, 0, width
*height
*3);
205 M_IMGDATA
->m_width
= width
;
206 M_IMGDATA
->m_height
= height
;
207 M_IMGDATA
->m_ok
= true;
212 bool wxImage::Create( int width
, int height
, unsigned char* data
, bool static_data
)
216 wxCHECK_MSG( data
, false, _T("NULL data in wxImage::Create") );
218 m_refData
= new wxImageRefData();
220 M_IMGDATA
->m_data
= data
;
221 M_IMGDATA
->m_width
= width
;
222 M_IMGDATA
->m_height
= height
;
223 M_IMGDATA
->m_ok
= true;
224 M_IMGDATA
->m_static
= static_data
;
229 bool wxImage::Create( int width
, int height
, unsigned char* data
, unsigned char* alpha
, bool static_data
)
233 wxCHECK_MSG( data
, false, _T("NULL data in wxImage::Create") );
235 m_refData
= new wxImageRefData();
237 M_IMGDATA
->m_data
= data
;
238 M_IMGDATA
->m_alpha
= alpha
;
239 M_IMGDATA
->m_width
= width
;
240 M_IMGDATA
->m_height
= height
;
241 M_IMGDATA
->m_ok
= true;
242 M_IMGDATA
->m_static
= static_data
;
247 void wxImage::Destroy()
252 wxImage
wxImage::Copy() const
256 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
258 image
.Create( M_IMGDATA
->m_width
, M_IMGDATA
->m_height
, false );
260 unsigned char *data
= image
.GetData();
262 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
264 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
265 image
.SetMask( M_IMGDATA
->m_hasMask
);
267 memcpy( data
, GetData(), M_IMGDATA
->m_width
*M_IMGDATA
->m_height
*3 );
269 // also copy the image options
270 wxImageRefData
*imgData
= (wxImageRefData
*)image
.m_refData
;
271 imgData
->m_optionNames
= M_IMGDATA
->m_optionNames
;
272 imgData
->m_optionValues
= M_IMGDATA
->m_optionValues
;
277 wxImage
wxImage::ShrinkBy( int xFactor
, int yFactor
) const
279 if( xFactor
== 1 && yFactor
== 1 )
284 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
286 // can't scale to/from 0 size
287 wxCHECK_MSG( (xFactor
> 0) && (yFactor
> 0), image
,
288 wxT("invalid new image size") );
290 long old_height
= M_IMGDATA
->m_height
,
291 old_width
= M_IMGDATA
->m_width
;
293 wxCHECK_MSG( (old_height
> 0) && (old_width
> 0), image
,
294 wxT("invalid old image size") );
296 long width
= old_width
/ xFactor
;
297 long height
= old_height
/ yFactor
;
299 image
.Create( width
, height
, false );
301 char unsigned *data
= image
.GetData();
303 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
305 bool hasMask
= false ;
306 unsigned char maskRed
= 0;
307 unsigned char maskGreen
= 0;
308 unsigned char maskBlue
=0 ;
310 unsigned char *source_data
= M_IMGDATA
->m_data
;
311 unsigned char *target_data
= data
;
312 unsigned char *source_alpha
= 0 ;
313 unsigned char *target_alpha
= 0 ;
314 if (M_IMGDATA
->m_hasMask
)
317 maskRed
= M_IMGDATA
->m_maskRed
;
318 maskGreen
= M_IMGDATA
->m_maskGreen
;
319 maskBlue
=M_IMGDATA
->m_maskBlue
;
321 image
.SetMaskColour( M_IMGDATA
->m_maskRed
,
322 M_IMGDATA
->m_maskGreen
,
323 M_IMGDATA
->m_maskBlue
);
327 source_alpha
= M_IMGDATA
->m_alpha
;
331 target_alpha
= image
.GetAlpha() ;
335 for (long y
= 0; y
< height
; y
++)
337 for (long x
= 0; x
< width
; x
++)
339 unsigned long avgRed
= 0 ;
340 unsigned long avgGreen
= 0;
341 unsigned long avgBlue
= 0;
342 unsigned long avgAlpha
= 0 ;
343 unsigned long counter
= 0 ;
345 for ( int y1
= 0 ; y1
< yFactor
; ++y1
)
347 long y_offset
= (y
* yFactor
+ y1
) * old_width
;
348 for ( int x1
= 0 ; x1
< xFactor
; ++x1
)
350 unsigned char *pixel
= source_data
+ 3 * ( y_offset
+ x
* xFactor
+ x1
) ;
351 unsigned char red
= pixel
[0] ;
352 unsigned char green
= pixel
[1] ;
353 unsigned char blue
= pixel
[2] ;
354 unsigned char alpha
= 255 ;
356 alpha
= *(source_alpha
+ y_offset
+ x
* xFactor
+ x1
) ;
357 if ( !hasMask
|| red
!= maskRed
|| green
!= maskGreen
|| blue
!= maskBlue
)
372 *(target_data
++) = M_IMGDATA
->m_maskRed
;
373 *(target_data
++) = M_IMGDATA
->m_maskGreen
;
374 *(target_data
++) = M_IMGDATA
->m_maskBlue
;
379 *(target_alpha
++) = (unsigned char)(avgAlpha
/ counter
) ;
380 *(target_data
++) = (unsigned char)(avgRed
/ counter
);
381 *(target_data
++) = (unsigned char)(avgGreen
/ counter
);
382 *(target_data
++) = (unsigned char)(avgBlue
/ counter
);
387 // In case this is a cursor, make sure the hotspot is scalled accordingly:
388 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
) )
389 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
,
390 (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X
))/xFactor
);
391 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
) )
392 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
,
393 (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y
))/yFactor
);
398 wxImage
wxImage::Scale( int width
, int height
) const
402 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
404 // can't scale to/from 0 size
405 wxCHECK_MSG( (width
> 0) && (height
> 0), image
,
406 wxT("invalid new image size") );
408 long old_height
= M_IMGDATA
->m_height
,
409 old_width
= M_IMGDATA
->m_width
;
410 wxCHECK_MSG( (old_height
> 0) && (old_width
> 0), image
,
411 wxT("invalid old image size") );
413 if ( old_width
% width
== 0 && old_width
>= width
&&
414 old_height
% height
== 0 && old_height
>= height
)
416 return ShrinkBy( old_width
/ width
, old_height
/ height
) ;
418 image
.Create( width
, height
, false );
420 unsigned char *data
= image
.GetData();
422 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
424 unsigned char *source_data
= M_IMGDATA
->m_data
;
425 unsigned char *target_data
= data
;
426 unsigned char *source_alpha
= 0 ;
427 unsigned char *target_alpha
= 0 ;
429 if (M_IMGDATA
->m_hasMask
)
431 image
.SetMaskColour( M_IMGDATA
->m_maskRed
,
432 M_IMGDATA
->m_maskGreen
,
433 M_IMGDATA
->m_maskBlue
);
437 source_alpha
= M_IMGDATA
->m_alpha
;
441 target_alpha
= image
.GetAlpha() ;
445 long x_delta
= (old_width
<<16) / width
;
446 long y_delta
= (old_height
<<16) / height
;
448 unsigned char* dest_pixel
= target_data
;
451 for ( long j
= 0; j
< height
; j
++ )
453 unsigned char* src_line
= &source_data
[(y
>>16)*old_width
*3];
454 unsigned char* src_alpha_line
= source_alpha
? &source_alpha
[(y
>>16)*old_width
] : 0 ;
457 for ( long i
= 0; i
< width
; i
++ )
459 unsigned char* src_pixel
= &src_line
[(x
>>16)*3];
460 unsigned char* src_alpha_pixel
= source_alpha
? &src_alpha_line
[(x
>>16)] : 0 ;
461 dest_pixel
[0] = src_pixel
[0];
462 dest_pixel
[1] = src_pixel
[1];
463 dest_pixel
[2] = src_pixel
[2];
466 *(target_alpha
++) = *src_alpha_pixel
;
473 // In case this is a cursor, make sure the hotspot is scalled accordingly:
474 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
) )
475 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
,
476 (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X
)*width
)/old_width
);
477 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
) )
478 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
,
479 (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y
)*height
)/old_height
);
484 wxImage
wxImage::Rotate90( bool clockwise
) const
488 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
490 image
.Create( M_IMGDATA
->m_height
, M_IMGDATA
->m_width
, false );
492 unsigned char *data
= image
.GetData();
494 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
496 if (M_IMGDATA
->m_hasMask
)
497 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
499 long height
= M_IMGDATA
->m_height
;
500 long width
= M_IMGDATA
->m_width
;
502 unsigned char *source_data
= M_IMGDATA
->m_data
;
503 unsigned char *target_data
;
505 for (long j
= 0; j
< height
; j
++)
507 for (long i
= 0; i
< width
; i
++)
510 target_data
= data
+ (((i
+1)*height
) - j
- 1)*3;
512 target_data
= data
+ ((height
*(width
-1)) + j
- (i
*height
))*3;
513 memcpy( target_data
, source_data
, 3 );
521 wxImage
wxImage::Mirror( bool horizontally
) const
525 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
527 image
.Create( M_IMGDATA
->m_width
, M_IMGDATA
->m_height
, false );
529 unsigned char *data
= image
.GetData();
531 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
533 if (M_IMGDATA
->m_hasMask
)
534 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
536 long height
= M_IMGDATA
->m_height
;
537 long width
= M_IMGDATA
->m_width
;
539 unsigned char *source_data
= M_IMGDATA
->m_data
;
540 unsigned char *target_data
;
544 for (long j
= 0; j
< height
; j
++)
547 target_data
= data
-3;
548 for (long i
= 0; i
< width
; i
++)
550 memcpy( target_data
, source_data
, 3 );
558 for (long i
= 0; i
< height
; i
++)
560 target_data
= data
+ 3*width
*(height
-1-i
);
561 memcpy( target_data
, source_data
, (size_t)3*width
);
562 source_data
+= 3*width
;
569 wxImage
wxImage::GetSubImage( const wxRect
&rect
) const
573 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
575 wxCHECK_MSG( (rect
.GetLeft()>=0) && (rect
.GetTop()>=0) && (rect
.GetRight()<=GetWidth()) && (rect
.GetBottom()<=GetHeight()),
576 image
, wxT("invalid subimage size") );
578 int subwidth
=rect
.GetWidth();
579 const int subheight
=rect
.GetHeight();
581 image
.Create( subwidth
, subheight
, false );
583 unsigned char *subdata
= image
.GetData(), *data
=GetData();
585 wxCHECK_MSG( subdata
, image
, wxT("unable to create image") );
587 if (M_IMGDATA
->m_hasMask
)
588 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
590 const int subleft
=3*rect
.GetLeft();
591 const int width
=3*GetWidth();
594 data
+=rect
.GetTop()*width
+subleft
;
596 for (long j
= 0; j
< subheight
; ++j
)
598 memcpy( subdata
, data
, subwidth
);
606 void wxImage::Paste( const wxImage
&image
, int x
, int y
)
608 wxCHECK_RET( Ok(), wxT("invalid image") );
609 wxCHECK_RET( image
.Ok(), wxT("invalid image") );
613 int width
= image
.GetWidth();
614 int height
= image
.GetHeight();
627 if ((x
+xx
)+width
> M_IMGDATA
->m_width
)
628 width
= M_IMGDATA
->m_width
- (x
+xx
);
629 if ((y
+yy
)+height
> M_IMGDATA
->m_height
)
630 height
= M_IMGDATA
->m_height
- (y
+yy
);
632 if (width
< 1) return;
633 if (height
< 1) return;
635 if ((!HasMask() && !image
.HasMask()) ||
636 ((HasMask() && image
.HasMask() &&
637 (GetMaskRed()==image
.GetMaskRed()) &&
638 (GetMaskGreen()==image
.GetMaskGreen()) &&
639 (GetMaskBlue()==image
.GetMaskBlue()))))
642 unsigned char* source_data
= image
.GetData() + xx
*3 + yy
*3*image
.GetWidth();
643 int source_step
= image
.GetWidth()*3;
645 unsigned char* target_data
= GetData() + (x
+xx
)*3 + (y
+yy
)*3*M_IMGDATA
->m_width
;
646 int target_step
= M_IMGDATA
->m_width
*3;
647 for (int j
= 0; j
< height
; j
++)
649 memcpy( target_data
, source_data
, width
);
650 source_data
+= source_step
;
651 target_data
+= target_step
;
656 if (!HasMask() && image
.HasMask())
658 unsigned char r
= image
.GetMaskRed();
659 unsigned char g
= image
.GetMaskGreen();
660 unsigned char b
= image
.GetMaskBlue();
663 unsigned char* source_data
= image
.GetData() + xx
*3 + yy
*3*image
.GetWidth();
664 int source_step
= image
.GetWidth()*3;
666 unsigned char* target_data
= GetData() + (x
+xx
)*3 + (y
+yy
)*3*M_IMGDATA
->m_width
;
667 int target_step
= M_IMGDATA
->m_width
*3;
669 for (int j
= 0; j
< height
; j
++)
671 for (int i
= 0; i
< width
; i
+=3)
673 if ((source_data
[i
] != r
) &&
674 (source_data
[i
+1] != g
) &&
675 (source_data
[i
+2] != b
))
677 memcpy( target_data
+i
, source_data
+i
, 3 );
680 source_data
+= source_step
;
681 target_data
+= target_step
;
686 void wxImage::Replace( unsigned char r1
, unsigned char g1
, unsigned char b1
,
687 unsigned char r2
, unsigned char g2
, unsigned char b2
)
689 wxCHECK_RET( Ok(), wxT("invalid image") );
691 unsigned char *data
= GetData();
693 const int w
= GetWidth();
694 const int h
= GetHeight();
696 for (int j
= 0; j
< h
; j
++)
697 for (int i
= 0; i
< w
; i
++)
699 if ((data
[0] == r1
) && (data
[1] == g1
) && (data
[2] == b1
))
709 wxImage
wxImage::ConvertToMono( unsigned char r
, unsigned char g
, unsigned char b
) const
713 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
715 image
.Create( M_IMGDATA
->m_width
, M_IMGDATA
->m_height
, false );
717 unsigned char *data
= image
.GetData();
719 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
721 if (M_IMGDATA
->m_hasMask
)
723 if (M_IMGDATA
->m_maskRed
== r
&& M_IMGDATA
->m_maskGreen
== g
&&
724 M_IMGDATA
->m_maskBlue
== b
)
725 image
.SetMaskColour( 255, 255, 255 );
727 image
.SetMaskColour( 0, 0, 0 );
730 long size
= M_IMGDATA
->m_height
* M_IMGDATA
->m_width
;
732 unsigned char *srcd
= M_IMGDATA
->m_data
;
733 unsigned char *tard
= image
.GetData();
735 for ( long i
= 0; i
< size
; i
++, srcd
+= 3, tard
+= 3 )
737 if (srcd
[0] == r
&& srcd
[1] == g
&& srcd
[2] == b
)
738 tard
[0] = tard
[1] = tard
[2] = 255;
740 tard
[0] = tard
[1] = tard
[2] = 0;
746 void wxImage::SetRGB( int x
, int y
, unsigned char r
, unsigned char g
, unsigned char b
)
748 wxCHECK_RET( Ok(), wxT("invalid image") );
750 int w
= M_IMGDATA
->m_width
;
751 int h
= M_IMGDATA
->m_height
;
753 wxCHECK_RET( (x
>=0) && (y
>=0) && (x
<w
) && (y
<h
), wxT("invalid image index") );
755 long pos
= (y
* w
+ x
) * 3;
757 M_IMGDATA
->m_data
[ pos
] = r
;
758 M_IMGDATA
->m_data
[ pos
+1 ] = g
;
759 M_IMGDATA
->m_data
[ pos
+2 ] = b
;
762 unsigned char wxImage::GetRed( int x
, int y
) const
764 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
766 int w
= M_IMGDATA
->m_width
;
767 int h
= M_IMGDATA
->m_height
;
769 wxCHECK_MSG( (x
>=0) && (y
>=0) && (x
<w
) && (y
<h
), 0, wxT("invalid image index") );
771 long pos
= (y
* w
+ x
) * 3;
773 return M_IMGDATA
->m_data
[pos
];
776 unsigned char wxImage::GetGreen( int x
, int y
) const
778 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
780 int w
= M_IMGDATA
->m_width
;
781 int h
= M_IMGDATA
->m_height
;
783 wxCHECK_MSG( (x
>=0) && (y
>=0) && (x
<w
) && (y
<h
), 0, wxT("invalid image index") );
785 long pos
= (y
* w
+ x
) * 3;
787 return M_IMGDATA
->m_data
[pos
+1];
790 unsigned char wxImage::GetBlue( int x
, int y
) const
792 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
794 int w
= M_IMGDATA
->m_width
;
795 int h
= M_IMGDATA
->m_height
;
797 wxCHECK_MSG( (x
>=0) && (y
>=0) && (x
<w
) && (y
<h
), 0, wxT("invalid image index") );
799 long pos
= (y
* w
+ x
) * 3;
801 return M_IMGDATA
->m_data
[pos
+2];
804 bool wxImage::Ok() const
806 // image of 0 width or height can't be considered ok - at least because it
807 // causes crashes in ConvertToBitmap() if we don't catch it in time
808 wxImageRefData
*data
= M_IMGDATA
;
809 return data
&& data
->m_ok
&& data
->m_width
&& data
->m_height
;
812 unsigned char *wxImage::GetData() const
814 wxCHECK_MSG( Ok(), (unsigned char *)NULL
, wxT("invalid image") );
816 return M_IMGDATA
->m_data
;
819 void wxImage::SetData( unsigned char *data
)
821 wxCHECK_RET( Ok(), wxT("invalid image") );
823 wxImageRefData
*newRefData
= new wxImageRefData();
825 newRefData
->m_width
= M_IMGDATA
->m_width
;
826 newRefData
->m_height
= M_IMGDATA
->m_height
;
827 newRefData
->m_data
= data
;
828 newRefData
->m_ok
= true;
829 newRefData
->m_maskRed
= M_IMGDATA
->m_maskRed
;
830 newRefData
->m_maskGreen
= M_IMGDATA
->m_maskGreen
;
831 newRefData
->m_maskBlue
= M_IMGDATA
->m_maskBlue
;
832 newRefData
->m_hasMask
= M_IMGDATA
->m_hasMask
;
836 m_refData
= newRefData
;
839 void wxImage::SetData( unsigned char *data
, int new_width
, int new_height
)
841 wxImageRefData
*newRefData
= new wxImageRefData();
845 newRefData
->m_width
= new_width
;
846 newRefData
->m_height
= new_height
;
847 newRefData
->m_data
= data
;
848 newRefData
->m_ok
= true;
849 newRefData
->m_maskRed
= M_IMGDATA
->m_maskRed
;
850 newRefData
->m_maskGreen
= M_IMGDATA
->m_maskGreen
;
851 newRefData
->m_maskBlue
= M_IMGDATA
->m_maskBlue
;
852 newRefData
->m_hasMask
= M_IMGDATA
->m_hasMask
;
856 newRefData
->m_width
= new_width
;
857 newRefData
->m_height
= new_height
;
858 newRefData
->m_data
= data
;
859 newRefData
->m_ok
= true;
864 m_refData
= newRefData
;
867 // ----------------------------------------------------------------------------
868 // alpha channel support
869 // ----------------------------------------------------------------------------
871 void wxImage::SetAlpha(int x
, int y
, unsigned char alpha
)
873 wxCHECK_RET( Ok() && HasAlpha(), wxT("invalid image or no alpha channel") );
875 int w
= M_IMGDATA
->m_width
,
876 h
= M_IMGDATA
->m_height
;
878 wxCHECK_RET( x
>=0 && y
>= 0 && x
< w
&& y
< h
, wxT("invalid image index") );
880 M_IMGDATA
->m_alpha
[y
*w
+ x
] = alpha
;
883 unsigned char wxImage::GetAlpha(int x
, int y
) const
885 wxCHECK_MSG( Ok() && HasAlpha(), 0, wxT("invalid image or no alpha channel") );
887 int w
= M_IMGDATA
->m_width
,
888 h
= M_IMGDATA
->m_height
;
890 wxCHECK_MSG( x
>=0 && y
>= 0 && x
< w
&& y
< h
, 0, wxT("invalid image index") );
892 return M_IMGDATA
->m_alpha
[y
*w
+ x
];
895 bool wxImage::ConvertColourToAlpha( unsigned char r
, unsigned char g
, unsigned char b
)
899 int w
= M_IMGDATA
->m_width
,
900 h
= M_IMGDATA
->m_height
;
902 unsigned char *alpha
= GetAlpha();
903 unsigned char *data
= GetData();
906 for (y
= 0; y
< h
; y
++)
907 for (x
= 0; x
< w
; x
++)
922 void wxImage::SetAlpha( unsigned char *alpha
)
924 wxCHECK_RET( Ok(), wxT("invalid image") );
928 alpha
= (unsigned char *)malloc(M_IMGDATA
->m_width
*M_IMGDATA
->m_height
);
931 free(M_IMGDATA
->m_alpha
);
932 M_IMGDATA
->m_alpha
= alpha
;
935 unsigned char *wxImage::GetAlpha() const
937 wxCHECK_MSG( Ok(), (unsigned char *)NULL
, wxT("invalid image") );
939 return M_IMGDATA
->m_alpha
;
942 void wxImage::InitAlpha()
944 wxCHECK_RET( !HasAlpha(), wxT("image already has an alpha channel") );
946 // initialize memory for alpha channel
949 unsigned char *alpha
= M_IMGDATA
->m_alpha
;
950 const size_t lenAlpha
= M_IMGDATA
->m_width
* M_IMGDATA
->m_height
;
952 static const unsigned char ALPHA_TRANSPARENT
= 0;
953 static const unsigned char ALPHA_OPAQUE
= 0xff;
956 // use the mask to initialize the alpha channel.
957 const unsigned char * const alphaEnd
= alpha
+ lenAlpha
;
959 const unsigned char mr
= M_IMGDATA
->m_maskRed
;
960 const unsigned char mg
= M_IMGDATA
->m_maskGreen
;
961 const unsigned char mb
= M_IMGDATA
->m_maskBlue
;
962 for ( unsigned char *src
= M_IMGDATA
->m_data
;
966 *alpha
= (src
[0] == mr
&& src
[1] == mg
&& src
[2] == mb
)
971 M_IMGDATA
->m_hasMask
= false;
975 // make the image fully opaque
976 memset(alpha
, ALPHA_OPAQUE
, lenAlpha
);
980 // ----------------------------------------------------------------------------
982 // ----------------------------------------------------------------------------
984 void wxImage::SetMaskColour( unsigned char r
, unsigned char g
, unsigned char b
)
986 wxCHECK_RET( Ok(), wxT("invalid image") );
988 M_IMGDATA
->m_maskRed
= r
;
989 M_IMGDATA
->m_maskGreen
= g
;
990 M_IMGDATA
->m_maskBlue
= b
;
991 M_IMGDATA
->m_hasMask
= true;
994 unsigned char wxImage::GetMaskRed() const
996 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
998 return M_IMGDATA
->m_maskRed
;
1001 unsigned char wxImage::GetMaskGreen() const
1003 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
1005 return M_IMGDATA
->m_maskGreen
;
1008 unsigned char wxImage::GetMaskBlue() const
1010 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
1012 return M_IMGDATA
->m_maskBlue
;
1015 void wxImage::SetMask( bool mask
)
1017 wxCHECK_RET( Ok(), wxT("invalid image") );
1019 M_IMGDATA
->m_hasMask
= mask
;
1022 bool wxImage::HasMask() const
1024 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1026 return M_IMGDATA
->m_hasMask
;
1029 int wxImage::GetWidth() const
1031 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
1033 return M_IMGDATA
->m_width
;
1036 int wxImage::GetHeight() const
1038 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
1040 return M_IMGDATA
->m_height
;
1043 bool wxImage::SetMaskFromImage(const wxImage
& mask
,
1044 unsigned char mr
, unsigned char mg
, unsigned char mb
)
1046 // check that the images are the same size
1047 if ( (M_IMGDATA
->m_height
!= mask
.GetHeight() ) || (M_IMGDATA
->m_width
!= mask
.GetWidth () ) )
1049 wxLogError( _("Image and mask have different sizes.") );
1053 // find unused colour
1054 unsigned char r
,g
,b
;
1055 if (!FindFirstUnusedColour(&r
, &g
, &b
))
1057 wxLogError( _("No unused colour in image being masked.") );
1061 unsigned char *imgdata
= GetData();
1062 unsigned char *maskdata
= mask
.GetData();
1064 const int w
= GetWidth();
1065 const int h
= GetHeight();
1067 for (int j
= 0; j
< h
; j
++)
1069 for (int i
= 0; i
< w
; i
++)
1071 if ((maskdata
[0] == mr
) && (maskdata
[1] == mg
) && (maskdata
[2] == mb
))
1082 SetMaskColour(r
, g
, b
);
1088 bool wxImage::ConvertAlphaToMask(unsigned char threshold
)
1093 unsigned char mr
, mg
, mb
;
1094 if (!FindFirstUnusedColour(&mr
, &mg
, &mb
))
1096 wxLogError( _("No unused colour in image being masked.") );
1101 SetMaskColour(mr
, mg
, mb
);
1103 unsigned char *imgdata
= GetData();
1104 unsigned char *alphadata
= GetAlpha();
1107 int h
= GetHeight();
1109 for (int y
= 0; y
< h
; y
++)
1111 for (int x
= 0; x
< w
; x
++, imgdata
+= 3, alphadata
++)
1113 if (*alphadata
< threshold
)
1122 free(M_IMGDATA
->m_alpha
);
1123 M_IMGDATA
->m_alpha
= NULL
;
1130 // Palette functions
1132 bool wxImage::HasPalette() const
1137 return M_IMGDATA
->m_palette
.Ok();
1140 const wxPalette
& wxImage::GetPalette() const
1142 wxCHECK_MSG( Ok(), wxNullPalette
, wxT("invalid image") );
1144 return M_IMGDATA
->m_palette
;
1147 void wxImage::SetPalette(const wxPalette
& palette
)
1149 wxCHECK_RET( Ok(), wxT("invalid image") );
1151 M_IMGDATA
->m_palette
= palette
;
1154 #endif // wxUSE_PALETTE
1156 // Option functions (arbitrary name/value mapping)
1157 void wxImage::SetOption(const wxString
& name
, const wxString
& value
)
1159 wxCHECK_RET( Ok(), wxT("invalid image") );
1161 int idx
= M_IMGDATA
->m_optionNames
.Index(name
, false);
1162 if (idx
== wxNOT_FOUND
)
1164 M_IMGDATA
->m_optionNames
.Add(name
);
1165 M_IMGDATA
->m_optionValues
.Add(value
);
1169 M_IMGDATA
->m_optionNames
[idx
] = name
;
1170 M_IMGDATA
->m_optionValues
[idx
] = value
;
1174 void wxImage::SetOption(const wxString
& name
, int value
)
1177 valStr
.Printf(wxT("%d"), value
);
1178 SetOption(name
, valStr
);
1181 wxString
wxImage::GetOption(const wxString
& name
) const
1183 wxCHECK_MSG( Ok(), wxEmptyString
, wxT("invalid image") );
1185 int idx
= M_IMGDATA
->m_optionNames
.Index(name
, false);
1186 if (idx
== wxNOT_FOUND
)
1187 return wxEmptyString
;
1189 return M_IMGDATA
->m_optionValues
[idx
];
1192 int wxImage::GetOptionInt(const wxString
& name
) const
1194 return wxAtoi(GetOption(name
));
1197 bool wxImage::HasOption(const wxString
& name
) const
1199 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1201 return (M_IMGDATA
->m_optionNames
.Index(name
, false) != wxNOT_FOUND
);
1204 bool wxImage::LoadFile( const wxString
& filename
, long type
, int index
)
1207 if (wxFileExists(filename
))
1209 wxFileInputStream
stream(filename
);
1210 wxBufferedInputStream
bstream( stream
);
1211 return LoadFile(bstream
, type
, index
);
1215 wxLogError( _("Can't load image from file '%s': file does not exist."), filename
.c_str() );
1219 #else // !wxUSE_STREAMS
1221 #endif // wxUSE_STREAMS
1224 bool wxImage::LoadFile( const wxString
& filename
, const wxString
& mimetype
, int index
)
1227 if (wxFileExists(filename
))
1229 wxFileInputStream
stream(filename
);
1230 wxBufferedInputStream
bstream( stream
);
1231 return LoadFile(bstream
, mimetype
, index
);
1235 wxLogError( _("Can't load image from file '%s': file does not exist."), filename
.c_str() );
1239 #else // !wxUSE_STREAMS
1241 #endif // wxUSE_STREAMS
1246 bool wxImage::SaveFile( const wxString
& filename
) const
1248 wxString ext
= filename
.AfterLast('.').Lower();
1250 wxImageHandler
* pHandler
= FindHandler(ext
, -1);
1253 SaveFile(filename
, pHandler
->GetType());
1257 wxLogError(_("Can't save image to file '%s': unknown extension."), filename
.c_str());
1262 bool wxImage::SaveFile( const wxString
& filename
, int type
) const
1265 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1267 ((wxImage
*)this)->SetOption(wxIMAGE_OPTION_FILENAME
, filename
);
1269 wxFileOutputStream
stream(filename
);
1271 if ( stream
.IsOk() )
1273 wxBufferedOutputStream
bstream( stream
);
1274 return SaveFile(bstream
, type
);
1276 #endif // wxUSE_STREAMS
1281 bool wxImage::SaveFile( const wxString
& filename
, const wxString
& mimetype
) const
1284 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1286 ((wxImage
*)this)->SetOption(wxIMAGE_OPTION_FILENAME
, filename
);
1288 wxFileOutputStream
stream(filename
);
1290 if ( stream
.IsOk() )
1292 wxBufferedOutputStream
bstream( stream
);
1293 return SaveFile(bstream
, mimetype
);
1295 #endif // wxUSE_STREAMS
1300 bool wxImage::CanRead( const wxString
&name
)
1303 wxFileInputStream
stream(name
);
1304 return CanRead(stream
);
1310 int wxImage::GetImageCount( const wxString
&name
, long type
)
1313 wxFileInputStream
stream(name
);
1315 return GetImageCount(stream
, type
);
1323 bool wxImage::CanRead( wxInputStream
&stream
)
1325 const wxList
& list
= GetHandlers();
1327 for ( wxList::compatibility_iterator node
= list
.GetFirst(); node
; node
= node
->GetNext() )
1329 wxImageHandler
*handler
=(wxImageHandler
*)node
->GetData();
1330 if (handler
->CanRead( stream
))
1337 int wxImage::GetImageCount( wxInputStream
&stream
, long type
)
1339 wxImageHandler
*handler
;
1341 if ( type
== wxBITMAP_TYPE_ANY
)
1343 wxList
&list
=GetHandlers();
1345 for (wxList::compatibility_iterator node
= list
.GetFirst(); node
; node
= node
->GetNext())
1347 handler
=(wxImageHandler
*)node
->GetData();
1348 if ( handler
->CanRead(stream
) )
1349 return handler
->GetImageCount(stream
);
1353 wxLogWarning(_("No handler found for image type."));
1357 handler
= FindHandler(type
);
1361 wxLogWarning(_("No image handler for type %d defined."), type
);
1365 if ( handler
->CanRead(stream
) )
1367 return handler
->GetImageCount(stream
);
1371 wxLogError(_("Image file is not of type %d."), type
);
1376 bool wxImage::LoadFile( wxInputStream
& stream
, long type
, int index
)
1380 m_refData
= new wxImageRefData
;
1382 wxImageHandler
*handler
;
1384 if ( type
== wxBITMAP_TYPE_ANY
)
1386 wxList
&list
=GetHandlers();
1388 for ( wxList::compatibility_iterator node
= list
.GetFirst(); node
; node
= node
->GetNext() )
1390 handler
=(wxImageHandler
*)node
->GetData();
1391 if ( handler
->CanRead(stream
) )
1392 return handler
->LoadFile(this, stream
, true/*verbose*/, index
);
1396 wxLogWarning( _("No handler found for image type.") );
1400 handler
= FindHandler(type
);
1404 wxLogWarning( _("No image handler for type %d defined."), type
);
1409 return handler
->LoadFile(this, stream
, true/*verbose*/, index
);
1412 bool wxImage::LoadFile( wxInputStream
& stream
, const wxString
& mimetype
, int index
)
1416 m_refData
= new wxImageRefData
;
1418 wxImageHandler
*handler
= FindHandlerMime(mimetype
);
1422 wxLogWarning( _("No image handler for type %s defined."), mimetype
.GetData() );
1427 return handler
->LoadFile( this, stream
, true/*verbose*/, index
);
1430 bool wxImage::SaveFile( wxOutputStream
& stream
, int type
) const
1432 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1434 wxImageHandler
*handler
= FindHandler(type
);
1437 wxLogWarning( _("No image handler for type %d defined."), type
);
1442 return handler
->SaveFile( (wxImage
*)this, stream
);
1445 bool wxImage::SaveFile( wxOutputStream
& stream
, const wxString
& mimetype
) const
1447 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1449 wxImageHandler
*handler
= FindHandlerMime(mimetype
);
1452 wxLogWarning( _("No image handler for type %s defined."), mimetype
.GetData() );
1457 return handler
->SaveFile( (wxImage
*)this, stream
);
1459 #endif // wxUSE_STREAMS
1461 void wxImage::AddHandler( wxImageHandler
*handler
)
1463 // Check for an existing handler of the type being added.
1464 if (FindHandler( handler
->GetType() ) == 0)
1466 sm_handlers
.Append( handler
);
1470 // This is not documented behaviour, merely the simplest 'fix'
1471 // for preventing duplicate additions. If someone ever has
1472 // a good reason to add and remove duplicate handlers (and they
1473 // may) we should probably refcount the duplicates.
1474 // also an issue in InsertHandler below.
1476 wxLogDebug( _T("Adding duplicate image handler for '%s'"),
1477 handler
->GetName().c_str() );
1482 void wxImage::InsertHandler( wxImageHandler
*handler
)
1484 // Check for an existing handler of the type being added.
1485 if (FindHandler( handler
->GetType() ) == 0)
1487 sm_handlers
.Insert( handler
);
1491 // see AddHandler for additional comments.
1492 wxLogDebug( _T("Inserting duplicate image handler for '%s'"),
1493 handler
->GetName().c_str() );
1498 bool wxImage::RemoveHandler( const wxString
& name
)
1500 wxImageHandler
*handler
= FindHandler(name
);
1503 sm_handlers
.DeleteObject(handler
);
1511 wxImageHandler
*wxImage::FindHandler( const wxString
& name
)
1513 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
1516 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
1517 if (handler
->GetName().Cmp(name
) == 0) return handler
;
1519 node
= node
->GetNext();
1524 wxImageHandler
*wxImage::FindHandler( const wxString
& extension
, long bitmapType
)
1526 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
1529 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
1530 if ( (handler
->GetExtension().Cmp(extension
) == 0) &&
1531 (bitmapType
== -1 || handler
->GetType() == bitmapType
) )
1533 node
= node
->GetNext();
1538 wxImageHandler
*wxImage::FindHandler( long bitmapType
)
1540 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
1543 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
1544 if (handler
->GetType() == bitmapType
) return handler
;
1545 node
= node
->GetNext();
1550 wxImageHandler
*wxImage::FindHandlerMime( const wxString
& mimetype
)
1552 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
1555 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
1556 if (handler
->GetMimeType().IsSameAs(mimetype
, false)) return handler
;
1557 node
= node
->GetNext();
1562 void wxImage::InitStandardHandlers()
1565 AddHandler(new wxBMPHandler
);
1566 #endif // wxUSE_STREAMS
1569 void wxImage::CleanUpHandlers()
1571 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
1574 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
1575 wxList::compatibility_iterator next
= node
->GetNext();
1580 sm_handlers
.Clear();
1583 wxString
wxImage::GetImageExtWildcard()
1587 wxList
& Handlers
= wxImage::GetHandlers();
1588 wxList::compatibility_iterator Node
= Handlers
.GetFirst();
1591 wxImageHandler
* Handler
= (wxImageHandler
*)Node
->GetData();
1592 fmts
+= wxT("*.") + Handler
->GetExtension();
1593 Node
= Node
->GetNext();
1594 if ( Node
) fmts
+= wxT(";");
1597 return wxT("(") + fmts
+ wxT(")|") + fmts
;
1600 //-----------------------------------------------------------------------------
1602 //-----------------------------------------------------------------------------
1604 IMPLEMENT_ABSTRACT_CLASS(wxImageHandler
,wxObject
)
1607 bool wxImageHandler::LoadFile( wxImage
*WXUNUSED(image
), wxInputStream
& WXUNUSED(stream
), bool WXUNUSED(verbose
), int WXUNUSED(index
) )
1612 bool wxImageHandler::SaveFile( wxImage
*WXUNUSED(image
), wxOutputStream
& WXUNUSED(stream
), bool WXUNUSED(verbose
) )
1617 int wxImageHandler::GetImageCount( wxInputStream
& WXUNUSED(stream
) )
1622 bool wxImageHandler::CanRead( const wxString
& name
)
1624 if (wxFileExists(name
))
1626 wxFileInputStream
stream(name
);
1627 return CanRead(stream
);
1630 wxLogError( _("Can't check image format of file '%s': file does not exist."), name
.c_str() );
1635 bool wxImageHandler::CallDoCanRead(wxInputStream
& stream
)
1637 wxFileOffset posOld
= stream
.TellI();
1638 if ( posOld
== wxInvalidOffset
)
1640 // can't test unseekable stream
1644 bool ok
= DoCanRead(stream
);
1646 // restore the old position to be able to test other formats and so on
1647 if ( stream
.SeekI(posOld
) == wxInvalidOffset
)
1649 wxLogDebug(_T("Failed to rewind the stream in wxImageHandler!"));
1651 // reading would fail anyhow as we're not at the right position
1658 #endif // wxUSE_STREAMS
1660 // ----------------------------------------------------------------------------
1661 // image histogram stuff
1662 // ----------------------------------------------------------------------------
1665 wxImageHistogram::FindFirstUnusedColour(unsigned char *r
,
1670 unsigned char g2
) const
1672 unsigned long key
= MakeKey(r2
, g2
, b2
);
1674 while ( find(key
) != end() )
1676 // color already used
1688 wxLogError(_("No unused colour in image.") );
1694 key
= MakeKey(r2
, g2
, b2
);
1708 wxImage::FindFirstUnusedColour(unsigned char *r
,
1713 unsigned char g2
) const
1715 wxImageHistogram histogram
;
1717 ComputeHistogram(histogram
);
1719 return histogram
.FindFirstUnusedColour(r
, g
, b
, r2
, g2
, b2
);
1725 // Counts and returns the number of different colours. Optionally stops
1726 // when it exceeds 'stopafter' different colours. This is useful, for
1727 // example, to see if the image can be saved as 8-bit (256 colour or
1728 // less, in this case it would be invoked as CountColours(256)). Default
1729 // value for stopafter is -1 (don't care).
1731 unsigned long wxImage::CountColours( unsigned long stopafter
) const
1735 unsigned char r
, g
, b
;
1737 unsigned long size
, nentries
, key
;
1740 size
= GetWidth() * GetHeight();
1743 for (unsigned long j
= 0; (j
< size
) && (nentries
<= stopafter
) ; j
++)
1748 key
= wxImageHistogram::MakeKey(r
, g
, b
);
1750 if (h
.Get(key
) == NULL
)
1761 unsigned long wxImage::ComputeHistogram( wxImageHistogram
&h
) const
1763 unsigned char *p
= GetData();
1764 unsigned long nentries
= 0;
1768 const unsigned long size
= GetWidth() * GetHeight();
1770 unsigned char r
, g
, b
;
1771 for ( unsigned long n
= 0; n
< size
; n
++ )
1777 wxImageHistogramEntry
& entry
= h
[wxImageHistogram::MakeKey(r
, g
, b
)];
1779 if ( entry
.value
++ == 0 )
1780 entry
.index
= nentries
++;
1787 * Rotation code by Carlos Moreno
1790 // GRG: I've removed wxRotationPoint - we already have wxRealPoint which
1791 // does exactly the same thing. And I also got rid of wxRotationPixel
1792 // bacause of potential problems in architectures where alignment
1793 // is an issue, so I had to rewrite parts of the code.
1795 static const double gs_Epsilon
= 1e-10;
1797 static inline int wxCint (double x
)
1799 return (x
> 0) ? (int) (x
+ 0.5) : (int) (x
- 0.5);
1803 // Auxiliary function to rotate a point (x,y) with respect to point p0
1804 // make it inline and use a straight return to facilitate optimization
1805 // also, the function receives the sine and cosine of the angle to avoid
1806 // repeating the time-consuming calls to these functions -- sin/cos can
1807 // be computed and stored in the calling function.
1809 inline wxRealPoint
rotated_point (const wxRealPoint
& p
, double cos_angle
, double sin_angle
, const wxRealPoint
& p0
)
1811 return wxRealPoint (p0
.x
+ (p
.x
- p0
.x
) * cos_angle
- (p
.y
- p0
.y
) * sin_angle
,
1812 p0
.y
+ (p
.y
- p0
.y
) * cos_angle
+ (p
.x
- p0
.x
) * sin_angle
);
1815 inline wxRealPoint
rotated_point (double x
, double y
, double cos_angle
, double sin_angle
, const wxRealPoint
& p0
)
1817 return rotated_point (wxRealPoint(x
,y
), cos_angle
, sin_angle
, p0
);
1820 wxImage
wxImage::Rotate(double angle
, const wxPoint
& centre_of_rotation
, bool interpolating
, wxPoint
* offset_after_rotation
) const
1823 angle
= -angle
; // screen coordinates are a mirror image of "real" coordinates
1825 bool has_alpha
= HasAlpha();
1827 // Create pointer-based array to accelerate access to wxImage's data
1828 unsigned char ** data
= new unsigned char * [GetHeight()];
1829 data
[0] = GetData();
1830 for (i
= 1; i
< GetHeight(); i
++)
1831 data
[i
] = data
[i
- 1] + (3 * GetWidth());
1833 // Same for alpha channel
1834 unsigned char ** alpha
= NULL
;
1837 alpha
= new unsigned char * [GetHeight()];
1838 alpha
[0] = GetAlpha();
1839 for (i
= 1; i
< GetHeight(); i
++)
1840 alpha
[i
] = alpha
[i
- 1] + GetWidth();
1843 // precompute coefficients for rotation formula
1844 // (sine and cosine of the angle)
1845 const double cos_angle
= cos(angle
);
1846 const double sin_angle
= sin(angle
);
1848 // Create new Image to store the result
1849 // First, find rectangle that covers the rotated image; to do that,
1850 // rotate the four corners
1852 const wxRealPoint
p0(centre_of_rotation
.x
, centre_of_rotation
.y
);
1854 wxRealPoint p1
= rotated_point (0, 0, cos_angle
, sin_angle
, p0
);
1855 wxRealPoint p2
= rotated_point (0, GetHeight(), cos_angle
, sin_angle
, p0
);
1856 wxRealPoint p3
= rotated_point (GetWidth(), 0, cos_angle
, sin_angle
, p0
);
1857 wxRealPoint p4
= rotated_point (GetWidth(), GetHeight(), cos_angle
, sin_angle
, p0
);
1859 int x1
= (int) floor (wxMin (wxMin(p1
.x
, p2
.x
), wxMin(p3
.x
, p4
.x
)));
1860 int y1
= (int) floor (wxMin (wxMin(p1
.y
, p2
.y
), wxMin(p3
.y
, p4
.y
)));
1861 int x2
= (int) ceil (wxMax (wxMax(p1
.x
, p2
.x
), wxMax(p3
.x
, p4
.x
)));
1862 int y2
= (int) ceil (wxMax (wxMax(p1
.y
, p2
.y
), wxMax(p3
.y
, p4
.y
)));
1864 // Create rotated image
1865 wxImage
rotated (x2
- x1
+ 1, y2
- y1
+ 1, false);
1866 // With alpha channel
1870 if (offset_after_rotation
!= NULL
)
1872 *offset_after_rotation
= wxPoint (x1
, y1
);
1875 // GRG: The rotated (destination) image is always accessed
1876 // sequentially, so there is no need for a pointer-based
1877 // array here (and in fact it would be slower).
1879 unsigned char * dst
= rotated
.GetData();
1881 unsigned char * alpha_dst
= NULL
;
1883 alpha_dst
= rotated
.GetAlpha();
1885 // GRG: if the original image has a mask, use its RGB values
1886 // as the blank pixel, else, fall back to default (black).
1888 unsigned char blank_r
= 0;
1889 unsigned char blank_g
= 0;
1890 unsigned char blank_b
= 0;
1894 blank_r
= GetMaskRed();
1895 blank_g
= GetMaskGreen();
1896 blank_b
= GetMaskBlue();
1897 rotated
.SetMaskColour( blank_r
, blank_g
, blank_b
);
1900 // Now, for each point of the rotated image, find where it came from, by
1901 // performing an inverse rotation (a rotation of -angle) and getting the
1902 // pixel at those coordinates
1904 // GRG: I've taken the (interpolating) test out of the loops, so that
1905 // it is done only once, instead of repeating it for each pixel.
1910 for (int y
= 0; y
< rotated
.GetHeight(); y
++)
1912 for (x
= 0; x
< rotated
.GetWidth(); x
++)
1914 wxRealPoint src
= rotated_point (x
+ x1
, y
+ y1
, cos_angle
, -sin_angle
, p0
);
1916 if (-0.25 < src
.x
&& src
.x
< GetWidth() - 0.75 &&
1917 -0.25 < src
.y
&& src
.y
< GetHeight() - 0.75)
1919 // interpolate using the 4 enclosing grid-points. Those
1920 // points can be obtained using floor and ceiling of the
1921 // exact coordinates of the point
1922 // C.M. 2000-02-17: when the point is near the border, special care is required.
1926 if (0 < src
.x
&& src
.x
< GetWidth() - 1)
1928 x1
= wxCint(floor(src
.x
));
1929 x2
= wxCint(ceil(src
.x
));
1931 else // else means that x is near one of the borders (0 or width-1)
1933 x1
= x2
= wxCint (src
.x
);
1936 if (0 < src
.y
&& src
.y
< GetHeight() - 1)
1938 y1
= wxCint(floor(src
.y
));
1939 y2
= wxCint(ceil(src
.y
));
1943 y1
= y2
= wxCint (src
.y
);
1946 // get four points and the distances (square of the distance,
1947 // for efficiency reasons) for the interpolation formula
1949 // GRG: Do not calculate the points until they are
1950 // really needed -- this way we can calculate
1951 // just one, instead of four, if d1, d2, d3
1952 // or d4 are < gs_Epsilon
1954 const double d1
= (src
.x
- x1
) * (src
.x
- x1
) + (src
.y
- y1
) * (src
.y
- y1
);
1955 const double d2
= (src
.x
- x2
) * (src
.x
- x2
) + (src
.y
- y1
) * (src
.y
- y1
);
1956 const double d3
= (src
.x
- x2
) * (src
.x
- x2
) + (src
.y
- y2
) * (src
.y
- y2
);
1957 const double d4
= (src
.x
- x1
) * (src
.x
- x1
) + (src
.y
- y2
) * (src
.y
- y2
);
1959 // Now interpolate as a weighted average of the four surrounding
1960 // points, where the weights are the distances to each of those points
1962 // If the point is exactly at one point of the grid of the source
1963 // image, then don't interpolate -- just assign the pixel
1965 if (d1
< gs_Epsilon
) // d1,d2,d3,d4 are positive -- no need for abs()
1967 unsigned char *p
= data
[y1
] + (3 * x1
);
1974 unsigned char *p
= alpha
[y1
] + x1
;
1975 *(alpha_dst
++) = *p
;
1978 else if (d2
< gs_Epsilon
)
1980 unsigned char *p
= data
[y1
] + (3 * x2
);
1987 unsigned char *p
= alpha
[y1
] + x2
;
1988 *(alpha_dst
++) = *p
;
1991 else if (d3
< gs_Epsilon
)
1993 unsigned char *p
= data
[y2
] + (3 * x2
);
2000 unsigned char *p
= alpha
[y2
] + x2
;
2001 *(alpha_dst
++) = *p
;
2004 else if (d4
< gs_Epsilon
)
2006 unsigned char *p
= data
[y2
] + (3 * x1
);
2013 unsigned char *p
= alpha
[y2
] + x1
;
2014 *(alpha_dst
++) = *p
;
2019 // weights for the weighted average are proportional to the inverse of the distance
2020 unsigned char *v1
= data
[y1
] + (3 * x1
);
2021 unsigned char *v2
= data
[y1
] + (3 * x2
);
2022 unsigned char *v3
= data
[y2
] + (3 * x2
);
2023 unsigned char *v4
= data
[y2
] + (3 * x1
);
2025 const double w1
= 1/d1
, w2
= 1/d2
, w3
= 1/d3
, w4
= 1/d4
;
2029 *(dst
++) = (unsigned char)
2030 ( (w1
* *(v1
++) + w2
* *(v2
++) +
2031 w3
* *(v3
++) + w4
* *(v4
++)) /
2032 (w1
+ w2
+ w3
+ w4
) );
2033 *(dst
++) = (unsigned char)
2034 ( (w1
* *(v1
++) + w2
* *(v2
++) +
2035 w3
* *(v3
++) + w4
* *(v4
++)) /
2036 (w1
+ w2
+ w3
+ w4
) );
2037 *(dst
++) = (unsigned char)
2038 ( (w1
* *v1
+ w2
* *v2
+
2039 w3
* *v3
+ w4
* *v4
) /
2040 (w1
+ w2
+ w3
+ w4
) );
2044 unsigned char *v1
= alpha
[y1
] + (x1
);
2045 unsigned char *v2
= alpha
[y1
] + (x2
);
2046 unsigned char *v3
= alpha
[y2
] + (x2
);
2047 unsigned char *v4
= alpha
[y2
] + (x1
);
2049 *(alpha_dst
++) = (unsigned char)
2050 ( (w1
* *v1
+ w2
* *v2
+
2051 w3
* *v3
+ w4
* *v4
) /
2052 (w1
+ w2
+ w3
+ w4
) );
2068 else // not interpolating
2070 for (int y
= 0; y
< rotated
.GetHeight(); y
++)
2072 for (x
= 0; x
< rotated
.GetWidth(); x
++)
2074 wxRealPoint src
= rotated_point (x
+ x1
, y
+ y1
, cos_angle
, -sin_angle
, p0
);
2076 const int xs
= wxCint (src
.x
); // wxCint rounds to the
2077 const int ys
= wxCint (src
.y
); // closest integer
2079 if (0 <= xs
&& xs
< GetWidth() &&
2080 0 <= ys
&& ys
< GetHeight())
2082 unsigned char *p
= data
[ys
] + (3 * xs
);
2089 unsigned char *p
= alpha
[ys
] + (xs
);
2090 *(alpha_dst
++) = *p
;
2100 *(alpha_dst
++) = 255;
2118 // A module to allow wxImage initialization/cleanup
2119 // without calling these functions from app.cpp or from
2120 // the user's application.
2122 class wxImageModule
: public wxModule
2124 DECLARE_DYNAMIC_CLASS(wxImageModule
)
2127 bool OnInit() { wxImage::InitStandardHandlers(); return true; };
2128 void OnExit() { wxImage::CleanUpHandlers(); };
2131 IMPLEMENT_DYNAMIC_CLASS(wxImageModule
, wxModule
)
2134 #endif // wxUSE_IMAGE