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"
27 #include "../png/png.h"
34 #include "wx/filefn.h"
35 #include "wx/wfstream.h"
48 //-----------------------------------------------------------------------------
50 //-----------------------------------------------------------------------------
52 class wxImageRefData
: public wxObjectRefData
57 ~wxImageRefData(void);
61 unsigned char *m_data
;
63 unsigned char m_maskRed
,m_maskGreen
,m_maskBlue
;
67 wxImageRefData::wxImageRefData(void)
71 m_data
= (unsigned char*) NULL
;
79 wxImageRefData::~wxImageRefData(void)
81 if (m_data
) free( m_data
);
84 wxList
wxImage::sm_handlers
;
86 //-----------------------------------------------------------------------------
88 #define M_IMGDATA ((wxImageRefData *)m_refData)
90 #if !USE_SHARED_LIBRARIES
91 IMPLEMENT_DYNAMIC_CLASS(wxImage
, wxObject
)
98 wxImage::wxImage( int width
, int height
)
100 Create( width
, height
);
103 wxImage::wxImage( const wxString
& name
, long type
)
105 LoadFile( name
, type
);
109 wxImage::wxImage( wxInputStream
& stream
, long type
)
111 LoadFile( stream
, type
);
113 #endif // wxUSE_STREAMS
115 wxImage::wxImage( const wxImage
& image
)
120 wxImage::wxImage( const wxImage
* image
)
122 if (image
) Ref(*image
);
125 void wxImage::Create( int width
, int height
)
127 m_refData
= new wxImageRefData();
129 M_IMGDATA
->m_data
= (unsigned char *) malloc( width
*height
*3 );
130 if (M_IMGDATA
->m_data
)
132 for (int l
= 0; l
< width
*height
*3; l
++) M_IMGDATA
->m_data
[l
] = 0;
134 M_IMGDATA
->m_width
= width
;
135 M_IMGDATA
->m_height
= height
;
136 M_IMGDATA
->m_ok
= TRUE
;
144 void wxImage::Destroy()
149 wxImage
wxImage::Scale( int width
, int height
)
153 wxCHECK_MSG( Ok(), image
, "invlaid image" );
155 wxCHECK_MSG( (width
> 0) && (height
> 0), image
, "invalid image size" );
157 image
.Create( width
, height
);
159 char unsigned *data
= image
.GetData();
161 wxCHECK_MSG( data
, image
, "unable to create image" );
163 if (M_IMGDATA
->m_hasMask
)
164 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
166 long old_height
= M_IMGDATA
->m_height
;
167 long old_width
= M_IMGDATA
->m_width
;
169 char unsigned *source_data
= M_IMGDATA
->m_data
;
170 char unsigned *target_data
= data
;
172 for (long j
= 0; j
< height
; j
++)
174 long y_offset
= (j
* old_height
/ height
) * old_width
;
176 for (long i
= 0; i
< width
; i
++)
179 source_data
+ 3*(y_offset
+ ((i
* old_width
)/ width
)),
188 void wxImage::SetRGB( int x
, int y
, unsigned char r
, unsigned char g
, unsigned char b
)
190 wxCHECK_RET( Ok(), "invalid image" );
192 int w
= M_IMGDATA
->m_width
;
193 int h
= M_IMGDATA
->m_height
;
195 wxCHECK_RET( (x
>=0) && (y
>=0) && (x
<w
) && (y
<h
), "invalid image index" );
197 long pos
= (y
* w
+ x
) * 3;
199 M_IMGDATA
->m_data
[ pos
] = r
;
200 M_IMGDATA
->m_data
[ pos
+1 ] = g
;
201 M_IMGDATA
->m_data
[ pos
+2 ] = b
;
204 unsigned char wxImage::GetRed( int x
, int y
)
206 wxCHECK_MSG( Ok(), 0, "invalid image" );
208 int w
= M_IMGDATA
->m_width
;
209 int h
= M_IMGDATA
->m_height
;
211 wxCHECK_MSG( (x
>=0) && (y
>=0) && (x
<w
) && (y
<h
), 0, "invalid image index" );
213 long pos
= (y
* w
+ x
) * 3;
215 return M_IMGDATA
->m_data
[pos
];
218 unsigned char wxImage::GetGreen( int x
, int y
)
220 wxCHECK_MSG( Ok(), 0, "invalid image" );
222 int w
= M_IMGDATA
->m_width
;
223 int h
= M_IMGDATA
->m_height
;
225 wxCHECK_MSG( (x
>=0) && (y
>=0) && (x
<w
) && (y
<h
), 0, "invalid image index" );
227 long pos
= (y
* w
+ x
) * 3;
229 return M_IMGDATA
->m_data
[pos
+1];
232 unsigned char wxImage::GetBlue( int x
, int y
)
234 wxCHECK_MSG( Ok(), 0, "invalid image" );
236 int w
= M_IMGDATA
->m_width
;
237 int h
= M_IMGDATA
->m_height
;
239 wxCHECK_MSG( (x
>=0) && (y
>=0) && (x
<w
) && (y
<h
), 0, "invalid image index" );
241 long pos
= (y
* w
+ x
) * 3;
243 return M_IMGDATA
->m_data
[pos
+2];
246 bool wxImage::Ok() const
248 return (M_IMGDATA
&& M_IMGDATA
->m_ok
);
251 char unsigned *wxImage::GetData() const
253 wxCHECK_MSG( Ok(), (char unsigned *)NULL
, "invalid image" );
255 return M_IMGDATA
->m_data
;
258 void wxImage::SetData( char unsigned *WXUNUSED(data
) )
260 wxCHECK_RET( Ok(), "invalid image" );
263 void wxImage::SetMaskColour( unsigned char r
, unsigned char g
, unsigned char b
)
265 wxCHECK_RET( Ok(), "invalid image" );
267 M_IMGDATA
->m_maskRed
= r
;
268 M_IMGDATA
->m_maskGreen
= g
;
269 M_IMGDATA
->m_maskBlue
= b
;
270 M_IMGDATA
->m_hasMask
= TRUE
;
273 unsigned char wxImage::GetMaskRed() const
275 wxCHECK_MSG( Ok(), 0, "invalid image" );
277 return M_IMGDATA
->m_maskRed
;
280 unsigned char wxImage::GetMaskGreen() const
282 wxCHECK_MSG( Ok(), 0, "invalid image" );
284 return M_IMGDATA
->m_maskGreen
;
287 unsigned char wxImage::GetMaskBlue() const
289 wxCHECK_MSG( Ok(), 0, "invalid image" );
291 return M_IMGDATA
->m_maskBlue
;
294 void wxImage::SetMask( bool mask
)
296 wxCHECK_RET( Ok(), "invalid image" );
298 M_IMGDATA
->m_hasMask
= mask
;
301 bool wxImage::HasMask() const
303 wxCHECK_MSG( Ok(), FALSE
, "invalid image" );
305 return M_IMGDATA
->m_hasMask
;
308 int wxImage::GetWidth() const
310 wxCHECK_MSG( Ok(), 0, "invalid image" );
312 return M_IMGDATA
->m_width
;
315 int wxImage::GetHeight() const
317 wxCHECK_MSG( Ok(), 0, "invalid image" );
319 return M_IMGDATA
->m_height
;
322 bool wxImage::LoadFile( const wxString
& filename
, long type
)
325 if (wxFileExists(filename
))
327 wxFileInputStream
stream(filename
);
328 return LoadFile(stream
, type
);
332 wxLogError( "Can't load image from file '%s': file does not exist.", filename
.c_str() );
336 #else // !wxUSE_STREAMS
338 #endif // wxUSE_STREAMS
341 bool wxImage::SaveFile( const wxString
& filename
, int type
)
344 wxFileOutputStream
stream(filename
);
346 if ( stream
.LastError() == wxStream_NOERROR
)
347 return SaveFile(stream
, type
);
349 #endif // wxUSE_STREAMS
354 bool wxImage::LoadFile( wxInputStream
& stream
, long type
)
358 m_refData
= new wxImageRefData
;
360 wxImageHandler
*handler
= FindHandler(type
);
364 wxLogWarning( "No image handler for type %d defined.", type
);
369 return handler
->LoadFile( this, stream
);
372 bool wxImage::SaveFile( wxOutputStream
& stream
, int type
)
374 wxCHECK_MSG( Ok(), FALSE
, "invalid image" );
376 wxImageHandler
*handler
= FindHandler(type
);
380 wxLogWarning( "No image handler for type %d defined.", type
);
385 return handler
->SaveFile( this, stream
);
387 #endif // wxUSE_STREAMS
389 void wxImage::AddHandler( wxImageHandler
*handler
)
391 // make sure that the memory will be freed at the program end
392 sm_handlers
.DeleteContents(TRUE
);
394 sm_handlers
.Append( handler
);
397 void wxImage::InsertHandler( wxImageHandler
*handler
)
399 // make sure that the memory will be freed at the program end
400 sm_handlers
.DeleteContents(TRUE
);
402 sm_handlers
.Insert( handler
);
405 bool wxImage::RemoveHandler( const wxString
& name
)
407 wxImageHandler
*handler
= FindHandler(name
);
410 sm_handlers
.DeleteObject(handler
);
417 wxImageHandler
*wxImage::FindHandler( const wxString
& name
)
419 wxNode
*node
= sm_handlers
.First();
422 wxImageHandler
*handler
= (wxImageHandler
*)node
->Data();
423 if (handler
->GetName().Cmp(name
) == 0) return handler
;
427 return (wxImageHandler
*)NULL
;
430 wxImageHandler
*wxImage::FindHandler( const wxString
& extension
, long bitmapType
)
432 wxNode
*node
= sm_handlers
.First();
435 wxImageHandler
*handler
= (wxImageHandler
*)node
->Data();
436 if ( (handler
->GetExtension().Cmp(extension
) == 0) &&
437 (bitmapType
== -1 || handler
->GetType() == bitmapType
) )
441 return (wxImageHandler
*)NULL
;
444 wxImageHandler
*wxImage::FindHandler( long bitmapType
)
446 wxNode
*node
= sm_handlers
.First();
449 wxImageHandler
*handler
= (wxImageHandler
*)node
->Data();
450 if (handler
->GetType() == bitmapType
) return handler
;
456 void wxImage::InitStandardHandlers()
458 AddHandler( new wxBMPHandler
);
460 AddHandler( new wxPNGHandler
);
464 void wxImage::CleanUpHandlers()
466 wxNode
*node
= sm_handlers
.First();
469 wxImageHandler
*handler
= (wxImageHandler
*)node
->Data();
470 wxNode
*next
= node
->Next();
477 //-----------------------------------------------------------------------------
479 //-----------------------------------------------------------------------------
481 #if !USE_SHARED_LIBRARIES
482 IMPLEMENT_DYNAMIC_CLASS(wxImageHandler
,wxObject
)
486 bool wxImageHandler::LoadFile( wxImage
*WXUNUSED(image
), wxInputStream
& WXUNUSED(stream
) )
491 bool wxImageHandler::SaveFile( wxImage
*WXUNUSED(image
), wxOutputStream
& WXUNUSED(stream
) )
495 #endif // wxUSE_STREAMS
497 //-----------------------------------------------------------------------------
499 //-----------------------------------------------------------------------------
503 #if !USE_SHARED_LIBRARIES
504 IMPLEMENT_DYNAMIC_CLASS(wxPNGHandler
,wxImageHandler
)
509 static void _PNG_stream_reader( png_structp png_ptr
, png_bytep data
, png_size_t length
)
511 ((wxInputStream
*) png_get_io_ptr( png_ptr
)) -> Read(data
, length
);
514 static void _PNG_stream_writer( png_structp png_ptr
, png_bytep data
, png_size_t length
)
516 ((wxOutputStream
*) png_get_io_ptr( png_ptr
)) -> Write(data
, length
);
519 bool wxPNGHandler::LoadFile( wxImage
*image
, wxInputStream
& stream
)
521 // VZ: as this function uses setjmp() the only fool proof error handling
522 // method is to use goto (setjmp is not really C++ dtors friendly...)
524 unsigned char **lines
= (unsigned char **) NULL
;
526 png_infop info_ptr
= (png_infop
) NULL
;
530 png_structp png_ptr
= png_create_read_struct( PNG_LIBPNG_VER_STRING
,
532 (png_error_ptr
) NULL
,
533 (png_error_ptr
) NULL
);
537 info_ptr
= png_create_info_struct( png_ptr
);
541 if (setjmp(png_ptr
->jmpbuf
))
544 if (info_ptr
->color_type
== PNG_COLOR_TYPE_RGB_ALPHA
)
547 png_set_read_fn( png_ptr
, &stream
, _PNG_stream_reader
);
549 png_uint_32 width
,height
;
550 int bit_depth
,color_type
,interlace_type
;
552 png_read_info( png_ptr
, info_ptr
);
553 png_get_IHDR( png_ptr
, info_ptr
, &width
, &height
, &bit_depth
, &color_type
, &interlace_type
, (int*) NULL
, (int*) NULL
);
555 if (color_type
== PNG_COLOR_TYPE_PALETTE
)
556 png_set_expand( png_ptr
);
558 png_set_strip_16( png_ptr
);
559 png_set_packing( png_ptr
);
560 if (png_get_valid( png_ptr
, info_ptr
, PNG_INFO_tRNS
))
561 png_set_expand( png_ptr
);
562 png_set_filler( png_ptr
, 0xff, PNG_FILLER_AFTER
);
564 image
->Create( width
, height
);
569 lines
= (unsigned char **)malloc( height
* sizeof(unsigned char *) );
573 for (i
= 0; i
< height
; i
++)
575 if ((lines
[i
] = (unsigned char *)malloc(width
* (sizeof(unsigned char) * 4))) == NULL
)
577 for ( unsigned int n
= 0; n
< i
; n
++ )
583 // loaded successfully!
586 png_read_image( png_ptr
, lines
);
587 png_destroy_read_struct( &png_ptr
, &info_ptr
, (png_infopp
) NULL
);
588 unsigned char *ptr
= image
->GetData();
589 if ((color_type
== PNG_COLOR_TYPE_GRAY
) ||
590 (color_type
== PNG_COLOR_TYPE_GRAY_ALPHA
))
592 for (unsigned int y
= 0; y
< height
; y
++)
594 unsigned char *ptr2
= lines
[y
];
595 for (unsigned int x
= 0; x
< width
; x
++)
597 unsigned char r
= *ptr2
++;
598 unsigned char a
= *ptr2
++;
617 for (unsigned int y
= 0; y
< height
; y
++)
619 unsigned char *ptr2
= lines
[y
];
620 for (unsigned int x
= 0; x
< width
; x
++)
622 unsigned char r
= *ptr2
++;
623 unsigned char g
= *ptr2
++;
624 unsigned char b
= *ptr2
++;
625 unsigned char a
= *ptr2
++;
635 if ((r
== 255) && (g
== 0) && (b
== 255)) r
= 254;
644 for ( unsigned int j
= 0; j
< height
; j
++ )
650 image
->SetMaskColour( 255, 0, 255 );
654 image
->SetMask( FALSE
);
661 wxLogError(_("Couldn't load a PNG image - probably file is corrupted."));
677 png_destroy_read_struct( &png_ptr
, &info_ptr
, (png_infopp
) NULL
);
681 png_destroy_read_struct( &png_ptr
, (png_infopp
) NULL
, (png_infopp
) NULL
);
687 bool wxPNGHandler::SaveFile( wxImage
*image
, wxOutputStream
& stream
)
690 png_structp png_ptr
= png_create_write_struct( PNG_LIBPNG_VER_STRING
, NULL
, NULL
, NULL
);
696 png_infop info_ptr
= png_create_info_struct(png_ptr
);
697 if (info_ptr
== NULL
)
699 png_destroy_write_struct( &png_ptr
, (png_infopp
)NULL
);
703 if (setjmp(png_ptr
->jmpbuf
))
705 png_destroy_write_struct( &png_ptr
, (png_infopp
)NULL
);
709 png_set_write_fn( png_ptr
, &stream
, _PNG_stream_writer
, NULL
);
711 png_set_IHDR( png_ptr
, info_ptr
, image
->GetWidth(), image
->GetHeight(), 8,
712 PNG_COLOR_TYPE_RGB_ALPHA
, PNG_INTERLACE_NONE
,
713 PNG_COMPRESSION_TYPE_BASE
, PNG_FILTER_TYPE_BASE
);
720 png_set_sBIT( png_ptr
, info_ptr
, &sig_bit
);
721 png_write_info( png_ptr
, info_ptr
);
722 png_set_shift( png_ptr
, &sig_bit
);
723 png_set_packing( png_ptr
);
725 unsigned char *data
= (unsigned char *)malloc( image
->GetWidth()*4 );
728 png_destroy_write_struct( &png_ptr
, (png_infopp
)NULL
);
732 for (int y
= 0; y
< image
->GetHeight(); y
++)
734 unsigned char *ptr
= image
->GetData() + (y
* image
->GetWidth() * 3);
735 for (int x
= 0; x
< image
->GetWidth(); x
++)
737 data
[(x
<< 2) + 0] = *ptr
++;
738 data
[(x
<< 2) + 1] = *ptr
++;
739 data
[(x
<< 2) + 2] = *ptr
++;
740 if ((data
[(x
<< 2) + 0] == image
->GetMaskRed()) &&
741 (data
[(x
<< 2) + 1] == image
->GetMaskGreen()) &&
742 (data
[(x
<< 2) + 2] == image
->GetMaskBlue()))
744 data
[(x
<< 2) + 3] = 0;
748 data
[(x
<< 2) + 3] = 255;
751 png_bytep row_ptr
= data
;
752 png_write_rows( png_ptr
, &row_ptr
, 1 );
756 png_write_end( png_ptr
, info_ptr
);
757 png_destroy_write_struct( &png_ptr
, (png_infopp
)&info_ptr
);
761 #endif // wxUSE_STREAMS
768 //-----------------------------------------------------------------------------
770 //-----------------------------------------------------------------------------
774 #if !USE_SHARED_LIBRARIES
775 IMPLEMENT_DYNAMIC_CLASS(wxJPEGHandler
,wxImageHandler
)
781 //------------- JPEG Data Source Manager
784 struct jpeg_source_mgr pub
; /* public fields */
786 JOCTET
* buffer
; /* start of buffer */
789 typedef my_source_mgr
* my_src_ptr
;
791 METHODDEF(void) my_init_source ( j_decompress_ptr cinfo
)
795 METHODDEF(boolean
) my_fill_input_buffer ( j_decompress_ptr cinfo
)
800 METHODDEF(void) my_skip_input_data ( j_decompress_ptr cinfo
, long num_bytes
)
802 my_src_ptr src
= (my_src_ptr
) cinfo
->src
;
804 src
->pub
.next_input_byte
+= (size_t) num_bytes
;
805 src
->pub
.bytes_in_buffer
-= (size_t) num_bytes
;
808 METHODDEF(void) my_term_source ( j_decompress_ptr cinfo
)
810 my_src_ptr src
= (my_src_ptr
) cinfo
->src
;
815 void jpeg_wxio_src( j_decompress_ptr cinfo
, wxInputStream
& infile
)
819 if (cinfo
->src
== NULL
) { /* first time for this JPEG object? */
820 cinfo
->src
= (struct jpeg_source_mgr
*)
821 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
) cinfo
, JPOOL_PERMANENT
,
822 sizeof(my_source_mgr
));
823 src
= (my_src_ptr
) cinfo
->src
;
825 src
= (my_src_ptr
) cinfo
->src
;
826 src
->pub
.bytes_in_buffer
= infile
.StreamSize(); /* forces fill_input_buffer on first read */
827 src
->buffer
= (JOCTET
*) malloc (infile
.StreamSize());
828 src
->pub
.next_input_byte
= src
->buffer
; /* until buffer loaded */
829 infile
.Read(src
->buffer
, infile
.StreamSize());
831 src
->pub
.init_source
= my_init_source
;
832 src
->pub
.fill_input_buffer
= my_fill_input_buffer
;
833 src
->pub
.skip_input_data
= my_skip_input_data
;
834 src
->pub
.resync_to_restart
= jpeg_resync_to_restart
; /* use default method */
835 src
->pub
.term_source
= my_term_source
;
840 bool wxJPEGHandler::LoadFile( wxImage
*image
, wxInputStream
& stream
)
842 struct jpeg_decompress_struct cinfo
;
843 struct jpeg_error_mgr jerr
;
849 cinfo
.err
= jpeg_std_error( &jerr
);
850 jpeg_create_decompress( &cinfo
);
851 jpeg_wxio_src( &cinfo
, stream
);
852 jpeg_read_header( &cinfo
, TRUE
);
853 cinfo
.out_color_space
= JCS_RGB
;
854 jpeg_start_decompress( &cinfo
);
856 image
->Create( cinfo
.image_width
, cinfo
.image_height
);
858 jpeg_finish_decompress( &cinfo
);
859 jpeg_destroy_decompress( &cinfo
);
862 image
->SetMask( FALSE
);
863 ptr
= image
->GetData();
864 stride
= cinfo
.output_width
* 3;
865 tempbuf
= (*cinfo
.mem
->alloc_sarray
)
866 ((j_common_ptr
) &cinfo
, JPOOL_IMAGE
, stride
, 1 );
868 while ( cinfo
.output_scanline
< cinfo
.output_height
) {
869 jpeg_read_scanlines( &cinfo
, tempbuf
, 1 );
870 memcpy( ptr
, tempbuf
[0], stride
);
873 jpeg_finish_decompress( &cinfo
);
874 jpeg_destroy_decompress( &cinfo
);
883 struct jpeg_destination_mgr pub
;
885 wxOutputStream
*stream
;
887 } my_destination_mgr
;
889 typedef my_destination_mgr
* my_dest_ptr
;
891 #define OUTPUT_BUF_SIZE 4096 /* choose an efficiently fwrite'able size */
893 METHODDEF(void) init_destination (j_compress_ptr cinfo
)
895 my_dest_ptr dest
= (my_dest_ptr
) cinfo
->dest
;
897 /* Allocate the output buffer --- it will be released when done with image */
898 dest
->buffer
= (JOCTET
*)
899 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
) cinfo
, JPOOL_IMAGE
,
900 OUTPUT_BUF_SIZE
* sizeof(JOCTET
));
901 dest
->pub
.next_output_byte
= dest
->buffer
;
902 dest
->pub
.free_in_buffer
= OUTPUT_BUF_SIZE
;
905 METHODDEF(boolean
) empty_output_buffer (j_compress_ptr cinfo
)
907 my_dest_ptr dest
= (my_dest_ptr
) cinfo
->dest
;
909 dest
->stream
->Write(dest
->buffer
, OUTPUT_BUF_SIZE
);
910 dest
->pub
.next_output_byte
= dest
->buffer
;
911 dest
->pub
.free_in_buffer
= OUTPUT_BUF_SIZE
;
915 METHODDEF(void) term_destination (j_compress_ptr cinfo
)
917 my_dest_ptr dest
= (my_dest_ptr
) cinfo
->dest
;
918 size_t datacount
= OUTPUT_BUF_SIZE
- dest
->pub
.free_in_buffer
;
919 /* Write any data remaining in the buffer */
921 dest
->stream
->Write(dest
->buffer
, datacount
);
924 GLOBAL(void) jpeg_wxio_dest (j_compress_ptr cinfo
, wxOutputStream
& outfile
)
928 if (cinfo
->dest
== NULL
) { /* first time for this JPEG object? */
929 cinfo
->dest
= (struct jpeg_destination_mgr
*)
930 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
) cinfo
, JPOOL_PERMANENT
,
931 sizeof(my_destination_mgr
));
934 dest
= (my_dest_ptr
) cinfo
->dest
;
935 dest
->pub
.init_destination
= init_destination
;
936 dest
->pub
.empty_output_buffer
= empty_output_buffer
;
937 dest
->pub
.term_destination
= term_destination
;
938 dest
->stream
= &outfile
;
943 bool wxJPEGHandler::SaveFile( wxImage
*image
, wxOutputStream
& stream
)
945 struct jpeg_compress_struct cinfo
;
946 struct jpeg_error_mgr jerr
;
947 JSAMPROW row_pointer
[1]; /* pointer to JSAMPLE row[s] */
948 JSAMPLE
*image_buffer
;
949 int stride
; /* physical row width in image buffer */
951 cinfo
.err
= jpeg_std_error(&jerr
);
952 jpeg_create_compress(&cinfo
);
953 jpeg_wxio_dest(&cinfo
, stream
);
955 cinfo
.image_width
= image
->GetWidth();
956 cinfo
.image_height
= image
->GetHeight();
957 cinfo
.input_components
= 3;
958 cinfo
.in_color_space
= JCS_RGB
;
959 jpeg_set_defaults(&cinfo
);
960 jpeg_start_compress(&cinfo
, TRUE
);
962 stride
= cinfo
.image_width
* 3; /* JSAMPLEs per row in image_buffer */
963 image_buffer
= image
->GetData();
964 while (cinfo
.next_scanline
< cinfo
.image_height
) {
965 row_pointer
[0] = &image_buffer
[cinfo
.next_scanline
* stride
];
966 jpeg_write_scanlines( &cinfo
, row_pointer
, 1 );
968 jpeg_finish_compress(&cinfo
);
969 jpeg_destroy_compress(&cinfo
);
973 #endif // wxUSE_STREAMS
981 //-----------------------------------------------------------------------------
983 //-----------------------------------------------------------------------------
985 #if !USE_SHARED_LIBRARIES
986 IMPLEMENT_DYNAMIC_CLASS(wxBMPHandler
,wxImageHandler
)
990 bool wxBMPHandler::LoadFile( wxImage
*image
, wxInputStream
& stream
)
992 unsigned char *data
, *ptr
;
993 int done
, i
, bpp
, planes
, comp
, ncolors
, line
, column
,
994 linesize
, linepos
, rshift
= 0, gshift
= 0, bshift
= 0;
997 long int dbuf
[4], dword
, rmask
= 0, gmask
= 0, bmask
= 0, offset
,
999 off_t start_offset
= stream
.TellI();
1000 signed char bbuf
[4];
1003 unsigned char r
, g
, b
;
1012 #ifndef BI_BITFIELDS
1013 #define BI_BITFIELDS 3
1020 * Reading the bmp header
1023 stream
.Read(&bbuf
, 2);
1025 stream
.Read(dbuf
, 4 * 4);
1030 stream
.Read(dbuf
, 4 * 2);
1031 int width
= (int)dbuf
[0];
1032 int height
= (int)dbuf
[1];
1035 wxLogError( "Image width > 32767 pixels for file\n" );
1040 wxLogError( "Image height > 32767 pixels for file\n" );
1043 stream
.Read(&word
, 2);
1045 stream
.Read(&word
, 2);
1047 if (bpp
!= 1 && bpp
!= 4 && bpp
!= 8 && bpp
&& 16 && bpp
!= 24 && bpp
!= 32)
1049 wxLogError( "unknown bitdepth in file\n" );
1052 stream
.Read(dbuf
, 4 * 4);
1053 comp
= (int)dbuf
[0];
1054 if (comp
!= BI_RGB
&& comp
!= BI_RLE4
&& comp
!= BI_RLE8
&& comp
!= BI_BITFIELDS
)
1056 wxLogError( "unknown encoding in Windows BMP file\n" );
1059 stream
.Read(dbuf
, 4 * 2);
1060 ncolors
= (int)dbuf
[0];
1063 /* some more sanity checks */
1064 if (((comp
== BI_RLE4
) && (bpp
!= 4)) || ((comp
== BI_RLE8
) && (bpp
!= 8)) || ((comp
== BI_BITFIELDS
) && (bpp
!= 16 && bpp
!= 32)))
1066 wxLogError( "encoding of BMP doesn't match bitdepth\n" );
1071 cmap
= (struct _cmap
*)malloc(sizeof(struct _cmap
) * ncolors
);
1075 wxLogError( "Cannot allocate RAM for color map in BMP file\n" );
1082 image
->Create( width
, height
);
1083 ptr
= image
->GetData();
1086 wxLogError( "Cannot allocate RAM for RGB data in file\n" );
1093 * Reading the palette, if it exists.
1095 if (bpp
< 16 && ncolors
!= 0)
1097 for (i
= 0; i
< ncolors
; i
++)
1099 stream
.Read(bbuf
, 4);
1100 cmap
[i
].b
= bbuf
[0];
1101 cmap
[i
].g
= bbuf
[1];
1102 cmap
[i
].r
= bbuf
[2];
1105 else if (bpp
== 16 || bpp
== 32)
1107 if (comp
== BI_BITFIELDS
)
1111 stream
.Read(dbuf
, 4 * 3);
1115 /* find shift amount.. ugly, but i can't think of a better way */
1116 for (bit
= 0; bit
< bpp
; bit
++)
1118 if (bmask
& (1 << bit
))
1120 if (gmask
& (1 << bit
))
1122 if (rmask
& (1 << bit
))
1147 * Reading the image data
1149 stream
.SeekI(start_offset
+ offset
);
1152 /* set the whole image to the background color */
1153 if (bpp
< 16 && (comp
== BI_RLE4
|| comp
== BI_RLE8
))
1155 for (i
= 0; i
< width
* height
; i
++)
1165 #define poffset (line * width * 3 + column * 3)
1168 * BMPs are stored upside down... hmmmmmmmmmm....
1171 linesize
= ((width
* bpp
+ 31) / 32) * 4;
1172 for (line
= (height
- 1); line
>= 0; line
--)
1175 for (column
= 0; column
< width
;)
1182 aByte
= stream
.GetC();
1187 for (bit
= 0; bit
< 8; bit
++)
1189 index
= ((aByte
& (0x80 >> bit
)) ? 1 : 0);
1190 ptr
[poffset
] = cmap
[index
].r
;
1191 ptr
[poffset
+ 1] = cmap
[index
].g
;
1192 ptr
[poffset
+ 2] = cmap
[index
].b
;
1198 if (comp
== BI_RLE4
)
1200 wxLogError( "can't deal with 4bit encoded yet.\n");
1209 for (nibble
= 0; nibble
< 2; nibble
++)
1211 index
= ((aByte
& (0xF0 >> nibble
* 4)) >> (!nibble
* 4));
1214 ptr
[poffset
] = cmap
[index
].r
;
1215 ptr
[poffset
+ 1] = cmap
[index
].g
;
1216 ptr
[poffset
+ 2] = cmap
[index
].b
;
1223 if (comp
== BI_RLE8
)
1225 unsigned char first
;
1228 aByte
= stream
.GetC();
1233 /* column = width; */
1235 else if (aByte
== 1)
1240 else if (aByte
== 2)
1242 aByte
= stream
.GetC();
1244 linepos
= column
* bpp
/ 8;
1245 aByte
= stream
.GetC();
1250 int absolute
= aByte
;
1252 for (i
= 0; i
< absolute
; i
++)
1255 aByte
= stream
.GetC();
1256 ptr
[poffset
] = cmap
[aByte
].r
;
1257 ptr
[poffset
+ 1] = cmap
[aByte
].g
;
1258 ptr
[poffset
+ 2] = cmap
[aByte
].b
;
1261 if (absolute
& 0x01)
1262 aByte
= stream
.GetC();
1267 for (i
= 0; i
< first
; i
++)
1269 ptr
[poffset
] = cmap
[aByte
].r
;
1270 ptr
[poffset
+ 1] = cmap
[aByte
].g
;
1271 ptr
[poffset
+ 2] = cmap
[aByte
].b
;
1279 ptr
[poffset
] = cmap
[aByte
].r
;
1280 ptr
[poffset
+ 1] = cmap
[aByte
].g
;
1281 ptr
[poffset
+ 2] = cmap
[aByte
].b
;
1289 stream
.Read(&bbuf
, 3);
1291 ptr
[poffset
] = (unsigned char)bbuf
[2];
1292 ptr
[poffset
+ 1] = (unsigned char)bbuf
[1];
1293 ptr
[poffset
+ 2] = (unsigned char)bbuf
[0];
1300 stream
.Read(&word
, 2);
1302 temp
= (word
& rmask
) >> rshift
;
1303 ptr
[poffset
] = temp
;
1304 temp
= (word
& gmask
) >> gshift
;
1305 ptr
[poffset
+ 1] = temp
;
1306 temp
= (word
& bmask
) >> gshift
;
1307 ptr
[poffset
+ 2] = temp
;
1314 stream
.Read(&dword
, 4);
1316 temp
= (dword
& rmask
) >> rshift
;
1317 ptr
[poffset
] = temp
;
1318 temp
= (dword
& gmask
) >> gshift
;
1319 ptr
[poffset
+ 1] = temp
;
1320 temp
= (dword
& bmask
) >> bshift
;
1321 ptr
[poffset
+ 2] = temp
;
1325 while ((linepos
< linesize
) && (comp
!= 1) && (comp
!= 2))
1327 stream
.Read(&aByte
, 1);
1329 if (stream
.LastError() != wxStream_NOERROR
)
1333 if (cmap
) free(cmap
);
1335 image
->SetMask( FALSE
);
1339 #endif // wxUSE_STREAMS
1343 wxBitmap
wxImage::ConvertToBitmap() const
1345 // sizeLimit is the MS upper limit for the DIB size
1346 int sizeLimit
= 1024*768*3;
1348 // width and height of the device-dependent bitmap
1349 int width
= GetWidth();
1350 int bmpHeight
= GetHeight();
1352 // calc the number of bytes per scanline and padding
1353 int bytePerLine
= width
*3;
1354 int sizeDWORD
= sizeof( DWORD
);
1355 div_t lineBoundary
= div( bytePerLine
, sizeDWORD
);
1357 if( lineBoundary
.rem
> 0 )
1359 padding
= sizeDWORD
- lineBoundary
.rem
;
1360 bytePerLine
+= padding
;
1362 // calc the number of DIBs and heights of DIBs
1365 int height
= sizeLimit
/bytePerLine
;
1366 if( height
>= bmpHeight
)
1370 div_t result
= div( bmpHeight
, height
);
1371 numDIB
= result
.quot
;
1372 hRemain
= result
.rem
;
1373 if( hRemain
>0 ) numDIB
++;
1376 // set bitmap parameters
1378 wxCHECK_MSG( Ok(), bitmap
, "invalid image" );
1379 bitmap
.SetWidth( width
);
1380 bitmap
.SetHeight( bmpHeight
);
1381 bitmap
.SetDepth( wxDisplayDepth() );
1383 // create a DIB header
1384 int headersize
= sizeof(BITMAPINFOHEADER
);
1385 LPBITMAPINFO lpDIBh
= (BITMAPINFO
*) malloc( headersize
);
1386 wxCHECK_MSG( lpDIBh
, bitmap
, "could not allocate memory for DIB header" );
1387 // Fill in the DIB header
1388 lpDIBh
->bmiHeader
.biSize
= headersize
;
1389 lpDIBh
->bmiHeader
.biWidth
= (DWORD
)width
;
1390 lpDIBh
->bmiHeader
.biHeight
= (DWORD
)(-height
);
1391 lpDIBh
->bmiHeader
.biSizeImage
= bytePerLine
*height
;
1392 // the general formula for biSizeImage:
1393 // ( ( ( ((DWORD)width*24) +31 ) & ~31 ) >> 3 ) * height;
1394 lpDIBh
->bmiHeader
.biPlanes
= 1;
1395 lpDIBh
->bmiHeader
.biBitCount
= 24;
1396 lpDIBh
->bmiHeader
.biCompression
= BI_RGB
;
1397 lpDIBh
->bmiHeader
.biClrUsed
= 0;
1398 // These seem not really needed for our purpose here.
1399 lpDIBh
->bmiHeader
.biClrImportant
= 0;
1400 lpDIBh
->bmiHeader
.biXPelsPerMeter
= 0;
1401 lpDIBh
->bmiHeader
.biYPelsPerMeter
= 0;
1402 // memory for DIB data
1403 unsigned char *lpBits
;
1404 lpBits
= (unsigned char *)malloc( lpDIBh
->bmiHeader
.biSizeImage
);
1407 wxFAIL_MSG( "could not allocate memory for DIB" );
1412 // create and set the device-dependent bitmap
1413 HDC hdc
= ::GetDC(NULL
);
1414 HDC memdc
= ::CreateCompatibleDC( hdc
);
1416 hbitmap
= ::CreateCompatibleBitmap( hdc
, width
, bmpHeight
);
1417 ::SelectObject( memdc
, hbitmap
);
1419 // copy image data into DIB data and then into DDB (in a loop)
1420 unsigned char *data
= GetData();
1423 unsigned char *ptdata
= data
;
1424 unsigned char *ptbits
;
1426 for( n
=0; n
<numDIB
; n
++ )
1428 if( numDIB
> 1 && n
== numDIB
-1 && hRemain
> 0 )
1430 // redefine height and size of the (possibly) last smaller DIB
1431 // memory is not reallocated
1433 lpDIBh
->bmiHeader
.biHeight
= (DWORD
)(-height
);
1434 lpDIBh
->bmiHeader
.biSizeImage
= bytePerLine
*height
;
1438 for( j
=0; j
<height
; j
++ )
1440 for( i
=0; i
<width
; i
++ )
1442 *(ptbits
++) = *(ptdata
+2);
1443 *(ptbits
++) = *(ptdata
+1);
1444 *(ptbits
++) = *(ptdata
);
1447 for( i
=0; i
< padding
; i
++ ) *(ptbits
++) = 0;
1449 ::StretchDIBits( memdc
, 0, origin
, width
, height
,\
1450 0, 0, width
, height
, lpBits
, lpDIBh
, DIB_RGB_COLORS
, SRCCOPY
);
1452 // if numDIB = 1, lines below can also be used
1453 // hbitmap = CreateDIBitmap( hdc, &(lpDIBh->bmiHeader), CBM_INIT, lpBits, lpDIBh, DIB_RGB_COLORS );
1454 // The above line is equivalent to the following two lines.
1455 // hbitmap = ::CreateCompatibleBitmap( hdc, width, height );
1456 // ::SetDIBits( hdc, hbitmap, 0, height, lpBits, lpDIBh, DIB_RGB_COLORS);
1457 // or the following lines
1458 // hbitmap = ::CreateCompatibleBitmap( hdc, width, height );
1459 // HDC memdc = ::CreateCompatibleDC( hdc );
1460 // ::SelectObject( memdc, hbitmap);
1461 // ::SetDIBitsToDevice( memdc, 0, 0, width, height,
1462 // 0, 0, 0, height, (void *)lpBits, lpDIBh, DIB_RGB_COLORS);
1463 // ::SelectObject( memdc, 0 );
1464 // ::DeleteDC( memdc );
1466 bitmap
.SetHBITMAP( (WXHBITMAP
) hbitmap
);
1468 // similarly, created an mono-bitmap for the possible mask
1471 hbitmap
= ::CreateBitmap( (WORD
)width
, (WORD
)bmpHeight
, 1, 1, NULL
);
1472 ::SelectObject( memdc
, hbitmap
);
1473 if( numDIB
== 1 ) height
= bmpHeight
;
1474 else height
= sizeLimit
/bytePerLine
;
1475 lpDIBh
->bmiHeader
.biHeight
= (DWORD
)(-height
);
1476 lpDIBh
->bmiHeader
.biSizeImage
= bytePerLine
*height
;
1478 unsigned char r
= GetMaskRed();
1479 unsigned char g
= GetMaskGreen();
1480 unsigned char b
= GetMaskBlue();
1481 unsigned char zero
= 0, one
= 255;
1483 for( n
=0; n
<numDIB
; n
++ )
1485 if( numDIB
> 1 && n
== numDIB
- 1 && hRemain
> 0 )
1487 // redefine height and size of the (possibly) last smaller DIB
1488 // memory is not reallocated
1490 lpDIBh
->bmiHeader
.biHeight
= (DWORD
)(-height
);
1491 lpDIBh
->bmiHeader
.biSizeImage
= bytePerLine
*height
;
1494 for( int j
=0; j
<height
; j
++ )
1496 for( int i
=0; i
<width
; i
++ )
1498 if( (*(ptdata
++)!=r
) | (*(ptdata
++)!=g
) | (*(ptdata
++)!=b
) )
1511 for( i
=0; i
< padding
; i
++ ) *(ptbits
++) = zero
;
1513 ::StretchDIBits( memdc
, 0, origin
, width
, height
,\
1514 0, 0, width
, height
, lpBits
, lpDIBh
, DIB_RGB_COLORS
, SRCCOPY
);
1517 // create a wxMask object
1518 wxMask
*mask
= new wxMask();
1519 mask
->SetMaskBitmap( (WXHBITMAP
) hbitmap
);
1520 bitmap
.SetMask( mask
);
1521 // It will be deleted when the wxBitmap object is deleted (as of 01/1999)
1522 /* The following can also be used but is slow to run
1523 wxColour colour( GetMaskRed(), GetMaskGreen(), GetMaskBlue());
1524 wxMask *mask = new wxMask( bitmap, colour );
1525 bitmap.SetMask( mask );
1529 // free allocated resources
1530 ::SelectObject( memdc
, 0 );
1531 ::DeleteDC( memdc
);
1532 ::ReleaseDC(NULL
, hdc
);
1536 // check the wxBitmap object
1537 if( bitmap
.GetHBITMAP() )
1538 bitmap
.SetOk( TRUE
);
1540 bitmap
.SetOk( FALSE
);
1545 wxImage::wxImage( const wxBitmap
&bitmap
)
1550 wxFAIL_MSG( "invalid bitmap" );
1554 // create an wxImage object
1555 int width
= bitmap
.GetWidth();
1556 int height
= bitmap
.GetHeight();
1557 Create( width
, height
);
1558 unsigned char *data
= GetData();
1561 wxFAIL_MSG( "could not allocate data for image" );
1565 // calc the number of bytes per scanline and padding in the DIB
1566 int bytePerLine
= width
*3;
1567 int sizeDWORD
= sizeof( DWORD
);
1568 div_t lineBoundary
= div( bytePerLine
, sizeDWORD
);
1570 if( lineBoundary
.rem
> 0 )
1572 padding
= sizeDWORD
- lineBoundary
.rem
;
1573 bytePerLine
+= padding
;
1576 // create a DIB header
1577 int headersize
= sizeof(BITMAPINFOHEADER
);
1578 LPBITMAPINFO lpDIBh
= (BITMAPINFO
*) malloc( headersize
);
1581 wxFAIL_MSG( "could not allocate data for DIB header" );
1585 // Fill in the DIB header
1586 lpDIBh
->bmiHeader
.biSize
= headersize
;
1587 lpDIBh
->bmiHeader
.biWidth
= width
;
1588 lpDIBh
->bmiHeader
.biHeight
= -height
;
1589 lpDIBh
->bmiHeader
.biSizeImage
= bytePerLine
* height
;
1590 lpDIBh
->bmiHeader
.biPlanes
= 1;
1591 lpDIBh
->bmiHeader
.biBitCount
= 24;
1592 lpDIBh
->bmiHeader
.biCompression
= BI_RGB
;
1593 lpDIBh
->bmiHeader
.biClrUsed
= 0;
1594 // These seem not really needed for our purpose here.
1595 lpDIBh
->bmiHeader
.biClrImportant
= 0;
1596 lpDIBh
->bmiHeader
.biXPelsPerMeter
= 0;
1597 lpDIBh
->bmiHeader
.biYPelsPerMeter
= 0;
1598 // memory for DIB data
1599 unsigned char *lpBits
;
1600 lpBits
= (unsigned char *) malloc( lpDIBh
->bmiHeader
.biSizeImage
);
1603 wxFAIL_MSG( "could not allocate data for DIB" );
1609 // copy data from the device-dependent bitmap to the DIB
1610 HDC hdc
= ::GetDC(NULL
);
1612 hbitmap
= (HBITMAP
) bitmap
.GetHBITMAP();
1613 ::GetDIBits( hdc
, hbitmap
, 0, height
, lpBits
, lpDIBh
, DIB_RGB_COLORS
);
1615 // copy DIB data into the wxImage object
1617 unsigned char *ptdata
= data
;
1618 unsigned char *ptbits
= lpBits
;
1619 for( i
=0; i
<height
; i
++ )
1621 for( j
=0; j
<width
; j
++ )
1623 *(ptdata
++) = *(ptbits
+2);
1624 *(ptdata
++) = *(ptbits
+1);
1625 *(ptdata
++) = *(ptbits
);
1631 // similarly, set data according to the possible mask bitmap
1632 if( bitmap
.GetMask() && bitmap
.GetMask()->GetMaskBitmap() )
1634 hbitmap
= (HBITMAP
) bitmap
.GetMask()->GetMaskBitmap();
1635 // memory DC created, color set, data copied, and memory DC deleted
1636 HDC memdc
= ::CreateCompatibleDC( hdc
);
1637 ::SetTextColor( memdc
, RGB( 0, 0, 0 ) );
1638 ::SetBkColor( memdc
, RGB( 255, 255, 255 ) );
1639 ::GetDIBits( memdc
, hbitmap
, 0, height
, lpBits
, lpDIBh
, DIB_RGB_COLORS
);
1640 ::DeleteDC( memdc
);
1641 // background color set to RGB(16,16,16) in consistent with wxGTK
1642 unsigned char r
=16, g
=16, b
=16;
1645 for( i
=0; i
<height
; i
++ )
1647 for( j
=0; j
<width
; j
++ )
1661 SetMaskColour( r
, g
, b
);
1668 // free allocated resources
1669 ::ReleaseDC(NULL
, hdc
);
1678 #include "gtk/gtk.h"
1679 #include "gdk/gdk.h"
1680 #include "gdk/gdkx.h"
1682 wxBitmap
wxImage::ConvertToBitmap() const
1686 wxCHECK_MSG( Ok(), bitmap
, "invalid image" );
1688 int width
= GetWidth();
1689 int height
= GetHeight();
1691 bitmap
.SetHeight( height
);
1692 bitmap
.SetWidth( width
);
1696 GdkImage
*data_image
=
1697 gdk_image_new( GDK_IMAGE_FASTEST
, gdk_visual_get_system(), width
, height
);
1699 bitmap
.SetPixmap( gdk_pixmap_new( (GdkWindow
*)&gdk_root_parent
, width
, height
, -1 ) );
1703 GdkImage
*mask_image
= (GdkImage
*) NULL
;
1707 unsigned char *mask_data
= (unsigned char*)malloc( ((width
>> 3)+8) * height
);
1709 mask_image
= gdk_image_new_bitmap( gdk_visual_get_system(), mask_data
, width
, height
);
1711 wxMask
*mask
= new wxMask();
1712 mask
->m_bitmap
= gdk_pixmap_new( (GdkWindow
*)&gdk_root_parent
, width
, height
, 1 );
1714 bitmap
.SetMask( mask
);
1719 GdkVisual
*visual
= gdk_window_get_visual( bitmap
.GetPixmap() );
1720 if (visual
== NULL
) visual
= gdk_window_get_visual( (GdkWindow
*) &gdk_root_parent
);
1721 int bpp
= visual
->depth
;
1723 bitmap
.SetDepth( bpp
);
1725 if ((bpp
== 16) && (visual
->red_mask
!= 0xf800)) bpp
= 15;
1726 if (bpp
< 8) bpp
= 8;
1730 enum byte_order
{ RGB
, RBG
, BRG
, BGR
, GRB
, GBR
};
1731 byte_order b_o
= RGB
;
1735 GdkVisual
*visual
= gdk_visual_get_system();
1736 if ((visual
->red_mask
> visual
->green_mask
) && (visual
->green_mask
> visual
->blue_mask
)) b_o
= RGB
;
1737 else if ((visual
->red_mask
> visual
->blue_mask
) && (visual
->blue_mask
> visual
->green_mask
)) b_o
= RGB
;
1738 else if ((visual
->blue_mask
> visual
->red_mask
) && (visual
->red_mask
> visual
->green_mask
)) b_o
= BRG
;
1739 else if ((visual
->blue_mask
> visual
->green_mask
) && (visual
->green_mask
> visual
->red_mask
)) b_o
= BGR
;
1740 else if ((visual
->green_mask
> visual
->red_mask
) && (visual
->red_mask
> visual
->blue_mask
)) b_o
= GRB
;
1741 else if ((visual
->green_mask
> visual
->blue_mask
) && (visual
->blue_mask
> visual
->red_mask
)) b_o
= GBR
;
1744 int r_mask
= GetMaskRed();
1745 int g_mask
= GetMaskGreen();
1746 int b_mask
= GetMaskBlue();
1748 unsigned char* data
= GetData();
1751 for (int y
= 0; y
< height
; y
++)
1753 for (int x
= 0; x
< width
; x
++)
1755 int r
= data
[index
];
1757 int g
= data
[index
];
1759 int b
= data
[index
];
1764 if ((r
== r_mask
) && (b
== b_mask
) && (g
== g_mask
))
1765 gdk_image_put_pixel( mask_image
, x
, y
, 1 );
1767 gdk_image_put_pixel( mask_image
, x
, y
, 0 );
1772 if ((r
== r_mask
) && (b
== b_mask
) && (g
== g_mask
))
1773 gdk_image_put_pixel( mask_image
, x
, y
, 1 );
1775 gdk_image_put_pixel( mask_image
, x
, y
, 0 );
1783 if (wxTheApp
->m_colorCube
)
1785 pixel
= wxTheApp
->m_colorCube
[ ((r
& 0xf8) << 7) + ((g
& 0xf8) << 2) + ((b
& 0xf8) >> 3) ];
1789 GdkColormap
*cmap
= gtk_widget_get_default_colormap();
1790 GdkColor
*colors
= cmap
->colors
;
1791 int max
= 3 * (65536);
1793 for (int i
= 0; i
< cmap
->size
; i
++)
1795 int rdiff
= (r
<< 8) - colors
[i
].red
;
1796 int gdiff
= (g
<< 8) - colors
[i
].green
;
1797 int bdiff
= (b
<< 8) - colors
[i
].blue
;
1798 int sum
= ABS (rdiff
) + ABS (gdiff
) + ABS (bdiff
);
1799 if (sum
< max
) { pixel
= i
; max
= sum
; }
1803 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
1809 guint32 pixel
= ((r
& 0xf8) << 7) | ((g
& 0xf8) << 2) | ((b
& 0xf8) >> 3);
1810 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
1815 guint32 pixel
= ((r
& 0xf8) << 8) | ((g
& 0xfc) << 3) | ((b
& 0xf8) >> 3);
1816 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
1825 case RGB
: pixel
= (r
<< 16) | (g
<< 8) | b
; break;
1826 case RBG
: pixel
= (r
<< 16) | (b
<< 8) | g
; break;
1827 case BRG
: pixel
= (b
<< 16) | (r
<< 8) | g
; break;
1828 case BGR
: pixel
= (b
<< 16) | (g
<< 8) | r
; break;
1829 case GRB
: pixel
= (g
<< 16) | (r
<< 8) | b
; break;
1830 case GBR
: pixel
= (g
<< 16) | (b
<< 8) | r
; break;
1832 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
1841 GdkGC
*data_gc
= gdk_gc_new( bitmap
.GetPixmap() );
1843 gdk_draw_image( bitmap
.GetPixmap(), data_gc
, data_image
, 0, 0, 0, 0, width
, height
);
1845 gdk_image_destroy( data_image
);
1846 gdk_gc_unref( data_gc
);
1852 GdkGC
*mask_gc
= gdk_gc_new( bitmap
.GetMask()->GetBitmap() );
1854 gdk_draw_image( bitmap
.GetMask()->GetBitmap(), mask_gc
, mask_image
, 0, 0, 0, 0, width
, height
);
1856 gdk_image_destroy( mask_image
);
1857 gdk_gc_unref( mask_gc
);
1863 wxImage::wxImage( const wxBitmap
&bitmap
)
1865 wxCHECK_RET( bitmap
.Ok(), "invalid bitmap" );
1867 GdkImage
*gdk_image
= gdk_image_get( bitmap
.GetPixmap(),
1869 bitmap
.GetWidth(), bitmap
.GetHeight() );
1871 wxCHECK_RET( gdk_image
, "couldn't create image" );
1873 Create( bitmap
.GetWidth(), bitmap
.GetHeight() );
1874 char unsigned *data
= GetData();
1878 gdk_image_destroy( gdk_image
);
1879 wxFAIL_MSG( "couldn't create image" );
1883 GdkImage
*gdk_image_mask
= (GdkImage
*) NULL
;
1884 if (bitmap
.GetMask())
1886 gdk_image_mask
= gdk_image_get( bitmap
.GetMask()->GetBitmap(),
1888 bitmap
.GetWidth(), bitmap
.GetHeight() );
1890 SetMaskColour( 16, 16, 16 ); // anything unlikely and dividable
1893 GdkVisual
*visual
= gdk_window_get_visual( bitmap
.GetPixmap() );
1894 if (visual
== NULL
) visual
= gdk_window_get_visual( (GdkWindow
*) &gdk_root_parent
);
1895 int bpp
= visual
->depth
;
1896 if ((bpp
== 16) && (visual
->red_mask
!= 0xf800)) bpp
= 15;
1898 GdkColormap
*cmap
= gtk_widget_get_default_colormap();
1901 for (int j
= 0; j
< bitmap
.GetHeight(); j
++)
1903 for (int i
= 0; i
< bitmap
.GetWidth(); i
++)
1905 int pixel
= gdk_image_get_pixel( gdk_image
, i
, j
);
1908 data
[pos
] = cmap
->colors
[pixel
].red
>> 8;
1909 data
[pos
+1] = cmap
->colors
[pixel
].green
>> 8;
1910 data
[pos
+2] = cmap
->colors
[pixel
].blue
>> 8;
1911 } else if (bpp
== 15)
1913 data
[pos
] = (pixel
>> 7) & 0xf8;
1914 data
[pos
+1] = (pixel
>> 2) & 0xf8;
1915 data
[pos
+2] = (pixel
<< 3) & 0xf8;
1916 } else if (bpp
== 16)
1918 data
[pos
] = (pixel
>> 8) & 0xf8;
1919 data
[pos
+1] = (pixel
>> 3) & 0xfc;
1920 data
[pos
+2] = (pixel
<< 3) & 0xf8;
1923 data
[pos
] = (pixel
>> 16) & 0xff;
1924 data
[pos
+1] = (pixel
>> 8) & 0xff;
1925 data
[pos
+2] = pixel
& 0xff;
1930 int mask_pixel
= gdk_image_get_pixel( gdk_image_mask
, i
, j
);
1931 if (mask_pixel
== 0)
1943 gdk_image_destroy( gdk_image
);
1944 if (gdk_image_mask
) gdk_image_destroy( gdk_image_mask
);
1952 #include "wx/utils.h"
1955 wxBitmap
wxImage::ConvertToBitmap() const
1959 wxCHECK_MSG( Ok(), bitmap
, "invalid image" );
1961 int width
= GetWidth();
1962 int height
= GetHeight();
1964 bitmap
.SetHeight( height
);
1965 bitmap
.SetWidth( width
);
1967 Display
*dpy
= (Display
*) wxGetDisplay();
1968 Visual
* vis
= DefaultVisual( dpy
, DefaultScreen( dpy
) );
1969 int bpp
= DefaultDepth( dpy
, DefaultScreen( dpy
) );
1973 XImage
*data_image
= XCreateImage( dpy
, vis
, bpp
, ZPixmap
, 0, 0, width
, height
, 32, 0 );
1974 data_image
->data
= new char[ data_image
->bytes_per_line
* data_image
->height
];
1976 bitmap
.Create( width
, height
, bpp
);
1981 GdkImage *mask_image = (GdkImage*) NULL;
1985 unsigned char *mask_data = (unsigned char*)malloc( ((width >> 3)+8) * height );
1987 mask_image = gdk_image_new_bitmap( gdk_visual_get_system(), mask_data, width, height );
1989 wxMask *mask = new wxMask();
1990 mask->m_bitmap = gdk_pixmap_new( (GdkWindow*)&gdk_root_parent, width, height, 1 );
1992 bitmap.SetMask( mask );
1996 // Retrieve depth info
1998 XVisualInfo vinfo_template
;
2001 vinfo_template
.visual
= vis
;
2002 vinfo_template
.visualid
= XVisualIDFromVisual( vis
);
2003 vinfo_template
.depth
= bpp
;
2006 vi
= XGetVisualInfo( dpy
, VisualIDMask
|VisualDepthMask
, &vinfo_template
, &nitem
);
2010 printf("no visual.\n" );
2011 return wxNullBitmap
;
2016 if ((bpp
== 16) && (vi
->red_mask
!= 0xf800)) bpp
= 15;
2017 if (bpp
< 8) bpp
= 8;
2021 enum byte_order
{ RGB
, RBG
, BRG
, BGR
, GRB
, GBR
};
2022 byte_order b_o
= RGB
;
2026 if ((vi
->red_mask
> vi
->green_mask
) && (vi
->green_mask
> vi
->blue_mask
)) b_o
= RGB
;
2027 else if ((vi
->red_mask
> vi
->blue_mask
) && (vi
->blue_mask
> vi
->green_mask
)) b_o
= RGB
;
2028 else if ((vi
->blue_mask
> vi
->red_mask
) && (vi
->red_mask
> vi
->green_mask
)) b_o
= BRG
;
2029 else if ((vi
->blue_mask
> vi
->green_mask
) && (vi
->green_mask
> vi
->red_mask
)) b_o
= BGR
;
2030 else if ((vi
->green_mask
> vi
->red_mask
) && (vi
->red_mask
> vi
->blue_mask
)) b_o
= GRB
;
2031 else if ((vi
->green_mask
> vi
->blue_mask
) && (vi
->blue_mask
> vi
->red_mask
)) b_o
= GBR
;
2035 int r_mask = GetMaskRed();
2036 int g_mask = GetMaskGreen();
2037 int b_mask = GetMaskBlue();
2043 Colormap cmap
= (Colormap
) wxTheApp
->GetMainColormap( dpy
);
2045 for (int i
= 0; i
< 256; i
++) colors
[i
].pixel
= i
;
2046 XQueryColors( dpy
, cmap
, colors
, 256 );
2049 unsigned char* data
= GetData();
2052 for (int y
= 0; y
< height
; y
++)
2054 for (int x
= 0; x
< width
; x
++)
2056 int r
= data
[index
];
2058 int g
= data
[index
];
2060 int b
= data
[index
];
2066 if ((r == r_mask) && (b == b_mask) && (g == g_mask))
2067 gdk_image_put_pixel( mask_image, x, y, 1 );
2069 gdk_image_put_pixel( mask_image, x, y, 0 );
2079 if (wxTheApp->m_colorCube)
2081 pixel = wxTheApp->m_colorCube
2082 [ ((r & 0xf8) << 7) + ((g & 0xf8) << 2) + ((b & 0xf8) >> 3) ];
2087 int max
= 3 * (65536);
2088 for (int i
= 0; i
< 256; i
++)
2090 int rdiff
= (r
<< 8) - colors
[i
].red
;
2091 int gdiff
= (g
<< 8) - colors
[i
].green
;
2092 int bdiff
= (b
<< 8) - colors
[i
].blue
;
2093 int sum
= abs (rdiff
) + abs (gdiff
) + abs (bdiff
);
2094 if (sum
< max
) { pixel
= i
; max
= sum
; }
2099 XPutPixel( data_image
, x
, y
, pixel
);
2104 int pixel
= ((r
& 0xf8) << 7) | ((g
& 0xf8) << 2) | ((b
& 0xf8) >> 3);
2105 XPutPixel( data_image
, x
, y
, pixel
);
2110 int pixel
= ((r
& 0xf8) << 8) | ((g
& 0xfc) << 3) | ((b
& 0xf8) >> 3);
2111 XPutPixel( data_image
, x
, y
, pixel
);
2120 case RGB
: pixel
= (r
<< 16) | (g
<< 8) | b
; break;
2121 case RBG
: pixel
= (r
<< 16) | (b
<< 8) | g
; break;
2122 case BRG
: pixel
= (b
<< 16) | (r
<< 8) | g
; break;
2123 case BGR
: pixel
= (b
<< 16) | (g
<< 8) | r
; break;
2124 case GRB
: pixel
= (g
<< 16) | (r
<< 8) | b
; break;
2125 case GBR
: pixel
= (g
<< 16) | (b
<< 8) | r
; break;
2127 XPutPixel( data_image
, x
, y
, pixel
);
2137 gcvalues
.foreground
= BlackPixel( dpy
, DefaultScreen( dpy
) );
2138 GC gc
= XCreateGC( dpy
, RootWindow ( dpy
, DefaultScreen(dpy
) ), GCForeground
, &gcvalues
);
2139 XPutImage( dpy
, (Drawable
)bitmap
.GetPixmap(), gc
, data_image
, 0, 0, 0, 0, width
, height
);
2141 XDestroyImage( data_image
);
2149 GdkGC *mask_gc = gdk_gc_new( bitmap.GetMask()->GetBitmap() );
2151 gdk_draw_image( bitmap.GetMask()->GetBitmap(), mask_gc, mask_image, 0, 0, 0, 0, width, height );
2153 gdk_image_destroy( mask_image );
2154 gdk_gc_unref( mask_gc );
2161 wxImage::wxImage( const wxBitmap
&bitmap
)
2163 wxCHECK_RET( bitmap
.Ok(), "invalid bitmap" );
2165 Display
*dpy
= (Display
*) wxGetDisplay();
2166 Visual
* vis
= DefaultVisual( dpy
, DefaultScreen( dpy
) );
2167 int bpp
= DefaultDepth( dpy
, DefaultScreen( dpy
) );
2169 XImage
*ximage
= XGetImage( dpy
,
2170 (Drawable
)bitmap
.GetPixmap(),
2172 bitmap
.GetWidth(), bitmap
.GetHeight(),
2173 AllPlanes
, ZPixmap
);
2175 wxCHECK_RET( ximage
, "couldn't create image" );
2177 Create( bitmap
.GetWidth(), bitmap
.GetHeight() );
2178 char unsigned *data
= GetData();
2182 XDestroyImage( ximage
);
2183 wxFAIL_MSG( "couldn't create image" );
2188 GdkImage *gdk_image_mask = (GdkImage*) NULL;
2189 if (bitmap.GetMask())
2191 gdk_image_mask = gdk_image_get( bitmap.GetMask()->GetBitmap(),
2193 bitmap.GetWidth(), bitmap.GetHeight() );
2195 SetMaskColour( 16, 16, 16 ); // anything unlikely and dividable
2199 // Retrieve depth info
2201 XVisualInfo vinfo_template
;
2204 vinfo_template
.visual
= vis
;
2205 vinfo_template
.visualid
= XVisualIDFromVisual( vis
);
2206 vinfo_template
.depth
= bpp
;
2209 vi
= XGetVisualInfo( dpy
, VisualIDMask
|VisualDepthMask
, &vinfo_template
, &nitem
);
2213 printf("no visual.\n" );
2217 if ((bpp
== 16) && (vi
->red_mask
!= 0xf800)) bpp
= 15;
2224 Colormap cmap
= (Colormap
)wxTheApp
->GetMainColormap( dpy
);
2226 for (int i
= 0; i
< 256; i
++) colors
[i
].pixel
= i
;
2227 XQueryColors( dpy
, cmap
, colors
, 256 );
2231 for (int j
= 0; j
< bitmap
.GetHeight(); j
++)
2233 for (int i
= 0; i
< bitmap
.GetWidth(); i
++)
2235 int pixel
= XGetPixel( ximage
, i
, j
);
2238 data
[pos
] = colors
[pixel
].red
>> 8;
2239 data
[pos
+1] = colors
[pixel
].green
>> 8;
2240 data
[pos
+2] = colors
[pixel
].blue
>> 8;
2241 } else if (bpp
== 15)
2243 data
[pos
] = (pixel
>> 7) & 0xf8;
2244 data
[pos
+1] = (pixel
>> 2) & 0xf8;
2245 data
[pos
+2] = (pixel
<< 3) & 0xf8;
2246 } else if (bpp
== 16)
2248 data
[pos
] = (pixel
>> 8) & 0xf8;
2249 data
[pos
+1] = (pixel
>> 3) & 0xfc;
2250 data
[pos
+2] = (pixel
<< 3) & 0xf8;
2253 data
[pos
] = (pixel
>> 16) & 0xff;
2254 data
[pos
+1] = (pixel
>> 8) & 0xff;
2255 data
[pos
+2] = pixel
& 0xff;
2261 int mask_pixel = gdk_image_get_pixel( gdk_image_mask, i, j );
2262 if (mask_pixel == 0)
2275 XDestroyImage( ximage
);
2277 if (gdk_image_mask) gdk_image_destroy( gdk_image_mask );