]> git.saurik.com Git - wxWidgets.git/blob - src/common/image.cpp
Fixes for compilation under AIX.
[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 int sizeLimit = 1024*768*3;
686
687 // width and height of the device-dependent bitmap
688 int width = GetWidth();
689 int bmpHeight = GetHeight();
690
691 // calc the number of bytes per scanline and padding
692 int bytePerLine = width*3;
693 int sizeDWORD = sizeof( DWORD );
694 int lineBoundary = bytePerLine % sizeDWORD;
695 int padding = 0;
696 if( lineBoundary > 0 )
697 {
698 padding = sizeDWORD - lineBoundary;
699 bytePerLine += padding;
700 }
701 // calc the number of DIBs and heights of DIBs
702 int numDIB = 1;
703 int hRemain = 0;
704 int height = sizeLimit/bytePerLine;
705 if( height >= bmpHeight )
706 height = bmpHeight;
707 else
708 {
709 numDIB = bmpHeight / height;
710 hRemain = bmpHeight % height;
711 if( hRemain >0 ) numDIB++;
712 }
713
714 // set bitmap parameters
715 wxBitmap bitmap;
716 wxCHECK_MSG( Ok(), bitmap, _T("invalid image") );
717 bitmap.SetWidth( width );
718 bitmap.SetHeight( bmpHeight );
719 bitmap.SetDepth( wxDisplayDepth() );
720
721 // create a DIB header
722 int headersize = sizeof(BITMAPINFOHEADER);
723 LPBITMAPINFO lpDIBh = (BITMAPINFO *) malloc( headersize );
724 wxCHECK_MSG( lpDIBh, bitmap, _T("could not allocate memory for DIB header") );
725 // Fill in the DIB header
726 lpDIBh->bmiHeader.biSize = headersize;
727 lpDIBh->bmiHeader.biWidth = (DWORD)width;
728 lpDIBh->bmiHeader.biHeight = (DWORD)(-height);
729 lpDIBh->bmiHeader.biSizeImage = bytePerLine*height;
730 // the general formula for biSizeImage:
731 // ( ( ( ((DWORD)width*24) +31 ) & ~31 ) >> 3 ) * height;
732 lpDIBh->bmiHeader.biPlanes = 1;
733 lpDIBh->bmiHeader.biBitCount = 24;
734 lpDIBh->bmiHeader.biCompression = BI_RGB;
735 lpDIBh->bmiHeader.biClrUsed = 0;
736 // These seem not really needed for our purpose here.
737 lpDIBh->bmiHeader.biClrImportant = 0;
738 lpDIBh->bmiHeader.biXPelsPerMeter = 0;
739 lpDIBh->bmiHeader.biYPelsPerMeter = 0;
740 // memory for DIB data
741 unsigned char *lpBits;
742 lpBits = (unsigned char *)malloc( lpDIBh->bmiHeader.biSizeImage );
743 if( !lpBits )
744 {
745 wxFAIL_MSG( _T("could not allocate memory for DIB") );
746 free( lpDIBh );
747 return bitmap;
748 }
749
750 // create and set the device-dependent bitmap
751 HDC hdc = ::GetDC(NULL);
752 HDC memdc = ::CreateCompatibleDC( hdc );
753 HBITMAP hbitmap;
754 hbitmap = ::CreateCompatibleBitmap( hdc, width, bmpHeight );
755 ::SelectObject( memdc, hbitmap);
756
757 // copy image data into DIB data and then into DDB (in a loop)
758 unsigned char *data = GetData();
759 int i, j, n;
760 int origin = 0;
761 unsigned char *ptdata = data;
762 unsigned char *ptbits;
763
764 for( n=0; n<numDIB; n++ )
765 {
766 if( numDIB > 1 && n == numDIB-1 && hRemain > 0 )
767 {
768 // redefine height and size of the (possibly) last smaller DIB
769 // memory is not reallocated
770 height = hRemain;
771 lpDIBh->bmiHeader.biHeight = (DWORD)(-height);
772 lpDIBh->bmiHeader.biSizeImage = bytePerLine*height;
773 }
774 ptbits = lpBits;
775
776 for( j=0; j<height; j++ )
777 {
778 for( i=0; i<width; i++ )
779 {
780 *(ptbits++) = *(ptdata+2);
781 *(ptbits++) = *(ptdata+1);
782 *(ptbits++) = *(ptdata );
783 ptdata += 3;
784 }
785 for( i=0; i< padding; i++ ) *(ptbits++) = 0;
786 }
787 ::StretchDIBits( memdc, 0, origin, width, height,\
788 0, 0, width, height, lpBits, lpDIBh, DIB_RGB_COLORS, SRCCOPY);
789 origin += height;
790 // if numDIB = 1, lines below can also be used
791 // hbitmap = CreateDIBitmap( hdc, &(lpDIBh->bmiHeader), CBM_INIT, lpBits, lpDIBh, DIB_RGB_COLORS );
792 // The above line is equivalent to the following two lines.
793 // hbitmap = ::CreateCompatibleBitmap( hdc, width, height );
794 // ::SetDIBits( hdc, hbitmap, 0, height, lpBits, lpDIBh, DIB_RGB_COLORS);
795 // or the following lines
796 // hbitmap = ::CreateCompatibleBitmap( hdc, width, height );
797 // HDC memdc = ::CreateCompatibleDC( hdc );
798 // ::SelectObject( memdc, hbitmap);
799 // ::SetDIBitsToDevice( memdc, 0, 0, width, height,
800 // 0, 0, 0, height, (void *)lpBits, lpDIBh, DIB_RGB_COLORS);
801 // ::SelectObject( memdc, 0 );
802 // ::DeleteDC( memdc );
803 }
804 bitmap.SetHBITMAP( (WXHBITMAP) hbitmap );
805
806 // similarly, created an mono-bitmap for the possible mask
807 if( HasMask() )
808 {
809 hbitmap = ::CreateBitmap( (WORD)width, (WORD)bmpHeight, 1, 1, NULL );
810 ::SelectObject( memdc, hbitmap);
811 if( numDIB == 1 ) height = bmpHeight;
812 else height = sizeLimit/bytePerLine;
813 lpDIBh->bmiHeader.biHeight = (DWORD)(-height);
814 lpDIBh->bmiHeader.biSizeImage = bytePerLine*height;
815 origin = 0;
816 unsigned char r = GetMaskRed();
817 unsigned char g = GetMaskGreen();
818 unsigned char b = GetMaskBlue();
819 unsigned char zero = 0, one = 255;
820 ptdata = data;
821 for( n=0; n<numDIB; n++ )
822 {
823 if( numDIB > 1 && n == numDIB - 1 && hRemain > 0 )
824 {
825 // redefine height and size of the (possibly) last smaller DIB
826 // memory is not reallocated
827 height = hRemain;
828 lpDIBh->bmiHeader.biHeight = (DWORD)(-height);
829 lpDIBh->bmiHeader.biSizeImage = bytePerLine*height;
830 }
831 ptbits = lpBits;
832 for( int j=0; j<height; j++ )
833 {
834 for(i=0; i<width; i++ )
835 {
836 if( (*(ptdata++)!=r) | (*(ptdata++)!=g) | (*(ptdata++)!=b) )
837 {
838 *(ptbits++) = one;
839 *(ptbits++) = one;
840 *(ptbits++) = one;
841 }
842 else
843 {
844 *(ptbits++) = zero;
845 *(ptbits++) = zero;
846 *(ptbits++) = zero;
847 }
848 }
849 for( i=0; i< padding; i++ ) *(ptbits++) = zero;
850 }
851 ::StretchDIBits( memdc, 0, origin, width, height,\
852 0, 0, width, height, lpBits, lpDIBh, DIB_RGB_COLORS, SRCCOPY);
853 origin += height;
854 }
855 // create a wxMask object
856 wxMask *mask = new wxMask();
857 mask->SetMaskBitmap( (WXHBITMAP) hbitmap );
858 bitmap.SetMask( mask );
859 // It will be deleted when the wxBitmap object is deleted (as of 01/1999)
860 /* The following can also be used but is slow to run
861 wxColour colour( GetMaskRed(), GetMaskGreen(), GetMaskBlue());
862 wxMask *mask = new wxMask( bitmap, colour );
863 bitmap.SetMask( mask );
864 */
865 }
866
867 // free allocated resources
868 ::SelectObject( memdc, 0 );
869 ::DeleteDC( memdc );
870 ::ReleaseDC(NULL, hdc);
871 free(lpDIBh);
872 free(lpBits);
873
874 // check the wxBitmap object
875 if( bitmap.GetHBITMAP() )
876 bitmap.SetOk( TRUE );
877 else
878 bitmap.SetOk( FALSE );
879
880 return bitmap;
881 }
882
883 wxImage::wxImage( const wxBitmap &bitmap )
884 {
885 // check the bitmap
886 if( !bitmap.Ok() )
887 {
888 wxFAIL_MSG( _T("invalid bitmap") );
889 return;
890 }
891
892 // create an wxImage object
893 int width = bitmap.GetWidth();
894 int height = bitmap.GetHeight();
895 Create( width, height );
896 unsigned char *data = GetData();
897 if( !data )
898 {
899 wxFAIL_MSG( _T("could not allocate data for image") );
900 return;
901 }
902
903 // calc the number of bytes per scanline and padding in the DIB
904 int bytePerLine = width*3;
905 int sizeDWORD = sizeof( DWORD );
906 int lineBoundary = bytePerLine % sizeDWORD;
907 int padding = 0;
908 if( lineBoundary > 0 )
909 {
910 padding = sizeDWORD - lineBoundary;
911 bytePerLine += padding;
912 }
913
914 // create a DIB header
915 int headersize = sizeof(BITMAPINFOHEADER);
916 LPBITMAPINFO lpDIBh = (BITMAPINFO *) malloc( headersize );
917 if( !lpDIBh )
918 {
919 wxFAIL_MSG( _T("could not allocate data for DIB header") );
920 free( data );
921 return;
922 }
923 // Fill in the DIB header
924 lpDIBh->bmiHeader.biSize = headersize;
925 lpDIBh->bmiHeader.biWidth = width;
926 lpDIBh->bmiHeader.biHeight = -height;
927 lpDIBh->bmiHeader.biSizeImage = bytePerLine * height;
928 lpDIBh->bmiHeader.biPlanes = 1;
929 lpDIBh->bmiHeader.biBitCount = 24;
930 lpDIBh->bmiHeader.biCompression = BI_RGB;
931 lpDIBh->bmiHeader.biClrUsed = 0;
932 // These seem not really needed for our purpose here.
933 lpDIBh->bmiHeader.biClrImportant = 0;
934 lpDIBh->bmiHeader.biXPelsPerMeter = 0;
935 lpDIBh->bmiHeader.biYPelsPerMeter = 0;
936 // memory for DIB data
937 unsigned char *lpBits;
938 lpBits = (unsigned char *) malloc( lpDIBh->bmiHeader.biSizeImage );
939 if( !lpBits )
940 {
941 wxFAIL_MSG( _T("could not allocate data for DIB") );
942 free( data );
943 free( lpDIBh );
944 return;
945 }
946
947 // copy data from the device-dependent bitmap to the DIB
948 HDC hdc = ::GetDC(NULL);
949 HBITMAP hbitmap;
950 hbitmap = (HBITMAP) bitmap.GetHBITMAP();
951 ::GetDIBits( hdc, hbitmap, 0, height, lpBits, lpDIBh, DIB_RGB_COLORS );
952
953 // copy DIB data into the wxImage object
954 int i, j;
955 unsigned char *ptdata = data;
956 unsigned char *ptbits = lpBits;
957 for( i=0; i<height; i++ )
958 {
959 for( j=0; j<width; j++ )
960 {
961 *(ptdata++) = *(ptbits+2);
962 *(ptdata++) = *(ptbits+1);
963 *(ptdata++) = *(ptbits );
964 ptbits += 3;
965 }
966 ptbits += padding;
967 }
968
969 // similarly, set data according to the possible mask bitmap
970 if( bitmap.GetMask() && bitmap.GetMask()->GetMaskBitmap() )
971 {
972 hbitmap = (HBITMAP) bitmap.GetMask()->GetMaskBitmap();
973 // memory DC created, color set, data copied, and memory DC deleted
974 HDC memdc = ::CreateCompatibleDC( hdc );
975 ::SetTextColor( memdc, RGB( 0, 0, 0 ) );
976 ::SetBkColor( memdc, RGB( 255, 255, 255 ) );
977 ::GetDIBits( memdc, hbitmap, 0, height, lpBits, lpDIBh, DIB_RGB_COLORS );
978 ::DeleteDC( memdc );
979 // background color set to RGB(16,16,16) in consistent with wxGTK
980 unsigned char r=16, g=16, b=16;
981 ptdata = data;
982 ptbits = lpBits;
983 for( i=0; i<height; i++ )
984 {
985 for( j=0; j<width; j++ )
986 {
987 if( *ptbits != 0 )
988 ptdata += 3;
989 else
990 {
991 *(ptdata++) = r;
992 *(ptdata++) = g;
993 *(ptdata++) = b;
994 }
995 ptbits += 3;
996 }
997 ptbits += padding;
998 }
999 SetMaskColour( r, g, b );
1000 SetMask( TRUE );
1001 }
1002 else
1003 {
1004 SetMask( FALSE );
1005 }
1006 // free allocated resources
1007 ::ReleaseDC(NULL, hdc);
1008 free(lpDIBh);
1009 free(lpBits);
1010 }
1011
1012 #endif
1013
1014 //-----------------------------------------------------------------------------
1015 // GTK conversion routines
1016 //-----------------------------------------------------------------------------
1017
1018 #ifdef __WXGTK__
1019
1020 #include "gtk/gtk.h"
1021 #include "gdk/gdk.h"
1022 #include "gdk/gdkx.h"
1023
1024 #if (GTK_MINOR_VERSION > 0)
1025 #include "gdk/gdkrgb.h"
1026 #endif
1027
1028 wxBitmap wxImage::ConvertToBitmap() const
1029 {
1030 wxBitmap bitmap;
1031
1032 wxCHECK_MSG( Ok(), bitmap, _T("invalid image") );
1033
1034 int width = GetWidth();
1035 int height = GetHeight();
1036
1037 bitmap.SetHeight( height );
1038 bitmap.SetWidth( width );
1039
1040 bitmap.SetPixmap( gdk_pixmap_new( (GdkWindow*)&gdk_root_parent, width, height, -1 ) );
1041
1042 // Retrieve depth
1043
1044 GdkVisual *visual = gdk_window_get_visual( bitmap.GetPixmap() );
1045 if (visual == NULL) visual = gdk_visual_get_system();
1046 int bpp = visual->depth;
1047
1048 bitmap.SetDepth( bpp );
1049
1050 if ((bpp == 16) && (visual->red_mask != 0xf800)) bpp = 15;
1051 if (bpp < 8) bpp = 8;
1052
1053 #if (GTK_MINOR_VERSION > 0)
1054
1055 if (!HasMask() && (bpp > 8))
1056 {
1057 static bool s_hasInitialized = FALSE;
1058
1059 if (!s_hasInitialized)
1060 {
1061 gdk_rgb_init();
1062 s_hasInitialized = TRUE;
1063 }
1064
1065 GdkGC *gc = gdk_gc_new( bitmap.GetPixmap() );
1066
1067 gdk_draw_rgb_image( bitmap.GetPixmap(),
1068 gc,
1069 0, 0,
1070 width, height,
1071 GDK_RGB_DITHER_NONE,
1072 GetData(),
1073 width*3 );
1074
1075 gdk_gc_unref( gc );
1076
1077 return bitmap;
1078 }
1079
1080 #endif
1081
1082 // Create picture image
1083
1084 GdkImage *data_image =
1085 gdk_image_new( GDK_IMAGE_FASTEST, gdk_visual_get_system(), width, height );
1086
1087 // Create mask image
1088
1089 GdkImage *mask_image = (GdkImage*) NULL;
1090
1091 if (HasMask())
1092 {
1093 unsigned char *mask_data = (unsigned char*)malloc( ((width >> 3)+8) * height );
1094
1095 mask_image = gdk_image_new_bitmap( gdk_visual_get_system(), mask_data, width, height );
1096
1097 wxMask *mask = new wxMask();
1098 mask->m_bitmap = gdk_pixmap_new( (GdkWindow*)&gdk_root_parent, width, height, 1 );
1099
1100 bitmap.SetMask( mask );
1101 }
1102
1103 // Render
1104
1105 enum byte_order { RGB, RBG, BRG, BGR, GRB, GBR };
1106 byte_order b_o = RGB;
1107
1108 if (bpp >= 24)
1109 {
1110 GdkVisual *visual = gdk_visual_get_system();
1111 if ((visual->red_mask > visual->green_mask) && (visual->green_mask > visual->blue_mask)) b_o = RGB;
1112 else if ((visual->red_mask > visual->blue_mask) && (visual->blue_mask > visual->green_mask)) b_o = RGB;
1113 else if ((visual->blue_mask > visual->red_mask) && (visual->red_mask > visual->green_mask)) b_o = BRG;
1114 else if ((visual->blue_mask > visual->green_mask) && (visual->green_mask > visual->red_mask)) b_o = BGR;
1115 else if ((visual->green_mask > visual->red_mask) && (visual->red_mask > visual->blue_mask)) b_o = GRB;
1116 else if ((visual->green_mask > visual->blue_mask) && (visual->blue_mask > visual->red_mask)) b_o = GBR;
1117 }
1118
1119 int r_mask = GetMaskRed();
1120 int g_mask = GetMaskGreen();
1121 int b_mask = GetMaskBlue();
1122
1123 unsigned char* data = GetData();
1124
1125 int index = 0;
1126 for (int y = 0; y < height; y++)
1127 {
1128 for (int x = 0; x < width; x++)
1129 {
1130 int r = data[index];
1131 index++;
1132 int g = data[index];
1133 index++;
1134 int b = data[index];
1135 index++;
1136
1137 if (HasMask())
1138 {
1139 if ((r == r_mask) && (b == b_mask) && (g == g_mask))
1140 gdk_image_put_pixel( mask_image, x, y, 1 );
1141 else
1142 gdk_image_put_pixel( mask_image, x, y, 0 );
1143 }
1144
1145 if (HasMask())
1146 {
1147 if ((r == r_mask) && (b == b_mask) && (g == g_mask))
1148 gdk_image_put_pixel( mask_image, x, y, 1 );
1149 else
1150 gdk_image_put_pixel( mask_image, x, y, 0 );
1151 }
1152
1153 switch (bpp)
1154 {
1155 case 8:
1156 {
1157 int pixel = -1;
1158 if (wxTheApp->m_colorCube)
1159 {
1160 pixel = wxTheApp->m_colorCube[ ((r & 0xf8) << 7) + ((g & 0xf8) << 2) + ((b & 0xf8) >> 3) ];
1161 }
1162 else
1163 {
1164 GdkColormap *cmap = gtk_widget_get_default_colormap();
1165 GdkColor *colors = cmap->colors;
1166 int max = 3 * (65536);
1167
1168 for (int i = 0; i < cmap->size; i++)
1169 {
1170 int rdiff = (r << 8) - colors[i].red;
1171 int gdiff = (g << 8) - colors[i].green;
1172 int bdiff = (b << 8) - colors[i].blue;
1173 int sum = ABS (rdiff) + ABS (gdiff) + ABS (bdiff);
1174 if (sum < max) { pixel = i; max = sum; }
1175 }
1176 }
1177
1178 gdk_image_put_pixel( data_image, x, y, pixel );
1179
1180 break;
1181 }
1182 case 15:
1183 {
1184 guint32 pixel = ((r & 0xf8) << 7) | ((g & 0xf8) << 2) | ((b & 0xf8) >> 3);
1185 gdk_image_put_pixel( data_image, x, y, pixel );
1186 break;
1187 }
1188 case 16:
1189 {
1190 guint32 pixel = ((r & 0xf8) << 8) | ((g & 0xfc) << 3) | ((b & 0xf8) >> 3);
1191 gdk_image_put_pixel( data_image, x, y, pixel );
1192 break;
1193 }
1194 case 32:
1195 case 24:
1196 {
1197 guint32 pixel = 0;
1198 switch (b_o)
1199 {
1200 case RGB: pixel = (r << 16) | (g << 8) | b; break;
1201 case RBG: pixel = (r << 16) | (b << 8) | g; break;
1202 case BRG: pixel = (b << 16) | (r << 8) | g; break;
1203 case BGR: pixel = (b << 16) | (g << 8) | r; break;
1204 case GRB: pixel = (g << 16) | (r << 8) | b; break;
1205 case GBR: pixel = (g << 16) | (b << 8) | r; break;
1206 }
1207 gdk_image_put_pixel( data_image, x, y, pixel );
1208 }
1209 default: break;
1210 }
1211 } // for
1212 } // for
1213
1214 // Blit picture
1215
1216 GdkGC *data_gc = gdk_gc_new( bitmap.GetPixmap() );
1217
1218 gdk_draw_image( bitmap.GetPixmap(), data_gc, data_image, 0, 0, 0, 0, width, height );
1219
1220 gdk_image_destroy( data_image );
1221 gdk_gc_unref( data_gc );
1222
1223 // Blit mask
1224
1225 if (HasMask())
1226 {
1227 GdkGC *mask_gc = gdk_gc_new( bitmap.GetMask()->GetBitmap() );
1228
1229 gdk_draw_image( bitmap.GetMask()->GetBitmap(), mask_gc, mask_image, 0, 0, 0, 0, width, height );
1230
1231 gdk_image_destroy( mask_image );
1232 gdk_gc_unref( mask_gc );
1233 }
1234
1235 return bitmap;
1236 }
1237
1238 wxImage::wxImage( const wxBitmap &bitmap )
1239 {
1240 wxCHECK_RET( bitmap.Ok(), _T("invalid bitmap") );
1241
1242 GdkImage *gdk_image = gdk_image_get( bitmap.GetPixmap(),
1243 0, 0,
1244 bitmap.GetWidth(), bitmap.GetHeight() );
1245
1246 wxCHECK_RET( gdk_image, _T("couldn't create image") );
1247
1248 Create( bitmap.GetWidth(), bitmap.GetHeight() );
1249 char unsigned *data = GetData();
1250
1251 if (!data)
1252 {
1253 gdk_image_destroy( gdk_image );
1254 wxFAIL_MSG( _T("couldn't create image") );
1255 return;
1256 }
1257
1258 GdkImage *gdk_image_mask = (GdkImage*) NULL;
1259 if (bitmap.GetMask())
1260 {
1261 gdk_image_mask = gdk_image_get( bitmap.GetMask()->GetBitmap(),
1262 0, 0,
1263 bitmap.GetWidth(), bitmap.GetHeight() );
1264
1265 SetMaskColour( 16, 16, 16 ); // anything unlikely and dividable
1266 }
1267
1268 GdkVisual *visual = gdk_window_get_visual( bitmap.GetPixmap() );
1269 if (visual == NULL) visual = gdk_window_get_visual( (GdkWindow*) &gdk_root_parent );
1270 int bpp = visual->depth;
1271 if ((bpp == 16) && (visual->red_mask != 0xf800)) bpp = 15;
1272
1273 GdkColormap *cmap = gtk_widget_get_default_colormap();
1274
1275 long pos = 0;
1276 for (int j = 0; j < bitmap.GetHeight(); j++)
1277 {
1278 for (int i = 0; i < bitmap.GetWidth(); i++)
1279 {
1280 wxInt32 pixel = gdk_image_get_pixel( gdk_image, i, j );
1281 pixel = wxINT32_SWAP_ON_BE( pixel );
1282 if (bpp <= 8)
1283 {
1284 data[pos] = cmap->colors[pixel].red >> 8;
1285 data[pos+1] = cmap->colors[pixel].green >> 8;
1286 data[pos+2] = cmap->colors[pixel].blue >> 8;
1287 } else if (bpp == 15)
1288 {
1289 data[pos] = (pixel >> 7) & 0xf8;
1290 data[pos+1] = (pixel >> 2) & 0xf8;
1291 data[pos+2] = (pixel << 3) & 0xf8;
1292 } else if (bpp == 16)
1293 {
1294 data[pos] = (pixel >> 8) & 0xf8;
1295 data[pos+1] = (pixel >> 3) & 0xfc;
1296 data[pos+2] = (pixel << 3) & 0xf8;
1297 } else
1298 {
1299 data[pos] = (pixel >> 16) & 0xff;
1300 data[pos+1] = (pixel >> 8) & 0xff;
1301 data[pos+2] = pixel & 0xff;
1302 }
1303
1304 if (gdk_image_mask)
1305 {
1306 int mask_pixel = gdk_image_get_pixel( gdk_image_mask, i, j );
1307 if (mask_pixel == 0)
1308 {
1309 data[pos] = 16;
1310 data[pos+1] = 16;
1311 data[pos+2] = 16;
1312 }
1313 }
1314
1315 pos += 3;
1316 }
1317 }
1318
1319 gdk_image_destroy( gdk_image );
1320 if (gdk_image_mask) gdk_image_destroy( gdk_image_mask );
1321 }
1322
1323 #endif
1324
1325 //-----------------------------------------------------------------------------
1326 // Motif conversion routines
1327 //-----------------------------------------------------------------------------
1328
1329 #ifdef __WXMOTIF__
1330
1331 #include <Xm/Xm.h>
1332 #include "wx/utils.h"
1333 #include <math.h>
1334
1335 wxBitmap wxImage::ConvertToBitmap() const
1336 {
1337 wxBitmap bitmap;
1338
1339 wxCHECK_MSG( Ok(), bitmap, _T("invalid image") );
1340
1341 int width = GetWidth();
1342 int height = GetHeight();
1343
1344 bitmap.SetHeight( height );
1345 bitmap.SetWidth( width );
1346
1347 Display *dpy = (Display*) wxGetDisplay();
1348 Visual* vis = DefaultVisual( dpy, DefaultScreen( dpy ) );
1349 int bpp = DefaultDepth( dpy, DefaultScreen( dpy ) );
1350
1351 // Create image
1352
1353 XImage *data_image = XCreateImage( dpy, vis, bpp, ZPixmap, 0, 0, width, height, 32, 0 );
1354 data_image->data = (char*) malloc( data_image->bytes_per_line * data_image->height );
1355
1356 bitmap.Create( width, height, bpp );
1357
1358 /*
1359 // Create mask
1360
1361 GdkImage *mask_image = (GdkImage*) NULL;
1362
1363 if (HasMask())
1364 {
1365 unsigned char *mask_data = (unsigned char*)malloc( ((width >> 3)+8) * height );
1366
1367 mask_image = gdk_image_new_bitmap( gdk_visual_get_system(), mask_data, width, height );
1368
1369 wxMask *mask = new wxMask();
1370 mask->m_bitmap = gdk_pixmap_new( (GdkWindow*)&gdk_root_parent, width, height, 1 );
1371
1372 bitmap.SetMask( mask );
1373 }
1374 */
1375
1376 // Retrieve depth info
1377
1378 XVisualInfo vinfo_template;
1379 XVisualInfo *vi;
1380
1381 vinfo_template.visual = vis;
1382 vinfo_template.visualid = XVisualIDFromVisual( vis );
1383 vinfo_template.depth = bpp;
1384 int nitem = 0;
1385
1386 vi = XGetVisualInfo( dpy, VisualIDMask|VisualDepthMask, &vinfo_template, &nitem );
1387
1388 wxCHECK_MSG( vi, wxNullBitmap, _T("no visual") );
1389
1390 XFree( vi );
1391
1392 if ((bpp == 16) && (vi->red_mask != 0xf800)) bpp = 15;
1393 if (bpp < 8) bpp = 8;
1394
1395 // Render
1396
1397 enum byte_order { RGB, RBG, BRG, BGR, GRB, GBR };
1398 byte_order b_o = RGB;
1399
1400 if (bpp >= 24)
1401 {
1402 if ((vi->red_mask > vi->green_mask) && (vi->green_mask > vi->blue_mask)) b_o = RGB;
1403 else if ((vi->red_mask > vi->blue_mask) && (vi->blue_mask > vi->green_mask)) b_o = RGB;
1404 else if ((vi->blue_mask > vi->red_mask) && (vi->red_mask > vi->green_mask)) b_o = BRG;
1405 else if ((vi->blue_mask > vi->green_mask) && (vi->green_mask > vi->red_mask)) b_o = BGR;
1406 else if ((vi->green_mask > vi->red_mask) && (vi->red_mask > vi->blue_mask)) b_o = GRB;
1407 else if ((vi->green_mask > vi->blue_mask) && (vi->blue_mask > vi->red_mask)) b_o = GBR;
1408 }
1409
1410 /*
1411 int r_mask = GetMaskRed();
1412 int g_mask = GetMaskGreen();
1413 int b_mask = GetMaskBlue();
1414 */
1415
1416 XColor colors[256];
1417 if (bpp == 8)
1418 {
1419 Colormap cmap = (Colormap) wxTheApp->GetMainColormap( dpy );
1420
1421 for (int i = 0; i < 256; i++) colors[i].pixel = i;
1422 XQueryColors( dpy, cmap, colors, 256 );
1423 }
1424
1425 unsigned char* data = GetData();
1426
1427 int index = 0;
1428 for (int y = 0; y < height; y++)
1429 {
1430 for (int x = 0; x < width; x++)
1431 {
1432 int r = data[index];
1433 index++;
1434 int g = data[index];
1435 index++;
1436 int b = data[index];
1437 index++;
1438
1439 /*
1440 if (HasMask())
1441 {
1442 if ((r == r_mask) && (b == b_mask) && (g == g_mask))
1443 gdk_image_put_pixel( mask_image, x, y, 1 );
1444 else
1445 gdk_image_put_pixel( mask_image, x, y, 0 );
1446 }
1447 */
1448
1449 switch (bpp)
1450 {
1451 case 8:
1452 {
1453 int pixel = -1;
1454 /*
1455 if (wxTheApp->m_colorCube)
1456 {
1457 pixel = wxTheApp->m_colorCube
1458 [ ((r & 0xf8) << 7) + ((g & 0xf8) << 2) + ((b & 0xf8) >> 3) ];
1459 }
1460 else
1461 {
1462 */
1463 int max = 3 * (65536);
1464 for (int i = 0; i < 256; i++)
1465 {
1466 int rdiff = (r << 8) - colors[i].red;
1467 int gdiff = (g << 8) - colors[i].green;
1468 int bdiff = (b << 8) - colors[i].blue;
1469 int sum = abs (rdiff) + abs (gdiff) + abs (bdiff);
1470 if (sum < max) { pixel = i; max = sum; }
1471 }
1472 /*
1473 }
1474 */
1475 XPutPixel( data_image, x, y, pixel );
1476 break;
1477 }
1478 case 15:
1479 {
1480 int pixel = ((r & 0xf8) << 7) | ((g & 0xf8) << 2) | ((b & 0xf8) >> 3);
1481 XPutPixel( data_image, x, y, pixel );
1482 break;
1483 }
1484 case 16:
1485 {
1486 int pixel = ((r & 0xf8) << 8) | ((g & 0xfc) << 3) | ((b & 0xf8) >> 3);
1487 XPutPixel( data_image, x, y, pixel );
1488 break;
1489 }
1490 case 32:
1491 case 24:
1492 {
1493 int pixel = 0;
1494 switch (b_o)
1495 {
1496 case RGB: pixel = (r << 16) | (g << 8) | b; break;
1497 case RBG: pixel = (r << 16) | (b << 8) | g; break;
1498 case BRG: pixel = (b << 16) | (r << 8) | g; break;
1499 case BGR: pixel = (b << 16) | (g << 8) | r; break;
1500 case GRB: pixel = (g << 16) | (r << 8) | b; break;
1501 case GBR: pixel = (g << 16) | (b << 8) | r; break;
1502 }
1503 XPutPixel( data_image, x, y, pixel );
1504 }
1505 default: break;
1506 }
1507 } // for
1508 } // for
1509
1510 // Blit picture
1511
1512 XGCValues gcvalues;
1513 gcvalues.foreground = BlackPixel( dpy, DefaultScreen( dpy ) );
1514 GC gc = XCreateGC( dpy, RootWindow ( dpy, DefaultScreen(dpy) ), GCForeground, &gcvalues );
1515 XPutImage( dpy, (Drawable)bitmap.GetPixmap(), gc, data_image, 0, 0, 0, 0, width, height );
1516
1517 XDestroyImage( data_image );
1518 XFreeGC( dpy, gc );
1519
1520 /*
1521 // Blit mask
1522
1523 if (HasMask())
1524 {
1525 GdkGC *mask_gc = gdk_gc_new( bitmap.GetMask()->GetBitmap() );
1526
1527 gdk_draw_image( bitmap.GetMask()->GetBitmap(), mask_gc, mask_image, 0, 0, 0, 0, width, height );
1528
1529 gdk_image_destroy( mask_image );
1530 gdk_gc_unref( mask_gc );
1531 }
1532 */
1533
1534 return bitmap;
1535 }
1536
1537 wxImage::wxImage( const wxBitmap &bitmap )
1538 {
1539 wxCHECK_RET( bitmap.Ok(), _T("invalid bitmap") );
1540
1541 Display *dpy = (Display*) wxGetDisplay();
1542 Visual* vis = DefaultVisual( dpy, DefaultScreen( dpy ) );
1543 int bpp = DefaultDepth( dpy, DefaultScreen( dpy ) );
1544
1545 XImage *ximage = XGetImage( dpy,
1546 (Drawable)bitmap.GetPixmap(),
1547 0, 0,
1548 bitmap.GetWidth(), bitmap.GetHeight(),
1549 AllPlanes, ZPixmap );
1550
1551 wxCHECK_RET( ximage, _T("couldn't create image") );
1552
1553 Create( bitmap.GetWidth(), bitmap.GetHeight() );
1554 char unsigned *data = GetData();
1555
1556 if (!data)
1557 {
1558 XDestroyImage( ximage );
1559 wxFAIL_MSG( _T("couldn't create image") );
1560 return;
1561 }
1562
1563 /*
1564 GdkImage *gdk_image_mask = (GdkImage*) NULL;
1565 if (bitmap.GetMask())
1566 {
1567 gdk_image_mask = gdk_image_get( bitmap.GetMask()->GetBitmap(),
1568 0, 0,
1569 bitmap.GetWidth(), bitmap.GetHeight() );
1570
1571 SetMaskColour( 16, 16, 16 ); // anything unlikely and dividable
1572 }
1573 */
1574
1575 // Retrieve depth info
1576
1577 XVisualInfo vinfo_template;
1578 XVisualInfo *vi;
1579
1580 vinfo_template.visual = vis;
1581 vinfo_template.visualid = XVisualIDFromVisual( vis );
1582 vinfo_template.depth = bpp;
1583 int nitem = 0;
1584
1585 vi = XGetVisualInfo( dpy, VisualIDMask|VisualDepthMask, &vinfo_template, &nitem );
1586
1587 wxCHECK_RET( vi, _T("no visual") );
1588
1589 if ((bpp == 16) && (vi->red_mask != 0xf800)) bpp = 15;
1590
1591 XFree( vi );
1592
1593 XColor colors[256];
1594 if (bpp == 8)
1595 {
1596 Colormap cmap = (Colormap)wxTheApp->GetMainColormap( dpy );
1597
1598 for (int i = 0; i < 256; i++) colors[i].pixel = i;
1599 XQueryColors( dpy, cmap, colors, 256 );
1600 }
1601
1602 long pos = 0;
1603 for (int j = 0; j < bitmap.GetHeight(); j++)
1604 {
1605 for (int i = 0; i < bitmap.GetWidth(); i++)
1606 {
1607 int pixel = XGetPixel( ximage, i, j );
1608 if (bpp <= 8)
1609 {
1610 data[pos] = colors[pixel].red >> 8;
1611 data[pos+1] = colors[pixel].green >> 8;
1612 data[pos+2] = colors[pixel].blue >> 8;
1613 } else if (bpp == 15)
1614 {
1615 data[pos] = (pixel >> 7) & 0xf8;
1616 data[pos+1] = (pixel >> 2) & 0xf8;
1617 data[pos+2] = (pixel << 3) & 0xf8;
1618 } else if (bpp == 16)
1619 {
1620 data[pos] = (pixel >> 8) & 0xf8;
1621 data[pos+1] = (pixel >> 3) & 0xfc;
1622 data[pos+2] = (pixel << 3) & 0xf8;
1623 } else
1624 {
1625 data[pos] = (pixel >> 16) & 0xff;
1626 data[pos+1] = (pixel >> 8) & 0xff;
1627 data[pos+2] = pixel & 0xff;
1628 }
1629
1630 /*
1631 if (gdk_image_mask)
1632 {
1633 int mask_pixel = gdk_image_get_pixel( gdk_image_mask, i, j );
1634 if (mask_pixel == 0)
1635 {
1636 data[pos] = 16;
1637 data[pos+1] = 16;
1638 data[pos+2] = 16;
1639 }
1640 }
1641 */
1642
1643 pos += 3;
1644 }
1645 }
1646
1647 XDestroyImage( ximage );
1648 /*
1649 if (gdk_image_mask) gdk_image_destroy( gdk_image_mask );
1650 */
1651 }
1652 #endif
1653
1654 // A module to allow wxImage initialization/cleanup
1655 // without calling these functions from app.cpp or from
1656 // the user's application.
1657
1658 class wxImageModule: public wxModule
1659 {
1660 DECLARE_DYNAMIC_CLASS(wxImageModule)
1661 public:
1662 wxImageModule() {}
1663 bool OnInit() { wxImage::InitStandardHandlers(); return TRUE; };
1664 void OnExit() { wxImage::CleanUpHandlers(); };
1665 };
1666
1667 IMPLEMENT_DYNAMIC_CLASS(wxImageModule, wxModule)