]> git.saurik.com Git - wxWidgets.git/blob - src/common/image.cpp
compilation problem fixed
[wxWidgets.git] / src / common / image.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: image.cpp
3 // Purpose: wxImage
4 // Author: Robert Roebling
5 // RCS-ID: $Id$
6 // Copyright: (c) Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 #ifdef __GNUG__
11 #pragma implementation "image.h"
12 #endif
13
14 // For compilers that support precompilation, includes "wx.h".
15 #include "wx/wxprec.h"
16
17 #ifdef __BORLANDC__
18 #pragma hdrstop
19 #endif
20
21 #include "wx/image.h"
22 #include "wx/bitmap.h"
23 #include "wx/debug.h"
24 #include "wx/log.h"
25 #include "wx/app.h"
26 #ifdef wxUSE_LIBPNG
27 #include "../png/png.h"
28 #endif
29 #include "wx/filefn.h"
30 #include "wx/wfstream.h"
31 #include "wx/intl.h"
32
33 #ifdef __WXMSW__
34 #include <windows.h>
35 #endif
36
37 //-----------------------------------------------------------------------------
38 // wxImage
39 //-----------------------------------------------------------------------------
40
41 class wxImageRefData: public wxObjectRefData
42 {
43
44 public:
45 wxImageRefData(void);
46 ~wxImageRefData(void);
47
48 int m_width;
49 int m_height;
50 unsigned char *m_data;
51 bool m_hasMask;
52 unsigned char m_maskRed,m_maskGreen,m_maskBlue;
53 bool m_ok;
54 };
55
56 wxImageRefData::wxImageRefData(void)
57 {
58 m_width = 0;
59 m_height = 0;
60 m_data = (unsigned char*) NULL;
61 m_ok = FALSE;
62 m_maskRed = 0;
63 m_maskGreen = 0;
64 m_maskBlue = 0;
65 m_hasMask = FALSE;
66 }
67
68 wxImageRefData::~wxImageRefData(void)
69 {
70 if (m_data) free( m_data );
71 }
72
73 wxList wxImage::sm_handlers;
74
75 //-----------------------------------------------------------------------------
76
77 #define M_IMGDATA ((wxImageRefData *)m_refData)
78
79 #if !USE_SHARED_LIBRARIES
80 IMPLEMENT_DYNAMIC_CLASS(wxImage, wxObject)
81 #endif
82
83 wxImage::wxImage()
84 {
85 }
86
87 wxImage::wxImage( int width, int height )
88 {
89 Create( width, height );
90 }
91
92 wxImage::wxImage( const wxString& name, long type )
93 {
94 LoadFile( name, type );
95 }
96
97 wxImage::wxImage( wxInputStream& stream, long type )
98 {
99 LoadFile( stream, type );
100 }
101
102 wxImage::wxImage( const wxImage& image )
103 {
104 Ref(image);
105 }
106
107 wxImage::wxImage( const wxImage* image )
108 {
109 if (image) Ref(*image);
110 }
111
112 void wxImage::Create( int width, int height )
113 {
114 m_refData = new wxImageRefData();
115
116 M_IMGDATA->m_data = (unsigned char *) malloc( width*height*3 );
117 if (M_IMGDATA->m_data)
118 {
119 for (int l = 0; l < width*height*3; l++) M_IMGDATA->m_data[l] = 0;
120
121 M_IMGDATA->m_width = width;
122 M_IMGDATA->m_height = height;
123 M_IMGDATA->m_ok = TRUE;
124 }
125 else
126 {
127 UnRef();
128 }
129 }
130
131 void wxImage::Destroy()
132 {
133 UnRef();
134 }
135
136 wxImage wxImage::Scale( int width, int height )
137 {
138 wxImage image;
139
140 wxCHECK_MSG( Ok(), image, "invlaid image" );
141
142 wxCHECK_MSG( (width > 0) && (height > 0), image, "invalid image size" );
143
144 image.Create( width, height );
145
146 char unsigned *data = image.GetData();
147
148 wxCHECK_MSG( data, image, "unable to create image" );
149
150 if (M_IMGDATA->m_hasMask)
151 image.SetMaskColour( M_IMGDATA->m_maskRed, M_IMGDATA->m_maskGreen, M_IMGDATA->m_maskBlue );
152
153 double xscale = (double)width / (double)M_IMGDATA->m_width;
154 double yscale = (double)height / (double)M_IMGDATA->m_height;
155
156 for (int j = 0; j < height; j++)
157 {
158 for (int i = 0; i < width; i++)
159 {
160 int new_pos = 3*(j*width + i);
161 int old_pos = 3*((long)(j/yscale)*M_IMGDATA->m_width + (long)(i/xscale));
162 data[ new_pos ] = M_IMGDATA->m_data[ old_pos ];
163 data[ new_pos+1 ] = M_IMGDATA->m_data[ old_pos+1 ];
164 data[ new_pos+2 ] = M_IMGDATA->m_data[ old_pos+2 ];
165 }
166 }
167
168 return image;
169 }
170
171 void wxImage::SetRGB( int x, int y, unsigned char r, unsigned char g, unsigned char b )
172 {
173 wxCHECK_RET( Ok(), "invalid image" );
174
175 int w = M_IMGDATA->m_width;
176 int h = M_IMGDATA->m_height;
177
178 wxCHECK_RET( (x>=0) && (y>=0) && (x<w) && (y<h), "invalid image index" );
179
180 long pos = (y * w + x) * 3;
181
182 M_IMGDATA->m_data[ pos ] = r;
183 M_IMGDATA->m_data[ pos+1 ] = g;
184 M_IMGDATA->m_data[ pos+2 ] = b;
185 }
186
187 unsigned char wxImage::GetRed( int x, int y )
188 {
189 wxCHECK_MSG( Ok(), 0, "invalid image" );
190
191 int w = M_IMGDATA->m_width;
192 int h = M_IMGDATA->m_height;
193
194 wxCHECK_MSG( (x>=0) && (y>=0) && (x<w) && (y<h), 0, "invalid image index" );
195
196 long pos = (y * w + x) * 3;
197
198 return M_IMGDATA->m_data[pos];
199 }
200
201 unsigned char wxImage::GetGreen( int x, int y )
202 {
203 wxCHECK_MSG( Ok(), 0, "invalid image" );
204
205 int w = M_IMGDATA->m_width;
206 int h = M_IMGDATA->m_height;
207
208 wxCHECK_MSG( (x>=0) && (y>=0) && (x<w) && (y<h), 0, "invalid image index" );
209
210 long pos = (y * w + x) * 3;
211
212 return M_IMGDATA->m_data[pos+1];
213 }
214
215 unsigned char wxImage::GetBlue( int x, int y )
216 {
217 wxCHECK_MSG( Ok(), 0, "invalid image" );
218
219 int w = M_IMGDATA->m_width;
220 int h = M_IMGDATA->m_height;
221
222 wxCHECK_MSG( (x>=0) && (y>=0) && (x<w) && (y<h), 0, "invalid image index" );
223
224 long pos = (y * w + x) * 3;
225
226 return M_IMGDATA->m_data[pos+2];
227 }
228
229 bool wxImage::Ok() const
230 {
231 return (M_IMGDATA && M_IMGDATA->m_ok);
232 }
233
234 char unsigned *wxImage::GetData() const
235 {
236 wxCHECK_MSG( Ok(), (char unsigned *)NULL, "invalid image" );
237
238 return M_IMGDATA->m_data;
239 }
240
241 void wxImage::SetData( char unsigned *WXUNUSED(data) )
242 {
243 wxCHECK_RET( Ok(), "invalid image" );
244 }
245
246 void wxImage::SetMaskColour( unsigned char r, unsigned char g, unsigned char b )
247 {
248 wxCHECK_RET( Ok(), "invalid image" );
249
250 M_IMGDATA->m_maskRed = r;
251 M_IMGDATA->m_maskGreen = g;
252 M_IMGDATA->m_maskBlue = b;
253 M_IMGDATA->m_hasMask = TRUE;
254 }
255
256 unsigned char wxImage::GetMaskRed() const
257 {
258 wxCHECK_MSG( Ok(), 0, "invalid image" );
259
260 return M_IMGDATA->m_maskRed;
261 }
262
263 unsigned char wxImage::GetMaskGreen() const
264 {
265 wxCHECK_MSG( Ok(), 0, "invalid image" );
266
267 return M_IMGDATA->m_maskGreen;
268 }
269
270 unsigned char wxImage::GetMaskBlue() const
271 {
272 wxCHECK_MSG( Ok(), 0, "invalid image" );
273
274 return M_IMGDATA->m_maskBlue;
275 }
276
277 void wxImage::SetMask( bool mask )
278 {
279 wxCHECK_RET( Ok(), "invalid image" );
280
281 M_IMGDATA->m_hasMask = mask;
282 }
283
284 bool wxImage::HasMask() const
285 {
286 wxCHECK_MSG( Ok(), FALSE, "invalid image" );
287
288 return M_IMGDATA->m_hasMask;
289 }
290
291 int wxImage::GetWidth() const
292 {
293 wxCHECK_MSG( Ok(), 0, "invalid image" );
294
295 return M_IMGDATA->m_width;
296 }
297
298 int wxImage::GetHeight() const
299 {
300 wxCHECK_MSG( Ok(), 0, "invalid image" );
301
302 return M_IMGDATA->m_height;
303 }
304
305 bool wxImage::LoadFile( const wxString& filename, long type )
306 {
307 if (wxFileExists(filename))
308 {
309 wxFileInputStream stream(filename);
310 return LoadFile(stream, type);
311 }
312
313 else {
314 wxLogWarning( "Image file does not exist." );
315
316 return FALSE;
317 }
318 }
319
320 bool wxImage::LoadFile( wxInputStream& stream, long type )
321 {
322 UnRef();
323
324 m_refData = new wxImageRefData;
325
326 wxImageHandler *handler = FindHandler(type);
327
328 if (handler == NULL)
329 {
330 wxLogWarning( "No image handler for type %d defined.", type );
331
332 return FALSE;
333 }
334
335 return handler->LoadFile( this, stream );
336 }
337
338 bool wxImage::SaveFile( const wxString& filename, int type )
339 {
340 wxFileOutputStream stream(filename);
341
342 if ( stream.LastError() == wxStream_NOERROR )
343 return SaveFile(stream, type);
344 else
345 return FALSE;
346 }
347
348 bool wxImage::SaveFile( wxOutputStream& stream, int type )
349 {
350 wxCHECK_MSG( Ok(), FALSE, "invalid image" );
351
352 wxImageHandler *handler = FindHandler(type);
353
354 if (handler == NULL)
355 {
356 wxLogWarning( "No image handler for type %d defined.", type );
357
358 return FALSE;
359 }
360
361 return handler->SaveFile( this, stream );
362 }
363
364 void wxImage::AddHandler( wxImageHandler *handler )
365 {
366 // make sure that the memory will be freed at the program end
367 sm_handlers.DeleteContents(TRUE);
368
369 sm_handlers.Append( handler );
370 }
371
372 void wxImage::InsertHandler( wxImageHandler *handler )
373 {
374 // make sure that the memory will be freed at the program end
375 sm_handlers.DeleteContents(TRUE);
376
377 sm_handlers.Insert( handler );
378 }
379
380 bool wxImage::RemoveHandler( const wxString& name )
381 {
382 wxImageHandler *handler = FindHandler(name);
383 if (handler)
384 {
385 sm_handlers.DeleteObject(handler);
386 return TRUE;
387 }
388 else
389 return FALSE;
390 }
391
392 wxImageHandler *wxImage::FindHandler( const wxString& name )
393 {
394 wxNode *node = sm_handlers.First();
395 while (node)
396 {
397 wxImageHandler *handler = (wxImageHandler*)node->Data();
398 if (handler->GetName() == name) return handler;
399 node = node->Next();
400 }
401 return (wxImageHandler *)NULL;
402 }
403
404 wxImageHandler *wxImage::FindHandler( const wxString& extension, long bitmapType )
405 {
406 wxNode *node = sm_handlers.First();
407 while (node)
408 {
409 wxImageHandler *handler = (wxImageHandler*)node->Data();
410 if ( handler->GetExtension() == extension &&
411 (bitmapType == -1 || handler->GetType() == bitmapType) )
412 return handler;
413 node = node->Next();
414 }
415 return (wxImageHandler*)NULL;
416 }
417
418 wxImageHandler *wxImage::FindHandler( long bitmapType )
419 {
420 wxNode *node = sm_handlers.First();
421 while (node)
422 {
423 wxImageHandler *handler = (wxImageHandler *)node->Data();
424 if (handler->GetType() == bitmapType) return handler;
425 node = node->Next();
426 }
427 return NULL;
428 }
429
430 void wxImage::InitStandardHandlers()
431 {
432 AddHandler( new wxBMPHandler );
433 #ifdef wxUSE_LIBPNG
434 AddHandler( new wxPNGHandler );
435 #endif
436 }
437
438 void wxImage::CleanUpHandlers()
439 {
440 wxNode *node = sm_handlers.First();
441 while (node)
442 {
443 wxImageHandler *handler = (wxImageHandler *)node->Data();
444 wxNode *next = node->Next();
445 delete handler;
446 delete node;
447 node = next;
448 }
449 }
450
451 //-----------------------------------------------------------------------------
452 // wxImageHandler
453 //-----------------------------------------------------------------------------
454
455 #if !USE_SHARED_LIBRARIES
456 IMPLEMENT_DYNAMIC_CLASS(wxImageHandler,wxObject)
457 #endif
458
459 bool wxImageHandler::LoadFile( wxImage *WXUNUSED(image), wxInputStream& WXUNUSED(stream) )
460 {
461 return FALSE;
462 }
463
464 bool wxImageHandler::SaveFile( wxImage *WXUNUSED(image), wxOutputStream& WXUNUSED(stream) )
465 {
466 return FALSE;
467 }
468
469 //-----------------------------------------------------------------------------
470 // wxPNGHandler
471 //-----------------------------------------------------------------------------
472
473 #ifdef wxUSE_LIBPNG
474
475 #if !USE_SHARED_LIBRARIES
476 IMPLEMENT_DYNAMIC_CLASS(wxPNGHandler,wxImageHandler)
477 #endif
478
479
480 static void _PNG_stream_reader( png_structp png_ptr, png_bytep data, png_size_t length )
481 {
482 ((wxInputStream*) png_get_io_ptr( png_ptr )) -> Read(data, length);
483 }
484
485 static void _PNG_stream_writer( png_structp png_ptr, png_bytep data, png_size_t length )
486 {
487 ((wxOutputStream*) png_get_io_ptr( png_ptr )) -> Write(data, length);
488 }
489
490 bool wxPNGHandler::LoadFile( wxImage *image, wxInputStream& stream )
491 {
492 // VZ: as this function uses setjmp() the only fool proof error handling
493 // method is to use goto (setjmp is not really C++ dtors friendly...)
494
495 unsigned char **lines = (unsigned char **) NULL;
496 unsigned int i;
497 png_infop info_ptr = (png_infop) NULL;
498
499 image->Destroy();
500
501 png_structp png_ptr = png_create_read_struct( PNG_LIBPNG_VER_STRING,
502 (voidp) NULL,
503 (png_error_ptr) NULL,
504 (png_error_ptr) NULL );
505 if (!png_ptr)
506 goto error;
507
508 info_ptr = png_create_info_struct( png_ptr );
509 if (!info_ptr)
510 goto error;
511
512 if (setjmp(png_ptr->jmpbuf))
513 goto error;
514
515 if (info_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
516 goto error;
517
518 png_set_read_fn( png_ptr, &stream, _PNG_stream_reader);
519
520 png_uint_32 width,height;
521 int bit_depth,color_type,interlace_type;
522
523 png_read_info( png_ptr, info_ptr );
524 png_get_IHDR( png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, &interlace_type, (int*) NULL, (int*) NULL );
525
526 if (color_type == PNG_COLOR_TYPE_PALETTE)
527 png_set_expand( png_ptr );
528
529 png_set_strip_16( png_ptr );
530 png_set_packing( png_ptr );
531 if (png_get_valid( png_ptr, info_ptr, PNG_INFO_tRNS))
532 png_set_expand( png_ptr );
533 png_set_filler( png_ptr, 0xff, PNG_FILLER_AFTER );
534
535 image->Create( width, height );
536
537 if (!image->Ok())
538 goto error;
539
540 lines = (unsigned char **)malloc( height * sizeof(unsigned char *) );
541 if (lines == NULL)
542 goto error;
543
544 for (i = 0; i < height; i++)
545 {
546 if ((lines[i] = (unsigned char *)malloc(width * (sizeof(unsigned char) * 4))) == NULL)
547 {
548 for ( unsigned int n = 0; n < i; n++ )
549 free( lines[n] );
550 goto error;
551 }
552 }
553
554 // loaded successfully!
555 {
556 int transp = 0;
557 png_read_image( png_ptr, lines );
558 png_destroy_read_struct( &png_ptr, &info_ptr, (png_infopp) NULL );
559 unsigned char *ptr = image->GetData();
560 if ((color_type == PNG_COLOR_TYPE_GRAY) ||
561 (color_type == PNG_COLOR_TYPE_GRAY_ALPHA))
562 {
563 for (unsigned int y = 0; y < height; y++)
564 {
565 unsigned char *ptr2 = lines[y];
566 for (unsigned int x = 0; x < width; x++)
567 {
568 unsigned char r = *ptr2++;
569 unsigned char a = *ptr2++;
570 if (a < 128)
571 {
572 *ptr++ = 255;
573 *ptr++ = 0;
574 *ptr++ = 255;
575 transp = 1;
576 }
577 else
578 {
579 *ptr++ = r;
580 *ptr++ = r;
581 *ptr++ = r;
582 }
583 }
584 }
585 }
586 else
587 {
588 for (unsigned int y = 0; y < height; y++)
589 {
590 unsigned char *ptr2 = lines[y];
591 for (unsigned int x = 0; x < width; x++)
592 {
593 unsigned char r = *ptr2++;
594 unsigned char g = *ptr2++;
595 unsigned char b = *ptr2++;
596 unsigned char a = *ptr2++;
597 if (a < 128)
598 {
599 *ptr++ = 255;
600 *ptr++ = 0;
601 *ptr++ = 255;
602 transp = 1;
603 }
604 else
605 {
606 if ((r == 255) && (g == 0) && (b == 255)) r = 254;
607 *ptr++ = r;
608 *ptr++ = g;
609 *ptr++ = b;
610 }
611 }
612 }
613 }
614
615 for ( unsigned int j = 0; j < height; j++ )
616 free( lines[j] );
617 free( lines );
618
619 if (transp)
620 {
621 image->SetMaskColour( 255, 0, 255 );
622 }
623 else
624 {
625 image->SetMask( FALSE );
626 }
627 }
628
629 return TRUE;
630
631 error:
632 wxLogError(_("Couldn't load a PNG image - probably file is corrupted."));
633
634 if ( image->Ok() )
635 {
636 image->Destroy();
637 }
638
639 if ( lines )
640 {
641 free( lines );
642 }
643
644 if ( png_ptr )
645 {
646 if ( info_ptr )
647 {
648 png_destroy_read_struct( &png_ptr, &info_ptr, (png_infopp) NULL );
649 free(info_ptr);
650 }
651 else
652 png_destroy_read_struct( &png_ptr, (png_infopp) NULL, (png_infopp) NULL );
653 }
654 return FALSE;
655 }
656
657
658 bool wxPNGHandler::SaveFile( wxImage *image, wxOutputStream& stream )
659 {
660 {
661 png_structp png_ptr = png_create_write_struct( PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
662 if (!png_ptr)
663 {
664 return FALSE;
665 }
666
667 png_infop info_ptr = png_create_info_struct(png_ptr);
668 if (info_ptr == NULL)
669 {
670 png_destroy_write_struct( &png_ptr, (png_infopp)NULL );
671 return FALSE;
672 }
673
674 if (setjmp(png_ptr->jmpbuf))
675 {
676 png_destroy_write_struct( &png_ptr, (png_infopp)NULL );
677 return FALSE;
678 }
679
680 png_set_write_fn( png_ptr, &stream, _PNG_stream_writer, NULL);
681
682 png_set_IHDR( png_ptr, info_ptr, image->GetWidth(), image->GetHeight(), 8,
683 PNG_COLOR_TYPE_RGB_ALPHA, PNG_INTERLACE_NONE,
684 PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
685
686 png_color_8 sig_bit;
687 sig_bit.red = 8;
688 sig_bit.green = 8;
689 sig_bit.blue = 8;
690 sig_bit.alpha = 8;
691 png_set_sBIT( png_ptr, info_ptr, &sig_bit );
692 png_write_info( png_ptr, info_ptr );
693 png_set_shift( png_ptr, &sig_bit );
694 png_set_packing( png_ptr );
695
696 unsigned char *data = (unsigned char *)malloc( image->GetWidth()*4 );
697 if (!data)
698 {
699 png_destroy_write_struct( &png_ptr, (png_infopp)NULL );
700 return FALSE;
701 }
702
703 for (int y = 0; y < image->GetHeight(); y++)
704 {
705 unsigned char *ptr = image->GetData() + (y * image->GetWidth() * 3);
706 for (int x = 0; x < image->GetWidth(); x++)
707 {
708 data[(x << 2) + 0] = *ptr++;
709 data[(x << 2) + 1] = *ptr++;
710 data[(x << 2) + 2] = *ptr++;
711 if ((data[(x << 2) + 0] == image->GetMaskRed()) &&
712 (data[(x << 2) + 1] == image->GetMaskGreen()) &&
713 (data[(x << 2) + 2] == image->GetMaskBlue()))
714 {
715 data[(x << 2) + 3] = 0;
716 }
717 else
718 {
719 data[(x << 2) + 3] = 255;
720 }
721 }
722 png_bytep row_ptr = data;
723 png_write_rows( png_ptr, &row_ptr, 1 );
724 }
725
726 free(data);
727 png_write_end( png_ptr, info_ptr );
728 png_destroy_write_struct( &png_ptr, (png_infopp)&info_ptr );
729 }
730 return TRUE;
731 }
732
733 #endif
734
735 // wxUSE_LIBPNG
736
737 //-----------------------------------------------------------------------------
738 // wxBMPHandler
739 //-----------------------------------------------------------------------------
740
741 #if !USE_SHARED_LIBRARIES
742 IMPLEMENT_DYNAMIC_CLASS(wxBMPHandler,wxImageHandler)
743 #endif
744
745 bool wxBMPHandler::LoadFile( wxImage *image, wxInputStream& stream )
746 {
747 unsigned char *data, *ptr;
748 int done, i, bpp, planes, comp, ncolors, line, column,
749 linesize, linepos, rshift = 0, gshift = 0, bshift = 0;
750 unsigned char aByte;
751 short int word;
752 long int dbuf[4], dword, rmask = 0, gmask = 0, bmask = 0, offset,
753 size;
754 off_t start_offset = stream.TellI();
755 signed char bbuf[4];
756 struct _cmap
757 {
758 unsigned char r, g, b;
759 }
760 *cmap = NULL;
761 #ifndef BI_RGB
762 #define BI_RGB 0
763 #define BI_RLE8 1
764 #define BI_RLE4 2
765 #endif
766
767 #ifndef BI_BITFIELDS
768 #define BI_BITFIELDS 3
769 #endif
770
771 image->Destroy();
772
773 done = 0;
774 /*
775 * Reading the bmp header
776 */
777
778 stream.Read(&bbuf, 2);
779
780 stream.Read(dbuf, 4 * 4);
781
782 size = dbuf[0];
783 offset = dbuf[2];
784
785 stream.Read(dbuf, 4 * 2);
786 int width = (int)dbuf[0];
787 int height = (int)dbuf[1];
788 if (width > 32767)
789 {
790 wxLogError( "Image width > 32767 pixels for file\n" );
791 return FALSE;
792 }
793 if (height > 32767)
794 {
795 wxLogError( "Image height > 32767 pixels for file\n" );
796 return FALSE;
797 }
798 stream.Read(&word, 2);
799 planes = (int)word;
800 stream.Read(&word, 2);
801 bpp = (int)word;
802 if (bpp != 1 && bpp != 4 && bpp != 8 && bpp && 16 && bpp != 24 && bpp != 32)
803 {
804 wxLogError( "unknown bitdepth in file\n" );
805 return FALSE;
806 }
807 stream.Read(dbuf, 4 * 4);
808 comp = (int)dbuf[0];
809 if (comp != BI_RGB && comp != BI_RLE4 && comp != BI_RLE8 && comp != BI_BITFIELDS)
810 {
811 wxLogError( "unknown encoding in Windows BMP file\n" );
812 return FALSE;
813 }
814 stream.Read(dbuf, 4 * 2);
815 ncolors = (int)dbuf[0];
816 if (ncolors == 0)
817 ncolors = 1 << bpp;
818 /* some more sanity checks */
819 if (((comp == BI_RLE4) && (bpp != 4)) || ((comp == BI_RLE8) && (bpp != 8)) || ((comp == BI_BITFIELDS) && (bpp != 16 && bpp != 32)))
820 {
821 wxLogError( "encoding of BMP doesn't match bitdepth\n" );
822 return FALSE;
823 }
824 if (bpp < 16)
825 {
826 cmap = (struct _cmap *)malloc(sizeof(struct _cmap) * ncolors);
827
828 if (!cmap)
829 {
830 wxLogError( "Cannot allocate RAM for color map in BMP file\n" );
831 return FALSE;
832 }
833 }
834 else
835 cmap = NULL;
836
837 image->Create( width, height );
838 ptr = image->GetData();
839 if (!ptr)
840 {
841 wxLogError( "Cannot allocate RAM for RGB data in file\n" );
842 if (cmap)
843 free(cmap);
844 return FALSE;
845 }
846
847 /*
848 * Reading the palette, if it exists.
849 */
850 if (bpp < 16 && ncolors != 0)
851 {
852 for (i = 0; i < ncolors; i++)
853 {
854 stream.Read(bbuf, 4);
855 cmap[i].b = bbuf[0];
856 cmap[i].g = bbuf[1];
857 cmap[i].r = bbuf[2];
858 }
859 }
860 else if (bpp == 16 || bpp == 32)
861 {
862 if (comp == BI_BITFIELDS)
863 {
864 int bit = 0;
865
866 stream.Read(dbuf, 4 * 3);
867 bmask = dbuf[0];
868 gmask = dbuf[1];
869 rmask = dbuf[2];
870 /* find shift amount.. ugly, but i can't think of a better way */
871 for (bit = 0; bit < bpp; bit++)
872 {
873 if (bmask & (1 << bit))
874 bshift = bit;
875 if (gmask & (1 << bit))
876 gshift = bit;
877 if (rmask & (1 << bit))
878 rshift = bit;
879 }
880 }
881 else if (bpp == 16)
882 {
883 rmask = 0x7C00;
884 gmask = 0x03E0;
885 bmask = 0x001F;
886 rshift = 10;
887 gshift = 5;
888 bshift = 0;
889 }
890 else if (bpp == 32)
891 {
892 rmask = 0x00FF0000;
893 gmask = 0x0000FF00;
894 bmask = 0x000000FF;
895 rshift = 16;
896 gshift = 8;
897 bshift = 0;
898 }
899 }
900
901 /*
902 * Reading the image data
903 */
904 stream.SeekI(start_offset + offset);
905 data = ptr;
906
907 /* set the whole image to the background color */
908 if (bpp < 16 && (comp == BI_RLE4 || comp == BI_RLE8))
909 {
910 for (i = 0; i < width * height; i++)
911 {
912 *ptr++ = cmap[0].r;
913 *ptr++ = cmap[0].g;
914 *ptr++ = cmap[0].b;
915 }
916 ptr = data;
917 }
918 line = 0;
919 column = 0;
920 #define poffset (line * width * 3 + column * 3)
921
922 /*
923 * BMPs are stored upside down... hmmmmmmmmmm....
924 */
925
926 linesize = ((width * bpp + 31) / 32) * 4;
927 for (line = (height - 1); line >= 0; line--)
928 {
929 linepos = 0;
930 for (column = 0; column < width;)
931 {
932 if (bpp < 16)
933 {
934 int index;
935
936 linepos++;
937 aByte = stream.GetC();
938 if (bpp == 1)
939 {
940 int bit = 0;
941
942 for (bit = 0; bit < 8; bit++)
943 {
944 index = ((aByte & (0x80 >> bit)) ? 1 : 0);
945 ptr[poffset] = cmap[index].r;
946 ptr[poffset + 1] = cmap[index].g;
947 ptr[poffset + 2] = cmap[index].b;
948 column++;
949 }
950 }
951 else if (bpp == 4)
952 {
953 if (comp == BI_RLE4)
954 {
955 wxLogError( "can't deal with 4bit encoded yet.\n");
956 image->Destroy();
957 free(cmap);
958 return FALSE;
959 }
960 else
961 {
962 int nibble = 0;
963
964 for (nibble = 0; nibble < 2; nibble++)
965 {
966 index = ((aByte & (0xF0 >> nibble * 4)) >> (!nibble * 4));
967 if (index >= 16)
968 index = 15;
969 ptr[poffset] = cmap[index].r;
970 ptr[poffset + 1] = cmap[index].g;
971 ptr[poffset + 2] = cmap[index].b;
972 column++;
973 }
974 }
975 }
976 else if (bpp == 8)
977 {
978 if (comp == BI_RLE8)
979 {
980 unsigned char first;
981
982 first = aByte;
983 aByte = stream.GetC();
984 if (first == 0)
985 {
986 if (aByte == 0)
987 {
988 /* column = width; */
989 }
990 else if (aByte == 1)
991 {
992 column = width;
993 line = -1;
994 }
995 else if (aByte == 2)
996 {
997 aByte = stream.GetC();
998 column += aByte;
999 linepos = column * bpp / 8;
1000 aByte = stream.GetC();
1001 line += aByte;
1002 }
1003 else
1004 {
1005 int absolute = aByte;
1006
1007 for (i = 0; i < absolute; i++)
1008 {
1009 linepos++;
1010 aByte = stream.GetC();
1011 ptr[poffset] = cmap[aByte].r;
1012 ptr[poffset + 1] = cmap[aByte].g;
1013 ptr[poffset + 2] = cmap[aByte].b;
1014 column++;
1015 }
1016 if (absolute & 0x01)
1017 aByte = stream.GetC();
1018 }
1019 }
1020 else
1021 {
1022 for (i = 0; i < first; i++)
1023 {
1024 ptr[poffset] = cmap[aByte].r;
1025 ptr[poffset + 1] = cmap[aByte].g;
1026 ptr[poffset + 2] = cmap[aByte].b;
1027 column++;
1028 linepos++;
1029 }
1030 }
1031 }
1032 else
1033 {
1034 ptr[poffset] = cmap[aByte].r;
1035 ptr[poffset + 1] = cmap[aByte].g;
1036 ptr[poffset + 2] = cmap[aByte].b;
1037 column++;
1038 linepos += size;
1039 }
1040 }
1041 }
1042 else if (bpp == 24)
1043 {
1044 stream.Read(&bbuf, 3);
1045 linepos += 3;
1046 ptr[poffset] = (unsigned char)bbuf[2];
1047 ptr[poffset + 1] = (unsigned char)bbuf[1];
1048 ptr[poffset + 2] = (unsigned char)bbuf[0];
1049 column++;
1050 }
1051 else if (bpp == 16)
1052 {
1053 unsigned char temp;
1054
1055 stream.Read(&word, 2);
1056 linepos += 2;
1057 temp = (word & rmask) >> rshift;
1058 ptr[poffset] = temp;
1059 temp = (word & gmask) >> gshift;
1060 ptr[poffset + 1] = temp;
1061 temp = (word & bmask) >> gshift;
1062 ptr[poffset + 2] = temp;
1063 column++;
1064 }
1065 else
1066 {
1067 unsigned char temp;
1068
1069 stream.Read(&dword, 4);
1070 linepos += 4;
1071 temp = (dword & rmask) >> rshift;
1072 ptr[poffset] = temp;
1073 temp = (dword & gmask) >> gshift;
1074 ptr[poffset + 1] = temp;
1075 temp = (dword & bmask) >> bshift;
1076 ptr[poffset + 2] = temp;
1077 column++;
1078 }
1079 }
1080 while ((linepos < linesize) && (comp != 1) && (comp != 2))
1081 {
1082 stream.Read(&aByte, 1);
1083 linepos += 1;
1084 if (stream.LastError() != wxStream_NOERROR)
1085 break;
1086 }
1087 }
1088 if (cmap) free(cmap);
1089
1090 image->SetMask( FALSE );
1091
1092 return TRUE;
1093 }
1094
1095 #ifdef __WXMSW__
1096
1097 wxBitmap wxImage::ConvertToBitmap() const
1098 {
1099
1100 wxBitmap bitmap;
1101 wxCHECK_MSG( Ok(), bitmap, "invalid image" );
1102 int width = GetWidth();
1103 int height = GetHeight();
1104 bitmap.SetWidth( width );
1105 bitmap.SetHeight( height );
1106 bitmap.SetDepth( wxDisplayDepth() );
1107
1108 int headersize = sizeof(BITMAPINFOHEADER);
1109 LPBITMAPINFO lpDIBh = (BITMAPINFO *) malloc( headersize );
1110 wxCHECK_MSG( lpDIBh, bitmap, "could not allocate memory for DIB header" );
1111
1112 // Fill in the DIB header
1113 lpDIBh->bmiHeader.biSize = headersize;
1114 lpDIBh->bmiHeader.biWidth = width;
1115 lpDIBh->bmiHeader.biHeight = -height;
1116 lpDIBh->bmiHeader.biSizeImage = width * height * 3;
1117
1118 lpDIBh->bmiHeader.biPlanes = 1;
1119 lpDIBh->bmiHeader.biBitCount = 24;
1120 lpDIBh->bmiHeader.biCompression = BI_RGB;
1121 lpDIBh->bmiHeader.biClrUsed = 0;
1122
1123 // These seem not needed for our purpose here.
1124 // lpDIBh->bmiHeader.biClrImportant = 0;
1125 // lpDIBh->bmiHeader.biXPelsPerMeter = 0;
1126 // lpDIBh->bmiHeader.biYPelsPerMeter = 0;
1127
1128 unsigned char *lpBits = (unsigned char *) malloc( width*height*3 );
1129 if( !lpBits )
1130 {
1131 wxFAIL_MSG( "could not allocate memory for DIB" );
1132 free( lpDIBh );
1133 return bitmap;
1134 }
1135
1136 unsigned char *data = GetData();
1137
1138 unsigned char *ptdata = data, *ptbits = lpBits;
1139 for( int i=0; i<width*height; i++ )
1140 {
1141 *(ptbits++) = *(ptdata+2);
1142 *(ptbits++) = *(ptdata+1);
1143 *(ptbits++) = *(ptdata );
1144 ptdata += 3;
1145 }
1146
1147 HDC hdc = ::GetDC(NULL);
1148
1149 HBITMAP hbitmap;
1150 hbitmap = CreateDIBitmap( hdc, &(lpDIBh->bmiHeader), CBM_INIT, lpBits, lpDIBh, DIB_RGB_COLORS );
1151
1152 // The above line is equivalent to the following two lines.
1153 // hbitmap = ::CreateCompatibleBitmap( hdc, width, height );
1154 // ::SetDIBits( hdc, hbitmap, 0, height, lpBits, lpDIBh, DIB_RGB_COLORS);
1155 // or the following lines
1156 // hbitmap = ::CreateCompatibleBitmap( hdc, width, height );
1157 // HDC memdc = ::CreateCompatibleDC( hdc );
1158 // ::SelectObject( memdc, hbitmap);
1159 // ::SetDIBitsToDevice( memdc, 0, 0, width, height,
1160 // 0, 0, 0, height, (void *)lpBits, lpDIBh, DIB_RGB_COLORS);
1161 // ::SelectObject( memdc, 0 );
1162 // ::DeleteDC( memdc );
1163
1164 bitmap.SetHBITMAP( (WXHBITMAP) hbitmap );
1165
1166 if( HasMask() )
1167 {
1168 unsigned char r = GetMaskRed();
1169 unsigned char g = GetMaskGreen();
1170 unsigned char b = GetMaskBlue();
1171 unsigned char zero = 0, one = 255;
1172 ptdata = data;
1173 ptbits = lpBits;
1174 for( int i=0; i<width*height; i++ )
1175 {
1176 if( (*(ptdata++)!=r) | (*(ptdata++)!=g) | (*(ptdata++)!=b) )
1177 {
1178 *(ptbits++) = one;
1179 *(ptbits++) = one;
1180 *(ptbits++) = one;
1181 }
1182 else
1183 {
1184 *(ptbits++) = zero;
1185 *(ptbits++) = zero;
1186 *(ptbits++) = zero;
1187 }
1188 }
1189 hbitmap = ::CreateBitmap( (WORD)width, (WORD)height, 1, 1, NULL );
1190 ::SetDIBits( hdc, hbitmap, 0, (WORD)height, lpBits, lpDIBh, DIB_RGB_COLORS);
1191 wxMask *mask = new wxMask();
1192 mask->SetMaskBitmap( (WXHBITMAP) hbitmap );
1193 bitmap.SetMask( mask );
1194
1195 /* The following can also be used but is slow to run
1196 wxColour colour( GetMaskRed(), GetMaskGreen(), GetMaskBlue());
1197 wxMask *mask = new wxMask( bitmap, colour );
1198 bitmap.SetMask( mask );
1199 */
1200 }
1201
1202 ::ReleaseDC(NULL, hdc);
1203 free(lpDIBh);
1204 free(lpBits);
1205
1206 if( bitmap.GetHBITMAP() )
1207 bitmap.SetOk( TRUE );
1208 else
1209 bitmap.SetOk( FALSE );
1210
1211 return bitmap;
1212 }
1213
1214
1215 wxImage::wxImage( const wxBitmap &bitmap )
1216 {
1217 if( !bitmap.Ok() )
1218 {
1219 wxFAIL_MSG( "invalid bitmap" );
1220 return;
1221 }
1222
1223 int width = bitmap.GetWidth();
1224 int height = bitmap.GetHeight();
1225 Create( width, height );
1226 unsigned char *data = GetData();
1227 if( !data )
1228 {
1229 wxFAIL_MSG( "could not allocate data for image" );
1230 return;
1231 }
1232
1233 int headersize = sizeof(BITMAPINFOHEADER);
1234 LPBITMAPINFO lpDIBh = (BITMAPINFO *) malloc( headersize );
1235 if( !lpDIBh )
1236 {
1237 wxFAIL_MSG( "could not allocate data for DIB header" );
1238 free( data );
1239 return;
1240 }
1241
1242 // Fill in the DIB header
1243 lpDIBh->bmiHeader.biSize = headersize;
1244 lpDIBh->bmiHeader.biWidth = width;
1245 lpDIBh->bmiHeader.biHeight = -height;
1246 lpDIBh->bmiHeader.biSizeImage = width * height * 3;
1247
1248 lpDIBh->bmiHeader.biPlanes = 1;
1249 lpDIBh->bmiHeader.biBitCount = 24;
1250 lpDIBh->bmiHeader.biCompression = BI_RGB;
1251 lpDIBh->bmiHeader.biClrUsed = 0;
1252
1253 // These seem not needed for our purpose here.
1254 // lpDIBh->bmiHeader.biClrImportant = 0;
1255 // lpDIBh->bmiHeader.biXPelsPerMeter = 0;
1256 // lpDIBh->bmiHeader.biYPelsPerMeter = 0;
1257
1258 unsigned char *lpBits = (unsigned char *) malloc( width*height*3 );
1259 if( !lpBits )
1260 {
1261 wxFAIL_MSG( "could not allocate data for DIB" );
1262 free( data );
1263 free( lpDIBh );
1264 return;
1265 }
1266
1267 HBITMAP hbitmap;
1268 hbitmap = (HBITMAP) bitmap.GetHBITMAP();
1269 HDC hdc = ::GetDC(NULL);
1270 ::GetDIBits( hdc, hbitmap, 0, height, lpBits, lpDIBh, DIB_RGB_COLORS );
1271
1272 unsigned char *ptdata = data, *ptbits = lpBits;
1273 for( int i=0; i<width*height; i++ )
1274 {
1275 *(ptdata++) = *(ptbits+2);
1276 *(ptdata++) = *(ptbits+1);
1277 *(ptdata++) = *(ptbits );
1278 ptbits += 3;
1279 }
1280
1281 if( bitmap.GetMask() && bitmap.GetMask()->GetMaskBitmap() )
1282 {
1283 hbitmap = (HBITMAP) bitmap.GetMask()->GetMaskBitmap();
1284 HDC memdc = ::CreateCompatibleDC( hdc );
1285 ::SetTextColor( memdc, RGB( 0, 0, 0 ) );
1286 ::SetBkColor( memdc, RGB( 255, 255, 255 ) );
1287 ::GetDIBits( memdc, hbitmap, 0, height, lpBits, lpDIBh, DIB_RGB_COLORS );
1288 ::DeleteDC( memdc );
1289 unsigned char r=16, g=16, b=16; // background set to RGB(16,16,16)
1290 ptdata = data;
1291 ptbits = lpBits;
1292 for( int i=0; i<width*height; i++ )
1293 {
1294 if( *ptbits != 0 )
1295 {
1296 *(ptdata++) = r;
1297 *(ptdata++) = g;
1298 *(ptdata++) = b;
1299 }
1300 ptbits += 3;
1301 }
1302 SetMaskColour( r, g, b );
1303 }
1304
1305 ::ReleaseDC(NULL, hdc);
1306 free(lpDIBh);
1307 free(lpBits);
1308 }
1309
1310 #endif
1311
1312 #ifdef __WXGTK__
1313
1314 #include "gtk/gtk.h"
1315 #include "gdk/gdk.h"
1316 #include "gdk/gdkx.h"
1317
1318 wxBitmap wxImage::ConvertToBitmap() const
1319 {
1320 wxBitmap bitmap;
1321
1322 wxCHECK_MSG( Ok(), bitmap, "invalid image" );
1323
1324 int width = GetWidth();
1325 int height = GetHeight();
1326
1327 bitmap.SetHeight( height );
1328 bitmap.SetWidth( width );
1329
1330 // Create picture
1331
1332 GdkImage *data_image =
1333 gdk_image_new( GDK_IMAGE_FASTEST, gdk_visual_get_system(), width, height );
1334
1335 bitmap.SetPixmap( gdk_pixmap_new( (GdkWindow*)&gdk_root_parent, width, height, -1 ) );
1336
1337 // Create mask
1338
1339 GdkImage *mask_image = (GdkImage*) NULL;
1340
1341 if (HasMask())
1342 {
1343 unsigned char *mask_data = (unsigned char*)malloc( ((width >> 3)+8) * height );
1344
1345 mask_image = gdk_image_new_bitmap( gdk_visual_get_system(), mask_data, width, height );
1346
1347 wxMask *mask = new wxMask();
1348 mask->m_bitmap = gdk_pixmap_new( (GdkWindow*)&gdk_root_parent, width, height, 1 );
1349
1350 bitmap.SetMask( mask );
1351 }
1352
1353 // Retrieve depth
1354
1355 GdkVisual *visual = gdk_window_get_visual( bitmap.GetPixmap() );
1356 if (visual == NULL) visual = gdk_window_get_visual( (GdkWindow*) &gdk_root_parent );
1357 int bpp = visual->depth;
1358
1359 bitmap.SetDepth( bpp );
1360
1361 if ((bpp == 16) && (visual->red_mask != 0xf800)) bpp = 15;
1362 if (bpp < 8) bpp = 8;
1363
1364 // Render
1365
1366 enum byte_order { RGB, RBG, BRG, BGR, GRB, GBR };
1367 byte_order b_o = RGB;
1368
1369 if (bpp >= 24)
1370 {
1371 GdkVisual *visual = gdk_visual_get_system();
1372 if ((visual->red_mask > visual->green_mask) && (visual->green_mask > visual->blue_mask)) b_o = RGB;
1373 else if ((visual->red_mask > visual->blue_mask) && (visual->blue_mask > visual->green_mask)) b_o = RGB;
1374 else if ((visual->blue_mask > visual->red_mask) && (visual->red_mask > visual->green_mask)) b_o = BRG;
1375 else if ((visual->blue_mask > visual->green_mask) && (visual->green_mask > visual->red_mask)) b_o = BGR;
1376 else if ((visual->green_mask > visual->red_mask) && (visual->red_mask > visual->blue_mask)) b_o = GRB;
1377 else if ((visual->green_mask > visual->blue_mask) && (visual->blue_mask > visual->red_mask)) b_o = GBR;
1378 }
1379
1380 int r_mask = GetMaskRed();
1381 int g_mask = GetMaskGreen();
1382 int b_mask = GetMaskBlue();
1383
1384 unsigned char* data = GetData();
1385
1386 int index = 0;
1387 for (int y = 0; y < height; y++)
1388 {
1389 for (int x = 0; x < width; x++)
1390 {
1391 int r = data[index];
1392 index++;
1393 int g = data[index];
1394 index++;
1395 int b = data[index];
1396 index++;
1397
1398 if (HasMask())
1399 {
1400 if ((r == r_mask) && (b == b_mask) && (g == g_mask))
1401 gdk_image_put_pixel( mask_image, x, y, 1 );
1402 else
1403 gdk_image_put_pixel( mask_image, x, y, 0 );
1404 }
1405
1406 if (HasMask())
1407 {
1408 if ((r == r_mask) && (b == b_mask) && (g == g_mask))
1409 gdk_image_put_pixel( mask_image, x, y, 1 );
1410 else
1411 gdk_image_put_pixel( mask_image, x, y, 0 );
1412 }
1413
1414 switch (bpp)
1415 {
1416 case 8:
1417 {
1418 int pixel = -1;
1419 if (wxTheApp->m_colorCube)
1420 {
1421 pixel = wxTheApp->m_colorCube[ ((r & 0xf8) << 7) + ((g & 0xf8) << 2) + ((b & 0xf8) >> 3) ];
1422 }
1423 else
1424 {
1425 GdkColormap *cmap = gtk_widget_get_default_colormap();
1426 GdkColor *colors = cmap->colors;
1427 int max = 3 * (65536);
1428
1429 for (int i = 0; i < cmap->size; i++)
1430 {
1431 int rdiff = (r << 8) - colors[i].red;
1432 int gdiff = (g << 8) - colors[i].green;
1433 int bdiff = (b << 8) - colors[i].blue;
1434 int sum = ABS (rdiff) + ABS (gdiff) + ABS (bdiff);
1435 if (sum < max) { pixel = i; max = sum; }
1436 }
1437 }
1438
1439 gdk_image_put_pixel( data_image, x, y, pixel );
1440
1441 break;
1442 }
1443 case 15:
1444 {
1445 guint32 pixel = ((r & 0xf8) << 7) | ((g & 0xf8) << 2) | ((b & 0xf8) >> 3);
1446 gdk_image_put_pixel( data_image, x, y, pixel );
1447 break;
1448 }
1449 case 16:
1450 {
1451 guint32 pixel = ((r & 0xf8) << 8) | ((g & 0xfc) << 3) | ((b & 0xf8) >> 3);
1452 gdk_image_put_pixel( data_image, x, y, pixel );
1453 break;
1454 }
1455 case 32:
1456 case 24:
1457 {
1458 guint32 pixel = 0;
1459 switch (b_o)
1460 {
1461 case RGB: pixel = (r << 16) | (g << 8) | b; break;
1462 case RBG: pixel = (r << 16) | (b << 8) | g; break;
1463 case BRG: pixel = (b << 16) | (r << 8) | g; break;
1464 case BGR: pixel = (b << 16) | (g << 8) | r; break;
1465 case GRB: pixel = (g << 16) | (r << 8) | b; break;
1466 case GBR: pixel = (g << 16) | (b << 8) | r; break;
1467 }
1468 gdk_image_put_pixel( data_image, x, y, pixel );
1469 }
1470 default: break;
1471 }
1472 } // for
1473 } // for
1474
1475 // Blit picture
1476
1477 GdkGC *data_gc = gdk_gc_new( bitmap.GetPixmap() );
1478
1479 gdk_draw_image( bitmap.GetPixmap(), data_gc, data_image, 0, 0, 0, 0, width, height );
1480
1481 gdk_image_destroy( data_image );
1482 gdk_gc_unref( data_gc );
1483
1484 // Blit mask
1485
1486 if (HasMask())
1487 {
1488 GdkGC *mask_gc = gdk_gc_new( bitmap.GetMask()->GetBitmap() );
1489
1490 gdk_draw_image( bitmap.GetMask()->GetBitmap(), mask_gc, mask_image, 0, 0, 0, 0, width, height );
1491
1492 gdk_image_destroy( mask_image );
1493 gdk_gc_unref( mask_gc );
1494 }
1495
1496 return bitmap;
1497 }
1498
1499 wxImage::wxImage( const wxBitmap &bitmap )
1500 {
1501 wxCHECK_RET( bitmap.Ok(), "invalid bitmap" );
1502
1503 GdkImage *gdk_image = gdk_image_get( bitmap.GetPixmap(),
1504 0, 0,
1505 bitmap.GetWidth(), bitmap.GetHeight() );
1506
1507 wxCHECK_RET( gdk_image, "couldn't create image" );
1508
1509 Create( bitmap.GetWidth(), bitmap.GetHeight() );
1510 char unsigned *data = GetData();
1511
1512 if (!data)
1513 {
1514 gdk_image_destroy( gdk_image );
1515 wxFAIL_MSG( "couldn't create image" );
1516 return;
1517 }
1518
1519 GdkImage *gdk_image_mask = (GdkImage*) NULL;
1520 if (bitmap.GetMask())
1521 {
1522 gdk_image_mask = gdk_image_get( bitmap.GetMask()->GetBitmap(),
1523 0, 0,
1524 bitmap.GetWidth(), bitmap.GetHeight() );
1525
1526 SetMaskColour( 16, 16, 16 ); // anything unlikely and dividable
1527 }
1528
1529 GdkVisual *visual = gdk_window_get_visual( bitmap.GetPixmap() );
1530 if (visual == NULL) visual = gdk_window_get_visual( (GdkWindow*) &gdk_root_parent );
1531 int bpp = visual->depth;
1532 if ((bpp == 16) && (visual->red_mask != 0xf800)) bpp = 15;
1533
1534 GdkColormap *cmap = gtk_widget_get_default_colormap();
1535
1536 long pos = 0;
1537 for (int j = 0; j < bitmap.GetHeight(); j++)
1538 {
1539 for (int i = 0; i < bitmap.GetWidth(); i++)
1540 {
1541 int pixel = gdk_image_get_pixel( gdk_image, i, j );
1542 if (bpp <= 8)
1543 {
1544 data[pos] = cmap->colors[pixel].red >> 8;
1545 data[pos+1] = cmap->colors[pixel].green >> 8;
1546 data[pos+2] = cmap->colors[pixel].blue >> 8;
1547 } else if (bpp == 15)
1548 {
1549 data[pos] = (pixel >> 7) & 0xf8;
1550 data[pos+1] = (pixel >> 2) & 0xf8;
1551 data[pos+2] = (pixel << 3) & 0xf8;
1552 } else if (bpp == 16)
1553 {
1554 data[pos] = (pixel >> 8) & 0xf8;
1555 data[pos+1] = (pixel >> 3) & 0xfc;
1556 data[pos+2] = (pixel << 3) & 0xf8;
1557 } else
1558 {
1559 data[pos] = (pixel >> 16) & 0xff;
1560 data[pos+1] = (pixel >> 8) & 0xff;
1561 data[pos+2] = pixel & 0xff;
1562 }
1563
1564 if (gdk_image_mask)
1565 {
1566 int mask_pixel = gdk_image_get_pixel( gdk_image_mask, i, j );
1567 if (mask_pixel == 0)
1568 {
1569 data[pos] = 16;
1570 data[pos+1] = 16;
1571 data[pos+2] = 16;
1572 }
1573 }
1574
1575 pos += 3;
1576 }
1577 }
1578
1579 gdk_image_destroy( gdk_image );
1580 if (gdk_image_mask) gdk_image_destroy( gdk_image_mask );
1581 }
1582
1583 #endif
1584
1585 // TODO
1586
1587 #ifdef __WXMOTIF__
1588
1589 #include <Xm/Xm.h>
1590 #include "wx/utils.h"
1591 #include <math.h>
1592
1593 wxBitmap wxImage::ConvertToBitmap() const
1594 {
1595 wxBitmap bitmap;
1596
1597 wxCHECK_MSG( Ok(), bitmap, "invalid image" );
1598
1599 int width = GetWidth();
1600 int height = GetHeight();
1601
1602 bitmap.SetHeight( height );
1603 bitmap.SetWidth( width );
1604
1605 Display *dpy = (Display*) wxGetDisplay();
1606 Visual* vis = DefaultVisual( dpy, DefaultScreen( dpy ) );
1607 int bpp = DefaultDepth( dpy, DefaultScreen( dpy ) );
1608
1609 // Create image
1610
1611 XImage *data_image = XCreateImage( dpy, vis, bpp, ZPixmap, 0, 0, width, height, 32, 0 );
1612 data_image->data = new char[ data_image->bytes_per_line * data_image->height ];
1613
1614 bitmap.Create( width, height, bpp );
1615
1616 /*
1617 // Create mask
1618
1619 GdkImage *mask_image = (GdkImage*) NULL;
1620
1621 if (HasMask())
1622 {
1623 unsigned char *mask_data = (unsigned char*)malloc( ((width >> 3)+8) * height );
1624
1625 mask_image = gdk_image_new_bitmap( gdk_visual_get_system(), mask_data, width, height );
1626
1627 wxMask *mask = new wxMask();
1628 mask->m_bitmap = gdk_pixmap_new( (GdkWindow*)&gdk_root_parent, width, height, 1 );
1629
1630 bitmap.SetMask( mask );
1631 }
1632 */
1633
1634 // Retrieve depth info
1635
1636 XVisualInfo vinfo_template;
1637 XVisualInfo *vi;
1638
1639 vinfo_template.visual = vis;
1640 vinfo_template.visualid = XVisualIDFromVisual( vis );
1641 vinfo_template.depth = bpp;
1642 int nitem = 0;
1643
1644 vi = XGetVisualInfo( dpy, VisualIDMask|VisualDepthMask, &vinfo_template, &nitem );
1645
1646 if (!vi)
1647 {
1648 printf("no visual.\n" );
1649 return wxNullBitmap;
1650 }
1651
1652 XFree( vi );
1653
1654 if ((bpp == 16) && (vi->red_mask != 0xf800)) bpp = 15;
1655 if (bpp < 8) bpp = 8;
1656
1657 // Render
1658
1659 enum byte_order { RGB, RBG, BRG, BGR, GRB, GBR };
1660 byte_order b_o = RGB;
1661
1662 if (bpp >= 24)
1663 {
1664 if ((vi->red_mask > vi->green_mask) && (vi->green_mask > vi->blue_mask)) b_o = RGB;
1665 else if ((vi->red_mask > vi->blue_mask) && (vi->blue_mask > vi->green_mask)) b_o = RGB;
1666 else if ((vi->blue_mask > vi->red_mask) && (vi->red_mask > vi->green_mask)) b_o = BRG;
1667 else if ((vi->blue_mask > vi->green_mask) && (vi->green_mask > vi->red_mask)) b_o = BGR;
1668 else if ((vi->green_mask > vi->red_mask) && (vi->red_mask > vi->blue_mask)) b_o = GRB;
1669 else if ((vi->green_mask > vi->blue_mask) && (vi->blue_mask > vi->red_mask)) b_o = GBR;
1670 }
1671
1672 /*
1673 int r_mask = GetMaskRed();
1674 int g_mask = GetMaskGreen();
1675 int b_mask = GetMaskBlue();
1676 */
1677
1678 XColor colors[256];
1679 if (bpp == 8)
1680 {
1681 Colormap cmap = (Colormap) wxTheApp->GetMainColormap( dpy );
1682
1683 for (int i = 0; i < 256; i++) colors[i].pixel = i;
1684 XQueryColors( dpy, cmap, colors, 256 );
1685 }
1686
1687 unsigned char* data = GetData();
1688
1689 int index = 0;
1690 for (int y = 0; y < height; y++)
1691 {
1692 for (int x = 0; x < width; x++)
1693 {
1694 int r = data[index];
1695 index++;
1696 int g = data[index];
1697 index++;
1698 int b = data[index];
1699 index++;
1700
1701 /*
1702 if (HasMask())
1703 {
1704 if ((r == r_mask) && (b == b_mask) && (g == g_mask))
1705 gdk_image_put_pixel( mask_image, x, y, 1 );
1706 else
1707 gdk_image_put_pixel( mask_image, x, y, 0 );
1708 }
1709 */
1710
1711 switch (bpp)
1712 {
1713 case 8:
1714 {
1715 int pixel = -1;
1716 /*
1717 if (wxTheApp->m_colorCube)
1718 {
1719 pixel = wxTheApp->m_colorCube
1720 [ ((r & 0xf8) << 7) + ((g & 0xf8) << 2) + ((b & 0xf8) >> 3) ];
1721 }
1722 else
1723 {
1724 */
1725 int max = 3 * (65536);
1726 for (int i = 0; i < 256; i++)
1727 {
1728 int rdiff = (r << 8) - colors[i].red;
1729 int gdiff = (g << 8) - colors[i].green;
1730 int bdiff = (b << 8) - colors[i].blue;
1731 int sum = abs (rdiff) + abs (gdiff) + abs (bdiff);
1732 if (sum < max) { pixel = i; max = sum; }
1733 }
1734 /*
1735 }
1736 */
1737 XPutPixel( data_image, x, y, pixel );
1738 break;
1739 }
1740 case 15:
1741 {
1742 int pixel = ((r & 0xf8) << 7) | ((g & 0xf8) << 2) | ((b & 0xf8) >> 3);
1743 XPutPixel( data_image, x, y, pixel );
1744 break;
1745 }
1746 case 16:
1747 {
1748 int pixel = ((r & 0xf8) << 8) | ((g & 0xfc) << 3) | ((b & 0xf8) >> 3);
1749 XPutPixel( data_image, x, y, pixel );
1750 break;
1751 }
1752 case 32:
1753 case 24:
1754 {
1755 int pixel = 0;
1756 switch (b_o)
1757 {
1758 case RGB: pixel = (r << 16) | (g << 8) | b; break;
1759 case RBG: pixel = (r << 16) | (b << 8) | g; break;
1760 case BRG: pixel = (b << 16) | (r << 8) | g; break;
1761 case BGR: pixel = (b << 16) | (g << 8) | r; break;
1762 case GRB: pixel = (g << 16) | (r << 8) | b; break;
1763 case GBR: pixel = (g << 16) | (b << 8) | r; break;
1764 }
1765 XPutPixel( data_image, x, y, pixel );
1766 }
1767 default: break;
1768 }
1769 } // for
1770 } // for
1771
1772 // Blit picture
1773
1774 XGCValues gcvalues;
1775 gcvalues.foreground = BlackPixel( dpy, DefaultScreen( dpy ) );
1776 GC gc = XCreateGC( dpy, RootWindow ( dpy, DefaultScreen(dpy) ), GCForeground, &gcvalues );
1777 XPutImage( dpy, (Drawable)bitmap.GetPixmap(), gc, data_image, 0, 0, 0, 0, width, height );
1778
1779 XDestroyImage( data_image );
1780 XFreeGC( dpy, gc );
1781
1782 /*
1783 // Blit mask
1784
1785 if (HasMask())
1786 {
1787 GdkGC *mask_gc = gdk_gc_new( bitmap.GetMask()->GetBitmap() );
1788
1789 gdk_draw_image( bitmap.GetMask()->GetBitmap(), mask_gc, mask_image, 0, 0, 0, 0, width, height );
1790
1791 gdk_image_destroy( mask_image );
1792 gdk_gc_unref( mask_gc );
1793 }
1794 */
1795
1796 return bitmap;
1797 }
1798
1799 wxImage::wxImage( const wxBitmap &bitmap )
1800 {
1801 wxCHECK_RET( bitmap.Ok(), "invalid bitmap" );
1802
1803 Display *dpy = (Display*) wxGetDisplay();
1804 Visual* vis = DefaultVisual( dpy, DefaultScreen( dpy ) );
1805 int bpp = DefaultDepth( dpy, DefaultScreen( dpy ) );
1806
1807 XImage *ximage = XGetImage( dpy,
1808 (Drawable)bitmap.GetPixmap(),
1809 0, 0,
1810 bitmap.GetWidth(), bitmap.GetHeight(),
1811 AllPlanes, ZPixmap );
1812
1813 wxCHECK_RET( ximage, "couldn't create image" );
1814
1815 Create( bitmap.GetWidth(), bitmap.GetHeight() );
1816 char unsigned *data = GetData();
1817
1818 if (!data)
1819 {
1820 XDestroyImage( ximage );
1821 wxFAIL_MSG( "couldn't create image" );
1822 return;
1823 }
1824
1825 /*
1826 GdkImage *gdk_image_mask = (GdkImage*) NULL;
1827 if (bitmap.GetMask())
1828 {
1829 gdk_image_mask = gdk_image_get( bitmap.GetMask()->GetBitmap(),
1830 0, 0,
1831 bitmap.GetWidth(), bitmap.GetHeight() );
1832
1833 SetMaskColour( 16, 16, 16 ); // anything unlikely and dividable
1834 }
1835 */
1836
1837 // Retrieve depth info
1838
1839 XVisualInfo vinfo_template;
1840 XVisualInfo *vi;
1841
1842 vinfo_template.visual = vis;
1843 vinfo_template.visualid = XVisualIDFromVisual( vis );
1844 vinfo_template.depth = bpp;
1845 int nitem = 0;
1846
1847 vi = XGetVisualInfo( dpy, VisualIDMask|VisualDepthMask, &vinfo_template, &nitem );
1848
1849 if (!vi)
1850 {
1851 printf("no visual.\n" );
1852 return;
1853 }
1854
1855 if ((bpp == 16) && (vi->red_mask != 0xf800)) bpp = 15;
1856
1857 XFree( vi );
1858
1859 XColor colors[256];
1860 if (bpp == 8)
1861 {
1862 Colormap cmap = (Colormap)wxTheApp->GetMainColormap( dpy );
1863
1864 for (int i = 0; i < 256; i++) colors[i].pixel = i;
1865 XQueryColors( dpy, cmap, colors, 256 );
1866 }
1867
1868 long pos = 0;
1869 for (int j = 0; j < bitmap.GetHeight(); j++)
1870 {
1871 for (int i = 0; i < bitmap.GetWidth(); i++)
1872 {
1873 int pixel = XGetPixel( ximage, i, j );
1874 if (bpp <= 8)
1875 {
1876 data[pos] = colors[pixel].red >> 8;
1877 data[pos+1] = colors[pixel].green >> 8;
1878 data[pos+2] = colors[pixel].blue >> 8;
1879 } else if (bpp == 15)
1880 {
1881 data[pos] = (pixel >> 7) & 0xf8;
1882 data[pos+1] = (pixel >> 2) & 0xf8;
1883 data[pos+2] = (pixel << 3) & 0xf8;
1884 } else if (bpp == 16)
1885 {
1886 data[pos] = (pixel >> 8) & 0xf8;
1887 data[pos+1] = (pixel >> 3) & 0xfc;
1888 data[pos+2] = (pixel << 3) & 0xf8;
1889 } else
1890 {
1891 data[pos] = (pixel >> 16) & 0xff;
1892 data[pos+1] = (pixel >> 8) & 0xff;
1893 data[pos+2] = pixel & 0xff;
1894 }
1895
1896 /*
1897 if (gdk_image_mask)
1898 {
1899 int mask_pixel = gdk_image_get_pixel( gdk_image_mask, i, j );
1900 if (mask_pixel == 0)
1901 {
1902 data[pos] = 16;
1903 data[pos+1] = 16;
1904 data[pos+2] = 16;
1905 }
1906 }
1907 */
1908
1909 pos += 3;
1910 }
1911 }
1912
1913 XDestroyImage( ximage );
1914 /*
1915 if (gdk_image_mask) gdk_image_destroy( gdk_image_mask );
1916 */
1917 }
1918 #endif