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
);
463 AddHandler( new wxJPEGHandler
);
467 void wxImage::CleanUpHandlers()
469 wxNode
*node
= sm_handlers
.First();
472 wxImageHandler
*handler
= (wxImageHandler
*)node
->Data();
473 wxNode
*next
= node
->Next();
480 //-----------------------------------------------------------------------------
482 //-----------------------------------------------------------------------------
484 #if !USE_SHARED_LIBRARIES
485 IMPLEMENT_DYNAMIC_CLASS(wxImageHandler
,wxObject
)
489 bool wxImageHandler::LoadFile( wxImage
*WXUNUSED(image
), wxInputStream
& WXUNUSED(stream
) )
494 bool wxImageHandler::SaveFile( wxImage
*WXUNUSED(image
), wxOutputStream
& WXUNUSED(stream
) )
498 #endif // wxUSE_STREAMS
500 //-----------------------------------------------------------------------------
502 //-----------------------------------------------------------------------------
506 #if !USE_SHARED_LIBRARIES
507 IMPLEMENT_DYNAMIC_CLASS(wxPNGHandler
,wxImageHandler
)
512 static void _PNG_stream_reader( png_structp png_ptr
, png_bytep data
, png_size_t length
)
514 ((wxInputStream
*) png_get_io_ptr( png_ptr
)) -> Read(data
, length
);
517 static void _PNG_stream_writer( png_structp png_ptr
, png_bytep data
, png_size_t length
)
519 ((wxOutputStream
*) png_get_io_ptr( png_ptr
)) -> Write(data
, length
);
522 bool wxPNGHandler::LoadFile( wxImage
*image
, wxInputStream
& stream
)
524 // VZ: as this function uses setjmp() the only fool proof error handling
525 // method is to use goto (setjmp is not really C++ dtors friendly...)
527 unsigned char **lines
= (unsigned char **) NULL
;
529 png_infop info_ptr
= (png_infop
) NULL
;
533 png_structp png_ptr
= png_create_read_struct( PNG_LIBPNG_VER_STRING
,
535 (png_error_ptr
) NULL
,
536 (png_error_ptr
) NULL
);
540 info_ptr
= png_create_info_struct( png_ptr
);
544 if (setjmp(png_ptr
->jmpbuf
))
547 if (info_ptr
->color_type
== PNG_COLOR_TYPE_RGB_ALPHA
)
550 png_set_read_fn( png_ptr
, &stream
, _PNG_stream_reader
);
552 png_uint_32 width
,height
;
553 int bit_depth
,color_type
,interlace_type
;
555 png_read_info( png_ptr
, info_ptr
);
556 png_get_IHDR( png_ptr
, info_ptr
, &width
, &height
, &bit_depth
, &color_type
, &interlace_type
, (int*) NULL
, (int*) NULL
);
558 if (color_type
== PNG_COLOR_TYPE_PALETTE
)
559 png_set_expand( png_ptr
);
561 png_set_strip_16( png_ptr
);
562 png_set_packing( png_ptr
);
563 if (png_get_valid( png_ptr
, info_ptr
, PNG_INFO_tRNS
))
564 png_set_expand( png_ptr
);
565 png_set_filler( png_ptr
, 0xff, PNG_FILLER_AFTER
);
567 image
->Create( width
, height
);
572 lines
= (unsigned char **)malloc( height
* sizeof(unsigned char *) );
576 for (i
= 0; i
< height
; i
++)
578 if ((lines
[i
] = (unsigned char *)malloc(width
* (sizeof(unsigned char) * 4))) == NULL
)
580 for ( unsigned int n
= 0; n
< i
; n
++ )
586 // loaded successfully!
589 png_read_image( png_ptr
, lines
);
590 png_destroy_read_struct( &png_ptr
, &info_ptr
, (png_infopp
) NULL
);
591 unsigned char *ptr
= image
->GetData();
592 if ((color_type
== PNG_COLOR_TYPE_GRAY
) ||
593 (color_type
== PNG_COLOR_TYPE_GRAY_ALPHA
))
595 for (unsigned int y
= 0; y
< height
; y
++)
597 unsigned char *ptr2
= lines
[y
];
598 for (unsigned int x
= 0; x
< width
; x
++)
600 unsigned char r
= *ptr2
++;
601 unsigned char a
= *ptr2
++;
620 for (unsigned int y
= 0; y
< height
; y
++)
622 unsigned char *ptr2
= lines
[y
];
623 for (unsigned int x
= 0; x
< width
; x
++)
625 unsigned char r
= *ptr2
++;
626 unsigned char g
= *ptr2
++;
627 unsigned char b
= *ptr2
++;
628 unsigned char a
= *ptr2
++;
638 if ((r
== 255) && (g
== 0) && (b
== 255)) r
= 254;
647 for ( unsigned int j
= 0; j
< height
; j
++ )
653 image
->SetMaskColour( 255, 0, 255 );
657 image
->SetMask( FALSE
);
664 wxLogError(_("Couldn't load a PNG image - probably file is corrupted."));
680 png_destroy_read_struct( &png_ptr
, &info_ptr
, (png_infopp
) NULL
);
684 png_destroy_read_struct( &png_ptr
, (png_infopp
) NULL
, (png_infopp
) NULL
);
690 bool wxPNGHandler::SaveFile( wxImage
*image
, wxOutputStream
& stream
)
693 png_structp png_ptr
= png_create_write_struct( PNG_LIBPNG_VER_STRING
, NULL
, NULL
, NULL
);
699 png_infop info_ptr
= png_create_info_struct(png_ptr
);
700 if (info_ptr
== NULL
)
702 png_destroy_write_struct( &png_ptr
, (png_infopp
)NULL
);
706 if (setjmp(png_ptr
->jmpbuf
))
708 png_destroy_write_struct( &png_ptr
, (png_infopp
)NULL
);
712 png_set_write_fn( png_ptr
, &stream
, _PNG_stream_writer
, NULL
);
714 png_set_IHDR( png_ptr
, info_ptr
, image
->GetWidth(), image
->GetHeight(), 8,
715 PNG_COLOR_TYPE_RGB_ALPHA
, PNG_INTERLACE_NONE
,
716 PNG_COMPRESSION_TYPE_BASE
, PNG_FILTER_TYPE_BASE
);
723 png_set_sBIT( png_ptr
, info_ptr
, &sig_bit
);
724 png_write_info( png_ptr
, info_ptr
);
725 png_set_shift( png_ptr
, &sig_bit
);
726 png_set_packing( png_ptr
);
728 unsigned char *data
= (unsigned char *)malloc( image
->GetWidth()*4 );
731 png_destroy_write_struct( &png_ptr
, (png_infopp
)NULL
);
735 for (int y
= 0; y
< image
->GetHeight(); y
++)
737 unsigned char *ptr
= image
->GetData() + (y
* image
->GetWidth() * 3);
738 for (int x
= 0; x
< image
->GetWidth(); x
++)
740 data
[(x
<< 2) + 0] = *ptr
++;
741 data
[(x
<< 2) + 1] = *ptr
++;
742 data
[(x
<< 2) + 2] = *ptr
++;
743 if ((data
[(x
<< 2) + 0] == image
->GetMaskRed()) &&
744 (data
[(x
<< 2) + 1] == image
->GetMaskGreen()) &&
745 (data
[(x
<< 2) + 2] == image
->GetMaskBlue()))
747 data
[(x
<< 2) + 3] = 0;
751 data
[(x
<< 2) + 3] = 255;
754 png_bytep row_ptr
= data
;
755 png_write_rows( png_ptr
, &row_ptr
, 1 );
759 png_write_end( png_ptr
, info_ptr
);
760 png_destroy_write_struct( &png_ptr
, (png_infopp
)&info_ptr
);
764 #endif // wxUSE_STREAMS
771 //-----------------------------------------------------------------------------
773 //-----------------------------------------------------------------------------
777 #if !USE_SHARED_LIBRARIES
778 IMPLEMENT_DYNAMIC_CLASS(wxJPEGHandler
,wxImageHandler
)
784 //------------- JPEG Data Source Manager
787 struct jpeg_source_mgr pub
; /* public fields */
789 JOCTET
* buffer
; /* start of buffer */
792 typedef my_source_mgr
* my_src_ptr
;
794 METHODDEF(void) my_init_source ( j_decompress_ptr cinfo
)
798 METHODDEF(boolean
) my_fill_input_buffer ( j_decompress_ptr cinfo
)
803 METHODDEF(void) my_skip_input_data ( j_decompress_ptr cinfo
, long num_bytes
)
805 my_src_ptr src
= (my_src_ptr
) cinfo
->src
;
807 src
->pub
.next_input_byte
+= (size_t) num_bytes
;
808 src
->pub
.bytes_in_buffer
-= (size_t) num_bytes
;
811 METHODDEF(void) my_term_source ( j_decompress_ptr cinfo
)
813 my_src_ptr src
= (my_src_ptr
) cinfo
->src
;
818 void jpeg_wxio_src( j_decompress_ptr cinfo
, wxInputStream
& infile
)
822 if (cinfo
->src
== NULL
) { /* first time for this JPEG object? */
823 cinfo
->src
= (struct jpeg_source_mgr
*)
824 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
) cinfo
, JPOOL_PERMANENT
,
825 sizeof(my_source_mgr
));
826 src
= (my_src_ptr
) cinfo
->src
;
828 src
= (my_src_ptr
) cinfo
->src
;
829 src
->pub
.bytes_in_buffer
= infile
.StreamSize(); /* forces fill_input_buffer on first read */
830 src
->buffer
= (JOCTET
*) malloc (infile
.StreamSize());
831 src
->pub
.next_input_byte
= src
->buffer
; /* until buffer loaded */
832 infile
.Read(src
->buffer
, infile
.StreamSize());
834 src
->pub
.init_source
= my_init_source
;
835 src
->pub
.fill_input_buffer
= my_fill_input_buffer
;
836 src
->pub
.skip_input_data
= my_skip_input_data
;
837 src
->pub
.resync_to_restart
= jpeg_resync_to_restart
; /* use default method */
838 src
->pub
.term_source
= my_term_source
;
843 bool wxJPEGHandler::LoadFile( wxImage
*image
, wxInputStream
& stream
)
845 struct jpeg_decompress_struct cinfo
;
846 struct jpeg_error_mgr jerr
;
852 cinfo
.err
= jpeg_std_error( &jerr
);
853 jpeg_create_decompress( &cinfo
);
854 jpeg_wxio_src( &cinfo
, stream
);
855 jpeg_read_header( &cinfo
, TRUE
);
856 cinfo
.out_color_space
= JCS_RGB
;
857 jpeg_start_decompress( &cinfo
);
859 image
->Create( cinfo
.image_width
, cinfo
.image_height
);
861 jpeg_finish_decompress( &cinfo
);
862 jpeg_destroy_decompress( &cinfo
);
865 image
->SetMask( FALSE
);
866 ptr
= image
->GetData();
867 stride
= cinfo
.output_width
* 3;
868 tempbuf
= (*cinfo
.mem
->alloc_sarray
)
869 ((j_common_ptr
) &cinfo
, JPOOL_IMAGE
, stride
, 1 );
871 while ( cinfo
.output_scanline
< cinfo
.output_height
) {
872 jpeg_read_scanlines( &cinfo
, tempbuf
, 1 );
873 memcpy( ptr
, tempbuf
[0], stride
);
876 jpeg_finish_decompress( &cinfo
);
877 jpeg_destroy_decompress( &cinfo
);
886 struct jpeg_destination_mgr pub
;
888 wxOutputStream
*stream
;
890 } my_destination_mgr
;
892 typedef my_destination_mgr
* my_dest_ptr
;
894 #define OUTPUT_BUF_SIZE 4096 /* choose an efficiently fwrite'able size */
896 METHODDEF(void) init_destination (j_compress_ptr cinfo
)
898 my_dest_ptr dest
= (my_dest_ptr
) cinfo
->dest
;
900 /* Allocate the output buffer --- it will be released when done with image */
901 dest
->buffer
= (JOCTET
*)
902 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
) cinfo
, JPOOL_IMAGE
,
903 OUTPUT_BUF_SIZE
* sizeof(JOCTET
));
904 dest
->pub
.next_output_byte
= dest
->buffer
;
905 dest
->pub
.free_in_buffer
= OUTPUT_BUF_SIZE
;
908 METHODDEF(boolean
) empty_output_buffer (j_compress_ptr cinfo
)
910 my_dest_ptr dest
= (my_dest_ptr
) cinfo
->dest
;
912 dest
->stream
->Write(dest
->buffer
, OUTPUT_BUF_SIZE
);
913 dest
->pub
.next_output_byte
= dest
->buffer
;
914 dest
->pub
.free_in_buffer
= OUTPUT_BUF_SIZE
;
918 METHODDEF(void) term_destination (j_compress_ptr cinfo
)
920 my_dest_ptr dest
= (my_dest_ptr
) cinfo
->dest
;
921 size_t datacount
= OUTPUT_BUF_SIZE
- dest
->pub
.free_in_buffer
;
922 /* Write any data remaining in the buffer */
924 dest
->stream
->Write(dest
->buffer
, datacount
);
927 GLOBAL(void) jpeg_wxio_dest (j_compress_ptr cinfo
, wxOutputStream
& outfile
)
931 if (cinfo
->dest
== NULL
) { /* first time for this JPEG object? */
932 cinfo
->dest
= (struct jpeg_destination_mgr
*)
933 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
) cinfo
, JPOOL_PERMANENT
,
934 sizeof(my_destination_mgr
));
937 dest
= (my_dest_ptr
) cinfo
->dest
;
938 dest
->pub
.init_destination
= init_destination
;
939 dest
->pub
.empty_output_buffer
= empty_output_buffer
;
940 dest
->pub
.term_destination
= term_destination
;
941 dest
->stream
= &outfile
;
946 bool wxJPEGHandler::SaveFile( wxImage
*image
, wxOutputStream
& stream
)
948 struct jpeg_compress_struct cinfo
;
949 struct jpeg_error_mgr jerr
;
950 JSAMPROW row_pointer
[1]; /* pointer to JSAMPLE row[s] */
951 JSAMPLE
*image_buffer
;
952 int stride
; /* physical row width in image buffer */
954 cinfo
.err
= jpeg_std_error(&jerr
);
955 jpeg_create_compress(&cinfo
);
956 jpeg_wxio_dest(&cinfo
, stream
);
958 cinfo
.image_width
= image
->GetWidth();
959 cinfo
.image_height
= image
->GetHeight();
960 cinfo
.input_components
= 3;
961 cinfo
.in_color_space
= JCS_RGB
;
962 jpeg_set_defaults(&cinfo
);
963 jpeg_start_compress(&cinfo
, TRUE
);
965 stride
= cinfo
.image_width
* 3; /* JSAMPLEs per row in image_buffer */
966 image_buffer
= image
->GetData();
967 while (cinfo
.next_scanline
< cinfo
.image_height
) {
968 row_pointer
[0] = &image_buffer
[cinfo
.next_scanline
* stride
];
969 jpeg_write_scanlines( &cinfo
, row_pointer
, 1 );
971 jpeg_finish_compress(&cinfo
);
972 jpeg_destroy_compress(&cinfo
);
976 #endif // wxUSE_STREAMS
984 //-----------------------------------------------------------------------------
986 //-----------------------------------------------------------------------------
988 #if !USE_SHARED_LIBRARIES
989 IMPLEMENT_DYNAMIC_CLASS(wxBMPHandler
,wxImageHandler
)
993 bool wxBMPHandler::LoadFile( wxImage
*image
, wxInputStream
& stream
)
995 unsigned char *data
, *ptr
;
996 int done
, i
, bpp
, planes
, comp
, ncolors
, line
, column
,
997 linesize
, linepos
, rshift
= 0, gshift
= 0, bshift
= 0;
1000 long int dbuf
[4], dword
, rmask
= 0, gmask
= 0, bmask
= 0, offset
,
1002 off_t start_offset
= stream
.TellI();
1003 signed char bbuf
[4];
1006 unsigned char r
, g
, b
;
1015 #ifndef BI_BITFIELDS
1016 #define BI_BITFIELDS 3
1023 * Reading the bmp header
1026 stream
.Read(&bbuf
, 2);
1028 stream
.Read(dbuf
, 4 * 4);
1033 stream
.Read(dbuf
, 4 * 2);
1034 int width
= (int)dbuf
[0];
1035 int height
= (int)dbuf
[1];
1038 wxLogError( "Image width > 32767 pixels for file\n" );
1043 wxLogError( "Image height > 32767 pixels for file\n" );
1046 stream
.Read(&word
, 2);
1048 stream
.Read(&word
, 2);
1050 if (bpp
!= 1 && bpp
!= 4 && bpp
!= 8 && bpp
&& 16 && bpp
!= 24 && bpp
!= 32)
1052 wxLogError( "unknown bitdepth in file\n" );
1055 stream
.Read(dbuf
, 4 * 4);
1056 comp
= (int)dbuf
[0];
1057 if (comp
!= BI_RGB
&& comp
!= BI_RLE4
&& comp
!= BI_RLE8
&& comp
!= BI_BITFIELDS
)
1059 wxLogError( "unknown encoding in Windows BMP file\n" );
1062 stream
.Read(dbuf
, 4 * 2);
1063 ncolors
= (int)dbuf
[0];
1066 /* some more sanity checks */
1067 if (((comp
== BI_RLE4
) && (bpp
!= 4)) || ((comp
== BI_RLE8
) && (bpp
!= 8)) || ((comp
== BI_BITFIELDS
) && (bpp
!= 16 && bpp
!= 32)))
1069 wxLogError( "encoding of BMP doesn't match bitdepth\n" );
1074 cmap
= (struct _cmap
*)malloc(sizeof(struct _cmap
) * ncolors
);
1078 wxLogError( "Cannot allocate RAM for color map in BMP file\n" );
1085 image
->Create( width
, height
);
1086 ptr
= image
->GetData();
1089 wxLogError( "Cannot allocate RAM for RGB data in file\n" );
1096 * Reading the palette, if it exists.
1098 if (bpp
< 16 && ncolors
!= 0)
1100 for (i
= 0; i
< ncolors
; i
++)
1102 stream
.Read(bbuf
, 4);
1103 cmap
[i
].b
= bbuf
[0];
1104 cmap
[i
].g
= bbuf
[1];
1105 cmap
[i
].r
= bbuf
[2];
1108 else if (bpp
== 16 || bpp
== 32)
1110 if (comp
== BI_BITFIELDS
)
1114 stream
.Read(dbuf
, 4 * 3);
1118 /* find shift amount.. ugly, but i can't think of a better way */
1119 for (bit
= 0; bit
< bpp
; bit
++)
1121 if (bmask
& (1 << bit
))
1123 if (gmask
& (1 << bit
))
1125 if (rmask
& (1 << bit
))
1150 * Reading the image data
1152 stream
.SeekI(start_offset
+ offset
);
1155 /* set the whole image to the background color */
1156 if (bpp
< 16 && (comp
== BI_RLE4
|| comp
== BI_RLE8
))
1158 for (i
= 0; i
< width
* height
; i
++)
1168 #define poffset (line * width * 3 + column * 3)
1171 * BMPs are stored upside down... hmmmmmmmmmm....
1174 linesize
= ((width
* bpp
+ 31) / 32) * 4;
1175 for (line
= (height
- 1); line
>= 0; line
--)
1178 for (column
= 0; column
< width
;)
1185 aByte
= stream
.GetC();
1190 for (bit
= 0; bit
< 8; bit
++)
1192 index
= ((aByte
& (0x80 >> bit
)) ? 1 : 0);
1193 ptr
[poffset
] = cmap
[index
].r
;
1194 ptr
[poffset
+ 1] = cmap
[index
].g
;
1195 ptr
[poffset
+ 2] = cmap
[index
].b
;
1201 if (comp
== BI_RLE4
)
1203 wxLogError( "can't deal with 4bit encoded yet.\n");
1212 for (nibble
= 0; nibble
< 2; nibble
++)
1214 index
= ((aByte
& (0xF0 >> nibble
* 4)) >> (!nibble
* 4));
1217 ptr
[poffset
] = cmap
[index
].r
;
1218 ptr
[poffset
+ 1] = cmap
[index
].g
;
1219 ptr
[poffset
+ 2] = cmap
[index
].b
;
1226 if (comp
== BI_RLE8
)
1228 unsigned char first
;
1231 aByte
= stream
.GetC();
1236 /* column = width; */
1238 else if (aByte
== 1)
1243 else if (aByte
== 2)
1245 aByte
= stream
.GetC();
1247 linepos
= column
* bpp
/ 8;
1248 aByte
= stream
.GetC();
1253 int absolute
= aByte
;
1255 for (i
= 0; i
< absolute
; i
++)
1258 aByte
= stream
.GetC();
1259 ptr
[poffset
] = cmap
[aByte
].r
;
1260 ptr
[poffset
+ 1] = cmap
[aByte
].g
;
1261 ptr
[poffset
+ 2] = cmap
[aByte
].b
;
1264 if (absolute
& 0x01)
1265 aByte
= stream
.GetC();
1270 for (i
= 0; i
< first
; i
++)
1272 ptr
[poffset
] = cmap
[aByte
].r
;
1273 ptr
[poffset
+ 1] = cmap
[aByte
].g
;
1274 ptr
[poffset
+ 2] = cmap
[aByte
].b
;
1282 ptr
[poffset
] = cmap
[aByte
].r
;
1283 ptr
[poffset
+ 1] = cmap
[aByte
].g
;
1284 ptr
[poffset
+ 2] = cmap
[aByte
].b
;
1292 stream
.Read(&bbuf
, 3);
1294 ptr
[poffset
] = (unsigned char)bbuf
[2];
1295 ptr
[poffset
+ 1] = (unsigned char)bbuf
[1];
1296 ptr
[poffset
+ 2] = (unsigned char)bbuf
[0];
1303 stream
.Read(&word
, 2);
1305 temp
= (word
& rmask
) >> rshift
;
1306 ptr
[poffset
] = temp
;
1307 temp
= (word
& gmask
) >> gshift
;
1308 ptr
[poffset
+ 1] = temp
;
1309 temp
= (word
& bmask
) >> gshift
;
1310 ptr
[poffset
+ 2] = temp
;
1317 stream
.Read(&dword
, 4);
1319 temp
= (dword
& rmask
) >> rshift
;
1320 ptr
[poffset
] = temp
;
1321 temp
= (dword
& gmask
) >> gshift
;
1322 ptr
[poffset
+ 1] = temp
;
1323 temp
= (dword
& bmask
) >> bshift
;
1324 ptr
[poffset
+ 2] = temp
;
1328 while ((linepos
< linesize
) && (comp
!= 1) && (comp
!= 2))
1330 stream
.Read(&aByte
, 1);
1332 if (stream
.LastError() != wxStream_NOERROR
)
1336 if (cmap
) free(cmap
);
1338 image
->SetMask( FALSE
);
1342 #endif // wxUSE_STREAMS
1346 wxBitmap
wxImage::ConvertToBitmap() const
1348 // sizeLimit is the MS upper limit for the DIB size
1349 int sizeLimit
= 1024*768*3;
1351 // width and height of the device-dependent bitmap
1352 int width
= GetWidth();
1353 int bmpHeight
= GetHeight();
1355 // calc the number of bytes per scanline and padding
1356 int bytePerLine
= width
*3;
1357 int sizeDWORD
= sizeof( DWORD
);
1358 div_t lineBoundary
= div( bytePerLine
, sizeDWORD
);
1360 if( lineBoundary
.rem
> 0 )
1362 padding
= sizeDWORD
- lineBoundary
.rem
;
1363 bytePerLine
+= padding
;
1365 // calc the number of DIBs and heights of DIBs
1368 int height
= sizeLimit
/bytePerLine
;
1369 if( height
>= bmpHeight
)
1373 div_t result
= div( bmpHeight
, height
);
1374 numDIB
= result
.quot
;
1375 hRemain
= result
.rem
;
1376 if( hRemain
>0 ) numDIB
++;
1379 // set bitmap parameters
1381 wxCHECK_MSG( Ok(), bitmap
, "invalid image" );
1382 bitmap
.SetWidth( width
);
1383 bitmap
.SetHeight( bmpHeight
);
1384 bitmap
.SetDepth( wxDisplayDepth() );
1386 // create a DIB header
1387 int headersize
= sizeof(BITMAPINFOHEADER
);
1388 LPBITMAPINFO lpDIBh
= (BITMAPINFO
*) malloc( headersize
);
1389 wxCHECK_MSG( lpDIBh
, bitmap
, "could not allocate memory for DIB header" );
1390 // Fill in the DIB header
1391 lpDIBh
->bmiHeader
.biSize
= headersize
;
1392 lpDIBh
->bmiHeader
.biWidth
= (DWORD
)width
;
1393 lpDIBh
->bmiHeader
.biHeight
= (DWORD
)(-height
);
1394 lpDIBh
->bmiHeader
.biSizeImage
= bytePerLine
*height
;
1395 // the general formula for biSizeImage:
1396 // ( ( ( ((DWORD)width*24) +31 ) & ~31 ) >> 3 ) * height;
1397 lpDIBh
->bmiHeader
.biPlanes
= 1;
1398 lpDIBh
->bmiHeader
.biBitCount
= 24;
1399 lpDIBh
->bmiHeader
.biCompression
= BI_RGB
;
1400 lpDIBh
->bmiHeader
.biClrUsed
= 0;
1401 // These seem not really needed for our purpose here.
1402 lpDIBh
->bmiHeader
.biClrImportant
= 0;
1403 lpDIBh
->bmiHeader
.biXPelsPerMeter
= 0;
1404 lpDIBh
->bmiHeader
.biYPelsPerMeter
= 0;
1405 // memory for DIB data
1406 unsigned char *lpBits
;
1407 lpBits
= (unsigned char *)malloc( lpDIBh
->bmiHeader
.biSizeImage
);
1410 wxFAIL_MSG( "could not allocate memory for DIB" );
1415 // create and set the device-dependent bitmap
1416 HDC hdc
= ::GetDC(NULL
);
1417 HDC memdc
= ::CreateCompatibleDC( hdc
);
1419 hbitmap
= ::CreateCompatibleBitmap( hdc
, width
, bmpHeight
);
1420 ::SelectObject( memdc
, hbitmap
);
1422 // copy image data into DIB data and then into DDB (in a loop)
1423 unsigned char *data
= GetData();
1426 unsigned char *ptdata
= data
;
1427 unsigned char *ptbits
;
1429 for( n
=0; n
<numDIB
; n
++ )
1431 if( numDIB
> 1 && n
== numDIB
-1 && hRemain
> 0 )
1433 // redefine height and size of the (possibly) last smaller DIB
1434 // memory is not reallocated
1436 lpDIBh
->bmiHeader
.biHeight
= (DWORD
)(-height
);
1437 lpDIBh
->bmiHeader
.biSizeImage
= bytePerLine
*height
;
1441 for( j
=0; j
<height
; j
++ )
1443 for( i
=0; i
<width
; i
++ )
1445 *(ptbits
++) = *(ptdata
+2);
1446 *(ptbits
++) = *(ptdata
+1);
1447 *(ptbits
++) = *(ptdata
);
1450 for( i
=0; i
< padding
; i
++ ) *(ptbits
++) = 0;
1452 ::StretchDIBits( memdc
, 0, origin
, width
, height
,\
1453 0, 0, width
, height
, lpBits
, lpDIBh
, DIB_RGB_COLORS
, SRCCOPY
);
1455 // if numDIB = 1, lines below can also be used
1456 // hbitmap = CreateDIBitmap( hdc, &(lpDIBh->bmiHeader), CBM_INIT, lpBits, lpDIBh, DIB_RGB_COLORS );
1457 // The above line is equivalent to the following two lines.
1458 // hbitmap = ::CreateCompatibleBitmap( hdc, width, height );
1459 // ::SetDIBits( hdc, hbitmap, 0, height, lpBits, lpDIBh, DIB_RGB_COLORS);
1460 // or the following lines
1461 // hbitmap = ::CreateCompatibleBitmap( hdc, width, height );
1462 // HDC memdc = ::CreateCompatibleDC( hdc );
1463 // ::SelectObject( memdc, hbitmap);
1464 // ::SetDIBitsToDevice( memdc, 0, 0, width, height,
1465 // 0, 0, 0, height, (void *)lpBits, lpDIBh, DIB_RGB_COLORS);
1466 // ::SelectObject( memdc, 0 );
1467 // ::DeleteDC( memdc );
1469 bitmap
.SetHBITMAP( (WXHBITMAP
) hbitmap
);
1471 // similarly, created an mono-bitmap for the possible mask
1474 hbitmap
= ::CreateBitmap( (WORD
)width
, (WORD
)bmpHeight
, 1, 1, NULL
);
1475 ::SelectObject( memdc
, hbitmap
);
1476 if( numDIB
== 1 ) height
= bmpHeight
;
1477 else height
= sizeLimit
/bytePerLine
;
1478 lpDIBh
->bmiHeader
.biHeight
= (DWORD
)(-height
);
1479 lpDIBh
->bmiHeader
.biSizeImage
= bytePerLine
*height
;
1481 unsigned char r
= GetMaskRed();
1482 unsigned char g
= GetMaskGreen();
1483 unsigned char b
= GetMaskBlue();
1484 unsigned char zero
= 0, one
= 255;
1486 for( n
=0; n
<numDIB
; n
++ )
1488 if( numDIB
> 1 && n
== numDIB
- 1 && hRemain
> 0 )
1490 // redefine height and size of the (possibly) last smaller DIB
1491 // memory is not reallocated
1493 lpDIBh
->bmiHeader
.biHeight
= (DWORD
)(-height
);
1494 lpDIBh
->bmiHeader
.biSizeImage
= bytePerLine
*height
;
1497 for( int j
=0; j
<height
; j
++ )
1499 for( int i
=0; i
<width
; i
++ )
1501 if( (*(ptdata
++)!=r
) | (*(ptdata
++)!=g
) | (*(ptdata
++)!=b
) )
1514 for( i
=0; i
< padding
; i
++ ) *(ptbits
++) = zero
;
1516 ::StretchDIBits( memdc
, 0, origin
, width
, height
,\
1517 0, 0, width
, height
, lpBits
, lpDIBh
, DIB_RGB_COLORS
, SRCCOPY
);
1520 // create a wxMask object
1521 wxMask
*mask
= new wxMask();
1522 mask
->SetMaskBitmap( (WXHBITMAP
) hbitmap
);
1523 bitmap
.SetMask( mask
);
1524 // It will be deleted when the wxBitmap object is deleted (as of 01/1999)
1525 /* The following can also be used but is slow to run
1526 wxColour colour( GetMaskRed(), GetMaskGreen(), GetMaskBlue());
1527 wxMask *mask = new wxMask( bitmap, colour );
1528 bitmap.SetMask( mask );
1532 // free allocated resources
1533 ::SelectObject( memdc
, 0 );
1534 ::DeleteDC( memdc
);
1535 ::ReleaseDC(NULL
, hdc
);
1539 // check the wxBitmap object
1540 if( bitmap
.GetHBITMAP() )
1541 bitmap
.SetOk( TRUE
);
1543 bitmap
.SetOk( FALSE
);
1548 wxImage::wxImage( const wxBitmap
&bitmap
)
1553 wxFAIL_MSG( "invalid bitmap" );
1557 // create an wxImage object
1558 int width
= bitmap
.GetWidth();
1559 int height
= bitmap
.GetHeight();
1560 Create( width
, height
);
1561 unsigned char *data
= GetData();
1564 wxFAIL_MSG( "could not allocate data for image" );
1568 // calc the number of bytes per scanline and padding in the DIB
1569 int bytePerLine
= width
*3;
1570 int sizeDWORD
= sizeof( DWORD
);
1571 div_t lineBoundary
= div( bytePerLine
, sizeDWORD
);
1573 if( lineBoundary
.rem
> 0 )
1575 padding
= sizeDWORD
- lineBoundary
.rem
;
1576 bytePerLine
+= padding
;
1579 // create a DIB header
1580 int headersize
= sizeof(BITMAPINFOHEADER
);
1581 LPBITMAPINFO lpDIBh
= (BITMAPINFO
*) malloc( headersize
);
1584 wxFAIL_MSG( "could not allocate data for DIB header" );
1588 // Fill in the DIB header
1589 lpDIBh
->bmiHeader
.biSize
= headersize
;
1590 lpDIBh
->bmiHeader
.biWidth
= width
;
1591 lpDIBh
->bmiHeader
.biHeight
= -height
;
1592 lpDIBh
->bmiHeader
.biSizeImage
= bytePerLine
* height
;
1593 lpDIBh
->bmiHeader
.biPlanes
= 1;
1594 lpDIBh
->bmiHeader
.biBitCount
= 24;
1595 lpDIBh
->bmiHeader
.biCompression
= BI_RGB
;
1596 lpDIBh
->bmiHeader
.biClrUsed
= 0;
1597 // These seem not really needed for our purpose here.
1598 lpDIBh
->bmiHeader
.biClrImportant
= 0;
1599 lpDIBh
->bmiHeader
.biXPelsPerMeter
= 0;
1600 lpDIBh
->bmiHeader
.biYPelsPerMeter
= 0;
1601 // memory for DIB data
1602 unsigned char *lpBits
;
1603 lpBits
= (unsigned char *) malloc( lpDIBh
->bmiHeader
.biSizeImage
);
1606 wxFAIL_MSG( "could not allocate data for DIB" );
1612 // copy data from the device-dependent bitmap to the DIB
1613 HDC hdc
= ::GetDC(NULL
);
1615 hbitmap
= (HBITMAP
) bitmap
.GetHBITMAP();
1616 ::GetDIBits( hdc
, hbitmap
, 0, height
, lpBits
, lpDIBh
, DIB_RGB_COLORS
);
1618 // copy DIB data into the wxImage object
1620 unsigned char *ptdata
= data
;
1621 unsigned char *ptbits
= lpBits
;
1622 for( i
=0; i
<height
; i
++ )
1624 for( j
=0; j
<width
; j
++ )
1626 *(ptdata
++) = *(ptbits
+2);
1627 *(ptdata
++) = *(ptbits
+1);
1628 *(ptdata
++) = *(ptbits
);
1634 // similarly, set data according to the possible mask bitmap
1635 if( bitmap
.GetMask() && bitmap
.GetMask()->GetMaskBitmap() )
1637 hbitmap
= (HBITMAP
) bitmap
.GetMask()->GetMaskBitmap();
1638 // memory DC created, color set, data copied, and memory DC deleted
1639 HDC memdc
= ::CreateCompatibleDC( hdc
);
1640 ::SetTextColor( memdc
, RGB( 0, 0, 0 ) );
1641 ::SetBkColor( memdc
, RGB( 255, 255, 255 ) );
1642 ::GetDIBits( memdc
, hbitmap
, 0, height
, lpBits
, lpDIBh
, DIB_RGB_COLORS
);
1643 ::DeleteDC( memdc
);
1644 // background color set to RGB(16,16,16) in consistent with wxGTK
1645 unsigned char r
=16, g
=16, b
=16;
1648 for( i
=0; i
<height
; i
++ )
1650 for( j
=0; j
<width
; j
++ )
1664 SetMaskColour( r
, g
, b
);
1671 // free allocated resources
1672 ::ReleaseDC(NULL
, hdc
);
1681 #include "gtk/gtk.h"
1682 #include "gdk/gdk.h"
1683 #include "gdk/gdkx.h"
1685 wxBitmap
wxImage::ConvertToBitmap() const
1689 wxCHECK_MSG( Ok(), bitmap
, "invalid image" );
1691 int width
= GetWidth();
1692 int height
= GetHeight();
1694 bitmap
.SetHeight( height
);
1695 bitmap
.SetWidth( width
);
1699 GdkImage
*data_image
=
1700 gdk_image_new( GDK_IMAGE_FASTEST
, gdk_visual_get_system(), width
, height
);
1702 bitmap
.SetPixmap( gdk_pixmap_new( (GdkWindow
*)&gdk_root_parent
, width
, height
, -1 ) );
1706 GdkImage
*mask_image
= (GdkImage
*) NULL
;
1710 unsigned char *mask_data
= (unsigned char*)malloc( ((width
>> 3)+8) * height
);
1712 mask_image
= gdk_image_new_bitmap( gdk_visual_get_system(), mask_data
, width
, height
);
1714 wxMask
*mask
= new wxMask();
1715 mask
->m_bitmap
= gdk_pixmap_new( (GdkWindow
*)&gdk_root_parent
, width
, height
, 1 );
1717 bitmap
.SetMask( mask
);
1722 GdkVisual
*visual
= gdk_window_get_visual( bitmap
.GetPixmap() );
1723 if (visual
== NULL
) visual
= gdk_window_get_visual( (GdkWindow
*) &gdk_root_parent
);
1724 int bpp
= visual
->depth
;
1726 bitmap
.SetDepth( bpp
);
1728 if ((bpp
== 16) && (visual
->red_mask
!= 0xf800)) bpp
= 15;
1729 if (bpp
< 8) bpp
= 8;
1733 enum byte_order
{ RGB
, RBG
, BRG
, BGR
, GRB
, GBR
};
1734 byte_order b_o
= RGB
;
1738 GdkVisual
*visual
= gdk_visual_get_system();
1739 if ((visual
->red_mask
> visual
->green_mask
) && (visual
->green_mask
> visual
->blue_mask
)) b_o
= RGB
;
1740 else if ((visual
->red_mask
> visual
->blue_mask
) && (visual
->blue_mask
> visual
->green_mask
)) b_o
= RGB
;
1741 else if ((visual
->blue_mask
> visual
->red_mask
) && (visual
->red_mask
> visual
->green_mask
)) b_o
= BRG
;
1742 else if ((visual
->blue_mask
> visual
->green_mask
) && (visual
->green_mask
> visual
->red_mask
)) b_o
= BGR
;
1743 else if ((visual
->green_mask
> visual
->red_mask
) && (visual
->red_mask
> visual
->blue_mask
)) b_o
= GRB
;
1744 else if ((visual
->green_mask
> visual
->blue_mask
) && (visual
->blue_mask
> visual
->red_mask
)) b_o
= GBR
;
1747 int r_mask
= GetMaskRed();
1748 int g_mask
= GetMaskGreen();
1749 int b_mask
= GetMaskBlue();
1751 unsigned char* data
= GetData();
1754 for (int y
= 0; y
< height
; y
++)
1756 for (int x
= 0; x
< width
; x
++)
1758 int r
= data
[index
];
1760 int g
= data
[index
];
1762 int b
= data
[index
];
1767 if ((r
== r_mask
) && (b
== b_mask
) && (g
== g_mask
))
1768 gdk_image_put_pixel( mask_image
, x
, y
, 1 );
1770 gdk_image_put_pixel( mask_image
, x
, y
, 0 );
1775 if ((r
== r_mask
) && (b
== b_mask
) && (g
== g_mask
))
1776 gdk_image_put_pixel( mask_image
, x
, y
, 1 );
1778 gdk_image_put_pixel( mask_image
, x
, y
, 0 );
1786 if (wxTheApp
->m_colorCube
)
1788 pixel
= wxTheApp
->m_colorCube
[ ((r
& 0xf8) << 7) + ((g
& 0xf8) << 2) + ((b
& 0xf8) >> 3) ];
1792 GdkColormap
*cmap
= gtk_widget_get_default_colormap();
1793 GdkColor
*colors
= cmap
->colors
;
1794 int max
= 3 * (65536);
1796 for (int i
= 0; i
< cmap
->size
; i
++)
1798 int rdiff
= (r
<< 8) - colors
[i
].red
;
1799 int gdiff
= (g
<< 8) - colors
[i
].green
;
1800 int bdiff
= (b
<< 8) - colors
[i
].blue
;
1801 int sum
= ABS (rdiff
) + ABS (gdiff
) + ABS (bdiff
);
1802 if (sum
< max
) { pixel
= i
; max
= sum
; }
1806 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
1812 guint32 pixel
= ((r
& 0xf8) << 7) | ((g
& 0xf8) << 2) | ((b
& 0xf8) >> 3);
1813 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
1818 guint32 pixel
= ((r
& 0xf8) << 8) | ((g
& 0xfc) << 3) | ((b
& 0xf8) >> 3);
1819 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
1828 case RGB
: pixel
= (r
<< 16) | (g
<< 8) | b
; break;
1829 case RBG
: pixel
= (r
<< 16) | (b
<< 8) | g
; break;
1830 case BRG
: pixel
= (b
<< 16) | (r
<< 8) | g
; break;
1831 case BGR
: pixel
= (b
<< 16) | (g
<< 8) | r
; break;
1832 case GRB
: pixel
= (g
<< 16) | (r
<< 8) | b
; break;
1833 case GBR
: pixel
= (g
<< 16) | (b
<< 8) | r
; break;
1835 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
1844 GdkGC
*data_gc
= gdk_gc_new( bitmap
.GetPixmap() );
1846 gdk_draw_image( bitmap
.GetPixmap(), data_gc
, data_image
, 0, 0, 0, 0, width
, height
);
1848 gdk_image_destroy( data_image
);
1849 gdk_gc_unref( data_gc
);
1855 GdkGC
*mask_gc
= gdk_gc_new( bitmap
.GetMask()->GetBitmap() );
1857 gdk_draw_image( bitmap
.GetMask()->GetBitmap(), mask_gc
, mask_image
, 0, 0, 0, 0, width
, height
);
1859 gdk_image_destroy( mask_image
);
1860 gdk_gc_unref( mask_gc
);
1866 wxImage::wxImage( const wxBitmap
&bitmap
)
1868 wxCHECK_RET( bitmap
.Ok(), "invalid bitmap" );
1870 GdkImage
*gdk_image
= gdk_image_get( bitmap
.GetPixmap(),
1872 bitmap
.GetWidth(), bitmap
.GetHeight() );
1874 wxCHECK_RET( gdk_image
, "couldn't create image" );
1876 Create( bitmap
.GetWidth(), bitmap
.GetHeight() );
1877 char unsigned *data
= GetData();
1881 gdk_image_destroy( gdk_image
);
1882 wxFAIL_MSG( "couldn't create image" );
1886 GdkImage
*gdk_image_mask
= (GdkImage
*) NULL
;
1887 if (bitmap
.GetMask())
1889 gdk_image_mask
= gdk_image_get( bitmap
.GetMask()->GetBitmap(),
1891 bitmap
.GetWidth(), bitmap
.GetHeight() );
1893 SetMaskColour( 16, 16, 16 ); // anything unlikely and dividable
1896 GdkVisual
*visual
= gdk_window_get_visual( bitmap
.GetPixmap() );
1897 if (visual
== NULL
) visual
= gdk_window_get_visual( (GdkWindow
*) &gdk_root_parent
);
1898 int bpp
= visual
->depth
;
1899 if ((bpp
== 16) && (visual
->red_mask
!= 0xf800)) bpp
= 15;
1901 GdkColormap
*cmap
= gtk_widget_get_default_colormap();
1904 for (int j
= 0; j
< bitmap
.GetHeight(); j
++)
1906 for (int i
= 0; i
< bitmap
.GetWidth(); i
++)
1908 int pixel
= gdk_image_get_pixel( gdk_image
, i
, j
);
1911 data
[pos
] = cmap
->colors
[pixel
].red
>> 8;
1912 data
[pos
+1] = cmap
->colors
[pixel
].green
>> 8;
1913 data
[pos
+2] = cmap
->colors
[pixel
].blue
>> 8;
1914 } else if (bpp
== 15)
1916 data
[pos
] = (pixel
>> 7) & 0xf8;
1917 data
[pos
+1] = (pixel
>> 2) & 0xf8;
1918 data
[pos
+2] = (pixel
<< 3) & 0xf8;
1919 } else if (bpp
== 16)
1921 data
[pos
] = (pixel
>> 8) & 0xf8;
1922 data
[pos
+1] = (pixel
>> 3) & 0xfc;
1923 data
[pos
+2] = (pixel
<< 3) & 0xf8;
1926 data
[pos
] = (pixel
>> 16) & 0xff;
1927 data
[pos
+1] = (pixel
>> 8) & 0xff;
1928 data
[pos
+2] = pixel
& 0xff;
1933 int mask_pixel
= gdk_image_get_pixel( gdk_image_mask
, i
, j
);
1934 if (mask_pixel
== 0)
1946 gdk_image_destroy( gdk_image
);
1947 if (gdk_image_mask
) gdk_image_destroy( gdk_image_mask
);
1955 #include "wx/utils.h"
1958 wxBitmap
wxImage::ConvertToBitmap() const
1962 wxCHECK_MSG( Ok(), bitmap
, "invalid image" );
1964 int width
= GetWidth();
1965 int height
= GetHeight();
1967 bitmap
.SetHeight( height
);
1968 bitmap
.SetWidth( width
);
1970 Display
*dpy
= (Display
*) wxGetDisplay();
1971 Visual
* vis
= DefaultVisual( dpy
, DefaultScreen( dpy
) );
1972 int bpp
= DefaultDepth( dpy
, DefaultScreen( dpy
) );
1976 XImage
*data_image
= XCreateImage( dpy
, vis
, bpp
, ZPixmap
, 0, 0, width
, height
, 32, 0 );
1977 data_image
->data
= new char[ data_image
->bytes_per_line
* data_image
->height
];
1979 bitmap
.Create( width
, height
, bpp
);
1984 GdkImage *mask_image = (GdkImage*) NULL;
1988 unsigned char *mask_data = (unsigned char*)malloc( ((width >> 3)+8) * height );
1990 mask_image = gdk_image_new_bitmap( gdk_visual_get_system(), mask_data, width, height );
1992 wxMask *mask = new wxMask();
1993 mask->m_bitmap = gdk_pixmap_new( (GdkWindow*)&gdk_root_parent, width, height, 1 );
1995 bitmap.SetMask( mask );
1999 // Retrieve depth info
2001 XVisualInfo vinfo_template
;
2004 vinfo_template
.visual
= vis
;
2005 vinfo_template
.visualid
= XVisualIDFromVisual( vis
);
2006 vinfo_template
.depth
= bpp
;
2009 vi
= XGetVisualInfo( dpy
, VisualIDMask
|VisualDepthMask
, &vinfo_template
, &nitem
);
2013 printf("no visual.\n" );
2014 return wxNullBitmap
;
2019 if ((bpp
== 16) && (vi
->red_mask
!= 0xf800)) bpp
= 15;
2020 if (bpp
< 8) bpp
= 8;
2024 enum byte_order
{ RGB
, RBG
, BRG
, BGR
, GRB
, GBR
};
2025 byte_order b_o
= RGB
;
2029 if ((vi
->red_mask
> vi
->green_mask
) && (vi
->green_mask
> vi
->blue_mask
)) b_o
= RGB
;
2030 else if ((vi
->red_mask
> vi
->blue_mask
) && (vi
->blue_mask
> vi
->green_mask
)) b_o
= RGB
;
2031 else if ((vi
->blue_mask
> vi
->red_mask
) && (vi
->red_mask
> vi
->green_mask
)) b_o
= BRG
;
2032 else if ((vi
->blue_mask
> vi
->green_mask
) && (vi
->green_mask
> vi
->red_mask
)) b_o
= BGR
;
2033 else if ((vi
->green_mask
> vi
->red_mask
) && (vi
->red_mask
> vi
->blue_mask
)) b_o
= GRB
;
2034 else if ((vi
->green_mask
> vi
->blue_mask
) && (vi
->blue_mask
> vi
->red_mask
)) b_o
= GBR
;
2038 int r_mask = GetMaskRed();
2039 int g_mask = GetMaskGreen();
2040 int b_mask = GetMaskBlue();
2046 Colormap cmap
= (Colormap
) wxTheApp
->GetMainColormap( dpy
);
2048 for (int i
= 0; i
< 256; i
++) colors
[i
].pixel
= i
;
2049 XQueryColors( dpy
, cmap
, colors
, 256 );
2052 unsigned char* data
= GetData();
2055 for (int y
= 0; y
< height
; y
++)
2057 for (int x
= 0; x
< width
; x
++)
2059 int r
= data
[index
];
2061 int g
= data
[index
];
2063 int b
= data
[index
];
2069 if ((r == r_mask) && (b == b_mask) && (g == g_mask))
2070 gdk_image_put_pixel( mask_image, x, y, 1 );
2072 gdk_image_put_pixel( mask_image, x, y, 0 );
2082 if (wxTheApp->m_colorCube)
2084 pixel = wxTheApp->m_colorCube
2085 [ ((r & 0xf8) << 7) + ((g & 0xf8) << 2) + ((b & 0xf8) >> 3) ];
2090 int max
= 3 * (65536);
2091 for (int i
= 0; i
< 256; i
++)
2093 int rdiff
= (r
<< 8) - colors
[i
].red
;
2094 int gdiff
= (g
<< 8) - colors
[i
].green
;
2095 int bdiff
= (b
<< 8) - colors
[i
].blue
;
2096 int sum
= abs (rdiff
) + abs (gdiff
) + abs (bdiff
);
2097 if (sum
< max
) { pixel
= i
; max
= sum
; }
2102 XPutPixel( data_image
, x
, y
, pixel
);
2107 int pixel
= ((r
& 0xf8) << 7) | ((g
& 0xf8) << 2) | ((b
& 0xf8) >> 3);
2108 XPutPixel( data_image
, x
, y
, pixel
);
2113 int pixel
= ((r
& 0xf8) << 8) | ((g
& 0xfc) << 3) | ((b
& 0xf8) >> 3);
2114 XPutPixel( data_image
, x
, y
, pixel
);
2123 case RGB
: pixel
= (r
<< 16) | (g
<< 8) | b
; break;
2124 case RBG
: pixel
= (r
<< 16) | (b
<< 8) | g
; break;
2125 case BRG
: pixel
= (b
<< 16) | (r
<< 8) | g
; break;
2126 case BGR
: pixel
= (b
<< 16) | (g
<< 8) | r
; break;
2127 case GRB
: pixel
= (g
<< 16) | (r
<< 8) | b
; break;
2128 case GBR
: pixel
= (g
<< 16) | (b
<< 8) | r
; break;
2130 XPutPixel( data_image
, x
, y
, pixel
);
2140 gcvalues
.foreground
= BlackPixel( dpy
, DefaultScreen( dpy
) );
2141 GC gc
= XCreateGC( dpy
, RootWindow ( dpy
, DefaultScreen(dpy
) ), GCForeground
, &gcvalues
);
2142 XPutImage( dpy
, (Drawable
)bitmap
.GetPixmap(), gc
, data_image
, 0, 0, 0, 0, width
, height
);
2144 XDestroyImage( data_image
);
2152 GdkGC *mask_gc = gdk_gc_new( bitmap.GetMask()->GetBitmap() );
2154 gdk_draw_image( bitmap.GetMask()->GetBitmap(), mask_gc, mask_image, 0, 0, 0, 0, width, height );
2156 gdk_image_destroy( mask_image );
2157 gdk_gc_unref( mask_gc );
2164 wxImage::wxImage( const wxBitmap
&bitmap
)
2166 wxCHECK_RET( bitmap
.Ok(), "invalid bitmap" );
2168 Display
*dpy
= (Display
*) wxGetDisplay();
2169 Visual
* vis
= DefaultVisual( dpy
, DefaultScreen( dpy
) );
2170 int bpp
= DefaultDepth( dpy
, DefaultScreen( dpy
) );
2172 XImage
*ximage
= XGetImage( dpy
,
2173 (Drawable
)bitmap
.GetPixmap(),
2175 bitmap
.GetWidth(), bitmap
.GetHeight(),
2176 AllPlanes
, ZPixmap
);
2178 wxCHECK_RET( ximage
, "couldn't create image" );
2180 Create( bitmap
.GetWidth(), bitmap
.GetHeight() );
2181 char unsigned *data
= GetData();
2185 XDestroyImage( ximage
);
2186 wxFAIL_MSG( "couldn't create image" );
2191 GdkImage *gdk_image_mask = (GdkImage*) NULL;
2192 if (bitmap.GetMask())
2194 gdk_image_mask = gdk_image_get( bitmap.GetMask()->GetBitmap(),
2196 bitmap.GetWidth(), bitmap.GetHeight() );
2198 SetMaskColour( 16, 16, 16 ); // anything unlikely and dividable
2202 // Retrieve depth info
2204 XVisualInfo vinfo_template
;
2207 vinfo_template
.visual
= vis
;
2208 vinfo_template
.visualid
= XVisualIDFromVisual( vis
);
2209 vinfo_template
.depth
= bpp
;
2212 vi
= XGetVisualInfo( dpy
, VisualIDMask
|VisualDepthMask
, &vinfo_template
, &nitem
);
2216 printf("no visual.\n" );
2220 if ((bpp
== 16) && (vi
->red_mask
!= 0xf800)) bpp
= 15;
2227 Colormap cmap
= (Colormap
)wxTheApp
->GetMainColormap( dpy
);
2229 for (int i
= 0; i
< 256; i
++) colors
[i
].pixel
= i
;
2230 XQueryColors( dpy
, cmap
, colors
, 256 );
2234 for (int j
= 0; j
< bitmap
.GetHeight(); j
++)
2236 for (int i
= 0; i
< bitmap
.GetWidth(); i
++)
2238 int pixel
= XGetPixel( ximage
, i
, j
);
2241 data
[pos
] = colors
[pixel
].red
>> 8;
2242 data
[pos
+1] = colors
[pixel
].green
>> 8;
2243 data
[pos
+2] = colors
[pixel
].blue
>> 8;
2244 } else if (bpp
== 15)
2246 data
[pos
] = (pixel
>> 7) & 0xf8;
2247 data
[pos
+1] = (pixel
>> 2) & 0xf8;
2248 data
[pos
+2] = (pixel
<< 3) & 0xf8;
2249 } else if (bpp
== 16)
2251 data
[pos
] = (pixel
>> 8) & 0xf8;
2252 data
[pos
+1] = (pixel
>> 3) & 0xfc;
2253 data
[pos
+2] = (pixel
<< 3) & 0xf8;
2256 data
[pos
] = (pixel
>> 16) & 0xff;
2257 data
[pos
+1] = (pixel
>> 8) & 0xff;
2258 data
[pos
+2] = pixel
& 0xff;
2264 int mask_pixel = gdk_image_get_pixel( gdk_image_mask, i, j );
2265 if (mask_pixel == 0)
2278 XDestroyImage( ximage
);
2280 if (gdk_image_mask) gdk_image_destroy( gdk_image_mask );