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