1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Robert Roebling
6 // Copyright: (c) Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
11 #pragma implementation "image.h"
14 // For compilers that support precompilation, includes "wx.h".
15 #include "wx/wxprec.h"
26 #include "wx/bitmap.h"
30 #include "wx/filefn.h"
31 #include "wx/wfstream.h"
33 #include "wx/module.h"
46 //-----------------------------------------------------------------------------
48 //-----------------------------------------------------------------------------
50 class wxImageRefData
: public wxObjectRefData
54 virtual ~wxImageRefData();
58 unsigned char *m_data
;
61 unsigned char m_maskRed
,m_maskGreen
,m_maskBlue
;
63 // alpha channel data, may be NULL for the formats without alpha support
64 unsigned char *m_alpha
;
71 #endif // wxUSE_PALETTE
73 wxArrayString m_optionNames
;
74 wxArrayString m_optionValues
;
76 DECLARE_NO_COPY_CLASS(wxImageRefData
)
79 wxImageRefData::wxImageRefData()
84 m_alpha
= (unsigned char *) NULL
;
95 wxImageRefData::~wxImageRefData()
103 wxList
wxImage::sm_handlers
;
107 //-----------------------------------------------------------------------------
109 #define M_IMGDATA ((wxImageRefData *)m_refData)
111 IMPLEMENT_DYNAMIC_CLASS(wxImage
, wxObject
)
117 wxImage::wxImage( int width
, int height
, bool clear
)
119 Create( width
, height
, clear
);
122 wxImage::wxImage( int width
, int height
, unsigned char* data
, bool static_data
)
124 Create( width
, height
, data
, static_data
);
127 wxImage::wxImage( const wxString
& name
, long type
, int index
)
129 LoadFile( name
, type
, index
);
132 wxImage::wxImage( const wxString
& name
, const wxString
& mimetype
, int index
)
134 LoadFile( name
, mimetype
, index
);
138 wxImage::wxImage( wxInputStream
& stream
, long type
, int index
)
140 LoadFile( stream
, type
, index
);
143 wxImage::wxImage( wxInputStream
& stream
, const wxString
& mimetype
, int index
)
145 LoadFile( stream
, mimetype
, index
);
147 #endif // wxUSE_STREAMS
149 wxImage::wxImage( const wxImage
& image
)
155 wxImage::wxImage( const wxImage
* image
)
157 if (image
) Ref(*image
);
160 bool wxImage::Create( int width
, int height
, bool clear
)
164 m_refData
= new wxImageRefData();
166 M_IMGDATA
->m_data
= (unsigned char *) malloc( width
*height
*3 );
167 if (!M_IMGDATA
->m_data
)
174 memset(M_IMGDATA
->m_data
, 0, width
*height
*3);
176 M_IMGDATA
->m_width
= width
;
177 M_IMGDATA
->m_height
= height
;
178 M_IMGDATA
->m_ok
= true;
183 bool wxImage::Create( int width
, int height
, unsigned char* data
, bool static_data
)
187 wxCHECK_MSG( data
, false, _T("NULL data in wxImage::Create") );
189 m_refData
= new wxImageRefData();
191 M_IMGDATA
->m_data
= data
;
192 M_IMGDATA
->m_width
= width
;
193 M_IMGDATA
->m_height
= height
;
194 M_IMGDATA
->m_ok
= true;
195 M_IMGDATA
->m_static
= static_data
;
200 void wxImage::Destroy()
205 wxImage
wxImage::Copy() const
209 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
211 image
.Create( M_IMGDATA
->m_width
, M_IMGDATA
->m_height
, false );
213 unsigned char *data
= image
.GetData();
215 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
217 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
218 image
.SetMask( M_IMGDATA
->m_hasMask
);
220 memcpy( data
, GetData(), M_IMGDATA
->m_width
*M_IMGDATA
->m_height
*3 );
222 // also copy the image options
223 wxImageRefData
*imgData
= (wxImageRefData
*)image
.m_refData
;
224 imgData
->m_optionNames
= M_IMGDATA
->m_optionNames
;
225 imgData
->m_optionValues
= M_IMGDATA
->m_optionValues
;
230 wxImage
wxImage::ShrinkBy( int xFactor
, int yFactor
) const
232 if( xFactor
== 1 && yFactor
== 1 )
237 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
239 // can't scale to/from 0 size
240 wxCHECK_MSG( (xFactor
> 0) && (yFactor
> 0), image
,
241 wxT("invalid new image size") );
243 long old_height
= M_IMGDATA
->m_height
,
244 old_width
= M_IMGDATA
->m_width
;
246 wxCHECK_MSG( (old_height
> 0) && (old_width
> 0), image
,
247 wxT("invalid old image size") );
249 long width
= old_width
/ xFactor
;
250 long height
= old_height
/ yFactor
;
252 image
.Create( width
, height
, false );
254 char unsigned *data
= image
.GetData();
256 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
258 bool hasMask
= false ;
259 unsigned char maskRed
= 0;
260 unsigned char maskGreen
= 0;
261 unsigned char maskBlue
=0 ;
262 if (M_IMGDATA
->m_hasMask
)
265 maskRed
= M_IMGDATA
->m_maskRed
;
266 maskGreen
= M_IMGDATA
->m_maskGreen
;
267 maskBlue
=M_IMGDATA
->m_maskBlue
;
269 image
.SetMaskColour( M_IMGDATA
->m_maskRed
,
270 M_IMGDATA
->m_maskGreen
,
271 M_IMGDATA
->m_maskBlue
);
273 char unsigned *source_data
= M_IMGDATA
->m_data
;
274 char unsigned *target_data
= data
;
276 for (long y
= 0; y
< height
; y
++)
278 for (long x
= 0; x
< width
; x
++)
280 unsigned long avgRed
= 0 ;
281 unsigned long avgGreen
= 0;
282 unsigned long avgBlue
= 0;
283 unsigned long counter
= 0 ;
285 for ( int y1
= 0 ; y1
< yFactor
; ++y1
)
287 long y_offset
= (y
* yFactor
+ y1
) * old_width
;
288 for ( int x1
= 0 ; x1
< xFactor
; ++x1
)
290 unsigned char *pixel
= source_data
+ 3 * ( y_offset
+ x
* xFactor
+ x1
) ;
291 unsigned char red
= pixel
[0] ;
292 unsigned char green
= pixel
[1] ;
293 unsigned char blue
= pixel
[2] ;
294 if ( !hasMask
|| red
!= maskRed
|| green
!= maskGreen
|| blue
!= maskBlue
)
305 *(target_data
++) = M_IMGDATA
->m_maskRed
;
306 *(target_data
++) = M_IMGDATA
->m_maskGreen
;
307 *(target_data
++) = M_IMGDATA
->m_maskBlue
;
311 *(target_data
++) = (unsigned char)(avgRed
/ counter
);
312 *(target_data
++) = (unsigned char)(avgGreen
/ counter
);
313 *(target_data
++) = (unsigned char)(avgBlue
/ counter
);
318 // In case this is a cursor, make sure the hotspot is scalled accordingly:
319 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
) )
320 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
,
321 (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X
))/xFactor
);
322 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
) )
323 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
,
324 (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y
))/yFactor
);
329 wxImage
wxImage::Scale( int width
, int height
) const
333 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
335 // can't scale to/from 0 size
336 wxCHECK_MSG( (width
> 0) && (height
> 0), image
,
337 wxT("invalid new image size") );
339 long old_height
= M_IMGDATA
->m_height
,
340 old_width
= M_IMGDATA
->m_width
;
341 wxCHECK_MSG( (old_height
> 0) && (old_width
> 0), image
,
342 wxT("invalid old image size") );
344 if ( old_width
% width
== 0 && old_width
>= width
&&
345 old_height
% height
== 0 && old_height
>= height
)
347 return ShrinkBy( old_width
/ width
, old_height
/ height
) ;
349 image
.Create( width
, height
, false );
351 unsigned char *data
= image
.GetData();
353 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
355 if (M_IMGDATA
->m_hasMask
)
357 image
.SetMaskColour( M_IMGDATA
->m_maskRed
,
358 M_IMGDATA
->m_maskGreen
,
359 M_IMGDATA
->m_maskBlue
);
362 unsigned char *source_data
= M_IMGDATA
->m_data
;
363 unsigned char *target_data
= data
;
365 long x_delta
= (old_width
<<16) / width
;
366 long y_delta
= (old_height
<<16) / height
;
368 unsigned char* dest_pixel
= target_data
;
371 for ( long j
= 0; j
< height
; j
++ )
373 unsigned char* src_line
= &source_data
[(y
>>16)*old_width
*3];
376 for ( long i
= 0; i
< width
; i
++ )
378 unsigned char* src_pixel
= &src_line
[(x
>>16)*3];
379 dest_pixel
[0] = src_pixel
[0];
380 dest_pixel
[1] = src_pixel
[1];
381 dest_pixel
[2] = src_pixel
[2];
389 // In case this is a cursor, make sure the hotspot is scalled accordingly:
390 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
) )
391 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
,
392 (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X
)*width
)/old_width
);
393 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
) )
394 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
,
395 (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y
)*height
)/old_height
);
400 wxImage
wxImage::Rotate90( bool clockwise
) const
404 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
406 image
.Create( M_IMGDATA
->m_height
, M_IMGDATA
->m_width
, false );
408 unsigned char *data
= image
.GetData();
410 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
412 if (M_IMGDATA
->m_hasMask
)
413 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
415 long height
= M_IMGDATA
->m_height
;
416 long width
= M_IMGDATA
->m_width
;
418 unsigned char *source_data
= M_IMGDATA
->m_data
;
419 unsigned char *target_data
;
421 for (long j
= 0; j
< height
; j
++)
423 for (long i
= 0; i
< width
; i
++)
426 target_data
= data
+ (((i
+1)*height
) - j
- 1)*3;
428 target_data
= data
+ ((height
*(width
-1)) + j
- (i
*height
))*3;
429 memcpy( target_data
, source_data
, 3 );
437 wxImage
wxImage::Mirror( bool horizontally
) const
441 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
443 image
.Create( M_IMGDATA
->m_width
, M_IMGDATA
->m_height
, false );
445 unsigned char *data
= image
.GetData();
447 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
449 if (M_IMGDATA
->m_hasMask
)
450 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
452 long height
= M_IMGDATA
->m_height
;
453 long width
= M_IMGDATA
->m_width
;
455 unsigned char *source_data
= M_IMGDATA
->m_data
;
456 unsigned char *target_data
;
460 for (long j
= 0; j
< height
; j
++)
463 target_data
= data
-3;
464 for (long i
= 0; i
< width
; i
++)
466 memcpy( target_data
, source_data
, 3 );
474 for (long i
= 0; i
< height
; i
++)
476 target_data
= data
+ 3*width
*(height
-1-i
);
477 memcpy( target_data
, source_data
, (size_t)3*width
);
478 source_data
+= 3*width
;
485 wxImage
wxImage::GetSubImage( const wxRect
&rect
) const
489 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
491 wxCHECK_MSG( (rect
.GetLeft()>=0) && (rect
.GetTop()>=0) && (rect
.GetRight()<=GetWidth()) && (rect
.GetBottom()<=GetHeight()),
492 image
, wxT("invalid subimage size") );
494 int subwidth
=rect
.GetWidth();
495 const int subheight
=rect
.GetHeight();
497 image
.Create( subwidth
, subheight
, false );
499 unsigned char *subdata
= image
.GetData(), *data
=GetData();
501 wxCHECK_MSG( subdata
, image
, wxT("unable to create image") );
503 if (M_IMGDATA
->m_hasMask
)
504 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
506 const int subleft
=3*rect
.GetLeft();
507 const int width
=3*GetWidth();
510 data
+=rect
.GetTop()*width
+subleft
;
512 for (long j
= 0; j
< subheight
; ++j
)
514 memcpy( subdata
, data
, subwidth
);
522 void wxImage::Paste( const wxImage
&image
, int x
, int y
)
524 wxCHECK_RET( Ok(), wxT("invalid image") );
525 wxCHECK_RET( image
.Ok(), wxT("invalid image") );
529 int width
= image
.GetWidth();
530 int height
= image
.GetHeight();
543 if ((x
+xx
)+width
> M_IMGDATA
->m_width
)
544 width
= M_IMGDATA
->m_width
- (x
+xx
);
545 if ((y
+yy
)+height
> M_IMGDATA
->m_height
)
546 height
= M_IMGDATA
->m_height
- (y
+yy
);
548 if (width
< 1) return;
549 if (height
< 1) return;
551 if ((!HasMask() && !image
.HasMask()) ||
552 ((HasMask() && image
.HasMask() &&
553 (GetMaskRed()==image
.GetMaskRed()) &&
554 (GetMaskGreen()==image
.GetMaskGreen()) &&
555 (GetMaskBlue()==image
.GetMaskBlue()))))
558 unsigned char* source_data
= image
.GetData() + xx
*3 + yy
*3*image
.GetWidth();
559 int source_step
= image
.GetWidth()*3;
561 unsigned char* target_data
= GetData() + (x
+xx
)*3 + (y
+yy
)*3*M_IMGDATA
->m_width
;
562 int target_step
= M_IMGDATA
->m_width
*3;
563 for (int j
= 0; j
< height
; j
++)
565 memcpy( target_data
, source_data
, width
);
566 source_data
+= source_step
;
567 target_data
+= target_step
;
572 if (!HasMask() && image
.HasMask())
574 unsigned char r
= image
.GetMaskRed();
575 unsigned char g
= image
.GetMaskGreen();
576 unsigned char b
= image
.GetMaskBlue();
579 unsigned char* source_data
= image
.GetData() + xx
*3 + yy
*3*image
.GetWidth();
580 int source_step
= image
.GetWidth()*3;
582 unsigned char* target_data
= GetData() + (x
+xx
)*3 + (y
+yy
)*3*M_IMGDATA
->m_width
;
583 int target_step
= M_IMGDATA
->m_width
*3;
585 for (int j
= 0; j
< height
; j
++)
587 for (int i
= 0; i
< width
; i
+=3)
589 if ((source_data
[i
] != r
) &&
590 (source_data
[i
+1] != g
) &&
591 (source_data
[i
+2] != b
))
593 memcpy( target_data
+i
, source_data
+i
, 3 );
596 source_data
+= source_step
;
597 target_data
+= target_step
;
602 void wxImage::Replace( unsigned char r1
, unsigned char g1
, unsigned char b1
,
603 unsigned char r2
, unsigned char g2
, unsigned char b2
)
605 wxCHECK_RET( Ok(), wxT("invalid image") );
607 unsigned char *data
= GetData();
609 const int w
= GetWidth();
610 const int h
= GetHeight();
612 for (int j
= 0; j
< h
; j
++)
613 for (int i
= 0; i
< w
; i
++)
615 if ((data
[0] == r1
) && (data
[1] == g1
) && (data
[2] == b1
))
625 wxImage
wxImage::ConvertToMono( unsigned char r
, unsigned char g
, unsigned char b
) const
629 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
631 image
.Create( M_IMGDATA
->m_width
, M_IMGDATA
->m_height
, false );
633 unsigned char *data
= image
.GetData();
635 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
637 if (M_IMGDATA
->m_hasMask
)
639 if (M_IMGDATA
->m_maskRed
== r
&& M_IMGDATA
->m_maskGreen
== g
&&
640 M_IMGDATA
->m_maskBlue
== b
)
641 image
.SetMaskColour( 255, 255, 255 );
643 image
.SetMaskColour( 0, 0, 0 );
646 long size
= M_IMGDATA
->m_height
* M_IMGDATA
->m_width
;
648 unsigned char *srcd
= M_IMGDATA
->m_data
;
649 unsigned char *tard
= image
.GetData();
651 for ( long i
= 0; i
< size
; i
++, srcd
+= 3, tard
+= 3 )
653 if (srcd
[0] == r
&& srcd
[1] == g
&& srcd
[2] == b
)
654 tard
[0] = tard
[1] = tard
[2] = 255;
656 tard
[0] = tard
[1] = tard
[2] = 0;
662 void wxImage::SetRGB( int x
, int y
, unsigned char r
, unsigned char g
, unsigned char b
)
664 wxCHECK_RET( Ok(), wxT("invalid image") );
666 int w
= M_IMGDATA
->m_width
;
667 int h
= M_IMGDATA
->m_height
;
669 wxCHECK_RET( (x
>=0) && (y
>=0) && (x
<w
) && (y
<h
), wxT("invalid image index") );
671 long pos
= (y
* w
+ x
) * 3;
673 M_IMGDATA
->m_data
[ pos
] = r
;
674 M_IMGDATA
->m_data
[ pos
+1 ] = g
;
675 M_IMGDATA
->m_data
[ pos
+2 ] = b
;
678 unsigned char wxImage::GetRed( int x
, int y
) const
680 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
682 int w
= M_IMGDATA
->m_width
;
683 int h
= M_IMGDATA
->m_height
;
685 wxCHECK_MSG( (x
>=0) && (y
>=0) && (x
<w
) && (y
<h
), 0, wxT("invalid image index") );
687 long pos
= (y
* w
+ x
) * 3;
689 return M_IMGDATA
->m_data
[pos
];
692 unsigned char wxImage::GetGreen( int x
, int y
) const
694 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
696 int w
= M_IMGDATA
->m_width
;
697 int h
= M_IMGDATA
->m_height
;
699 wxCHECK_MSG( (x
>=0) && (y
>=0) && (x
<w
) && (y
<h
), 0, wxT("invalid image index") );
701 long pos
= (y
* w
+ x
) * 3;
703 return M_IMGDATA
->m_data
[pos
+1];
706 unsigned char wxImage::GetBlue( int x
, int y
) const
708 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
710 int w
= M_IMGDATA
->m_width
;
711 int h
= M_IMGDATA
->m_height
;
713 wxCHECK_MSG( (x
>=0) && (y
>=0) && (x
<w
) && (y
<h
), 0, wxT("invalid image index") );
715 long pos
= (y
* w
+ x
) * 3;
717 return M_IMGDATA
->m_data
[pos
+2];
720 bool wxImage::Ok() const
722 // image of 0 width or height can't be considered ok - at least because it
723 // causes crashes in ConvertToBitmap() if we don't catch it in time
724 wxImageRefData
*data
= M_IMGDATA
;
725 return data
&& data
->m_ok
&& data
->m_width
&& data
->m_height
;
728 unsigned char *wxImage::GetData() const
730 wxCHECK_MSG( Ok(), (unsigned char *)NULL
, wxT("invalid image") );
732 return M_IMGDATA
->m_data
;
735 void wxImage::SetData( unsigned char *data
)
737 wxCHECK_RET( Ok(), wxT("invalid image") );
739 wxImageRefData
*newRefData
= new wxImageRefData();
741 newRefData
->m_width
= M_IMGDATA
->m_width
;
742 newRefData
->m_height
= M_IMGDATA
->m_height
;
743 newRefData
->m_data
= data
;
744 newRefData
->m_ok
= true;
745 newRefData
->m_maskRed
= M_IMGDATA
->m_maskRed
;
746 newRefData
->m_maskGreen
= M_IMGDATA
->m_maskGreen
;
747 newRefData
->m_maskBlue
= M_IMGDATA
->m_maskBlue
;
748 newRefData
->m_hasMask
= M_IMGDATA
->m_hasMask
;
752 m_refData
= newRefData
;
755 void wxImage::SetData( unsigned char *data
, int new_width
, int new_height
)
757 wxImageRefData
*newRefData
= new wxImageRefData();
761 newRefData
->m_width
= new_width
;
762 newRefData
->m_height
= new_height
;
763 newRefData
->m_data
= data
;
764 newRefData
->m_ok
= true;
765 newRefData
->m_maskRed
= M_IMGDATA
->m_maskRed
;
766 newRefData
->m_maskGreen
= M_IMGDATA
->m_maskGreen
;
767 newRefData
->m_maskBlue
= M_IMGDATA
->m_maskBlue
;
768 newRefData
->m_hasMask
= M_IMGDATA
->m_hasMask
;
772 newRefData
->m_width
= new_width
;
773 newRefData
->m_height
= new_height
;
774 newRefData
->m_data
= data
;
775 newRefData
->m_ok
= true;
780 m_refData
= newRefData
;
783 // ----------------------------------------------------------------------------
784 // alpha channel support
785 // ----------------------------------------------------------------------------
787 void wxImage::SetAlpha(int x
, int y
, unsigned char alpha
)
789 wxCHECK_RET( Ok() && HasAlpha(), wxT("invalid image or no alpha channel") );
791 int w
= M_IMGDATA
->m_width
,
792 h
= M_IMGDATA
->m_height
;
794 wxCHECK_RET( x
>=0 && y
>= 0 && x
< w
&& y
< h
, wxT("invalid image index") );
796 M_IMGDATA
->m_alpha
[y
*w
+ x
] = alpha
;
799 unsigned char wxImage::GetAlpha(int x
, int y
) const
801 wxCHECK_MSG( Ok() && HasAlpha(), 0, wxT("invalid image or no alpha channel") );
803 int w
= M_IMGDATA
->m_width
,
804 h
= M_IMGDATA
->m_height
;
806 wxCHECK_MSG( x
>=0 && y
>= 0 && x
< w
&& y
< h
, 0, wxT("invalid image index") );
808 return M_IMGDATA
->m_alpha
[y
*w
+ x
];
811 void wxImage::SetAlpha( unsigned char *alpha
)
813 wxCHECK_RET( Ok(), wxT("invalid image") );
817 alpha
= (unsigned char *)malloc(M_IMGDATA
->m_width
*M_IMGDATA
->m_height
);
820 free(M_IMGDATA
->m_alpha
);
821 M_IMGDATA
->m_alpha
= alpha
;
824 unsigned char *wxImage::GetAlpha() const
826 wxCHECK_MSG( Ok(), (unsigned char *)NULL
, wxT("invalid image") );
828 return M_IMGDATA
->m_alpha
;
831 // ----------------------------------------------------------------------------
833 // ----------------------------------------------------------------------------
835 void wxImage::SetMaskColour( unsigned char r
, unsigned char g
, unsigned char b
)
837 wxCHECK_RET( Ok(), wxT("invalid image") );
839 M_IMGDATA
->m_maskRed
= r
;
840 M_IMGDATA
->m_maskGreen
= g
;
841 M_IMGDATA
->m_maskBlue
= b
;
842 M_IMGDATA
->m_hasMask
= true;
845 unsigned char wxImage::GetMaskRed() const
847 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
849 return M_IMGDATA
->m_maskRed
;
852 unsigned char wxImage::GetMaskGreen() const
854 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
856 return M_IMGDATA
->m_maskGreen
;
859 unsigned char wxImage::GetMaskBlue() const
861 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
863 return M_IMGDATA
->m_maskBlue
;
866 void wxImage::SetMask( bool mask
)
868 wxCHECK_RET( Ok(), wxT("invalid image") );
870 M_IMGDATA
->m_hasMask
= mask
;
873 bool wxImage::HasMask() const
875 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
877 return M_IMGDATA
->m_hasMask
;
880 int wxImage::GetWidth() const
882 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
884 return M_IMGDATA
->m_width
;
887 int wxImage::GetHeight() const
889 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
891 return M_IMGDATA
->m_height
;
894 bool wxImage::SetMaskFromImage(const wxImage
& mask
,
895 unsigned char mr
, unsigned char mg
, unsigned char mb
)
897 // check that the images are the same size
898 if ( (M_IMGDATA
->m_height
!= mask
.GetHeight() ) || (M_IMGDATA
->m_width
!= mask
.GetWidth () ) )
900 wxLogError( _("Image and mask have different sizes.") );
904 // find unused colour
905 unsigned char r
,g
,b
;
906 if (!FindFirstUnusedColour(&r
, &g
, &b
))
908 wxLogError( _("No unused colour in image being masked.") );
912 unsigned char *imgdata
= GetData();
913 unsigned char *maskdata
= mask
.GetData();
915 const int w
= GetWidth();
916 const int h
= GetHeight();
918 for (int j
= 0; j
< h
; j
++)
920 for (int i
= 0; i
< w
; i
++)
922 if ((maskdata
[0] == mr
) && (maskdata
[1] == mg
) && (maskdata
[2] == mb
))
933 SetMaskColour(r
, g
, b
);
939 bool wxImage::ConvertAlphaToMask(unsigned threshold
)
944 unsigned char mr
, mg
, mb
;
945 if (!FindFirstUnusedColour(&mr
, &mg
, &mb
))
947 wxLogError( _("No unused colour in image being masked.") );
952 SetMaskColour(mr
, mg
, mb
);
954 unsigned char *imgdata
= GetData();
955 unsigned char *alphadata
= GetAlpha();
957 size_t w
= GetWidth();
958 size_t h
= GetHeight();
960 for (size_t y
= 0; y
< h
; y
++)
962 for (size_t x
= 0; x
< w
; x
++, imgdata
+= 3, alphadata
++)
964 if (*alphadata
< threshold
)
973 free(M_IMGDATA
->m_alpha
);
974 M_IMGDATA
->m_alpha
= NULL
;
983 bool wxImage::HasPalette() const
988 return M_IMGDATA
->m_palette
.Ok();
991 const wxPalette
& wxImage::GetPalette() const
993 wxCHECK_MSG( Ok(), wxNullPalette
, wxT("invalid image") );
995 return M_IMGDATA
->m_palette
;
998 void wxImage::SetPalette(const wxPalette
& palette
)
1000 wxCHECK_RET( Ok(), wxT("invalid image") );
1002 M_IMGDATA
->m_palette
= palette
;
1005 #endif // wxUSE_PALETTE
1007 // Option functions (arbitrary name/value mapping)
1008 void wxImage::SetOption(const wxString
& name
, const wxString
& value
)
1010 wxCHECK_RET( Ok(), wxT("invalid image") );
1012 int idx
= M_IMGDATA
->m_optionNames
.Index(name
, false);
1013 if (idx
== wxNOT_FOUND
)
1015 M_IMGDATA
->m_optionNames
.Add(name
);
1016 M_IMGDATA
->m_optionValues
.Add(value
);
1020 M_IMGDATA
->m_optionNames
[idx
] = name
;
1021 M_IMGDATA
->m_optionValues
[idx
] = value
;
1025 void wxImage::SetOption(const wxString
& name
, int value
)
1028 valStr
.Printf(wxT("%d"), value
);
1029 SetOption(name
, valStr
);
1032 wxString
wxImage::GetOption(const wxString
& name
) const
1034 wxCHECK_MSG( Ok(), wxEmptyString
, wxT("invalid image") );
1036 int idx
= M_IMGDATA
->m_optionNames
.Index(name
, false);
1037 if (idx
== wxNOT_FOUND
)
1038 return wxEmptyString
;
1040 return M_IMGDATA
->m_optionValues
[idx
];
1043 int wxImage::GetOptionInt(const wxString
& name
) const
1045 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
1047 return wxAtoi(GetOption(name
));
1050 bool wxImage::HasOption(const wxString
& name
) const
1052 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1054 return (M_IMGDATA
->m_optionNames
.Index(name
, false) != wxNOT_FOUND
);
1057 bool wxImage::LoadFile( const wxString
& filename
, long type
, int index
)
1060 if (wxFileExists(filename
))
1062 wxFileInputStream
stream(filename
);
1063 wxBufferedInputStream
bstream( stream
);
1064 return LoadFile(bstream
, type
, index
);
1068 wxLogError( _("Can't load image from file '%s': file does not exist."), filename
.c_str() );
1072 #else // !wxUSE_STREAMS
1074 #endif // wxUSE_STREAMS
1077 bool wxImage::LoadFile( const wxString
& filename
, const wxString
& mimetype
, int index
)
1080 if (wxFileExists(filename
))
1082 wxFileInputStream
stream(filename
);
1083 wxBufferedInputStream
bstream( stream
);
1084 return LoadFile(bstream
, mimetype
, index
);
1088 wxLogError( _("Can't load image from file '%s': file does not exist."), filename
.c_str() );
1092 #else // !wxUSE_STREAMS
1094 #endif // wxUSE_STREAMS
1099 bool wxImage::SaveFile( const wxString
& filename
) const
1101 wxString ext
= filename
.AfterLast('.').Lower();
1103 wxImageHandler
* pHandler
= FindHandler(ext
, -1);
1106 SaveFile(filename
, pHandler
->GetType());
1110 wxLogError(_("Can't save image to file '%s': unknown extension."), filename
.c_str());
1115 bool wxImage::SaveFile( const wxString
& filename
, int type
) const
1118 ((wxImage
*)this)->SetOption(wxIMAGE_OPTION_FILENAME
, filename
);
1120 wxFileOutputStream
stream(filename
);
1122 if ( stream
.IsOk() )
1124 wxBufferedOutputStream
bstream( stream
);
1125 return SaveFile(bstream
, type
);
1127 #endif // wxUSE_STREAMS
1132 bool wxImage::SaveFile( const wxString
& filename
, const wxString
& mimetype
) const
1135 ((wxImage
*)this)->SetOption(wxIMAGE_OPTION_FILENAME
, filename
);
1137 wxFileOutputStream
stream(filename
);
1139 if ( stream
.IsOk() )
1141 wxBufferedOutputStream
bstream( stream
);
1142 return SaveFile(bstream
, mimetype
);
1144 #endif // wxUSE_STREAMS
1149 bool wxImage::CanRead( const wxString
&name
)
1152 wxFileInputStream
stream(name
);
1153 return CanRead(stream
);
1159 int wxImage::GetImageCount( const wxString
&name
, long type
)
1162 wxFileInputStream
stream(name
);
1164 return GetImageCount(stream
, type
);
1172 bool wxImage::CanRead( wxInputStream
&stream
)
1174 const wxList
& list
= GetHandlers();
1176 for ( wxList::compatibility_iterator node
= list
.GetFirst(); node
; node
= node
->GetNext() )
1178 wxImageHandler
*handler
=(wxImageHandler
*)node
->GetData();
1179 if (handler
->CanRead( stream
))
1186 int wxImage::GetImageCount( wxInputStream
&stream
, long type
)
1188 wxImageHandler
*handler
;
1190 if ( type
== wxBITMAP_TYPE_ANY
)
1192 wxList
&list
=GetHandlers();
1194 for (wxList::compatibility_iterator node
= list
.GetFirst(); node
; node
= node
->GetNext())
1196 handler
=(wxImageHandler
*)node
->GetData();
1197 if ( handler
->CanRead(stream
) )
1198 return handler
->GetImageCount(stream
);
1202 wxLogWarning(_("No handler found for image type."));
1206 handler
= FindHandler(type
);
1210 wxLogWarning(_("No image handler for type %d defined."), type
);
1214 if ( handler
->CanRead(stream
) )
1216 return handler
->GetImageCount(stream
);
1220 wxLogError(_("Image file is not of type %d."), type
);
1225 bool wxImage::LoadFile( wxInputStream
& stream
, long type
, int index
)
1229 m_refData
= new wxImageRefData
;
1231 wxImageHandler
*handler
;
1233 if ( type
== wxBITMAP_TYPE_ANY
)
1235 wxList
&list
=GetHandlers();
1237 for ( wxList::compatibility_iterator node
= list
.GetFirst(); node
; node
= node
->GetNext() )
1239 handler
=(wxImageHandler
*)node
->GetData();
1240 if ( handler
->CanRead(stream
) )
1241 return handler
->LoadFile(this, stream
, true/*verbose*/, index
);
1245 wxLogWarning( _("No handler found for image type.") );
1249 handler
= FindHandler(type
);
1253 wxLogWarning( _("No image handler for type %d defined."), type
);
1258 return handler
->LoadFile(this, stream
, true/*verbose*/, index
);
1261 bool wxImage::LoadFile( wxInputStream
& stream
, const wxString
& mimetype
, int index
)
1265 m_refData
= new wxImageRefData
;
1267 wxImageHandler
*handler
= FindHandlerMime(mimetype
);
1271 wxLogWarning( _("No image handler for type %s defined."), mimetype
.GetData() );
1276 return handler
->LoadFile( this, stream
, true/*verbose*/, index
);
1279 bool wxImage::SaveFile( wxOutputStream
& stream
, int type
) const
1281 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1283 wxImageHandler
*handler
= FindHandler(type
);
1287 wxLogWarning( _("No image handler for type %d defined."), type
);
1292 return handler
->SaveFile( (wxImage
*)this, stream
);
1295 bool wxImage::SaveFile( wxOutputStream
& stream
, const wxString
& mimetype
) const
1297 wxCHECK_MSG( Ok(), false, wxT("invalid image") );
1299 wxImageHandler
*handler
= FindHandlerMime(mimetype
);
1303 wxLogWarning( _("No image handler for type %s defined."), mimetype
.GetData() );
1308 return handler
->SaveFile( (wxImage
*)this, stream
);
1310 #endif // wxUSE_STREAMS
1312 void wxImage::AddHandler( wxImageHandler
*handler
)
1314 // Check for an existing handler of the type being added.
1315 if (FindHandler( handler
->GetType() ) == 0)
1317 sm_handlers
.Append( handler
);
1321 // This is not documented behaviour, merely the simplest 'fix'
1322 // for preventing duplicate additions. If someone ever has
1323 // a good reason to add and remove duplicate handlers (and they
1324 // may) we should probably refcount the duplicates.
1325 // also an issue in InsertHandler below.
1327 wxLogDebug( _T("Adding duplicate image handler for '%s'"),
1328 handler
->GetName().c_str() );
1333 void wxImage::InsertHandler( wxImageHandler
*handler
)
1335 // Check for an existing handler of the type being added.
1336 if (FindHandler( handler
->GetType() ) == 0)
1338 sm_handlers
.Insert( handler
);
1342 // see AddHandler for additional comments.
1343 wxLogDebug( _T("Inserting duplicate image handler for '%s'"),
1344 handler
->GetName().c_str() );
1349 bool wxImage::RemoveHandler( const wxString
& name
)
1351 wxImageHandler
*handler
= FindHandler(name
);
1354 sm_handlers
.DeleteObject(handler
);
1362 wxImageHandler
*wxImage::FindHandler( const wxString
& name
)
1364 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
1367 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
1368 if (handler
->GetName().Cmp(name
) == 0) return handler
;
1370 node
= node
->GetNext();
1375 wxImageHandler
*wxImage::FindHandler( const wxString
& extension
, long bitmapType
)
1377 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
1380 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
1381 if ( (handler
->GetExtension().Cmp(extension
) == 0) &&
1382 (bitmapType
== -1 || handler
->GetType() == bitmapType
) )
1384 node
= node
->GetNext();
1389 wxImageHandler
*wxImage::FindHandler( long bitmapType
)
1391 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
1394 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
1395 if (handler
->GetType() == bitmapType
) return handler
;
1396 node
= node
->GetNext();
1401 wxImageHandler
*wxImage::FindHandlerMime( const wxString
& mimetype
)
1403 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
1406 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
1407 if (handler
->GetMimeType().IsSameAs(mimetype
, false)) return handler
;
1408 node
= node
->GetNext();
1413 void wxImage::InitStandardHandlers()
1416 AddHandler(new wxBMPHandler
);
1417 #endif // wxUSE_STREAMS
1420 void wxImage::CleanUpHandlers()
1422 wxList::compatibility_iterator node
= sm_handlers
.GetFirst();
1425 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
1426 wxList::compatibility_iterator next
= node
->GetNext();
1431 sm_handlers
.Clear();
1434 wxString
wxImage::GetImageExtWildcard()
1438 wxList
& Handlers
= wxImage::GetHandlers();
1439 wxList::compatibility_iterator Node
= Handlers
.GetFirst();
1442 wxImageHandler
* Handler
= (wxImageHandler
*)Node
->GetData();
1443 fmts
+= wxT("*.") + Handler
->GetExtension();
1444 Node
= Node
->GetNext();
1445 if ( Node
) fmts
+= wxT(";");
1448 return wxT("(") + fmts
+ wxT(")|") + fmts
;
1451 //-----------------------------------------------------------------------------
1453 //-----------------------------------------------------------------------------
1455 IMPLEMENT_ABSTRACT_CLASS(wxImageHandler
,wxObject
)
1458 bool wxImageHandler::LoadFile( wxImage
*WXUNUSED(image
), wxInputStream
& WXUNUSED(stream
), bool WXUNUSED(verbose
), int WXUNUSED(index
) )
1463 bool wxImageHandler::SaveFile( wxImage
*WXUNUSED(image
), wxOutputStream
& WXUNUSED(stream
), bool WXUNUSED(verbose
) )
1468 int wxImageHandler::GetImageCount( wxInputStream
& WXUNUSED(stream
) )
1473 bool wxImageHandler::CanRead( const wxString
& name
)
1475 if (wxFileExists(name
))
1477 wxFileInputStream
stream(name
);
1478 return CanRead(stream
);
1481 wxLogError( _("Can't check image format of file '%s': file does not exist."), name
.c_str() );
1486 bool wxImageHandler::CallDoCanRead(wxInputStream
& stream
)
1488 off_t posOld
= stream
.TellI();
1489 if ( posOld
== wxInvalidOffset
)
1491 // can't test unseekable stream
1495 bool ok
= DoCanRead(stream
);
1497 // restore the old position to be able to test other formats and so on
1498 if ( stream
.SeekI(posOld
) == wxInvalidOffset
)
1500 wxLogDebug(_T("Failed to rewind the stream in wxImageHandler!"));
1502 // reading would fail anyhow as we're not at the right position
1509 #endif // wxUSE_STREAMS
1511 // ----------------------------------------------------------------------------
1512 // image histogram stuff
1513 // ----------------------------------------------------------------------------
1516 wxImageHistogram::FindFirstUnusedColour(unsigned char *r
,
1521 unsigned char g2
) const
1523 unsigned long key
= MakeKey(r2
, g2
, b2
);
1525 while ( find(key
) != end() )
1527 // color already used
1539 wxLogError(_("No unused colour in image.") );
1545 key
= MakeKey(r2
, g2
, b2
);
1559 wxImage::FindFirstUnusedColour(unsigned char *r
,
1564 unsigned char g2
) const
1566 wxImageHistogram histogram
;
1568 ComputeHistogram(histogram
);
1570 return histogram
.FindFirstUnusedColour(r
, g
, b
, r2
, g2
, b2
);
1576 // Counts and returns the number of different colours. Optionally stops
1577 // when it exceeds 'stopafter' different colours. This is useful, for
1578 // example, to see if the image can be saved as 8-bit (256 colour or
1579 // less, in this case it would be invoked as CountColours(256)). Default
1580 // value for stopafter is -1 (don't care).
1582 unsigned long wxImage::CountColours( unsigned long stopafter
) const
1586 unsigned char r
, g
, b
;
1588 unsigned long size
, nentries
, key
;
1591 size
= GetWidth() * GetHeight();
1594 for (unsigned long j
= 0; (j
< size
) && (nentries
<= stopafter
) ; j
++)
1599 key
= wxImageHistogram::MakeKey(r
, g
, b
);
1601 if (h
.Get(key
) == NULL
)
1612 unsigned long wxImage::ComputeHistogram( wxImageHistogram
&h
) const
1614 unsigned char *p
= GetData();
1615 unsigned long nentries
= 0;
1619 const unsigned long size
= GetWidth() * GetHeight();
1621 unsigned char r
, g
, b
;
1622 for ( unsigned long n
= 0; n
< size
; n
++ )
1628 wxImageHistogramEntry
& entry
= h
[wxImageHistogram::MakeKey(r
, g
, b
)];
1630 if ( entry
.value
++ == 0 )
1631 entry
.index
= nentries
++;
1638 * Rotation code by Carlos Moreno
1641 // GRG: I've removed wxRotationPoint - we already have wxRealPoint which
1642 // does exactly the same thing. And I also got rid of wxRotationPixel
1643 // bacause of potential problems in architectures where alignment
1644 // is an issue, so I had to rewrite parts of the code.
1646 static const double gs_Epsilon
= 1e-10;
1648 static inline int wxCint (double x
)
1650 return (x
> 0) ? (int) (x
+ 0.5) : (int) (x
- 0.5);
1654 // Auxiliary function to rotate a point (x,y) with respect to point p0
1655 // make it inline and use a straight return to facilitate optimization
1656 // also, the function receives the sine and cosine of the angle to avoid
1657 // repeating the time-consuming calls to these functions -- sin/cos can
1658 // be computed and stored in the calling function.
1660 inline wxRealPoint
rotated_point (const wxRealPoint
& p
, double cos_angle
, double sin_angle
, const wxRealPoint
& p0
)
1662 return wxRealPoint (p0
.x
+ (p
.x
- p0
.x
) * cos_angle
- (p
.y
- p0
.y
) * sin_angle
,
1663 p0
.y
+ (p
.y
- p0
.y
) * cos_angle
+ (p
.x
- p0
.x
) * sin_angle
);
1666 inline wxRealPoint
rotated_point (double x
, double y
, double cos_angle
, double sin_angle
, const wxRealPoint
& p0
)
1668 return rotated_point (wxRealPoint(x
,y
), cos_angle
, sin_angle
, p0
);
1671 wxImage
wxImage::Rotate(double angle
, const wxPoint
& centre_of_rotation
, bool interpolating
, wxPoint
* offset_after_rotation
) const
1674 angle
= -angle
; // screen coordinates are a mirror image of "real" coordinates
1676 // Create pointer-based array to accelerate access to wxImage's data
1677 unsigned char ** data
= new unsigned char * [GetHeight()];
1679 data
[0] = GetData();
1681 for (i
= 1; i
< GetHeight(); i
++)
1682 data
[i
] = data
[i
- 1] + (3 * GetWidth());
1684 // precompute coefficients for rotation formula
1685 // (sine and cosine of the angle)
1686 const double cos_angle
= cos(angle
);
1687 const double sin_angle
= sin(angle
);
1689 // Create new Image to store the result
1690 // First, find rectangle that covers the rotated image; to do that,
1691 // rotate the four corners
1693 const wxRealPoint
p0(centre_of_rotation
.x
, centre_of_rotation
.y
);
1695 wxRealPoint p1
= rotated_point (0, 0, cos_angle
, sin_angle
, p0
);
1696 wxRealPoint p2
= rotated_point (0, GetHeight(), cos_angle
, sin_angle
, p0
);
1697 wxRealPoint p3
= rotated_point (GetWidth(), 0, cos_angle
, sin_angle
, p0
);
1698 wxRealPoint p4
= rotated_point (GetWidth(), GetHeight(), cos_angle
, sin_angle
, p0
);
1700 int x1
= (int) floor (wxMin (wxMin(p1
.x
, p2
.x
), wxMin(p3
.x
, p4
.x
)));
1701 int y1
= (int) floor (wxMin (wxMin(p1
.y
, p2
.y
), wxMin(p3
.y
, p4
.y
)));
1702 int x2
= (int) ceil (wxMax (wxMax(p1
.x
, p2
.x
), wxMax(p3
.x
, p4
.x
)));
1703 int y2
= (int) ceil (wxMax (wxMax(p1
.y
, p2
.y
), wxMax(p3
.y
, p4
.y
)));
1705 wxImage
rotated (x2
- x1
+ 1, y2
- y1
+ 1, false);
1707 if (offset_after_rotation
!= NULL
)
1709 *offset_after_rotation
= wxPoint (x1
, y1
);
1712 // GRG: The rotated (destination) image is always accessed
1713 // sequentially, so there is no need for a pointer-based
1714 // array here (and in fact it would be slower).
1716 unsigned char * dst
= rotated
.GetData();
1718 // GRG: if the original image has a mask, use its RGB values
1719 // as the blank pixel, else, fall back to default (black).
1721 unsigned char blank_r
= 0;
1722 unsigned char blank_g
= 0;
1723 unsigned char blank_b
= 0;
1727 blank_r
= GetMaskRed();
1728 blank_g
= GetMaskGreen();
1729 blank_b
= GetMaskBlue();
1730 rotated
.SetMaskColour( blank_r
, blank_g
, blank_b
);
1733 // Now, for each point of the rotated image, find where it came from, by
1734 // performing an inverse rotation (a rotation of -angle) and getting the
1735 // pixel at those coordinates
1737 // GRG: I've taken the (interpolating) test out of the loops, so that
1738 // it is done only once, instead of repeating it for each pixel.
1743 for (int y
= 0; y
< rotated
.GetHeight(); y
++)
1745 for (x
= 0; x
< rotated
.GetWidth(); x
++)
1747 wxRealPoint src
= rotated_point (x
+ x1
, y
+ y1
, cos_angle
, -sin_angle
, p0
);
1749 if (-0.25 < src
.x
&& src
.x
< GetWidth() - 0.75 &&
1750 -0.25 < src
.y
&& src
.y
< GetHeight() - 0.75)
1752 // interpolate using the 4 enclosing grid-points. Those
1753 // points can be obtained using floor and ceiling of the
1754 // exact coordinates of the point
1755 // C.M. 2000-02-17: when the point is near the border, special care is required.
1759 if (0 < src
.x
&& src
.x
< GetWidth() - 1)
1761 x1
= wxCint(floor(src
.x
));
1762 x2
= wxCint(ceil(src
.x
));
1764 else // else means that x is near one of the borders (0 or width-1)
1766 x1
= x2
= wxCint (src
.x
);
1769 if (0 < src
.y
&& src
.y
< GetHeight() - 1)
1771 y1
= wxCint(floor(src
.y
));
1772 y2
= wxCint(ceil(src
.y
));
1776 y1
= y2
= wxCint (src
.y
);
1779 // get four points and the distances (square of the distance,
1780 // for efficiency reasons) for the interpolation formula
1782 // GRG: Do not calculate the points until they are
1783 // really needed -- this way we can calculate
1784 // just one, instead of four, if d1, d2, d3
1785 // or d4 are < gs_Epsilon
1787 const double d1
= (src
.x
- x1
) * (src
.x
- x1
) + (src
.y
- y1
) * (src
.y
- y1
);
1788 const double d2
= (src
.x
- x2
) * (src
.x
- x2
) + (src
.y
- y1
) * (src
.y
- y1
);
1789 const double d3
= (src
.x
- x2
) * (src
.x
- x2
) + (src
.y
- y2
) * (src
.y
- y2
);
1790 const double d4
= (src
.x
- x1
) * (src
.x
- x1
) + (src
.y
- y2
) * (src
.y
- y2
);
1792 // Now interpolate as a weighted average of the four surrounding
1793 // points, where the weights are the distances to each of those points
1795 // If the point is exactly at one point of the grid of the source
1796 // image, then don't interpolate -- just assign the pixel
1798 if (d1
< gs_Epsilon
) // d1,d2,d3,d4 are positive -- no need for abs()
1800 unsigned char *p
= data
[y1
] + (3 * x1
);
1805 else if (d2
< gs_Epsilon
)
1807 unsigned char *p
= data
[y1
] + (3 * x2
);
1812 else if (d3
< gs_Epsilon
)
1814 unsigned char *p
= data
[y2
] + (3 * x2
);
1819 else if (d4
< gs_Epsilon
)
1821 unsigned char *p
= data
[y2
] + (3 * x1
);
1828 // weights for the weighted average are proportional to the inverse of the distance
1829 unsigned char *v1
= data
[y1
] + (3 * x1
);
1830 unsigned char *v2
= data
[y1
] + (3 * x2
);
1831 unsigned char *v3
= data
[y2
] + (3 * x2
);
1832 unsigned char *v4
= data
[y2
] + (3 * x1
);
1834 const double w1
= 1/d1
, w2
= 1/d2
, w3
= 1/d3
, w4
= 1/d4
;
1838 *(dst
++) = (unsigned char)
1839 ( (w1
* *(v1
++) + w2
* *(v2
++) +
1840 w3
* *(v3
++) + w4
* *(v4
++)) /
1841 (w1
+ w2
+ w3
+ w4
) );
1842 *(dst
++) = (unsigned char)
1843 ( (w1
* *(v1
++) + w2
* *(v2
++) +
1844 w3
* *(v3
++) + w4
* *(v4
++)) /
1845 (w1
+ w2
+ w3
+ w4
) );
1846 *(dst
++) = (unsigned char)
1847 ( (w1
* *v1
+ w2
* *v2
+
1848 w3
* *v3
+ w4
* *v4
) /
1849 (w1
+ w2
+ w3
+ w4
) );
1861 else // not interpolating
1863 for (int y
= 0; y
< rotated
.GetHeight(); y
++)
1865 for (x
= 0; x
< rotated
.GetWidth(); x
++)
1867 wxRealPoint src
= rotated_point (x
+ x1
, y
+ y1
, cos_angle
, -sin_angle
, p0
);
1869 const int xs
= wxCint (src
.x
); // wxCint rounds to the
1870 const int ys
= wxCint (src
.y
); // closest integer
1872 if (0 <= xs
&& xs
< GetWidth() &&
1873 0 <= ys
&& ys
< GetHeight())
1875 unsigned char *p
= data
[ys
] + (3 * xs
);
1899 // A module to allow wxImage initialization/cleanup
1900 // without calling these functions from app.cpp or from
1901 // the user's application.
1903 class wxImageModule
: public wxModule
1905 DECLARE_DYNAMIC_CLASS(wxImageModule
)
1908 bool OnInit() { wxImage::InitStandardHandlers(); return true; };
1909 void OnExit() { wxImage::CleanUpHandlers(); };
1912 IMPLEMENT_DYNAMIC_CLASS(wxImageModule
, wxModule
)
1915 #endif // wxUSE_IMAGE