For MSW, scan line boundary aligned, and >2MB case enabled
[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 // sizeLimit is the MS upper limit for the DIB size
1100 int sizeLimit = 1024*768*3;
1101
1102 // width and height of the device-dependent bitmap
1103 int width = GetWidth();
1104 int bmpHeight = GetHeight();
1105
1106 // calc the number of bytes per scanline and padding
1107 int bytePerLine = width*3;
1108 int sizeDWORD = sizeof( DWORD );
1109 div_t lineBoundary = div( bytePerLine, sizeDWORD );
1110 int padding = 0;
1111 if( lineBoundary.rem > 0 )
1112 {
1113 padding = sizeDWORD - lineBoundary.rem;
1114 bytePerLine += padding;
1115 }
1116 // calc the number of DIBs and heights of DIBs
1117 int numDIB = 1;
1118 int hRemain = 0;
1119 int height = sizeLimit/bytePerLine;
1120 if( height >= bmpHeight )
1121 height = bmpHeight;
1122 else
1123 {
1124 div_t result = div( bmpHeight, height );
1125 numDIB = result.quot;
1126 hRemain = result.rem;
1127 if( hRemain >0 ) numDIB++;
1128 }
1129
1130 // set bitmap parameters
1131 wxBitmap bitmap;
1132 wxCHECK_MSG( Ok(), bitmap, "invalid image" );
1133 bitmap.SetWidth( width );
1134 bitmap.SetHeight( bmpHeight );
1135 bitmap.SetDepth( wxDisplayDepth() );
1136
1137 // create a DIB header
1138 int headersize = sizeof(BITMAPINFOHEADER);
1139 LPBITMAPINFO lpDIBh = (BITMAPINFO *) malloc( headersize );
1140 wxCHECK_MSG( lpDIBh, bitmap, "could not allocate memory for DIB header" );
1141 // Fill in the DIB header
1142 lpDIBh->bmiHeader.biSize = headersize;
1143 lpDIBh->bmiHeader.biWidth = (DWORD)width;
1144 lpDIBh->bmiHeader.biHeight = (DWORD)(-height);
1145 lpDIBh->bmiHeader.biSizeImage = bytePerLine*height;
1146 // the general formula for biSizeImage:
1147 // ( ( ( ((DWORD)width*24) +31 ) & ~31 ) >> 3 ) * height;
1148 lpDIBh->bmiHeader.biPlanes = 1;
1149 lpDIBh->bmiHeader.biBitCount = 24;
1150 lpDIBh->bmiHeader.biCompression = BI_RGB;
1151 lpDIBh->bmiHeader.biClrUsed = 0;
1152 // These seem not really needed for our purpose here.
1153 lpDIBh->bmiHeader.biClrImportant = 0;
1154 lpDIBh->bmiHeader.biXPelsPerMeter = 0;
1155 lpDIBh->bmiHeader.biYPelsPerMeter = 0;
1156 // memory for DIB data
1157 unsigned char *lpBits;
1158 lpBits = (unsigned char *)malloc( lpDIBh->bmiHeader.biSizeImage );
1159 if( !lpBits )
1160 {
1161 wxFAIL_MSG( "could not allocate memory for DIB" );
1162 free( lpDIBh );
1163 return bitmap;
1164 }
1165
1166 // create and set the device-dependent bitmap
1167 HDC hdc = ::GetDC(NULL);
1168 HDC memdc = ::CreateCompatibleDC( hdc );
1169 HBITMAP hbitmap;
1170 hbitmap = ::CreateCompatibleBitmap( hdc, width, bmpHeight );
1171 ::SelectObject( memdc, hbitmap);
1172
1173 // copy image data into DIB data and then into DDB (in a loop)
1174 unsigned char *data = GetData();
1175 int i, j, n;
1176 int origin = 0;
1177 unsigned char *ptdata = data;
1178 unsigned char *ptbits;
1179
1180 for( n=0; n<numDIB; n++ )
1181 {
1182 if( numDIB > 1 && n == numDIB-1 && hRemain > 0 )
1183 {
1184 // redefine height and size of the (possibly) last smaller DIB
1185 // memory is not reallocated
1186 height = hRemain;
1187 lpDIBh->bmiHeader.biHeight = (DWORD)(-height);
1188 lpDIBh->bmiHeader.biSizeImage = bytePerLine*height;
1189 }
1190 ptbits = lpBits;
1191
1192 for( j=0; j<height; j++ )
1193 {
1194 for( i=0; i<width; i++ )
1195 {
1196 *(ptbits++) = *(ptdata+2);
1197 *(ptbits++) = *(ptdata+1);
1198 *(ptbits++) = *(ptdata );
1199 ptdata += 3;
1200 }
1201 for( i=0; i< padding; i++ ) *(ptbits++) = 0;
1202 }
1203 ::StretchDIBits( memdc, 0, origin, width, height,\
1204 0, 0, width, height, lpBits, lpDIBh, DIB_RGB_COLORS, SRCCOPY);
1205 origin += height;
1206 // if numDIB = 1, lines below can also be used
1207 // hbitmap = CreateDIBitmap( hdc, &(lpDIBh->bmiHeader), CBM_INIT, lpBits, lpDIBh, DIB_RGB_COLORS );
1208 // The above line is equivalent to the following two lines.
1209 // hbitmap = ::CreateCompatibleBitmap( hdc, width, height );
1210 // ::SetDIBits( hdc, hbitmap, 0, height, lpBits, lpDIBh, DIB_RGB_COLORS);
1211 // or the following lines
1212 // hbitmap = ::CreateCompatibleBitmap( hdc, width, height );
1213 // HDC memdc = ::CreateCompatibleDC( hdc );
1214 // ::SelectObject( memdc, hbitmap);
1215 // ::SetDIBitsToDevice( memdc, 0, 0, width, height,
1216 // 0, 0, 0, height, (void *)lpBits, lpDIBh, DIB_RGB_COLORS);
1217 // ::SelectObject( memdc, 0 );
1218 // ::DeleteDC( memdc );
1219 }
1220 bitmap.SetHBITMAP( (WXHBITMAP) hbitmap );
1221
1222 // similarly, created an mono-bitmap for the possible mask
1223 if( HasMask() )
1224 {
1225 hbitmap = ::CreateBitmap( (WORD)width, (WORD)bmpHeight, 1, 1, NULL );
1226 ::SelectObject( memdc, hbitmap);
1227 if( numDIB == 1 ) height = bmpHeight;
1228 else height = sizeLimit/bytePerLine;
1229 lpDIBh->bmiHeader.biHeight = (DWORD)(-height);
1230 lpDIBh->bmiHeader.biSizeImage = bytePerLine*height;
1231 origin = 0;
1232 unsigned char r = GetMaskRed();
1233 unsigned char g = GetMaskGreen();
1234 unsigned char b = GetMaskBlue();
1235 unsigned char zero = 0, one = 255;
1236 ptdata = data;
1237 for( n=0; n<numDIB; n++ )
1238 {
1239 if( numDIB > 1 && n == numDIB - 1 && hRemain > 0 )
1240 {
1241 // redefine height and size of the (possibly) last smaller DIB
1242 // memory is not reallocated
1243 height = hRemain;
1244 lpDIBh->bmiHeader.biHeight = (DWORD)(-height);
1245 lpDIBh->bmiHeader.biSizeImage = bytePerLine*height;
1246 }
1247 ptbits = lpBits;
1248 for( int j=0; j<height; j++ )
1249 {
1250 for( int i=0; i<width; i++ )
1251 {
1252 if( (*(ptdata++)!=r) | (*(ptdata++)!=g) | (*(ptdata++)!=b) )
1253 {
1254 *(ptbits++) = one;
1255 *(ptbits++) = one;
1256 *(ptbits++) = one;
1257 }
1258 else
1259 {
1260 *(ptbits++) = zero;
1261 *(ptbits++) = zero;
1262 *(ptbits++) = zero;
1263 }
1264 }
1265 for( i=0; i< padding; i++ ) *(ptbits++) = zero;
1266 }
1267 ::StretchDIBits( memdc, 0, origin, width, height,\
1268 0, 0, width, height, lpBits, lpDIBh, DIB_RGB_COLORS, SRCCOPY);
1269 origin += height;
1270 }
1271 // create a wxMask object
1272 wxMask *mask = new wxMask();
1273 mask->SetMaskBitmap( (WXHBITMAP) hbitmap );
1274 bitmap.SetMask( mask );
1275 // It will be deleted when the wxBitmap object is deleted (as of 01/1999)
1276 /* The following can also be used but is slow to run
1277 wxColour colour( GetMaskRed(), GetMaskGreen(), GetMaskBlue());
1278 wxMask *mask = new wxMask( bitmap, colour );
1279 bitmap.SetMask( mask );
1280 */
1281 }
1282
1283 // free allocated resources
1284 ::SelectObject( memdc, 0 );
1285 ::DeleteDC( memdc );
1286 ::ReleaseDC(NULL, hdc);
1287 free(lpDIBh);
1288 free(lpBits);
1289
1290 // check the wxBitmap object
1291 if( bitmap.GetHBITMAP() )
1292 bitmap.SetOk( TRUE );
1293 else
1294 bitmap.SetOk( FALSE );
1295
1296 return bitmap;
1297 }
1298
1299 wxImage::wxImage( const wxBitmap &bitmap )
1300 {
1301 // check the bitmap
1302 if( !bitmap.Ok() )
1303 {
1304 wxFAIL_MSG( "invalid bitmap" );
1305 return;
1306 }
1307
1308 // create an wxImage object
1309 int width = bitmap.GetWidth();
1310 int height = bitmap.GetHeight();
1311 Create( width, height );
1312 unsigned char *data = GetData();
1313 if( !data )
1314 {
1315 wxFAIL_MSG( "could not allocate data for image" );
1316 return;
1317 }
1318
1319 // calc the number of bytes per scanline and padding in the DIB
1320 int bytePerLine = width*3;
1321 int sizeDWORD = sizeof( DWORD );
1322 div_t lineBoundary = div( bytePerLine, sizeDWORD );
1323 int padding = 0;
1324 if( lineBoundary.rem > 0 )
1325 {
1326 padding = sizeDWORD - lineBoundary.rem;
1327 bytePerLine += padding;
1328 }
1329
1330 // create a DIB header
1331 int headersize = sizeof(BITMAPINFOHEADER);
1332 LPBITMAPINFO lpDIBh = (BITMAPINFO *) malloc( headersize );
1333 if( !lpDIBh )
1334 {
1335 wxFAIL_MSG( "could not allocate data for DIB header" );
1336 free( data );
1337 return;
1338 }
1339 // Fill in the DIB header
1340 lpDIBh->bmiHeader.biSize = headersize;
1341 lpDIBh->bmiHeader.biWidth = width;
1342 lpDIBh->bmiHeader.biHeight = -height;
1343 lpDIBh->bmiHeader.biSizeImage = bytePerLine * height;
1344 lpDIBh->bmiHeader.biPlanes = 1;
1345 lpDIBh->bmiHeader.biBitCount = 24;
1346 lpDIBh->bmiHeader.biCompression = BI_RGB;
1347 lpDIBh->bmiHeader.biClrUsed = 0;
1348 // These seem not really needed for our purpose here.
1349 lpDIBh->bmiHeader.biClrImportant = 0;
1350 lpDIBh->bmiHeader.biXPelsPerMeter = 0;
1351 lpDIBh->bmiHeader.biYPelsPerMeter = 0;
1352 // memory for DIB data
1353 unsigned char *lpBits;
1354 lpBits = (unsigned char *) malloc( lpDIBh->bmiHeader.biSizeImage );
1355 if( !lpBits )
1356 {
1357 wxFAIL_MSG( "could not allocate data for DIB" );
1358 free( data );
1359 free( lpDIBh );
1360 return;
1361 }
1362
1363 // copy data from the device-dependent bitmap to the DIB
1364 HDC hdc = ::GetDC(NULL);
1365 HBITMAP hbitmap;
1366 hbitmap = (HBITMAP) bitmap.GetHBITMAP();
1367 ::GetDIBits( hdc, hbitmap, 0, height, lpBits, lpDIBh, DIB_RGB_COLORS );
1368
1369 // copy DIB data into the wxImage object
1370 int i, j;
1371 unsigned char *ptdata = data;
1372 unsigned char *ptbits = lpBits;
1373 for( i=0; i<height; i++ )
1374 {
1375 for( j=0; j<width; j++ )
1376 {
1377 *(ptdata++) = *(ptbits+2);
1378 *(ptdata++) = *(ptbits+1);
1379 *(ptdata++) = *(ptbits );
1380 ptbits += 3;
1381 }
1382 ptbits += padding;
1383 }
1384
1385 // similarly, set data according to the possible mask bitmap
1386 if( bitmap.GetMask() && bitmap.GetMask()->GetMaskBitmap() )
1387 {
1388 hbitmap = (HBITMAP) bitmap.GetMask()->GetMaskBitmap();
1389 // memory DC created, color set, data copied, and memory DC deleted
1390 HDC memdc = ::CreateCompatibleDC( hdc );
1391 ::SetTextColor( memdc, RGB( 0, 0, 0 ) );
1392 ::SetBkColor( memdc, RGB( 255, 255, 255 ) );
1393 ::GetDIBits( memdc, hbitmap, 0, height, lpBits, lpDIBh, DIB_RGB_COLORS );
1394 ::DeleteDC( memdc );
1395 // background color set to RGB(16,16,16) in consistent with wxGTK
1396 unsigned char r=16, g=16, b=16;
1397 ptdata = data;
1398 ptbits = lpBits;
1399 for( i=0; i<height; i++ )
1400 {
1401 for( j=0; j<width; j++ )
1402 {
1403 if( *ptbits != 0 )
1404 ptdata += 3;
1405 else
1406 {
1407 *(ptdata++) = r;
1408 *(ptdata++) = g;
1409 *(ptdata++) = b;
1410 }
1411 ptbits += 3;
1412 }
1413 ptbits += padding;
1414 }
1415 SetMaskColour( r, g, b );
1416 SetMask( TRUE );
1417 }
1418 else
1419 {
1420 SetMask( FALSE );
1421 }
1422 // free allocated resources
1423 ::ReleaseDC(NULL, hdc);
1424 free(lpDIBh);
1425 free(lpBits);
1426 }
1427
1428 #endif
1429
1430 #ifdef __WXGTK__
1431
1432 #include "gtk/gtk.h"
1433 #include "gdk/gdk.h"
1434 #include "gdk/gdkx.h"
1435
1436 wxBitmap wxImage::ConvertToBitmap() const
1437 {
1438 wxBitmap bitmap;
1439
1440 wxCHECK_MSG( Ok(), bitmap, "invalid image" );
1441
1442 int width = GetWidth();
1443 int height = GetHeight();
1444
1445 bitmap.SetHeight( height );
1446 bitmap.SetWidth( width );
1447
1448 // Create picture
1449
1450 GdkImage *data_image =
1451 gdk_image_new( GDK_IMAGE_FASTEST, gdk_visual_get_system(), width, height );
1452
1453 bitmap.SetPixmap( gdk_pixmap_new( (GdkWindow*)&gdk_root_parent, width, height, -1 ) );
1454
1455 // Create mask
1456
1457 GdkImage *mask_image = (GdkImage*) NULL;
1458
1459 if (HasMask())
1460 {
1461 unsigned char *mask_data = (unsigned char*)malloc( ((width >> 3)+8) * height );
1462
1463 mask_image = gdk_image_new_bitmap( gdk_visual_get_system(), mask_data, width, height );
1464
1465 wxMask *mask = new wxMask();
1466 mask->m_bitmap = gdk_pixmap_new( (GdkWindow*)&gdk_root_parent, width, height, 1 );
1467
1468 bitmap.SetMask( mask );
1469 }
1470
1471 // Retrieve depth
1472
1473 GdkVisual *visual = gdk_window_get_visual( bitmap.GetPixmap() );
1474 if (visual == NULL) visual = gdk_window_get_visual( (GdkWindow*) &gdk_root_parent );
1475 int bpp = visual->depth;
1476
1477 bitmap.SetDepth( bpp );
1478
1479 if ((bpp == 16) && (visual->red_mask != 0xf800)) bpp = 15;
1480 if (bpp < 8) bpp = 8;
1481
1482 // Render
1483
1484 enum byte_order { RGB, RBG, BRG, BGR, GRB, GBR };
1485 byte_order b_o = RGB;
1486
1487 if (bpp >= 24)
1488 {
1489 GdkVisual *visual = gdk_visual_get_system();
1490 if ((visual->red_mask > visual->green_mask) && (visual->green_mask > visual->blue_mask)) b_o = RGB;
1491 else if ((visual->red_mask > visual->blue_mask) && (visual->blue_mask > visual->green_mask)) b_o = RGB;
1492 else if ((visual->blue_mask > visual->red_mask) && (visual->red_mask > visual->green_mask)) b_o = BRG;
1493 else if ((visual->blue_mask > visual->green_mask) && (visual->green_mask > visual->red_mask)) b_o = BGR;
1494 else if ((visual->green_mask > visual->red_mask) && (visual->red_mask > visual->blue_mask)) b_o = GRB;
1495 else if ((visual->green_mask > visual->blue_mask) && (visual->blue_mask > visual->red_mask)) b_o = GBR;
1496 }
1497
1498 int r_mask = GetMaskRed();
1499 int g_mask = GetMaskGreen();
1500 int b_mask = GetMaskBlue();
1501
1502 unsigned char* data = GetData();
1503
1504 int index = 0;
1505 for (int y = 0; y < height; y++)
1506 {
1507 for (int x = 0; x < width; x++)
1508 {
1509 int r = data[index];
1510 index++;
1511 int g = data[index];
1512 index++;
1513 int b = data[index];
1514 index++;
1515
1516 if (HasMask())
1517 {
1518 if ((r == r_mask) && (b == b_mask) && (g == g_mask))
1519 gdk_image_put_pixel( mask_image, x, y, 1 );
1520 else
1521 gdk_image_put_pixel( mask_image, x, y, 0 );
1522 }
1523
1524 if (HasMask())
1525 {
1526 if ((r == r_mask) && (b == b_mask) && (g == g_mask))
1527 gdk_image_put_pixel( mask_image, x, y, 1 );
1528 else
1529 gdk_image_put_pixel( mask_image, x, y, 0 );
1530 }
1531
1532 switch (bpp)
1533 {
1534 case 8:
1535 {
1536 int pixel = -1;
1537 if (wxTheApp->m_colorCube)
1538 {
1539 pixel = wxTheApp->m_colorCube[ ((r & 0xf8) << 7) + ((g & 0xf8) << 2) + ((b & 0xf8) >> 3) ];
1540 }
1541 else
1542 {
1543 GdkColormap *cmap = gtk_widget_get_default_colormap();
1544 GdkColor *colors = cmap->colors;
1545 int max = 3 * (65536);
1546
1547 for (int i = 0; i < cmap->size; i++)
1548 {
1549 int rdiff = (r << 8) - colors[i].red;
1550 int gdiff = (g << 8) - colors[i].green;
1551 int bdiff = (b << 8) - colors[i].blue;
1552 int sum = ABS (rdiff) + ABS (gdiff) + ABS (bdiff);
1553 if (sum < max) { pixel = i; max = sum; }
1554 }
1555 }
1556
1557 gdk_image_put_pixel( data_image, x, y, pixel );
1558
1559 break;
1560 }
1561 case 15:
1562 {
1563 guint32 pixel = ((r & 0xf8) << 7) | ((g & 0xf8) << 2) | ((b & 0xf8) >> 3);
1564 gdk_image_put_pixel( data_image, x, y, pixel );
1565 break;
1566 }
1567 case 16:
1568 {
1569 guint32 pixel = ((r & 0xf8) << 8) | ((g & 0xfc) << 3) | ((b & 0xf8) >> 3);
1570 gdk_image_put_pixel( data_image, x, y, pixel );
1571 break;
1572 }
1573 case 32:
1574 case 24:
1575 {
1576 guint32 pixel = 0;
1577 switch (b_o)
1578 {
1579 case RGB: pixel = (r << 16) | (g << 8) | b; break;
1580 case RBG: pixel = (r << 16) | (b << 8) | g; break;
1581 case BRG: pixel = (b << 16) | (r << 8) | g; break;
1582 case BGR: pixel = (b << 16) | (g << 8) | r; break;
1583 case GRB: pixel = (g << 16) | (r << 8) | b; break;
1584 case GBR: pixel = (g << 16) | (b << 8) | r; break;
1585 }
1586 gdk_image_put_pixel( data_image, x, y, pixel );
1587 }
1588 default: break;
1589 }
1590 } // for
1591 } // for
1592
1593 // Blit picture
1594
1595 GdkGC *data_gc = gdk_gc_new( bitmap.GetPixmap() );
1596
1597 gdk_draw_image( bitmap.GetPixmap(), data_gc, data_image, 0, 0, 0, 0, width, height );
1598
1599 gdk_image_destroy( data_image );
1600 gdk_gc_unref( data_gc );
1601
1602 // Blit mask
1603
1604 if (HasMask())
1605 {
1606 GdkGC *mask_gc = gdk_gc_new( bitmap.GetMask()->GetBitmap() );
1607
1608 gdk_draw_image( bitmap.GetMask()->GetBitmap(), mask_gc, mask_image, 0, 0, 0, 0, width, height );
1609
1610 gdk_image_destroy( mask_image );
1611 gdk_gc_unref( mask_gc );
1612 }
1613
1614 return bitmap;
1615 }
1616
1617 wxImage::wxImage( const wxBitmap &bitmap )
1618 {
1619 wxCHECK_RET( bitmap.Ok(), "invalid bitmap" );
1620
1621 GdkImage *gdk_image = gdk_image_get( bitmap.GetPixmap(),
1622 0, 0,
1623 bitmap.GetWidth(), bitmap.GetHeight() );
1624
1625 wxCHECK_RET( gdk_image, "couldn't create image" );
1626
1627 Create( bitmap.GetWidth(), bitmap.GetHeight() );
1628 char unsigned *data = GetData();
1629
1630 if (!data)
1631 {
1632 gdk_image_destroy( gdk_image );
1633 wxFAIL_MSG( "couldn't create image" );
1634 return;
1635 }
1636
1637 GdkImage *gdk_image_mask = (GdkImage*) NULL;
1638 if (bitmap.GetMask())
1639 {
1640 gdk_image_mask = gdk_image_get( bitmap.GetMask()->GetBitmap(),
1641 0, 0,
1642 bitmap.GetWidth(), bitmap.GetHeight() );
1643
1644 SetMaskColour( 16, 16, 16 ); // anything unlikely and dividable
1645 }
1646
1647 GdkVisual *visual = gdk_window_get_visual( bitmap.GetPixmap() );
1648 if (visual == NULL) visual = gdk_window_get_visual( (GdkWindow*) &gdk_root_parent );
1649 int bpp = visual->depth;
1650 if ((bpp == 16) && (visual->red_mask != 0xf800)) bpp = 15;
1651
1652 GdkColormap *cmap = gtk_widget_get_default_colormap();
1653
1654 long pos = 0;
1655 for (int j = 0; j < bitmap.GetHeight(); j++)
1656 {
1657 for (int i = 0; i < bitmap.GetWidth(); i++)
1658 {
1659 int pixel = gdk_image_get_pixel( gdk_image, i, j );
1660 if (bpp <= 8)
1661 {
1662 data[pos] = cmap->colors[pixel].red >> 8;
1663 data[pos+1] = cmap->colors[pixel].green >> 8;
1664 data[pos+2] = cmap->colors[pixel].blue >> 8;
1665 } else if (bpp == 15)
1666 {
1667 data[pos] = (pixel >> 7) & 0xf8;
1668 data[pos+1] = (pixel >> 2) & 0xf8;
1669 data[pos+2] = (pixel << 3) & 0xf8;
1670 } else if (bpp == 16)
1671 {
1672 data[pos] = (pixel >> 8) & 0xf8;
1673 data[pos+1] = (pixel >> 3) & 0xfc;
1674 data[pos+2] = (pixel << 3) & 0xf8;
1675 } else
1676 {
1677 data[pos] = (pixel >> 16) & 0xff;
1678 data[pos+1] = (pixel >> 8) & 0xff;
1679 data[pos+2] = pixel & 0xff;
1680 }
1681
1682 if (gdk_image_mask)
1683 {
1684 int mask_pixel = gdk_image_get_pixel( gdk_image_mask, i, j );
1685 if (mask_pixel == 0)
1686 {
1687 data[pos] = 16;
1688 data[pos+1] = 16;
1689 data[pos+2] = 16;
1690 }
1691 }
1692
1693 pos += 3;
1694 }
1695 }
1696
1697 gdk_image_destroy( gdk_image );
1698 if (gdk_image_mask) gdk_image_destroy( gdk_image_mask );
1699 }
1700
1701 #endif
1702
1703 // TODO
1704
1705 #ifdef __WXMOTIF__
1706
1707 #include <Xm/Xm.h>
1708 #include "wx/utils.h"
1709 #include <math.h>
1710
1711 wxBitmap wxImage::ConvertToBitmap() const
1712 {
1713 wxBitmap bitmap;
1714
1715 wxCHECK_MSG( Ok(), bitmap, "invalid image" );
1716
1717 int width = GetWidth();
1718 int height = GetHeight();
1719
1720 bitmap.SetHeight( height );
1721 bitmap.SetWidth( width );
1722
1723 Display *dpy = (Display*) wxGetDisplay();
1724 Visual* vis = DefaultVisual( dpy, DefaultScreen( dpy ) );
1725 int bpp = DefaultDepth( dpy, DefaultScreen( dpy ) );
1726
1727 // Create image
1728
1729 XImage *data_image = XCreateImage( dpy, vis, bpp, ZPixmap, 0, 0, width, height, 32, 0 );
1730 data_image->data = new char[ data_image->bytes_per_line * data_image->height ];
1731
1732 bitmap.Create( width, height, bpp );
1733
1734 /*
1735 // Create mask
1736
1737 GdkImage *mask_image = (GdkImage*) NULL;
1738
1739 if (HasMask())
1740 {
1741 unsigned char *mask_data = (unsigned char*)malloc( ((width >> 3)+8) * height );
1742
1743 mask_image = gdk_image_new_bitmap( gdk_visual_get_system(), mask_data, width, height );
1744
1745 wxMask *mask = new wxMask();
1746 mask->m_bitmap = gdk_pixmap_new( (GdkWindow*)&gdk_root_parent, width, height, 1 );
1747
1748 bitmap.SetMask( mask );
1749 }
1750 */
1751
1752 // Retrieve depth info
1753
1754 XVisualInfo vinfo_template;
1755 XVisualInfo *vi;
1756
1757 vinfo_template.visual = vis;
1758 vinfo_template.visualid = XVisualIDFromVisual( vis );
1759 vinfo_template.depth = bpp;
1760 int nitem = 0;
1761
1762 vi = XGetVisualInfo( dpy, VisualIDMask|VisualDepthMask, &vinfo_template, &nitem );
1763
1764 if (!vi)
1765 {
1766 printf("no visual.\n" );
1767 return wxNullBitmap;
1768 }
1769
1770 XFree( vi );
1771
1772 if ((bpp == 16) && (vi->red_mask != 0xf800)) bpp = 15;
1773 if (bpp < 8) bpp = 8;
1774
1775 // Render
1776
1777 enum byte_order { RGB, RBG, BRG, BGR, GRB, GBR };
1778 byte_order b_o = RGB;
1779
1780 if (bpp >= 24)
1781 {
1782 if ((vi->red_mask > vi->green_mask) && (vi->green_mask > vi->blue_mask)) b_o = RGB;
1783 else if ((vi->red_mask > vi->blue_mask) && (vi->blue_mask > vi->green_mask)) b_o = RGB;
1784 else if ((vi->blue_mask > vi->red_mask) && (vi->red_mask > vi->green_mask)) b_o = BRG;
1785 else if ((vi->blue_mask > vi->green_mask) && (vi->green_mask > vi->red_mask)) b_o = BGR;
1786 else if ((vi->green_mask > vi->red_mask) && (vi->red_mask > vi->blue_mask)) b_o = GRB;
1787 else if ((vi->green_mask > vi->blue_mask) && (vi->blue_mask > vi->red_mask)) b_o = GBR;
1788 }
1789
1790 /*
1791 int r_mask = GetMaskRed();
1792 int g_mask = GetMaskGreen();
1793 int b_mask = GetMaskBlue();
1794 */
1795
1796 XColor colors[256];
1797 if (bpp == 8)
1798 {
1799 Colormap cmap = (Colormap) wxTheApp->GetMainColormap( dpy );
1800
1801 for (int i = 0; i < 256; i++) colors[i].pixel = i;
1802 XQueryColors( dpy, cmap, colors, 256 );
1803 }
1804
1805 unsigned char* data = GetData();
1806
1807 int index = 0;
1808 for (int y = 0; y < height; y++)
1809 {
1810 for (int x = 0; x < width; x++)
1811 {
1812 int r = data[index];
1813 index++;
1814 int g = data[index];
1815 index++;
1816 int b = data[index];
1817 index++;
1818
1819 /*
1820 if (HasMask())
1821 {
1822 if ((r == r_mask) && (b == b_mask) && (g == g_mask))
1823 gdk_image_put_pixel( mask_image, x, y, 1 );
1824 else
1825 gdk_image_put_pixel( mask_image, x, y, 0 );
1826 }
1827 */
1828
1829 switch (bpp)
1830 {
1831 case 8:
1832 {
1833 int pixel = -1;
1834 /*
1835 if (wxTheApp->m_colorCube)
1836 {
1837 pixel = wxTheApp->m_colorCube
1838 [ ((r & 0xf8) << 7) + ((g & 0xf8) << 2) + ((b & 0xf8) >> 3) ];
1839 }
1840 else
1841 {
1842 */
1843 int max = 3 * (65536);
1844 for (int i = 0; i < 256; i++)
1845 {
1846 int rdiff = (r << 8) - colors[i].red;
1847 int gdiff = (g << 8) - colors[i].green;
1848 int bdiff = (b << 8) - colors[i].blue;
1849 int sum = abs (rdiff) + abs (gdiff) + abs (bdiff);
1850 if (sum < max) { pixel = i; max = sum; }
1851 }
1852 /*
1853 }
1854 */
1855 XPutPixel( data_image, x, y, pixel );
1856 break;
1857 }
1858 case 15:
1859 {
1860 int pixel = ((r & 0xf8) << 7) | ((g & 0xf8) << 2) | ((b & 0xf8) >> 3);
1861 XPutPixel( data_image, x, y, pixel );
1862 break;
1863 }
1864 case 16:
1865 {
1866 int pixel = ((r & 0xf8) << 8) | ((g & 0xfc) << 3) | ((b & 0xf8) >> 3);
1867 XPutPixel( data_image, x, y, pixel );
1868 break;
1869 }
1870 case 32:
1871 case 24:
1872 {
1873 int pixel = 0;
1874 switch (b_o)
1875 {
1876 case RGB: pixel = (r << 16) | (g << 8) | b; break;
1877 case RBG: pixel = (r << 16) | (b << 8) | g; break;
1878 case BRG: pixel = (b << 16) | (r << 8) | g; break;
1879 case BGR: pixel = (b << 16) | (g << 8) | r; break;
1880 case GRB: pixel = (g << 16) | (r << 8) | b; break;
1881 case GBR: pixel = (g << 16) | (b << 8) | r; break;
1882 }
1883 XPutPixel( data_image, x, y, pixel );
1884 }
1885 default: break;
1886 }
1887 } // for
1888 } // for
1889
1890 // Blit picture
1891
1892 XGCValues gcvalues;
1893 gcvalues.foreground = BlackPixel( dpy, DefaultScreen( dpy ) );
1894 GC gc = XCreateGC( dpy, RootWindow ( dpy, DefaultScreen(dpy) ), GCForeground, &gcvalues );
1895 XPutImage( dpy, (Drawable)bitmap.GetPixmap(), gc, data_image, 0, 0, 0, 0, width, height );
1896
1897 XDestroyImage( data_image );
1898 XFreeGC( dpy, gc );
1899
1900 /*
1901 // Blit mask
1902
1903 if (HasMask())
1904 {
1905 GdkGC *mask_gc = gdk_gc_new( bitmap.GetMask()->GetBitmap() );
1906
1907 gdk_draw_image( bitmap.GetMask()->GetBitmap(), mask_gc, mask_image, 0, 0, 0, 0, width, height );
1908
1909 gdk_image_destroy( mask_image );
1910 gdk_gc_unref( mask_gc );
1911 }
1912 */
1913
1914 return bitmap;
1915 }
1916
1917 wxImage::wxImage( const wxBitmap &bitmap )
1918 {
1919 wxCHECK_RET( bitmap.Ok(), "invalid bitmap" );
1920
1921 Display *dpy = (Display*) wxGetDisplay();
1922 Visual* vis = DefaultVisual( dpy, DefaultScreen( dpy ) );
1923 int bpp = DefaultDepth( dpy, DefaultScreen( dpy ) );
1924
1925 XImage *ximage = XGetImage( dpy,
1926 (Drawable)bitmap.GetPixmap(),
1927 0, 0,
1928 bitmap.GetWidth(), bitmap.GetHeight(),
1929 AllPlanes, ZPixmap );
1930
1931 wxCHECK_RET( ximage, "couldn't create image" );
1932
1933 Create( bitmap.GetWidth(), bitmap.GetHeight() );
1934 char unsigned *data = GetData();
1935
1936 if (!data)
1937 {
1938 XDestroyImage( ximage );
1939 wxFAIL_MSG( "couldn't create image" );
1940 return;
1941 }
1942
1943 /*
1944 GdkImage *gdk_image_mask = (GdkImage*) NULL;
1945 if (bitmap.GetMask())
1946 {
1947 gdk_image_mask = gdk_image_get( bitmap.GetMask()->GetBitmap(),
1948 0, 0,
1949 bitmap.GetWidth(), bitmap.GetHeight() );
1950
1951 SetMaskColour( 16, 16, 16 ); // anything unlikely and dividable
1952 }
1953 */
1954
1955 // Retrieve depth info
1956
1957 XVisualInfo vinfo_template;
1958 XVisualInfo *vi;
1959
1960 vinfo_template.visual = vis;
1961 vinfo_template.visualid = XVisualIDFromVisual( vis );
1962 vinfo_template.depth = bpp;
1963 int nitem = 0;
1964
1965 vi = XGetVisualInfo( dpy, VisualIDMask|VisualDepthMask, &vinfo_template, &nitem );
1966
1967 if (!vi)
1968 {
1969 printf("no visual.\n" );
1970 return;
1971 }
1972
1973 if ((bpp == 16) && (vi->red_mask != 0xf800)) bpp = 15;
1974
1975 XFree( vi );
1976
1977 XColor colors[256];
1978 if (bpp == 8)
1979 {
1980 Colormap cmap = (Colormap)wxTheApp->GetMainColormap( dpy );
1981
1982 for (int i = 0; i < 256; i++) colors[i].pixel = i;
1983 XQueryColors( dpy, cmap, colors, 256 );
1984 }
1985
1986 long pos = 0;
1987 for (int j = 0; j < bitmap.GetHeight(); j++)
1988 {
1989 for (int i = 0; i < bitmap.GetWidth(); i++)
1990 {
1991 int pixel = XGetPixel( ximage, i, j );
1992 if (bpp <= 8)
1993 {
1994 data[pos] = colors[pixel].red >> 8;
1995 data[pos+1] = colors[pixel].green >> 8;
1996 data[pos+2] = colors[pixel].blue >> 8;
1997 } else if (bpp == 15)
1998 {
1999 data[pos] = (pixel >> 7) & 0xf8;
2000 data[pos+1] = (pixel >> 2) & 0xf8;
2001 data[pos+2] = (pixel << 3) & 0xf8;
2002 } else if (bpp == 16)
2003 {
2004 data[pos] = (pixel >> 8) & 0xf8;
2005 data[pos+1] = (pixel >> 3) & 0xfc;
2006 data[pos+2] = (pixel << 3) & 0xf8;
2007 } else
2008 {
2009 data[pos] = (pixel >> 16) & 0xff;
2010 data[pos+1] = (pixel >> 8) & 0xff;
2011 data[pos+2] = pixel & 0xff;
2012 }
2013
2014 /*
2015 if (gdk_image_mask)
2016 {
2017 int mask_pixel = gdk_image_get_pixel( gdk_image_mask, i, j );
2018 if (mask_pixel == 0)
2019 {
2020 data[pos] = 16;
2021 data[pos+1] = 16;
2022 data[pos+2] = 16;
2023 }
2024 }
2025 */
2026
2027 pos += 3;
2028 }
2029 }
2030
2031 XDestroyImage( ximage );
2032 /*
2033 if (gdk_image_mask) gdk_image_destroy( gdk_image_mask );
2034 */
2035 }
2036 #endif