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/wfstream.h"
33 #include "wx/module.h"
44 //-----------------------------------------------------------------------------
46 //-----------------------------------------------------------------------------
48 class wxImageRefData
: public wxObjectRefData
56 unsigned char *m_data
;
58 unsigned char m_maskRed
,m_maskGreen
,m_maskBlue
;
62 wxArrayString m_optionNames
;
63 wxArrayString m_optionValues
;
66 wxImageRefData::wxImageRefData()
70 m_data
= (unsigned char*) NULL
;
79 wxImageRefData::~wxImageRefData()
81 if (m_data
&& !m_static
)
85 wxList
wxImage::sm_handlers
;
89 //-----------------------------------------------------------------------------
91 #define M_IMGDATA ((wxImageRefData *)m_refData)
93 IMPLEMENT_DYNAMIC_CLASS(wxImage
, wxObject
)
99 wxImage::wxImage( int width
, int height
)
101 Create( width
, height
);
104 wxImage::wxImage( int width
, int height
, unsigned char* data
, bool static_data
)
106 Create( width
, height
, data
, static_data
);
109 wxImage::wxImage( const wxString
& name
, long type
)
111 LoadFile( name
, type
);
114 wxImage::wxImage( const wxString
& name
, const wxString
& mimetype
)
116 LoadFile( name
, mimetype
);
120 wxImage::wxImage( wxInputStream
& stream
, long type
)
122 LoadFile( stream
, type
);
125 wxImage::wxImage( wxInputStream
& stream
, const wxString
& mimetype
)
127 LoadFile( stream
, mimetype
);
129 #endif // wxUSE_STREAMS
131 wxImage::wxImage( const wxImage
& image
)
136 wxImage::wxImage( const wxImage
* image
)
138 if (image
) Ref(*image
);
141 void wxImage::Create( int width
, int height
)
145 m_refData
= new wxImageRefData();
147 M_IMGDATA
->m_data
= (unsigned char *) malloc( width
*height
*3 );
148 if (M_IMGDATA
->m_data
)
150 for (int l
= 0; l
< width
*height
*3; l
++) M_IMGDATA
->m_data
[l
] = 0;
152 M_IMGDATA
->m_width
= width
;
153 M_IMGDATA
->m_height
= height
;
154 M_IMGDATA
->m_ok
= TRUE
;
162 void wxImage::Create( int width
, int height
, unsigned char* data
, bool static_data
)
166 m_refData
= new wxImageRefData();
168 M_IMGDATA
->m_data
= data
;
169 if (M_IMGDATA
->m_data
)
171 M_IMGDATA
->m_width
= width
;
172 M_IMGDATA
->m_height
= height
;
173 M_IMGDATA
->m_ok
= TRUE
;
174 M_IMGDATA
->m_static
= static_data
;
182 void wxImage::Destroy()
187 wxImage
wxImage::Copy() const
191 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
193 image
.Create( M_IMGDATA
->m_width
, M_IMGDATA
->m_height
);
195 char unsigned *data
= image
.GetData();
197 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
199 if (M_IMGDATA
->m_hasMask
)
200 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
202 memcpy( data
, GetData(), M_IMGDATA
->m_width
*M_IMGDATA
->m_height
*3 );
207 wxImage
wxImage::Scale( int width
, int height
) const
211 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
213 wxCHECK_MSG( (width
> 0) && (height
> 0), image
, wxT("invalid image size") );
215 image
.Create( width
, height
);
217 char unsigned *data
= image
.GetData();
219 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
221 if (M_IMGDATA
->m_hasMask
)
222 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
224 long old_height
= M_IMGDATA
->m_height
;
225 long old_width
= M_IMGDATA
->m_width
;
227 char unsigned *source_data
= M_IMGDATA
->m_data
;
228 char unsigned *target_data
= data
;
230 for (long j
= 0; j
< height
; j
++)
232 long y_offset
= (j
* old_height
/ height
) * old_width
;
234 for (long i
= 0; i
< width
; i
++)
237 source_data
+ 3*(y_offset
+ ((i
* old_width
)/ width
)),
246 wxImage
wxImage::Rotate90( bool clockwise
) const
250 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
252 image
.Create( M_IMGDATA
->m_height
, M_IMGDATA
->m_width
);
254 char unsigned *data
= image
.GetData();
256 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
258 if (M_IMGDATA
->m_hasMask
)
259 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
261 long height
= M_IMGDATA
->m_height
;
262 long width
= M_IMGDATA
->m_width
;
264 char unsigned *source_data
= M_IMGDATA
->m_data
;
265 char unsigned *target_data
;
267 for (long j
= 0; j
< height
; j
++)
269 for (long i
= 0; i
< width
; i
++)
272 target_data
= data
+ (((i
+1)*height
) - j
- 1)*3;
274 target_data
= data
+ ((height
*(width
-1)) + j
- (i
*height
))*3;
275 memcpy( target_data
, source_data
, 3 );
283 wxImage
wxImage::Mirror( bool horizontally
) const
287 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
289 image
.Create( M_IMGDATA
->m_width
, M_IMGDATA
->m_height
);
291 char unsigned *data
= image
.GetData();
293 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
295 if (M_IMGDATA
->m_hasMask
)
296 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
298 long height
= M_IMGDATA
->m_height
;
299 long width
= M_IMGDATA
->m_width
;
301 char unsigned *source_data
= M_IMGDATA
->m_data
;
302 char unsigned *target_data
;
306 for (long j
= 0; j
< height
; j
++)
309 target_data
= data
-3;
310 for (long i
= 0; i
< width
; i
++)
312 memcpy( target_data
, source_data
, 3 );
320 for (long i
= 0; i
< height
; i
++)
322 target_data
= data
+ 3*width
*(height
-1-i
);
323 memcpy( target_data
, source_data
, (size_t)3*width
);
324 source_data
+= 3*width
;
331 wxImage
wxImage::GetSubImage( const wxRect
&rect
) const
335 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
337 wxCHECK_MSG( (rect
.GetLeft()>=0) && (rect
.GetTop()>=0) && (rect
.GetRight()<=GetWidth()) && (rect
.GetBottom()<=GetHeight()),
338 image
, wxT("invalid subimage size") );
340 int subwidth
=rect
.GetWidth();
341 const int subheight
=rect
.GetHeight();
343 image
.Create( subwidth
, subheight
);
345 char unsigned *subdata
= image
.GetData(), *data
=GetData();
347 wxCHECK_MSG( subdata
, image
, wxT("unable to create image") );
349 if (M_IMGDATA
->m_hasMask
)
350 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
352 const int subleft
=3*rect
.GetLeft();
353 const int width
=3*GetWidth();
356 data
+=rect
.GetTop()*width
+subleft
;
358 for (long j
= 0; j
< subheight
; ++j
)
360 memcpy( subdata
, data
, subwidth
);
368 void wxImage::Paste( const wxImage
&image
, int x
, int y
)
370 wxCHECK_RET( Ok(), wxT("invalid image") );
371 wxCHECK_RET( image
.Ok(), wxT("invalid image") );
375 int width
= image
.GetWidth();
376 int height
= image
.GetHeight();
389 if ((x
+xx
)+width
> M_IMGDATA
->m_width
)
390 width
= M_IMGDATA
->m_width
- (x
+xx
);
391 if ((y
+yy
)+height
> M_IMGDATA
->m_height
)
392 height
= M_IMGDATA
->m_height
- (y
+yy
);
394 if (width
< 1) return;
395 if (height
< 1) return;
397 if ((!HasMask() && !image
.HasMask()) ||
398 ((HasMask() && image
.HasMask() &&
399 (GetMaskRed()==image
.GetMaskRed()) &&
400 (GetMaskGreen()==image
.GetMaskGreen()) &&
401 (GetMaskBlue()==image
.GetMaskBlue()))))
404 unsigned char* source_data
= image
.GetData() + xx
*3 + yy
*3*image
.GetWidth();
405 int source_step
= image
.GetWidth()*3;
407 unsigned char* target_data
= GetData() + (x
+xx
)*3 + (y
+yy
)*3*M_IMGDATA
->m_width
;
408 int target_step
= M_IMGDATA
->m_width
*3;
409 for (int j
= 0; j
< height
; j
++)
411 memcpy( target_data
, source_data
, width
);
412 source_data
+= source_step
;
413 target_data
+= target_step
;
418 if (!HasMask() && image
.HasMask())
420 unsigned char r
= image
.GetMaskRed();
421 unsigned char g
= image
.GetMaskGreen();
422 unsigned char b
= image
.GetMaskBlue();
425 unsigned char* source_data
= image
.GetData() + xx
*3 + yy
*3*image
.GetWidth();
426 int source_step
= image
.GetWidth()*3;
428 unsigned char* target_data
= GetData() + (x
+xx
)*3 + (y
+yy
)*3*M_IMGDATA
->m_width
;
429 int target_step
= M_IMGDATA
->m_width
*3;
431 for (int j
= 0; j
< height
; j
++)
433 for (int i
= 0; i
< width
; i
+=3)
435 if ((source_data
[i
] != r
) &&
436 (source_data
[i
+1] != g
) &&
437 (source_data
[i
+2] != b
))
439 memcpy( target_data
+i
, source_data
+i
, 3 );
442 source_data
+= source_step
;
443 target_data
+= target_step
;
448 void wxImage::Replace( unsigned char r1
, unsigned char g1
, unsigned char b1
,
449 unsigned char r2
, unsigned char g2
, unsigned char b2
)
451 wxCHECK_RET( Ok(), wxT("invalid image") );
453 char unsigned *data
= GetData();
455 const int w
= GetWidth();
456 const int h
= GetHeight();
458 for (int j
= 0; j
< h
; j
++)
459 for (int i
= 0; i
< w
; i
++)
461 if ((data
[0] == r1
) && (data
[1] == g1
) && (data
[2] == b1
))
471 wxImage
wxImage::ConvertToMono( unsigned char r
, unsigned char g
, unsigned char b
) const
475 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
477 image
.Create( M_IMGDATA
->m_width
, M_IMGDATA
->m_height
);
479 char unsigned *data
= image
.GetData();
481 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
483 if (M_IMGDATA
->m_hasMask
)
485 if (M_IMGDATA
->m_maskRed
== r
&& M_IMGDATA
->m_maskGreen
== g
&&
486 M_IMGDATA
->m_maskBlue
== b
)
487 image
.SetMaskColour( 255, 255, 255 );
489 image
.SetMaskColour( 0, 0, 0 );
492 long size
= M_IMGDATA
->m_height
* M_IMGDATA
->m_width
;
494 char unsigned *srcd
= M_IMGDATA
->m_data
;
495 char unsigned *tard
= image
.GetData();
497 for ( long i
= 0; i
< size
; i
++, srcd
+= 3, tard
+= 3 )
499 if (srcd
[0] == r
&& srcd
[1] == g
&& srcd
[2] == b
)
500 tard
[0] = tard
[1] = tard
[2] = 255;
502 tard
[0] = tard
[1] = tard
[2] = 0;
508 void wxImage::SetRGB( int x
, int y
, unsigned char r
, unsigned char g
, unsigned char b
)
510 wxCHECK_RET( Ok(), wxT("invalid image") );
512 int w
= M_IMGDATA
->m_width
;
513 int h
= M_IMGDATA
->m_height
;
515 wxCHECK_RET( (x
>=0) && (y
>=0) && (x
<w
) && (y
<h
), wxT("invalid image index") );
517 long pos
= (y
* w
+ x
) * 3;
519 M_IMGDATA
->m_data
[ pos
] = r
;
520 M_IMGDATA
->m_data
[ pos
+1 ] = g
;
521 M_IMGDATA
->m_data
[ pos
+2 ] = b
;
524 unsigned char wxImage::GetRed( int x
, int y
) const
526 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
528 int w
= M_IMGDATA
->m_width
;
529 int h
= M_IMGDATA
->m_height
;
531 wxCHECK_MSG( (x
>=0) && (y
>=0) && (x
<w
) && (y
<h
), 0, wxT("invalid image index") );
533 long pos
= (y
* w
+ x
) * 3;
535 return M_IMGDATA
->m_data
[pos
];
538 unsigned char wxImage::GetGreen( int x
, int y
) const
540 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
542 int w
= M_IMGDATA
->m_width
;
543 int h
= M_IMGDATA
->m_height
;
545 wxCHECK_MSG( (x
>=0) && (y
>=0) && (x
<w
) && (y
<h
), 0, wxT("invalid image index") );
547 long pos
= (y
* w
+ x
) * 3;
549 return M_IMGDATA
->m_data
[pos
+1];
552 unsigned char wxImage::GetBlue( int x
, int y
) const
554 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
556 int w
= M_IMGDATA
->m_width
;
557 int h
= M_IMGDATA
->m_height
;
559 wxCHECK_MSG( (x
>=0) && (y
>=0) && (x
<w
) && (y
<h
), 0, wxT("invalid image index") );
561 long pos
= (y
* w
+ x
) * 3;
563 return M_IMGDATA
->m_data
[pos
+2];
566 bool wxImage::Ok() const
568 // image of 0 width or height can't be considered ok - at least because it
569 // causes crashes in ConvertToBitmap() if we don't catch it in time
570 wxImageRefData
*data
= M_IMGDATA
;
571 return data
&& data
->m_ok
&& data
->m_width
&& data
->m_height
;
574 char unsigned *wxImage::GetData() const
576 wxCHECK_MSG( Ok(), (char unsigned *)NULL
, wxT("invalid image") );
578 return M_IMGDATA
->m_data
;
581 void wxImage::SetData( char unsigned *data
)
583 wxCHECK_RET( Ok(), wxT("invalid image") );
585 wxImageRefData
*newRefData
= new wxImageRefData();
587 newRefData
->m_width
= M_IMGDATA
->m_width
;
588 newRefData
->m_height
= M_IMGDATA
->m_height
;
589 newRefData
->m_data
= data
;
590 newRefData
->m_ok
= TRUE
;
591 newRefData
->m_maskRed
= M_IMGDATA
->m_maskRed
;
592 newRefData
->m_maskGreen
= M_IMGDATA
->m_maskGreen
;
593 newRefData
->m_maskBlue
= M_IMGDATA
->m_maskBlue
;
594 newRefData
->m_hasMask
= M_IMGDATA
->m_hasMask
;
598 m_refData
= newRefData
;
601 void wxImage::SetData( char unsigned *data
, int new_width
, int new_height
)
603 wxImageRefData
*newRefData
= new wxImageRefData();
607 newRefData
->m_width
= new_width
;
608 newRefData
->m_height
= new_height
;
609 newRefData
->m_data
= data
;
610 newRefData
->m_ok
= TRUE
;
611 newRefData
->m_maskRed
= M_IMGDATA
->m_maskRed
;
612 newRefData
->m_maskGreen
= M_IMGDATA
->m_maskGreen
;
613 newRefData
->m_maskBlue
= M_IMGDATA
->m_maskBlue
;
614 newRefData
->m_hasMask
= M_IMGDATA
->m_hasMask
;
618 newRefData
->m_width
= new_width
;
619 newRefData
->m_height
= new_height
;
620 newRefData
->m_data
= data
;
621 newRefData
->m_ok
= TRUE
;
626 m_refData
= newRefData
;
629 void wxImage::SetMaskColour( unsigned char r
, unsigned char g
, unsigned char b
)
631 wxCHECK_RET( Ok(), wxT("invalid image") );
633 M_IMGDATA
->m_maskRed
= r
;
634 M_IMGDATA
->m_maskGreen
= g
;
635 M_IMGDATA
->m_maskBlue
= b
;
636 M_IMGDATA
->m_hasMask
= TRUE
;
639 unsigned char wxImage::GetMaskRed() const
641 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
643 return M_IMGDATA
->m_maskRed
;
646 unsigned char wxImage::GetMaskGreen() const
648 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
650 return M_IMGDATA
->m_maskGreen
;
653 unsigned char wxImage::GetMaskBlue() const
655 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
657 return M_IMGDATA
->m_maskBlue
;
660 void wxImage::SetMask( bool mask
)
662 wxCHECK_RET( Ok(), wxT("invalid image") );
664 M_IMGDATA
->m_hasMask
= mask
;
667 bool wxImage::HasMask() const
669 wxCHECK_MSG( Ok(), FALSE
, wxT("invalid image") );
671 return M_IMGDATA
->m_hasMask
;
674 int wxImage::GetWidth() const
676 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
678 return M_IMGDATA
->m_width
;
681 int wxImage::GetHeight() const
683 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
685 return M_IMGDATA
->m_height
;
690 bool wxImage::HasPalette() const
695 return M_IMGDATA
->m_palette
.Ok();
698 const wxPalette
& wxImage::GetPalette() const
700 wxCHECK_MSG( Ok(), wxNullPalette
, wxT("invalid image") );
702 return M_IMGDATA
->m_palette
;
705 void wxImage::SetPalette(const wxPalette
& palette
)
707 wxCHECK_RET( Ok(), wxT("invalid image") );
709 M_IMGDATA
->m_palette
= palette
;
712 // Option functions (arbitrary name/value mapping)
713 void wxImage::SetOption(const wxString
& name
, const wxString
& value
)
715 wxCHECK_RET( Ok(), wxT("invalid image") );
717 int idx
= M_IMGDATA
->m_optionNames
.Index(name
, FALSE
);
718 if (idx
== wxNOT_FOUND
)
720 M_IMGDATA
->m_optionNames
.Add(name
);
721 M_IMGDATA
->m_optionValues
.Add(value
);
725 M_IMGDATA
->m_optionNames
[idx
] = name
;
726 M_IMGDATA
->m_optionValues
[idx
] = value
;
730 void wxImage::SetOption(const wxString
& name
, int value
)
733 valStr
.Printf(wxT("%d"), value
);
734 SetOption(name
, valStr
);
737 wxString
wxImage::GetOption(const wxString
& name
) const
739 wxCHECK_MSG( Ok(), wxEmptyString
, wxT("invalid image") );
741 int idx
= M_IMGDATA
->m_optionNames
.Index(name
, FALSE
);
742 if (idx
== wxNOT_FOUND
)
743 return wxEmptyString
;
745 return M_IMGDATA
->m_optionValues
[idx
];
748 int wxImage::GetOptionInt(const wxString
& name
) const
750 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
752 return wxAtoi(GetOption(name
));
755 bool wxImage::HasOption(const wxString
& name
) const
757 wxCHECK_MSG( Ok(), FALSE
, wxT("invalid image") );
759 return (M_IMGDATA
->m_optionNames
.Index(name
, FALSE
) != wxNOT_FOUND
);
762 bool wxImage::LoadFile( const wxString
& filename
, long type
)
765 if (wxFileExists(filename
))
767 wxFileInputStream
stream(filename
);
768 wxBufferedInputStream
bstream( stream
);
769 return LoadFile(bstream
, type
);
773 wxLogError( _("Can't load image from file '%s': file does not exist."), filename
.c_str() );
777 #else // !wxUSE_STREAMS
779 #endif // wxUSE_STREAMS
782 bool wxImage::LoadFile( const wxString
& filename
, const wxString
& mimetype
)
785 if (wxFileExists(filename
))
787 wxFileInputStream
stream(filename
);
788 wxBufferedInputStream
bstream( stream
);
789 return LoadFile(bstream
, mimetype
);
793 wxLogError( _("Can't load image from file '%s': file does not exist."), filename
.c_str() );
797 #else // !wxUSE_STREAMS
799 #endif // wxUSE_STREAMS
802 bool wxImage::SaveFile( const wxString
& filename
, int type
)
805 wxFileOutputStream
stream(filename
);
807 if ( stream
.LastError() == wxStream_NOERROR
)
809 wxBufferedOutputStream
bstream( stream
);
810 return SaveFile(bstream
, type
);
812 #endif // wxUSE_STREAMS
817 bool wxImage::SaveFile( const wxString
& filename
, const wxString
& mimetype
)
820 wxFileOutputStream
stream(filename
);
822 if ( stream
.LastError() == wxStream_NOERROR
)
824 wxBufferedOutputStream
bstream( stream
);
825 return SaveFile(bstream
, mimetype
);
827 #endif // wxUSE_STREAMS
832 bool wxImage::CanRead( const wxString
&name
)
835 wxFileInputStream
stream(name
);
836 return CanRead(stream
);
844 bool wxImage::CanRead( wxInputStream
&stream
)
846 wxList
&list
=GetHandlers();
848 for ( wxList::Node
*node
= list
.GetFirst(); node
; node
= node
->GetNext() )
850 wxImageHandler
*handler
=(wxImageHandler
*)node
->GetData();
851 if (handler
->CanRead( stream
))
858 bool wxImage::LoadFile( wxInputStream
& stream
, long type
)
862 m_refData
= new wxImageRefData
;
864 wxImageHandler
*handler
;
866 if (type
==wxBITMAP_TYPE_ANY
)
868 wxList
&list
=GetHandlers();
870 for ( wxList::Node
*node
= list
.GetFirst(); node
; node
= node
->GetNext() )
872 handler
=(wxImageHandler
*)node
->GetData();
873 if (handler
->CanRead( stream
))
874 return handler
->LoadFile( this, stream
);
878 wxLogWarning( _("No handler found for image type.") );
882 handler
= FindHandler(type
);
886 wxLogWarning( _("No image handler for type %d defined."), type
);
891 return handler
->LoadFile( this, stream
);
894 bool wxImage::LoadFile( wxInputStream
& stream
, const wxString
& mimetype
)
898 m_refData
= new wxImageRefData
;
900 wxImageHandler
*handler
= FindHandlerMime(mimetype
);
904 wxLogWarning( _("No image handler for type %s defined."), mimetype
.GetData() );
909 return handler
->LoadFile( this, stream
);
912 bool wxImage::SaveFile( wxOutputStream
& stream
, int type
)
914 wxCHECK_MSG( Ok(), FALSE
, wxT("invalid image") );
916 wxImageHandler
*handler
= FindHandler(type
);
920 wxLogWarning( _("No image handler for type %d defined."), type
);
925 return handler
->SaveFile( this, stream
);
928 bool wxImage::SaveFile( wxOutputStream
& stream
, const wxString
& mimetype
)
930 wxCHECK_MSG( Ok(), FALSE
, wxT("invalid image") );
932 wxImageHandler
*handler
= FindHandlerMime(mimetype
);
936 wxLogWarning( _("No image handler for type %s defined."), mimetype
.GetData() );
941 return handler
->SaveFile( this, stream
);
943 #endif // wxUSE_STREAMS
945 void wxImage::AddHandler( wxImageHandler
*handler
)
947 // make sure that the memory will be freed at the program end
948 sm_handlers
.DeleteContents(TRUE
);
950 sm_handlers
.Append( handler
);
953 void wxImage::InsertHandler( wxImageHandler
*handler
)
955 // make sure that the memory will be freed at the program end
956 sm_handlers
.DeleteContents(TRUE
);
958 sm_handlers
.Insert( handler
);
961 bool wxImage::RemoveHandler( const wxString
& name
)
963 wxImageHandler
*handler
= FindHandler(name
);
966 sm_handlers
.DeleteObject(handler
);
973 wxImageHandler
*wxImage::FindHandler( const wxString
& name
)
975 wxNode
*node
= sm_handlers
.First();
978 wxImageHandler
*handler
= (wxImageHandler
*)node
->Data();
979 if (handler
->GetName().Cmp(name
) == 0) return handler
;
983 return (wxImageHandler
*)NULL
;
986 wxImageHandler
*wxImage::FindHandler( const wxString
& extension
, long bitmapType
)
988 wxNode
*node
= sm_handlers
.First();
991 wxImageHandler
*handler
= (wxImageHandler
*)node
->Data();
992 if ( (handler
->GetExtension().Cmp(extension
) == 0) &&
993 (bitmapType
== -1 || handler
->GetType() == bitmapType
) )
997 return (wxImageHandler
*)NULL
;
1000 wxImageHandler
*wxImage::FindHandler( long bitmapType
)
1002 wxNode
*node
= sm_handlers
.First();
1005 wxImageHandler
*handler
= (wxImageHandler
*)node
->Data();
1006 if (handler
->GetType() == bitmapType
) return handler
;
1007 node
= node
->Next();
1012 wxImageHandler
*wxImage::FindHandlerMime( const wxString
& mimetype
)
1014 wxNode
*node
= sm_handlers
.First();
1017 wxImageHandler
*handler
= (wxImageHandler
*)node
->Data();
1018 if (handler
->GetMimeType().IsSameAs(mimetype
, FALSE
)) return handler
;
1019 node
= node
->Next();
1024 void wxImage::InitStandardHandlers()
1026 AddHandler( new wxBMPHandler
);
1029 void wxImage::CleanUpHandlers()
1031 wxNode
*node
= sm_handlers
.First();
1034 wxImageHandler
*handler
= (wxImageHandler
*)node
->Data();
1035 wxNode
*next
= node
->Next();
1042 //-----------------------------------------------------------------------------
1044 //-----------------------------------------------------------------------------
1046 IMPLEMENT_ABSTRACT_CLASS(wxImageHandler
,wxObject
)
1049 bool wxImageHandler::LoadFile( wxImage
*WXUNUSED(image
), wxInputStream
& WXUNUSED(stream
), bool WXUNUSED(verbose
), int WXUNUSED(index
) )
1054 bool wxImageHandler::SaveFile( wxImage
*WXUNUSED(image
), wxOutputStream
& WXUNUSED(stream
), bool WXUNUSED(verbose
) )
1059 int wxImageHandler::GetImageCount( wxInputStream
& WXUNUSED(stream
) )
1064 bool wxImageHandler::CanRead( const wxString
& name
)
1066 if (wxFileExists(name
))
1068 wxFileInputStream
stream(name
);
1069 return CanRead(stream
);
1073 wxLogError( _("Can't check image format of file '%s': file does not exist."), name
.c_str() );
1080 #endif // wxUSE_STREAMS
1084 //-----------------------------------------------------------------------------
1085 // wxBitmap convertion routines
1086 //-----------------------------------------------------------------------------
1091 wxBitmap
wxImage::ConvertToMonoBitmap( unsigned char red
, unsigned char green
, unsigned char blue
) const
1093 wxImage mono
= this->ConvertToMono( red
, green
, blue
);
1094 wxBitmap
bitmap( mono
, 1 );
1099 wxBitmap
wxImage::ConvertToBitmap() const
1101 wxBitmap
bitmap( *this );
1105 wxImage::wxImage( const wxBitmap
&bitmap
)
1107 *this = bitmap
.ConvertToImage();
1114 // A module to allow wxImage initialization/cleanup
1115 // without calling these functions from app.cpp or from
1116 // the user's application.
1118 class wxImageModule
: public wxModule
1120 DECLARE_DYNAMIC_CLASS(wxImageModule
)
1123 bool OnInit() { wxImage::InitStandardHandlers(); return TRUE
; };
1124 void OnExit() { wxImage::CleanUpHandlers(); };
1127 IMPLEMENT_DYNAMIC_CLASS(wxImageModule
, wxModule
)
1130 //-----------------------------------------------------------------------------
1133 // Counts and returns the number of different colours. Optionally stops
1134 // when it exceeds 'stopafter' different colours. This is useful, for
1135 // example, to see if the image can be saved as 8-bit (256 colour or
1136 // less, in this case it would be invoked as CountColours(256)). Default
1137 // value for stopafter is -1 (don't care).
1139 unsigned long wxImage::CountColours( unsigned long stopafter
)
1143 unsigned char r
, g
, b
;
1145 unsigned long size
, nentries
, key
;
1148 size
= GetWidth() * GetHeight();
1151 for (unsigned long j
= 0; (j
< size
) && (nentries
<= stopafter
) ; j
++)
1156 key
= (r
<< 16) | (g
<< 8) | b
;
1158 if (h
.Get(key
) == NULL
)
1170 // Computes the histogram of the image and fills a hash table, indexed
1171 // with integer keys built as 0xRRGGBB, containing wxHNode objects. Each
1172 // wxHNode contains an 'index' (useful to build a palette with the image
1173 // colours) and a 'value', which is the number of pixels in the image with
1176 unsigned long wxImage::ComputeHistogram( wxHashTable
&h
)
1178 unsigned char r
, g
, b
;
1180 unsigned long size
, nentries
, key
;
1184 size
= GetWidth() * GetHeight();
1187 for (unsigned long j
= 0; j
< size
; j
++)
1192 key
= (r
<< 16) | (g
<< 8) | b
;
1194 hnode
= (wxHNode
*) h
.Get(key
);
1200 hnode
= new wxHNode();
1201 hnode
->index
= nentries
++;
1204 h
.Put(key
, (wxObject
*)hnode
);
1212 * Rotation code by Carlos Moreno
1215 // GRG: I've removed wxRotationPoint - we already have wxRealPoint which
1216 // does exactly the same thing. And I also got rid of wxRotationPixel
1217 // bacause of potential problems in architectures where alignment
1218 // is an issue, so I had to rewrite parts of the code.
1220 static const double gs_Epsilon
= 1e-10;
1222 static inline int wxCint (double x
)
1224 return (x
> 0) ? (int) (x
+ 0.5) : (int) (x
- 0.5);
1228 // Auxiliary function to rotate a point (x,y) with respect to point p0
1229 // make it inline and use a straight return to facilitate optimization
1230 // also, the function receives the sine and cosine of the angle to avoid
1231 // repeating the time-consuming calls to these functions -- sin/cos can
1232 // be computed and stored in the calling function.
1234 inline wxRealPoint
rotated_point (const wxRealPoint
& p
, double cos_angle
, double sin_angle
, const wxRealPoint
& p0
)
1236 return wxRealPoint (p0
.x
+ (p
.x
- p0
.x
) * cos_angle
- (p
.y
- p0
.y
) * sin_angle
,
1237 p0
.y
+ (p
.y
- p0
.y
) * cos_angle
+ (p
.x
- p0
.x
) * sin_angle
);
1240 inline wxRealPoint
rotated_point (double x
, double y
, double cos_angle
, double sin_angle
, const wxRealPoint
& p0
)
1242 return rotated_point (wxRealPoint(x
,y
), cos_angle
, sin_angle
, p0
);
1245 wxImage
wxImage::Rotate(double angle
, const wxPoint
& centre_of_rotation
, bool interpolating
, wxPoint
* offset_after_rotation
) const
1248 angle
= -angle
; // screen coordinates are a mirror image of "real" coordinates
1250 // Create pointer-based array to accelerate access to wxImage's data
1251 unsigned char ** data
= new unsigned char * [GetHeight()];
1253 data
[0] = GetData();
1255 for (i
= 1; i
< GetHeight(); i
++)
1256 data
[i
] = data
[i
- 1] + (3 * GetWidth());
1258 // precompute coefficients for rotation formula
1259 // (sine and cosine of the angle)
1260 const double cos_angle
= cos(angle
);
1261 const double sin_angle
= sin(angle
);
1263 // Create new Image to store the result
1264 // First, find rectangle that covers the rotated image; to do that,
1265 // rotate the four corners
1267 const wxRealPoint
p0(centre_of_rotation
.x
, centre_of_rotation
.y
);
1269 wxRealPoint p1
= rotated_point (0, 0, cos_angle
, sin_angle
, p0
);
1270 wxRealPoint p2
= rotated_point (0, GetHeight(), cos_angle
, sin_angle
, p0
);
1271 wxRealPoint p3
= rotated_point (GetWidth(), 0, cos_angle
, sin_angle
, p0
);
1272 wxRealPoint p4
= rotated_point (GetWidth(), GetHeight(), cos_angle
, sin_angle
, p0
);
1274 int x1
= (int) floor (wxMin (wxMin(p1
.x
, p2
.x
), wxMin(p3
.x
, p4
.x
)));
1275 int y1
= (int) floor (wxMin (wxMin(p1
.y
, p2
.y
), wxMin(p3
.y
, p4
.y
)));
1276 int x2
= (int) ceil (wxMax (wxMax(p1
.x
, p2
.x
), wxMax(p3
.x
, p4
.x
)));
1277 int y2
= (int) ceil (wxMax (wxMax(p1
.y
, p2
.y
), wxMax(p3
.y
, p4
.y
)));
1279 wxImage
rotated (x2
- x1
+ 1, y2
- y1
+ 1);
1281 if (offset_after_rotation
!= NULL
)
1283 *offset_after_rotation
= wxPoint (x1
, y1
);
1286 // GRG: The rotated (destination) image is always accessed
1287 // sequentially, so there is no need for a pointer-based
1288 // array here (and in fact it would be slower).
1290 unsigned char * dst
= rotated
.GetData();
1292 // GRG: if the original image has a mask, use its RGB values
1293 // as the blank pixel, else, fall back to default (black).
1295 unsigned char blank_r
= 0;
1296 unsigned char blank_g
= 0;
1297 unsigned char blank_b
= 0;
1301 blank_r
= GetMaskRed();
1302 blank_g
= GetMaskGreen();
1303 blank_b
= GetMaskBlue();
1304 rotated
.SetMaskColour( blank_r
, blank_g
, blank_b
);
1307 // Now, for each point of the rotated image, find where it came from, by
1308 // performing an inverse rotation (a rotation of -angle) and getting the
1309 // pixel at those coordinates
1311 // GRG: I've taken the (interpolating) test out of the loops, so that
1312 // it is done only once, instead of repeating it for each pixel.
1317 for (int y
= 0; y
< rotated
.GetHeight(); y
++)
1319 for (x
= 0; x
< rotated
.GetWidth(); x
++)
1321 wxRealPoint src
= rotated_point (x
+ x1
, y
+ y1
, cos_angle
, -sin_angle
, p0
);
1323 if (-0.25 < src
.x
&& src
.x
< GetWidth() - 0.75 &&
1324 -0.25 < src
.y
&& src
.y
< GetHeight() - 0.75)
1326 // interpolate using the 4 enclosing grid-points. Those
1327 // points can be obtained using floor and ceiling of the
1328 // exact coordinates of the point
1329 // C.M. 2000-02-17: when the point is near the border, special care is required.
1333 if (0 < src
.x
&& src
.x
< GetWidth() - 1)
1335 x1
= wxCint(floor(src
.x
));
1336 x2
= wxCint(ceil(src
.x
));
1338 else // else means that x is near one of the borders (0 or width-1)
1340 x1
= x2
= wxCint (src
.x
);
1343 if (0 < src
.y
&& src
.y
< GetHeight() - 1)
1345 y1
= wxCint(floor(src
.y
));
1346 y2
= wxCint(ceil(src
.y
));
1350 y1
= y2
= wxCint (src
.y
);
1353 // get four points and the distances (square of the distance,
1354 // for efficiency reasons) for the interpolation formula
1356 // GRG: Do not calculate the points until they are
1357 // really needed -- this way we can calculate
1358 // just one, instead of four, if d1, d2, d3
1359 // or d4 are < gs_Epsilon
1361 const double d1
= (src
.x
- x1
) * (src
.x
- x1
) + (src
.y
- y1
) * (src
.y
- y1
);
1362 const double d2
= (src
.x
- x2
) * (src
.x
- x2
) + (src
.y
- y1
) * (src
.y
- y1
);
1363 const double d3
= (src
.x
- x2
) * (src
.x
- x2
) + (src
.y
- y2
) * (src
.y
- y2
);
1364 const double d4
= (src
.x
- x1
) * (src
.x
- x1
) + (src
.y
- y2
) * (src
.y
- y2
);
1366 // Now interpolate as a weighted average of the four surrounding
1367 // points, where the weights are the distances to each of those points
1369 // If the point is exactly at one point of the grid of the source
1370 // image, then don't interpolate -- just assign the pixel
1372 if (d1
< gs_Epsilon
) // d1,d2,d3,d4 are positive -- no need for abs()
1374 unsigned char *p
= data
[y1
] + (3 * x1
);
1379 else if (d2
< gs_Epsilon
)
1381 unsigned char *p
= data
[y1
] + (3 * x2
);
1386 else if (d3
< gs_Epsilon
)
1388 unsigned char *p
= data
[y2
] + (3 * x2
);
1393 else if (d4
< gs_Epsilon
)
1395 unsigned char *p
= data
[y2
] + (3 * x1
);
1402 // weights for the weighted average are proportional to the inverse of the distance
1403 unsigned char *v1
= data
[y1
] + (3 * x1
);
1404 unsigned char *v2
= data
[y1
] + (3 * x2
);
1405 unsigned char *v3
= data
[y2
] + (3 * x2
);
1406 unsigned char *v4
= data
[y2
] + (3 * x1
);
1408 const double w1
= 1/d1
, w2
= 1/d2
, w3
= 1/d3
, w4
= 1/d4
;
1412 *(dst
++) = (unsigned char)
1413 ( (w1
* *(v1
++) + w2
* *(v2
++) +
1414 w3
* *(v3
++) + w4
* *(v4
++)) /
1415 (w1
+ w2
+ w3
+ w4
) );
1416 *(dst
++) = (unsigned char)
1417 ( (w1
* *(v1
++) + w2
* *(v2
++) +
1418 w3
* *(v3
++) + w4
* *(v4
++)) /
1419 (w1
+ w2
+ w3
+ w4
) );
1420 *(dst
++) = (unsigned char)
1421 ( (w1
* *(v1
++) + w2
* *(v2
++) +
1422 w3
* *(v3
++) + w4
* *(v4
++)) /
1423 (w1
+ w2
+ w3
+ w4
) );
1435 else // not interpolating
1437 for (int y
= 0; y
< rotated
.GetHeight(); y
++)
1439 for (x
= 0; x
< rotated
.GetWidth(); x
++)
1441 wxRealPoint src
= rotated_point (x
+ x1
, y
+ y1
, cos_angle
, -sin_angle
, p0
);
1443 const int xs
= wxCint (src
.x
); // wxCint rounds to the
1444 const int ys
= wxCint (src
.y
); // closest integer
1446 if (0 <= xs
&& xs
< GetWidth() &&
1447 0 <= ys
&& ys
< GetHeight())
1449 unsigned char *p
= data
[ys
] + (3 * xs
);
1469 #endif // wxUSE_IMAGE