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
685 int sizeLimit
= 1024*768*3;
687 // width and height of the device-dependent bitmap
688 int width
= GetWidth();
689 int bmpHeight
= GetHeight();
691 // calc the number of bytes per scanline and padding
692 int bytePerLine
= width
*3;
693 int sizeDWORD
= sizeof( DWORD
);
694 int lineBoundary
= bytePerLine
% sizeDWORD
;
696 if( lineBoundary
> 0 )
698 padding
= sizeDWORD
- lineBoundary
;
699 bytePerLine
+= padding
;
701 // calc the number of DIBs and heights of DIBs
704 int height
= sizeLimit
/bytePerLine
;
705 if( height
>= bmpHeight
)
709 numDIB
= bmpHeight
/ height
;
710 hRemain
= bmpHeight
% height
;
711 if( hRemain
>0 ) numDIB
++;
714 // set bitmap parameters
716 wxCHECK_MSG( Ok(), bitmap
, _T("invalid image") );
717 bitmap
.SetWidth( width
);
718 bitmap
.SetHeight( bmpHeight
);
719 bitmap
.SetDepth( wxDisplayDepth() );
721 // create a DIB header
722 int headersize
= sizeof(BITMAPINFOHEADER
);
723 LPBITMAPINFO lpDIBh
= (BITMAPINFO
*) malloc( headersize
);
724 wxCHECK_MSG( lpDIBh
, bitmap
, _T("could not allocate memory for DIB header") );
725 // Fill in the DIB header
726 lpDIBh
->bmiHeader
.biSize
= headersize
;
727 lpDIBh
->bmiHeader
.biWidth
= (DWORD
)width
;
728 lpDIBh
->bmiHeader
.biHeight
= (DWORD
)(-height
);
729 lpDIBh
->bmiHeader
.biSizeImage
= bytePerLine
*height
;
730 // the general formula for biSizeImage:
731 // ( ( ( ((DWORD)width*24) +31 ) & ~31 ) >> 3 ) * height;
732 lpDIBh
->bmiHeader
.biPlanes
= 1;
733 lpDIBh
->bmiHeader
.biBitCount
= 24;
734 lpDIBh
->bmiHeader
.biCompression
= BI_RGB
;
735 lpDIBh
->bmiHeader
.biClrUsed
= 0;
736 // These seem not really needed for our purpose here.
737 lpDIBh
->bmiHeader
.biClrImportant
= 0;
738 lpDIBh
->bmiHeader
.biXPelsPerMeter
= 0;
739 lpDIBh
->bmiHeader
.biYPelsPerMeter
= 0;
740 // memory for DIB data
741 unsigned char *lpBits
;
742 lpBits
= (unsigned char *)malloc( lpDIBh
->bmiHeader
.biSizeImage
);
745 wxFAIL_MSG( _T("could not allocate memory for DIB") );
750 // create and set the device-dependent bitmap
751 HDC hdc
= ::GetDC(NULL
);
752 HDC memdc
= ::CreateCompatibleDC( hdc
);
754 hbitmap
= ::CreateCompatibleBitmap( hdc
, width
, bmpHeight
);
755 ::SelectObject( memdc
, hbitmap
);
757 // copy image data into DIB data and then into DDB (in a loop)
758 unsigned char *data
= GetData();
761 unsigned char *ptdata
= data
;
762 unsigned char *ptbits
;
764 for( n
=0; n
<numDIB
; n
++ )
766 if( numDIB
> 1 && n
== numDIB
-1 && hRemain
> 0 )
768 // redefine height and size of the (possibly) last smaller DIB
769 // memory is not reallocated
771 lpDIBh
->bmiHeader
.biHeight
= (DWORD
)(-height
);
772 lpDIBh
->bmiHeader
.biSizeImage
= bytePerLine
*height
;
776 for( j
=0; j
<height
; j
++ )
778 for( i
=0; i
<width
; i
++ )
780 *(ptbits
++) = *(ptdata
+2);
781 *(ptbits
++) = *(ptdata
+1);
782 *(ptbits
++) = *(ptdata
);
785 for( i
=0; i
< padding
; i
++ ) *(ptbits
++) = 0;
787 ::StretchDIBits( memdc
, 0, origin
, width
, height
,\
788 0, 0, width
, height
, lpBits
, lpDIBh
, DIB_RGB_COLORS
, SRCCOPY
);
790 // if numDIB = 1, lines below can also be used
791 // hbitmap = CreateDIBitmap( hdc, &(lpDIBh->bmiHeader), CBM_INIT, lpBits, lpDIBh, DIB_RGB_COLORS );
792 // The above line is equivalent to the following two lines.
793 // hbitmap = ::CreateCompatibleBitmap( hdc, width, height );
794 // ::SetDIBits( hdc, hbitmap, 0, height, lpBits, lpDIBh, DIB_RGB_COLORS);
795 // or the following lines
796 // hbitmap = ::CreateCompatibleBitmap( hdc, width, height );
797 // HDC memdc = ::CreateCompatibleDC( hdc );
798 // ::SelectObject( memdc, hbitmap);
799 // ::SetDIBitsToDevice( memdc, 0, 0, width, height,
800 // 0, 0, 0, height, (void *)lpBits, lpDIBh, DIB_RGB_COLORS);
801 // ::SelectObject( memdc, 0 );
802 // ::DeleteDC( memdc );
804 bitmap
.SetHBITMAP( (WXHBITMAP
) hbitmap
);
806 // similarly, created an mono-bitmap for the possible mask
809 hbitmap
= ::CreateBitmap( (WORD
)width
, (WORD
)bmpHeight
, 1, 1, NULL
);
810 ::SelectObject( memdc
, hbitmap
);
811 if( numDIB
== 1 ) height
= bmpHeight
;
812 else height
= sizeLimit
/bytePerLine
;
813 lpDIBh
->bmiHeader
.biHeight
= (DWORD
)(-height
);
814 lpDIBh
->bmiHeader
.biSizeImage
= bytePerLine
*height
;
816 unsigned char r
= GetMaskRed();
817 unsigned char g
= GetMaskGreen();
818 unsigned char b
= GetMaskBlue();
819 unsigned char zero
= 0, one
= 255;
821 for( n
=0; n
<numDIB
; n
++ )
823 if( numDIB
> 1 && n
== numDIB
- 1 && hRemain
> 0 )
825 // redefine height and size of the (possibly) last smaller DIB
826 // memory is not reallocated
828 lpDIBh
->bmiHeader
.biHeight
= (DWORD
)(-height
);
829 lpDIBh
->bmiHeader
.biSizeImage
= bytePerLine
*height
;
832 for( int j
=0; j
<height
; j
++ )
834 for(i
=0; i
<width
; i
++ )
836 if( (*(ptdata
++)!=r
) | (*(ptdata
++)!=g
) | (*(ptdata
++)!=b
) )
849 for( i
=0; i
< padding
; i
++ ) *(ptbits
++) = zero
;
851 ::StretchDIBits( memdc
, 0, origin
, width
, height
,\
852 0, 0, width
, height
, lpBits
, lpDIBh
, DIB_RGB_COLORS
, SRCCOPY
);
855 // create a wxMask object
856 wxMask
*mask
= new wxMask();
857 mask
->SetMaskBitmap( (WXHBITMAP
) hbitmap
);
858 bitmap
.SetMask( mask
);
859 // It will be deleted when the wxBitmap object is deleted (as of 01/1999)
860 /* The following can also be used but is slow to run
861 wxColour colour( GetMaskRed(), GetMaskGreen(), GetMaskBlue());
862 wxMask *mask = new wxMask( bitmap, colour );
863 bitmap.SetMask( mask );
867 // free allocated resources
868 ::SelectObject( memdc
, 0 );
870 ::ReleaseDC(NULL
, hdc
);
874 // check the wxBitmap object
875 if( bitmap
.GetHBITMAP() )
876 bitmap
.SetOk( TRUE
);
878 bitmap
.SetOk( FALSE
);
883 wxImage::wxImage( const wxBitmap
&bitmap
)
888 wxFAIL_MSG( _T("invalid bitmap") );
892 // create an wxImage object
893 int width
= bitmap
.GetWidth();
894 int height
= bitmap
.GetHeight();
895 Create( width
, height
);
896 unsigned char *data
= GetData();
899 wxFAIL_MSG( _T("could not allocate data for image") );
903 // calc the number of bytes per scanline and padding in the DIB
904 int bytePerLine
= width
*3;
905 int sizeDWORD
= sizeof( DWORD
);
906 int lineBoundary
= bytePerLine
% sizeDWORD
;
908 if( lineBoundary
> 0 )
910 padding
= sizeDWORD
- lineBoundary
;
911 bytePerLine
+= padding
;
914 // create a DIB header
915 int headersize
= sizeof(BITMAPINFOHEADER
);
916 LPBITMAPINFO lpDIBh
= (BITMAPINFO
*) malloc( headersize
);
919 wxFAIL_MSG( _T("could not allocate data for DIB header") );
923 // Fill in the DIB header
924 lpDIBh
->bmiHeader
.biSize
= headersize
;
925 lpDIBh
->bmiHeader
.biWidth
= width
;
926 lpDIBh
->bmiHeader
.biHeight
= -height
;
927 lpDIBh
->bmiHeader
.biSizeImage
= bytePerLine
* height
;
928 lpDIBh
->bmiHeader
.biPlanes
= 1;
929 lpDIBh
->bmiHeader
.biBitCount
= 24;
930 lpDIBh
->bmiHeader
.biCompression
= BI_RGB
;
931 lpDIBh
->bmiHeader
.biClrUsed
= 0;
932 // These seem not really needed for our purpose here.
933 lpDIBh
->bmiHeader
.biClrImportant
= 0;
934 lpDIBh
->bmiHeader
.biXPelsPerMeter
= 0;
935 lpDIBh
->bmiHeader
.biYPelsPerMeter
= 0;
936 // memory for DIB data
937 unsigned char *lpBits
;
938 lpBits
= (unsigned char *) malloc( lpDIBh
->bmiHeader
.biSizeImage
);
941 wxFAIL_MSG( _T("could not allocate data for DIB") );
947 // copy data from the device-dependent bitmap to the DIB
948 HDC hdc
= ::GetDC(NULL
);
950 hbitmap
= (HBITMAP
) bitmap
.GetHBITMAP();
951 ::GetDIBits( hdc
, hbitmap
, 0, height
, lpBits
, lpDIBh
, DIB_RGB_COLORS
);
953 // copy DIB data into the wxImage object
955 unsigned char *ptdata
= data
;
956 unsigned char *ptbits
= lpBits
;
957 for( i
=0; i
<height
; i
++ )
959 for( j
=0; j
<width
; j
++ )
961 *(ptdata
++) = *(ptbits
+2);
962 *(ptdata
++) = *(ptbits
+1);
963 *(ptdata
++) = *(ptbits
);
969 // similarly, set data according to the possible mask bitmap
970 if( bitmap
.GetMask() && bitmap
.GetMask()->GetMaskBitmap() )
972 hbitmap
= (HBITMAP
) bitmap
.GetMask()->GetMaskBitmap();
973 // memory DC created, color set, data copied, and memory DC deleted
974 HDC memdc
= ::CreateCompatibleDC( hdc
);
975 ::SetTextColor( memdc
, RGB( 0, 0, 0 ) );
976 ::SetBkColor( memdc
, RGB( 255, 255, 255 ) );
977 ::GetDIBits( memdc
, hbitmap
, 0, height
, lpBits
, lpDIBh
, DIB_RGB_COLORS
);
979 // background color set to RGB(16,16,16) in consistent with wxGTK
980 unsigned char r
=16, g
=16, b
=16;
983 for( i
=0; i
<height
; i
++ )
985 for( j
=0; j
<width
; j
++ )
999 SetMaskColour( r
, g
, b
);
1006 // free allocated resources
1007 ::ReleaseDC(NULL
, hdc
);
1014 //-----------------------------------------------------------------------------
1015 // GTK conversion routines
1016 //-----------------------------------------------------------------------------
1020 #include "gtk/gtk.h"
1021 #include "gdk/gdk.h"
1022 #include "gdk/gdkx.h"
1024 #if (GTK_MINOR_VERSION > 0)
1025 #include "gdk/gdkrgb.h"
1028 wxBitmap
wxImage::ConvertToBitmap() const
1032 wxCHECK_MSG( Ok(), bitmap
, _T("invalid image") );
1034 int width
= GetWidth();
1035 int height
= GetHeight();
1037 bitmap
.SetHeight( height
);
1038 bitmap
.SetWidth( width
);
1040 bitmap
.SetPixmap( gdk_pixmap_new( (GdkWindow
*)&gdk_root_parent
, width
, height
, -1 ) );
1044 GdkVisual
*visual
= gdk_window_get_visual( bitmap
.GetPixmap() );
1045 if (visual
== NULL
) visual
= gdk_visual_get_system();
1046 int bpp
= visual
->depth
;
1048 bitmap
.SetDepth( bpp
);
1050 if ((bpp
== 16) && (visual
->red_mask
!= 0xf800)) bpp
= 15;
1051 if (bpp
< 8) bpp
= 8;
1053 #if (GTK_MINOR_VERSION > 0)
1055 if (!HasMask() && (bpp
> 8))
1057 static bool s_hasInitialized
= FALSE
;
1059 if (!s_hasInitialized
)
1062 s_hasInitialized
= TRUE
;
1065 GdkGC
*gc
= gdk_gc_new( bitmap
.GetPixmap() );
1067 gdk_draw_rgb_image( bitmap
.GetPixmap(),
1071 GDK_RGB_DITHER_NONE
,
1082 // Create picture image
1084 GdkImage
*data_image
=
1085 gdk_image_new( GDK_IMAGE_FASTEST
, gdk_visual_get_system(), width
, height
);
1087 // Create mask image
1089 GdkImage
*mask_image
= (GdkImage
*) NULL
;
1093 unsigned char *mask_data
= (unsigned char*)malloc( ((width
>> 3)+8) * height
);
1095 mask_image
= gdk_image_new_bitmap( gdk_visual_get_system(), mask_data
, width
, height
);
1097 wxMask
*mask
= new wxMask();
1098 mask
->m_bitmap
= gdk_pixmap_new( (GdkWindow
*)&gdk_root_parent
, width
, height
, 1 );
1100 bitmap
.SetMask( mask
);
1105 enum byte_order
{ RGB
, RBG
, BRG
, BGR
, GRB
, GBR
};
1106 byte_order b_o
= RGB
;
1110 GdkVisual
*visual
= gdk_visual_get_system();
1111 if ((visual
->red_mask
> visual
->green_mask
) && (visual
->green_mask
> visual
->blue_mask
)) b_o
= RGB
;
1112 else if ((visual
->red_mask
> visual
->blue_mask
) && (visual
->blue_mask
> visual
->green_mask
)) b_o
= RGB
;
1113 else if ((visual
->blue_mask
> visual
->red_mask
) && (visual
->red_mask
> visual
->green_mask
)) b_o
= BRG
;
1114 else if ((visual
->blue_mask
> visual
->green_mask
) && (visual
->green_mask
> visual
->red_mask
)) b_o
= BGR
;
1115 else if ((visual
->green_mask
> visual
->red_mask
) && (visual
->red_mask
> visual
->blue_mask
)) b_o
= GRB
;
1116 else if ((visual
->green_mask
> visual
->blue_mask
) && (visual
->blue_mask
> visual
->red_mask
)) b_o
= GBR
;
1119 int r_mask
= GetMaskRed();
1120 int g_mask
= GetMaskGreen();
1121 int b_mask
= GetMaskBlue();
1123 unsigned char* data
= GetData();
1126 for (int y
= 0; y
< height
; y
++)
1128 for (int x
= 0; x
< width
; x
++)
1130 int r
= data
[index
];
1132 int g
= data
[index
];
1134 int b
= data
[index
];
1139 if ((r
== r_mask
) && (b
== b_mask
) && (g
== g_mask
))
1140 gdk_image_put_pixel( mask_image
, x
, y
, 1 );
1142 gdk_image_put_pixel( mask_image
, x
, y
, 0 );
1147 if ((r
== r_mask
) && (b
== b_mask
) && (g
== g_mask
))
1148 gdk_image_put_pixel( mask_image
, x
, y
, 1 );
1150 gdk_image_put_pixel( mask_image
, x
, y
, 0 );
1158 if (wxTheApp
->m_colorCube
)
1160 pixel
= wxTheApp
->m_colorCube
[ ((r
& 0xf8) << 7) + ((g
& 0xf8) << 2) + ((b
& 0xf8) >> 3) ];
1164 GdkColormap
*cmap
= gtk_widget_get_default_colormap();
1165 GdkColor
*colors
= cmap
->colors
;
1166 int max
= 3 * (65536);
1168 for (int i
= 0; i
< cmap
->size
; i
++)
1170 int rdiff
= (r
<< 8) - colors
[i
].red
;
1171 int gdiff
= (g
<< 8) - colors
[i
].green
;
1172 int bdiff
= (b
<< 8) - colors
[i
].blue
;
1173 int sum
= ABS (rdiff
) + ABS (gdiff
) + ABS (bdiff
);
1174 if (sum
< max
) { pixel
= i
; max
= sum
; }
1178 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
1184 guint32 pixel
= ((r
& 0xf8) << 7) | ((g
& 0xf8) << 2) | ((b
& 0xf8) >> 3);
1185 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
1190 guint32 pixel
= ((r
& 0xf8) << 8) | ((g
& 0xfc) << 3) | ((b
& 0xf8) >> 3);
1191 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
1200 case RGB
: pixel
= (r
<< 16) | (g
<< 8) | b
; break;
1201 case RBG
: pixel
= (r
<< 16) | (b
<< 8) | g
; break;
1202 case BRG
: pixel
= (b
<< 16) | (r
<< 8) | g
; break;
1203 case BGR
: pixel
= (b
<< 16) | (g
<< 8) | r
; break;
1204 case GRB
: pixel
= (g
<< 16) | (r
<< 8) | b
; break;
1205 case GBR
: pixel
= (g
<< 16) | (b
<< 8) | r
; break;
1207 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
1216 GdkGC
*data_gc
= gdk_gc_new( bitmap
.GetPixmap() );
1218 gdk_draw_image( bitmap
.GetPixmap(), data_gc
, data_image
, 0, 0, 0, 0, width
, height
);
1220 gdk_image_destroy( data_image
);
1221 gdk_gc_unref( data_gc
);
1227 GdkGC
*mask_gc
= gdk_gc_new( bitmap
.GetMask()->GetBitmap() );
1229 gdk_draw_image( bitmap
.GetMask()->GetBitmap(), mask_gc
, mask_image
, 0, 0, 0, 0, width
, height
);
1231 gdk_image_destroy( mask_image
);
1232 gdk_gc_unref( mask_gc
);
1238 wxImage::wxImage( const wxBitmap
&bitmap
)
1240 wxCHECK_RET( bitmap
.Ok(), _T("invalid bitmap") );
1242 GdkImage
*gdk_image
= gdk_image_get( bitmap
.GetPixmap(),
1244 bitmap
.GetWidth(), bitmap
.GetHeight() );
1246 wxCHECK_RET( gdk_image
, _T("couldn't create image") );
1248 Create( bitmap
.GetWidth(), bitmap
.GetHeight() );
1249 char unsigned *data
= GetData();
1253 gdk_image_destroy( gdk_image
);
1254 wxFAIL_MSG( _T("couldn't create image") );
1258 GdkImage
*gdk_image_mask
= (GdkImage
*) NULL
;
1259 if (bitmap
.GetMask())
1261 gdk_image_mask
= gdk_image_get( bitmap
.GetMask()->GetBitmap(),
1263 bitmap
.GetWidth(), bitmap
.GetHeight() );
1265 SetMaskColour( 16, 16, 16 ); // anything unlikely and dividable
1268 GdkVisual
*visual
= gdk_window_get_visual( bitmap
.GetPixmap() );
1269 if (visual
== NULL
) visual
= gdk_window_get_visual( (GdkWindow
*) &gdk_root_parent
);
1270 int bpp
= visual
->depth
;
1271 if ((bpp
== 16) && (visual
->red_mask
!= 0xf800)) bpp
= 15;
1273 GdkColormap
*cmap
= gtk_widget_get_default_colormap();
1276 for (int j
= 0; j
< bitmap
.GetHeight(); j
++)
1278 for (int i
= 0; i
< bitmap
.GetWidth(); i
++)
1280 wxInt32 pixel
= gdk_image_get_pixel( gdk_image
, i
, j
);
1281 pixel
= wxINT32_SWAP_ON_BE( pixel
);
1284 data
[pos
] = cmap
->colors
[pixel
].red
>> 8;
1285 data
[pos
+1] = cmap
->colors
[pixel
].green
>> 8;
1286 data
[pos
+2] = cmap
->colors
[pixel
].blue
>> 8;
1287 } else if (bpp
== 15)
1289 data
[pos
] = (pixel
>> 7) & 0xf8;
1290 data
[pos
+1] = (pixel
>> 2) & 0xf8;
1291 data
[pos
+2] = (pixel
<< 3) & 0xf8;
1292 } else if (bpp
== 16)
1294 data
[pos
] = (pixel
>> 8) & 0xf8;
1295 data
[pos
+1] = (pixel
>> 3) & 0xfc;
1296 data
[pos
+2] = (pixel
<< 3) & 0xf8;
1299 data
[pos
] = (pixel
>> 16) & 0xff;
1300 data
[pos
+1] = (pixel
>> 8) & 0xff;
1301 data
[pos
+2] = pixel
& 0xff;
1306 int mask_pixel
= gdk_image_get_pixel( gdk_image_mask
, i
, j
);
1307 if (mask_pixel
== 0)
1319 gdk_image_destroy( gdk_image
);
1320 if (gdk_image_mask
) gdk_image_destroy( gdk_image_mask
);
1325 //-----------------------------------------------------------------------------
1326 // Motif conversion routines
1327 //-----------------------------------------------------------------------------
1332 #include "wx/utils.h"
1335 wxBitmap
wxImage::ConvertToBitmap() const
1339 wxCHECK_MSG( Ok(), bitmap
, _T("invalid image") );
1341 int width
= GetWidth();
1342 int height
= GetHeight();
1344 bitmap
.SetHeight( height
);
1345 bitmap
.SetWidth( width
);
1347 Display
*dpy
= (Display
*) wxGetDisplay();
1348 Visual
* vis
= DefaultVisual( dpy
, DefaultScreen( dpy
) );
1349 int bpp
= DefaultDepth( dpy
, DefaultScreen( dpy
) );
1353 XImage
*data_image
= XCreateImage( dpy
, vis
, bpp
, ZPixmap
, 0, 0, width
, height
, 32, 0 );
1354 data_image
->data
= (char*) malloc( data_image
->bytes_per_line
* data_image
->height
);
1356 bitmap
.Create( width
, height
, bpp
);
1361 GdkImage *mask_image = (GdkImage*) NULL;
1365 unsigned char *mask_data = (unsigned char*)malloc( ((width >> 3)+8) * height );
1367 mask_image = gdk_image_new_bitmap( gdk_visual_get_system(), mask_data, width, height );
1369 wxMask *mask = new wxMask();
1370 mask->m_bitmap = gdk_pixmap_new( (GdkWindow*)&gdk_root_parent, width, height, 1 );
1372 bitmap.SetMask( mask );
1376 // Retrieve depth info
1378 XVisualInfo vinfo_template
;
1381 vinfo_template
.visual
= vis
;
1382 vinfo_template
.visualid
= XVisualIDFromVisual( vis
);
1383 vinfo_template
.depth
= bpp
;
1386 vi
= XGetVisualInfo( dpy
, VisualIDMask
|VisualDepthMask
, &vinfo_template
, &nitem
);
1388 wxCHECK_MSG( vi
, wxNullBitmap
, _T("no visual") );
1392 if ((bpp
== 16) && (vi
->red_mask
!= 0xf800)) bpp
= 15;
1393 if (bpp
< 8) bpp
= 8;
1397 enum byte_order
{ RGB
, RBG
, BRG
, BGR
, GRB
, GBR
};
1398 byte_order b_o
= RGB
;
1402 if ((vi
->red_mask
> vi
->green_mask
) && (vi
->green_mask
> vi
->blue_mask
)) b_o
= RGB
;
1403 else if ((vi
->red_mask
> vi
->blue_mask
) && (vi
->blue_mask
> vi
->green_mask
)) b_o
= RGB
;
1404 else if ((vi
->blue_mask
> vi
->red_mask
) && (vi
->red_mask
> vi
->green_mask
)) b_o
= BRG
;
1405 else if ((vi
->blue_mask
> vi
->green_mask
) && (vi
->green_mask
> vi
->red_mask
)) b_o
= BGR
;
1406 else if ((vi
->green_mask
> vi
->red_mask
) && (vi
->red_mask
> vi
->blue_mask
)) b_o
= GRB
;
1407 else if ((vi
->green_mask
> vi
->blue_mask
) && (vi
->blue_mask
> vi
->red_mask
)) b_o
= GBR
;
1411 int r_mask = GetMaskRed();
1412 int g_mask = GetMaskGreen();
1413 int b_mask = GetMaskBlue();
1419 Colormap cmap
= (Colormap
) wxTheApp
->GetMainColormap( dpy
);
1421 for (int i
= 0; i
< 256; i
++) colors
[i
].pixel
= i
;
1422 XQueryColors( dpy
, cmap
, colors
, 256 );
1425 unsigned char* data
= GetData();
1428 for (int y
= 0; y
< height
; y
++)
1430 for (int x
= 0; x
< width
; x
++)
1432 int r
= data
[index
];
1434 int g
= data
[index
];
1436 int b
= data
[index
];
1442 if ((r == r_mask) && (b == b_mask) && (g == g_mask))
1443 gdk_image_put_pixel( mask_image, x, y, 1 );
1445 gdk_image_put_pixel( mask_image, x, y, 0 );
1455 if (wxTheApp->m_colorCube)
1457 pixel = wxTheApp->m_colorCube
1458 [ ((r & 0xf8) << 7) + ((g & 0xf8) << 2) + ((b & 0xf8) >> 3) ];
1463 int max
= 3 * (65536);
1464 for (int i
= 0; i
< 256; i
++)
1466 int rdiff
= (r
<< 8) - colors
[i
].red
;
1467 int gdiff
= (g
<< 8) - colors
[i
].green
;
1468 int bdiff
= (b
<< 8) - colors
[i
].blue
;
1469 int sum
= abs (rdiff
) + abs (gdiff
) + abs (bdiff
);
1470 if (sum
< max
) { pixel
= i
; max
= sum
; }
1475 XPutPixel( data_image
, x
, y
, pixel
);
1480 int pixel
= ((r
& 0xf8) << 7) | ((g
& 0xf8) << 2) | ((b
& 0xf8) >> 3);
1481 XPutPixel( data_image
, x
, y
, pixel
);
1486 int pixel
= ((r
& 0xf8) << 8) | ((g
& 0xfc) << 3) | ((b
& 0xf8) >> 3);
1487 XPutPixel( data_image
, x
, y
, pixel
);
1496 case RGB
: pixel
= (r
<< 16) | (g
<< 8) | b
; break;
1497 case RBG
: pixel
= (r
<< 16) | (b
<< 8) | g
; break;
1498 case BRG
: pixel
= (b
<< 16) | (r
<< 8) | g
; break;
1499 case BGR
: pixel
= (b
<< 16) | (g
<< 8) | r
; break;
1500 case GRB
: pixel
= (g
<< 16) | (r
<< 8) | b
; break;
1501 case GBR
: pixel
= (g
<< 16) | (b
<< 8) | r
; break;
1503 XPutPixel( data_image
, x
, y
, pixel
);
1513 gcvalues
.foreground
= BlackPixel( dpy
, DefaultScreen( dpy
) );
1514 GC gc
= XCreateGC( dpy
, RootWindow ( dpy
, DefaultScreen(dpy
) ), GCForeground
, &gcvalues
);
1515 XPutImage( dpy
, (Drawable
)bitmap
.GetPixmap(), gc
, data_image
, 0, 0, 0, 0, width
, height
);
1517 XDestroyImage( data_image
);
1525 GdkGC *mask_gc = gdk_gc_new( bitmap.GetMask()->GetBitmap() );
1527 gdk_draw_image( bitmap.GetMask()->GetBitmap(), mask_gc, mask_image, 0, 0, 0, 0, width, height );
1529 gdk_image_destroy( mask_image );
1530 gdk_gc_unref( mask_gc );
1537 wxImage::wxImage( const wxBitmap
&bitmap
)
1539 wxCHECK_RET( bitmap
.Ok(), _T("invalid bitmap") );
1541 Display
*dpy
= (Display
*) wxGetDisplay();
1542 Visual
* vis
= DefaultVisual( dpy
, DefaultScreen( dpy
) );
1543 int bpp
= DefaultDepth( dpy
, DefaultScreen( dpy
) );
1545 XImage
*ximage
= XGetImage( dpy
,
1546 (Drawable
)bitmap
.GetPixmap(),
1548 bitmap
.GetWidth(), bitmap
.GetHeight(),
1549 AllPlanes
, ZPixmap
);
1551 wxCHECK_RET( ximage
, _T("couldn't create image") );
1553 Create( bitmap
.GetWidth(), bitmap
.GetHeight() );
1554 char unsigned *data
= GetData();
1558 XDestroyImage( ximage
);
1559 wxFAIL_MSG( _T("couldn't create image") );
1564 GdkImage *gdk_image_mask = (GdkImage*) NULL;
1565 if (bitmap.GetMask())
1567 gdk_image_mask = gdk_image_get( bitmap.GetMask()->GetBitmap(),
1569 bitmap.GetWidth(), bitmap.GetHeight() );
1571 SetMaskColour( 16, 16, 16 ); // anything unlikely and dividable
1575 // Retrieve depth info
1577 XVisualInfo vinfo_template
;
1580 vinfo_template
.visual
= vis
;
1581 vinfo_template
.visualid
= XVisualIDFromVisual( vis
);
1582 vinfo_template
.depth
= bpp
;
1585 vi
= XGetVisualInfo( dpy
, VisualIDMask
|VisualDepthMask
, &vinfo_template
, &nitem
);
1587 wxCHECK_RET( vi
, _T("no visual") );
1589 if ((bpp
== 16) && (vi
->red_mask
!= 0xf800)) bpp
= 15;
1596 Colormap cmap
= (Colormap
)wxTheApp
->GetMainColormap( dpy
);
1598 for (int i
= 0; i
< 256; i
++) colors
[i
].pixel
= i
;
1599 XQueryColors( dpy
, cmap
, colors
, 256 );
1603 for (int j
= 0; j
< bitmap
.GetHeight(); j
++)
1605 for (int i
= 0; i
< bitmap
.GetWidth(); i
++)
1607 int pixel
= XGetPixel( ximage
, i
, j
);
1610 data
[pos
] = colors
[pixel
].red
>> 8;
1611 data
[pos
+1] = colors
[pixel
].green
>> 8;
1612 data
[pos
+2] = colors
[pixel
].blue
>> 8;
1613 } else if (bpp
== 15)
1615 data
[pos
] = (pixel
>> 7) & 0xf8;
1616 data
[pos
+1] = (pixel
>> 2) & 0xf8;
1617 data
[pos
+2] = (pixel
<< 3) & 0xf8;
1618 } else if (bpp
== 16)
1620 data
[pos
] = (pixel
>> 8) & 0xf8;
1621 data
[pos
+1] = (pixel
>> 3) & 0xfc;
1622 data
[pos
+2] = (pixel
<< 3) & 0xf8;
1625 data
[pos
] = (pixel
>> 16) & 0xff;
1626 data
[pos
+1] = (pixel
>> 8) & 0xff;
1627 data
[pos
+2] = pixel
& 0xff;
1633 int mask_pixel = gdk_image_get_pixel( gdk_image_mask, i, j );
1634 if (mask_pixel == 0)
1647 XDestroyImage( ximage
);
1649 if (gdk_image_mask) gdk_image_destroy( gdk_image_mask );
1654 // A module to allow wxImage initialization/cleanup
1655 // without calling these functions from app.cpp or from
1656 // the user's application.
1658 class wxImageModule
: public wxModule
1660 DECLARE_DYNAMIC_CLASS(wxImageModule
)
1663 bool OnInit() { wxImage::InitStandardHandlers(); return TRUE
; };
1664 void OnExit() { wxImage::CleanUpHandlers(); };
1667 IMPLEMENT_DYNAMIC_CLASS(wxImageModule
, wxModule
)