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"
45 //-----------------------------------------------------------------------------
47 //-----------------------------------------------------------------------------
49 class wxImageRefData
: public wxObjectRefData
57 unsigned char *m_data
;
59 unsigned char m_maskRed
,m_maskGreen
,m_maskBlue
;
64 #endif // wxUSE_PALETTE
65 wxArrayString m_optionNames
;
66 wxArrayString m_optionValues
;
68 DECLARE_NO_COPY_CLASS(wxImageRefData
)
71 wxImageRefData::wxImageRefData()
75 m_data
= (unsigned char*) NULL
;
84 wxImageRefData::~wxImageRefData()
86 if (m_data
&& !m_static
)
90 wxList
wxImage::sm_handlers
;
94 //-----------------------------------------------------------------------------
96 #define M_IMGDATA ((wxImageRefData *)m_refData)
98 IMPLEMENT_DYNAMIC_CLASS(wxImage
, wxObject
)
104 wxImage::wxImage( int width
, int height
)
106 Create( width
, height
);
109 wxImage::wxImage( int width
, int height
, unsigned char* data
, bool static_data
)
111 Create( width
, height
, data
, static_data
);
114 wxImage::wxImage( const wxString
& name
, long type
, int index
)
116 LoadFile( name
, type
, index
);
119 wxImage::wxImage( const wxString
& name
, const wxString
& mimetype
, int index
)
121 LoadFile( name
, mimetype
, index
);
125 wxImage::wxImage( wxInputStream
& stream
, long type
, int index
)
127 LoadFile( stream
, type
, index
);
130 wxImage::wxImage( wxInputStream
& stream
, const wxString
& mimetype
, int index
)
132 LoadFile( stream
, mimetype
, index
);
134 #endif // wxUSE_STREAMS
136 wxImage::wxImage( const wxImage
& image
)
142 wxImage::wxImage( const wxImage
* image
)
144 if (image
) Ref(*image
);
147 void wxImage::Create( int width
, int height
)
151 m_refData
= new wxImageRefData();
153 M_IMGDATA
->m_data
= (unsigned char *) malloc( width
*height
*3 );
154 if (M_IMGDATA
->m_data
)
156 for (int l
= 0; l
< width
*height
*3; l
++) M_IMGDATA
->m_data
[l
] = 0;
158 M_IMGDATA
->m_width
= width
;
159 M_IMGDATA
->m_height
= height
;
160 M_IMGDATA
->m_ok
= TRUE
;
168 void wxImage::Create( int width
, int height
, unsigned char* data
, bool static_data
)
172 m_refData
= new wxImageRefData();
174 M_IMGDATA
->m_data
= data
;
175 if (M_IMGDATA
->m_data
)
177 M_IMGDATA
->m_width
= width
;
178 M_IMGDATA
->m_height
= height
;
179 M_IMGDATA
->m_ok
= TRUE
;
180 M_IMGDATA
->m_static
= static_data
;
188 void wxImage::Destroy()
193 wxImage
wxImage::Copy() const
197 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
199 image
.Create( M_IMGDATA
->m_width
, M_IMGDATA
->m_height
);
201 char unsigned *data
= image
.GetData();
203 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
205 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
206 image
.SetMask( M_IMGDATA
->m_hasMask
);
208 memcpy( data
, GetData(), M_IMGDATA
->m_width
*M_IMGDATA
->m_height
*3 );
213 wxImage
wxImage::Scale( int width
, int height
) const
217 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
219 // can't scale to/from 0 size
220 wxCHECK_MSG( (width
> 0) && (height
> 0), image
,
221 wxT("invalid new image size") );
223 long old_height
= M_IMGDATA
->m_height
,
224 old_width
= M_IMGDATA
->m_width
;
225 wxCHECK_MSG( (old_height
> 0) && (old_width
> 0), image
,
226 wxT("invalid old image size") );
228 image
.Create( width
, height
);
230 char unsigned *data
= image
.GetData();
232 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
234 if (M_IMGDATA
->m_hasMask
)
236 image
.SetMaskColour( M_IMGDATA
->m_maskRed
,
237 M_IMGDATA
->m_maskGreen
,
238 M_IMGDATA
->m_maskBlue
);
241 char unsigned *source_data
= M_IMGDATA
->m_data
;
242 char unsigned *target_data
= data
;
245 // This is nonsense, RR.
247 // We do (x, y) -> (x, y)*oldSize/newSize but the valid values of x and y
248 // are from 0 to size-1, hence all decrement the sizes
249 long old_old_width
= old_width
;
254 for ( long j
= 0; j
<= height
; j
++ )
256 // don't crash for images with height == 1
257 long y_offset
= height
? (j
* old_height
/ height
)* old_old_width
: 0;
259 for ( long i
= 0; i
<= width
; i
++ )
261 long x_offset
= width
? (i
* old_width
) / width
: 0;
263 memcpy( target_data
, source_data
+ 3*(y_offset
+ x_offset
), 3 );
268 for (long j
= 0; j
< height
; j
++)
270 long y_offset
= (j
* old_height
/ height
) * old_width
;
272 for (long i
= 0; i
< width
; i
++)
275 source_data
+ 3*(y_offset
+ ((i
* old_width
)/ width
)),
282 // In case this is a cursor, make sure the hotspot is scalled accordingly:
283 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
) )
284 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
,
285 (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X
)*width
)/old_width
);
286 if ( HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
) )
287 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
,
288 (GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y
)*height
)/old_height
);
293 wxImage
wxImage::Rotate90( bool clockwise
) const
297 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
299 image
.Create( M_IMGDATA
->m_height
, M_IMGDATA
->m_width
);
301 char unsigned *data
= image
.GetData();
303 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
305 if (M_IMGDATA
->m_hasMask
)
306 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
308 long height
= M_IMGDATA
->m_height
;
309 long width
= M_IMGDATA
->m_width
;
311 char unsigned *source_data
= M_IMGDATA
->m_data
;
312 char unsigned *target_data
;
314 for (long j
= 0; j
< height
; j
++)
316 for (long i
= 0; i
< width
; i
++)
319 target_data
= data
+ (((i
+1)*height
) - j
- 1)*3;
321 target_data
= data
+ ((height
*(width
-1)) + j
- (i
*height
))*3;
322 memcpy( target_data
, source_data
, 3 );
330 wxImage
wxImage::Mirror( bool horizontally
) const
334 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
336 image
.Create( M_IMGDATA
->m_width
, M_IMGDATA
->m_height
);
338 char unsigned *data
= image
.GetData();
340 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
342 if (M_IMGDATA
->m_hasMask
)
343 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
345 long height
= M_IMGDATA
->m_height
;
346 long width
= M_IMGDATA
->m_width
;
348 char unsigned *source_data
= M_IMGDATA
->m_data
;
349 char unsigned *target_data
;
353 for (long j
= 0; j
< height
; j
++)
356 target_data
= data
-3;
357 for (long i
= 0; i
< width
; i
++)
359 memcpy( target_data
, source_data
, 3 );
367 for (long i
= 0; i
< height
; i
++)
369 target_data
= data
+ 3*width
*(height
-1-i
);
370 memcpy( target_data
, source_data
, (size_t)3*width
);
371 source_data
+= 3*width
;
378 wxImage
wxImage::GetSubImage( const wxRect
&rect
) const
382 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
384 wxCHECK_MSG( (rect
.GetLeft()>=0) && (rect
.GetTop()>=0) && (rect
.GetRight()<=GetWidth()) && (rect
.GetBottom()<=GetHeight()),
385 image
, wxT("invalid subimage size") );
387 int subwidth
=rect
.GetWidth();
388 const int subheight
=rect
.GetHeight();
390 image
.Create( subwidth
, subheight
);
392 char unsigned *subdata
= image
.GetData(), *data
=GetData();
394 wxCHECK_MSG( subdata
, image
, wxT("unable to create image") );
396 if (M_IMGDATA
->m_hasMask
)
397 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
399 const int subleft
=3*rect
.GetLeft();
400 const int width
=3*GetWidth();
403 data
+=rect
.GetTop()*width
+subleft
;
405 for (long j
= 0; j
< subheight
; ++j
)
407 memcpy( subdata
, data
, subwidth
);
415 void wxImage::Paste( const wxImage
&image
, int x
, int y
)
417 wxCHECK_RET( Ok(), wxT("invalid image") );
418 wxCHECK_RET( image
.Ok(), wxT("invalid image") );
422 int width
= image
.GetWidth();
423 int height
= image
.GetHeight();
436 if ((x
+xx
)+width
> M_IMGDATA
->m_width
)
437 width
= M_IMGDATA
->m_width
- (x
+xx
);
438 if ((y
+yy
)+height
> M_IMGDATA
->m_height
)
439 height
= M_IMGDATA
->m_height
- (y
+yy
);
441 if (width
< 1) return;
442 if (height
< 1) return;
444 if ((!HasMask() && !image
.HasMask()) ||
445 ((HasMask() && image
.HasMask() &&
446 (GetMaskRed()==image
.GetMaskRed()) &&
447 (GetMaskGreen()==image
.GetMaskGreen()) &&
448 (GetMaskBlue()==image
.GetMaskBlue()))))
451 unsigned char* source_data
= image
.GetData() + xx
*3 + yy
*3*image
.GetWidth();
452 int source_step
= image
.GetWidth()*3;
454 unsigned char* target_data
= GetData() + (x
+xx
)*3 + (y
+yy
)*3*M_IMGDATA
->m_width
;
455 int target_step
= M_IMGDATA
->m_width
*3;
456 for (int j
= 0; j
< height
; j
++)
458 memcpy( target_data
, source_data
, width
);
459 source_data
+= source_step
;
460 target_data
+= target_step
;
465 if (!HasMask() && image
.HasMask())
467 unsigned char r
= image
.GetMaskRed();
468 unsigned char g
= image
.GetMaskGreen();
469 unsigned char b
= image
.GetMaskBlue();
472 unsigned char* source_data
= image
.GetData() + xx
*3 + yy
*3*image
.GetWidth();
473 int source_step
= image
.GetWidth()*3;
475 unsigned char* target_data
= GetData() + (x
+xx
)*3 + (y
+yy
)*3*M_IMGDATA
->m_width
;
476 int target_step
= M_IMGDATA
->m_width
*3;
478 for (int j
= 0; j
< height
; j
++)
480 for (int i
= 0; i
< width
; i
+=3)
482 if ((source_data
[i
] != r
) &&
483 (source_data
[i
+1] != g
) &&
484 (source_data
[i
+2] != b
))
486 memcpy( target_data
+i
, source_data
+i
, 3 );
489 source_data
+= source_step
;
490 target_data
+= target_step
;
495 void wxImage::Replace( unsigned char r1
, unsigned char g1
, unsigned char b1
,
496 unsigned char r2
, unsigned char g2
, unsigned char b2
)
498 wxCHECK_RET( Ok(), wxT("invalid image") );
500 char unsigned *data
= GetData();
502 const int w
= GetWidth();
503 const int h
= GetHeight();
505 for (int j
= 0; j
< h
; j
++)
506 for (int i
= 0; i
< w
; i
++)
508 if ((data
[0] == r1
) && (data
[1] == g1
) && (data
[2] == b1
))
518 wxImage
wxImage::ConvertToMono( unsigned char r
, unsigned char g
, unsigned char b
) const
522 wxCHECK_MSG( Ok(), image
, wxT("invalid image") );
524 image
.Create( M_IMGDATA
->m_width
, M_IMGDATA
->m_height
);
526 char unsigned *data
= image
.GetData();
528 wxCHECK_MSG( data
, image
, wxT("unable to create image") );
530 if (M_IMGDATA
->m_hasMask
)
532 if (M_IMGDATA
->m_maskRed
== r
&& M_IMGDATA
->m_maskGreen
== g
&&
533 M_IMGDATA
->m_maskBlue
== b
)
534 image
.SetMaskColour( 255, 255, 255 );
536 image
.SetMaskColour( 0, 0, 0 );
539 long size
= M_IMGDATA
->m_height
* M_IMGDATA
->m_width
;
541 char unsigned *srcd
= M_IMGDATA
->m_data
;
542 char unsigned *tard
= image
.GetData();
544 for ( long i
= 0; i
< size
; i
++, srcd
+= 3, tard
+= 3 )
546 if (srcd
[0] == r
&& srcd
[1] == g
&& srcd
[2] == b
)
547 tard
[0] = tard
[1] = tard
[2] = 255;
549 tard
[0] = tard
[1] = tard
[2] = 0;
555 void wxImage::SetRGB( int x
, int y
, unsigned char r
, unsigned char g
, unsigned char b
)
557 wxCHECK_RET( Ok(), wxT("invalid image") );
559 int w
= M_IMGDATA
->m_width
;
560 int h
= M_IMGDATA
->m_height
;
562 wxCHECK_RET( (x
>=0) && (y
>=0) && (x
<w
) && (y
<h
), wxT("invalid image index") );
564 long pos
= (y
* w
+ x
) * 3;
566 M_IMGDATA
->m_data
[ pos
] = r
;
567 M_IMGDATA
->m_data
[ pos
+1 ] = g
;
568 M_IMGDATA
->m_data
[ pos
+2 ] = b
;
571 unsigned char wxImage::GetRed( int x
, int y
) const
573 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
575 int w
= M_IMGDATA
->m_width
;
576 int h
= M_IMGDATA
->m_height
;
578 wxCHECK_MSG( (x
>=0) && (y
>=0) && (x
<w
) && (y
<h
), 0, wxT("invalid image index") );
580 long pos
= (y
* w
+ x
) * 3;
582 return M_IMGDATA
->m_data
[pos
];
585 unsigned char wxImage::GetGreen( int x
, int y
) const
587 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
589 int w
= M_IMGDATA
->m_width
;
590 int h
= M_IMGDATA
->m_height
;
592 wxCHECK_MSG( (x
>=0) && (y
>=0) && (x
<w
) && (y
<h
), 0, wxT("invalid image index") );
594 long pos
= (y
* w
+ x
) * 3;
596 return M_IMGDATA
->m_data
[pos
+1];
599 unsigned char wxImage::GetBlue( int x
, int y
) const
601 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
603 int w
= M_IMGDATA
->m_width
;
604 int h
= M_IMGDATA
->m_height
;
606 wxCHECK_MSG( (x
>=0) && (y
>=0) && (x
<w
) && (y
<h
), 0, wxT("invalid image index") );
608 long pos
= (y
* w
+ x
) * 3;
610 return M_IMGDATA
->m_data
[pos
+2];
613 bool wxImage::Ok() const
615 // image of 0 width or height can't be considered ok - at least because it
616 // causes crashes in ConvertToBitmap() if we don't catch it in time
617 wxImageRefData
*data
= M_IMGDATA
;
618 return data
&& data
->m_ok
&& data
->m_width
&& data
->m_height
;
621 char unsigned *wxImage::GetData() const
623 wxCHECK_MSG( Ok(), (char unsigned *)NULL
, wxT("invalid image") );
625 return M_IMGDATA
->m_data
;
628 void wxImage::SetData( char unsigned *data
)
630 wxCHECK_RET( Ok(), wxT("invalid image") );
632 wxImageRefData
*newRefData
= new wxImageRefData();
634 newRefData
->m_width
= M_IMGDATA
->m_width
;
635 newRefData
->m_height
= M_IMGDATA
->m_height
;
636 newRefData
->m_data
= data
;
637 newRefData
->m_ok
= TRUE
;
638 newRefData
->m_maskRed
= M_IMGDATA
->m_maskRed
;
639 newRefData
->m_maskGreen
= M_IMGDATA
->m_maskGreen
;
640 newRefData
->m_maskBlue
= M_IMGDATA
->m_maskBlue
;
641 newRefData
->m_hasMask
= M_IMGDATA
->m_hasMask
;
645 m_refData
= newRefData
;
648 void wxImage::SetData( char unsigned *data
, int new_width
, int new_height
)
650 wxImageRefData
*newRefData
= new wxImageRefData();
654 newRefData
->m_width
= new_width
;
655 newRefData
->m_height
= new_height
;
656 newRefData
->m_data
= data
;
657 newRefData
->m_ok
= TRUE
;
658 newRefData
->m_maskRed
= M_IMGDATA
->m_maskRed
;
659 newRefData
->m_maskGreen
= M_IMGDATA
->m_maskGreen
;
660 newRefData
->m_maskBlue
= M_IMGDATA
->m_maskBlue
;
661 newRefData
->m_hasMask
= M_IMGDATA
->m_hasMask
;
665 newRefData
->m_width
= new_width
;
666 newRefData
->m_height
= new_height
;
667 newRefData
->m_data
= data
;
668 newRefData
->m_ok
= TRUE
;
673 m_refData
= newRefData
;
676 void wxImage::SetMaskColour( unsigned char r
, unsigned char g
, unsigned char b
)
678 wxCHECK_RET( Ok(), wxT("invalid image") );
680 M_IMGDATA
->m_maskRed
= r
;
681 M_IMGDATA
->m_maskGreen
= g
;
682 M_IMGDATA
->m_maskBlue
= b
;
683 M_IMGDATA
->m_hasMask
= TRUE
;
686 unsigned char wxImage::GetMaskRed() const
688 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
690 return M_IMGDATA
->m_maskRed
;
693 unsigned char wxImage::GetMaskGreen() const
695 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
697 return M_IMGDATA
->m_maskGreen
;
700 unsigned char wxImage::GetMaskBlue() const
702 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
704 return M_IMGDATA
->m_maskBlue
;
707 void wxImage::SetMask( bool mask
)
709 wxCHECK_RET( Ok(), wxT("invalid image") );
711 M_IMGDATA
->m_hasMask
= mask
;
714 bool wxImage::HasMask() const
716 wxCHECK_MSG( Ok(), FALSE
, wxT("invalid image") );
718 return M_IMGDATA
->m_hasMask
;
721 int wxImage::GetWidth() const
723 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
725 return M_IMGDATA
->m_width
;
728 int wxImage::GetHeight() const
730 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
732 return M_IMGDATA
->m_height
;
736 bool wxImage::FindFirstUnusedColour(
737 unsigned char *r
, unsigned char *g
, unsigned char *b
,
738 unsigned char startR
, unsigned char startG
, unsigned char startB
) const
740 wxImageHistogram histogram
;
743 ComputeHistogram(histogram
);
745 unsigned char r2
= startR
;
746 unsigned char g2
= startG
;
747 unsigned char b2
= startB
;
749 key
= (r2
<< 16) | (g2
<< 8) | b2
;
751 while ( histogram
.find(key
) != histogram
.end() )
753 // color already used
765 wxLogError( _("GetUnusedColour:: No Unused Color in image ") );
771 key
= (r2
<< 16) | (g2
<< 8) | b2
;
782 bool wxImage::SetMaskFromImage(const wxImage
& mask
,
783 unsigned char mr
, unsigned char mg
, unsigned char mb
)
785 // check that the images are the same size
786 if ( (M_IMGDATA
->m_height
!= mask
.GetHeight() ) || (M_IMGDATA
->m_width
!= mask
.GetWidth () ) )
788 wxLogError( _("Image and Mask have different sizes") );
792 // find unused colour
793 unsigned char r
,g
,b
;
794 if (!FindFirstUnusedColour(&r
, &g
, &b
))
796 wxLogError( _("No Unused Color in image being masked") );
800 char unsigned *imgdata
= GetData();
801 char unsigned *maskdata
= mask
.GetData();
803 const int w
= GetWidth();
804 const int h
= GetHeight();
806 for (int j
= 0; j
< h
; j
++)
808 for (int i
= 0; i
< w
; i
++)
810 if ((maskdata
[0] == mr
) && (maskdata
[1] == mg
) && (maskdata
[2] == mb
))
821 SetMaskColour(r
, g
, b
);
831 bool wxImage::HasPalette() const
836 return M_IMGDATA
->m_palette
.Ok();
839 const wxPalette
& wxImage::GetPalette() const
841 wxCHECK_MSG( Ok(), wxNullPalette
, wxT("invalid image") );
843 return M_IMGDATA
->m_palette
;
846 void wxImage::SetPalette(const wxPalette
& palette
)
848 wxCHECK_RET( Ok(), wxT("invalid image") );
850 M_IMGDATA
->m_palette
= palette
;
853 #endif // wxUSE_PALETTE
855 // Option functions (arbitrary name/value mapping)
856 void wxImage::SetOption(const wxString
& name
, const wxString
& value
)
858 wxCHECK_RET( Ok(), wxT("invalid image") );
860 int idx
= M_IMGDATA
->m_optionNames
.Index(name
, FALSE
);
861 if (idx
== wxNOT_FOUND
)
863 M_IMGDATA
->m_optionNames
.Add(name
);
864 M_IMGDATA
->m_optionValues
.Add(value
);
868 M_IMGDATA
->m_optionNames
[idx
] = name
;
869 M_IMGDATA
->m_optionValues
[idx
] = value
;
873 void wxImage::SetOption(const wxString
& name
, int value
)
876 valStr
.Printf(wxT("%d"), value
);
877 SetOption(name
, valStr
);
880 wxString
wxImage::GetOption(const wxString
& name
) const
882 wxCHECK_MSG( Ok(), wxEmptyString
, wxT("invalid image") );
884 int idx
= M_IMGDATA
->m_optionNames
.Index(name
, FALSE
);
885 if (idx
== wxNOT_FOUND
)
886 return wxEmptyString
;
888 return M_IMGDATA
->m_optionValues
[idx
];
891 int wxImage::GetOptionInt(const wxString
& name
) const
893 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
895 return wxAtoi(GetOption(name
));
898 bool wxImage::HasOption(const wxString
& name
) const
900 wxCHECK_MSG( Ok(), FALSE
, wxT("invalid image") );
902 return (M_IMGDATA
->m_optionNames
.Index(name
, FALSE
) != wxNOT_FOUND
);
905 bool wxImage::LoadFile( const wxString
& filename
, long type
, int index
)
908 if (wxFileExists(filename
))
910 wxFileInputStream
stream(filename
);
911 wxBufferedInputStream
bstream( stream
);
912 return LoadFile(bstream
, type
, index
);
916 wxLogError( _("Can't load image from file '%s': file does not exist."), filename
.c_str() );
920 #else // !wxUSE_STREAMS
922 #endif // wxUSE_STREAMS
925 bool wxImage::LoadFile( const wxString
& filename
, const wxString
& mimetype
, int index
)
928 if (wxFileExists(filename
))
930 wxFileInputStream
stream(filename
);
931 wxBufferedInputStream
bstream( stream
);
932 return LoadFile(bstream
, mimetype
, index
);
936 wxLogError( _("Can't load image from file '%s': file does not exist."), filename
.c_str() );
940 #else // !wxUSE_STREAMS
942 #endif // wxUSE_STREAMS
947 bool wxImage::SaveFile( const wxString
& filename
) const
949 wxString ext
= filename
.AfterLast('.').Lower();
951 wxImageHandler
* pHandler
= FindHandler(ext
, -1);
954 SaveFile(filename
, pHandler
->GetType());
958 wxLogError(_("Can't save image to file '%s': unknown extension."), filename
.c_str());
963 bool wxImage::SaveFile( const wxString
& filename
, int type
) const
966 ((wxImage
*)this)->SetOption(wxIMAGE_OPTION_FILENAME
, filename
);
968 wxFileOutputStream
stream(filename
);
972 wxBufferedOutputStream
bstream( stream
);
973 return SaveFile(bstream
, type
);
975 #endif // wxUSE_STREAMS
980 bool wxImage::SaveFile( const wxString
& filename
, const wxString
& mimetype
) const
983 ((wxImage
*)this)->SetOption(wxIMAGE_OPTION_FILENAME
, filename
);
985 wxFileOutputStream
stream(filename
);
989 wxBufferedOutputStream
bstream( stream
);
990 return SaveFile(bstream
, mimetype
);
992 #endif // wxUSE_STREAMS
997 bool wxImage::CanRead( const wxString
&name
)
1000 wxFileInputStream
stream(name
);
1001 return CanRead(stream
);
1007 int wxImage::GetImageCount( const wxString
&name
, long type
)
1010 wxFileInputStream
stream(name
);
1012 return GetImageCount(stream
, type
);
1020 bool wxImage::CanRead( wxInputStream
&stream
)
1022 const wxList
& list
= GetHandlers();
1024 for ( wxList::Node
*node
= list
.GetFirst(); node
; node
= node
->GetNext() )
1026 wxImageHandler
*handler
=(wxImageHandler
*)node
->GetData();
1027 if (handler
->CanRead( stream
))
1034 int wxImage::GetImageCount( wxInputStream
&stream
, long type
)
1036 wxImageHandler
*handler
;
1038 if ( type
== wxBITMAP_TYPE_ANY
)
1040 wxList
&list
=GetHandlers();
1042 for (wxList::Node
*node
= list
.GetFirst(); node
; node
= node
->GetNext())
1044 handler
=(wxImageHandler
*)node
->GetData();
1045 if ( handler
->CanRead(stream
) )
1046 return handler
->GetImageCount(stream
);
1050 wxLogWarning(_("No handler found for image type."));
1054 handler
= FindHandler(type
);
1058 wxLogWarning(_("No image handler for type %d defined."), type
);
1062 if ( handler
->CanRead(stream
) )
1064 return handler
->GetImageCount(stream
);
1068 wxLogError(_("Image file is not of type %d."), type
);
1073 bool wxImage::LoadFile( wxInputStream
& stream
, long type
, int index
)
1077 m_refData
= new wxImageRefData
;
1079 wxImageHandler
*handler
;
1081 if ( type
== wxBITMAP_TYPE_ANY
)
1083 wxList
&list
=GetHandlers();
1085 for ( wxList::Node
*node
= list
.GetFirst(); node
; node
= node
->GetNext() )
1087 handler
=(wxImageHandler
*)node
->GetData();
1088 if ( handler
->CanRead(stream
) )
1089 return handler
->LoadFile(this, stream
, TRUE
/*verbose*/, index
);
1093 wxLogWarning( _("No handler found for image type.") );
1097 handler
= FindHandler(type
);
1101 wxLogWarning( _("No image handler for type %d defined."), type
);
1106 return handler
->LoadFile(this, stream
, TRUE
/*verbose*/, index
);
1109 bool wxImage::LoadFile( wxInputStream
& stream
, const wxString
& mimetype
, int index
)
1113 m_refData
= new wxImageRefData
;
1115 wxImageHandler
*handler
= FindHandlerMime(mimetype
);
1119 wxLogWarning( _("No image handler for type %s defined."), mimetype
.GetData() );
1124 return handler
->LoadFile( this, stream
, TRUE
/*verbose*/, index
);
1127 bool wxImage::SaveFile( wxOutputStream
& stream
, int type
) const
1129 wxCHECK_MSG( Ok(), FALSE
, wxT("invalid image") );
1131 wxImageHandler
*handler
= FindHandler(type
);
1135 wxLogWarning( _("No image handler for type %d defined."), type
);
1140 return handler
->SaveFile( (wxImage
*)this, stream
);
1143 bool wxImage::SaveFile( wxOutputStream
& stream
, const wxString
& mimetype
) const
1145 wxCHECK_MSG( Ok(), FALSE
, wxT("invalid image") );
1147 wxImageHandler
*handler
= FindHandlerMime(mimetype
);
1151 wxLogWarning( _("No image handler for type %s defined."), mimetype
.GetData() );
1156 return handler
->SaveFile( (wxImage
*)this, stream
);
1158 #endif // wxUSE_STREAMS
1160 void wxImage::AddHandler( wxImageHandler
*handler
)
1162 // make sure that the memory will be freed at the program end
1163 sm_handlers
.DeleteContents(TRUE
);
1165 // Check for an existing handler of the type being added.
1166 if (FindHandler( handler
->GetType() ) == 0)
1168 sm_handlers
.Append( handler
);
1172 // This is not documented behaviour, merely the simplest 'fix'
1173 // for preventing duplicate additions. If someone ever has
1174 // a good reason to add and remove duplicate handlers (and they
1175 // may) we should probably refcount the duplicates.
1176 // also an issue in InsertHandler below.
1178 wxLogDebug( _T("Adding duplicate image handler for '%s'"),
1179 handler
->GetName().c_str() );
1184 void wxImage::InsertHandler( wxImageHandler
*handler
)
1186 // make sure that the memory will be freed at the program end
1187 sm_handlers
.DeleteContents(TRUE
);
1189 // Check for an existing handler of the type being added.
1190 if (FindHandler( handler
->GetType() ) == 0)
1192 sm_handlers
.Insert( handler
);
1196 // see AddHandler for additional comments.
1197 wxLogDebug( _T("Inserting duplicate image handler for '%s'"),
1198 handler
->GetName().c_str() );
1203 bool wxImage::RemoveHandler( const wxString
& name
)
1205 wxImageHandler
*handler
= FindHandler(name
);
1208 sm_handlers
.DeleteObject(handler
);
1215 wxImageHandler
*wxImage::FindHandler( const wxString
& name
)
1217 wxNode
*node
= sm_handlers
.GetFirst();
1220 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
1221 if (handler
->GetName().Cmp(name
) == 0) return handler
;
1223 node
= node
->GetNext();
1228 wxImageHandler
*wxImage::FindHandler( const wxString
& extension
, long bitmapType
)
1230 wxNode
*node
= sm_handlers
.GetFirst();
1233 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
1234 if ( (handler
->GetExtension().Cmp(extension
) == 0) &&
1235 (bitmapType
== -1 || handler
->GetType() == bitmapType
) )
1237 node
= node
->GetNext();
1242 wxImageHandler
*wxImage::FindHandler( long bitmapType
)
1244 wxNode
*node
= sm_handlers
.GetFirst();
1247 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
1248 if (handler
->GetType() == bitmapType
) return handler
;
1249 node
= node
->GetNext();
1254 wxImageHandler
*wxImage::FindHandlerMime( const wxString
& mimetype
)
1256 wxNode
*node
= sm_handlers
.GetFirst();
1259 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
1260 if (handler
->GetMimeType().IsSameAs(mimetype
, FALSE
)) return handler
;
1261 node
= node
->GetNext();
1266 void wxImage::InitStandardHandlers()
1269 AddHandler(new wxBMPHandler
);
1270 #endif // wxUSE_STREAMS
1273 void wxImage::CleanUpHandlers()
1275 wxNode
*node
= sm_handlers
.GetFirst();
1278 wxImageHandler
*handler
= (wxImageHandler
*)node
->GetData();
1279 wxNode
*next
= node
->GetNext();
1286 //-----------------------------------------------------------------------------
1288 //-----------------------------------------------------------------------------
1290 IMPLEMENT_ABSTRACT_CLASS(wxImageHandler
,wxObject
)
1293 bool wxImageHandler::LoadFile( wxImage
*WXUNUSED(image
), wxInputStream
& WXUNUSED(stream
), bool WXUNUSED(verbose
), int WXUNUSED(index
) )
1298 bool wxImageHandler::SaveFile( wxImage
*WXUNUSED(image
), wxOutputStream
& WXUNUSED(stream
), bool WXUNUSED(verbose
) )
1303 int wxImageHandler::GetImageCount( wxInputStream
& WXUNUSED(stream
) )
1308 bool wxImageHandler::CanRead( const wxString
& name
)
1310 if (wxFileExists(name
))
1312 wxFileInputStream
stream(name
);
1313 return CanRead(stream
);
1316 wxLogError( _("Can't check image format of file '%s': file does not exist."), name
.c_str() );
1321 bool wxImageHandler::CallDoCanRead(wxInputStream
& stream
)
1323 off_t posOld
= stream
.TellI();
1324 if ( posOld
== wxInvalidOffset
)
1326 // can't test unseekable stream
1330 bool ok
= DoCanRead(stream
);
1332 // restore the old position to be able to test other formats and so on
1333 if ( stream
.SeekI(posOld
) == wxInvalidOffset
)
1335 wxLogDebug(_T("Failed to rewind the stream in wxImageHandler!"));
1337 // reading would fail anyhow as we're not at the right position
1344 #endif // wxUSE_STREAMS
1348 //-----------------------------------------------------------------------------
1349 // Deprecated wxBitmap conversion routines
1350 //-----------------------------------------------------------------------------
1352 #if WXWIN_COMPATIBILITY_2_2 && wxUSE_GUI
1355 wxBitmap
wxImage::ConvertToMonoBitmap( unsigned char red
, unsigned char green
, unsigned char blue
) const
1357 wxImage mono
= this->ConvertToMono( red
, green
, blue
);
1358 wxBitmap
bitmap( mono
, 1 );
1363 wxBitmap
wxImage::ConvertToBitmap() const
1365 wxBitmap
bitmap( *this );
1369 wxImage::wxImage( const wxBitmap
&bitmap
)
1371 *this = bitmap
.ConvertToImage();
1374 #endif // WXWIN_COMPATIBILITY_2_2 && wxUSE_GUI
1377 //-----------------------------------------------------------------------------
1380 // Counts and returns the number of different colours. Optionally stops
1381 // when it exceeds 'stopafter' different colours. This is useful, for
1382 // example, to see if the image can be saved as 8-bit (256 colour or
1383 // less, in this case it would be invoked as CountColours(256)). Default
1384 // value for stopafter is -1 (don't care).
1386 unsigned long wxImage::CountColours( unsigned long stopafter
) const
1390 unsigned char r
, g
, b
;
1392 unsigned long size
, nentries
, key
;
1395 size
= GetWidth() * GetHeight();
1398 for (unsigned long j
= 0; (j
< size
) && (nentries
<= stopafter
) ; j
++)
1403 key
= (r
<< 16) | (g
<< 8) | b
;
1405 if (h
.Get(key
) == NULL
)
1416 unsigned long wxImage::ComputeHistogram( wxImageHistogram
&h
) const
1418 unsigned char r
, g
, b
;
1420 unsigned long size
, nentries
, key
;
1425 size
= GetWidth() * GetHeight();
1428 for (unsigned long j
= 0; j
< size
; j
++)
1433 key
= (r
<< 16) | (g
<< 8) | b
;
1435 wxImageHistogramEntry
& entry
= h
[key
];
1436 if ( entry
.value
++ == 0 )
1437 entry
.index
= nentries
++;
1444 * Rotation code by Carlos Moreno
1447 // GRG: I've removed wxRotationPoint - we already have wxRealPoint which
1448 // does exactly the same thing. And I also got rid of wxRotationPixel
1449 // bacause of potential problems in architectures where alignment
1450 // is an issue, so I had to rewrite parts of the code.
1452 static const double gs_Epsilon
= 1e-10;
1454 static inline int wxCint (double x
)
1456 return (x
> 0) ? (int) (x
+ 0.5) : (int) (x
- 0.5);
1460 // Auxiliary function to rotate a point (x,y) with respect to point p0
1461 // make it inline and use a straight return to facilitate optimization
1462 // also, the function receives the sine and cosine of the angle to avoid
1463 // repeating the time-consuming calls to these functions -- sin/cos can
1464 // be computed and stored in the calling function.
1466 inline wxRealPoint
rotated_point (const wxRealPoint
& p
, double cos_angle
, double sin_angle
, const wxRealPoint
& p0
)
1468 return wxRealPoint (p0
.x
+ (p
.x
- p0
.x
) * cos_angle
- (p
.y
- p0
.y
) * sin_angle
,
1469 p0
.y
+ (p
.y
- p0
.y
) * cos_angle
+ (p
.x
- p0
.x
) * sin_angle
);
1472 inline wxRealPoint
rotated_point (double x
, double y
, double cos_angle
, double sin_angle
, const wxRealPoint
& p0
)
1474 return rotated_point (wxRealPoint(x
,y
), cos_angle
, sin_angle
, p0
);
1477 wxImage
wxImage::Rotate(double angle
, const wxPoint
& centre_of_rotation
, bool interpolating
, wxPoint
* offset_after_rotation
) const
1480 angle
= -angle
; // screen coordinates are a mirror image of "real" coordinates
1482 // Create pointer-based array to accelerate access to wxImage's data
1483 unsigned char ** data
= new unsigned char * [GetHeight()];
1485 data
[0] = GetData();
1487 for (i
= 1; i
< GetHeight(); i
++)
1488 data
[i
] = data
[i
- 1] + (3 * GetWidth());
1490 // precompute coefficients for rotation formula
1491 // (sine and cosine of the angle)
1492 const double cos_angle
= cos(angle
);
1493 const double sin_angle
= sin(angle
);
1495 // Create new Image to store the result
1496 // First, find rectangle that covers the rotated image; to do that,
1497 // rotate the four corners
1499 const wxRealPoint
p0(centre_of_rotation
.x
, centre_of_rotation
.y
);
1501 wxRealPoint p1
= rotated_point (0, 0, cos_angle
, sin_angle
, p0
);
1502 wxRealPoint p2
= rotated_point (0, GetHeight(), cos_angle
, sin_angle
, p0
);
1503 wxRealPoint p3
= rotated_point (GetWidth(), 0, cos_angle
, sin_angle
, p0
);
1504 wxRealPoint p4
= rotated_point (GetWidth(), GetHeight(), cos_angle
, sin_angle
, p0
);
1506 int x1
= (int) floor (wxMin (wxMin(p1
.x
, p2
.x
), wxMin(p3
.x
, p4
.x
)));
1507 int y1
= (int) floor (wxMin (wxMin(p1
.y
, p2
.y
), wxMin(p3
.y
, p4
.y
)));
1508 int x2
= (int) ceil (wxMax (wxMax(p1
.x
, p2
.x
), wxMax(p3
.x
, p4
.x
)));
1509 int y2
= (int) ceil (wxMax (wxMax(p1
.y
, p2
.y
), wxMax(p3
.y
, p4
.y
)));
1511 wxImage
rotated (x2
- x1
+ 1, y2
- y1
+ 1);
1513 if (offset_after_rotation
!= NULL
)
1515 *offset_after_rotation
= wxPoint (x1
, y1
);
1518 // GRG: The rotated (destination) image is always accessed
1519 // sequentially, so there is no need for a pointer-based
1520 // array here (and in fact it would be slower).
1522 unsigned char * dst
= rotated
.GetData();
1524 // GRG: if the original image has a mask, use its RGB values
1525 // as the blank pixel, else, fall back to default (black).
1527 unsigned char blank_r
= 0;
1528 unsigned char blank_g
= 0;
1529 unsigned char blank_b
= 0;
1533 blank_r
= GetMaskRed();
1534 blank_g
= GetMaskGreen();
1535 blank_b
= GetMaskBlue();
1536 rotated
.SetMaskColour( blank_r
, blank_g
, blank_b
);
1539 // Now, for each point of the rotated image, find where it came from, by
1540 // performing an inverse rotation (a rotation of -angle) and getting the
1541 // pixel at those coordinates
1543 // GRG: I've taken the (interpolating) test out of the loops, so that
1544 // it is done only once, instead of repeating it for each pixel.
1549 for (int y
= 0; y
< rotated
.GetHeight(); y
++)
1551 for (x
= 0; x
< rotated
.GetWidth(); x
++)
1553 wxRealPoint src
= rotated_point (x
+ x1
, y
+ y1
, cos_angle
, -sin_angle
, p0
);
1555 if (-0.25 < src
.x
&& src
.x
< GetWidth() - 0.75 &&
1556 -0.25 < src
.y
&& src
.y
< GetHeight() - 0.75)
1558 // interpolate using the 4 enclosing grid-points. Those
1559 // points can be obtained using floor and ceiling of the
1560 // exact coordinates of the point
1561 // C.M. 2000-02-17: when the point is near the border, special care is required.
1565 if (0 < src
.x
&& src
.x
< GetWidth() - 1)
1567 x1
= wxCint(floor(src
.x
));
1568 x2
= wxCint(ceil(src
.x
));
1570 else // else means that x is near one of the borders (0 or width-1)
1572 x1
= x2
= wxCint (src
.x
);
1575 if (0 < src
.y
&& src
.y
< GetHeight() - 1)
1577 y1
= wxCint(floor(src
.y
));
1578 y2
= wxCint(ceil(src
.y
));
1582 y1
= y2
= wxCint (src
.y
);
1585 // get four points and the distances (square of the distance,
1586 // for efficiency reasons) for the interpolation formula
1588 // GRG: Do not calculate the points until they are
1589 // really needed -- this way we can calculate
1590 // just one, instead of four, if d1, d2, d3
1591 // or d4 are < gs_Epsilon
1593 const double d1
= (src
.x
- x1
) * (src
.x
- x1
) + (src
.y
- y1
) * (src
.y
- y1
);
1594 const double d2
= (src
.x
- x2
) * (src
.x
- x2
) + (src
.y
- y1
) * (src
.y
- y1
);
1595 const double d3
= (src
.x
- x2
) * (src
.x
- x2
) + (src
.y
- y2
) * (src
.y
- y2
);
1596 const double d4
= (src
.x
- x1
) * (src
.x
- x1
) + (src
.y
- y2
) * (src
.y
- y2
);
1598 // Now interpolate as a weighted average of the four surrounding
1599 // points, where the weights are the distances to each of those points
1601 // If the point is exactly at one point of the grid of the source
1602 // image, then don't interpolate -- just assign the pixel
1604 if (d1
< gs_Epsilon
) // d1,d2,d3,d4 are positive -- no need for abs()
1606 unsigned char *p
= data
[y1
] + (3 * x1
);
1611 else if (d2
< gs_Epsilon
)
1613 unsigned char *p
= data
[y1
] + (3 * x2
);
1618 else if (d3
< gs_Epsilon
)
1620 unsigned char *p
= data
[y2
] + (3 * x2
);
1625 else if (d4
< gs_Epsilon
)
1627 unsigned char *p
= data
[y2
] + (3 * x1
);
1634 // weights for the weighted average are proportional to the inverse of the distance
1635 unsigned char *v1
= data
[y1
] + (3 * x1
);
1636 unsigned char *v2
= data
[y1
] + (3 * x2
);
1637 unsigned char *v3
= data
[y2
] + (3 * x2
);
1638 unsigned char *v4
= data
[y2
] + (3 * x1
);
1640 const double w1
= 1/d1
, w2
= 1/d2
, w3
= 1/d3
, w4
= 1/d4
;
1644 *(dst
++) = (unsigned char)
1645 ( (w1
* *(v1
++) + w2
* *(v2
++) +
1646 w3
* *(v3
++) + w4
* *(v4
++)) /
1647 (w1
+ w2
+ w3
+ w4
) );
1648 *(dst
++) = (unsigned char)
1649 ( (w1
* *(v1
++) + w2
* *(v2
++) +
1650 w3
* *(v3
++) + w4
* *(v4
++)) /
1651 (w1
+ w2
+ w3
+ w4
) );
1652 *(dst
++) = (unsigned char)
1653 ( (w1
* *(v1
++) + w2
* *(v2
++) +
1654 w3
* *(v3
++) + w4
* *(v4
++)) /
1655 (w1
+ w2
+ w3
+ w4
) );
1667 else // not interpolating
1669 for (int y
= 0; y
< rotated
.GetHeight(); y
++)
1671 for (x
= 0; x
< rotated
.GetWidth(); x
++)
1673 wxRealPoint src
= rotated_point (x
+ x1
, y
+ y1
, cos_angle
, -sin_angle
, p0
);
1675 const int xs
= wxCint (src
.x
); // wxCint rounds to the
1676 const int ys
= wxCint (src
.y
); // closest integer
1678 if (0 <= xs
&& xs
< GetWidth() &&
1679 0 <= ys
&& ys
< GetHeight())
1681 unsigned char *p
= data
[ys
] + (3 * xs
);
1705 // A module to allow wxImage initialization/cleanup
1706 // without calling these functions from app.cpp or from
1707 // the user's application.
1709 class wxImageModule
: public wxModule
1711 DECLARE_DYNAMIC_CLASS(wxImageModule
)
1714 bool OnInit() { wxImage::InitStandardHandlers(); return TRUE
; };
1715 void OnExit() { wxImage::CleanUpHandlers(); };
1718 IMPLEMENT_DYNAMIC_CLASS(wxImageModule
, wxModule
)
1721 #endif // wxUSE_IMAGE