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
, wxT("invalid image") );
159 wxCHECK_MSG( (width
> 0) && (height
> 0), image
, wxT("invalid image size") );
161 image
.Create( width
, height
);
163 char unsigned *data
= image
.GetData();
165 wxCHECK_MSG( data
, image
, wxT("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
, wxT("invalid image") );
198 wxCHECK_MSG( (rect
.GetLeft()>=0) && (rect
.GetTop()>=0) && (rect
.GetRight()<=GetWidth()) && (rect
.GetBottom()<=GetHeight())
199 , image
, wxT("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
, wxT("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(), wxT("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
), wxT("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, wxT("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, wxT("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, wxT("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, wxT("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, wxT("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, wxT("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
, wxT("invalid image") );
296 return M_IMGDATA
->m_data
;
299 void wxImage::SetData( char unsigned *data
)
301 wxCHECK_RET( Ok(), wxT("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(), wxT("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, wxT("invalid image") );
333 return M_IMGDATA
->m_maskRed
;
336 unsigned char wxImage::GetMaskGreen() const
338 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
340 return M_IMGDATA
->m_maskGreen
;
343 unsigned char wxImage::GetMaskBlue() const
345 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
347 return M_IMGDATA
->m_maskBlue
;
350 void wxImage::SetMask( bool mask
)
352 wxCHECK_RET( Ok(), wxT("invalid image") );
354 M_IMGDATA
->m_hasMask
= mask
;
357 bool wxImage::HasMask() const
359 wxCHECK_MSG( Ok(), FALSE
, wxT("invalid image") );
361 return M_IMGDATA
->m_hasMask
;
364 int wxImage::GetWidth() const
366 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
368 return M_IMGDATA
->m_width
;
371 int wxImage::GetHeight() const
373 wxCHECK_MSG( Ok(), 0, wxT("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( wxT("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( wxT("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
440 bool wxImage::CanRead( const wxString
&name
)
443 wxFileInputStream
stream(name
);
444 return CanRead(stream
);
452 bool wxImage::CanRead( wxInputStream
&stream
)
454 wxList
&list
=GetHandlers();
456 for ( wxList::Node
*node
= list
.GetFirst(); node
; node
= node
->GetNext() )
458 wxImageHandler
*handler
=(wxImageHandler
*)node
->GetData();
459 if (handler
->CanRead( stream
))
466 bool wxImage::LoadFile( wxInputStream
& stream
, long type
)
470 m_refData
= new wxImageRefData
;
472 wxImageHandler
*handler
;
474 if (type
==wxBITMAP_TYPE_ANY
)
476 wxList
&list
=GetHandlers();
478 for ( wxList::Node
*node
= list
.GetFirst(); node
; node
= node
->GetNext() )
480 handler
=(wxImageHandler
*)node
->GetData();
481 if (handler
->CanRead( stream
))
482 return handler
->LoadFile( this, stream
);
486 wxLogWarning( wxT("No handler found for this image.") );
490 handler
= FindHandler(type
);
494 wxLogWarning( wxT("No image handler for type %d defined."), type
);
499 return handler
->LoadFile( this, stream
);
502 bool wxImage::LoadFile( wxInputStream
& stream
, const wxString
& mimetype
)
506 m_refData
= new wxImageRefData
;
508 wxImageHandler
*handler
= FindHandlerMime(mimetype
);
512 wxLogWarning( wxT("No image handler for type %s defined."), mimetype
.GetData() );
517 return handler
->LoadFile( this, stream
);
520 bool wxImage::SaveFile( wxOutputStream
& stream
, int type
)
522 wxCHECK_MSG( Ok(), FALSE
, wxT("invalid image") );
524 wxImageHandler
*handler
= FindHandler(type
);
528 wxLogWarning( wxT("No image handler for type %d defined."), type
);
533 return handler
->SaveFile( this, stream
);
536 bool wxImage::SaveFile( wxOutputStream
& stream
, const wxString
& mimetype
)
538 wxCHECK_MSG( Ok(), FALSE
, wxT("invalid image") );
540 wxImageHandler
*handler
= FindHandlerMime(mimetype
);
544 wxLogWarning( wxT("No image handler for type %s defined."), mimetype
.GetData() );
549 return handler
->SaveFile( this, stream
);
551 #endif // wxUSE_STREAMS
553 void wxImage::AddHandler( wxImageHandler
*handler
)
555 // make sure that the memory will be freed at the program end
556 sm_handlers
.DeleteContents(TRUE
);
558 sm_handlers
.Append( handler
);
561 void wxImage::InsertHandler( wxImageHandler
*handler
)
563 // make sure that the memory will be freed at the program end
564 sm_handlers
.DeleteContents(TRUE
);
566 sm_handlers
.Insert( handler
);
569 bool wxImage::RemoveHandler( const wxString
& name
)
571 wxImageHandler
*handler
= FindHandler(name
);
574 sm_handlers
.DeleteObject(handler
);
581 wxImageHandler
*wxImage::FindHandler( const wxString
& name
)
583 wxNode
*node
= sm_handlers
.First();
586 wxImageHandler
*handler
= (wxImageHandler
*)node
->Data();
587 if (handler
->GetName().Cmp(name
) == 0) return handler
;
591 return (wxImageHandler
*)NULL
;
594 wxImageHandler
*wxImage::FindHandler( const wxString
& extension
, long bitmapType
)
596 wxNode
*node
= sm_handlers
.First();
599 wxImageHandler
*handler
= (wxImageHandler
*)node
->Data();
600 if ( (handler
->GetExtension().Cmp(extension
) == 0) &&
601 (bitmapType
== -1 || handler
->GetType() == bitmapType
) )
605 return (wxImageHandler
*)NULL
;
608 wxImageHandler
*wxImage::FindHandler( long bitmapType
)
610 wxNode
*node
= sm_handlers
.First();
613 wxImageHandler
*handler
= (wxImageHandler
*)node
->Data();
614 if (handler
->GetType() == bitmapType
) return handler
;
620 wxImageHandler
*wxImage::FindHandlerMime( const wxString
& mimetype
)
622 wxNode
*node
= sm_handlers
.First();
625 wxImageHandler
*handler
= (wxImageHandler
*)node
->Data();
626 if (handler
->GetMimeType().IsSameAs(mimetype
, FALSE
)) return handler
;
632 void wxImage::InitStandardHandlers()
634 AddHandler( new wxBMPHandler
);
637 void wxImage::CleanUpHandlers()
639 wxNode
*node
= sm_handlers
.First();
642 wxImageHandler
*handler
= (wxImageHandler
*)node
->Data();
643 wxNode
*next
= node
->Next();
650 //-----------------------------------------------------------------------------
652 //-----------------------------------------------------------------------------
654 #if !USE_SHARED_LIBRARIES
655 IMPLEMENT_ABSTRACT_CLASS(wxImageHandler
,wxObject
)
659 bool wxImageHandler::LoadFile( wxImage
*WXUNUSED(image
), wxInputStream
& WXUNUSED(stream
), bool WXUNUSED(verbose
) )
664 bool wxImageHandler::SaveFile( wxImage
*WXUNUSED(image
), wxOutputStream
& WXUNUSED(stream
), bool WXUNUSED(verbose
) )
669 bool wxImageHandler::CanRead( const wxString
& name
)
672 if (wxFileExists(name
))
674 wxFileInputStream
stream(name
);
675 return CanRead(stream
);
679 wxLogError( wxT("Can't check image format of file '%s': file does not exist."), name
.c_str() );
683 #else // !wxUSE_STREAMS
685 #endif // wxUSE_STREAMS
690 #endif // wxUSE_STREAMS
692 //-----------------------------------------------------------------------------
693 // MSW conversion routines
694 //-----------------------------------------------------------------------------
698 wxBitmap
wxImage::ConvertToBitmap() const
703 // sizeLimit is the MS upper limit for the DIB size
705 int sizeLimit
= 1024*768*3;
707 int sizeLimit
= 0x7fff ;
710 // width and height of the device-dependent bitmap
711 int width
= GetWidth();
712 int bmpHeight
= GetHeight();
714 // calc the number of bytes per scanline and padding
715 int bytePerLine
= width
*3;
716 int sizeDWORD
= sizeof( DWORD
);
717 int lineBoundary
= bytePerLine
% sizeDWORD
;
719 if( lineBoundary
> 0 )
721 padding
= sizeDWORD
- lineBoundary
;
722 bytePerLine
+= padding
;
724 // calc the number of DIBs and heights of DIBs
727 int height
= sizeLimit
/bytePerLine
;
728 if( height
>= bmpHeight
)
732 numDIB
= bmpHeight
/ height
;
733 hRemain
= bmpHeight
% height
;
734 if( hRemain
>0 ) numDIB
++;
737 // set bitmap parameters
739 wxCHECK_MSG( Ok(), bitmap
, wxT("invalid image") );
740 bitmap
.SetWidth( width
);
741 bitmap
.SetHeight( bmpHeight
);
742 bitmap
.SetDepth( wxDisplayDepth() );
744 // create a DIB header
745 int headersize
= sizeof(BITMAPINFOHEADER
);
746 LPBITMAPINFO lpDIBh
= (BITMAPINFO
*) malloc( headersize
);
747 wxCHECK_MSG( lpDIBh
, bitmap
, wxT("could not allocate memory for DIB header") );
748 // Fill in the DIB header
749 lpDIBh
->bmiHeader
.biSize
= headersize
;
750 lpDIBh
->bmiHeader
.biWidth
= (DWORD
)width
;
751 lpDIBh
->bmiHeader
.biHeight
= (DWORD
)(-height
);
752 lpDIBh
->bmiHeader
.biSizeImage
= bytePerLine
*height
;
753 // the general formula for biSizeImage:
754 // ( ( ( ((DWORD)width*24) +31 ) & ~31 ) >> 3 ) * height;
755 lpDIBh
->bmiHeader
.biPlanes
= 1;
756 lpDIBh
->bmiHeader
.biBitCount
= 24;
757 lpDIBh
->bmiHeader
.biCompression
= BI_RGB
;
758 lpDIBh
->bmiHeader
.biClrUsed
= 0;
759 // These seem not really needed for our purpose here.
760 lpDIBh
->bmiHeader
.biClrImportant
= 0;
761 lpDIBh
->bmiHeader
.biXPelsPerMeter
= 0;
762 lpDIBh
->bmiHeader
.biYPelsPerMeter
= 0;
763 // memory for DIB data
764 unsigned char *lpBits
;
765 lpBits
= (unsigned char *)malloc( lpDIBh
->bmiHeader
.biSizeImage
);
768 wxFAIL_MSG( wxT("could not allocate memory for DIB") );
773 // create and set the device-dependent bitmap
774 HDC hdc
= ::GetDC(NULL
);
775 HDC memdc
= ::CreateCompatibleDC( hdc
);
777 hbitmap
= ::CreateCompatibleBitmap( hdc
, width
, bmpHeight
);
778 ::SelectObject( memdc
, hbitmap
);
780 // copy image data into DIB data and then into DDB (in a loop)
781 unsigned char *data
= GetData();
784 unsigned char *ptdata
= data
;
785 unsigned char *ptbits
;
787 for( n
=0; n
<numDIB
; n
++ )
789 if( numDIB
> 1 && n
== numDIB
-1 && hRemain
> 0 )
791 // redefine height and size of the (possibly) last smaller DIB
792 // memory is not reallocated
794 lpDIBh
->bmiHeader
.biHeight
= (DWORD
)(-height
);
795 lpDIBh
->bmiHeader
.biSizeImage
= bytePerLine
*height
;
799 for( j
=0; j
<height
; j
++ )
801 for( i
=0; i
<width
; i
++ )
803 *(ptbits
++) = *(ptdata
+2);
804 *(ptbits
++) = *(ptdata
+1);
805 *(ptbits
++) = *(ptdata
);
808 for( i
=0; i
< padding
; i
++ ) *(ptbits
++) = 0;
810 ::StretchDIBits( memdc
, 0, origin
, width
, height
,\
811 0, 0, width
, height
, lpBits
, lpDIBh
, DIB_RGB_COLORS
, SRCCOPY
);
813 // if numDIB = 1, lines below can also be used
814 // hbitmap = CreateDIBitmap( hdc, &(lpDIBh->bmiHeader), CBM_INIT, lpBits, lpDIBh, DIB_RGB_COLORS );
815 // The above line is equivalent to the following two lines.
816 // hbitmap = ::CreateCompatibleBitmap( hdc, width, height );
817 // ::SetDIBits( hdc, hbitmap, 0, height, lpBits, lpDIBh, DIB_RGB_COLORS);
818 // or the following lines
819 // hbitmap = ::CreateCompatibleBitmap( hdc, width, height );
820 // HDC memdc = ::CreateCompatibleDC( hdc );
821 // ::SelectObject( memdc, hbitmap);
822 // ::SetDIBitsToDevice( memdc, 0, 0, width, height,
823 // 0, 0, 0, height, (void *)lpBits, lpDIBh, DIB_RGB_COLORS);
824 // ::SelectObject( memdc, 0 );
825 // ::DeleteDC( memdc );
827 bitmap
.SetHBITMAP( (WXHBITMAP
) hbitmap
);
829 // similarly, created an mono-bitmap for the possible mask
832 hbitmap
= ::CreateBitmap( (WORD
)width
, (WORD
)bmpHeight
, 1, 1, NULL
);
833 ::SelectObject( memdc
, hbitmap
);
834 if( numDIB
== 1 ) height
= bmpHeight
;
835 else height
= sizeLimit
/bytePerLine
;
836 lpDIBh
->bmiHeader
.biHeight
= (DWORD
)(-height
);
837 lpDIBh
->bmiHeader
.biSizeImage
= bytePerLine
*height
;
839 unsigned char r
= GetMaskRed();
840 unsigned char g
= GetMaskGreen();
841 unsigned char b
= GetMaskBlue();
842 unsigned char zero
= 0, one
= 255;
844 for( n
=0; n
<numDIB
; n
++ )
846 if( numDIB
> 1 && n
== numDIB
- 1 && hRemain
> 0 )
848 // redefine height and size of the (possibly) last smaller DIB
849 // memory is not reallocated
851 lpDIBh
->bmiHeader
.biHeight
= (DWORD
)(-height
);
852 lpDIBh
->bmiHeader
.biSizeImage
= bytePerLine
*height
;
855 for( int j
=0; j
<height
; j
++ )
857 for(i
=0; i
<width
; i
++ )
859 if( (*(ptdata
++)!=r
) | (*(ptdata
++)!=g
) | (*(ptdata
++)!=b
) )
872 for( i
=0; i
< padding
; i
++ ) *(ptbits
++) = zero
;
874 ::StretchDIBits( memdc
, 0, origin
, width
, height
,\
875 0, 0, width
, height
, lpBits
, lpDIBh
, DIB_RGB_COLORS
, SRCCOPY
);
878 // create a wxMask object
879 wxMask
*mask
= new wxMask();
880 mask
->SetMaskBitmap( (WXHBITMAP
) hbitmap
);
881 bitmap
.SetMask( mask
);
882 // It will be deleted when the wxBitmap object is deleted (as of 01/1999)
883 /* The following can also be used but is slow to run
884 wxColour colour( GetMaskRed(), GetMaskGreen(), GetMaskBlue());
885 wxMask *mask = new wxMask( bitmap, colour );
886 bitmap.SetMask( mask );
890 // free allocated resources
891 ::SelectObject( memdc
, 0 );
893 ::ReleaseDC(NULL
, hdc
);
897 // check the wxBitmap object
898 if( bitmap
.GetHBITMAP() )
899 bitmap
.SetOk( TRUE
);
901 bitmap
.SetOk( FALSE
);
906 wxImage::wxImage( const wxBitmap
&bitmap
)
911 wxFAIL_MSG( wxT("invalid bitmap") );
915 // create an wxImage object
916 int width
= bitmap
.GetWidth();
917 int height
= bitmap
.GetHeight();
918 Create( width
, height
);
919 unsigned char *data
= GetData();
922 wxFAIL_MSG( wxT("could not allocate data for image") );
926 // calc the number of bytes per scanline and padding in the DIB
927 int bytePerLine
= width
*3;
928 int sizeDWORD
= sizeof( DWORD
);
929 int lineBoundary
= bytePerLine
% sizeDWORD
;
931 if( lineBoundary
> 0 )
933 padding
= sizeDWORD
- lineBoundary
;
934 bytePerLine
+= padding
;
937 // create a DIB header
938 int headersize
= sizeof(BITMAPINFOHEADER
);
939 LPBITMAPINFO lpDIBh
= (BITMAPINFO
*) malloc( headersize
);
942 wxFAIL_MSG( wxT("could not allocate data for DIB header") );
946 // Fill in the DIB header
947 lpDIBh
->bmiHeader
.biSize
= headersize
;
948 lpDIBh
->bmiHeader
.biWidth
= width
;
949 lpDIBh
->bmiHeader
.biHeight
= -height
;
950 lpDIBh
->bmiHeader
.biSizeImage
= bytePerLine
* height
;
951 lpDIBh
->bmiHeader
.biPlanes
= 1;
952 lpDIBh
->bmiHeader
.biBitCount
= 24;
953 lpDIBh
->bmiHeader
.biCompression
= BI_RGB
;
954 lpDIBh
->bmiHeader
.biClrUsed
= 0;
955 // These seem not really needed for our purpose here.
956 lpDIBh
->bmiHeader
.biClrImportant
= 0;
957 lpDIBh
->bmiHeader
.biXPelsPerMeter
= 0;
958 lpDIBh
->bmiHeader
.biYPelsPerMeter
= 0;
959 // memory for DIB data
960 unsigned char *lpBits
;
961 lpBits
= (unsigned char *) malloc( lpDIBh
->bmiHeader
.biSizeImage
);
964 wxFAIL_MSG( wxT("could not allocate data for DIB") );
970 // copy data from the device-dependent bitmap to the DIB
971 HDC hdc
= ::GetDC(NULL
);
973 hbitmap
= (HBITMAP
) bitmap
.GetHBITMAP();
974 ::GetDIBits( hdc
, hbitmap
, 0, height
, lpBits
, lpDIBh
, DIB_RGB_COLORS
);
976 // copy DIB data into the wxImage object
978 unsigned char *ptdata
= data
;
979 unsigned char *ptbits
= lpBits
;
980 for( i
=0; i
<height
; i
++ )
982 for( j
=0; j
<width
; j
++ )
984 *(ptdata
++) = *(ptbits
+2);
985 *(ptdata
++) = *(ptbits
+1);
986 *(ptdata
++) = *(ptbits
);
992 // similarly, set data according to the possible mask bitmap
993 if( bitmap
.GetMask() && bitmap
.GetMask()->GetMaskBitmap() )
995 hbitmap
= (HBITMAP
) bitmap
.GetMask()->GetMaskBitmap();
996 // memory DC created, color set, data copied, and memory DC deleted
997 HDC memdc
= ::CreateCompatibleDC( hdc
);
998 ::SetTextColor( memdc
, RGB( 0, 0, 0 ) );
999 ::SetBkColor( memdc
, RGB( 255, 255, 255 ) );
1000 ::GetDIBits( memdc
, hbitmap
, 0, height
, lpBits
, lpDIBh
, DIB_RGB_COLORS
);
1001 ::DeleteDC( memdc
);
1002 // background color set to RGB(16,16,16) in consistent with wxGTK
1003 unsigned char r
=16, g
=16, b
=16;
1006 for( i
=0; i
<height
; i
++ )
1008 for( j
=0; j
<width
; j
++ )
1022 SetMaskColour( r
, g
, b
);
1029 // free allocated resources
1030 ::ReleaseDC(NULL
, hdc
);
1037 //-----------------------------------------------------------------------------
1038 // GTK conversion routines
1039 //-----------------------------------------------------------------------------
1043 #include "gtk/gtk.h"
1044 #include "gdk/gdk.h"
1045 #include "gdk/gdkx.h"
1047 #if (GTK_MINOR_VERSION > 0)
1048 #include "gdk/gdkrgb.h"
1051 wxBitmap
wxImage::ConvertToBitmap() const
1055 wxCHECK_MSG( Ok(), bitmap
, wxT("invalid image") );
1057 int width
= GetWidth();
1058 int height
= GetHeight();
1060 bitmap
.SetHeight( height
);
1061 bitmap
.SetWidth( width
);
1063 bitmap
.SetPixmap( gdk_pixmap_new( (GdkWindow
*)&gdk_root_parent
, width
, height
, -1 ) );
1067 GdkVisual
*visual
= gdk_window_get_visual( bitmap
.GetPixmap() );
1068 if (visual
== NULL
) visual
= gdk_visual_get_system();
1069 int bpp
= visual
->depth
;
1071 bitmap
.SetDepth( bpp
);
1073 if ((bpp
== 16) && (visual
->red_mask
!= 0xf800)) bpp
= 15;
1074 if (bpp
< 8) bpp
= 8;
1076 #if (GTK_MINOR_VERSION > 0)
1078 if (!HasMask() && (bpp
> 8))
1080 static bool s_hasInitialized
= FALSE
;
1082 if (!s_hasInitialized
)
1085 s_hasInitialized
= TRUE
;
1088 GdkGC
*gc
= gdk_gc_new( bitmap
.GetPixmap() );
1090 gdk_draw_rgb_image( bitmap
.GetPixmap(),
1094 GDK_RGB_DITHER_NONE
,
1105 // Create picture image
1107 GdkImage
*data_image
=
1108 gdk_image_new( GDK_IMAGE_FASTEST
, gdk_visual_get_system(), width
, height
);
1110 // Create mask image
1112 GdkImage
*mask_image
= (GdkImage
*) NULL
;
1116 unsigned char *mask_data
= (unsigned char*)malloc( ((width
>> 3)+8) * height
);
1118 mask_image
= gdk_image_new_bitmap( gdk_visual_get_system(), mask_data
, width
, height
);
1120 wxMask
*mask
= new wxMask();
1121 mask
->m_bitmap
= gdk_pixmap_new( (GdkWindow
*)&gdk_root_parent
, width
, height
, 1 );
1123 bitmap
.SetMask( mask
);
1128 enum byte_order
{ RGB
, RBG
, BRG
, BGR
, GRB
, GBR
};
1129 byte_order b_o
= RGB
;
1133 GdkVisual
*visual
= gdk_visual_get_system();
1134 if ((visual
->red_mask
> visual
->green_mask
) && (visual
->green_mask
> visual
->blue_mask
)) b_o
= RGB
;
1135 else if ((visual
->red_mask
> visual
->blue_mask
) && (visual
->blue_mask
> visual
->green_mask
)) b_o
= RGB
;
1136 else if ((visual
->blue_mask
> visual
->red_mask
) && (visual
->red_mask
> visual
->green_mask
)) b_o
= BRG
;
1137 else if ((visual
->blue_mask
> visual
->green_mask
) && (visual
->green_mask
> visual
->red_mask
)) b_o
= BGR
;
1138 else if ((visual
->green_mask
> visual
->red_mask
) && (visual
->red_mask
> visual
->blue_mask
)) b_o
= GRB
;
1139 else if ((visual
->green_mask
> visual
->blue_mask
) && (visual
->blue_mask
> visual
->red_mask
)) b_o
= GBR
;
1142 int r_mask
= GetMaskRed();
1143 int g_mask
= GetMaskGreen();
1144 int b_mask
= GetMaskBlue();
1146 unsigned char* data
= GetData();
1149 for (int y
= 0; y
< height
; y
++)
1151 for (int x
= 0; x
< width
; x
++)
1153 int r
= data
[index
];
1155 int g
= data
[index
];
1157 int b
= data
[index
];
1162 if ((r
== r_mask
) && (b
== b_mask
) && (g
== g_mask
))
1163 gdk_image_put_pixel( mask_image
, x
, y
, 1 );
1165 gdk_image_put_pixel( mask_image
, x
, y
, 0 );
1170 if ((r
== r_mask
) && (b
== b_mask
) && (g
== g_mask
))
1171 gdk_image_put_pixel( mask_image
, x
, y
, 1 );
1173 gdk_image_put_pixel( mask_image
, x
, y
, 0 );
1181 if (wxTheApp
->m_colorCube
)
1183 pixel
= wxTheApp
->m_colorCube
[ ((r
& 0xf8) << 7) + ((g
& 0xf8) << 2) + ((b
& 0xf8) >> 3) ];
1187 GdkColormap
*cmap
= gtk_widget_get_default_colormap();
1188 GdkColor
*colors
= cmap
->colors
;
1189 int max
= 3 * (65536);
1191 for (int i
= 0; i
< cmap
->size
; i
++)
1193 int rdiff
= (r
<< 8) - colors
[i
].red
;
1194 int gdiff
= (g
<< 8) - colors
[i
].green
;
1195 int bdiff
= (b
<< 8) - colors
[i
].blue
;
1196 int sum
= ABS (rdiff
) + ABS (gdiff
) + ABS (bdiff
);
1197 if (sum
< max
) { pixel
= i
; max
= sum
; }
1201 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
1207 guint32 pixel
= ((r
& 0xf8) << 7) | ((g
& 0xf8) << 2) | ((b
& 0xf8) >> 3);
1208 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
1213 guint32 pixel
= ((r
& 0xf8) << 8) | ((g
& 0xfc) << 3) | ((b
& 0xf8) >> 3);
1214 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
1223 case RGB
: pixel
= (r
<< 16) | (g
<< 8) | b
; break;
1224 case RBG
: pixel
= (r
<< 16) | (b
<< 8) | g
; break;
1225 case BRG
: pixel
= (b
<< 16) | (r
<< 8) | g
; break;
1226 case BGR
: pixel
= (b
<< 16) | (g
<< 8) | r
; break;
1227 case GRB
: pixel
= (g
<< 16) | (r
<< 8) | b
; break;
1228 case GBR
: pixel
= (g
<< 16) | (b
<< 8) | r
; break;
1230 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
1239 GdkGC
*data_gc
= gdk_gc_new( bitmap
.GetPixmap() );
1241 gdk_draw_image( bitmap
.GetPixmap(), data_gc
, data_image
, 0, 0, 0, 0, width
, height
);
1243 gdk_image_destroy( data_image
);
1244 gdk_gc_unref( data_gc
);
1250 GdkGC
*mask_gc
= gdk_gc_new( bitmap
.GetMask()->GetBitmap() );
1252 gdk_draw_image( bitmap
.GetMask()->GetBitmap(), mask_gc
, mask_image
, 0, 0, 0, 0, width
, height
);
1254 gdk_image_destroy( mask_image
);
1255 gdk_gc_unref( mask_gc
);
1261 wxImage::wxImage( const wxBitmap
&bitmap
)
1263 wxCHECK_RET( bitmap
.Ok(), wxT("invalid bitmap") );
1265 GdkImage
*gdk_image
= (GdkImage
*) NULL
;
1266 if (bitmap
.GetPixmap())
1268 gdk_image
= gdk_image_get( bitmap
.GetPixmap(),
1270 bitmap
.GetWidth(), bitmap
.GetHeight() );
1272 if (bitmap
.GetBitmap())
1274 gdk_image
= gdk_image_get( bitmap
.GetBitmap(),
1276 bitmap
.GetWidth(), bitmap
.GetHeight() );
1279 wxFAIL_MSG( wxT("Ill-formed bitmap") );
1282 wxCHECK_RET( gdk_image
, wxT("couldn't create image") );
1284 Create( bitmap
.GetWidth(), bitmap
.GetHeight() );
1285 char unsigned *data
= GetData();
1289 gdk_image_destroy( gdk_image
);
1290 wxFAIL_MSG( wxT("couldn't create image") );
1294 GdkImage
*gdk_image_mask
= (GdkImage
*) NULL
;
1295 if (bitmap
.GetMask())
1297 gdk_image_mask
= gdk_image_get( bitmap
.GetMask()->GetBitmap(),
1299 bitmap
.GetWidth(), bitmap
.GetHeight() );
1301 SetMaskColour( 16, 16, 16 ); // anything unlikely and dividable
1304 GdkVisual
*visual
= (GdkVisual
*) NULL
;
1305 if (bitmap
.GetPixmap())
1306 visual
= gdk_window_get_visual( bitmap
.GetPixmap() );
1308 visual
= gdk_window_get_visual( bitmap
.GetBitmap() );
1310 if (visual
== NULL
) visual
= gdk_window_get_visual( (GdkWindow
*) &gdk_root_parent
);
1311 int bpp
= visual
->depth
;
1312 if ((bpp
== 16) && (visual
->red_mask
!= 0xf800)) bpp
= 15;
1314 GdkColormap
*cmap
= gtk_widget_get_default_colormap();
1317 for (int j
= 0; j
< bitmap
.GetHeight(); j
++)
1319 for (int i
= 0; i
< bitmap
.GetWidth(); i
++)
1321 wxInt32 pixel
= gdk_image_get_pixel( gdk_image
, i
, j
);
1322 // pixel = wxINT32_SWAP_ON_BE( pixel );
1325 data
[pos
] = cmap
->colors
[pixel
].red
>> 8;
1326 data
[pos
+1] = cmap
->colors
[pixel
].green
>> 8;
1327 data
[pos
+2] = cmap
->colors
[pixel
].blue
>> 8;
1328 } else if (bpp
== 15)
1330 data
[pos
] = (pixel
>> 7) & 0xf8;
1331 data
[pos
+1] = (pixel
>> 2) & 0xf8;
1332 data
[pos
+2] = (pixel
<< 3) & 0xf8;
1333 } else if (bpp
== 16)
1335 data
[pos
] = (pixel
>> 8) & 0xf8;
1336 data
[pos
+1] = (pixel
>> 3) & 0xfc;
1337 data
[pos
+2] = (pixel
<< 3) & 0xf8;
1340 data
[pos
] = (pixel
>> 16) & 0xff;
1341 data
[pos
+1] = (pixel
>> 8) & 0xff;
1342 data
[pos
+2] = pixel
& 0xff;
1347 int mask_pixel
= gdk_image_get_pixel( gdk_image_mask
, i
, j
);
1348 if (mask_pixel
== 0)
1360 gdk_image_destroy( gdk_image
);
1361 if (gdk_image_mask
) gdk_image_destroy( gdk_image_mask
);
1366 //-----------------------------------------------------------------------------
1367 // Motif conversion routines
1368 //-----------------------------------------------------------------------------
1373 #include "wx/utils.h"
1376 wxBitmap
wxImage::ConvertToBitmap() const
1380 wxCHECK_MSG( Ok(), bitmap
, wxT("invalid image") );
1382 int width
= GetWidth();
1383 int height
= GetHeight();
1385 bitmap
.SetHeight( height
);
1386 bitmap
.SetWidth( width
);
1388 Display
*dpy
= (Display
*) wxGetDisplay();
1389 Visual
* vis
= DefaultVisual( dpy
, DefaultScreen( dpy
) );
1390 int bpp
= DefaultDepth( dpy
, DefaultScreen( dpy
) );
1394 XImage
*data_image
= XCreateImage( dpy
, vis
, bpp
, ZPixmap
, 0, 0, width
, height
, 32, 0 );
1395 data_image
->data
= (char*) malloc( data_image
->bytes_per_line
* data_image
->height
);
1397 bitmap
.Create( width
, height
, bpp
);
1402 GdkImage *mask_image = (GdkImage*) NULL;
1406 unsigned char *mask_data = (unsigned char*)malloc( ((width >> 3)+8) * height );
1408 mask_image = gdk_image_new_bitmap( gdk_visual_get_system(), mask_data, width, height );
1410 wxMask *mask = new wxMask();
1411 mask->m_bitmap = gdk_pixmap_new( (GdkWindow*)&gdk_root_parent, width, height, 1 );
1413 bitmap.SetMask( mask );
1417 // Retrieve depth info
1419 XVisualInfo vinfo_template
;
1422 vinfo_template
.visual
= vis
;
1423 vinfo_template
.visualid
= XVisualIDFromVisual( vis
);
1424 vinfo_template
.depth
= bpp
;
1427 vi
= XGetVisualInfo( dpy
, VisualIDMask
|VisualDepthMask
, &vinfo_template
, &nitem
);
1429 wxCHECK_MSG( vi
, wxNullBitmap
, wxT("no visual") );
1433 if ((bpp
== 16) && (vi
->red_mask
!= 0xf800)) bpp
= 15;
1434 if (bpp
< 8) bpp
= 8;
1438 enum byte_order
{ RGB
, RBG
, BRG
, BGR
, GRB
, GBR
};
1439 byte_order b_o
= RGB
;
1443 if ((vi
->red_mask
> vi
->green_mask
) && (vi
->green_mask
> vi
->blue_mask
)) b_o
= RGB
;
1444 else if ((vi
->red_mask
> vi
->blue_mask
) && (vi
->blue_mask
> vi
->green_mask
)) b_o
= RGB
;
1445 else if ((vi
->blue_mask
> vi
->red_mask
) && (vi
->red_mask
> vi
->green_mask
)) b_o
= BRG
;
1446 else if ((vi
->blue_mask
> vi
->green_mask
) && (vi
->green_mask
> vi
->red_mask
)) b_o
= BGR
;
1447 else if ((vi
->green_mask
> vi
->red_mask
) && (vi
->red_mask
> vi
->blue_mask
)) b_o
= GRB
;
1448 else if ((vi
->green_mask
> vi
->blue_mask
) && (vi
->blue_mask
> vi
->red_mask
)) b_o
= GBR
;
1452 int r_mask = GetMaskRed();
1453 int g_mask = GetMaskGreen();
1454 int b_mask = GetMaskBlue();
1460 Colormap cmap
= (Colormap
) wxTheApp
->GetMainColormap( dpy
);
1462 for (int i
= 0; i
< 256; i
++) colors
[i
].pixel
= i
;
1463 XQueryColors( dpy
, cmap
, colors
, 256 );
1466 unsigned char* data
= GetData();
1469 for (int y
= 0; y
< height
; y
++)
1471 for (int x
= 0; x
< width
; x
++)
1473 int r
= data
[index
];
1475 int g
= data
[index
];
1477 int b
= data
[index
];
1483 if ((r == r_mask) && (b == b_mask) && (g == g_mask))
1484 gdk_image_put_pixel( mask_image, x, y, 1 );
1486 gdk_image_put_pixel( mask_image, x, y, 0 );
1496 if (wxTheApp->m_colorCube)
1498 pixel = wxTheApp->m_colorCube
1499 [ ((r & 0xf8) << 7) + ((g & 0xf8) << 2) + ((b & 0xf8) >> 3) ];
1504 int max
= 3 * (65536);
1505 for (int i
= 0; i
< 256; i
++)
1507 int rdiff
= (r
<< 8) - colors
[i
].red
;
1508 int gdiff
= (g
<< 8) - colors
[i
].green
;
1509 int bdiff
= (b
<< 8) - colors
[i
].blue
;
1510 int sum
= abs (rdiff
) + abs (gdiff
) + abs (bdiff
);
1511 if (sum
< max
) { pixel
= i
; max
= sum
; }
1516 XPutPixel( data_image
, x
, y
, pixel
);
1521 int pixel
= ((r
& 0xf8) << 7) | ((g
& 0xf8) << 2) | ((b
& 0xf8) >> 3);
1522 XPutPixel( data_image
, x
, y
, pixel
);
1527 int pixel
= ((r
& 0xf8) << 8) | ((g
& 0xfc) << 3) | ((b
& 0xf8) >> 3);
1528 XPutPixel( data_image
, x
, y
, pixel
);
1537 case RGB
: pixel
= (r
<< 16) | (g
<< 8) | b
; break;
1538 case RBG
: pixel
= (r
<< 16) | (b
<< 8) | g
; break;
1539 case BRG
: pixel
= (b
<< 16) | (r
<< 8) | g
; break;
1540 case BGR
: pixel
= (b
<< 16) | (g
<< 8) | r
; break;
1541 case GRB
: pixel
= (g
<< 16) | (r
<< 8) | b
; break;
1542 case GBR
: pixel
= (g
<< 16) | (b
<< 8) | r
; break;
1544 XPutPixel( data_image
, x
, y
, pixel
);
1554 gcvalues
.foreground
= BlackPixel( dpy
, DefaultScreen( dpy
) );
1555 GC gc
= XCreateGC( dpy
, RootWindow ( dpy
, DefaultScreen(dpy
) ), GCForeground
, &gcvalues
);
1556 XPutImage( dpy
, (Drawable
)bitmap
.GetPixmap(), gc
, data_image
, 0, 0, 0, 0, width
, height
);
1558 XDestroyImage( data_image
);
1566 GdkGC *mask_gc = gdk_gc_new( bitmap.GetMask()->GetBitmap() );
1568 gdk_draw_image( bitmap.GetMask()->GetBitmap(), mask_gc, mask_image, 0, 0, 0, 0, width, height );
1570 gdk_image_destroy( mask_image );
1571 gdk_gc_unref( mask_gc );
1578 wxImage::wxImage( const wxBitmap
&bitmap
)
1580 wxCHECK_RET( bitmap
.Ok(), wxT("invalid bitmap") );
1582 Display
*dpy
= (Display
*) wxGetDisplay();
1583 Visual
* vis
= DefaultVisual( dpy
, DefaultScreen( dpy
) );
1584 int bpp
= DefaultDepth( dpy
, DefaultScreen( dpy
) );
1586 XImage
*ximage
= XGetImage( dpy
,
1587 (Drawable
)bitmap
.GetPixmap(),
1589 bitmap
.GetWidth(), bitmap
.GetHeight(),
1590 AllPlanes
, ZPixmap
);
1592 wxCHECK_RET( ximage
, wxT("couldn't create image") );
1594 Create( bitmap
.GetWidth(), bitmap
.GetHeight() );
1595 char unsigned *data
= GetData();
1599 XDestroyImage( ximage
);
1600 wxFAIL_MSG( wxT("couldn't create image") );
1605 GdkImage *gdk_image_mask = (GdkImage*) NULL;
1606 if (bitmap.GetMask())
1608 gdk_image_mask = gdk_image_get( bitmap.GetMask()->GetBitmap(),
1610 bitmap.GetWidth(), bitmap.GetHeight() );
1612 SetMaskColour( 16, 16, 16 ); // anything unlikely and dividable
1616 // Retrieve depth info
1618 XVisualInfo vinfo_template
;
1621 vinfo_template
.visual
= vis
;
1622 vinfo_template
.visualid
= XVisualIDFromVisual( vis
);
1623 vinfo_template
.depth
= bpp
;
1626 vi
= XGetVisualInfo( dpy
, VisualIDMask
|VisualDepthMask
, &vinfo_template
, &nitem
);
1628 wxCHECK_RET( vi
, wxT("no visual") );
1630 if ((bpp
== 16) && (vi
->red_mask
!= 0xf800)) bpp
= 15;
1637 Colormap cmap
= (Colormap
)wxTheApp
->GetMainColormap( dpy
);
1639 for (int i
= 0; i
< 256; i
++) colors
[i
].pixel
= i
;
1640 XQueryColors( dpy
, cmap
, colors
, 256 );
1644 for (int j
= 0; j
< bitmap
.GetHeight(); j
++)
1646 for (int i
= 0; i
< bitmap
.GetWidth(); i
++)
1648 int pixel
= XGetPixel( ximage
, i
, j
);
1651 data
[pos
] = colors
[pixel
].red
>> 8;
1652 data
[pos
+1] = colors
[pixel
].green
>> 8;
1653 data
[pos
+2] = colors
[pixel
].blue
>> 8;
1654 } else if (bpp
== 15)
1656 data
[pos
] = (pixel
>> 7) & 0xf8;
1657 data
[pos
+1] = (pixel
>> 2) & 0xf8;
1658 data
[pos
+2] = (pixel
<< 3) & 0xf8;
1659 } else if (bpp
== 16)
1661 data
[pos
] = (pixel
>> 8) & 0xf8;
1662 data
[pos
+1] = (pixel
>> 3) & 0xfc;
1663 data
[pos
+2] = (pixel
<< 3) & 0xf8;
1666 data
[pos
] = (pixel
>> 16) & 0xff;
1667 data
[pos
+1] = (pixel
>> 8) & 0xff;
1668 data
[pos
+2] = pixel
& 0xff;
1674 int mask_pixel = gdk_image_get_pixel( gdk_image_mask, i, j );
1675 if (mask_pixel == 0)
1688 XDestroyImage( ximage
);
1690 if (gdk_image_mask) gdk_image_destroy( gdk_image_mask );
1696 // OS/2 Presentation manager conversion routings
1698 wxBitmap
wxImage::ConvertToBitmap() const
1701 return wxNullBitmap
;
1702 wxBitmap bitmap
; // remove
1705 int sizeLimit = 1024*768*3;
1707 // width and height of the device-dependent bitmap
1708 int width = GetWidth();
1709 int bmpHeight = GetHeight();
1711 // calc the number of bytes per scanline and padding
1712 int bytePerLine = width*3;
1713 int sizeDWORD = sizeof( DWORD );
1714 int lineBoundary = bytePerLine % sizeDWORD;
1716 if( lineBoundary > 0 )
1718 padding = sizeDWORD - lineBoundary;
1719 bytePerLine += padding;
1721 // calc the number of DIBs and heights of DIBs
1724 int height = sizeLimit/bytePerLine;
1725 if( height >= bmpHeight )
1729 numDIB = bmpHeight / height;
1730 hRemain = bmpHeight % height;
1731 if( hRemain >0 ) numDIB++;
1734 // set bitmap parameters
1736 wxCHECK_MSG( Ok(), bitmap, wxT("invalid image") );
1737 bitmap.SetWidth( width );
1738 bitmap.SetHeight( bmpHeight );
1739 bitmap.SetDepth( wxDisplayDepth() );
1741 // create a DIB header
1742 int headersize = sizeof(BITMAPINFOHEADER);
1743 LPBITMAPINFO lpDIBh = (BITMAPINFO *) malloc( headersize );
1744 wxCHECK_MSG( lpDIBh, bitmap, wxT("could not allocate memory for DIB header") );
1745 // Fill in the DIB header
1746 lpDIBh->bmiHeader.biSize = headersize;
1747 lpDIBh->bmiHeader.biWidth = (DWORD)width;
1748 lpDIBh->bmiHeader.biHeight = (DWORD)(-height);
1749 lpDIBh->bmiHeader.biSizeImage = bytePerLine*height;
1750 // the general formula for biSizeImage:
1751 // ( ( ( ((DWORD)width*24) +31 ) & ~31 ) >> 3 ) * height;
1752 lpDIBh->bmiHeader.biPlanes = 1;
1753 lpDIBh->bmiHeader.biBitCount = 24;
1754 lpDIBh->bmiHeader.biCompression = BI_RGB;
1755 lpDIBh->bmiHeader.biClrUsed = 0;
1756 // These seem not really needed for our purpose here.
1757 lpDIBh->bmiHeader.biClrImportant = 0;
1758 lpDIBh->bmiHeader.biXPelsPerMeter = 0;
1759 lpDIBh->bmiHeader.biYPelsPerMeter = 0;
1760 // memory for DIB data
1761 unsigned char *lpBits;
1762 lpBits = (unsigned char *)malloc( lpDIBh->bmiHeader.biSizeImage );
1765 wxFAIL_MSG( wxT("could not allocate memory for DIB") );
1770 // create and set the device-dependent bitmap
1771 HDC hdc = ::GetDC(NULL);
1772 HDC memdc = ::CreateCompatibleDC( hdc );
1774 hbitmap = ::CreateCompatibleBitmap( hdc, width, bmpHeight );
1775 ::SelectObject( memdc, hbitmap);
1777 // copy image data into DIB data and then into DDB (in a loop)
1778 unsigned char *data = GetData();
1781 unsigned char *ptdata = data;
1782 unsigned char *ptbits;
1784 for( n=0; n<numDIB; n++ )
1786 if( numDIB > 1 && n == numDIB-1 && hRemain > 0 )
1788 // redefine height and size of the (possibly) last smaller DIB
1789 // memory is not reallocated
1791 lpDIBh->bmiHeader.biHeight = (DWORD)(-height);
1792 lpDIBh->bmiHeader.biSizeImage = bytePerLine*height;
1796 for( j=0; j<height; j++ )
1798 for( i=0; i<width; i++ )
1800 *(ptbits++) = *(ptdata+2);
1801 *(ptbits++) = *(ptdata+1);
1802 *(ptbits++) = *(ptdata );
1805 for( i=0; i< padding; i++ ) *(ptbits++) = 0;
1807 ::StretchDIBits( memdc, 0, origin, width, height,\
1808 0, 0, width, height, lpBits, lpDIBh, DIB_RGB_COLORS, SRCCOPY);
1810 // if numDIB = 1, lines below can also be used
1811 // hbitmap = CreateDIBitmap( hdc, &(lpDIBh->bmiHeader), CBM_INIT, lpBits, lpDIBh, DIB_RGB_COLORS );
1812 // The above line is equivalent to the following two lines.
1813 // hbitmap = ::CreateCompatibleBitmap( hdc, width, height );
1814 // ::SetDIBits( hdc, hbitmap, 0, height, lpBits, lpDIBh, DIB_RGB_COLORS);
1815 // or the following lines
1816 // hbitmap = ::CreateCompatibleBitmap( hdc, width, height );
1817 // HDC memdc = ::CreateCompatibleDC( hdc );
1818 // ::SelectObject( memdc, hbitmap);
1819 // ::SetDIBitsToDevice( memdc, 0, 0, width, height,
1820 // 0, 0, 0, height, (void *)lpBits, lpDIBh, DIB_RGB_COLORS);
1821 // ::SelectObject( memdc, 0 );
1822 // ::DeleteDC( memdc );
1824 bitmap.SetHBITMAP( (WXHBITMAP) hbitmap );
1826 // similarly, created an mono-bitmap for the possible mask
1829 hbitmap = ::CreateBitmap( (WORD)width, (WORD)bmpHeight, 1, 1, NULL );
1830 ::SelectObject( memdc, hbitmap);
1831 if( numDIB == 1 ) height = bmpHeight;
1832 else height = sizeLimit/bytePerLine;
1833 lpDIBh->bmiHeader.biHeight = (DWORD)(-height);
1834 lpDIBh->bmiHeader.biSizeImage = bytePerLine*height;
1836 unsigned char r = GetMaskRed();
1837 unsigned char g = GetMaskGreen();
1838 unsigned char b = GetMaskBlue();
1839 unsigned char zero = 0, one = 255;
1841 for( n=0; n<numDIB; n++ )
1843 if( numDIB > 1 && n == numDIB - 1 && hRemain > 0 )
1845 // redefine height and size of the (possibly) last smaller DIB
1846 // memory is not reallocated
1848 lpDIBh->bmiHeader.biHeight = (DWORD)(-height);
1849 lpDIBh->bmiHeader.biSizeImage = bytePerLine*height;
1852 for( int j=0; j<height; j++ )
1854 for(i=0; i<width; i++ )
1856 if( (*(ptdata++)!=r) | (*(ptdata++)!=g) | (*(ptdata++)!=b) )
1869 for( i=0; i< padding; i++ ) *(ptbits++) = zero;
1871 ::StretchDIBits( memdc, 0, origin, width, height,\
1872 0, 0, width, height, lpBits, lpDIBh, DIB_RGB_COLORS, SRCCOPY);
1875 // create a wxMask object
1876 wxMask *mask = new wxMask();
1877 mask->SetMaskBitmap( (WXHBITMAP) hbitmap );
1878 bitmap.SetMask( mask );
1881 // free allocated resources
1882 ::SelectObject( memdc, 0 );
1883 ::DeleteDC( memdc );
1884 ::ReleaseDC(NULL, hdc);
1888 // check the wxBitmap object
1889 if( bitmap.GetHBITMAP() )
1890 bitmap.SetOk( TRUE );
1892 bitmap.SetOk( FALSE );
1897 wxImage::wxImage( const wxBitmap
&bitmap
)
1902 wxFAIL_MSG( wxT("invalid bitmap") );
1906 // create an wxImage object
1907 int width
= bitmap
.GetWidth();
1908 int height
= bitmap
.GetHeight();
1909 Create( width
, height
);
1910 unsigned char *data
= GetData();
1913 wxFAIL_MSG( wxT("could not allocate data for image") );
1917 // calc the number of bytes per scanline and padding in the DIB
1918 int bytePerLine
= width
*3;
1919 int sizeDWORD
= sizeof( DWORD
);
1920 int lineBoundary
= bytePerLine
% sizeDWORD
;
1922 if( lineBoundary
> 0 )
1924 padding
= sizeDWORD
- lineBoundary
;
1925 bytePerLine
+= padding
;
1929 // create a DIB header
1930 int headersize = sizeof(BITMAPINFOHEADER);
1931 LPBITMAPINFO lpDIBh = (BITMAPINFO *) malloc( headersize );
1934 wxFAIL_MSG( wxT("could not allocate data for DIB header") );
1938 // Fill in the DIB header
1939 lpDIBh->bmiHeader.biSize = headersize;
1940 lpDIBh->bmiHeader.biWidth = width;
1941 lpDIBh->bmiHeader.biHeight = -height;
1942 lpDIBh->bmiHeader.biSizeImage = bytePerLine * height;
1943 lpDIBh->bmiHeader.biPlanes = 1;
1944 lpDIBh->bmiHeader.biBitCount = 24;
1945 lpDIBh->bmiHeader.biCompression = BI_RGB;
1946 lpDIBh->bmiHeader.biClrUsed = 0;
1947 // These seem not really needed for our purpose here.
1948 lpDIBh->bmiHeader.biClrImportant = 0;
1949 lpDIBh->bmiHeader.biXPelsPerMeter = 0;
1950 lpDIBh->bmiHeader.biYPelsPerMeter = 0;
1951 // memory for DIB data
1952 unsigned char *lpBits;
1953 lpBits = (unsigned char *) malloc( lpDIBh->bmiHeader.biSizeImage );
1956 wxFAIL_MSG( wxT("could not allocate data for DIB") );
1962 // copy data from the device-dependent bitmap to the DIB
1963 HDC hdc = ::GetDC(NULL);
1965 hbitmap = (HBITMAP) bitmap.GetHBITMAP();
1966 ::GetDIBits( hdc, hbitmap, 0, height, lpBits, lpDIBh, DIB_RGB_COLORS );
1968 // copy DIB data into the wxImage object
1970 unsigned char *ptdata = data;
1971 unsigned char *ptbits = lpBits;
1972 for( i=0; i<height; i++ )
1974 for( j=0; j<width; j++ )
1976 *(ptdata++) = *(ptbits+2);
1977 *(ptdata++) = *(ptbits+1);
1978 *(ptdata++) = *(ptbits );
1984 // similarly, set data according to the possible mask bitmap
1985 if( bitmap.GetMask() && bitmap.GetMask()->GetMaskBitmap() )
1987 hbitmap = (HBITMAP) bitmap.GetMask()->GetMaskBitmap();
1988 // memory DC created, color set, data copied, and memory DC deleted
1989 HDC memdc = ::CreateCompatibleDC( hdc );
1990 ::SetTextColor( memdc, RGB( 0, 0, 0 ) );
1991 ::SetBkColor( memdc, RGB( 255, 255, 255 ) );
1992 ::GetDIBits( memdc, hbitmap, 0, height, lpBits, lpDIBh, DIB_RGB_COLORS );
1993 ::DeleteDC( memdc );
1994 // background color set to RGB(16,16,16) in consistent with wxGTK
1995 unsigned char r=16, g=16, b=16;
1998 for( i=0; i<height; i++ )
2000 for( j=0; j<width; j++ )
2014 SetMaskColour( r, g, b );
2021 // free allocated resources
2022 ::ReleaseDC(NULL, hdc);
2030 // A module to allow wxImage initialization/cleanup
2031 // without calling these functions from app.cpp or from
2032 // the user's application.
2034 class wxImageModule
: public wxModule
2036 DECLARE_DYNAMIC_CLASS(wxImageModule
)
2039 bool OnInit() { wxImage::InitStandardHandlers(); return TRUE
; };
2040 void OnExit() { wxImage::CleanUpHandlers(); };
2043 IMPLEMENT_DYNAMIC_CLASS(wxImageModule
, wxModule
)