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"
37 #include "wx/module.h"
49 //-----------------------------------------------------------------------------
51 //-----------------------------------------------------------------------------
53 class wxImageRefData
: public wxObjectRefData
58 ~wxImageRefData(void);
62 unsigned char *m_data
;
64 unsigned char m_maskRed
,m_maskGreen
,m_maskBlue
;
68 wxImageRefData::wxImageRefData(void)
72 m_data
= (unsigned char*) NULL
;
80 wxImageRefData::~wxImageRefData(void)
82 if (m_data
) free( m_data
);
85 wxList
wxImage::sm_handlers
;
87 //-----------------------------------------------------------------------------
89 #define M_IMGDATA ((wxImageRefData *)m_refData)
91 #if !USE_SHARED_LIBRARIES
92 IMPLEMENT_DYNAMIC_CLASS(wxImage
, wxObject
)
99 wxImage::wxImage( int width
, int height
)
101 Create( width
, height
);
104 wxImage::wxImage( const wxString
& name
, long type
)
106 LoadFile( name
, type
);
110 wxImage::wxImage( wxInputStream
& stream
, long type
)
112 LoadFile( stream
, type
);
114 #endif // wxUSE_STREAMS
116 wxImage::wxImage( const wxImage
& image
)
121 wxImage::wxImage( const wxImage
* image
)
123 if (image
) Ref(*image
);
126 void wxImage::Create( int width
, int height
)
128 m_refData
= new wxImageRefData();
130 M_IMGDATA
->m_data
= (unsigned char *) malloc( width
*height
*3 );
131 if (M_IMGDATA
->m_data
)
133 for (int l
= 0; l
< width
*height
*3; l
++) M_IMGDATA
->m_data
[l
] = 0;
135 M_IMGDATA
->m_width
= width
;
136 M_IMGDATA
->m_height
= height
;
137 M_IMGDATA
->m_ok
= TRUE
;
145 void wxImage::Destroy()
150 wxImage
wxImage::Scale( int width
, int height
)
154 wxCHECK_MSG( Ok(), image
, "invlaid image" );
156 wxCHECK_MSG( (width
> 0) && (height
> 0), image
, "invalid image size" );
158 image
.Create( width
, height
);
160 char unsigned *data
= image
.GetData();
162 wxCHECK_MSG( data
, image
, "unable to create image" );
164 if (M_IMGDATA
->m_hasMask
)
165 image
.SetMaskColour( M_IMGDATA
->m_maskRed
, M_IMGDATA
->m_maskGreen
, M_IMGDATA
->m_maskBlue
);
167 long old_height
= M_IMGDATA
->m_height
;
168 long old_width
= M_IMGDATA
->m_width
;
170 char unsigned *source_data
= M_IMGDATA
->m_data
;
171 char unsigned *target_data
= data
;
173 for (long j
= 0; j
< height
; j
++)
175 long y_offset
= (j
* old_height
/ height
) * old_width
;
177 for (long i
= 0; i
< width
; i
++)
180 source_data
+ 3*(y_offset
+ ((i
* old_width
)/ width
)),
189 void wxImage::SetRGB( int x
, int y
, unsigned char r
, unsigned char g
, unsigned char b
)
191 wxCHECK_RET( Ok(), "invalid image" );
193 int w
= M_IMGDATA
->m_width
;
194 int h
= M_IMGDATA
->m_height
;
196 wxCHECK_RET( (x
>=0) && (y
>=0) && (x
<w
) && (y
<h
), "invalid image index" );
198 long pos
= (y
* w
+ x
) * 3;
200 M_IMGDATA
->m_data
[ pos
] = r
;
201 M_IMGDATA
->m_data
[ pos
+1 ] = g
;
202 M_IMGDATA
->m_data
[ pos
+2 ] = b
;
205 unsigned char wxImage::GetRed( int x
, int y
)
207 wxCHECK_MSG( Ok(), 0, "invalid image" );
209 int w
= M_IMGDATA
->m_width
;
210 int h
= M_IMGDATA
->m_height
;
212 wxCHECK_MSG( (x
>=0) && (y
>=0) && (x
<w
) && (y
<h
), 0, "invalid image index" );
214 long pos
= (y
* w
+ x
) * 3;
216 return M_IMGDATA
->m_data
[pos
];
219 unsigned char wxImage::GetGreen( int x
, int y
)
221 wxCHECK_MSG( Ok(), 0, "invalid image" );
223 int w
= M_IMGDATA
->m_width
;
224 int h
= M_IMGDATA
->m_height
;
226 wxCHECK_MSG( (x
>=0) && (y
>=0) && (x
<w
) && (y
<h
), 0, "invalid image index" );
228 long pos
= (y
* w
+ x
) * 3;
230 return M_IMGDATA
->m_data
[pos
+1];
233 unsigned char wxImage::GetBlue( int x
, int y
)
235 wxCHECK_MSG( Ok(), 0, "invalid image" );
237 int w
= M_IMGDATA
->m_width
;
238 int h
= M_IMGDATA
->m_height
;
240 wxCHECK_MSG( (x
>=0) && (y
>=0) && (x
<w
) && (y
<h
), 0, "invalid image index" );
242 long pos
= (y
* w
+ x
) * 3;
244 return M_IMGDATA
->m_data
[pos
+2];
247 bool wxImage::Ok() const
249 return (M_IMGDATA
&& M_IMGDATA
->m_ok
);
252 char unsigned *wxImage::GetData() const
254 wxCHECK_MSG( Ok(), (char unsigned *)NULL
, "invalid image" );
256 return M_IMGDATA
->m_data
;
259 void wxImage::SetData( char unsigned *WXUNUSED(data
) )
261 wxCHECK_RET( Ok(), "invalid image" );
264 void wxImage::SetMaskColour( unsigned char r
, unsigned char g
, unsigned char b
)
266 wxCHECK_RET( Ok(), "invalid image" );
268 M_IMGDATA
->m_maskRed
= r
;
269 M_IMGDATA
->m_maskGreen
= g
;
270 M_IMGDATA
->m_maskBlue
= b
;
271 M_IMGDATA
->m_hasMask
= TRUE
;
274 unsigned char wxImage::GetMaskRed() const
276 wxCHECK_MSG( Ok(), 0, "invalid image" );
278 return M_IMGDATA
->m_maskRed
;
281 unsigned char wxImage::GetMaskGreen() const
283 wxCHECK_MSG( Ok(), 0, "invalid image" );
285 return M_IMGDATA
->m_maskGreen
;
288 unsigned char wxImage::GetMaskBlue() const
290 wxCHECK_MSG( Ok(), 0, "invalid image" );
292 return M_IMGDATA
->m_maskBlue
;
295 void wxImage::SetMask( bool mask
)
297 wxCHECK_RET( Ok(), "invalid image" );
299 M_IMGDATA
->m_hasMask
= mask
;
302 bool wxImage::HasMask() const
304 wxCHECK_MSG( Ok(), FALSE
, "invalid image" );
306 return M_IMGDATA
->m_hasMask
;
309 int wxImage::GetWidth() const
311 wxCHECK_MSG( Ok(), 0, "invalid image" );
313 return M_IMGDATA
->m_width
;
316 int wxImage::GetHeight() const
318 wxCHECK_MSG( Ok(), 0, "invalid image" );
320 return M_IMGDATA
->m_height
;
323 bool wxImage::LoadFile( const wxString
& filename
, long type
)
326 if (wxFileExists(filename
))
328 wxFileInputStream
stream(filename
);
329 return LoadFile(stream
, type
);
333 wxLogError( "Can't load image from file '%s': file does not exist.", filename
.c_str() );
337 #else // !wxUSE_STREAMS
339 #endif // wxUSE_STREAMS
342 bool wxImage::SaveFile( const wxString
& filename
, int type
)
345 wxFileOutputStream
stream(filename
);
347 if ( stream
.LastError() == wxStream_NOERROR
)
348 return SaveFile(stream
, type
);
350 #endif // wxUSE_STREAMS
355 bool wxImage::LoadFile( wxInputStream
& stream
, long type
)
359 m_refData
= new wxImageRefData
;
361 wxImageHandler
*handler
= FindHandler(type
);
365 wxLogWarning( "No image handler for type %d defined.", type
);
370 return handler
->LoadFile( this, stream
);
373 bool wxImage::SaveFile( wxOutputStream
& stream
, int type
)
375 wxCHECK_MSG( Ok(), FALSE
, "invalid image" );
377 wxImageHandler
*handler
= FindHandler(type
);
381 wxLogWarning( "No image handler for type %d defined.", type
);
386 return handler
->SaveFile( this, stream
);
388 #endif // wxUSE_STREAMS
390 void wxImage::AddHandler( wxImageHandler
*handler
)
392 // make sure that the memory will be freed at the program end
393 sm_handlers
.DeleteContents(TRUE
);
395 sm_handlers
.Append( handler
);
398 void wxImage::InsertHandler( wxImageHandler
*handler
)
400 // make sure that the memory will be freed at the program end
401 sm_handlers
.DeleteContents(TRUE
);
403 sm_handlers
.Insert( handler
);
406 bool wxImage::RemoveHandler( const wxString
& name
)
408 wxImageHandler
*handler
= FindHandler(name
);
411 sm_handlers
.DeleteObject(handler
);
418 wxImageHandler
*wxImage::FindHandler( const wxString
& name
)
420 wxNode
*node
= sm_handlers
.First();
423 wxImageHandler
*handler
= (wxImageHandler
*)node
->Data();
424 if (handler
->GetName().Cmp(name
) == 0) return handler
;
428 return (wxImageHandler
*)NULL
;
431 wxImageHandler
*wxImage::FindHandler( const wxString
& extension
, long bitmapType
)
433 wxNode
*node
= sm_handlers
.First();
436 wxImageHandler
*handler
= (wxImageHandler
*)node
->Data();
437 if ( (handler
->GetExtension().Cmp(extension
) == 0) &&
438 (bitmapType
== -1 || handler
->GetType() == bitmapType
) )
442 return (wxImageHandler
*)NULL
;
445 wxImageHandler
*wxImage::FindHandler( long bitmapType
)
447 wxNode
*node
= sm_handlers
.First();
450 wxImageHandler
*handler
= (wxImageHandler
*)node
->Data();
451 if (handler
->GetType() == bitmapType
) return handler
;
457 void wxImage::InitStandardHandlers()
459 AddHandler( new wxBMPHandler
);
461 AddHandler( new wxPNGHandler
);
465 void wxImage::CleanUpHandlers()
467 wxNode
*node
= sm_handlers
.First();
470 wxImageHandler
*handler
= (wxImageHandler
*)node
->Data();
471 wxNode
*next
= node
->Next();
478 //-----------------------------------------------------------------------------
480 //-----------------------------------------------------------------------------
482 #if !USE_SHARED_LIBRARIES
483 IMPLEMENT_DYNAMIC_CLASS(wxImageHandler
,wxObject
)
487 bool wxImageHandler::LoadFile( wxImage
*WXUNUSED(image
), wxInputStream
& WXUNUSED(stream
) )
492 bool wxImageHandler::SaveFile( wxImage
*WXUNUSED(image
), wxOutputStream
& WXUNUSED(stream
) )
496 #endif // wxUSE_STREAMS
498 //-----------------------------------------------------------------------------
500 //-----------------------------------------------------------------------------
504 #if !USE_SHARED_LIBRARIES
505 IMPLEMENT_DYNAMIC_CLASS(wxPNGHandler
,wxImageHandler
)
510 static void _PNG_stream_reader( png_structp png_ptr
, png_bytep data
, png_size_t length
)
512 ((wxInputStream
*) png_get_io_ptr( png_ptr
)) -> Read(data
, length
);
515 static void _PNG_stream_writer( png_structp png_ptr
, png_bytep data
, png_size_t length
)
517 ((wxOutputStream
*) png_get_io_ptr( png_ptr
)) -> Write(data
, length
);
520 bool wxPNGHandler::LoadFile( wxImage
*image
, wxInputStream
& stream
)
522 // VZ: as this function uses setjmp() the only fool proof error handling
523 // method is to use goto (setjmp is not really C++ dtors friendly...)
525 unsigned char **lines
= (unsigned char **) NULL
;
527 png_infop info_ptr
= (png_infop
) NULL
;
531 png_structp png_ptr
= png_create_read_struct( PNG_LIBPNG_VER_STRING
,
533 (png_error_ptr
) NULL
,
534 (png_error_ptr
) NULL
);
538 info_ptr
= png_create_info_struct( png_ptr
);
542 if (setjmp(png_ptr
->jmpbuf
))
545 if (info_ptr
->color_type
== PNG_COLOR_TYPE_RGB_ALPHA
)
548 png_set_read_fn( png_ptr
, &stream
, _PNG_stream_reader
);
550 png_uint_32 width
,height
;
551 int bit_depth
,color_type
,interlace_type
;
553 png_read_info( png_ptr
, info_ptr
);
554 png_get_IHDR( png_ptr
, info_ptr
, &width
, &height
, &bit_depth
, &color_type
, &interlace_type
, (int*) NULL
, (int*) NULL
);
556 if (color_type
== PNG_COLOR_TYPE_PALETTE
)
557 png_set_expand( png_ptr
);
559 png_set_strip_16( png_ptr
);
560 png_set_packing( png_ptr
);
561 if (png_get_valid( png_ptr
, info_ptr
, PNG_INFO_tRNS
))
562 png_set_expand( png_ptr
);
563 png_set_filler( png_ptr
, 0xff, PNG_FILLER_AFTER
);
565 image
->Create( width
, height
);
570 lines
= (unsigned char **)malloc( height
* sizeof(unsigned char *) );
574 for (i
= 0; i
< height
; i
++)
576 if ((lines
[i
] = (unsigned char *)malloc(width
* (sizeof(unsigned char) * 4))) == NULL
)
578 for ( unsigned int n
= 0; n
< i
; n
++ )
584 // loaded successfully!
587 png_read_image( png_ptr
, lines
);
588 png_destroy_read_struct( &png_ptr
, &info_ptr
, (png_infopp
) NULL
);
589 unsigned char *ptr
= image
->GetData();
590 if ((color_type
== PNG_COLOR_TYPE_GRAY
) ||
591 (color_type
== PNG_COLOR_TYPE_GRAY_ALPHA
))
593 for (unsigned int y
= 0; y
< height
; y
++)
595 unsigned char *ptr2
= lines
[y
];
596 for (unsigned int x
= 0; x
< width
; x
++)
598 unsigned char r
= *ptr2
++;
599 unsigned char a
= *ptr2
++;
618 for (unsigned int y
= 0; y
< height
; y
++)
620 unsigned char *ptr2
= lines
[y
];
621 for (unsigned int x
= 0; x
< width
; x
++)
623 unsigned char r
= *ptr2
++;
624 unsigned char g
= *ptr2
++;
625 unsigned char b
= *ptr2
++;
626 unsigned char a
= *ptr2
++;
636 if ((r
== 255) && (g
== 0) && (b
== 255)) r
= 254;
645 for ( unsigned int j
= 0; j
< height
; j
++ )
651 image
->SetMaskColour( 255, 0, 255 );
655 image
->SetMask( FALSE
);
662 wxLogError(_("Couldn't load a PNG image - probably file is corrupted."));
678 png_destroy_read_struct( &png_ptr
, &info_ptr
, (png_infopp
) NULL
);
682 png_destroy_read_struct( &png_ptr
, (png_infopp
) NULL
, (png_infopp
) NULL
);
688 bool wxPNGHandler::SaveFile( wxImage
*image
, wxOutputStream
& stream
)
691 png_structp png_ptr
= png_create_write_struct( PNG_LIBPNG_VER_STRING
, NULL
, NULL
, NULL
);
697 png_infop info_ptr
= png_create_info_struct(png_ptr
);
698 if (info_ptr
== NULL
)
700 png_destroy_write_struct( &png_ptr
, (png_infopp
)NULL
);
704 if (setjmp(png_ptr
->jmpbuf
))
706 png_destroy_write_struct( &png_ptr
, (png_infopp
)NULL
);
710 png_set_write_fn( png_ptr
, &stream
, _PNG_stream_writer
, NULL
);
712 png_set_IHDR( png_ptr
, info_ptr
, image
->GetWidth(), image
->GetHeight(), 8,
713 PNG_COLOR_TYPE_RGB_ALPHA
, PNG_INTERLACE_NONE
,
714 PNG_COMPRESSION_TYPE_BASE
, PNG_FILTER_TYPE_BASE
);
721 png_set_sBIT( png_ptr
, info_ptr
, &sig_bit
);
722 png_write_info( png_ptr
, info_ptr
);
723 png_set_shift( png_ptr
, &sig_bit
);
724 png_set_packing( png_ptr
);
726 unsigned char *data
= (unsigned char *)malloc( image
->GetWidth()*4 );
729 png_destroy_write_struct( &png_ptr
, (png_infopp
)NULL
);
733 for (int y
= 0; y
< image
->GetHeight(); y
++)
735 unsigned char *ptr
= image
->GetData() + (y
* image
->GetWidth() * 3);
736 for (int x
= 0; x
< image
->GetWidth(); x
++)
738 data
[(x
<< 2) + 0] = *ptr
++;
739 data
[(x
<< 2) + 1] = *ptr
++;
740 data
[(x
<< 2) + 2] = *ptr
++;
741 if ((data
[(x
<< 2) + 0] == image
->GetMaskRed()) &&
742 (data
[(x
<< 2) + 1] == image
->GetMaskGreen()) &&
743 (data
[(x
<< 2) + 2] == image
->GetMaskBlue()))
745 data
[(x
<< 2) + 3] = 0;
749 data
[(x
<< 2) + 3] = 255;
752 png_bytep row_ptr
= data
;
753 png_write_rows( png_ptr
, &row_ptr
, 1 );
757 png_write_end( png_ptr
, info_ptr
);
758 png_destroy_write_struct( &png_ptr
, (png_infopp
)&info_ptr
);
762 #endif // wxUSE_STREAMS
769 //-----------------------------------------------------------------------------
771 //-----------------------------------------------------------------------------
775 #if !USE_SHARED_LIBRARIES
776 IMPLEMENT_DYNAMIC_CLASS(wxJPEGHandler
,wxImageHandler
)
782 //------------- JPEG Data Source Manager
785 struct jpeg_source_mgr pub
; /* public fields */
787 JOCTET
* buffer
; /* start of buffer */
790 typedef my_source_mgr
* my_src_ptr
;
792 METHODDEF(void) my_init_source ( j_decompress_ptr cinfo
)
796 METHODDEF(boolean
) my_fill_input_buffer ( j_decompress_ptr cinfo
)
801 METHODDEF(void) my_skip_input_data ( j_decompress_ptr cinfo
, long num_bytes
)
803 my_src_ptr src
= (my_src_ptr
) cinfo
->src
;
805 src
->pub
.next_input_byte
+= (size_t) num_bytes
;
806 src
->pub
.bytes_in_buffer
-= (size_t) num_bytes
;
809 METHODDEF(void) my_term_source ( j_decompress_ptr cinfo
)
811 my_src_ptr src
= (my_src_ptr
) cinfo
->src
;
816 void jpeg_wxio_src( j_decompress_ptr cinfo
, wxInputStream
& infile
)
820 if (cinfo
->src
== NULL
) { /* first time for this JPEG object? */
821 cinfo
->src
= (struct jpeg_source_mgr
*)
822 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
) cinfo
, JPOOL_PERMANENT
,
823 sizeof(my_source_mgr
));
824 src
= (my_src_ptr
) cinfo
->src
;
826 src
= (my_src_ptr
) cinfo
->src
;
827 src
->pub
.bytes_in_buffer
= infile
.StreamSize(); /* forces fill_input_buffer on first read */
828 src
->buffer
= (JOCTET
*) malloc (infile
.StreamSize());
829 src
->pub
.next_input_byte
= src
->buffer
; /* until buffer loaded */
830 infile
.Read(src
->buffer
, infile
.StreamSize());
832 src
->pub
.init_source
= my_init_source
;
833 src
->pub
.fill_input_buffer
= my_fill_input_buffer
;
834 src
->pub
.skip_input_data
= my_skip_input_data
;
835 src
->pub
.resync_to_restart
= jpeg_resync_to_restart
; /* use default method */
836 src
->pub
.term_source
= my_term_source
;
841 bool wxJPEGHandler::LoadFile( wxImage
*image
, wxInputStream
& stream
)
843 struct jpeg_decompress_struct cinfo
;
844 struct jpeg_error_mgr jerr
;
850 cinfo
.err
= jpeg_std_error( &jerr
);
851 jpeg_create_decompress( &cinfo
);
852 jpeg_wxio_src( &cinfo
, stream
);
853 jpeg_read_header( &cinfo
, TRUE
);
854 cinfo
.out_color_space
= JCS_RGB
;
855 jpeg_start_decompress( &cinfo
);
857 image
->Create( cinfo
.image_width
, cinfo
.image_height
);
859 jpeg_finish_decompress( &cinfo
);
860 jpeg_destroy_decompress( &cinfo
);
863 image
->SetMask( FALSE
);
864 ptr
= image
->GetData();
865 stride
= cinfo
.output_width
* 3;
866 tempbuf
= (*cinfo
.mem
->alloc_sarray
)
867 ((j_common_ptr
) &cinfo
, JPOOL_IMAGE
, stride
, 1 );
869 while ( cinfo
.output_scanline
< cinfo
.output_height
) {
870 jpeg_read_scanlines( &cinfo
, tempbuf
, 1 );
871 memcpy( ptr
, tempbuf
[0], stride
);
874 jpeg_finish_decompress( &cinfo
);
875 jpeg_destroy_decompress( &cinfo
);
884 struct jpeg_destination_mgr pub
;
886 wxOutputStream
*stream
;
888 } my_destination_mgr
;
890 typedef my_destination_mgr
* my_dest_ptr
;
892 #define OUTPUT_BUF_SIZE 4096 /* choose an efficiently fwrite'able size */
894 METHODDEF(void) init_destination (j_compress_ptr cinfo
)
896 my_dest_ptr dest
= (my_dest_ptr
) cinfo
->dest
;
898 /* Allocate the output buffer --- it will be released when done with image */
899 dest
->buffer
= (JOCTET
*)
900 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
) cinfo
, JPOOL_IMAGE
,
901 OUTPUT_BUF_SIZE
* sizeof(JOCTET
));
902 dest
->pub
.next_output_byte
= dest
->buffer
;
903 dest
->pub
.free_in_buffer
= OUTPUT_BUF_SIZE
;
906 METHODDEF(boolean
) empty_output_buffer (j_compress_ptr cinfo
)
908 my_dest_ptr dest
= (my_dest_ptr
) cinfo
->dest
;
910 dest
->stream
->Write(dest
->buffer
, OUTPUT_BUF_SIZE
);
911 dest
->pub
.next_output_byte
= dest
->buffer
;
912 dest
->pub
.free_in_buffer
= OUTPUT_BUF_SIZE
;
916 METHODDEF(void) term_destination (j_compress_ptr cinfo
)
918 my_dest_ptr dest
= (my_dest_ptr
) cinfo
->dest
;
919 size_t datacount
= OUTPUT_BUF_SIZE
- dest
->pub
.free_in_buffer
;
920 /* Write any data remaining in the buffer */
922 dest
->stream
->Write(dest
->buffer
, datacount
);
925 GLOBAL(void) jpeg_wxio_dest (j_compress_ptr cinfo
, wxOutputStream
& outfile
)
929 if (cinfo
->dest
== NULL
) { /* first time for this JPEG object? */
930 cinfo
->dest
= (struct jpeg_destination_mgr
*)
931 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
) cinfo
, JPOOL_PERMANENT
,
932 sizeof(my_destination_mgr
));
935 dest
= (my_dest_ptr
) cinfo
->dest
;
936 dest
->pub
.init_destination
= init_destination
;
937 dest
->pub
.empty_output_buffer
= empty_output_buffer
;
938 dest
->pub
.term_destination
= term_destination
;
939 dest
->stream
= &outfile
;
944 bool wxJPEGHandler::SaveFile( wxImage
*image
, wxOutputStream
& stream
)
946 struct jpeg_compress_struct cinfo
;
947 struct jpeg_error_mgr jerr
;
948 JSAMPROW row_pointer
[1]; /* pointer to JSAMPLE row[s] */
949 JSAMPLE
*image_buffer
;
950 int stride
; /* physical row width in image buffer */
952 cinfo
.err
= jpeg_std_error(&jerr
);
953 jpeg_create_compress(&cinfo
);
954 jpeg_wxio_dest(&cinfo
, stream
);
956 cinfo
.image_width
= image
->GetWidth();
957 cinfo
.image_height
= image
->GetHeight();
958 cinfo
.input_components
= 3;
959 cinfo
.in_color_space
= JCS_RGB
;
960 jpeg_set_defaults(&cinfo
);
961 jpeg_start_compress(&cinfo
, TRUE
);
963 stride
= cinfo
.image_width
* 3; /* JSAMPLEs per row in image_buffer */
964 image_buffer
= image
->GetData();
965 while (cinfo
.next_scanline
< cinfo
.image_height
) {
966 row_pointer
[0] = &image_buffer
[cinfo
.next_scanline
* stride
];
967 jpeg_write_scanlines( &cinfo
, row_pointer
, 1 );
969 jpeg_finish_compress(&cinfo
);
970 jpeg_destroy_compress(&cinfo
);
974 #endif // wxUSE_STREAMS
982 //-----------------------------------------------------------------------------
984 //-----------------------------------------------------------------------------
986 #if !USE_SHARED_LIBRARIES
987 IMPLEMENT_DYNAMIC_CLASS(wxBMPHandler
,wxImageHandler
)
991 bool wxBMPHandler::LoadFile( wxImage
*image
, wxInputStream
& stream
)
993 unsigned char *data
, *ptr
;
994 int done
, i
, bpp
, planes
, comp
, ncolors
, line
, column
,
995 linesize
, linepos
, rshift
= 0, gshift
= 0, bshift
= 0;
998 long int dbuf
[4], dword
, rmask
= 0, gmask
= 0, bmask
= 0, offset
,
1000 off_t start_offset
= stream
.TellI();
1001 signed char bbuf
[4];
1004 unsigned char r
, g
, b
;
1013 #ifndef BI_BITFIELDS
1014 #define BI_BITFIELDS 3
1021 * Reading the bmp header
1024 stream
.Read(&bbuf
, 2);
1026 stream
.Read(dbuf
, 4 * 4);
1031 stream
.Read(dbuf
, 4 * 2);
1032 int width
= (int)dbuf
[0];
1033 int height
= (int)dbuf
[1];
1036 wxLogError( "Image width > 32767 pixels for file\n" );
1041 wxLogError( "Image height > 32767 pixels for file\n" );
1044 stream
.Read(&word
, 2);
1046 stream
.Read(&word
, 2);
1048 if (bpp
!= 1 && bpp
!= 4 && bpp
!= 8 && bpp
&& 16 && bpp
!= 24 && bpp
!= 32)
1050 wxLogError( "unknown bitdepth in file\n" );
1053 stream
.Read(dbuf
, 4 * 4);
1054 comp
= (int)dbuf
[0];
1055 if (comp
!= BI_RGB
&& comp
!= BI_RLE4
&& comp
!= BI_RLE8
&& comp
!= BI_BITFIELDS
)
1057 wxLogError( "unknown encoding in Windows BMP file\n" );
1060 stream
.Read(dbuf
, 4 * 2);
1061 ncolors
= (int)dbuf
[0];
1064 /* some more sanity checks */
1065 if (((comp
== BI_RLE4
) && (bpp
!= 4)) || ((comp
== BI_RLE8
) && (bpp
!= 8)) || ((comp
== BI_BITFIELDS
) && (bpp
!= 16 && bpp
!= 32)))
1067 wxLogError( "encoding of BMP doesn't match bitdepth\n" );
1072 cmap
= (struct _cmap
*)malloc(sizeof(struct _cmap
) * ncolors
);
1076 wxLogError( "Cannot allocate RAM for color map in BMP file\n" );
1083 image
->Create( width
, height
);
1084 ptr
= image
->GetData();
1087 wxLogError( "Cannot allocate RAM for RGB data in file\n" );
1094 * Reading the palette, if it exists.
1096 if (bpp
< 16 && ncolors
!= 0)
1098 for (i
= 0; i
< ncolors
; i
++)
1100 stream
.Read(bbuf
, 4);
1101 cmap
[i
].b
= bbuf
[0];
1102 cmap
[i
].g
= bbuf
[1];
1103 cmap
[i
].r
= bbuf
[2];
1106 else if (bpp
== 16 || bpp
== 32)
1108 if (comp
== BI_BITFIELDS
)
1112 stream
.Read(dbuf
, 4 * 3);
1116 /* find shift amount.. ugly, but i can't think of a better way */
1117 for (bit
= 0; bit
< bpp
; bit
++)
1119 if (bmask
& (1 << bit
))
1121 if (gmask
& (1 << bit
))
1123 if (rmask
& (1 << bit
))
1148 * Reading the image data
1150 stream
.SeekI(start_offset
+ offset
);
1153 /* set the whole image to the background color */
1154 if (bpp
< 16 && (comp
== BI_RLE4
|| comp
== BI_RLE8
))
1156 for (i
= 0; i
< width
* height
; i
++)
1166 #define poffset (line * width * 3 + column * 3)
1169 * BMPs are stored upside down... hmmmmmmmmmm....
1172 linesize
= ((width
* bpp
+ 31) / 32) * 4;
1173 for (line
= (height
- 1); line
>= 0; line
--)
1176 for (column
= 0; column
< width
;)
1183 aByte
= stream
.GetC();
1188 for (bit
= 0; bit
< 8; bit
++)
1190 index
= ((aByte
& (0x80 >> bit
)) ? 1 : 0);
1191 ptr
[poffset
] = cmap
[index
].r
;
1192 ptr
[poffset
+ 1] = cmap
[index
].g
;
1193 ptr
[poffset
+ 2] = cmap
[index
].b
;
1199 if (comp
== BI_RLE4
)
1201 wxLogError( "can't deal with 4bit encoded yet.\n");
1210 for (nibble
= 0; nibble
< 2; nibble
++)
1212 index
= ((aByte
& (0xF0 >> nibble
* 4)) >> (!nibble
* 4));
1215 ptr
[poffset
] = cmap
[index
].r
;
1216 ptr
[poffset
+ 1] = cmap
[index
].g
;
1217 ptr
[poffset
+ 2] = cmap
[index
].b
;
1224 if (comp
== BI_RLE8
)
1226 unsigned char first
;
1229 aByte
= stream
.GetC();
1234 /* column = width; */
1236 else if (aByte
== 1)
1241 else if (aByte
== 2)
1243 aByte
= stream
.GetC();
1245 linepos
= column
* bpp
/ 8;
1246 aByte
= stream
.GetC();
1251 int absolute
= aByte
;
1253 for (i
= 0; i
< absolute
; i
++)
1256 aByte
= stream
.GetC();
1257 ptr
[poffset
] = cmap
[aByte
].r
;
1258 ptr
[poffset
+ 1] = cmap
[aByte
].g
;
1259 ptr
[poffset
+ 2] = cmap
[aByte
].b
;
1262 if (absolute
& 0x01)
1263 aByte
= stream
.GetC();
1268 for (i
= 0; i
< first
; i
++)
1270 ptr
[poffset
] = cmap
[aByte
].r
;
1271 ptr
[poffset
+ 1] = cmap
[aByte
].g
;
1272 ptr
[poffset
+ 2] = cmap
[aByte
].b
;
1280 ptr
[poffset
] = cmap
[aByte
].r
;
1281 ptr
[poffset
+ 1] = cmap
[aByte
].g
;
1282 ptr
[poffset
+ 2] = cmap
[aByte
].b
;
1290 stream
.Read(&bbuf
, 3);
1292 ptr
[poffset
] = (unsigned char)bbuf
[2];
1293 ptr
[poffset
+ 1] = (unsigned char)bbuf
[1];
1294 ptr
[poffset
+ 2] = (unsigned char)bbuf
[0];
1301 stream
.Read(&word
, 2);
1303 temp
= (word
& rmask
) >> rshift
;
1304 ptr
[poffset
] = temp
;
1305 temp
= (word
& gmask
) >> gshift
;
1306 ptr
[poffset
+ 1] = temp
;
1307 temp
= (word
& bmask
) >> gshift
;
1308 ptr
[poffset
+ 2] = temp
;
1315 stream
.Read(&dword
, 4);
1317 temp
= (dword
& rmask
) >> rshift
;
1318 ptr
[poffset
] = temp
;
1319 temp
= (dword
& gmask
) >> gshift
;
1320 ptr
[poffset
+ 1] = temp
;
1321 temp
= (dword
& bmask
) >> bshift
;
1322 ptr
[poffset
+ 2] = temp
;
1326 while ((linepos
< linesize
) && (comp
!= 1) && (comp
!= 2))
1328 stream
.Read(&aByte
, 1);
1330 if (stream
.LastError() != wxStream_NOERROR
)
1334 if (cmap
) free(cmap
);
1336 image
->SetMask( FALSE
);
1340 #endif // wxUSE_STREAMS
1344 wxBitmap
wxImage::ConvertToBitmap() const
1346 // sizeLimit is the MS upper limit for the DIB size
1347 int sizeLimit
= 1024*768*3;
1349 // width and height of the device-dependent bitmap
1350 int width
= GetWidth();
1351 int bmpHeight
= GetHeight();
1353 // calc the number of bytes per scanline and padding
1354 int bytePerLine
= width
*3;
1355 int sizeDWORD
= sizeof( DWORD
);
1356 div_t lineBoundary
= div( bytePerLine
, sizeDWORD
);
1358 if( lineBoundary
.rem
> 0 )
1360 padding
= sizeDWORD
- lineBoundary
.rem
;
1361 bytePerLine
+= padding
;
1363 // calc the number of DIBs and heights of DIBs
1366 int height
= sizeLimit
/bytePerLine
;
1367 if( height
>= bmpHeight
)
1371 div_t result
= div( bmpHeight
, height
);
1372 numDIB
= result
.quot
;
1373 hRemain
= result
.rem
;
1374 if( hRemain
>0 ) numDIB
++;
1377 // set bitmap parameters
1379 wxCHECK_MSG( Ok(), bitmap
, "invalid image" );
1380 bitmap
.SetWidth( width
);
1381 bitmap
.SetHeight( bmpHeight
);
1382 bitmap
.SetDepth( wxDisplayDepth() );
1384 // create a DIB header
1385 int headersize
= sizeof(BITMAPINFOHEADER
);
1386 LPBITMAPINFO lpDIBh
= (BITMAPINFO
*) malloc( headersize
);
1387 wxCHECK_MSG( lpDIBh
, bitmap
, "could not allocate memory for DIB header" );
1388 // Fill in the DIB header
1389 lpDIBh
->bmiHeader
.biSize
= headersize
;
1390 lpDIBh
->bmiHeader
.biWidth
= (DWORD
)width
;
1391 lpDIBh
->bmiHeader
.biHeight
= (DWORD
)(-height
);
1392 lpDIBh
->bmiHeader
.biSizeImage
= bytePerLine
*height
;
1393 // the general formula for biSizeImage:
1394 // ( ( ( ((DWORD)width*24) +31 ) & ~31 ) >> 3 ) * height;
1395 lpDIBh
->bmiHeader
.biPlanes
= 1;
1396 lpDIBh
->bmiHeader
.biBitCount
= 24;
1397 lpDIBh
->bmiHeader
.biCompression
= BI_RGB
;
1398 lpDIBh
->bmiHeader
.biClrUsed
= 0;
1399 // These seem not really needed for our purpose here.
1400 lpDIBh
->bmiHeader
.biClrImportant
= 0;
1401 lpDIBh
->bmiHeader
.biXPelsPerMeter
= 0;
1402 lpDIBh
->bmiHeader
.biYPelsPerMeter
= 0;
1403 // memory for DIB data
1404 unsigned char *lpBits
;
1405 lpBits
= (unsigned char *)malloc( lpDIBh
->bmiHeader
.biSizeImage
);
1408 wxFAIL_MSG( "could not allocate memory for DIB" );
1413 // create and set the device-dependent bitmap
1414 HDC hdc
= ::GetDC(NULL
);
1415 HDC memdc
= ::CreateCompatibleDC( hdc
);
1417 hbitmap
= ::CreateCompatibleBitmap( hdc
, width
, bmpHeight
);
1418 ::SelectObject( memdc
, hbitmap
);
1420 // copy image data into DIB data and then into DDB (in a loop)
1421 unsigned char *data
= GetData();
1424 unsigned char *ptdata
= data
;
1425 unsigned char *ptbits
;
1427 for( n
=0; n
<numDIB
; n
++ )
1429 if( numDIB
> 1 && n
== numDIB
-1 && hRemain
> 0 )
1431 // redefine height and size of the (possibly) last smaller DIB
1432 // memory is not reallocated
1434 lpDIBh
->bmiHeader
.biHeight
= (DWORD
)(-height
);
1435 lpDIBh
->bmiHeader
.biSizeImage
= bytePerLine
*height
;
1439 for( j
=0; j
<height
; j
++ )
1441 for( i
=0; i
<width
; i
++ )
1443 *(ptbits
++) = *(ptdata
+2);
1444 *(ptbits
++) = *(ptdata
+1);
1445 *(ptbits
++) = *(ptdata
);
1448 for( i
=0; i
< padding
; i
++ ) *(ptbits
++) = 0;
1450 ::StretchDIBits( memdc
, 0, origin
, width
, height
,\
1451 0, 0, width
, height
, lpBits
, lpDIBh
, DIB_RGB_COLORS
, SRCCOPY
);
1453 // if numDIB = 1, lines below can also be used
1454 // hbitmap = CreateDIBitmap( hdc, &(lpDIBh->bmiHeader), CBM_INIT, lpBits, lpDIBh, DIB_RGB_COLORS );
1455 // The above line is equivalent to the following two lines.
1456 // hbitmap = ::CreateCompatibleBitmap( hdc, width, height );
1457 // ::SetDIBits( hdc, hbitmap, 0, height, lpBits, lpDIBh, DIB_RGB_COLORS);
1458 // or the following lines
1459 // hbitmap = ::CreateCompatibleBitmap( hdc, width, height );
1460 // HDC memdc = ::CreateCompatibleDC( hdc );
1461 // ::SelectObject( memdc, hbitmap);
1462 // ::SetDIBitsToDevice( memdc, 0, 0, width, height,
1463 // 0, 0, 0, height, (void *)lpBits, lpDIBh, DIB_RGB_COLORS);
1464 // ::SelectObject( memdc, 0 );
1465 // ::DeleteDC( memdc );
1467 bitmap
.SetHBITMAP( (WXHBITMAP
) hbitmap
);
1469 // similarly, created an mono-bitmap for the possible mask
1472 hbitmap
= ::CreateBitmap( (WORD
)width
, (WORD
)bmpHeight
, 1, 1, NULL
);
1473 ::SelectObject( memdc
, hbitmap
);
1474 if( numDIB
== 1 ) height
= bmpHeight
;
1475 else height
= sizeLimit
/bytePerLine
;
1476 lpDIBh
->bmiHeader
.biHeight
= (DWORD
)(-height
);
1477 lpDIBh
->bmiHeader
.biSizeImage
= bytePerLine
*height
;
1479 unsigned char r
= GetMaskRed();
1480 unsigned char g
= GetMaskGreen();
1481 unsigned char b
= GetMaskBlue();
1482 unsigned char zero
= 0, one
= 255;
1484 for( n
=0; n
<numDIB
; n
++ )
1486 if( numDIB
> 1 && n
== numDIB
- 1 && hRemain
> 0 )
1488 // redefine height and size of the (possibly) last smaller DIB
1489 // memory is not reallocated
1491 lpDIBh
->bmiHeader
.biHeight
= (DWORD
)(-height
);
1492 lpDIBh
->bmiHeader
.biSizeImage
= bytePerLine
*height
;
1495 for( int j
=0; j
<height
; j
++ )
1497 for(i
=0; i
<width
; i
++ )
1499 if( (*(ptdata
++)!=r
) | (*(ptdata
++)!=g
) | (*(ptdata
++)!=b
) )
1512 for( i
=0; i
< padding
; i
++ ) *(ptbits
++) = zero
;
1514 ::StretchDIBits( memdc
, 0, origin
, width
, height
,\
1515 0, 0, width
, height
, lpBits
, lpDIBh
, DIB_RGB_COLORS
, SRCCOPY
);
1518 // create a wxMask object
1519 wxMask
*mask
= new wxMask();
1520 mask
->SetMaskBitmap( (WXHBITMAP
) hbitmap
);
1521 bitmap
.SetMask( mask
);
1522 // It will be deleted when the wxBitmap object is deleted (as of 01/1999)
1523 /* The following can also be used but is slow to run
1524 wxColour colour( GetMaskRed(), GetMaskGreen(), GetMaskBlue());
1525 wxMask *mask = new wxMask( bitmap, colour );
1526 bitmap.SetMask( mask );
1530 // free allocated resources
1531 ::SelectObject( memdc
, 0 );
1532 ::DeleteDC( memdc
);
1533 ::ReleaseDC(NULL
, hdc
);
1537 // check the wxBitmap object
1538 if( bitmap
.GetHBITMAP() )
1539 bitmap
.SetOk( TRUE
);
1541 bitmap
.SetOk( FALSE
);
1546 wxImage::wxImage( const wxBitmap
&bitmap
)
1551 wxFAIL_MSG( "invalid bitmap" );
1555 // create an wxImage object
1556 int width
= bitmap
.GetWidth();
1557 int height
= bitmap
.GetHeight();
1558 Create( width
, height
);
1559 unsigned char *data
= GetData();
1562 wxFAIL_MSG( "could not allocate data for image" );
1566 // calc the number of bytes per scanline and padding in the DIB
1567 int bytePerLine
= width
*3;
1568 int sizeDWORD
= sizeof( DWORD
);
1569 div_t lineBoundary
= div( bytePerLine
, sizeDWORD
);
1571 if( lineBoundary
.rem
> 0 )
1573 padding
= sizeDWORD
- lineBoundary
.rem
;
1574 bytePerLine
+= padding
;
1577 // create a DIB header
1578 int headersize
= sizeof(BITMAPINFOHEADER
);
1579 LPBITMAPINFO lpDIBh
= (BITMAPINFO
*) malloc( headersize
);
1582 wxFAIL_MSG( "could not allocate data for DIB header" );
1586 // Fill in the DIB header
1587 lpDIBh
->bmiHeader
.biSize
= headersize
;
1588 lpDIBh
->bmiHeader
.biWidth
= width
;
1589 lpDIBh
->bmiHeader
.biHeight
= -height
;
1590 lpDIBh
->bmiHeader
.biSizeImage
= bytePerLine
* height
;
1591 lpDIBh
->bmiHeader
.biPlanes
= 1;
1592 lpDIBh
->bmiHeader
.biBitCount
= 24;
1593 lpDIBh
->bmiHeader
.biCompression
= BI_RGB
;
1594 lpDIBh
->bmiHeader
.biClrUsed
= 0;
1595 // These seem not really needed for our purpose here.
1596 lpDIBh
->bmiHeader
.biClrImportant
= 0;
1597 lpDIBh
->bmiHeader
.biXPelsPerMeter
= 0;
1598 lpDIBh
->bmiHeader
.biYPelsPerMeter
= 0;
1599 // memory for DIB data
1600 unsigned char *lpBits
;
1601 lpBits
= (unsigned char *) malloc( lpDIBh
->bmiHeader
.biSizeImage
);
1604 wxFAIL_MSG( "could not allocate data for DIB" );
1610 // copy data from the device-dependent bitmap to the DIB
1611 HDC hdc
= ::GetDC(NULL
);
1613 hbitmap
= (HBITMAP
) bitmap
.GetHBITMAP();
1614 ::GetDIBits( hdc
, hbitmap
, 0, height
, lpBits
, lpDIBh
, DIB_RGB_COLORS
);
1616 // copy DIB data into the wxImage object
1618 unsigned char *ptdata
= data
;
1619 unsigned char *ptbits
= lpBits
;
1620 for( i
=0; i
<height
; i
++ )
1622 for( j
=0; j
<width
; j
++ )
1624 *(ptdata
++) = *(ptbits
+2);
1625 *(ptdata
++) = *(ptbits
+1);
1626 *(ptdata
++) = *(ptbits
);
1632 // similarly, set data according to the possible mask bitmap
1633 if( bitmap
.GetMask() && bitmap
.GetMask()->GetMaskBitmap() )
1635 hbitmap
= (HBITMAP
) bitmap
.GetMask()->GetMaskBitmap();
1636 // memory DC created, color set, data copied, and memory DC deleted
1637 HDC memdc
= ::CreateCompatibleDC( hdc
);
1638 ::SetTextColor( memdc
, RGB( 0, 0, 0 ) );
1639 ::SetBkColor( memdc
, RGB( 255, 255, 255 ) );
1640 ::GetDIBits( memdc
, hbitmap
, 0, height
, lpBits
, lpDIBh
, DIB_RGB_COLORS
);
1641 ::DeleteDC( memdc
);
1642 // background color set to RGB(16,16,16) in consistent with wxGTK
1643 unsigned char r
=16, g
=16, b
=16;
1646 for( i
=0; i
<height
; i
++ )
1648 for( j
=0; j
<width
; j
++ )
1662 SetMaskColour( r
, g
, b
);
1669 // free allocated resources
1670 ::ReleaseDC(NULL
, hdc
);
1679 #include "gtk/gtk.h"
1680 #include "gdk/gdk.h"
1681 #include "gdk/gdkx.h"
1683 wxBitmap
wxImage::ConvertToBitmap() const
1687 wxCHECK_MSG( Ok(), bitmap
, "invalid image" );
1689 int width
= GetWidth();
1690 int height
= GetHeight();
1692 bitmap
.SetHeight( height
);
1693 bitmap
.SetWidth( width
);
1697 GdkImage
*data_image
=
1698 gdk_image_new( GDK_IMAGE_FASTEST
, gdk_visual_get_system(), width
, height
);
1700 bitmap
.SetPixmap( gdk_pixmap_new( (GdkWindow
*)&gdk_root_parent
, width
, height
, -1 ) );
1704 GdkImage
*mask_image
= (GdkImage
*) NULL
;
1708 unsigned char *mask_data
= (unsigned char*)malloc( ((width
>> 3)+8) * height
);
1710 mask_image
= gdk_image_new_bitmap( gdk_visual_get_system(), mask_data
, width
, height
);
1712 wxMask
*mask
= new wxMask();
1713 mask
->m_bitmap
= gdk_pixmap_new( (GdkWindow
*)&gdk_root_parent
, width
, height
, 1 );
1715 bitmap
.SetMask( mask
);
1720 GdkVisual
*visual
= gdk_window_get_visual( bitmap
.GetPixmap() );
1721 if (visual
== NULL
) visual
= gdk_window_get_visual( (GdkWindow
*) &gdk_root_parent
);
1722 int bpp
= visual
->depth
;
1724 bitmap
.SetDepth( bpp
);
1726 if ((bpp
== 16) && (visual
->red_mask
!= 0xf800)) bpp
= 15;
1727 if (bpp
< 8) bpp
= 8;
1731 enum byte_order
{ RGB
, RBG
, BRG
, BGR
, GRB
, GBR
};
1732 byte_order b_o
= RGB
;
1736 GdkVisual
*visual
= gdk_visual_get_system();
1737 if ((visual
->red_mask
> visual
->green_mask
) && (visual
->green_mask
> visual
->blue_mask
)) b_o
= RGB
;
1738 else if ((visual
->red_mask
> visual
->blue_mask
) && (visual
->blue_mask
> visual
->green_mask
)) b_o
= RGB
;
1739 else if ((visual
->blue_mask
> visual
->red_mask
) && (visual
->red_mask
> visual
->green_mask
)) b_o
= BRG
;
1740 else if ((visual
->blue_mask
> visual
->green_mask
) && (visual
->green_mask
> visual
->red_mask
)) b_o
= BGR
;
1741 else if ((visual
->green_mask
> visual
->red_mask
) && (visual
->red_mask
> visual
->blue_mask
)) b_o
= GRB
;
1742 else if ((visual
->green_mask
> visual
->blue_mask
) && (visual
->blue_mask
> visual
->red_mask
)) b_o
= GBR
;
1745 int r_mask
= GetMaskRed();
1746 int g_mask
= GetMaskGreen();
1747 int b_mask
= GetMaskBlue();
1749 unsigned char* data
= GetData();
1752 for (int y
= 0; y
< height
; y
++)
1754 for (int x
= 0; x
< width
; x
++)
1756 int r
= data
[index
];
1758 int g
= data
[index
];
1760 int b
= data
[index
];
1765 if ((r
== r_mask
) && (b
== b_mask
) && (g
== g_mask
))
1766 gdk_image_put_pixel( mask_image
, x
, y
, 1 );
1768 gdk_image_put_pixel( mask_image
, x
, y
, 0 );
1773 if ((r
== r_mask
) && (b
== b_mask
) && (g
== g_mask
))
1774 gdk_image_put_pixel( mask_image
, x
, y
, 1 );
1776 gdk_image_put_pixel( mask_image
, x
, y
, 0 );
1784 if (wxTheApp
->m_colorCube
)
1786 pixel
= wxTheApp
->m_colorCube
[ ((r
& 0xf8) << 7) + ((g
& 0xf8) << 2) + ((b
& 0xf8) >> 3) ];
1790 GdkColormap
*cmap
= gtk_widget_get_default_colormap();
1791 GdkColor
*colors
= cmap
->colors
;
1792 int max
= 3 * (65536);
1794 for (int i
= 0; i
< cmap
->size
; i
++)
1796 int rdiff
= (r
<< 8) - colors
[i
].red
;
1797 int gdiff
= (g
<< 8) - colors
[i
].green
;
1798 int bdiff
= (b
<< 8) - colors
[i
].blue
;
1799 int sum
= ABS (rdiff
) + ABS (gdiff
) + ABS (bdiff
);
1800 if (sum
< max
) { pixel
= i
; max
= sum
; }
1804 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
1810 guint32 pixel
= ((r
& 0xf8) << 7) | ((g
& 0xf8) << 2) | ((b
& 0xf8) >> 3);
1811 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
1816 guint32 pixel
= ((r
& 0xf8) << 8) | ((g
& 0xfc) << 3) | ((b
& 0xf8) >> 3);
1817 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
1826 case RGB
: pixel
= (r
<< 16) | (g
<< 8) | b
; break;
1827 case RBG
: pixel
= (r
<< 16) | (b
<< 8) | g
; break;
1828 case BRG
: pixel
= (b
<< 16) | (r
<< 8) | g
; break;
1829 case BGR
: pixel
= (b
<< 16) | (g
<< 8) | r
; break;
1830 case GRB
: pixel
= (g
<< 16) | (r
<< 8) | b
; break;
1831 case GBR
: pixel
= (g
<< 16) | (b
<< 8) | r
; break;
1833 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
1842 GdkGC
*data_gc
= gdk_gc_new( bitmap
.GetPixmap() );
1844 gdk_draw_image( bitmap
.GetPixmap(), data_gc
, data_image
, 0, 0, 0, 0, width
, height
);
1846 gdk_image_destroy( data_image
);
1847 gdk_gc_unref( data_gc
);
1853 GdkGC
*mask_gc
= gdk_gc_new( bitmap
.GetMask()->GetBitmap() );
1855 gdk_draw_image( bitmap
.GetMask()->GetBitmap(), mask_gc
, mask_image
, 0, 0, 0, 0, width
, height
);
1857 gdk_image_destroy( mask_image
);
1858 gdk_gc_unref( mask_gc
);
1864 wxImage::wxImage( const wxBitmap
&bitmap
)
1866 wxCHECK_RET( bitmap
.Ok(), "invalid bitmap" );
1868 GdkImage
*gdk_image
= gdk_image_get( bitmap
.GetPixmap(),
1870 bitmap
.GetWidth(), bitmap
.GetHeight() );
1872 wxCHECK_RET( gdk_image
, "couldn't create image" );
1874 Create( bitmap
.GetWidth(), bitmap
.GetHeight() );
1875 char unsigned *data
= GetData();
1879 gdk_image_destroy( gdk_image
);
1880 wxFAIL_MSG( "couldn't create image" );
1884 GdkImage
*gdk_image_mask
= (GdkImage
*) NULL
;
1885 if (bitmap
.GetMask())
1887 gdk_image_mask
= gdk_image_get( bitmap
.GetMask()->GetBitmap(),
1889 bitmap
.GetWidth(), bitmap
.GetHeight() );
1891 SetMaskColour( 16, 16, 16 ); // anything unlikely and dividable
1894 GdkVisual
*visual
= gdk_window_get_visual( bitmap
.GetPixmap() );
1895 if (visual
== NULL
) visual
= gdk_window_get_visual( (GdkWindow
*) &gdk_root_parent
);
1896 int bpp
= visual
->depth
;
1897 if ((bpp
== 16) && (visual
->red_mask
!= 0xf800)) bpp
= 15;
1899 GdkColormap
*cmap
= gtk_widget_get_default_colormap();
1902 for (int j
= 0; j
< bitmap
.GetHeight(); j
++)
1904 for (int i
= 0; i
< bitmap
.GetWidth(); i
++)
1906 int pixel
= gdk_image_get_pixel( gdk_image
, i
, j
);
1909 data
[pos
] = cmap
->colors
[pixel
].red
>> 8;
1910 data
[pos
+1] = cmap
->colors
[pixel
].green
>> 8;
1911 data
[pos
+2] = cmap
->colors
[pixel
].blue
>> 8;
1912 } else if (bpp
== 15)
1914 data
[pos
] = (pixel
>> 7) & 0xf8;
1915 data
[pos
+1] = (pixel
>> 2) & 0xf8;
1916 data
[pos
+2] = (pixel
<< 3) & 0xf8;
1917 } else if (bpp
== 16)
1919 data
[pos
] = (pixel
>> 8) & 0xf8;
1920 data
[pos
+1] = (pixel
>> 3) & 0xfc;
1921 data
[pos
+2] = (pixel
<< 3) & 0xf8;
1924 data
[pos
] = (pixel
>> 16) & 0xff;
1925 data
[pos
+1] = (pixel
>> 8) & 0xff;
1926 data
[pos
+2] = pixel
& 0xff;
1931 int mask_pixel
= gdk_image_get_pixel( gdk_image_mask
, i
, j
);
1932 if (mask_pixel
== 0)
1944 gdk_image_destroy( gdk_image
);
1945 if (gdk_image_mask
) gdk_image_destroy( gdk_image_mask
);
1953 #include "wx/utils.h"
1956 wxBitmap
wxImage::ConvertToBitmap() const
1960 wxCHECK_MSG( Ok(), bitmap
, "invalid image" );
1962 int width
= GetWidth();
1963 int height
= GetHeight();
1965 bitmap
.SetHeight( height
);
1966 bitmap
.SetWidth( width
);
1968 Display
*dpy
= (Display
*) wxGetDisplay();
1969 Visual
* vis
= DefaultVisual( dpy
, DefaultScreen( dpy
) );
1970 int bpp
= DefaultDepth( dpy
, DefaultScreen( dpy
) );
1974 XImage
*data_image
= XCreateImage( dpy
, vis
, bpp
, ZPixmap
, 0, 0, width
, height
, 32, 0 );
1975 data_image
->data
= (char*) malloc( data_image
->bytes_per_line
* data_image
->height
);
1977 bitmap
.Create( width
, height
, bpp
);
1982 GdkImage *mask_image = (GdkImage*) NULL;
1986 unsigned char *mask_data = (unsigned char*)malloc( ((width >> 3)+8) * height );
1988 mask_image = gdk_image_new_bitmap( gdk_visual_get_system(), mask_data, width, height );
1990 wxMask *mask = new wxMask();
1991 mask->m_bitmap = gdk_pixmap_new( (GdkWindow*)&gdk_root_parent, width, height, 1 );
1993 bitmap.SetMask( mask );
1997 // Retrieve depth info
1999 XVisualInfo vinfo_template
;
2002 vinfo_template
.visual
= vis
;
2003 vinfo_template
.visualid
= XVisualIDFromVisual( vis
);
2004 vinfo_template
.depth
= bpp
;
2007 vi
= XGetVisualInfo( dpy
, VisualIDMask
|VisualDepthMask
, &vinfo_template
, &nitem
);
2011 printf("no visual.\n" );
2012 return wxNullBitmap
;
2017 if ((bpp
== 16) && (vi
->red_mask
!= 0xf800)) bpp
= 15;
2018 if (bpp
< 8) bpp
= 8;
2022 enum byte_order
{ RGB
, RBG
, BRG
, BGR
, GRB
, GBR
};
2023 byte_order b_o
= RGB
;
2027 if ((vi
->red_mask
> vi
->green_mask
) && (vi
->green_mask
> vi
->blue_mask
)) b_o
= RGB
;
2028 else if ((vi
->red_mask
> vi
->blue_mask
) && (vi
->blue_mask
> vi
->green_mask
)) b_o
= RGB
;
2029 else if ((vi
->blue_mask
> vi
->red_mask
) && (vi
->red_mask
> vi
->green_mask
)) b_o
= BRG
;
2030 else if ((vi
->blue_mask
> vi
->green_mask
) && (vi
->green_mask
> vi
->red_mask
)) b_o
= BGR
;
2031 else if ((vi
->green_mask
> vi
->red_mask
) && (vi
->red_mask
> vi
->blue_mask
)) b_o
= GRB
;
2032 else if ((vi
->green_mask
> vi
->blue_mask
) && (vi
->blue_mask
> vi
->red_mask
)) b_o
= GBR
;
2036 int r_mask = GetMaskRed();
2037 int g_mask = GetMaskGreen();
2038 int b_mask = GetMaskBlue();
2044 Colormap cmap
= (Colormap
) wxTheApp
->GetMainColormap( dpy
);
2046 for (int i
= 0; i
< 256; i
++) colors
[i
].pixel
= i
;
2047 XQueryColors( dpy
, cmap
, colors
, 256 );
2050 unsigned char* data
= GetData();
2053 for (int y
= 0; y
< height
; y
++)
2055 for (int x
= 0; x
< width
; x
++)
2057 int r
= data
[index
];
2059 int g
= data
[index
];
2061 int b
= data
[index
];
2067 if ((r == r_mask) && (b == b_mask) && (g == g_mask))
2068 gdk_image_put_pixel( mask_image, x, y, 1 );
2070 gdk_image_put_pixel( mask_image, x, y, 0 );
2080 if (wxTheApp->m_colorCube)
2082 pixel = wxTheApp->m_colorCube
2083 [ ((r & 0xf8) << 7) + ((g & 0xf8) << 2) + ((b & 0xf8) >> 3) ];
2088 int max
= 3 * (65536);
2089 for (int i
= 0; i
< 256; i
++)
2091 int rdiff
= (r
<< 8) - colors
[i
].red
;
2092 int gdiff
= (g
<< 8) - colors
[i
].green
;
2093 int bdiff
= (b
<< 8) - colors
[i
].blue
;
2094 int sum
= abs (rdiff
) + abs (gdiff
) + abs (bdiff
);
2095 if (sum
< max
) { pixel
= i
; max
= sum
; }
2100 XPutPixel( data_image
, x
, y
, pixel
);
2105 int pixel
= ((r
& 0xf8) << 7) | ((g
& 0xf8) << 2) | ((b
& 0xf8) >> 3);
2106 XPutPixel( data_image
, x
, y
, pixel
);
2111 int pixel
= ((r
& 0xf8) << 8) | ((g
& 0xfc) << 3) | ((b
& 0xf8) >> 3);
2112 XPutPixel( data_image
, x
, y
, pixel
);
2121 case RGB
: pixel
= (r
<< 16) | (g
<< 8) | b
; break;
2122 case RBG
: pixel
= (r
<< 16) | (b
<< 8) | g
; break;
2123 case BRG
: pixel
= (b
<< 16) | (r
<< 8) | g
; break;
2124 case BGR
: pixel
= (b
<< 16) | (g
<< 8) | r
; break;
2125 case GRB
: pixel
= (g
<< 16) | (r
<< 8) | b
; break;
2126 case GBR
: pixel
= (g
<< 16) | (b
<< 8) | r
; break;
2128 XPutPixel( data_image
, x
, y
, pixel
);
2138 gcvalues
.foreground
= BlackPixel( dpy
, DefaultScreen( dpy
) );
2139 GC gc
= XCreateGC( dpy
, RootWindow ( dpy
, DefaultScreen(dpy
) ), GCForeground
, &gcvalues
);
2140 XPutImage( dpy
, (Drawable
)bitmap
.GetPixmap(), gc
, data_image
, 0, 0, 0, 0, width
, height
);
2142 XDestroyImage( data_image
);
2150 GdkGC *mask_gc = gdk_gc_new( bitmap.GetMask()->GetBitmap() );
2152 gdk_draw_image( bitmap.GetMask()->GetBitmap(), mask_gc, mask_image, 0, 0, 0, 0, width, height );
2154 gdk_image_destroy( mask_image );
2155 gdk_gc_unref( mask_gc );
2162 wxImage::wxImage( const wxBitmap
&bitmap
)
2164 wxCHECK_RET( bitmap
.Ok(), "invalid bitmap" );
2166 Display
*dpy
= (Display
*) wxGetDisplay();
2167 Visual
* vis
= DefaultVisual( dpy
, DefaultScreen( dpy
) );
2168 int bpp
= DefaultDepth( dpy
, DefaultScreen( dpy
) );
2170 XImage
*ximage
= XGetImage( dpy
,
2171 (Drawable
)bitmap
.GetPixmap(),
2173 bitmap
.GetWidth(), bitmap
.GetHeight(),
2174 AllPlanes
, ZPixmap
);
2176 wxCHECK_RET( ximage
, "couldn't create image" );
2178 Create( bitmap
.GetWidth(), bitmap
.GetHeight() );
2179 char unsigned *data
= GetData();
2183 XDestroyImage( ximage
);
2184 wxFAIL_MSG( "couldn't create image" );
2189 GdkImage *gdk_image_mask = (GdkImage*) NULL;
2190 if (bitmap.GetMask())
2192 gdk_image_mask = gdk_image_get( bitmap.GetMask()->GetBitmap(),
2194 bitmap.GetWidth(), bitmap.GetHeight() );
2196 SetMaskColour( 16, 16, 16 ); // anything unlikely and dividable
2200 // Retrieve depth info
2202 XVisualInfo vinfo_template
;
2205 vinfo_template
.visual
= vis
;
2206 vinfo_template
.visualid
= XVisualIDFromVisual( vis
);
2207 vinfo_template
.depth
= bpp
;
2210 vi
= XGetVisualInfo( dpy
, VisualIDMask
|VisualDepthMask
, &vinfo_template
, &nitem
);
2214 printf("no visual.\n" );
2218 if ((bpp
== 16) && (vi
->red_mask
!= 0xf800)) bpp
= 15;
2225 Colormap cmap
= (Colormap
)wxTheApp
->GetMainColormap( dpy
);
2227 for (int i
= 0; i
< 256; i
++) colors
[i
].pixel
= i
;
2228 XQueryColors( dpy
, cmap
, colors
, 256 );
2232 for (int j
= 0; j
< bitmap
.GetHeight(); j
++)
2234 for (int i
= 0; i
< bitmap
.GetWidth(); i
++)
2236 int pixel
= XGetPixel( ximage
, i
, j
);
2239 data
[pos
] = colors
[pixel
].red
>> 8;
2240 data
[pos
+1] = colors
[pixel
].green
>> 8;
2241 data
[pos
+2] = colors
[pixel
].blue
>> 8;
2242 } else if (bpp
== 15)
2244 data
[pos
] = (pixel
>> 7) & 0xf8;
2245 data
[pos
+1] = (pixel
>> 2) & 0xf8;
2246 data
[pos
+2] = (pixel
<< 3) & 0xf8;
2247 } else if (bpp
== 16)
2249 data
[pos
] = (pixel
>> 8) & 0xf8;
2250 data
[pos
+1] = (pixel
>> 3) & 0xfc;
2251 data
[pos
+2] = (pixel
<< 3) & 0xf8;
2254 data
[pos
] = (pixel
>> 16) & 0xff;
2255 data
[pos
+1] = (pixel
>> 8) & 0xff;
2256 data
[pos
+2] = pixel
& 0xff;
2262 int mask_pixel = gdk_image_get_pixel( gdk_image_mask, i, j );
2263 if (mask_pixel == 0)
2276 XDestroyImage( ximage
);
2278 if (gdk_image_mask) gdk_image_destroy( gdk_image_mask );
2283 // A module to allow wxImage initialization/cleanup
2284 // without calling these functions from app.cpp or from
2285 // the user's application.
2287 class wxImageModule
: public wxModule
2289 DECLARE_DYNAMIC_CLASS(wxImageModule
)
2292 bool OnInit() { wxImage::InitStandardHandlers(); return TRUE
; };
2293 void OnExit() { wxImage::CleanUpHandlers(); };
2296 IMPLEMENT_DYNAMIC_CLASS(wxImageModule
, wxModule
)