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
);
771 wxLogError(_("Can't open file '%s'"), filename
.c_str());
774 wxInputStream
*stream
= file
->GetStream();
777 wxLogError(_("Can't open stream for file '%s'"), filename
.c_str());
781 return LoadFile(*stream
, type
);
782 #else // !wxUSE_STREAMS
784 #endif // wxUSE_STREAMS
787 bool wxImage::LoadFile( const wxString
& filename
, const wxString
& mimetype
)
790 // We want to use wxFileSystem for virtual FS compatibility
792 wxFSFile
*file
= fsys
.OpenFile(filename
);
795 wxLogError(_("Can't open file '%s'"), filename
.c_str());
798 wxInputStream
*stream
= file
->GetStream();
801 wxLogError(_("Can't open stream for file '%s'"), filename
.c_str());
804 return LoadFile(*stream
, mimetype
);
805 #else // !wxUSE_STREAMS
807 #endif // wxUSE_STREAMS
810 bool wxImage::SaveFile( const wxString
& filename
, int type
)
813 wxFileOutputStream
stream(filename
);
815 if ( stream
.LastError() == wxStream_NOERROR
)
817 wxBufferedOutputStream
bstream( stream
);
818 return SaveFile(bstream
, type
);
820 #endif // wxUSE_STREAMS
825 bool wxImage::SaveFile( const wxString
& filename
, const wxString
& mimetype
)
828 wxFileOutputStream
stream(filename
);
830 if ( stream
.LastError() == wxStream_NOERROR
)
832 wxBufferedOutputStream
bstream( stream
);
833 return SaveFile(bstream
, mimetype
);
835 #endif // wxUSE_STREAMS
840 bool wxImage::CanRead( const wxString
&name
)
843 wxFileInputStream
stream(name
);
844 return CanRead(stream
);
852 bool wxImage::CanRead( wxInputStream
&stream
)
854 wxList
&list
=GetHandlers();
856 for ( wxList::Node
*node
= list
.GetFirst(); node
; node
= node
->GetNext() )
858 wxImageHandler
*handler
=(wxImageHandler
*)node
->GetData();
859 if (handler
->CanRead( stream
))
866 bool wxImage::LoadFile( wxInputStream
& stream
, long type
)
870 m_refData
= new wxImageRefData
;
872 wxImageHandler
*handler
;
874 if (type
==wxBITMAP_TYPE_ANY
)
876 wxList
&list
=GetHandlers();
878 for ( wxList::Node
*node
= list
.GetFirst(); node
; node
= node
->GetNext() )
880 handler
=(wxImageHandler
*)node
->GetData();
881 if (handler
->CanRead( stream
))
882 return handler
->LoadFile( this, stream
);
886 wxLogWarning( _("No handler found for image type.") );
890 handler
= FindHandler(type
);
894 wxLogWarning( _("No image handler for type %d defined."), type
);
899 return handler
->LoadFile( this, stream
);
902 bool wxImage::LoadFile( wxInputStream
& stream
, const wxString
& mimetype
)
906 m_refData
= new wxImageRefData
;
908 wxImageHandler
*handler
= FindHandlerMime(mimetype
);
912 wxLogWarning( _("No image handler for type %s defined."), mimetype
.GetData() );
917 return handler
->LoadFile( this, stream
);
920 bool wxImage::SaveFile( wxOutputStream
& stream
, int type
)
922 wxCHECK_MSG( Ok(), FALSE
, wxT("invalid image") );
924 wxImageHandler
*handler
= FindHandler(type
);
928 wxLogWarning( _("No image handler for type %d defined."), type
);
933 return handler
->SaveFile( this, stream
);
936 bool wxImage::SaveFile( wxOutputStream
& stream
, const wxString
& mimetype
)
938 wxCHECK_MSG( Ok(), FALSE
, wxT("invalid image") );
940 wxImageHandler
*handler
= FindHandlerMime(mimetype
);
944 wxLogWarning( _("No image handler for type %s defined."), mimetype
.GetData() );
949 return handler
->SaveFile( this, stream
);
951 #endif // wxUSE_STREAMS
953 void wxImage::AddHandler( wxImageHandler
*handler
)
955 // make sure that the memory will be freed at the program end
956 sm_handlers
.DeleteContents(TRUE
);
958 sm_handlers
.Append( handler
);
961 void wxImage::InsertHandler( wxImageHandler
*handler
)
963 // make sure that the memory will be freed at the program end
964 sm_handlers
.DeleteContents(TRUE
);
966 sm_handlers
.Insert( handler
);
969 bool wxImage::RemoveHandler( const wxString
& name
)
971 wxImageHandler
*handler
= FindHandler(name
);
974 sm_handlers
.DeleteObject(handler
);
981 wxImageHandler
*wxImage::FindHandler( const wxString
& name
)
983 wxNode
*node
= sm_handlers
.First();
986 wxImageHandler
*handler
= (wxImageHandler
*)node
->Data();
987 if (handler
->GetName().Cmp(name
) == 0) return handler
;
991 return (wxImageHandler
*)NULL
;
994 wxImageHandler
*wxImage::FindHandler( const wxString
& extension
, long bitmapType
)
996 wxNode
*node
= sm_handlers
.First();
999 wxImageHandler
*handler
= (wxImageHandler
*)node
->Data();
1000 if ( (handler
->GetExtension().Cmp(extension
) == 0) &&
1001 (bitmapType
== -1 || handler
->GetType() == bitmapType
) )
1003 node
= node
->Next();
1005 return (wxImageHandler
*)NULL
;
1008 wxImageHandler
*wxImage::FindHandler( long bitmapType
)
1010 wxNode
*node
= sm_handlers
.First();
1013 wxImageHandler
*handler
= (wxImageHandler
*)node
->Data();
1014 if (handler
->GetType() == bitmapType
) return handler
;
1015 node
= node
->Next();
1020 wxImageHandler
*wxImage::FindHandlerMime( const wxString
& mimetype
)
1022 wxNode
*node
= sm_handlers
.First();
1025 wxImageHandler
*handler
= (wxImageHandler
*)node
->Data();
1026 if (handler
->GetMimeType().IsSameAs(mimetype
, FALSE
)) return handler
;
1027 node
= node
->Next();
1032 void wxImage::InitStandardHandlers()
1034 AddHandler(new wxBMPHandler
);
1035 #if !defined(__WXGTK__) && !defined(__WXMOTIF__)
1036 AddHandler(new wxXPMHandler
);
1040 void wxImage::CleanUpHandlers()
1042 wxNode
*node
= sm_handlers
.First();
1045 wxImageHandler
*handler
= (wxImageHandler
*)node
->Data();
1046 wxNode
*next
= node
->Next();
1053 //-----------------------------------------------------------------------------
1055 //-----------------------------------------------------------------------------
1057 IMPLEMENT_ABSTRACT_CLASS(wxImageHandler
,wxObject
)
1060 bool wxImageHandler::LoadFile( wxImage
*WXUNUSED(image
), wxInputStream
& WXUNUSED(stream
), bool WXUNUSED(verbose
), int WXUNUSED(index
) )
1065 bool wxImageHandler::SaveFile( wxImage
*WXUNUSED(image
), wxOutputStream
& WXUNUSED(stream
), bool WXUNUSED(verbose
) )
1070 int wxImageHandler::GetImageCount( wxInputStream
& WXUNUSED(stream
) )
1075 bool wxImageHandler::CanRead( const wxString
& name
)
1077 if (wxFileExists(name
))
1079 wxFileInputStream
stream(name
);
1080 return CanRead(stream
);
1084 wxLogError( _("Can't check image format of file '%s': file does not exist."), name
.c_str() );
1091 #endif // wxUSE_STREAMS
1095 //-----------------------------------------------------------------------------
1096 // wxBitmap convertion routines
1097 //-----------------------------------------------------------------------------
1102 wxBitmap
wxImage::ConvertToMonoBitmap( unsigned char red
, unsigned char green
, unsigned char blue
) const
1104 wxImage mono
= this->ConvertToMono( red
, green
, blue
);
1105 wxBitmap
bitmap( mono
, 1 );
1110 wxBitmap
wxImage::ConvertToBitmap() const
1112 wxBitmap
bitmap( *this );
1116 wxImage::wxImage( const wxBitmap
&bitmap
)
1118 *this = bitmap
.ConvertToImage();
1125 // A module to allow wxImage initialization/cleanup
1126 // without calling these functions from app.cpp or from
1127 // the user's application.
1129 class wxImageModule
: public wxModule
1131 DECLARE_DYNAMIC_CLASS(wxImageModule
)
1134 bool OnInit() { wxImage::InitStandardHandlers(); return TRUE
; };
1135 void OnExit() { wxImage::CleanUpHandlers(); };
1138 IMPLEMENT_DYNAMIC_CLASS(wxImageModule
, wxModule
)
1141 //-----------------------------------------------------------------------------
1144 // Counts and returns the number of different colours. Optionally stops
1145 // when it exceeds 'stopafter' different colours. This is useful, for
1146 // example, to see if the image can be saved as 8-bit (256 colour or
1147 // less, in this case it would be invoked as CountColours(256)). Default
1148 // value for stopafter is -1 (don't care).
1150 unsigned long wxImage::CountColours( unsigned long stopafter
)
1154 unsigned char r
, g
, b
;
1156 unsigned long size
, nentries
, key
;
1159 size
= GetWidth() * GetHeight();
1162 for (unsigned long j
= 0; (j
< size
) && (nentries
<= stopafter
) ; j
++)
1167 key
= (r
<< 16) | (g
<< 8) | b
;
1169 if (h
.Get(key
) == NULL
)
1181 // Computes the histogram of the image and fills a hash table, indexed
1182 // with integer keys built as 0xRRGGBB, containing wxHNode objects. Each
1183 // wxHNode contains an 'index' (useful to build a palette with the image
1184 // colours) and a 'value', which is the number of pixels in the image with
1187 unsigned long wxImage::ComputeHistogram( wxHashTable
&h
)
1189 unsigned char r
, g
, b
;
1191 unsigned long size
, nentries
, key
;
1195 size
= GetWidth() * GetHeight();
1198 for (unsigned long j
= 0; j
< size
; j
++)
1203 key
= (r
<< 16) | (g
<< 8) | b
;
1205 hnode
= (wxHNode
*) h
.Get(key
);
1211 hnode
= new wxHNode();
1212 hnode
->index
= nentries
++;
1215 h
.Put(key
, (wxObject
*)hnode
);
1223 * Rotation code by Carlos Moreno
1226 // GRG: I've removed wxRotationPoint - we already have wxRealPoint which
1227 // does exactly the same thing. And I also got rid of wxRotationPixel
1228 // bacause of potential problems in architectures where alignment
1229 // is an issue, so I had to rewrite parts of the code.
1231 static const double gs_Epsilon
= 1e-10;
1233 static inline int wxCint (double x
)
1235 return (x
> 0) ? (int) (x
+ 0.5) : (int) (x
- 0.5);
1239 // Auxiliary function to rotate a point (x,y) with respect to point p0
1240 // make it inline and use a straight return to facilitate optimization
1241 // also, the function receives the sine and cosine of the angle to avoid
1242 // repeating the time-consuming calls to these functions -- sin/cos can
1243 // be computed and stored in the calling function.
1245 inline wxRealPoint
rotated_point (const wxRealPoint
& p
, double cos_angle
, double sin_angle
, const wxRealPoint
& p0
)
1247 return wxRealPoint (p0
.x
+ (p
.x
- p0
.x
) * cos_angle
- (p
.y
- p0
.y
) * sin_angle
,
1248 p0
.y
+ (p
.y
- p0
.y
) * cos_angle
+ (p
.x
- p0
.x
) * sin_angle
);
1251 inline wxRealPoint
rotated_point (double x
, double y
, double cos_angle
, double sin_angle
, const wxRealPoint
& p0
)
1253 return rotated_point (wxRealPoint(x
,y
), cos_angle
, sin_angle
, p0
);
1256 wxImage
wxImage::Rotate(double angle
, const wxPoint
& centre_of_rotation
, bool interpolating
, wxPoint
* offset_after_rotation
) const
1259 angle
= -angle
; // screen coordinates are a mirror image of "real" coordinates
1261 // Create pointer-based array to accelerate access to wxImage's data
1262 unsigned char ** data
= new unsigned char * [GetHeight()];
1264 data
[0] = GetData();
1266 for (i
= 1; i
< GetHeight(); i
++)
1267 data
[i
] = data
[i
- 1] + (3 * GetWidth());
1269 // precompute coefficients for rotation formula
1270 // (sine and cosine of the angle)
1271 const double cos_angle
= cos(angle
);
1272 const double sin_angle
= sin(angle
);
1274 // Create new Image to store the result
1275 // First, find rectangle that covers the rotated image; to do that,
1276 // rotate the four corners
1278 const wxRealPoint
p0(centre_of_rotation
.x
, centre_of_rotation
.y
);
1280 wxRealPoint p1
= rotated_point (0, 0, cos_angle
, sin_angle
, p0
);
1281 wxRealPoint p2
= rotated_point (0, GetHeight(), cos_angle
, sin_angle
, p0
);
1282 wxRealPoint p3
= rotated_point (GetWidth(), 0, cos_angle
, sin_angle
, p0
);
1283 wxRealPoint p4
= rotated_point (GetWidth(), GetHeight(), cos_angle
, sin_angle
, p0
);
1285 int x1
= (int) floor (wxMin (wxMin(p1
.x
, p2
.x
), wxMin(p3
.x
, p4
.x
)));
1286 int y1
= (int) floor (wxMin (wxMin(p1
.y
, p2
.y
), wxMin(p3
.y
, p4
.y
)));
1287 int x2
= (int) ceil (wxMax (wxMax(p1
.x
, p2
.x
), wxMax(p3
.x
, p4
.x
)));
1288 int y2
= (int) ceil (wxMax (wxMax(p1
.y
, p2
.y
), wxMax(p3
.y
, p4
.y
)));
1290 wxImage
rotated (x2
- x1
+ 1, y2
- y1
+ 1);
1292 if (offset_after_rotation
!= NULL
)
1294 *offset_after_rotation
= wxPoint (x1
, y1
);
1297 // GRG: The rotated (destination) image is always accessed
1298 // sequentially, so there is no need for a pointer-based
1299 // array here (and in fact it would be slower).
1301 unsigned char * dst
= rotated
.GetData();
1303 // GRG: if the original image has a mask, use its RGB values
1304 // as the blank pixel, else, fall back to default (black).
1306 unsigned char blank_r
= 0;
1307 unsigned char blank_g
= 0;
1308 unsigned char blank_b
= 0;
1312 blank_r
= GetMaskRed();
1313 blank_g
= GetMaskGreen();
1314 blank_b
= GetMaskBlue();
1315 rotated
.SetMaskColour( blank_r
, blank_g
, blank_b
);
1318 // Now, for each point of the rotated image, find where it came from, by
1319 // performing an inverse rotation (a rotation of -angle) and getting the
1320 // pixel at those coordinates
1322 // GRG: I've taken the (interpolating) test out of the loops, so that
1323 // it is done only once, instead of repeating it for each pixel.
1328 for (int y
= 0; y
< rotated
.GetHeight(); y
++)
1330 for (x
= 0; x
< rotated
.GetWidth(); x
++)
1332 wxRealPoint src
= rotated_point (x
+ x1
, y
+ y1
, cos_angle
, -sin_angle
, p0
);
1334 if (-0.25 < src
.x
&& src
.x
< GetWidth() - 0.75 &&
1335 -0.25 < src
.y
&& src
.y
< GetHeight() - 0.75)
1337 // interpolate using the 4 enclosing grid-points. Those
1338 // points can be obtained using floor and ceiling of the
1339 // exact coordinates of the point
1340 // C.M. 2000-02-17: when the point is near the border, special care is required.
1344 if (0 < src
.x
&& src
.x
< GetWidth() - 1)
1346 x1
= wxCint(floor(src
.x
));
1347 x2
= wxCint(ceil(src
.x
));
1349 else // else means that x is near one of the borders (0 or width-1)
1351 x1
= x2
= wxCint (src
.x
);
1354 if (0 < src
.y
&& src
.y
< GetHeight() - 1)
1356 y1
= wxCint(floor(src
.y
));
1357 y2
= wxCint(ceil(src
.y
));
1361 y1
= y2
= wxCint (src
.y
);
1364 // get four points and the distances (square of the distance,
1365 // for efficiency reasons) for the interpolation formula
1367 // GRG: Do not calculate the points until they are
1368 // really needed -- this way we can calculate
1369 // just one, instead of four, if d1, d2, d3
1370 // or d4 are < gs_Epsilon
1372 const double d1
= (src
.x
- x1
) * (src
.x
- x1
) + (src
.y
- y1
) * (src
.y
- y1
);
1373 const double d2
= (src
.x
- x2
) * (src
.x
- x2
) + (src
.y
- y1
) * (src
.y
- y1
);
1374 const double d3
= (src
.x
- x2
) * (src
.x
- x2
) + (src
.y
- y2
) * (src
.y
- y2
);
1375 const double d4
= (src
.x
- x1
) * (src
.x
- x1
) + (src
.y
- y2
) * (src
.y
- y2
);
1377 // Now interpolate as a weighted average of the four surrounding
1378 // points, where the weights are the distances to each of those points
1380 // If the point is exactly at one point of the grid of the source
1381 // image, then don't interpolate -- just assign the pixel
1383 if (d1
< gs_Epsilon
) // d1,d2,d3,d4 are positive -- no need for abs()
1385 unsigned char *p
= data
[y1
] + (3 * x1
);
1390 else if (d2
< gs_Epsilon
)
1392 unsigned char *p
= data
[y1
] + (3 * x2
);
1397 else if (d3
< gs_Epsilon
)
1399 unsigned char *p
= data
[y2
] + (3 * x2
);
1404 else if (d4
< gs_Epsilon
)
1406 unsigned char *p
= data
[y2
] + (3 * x1
);
1413 // weights for the weighted average are proportional to the inverse of the distance
1414 unsigned char *v1
= data
[y1
] + (3 * x1
);
1415 unsigned char *v2
= data
[y1
] + (3 * x2
);
1416 unsigned char *v3
= data
[y2
] + (3 * x2
);
1417 unsigned char *v4
= data
[y2
] + (3 * x1
);
1419 const double w1
= 1/d1
, w2
= 1/d2
, w3
= 1/d3
, w4
= 1/d4
;
1423 *(dst
++) = (unsigned char)
1424 ( (w1
* *(v1
++) + w2
* *(v2
++) +
1425 w3
* *(v3
++) + w4
* *(v4
++)) /
1426 (w1
+ w2
+ w3
+ w4
) );
1427 *(dst
++) = (unsigned char)
1428 ( (w1
* *(v1
++) + w2
* *(v2
++) +
1429 w3
* *(v3
++) + w4
* *(v4
++)) /
1430 (w1
+ w2
+ w3
+ w4
) );
1431 *(dst
++) = (unsigned char)
1432 ( (w1
* *(v1
++) + w2
* *(v2
++) +
1433 w3
* *(v3
++) + w4
* *(v4
++)) /
1434 (w1
+ w2
+ w3
+ w4
) );
1446 else // not interpolating
1448 for (int y
= 0; y
< rotated
.GetHeight(); y
++)
1450 for (x
= 0; x
< rotated
.GetWidth(); x
++)
1452 wxRealPoint src
= rotated_point (x
+ x1
, y
+ y1
, cos_angle
, -sin_angle
, p0
);
1454 const int xs
= wxCint (src
.x
); // wxCint rounds to the
1455 const int ys
= wxCint (src
.y
); // closest integer
1457 if (0 <= xs
&& xs
< GetWidth() &&
1458 0 <= ys
&& ys
< GetHeight())
1460 unsigned char *p
= data
[ys
] + (3 * xs
);
1480 #endif // wxUSE_IMAGE