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