1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Robert Roebling
6 // Copyright: (c) Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
11 #pragma implementation "image.h"
14 // For compilers that support precompilation, includes "wx.h".
15 #include "wx/wxprec.h"
26 #include "wx/bitmap.h"
30 #include "wx/filefn.h"
31 #include "wx/filesys.h"
32 #include "wx/wfstream.h"
34 #include "wx/module.h"
45 //-----------------------------------------------------------------------------
47 //-----------------------------------------------------------------------------
49 class wxImageRefData
: public wxObjectRefData
57 unsigned char *m_data
;
59 unsigned char m_maskRed
,m_maskGreen
,m_maskBlue
;
63 wxArrayString m_optionNames
;
64 wxArrayString m_optionValues
;
67 wxImageRefData::wxImageRefData()
71 m_data
= (unsigned char*) NULL
;
80 wxImageRefData::~wxImageRefData()
82 if (m_data
&& !m_static
)
86 wxList
wxImage::sm_handlers
;
90 //-----------------------------------------------------------------------------
92 #define M_IMGDATA ((wxImageRefData *)m_refData)
94 IMPLEMENT_DYNAMIC_CLASS(wxImage
, wxObject
)
100 wxImage::wxImage( int width
, int height
)
102 Create( width
, height
);
105 wxImage::wxImage( int width
, int height
, unsigned char* data
, bool static_data
)
107 Create( width
, height
, data
, static_data
);
110 wxImage::wxImage( const wxString
& name
, long type
)
112 LoadFile( name
, type
);
115 wxImage::wxImage( const wxString
& name
, const wxString
& mimetype
)
117 LoadFile( name
, mimetype
);
121 wxImage::wxImage( wxInputStream
& stream
, long type
)
123 LoadFile( stream
, type
);
126 wxImage::wxImage( wxInputStream
& stream
, const wxString
& mimetype
)
128 LoadFile( stream
, mimetype
);
130 #endif // wxUSE_STREAMS
132 wxImage::wxImage( const wxImage
& image
)
137 wxImage::wxImage( const wxImage
* image
)
139 if (image
) Ref(*image
);
142 void wxImage::Create( int width
, int height
)
146 m_refData
= new wxImageRefData();
148 M_IMGDATA
->m_data
= (unsigned char *) malloc( width
*height
*3 );
149 if (M_IMGDATA
->m_data
)
151 for (int l
= 0; l
< width
*height
*3; l
++) M_IMGDATA
->m_data
[l
] = 0;
153 M_IMGDATA
->m_width
= width
;
154 M_IMGDATA
->m_height
= height
;
155 M_IMGDATA
->m_ok
= TRUE
;
163 void wxImage::Create( int width
, int height
, unsigned char* data
, bool static_data
)
167 m_refData
= new wxImageRefData();
169 M_IMGDATA
->m_data
= data
;
170 if (M_IMGDATA
->m_data
)
172 M_IMGDATA
->m_width
= width
;
173 M_IMGDATA
->m_height
= height
;
174 M_IMGDATA
->m_ok
= TRUE
;
175 M_IMGDATA
->m_static
= static_data
;
183 void wxImage::Destroy()
188 wxImage
wxImage::Copy() const
192 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
194 image
.Create( M_IMGDATA
->m_width
, M_IMGDATA
->m_height
);
196 char unsigned *data
= image
.GetData();
198 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
200 if (M_IMGDATA
->m_hasMask
)
201 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
203 memcpy( data
, GetData(), M_IMGDATA
->m_width
*M_IMGDATA
->m_height
*3 );
208 wxImage
wxImage::Scale( int width
, int height
) const
212 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
214 wxCHECK_MSG( (width
> 0) && (height
> 0), image
, wxT("invalid image size") );
216 image
.Create( width
, height
);
218 char unsigned *data
= image
.GetData();
220 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
222 if (M_IMGDATA
->m_hasMask
)
223 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
225 long old_height
= M_IMGDATA
->m_height
;
226 long old_width
= M_IMGDATA
->m_width
;
228 char unsigned *source_data
= M_IMGDATA
->m_data
;
229 char unsigned *target_data
= data
;
231 for (long j
= 0; j
< height
; j
++)
233 long y_offset
= (j
* (old_height
-1) / (height
-1)) * old_width
;
235 for (long i
= 0; i
< width
; i
++)
238 source_data
+ 3*(y_offset
+ ((i
* (old_width
-1) )/ (width
-1))),
247 wxImage
wxImage::Rotate90( bool clockwise
) const
251 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
253 image
.Create( M_IMGDATA
->m_height
, M_IMGDATA
->m_width
);
255 char unsigned *data
= image
.GetData();
257 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
259 if (M_IMGDATA
->m_hasMask
)
260 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
262 long height
= M_IMGDATA
->m_height
;
263 long width
= M_IMGDATA
->m_width
;
265 char unsigned *source_data
= M_IMGDATA
->m_data
;
266 char unsigned *target_data
;
268 for (long j
= 0; j
< height
; j
++)
270 for (long i
= 0; i
< width
; i
++)
273 target_data
= data
+ (((i
+1)*height
) - j
- 1)*3;
275 target_data
= data
+ ((height
*(width
-1)) + j
- (i
*height
))*3;
276 memcpy( target_data
, source_data
, 3 );
284 wxImage
wxImage::Mirror( bool horizontally
) const
288 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
290 image
.Create( M_IMGDATA
->m_width
, M_IMGDATA
->m_height
);
292 char unsigned *data
= image
.GetData();
294 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
296 if (M_IMGDATA
->m_hasMask
)
297 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
299 long height
= M_IMGDATA
->m_height
;
300 long width
= M_IMGDATA
->m_width
;
302 char unsigned *source_data
= M_IMGDATA
->m_data
;
303 char unsigned *target_data
;
307 for (long j
= 0; j
< height
; j
++)
310 target_data
= data
-3;
311 for (long i
= 0; i
< width
; i
++)
313 memcpy( target_data
, source_data
, 3 );
321 for (long i
= 0; i
< height
; i
++)
323 target_data
= data
+ 3*width
*(height
-1-i
);
324 memcpy( target_data
, source_data
, (size_t)3*width
);
325 source_data
+= 3*width
;
332 wxImage
wxImage::GetSubImage( const wxRect
&rect
) const
336 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
338 wxCHECK_MSG( (rect
.GetLeft()>=0) && (rect
.GetTop()>=0) && (rect
.GetRight()<=GetWidth()) && (rect
.GetBottom()<=GetHeight()),
339 image
, wxT("invalid subimage size") );
341 int subwidth
=rect
.GetWidth();
342 const int subheight
=rect
.GetHeight();
344 image
.Create( subwidth
, subheight
);
346 char unsigned *subdata
= image
.GetData(), *data
=GetData();
348 wxCHECK_MSG( subdata
, image
, wxT("unable to create image") );
350 if (M_IMGDATA
->m_hasMask
)
351 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
353 const int subleft
=3*rect
.GetLeft();
354 const int width
=3*GetWidth();
357 data
+=rect
.GetTop()*width
+subleft
;
359 for (long j
= 0; j
< subheight
; ++j
)
361 memcpy( subdata
, data
, subwidth
);
369 void wxImage::Paste( const wxImage
&image
, int x
, int y
)
371 wxCHECK_RET( Ok(), wxT("invalid image") );
372 wxCHECK_RET( image
.Ok(), wxT("invalid image") );
376 int width
= image
.GetWidth();
377 int height
= image
.GetHeight();
390 if ((x
+xx
)+width
> M_IMGDATA
->m_width
)
391 width
= M_IMGDATA
->m_width
- (x
+xx
);
392 if ((y
+yy
)+height
> M_IMGDATA
->m_height
)
393 height
= M_IMGDATA
->m_height
- (y
+yy
);
395 if (width
< 1) return;
396 if (height
< 1) return;
398 if ((!HasMask() && !image
.HasMask()) ||
399 ((HasMask() && image
.HasMask() &&
400 (GetMaskRed()==image
.GetMaskRed()) &&
401 (GetMaskGreen()==image
.GetMaskGreen()) &&
402 (GetMaskBlue()==image
.GetMaskBlue()))))
405 unsigned char* source_data
= image
.GetData() + xx
*3 + yy
*3*image
.GetWidth();
406 int source_step
= image
.GetWidth()*3;
408 unsigned char* target_data
= GetData() + (x
+xx
)*3 + (y
+yy
)*3*M_IMGDATA
->m_width
;
409 int target_step
= M_IMGDATA
->m_width
*3;
410 for (int j
= 0; j
< height
; j
++)
412 memcpy( target_data
, source_data
, width
);
413 source_data
+= source_step
;
414 target_data
+= target_step
;
419 if (!HasMask() && image
.HasMask())
421 unsigned char r
= image
.GetMaskRed();
422 unsigned char g
= image
.GetMaskGreen();
423 unsigned char b
= image
.GetMaskBlue();
426 unsigned char* source_data
= image
.GetData() + xx
*3 + yy
*3*image
.GetWidth();
427 int source_step
= image
.GetWidth()*3;
429 unsigned char* target_data
= GetData() + (x
+xx
)*3 + (y
+yy
)*3*M_IMGDATA
->m_width
;
430 int target_step
= M_IMGDATA
->m_width
*3;
432 for (int j
= 0; j
< height
; j
++)
434 for (int i
= 0; i
< width
; i
+=3)
436 if ((source_data
[i
] != r
) &&
437 (source_data
[i
+1] != g
) &&
438 (source_data
[i
+2] != b
))
440 memcpy( target_data
+i
, source_data
+i
, 3 );
443 source_data
+= source_step
;
444 target_data
+= target_step
;
449 void wxImage::Replace( unsigned char r1
, unsigned char g1
, unsigned char b1
,
450 unsigned char r2
, unsigned char g2
, unsigned char b2
)
452 wxCHECK_RET( Ok(), wxT("invalid image") );
454 char unsigned *data
= GetData();
456 const int w
= GetWidth();
457 const int h
= GetHeight();
459 for (int j
= 0; j
< h
; j
++)
460 for (int i
= 0; i
< w
; i
++)
462 if ((data
[0] == r1
) && (data
[1] == g1
) && (data
[2] == b1
))
472 wxImage
wxImage::ConvertToMono( unsigned char r
, unsigned char g
, unsigned char b
) const
476 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
478 image
.Create( M_IMGDATA
->m_width
, M_IMGDATA
->m_height
);
480 char unsigned *data
= image
.GetData();
482 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
484 if (M_IMGDATA
->m_hasMask
)
486 if (M_IMGDATA
->m_maskRed
== r
&& M_IMGDATA
->m_maskGreen
== g
&&
487 M_IMGDATA
->m_maskBlue
== b
)
488 image
.SetMaskColour( 255, 255, 255 );
490 image
.SetMaskColour( 0, 0, 0 );
493 long size
= M_IMGDATA
->m_height
* M_IMGDATA
->m_width
;
495 char unsigned *srcd
= M_IMGDATA
->m_data
;
496 char unsigned *tard
= image
.GetData();
498 for ( long i
= 0; i
< size
; i
++, srcd
+= 3, tard
+= 3 )
500 if (srcd
[0] == r
&& srcd
[1] == g
&& srcd
[2] == b
)
501 tard
[0] = tard
[1] = tard
[2] = 255;
503 tard
[0] = tard
[1] = tard
[2] = 0;
509 void wxImage::SetRGB( int x
, int y
, unsigned char r
, unsigned char g
, unsigned char b
)
511 wxCHECK_RET( Ok(), wxT("invalid image") );
513 int w
= M_IMGDATA
->m_width
;
514 int h
= M_IMGDATA
->m_height
;
516 wxCHECK_RET( (x
>=0) && (y
>=0) && (x
<w
) && (y
<h
), wxT("invalid image index") );
518 long pos
= (y
* w
+ x
) * 3;
520 M_IMGDATA
->m_data
[ pos
] = r
;
521 M_IMGDATA
->m_data
[ pos
+1 ] = g
;
522 M_IMGDATA
->m_data
[ pos
+2 ] = b
;
525 unsigned char wxImage::GetRed( int x
, int y
) const
527 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
529 int w
= M_IMGDATA
->m_width
;
530 int h
= M_IMGDATA
->m_height
;
532 wxCHECK_MSG( (x
>=0) && (y
>=0) && (x
<w
) && (y
<h
), 0, wxT("invalid image index") );
534 long pos
= (y
* w
+ x
) * 3;
536 return M_IMGDATA
->m_data
[pos
];
539 unsigned char wxImage::GetGreen( int x
, int y
) const
541 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
543 int w
= M_IMGDATA
->m_width
;
544 int h
= M_IMGDATA
->m_height
;
546 wxCHECK_MSG( (x
>=0) && (y
>=0) && (x
<w
) && (y
<h
), 0, wxT("invalid image index") );
548 long pos
= (y
* w
+ x
) * 3;
550 return M_IMGDATA
->m_data
[pos
+1];
553 unsigned char wxImage::GetBlue( int x
, int y
) const
555 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
557 int w
= M_IMGDATA
->m_width
;
558 int h
= M_IMGDATA
->m_height
;
560 wxCHECK_MSG( (x
>=0) && (y
>=0) && (x
<w
) && (y
<h
), 0, wxT("invalid image index") );
562 long pos
= (y
* w
+ x
) * 3;
564 return M_IMGDATA
->m_data
[pos
+2];
567 bool wxImage::Ok() const
569 // image of 0 width or height can't be considered ok - at least because it
570 // causes crashes in ConvertToBitmap() if we don't catch it in time
571 wxImageRefData
*data
= M_IMGDATA
;
572 return data
&& data
->m_ok
&& data
->m_width
&& data
->m_height
;
575 char unsigned *wxImage::GetData() const
577 wxCHECK_MSG( Ok(), (char unsigned *)NULL
, wxT("invalid image") );
579 return M_IMGDATA
->m_data
;
582 void wxImage::SetData( char unsigned *data
)
584 wxCHECK_RET( Ok(), wxT("invalid image") );
586 wxImageRefData
*newRefData
= new wxImageRefData();
588 newRefData
->m_width
= M_IMGDATA
->m_width
;
589 newRefData
->m_height
= M_IMGDATA
->m_height
;
590 newRefData
->m_data
= data
;
591 newRefData
->m_ok
= TRUE
;
592 newRefData
->m_maskRed
= M_IMGDATA
->m_maskRed
;
593 newRefData
->m_maskGreen
= M_IMGDATA
->m_maskGreen
;
594 newRefData
->m_maskBlue
= M_IMGDATA
->m_maskBlue
;
595 newRefData
->m_hasMask
= M_IMGDATA
->m_hasMask
;
599 m_refData
= newRefData
;
602 void wxImage::SetData( char unsigned *data
, int new_width
, int new_height
)
604 wxImageRefData
*newRefData
= new wxImageRefData();
608 newRefData
->m_width
= new_width
;
609 newRefData
->m_height
= new_height
;
610 newRefData
->m_data
= data
;
611 newRefData
->m_ok
= TRUE
;
612 newRefData
->m_maskRed
= M_IMGDATA
->m_maskRed
;
613 newRefData
->m_maskGreen
= M_IMGDATA
->m_maskGreen
;
614 newRefData
->m_maskBlue
= M_IMGDATA
->m_maskBlue
;
615 newRefData
->m_hasMask
= M_IMGDATA
->m_hasMask
;
619 newRefData
->m_width
= new_width
;
620 newRefData
->m_height
= new_height
;
621 newRefData
->m_data
= data
;
622 newRefData
->m_ok
= TRUE
;
627 m_refData
= newRefData
;
630 void wxImage::SetMaskColour( unsigned char r
, unsigned char g
, unsigned char b
)
632 wxCHECK_RET( Ok(), wxT("invalid image") );
634 M_IMGDATA
->m_maskRed
= r
;
635 M_IMGDATA
->m_maskGreen
= g
;
636 M_IMGDATA
->m_maskBlue
= b
;
637 M_IMGDATA
->m_hasMask
= TRUE
;
640 unsigned char wxImage::GetMaskRed() const
642 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
644 return M_IMGDATA
->m_maskRed
;
647 unsigned char wxImage::GetMaskGreen() const
649 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
651 return M_IMGDATA
->m_maskGreen
;
654 unsigned char wxImage::GetMaskBlue() const
656 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
658 return M_IMGDATA
->m_maskBlue
;
661 void wxImage::SetMask( bool mask
)
663 wxCHECK_RET( Ok(), wxT("invalid image") );
665 M_IMGDATA
->m_hasMask
= mask
;
668 bool wxImage::HasMask() const
670 wxCHECK_MSG( Ok(), FALSE
, wxT("invalid image") );
672 return M_IMGDATA
->m_hasMask
;
675 int wxImage::GetWidth() const
677 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
679 return M_IMGDATA
->m_width
;
682 int wxImage::GetHeight() const
684 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
686 return M_IMGDATA
->m_height
;
691 bool wxImage::HasPalette() const
696 return M_IMGDATA
->m_palette
.Ok();
699 const wxPalette
& wxImage::GetPalette() const
701 wxCHECK_MSG( Ok(), wxNullPalette
, wxT("invalid image") );
703 return M_IMGDATA
->m_palette
;
706 void wxImage::SetPalette(const wxPalette
& palette
)
708 wxCHECK_RET( Ok(), wxT("invalid image") );
710 M_IMGDATA
->m_palette
= palette
;
713 // Option functions (arbitrary name/value mapping)
714 void wxImage::SetOption(const wxString
& name
, const wxString
& value
)
716 wxCHECK_RET( Ok(), wxT("invalid image") );
718 int idx
= M_IMGDATA
->m_optionNames
.Index(name
, FALSE
);
719 if (idx
== wxNOT_FOUND
)
721 M_IMGDATA
->m_optionNames
.Add(name
);
722 M_IMGDATA
->m_optionValues
.Add(value
);
726 M_IMGDATA
->m_optionNames
[idx
] = name
;
727 M_IMGDATA
->m_optionValues
[idx
] = value
;
731 void wxImage::SetOption(const wxString
& name
, int value
)
734 valStr
.Printf(wxT("%d"), value
);
735 SetOption(name
, valStr
);
738 wxString
wxImage::GetOption(const wxString
& name
) const
740 wxCHECK_MSG( Ok(), wxEmptyString
, wxT("invalid image") );
742 int idx
= M_IMGDATA
->m_optionNames
.Index(name
, FALSE
);
743 if (idx
== wxNOT_FOUND
)
744 return wxEmptyString
;
746 return M_IMGDATA
->m_optionValues
[idx
];
749 int wxImage::GetOptionInt(const wxString
& name
) const
751 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
753 return wxAtoi(GetOption(name
));
756 bool wxImage::HasOption(const wxString
& name
) const
758 wxCHECK_MSG( Ok(), FALSE
, wxT("invalid image") );
760 return (M_IMGDATA
->m_optionNames
.Index(name
, FALSE
) != wxNOT_FOUND
);
763 bool wxImage::LoadFile( const wxString
& filename
, long type
)
766 // We want to use wxFileSystem for virtual FS compatibility
768 wxFSFile
*file
= fsys
.OpenFile(filename
);
770 wxLogError(_("Can't open file '%s'"), filename
);
773 wxInputStream
*stream
= file
->GetStream();
775 wxLogError(_("Can't open stream for file '%s'"), filename
);
778 return LoadFile(*stream
, type
);
779 #else // !wxUSE_STREAMS
781 #endif // wxUSE_STREAMS
784 bool wxImage::LoadFile( const wxString
& filename
, const wxString
& mimetype
)
787 // We want to use wxFileSystem for virtual FS compatibility
789 wxFSFile
*file
= fsys
.OpenFile(filename
);
791 wxLogError(_("Can't open file '%s'"), filename
);
794 wxInputStream
*stream
= file
->GetStream();
796 wxLogError(_("Can't open stream for file '%s'"), filename
);
799 return LoadFile(*stream
, mimetype
);
800 #else // !wxUSE_STREAMS
802 #endif // wxUSE_STREAMS
805 bool wxImage::SaveFile( const wxString
& filename
, int type
)
808 wxFileOutputStream
stream(filename
);
810 if ( stream
.LastError() == wxStream_NOERROR
)
812 wxBufferedOutputStream
bstream( stream
);
813 return SaveFile(bstream
, type
);
815 #endif // wxUSE_STREAMS
820 bool wxImage::SaveFile( const wxString
& filename
, const wxString
& mimetype
)
823 wxFileOutputStream
stream(filename
);
825 if ( stream
.LastError() == wxStream_NOERROR
)
827 wxBufferedOutputStream
bstream( stream
);
828 return SaveFile(bstream
, mimetype
);
830 #endif // wxUSE_STREAMS
835 bool wxImage::CanRead( const wxString
&name
)
838 wxFileInputStream
stream(name
);
839 return CanRead(stream
);
847 bool wxImage::CanRead( wxInputStream
&stream
)
849 wxList
&list
=GetHandlers();
851 for ( wxList::Node
*node
= list
.GetFirst(); node
; node
= node
->GetNext() )
853 wxImageHandler
*handler
=(wxImageHandler
*)node
->GetData();
854 if (handler
->CanRead( stream
))
861 bool wxImage::LoadFile( wxInputStream
& stream
, long type
)
865 m_refData
= new wxImageRefData
;
867 wxImageHandler
*handler
;
869 if (type
==wxBITMAP_TYPE_ANY
)
871 wxList
&list
=GetHandlers();
873 for ( wxList::Node
*node
= list
.GetFirst(); node
; node
= node
->GetNext() )
875 handler
=(wxImageHandler
*)node
->GetData();
876 if (handler
->CanRead( stream
))
877 return handler
->LoadFile( this, stream
);
881 wxLogWarning( _("No handler found for image type.") );
885 handler
= FindHandler(type
);
889 wxLogWarning( _("No image handler for type %d defined."), type
);
894 return handler
->LoadFile( this, stream
);
897 bool wxImage::LoadFile( wxInputStream
& stream
, const wxString
& mimetype
)
901 m_refData
= new wxImageRefData
;
903 wxImageHandler
*handler
= FindHandlerMime(mimetype
);
907 wxLogWarning( _("No image handler for type %s defined."), mimetype
.GetData() );
912 return handler
->LoadFile( this, stream
);
915 bool wxImage::SaveFile( wxOutputStream
& stream
, int type
)
917 wxCHECK_MSG( Ok(), FALSE
, wxT("invalid image") );
919 wxImageHandler
*handler
= FindHandler(type
);
923 wxLogWarning( _("No image handler for type %d defined."), type
);
928 return handler
->SaveFile( this, stream
);
931 bool wxImage::SaveFile( wxOutputStream
& stream
, const wxString
& mimetype
)
933 wxCHECK_MSG( Ok(), FALSE
, wxT("invalid image") );
935 wxImageHandler
*handler
= FindHandlerMime(mimetype
);
939 wxLogWarning( _("No image handler for type %s defined."), mimetype
.GetData() );
944 return handler
->SaveFile( this, stream
);
946 #endif // wxUSE_STREAMS
948 void wxImage::AddHandler( wxImageHandler
*handler
)
950 // make sure that the memory will be freed at the program end
951 sm_handlers
.DeleteContents(TRUE
);
953 sm_handlers
.Append( handler
);
956 void wxImage::InsertHandler( wxImageHandler
*handler
)
958 // make sure that the memory will be freed at the program end
959 sm_handlers
.DeleteContents(TRUE
);
961 sm_handlers
.Insert( handler
);
964 bool wxImage::RemoveHandler( const wxString
& name
)
966 wxImageHandler
*handler
= FindHandler(name
);
969 sm_handlers
.DeleteObject(handler
);
976 wxImageHandler
*wxImage::FindHandler( const wxString
& name
)
978 wxNode
*node
= sm_handlers
.First();
981 wxImageHandler
*handler
= (wxImageHandler
*)node
->Data();
982 if (handler
->GetName().Cmp(name
) == 0) return handler
;
986 return (wxImageHandler
*)NULL
;
989 wxImageHandler
*wxImage::FindHandler( const wxString
& extension
, long bitmapType
)
991 wxNode
*node
= sm_handlers
.First();
994 wxImageHandler
*handler
= (wxImageHandler
*)node
->Data();
995 if ( (handler
->GetExtension().Cmp(extension
) == 0) &&
996 (bitmapType
== -1 || handler
->GetType() == bitmapType
) )
1000 return (wxImageHandler
*)NULL
;
1003 wxImageHandler
*wxImage::FindHandler( long bitmapType
)
1005 wxNode
*node
= sm_handlers
.First();
1008 wxImageHandler
*handler
= (wxImageHandler
*)node
->Data();
1009 if (handler
->GetType() == bitmapType
) return handler
;
1010 node
= node
->Next();
1015 wxImageHandler
*wxImage::FindHandlerMime( const wxString
& mimetype
)
1017 wxNode
*node
= sm_handlers
.First();
1020 wxImageHandler
*handler
= (wxImageHandler
*)node
->Data();
1021 if (handler
->GetMimeType().IsSameAs(mimetype
, FALSE
)) return handler
;
1022 node
= node
->Next();
1027 void wxImage::InitStandardHandlers()
1029 AddHandler(new wxBMPHandler
);
1030 #if !defined(__WXGTK__) && !defined(__WXMOTIF__)
1031 AddHandler(new wxXPMHandler
);
1035 void wxImage::CleanUpHandlers()
1037 wxNode
*node
= sm_handlers
.First();
1040 wxImageHandler
*handler
= (wxImageHandler
*)node
->Data();
1041 wxNode
*next
= node
->Next();
1048 //-----------------------------------------------------------------------------
1050 //-----------------------------------------------------------------------------
1052 IMPLEMENT_ABSTRACT_CLASS(wxImageHandler
,wxObject
)
1055 bool wxImageHandler::LoadFile( wxImage
*WXUNUSED(image
), wxInputStream
& WXUNUSED(stream
), bool WXUNUSED(verbose
), int WXUNUSED(index
) )
1060 bool wxImageHandler::SaveFile( wxImage
*WXUNUSED(image
), wxOutputStream
& WXUNUSED(stream
), bool WXUNUSED(verbose
) )
1065 int wxImageHandler::GetImageCount( wxInputStream
& WXUNUSED(stream
) )
1070 bool wxImageHandler::CanRead( const wxString
& name
)
1072 if (wxFileExists(name
))
1074 wxFileInputStream
stream(name
);
1075 return CanRead(stream
);
1079 wxLogError( _("Can't check image format of file '%s': file does not exist."), name
.c_str() );
1086 #endif // wxUSE_STREAMS
1090 //-----------------------------------------------------------------------------
1091 // wxBitmap convertion routines
1092 //-----------------------------------------------------------------------------
1097 wxBitmap
wxImage::ConvertToMonoBitmap( unsigned char red
, unsigned char green
, unsigned char blue
) const
1099 wxImage mono
= this->ConvertToMono( red
, green
, blue
);
1100 wxBitmap
bitmap( mono
, 1 );
1105 wxBitmap
wxImage::ConvertToBitmap() const
1107 wxBitmap
bitmap( *this );
1111 wxImage::wxImage( const wxBitmap
&bitmap
)
1113 *this = bitmap
.ConvertToImage();
1120 // A module to allow wxImage initialization/cleanup
1121 // without calling these functions from app.cpp or from
1122 // the user's application.
1124 class wxImageModule
: public wxModule
1126 DECLARE_DYNAMIC_CLASS(wxImageModule
)
1129 bool OnInit() { wxImage::InitStandardHandlers(); return TRUE
; };
1130 void OnExit() { wxImage::CleanUpHandlers(); };
1133 IMPLEMENT_DYNAMIC_CLASS(wxImageModule
, wxModule
)
1136 //-----------------------------------------------------------------------------
1139 // Counts and returns the number of different colours. Optionally stops
1140 // when it exceeds 'stopafter' different colours. This is useful, for
1141 // example, to see if the image can be saved as 8-bit (256 colour or
1142 // less, in this case it would be invoked as CountColours(256)). Default
1143 // value for stopafter is -1 (don't care).
1145 unsigned long wxImage::CountColours( unsigned long stopafter
)
1149 unsigned char r
, g
, b
;
1151 unsigned long size
, nentries
, key
;
1154 size
= GetWidth() * GetHeight();
1157 for (unsigned long j
= 0; (j
< size
) && (nentries
<= stopafter
) ; j
++)
1162 key
= (r
<< 16) | (g
<< 8) | b
;
1164 if (h
.Get(key
) == NULL
)
1176 // Computes the histogram of the image and fills a hash table, indexed
1177 // with integer keys built as 0xRRGGBB, containing wxHNode objects. Each
1178 // wxHNode contains an 'index' (useful to build a palette with the image
1179 // colours) and a 'value', which is the number of pixels in the image with
1182 unsigned long wxImage::ComputeHistogram( wxHashTable
&h
)
1184 unsigned char r
, g
, b
;
1186 unsigned long size
, nentries
, key
;
1190 size
= GetWidth() * GetHeight();
1193 for (unsigned long j
= 0; j
< size
; j
++)
1198 key
= (r
<< 16) | (g
<< 8) | b
;
1200 hnode
= (wxHNode
*) h
.Get(key
);
1206 hnode
= new wxHNode();
1207 hnode
->index
= nentries
++;
1210 h
.Put(key
, (wxObject
*)hnode
);
1218 * Rotation code by Carlos Moreno
1221 // GRG: I've removed wxRotationPoint - we already have wxRealPoint which
1222 // does exactly the same thing. And I also got rid of wxRotationPixel
1223 // bacause of potential problems in architectures where alignment
1224 // is an issue, so I had to rewrite parts of the code.
1226 static const double gs_Epsilon
= 1e-10;
1228 static inline int wxCint (double x
)
1230 return (x
> 0) ? (int) (x
+ 0.5) : (int) (x
- 0.5);
1234 // Auxiliary function to rotate a point (x,y) with respect to point p0
1235 // make it inline and use a straight return to facilitate optimization
1236 // also, the function receives the sine and cosine of the angle to avoid
1237 // repeating the time-consuming calls to these functions -- sin/cos can
1238 // be computed and stored in the calling function.
1240 inline wxRealPoint
rotated_point (const wxRealPoint
& p
, double cos_angle
, double sin_angle
, const wxRealPoint
& p0
)
1242 return wxRealPoint (p0
.x
+ (p
.x
- p0
.x
) * cos_angle
- (p
.y
- p0
.y
) * sin_angle
,
1243 p0
.y
+ (p
.y
- p0
.y
) * cos_angle
+ (p
.x
- p0
.x
) * sin_angle
);
1246 inline wxRealPoint
rotated_point (double x
, double y
, double cos_angle
, double sin_angle
, const wxRealPoint
& p0
)
1248 return rotated_point (wxRealPoint(x
,y
), cos_angle
, sin_angle
, p0
);
1251 wxImage
wxImage::Rotate(double angle
, const wxPoint
& centre_of_rotation
, bool interpolating
, wxPoint
* offset_after_rotation
) const
1254 angle
= -angle
; // screen coordinates are a mirror image of "real" coordinates
1256 // Create pointer-based array to accelerate access to wxImage's data
1257 unsigned char ** data
= new unsigned char * [GetHeight()];
1259 data
[0] = GetData();
1261 for (i
= 1; i
< GetHeight(); i
++)
1262 data
[i
] = data
[i
- 1] + (3 * GetWidth());
1264 // precompute coefficients for rotation formula
1265 // (sine and cosine of the angle)
1266 const double cos_angle
= cos(angle
);
1267 const double sin_angle
= sin(angle
);
1269 // Create new Image to store the result
1270 // First, find rectangle that covers the rotated image; to do that,
1271 // rotate the four corners
1273 const wxRealPoint
p0(centre_of_rotation
.x
, centre_of_rotation
.y
);
1275 wxRealPoint p1
= rotated_point (0, 0, cos_angle
, sin_angle
, p0
);
1276 wxRealPoint p2
= rotated_point (0, GetHeight(), cos_angle
, sin_angle
, p0
);
1277 wxRealPoint p3
= rotated_point (GetWidth(), 0, cos_angle
, sin_angle
, p0
);
1278 wxRealPoint p4
= rotated_point (GetWidth(), GetHeight(), cos_angle
, sin_angle
, p0
);
1280 int x1
= (int) floor (wxMin (wxMin(p1
.x
, p2
.x
), wxMin(p3
.x
, p4
.x
)));
1281 int y1
= (int) floor (wxMin (wxMin(p1
.y
, p2
.y
), wxMin(p3
.y
, p4
.y
)));
1282 int x2
= (int) ceil (wxMax (wxMax(p1
.x
, p2
.x
), wxMax(p3
.x
, p4
.x
)));
1283 int y2
= (int) ceil (wxMax (wxMax(p1
.y
, p2
.y
), wxMax(p3
.y
, p4
.y
)));
1285 wxImage
rotated (x2
- x1
+ 1, y2
- y1
+ 1);
1287 if (offset_after_rotation
!= NULL
)
1289 *offset_after_rotation
= wxPoint (x1
, y1
);
1292 // GRG: The rotated (destination) image is always accessed
1293 // sequentially, so there is no need for a pointer-based
1294 // array here (and in fact it would be slower).
1296 unsigned char * dst
= rotated
.GetData();
1298 // GRG: if the original image has a mask, use its RGB values
1299 // as the blank pixel, else, fall back to default (black).
1301 unsigned char blank_r
= 0;
1302 unsigned char blank_g
= 0;
1303 unsigned char blank_b
= 0;
1307 blank_r
= GetMaskRed();
1308 blank_g
= GetMaskGreen();
1309 blank_b
= GetMaskBlue();
1310 rotated
.SetMaskColour( blank_r
, blank_g
, blank_b
);
1313 // Now, for each point of the rotated image, find where it came from, by
1314 // performing an inverse rotation (a rotation of -angle) and getting the
1315 // pixel at those coordinates
1317 // GRG: I've taken the (interpolating) test out of the loops, so that
1318 // it is done only once, instead of repeating it for each pixel.
1323 for (int y
= 0; y
< rotated
.GetHeight(); y
++)
1325 for (x
= 0; x
< rotated
.GetWidth(); x
++)
1327 wxRealPoint src
= rotated_point (x
+ x1
, y
+ y1
, cos_angle
, -sin_angle
, p0
);
1329 if (-0.25 < src
.x
&& src
.x
< GetWidth() - 0.75 &&
1330 -0.25 < src
.y
&& src
.y
< GetHeight() - 0.75)
1332 // interpolate using the 4 enclosing grid-points. Those
1333 // points can be obtained using floor and ceiling of the
1334 // exact coordinates of the point
1335 // C.M. 2000-02-17: when the point is near the border, special care is required.
1339 if (0 < src
.x
&& src
.x
< GetWidth() - 1)
1341 x1
= wxCint(floor(src
.x
));
1342 x2
= wxCint(ceil(src
.x
));
1344 else // else means that x is near one of the borders (0 or width-1)
1346 x1
= x2
= wxCint (src
.x
);
1349 if (0 < src
.y
&& src
.y
< GetHeight() - 1)
1351 y1
= wxCint(floor(src
.y
));
1352 y2
= wxCint(ceil(src
.y
));
1356 y1
= y2
= wxCint (src
.y
);
1359 // get four points and the distances (square of the distance,
1360 // for efficiency reasons) for the interpolation formula
1362 // GRG: Do not calculate the points until they are
1363 // really needed -- this way we can calculate
1364 // just one, instead of four, if d1, d2, d3
1365 // or d4 are < gs_Epsilon
1367 const double d1
= (src
.x
- x1
) * (src
.x
- x1
) + (src
.y
- y1
) * (src
.y
- y1
);
1368 const double d2
= (src
.x
- x2
) * (src
.x
- x2
) + (src
.y
- y1
) * (src
.y
- y1
);
1369 const double d3
= (src
.x
- x2
) * (src
.x
- x2
) + (src
.y
- y2
) * (src
.y
- y2
);
1370 const double d4
= (src
.x
- x1
) * (src
.x
- x1
) + (src
.y
- y2
) * (src
.y
- y2
);
1372 // Now interpolate as a weighted average of the four surrounding
1373 // points, where the weights are the distances to each of those points
1375 // If the point is exactly at one point of the grid of the source
1376 // image, then don't interpolate -- just assign the pixel
1378 if (d1
< gs_Epsilon
) // d1,d2,d3,d4 are positive -- no need for abs()
1380 unsigned char *p
= data
[y1
] + (3 * x1
);
1385 else if (d2
< gs_Epsilon
)
1387 unsigned char *p
= data
[y1
] + (3 * x2
);
1392 else if (d3
< gs_Epsilon
)
1394 unsigned char *p
= data
[y2
] + (3 * x2
);
1399 else if (d4
< gs_Epsilon
)
1401 unsigned char *p
= data
[y2
] + (3 * x1
);
1408 // weights for the weighted average are proportional to the inverse of the distance
1409 unsigned char *v1
= data
[y1
] + (3 * x1
);
1410 unsigned char *v2
= data
[y1
] + (3 * x2
);
1411 unsigned char *v3
= data
[y2
] + (3 * x2
);
1412 unsigned char *v4
= data
[y2
] + (3 * x1
);
1414 const double w1
= 1/d1
, w2
= 1/d2
, w3
= 1/d3
, w4
= 1/d4
;
1418 *(dst
++) = (unsigned char)
1419 ( (w1
* *(v1
++) + w2
* *(v2
++) +
1420 w3
* *(v3
++) + w4
* *(v4
++)) /
1421 (w1
+ w2
+ w3
+ w4
) );
1422 *(dst
++) = (unsigned char)
1423 ( (w1
* *(v1
++) + w2
* *(v2
++) +
1424 w3
* *(v3
++) + w4
* *(v4
++)) /
1425 (w1
+ w2
+ w3
+ w4
) );
1426 *(dst
++) = (unsigned char)
1427 ( (w1
* *(v1
++) + w2
* *(v2
++) +
1428 w3
* *(v3
++) + w4
* *(v4
++)) /
1429 (w1
+ w2
+ w3
+ w4
) );
1441 else // not interpolating
1443 for (int y
= 0; y
< rotated
.GetHeight(); y
++)
1445 for (x
= 0; x
< rotated
.GetWidth(); x
++)
1447 wxRealPoint src
= rotated_point (x
+ x1
, y
+ y1
, cos_angle
, -sin_angle
, p0
);
1449 const int xs
= wxCint (src
.x
); // wxCint rounds to the
1450 const int ys
= wxCint (src
.y
); // closest integer
1452 if (0 <= xs
&& xs
< GetWidth() &&
1453 0 <= ys
&& ys
< GetHeight())
1455 unsigned char *p
= data
[ys
] + (3 * xs
);
1475 #endif // wxUSE_IMAGE