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 double xscale
= (double)width
/ (double)M_IMGDATA
->m_width
;
167 double yscale
= (double)height
/ (double)M_IMGDATA
->m_height
;
169 for (int j
= 0; j
< height
; j
++)
171 for (int i
= 0; i
< width
; i
++)
173 int new_pos
= 3*(j
*width
+ i
);
174 int old_pos
= 3*((long)(j
/yscale
)*M_IMGDATA
->m_width
+ (long)(i
/xscale
));
175 data
[ new_pos
] = M_IMGDATA
->m_data
[ old_pos
];
176 data
[ new_pos
+1 ] = M_IMGDATA
->m_data
[ old_pos
+1 ];
177 data
[ new_pos
+2 ] = M_IMGDATA
->m_data
[ old_pos
+2 ];
184 void wxImage::SetRGB( int x
, int y
, unsigned char r
, unsigned char g
, unsigned char b
)
186 wxCHECK_RET( Ok(), "invalid image" );
188 int w
= M_IMGDATA
->m_width
;
189 int h
= M_IMGDATA
->m_height
;
191 wxCHECK_RET( (x
>=0) && (y
>=0) && (x
<w
) && (y
<h
), "invalid image index" );
193 long pos
= (y
* w
+ x
) * 3;
195 M_IMGDATA
->m_data
[ pos
] = r
;
196 M_IMGDATA
->m_data
[ pos
+1 ] = g
;
197 M_IMGDATA
->m_data
[ pos
+2 ] = b
;
200 unsigned char wxImage::GetRed( int x
, int y
)
202 wxCHECK_MSG( Ok(), 0, "invalid image" );
204 int w
= M_IMGDATA
->m_width
;
205 int h
= M_IMGDATA
->m_height
;
207 wxCHECK_MSG( (x
>=0) && (y
>=0) && (x
<w
) && (y
<h
), 0, "invalid image index" );
209 long pos
= (y
* w
+ x
) * 3;
211 return M_IMGDATA
->m_data
[pos
];
214 unsigned char wxImage::GetGreen( int x
, int y
)
216 wxCHECK_MSG( Ok(), 0, "invalid image" );
218 int w
= M_IMGDATA
->m_width
;
219 int h
= M_IMGDATA
->m_height
;
221 wxCHECK_MSG( (x
>=0) && (y
>=0) && (x
<w
) && (y
<h
), 0, "invalid image index" );
223 long pos
= (y
* w
+ x
) * 3;
225 return M_IMGDATA
->m_data
[pos
+1];
228 unsigned char wxImage::GetBlue( int x
, int y
)
230 wxCHECK_MSG( Ok(), 0, "invalid image" );
232 int w
= M_IMGDATA
->m_width
;
233 int h
= M_IMGDATA
->m_height
;
235 wxCHECK_MSG( (x
>=0) && (y
>=0) && (x
<w
) && (y
<h
), 0, "invalid image index" );
237 long pos
= (y
* w
+ x
) * 3;
239 return M_IMGDATA
->m_data
[pos
+2];
242 bool wxImage::Ok() const
244 return (M_IMGDATA
&& M_IMGDATA
->m_ok
);
247 char unsigned *wxImage::GetData() const
249 wxCHECK_MSG( Ok(), (char unsigned *)NULL
, "invalid image" );
251 return M_IMGDATA
->m_data
;
254 void wxImage::SetData( char unsigned *WXUNUSED(data
) )
256 wxCHECK_RET( Ok(), "invalid image" );
259 void wxImage::SetMaskColour( unsigned char r
, unsigned char g
, unsigned char b
)
261 wxCHECK_RET( Ok(), "invalid image" );
263 M_IMGDATA
->m_maskRed
= r
;
264 M_IMGDATA
->m_maskGreen
= g
;
265 M_IMGDATA
->m_maskBlue
= b
;
266 M_IMGDATA
->m_hasMask
= TRUE
;
269 unsigned char wxImage::GetMaskRed() const
271 wxCHECK_MSG( Ok(), 0, "invalid image" );
273 return M_IMGDATA
->m_maskRed
;
276 unsigned char wxImage::GetMaskGreen() const
278 wxCHECK_MSG( Ok(), 0, "invalid image" );
280 return M_IMGDATA
->m_maskGreen
;
283 unsigned char wxImage::GetMaskBlue() const
285 wxCHECK_MSG( Ok(), 0, "invalid image" );
287 return M_IMGDATA
->m_maskBlue
;
290 void wxImage::SetMask( bool mask
)
292 wxCHECK_RET( Ok(), "invalid image" );
294 M_IMGDATA
->m_hasMask
= mask
;
297 bool wxImage::HasMask() const
299 wxCHECK_MSG( Ok(), FALSE
, "invalid image" );
301 return M_IMGDATA
->m_hasMask
;
304 int wxImage::GetWidth() const
306 wxCHECK_MSG( Ok(), 0, "invalid image" );
308 return M_IMGDATA
->m_width
;
311 int wxImage::GetHeight() const
313 wxCHECK_MSG( Ok(), 0, "invalid image" );
315 return M_IMGDATA
->m_height
;
318 bool wxImage::LoadFile( const wxString
& filename
, long type
)
321 if (wxFileExists(filename
))
323 wxFileInputStream
stream(filename
);
324 return LoadFile(stream
, type
);
328 wxLogError( "Can't load image from file '%s': file does not exist.", filename
.c_str() );
332 #else // !wxUSE_STREAMS
334 #endif // wxUSE_STREAMS
337 bool wxImage::SaveFile( const wxString
& filename
, int type
)
340 wxFileOutputStream
stream(filename
);
342 if ( stream
.LastError() == wxStream_NOERROR
)
343 return SaveFile(stream
, type
);
345 #endif // wxUSE_STREAMS
350 bool wxImage::LoadFile( wxInputStream
& stream
, long type
)
354 m_refData
= new wxImageRefData
;
356 wxImageHandler
*handler
= FindHandler(type
);
360 wxLogWarning( "No image handler for type %d defined.", type
);
365 return handler
->LoadFile( this, stream
);
368 bool wxImage::SaveFile( wxOutputStream
& stream
, int type
)
370 wxCHECK_MSG( Ok(), FALSE
, "invalid image" );
372 wxImageHandler
*handler
= FindHandler(type
);
376 wxLogWarning( "No image handler for type %d defined.", type
);
381 return handler
->SaveFile( this, stream
);
383 #endif // wxUSE_STREAMS
385 void wxImage::AddHandler( wxImageHandler
*handler
)
387 // make sure that the memory will be freed at the program end
388 sm_handlers
.DeleteContents(TRUE
);
390 sm_handlers
.Append( handler
);
393 void wxImage::InsertHandler( wxImageHandler
*handler
)
395 // make sure that the memory will be freed at the program end
396 sm_handlers
.DeleteContents(TRUE
);
398 sm_handlers
.Insert( handler
);
401 bool wxImage::RemoveHandler( const wxString
& name
)
403 wxImageHandler
*handler
= FindHandler(name
);
406 sm_handlers
.DeleteObject(handler
);
413 wxImageHandler
*wxImage::FindHandler( const wxString
& name
)
415 wxNode
*node
= sm_handlers
.First();
418 wxImageHandler
*handler
= (wxImageHandler
*)node
->Data();
419 if (handler
->GetName().Cmp(name
) == 0) return handler
;
423 return (wxImageHandler
*)NULL
;
426 wxImageHandler
*wxImage::FindHandler( const wxString
& extension
, long bitmapType
)
428 wxNode
*node
= sm_handlers
.First();
431 wxImageHandler
*handler
= (wxImageHandler
*)node
->Data();
432 if ( (handler
->GetExtension().Cmp(extension
) == 0) &&
433 (bitmapType
== -1 || handler
->GetType() == bitmapType
) )
437 return (wxImageHandler
*)NULL
;
440 wxImageHandler
*wxImage::FindHandler( long bitmapType
)
442 wxNode
*node
= sm_handlers
.First();
445 wxImageHandler
*handler
= (wxImageHandler
*)node
->Data();
446 if (handler
->GetType() == bitmapType
) return handler
;
452 void wxImage::InitStandardHandlers()
454 AddHandler( new wxBMPHandler
);
456 AddHandler( new wxPNGHandler
);
459 AddHandler( new wxJPEGHandler
);
463 void wxImage::CleanUpHandlers()
465 wxNode
*node
= sm_handlers
.First();
468 wxImageHandler
*handler
= (wxImageHandler
*)node
->Data();
469 wxNode
*next
= node
->Next();
476 //-----------------------------------------------------------------------------
478 //-----------------------------------------------------------------------------
480 #if !USE_SHARED_LIBRARIES
481 IMPLEMENT_DYNAMIC_CLASS(wxImageHandler
,wxObject
)
485 bool wxImageHandler::LoadFile( wxImage
*WXUNUSED(image
), wxInputStream
& WXUNUSED(stream
) )
490 bool wxImageHandler::SaveFile( wxImage
*WXUNUSED(image
), wxOutputStream
& WXUNUSED(stream
) )
494 #endif // wxUSE_STREAMS
496 //-----------------------------------------------------------------------------
498 //-----------------------------------------------------------------------------
502 #if !USE_SHARED_LIBRARIES
503 IMPLEMENT_DYNAMIC_CLASS(wxPNGHandler
,wxImageHandler
)
508 static void _PNG_stream_reader( png_structp png_ptr
, png_bytep data
, png_size_t length
)
510 ((wxInputStream
*) png_get_io_ptr( png_ptr
)) -> Read(data
, length
);
513 static void _PNG_stream_writer( png_structp png_ptr
, png_bytep data
, png_size_t length
)
515 ((wxOutputStream
*) png_get_io_ptr( png_ptr
)) -> Write(data
, length
);
518 bool wxPNGHandler::LoadFile( wxImage
*image
, wxInputStream
& stream
)
520 // VZ: as this function uses setjmp() the only fool proof error handling
521 // method is to use goto (setjmp is not really C++ dtors friendly...)
523 unsigned char **lines
= (unsigned char **) NULL
;
525 png_infop info_ptr
= (png_infop
) NULL
;
529 png_structp png_ptr
= png_create_read_struct( PNG_LIBPNG_VER_STRING
,
531 (png_error_ptr
) NULL
,
532 (png_error_ptr
) NULL
);
536 info_ptr
= png_create_info_struct( png_ptr
);
540 if (setjmp(png_ptr
->jmpbuf
))
543 if (info_ptr
->color_type
== PNG_COLOR_TYPE_RGB_ALPHA
)
546 png_set_read_fn( png_ptr
, &stream
, _PNG_stream_reader
);
548 png_uint_32 width
,height
;
549 int bit_depth
,color_type
,interlace_type
;
551 png_read_info( png_ptr
, info_ptr
);
552 png_get_IHDR( png_ptr
, info_ptr
, &width
, &height
, &bit_depth
, &color_type
, &interlace_type
, (int*) NULL
, (int*) NULL
);
554 if (color_type
== PNG_COLOR_TYPE_PALETTE
)
555 png_set_expand( png_ptr
);
557 png_set_strip_16( png_ptr
);
558 png_set_packing( png_ptr
);
559 if (png_get_valid( png_ptr
, info_ptr
, PNG_INFO_tRNS
))
560 png_set_expand( png_ptr
);
561 png_set_filler( png_ptr
, 0xff, PNG_FILLER_AFTER
);
563 image
->Create( width
, height
);
568 lines
= (unsigned char **)malloc( height
* sizeof(unsigned char *) );
572 for (i
= 0; i
< height
; i
++)
574 if ((lines
[i
] = (unsigned char *)malloc(width
* (sizeof(unsigned char) * 4))) == NULL
)
576 for ( unsigned int n
= 0; n
< i
; n
++ )
582 // loaded successfully!
585 png_read_image( png_ptr
, lines
);
586 png_destroy_read_struct( &png_ptr
, &info_ptr
, (png_infopp
) NULL
);
587 unsigned char *ptr
= image
->GetData();
588 if ((color_type
== PNG_COLOR_TYPE_GRAY
) ||
589 (color_type
== PNG_COLOR_TYPE_GRAY_ALPHA
))
591 for (unsigned int y
= 0; y
< height
; y
++)
593 unsigned char *ptr2
= lines
[y
];
594 for (unsigned int x
= 0; x
< width
; x
++)
596 unsigned char r
= *ptr2
++;
597 unsigned char a
= *ptr2
++;
616 for (unsigned int y
= 0; y
< height
; y
++)
618 unsigned char *ptr2
= lines
[y
];
619 for (unsigned int x
= 0; x
< width
; x
++)
621 unsigned char r
= *ptr2
++;
622 unsigned char g
= *ptr2
++;
623 unsigned char b
= *ptr2
++;
624 unsigned char a
= *ptr2
++;
634 if ((r
== 255) && (g
== 0) && (b
== 255)) r
= 254;
643 for ( unsigned int j
= 0; j
< height
; j
++ )
649 image
->SetMaskColour( 255, 0, 255 );
653 image
->SetMask( FALSE
);
660 wxLogError(_("Couldn't load a PNG image - probably file is corrupted."));
676 png_destroy_read_struct( &png_ptr
, &info_ptr
, (png_infopp
) NULL
);
680 png_destroy_read_struct( &png_ptr
, (png_infopp
) NULL
, (png_infopp
) NULL
);
686 bool wxPNGHandler::SaveFile( wxImage
*image
, wxOutputStream
& stream
)
689 png_structp png_ptr
= png_create_write_struct( PNG_LIBPNG_VER_STRING
, NULL
, NULL
, NULL
);
695 png_infop info_ptr
= png_create_info_struct(png_ptr
);
696 if (info_ptr
== NULL
)
698 png_destroy_write_struct( &png_ptr
, (png_infopp
)NULL
);
702 if (setjmp(png_ptr
->jmpbuf
))
704 png_destroy_write_struct( &png_ptr
, (png_infopp
)NULL
);
708 png_set_write_fn( png_ptr
, &stream
, _PNG_stream_writer
, NULL
);
710 png_set_IHDR( png_ptr
, info_ptr
, image
->GetWidth(), image
->GetHeight(), 8,
711 PNG_COLOR_TYPE_RGB_ALPHA
, PNG_INTERLACE_NONE
,
712 PNG_COMPRESSION_TYPE_BASE
, PNG_FILTER_TYPE_BASE
);
719 png_set_sBIT( png_ptr
, info_ptr
, &sig_bit
);
720 png_write_info( png_ptr
, info_ptr
);
721 png_set_shift( png_ptr
, &sig_bit
);
722 png_set_packing( png_ptr
);
724 unsigned char *data
= (unsigned char *)malloc( image
->GetWidth()*4 );
727 png_destroy_write_struct( &png_ptr
, (png_infopp
)NULL
);
731 for (int y
= 0; y
< image
->GetHeight(); y
++)
733 unsigned char *ptr
= image
->GetData() + (y
* image
->GetWidth() * 3);
734 for (int x
= 0; x
< image
->GetWidth(); x
++)
736 data
[(x
<< 2) + 0] = *ptr
++;
737 data
[(x
<< 2) + 1] = *ptr
++;
738 data
[(x
<< 2) + 2] = *ptr
++;
739 if ((data
[(x
<< 2) + 0] == image
->GetMaskRed()) &&
740 (data
[(x
<< 2) + 1] == image
->GetMaskGreen()) &&
741 (data
[(x
<< 2) + 2] == image
->GetMaskBlue()))
743 data
[(x
<< 2) + 3] = 0;
747 data
[(x
<< 2) + 3] = 255;
750 png_bytep row_ptr
= data
;
751 png_write_rows( png_ptr
, &row_ptr
, 1 );
755 png_write_end( png_ptr
, info_ptr
);
756 png_destroy_write_struct( &png_ptr
, (png_infopp
)&info_ptr
);
760 #endif // wxUSE_STREAMS
767 //-----------------------------------------------------------------------------
769 //-----------------------------------------------------------------------------
773 #if !USE_SHARED_LIBRARIES
774 IMPLEMENT_DYNAMIC_CLASS(wxJPEGHandler
,wxImageHandler
)
780 //------------- JPEG Data Source Manager
783 struct jpeg_source_mgr pub
; /* public fields */
785 JOCTET
* buffer
; /* start of buffer */
788 typedef my_source_mgr
* my_src_ptr
;
790 METHODDEF(void) my_init_source ( j_decompress_ptr cinfo
)
794 METHODDEF(boolean
) my_fill_input_buffer ( j_decompress_ptr cinfo
)
799 METHODDEF(void) my_skip_input_data ( j_decompress_ptr cinfo
, long num_bytes
)
801 my_src_ptr src
= (my_src_ptr
) cinfo
->src
;
803 src
->pub
.next_input_byte
+= (size_t) num_bytes
;
804 src
->pub
.bytes_in_buffer
-= (size_t) num_bytes
;
807 METHODDEF(void) my_term_source ( j_decompress_ptr cinfo
)
809 my_src_ptr src
= (my_src_ptr
) cinfo
->src
;
814 void jpeg_wxio_src( j_decompress_ptr cinfo
, wxInputStream
& infile
)
818 if (cinfo
->src
== NULL
) { /* first time for this JPEG object? */
819 cinfo
->src
= (struct jpeg_source_mgr
*)
820 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
) cinfo
, JPOOL_PERMANENT
,
821 sizeof(my_source_mgr
));
822 src
= (my_src_ptr
) cinfo
->src
;
824 src
= (my_src_ptr
) cinfo
->src
;
825 src
->pub
.bytes_in_buffer
= infile
.StreamSize(); /* forces fill_input_buffer on first read */
826 src
->buffer
= (JOCTET
*) malloc (infile
.StreamSize());
827 src
->pub
.next_input_byte
= src
->buffer
; /* until buffer loaded */
828 infile
.Read(src
->buffer
, infile
.StreamSize());
830 src
->pub
.init_source
= my_init_source
;
831 src
->pub
.fill_input_buffer
= my_fill_input_buffer
;
832 src
->pub
.skip_input_data
= my_skip_input_data
;
833 src
->pub
.resync_to_restart
= jpeg_resync_to_restart
; /* use default method */
834 src
->pub
.term_source
= my_term_source
;
839 bool wxJPEGHandler::LoadFile( wxImage
*image
, wxInputStream
& stream
)
841 struct jpeg_decompress_struct cinfo
;
842 struct jpeg_error_mgr jerr
;
848 cinfo
.err
= jpeg_std_error( &jerr
);
849 jpeg_create_decompress( &cinfo
);
850 jpeg_wxio_src( &cinfo
, stream
);
851 jpeg_read_header( &cinfo
, TRUE
);
852 cinfo
.out_color_space
= JCS_RGB
;
853 jpeg_start_decompress( &cinfo
);
855 image
->Create( cinfo
.image_width
, cinfo
.image_height
);
857 jpeg_finish_decompress( &cinfo
);
858 jpeg_destroy_decompress( &cinfo
);
861 image
->SetMask( FALSE
);
862 ptr
= image
->GetData();
863 stride
= cinfo
.output_width
* 3;
864 tempbuf
= (*cinfo
.mem
->alloc_sarray
)
865 ((j_common_ptr
) &cinfo
, JPOOL_IMAGE
, stride
, 1 );
867 while ( cinfo
.output_scanline
< cinfo
.output_height
) {
868 jpeg_read_scanlines( &cinfo
, tempbuf
, 1 );
869 memcpy( ptr
, tempbuf
[0], stride
);
872 jpeg_finish_decompress( &cinfo
);
873 jpeg_destroy_decompress( &cinfo
);
882 struct jpeg_destination_mgr pub
;
884 wxOutputStream
*stream
;
886 } my_destination_mgr
;
888 typedef my_destination_mgr
* my_dest_ptr
;
890 #define OUTPUT_BUF_SIZE 4096 /* choose an efficiently fwrite'able size */
892 METHODDEF(void) init_destination (j_compress_ptr cinfo
)
894 my_dest_ptr dest
= (my_dest_ptr
) cinfo
->dest
;
896 /* Allocate the output buffer --- it will be released when done with image */
897 dest
->buffer
= (JOCTET
*)
898 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
) cinfo
, JPOOL_IMAGE
,
899 OUTPUT_BUF_SIZE
* sizeof(JOCTET
));
900 dest
->pub
.next_output_byte
= dest
->buffer
;
901 dest
->pub
.free_in_buffer
= OUTPUT_BUF_SIZE
;
904 METHODDEF(boolean
) empty_output_buffer (j_compress_ptr cinfo
)
906 my_dest_ptr dest
= (my_dest_ptr
) cinfo
->dest
;
908 dest
->stream
->Write(dest
->buffer
, OUTPUT_BUF_SIZE
);
909 dest
->pub
.next_output_byte
= dest
->buffer
;
910 dest
->pub
.free_in_buffer
= OUTPUT_BUF_SIZE
;
914 METHODDEF(void) term_destination (j_compress_ptr cinfo
)
916 my_dest_ptr dest
= (my_dest_ptr
) cinfo
->dest
;
917 size_t datacount
= OUTPUT_BUF_SIZE
- dest
->pub
.free_in_buffer
;
918 /* Write any data remaining in the buffer */
920 dest
->stream
->Write(dest
->buffer
, datacount
);
923 GLOBAL(void) jpeg_wxio_dest (j_compress_ptr cinfo
, wxOutputStream
& outfile
)
927 if (cinfo
->dest
== NULL
) { /* first time for this JPEG object? */
928 cinfo
->dest
= (struct jpeg_destination_mgr
*)
929 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
) cinfo
, JPOOL_PERMANENT
,
930 sizeof(my_destination_mgr
));
933 dest
= (my_dest_ptr
) cinfo
->dest
;
934 dest
->pub
.init_destination
= init_destination
;
935 dest
->pub
.empty_output_buffer
= empty_output_buffer
;
936 dest
->pub
.term_destination
= term_destination
;
937 dest
->stream
= &outfile
;
942 bool wxJPEGHandler::SaveFile( wxImage
*image
, wxOutputStream
& stream
)
944 struct jpeg_compress_struct cinfo
;
945 struct jpeg_error_mgr jerr
;
946 JSAMPROW row_pointer
[1]; /* pointer to JSAMPLE row[s] */
947 JSAMPLE
*image_buffer
;
948 int stride
; /* physical row width in image buffer */
950 cinfo
.err
= jpeg_std_error(&jerr
);
951 jpeg_create_compress(&cinfo
);
952 jpeg_wxio_dest(&cinfo
, stream
);
954 cinfo
.image_width
= image
->GetWidth();
955 cinfo
.image_height
= image
->GetHeight();
956 cinfo
.input_components
= 3;
957 cinfo
.in_color_space
= JCS_RGB
;
958 jpeg_set_defaults(&cinfo
);
959 jpeg_start_compress(&cinfo
, TRUE
);
961 stride
= cinfo
.image_width
* 3; /* JSAMPLEs per row in image_buffer */
962 image_buffer
= image
->GetData();
963 while (cinfo
.next_scanline
< cinfo
.image_height
) {
964 row_pointer
[0] = &image_buffer
[cinfo
.next_scanline
* stride
];
965 jpeg_write_scanlines( &cinfo
, row_pointer
, 1 );
967 jpeg_finish_compress(&cinfo
);
968 jpeg_destroy_compress(&cinfo
);
972 #endif // wxUSE_STREAMS
980 //-----------------------------------------------------------------------------
982 //-----------------------------------------------------------------------------
984 #if !USE_SHARED_LIBRARIES
985 IMPLEMENT_DYNAMIC_CLASS(wxBMPHandler
,wxImageHandler
)
989 bool wxBMPHandler::LoadFile( wxImage
*image
, wxInputStream
& stream
)
991 unsigned char *data
, *ptr
;
992 int done
, i
, bpp
, planes
, comp
, ncolors
, line
, column
,
993 linesize
, linepos
, rshift
= 0, gshift
= 0, bshift
= 0;
996 long int dbuf
[4], dword
, rmask
= 0, gmask
= 0, bmask
= 0, offset
,
998 off_t start_offset
= stream
.TellI();
1002 unsigned char r
, g
, b
;
1011 #ifndef BI_BITFIELDS
1012 #define BI_BITFIELDS 3
1019 * Reading the bmp header
1022 stream
.Read(&bbuf
, 2);
1024 stream
.Read(dbuf
, 4 * 4);
1029 stream
.Read(dbuf
, 4 * 2);
1030 int width
= (int)dbuf
[0];
1031 int height
= (int)dbuf
[1];
1034 wxLogError( "Image width > 32767 pixels for file\n" );
1039 wxLogError( "Image height > 32767 pixels for file\n" );
1042 stream
.Read(&word
, 2);
1044 stream
.Read(&word
, 2);
1046 if (bpp
!= 1 && bpp
!= 4 && bpp
!= 8 && bpp
&& 16 && bpp
!= 24 && bpp
!= 32)
1048 wxLogError( "unknown bitdepth in file\n" );
1051 stream
.Read(dbuf
, 4 * 4);
1052 comp
= (int)dbuf
[0];
1053 if (comp
!= BI_RGB
&& comp
!= BI_RLE4
&& comp
!= BI_RLE8
&& comp
!= BI_BITFIELDS
)
1055 wxLogError( "unknown encoding in Windows BMP file\n" );
1058 stream
.Read(dbuf
, 4 * 2);
1059 ncolors
= (int)dbuf
[0];
1062 /* some more sanity checks */
1063 if (((comp
== BI_RLE4
) && (bpp
!= 4)) || ((comp
== BI_RLE8
) && (bpp
!= 8)) || ((comp
== BI_BITFIELDS
) && (bpp
!= 16 && bpp
!= 32)))
1065 wxLogError( "encoding of BMP doesn't match bitdepth\n" );
1070 cmap
= (struct _cmap
*)malloc(sizeof(struct _cmap
) * ncolors
);
1074 wxLogError( "Cannot allocate RAM for color map in BMP file\n" );
1081 image
->Create( width
, height
);
1082 ptr
= image
->GetData();
1085 wxLogError( "Cannot allocate RAM for RGB data in file\n" );
1092 * Reading the palette, if it exists.
1094 if (bpp
< 16 && ncolors
!= 0)
1096 for (i
= 0; i
< ncolors
; i
++)
1098 stream
.Read(bbuf
, 4);
1099 cmap
[i
].b
= bbuf
[0];
1100 cmap
[i
].g
= bbuf
[1];
1101 cmap
[i
].r
= bbuf
[2];
1104 else if (bpp
== 16 || bpp
== 32)
1106 if (comp
== BI_BITFIELDS
)
1110 stream
.Read(dbuf
, 4 * 3);
1114 /* find shift amount.. ugly, but i can't think of a better way */
1115 for (bit
= 0; bit
< bpp
; bit
++)
1117 if (bmask
& (1 << bit
))
1119 if (gmask
& (1 << bit
))
1121 if (rmask
& (1 << bit
))
1146 * Reading the image data
1148 stream
.SeekI(start_offset
+ offset
);
1151 /* set the whole image to the background color */
1152 if (bpp
< 16 && (comp
== BI_RLE4
|| comp
== BI_RLE8
))
1154 for (i
= 0; i
< width
* height
; i
++)
1164 #define poffset (line * width * 3 + column * 3)
1167 * BMPs are stored upside down... hmmmmmmmmmm....
1170 linesize
= ((width
* bpp
+ 31) / 32) * 4;
1171 for (line
= (height
- 1); line
>= 0; line
--)
1174 for (column
= 0; column
< width
;)
1181 aByte
= stream
.GetC();
1186 for (bit
= 0; bit
< 8; bit
++)
1188 index
= ((aByte
& (0x80 >> bit
)) ? 1 : 0);
1189 ptr
[poffset
] = cmap
[index
].r
;
1190 ptr
[poffset
+ 1] = cmap
[index
].g
;
1191 ptr
[poffset
+ 2] = cmap
[index
].b
;
1197 if (comp
== BI_RLE4
)
1199 wxLogError( "can't deal with 4bit encoded yet.\n");
1208 for (nibble
= 0; nibble
< 2; nibble
++)
1210 index
= ((aByte
& (0xF0 >> nibble
* 4)) >> (!nibble
* 4));
1213 ptr
[poffset
] = cmap
[index
].r
;
1214 ptr
[poffset
+ 1] = cmap
[index
].g
;
1215 ptr
[poffset
+ 2] = cmap
[index
].b
;
1222 if (comp
== BI_RLE8
)
1224 unsigned char first
;
1227 aByte
= stream
.GetC();
1232 /* column = width; */
1234 else if (aByte
== 1)
1239 else if (aByte
== 2)
1241 aByte
= stream
.GetC();
1243 linepos
= column
* bpp
/ 8;
1244 aByte
= stream
.GetC();
1249 int absolute
= aByte
;
1251 for (i
= 0; i
< absolute
; i
++)
1254 aByte
= stream
.GetC();
1255 ptr
[poffset
] = cmap
[aByte
].r
;
1256 ptr
[poffset
+ 1] = cmap
[aByte
].g
;
1257 ptr
[poffset
+ 2] = cmap
[aByte
].b
;
1260 if (absolute
& 0x01)
1261 aByte
= stream
.GetC();
1266 for (i
= 0; i
< first
; i
++)
1268 ptr
[poffset
] = cmap
[aByte
].r
;
1269 ptr
[poffset
+ 1] = cmap
[aByte
].g
;
1270 ptr
[poffset
+ 2] = cmap
[aByte
].b
;
1278 ptr
[poffset
] = cmap
[aByte
].r
;
1279 ptr
[poffset
+ 1] = cmap
[aByte
].g
;
1280 ptr
[poffset
+ 2] = cmap
[aByte
].b
;
1288 stream
.Read(&bbuf
, 3);
1290 ptr
[poffset
] = (unsigned char)bbuf
[2];
1291 ptr
[poffset
+ 1] = (unsigned char)bbuf
[1];
1292 ptr
[poffset
+ 2] = (unsigned char)bbuf
[0];
1299 stream
.Read(&word
, 2);
1301 temp
= (word
& rmask
) >> rshift
;
1302 ptr
[poffset
] = temp
;
1303 temp
= (word
& gmask
) >> gshift
;
1304 ptr
[poffset
+ 1] = temp
;
1305 temp
= (word
& bmask
) >> gshift
;
1306 ptr
[poffset
+ 2] = temp
;
1313 stream
.Read(&dword
, 4);
1315 temp
= (dword
& rmask
) >> rshift
;
1316 ptr
[poffset
] = temp
;
1317 temp
= (dword
& gmask
) >> gshift
;
1318 ptr
[poffset
+ 1] = temp
;
1319 temp
= (dword
& bmask
) >> bshift
;
1320 ptr
[poffset
+ 2] = temp
;
1324 while ((linepos
< linesize
) && (comp
!= 1) && (comp
!= 2))
1326 stream
.Read(&aByte
, 1);
1328 if (stream
.LastError() != wxStream_NOERROR
)
1332 if (cmap
) free(cmap
);
1334 image
->SetMask( FALSE
);
1338 #endif // wxUSE_STREAMS
1342 wxBitmap
wxImage::ConvertToBitmap() const
1344 // sizeLimit is the MS upper limit for the DIB size
1345 int sizeLimit
= 1024*768*3;
1347 // width and height of the device-dependent bitmap
1348 int width
= GetWidth();
1349 int bmpHeight
= GetHeight();
1351 // calc the number of bytes per scanline and padding
1352 int bytePerLine
= width
*3;
1353 int sizeDWORD
= sizeof( DWORD
);
1354 div_t lineBoundary
= div( bytePerLine
, sizeDWORD
);
1356 if( lineBoundary
.rem
> 0 )
1358 padding
= sizeDWORD
- lineBoundary
.rem
;
1359 bytePerLine
+= padding
;
1361 // calc the number of DIBs and heights of DIBs
1364 int height
= sizeLimit
/bytePerLine
;
1365 if( height
>= bmpHeight
)
1369 div_t result
= div( bmpHeight
, height
);
1370 numDIB
= result
.quot
;
1371 hRemain
= result
.rem
;
1372 if( hRemain
>0 ) numDIB
++;
1375 // set bitmap parameters
1377 wxCHECK_MSG( Ok(), bitmap
, "invalid image" );
1378 bitmap
.SetWidth( width
);
1379 bitmap
.SetHeight( bmpHeight
);
1380 bitmap
.SetDepth( wxDisplayDepth() );
1382 // create a DIB header
1383 int headersize
= sizeof(BITMAPINFOHEADER
);
1384 LPBITMAPINFO lpDIBh
= (BITMAPINFO
*) malloc( headersize
);
1385 wxCHECK_MSG( lpDIBh
, bitmap
, "could not allocate memory for DIB header" );
1386 // Fill in the DIB header
1387 lpDIBh
->bmiHeader
.biSize
= headersize
;
1388 lpDIBh
->bmiHeader
.biWidth
= (DWORD
)width
;
1389 lpDIBh
->bmiHeader
.biHeight
= (DWORD
)(-height
);
1390 lpDIBh
->bmiHeader
.biSizeImage
= bytePerLine
*height
;
1391 // the general formula for biSizeImage:
1392 // ( ( ( ((DWORD)width*24) +31 ) & ~31 ) >> 3 ) * height;
1393 lpDIBh
->bmiHeader
.biPlanes
= 1;
1394 lpDIBh
->bmiHeader
.biBitCount
= 24;
1395 lpDIBh
->bmiHeader
.biCompression
= BI_RGB
;
1396 lpDIBh
->bmiHeader
.biClrUsed
= 0;
1397 // These seem not really needed for our purpose here.
1398 lpDIBh
->bmiHeader
.biClrImportant
= 0;
1399 lpDIBh
->bmiHeader
.biXPelsPerMeter
= 0;
1400 lpDIBh
->bmiHeader
.biYPelsPerMeter
= 0;
1401 // memory for DIB data
1402 unsigned char *lpBits
;
1403 lpBits
= (unsigned char *)malloc( lpDIBh
->bmiHeader
.biSizeImage
);
1406 wxFAIL_MSG( "could not allocate memory for DIB" );
1411 // create and set the device-dependent bitmap
1412 HDC hdc
= ::GetDC(NULL
);
1413 HDC memdc
= ::CreateCompatibleDC( hdc
);
1415 hbitmap
= ::CreateCompatibleBitmap( hdc
, width
, bmpHeight
);
1416 ::SelectObject( memdc
, hbitmap
);
1418 // copy image data into DIB data and then into DDB (in a loop)
1419 unsigned char *data
= GetData();
1422 unsigned char *ptdata
= data
;
1423 unsigned char *ptbits
;
1425 for( n
=0; n
<numDIB
; n
++ )
1427 if( numDIB
> 1 && n
== numDIB
-1 && hRemain
> 0 )
1429 // redefine height and size of the (possibly) last smaller DIB
1430 // memory is not reallocated
1432 lpDIBh
->bmiHeader
.biHeight
= (DWORD
)(-height
);
1433 lpDIBh
->bmiHeader
.biSizeImage
= bytePerLine
*height
;
1437 for( j
=0; j
<height
; j
++ )
1439 for( i
=0; i
<width
; i
++ )
1441 *(ptbits
++) = *(ptdata
+2);
1442 *(ptbits
++) = *(ptdata
+1);
1443 *(ptbits
++) = *(ptdata
);
1446 for( i
=0; i
< padding
; i
++ ) *(ptbits
++) = 0;
1448 ::StretchDIBits( memdc
, 0, origin
, width
, height
,\
1449 0, 0, width
, height
, lpBits
, lpDIBh
, DIB_RGB_COLORS
, SRCCOPY
);
1451 // if numDIB = 1, lines below can also be used
1452 // hbitmap = CreateDIBitmap( hdc, &(lpDIBh->bmiHeader), CBM_INIT, lpBits, lpDIBh, DIB_RGB_COLORS );
1453 // The above line is equivalent to the following two lines.
1454 // hbitmap = ::CreateCompatibleBitmap( hdc, width, height );
1455 // ::SetDIBits( hdc, hbitmap, 0, height, lpBits, lpDIBh, DIB_RGB_COLORS);
1456 // or the following lines
1457 // hbitmap = ::CreateCompatibleBitmap( hdc, width, height );
1458 // HDC memdc = ::CreateCompatibleDC( hdc );
1459 // ::SelectObject( memdc, hbitmap);
1460 // ::SetDIBitsToDevice( memdc, 0, 0, width, height,
1461 // 0, 0, 0, height, (void *)lpBits, lpDIBh, DIB_RGB_COLORS);
1462 // ::SelectObject( memdc, 0 );
1463 // ::DeleteDC( memdc );
1465 bitmap
.SetHBITMAP( (WXHBITMAP
) hbitmap
);
1467 // similarly, created an mono-bitmap for the possible mask
1470 hbitmap
= ::CreateBitmap( (WORD
)width
, (WORD
)bmpHeight
, 1, 1, NULL
);
1471 ::SelectObject( memdc
, hbitmap
);
1472 if( numDIB
== 1 ) height
= bmpHeight
;
1473 else height
= sizeLimit
/bytePerLine
;
1474 lpDIBh
->bmiHeader
.biHeight
= (DWORD
)(-height
);
1475 lpDIBh
->bmiHeader
.biSizeImage
= bytePerLine
*height
;
1477 unsigned char r
= GetMaskRed();
1478 unsigned char g
= GetMaskGreen();
1479 unsigned char b
= GetMaskBlue();
1480 unsigned char zero
= 0, one
= 255;
1482 for( n
=0; n
<numDIB
; n
++ )
1484 if( numDIB
> 1 && n
== numDIB
- 1 && hRemain
> 0 )
1486 // redefine height and size of the (possibly) last smaller DIB
1487 // memory is not reallocated
1489 lpDIBh
->bmiHeader
.biHeight
= (DWORD
)(-height
);
1490 lpDIBh
->bmiHeader
.biSizeImage
= bytePerLine
*height
;
1493 for( int j
=0; j
<height
; j
++ )
1495 for( int i
=0; i
<width
; i
++ )
1497 if( (*(ptdata
++)!=r
) | (*(ptdata
++)!=g
) | (*(ptdata
++)!=b
) )
1510 for( i
=0; i
< padding
; i
++ ) *(ptbits
++) = zero
;
1512 ::StretchDIBits( memdc
, 0, origin
, width
, height
,\
1513 0, 0, width
, height
, lpBits
, lpDIBh
, DIB_RGB_COLORS
, SRCCOPY
);
1516 // create a wxMask object
1517 wxMask
*mask
= new wxMask();
1518 mask
->SetMaskBitmap( (WXHBITMAP
) hbitmap
);
1519 bitmap
.SetMask( mask
);
1520 // It will be deleted when the wxBitmap object is deleted (as of 01/1999)
1521 /* The following can also be used but is slow to run
1522 wxColour colour( GetMaskRed(), GetMaskGreen(), GetMaskBlue());
1523 wxMask *mask = new wxMask( bitmap, colour );
1524 bitmap.SetMask( mask );
1528 // free allocated resources
1529 ::SelectObject( memdc
, 0 );
1530 ::DeleteDC( memdc
);
1531 ::ReleaseDC(NULL
, hdc
);
1535 // check the wxBitmap object
1536 if( bitmap
.GetHBITMAP() )
1537 bitmap
.SetOk( TRUE
);
1539 bitmap
.SetOk( FALSE
);
1544 wxImage::wxImage( const wxBitmap
&bitmap
)
1549 wxFAIL_MSG( "invalid bitmap" );
1553 // create an wxImage object
1554 int width
= bitmap
.GetWidth();
1555 int height
= bitmap
.GetHeight();
1556 Create( width
, height
);
1557 unsigned char *data
= GetData();
1560 wxFAIL_MSG( "could not allocate data for image" );
1564 // calc the number of bytes per scanline and padding in the DIB
1565 int bytePerLine
= width
*3;
1566 int sizeDWORD
= sizeof( DWORD
);
1567 div_t lineBoundary
= div( bytePerLine
, sizeDWORD
);
1569 if( lineBoundary
.rem
> 0 )
1571 padding
= sizeDWORD
- lineBoundary
.rem
;
1572 bytePerLine
+= padding
;
1575 // create a DIB header
1576 int headersize
= sizeof(BITMAPINFOHEADER
);
1577 LPBITMAPINFO lpDIBh
= (BITMAPINFO
*) malloc( headersize
);
1580 wxFAIL_MSG( "could not allocate data for DIB header" );
1584 // Fill in the DIB header
1585 lpDIBh
->bmiHeader
.biSize
= headersize
;
1586 lpDIBh
->bmiHeader
.biWidth
= width
;
1587 lpDIBh
->bmiHeader
.biHeight
= -height
;
1588 lpDIBh
->bmiHeader
.biSizeImage
= bytePerLine
* height
;
1589 lpDIBh
->bmiHeader
.biPlanes
= 1;
1590 lpDIBh
->bmiHeader
.biBitCount
= 24;
1591 lpDIBh
->bmiHeader
.biCompression
= BI_RGB
;
1592 lpDIBh
->bmiHeader
.biClrUsed
= 0;
1593 // These seem not really needed for our purpose here.
1594 lpDIBh
->bmiHeader
.biClrImportant
= 0;
1595 lpDIBh
->bmiHeader
.biXPelsPerMeter
= 0;
1596 lpDIBh
->bmiHeader
.biYPelsPerMeter
= 0;
1597 // memory for DIB data
1598 unsigned char *lpBits
;
1599 lpBits
= (unsigned char *) malloc( lpDIBh
->bmiHeader
.biSizeImage
);
1602 wxFAIL_MSG( "could not allocate data for DIB" );
1608 // copy data from the device-dependent bitmap to the DIB
1609 HDC hdc
= ::GetDC(NULL
);
1611 hbitmap
= (HBITMAP
) bitmap
.GetHBITMAP();
1612 ::GetDIBits( hdc
, hbitmap
, 0, height
, lpBits
, lpDIBh
, DIB_RGB_COLORS
);
1614 // copy DIB data into the wxImage object
1616 unsigned char *ptdata
= data
;
1617 unsigned char *ptbits
= lpBits
;
1618 for( i
=0; i
<height
; i
++ )
1620 for( j
=0; j
<width
; j
++ )
1622 *(ptdata
++) = *(ptbits
+2);
1623 *(ptdata
++) = *(ptbits
+1);
1624 *(ptdata
++) = *(ptbits
);
1630 // similarly, set data according to the possible mask bitmap
1631 if( bitmap
.GetMask() && bitmap
.GetMask()->GetMaskBitmap() )
1633 hbitmap
= (HBITMAP
) bitmap
.GetMask()->GetMaskBitmap();
1634 // memory DC created, color set, data copied, and memory DC deleted
1635 HDC memdc
= ::CreateCompatibleDC( hdc
);
1636 ::SetTextColor( memdc
, RGB( 0, 0, 0 ) );
1637 ::SetBkColor( memdc
, RGB( 255, 255, 255 ) );
1638 ::GetDIBits( memdc
, hbitmap
, 0, height
, lpBits
, lpDIBh
, DIB_RGB_COLORS
);
1639 ::DeleteDC( memdc
);
1640 // background color set to RGB(16,16,16) in consistent with wxGTK
1641 unsigned char r
=16, g
=16, b
=16;
1644 for( i
=0; i
<height
; i
++ )
1646 for( j
=0; j
<width
; j
++ )
1660 SetMaskColour( r
, g
, b
);
1667 // free allocated resources
1668 ::ReleaseDC(NULL
, hdc
);
1677 #include "gtk/gtk.h"
1678 #include "gdk/gdk.h"
1679 #include "gdk/gdkx.h"
1681 wxBitmap
wxImage::ConvertToBitmap() const
1685 wxCHECK_MSG( Ok(), bitmap
, "invalid image" );
1687 int width
= GetWidth();
1688 int height
= GetHeight();
1690 bitmap
.SetHeight( height
);
1691 bitmap
.SetWidth( width
);
1695 GdkImage
*data_image
=
1696 gdk_image_new( GDK_IMAGE_FASTEST
, gdk_visual_get_system(), width
, height
);
1698 bitmap
.SetPixmap( gdk_pixmap_new( (GdkWindow
*)&gdk_root_parent
, width
, height
, -1 ) );
1702 GdkImage
*mask_image
= (GdkImage
*) NULL
;
1706 unsigned char *mask_data
= (unsigned char*)malloc( ((width
>> 3)+8) * height
);
1708 mask_image
= gdk_image_new_bitmap( gdk_visual_get_system(), mask_data
, width
, height
);
1710 wxMask
*mask
= new wxMask();
1711 mask
->m_bitmap
= gdk_pixmap_new( (GdkWindow
*)&gdk_root_parent
, width
, height
, 1 );
1713 bitmap
.SetMask( mask
);
1718 GdkVisual
*visual
= gdk_window_get_visual( bitmap
.GetPixmap() );
1719 if (visual
== NULL
) visual
= gdk_window_get_visual( (GdkWindow
*) &gdk_root_parent
);
1720 int bpp
= visual
->depth
;
1722 bitmap
.SetDepth( bpp
);
1724 if ((bpp
== 16) && (visual
->red_mask
!= 0xf800)) bpp
= 15;
1725 if (bpp
< 8) bpp
= 8;
1729 enum byte_order
{ RGB
, RBG
, BRG
, BGR
, GRB
, GBR
};
1730 byte_order b_o
= RGB
;
1734 GdkVisual
*visual
= gdk_visual_get_system();
1735 if ((visual
->red_mask
> visual
->green_mask
) && (visual
->green_mask
> visual
->blue_mask
)) b_o
= RGB
;
1736 else if ((visual
->red_mask
> visual
->blue_mask
) && (visual
->blue_mask
> visual
->green_mask
)) b_o
= RGB
;
1737 else if ((visual
->blue_mask
> visual
->red_mask
) && (visual
->red_mask
> visual
->green_mask
)) b_o
= BRG
;
1738 else if ((visual
->blue_mask
> visual
->green_mask
) && (visual
->green_mask
> visual
->red_mask
)) b_o
= BGR
;
1739 else if ((visual
->green_mask
> visual
->red_mask
) && (visual
->red_mask
> visual
->blue_mask
)) b_o
= GRB
;
1740 else if ((visual
->green_mask
> visual
->blue_mask
) && (visual
->blue_mask
> visual
->red_mask
)) b_o
= GBR
;
1743 int r_mask
= GetMaskRed();
1744 int g_mask
= GetMaskGreen();
1745 int b_mask
= GetMaskBlue();
1747 unsigned char* data
= GetData();
1750 for (int y
= 0; y
< height
; y
++)
1752 for (int x
= 0; x
< width
; x
++)
1754 int r
= data
[index
];
1756 int g
= data
[index
];
1758 int b
= data
[index
];
1763 if ((r
== r_mask
) && (b
== b_mask
) && (g
== g_mask
))
1764 gdk_image_put_pixel( mask_image
, x
, y
, 1 );
1766 gdk_image_put_pixel( mask_image
, x
, y
, 0 );
1771 if ((r
== r_mask
) && (b
== b_mask
) && (g
== g_mask
))
1772 gdk_image_put_pixel( mask_image
, x
, y
, 1 );
1774 gdk_image_put_pixel( mask_image
, x
, y
, 0 );
1782 if (wxTheApp
->m_colorCube
)
1784 pixel
= wxTheApp
->m_colorCube
[ ((r
& 0xf8) << 7) + ((g
& 0xf8) << 2) + ((b
& 0xf8) >> 3) ];
1788 GdkColormap
*cmap
= gtk_widget_get_default_colormap();
1789 GdkColor
*colors
= cmap
->colors
;
1790 int max
= 3 * (65536);
1792 for (int i
= 0; i
< cmap
->size
; i
++)
1794 int rdiff
= (r
<< 8) - colors
[i
].red
;
1795 int gdiff
= (g
<< 8) - colors
[i
].green
;
1796 int bdiff
= (b
<< 8) - colors
[i
].blue
;
1797 int sum
= ABS (rdiff
) + ABS (gdiff
) + ABS (bdiff
);
1798 if (sum
< max
) { pixel
= i
; max
= sum
; }
1802 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
1808 guint32 pixel
= ((r
& 0xf8) << 7) | ((g
& 0xf8) << 2) | ((b
& 0xf8) >> 3);
1809 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
1814 guint32 pixel
= ((r
& 0xf8) << 8) | ((g
& 0xfc) << 3) | ((b
& 0xf8) >> 3);
1815 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
1824 case RGB
: pixel
= (r
<< 16) | (g
<< 8) | b
; break;
1825 case RBG
: pixel
= (r
<< 16) | (b
<< 8) | g
; break;
1826 case BRG
: pixel
= (b
<< 16) | (r
<< 8) | g
; break;
1827 case BGR
: pixel
= (b
<< 16) | (g
<< 8) | r
; break;
1828 case GRB
: pixel
= (g
<< 16) | (r
<< 8) | b
; break;
1829 case GBR
: pixel
= (g
<< 16) | (b
<< 8) | r
; break;
1831 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
1840 GdkGC
*data_gc
= gdk_gc_new( bitmap
.GetPixmap() );
1842 gdk_draw_image( bitmap
.GetPixmap(), data_gc
, data_image
, 0, 0, 0, 0, width
, height
);
1844 gdk_image_destroy( data_image
);
1845 gdk_gc_unref( data_gc
);
1851 GdkGC
*mask_gc
= gdk_gc_new( bitmap
.GetMask()->GetBitmap() );
1853 gdk_draw_image( bitmap
.GetMask()->GetBitmap(), mask_gc
, mask_image
, 0, 0, 0, 0, width
, height
);
1855 gdk_image_destroy( mask_image
);
1856 gdk_gc_unref( mask_gc
);
1862 wxImage::wxImage( const wxBitmap
&bitmap
)
1864 wxCHECK_RET( bitmap
.Ok(), "invalid bitmap" );
1866 GdkImage
*gdk_image
= gdk_image_get( bitmap
.GetPixmap(),
1868 bitmap
.GetWidth(), bitmap
.GetHeight() );
1870 wxCHECK_RET( gdk_image
, "couldn't create image" );
1872 Create( bitmap
.GetWidth(), bitmap
.GetHeight() );
1873 char unsigned *data
= GetData();
1877 gdk_image_destroy( gdk_image
);
1878 wxFAIL_MSG( "couldn't create image" );
1882 GdkImage
*gdk_image_mask
= (GdkImage
*) NULL
;
1883 if (bitmap
.GetMask())
1885 gdk_image_mask
= gdk_image_get( bitmap
.GetMask()->GetBitmap(),
1887 bitmap
.GetWidth(), bitmap
.GetHeight() );
1889 SetMaskColour( 16, 16, 16 ); // anything unlikely and dividable
1892 GdkVisual
*visual
= gdk_window_get_visual( bitmap
.GetPixmap() );
1893 if (visual
== NULL
) visual
= gdk_window_get_visual( (GdkWindow
*) &gdk_root_parent
);
1894 int bpp
= visual
->depth
;
1895 if ((bpp
== 16) && (visual
->red_mask
!= 0xf800)) bpp
= 15;
1897 GdkColormap
*cmap
= gtk_widget_get_default_colormap();
1900 for (int j
= 0; j
< bitmap
.GetHeight(); j
++)
1902 for (int i
= 0; i
< bitmap
.GetWidth(); i
++)
1904 int pixel
= gdk_image_get_pixel( gdk_image
, i
, j
);
1907 data
[pos
] = cmap
->colors
[pixel
].red
>> 8;
1908 data
[pos
+1] = cmap
->colors
[pixel
].green
>> 8;
1909 data
[pos
+2] = cmap
->colors
[pixel
].blue
>> 8;
1910 } else if (bpp
== 15)
1912 data
[pos
] = (pixel
>> 7) & 0xf8;
1913 data
[pos
+1] = (pixel
>> 2) & 0xf8;
1914 data
[pos
+2] = (pixel
<< 3) & 0xf8;
1915 } else if (bpp
== 16)
1917 data
[pos
] = (pixel
>> 8) & 0xf8;
1918 data
[pos
+1] = (pixel
>> 3) & 0xfc;
1919 data
[pos
+2] = (pixel
<< 3) & 0xf8;
1922 data
[pos
] = (pixel
>> 16) & 0xff;
1923 data
[pos
+1] = (pixel
>> 8) & 0xff;
1924 data
[pos
+2] = pixel
& 0xff;
1929 int mask_pixel
= gdk_image_get_pixel( gdk_image_mask
, i
, j
);
1930 if (mask_pixel
== 0)
1942 gdk_image_destroy( gdk_image
);
1943 if (gdk_image_mask
) gdk_image_destroy( gdk_image_mask
);
1951 #include "wx/utils.h"
1954 wxBitmap
wxImage::ConvertToBitmap() const
1958 wxCHECK_MSG( Ok(), bitmap
, "invalid image" );
1960 int width
= GetWidth();
1961 int height
= GetHeight();
1963 bitmap
.SetHeight( height
);
1964 bitmap
.SetWidth( width
);
1966 Display
*dpy
= (Display
*) wxGetDisplay();
1967 Visual
* vis
= DefaultVisual( dpy
, DefaultScreen( dpy
) );
1968 int bpp
= DefaultDepth( dpy
, DefaultScreen( dpy
) );
1972 XImage
*data_image
= XCreateImage( dpy
, vis
, bpp
, ZPixmap
, 0, 0, width
, height
, 32, 0 );
1973 data_image
->data
= new char[ data_image
->bytes_per_line
* data_image
->height
];
1975 bitmap
.Create( width
, height
, bpp
);
1980 GdkImage *mask_image = (GdkImage*) NULL;
1984 unsigned char *mask_data = (unsigned char*)malloc( ((width >> 3)+8) * height );
1986 mask_image = gdk_image_new_bitmap( gdk_visual_get_system(), mask_data, width, height );
1988 wxMask *mask = new wxMask();
1989 mask->m_bitmap = gdk_pixmap_new( (GdkWindow*)&gdk_root_parent, width, height, 1 );
1991 bitmap.SetMask( mask );
1995 // Retrieve depth info
1997 XVisualInfo vinfo_template
;
2000 vinfo_template
.visual
= vis
;
2001 vinfo_template
.visualid
= XVisualIDFromVisual( vis
);
2002 vinfo_template
.depth
= bpp
;
2005 vi
= XGetVisualInfo( dpy
, VisualIDMask
|VisualDepthMask
, &vinfo_template
, &nitem
);
2009 printf("no visual.\n" );
2010 return wxNullBitmap
;
2015 if ((bpp
== 16) && (vi
->red_mask
!= 0xf800)) bpp
= 15;
2016 if (bpp
< 8) bpp
= 8;
2020 enum byte_order
{ RGB
, RBG
, BRG
, BGR
, GRB
, GBR
};
2021 byte_order b_o
= RGB
;
2025 if ((vi
->red_mask
> vi
->green_mask
) && (vi
->green_mask
> vi
->blue_mask
)) b_o
= RGB
;
2026 else if ((vi
->red_mask
> vi
->blue_mask
) && (vi
->blue_mask
> vi
->green_mask
)) b_o
= RGB
;
2027 else if ((vi
->blue_mask
> vi
->red_mask
) && (vi
->red_mask
> vi
->green_mask
)) b_o
= BRG
;
2028 else if ((vi
->blue_mask
> vi
->green_mask
) && (vi
->green_mask
> vi
->red_mask
)) b_o
= BGR
;
2029 else if ((vi
->green_mask
> vi
->red_mask
) && (vi
->red_mask
> vi
->blue_mask
)) b_o
= GRB
;
2030 else if ((vi
->green_mask
> vi
->blue_mask
) && (vi
->blue_mask
> vi
->red_mask
)) b_o
= GBR
;
2034 int r_mask = GetMaskRed();
2035 int g_mask = GetMaskGreen();
2036 int b_mask = GetMaskBlue();
2042 Colormap cmap
= (Colormap
) wxTheApp
->GetMainColormap( dpy
);
2044 for (int i
= 0; i
< 256; i
++) colors
[i
].pixel
= i
;
2045 XQueryColors( dpy
, cmap
, colors
, 256 );
2048 unsigned char* data
= GetData();
2051 for (int y
= 0; y
< height
; y
++)
2053 for (int x
= 0; x
< width
; x
++)
2055 int r
= data
[index
];
2057 int g
= data
[index
];
2059 int b
= data
[index
];
2065 if ((r == r_mask) && (b == b_mask) && (g == g_mask))
2066 gdk_image_put_pixel( mask_image, x, y, 1 );
2068 gdk_image_put_pixel( mask_image, x, y, 0 );
2078 if (wxTheApp->m_colorCube)
2080 pixel = wxTheApp->m_colorCube
2081 [ ((r & 0xf8) << 7) + ((g & 0xf8) << 2) + ((b & 0xf8) >> 3) ];
2086 int max
= 3 * (65536);
2087 for (int i
= 0; i
< 256; i
++)
2089 int rdiff
= (r
<< 8) - colors
[i
].red
;
2090 int gdiff
= (g
<< 8) - colors
[i
].green
;
2091 int bdiff
= (b
<< 8) - colors
[i
].blue
;
2092 int sum
= abs (rdiff
) + abs (gdiff
) + abs (bdiff
);
2093 if (sum
< max
) { pixel
= i
; max
= sum
; }
2098 XPutPixel( data_image
, x
, y
, pixel
);
2103 int pixel
= ((r
& 0xf8) << 7) | ((g
& 0xf8) << 2) | ((b
& 0xf8) >> 3);
2104 XPutPixel( data_image
, x
, y
, pixel
);
2109 int pixel
= ((r
& 0xf8) << 8) | ((g
& 0xfc) << 3) | ((b
& 0xf8) >> 3);
2110 XPutPixel( data_image
, x
, y
, pixel
);
2119 case RGB
: pixel
= (r
<< 16) | (g
<< 8) | b
; break;
2120 case RBG
: pixel
= (r
<< 16) | (b
<< 8) | g
; break;
2121 case BRG
: pixel
= (b
<< 16) | (r
<< 8) | g
; break;
2122 case BGR
: pixel
= (b
<< 16) | (g
<< 8) | r
; break;
2123 case GRB
: pixel
= (g
<< 16) | (r
<< 8) | b
; break;
2124 case GBR
: pixel
= (g
<< 16) | (b
<< 8) | r
; break;
2126 XPutPixel( data_image
, x
, y
, pixel
);
2136 gcvalues
.foreground
= BlackPixel( dpy
, DefaultScreen( dpy
) );
2137 GC gc
= XCreateGC( dpy
, RootWindow ( dpy
, DefaultScreen(dpy
) ), GCForeground
, &gcvalues
);
2138 XPutImage( dpy
, (Drawable
)bitmap
.GetPixmap(), gc
, data_image
, 0, 0, 0, 0, width
, height
);
2140 XDestroyImage( data_image
);
2148 GdkGC *mask_gc = gdk_gc_new( bitmap.GetMask()->GetBitmap() );
2150 gdk_draw_image( bitmap.GetMask()->GetBitmap(), mask_gc, mask_image, 0, 0, 0, 0, width, height );
2152 gdk_image_destroy( mask_image );
2153 gdk_gc_unref( mask_gc );
2160 wxImage::wxImage( const wxBitmap
&bitmap
)
2162 wxCHECK_RET( bitmap
.Ok(), "invalid bitmap" );
2164 Display
*dpy
= (Display
*) wxGetDisplay();
2165 Visual
* vis
= DefaultVisual( dpy
, DefaultScreen( dpy
) );
2166 int bpp
= DefaultDepth( dpy
, DefaultScreen( dpy
) );
2168 XImage
*ximage
= XGetImage( dpy
,
2169 (Drawable
)bitmap
.GetPixmap(),
2171 bitmap
.GetWidth(), bitmap
.GetHeight(),
2172 AllPlanes
, ZPixmap
);
2174 wxCHECK_RET( ximage
, "couldn't create image" );
2176 Create( bitmap
.GetWidth(), bitmap
.GetHeight() );
2177 char unsigned *data
= GetData();
2181 XDestroyImage( ximage
);
2182 wxFAIL_MSG( "couldn't create image" );
2187 GdkImage *gdk_image_mask = (GdkImage*) NULL;
2188 if (bitmap.GetMask())
2190 gdk_image_mask = gdk_image_get( bitmap.GetMask()->GetBitmap(),
2192 bitmap.GetWidth(), bitmap.GetHeight() );
2194 SetMaskColour( 16, 16, 16 ); // anything unlikely and dividable
2198 // Retrieve depth info
2200 XVisualInfo vinfo_template
;
2203 vinfo_template
.visual
= vis
;
2204 vinfo_template
.visualid
= XVisualIDFromVisual( vis
);
2205 vinfo_template
.depth
= bpp
;
2208 vi
= XGetVisualInfo( dpy
, VisualIDMask
|VisualDepthMask
, &vinfo_template
, &nitem
);
2212 printf("no visual.\n" );
2216 if ((bpp
== 16) && (vi
->red_mask
!= 0xf800)) bpp
= 15;
2223 Colormap cmap
= (Colormap
)wxTheApp
->GetMainColormap( dpy
);
2225 for (int i
= 0; i
< 256; i
++) colors
[i
].pixel
= i
;
2226 XQueryColors( dpy
, cmap
, colors
, 256 );
2230 for (int j
= 0; j
< bitmap
.GetHeight(); j
++)
2232 for (int i
= 0; i
< bitmap
.GetWidth(); i
++)
2234 int pixel
= XGetPixel( ximage
, i
, j
);
2237 data
[pos
] = colors
[pixel
].red
>> 8;
2238 data
[pos
+1] = colors
[pixel
].green
>> 8;
2239 data
[pos
+2] = colors
[pixel
].blue
>> 8;
2240 } else if (bpp
== 15)
2242 data
[pos
] = (pixel
>> 7) & 0xf8;
2243 data
[pos
+1] = (pixel
>> 2) & 0xf8;
2244 data
[pos
+2] = (pixel
<< 3) & 0xf8;
2245 } else if (bpp
== 16)
2247 data
[pos
] = (pixel
>> 8) & 0xf8;
2248 data
[pos
+1] = (pixel
>> 3) & 0xfc;
2249 data
[pos
+2] = (pixel
<< 3) & 0xf8;
2252 data
[pos
] = (pixel
>> 16) & 0xff;
2253 data
[pos
+1] = (pixel
>> 8) & 0xff;
2254 data
[pos
+2] = pixel
& 0xff;
2260 int mask_pixel = gdk_image_get_pixel( gdk_image_mask, i, j );
2261 if (mask_pixel == 0)
2274 XDestroyImage( ximage
);
2276 if (gdk_image_mask) gdk_image_destroy( gdk_image_mask );