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"
22 #include "wx/bitmap.h"
26 #include "wx/filefn.h"
27 #include "wx/wfstream.h"
29 #include "wx/module.h"
39 #include "wx/msw/private.h"
42 //-----------------------------------------------------------------------------
44 //-----------------------------------------------------------------------------
46 class wxImageRefData
: public wxObjectRefData
55 unsigned char *m_data
;
57 unsigned char m_maskRed
,m_maskGreen
,m_maskBlue
;
61 wxImageRefData::wxImageRefData()
65 m_data
= (unsigned char*) NULL
;
73 wxImageRefData::~wxImageRefData()
75 if (m_data
) free( m_data
);
78 wxList
wxImage::sm_handlers
;
80 //-----------------------------------------------------------------------------
82 #define M_IMGDATA ((wxImageRefData *)m_refData)
84 #if !USE_SHARED_LIBRARIES
85 IMPLEMENT_DYNAMIC_CLASS(wxImage
, wxObject
)
92 wxImage::wxImage( int width
, int height
)
94 Create( width
, height
);
97 wxImage::wxImage( const wxString
& name
, long type
)
99 LoadFile( name
, type
);
102 wxImage::wxImage( const wxString
& name
, const wxString
& mimetype
)
104 LoadFile( name
, mimetype
);
108 wxImage::wxImage( wxInputStream
& stream
, long type
)
110 LoadFile( stream
, type
);
113 wxImage::wxImage( wxInputStream
& stream
, const wxString
& mimetype
)
115 LoadFile( stream
, mimetype
);
117 #endif // wxUSE_STREAMS
119 wxImage::wxImage( const wxImage
& image
)
124 wxImage::wxImage( const wxImage
* image
)
126 if (image
) Ref(*image
);
129 void wxImage::Create( int width
, int height
)
131 m_refData
= new wxImageRefData();
133 M_IMGDATA
->m_data
= (unsigned char *) malloc( width
*height
*3 );
134 if (M_IMGDATA
->m_data
)
136 for (int l
= 0; l
< width
*height
*3; l
++) M_IMGDATA
->m_data
[l
] = 0;
138 M_IMGDATA
->m_width
= width
;
139 M_IMGDATA
->m_height
= height
;
140 M_IMGDATA
->m_ok
= TRUE
;
148 void wxImage::Destroy()
153 wxImage
wxImage::Scale( int width
, int height
) const
157 wxCHECK_MSG( Ok(), image
, _T("invalid image") );
159 wxCHECK_MSG( (width
> 0) && (height
> 0), image
, _T("invalid image size") );
161 image
.Create( width
, height
);
163 char unsigned *data
= image
.GetData();
165 wxCHECK_MSG( data
, image
, _T("unable to create image") );
167 if (M_IMGDATA
->m_hasMask
)
168 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
170 long old_height
= M_IMGDATA
->m_height
;
171 long old_width
= M_IMGDATA
->m_width
;
173 char unsigned *source_data
= M_IMGDATA
->m_data
;
174 char unsigned *target_data
= data
;
176 for (long j
= 0; j
< height
; j
++)
178 long y_offset
= (j
* old_height
/ height
) * old_width
;
180 for (long i
= 0; i
< width
; i
++)
183 source_data
+ 3*(y_offset
+ ((i
* old_width
)/ width
)),
192 wxImage
wxImage::GetSubImage( const wxRect
&rect
) const
196 wxCHECK_MSG( Ok(), image
, _T("invalid image") );
198 wxCHECK_MSG( (rect
.GetLeft()>=0) && (rect
.GetTop()>=0) && (rect
.GetRight()<=GetWidth()) && (rect
.GetBottom()<=GetHeight())
199 , image
, _T("invalid subimage size") );
201 int subwidth
=rect
.GetWidth();
202 const int subheight
=rect
.GetHeight();
204 image
.Create( subwidth
, subheight
);
206 char unsigned *subdata
= image
.GetData(), *data
=GetData();
208 wxCHECK_MSG( subdata
, image
, _T("unable to create image") );
210 if (M_IMGDATA
->m_hasMask
)
211 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
213 const int subleft
=3*rect
.GetLeft();
214 const int width
=3*GetWidth();
217 data
+=rect
.GetTop()*width
+subleft
;
219 for (long j
= 0; j
< subheight
; ++j
)
221 memcpy( subdata
, data
, subwidth
);
229 void wxImage::SetRGB( int x
, int y
, unsigned char r
, unsigned char g
, unsigned char b
)
231 wxCHECK_RET( Ok(), _T("invalid image") );
233 int w
= M_IMGDATA
->m_width
;
234 int h
= M_IMGDATA
->m_height
;
236 wxCHECK_RET( (x
>=0) && (y
>=0) && (x
<w
) && (y
<h
), _T("invalid image index") );
238 long pos
= (y
* w
+ x
) * 3;
240 M_IMGDATA
->m_data
[ pos
] = r
;
241 M_IMGDATA
->m_data
[ pos
+1 ] = g
;
242 M_IMGDATA
->m_data
[ pos
+2 ] = b
;
245 unsigned char wxImage::GetRed( int x
, int y
)
247 wxCHECK_MSG( Ok(), 0, _T("invalid image") );
249 int w
= M_IMGDATA
->m_width
;
250 int h
= M_IMGDATA
->m_height
;
252 wxCHECK_MSG( (x
>=0) && (y
>=0) && (x
<w
) && (y
<h
), 0, _T("invalid image index") );
254 long pos
= (y
* w
+ x
) * 3;
256 return M_IMGDATA
->m_data
[pos
];
259 unsigned char wxImage::GetGreen( int x
, int y
)
261 wxCHECK_MSG( Ok(), 0, _T("invalid image") );
263 int w
= M_IMGDATA
->m_width
;
264 int h
= M_IMGDATA
->m_height
;
266 wxCHECK_MSG( (x
>=0) && (y
>=0) && (x
<w
) && (y
<h
), 0, _T("invalid image index") );
268 long pos
= (y
* w
+ x
) * 3;
270 return M_IMGDATA
->m_data
[pos
+1];
273 unsigned char wxImage::GetBlue( int x
, int y
)
275 wxCHECK_MSG( Ok(), 0, _T("invalid image") );
277 int w
= M_IMGDATA
->m_width
;
278 int h
= M_IMGDATA
->m_height
;
280 wxCHECK_MSG( (x
>=0) && (y
>=0) && (x
<w
) && (y
<h
), 0, _T("invalid image index") );
282 long pos
= (y
* w
+ x
) * 3;
284 return M_IMGDATA
->m_data
[pos
+2];
287 bool wxImage::Ok() const
289 return (M_IMGDATA
&& M_IMGDATA
->m_ok
);
292 char unsigned *wxImage::GetData() const
294 wxCHECK_MSG( Ok(), (char unsigned *)NULL
, _T("invalid image") );
296 return M_IMGDATA
->m_data
;
299 void wxImage::SetData( char unsigned *data
)
301 wxCHECK_RET( Ok(), _T("invalid image") );
303 wxImageRefData
*newRefData
= new wxImageRefData();
305 newRefData
->m_width
= M_IMGDATA
->m_width
;
306 newRefData
->m_height
= M_IMGDATA
->m_height
;
307 newRefData
->m_data
= data
;
308 newRefData
->m_ok
= TRUE
;
309 newRefData
->m_maskRed
= M_IMGDATA
->m_maskRed
;
310 newRefData
->m_maskGreen
= M_IMGDATA
->m_maskGreen
;
311 newRefData
->m_maskBlue
= M_IMGDATA
->m_maskBlue
;
312 newRefData
->m_hasMask
= M_IMGDATA
->m_hasMask
;
316 m_refData
= newRefData
;
319 void wxImage::SetMaskColour( unsigned char r
, unsigned char g
, unsigned char b
)
321 wxCHECK_RET( Ok(), _T("invalid image") );
323 M_IMGDATA
->m_maskRed
= r
;
324 M_IMGDATA
->m_maskGreen
= g
;
325 M_IMGDATA
->m_maskBlue
= b
;
326 M_IMGDATA
->m_hasMask
= TRUE
;
329 unsigned char wxImage::GetMaskRed() const
331 wxCHECK_MSG( Ok(), 0, _T("invalid image") );
333 return M_IMGDATA
->m_maskRed
;
336 unsigned char wxImage::GetMaskGreen() const
338 wxCHECK_MSG( Ok(), 0, _T("invalid image") );
340 return M_IMGDATA
->m_maskGreen
;
343 unsigned char wxImage::GetMaskBlue() const
345 wxCHECK_MSG( Ok(), 0, _T("invalid image") );
347 return M_IMGDATA
->m_maskBlue
;
350 void wxImage::SetMask( bool mask
)
352 wxCHECK_RET( Ok(), _T("invalid image") );
354 M_IMGDATA
->m_hasMask
= mask
;
357 bool wxImage::HasMask() const
359 wxCHECK_MSG( Ok(), FALSE
, _T("invalid image") );
361 return M_IMGDATA
->m_hasMask
;
364 int wxImage::GetWidth() const
366 wxCHECK_MSG( Ok(), 0, _T("invalid image") );
368 return M_IMGDATA
->m_width
;
371 int wxImage::GetHeight() const
373 wxCHECK_MSG( Ok(), 0, _T("invalid image") );
375 return M_IMGDATA
->m_height
;
378 bool wxImage::LoadFile( const wxString
& filename
, long type
)
381 if (wxFileExists(filename
))
383 wxFileInputStream
stream(filename
);
384 return LoadFile(stream
, type
);
388 wxLogError( _T("Can't load image from file '%s': file does not exist."), filename
.c_str() );
392 #else // !wxUSE_STREAMS
394 #endif // wxUSE_STREAMS
397 bool wxImage::LoadFile( const wxString
& filename
, const wxString
& mimetype
)
400 if (wxFileExists(filename
))
402 wxFileInputStream
stream(filename
);
403 return LoadFile(stream
, mimetype
);
407 wxLogError( _T("Can't load image from file '%s': file does not exist."), filename
.c_str() );
411 #else // !wxUSE_STREAMS
413 #endif // wxUSE_STREAMS
416 bool wxImage::SaveFile( const wxString
& filename
, int type
)
419 wxFileOutputStream
stream(filename
);
421 if ( stream
.LastError() == wxStream_NOERROR
)
422 return SaveFile(stream
, type
);
424 #endif // wxUSE_STREAMS
428 bool wxImage::SaveFile( const wxString
& filename
, const wxString
& mimetype
)
431 wxFileOutputStream
stream(filename
);
433 if ( stream
.LastError() == wxStream_NOERROR
)
434 return SaveFile(stream
, mimetype
);
436 #endif // wxUSE_STREAMS
442 bool wxImage::LoadFile( wxInputStream
& stream
, long type
)
446 m_refData
= new wxImageRefData
;
448 wxImageHandler
*handler
;
450 if (type
==wxBITMAP_TYPE_ANY
)
452 wxList
&list
=GetHandlers();
454 for ( wxList::Node
*node
= list
.GetFirst(); node
; node
= node
->GetNext() )
456 handler
=(wxImageHandler
*)node
->GetData();
457 if (handler
->CanRead( stream
))
458 return handler
->LoadFile( this, stream
);
462 wxLogWarning( _T("No handler found for this image.") );
466 handler
= FindHandler(type
);
470 wxLogWarning( _T("No image handler for type %d defined."), type
);
475 return handler
->LoadFile( this, stream
);
478 bool wxImage::LoadFile( wxInputStream
& stream
, const wxString
& mimetype
)
482 m_refData
= new wxImageRefData
;
484 wxImageHandler
*handler
= FindHandlerMime(mimetype
);
488 wxLogWarning( _T("No image handler for type %s defined."), mimetype
.GetData() );
493 return handler
->LoadFile( this, stream
);
496 bool wxImage::SaveFile( wxOutputStream
& stream
, int type
)
498 wxCHECK_MSG( Ok(), FALSE
, _T("invalid image") );
500 wxImageHandler
*handler
= FindHandler(type
);
504 wxLogWarning( _T("No image handler for type %d defined."), type
);
509 return handler
->SaveFile( this, stream
);
512 bool wxImage::SaveFile( wxOutputStream
& stream
, const wxString
& mimetype
)
514 wxCHECK_MSG( Ok(), FALSE
, _T("invalid image") );
516 wxImageHandler
*handler
= FindHandlerMime(mimetype
);
520 wxLogWarning( _T("No image handler for type %s defined."), mimetype
.GetData() );
525 return handler
->SaveFile( this, stream
);
527 #endif // wxUSE_STREAMS
529 void wxImage::AddHandler( wxImageHandler
*handler
)
531 // make sure that the memory will be freed at the program end
532 sm_handlers
.DeleteContents(TRUE
);
534 sm_handlers
.Append( handler
);
537 void wxImage::InsertHandler( wxImageHandler
*handler
)
539 // make sure that the memory will be freed at the program end
540 sm_handlers
.DeleteContents(TRUE
);
542 sm_handlers
.Insert( handler
);
545 bool wxImage::RemoveHandler( const wxString
& name
)
547 wxImageHandler
*handler
= FindHandler(name
);
550 sm_handlers
.DeleteObject(handler
);
557 wxImageHandler
*wxImage::FindHandler( const wxString
& name
)
559 wxNode
*node
= sm_handlers
.First();
562 wxImageHandler
*handler
= (wxImageHandler
*)node
->Data();
563 if (handler
->GetName().Cmp(name
) == 0) return handler
;
567 return (wxImageHandler
*)NULL
;
570 wxImageHandler
*wxImage::FindHandler( const wxString
& extension
, long bitmapType
)
572 wxNode
*node
= sm_handlers
.First();
575 wxImageHandler
*handler
= (wxImageHandler
*)node
->Data();
576 if ( (handler
->GetExtension().Cmp(extension
) == 0) &&
577 (bitmapType
== -1 || handler
->GetType() == bitmapType
) )
581 return (wxImageHandler
*)NULL
;
584 wxImageHandler
*wxImage::FindHandler( long bitmapType
)
586 wxNode
*node
= sm_handlers
.First();
589 wxImageHandler
*handler
= (wxImageHandler
*)node
->Data();
590 if (handler
->GetType() == bitmapType
) return handler
;
596 wxImageHandler
*wxImage::FindHandlerMime( const wxString
& mimetype
)
598 wxNode
*node
= sm_handlers
.First();
601 wxImageHandler
*handler
= (wxImageHandler
*)node
->Data();
602 if (handler
->GetMimeType().IsSameAs(mimetype
, FALSE
)) return handler
;
608 void wxImage::InitStandardHandlers()
610 AddHandler( new wxBMPHandler
);
613 void wxImage::CleanUpHandlers()
615 wxNode
*node
= sm_handlers
.First();
618 wxImageHandler
*handler
= (wxImageHandler
*)node
->Data();
619 wxNode
*next
= node
->Next();
626 //-----------------------------------------------------------------------------
628 //-----------------------------------------------------------------------------
630 #if !USE_SHARED_LIBRARIES
631 IMPLEMENT_DYNAMIC_CLASS(wxImageHandler
,wxObject
)
635 bool wxImageHandler::LoadFile( wxImage
*WXUNUSED(image
), wxInputStream
& WXUNUSED(stream
), bool WXUNUSED(verbose
) )
640 bool wxImageHandler::SaveFile( wxImage
*WXUNUSED(image
), wxOutputStream
& WXUNUSED(stream
), bool WXUNUSED(verbose
) )
645 bool wxImageHandler::CanRead( wxInputStream
& stream
)
650 bool wxImageHandler::CanRead( const wxString
& name
)
653 if (wxFileExists(name
))
655 wxFileInputStream
stream(name
);
656 return CanRead(stream
);
660 wxLogError( _T("Can't check image format of file '%s': file does not exist."), name
.c_str() );
664 #else // !wxUSE_STREAMS
666 #endif // wxUSE_STREAMS
671 #endif // wxUSE_STREAMS
673 //-----------------------------------------------------------------------------
674 // MSW conversion routines
675 //-----------------------------------------------------------------------------
679 wxBitmap
wxImage::ConvertToBitmap() const
684 // sizeLimit is the MS upper limit for the DIB size
686 int sizeLimit
= 1024*768*3;
688 int sizeLimit
= 0x7fff ;
691 // width and height of the device-dependent bitmap
692 int width
= GetWidth();
693 int bmpHeight
= GetHeight();
695 // calc the number of bytes per scanline and padding
696 int bytePerLine
= width
*3;
697 int sizeDWORD
= sizeof( DWORD
);
698 int lineBoundary
= bytePerLine
% sizeDWORD
;
700 if( lineBoundary
> 0 )
702 padding
= sizeDWORD
- lineBoundary
;
703 bytePerLine
+= padding
;
705 // calc the number of DIBs and heights of DIBs
708 int height
= sizeLimit
/bytePerLine
;
709 if( height
>= bmpHeight
)
713 numDIB
= bmpHeight
/ height
;
714 hRemain
= bmpHeight
% height
;
715 if( hRemain
>0 ) numDIB
++;
718 // set bitmap parameters
720 wxCHECK_MSG( Ok(), bitmap
, _T("invalid image") );
721 bitmap
.SetWidth( width
);
722 bitmap
.SetHeight( bmpHeight
);
723 bitmap
.SetDepth( wxDisplayDepth() );
725 // create a DIB header
726 int headersize
= sizeof(BITMAPINFOHEADER
);
727 LPBITMAPINFO lpDIBh
= (BITMAPINFO
*) malloc( headersize
);
728 wxCHECK_MSG( lpDIBh
, bitmap
, _T("could not allocate memory for DIB header") );
729 // Fill in the DIB header
730 lpDIBh
->bmiHeader
.biSize
= headersize
;
731 lpDIBh
->bmiHeader
.biWidth
= (DWORD
)width
;
732 lpDIBh
->bmiHeader
.biHeight
= (DWORD
)(-height
);
733 lpDIBh
->bmiHeader
.biSizeImage
= bytePerLine
*height
;
734 // the general formula for biSizeImage:
735 // ( ( ( ((DWORD)width*24) +31 ) & ~31 ) >> 3 ) * height;
736 lpDIBh
->bmiHeader
.biPlanes
= 1;
737 lpDIBh
->bmiHeader
.biBitCount
= 24;
738 lpDIBh
->bmiHeader
.biCompression
= BI_RGB
;
739 lpDIBh
->bmiHeader
.biClrUsed
= 0;
740 // These seem not really needed for our purpose here.
741 lpDIBh
->bmiHeader
.biClrImportant
= 0;
742 lpDIBh
->bmiHeader
.biXPelsPerMeter
= 0;
743 lpDIBh
->bmiHeader
.biYPelsPerMeter
= 0;
744 // memory for DIB data
745 unsigned char *lpBits
;
746 lpBits
= (unsigned char *)malloc( lpDIBh
->bmiHeader
.biSizeImage
);
749 wxFAIL_MSG( _T("could not allocate memory for DIB") );
754 // create and set the device-dependent bitmap
755 HDC hdc
= ::GetDC(NULL
);
756 HDC memdc
= ::CreateCompatibleDC( hdc
);
758 hbitmap
= ::CreateCompatibleBitmap( hdc
, width
, bmpHeight
);
759 ::SelectObject( memdc
, hbitmap
);
761 // copy image data into DIB data and then into DDB (in a loop)
762 unsigned char *data
= GetData();
765 unsigned char *ptdata
= data
;
766 unsigned char *ptbits
;
768 for( n
=0; n
<numDIB
; n
++ )
770 if( numDIB
> 1 && n
== numDIB
-1 && hRemain
> 0 )
772 // redefine height and size of the (possibly) last smaller DIB
773 // memory is not reallocated
775 lpDIBh
->bmiHeader
.biHeight
= (DWORD
)(-height
);
776 lpDIBh
->bmiHeader
.biSizeImage
= bytePerLine
*height
;
780 for( j
=0; j
<height
; j
++ )
782 for( i
=0; i
<width
; i
++ )
784 *(ptbits
++) = *(ptdata
+2);
785 *(ptbits
++) = *(ptdata
+1);
786 *(ptbits
++) = *(ptdata
);
789 for( i
=0; i
< padding
; i
++ ) *(ptbits
++) = 0;
791 ::StretchDIBits( memdc
, 0, origin
, width
, height
,\
792 0, 0, width
, height
, lpBits
, lpDIBh
, DIB_RGB_COLORS
, SRCCOPY
);
794 // if numDIB = 1, lines below can also be used
795 // hbitmap = CreateDIBitmap( hdc, &(lpDIBh->bmiHeader), CBM_INIT, lpBits, lpDIBh, DIB_RGB_COLORS );
796 // The above line is equivalent to the following two lines.
797 // hbitmap = ::CreateCompatibleBitmap( hdc, width, height );
798 // ::SetDIBits( hdc, hbitmap, 0, height, lpBits, lpDIBh, DIB_RGB_COLORS);
799 // or the following lines
800 // hbitmap = ::CreateCompatibleBitmap( hdc, width, height );
801 // HDC memdc = ::CreateCompatibleDC( hdc );
802 // ::SelectObject( memdc, hbitmap);
803 // ::SetDIBitsToDevice( memdc, 0, 0, width, height,
804 // 0, 0, 0, height, (void *)lpBits, lpDIBh, DIB_RGB_COLORS);
805 // ::SelectObject( memdc, 0 );
806 // ::DeleteDC( memdc );
808 bitmap
.SetHBITMAP( (WXHBITMAP
) hbitmap
);
810 // similarly, created an mono-bitmap for the possible mask
813 hbitmap
= ::CreateBitmap( (WORD
)width
, (WORD
)bmpHeight
, 1, 1, NULL
);
814 ::SelectObject( memdc
, hbitmap
);
815 if( numDIB
== 1 ) height
= bmpHeight
;
816 else height
= sizeLimit
/bytePerLine
;
817 lpDIBh
->bmiHeader
.biHeight
= (DWORD
)(-height
);
818 lpDIBh
->bmiHeader
.biSizeImage
= bytePerLine
*height
;
820 unsigned char r
= GetMaskRed();
821 unsigned char g
= GetMaskGreen();
822 unsigned char b
= GetMaskBlue();
823 unsigned char zero
= 0, one
= 255;
825 for( n
=0; n
<numDIB
; n
++ )
827 if( numDIB
> 1 && n
== numDIB
- 1 && hRemain
> 0 )
829 // redefine height and size of the (possibly) last smaller DIB
830 // memory is not reallocated
832 lpDIBh
->bmiHeader
.biHeight
= (DWORD
)(-height
);
833 lpDIBh
->bmiHeader
.biSizeImage
= bytePerLine
*height
;
836 for( int j
=0; j
<height
; j
++ )
838 for(i
=0; i
<width
; i
++ )
840 if( (*(ptdata
++)!=r
) | (*(ptdata
++)!=g
) | (*(ptdata
++)!=b
) )
853 for( i
=0; i
< padding
; i
++ ) *(ptbits
++) = zero
;
855 ::StretchDIBits( memdc
, 0, origin
, width
, height
,\
856 0, 0, width
, height
, lpBits
, lpDIBh
, DIB_RGB_COLORS
, SRCCOPY
);
859 // create a wxMask object
860 wxMask
*mask
= new wxMask();
861 mask
->SetMaskBitmap( (WXHBITMAP
) hbitmap
);
862 bitmap
.SetMask( mask
);
863 // It will be deleted when the wxBitmap object is deleted (as of 01/1999)
864 /* The following can also be used but is slow to run
865 wxColour colour( GetMaskRed(), GetMaskGreen(), GetMaskBlue());
866 wxMask *mask = new wxMask( bitmap, colour );
867 bitmap.SetMask( mask );
871 // free allocated resources
872 ::SelectObject( memdc
, 0 );
874 ::ReleaseDC(NULL
, hdc
);
878 // check the wxBitmap object
879 if( bitmap
.GetHBITMAP() )
880 bitmap
.SetOk( TRUE
);
882 bitmap
.SetOk( FALSE
);
887 wxImage::wxImage( const wxBitmap
&bitmap
)
892 wxFAIL_MSG( _T("invalid bitmap") );
896 // create an wxImage object
897 int width
= bitmap
.GetWidth();
898 int height
= bitmap
.GetHeight();
899 Create( width
, height
);
900 unsigned char *data
= GetData();
903 wxFAIL_MSG( _T("could not allocate data for image") );
907 // calc the number of bytes per scanline and padding in the DIB
908 int bytePerLine
= width
*3;
909 int sizeDWORD
= sizeof( DWORD
);
910 int lineBoundary
= bytePerLine
% sizeDWORD
;
912 if( lineBoundary
> 0 )
914 padding
= sizeDWORD
- lineBoundary
;
915 bytePerLine
+= padding
;
918 // create a DIB header
919 int headersize
= sizeof(BITMAPINFOHEADER
);
920 LPBITMAPINFO lpDIBh
= (BITMAPINFO
*) malloc( headersize
);
923 wxFAIL_MSG( _T("could not allocate data for DIB header") );
927 // Fill in the DIB header
928 lpDIBh
->bmiHeader
.biSize
= headersize
;
929 lpDIBh
->bmiHeader
.biWidth
= width
;
930 lpDIBh
->bmiHeader
.biHeight
= -height
;
931 lpDIBh
->bmiHeader
.biSizeImage
= bytePerLine
* height
;
932 lpDIBh
->bmiHeader
.biPlanes
= 1;
933 lpDIBh
->bmiHeader
.biBitCount
= 24;
934 lpDIBh
->bmiHeader
.biCompression
= BI_RGB
;
935 lpDIBh
->bmiHeader
.biClrUsed
= 0;
936 // These seem not really needed for our purpose here.
937 lpDIBh
->bmiHeader
.biClrImportant
= 0;
938 lpDIBh
->bmiHeader
.biXPelsPerMeter
= 0;
939 lpDIBh
->bmiHeader
.biYPelsPerMeter
= 0;
940 // memory for DIB data
941 unsigned char *lpBits
;
942 lpBits
= (unsigned char *) malloc( lpDIBh
->bmiHeader
.biSizeImage
);
945 wxFAIL_MSG( _T("could not allocate data for DIB") );
951 // copy data from the device-dependent bitmap to the DIB
952 HDC hdc
= ::GetDC(NULL
);
954 hbitmap
= (HBITMAP
) bitmap
.GetHBITMAP();
955 ::GetDIBits( hdc
, hbitmap
, 0, height
, lpBits
, lpDIBh
, DIB_RGB_COLORS
);
957 // copy DIB data into the wxImage object
959 unsigned char *ptdata
= data
;
960 unsigned char *ptbits
= lpBits
;
961 for( i
=0; i
<height
; i
++ )
963 for( j
=0; j
<width
; j
++ )
965 *(ptdata
++) = *(ptbits
+2);
966 *(ptdata
++) = *(ptbits
+1);
967 *(ptdata
++) = *(ptbits
);
973 // similarly, set data according to the possible mask bitmap
974 if( bitmap
.GetMask() && bitmap
.GetMask()->GetMaskBitmap() )
976 hbitmap
= (HBITMAP
) bitmap
.GetMask()->GetMaskBitmap();
977 // memory DC created, color set, data copied, and memory DC deleted
978 HDC memdc
= ::CreateCompatibleDC( hdc
);
979 ::SetTextColor( memdc
, RGB( 0, 0, 0 ) );
980 ::SetBkColor( memdc
, RGB( 255, 255, 255 ) );
981 ::GetDIBits( memdc
, hbitmap
, 0, height
, lpBits
, lpDIBh
, DIB_RGB_COLORS
);
983 // background color set to RGB(16,16,16) in consistent with wxGTK
984 unsigned char r
=16, g
=16, b
=16;
987 for( i
=0; i
<height
; i
++ )
989 for( j
=0; j
<width
; j
++ )
1003 SetMaskColour( r
, g
, b
);
1010 // free allocated resources
1011 ::ReleaseDC(NULL
, hdc
);
1018 //-----------------------------------------------------------------------------
1019 // GTK conversion routines
1020 //-----------------------------------------------------------------------------
1024 #include "gtk/gtk.h"
1025 #include "gdk/gdk.h"
1026 #include "gdk/gdkx.h"
1028 #if (GTK_MINOR_VERSION > 0)
1029 #include "gdk/gdkrgb.h"
1032 wxBitmap
wxImage::ConvertToBitmap() const
1036 wxCHECK_MSG( Ok(), bitmap
, _T("invalid image") );
1038 int width
= GetWidth();
1039 int height
= GetHeight();
1041 bitmap
.SetHeight( height
);
1042 bitmap
.SetWidth( width
);
1044 bitmap
.SetPixmap( gdk_pixmap_new( (GdkWindow
*)&gdk_root_parent
, width
, height
, -1 ) );
1048 GdkVisual
*visual
= gdk_window_get_visual( bitmap
.GetPixmap() );
1049 if (visual
== NULL
) visual
= gdk_visual_get_system();
1050 int bpp
= visual
->depth
;
1052 bitmap
.SetDepth( bpp
);
1054 if ((bpp
== 16) && (visual
->red_mask
!= 0xf800)) bpp
= 15;
1055 if (bpp
< 8) bpp
= 8;
1057 #if (GTK_MINOR_VERSION > 0)
1059 if (!HasMask() && (bpp
> 8))
1061 static bool s_hasInitialized
= FALSE
;
1063 if (!s_hasInitialized
)
1066 s_hasInitialized
= TRUE
;
1069 GdkGC
*gc
= gdk_gc_new( bitmap
.GetPixmap() );
1071 gdk_draw_rgb_image( bitmap
.GetPixmap(),
1075 GDK_RGB_DITHER_NONE
,
1086 // Create picture image
1088 GdkImage
*data_image
=
1089 gdk_image_new( GDK_IMAGE_FASTEST
, gdk_visual_get_system(), width
, height
);
1091 // Create mask image
1093 GdkImage
*mask_image
= (GdkImage
*) NULL
;
1097 unsigned char *mask_data
= (unsigned char*)malloc( ((width
>> 3)+8) * height
);
1099 mask_image
= gdk_image_new_bitmap( gdk_visual_get_system(), mask_data
, width
, height
);
1101 wxMask
*mask
= new wxMask();
1102 mask
->m_bitmap
= gdk_pixmap_new( (GdkWindow
*)&gdk_root_parent
, width
, height
, 1 );
1104 bitmap
.SetMask( mask
);
1109 enum byte_order
{ RGB
, RBG
, BRG
, BGR
, GRB
, GBR
};
1110 byte_order b_o
= RGB
;
1114 GdkVisual
*visual
= gdk_visual_get_system();
1115 if ((visual
->red_mask
> visual
->green_mask
) && (visual
->green_mask
> visual
->blue_mask
)) b_o
= RGB
;
1116 else if ((visual
->red_mask
> visual
->blue_mask
) && (visual
->blue_mask
> visual
->green_mask
)) b_o
= RGB
;
1117 else if ((visual
->blue_mask
> visual
->red_mask
) && (visual
->red_mask
> visual
->green_mask
)) b_o
= BRG
;
1118 else if ((visual
->blue_mask
> visual
->green_mask
) && (visual
->green_mask
> visual
->red_mask
)) b_o
= BGR
;
1119 else if ((visual
->green_mask
> visual
->red_mask
) && (visual
->red_mask
> visual
->blue_mask
)) b_o
= GRB
;
1120 else if ((visual
->green_mask
> visual
->blue_mask
) && (visual
->blue_mask
> visual
->red_mask
)) b_o
= GBR
;
1123 int r_mask
= GetMaskRed();
1124 int g_mask
= GetMaskGreen();
1125 int b_mask
= GetMaskBlue();
1127 unsigned char* data
= GetData();
1130 for (int y
= 0; y
< height
; y
++)
1132 for (int x
= 0; x
< width
; x
++)
1134 int r
= data
[index
];
1136 int g
= data
[index
];
1138 int b
= data
[index
];
1143 if ((r
== r_mask
) && (b
== b_mask
) && (g
== g_mask
))
1144 gdk_image_put_pixel( mask_image
, x
, y
, 1 );
1146 gdk_image_put_pixel( mask_image
, x
, y
, 0 );
1151 if ((r
== r_mask
) && (b
== b_mask
) && (g
== g_mask
))
1152 gdk_image_put_pixel( mask_image
, x
, y
, 1 );
1154 gdk_image_put_pixel( mask_image
, x
, y
, 0 );
1162 if (wxTheApp
->m_colorCube
)
1164 pixel
= wxTheApp
->m_colorCube
[ ((r
& 0xf8) << 7) + ((g
& 0xf8) << 2) + ((b
& 0xf8) >> 3) ];
1168 GdkColormap
*cmap
= gtk_widget_get_default_colormap();
1169 GdkColor
*colors
= cmap
->colors
;
1170 int max
= 3 * (65536);
1172 for (int i
= 0; i
< cmap
->size
; i
++)
1174 int rdiff
= (r
<< 8) - colors
[i
].red
;
1175 int gdiff
= (g
<< 8) - colors
[i
].green
;
1176 int bdiff
= (b
<< 8) - colors
[i
].blue
;
1177 int sum
= ABS (rdiff
) + ABS (gdiff
) + ABS (bdiff
);
1178 if (sum
< max
) { pixel
= i
; max
= sum
; }
1182 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
1188 guint32 pixel
= ((r
& 0xf8) << 7) | ((g
& 0xf8) << 2) | ((b
& 0xf8) >> 3);
1189 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
1194 guint32 pixel
= ((r
& 0xf8) << 8) | ((g
& 0xfc) << 3) | ((b
& 0xf8) >> 3);
1195 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
1204 case RGB
: pixel
= (r
<< 16) | (g
<< 8) | b
; break;
1205 case RBG
: pixel
= (r
<< 16) | (b
<< 8) | g
; break;
1206 case BRG
: pixel
= (b
<< 16) | (r
<< 8) | g
; break;
1207 case BGR
: pixel
= (b
<< 16) | (g
<< 8) | r
; break;
1208 case GRB
: pixel
= (g
<< 16) | (r
<< 8) | b
; break;
1209 case GBR
: pixel
= (g
<< 16) | (b
<< 8) | r
; break;
1211 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
1220 GdkGC
*data_gc
= gdk_gc_new( bitmap
.GetPixmap() );
1222 gdk_draw_image( bitmap
.GetPixmap(), data_gc
, data_image
, 0, 0, 0, 0, width
, height
);
1224 gdk_image_destroy( data_image
);
1225 gdk_gc_unref( data_gc
);
1231 GdkGC
*mask_gc
= gdk_gc_new( bitmap
.GetMask()->GetBitmap() );
1233 gdk_draw_image( bitmap
.GetMask()->GetBitmap(), mask_gc
, mask_image
, 0, 0, 0, 0, width
, height
);
1235 gdk_image_destroy( mask_image
);
1236 gdk_gc_unref( mask_gc
);
1242 wxImage::wxImage( const wxBitmap
&bitmap
)
1244 wxCHECK_RET( bitmap
.Ok(), _T("invalid bitmap") );
1246 GdkImage
*gdk_image
= gdk_image_get( bitmap
.GetPixmap(),
1248 bitmap
.GetWidth(), bitmap
.GetHeight() );
1250 wxCHECK_RET( gdk_image
, _T("couldn't create image") );
1252 Create( bitmap
.GetWidth(), bitmap
.GetHeight() );
1253 char unsigned *data
= GetData();
1257 gdk_image_destroy( gdk_image
);
1258 wxFAIL_MSG( _T("couldn't create image") );
1262 GdkImage
*gdk_image_mask
= (GdkImage
*) NULL
;
1263 if (bitmap
.GetMask())
1265 gdk_image_mask
= gdk_image_get( bitmap
.GetMask()->GetBitmap(),
1267 bitmap
.GetWidth(), bitmap
.GetHeight() );
1269 SetMaskColour( 16, 16, 16 ); // anything unlikely and dividable
1272 GdkVisual
*visual
= gdk_window_get_visual( bitmap
.GetPixmap() );
1273 if (visual
== NULL
) visual
= gdk_window_get_visual( (GdkWindow
*) &gdk_root_parent
);
1274 int bpp
= visual
->depth
;
1275 if ((bpp
== 16) && (visual
->red_mask
!= 0xf800)) bpp
= 15;
1277 GdkColormap
*cmap
= gtk_widget_get_default_colormap();
1280 for (int j
= 0; j
< bitmap
.GetHeight(); j
++)
1282 for (int i
= 0; i
< bitmap
.GetWidth(); i
++)
1284 wxInt32 pixel
= gdk_image_get_pixel( gdk_image
, i
, j
);
1285 pixel
= wxINT32_SWAP_ON_BE( pixel
);
1288 data
[pos
] = cmap
->colors
[pixel
].red
>> 8;
1289 data
[pos
+1] = cmap
->colors
[pixel
].green
>> 8;
1290 data
[pos
+2] = cmap
->colors
[pixel
].blue
>> 8;
1291 } else if (bpp
== 15)
1293 data
[pos
] = (pixel
>> 7) & 0xf8;
1294 data
[pos
+1] = (pixel
>> 2) & 0xf8;
1295 data
[pos
+2] = (pixel
<< 3) & 0xf8;
1296 } else if (bpp
== 16)
1298 data
[pos
] = (pixel
>> 8) & 0xf8;
1299 data
[pos
+1] = (pixel
>> 3) & 0xfc;
1300 data
[pos
+2] = (pixel
<< 3) & 0xf8;
1303 data
[pos
] = (pixel
>> 16) & 0xff;
1304 data
[pos
+1] = (pixel
>> 8) & 0xff;
1305 data
[pos
+2] = pixel
& 0xff;
1310 int mask_pixel
= gdk_image_get_pixel( gdk_image_mask
, i
, j
);
1311 if (mask_pixel
== 0)
1323 gdk_image_destroy( gdk_image
);
1324 if (gdk_image_mask
) gdk_image_destroy( gdk_image_mask
);
1329 //-----------------------------------------------------------------------------
1330 // Motif conversion routines
1331 //-----------------------------------------------------------------------------
1336 #include "wx/utils.h"
1339 wxBitmap
wxImage::ConvertToBitmap() const
1343 wxCHECK_MSG( Ok(), bitmap
, _T("invalid image") );
1345 int width
= GetWidth();
1346 int height
= GetHeight();
1348 bitmap
.SetHeight( height
);
1349 bitmap
.SetWidth( width
);
1351 Display
*dpy
= (Display
*) wxGetDisplay();
1352 Visual
* vis
= DefaultVisual( dpy
, DefaultScreen( dpy
) );
1353 int bpp
= DefaultDepth( dpy
, DefaultScreen( dpy
) );
1357 XImage
*data_image
= XCreateImage( dpy
, vis
, bpp
, ZPixmap
, 0, 0, width
, height
, 32, 0 );
1358 data_image
->data
= (char*) malloc( data_image
->bytes_per_line
* data_image
->height
);
1360 bitmap
.Create( width
, height
, bpp
);
1365 GdkImage *mask_image = (GdkImage*) NULL;
1369 unsigned char *mask_data = (unsigned char*)malloc( ((width >> 3)+8) * height );
1371 mask_image = gdk_image_new_bitmap( gdk_visual_get_system(), mask_data, width, height );
1373 wxMask *mask = new wxMask();
1374 mask->m_bitmap = gdk_pixmap_new( (GdkWindow*)&gdk_root_parent, width, height, 1 );
1376 bitmap.SetMask( mask );
1380 // Retrieve depth info
1382 XVisualInfo vinfo_template
;
1385 vinfo_template
.visual
= vis
;
1386 vinfo_template
.visualid
= XVisualIDFromVisual( vis
);
1387 vinfo_template
.depth
= bpp
;
1390 vi
= XGetVisualInfo( dpy
, VisualIDMask
|VisualDepthMask
, &vinfo_template
, &nitem
);
1392 wxCHECK_MSG( vi
, wxNullBitmap
, _T("no visual") );
1396 if ((bpp
== 16) && (vi
->red_mask
!= 0xf800)) bpp
= 15;
1397 if (bpp
< 8) bpp
= 8;
1401 enum byte_order
{ RGB
, RBG
, BRG
, BGR
, GRB
, GBR
};
1402 byte_order b_o
= RGB
;
1406 if ((vi
->red_mask
> vi
->green_mask
) && (vi
->green_mask
> vi
->blue_mask
)) b_o
= RGB
;
1407 else if ((vi
->red_mask
> vi
->blue_mask
) && (vi
->blue_mask
> vi
->green_mask
)) b_o
= RGB
;
1408 else if ((vi
->blue_mask
> vi
->red_mask
) && (vi
->red_mask
> vi
->green_mask
)) b_o
= BRG
;
1409 else if ((vi
->blue_mask
> vi
->green_mask
) && (vi
->green_mask
> vi
->red_mask
)) b_o
= BGR
;
1410 else if ((vi
->green_mask
> vi
->red_mask
) && (vi
->red_mask
> vi
->blue_mask
)) b_o
= GRB
;
1411 else if ((vi
->green_mask
> vi
->blue_mask
) && (vi
->blue_mask
> vi
->red_mask
)) b_o
= GBR
;
1415 int r_mask = GetMaskRed();
1416 int g_mask = GetMaskGreen();
1417 int b_mask = GetMaskBlue();
1423 Colormap cmap
= (Colormap
) wxTheApp
->GetMainColormap( dpy
);
1425 for (int i
= 0; i
< 256; i
++) colors
[i
].pixel
= i
;
1426 XQueryColors( dpy
, cmap
, colors
, 256 );
1429 unsigned char* data
= GetData();
1432 for (int y
= 0; y
< height
; y
++)
1434 for (int x
= 0; x
< width
; x
++)
1436 int r
= data
[index
];
1438 int g
= data
[index
];
1440 int b
= data
[index
];
1446 if ((r == r_mask) && (b == b_mask) && (g == g_mask))
1447 gdk_image_put_pixel( mask_image, x, y, 1 );
1449 gdk_image_put_pixel( mask_image, x, y, 0 );
1459 if (wxTheApp->m_colorCube)
1461 pixel = wxTheApp->m_colorCube
1462 [ ((r & 0xf8) << 7) + ((g & 0xf8) << 2) + ((b & 0xf8) >> 3) ];
1467 int max
= 3 * (65536);
1468 for (int i
= 0; i
< 256; i
++)
1470 int rdiff
= (r
<< 8) - colors
[i
].red
;
1471 int gdiff
= (g
<< 8) - colors
[i
].green
;
1472 int bdiff
= (b
<< 8) - colors
[i
].blue
;
1473 int sum
= abs (rdiff
) + abs (gdiff
) + abs (bdiff
);
1474 if (sum
< max
) { pixel
= i
; max
= sum
; }
1479 XPutPixel( data_image
, x
, y
, pixel
);
1484 int pixel
= ((r
& 0xf8) << 7) | ((g
& 0xf8) << 2) | ((b
& 0xf8) >> 3);
1485 XPutPixel( data_image
, x
, y
, pixel
);
1490 int pixel
= ((r
& 0xf8) << 8) | ((g
& 0xfc) << 3) | ((b
& 0xf8) >> 3);
1491 XPutPixel( data_image
, x
, y
, pixel
);
1500 case RGB
: pixel
= (r
<< 16) | (g
<< 8) | b
; break;
1501 case RBG
: pixel
= (r
<< 16) | (b
<< 8) | g
; break;
1502 case BRG
: pixel
= (b
<< 16) | (r
<< 8) | g
; break;
1503 case BGR
: pixel
= (b
<< 16) | (g
<< 8) | r
; break;
1504 case GRB
: pixel
= (g
<< 16) | (r
<< 8) | b
; break;
1505 case GBR
: pixel
= (g
<< 16) | (b
<< 8) | r
; break;
1507 XPutPixel( data_image
, x
, y
, pixel
);
1517 gcvalues
.foreground
= BlackPixel( dpy
, DefaultScreen( dpy
) );
1518 GC gc
= XCreateGC( dpy
, RootWindow ( dpy
, DefaultScreen(dpy
) ), GCForeground
, &gcvalues
);
1519 XPutImage( dpy
, (Drawable
)bitmap
.GetPixmap(), gc
, data_image
, 0, 0, 0, 0, width
, height
);
1521 XDestroyImage( data_image
);
1529 GdkGC *mask_gc = gdk_gc_new( bitmap.GetMask()->GetBitmap() );
1531 gdk_draw_image( bitmap.GetMask()->GetBitmap(), mask_gc, mask_image, 0, 0, 0, 0, width, height );
1533 gdk_image_destroy( mask_image );
1534 gdk_gc_unref( mask_gc );
1541 wxImage::wxImage( const wxBitmap
&bitmap
)
1543 wxCHECK_RET( bitmap
.Ok(), _T("invalid bitmap") );
1545 Display
*dpy
= (Display
*) wxGetDisplay();
1546 Visual
* vis
= DefaultVisual( dpy
, DefaultScreen( dpy
) );
1547 int bpp
= DefaultDepth( dpy
, DefaultScreen( dpy
) );
1549 XImage
*ximage
= XGetImage( dpy
,
1550 (Drawable
)bitmap
.GetPixmap(),
1552 bitmap
.GetWidth(), bitmap
.GetHeight(),
1553 AllPlanes
, ZPixmap
);
1555 wxCHECK_RET( ximage
, _T("couldn't create image") );
1557 Create( bitmap
.GetWidth(), bitmap
.GetHeight() );
1558 char unsigned *data
= GetData();
1562 XDestroyImage( ximage
);
1563 wxFAIL_MSG( _T("couldn't create image") );
1568 GdkImage *gdk_image_mask = (GdkImage*) NULL;
1569 if (bitmap.GetMask())
1571 gdk_image_mask = gdk_image_get( bitmap.GetMask()->GetBitmap(),
1573 bitmap.GetWidth(), bitmap.GetHeight() );
1575 SetMaskColour( 16, 16, 16 ); // anything unlikely and dividable
1579 // Retrieve depth info
1581 XVisualInfo vinfo_template
;
1584 vinfo_template
.visual
= vis
;
1585 vinfo_template
.visualid
= XVisualIDFromVisual( vis
);
1586 vinfo_template
.depth
= bpp
;
1589 vi
= XGetVisualInfo( dpy
, VisualIDMask
|VisualDepthMask
, &vinfo_template
, &nitem
);
1591 wxCHECK_RET( vi
, _T("no visual") );
1593 if ((bpp
== 16) && (vi
->red_mask
!= 0xf800)) bpp
= 15;
1600 Colormap cmap
= (Colormap
)wxTheApp
->GetMainColormap( dpy
);
1602 for (int i
= 0; i
< 256; i
++) colors
[i
].pixel
= i
;
1603 XQueryColors( dpy
, cmap
, colors
, 256 );
1607 for (int j
= 0; j
< bitmap
.GetHeight(); j
++)
1609 for (int i
= 0; i
< bitmap
.GetWidth(); i
++)
1611 int pixel
= XGetPixel( ximage
, i
, j
);
1614 data
[pos
] = colors
[pixel
].red
>> 8;
1615 data
[pos
+1] = colors
[pixel
].green
>> 8;
1616 data
[pos
+2] = colors
[pixel
].blue
>> 8;
1617 } else if (bpp
== 15)
1619 data
[pos
] = (pixel
>> 7) & 0xf8;
1620 data
[pos
+1] = (pixel
>> 2) & 0xf8;
1621 data
[pos
+2] = (pixel
<< 3) & 0xf8;
1622 } else if (bpp
== 16)
1624 data
[pos
] = (pixel
>> 8) & 0xf8;
1625 data
[pos
+1] = (pixel
>> 3) & 0xfc;
1626 data
[pos
+2] = (pixel
<< 3) & 0xf8;
1629 data
[pos
] = (pixel
>> 16) & 0xff;
1630 data
[pos
+1] = (pixel
>> 8) & 0xff;
1631 data
[pos
+2] = pixel
& 0xff;
1637 int mask_pixel = gdk_image_get_pixel( gdk_image_mask, i, j );
1638 if (mask_pixel == 0)
1651 XDestroyImage( ximage
);
1653 if (gdk_image_mask) gdk_image_destroy( gdk_image_mask );
1658 // A module to allow wxImage initialization/cleanup
1659 // without calling these functions from app.cpp or from
1660 // the user's application.
1662 class wxImageModule
: public wxModule
1664 DECLARE_DYNAMIC_CLASS(wxImageModule
)
1667 bool OnInit() { wxImage::InitStandardHandlers(); return TRUE
; };
1668 void OnExit() { wxImage::CleanUpHandlers(); };
1671 IMPLEMENT_DYNAMIC_CLASS(wxImageModule
, wxModule
)