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
;
63 #endif // wxUSE_PALETTE
64 wxArrayString m_optionNames
;
65 wxArrayString m_optionValues
;
68 wxImageRefData::wxImageRefData()
72 m_data
= (unsigned char*) NULL
;
81 wxImageRefData::~wxImageRefData()
83 if (m_data
&& !m_static
)
87 wxList
wxImage::sm_handlers
;
91 //-----------------------------------------------------------------------------
93 #define M_IMGDATA ((wxImageRefData *)m_refData)
95 IMPLEMENT_DYNAMIC_CLASS(wxImage
, wxObject
)
101 wxImage::wxImage( int width
, int height
)
103 Create( width
, height
);
106 wxImage::wxImage( int width
, int height
, unsigned char* data
, bool static_data
)
108 Create( width
, height
, data
, static_data
);
111 wxImage::wxImage( const wxString
& name
, long type
, int index
)
113 LoadFile( name
, type
, index
);
116 wxImage::wxImage( const wxString
& name
, const wxString
& mimetype
, int index
)
118 LoadFile( name
, mimetype
, index
);
122 wxImage::wxImage( wxInputStream
& stream
, long type
, int index
)
124 LoadFile( stream
, type
, index
);
127 wxImage::wxImage( wxInputStream
& stream
, const wxString
& mimetype
, int index
)
129 LoadFile( stream
, mimetype
, index
);
131 #endif // wxUSE_STREAMS
133 wxImage::wxImage( const wxImage
& image
)
139 wxImage::wxImage( const wxImage
* image
)
141 if (image
) Ref(*image
);
144 void wxImage::Create( int width
, int height
)
148 m_refData
= new wxImageRefData();
150 M_IMGDATA
->m_data
= (unsigned char *) malloc( width
*height
*3 );
151 if (M_IMGDATA
->m_data
)
153 for (int l
= 0; l
< width
*height
*3; l
++) M_IMGDATA
->m_data
[l
] = 0;
155 M_IMGDATA
->m_width
= width
;
156 M_IMGDATA
->m_height
= height
;
157 M_IMGDATA
->m_ok
= TRUE
;
165 void wxImage::Create( int width
, int height
, unsigned char* data
, bool static_data
)
169 m_refData
= new wxImageRefData();
171 M_IMGDATA
->m_data
= data
;
172 if (M_IMGDATA
->m_data
)
174 M_IMGDATA
->m_width
= width
;
175 M_IMGDATA
->m_height
= height
;
176 M_IMGDATA
->m_ok
= TRUE
;
177 M_IMGDATA
->m_static
= static_data
;
185 void wxImage::Destroy()
190 wxImage
wxImage::Copy() const
194 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
196 image
.Create( M_IMGDATA
->m_width
, M_IMGDATA
->m_height
);
198 char unsigned *data
= image
.GetData();
200 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
202 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
203 image
.SetMask( M_IMGDATA
->m_hasMask
);
205 memcpy( data
, GetData(), M_IMGDATA
->m_width
*M_IMGDATA
->m_height
*3 );
210 wxImage
wxImage::Scale( int width
, int height
) const
214 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
216 // can't scale to/from 0 size
217 wxCHECK_MSG( (width
> 0) && (height
> 0), image
,
218 wxT("invalid new image size") );
220 long old_height
= M_IMGDATA
->m_height
,
221 old_width
= M_IMGDATA
->m_width
;
222 wxCHECK_MSG( (old_height
> 0) && (old_width
> 0), image
,
223 wxT("invalid old image size") );
225 image
.Create( width
, height
);
227 char unsigned *data
= image
.GetData();
229 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
231 if (M_IMGDATA
->m_hasMask
)
233 image
.SetMaskColour( M_IMGDATA
->m_maskRed
,
234 M_IMGDATA
->m_maskGreen
,
235 M_IMGDATA
->m_maskBlue
);
238 char unsigned *source_data
= M_IMGDATA
->m_data
;
239 char unsigned *target_data
= data
;
242 // This is nonsense, RR.
244 // We do (x, y) -> (x, y)*oldSize/newSize but the valid values of x and y
245 // are from 0 to size-1, hence all decrement the sizes
246 long old_old_width
= old_width
;
251 for ( long j
= 0; j
<= height
; j
++ )
253 // don't crash for images with height == 1
254 long y_offset
= height
? (j
* old_height
/ height
)* old_old_width
: 0;
256 for ( long i
= 0; i
<= width
; i
++ )
258 long x_offset
= width
? (i
* old_width
) / width
: 0;
260 memcpy( target_data
, source_data
+ 3*(y_offset
+ x_offset
), 3 );
265 for (long j
= 0; j
< height
; j
++)
267 long y_offset
= (j
* old_height
/ height
) * old_width
;
269 for (long i
= 0; i
< width
; i
++)
272 source_data
+ 3*(y_offset
+ ((i
* old_width
)/ width
)),
279 // In case this is a cursor, make sure the hotspot is scalled accordingly:
280 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
) )
281 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
,
282 (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X
)*width
)/old_width
);
283 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
) )
284 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
,
285 (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y
)*height
)/old_height
);
290 wxImage
wxImage::Rotate90( bool clockwise
) const
294 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
296 image
.Create( M_IMGDATA
->m_height
, M_IMGDATA
->m_width
);
298 char unsigned *data
= image
.GetData();
300 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
302 if (M_IMGDATA
->m_hasMask
)
303 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
305 long height
= M_IMGDATA
->m_height
;
306 long width
= M_IMGDATA
->m_width
;
308 char unsigned *source_data
= M_IMGDATA
->m_data
;
309 char unsigned *target_data
;
311 for (long j
= 0; j
< height
; j
++)
313 for (long i
= 0; i
< width
; i
++)
316 target_data
= data
+ (((i
+1)*height
) - j
- 1)*3;
318 target_data
= data
+ ((height
*(width
-1)) + j
- (i
*height
))*3;
319 memcpy( target_data
, source_data
, 3 );
327 wxImage
wxImage::Mirror( bool horizontally
) const
331 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
333 image
.Create( M_IMGDATA
->m_width
, M_IMGDATA
->m_height
);
335 char unsigned *data
= image
.GetData();
337 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
339 if (M_IMGDATA
->m_hasMask
)
340 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
342 long height
= M_IMGDATA
->m_height
;
343 long width
= M_IMGDATA
->m_width
;
345 char unsigned *source_data
= M_IMGDATA
->m_data
;
346 char unsigned *target_data
;
350 for (long j
= 0; j
< height
; j
++)
353 target_data
= data
-3;
354 for (long i
= 0; i
< width
; i
++)
356 memcpy( target_data
, source_data
, 3 );
364 for (long i
= 0; i
< height
; i
++)
366 target_data
= data
+ 3*width
*(height
-1-i
);
367 memcpy( target_data
, source_data
, (size_t)3*width
);
368 source_data
+= 3*width
;
375 wxImage
wxImage::GetSubImage( const wxRect
&rect
) const
379 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
381 wxCHECK_MSG( (rect
.GetLeft()>=0) && (rect
.GetTop()>=0) && (rect
.GetRight()<=GetWidth()) && (rect
.GetBottom()<=GetHeight()),
382 image
, wxT("invalid subimage size") );
384 int subwidth
=rect
.GetWidth();
385 const int subheight
=rect
.GetHeight();
387 image
.Create( subwidth
, subheight
);
389 char unsigned *subdata
= image
.GetData(), *data
=GetData();
391 wxCHECK_MSG( subdata
, image
, wxT("unable to create image") );
393 if (M_IMGDATA
->m_hasMask
)
394 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
396 const int subleft
=3*rect
.GetLeft();
397 const int width
=3*GetWidth();
400 data
+=rect
.GetTop()*width
+subleft
;
402 for (long j
= 0; j
< subheight
; ++j
)
404 memcpy( subdata
, data
, subwidth
);
412 void wxImage::Paste( const wxImage
&image
, int x
, int y
)
414 wxCHECK_RET( Ok(), wxT("invalid image") );
415 wxCHECK_RET( image
.Ok(), wxT("invalid image") );
419 int width
= image
.GetWidth();
420 int height
= image
.GetHeight();
433 if ((x
+xx
)+width
> M_IMGDATA
->m_width
)
434 width
= M_IMGDATA
->m_width
- (x
+xx
);
435 if ((y
+yy
)+height
> M_IMGDATA
->m_height
)
436 height
= M_IMGDATA
->m_height
- (y
+yy
);
438 if (width
< 1) return;
439 if (height
< 1) return;
441 if ((!HasMask() && !image
.HasMask()) ||
442 ((HasMask() && image
.HasMask() &&
443 (GetMaskRed()==image
.GetMaskRed()) &&
444 (GetMaskGreen()==image
.GetMaskGreen()) &&
445 (GetMaskBlue()==image
.GetMaskBlue()))))
448 unsigned char* source_data
= image
.GetData() + xx
*3 + yy
*3*image
.GetWidth();
449 int source_step
= image
.GetWidth()*3;
451 unsigned char* target_data
= GetData() + (x
+xx
)*3 + (y
+yy
)*3*M_IMGDATA
->m_width
;
452 int target_step
= M_IMGDATA
->m_width
*3;
453 for (int j
= 0; j
< height
; j
++)
455 memcpy( target_data
, source_data
, width
);
456 source_data
+= source_step
;
457 target_data
+= target_step
;
462 if (!HasMask() && image
.HasMask())
464 unsigned char r
= image
.GetMaskRed();
465 unsigned char g
= image
.GetMaskGreen();
466 unsigned char b
= image
.GetMaskBlue();
469 unsigned char* source_data
= image
.GetData() + xx
*3 + yy
*3*image
.GetWidth();
470 int source_step
= image
.GetWidth()*3;
472 unsigned char* target_data
= GetData() + (x
+xx
)*3 + (y
+yy
)*3*M_IMGDATA
->m_width
;
473 int target_step
= M_IMGDATA
->m_width
*3;
475 for (int j
= 0; j
< height
; j
++)
477 for (int i
= 0; i
< width
; i
+=3)
479 if ((source_data
[i
] != r
) &&
480 (source_data
[i
+1] != g
) &&
481 (source_data
[i
+2] != b
))
483 memcpy( target_data
+i
, source_data
+i
, 3 );
486 source_data
+= source_step
;
487 target_data
+= target_step
;
492 void wxImage::Replace( unsigned char r1
, unsigned char g1
, unsigned char b1
,
493 unsigned char r2
, unsigned char g2
, unsigned char b2
)
495 wxCHECK_RET( Ok(), wxT("invalid image") );
497 char unsigned *data
= GetData();
499 const int w
= GetWidth();
500 const int h
= GetHeight();
502 for (int j
= 0; j
< h
; j
++)
503 for (int i
= 0; i
< w
; i
++)
505 if ((data
[0] == r1
) && (data
[1] == g1
) && (data
[2] == b1
))
515 wxImage
wxImage::ConvertToMono( unsigned char r
, unsigned char g
, unsigned char b
) const
519 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
521 image
.Create( M_IMGDATA
->m_width
, M_IMGDATA
->m_height
);
523 char unsigned *data
= image
.GetData();
525 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
527 if (M_IMGDATA
->m_hasMask
)
529 if (M_IMGDATA
->m_maskRed
== r
&& M_IMGDATA
->m_maskGreen
== g
&&
530 M_IMGDATA
->m_maskBlue
== b
)
531 image
.SetMaskColour( 255, 255, 255 );
533 image
.SetMaskColour( 0, 0, 0 );
536 long size
= M_IMGDATA
->m_height
* M_IMGDATA
->m_width
;
538 char unsigned *srcd
= M_IMGDATA
->m_data
;
539 char unsigned *tard
= image
.GetData();
541 for ( long i
= 0; i
< size
; i
++, srcd
+= 3, tard
+= 3 )
543 if (srcd
[0] == r
&& srcd
[1] == g
&& srcd
[2] == b
)
544 tard
[0] = tard
[1] = tard
[2] = 255;
546 tard
[0] = tard
[1] = tard
[2] = 0;
552 void wxImage::SetRGB( int x
, int y
, unsigned char r
, unsigned char g
, unsigned char b
)
554 wxCHECK_RET( Ok(), wxT("invalid image") );
556 int w
= M_IMGDATA
->m_width
;
557 int h
= M_IMGDATA
->m_height
;
559 wxCHECK_RET( (x
>=0) && (y
>=0) && (x
<w
) && (y
<h
), wxT("invalid image index") );
561 long pos
= (y
* w
+ x
) * 3;
563 M_IMGDATA
->m_data
[ pos
] = r
;
564 M_IMGDATA
->m_data
[ pos
+1 ] = g
;
565 M_IMGDATA
->m_data
[ pos
+2 ] = b
;
568 unsigned char wxImage::GetRed( int x
, int y
) const
570 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
572 int w
= M_IMGDATA
->m_width
;
573 int h
= M_IMGDATA
->m_height
;
575 wxCHECK_MSG( (x
>=0) && (y
>=0) && (x
<w
) && (y
<h
), 0, wxT("invalid image index") );
577 long pos
= (y
* w
+ x
) * 3;
579 return M_IMGDATA
->m_data
[pos
];
582 unsigned char wxImage::GetGreen( int x
, int y
) const
584 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
586 int w
= M_IMGDATA
->m_width
;
587 int h
= M_IMGDATA
->m_height
;
589 wxCHECK_MSG( (x
>=0) && (y
>=0) && (x
<w
) && (y
<h
), 0, wxT("invalid image index") );
591 long pos
= (y
* w
+ x
) * 3;
593 return M_IMGDATA
->m_data
[pos
+1];
596 unsigned char wxImage::GetBlue( int x
, int y
) const
598 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
600 int w
= M_IMGDATA
->m_width
;
601 int h
= M_IMGDATA
->m_height
;
603 wxCHECK_MSG( (x
>=0) && (y
>=0) && (x
<w
) && (y
<h
), 0, wxT("invalid image index") );
605 long pos
= (y
* w
+ x
) * 3;
607 return M_IMGDATA
->m_data
[pos
+2];
610 bool wxImage::Ok() const
612 // image of 0 width or height can't be considered ok - at least because it
613 // causes crashes in ConvertToBitmap() if we don't catch it in time
614 wxImageRefData
*data
= M_IMGDATA
;
615 return data
&& data
->m_ok
&& data
->m_width
&& data
->m_height
;
618 char unsigned *wxImage::GetData() const
620 wxCHECK_MSG( Ok(), (char unsigned *)NULL
, wxT("invalid image") );
622 return M_IMGDATA
->m_data
;
625 void wxImage::SetData( char unsigned *data
)
627 wxCHECK_RET( Ok(), wxT("invalid image") );
629 wxImageRefData
*newRefData
= new wxImageRefData();
631 newRefData
->m_width
= M_IMGDATA
->m_width
;
632 newRefData
->m_height
= M_IMGDATA
->m_height
;
633 newRefData
->m_data
= data
;
634 newRefData
->m_ok
= TRUE
;
635 newRefData
->m_maskRed
= M_IMGDATA
->m_maskRed
;
636 newRefData
->m_maskGreen
= M_IMGDATA
->m_maskGreen
;
637 newRefData
->m_maskBlue
= M_IMGDATA
->m_maskBlue
;
638 newRefData
->m_hasMask
= M_IMGDATA
->m_hasMask
;
642 m_refData
= newRefData
;
645 void wxImage::SetData( char unsigned *data
, int new_width
, int new_height
)
647 wxImageRefData
*newRefData
= new wxImageRefData();
651 newRefData
->m_width
= new_width
;
652 newRefData
->m_height
= new_height
;
653 newRefData
->m_data
= data
;
654 newRefData
->m_ok
= TRUE
;
655 newRefData
->m_maskRed
= M_IMGDATA
->m_maskRed
;
656 newRefData
->m_maskGreen
= M_IMGDATA
->m_maskGreen
;
657 newRefData
->m_maskBlue
= M_IMGDATA
->m_maskBlue
;
658 newRefData
->m_hasMask
= M_IMGDATA
->m_hasMask
;
662 newRefData
->m_width
= new_width
;
663 newRefData
->m_height
= new_height
;
664 newRefData
->m_data
= data
;
665 newRefData
->m_ok
= TRUE
;
670 m_refData
= newRefData
;
673 void wxImage::SetMaskColour( unsigned char r
, unsigned char g
, unsigned char b
)
675 wxCHECK_RET( Ok(), wxT("invalid image") );
677 M_IMGDATA
->m_maskRed
= r
;
678 M_IMGDATA
->m_maskGreen
= g
;
679 M_IMGDATA
->m_maskBlue
= b
;
680 M_IMGDATA
->m_hasMask
= TRUE
;
683 unsigned char wxImage::GetMaskRed() const
685 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
687 return M_IMGDATA
->m_maskRed
;
690 unsigned char wxImage::GetMaskGreen() const
692 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
694 return M_IMGDATA
->m_maskGreen
;
697 unsigned char wxImage::GetMaskBlue() const
699 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
701 return M_IMGDATA
->m_maskBlue
;
704 void wxImage::SetMask( bool mask
)
706 wxCHECK_RET( Ok(), wxT("invalid image") );
708 M_IMGDATA
->m_hasMask
= mask
;
711 bool wxImage::HasMask() const
713 wxCHECK_MSG( Ok(), FALSE
, wxT("invalid image") );
715 return M_IMGDATA
->m_hasMask
;
718 int wxImage::GetWidth() const
720 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
722 return M_IMGDATA
->m_width
;
725 int wxImage::GetHeight() const
727 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
729 return M_IMGDATA
->m_height
;
733 bool wxImage::FindFirstUnusedColour(
734 unsigned char *r
, unsigned char *g
, unsigned char *b
,
735 unsigned char startR
, unsigned char startG
, unsigned char startB
) const
737 wxImageHistogram histogram
;
740 ComputeHistogram(histogram
);
742 unsigned char r2
= startR
;
743 unsigned char g2
= startG
;
744 unsigned char b2
= startB
;
746 key
= (r2
<< 16) | (g2
<< 8) | b2
;
748 while ( histogram
.find(key
) != histogram
.end() )
750 // color already used
762 wxLogError( _("GetUnusedColour:: No Unused Color in image ") );
768 key
= (r2
<< 16) | (g2
<< 8) | b2
;
779 bool wxImage::SetMaskFromImage(const wxImage
& mask
,
780 unsigned char mr
, unsigned char mg
, unsigned char mb
)
782 // check that the images are the same size
783 if ( (M_IMGDATA
->m_height
!= mask
.GetHeight() ) || (M_IMGDATA
->m_width
!= mask
.GetWidth () ) )
785 wxLogError( _("Image and Mask have different sizes") );
789 // find unused colour
790 unsigned char r
,g
,b
;
791 if (!FindFirstUnusedColour(&r
, &g
, &b
))
793 wxLogError( _("No Unused Color in image being masked") );
797 char unsigned *imgdata
= GetData();
798 char unsigned *maskdata
= mask
.GetData();
800 const int w
= GetWidth();
801 const int h
= GetHeight();
803 for (int j
= 0; j
< h
; j
++)
805 for (int i
= 0; i
< w
; i
++)
807 if ((maskdata
[0] == mr
) && (maskdata
[1] == mg
) && (maskdata
[2] == mb
))
818 SetMaskColour(r
, g
, b
);
828 bool wxImage::HasPalette() const
833 return M_IMGDATA
->m_palette
.Ok();
836 const wxPalette
& wxImage::GetPalette() const
838 wxCHECK_MSG( Ok(), wxNullPalette
, wxT("invalid image") );
840 return M_IMGDATA
->m_palette
;
843 void wxImage::SetPalette(const wxPalette
& palette
)
845 wxCHECK_RET( Ok(), wxT("invalid image") );
847 M_IMGDATA
->m_palette
= palette
;
850 #endif // wxUSE_PALETTE
852 // Option functions (arbitrary name/value mapping)
853 void wxImage::SetOption(const wxString
& name
, const wxString
& value
)
855 wxCHECK_RET( Ok(), wxT("invalid image") );
857 int idx
= M_IMGDATA
->m_optionNames
.Index(name
, FALSE
);
858 if (idx
== wxNOT_FOUND
)
860 M_IMGDATA
->m_optionNames
.Add(name
);
861 M_IMGDATA
->m_optionValues
.Add(value
);
865 M_IMGDATA
->m_optionNames
[idx
] = name
;
866 M_IMGDATA
->m_optionValues
[idx
] = value
;
870 void wxImage::SetOption(const wxString
& name
, int value
)
873 valStr
.Printf(wxT("%d"), value
);
874 SetOption(name
, valStr
);
877 wxString
wxImage::GetOption(const wxString
& name
) const
879 wxCHECK_MSG( Ok(), wxEmptyString
, wxT("invalid image") );
881 int idx
= M_IMGDATA
->m_optionNames
.Index(name
, FALSE
);
882 if (idx
== wxNOT_FOUND
)
883 return wxEmptyString
;
885 return M_IMGDATA
->m_optionValues
[idx
];
888 int wxImage::GetOptionInt(const wxString
& name
) const
890 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
892 return wxAtoi(GetOption(name
));
895 bool wxImage::HasOption(const wxString
& name
) const
897 wxCHECK_MSG( Ok(), FALSE
, wxT("invalid image") );
899 return (M_IMGDATA
->m_optionNames
.Index(name
, FALSE
) != wxNOT_FOUND
);
902 bool wxImage::LoadFile( const wxString
& filename
, long type
, int index
)
905 if (wxFileExists(filename
))
907 wxFileInputStream
stream(filename
);
908 wxBufferedInputStream
bstream( stream
);
909 return LoadFile(bstream
, type
, index
);
913 wxLogError( _("Can't load image from file '%s': file does not exist."), filename
.c_str() );
917 #else // !wxUSE_STREAMS
919 #endif // wxUSE_STREAMS
922 bool wxImage::LoadFile( const wxString
& filename
, const wxString
& mimetype
, int index
)
925 if (wxFileExists(filename
))
927 wxFileInputStream
stream(filename
);
928 wxBufferedInputStream
bstream( stream
);
929 return LoadFile(bstream
, mimetype
, index
);
933 wxLogError( _("Can't load image from file '%s': file does not exist."), filename
.c_str() );
937 #else // !wxUSE_STREAMS
939 #endif // wxUSE_STREAMS
944 bool wxImage::SaveFile( const wxString
& filename
) const
946 wxString ext
= filename
.AfterLast('.').Lower();
948 wxImageHandler
* pHandler
= FindHandler(ext
, -1);
951 SaveFile(filename
, pHandler
->GetType());
955 wxLogError(_("Can't save image to file '%s': unknown extension."), filename
.c_str());
960 bool wxImage::SaveFile( const wxString
& filename
, int type
) const
963 ((wxImage
*)this)->SetOption(wxIMAGE_OPTION_FILENAME
, filename
);
965 wxFileOutputStream
stream(filename
);
969 wxBufferedOutputStream
bstream( stream
);
970 return SaveFile(bstream
, type
);
972 #endif // wxUSE_STREAMS
977 bool wxImage::SaveFile( const wxString
& filename
, const wxString
& mimetype
) const
980 ((wxImage
*)this)->SetOption(wxIMAGE_OPTION_FILENAME
, filename
);
982 wxFileOutputStream
stream(filename
);
986 wxBufferedOutputStream
bstream( stream
);
987 return SaveFile(bstream
, mimetype
);
989 #endif // wxUSE_STREAMS
994 bool wxImage::CanRead( const wxString
&name
)
997 wxFileInputStream
stream(name
);
998 return CanRead(stream
);
1004 int wxImage::GetImageCount( const wxString
&name
, long type
)
1007 wxFileInputStream
stream(name
);
1009 return GetImageCount(stream
, type
);
1017 bool wxImage::CanRead( wxInputStream
&stream
)
1019 const wxList
& list
= GetHandlers();
1021 for ( wxList::Node
*node
= list
.GetFirst(); node
; node
= node
->GetNext() )
1023 wxImageHandler
*handler
=(wxImageHandler
*)node
->GetData();
1024 if (handler
->CanRead( stream
))
1031 int wxImage::GetImageCount( wxInputStream
&stream
, long type
)
1033 wxImageHandler
*handler
;
1035 if ( type
== wxBITMAP_TYPE_ANY
)
1037 wxList
&list
=GetHandlers();
1039 for (wxList::Node
*node
= list
.GetFirst(); node
; node
= node
->GetNext())
1041 handler
=(wxImageHandler
*)node
->GetData();
1042 if ( handler
->CanRead(stream
) )
1043 return handler
->GetImageCount(stream
);
1047 wxLogWarning(_("No handler found for image type."));
1051 handler
= FindHandler(type
);
1055 wxLogWarning(_("No image handler for type %d defined."), type
);
1059 if ( handler
->CanRead(stream
) )
1061 return handler
->GetImageCount(stream
);
1065 wxLogError(_("Image file is not of type %d."), type
);
1070 bool wxImage::LoadFile( wxInputStream
& stream
, long type
, int index
)
1074 m_refData
= new wxImageRefData
;
1076 wxImageHandler
*handler
;
1078 if ( type
== wxBITMAP_TYPE_ANY
)
1080 wxList
&list
=GetHandlers();
1082 for ( wxList::Node
*node
= list
.GetFirst(); node
; node
= node
->GetNext() )
1084 handler
=(wxImageHandler
*)node
->GetData();
1085 if ( handler
->CanRead(stream
) )
1086 return handler
->LoadFile(this, stream
, TRUE
/*verbose*/, index
);
1090 wxLogWarning( _("No handler found for image type.") );
1094 handler
= FindHandler(type
);
1098 wxLogWarning( _("No image handler for type %d defined."), type
);
1103 return handler
->LoadFile(this, stream
, TRUE
/*verbose*/, index
);
1106 bool wxImage::LoadFile( wxInputStream
& stream
, const wxString
& mimetype
, int index
)
1110 m_refData
= new wxImageRefData
;
1112 wxImageHandler
*handler
= FindHandlerMime(mimetype
);
1116 wxLogWarning( _("No image handler for type %s defined."), mimetype
.GetData() );
1121 return handler
->LoadFile( this, stream
, TRUE
/*verbose*/, index
);
1124 bool wxImage::SaveFile( wxOutputStream
& stream
, int type
) const
1126 wxCHECK_MSG( Ok(), FALSE
, wxT("invalid image") );
1128 wxImageHandler
*handler
= FindHandler(type
);
1132 wxLogWarning( _("No image handler for type %d defined."), type
);
1137 return handler
->SaveFile( (wxImage
*)this, stream
);
1140 bool wxImage::SaveFile( wxOutputStream
& stream
, const wxString
& mimetype
) const
1142 wxCHECK_MSG( Ok(), FALSE
, wxT("invalid image") );
1144 wxImageHandler
*handler
= FindHandlerMime(mimetype
);
1148 wxLogWarning( _("No image handler for type %s defined."), mimetype
.GetData() );
1153 return handler
->SaveFile( (wxImage
*)this, stream
);
1155 #endif // wxUSE_STREAMS
1157 void wxImage::AddHandler( wxImageHandler
*handler
)
1159 // make sure that the memory will be freed at the program end
1160 sm_handlers
.DeleteContents(TRUE
);
1162 // Check for an existing handler of the type being added.
1163 if (FindHandler( handler
->GetType() ) == 0)
1165 sm_handlers
.Append( handler
);
1169 // This is not documented behaviour, merely the simplest 'fix'
1170 // for preventing duplicate additions. If someone ever has
1171 // a good reason to add and remove duplicate handlers (and they
1172 // may) we should probably refcount the duplicates.
1173 // also an issue in InsertHandler below.
1175 wxLogDebug( _T("Adding duplicate image handler for '%s'"),
1176 handler
->GetName().c_str() );
1181 void wxImage::InsertHandler( wxImageHandler
*handler
)
1183 // make sure that the memory will be freed at the program end
1184 sm_handlers
.DeleteContents(TRUE
);
1186 // Check for an existing handler of the type being added.
1187 if (FindHandler( handler
->GetType() ) == 0)
1189 sm_handlers
.Insert( handler
);
1193 // see AddHandler for additional comments.
1194 wxLogDebug( _T("Inserting duplicate image handler for '%s'"),
1195 handler
->GetName().c_str() );
1200 bool wxImage::RemoveHandler( const wxString
& name
)
1202 wxImageHandler
*handler
= FindHandler(name
);
1205 sm_handlers
.DeleteObject(handler
);
1212 wxImageHandler
*wxImage::FindHandler( const wxString
& name
)
1214 wxNode
*node
= sm_handlers
.First();
1217 wxImageHandler
*handler
= (wxImageHandler
*)node
->Data();
1218 if (handler
->GetName().Cmp(name
) == 0) return handler
;
1220 node
= node
->Next();
1225 wxImageHandler
*wxImage::FindHandler( const wxString
& extension
, long bitmapType
)
1227 wxNode
*node
= sm_handlers
.First();
1230 wxImageHandler
*handler
= (wxImageHandler
*)node
->Data();
1231 if ( (handler
->GetExtension().Cmp(extension
) == 0) &&
1232 (bitmapType
== -1 || handler
->GetType() == bitmapType
) )
1234 node
= node
->Next();
1239 wxImageHandler
*wxImage::FindHandler( long bitmapType
)
1241 wxNode
*node
= sm_handlers
.First();
1244 wxImageHandler
*handler
= (wxImageHandler
*)node
->Data();
1245 if (handler
->GetType() == bitmapType
) return handler
;
1246 node
= node
->Next();
1251 wxImageHandler
*wxImage::FindHandlerMime( const wxString
& mimetype
)
1253 wxNode
*node
= sm_handlers
.First();
1256 wxImageHandler
*handler
= (wxImageHandler
*)node
->Data();
1257 if (handler
->GetMimeType().IsSameAs(mimetype
, FALSE
)) return handler
;
1258 node
= node
->Next();
1263 void wxImage::InitStandardHandlers()
1266 AddHandler(new wxBMPHandler
);
1267 #endif // wxUSE_STREAMS
1270 void wxImage::CleanUpHandlers()
1272 wxNode
*node
= sm_handlers
.First();
1275 wxImageHandler
*handler
= (wxImageHandler
*)node
->Data();
1276 wxNode
*next
= node
->Next();
1283 //-----------------------------------------------------------------------------
1285 //-----------------------------------------------------------------------------
1287 IMPLEMENT_ABSTRACT_CLASS(wxImageHandler
,wxObject
)
1290 bool wxImageHandler::LoadFile( wxImage
*WXUNUSED(image
), wxInputStream
& WXUNUSED(stream
), bool WXUNUSED(verbose
), int WXUNUSED(index
) )
1295 bool wxImageHandler::SaveFile( wxImage
*WXUNUSED(image
), wxOutputStream
& WXUNUSED(stream
), bool WXUNUSED(verbose
) )
1300 int wxImageHandler::GetImageCount( wxInputStream
& WXUNUSED(stream
) )
1305 bool wxImageHandler::CanRead( const wxString
& name
)
1307 if (wxFileExists(name
))
1309 wxFileInputStream
stream(name
);
1310 return CanRead(stream
);
1313 wxLogError( _("Can't check image format of file '%s': file does not exist."), name
.c_str() );
1318 bool wxImageHandler::CallDoCanRead(wxInputStream
& stream
)
1320 off_t posOld
= stream
.TellI();
1321 if ( posOld
== wxInvalidOffset
)
1323 // can't test unseekable stream
1327 bool ok
= DoCanRead(stream
);
1329 // restore the old position to be able to test other formats and so on
1330 if ( stream
.SeekI(posOld
) == wxInvalidOffset
)
1332 wxLogDebug(_T("Failed to rewind the stream in wxImageHandler!"));
1334 // reading would fail anyhow as we're not at the right position
1341 #endif // wxUSE_STREAMS
1345 //-----------------------------------------------------------------------------
1346 // Deprecated wxBitmap conversion routines
1347 //-----------------------------------------------------------------------------
1349 #if WXWIN_COMPATIBILITY_2_2 && wxUSE_GUI
1352 wxBitmap
wxImage::ConvertToMonoBitmap( unsigned char red
, unsigned char green
, unsigned char blue
) const
1354 wxImage mono
= this->ConvertToMono( red
, green
, blue
);
1355 wxBitmap
bitmap( mono
, 1 );
1360 wxBitmap
wxImage::ConvertToBitmap() const
1362 wxBitmap
bitmap( *this );
1366 wxImage::wxImage( const wxBitmap
&bitmap
)
1368 *this = bitmap
.ConvertToImage();
1371 #endif // WXWIN_COMPATIBILITY_2_2 && wxUSE_GUI
1374 //-----------------------------------------------------------------------------
1377 // Counts and returns the number of different colours. Optionally stops
1378 // when it exceeds 'stopafter' different colours. This is useful, for
1379 // example, to see if the image can be saved as 8-bit (256 colour or
1380 // less, in this case it would be invoked as CountColours(256)). Default
1381 // value for stopafter is -1 (don't care).
1383 unsigned long wxImage::CountColours( unsigned long stopafter
) const
1387 unsigned char r
, g
, b
;
1389 unsigned long size
, nentries
, key
;
1392 size
= GetWidth() * GetHeight();
1395 for (unsigned long j
= 0; (j
< size
) && (nentries
<= stopafter
) ; j
++)
1400 key
= (r
<< 16) | (g
<< 8) | b
;
1402 if (h
.Get(key
) == NULL
)
1413 unsigned long wxImage::ComputeHistogram( wxImageHistogram
&h
) const
1415 unsigned char r
, g
, b
;
1417 unsigned long size
, nentries
, key
;
1422 size
= GetWidth() * GetHeight();
1425 for (unsigned long j
= 0; j
< size
; j
++)
1430 key
= (r
<< 16) | (g
<< 8) | b
;
1432 wxImageHistogramEntry
& entry
= h
[key
];
1433 if ( entry
.value
++ == 0 )
1434 entry
.index
= nentries
++;
1441 * Rotation code by Carlos Moreno
1444 // GRG: I've removed wxRotationPoint - we already have wxRealPoint which
1445 // does exactly the same thing. And I also got rid of wxRotationPixel
1446 // bacause of potential problems in architectures where alignment
1447 // is an issue, so I had to rewrite parts of the code.
1449 static const double gs_Epsilon
= 1e-10;
1451 static inline int wxCint (double x
)
1453 return (x
> 0) ? (int) (x
+ 0.5) : (int) (x
- 0.5);
1457 // Auxiliary function to rotate a point (x,y) with respect to point p0
1458 // make it inline and use a straight return to facilitate optimization
1459 // also, the function receives the sine and cosine of the angle to avoid
1460 // repeating the time-consuming calls to these functions -- sin/cos can
1461 // be computed and stored in the calling function.
1463 inline wxRealPoint
rotated_point (const wxRealPoint
& p
, double cos_angle
, double sin_angle
, const wxRealPoint
& p0
)
1465 return wxRealPoint (p0
.x
+ (p
.x
- p0
.x
) * cos_angle
- (p
.y
- p0
.y
) * sin_angle
,
1466 p0
.y
+ (p
.y
- p0
.y
) * cos_angle
+ (p
.x
- p0
.x
) * sin_angle
);
1469 inline wxRealPoint
rotated_point (double x
, double y
, double cos_angle
, double sin_angle
, const wxRealPoint
& p0
)
1471 return rotated_point (wxRealPoint(x
,y
), cos_angle
, sin_angle
, p0
);
1474 wxImage
wxImage::Rotate(double angle
, const wxPoint
& centre_of_rotation
, bool interpolating
, wxPoint
* offset_after_rotation
) const
1477 angle
= -angle
; // screen coordinates are a mirror image of "real" coordinates
1479 // Create pointer-based array to accelerate access to wxImage's data
1480 unsigned char ** data
= new unsigned char * [GetHeight()];
1482 data
[0] = GetData();
1484 for (i
= 1; i
< GetHeight(); i
++)
1485 data
[i
] = data
[i
- 1] + (3 * GetWidth());
1487 // precompute coefficients for rotation formula
1488 // (sine and cosine of the angle)
1489 const double cos_angle
= cos(angle
);
1490 const double sin_angle
= sin(angle
);
1492 // Create new Image to store the result
1493 // First, find rectangle that covers the rotated image; to do that,
1494 // rotate the four corners
1496 const wxRealPoint
p0(centre_of_rotation
.x
, centre_of_rotation
.y
);
1498 wxRealPoint p1
= rotated_point (0, 0, cos_angle
, sin_angle
, p0
);
1499 wxRealPoint p2
= rotated_point (0, GetHeight(), cos_angle
, sin_angle
, p0
);
1500 wxRealPoint p3
= rotated_point (GetWidth(), 0, cos_angle
, sin_angle
, p0
);
1501 wxRealPoint p4
= rotated_point (GetWidth(), GetHeight(), cos_angle
, sin_angle
, p0
);
1503 int x1
= (int) floor (wxMin (wxMin(p1
.x
, p2
.x
), wxMin(p3
.x
, p4
.x
)));
1504 int y1
= (int) floor (wxMin (wxMin(p1
.y
, p2
.y
), wxMin(p3
.y
, p4
.y
)));
1505 int x2
= (int) ceil (wxMax (wxMax(p1
.x
, p2
.x
), wxMax(p3
.x
, p4
.x
)));
1506 int y2
= (int) ceil (wxMax (wxMax(p1
.y
, p2
.y
), wxMax(p3
.y
, p4
.y
)));
1508 wxImage
rotated (x2
- x1
+ 1, y2
- y1
+ 1);
1510 if (offset_after_rotation
!= NULL
)
1512 *offset_after_rotation
= wxPoint (x1
, y1
);
1515 // GRG: The rotated (destination) image is always accessed
1516 // sequentially, so there is no need for a pointer-based
1517 // array here (and in fact it would be slower).
1519 unsigned char * dst
= rotated
.GetData();
1521 // GRG: if the original image has a mask, use its RGB values
1522 // as the blank pixel, else, fall back to default (black).
1524 unsigned char blank_r
= 0;
1525 unsigned char blank_g
= 0;
1526 unsigned char blank_b
= 0;
1530 blank_r
= GetMaskRed();
1531 blank_g
= GetMaskGreen();
1532 blank_b
= GetMaskBlue();
1533 rotated
.SetMaskColour( blank_r
, blank_g
, blank_b
);
1536 // Now, for each point of the rotated image, find where it came from, by
1537 // performing an inverse rotation (a rotation of -angle) and getting the
1538 // pixel at those coordinates
1540 // GRG: I've taken the (interpolating) test out of the loops, so that
1541 // it is done only once, instead of repeating it for each pixel.
1546 for (int y
= 0; y
< rotated
.GetHeight(); y
++)
1548 for (x
= 0; x
< rotated
.GetWidth(); x
++)
1550 wxRealPoint src
= rotated_point (x
+ x1
, y
+ y1
, cos_angle
, -sin_angle
, p0
);
1552 if (-0.25 < src
.x
&& src
.x
< GetWidth() - 0.75 &&
1553 -0.25 < src
.y
&& src
.y
< GetHeight() - 0.75)
1555 // interpolate using the 4 enclosing grid-points. Those
1556 // points can be obtained using floor and ceiling of the
1557 // exact coordinates of the point
1558 // C.M. 2000-02-17: when the point is near the border, special care is required.
1562 if (0 < src
.x
&& src
.x
< GetWidth() - 1)
1564 x1
= wxCint(floor(src
.x
));
1565 x2
= wxCint(ceil(src
.x
));
1567 else // else means that x is near one of the borders (0 or width-1)
1569 x1
= x2
= wxCint (src
.x
);
1572 if (0 < src
.y
&& src
.y
< GetHeight() - 1)
1574 y1
= wxCint(floor(src
.y
));
1575 y2
= wxCint(ceil(src
.y
));
1579 y1
= y2
= wxCint (src
.y
);
1582 // get four points and the distances (square of the distance,
1583 // for efficiency reasons) for the interpolation formula
1585 // GRG: Do not calculate the points until they are
1586 // really needed -- this way we can calculate
1587 // just one, instead of four, if d1, d2, d3
1588 // or d4 are < gs_Epsilon
1590 const double d1
= (src
.x
- x1
) * (src
.x
- x1
) + (src
.y
- y1
) * (src
.y
- y1
);
1591 const double d2
= (src
.x
- x2
) * (src
.x
- x2
) + (src
.y
- y1
) * (src
.y
- y1
);
1592 const double d3
= (src
.x
- x2
) * (src
.x
- x2
) + (src
.y
- y2
) * (src
.y
- y2
);
1593 const double d4
= (src
.x
- x1
) * (src
.x
- x1
) + (src
.y
- y2
) * (src
.y
- y2
);
1595 // Now interpolate as a weighted average of the four surrounding
1596 // points, where the weights are the distances to each of those points
1598 // If the point is exactly at one point of the grid of the source
1599 // image, then don't interpolate -- just assign the pixel
1601 if (d1
< gs_Epsilon
) // d1,d2,d3,d4 are positive -- no need for abs()
1603 unsigned char *p
= data
[y1
] + (3 * x1
);
1608 else if (d2
< gs_Epsilon
)
1610 unsigned char *p
= data
[y1
] + (3 * x2
);
1615 else if (d3
< gs_Epsilon
)
1617 unsigned char *p
= data
[y2
] + (3 * x2
);
1622 else if (d4
< gs_Epsilon
)
1624 unsigned char *p
= data
[y2
] + (3 * x1
);
1631 // weights for the weighted average are proportional to the inverse of the distance
1632 unsigned char *v1
= data
[y1
] + (3 * x1
);
1633 unsigned char *v2
= data
[y1
] + (3 * x2
);
1634 unsigned char *v3
= data
[y2
] + (3 * x2
);
1635 unsigned char *v4
= data
[y2
] + (3 * x1
);
1637 const double w1
= 1/d1
, w2
= 1/d2
, w3
= 1/d3
, w4
= 1/d4
;
1641 *(dst
++) = (unsigned char)
1642 ( (w1
* *(v1
++) + w2
* *(v2
++) +
1643 w3
* *(v3
++) + w4
* *(v4
++)) /
1644 (w1
+ w2
+ w3
+ w4
) );
1645 *(dst
++) = (unsigned char)
1646 ( (w1
* *(v1
++) + w2
* *(v2
++) +
1647 w3
* *(v3
++) + w4
* *(v4
++)) /
1648 (w1
+ w2
+ w3
+ w4
) );
1649 *(dst
++) = (unsigned char)
1650 ( (w1
* *(v1
++) + w2
* *(v2
++) +
1651 w3
* *(v3
++) + w4
* *(v4
++)) /
1652 (w1
+ w2
+ w3
+ w4
) );
1664 else // not interpolating
1666 for (int y
= 0; y
< rotated
.GetHeight(); y
++)
1668 for (x
= 0; x
< rotated
.GetWidth(); x
++)
1670 wxRealPoint src
= rotated_point (x
+ x1
, y
+ y1
, cos_angle
, -sin_angle
, p0
);
1672 const int xs
= wxCint (src
.x
); // wxCint rounds to the
1673 const int ys
= wxCint (src
.y
); // closest integer
1675 if (0 <= xs
&& xs
< GetWidth() &&
1676 0 <= ys
&& ys
< GetHeight())
1678 unsigned char *p
= data
[ys
] + (3 * xs
);
1702 // A module to allow wxImage initialization/cleanup
1703 // without calling these functions from app.cpp or from
1704 // the user's application.
1706 class wxImageModule
: public wxModule
1708 DECLARE_DYNAMIC_CLASS(wxImageModule
)
1711 bool OnInit() { wxImage::InitStandardHandlers(); return TRUE
; };
1712 void OnExit() { wxImage::CleanUpHandlers(); };
1715 IMPLEMENT_DYNAMIC_CLASS(wxImageModule
, wxModule
)
1718 #endif // wxUSE_IMAGE