]> git.saurik.com Git - wxWidgets.git/blob - src/common/image.cpp
added default ctor for wxDirDialog
[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 public:
49 wxImageRefData();
50 ~wxImageRefData();
51
52 int m_width;
53 int m_height;
54 unsigned char *m_data;
55 bool m_hasMask;
56 unsigned char m_maskRed,m_maskGreen,m_maskBlue;
57 bool m_ok;
58 };
59
60 wxImageRefData::wxImageRefData()
61 {
62 m_width = 0;
63 m_height = 0;
64 m_data = (unsigned char*) NULL;
65 m_ok = FALSE;
66 m_maskRed = 0;
67 m_maskGreen = 0;
68 m_maskBlue = 0;
69 m_hasMask = FALSE;
70 }
71
72 wxImageRefData::~wxImageRefData()
73 {
74 if (m_data)
75 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, wxT("invalid image") );
158
159 wxCHECK_MSG( (width > 0) && (height > 0), image, wxT("invalid image size") );
160
161 image.Create( width, height );
162
163 char unsigned *data = image.GetData();
164
165 wxCHECK_MSG( data, image, wxT("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, wxT("invalid image") );
197
198 wxCHECK_MSG( (rect.GetLeft()>=0) && (rect.GetTop()>=0) && (rect.GetRight()<=GetWidth()) && (rect.GetBottom()<=GetHeight()),
199 image, wxT("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, wxT("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(), wxT("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), wxT("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, wxT("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, wxT("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, wxT("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, wxT("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, wxT("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, wxT("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, wxT("invalid image") );
295
296 return M_IMGDATA->m_data;
297 }
298
299 void wxImage::SetData( char unsigned *data )
300 {
301 wxCHECK_RET( Ok(), wxT("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(), wxT("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, wxT("invalid image") );
332
333 return M_IMGDATA->m_maskRed;
334 }
335
336 unsigned char wxImage::GetMaskGreen() const
337 {
338 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
339
340 return M_IMGDATA->m_maskGreen;
341 }
342
343 unsigned char wxImage::GetMaskBlue() const
344 {
345 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
346
347 return M_IMGDATA->m_maskBlue;
348 }
349
350 void wxImage::SetMask( bool mask )
351 {
352 wxCHECK_RET( Ok(), wxT("invalid image") );
353
354 M_IMGDATA->m_hasMask = mask;
355 }
356
357 bool wxImage::HasMask() const
358 {
359 wxCHECK_MSG( Ok(), FALSE, wxT("invalid image") );
360
361 return M_IMGDATA->m_hasMask;
362 }
363
364 int wxImage::GetWidth() const
365 {
366 wxCHECK_MSG( Ok(), 0, wxT("invalid image") );
367
368 return M_IMGDATA->m_width;
369 }
370
371 int wxImage::GetHeight() const
372 {
373 wxCHECK_MSG( Ok(), 0, wxT("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 wxBufferedInputStream bstream( stream );
385 return LoadFile(bstream, type);
386 }
387 else
388 {
389 wxLogError( _("Can't load image from file '%s': file does not exist."), filename.c_str() );
390
391 return FALSE;
392 }
393 #else // !wxUSE_STREAMS
394 return FALSE;
395 #endif // wxUSE_STREAMS
396 }
397
398 bool wxImage::LoadFile( const wxString& filename, const wxString& mimetype )
399 {
400 #if wxUSE_STREAMS
401 if (wxFileExists(filename))
402 {
403 wxFileInputStream stream(filename);
404 wxBufferedInputStream bstream( stream );
405 return LoadFile(bstream, mimetype);
406 }
407 else
408 {
409 wxLogError( _("Can't load image from file '%s': file does not exist."), filename.c_str() );
410
411 return FALSE;
412 }
413 #else // !wxUSE_STREAMS
414 return FALSE;
415 #endif // wxUSE_STREAMS
416 }
417
418 bool wxImage::SaveFile( const wxString& filename, int type )
419 {
420 #if wxUSE_STREAMS
421 wxFileOutputStream stream(filename);
422
423 if ( stream.LastError() == wxStream_NOERROR )
424 {
425 wxBufferedOutputStream bstream( stream );
426 return SaveFile(bstream, type);
427 }
428 else
429 #endif // wxUSE_STREAMS
430 return FALSE;
431 }
432
433 bool wxImage::SaveFile( const wxString& filename, const wxString& mimetype )
434 {
435 #if wxUSE_STREAMS
436 wxFileOutputStream stream(filename);
437
438 if ( stream.LastError() == wxStream_NOERROR )
439 {
440 wxBufferedOutputStream bstream( stream );
441 return SaveFile(bstream, mimetype);
442 }
443 else
444 #endif // wxUSE_STREAMS
445 return FALSE;
446 }
447
448 bool wxImage::CanRead( const wxString &name )
449 {
450 #if wxUSE_STREAMS
451 wxFileInputStream stream(name);
452 return CanRead(stream);
453 #else
454 return FALSE;
455 #endif
456 }
457
458 #if wxUSE_STREAMS
459
460 bool wxImage::CanRead( wxInputStream &stream )
461 {
462 wxList &list=GetHandlers();
463
464 for ( wxList::Node *node = list.GetFirst(); node; node = node->GetNext() )
465 {
466 wxImageHandler *handler=(wxImageHandler*)node->GetData();
467 if (handler->CanRead( stream ))
468 return TRUE;
469 }
470
471 return FALSE;
472 }
473
474 bool wxImage::LoadFile( wxInputStream& stream, long type )
475 {
476 UnRef();
477
478 m_refData = new wxImageRefData;
479
480 wxImageHandler *handler;
481
482 if (type==wxBITMAP_TYPE_ANY)
483 {
484 wxList &list=GetHandlers();
485
486 for ( wxList::Node *node = list.GetFirst(); node; node = node->GetNext() )
487 {
488 handler=(wxImageHandler*)node->GetData();
489 if (handler->CanRead( stream ))
490 return handler->LoadFile( this, stream );
491
492 }
493
494 wxLogWarning( _("No handler found for image type.") );
495 return FALSE;
496 }
497
498 handler = FindHandler(type);
499
500 if (handler == NULL)
501 {
502 wxLogWarning( _("No image handler for type %d defined."), type );
503
504 return FALSE;
505 }
506
507 return handler->LoadFile( this, stream );
508 }
509
510 bool wxImage::LoadFile( wxInputStream& stream, const wxString& mimetype )
511 {
512 UnRef();
513
514 m_refData = new wxImageRefData;
515
516 wxImageHandler *handler = FindHandlerMime(mimetype);
517
518 if (handler == NULL)
519 {
520 wxLogWarning( _("No image handler for type %s defined."), mimetype.GetData() );
521
522 return FALSE;
523 }
524
525 return handler->LoadFile( this, stream );
526 }
527
528 bool wxImage::SaveFile( wxOutputStream& stream, int type )
529 {
530 wxCHECK_MSG( Ok(), FALSE, wxT("invalid image") );
531
532 wxImageHandler *handler = FindHandler(type);
533
534 if (handler == NULL)
535 {
536 wxLogWarning( _("No image handler for type %d defined."), type );
537
538 return FALSE;
539 }
540
541 return handler->SaveFile( this, stream );
542 }
543
544 bool wxImage::SaveFile( wxOutputStream& stream, const wxString& mimetype )
545 {
546 wxCHECK_MSG( Ok(), FALSE, wxT("invalid image") );
547
548 wxImageHandler *handler = FindHandlerMime(mimetype);
549
550 if (handler == NULL)
551 {
552 wxLogWarning( _("No image handler for type %s defined."), mimetype.GetData() );
553
554 return FALSE;
555 }
556
557 return handler->SaveFile( this, stream );
558 }
559 #endif // wxUSE_STREAMS
560
561 void wxImage::AddHandler( wxImageHandler *handler )
562 {
563 // make sure that the memory will be freed at the program end
564 sm_handlers.DeleteContents(TRUE);
565
566 sm_handlers.Append( handler );
567 }
568
569 void wxImage::InsertHandler( wxImageHandler *handler )
570 {
571 // make sure that the memory will be freed at the program end
572 sm_handlers.DeleteContents(TRUE);
573
574 sm_handlers.Insert( handler );
575 }
576
577 bool wxImage::RemoveHandler( const wxString& name )
578 {
579 wxImageHandler *handler = FindHandler(name);
580 if (handler)
581 {
582 sm_handlers.DeleteObject(handler);
583 return TRUE;
584 }
585 else
586 return FALSE;
587 }
588
589 wxImageHandler *wxImage::FindHandler( const wxString& name )
590 {
591 wxNode *node = sm_handlers.First();
592 while (node)
593 {
594 wxImageHandler *handler = (wxImageHandler*)node->Data();
595 if (handler->GetName().Cmp(name) == 0) return handler;
596
597 node = node->Next();
598 }
599 return (wxImageHandler *)NULL;
600 }
601
602 wxImageHandler *wxImage::FindHandler( const wxString& extension, long bitmapType )
603 {
604 wxNode *node = sm_handlers.First();
605 while (node)
606 {
607 wxImageHandler *handler = (wxImageHandler*)node->Data();
608 if ( (handler->GetExtension().Cmp(extension) == 0) &&
609 (bitmapType == -1 || handler->GetType() == bitmapType) )
610 return handler;
611 node = node->Next();
612 }
613 return (wxImageHandler*)NULL;
614 }
615
616 wxImageHandler *wxImage::FindHandler( long bitmapType )
617 {
618 wxNode *node = sm_handlers.First();
619 while (node)
620 {
621 wxImageHandler *handler = (wxImageHandler *)node->Data();
622 if (handler->GetType() == bitmapType) return handler;
623 node = node->Next();
624 }
625 return NULL;
626 }
627
628 wxImageHandler *wxImage::FindHandlerMime( const wxString& mimetype )
629 {
630 wxNode *node = sm_handlers.First();
631 while (node)
632 {
633 wxImageHandler *handler = (wxImageHandler *)node->Data();
634 if (handler->GetMimeType().IsSameAs(mimetype, FALSE)) return handler;
635 node = node->Next();
636 }
637 return NULL;
638 }
639
640 void wxImage::InitStandardHandlers()
641 {
642 AddHandler( new wxBMPHandler );
643 }
644
645 void wxImage::CleanUpHandlers()
646 {
647 wxNode *node = sm_handlers.First();
648 while (node)
649 {
650 wxImageHandler *handler = (wxImageHandler *)node->Data();
651 wxNode *next = node->Next();
652 delete handler;
653 delete node;
654 node = next;
655 }
656 }
657
658 //-----------------------------------------------------------------------------
659 // wxImageHandler
660 //-----------------------------------------------------------------------------
661
662 #if !USE_SHARED_LIBRARIES
663 IMPLEMENT_ABSTRACT_CLASS(wxImageHandler,wxObject)
664 #endif
665
666 #if wxUSE_STREAMS
667 bool wxImageHandler::LoadFile( wxImage *WXUNUSED(image), wxInputStream& WXUNUSED(stream), bool WXUNUSED(verbose), int WXUNUSED(index) )
668 {
669 return FALSE;
670 }
671
672 bool wxImageHandler::SaveFile( wxImage *WXUNUSED(image), wxOutputStream& WXUNUSED(stream), bool WXUNUSED(verbose) )
673 {
674 return FALSE;
675 }
676
677 int wxImageHandler::GetImageCount( wxInputStream& WXUNUSED(stream) )
678 {
679 return 1;
680 }
681
682 bool wxImageHandler::CanRead( const wxString& name )
683 {
684 if (wxFileExists(name))
685 {
686 wxFileInputStream stream(name);
687 return CanRead(stream);
688 }
689
690 else {
691 wxLogError( _("Can't check image format of file '%s': file does not exist."), name.c_str() );
692
693 return FALSE;
694 }
695 // return FALSE;
696 }
697
698 #endif // wxUSE_STREAMS
699
700 //-----------------------------------------------------------------------------
701 // MSW conversion routines
702 //-----------------------------------------------------------------------------
703
704 #ifdef __WXMSW__
705
706 wxBitmap wxImage::ConvertToBitmap() const
707 {
708 if ( !Ok() )
709 return wxNullBitmap;
710
711 // sizeLimit is the MS upper limit for the DIB size
712 #ifdef WIN32
713 int sizeLimit = 1024*768*3;
714 #else
715 int sizeLimit = 0x7fff ;
716 #endif
717
718 // width and height of the device-dependent bitmap
719 int width = GetWidth();
720 int bmpHeight = GetHeight();
721
722 // calc the number of bytes per scanline and padding
723 int bytePerLine = width*3;
724 int sizeDWORD = sizeof( DWORD );
725 int lineBoundary = bytePerLine % sizeDWORD;
726 int padding = 0;
727 if( lineBoundary > 0 )
728 {
729 padding = sizeDWORD - lineBoundary;
730 bytePerLine += padding;
731 }
732 // calc the number of DIBs and heights of DIBs
733 int numDIB = 1;
734 int hRemain = 0;
735 int height = sizeLimit/bytePerLine;
736 if( height >= bmpHeight )
737 height = bmpHeight;
738 else
739 {
740 numDIB = bmpHeight / height;
741 hRemain = bmpHeight % height;
742 if( hRemain >0 ) numDIB++;
743 }
744
745 // set bitmap parameters
746 wxBitmap bitmap;
747 wxCHECK_MSG( Ok(), bitmap, wxT("invalid image") );
748 bitmap.SetWidth( width );
749 bitmap.SetHeight( bmpHeight );
750 bitmap.SetDepth( wxDisplayDepth() );
751
752 // create a DIB header
753 int headersize = sizeof(BITMAPINFOHEADER);
754 LPBITMAPINFO lpDIBh = (BITMAPINFO *) malloc( headersize );
755 wxCHECK_MSG( lpDIBh, bitmap, wxT("could not allocate memory for DIB header") );
756 // Fill in the DIB header
757 lpDIBh->bmiHeader.biSize = headersize;
758 lpDIBh->bmiHeader.biWidth = (DWORD)width;
759 lpDIBh->bmiHeader.biHeight = (DWORD)(-height);
760 lpDIBh->bmiHeader.biSizeImage = bytePerLine*height;
761 // the general formula for biSizeImage:
762 // ( ( ( ((DWORD)width*24) +31 ) & ~31 ) >> 3 ) * height;
763 lpDIBh->bmiHeader.biPlanes = 1;
764 lpDIBh->bmiHeader.biBitCount = 24;
765 lpDIBh->bmiHeader.biCompression = BI_RGB;
766 lpDIBh->bmiHeader.biClrUsed = 0;
767 // These seem not really needed for our purpose here.
768 lpDIBh->bmiHeader.biClrImportant = 0;
769 lpDIBh->bmiHeader.biXPelsPerMeter = 0;
770 lpDIBh->bmiHeader.biYPelsPerMeter = 0;
771 // memory for DIB data
772 unsigned char *lpBits;
773 lpBits = (unsigned char *)malloc( lpDIBh->bmiHeader.biSizeImage );
774 if( !lpBits )
775 {
776 wxFAIL_MSG( wxT("could not allocate memory for DIB") );
777 free( lpDIBh );
778 return bitmap;
779 }
780
781 // create and set the device-dependent bitmap
782 HDC hdc = ::GetDC(NULL);
783 HDC memdc = ::CreateCompatibleDC( hdc );
784 HBITMAP hbitmap;
785 hbitmap = ::CreateCompatibleBitmap( hdc, width, bmpHeight );
786 ::SelectObject( memdc, hbitmap);
787
788 // copy image data into DIB data and then into DDB (in a loop)
789 unsigned char *data = GetData();
790 int i, j, n;
791 int origin = 0;
792 unsigned char *ptdata = data;
793 unsigned char *ptbits;
794
795 for( n=0; n<numDIB; n++ )
796 {
797 if( numDIB > 1 && n == numDIB-1 && hRemain > 0 )
798 {
799 // redefine height and size of the (possibly) last smaller DIB
800 // memory is not reallocated
801 height = hRemain;
802 lpDIBh->bmiHeader.biHeight = (DWORD)(-height);
803 lpDIBh->bmiHeader.biSizeImage = bytePerLine*height;
804 }
805 ptbits = lpBits;
806
807 for( j=0; j<height; j++ )
808 {
809 for( i=0; i<width; i++ )
810 {
811 *(ptbits++) = *(ptdata+2);
812 *(ptbits++) = *(ptdata+1);
813 *(ptbits++) = *(ptdata );
814 ptdata += 3;
815 }
816 for( i=0; i< padding; i++ ) *(ptbits++) = 0;
817 }
818 ::StretchDIBits( memdc, 0, origin, width, height,\
819 0, 0, width, height, lpBits, lpDIBh, DIB_RGB_COLORS, SRCCOPY);
820 origin += height;
821 // if numDIB = 1, lines below can also be used
822 // hbitmap = CreateDIBitmap( hdc, &(lpDIBh->bmiHeader), CBM_INIT, lpBits, lpDIBh, DIB_RGB_COLORS );
823 // The above line is equivalent to the following two lines.
824 // hbitmap = ::CreateCompatibleBitmap( hdc, width, height );
825 // ::SetDIBits( hdc, hbitmap, 0, height, lpBits, lpDIBh, DIB_RGB_COLORS);
826 // or the following lines
827 // hbitmap = ::CreateCompatibleBitmap( hdc, width, height );
828 // HDC memdc = ::CreateCompatibleDC( hdc );
829 // ::SelectObject( memdc, hbitmap);
830 // ::SetDIBitsToDevice( memdc, 0, 0, width, height,
831 // 0, 0, 0, height, (void *)lpBits, lpDIBh, DIB_RGB_COLORS);
832 // ::SelectObject( memdc, 0 );
833 // ::DeleteDC( memdc );
834 }
835 bitmap.SetHBITMAP( (WXHBITMAP) hbitmap );
836
837 // similarly, created an mono-bitmap for the possible mask
838 if( HasMask() )
839 {
840 hbitmap = ::CreateBitmap( (WORD)width, (WORD)bmpHeight, 1, 1, NULL );
841 ::SelectObject( memdc, hbitmap);
842 if( numDIB == 1 ) height = bmpHeight;
843 else height = sizeLimit/bytePerLine;
844 lpDIBh->bmiHeader.biHeight = (DWORD)(-height);
845 lpDIBh->bmiHeader.biSizeImage = bytePerLine*height;
846 origin = 0;
847 unsigned char r = GetMaskRed();
848 unsigned char g = GetMaskGreen();
849 unsigned char b = GetMaskBlue();
850 unsigned char zero = 0, one = 255;
851 ptdata = data;
852 for( n=0; n<numDIB; n++ )
853 {
854 if( numDIB > 1 && n == numDIB - 1 && hRemain > 0 )
855 {
856 // redefine height and size of the (possibly) last smaller DIB
857 // memory is not reallocated
858 height = hRemain;
859 lpDIBh->bmiHeader.biHeight = (DWORD)(-height);
860 lpDIBh->bmiHeader.biSizeImage = bytePerLine*height;
861 }
862 ptbits = lpBits;
863 for( int j=0; j<height; j++ )
864 {
865 for(i=0; i<width; i++ )
866 {
867 if( (*(ptdata++)!=r) | (*(ptdata++)!=g) | (*(ptdata++)!=b) )
868 {
869 *(ptbits++) = one;
870 *(ptbits++) = one;
871 *(ptbits++) = one;
872 }
873 else
874 {
875 *(ptbits++) = zero;
876 *(ptbits++) = zero;
877 *(ptbits++) = zero;
878 }
879 }
880 for( i=0; i< padding; i++ ) *(ptbits++) = zero;
881 }
882 ::StretchDIBits( memdc, 0, origin, width, height,\
883 0, 0, width, height, lpBits, lpDIBh, DIB_RGB_COLORS, SRCCOPY);
884 origin += height;
885 }
886 // create a wxMask object
887 wxMask *mask = new wxMask();
888 mask->SetMaskBitmap( (WXHBITMAP) hbitmap );
889 bitmap.SetMask( mask );
890 // It will be deleted when the wxBitmap object is deleted (as of 01/1999)
891 /* The following can also be used but is slow to run
892 wxColour colour( GetMaskRed(), GetMaskGreen(), GetMaskBlue());
893 wxMask *mask = new wxMask( bitmap, colour );
894 bitmap.SetMask( mask );
895 */
896 }
897
898 // free allocated resources
899 ::SelectObject( memdc, 0 );
900 ::DeleteDC( memdc );
901 ::ReleaseDC(NULL, hdc);
902 free(lpDIBh);
903 free(lpBits);
904
905 #if WXWIN_COMPATIBILITY_2
906 // check the wxBitmap object
907 bitmap.GetBitmapData()->SetOk();
908 #endif // WXWIN_COMPATIBILITY_2
909
910 return bitmap;
911 }
912
913 wxImage::wxImage( const wxBitmap &bitmap )
914 {
915 // check the bitmap
916 if( !bitmap.Ok() )
917 {
918 wxFAIL_MSG( wxT("invalid bitmap") );
919 return;
920 }
921
922 // create an wxImage object
923 int width = bitmap.GetWidth();
924 int height = bitmap.GetHeight();
925 Create( width, height );
926 unsigned char *data = GetData();
927 if( !data )
928 {
929 wxFAIL_MSG( wxT("could not allocate data for image") );
930 return;
931 }
932
933 // calc the number of bytes per scanline and padding in the DIB
934 int bytePerLine = width*3;
935 int sizeDWORD = sizeof( DWORD );
936 int lineBoundary = bytePerLine % sizeDWORD;
937 int padding = 0;
938 if( lineBoundary > 0 )
939 {
940 padding = sizeDWORD - lineBoundary;
941 bytePerLine += padding;
942 }
943
944 // create a DIB header
945 int headersize = sizeof(BITMAPINFOHEADER);
946 LPBITMAPINFO lpDIBh = (BITMAPINFO *) malloc( headersize );
947 if( !lpDIBh )
948 {
949 wxFAIL_MSG( wxT("could not allocate data for DIB header") );
950 free( data );
951 return;
952 }
953 // Fill in the DIB header
954 lpDIBh->bmiHeader.biSize = headersize;
955 lpDIBh->bmiHeader.biWidth = width;
956 lpDIBh->bmiHeader.biHeight = -height;
957 lpDIBh->bmiHeader.biSizeImage = bytePerLine * height;
958 lpDIBh->bmiHeader.biPlanes = 1;
959 lpDIBh->bmiHeader.biBitCount = 24;
960 lpDIBh->bmiHeader.biCompression = BI_RGB;
961 lpDIBh->bmiHeader.biClrUsed = 0;
962 // These seem not really needed for our purpose here.
963 lpDIBh->bmiHeader.biClrImportant = 0;
964 lpDIBh->bmiHeader.biXPelsPerMeter = 0;
965 lpDIBh->bmiHeader.biYPelsPerMeter = 0;
966 // memory for DIB data
967 unsigned char *lpBits;
968 lpBits = (unsigned char *) malloc( lpDIBh->bmiHeader.biSizeImage );
969 if( !lpBits )
970 {
971 wxFAIL_MSG( wxT("could not allocate data for DIB") );
972 free( data );
973 free( lpDIBh );
974 return;
975 }
976
977 // copy data from the device-dependent bitmap to the DIB
978 HDC hdc = ::GetDC(NULL);
979 HBITMAP hbitmap;
980 hbitmap = (HBITMAP) bitmap.GetHBITMAP();
981 ::GetDIBits( hdc, hbitmap, 0, height, lpBits, lpDIBh, DIB_RGB_COLORS );
982
983 // copy DIB data into the wxImage object
984 int i, j;
985 unsigned char *ptdata = data;
986 unsigned char *ptbits = lpBits;
987 for( i=0; i<height; i++ )
988 {
989 for( j=0; j<width; j++ )
990 {
991 *(ptdata++) = *(ptbits+2);
992 *(ptdata++) = *(ptbits+1);
993 *(ptdata++) = *(ptbits );
994 ptbits += 3;
995 }
996 ptbits += padding;
997 }
998
999 // similarly, set data according to the possible mask bitmap
1000 if( bitmap.GetMask() && bitmap.GetMask()->GetMaskBitmap() )
1001 {
1002 hbitmap = (HBITMAP) bitmap.GetMask()->GetMaskBitmap();
1003 // memory DC created, color set, data copied, and memory DC deleted
1004 HDC memdc = ::CreateCompatibleDC( hdc );
1005 ::SetTextColor( memdc, RGB( 0, 0, 0 ) );
1006 ::SetBkColor( memdc, RGB( 255, 255, 255 ) );
1007 ::GetDIBits( memdc, hbitmap, 0, height, lpBits, lpDIBh, DIB_RGB_COLORS );
1008 ::DeleteDC( memdc );
1009 // background color set to RGB(16,16,16) in consistent with wxGTK
1010 unsigned char r=16, g=16, b=16;
1011 ptdata = data;
1012 ptbits = lpBits;
1013 for( i=0; i<height; i++ )
1014 {
1015 for( j=0; j<width; j++ )
1016 {
1017 if( *ptbits != 0 )
1018 ptdata += 3;
1019 else
1020 {
1021 *(ptdata++) = r;
1022 *(ptdata++) = g;
1023 *(ptdata++) = b;
1024 }
1025 ptbits += 3;
1026 }
1027 ptbits += padding;
1028 }
1029 SetMaskColour( r, g, b );
1030 SetMask( TRUE );
1031 }
1032 else
1033 {
1034 SetMask( FALSE );
1035 }
1036 // free allocated resources
1037 ::ReleaseDC(NULL, hdc);
1038 free(lpDIBh);
1039 free(lpBits);
1040 }
1041
1042 #endif
1043
1044 #ifdef __WXMAC__
1045
1046 #include <PictUtils.h>
1047
1048 extern CTabHandle wxMacCreateColorTable( int numColors ) ;
1049 extern void wxMacDestroyColorTable( CTabHandle colors ) ;
1050 extern void wxMacSetColorTableEntry( CTabHandle newColors , int index , int red , int green , int blue ) ;
1051 extern GWorldPtr wxMacCreateGWorld( int height , int width , int depth ) ;
1052 extern void wxMacDestroyGWorld( GWorldPtr gw ) ;
1053
1054 wxBitmap wxImage::ConvertToBitmap() const
1055 {
1056 // width and height of the device-dependent bitmap
1057 int width = GetWidth();
1058 int height = GetHeight();
1059
1060 // Create picture
1061
1062 wxBitmap bitmap( width , height , wxDisplayDepth() ) ;
1063
1064 // Create mask
1065
1066 if (HasMask())
1067 {
1068 /*
1069 unsigned char *mask_data = (unsigned char*)malloc( ((width >> 3)+8) * height );
1070
1071 mask_image = gdk_image_new_bitmap( gdk_visual_get_system(), mask_data, width, height );
1072
1073 wxMask *mask = new wxMask();
1074 mask->m_bitmap = gdk_pixmap_new( (GdkWindow*)&gdk_root_parent, width, height, 1 );
1075
1076 bitmap.SetMask( mask );
1077 */
1078 }
1079
1080 // Render
1081
1082 int r_mask = GetMaskRed();
1083 int g_mask = GetMaskGreen();
1084 int b_mask = GetMaskBlue();
1085
1086 CGrafPtr origPort ;
1087 GDHandle origDevice ;
1088
1089 GetGWorld( &origPort , &origDevice ) ;
1090 SetGWorld( bitmap.GetHBITMAP() , NULL ) ;
1091
1092 register unsigned char* data = GetData();
1093
1094 int index = 0;
1095 for (int y = 0; y < height; y++)
1096 {
1097 #if 0
1098 unsigned char lastr = 0 ;
1099 unsigned char lastg = 0 ;
1100 unsigned char lastb = 0 ;
1101 RGBColor lastcolor ;
1102
1103 MoveTo( 0 , y ) ;
1104 for (int x = 0; x < width; x++)
1105 {
1106 unsigned char r = data[index++];
1107 unsigned char g = data[index++];
1108 unsigned char b = data[index++];
1109
1110 if ( r != lastr || g != lastg || b != lastb )
1111 {
1112 lastcolor.red = ( lastr << 8 ) + lastr ;
1113 lastcolor.green = ( lastg << 8 ) + lastg ;
1114 lastcolor.blue = ( lastb << 8 ) + lastb ;
1115 RGBForeColor( &lastcolor ) ;
1116 LineTo( x , y ) ;
1117 lastr = r ;
1118 lastg = g ;
1119 lastb = b ;
1120 }
1121 } // for width
1122 lastcolor.red = ( lastr << 8 ) + lastr ;
1123 lastcolor.green = ( lastg << 8 ) + lastg ;
1124 lastcolor.blue = ( lastb << 8 ) + lastb ;
1125 RGBForeColor( &lastcolor ) ;
1126 LineTo( width - 1 , y ) ;
1127 #else
1128 for (int x = 0; x < width; x++)
1129 {
1130 unsigned char r = data[index++];
1131 unsigned char g = data[index++];
1132 unsigned char b = data[index++];
1133 RGBColor color ;
1134 color.red = ( r << 8 ) + r ;
1135 color.green = ( g << 8 ) + g ;
1136 color.blue = ( b << 8 ) + b ;
1137 SetCPixel( x , y , &color ) ;
1138 }
1139 #endif
1140 } // for height
1141
1142 SetGWorld( origPort , origDevice ) ;
1143
1144 return bitmap;
1145
1146 }
1147
1148 wxImage::wxImage( const wxBitmap &bitmap )
1149 {
1150 // check the bitmap
1151 if( !bitmap.Ok() )
1152 {
1153 wxFAIL_MSG( "invalid bitmap" );
1154 return;
1155 }
1156
1157 // create an wxImage object
1158 int width = bitmap.GetWidth();
1159 int height = bitmap.GetHeight();
1160 Create( width, height );
1161 /*
1162 unsigned char *data = GetData();
1163 if( !data )
1164 {
1165 wxFAIL_MSG( "could not allocate data for image" );
1166 return;
1167 }
1168
1169 // calc the number of bytes per scanline and padding in the DIB
1170 int bytePerLine = width*3;
1171 int sizeDWORD = sizeof( DWORD );
1172 div_t lineBoundary = div( bytePerLine, sizeDWORD );
1173 int padding = 0;
1174 if( lineBoundary.rem > 0 )
1175 {
1176 padding = sizeDWORD - lineBoundary.rem;
1177 bytePerLine += padding;
1178 }
1179
1180 // create a DIB header
1181 int headersize = sizeof(BITMAPINFOHEADER);
1182 LPBITMAPINFO lpDIBh = (BITMAPINFO *) malloc( headersize );
1183 if( !lpDIBh )
1184 {
1185 wxFAIL_MSG( "could not allocate data for DIB header" );
1186 free( data );
1187 return;
1188 }
1189 // Fill in the DIB header
1190 lpDIBh->bmiHeader.biSize = headersize;
1191 lpDIBh->bmiHeader.biWidth = width;
1192 lpDIBh->bmiHeader.biHeight = -height;
1193 lpDIBh->bmiHeader.biSizeImage = bytePerLine * height;
1194 lpDIBh->bmiHeader.biPlanes = 1;
1195 lpDIBh->bmiHeader.biBitCount = 24;
1196 lpDIBh->bmiHeader.biCompression = BI_RGB;
1197 lpDIBh->bmiHeader.biClrUsed = 0;
1198 // These seem not really needed for our purpose here.
1199 lpDIBh->bmiHeader.biClrImportant = 0;
1200 lpDIBh->bmiHeader.biXPelsPerMeter = 0;
1201 lpDIBh->bmiHeader.biYPelsPerMeter = 0;
1202 // memory for DIB data
1203 unsigned char *lpBits;
1204 lpBits = (unsigned char *) malloc( lpDIBh->bmiHeader.biSizeImage );
1205 if( !lpBits )
1206 {
1207 wxFAIL_MSG( "could not allocate data for DIB" );
1208 free( data );
1209 free( lpDIBh );
1210 return;
1211 }
1212
1213 // copy data from the device-dependent bitmap to the DIB
1214 HDC hdc = ::GetDC(NULL);
1215 HBITMAP hbitmap;
1216 hbitmap = (HBITMAP) bitmap.GetHBITMAP();
1217 ::GetDIBits( hdc, hbitmap, 0, height, lpBits, lpDIBh, DIB_RGB_COLORS );
1218
1219 // copy DIB data into the wxImage object
1220 int i, j;
1221 unsigned char *ptdata = data;
1222 unsigned char *ptbits = lpBits;
1223 for( i=0; i<height; i++ )
1224 {
1225 for( j=0; j<width; j++ )
1226 {
1227 *(ptdata++) = *(ptbits+2);
1228 *(ptdata++) = *(ptbits+1);
1229 *(ptdata++) = *(ptbits );
1230 ptbits += 3;
1231 }
1232 ptbits += padding;
1233 }
1234
1235 // similarly, set data according to the possible mask bitmap
1236 if( bitmap.GetMask() && bitmap.GetMask()->GetMaskBitmap() )
1237 {
1238 hbitmap = (HBITMAP) bitmap.GetMask()->GetMaskBitmap();
1239 // memory DC created, color set, data copied, and memory DC deleted
1240 HDC memdc = ::CreateCompatibleDC( hdc );
1241 ::SetTextColor( memdc, RGB( 0, 0, 0 ) );
1242 ::SetBkColor( memdc, RGB( 255, 255, 255 ) );
1243 ::GetDIBits( memdc, hbitmap, 0, height, lpBits, lpDIBh, DIB_RGB_COLORS );
1244 ::DeleteDC( memdc );
1245 // background color set to RGB(16,16,16) in consistent with wxGTK
1246 unsigned char r=16, g=16, b=16;
1247 ptdata = data;
1248 ptbits = lpBits;
1249 for( i=0; i<height; i++ )
1250 {
1251 for( j=0; j<width; j++ )
1252 {
1253 if( *ptbits != 0 )
1254 ptdata += 3;
1255 else
1256 {
1257 *(ptdata++) = r;
1258 *(ptdata++) = g;
1259 *(ptdata++) = b;
1260 }
1261 ptbits += 3;
1262 }
1263 ptbits += padding;
1264 }
1265 SetMaskColour( r, g, b );
1266 SetMask( TRUE );
1267 }
1268 else
1269 {
1270 SetMask( FALSE );
1271 }
1272 // free allocated resources
1273 ::ReleaseDC(NULL, hdc);
1274 free(lpDIBh);
1275 free(lpBits);
1276 */
1277 }
1278
1279 #endif
1280
1281 //-----------------------------------------------------------------------------
1282 // GTK conversion routines
1283 //-----------------------------------------------------------------------------
1284
1285 #ifdef __WXGTK__
1286
1287 #include <gtk/gtk.h>
1288 #include <gdk/gdk.h>
1289 #include <gdk/gdkx.h>
1290
1291 #if (GTK_MINOR_VERSION > 0)
1292 #include <gdk/gdkrgb.h>
1293 #endif
1294
1295 wxBitmap wxImage::ConvertToBitmap() const
1296 {
1297 wxBitmap bitmap;
1298
1299 wxCHECK_MSG( Ok(), bitmap, wxT("invalid image") );
1300
1301 int width = GetWidth();
1302 int height = GetHeight();
1303
1304 bitmap.SetHeight( height );
1305 bitmap.SetWidth( width );
1306
1307 bitmap.SetPixmap( gdk_pixmap_new( (GdkWindow*)&gdk_root_parent, width, height, -1 ) );
1308
1309 // Retrieve depth
1310
1311 GdkVisual *visual = gdk_window_get_visual( bitmap.GetPixmap() );
1312 if (visual == NULL) visual = gdk_visual_get_system();
1313 int bpp = visual->depth;
1314
1315 bitmap.SetDepth( bpp );
1316
1317 if ((bpp == 16) && (visual->red_mask != 0xf800)) bpp = 15;
1318 if (bpp < 8) bpp = 8;
1319
1320 #if (GTK_MINOR_VERSION > 0)
1321
1322 if (!HasMask() && (bpp > 8))
1323 {
1324 static bool s_hasInitialized = FALSE;
1325
1326 if (!s_hasInitialized)
1327 {
1328 gdk_rgb_init();
1329 s_hasInitialized = TRUE;
1330 }
1331
1332 GdkGC *gc = gdk_gc_new( bitmap.GetPixmap() );
1333
1334 gdk_draw_rgb_image( bitmap.GetPixmap(),
1335 gc,
1336 0, 0,
1337 width, height,
1338 GDK_RGB_DITHER_NONE,
1339 GetData(),
1340 width*3 );
1341
1342 gdk_gc_unref( gc );
1343
1344 return bitmap;
1345 }
1346
1347 #endif
1348
1349 // Create picture image
1350
1351 GdkImage *data_image =
1352 gdk_image_new( GDK_IMAGE_FASTEST, gdk_visual_get_system(), width, height );
1353
1354 // Create mask image
1355
1356 GdkImage *mask_image = (GdkImage*) NULL;
1357
1358 if (HasMask())
1359 {
1360 unsigned char *mask_data = (unsigned char*)malloc( ((width >> 3)+8) * height );
1361
1362 mask_image = gdk_image_new_bitmap( gdk_visual_get_system(), mask_data, width, height );
1363
1364 wxMask *mask = new wxMask();
1365 mask->m_bitmap = gdk_pixmap_new( (GdkWindow*)&gdk_root_parent, width, height, 1 );
1366
1367 bitmap.SetMask( mask );
1368 }
1369
1370 // Render
1371
1372 enum byte_order { RGB, RBG, BRG, BGR, GRB, GBR };
1373 byte_order b_o = RGB;
1374
1375 if (bpp >= 24)
1376 {
1377 GdkVisual *visual = gdk_visual_get_system();
1378 if ((visual->red_mask > visual->green_mask) && (visual->green_mask > visual->blue_mask)) b_o = RGB;
1379 else if ((visual->red_mask > visual->blue_mask) && (visual->blue_mask > visual->green_mask)) b_o = RGB;
1380 else if ((visual->blue_mask > visual->red_mask) && (visual->red_mask > visual->green_mask)) b_o = BRG;
1381 else if ((visual->blue_mask > visual->green_mask) && (visual->green_mask > visual->red_mask)) b_o = BGR;
1382 else if ((visual->green_mask > visual->red_mask) && (visual->red_mask > visual->blue_mask)) b_o = GRB;
1383 else if ((visual->green_mask > visual->blue_mask) && (visual->blue_mask > visual->red_mask)) b_o = GBR;
1384 }
1385
1386 int r_mask = GetMaskRed();
1387 int g_mask = GetMaskGreen();
1388 int b_mask = GetMaskBlue();
1389
1390 unsigned char* data = GetData();
1391
1392 int index = 0;
1393 for (int y = 0; y < height; y++)
1394 {
1395 for (int x = 0; x < width; x++)
1396 {
1397 int r = data[index];
1398 index++;
1399 int g = data[index];
1400 index++;
1401 int b = data[index];
1402 index++;
1403
1404 if (HasMask())
1405 {
1406 if ((r == r_mask) && (b == b_mask) && (g == g_mask))
1407 gdk_image_put_pixel( mask_image, x, y, 1 );
1408 else
1409 gdk_image_put_pixel( mask_image, x, y, 0 );
1410 }
1411
1412 if (HasMask())
1413 {
1414 if ((r == r_mask) && (b == b_mask) && (g == g_mask))
1415 gdk_image_put_pixel( mask_image, x, y, 1 );
1416 else
1417 gdk_image_put_pixel( mask_image, x, y, 0 );
1418 }
1419
1420 switch (bpp)
1421 {
1422 case 8:
1423 {
1424 int pixel = -1;
1425 if (wxTheApp->m_colorCube)
1426 {
1427 pixel = wxTheApp->m_colorCube[ ((r & 0xf8) << 7) + ((g & 0xf8) << 2) + ((b & 0xf8) >> 3) ];
1428 }
1429 else
1430 {
1431 GdkColormap *cmap = gtk_widget_get_default_colormap();
1432 GdkColor *colors = cmap->colors;
1433 int max = 3 * (65536);
1434
1435 for (int i = 0; i < cmap->size; i++)
1436 {
1437 int rdiff = (r << 8) - colors[i].red;
1438 int gdiff = (g << 8) - colors[i].green;
1439 int bdiff = (b << 8) - colors[i].blue;
1440 int sum = ABS (rdiff) + ABS (gdiff) + ABS (bdiff);
1441 if (sum < max) { pixel = i; max = sum; }
1442 }
1443 }
1444
1445 gdk_image_put_pixel( data_image, x, y, pixel );
1446
1447 break;
1448 }
1449 case 15:
1450 {
1451 guint32 pixel = ((r & 0xf8) << 7) | ((g & 0xf8) << 2) | ((b & 0xf8) >> 3);
1452 gdk_image_put_pixel( data_image, x, y, pixel );
1453 break;
1454 }
1455 case 16:
1456 {
1457 guint32 pixel = ((r & 0xf8) << 8) | ((g & 0xfc) << 3) | ((b & 0xf8) >> 3);
1458 gdk_image_put_pixel( data_image, x, y, pixel );
1459 break;
1460 }
1461 case 32:
1462 case 24:
1463 {
1464 guint32 pixel = 0;
1465 switch (b_o)
1466 {
1467 case RGB: pixel = (r << 16) | (g << 8) | b; break;
1468 case RBG: pixel = (r << 16) | (b << 8) | g; break;
1469 case BRG: pixel = (b << 16) | (r << 8) | g; break;
1470 case BGR: pixel = (b << 16) | (g << 8) | r; break;
1471 case GRB: pixel = (g << 16) | (r << 8) | b; break;
1472 case GBR: pixel = (g << 16) | (b << 8) | r; break;
1473 }
1474 gdk_image_put_pixel( data_image, x, y, pixel );
1475 }
1476 default: break;
1477 }
1478 } // for
1479 } // for
1480
1481 // Blit picture
1482
1483 GdkGC *data_gc = gdk_gc_new( bitmap.GetPixmap() );
1484
1485 gdk_draw_image( bitmap.GetPixmap(), data_gc, data_image, 0, 0, 0, 0, width, height );
1486
1487 gdk_image_destroy( data_image );
1488 gdk_gc_unref( data_gc );
1489
1490 // Blit mask
1491
1492 if (HasMask())
1493 {
1494 GdkGC *mask_gc = gdk_gc_new( bitmap.GetMask()->GetBitmap() );
1495
1496 gdk_draw_image( bitmap.GetMask()->GetBitmap(), mask_gc, mask_image, 0, 0, 0, 0, width, height );
1497
1498 gdk_image_destroy( mask_image );
1499 gdk_gc_unref( mask_gc );
1500 }
1501
1502 return bitmap;
1503 }
1504
1505 wxImage::wxImage( const wxBitmap &bitmap )
1506 {
1507 wxCHECK_RET( bitmap.Ok(), wxT("invalid bitmap") );
1508
1509 GdkImage *gdk_image = (GdkImage*) NULL;
1510 if (bitmap.GetPixmap())
1511 {
1512 gdk_image = gdk_image_get( bitmap.GetPixmap(),
1513 0, 0,
1514 bitmap.GetWidth(), bitmap.GetHeight() );
1515 } else
1516 if (bitmap.GetBitmap())
1517 {
1518 gdk_image = gdk_image_get( bitmap.GetBitmap(),
1519 0, 0,
1520 bitmap.GetWidth(), bitmap.GetHeight() );
1521 } else
1522 {
1523 wxFAIL_MSG( wxT("Ill-formed bitmap") );
1524 }
1525
1526 wxCHECK_RET( gdk_image, wxT("couldn't create image") );
1527
1528 Create( bitmap.GetWidth(), bitmap.GetHeight() );
1529 char unsigned *data = GetData();
1530
1531 if (!data)
1532 {
1533 gdk_image_destroy( gdk_image );
1534 wxFAIL_MSG( wxT("couldn't create image") );
1535 return;
1536 }
1537
1538 GdkImage *gdk_image_mask = (GdkImage*) NULL;
1539 if (bitmap.GetMask())
1540 {
1541 gdk_image_mask = gdk_image_get( bitmap.GetMask()->GetBitmap(),
1542 0, 0,
1543 bitmap.GetWidth(), bitmap.GetHeight() );
1544
1545 SetMaskColour( 16, 16, 16 ); // anything unlikely and dividable
1546 }
1547
1548 GdkVisual *visual = (GdkVisual*) NULL;
1549 if (bitmap.GetPixmap())
1550 visual = gdk_window_get_visual( bitmap.GetPixmap() );
1551 else
1552 visual = gdk_window_get_visual( bitmap.GetBitmap() );
1553
1554 if (visual == NULL) visual = gdk_window_get_visual( (GdkWindow*) &gdk_root_parent );
1555 int bpp = visual->depth;
1556 if ((bpp == 16) && (visual->red_mask != 0xf800)) bpp = 15;
1557
1558 GdkColormap *cmap = gtk_widget_get_default_colormap();
1559
1560 long pos = 0;
1561 for (int j = 0; j < bitmap.GetHeight(); j++)
1562 {
1563 for (int i = 0; i < bitmap.GetWidth(); i++)
1564 {
1565 wxInt32 pixel = gdk_image_get_pixel( gdk_image, i, j );
1566 if (bpp <= 8)
1567 {
1568 data[pos] = cmap->colors[pixel].red >> 8;
1569 data[pos+1] = cmap->colors[pixel].green >> 8;
1570 data[pos+2] = cmap->colors[pixel].blue >> 8;
1571 } else if (bpp == 15)
1572 {
1573 #if (wxBYTE_ORDER == wxBIG_ENDIAN)
1574 // ?
1575 #endif
1576 data[pos] = (pixel >> 7) & 0xf8;
1577 data[pos+1] = (pixel >> 2) & 0xf8;
1578 data[pos+2] = (pixel << 3) & 0xf8;
1579 } else if (bpp == 16)
1580 {
1581 #if (wxBYTE_ORDER == wxBIG_ENDIAN)
1582 // ?
1583 #endif
1584 data[pos] = (pixel >> 8) & 0xf8;
1585 data[pos+1] = (pixel >> 3) & 0xfc;
1586 data[pos+2] = (pixel << 3) & 0xf8;
1587 } else
1588 {
1589 #if (wxBYTE_ORDER == wxBIG_ENDIAN)
1590 data[pos] = (pixel) & 0xff; // Red
1591 data[pos+1] = (pixel >> 8) & 0xff; // Green
1592 data[pos+2] = (pixel >> 16) & 0xff; // Blue
1593 #else
1594 data[pos] = (pixel >> 16) & 0xff;
1595 data[pos+1] = (pixel >> 8) & 0xff;
1596 data[pos+2] = pixel & 0xff;
1597 #endif
1598 }
1599
1600 if (gdk_image_mask)
1601 {
1602 int mask_pixel = gdk_image_get_pixel( gdk_image_mask, i, j );
1603 if (mask_pixel == 0)
1604 {
1605 data[pos] = 16;
1606 data[pos+1] = 16;
1607 data[pos+2] = 16;
1608 }
1609 }
1610
1611 pos += 3;
1612 }
1613 }
1614
1615 gdk_image_destroy( gdk_image );
1616 if (gdk_image_mask) gdk_image_destroy( gdk_image_mask );
1617 }
1618
1619 #endif
1620
1621 //-----------------------------------------------------------------------------
1622 // Motif conversion routines
1623 //-----------------------------------------------------------------------------
1624
1625 #ifdef __WXMOTIF__
1626 #ifdef __VMS__
1627 #pragma message disable nosimpint
1628 #endif
1629 #include <Xm/Xm.h>
1630 #ifdef __VMS__
1631 #pragma message enable nosimpint
1632 #endif
1633 #include "wx/utils.h"
1634 #include <math.h>
1635
1636 wxBitmap wxImage::ConvertToBitmap() const
1637 {
1638 wxBitmap bitmap;
1639
1640 wxCHECK_MSG( Ok(), bitmap, wxT("invalid image") );
1641
1642 int width = GetWidth();
1643 int height = GetHeight();
1644
1645 bitmap.SetHeight( height );
1646 bitmap.SetWidth( width );
1647
1648 Display *dpy = (Display*) wxGetDisplay();
1649 Visual* vis = DefaultVisual( dpy, DefaultScreen( dpy ) );
1650 int bpp = DefaultDepth( dpy, DefaultScreen( dpy ) );
1651
1652 // Create image
1653
1654 XImage *data_image = XCreateImage( dpy, vis, bpp, ZPixmap, 0, 0, width, height, 32, 0 );
1655 data_image->data = (char*) malloc( data_image->bytes_per_line * data_image->height );
1656
1657 bitmap.Create( width, height, bpp );
1658
1659 /*
1660 // Create mask
1661
1662 GdkImage *mask_image = (GdkImage*) NULL;
1663
1664 if (HasMask())
1665 {
1666 unsigned char *mask_data = (unsigned char*)malloc( ((width >> 3)+8) * height );
1667
1668 mask_image = gdk_image_new_bitmap( gdk_visual_get_system(), mask_data, width, height );
1669
1670 wxMask *mask = new wxMask();
1671 mask->m_bitmap = gdk_pixmap_new( (GdkWindow*)&gdk_root_parent, width, height, 1 );
1672
1673 bitmap.SetMask( mask );
1674 }
1675 */
1676
1677 // Retrieve depth info
1678
1679 XVisualInfo vinfo_template;
1680 XVisualInfo *vi;
1681
1682 vinfo_template.visual = vis;
1683 vinfo_template.visualid = XVisualIDFromVisual( vis );
1684 vinfo_template.depth = bpp;
1685 int nitem = 0;
1686
1687 vi = XGetVisualInfo( dpy, VisualIDMask|VisualDepthMask, &vinfo_template, &nitem );
1688
1689 wxCHECK_MSG( vi, wxNullBitmap, wxT("no visual") );
1690
1691 XFree( vi );
1692
1693 if ((bpp == 16) && (vi->red_mask != 0xf800)) bpp = 15;
1694 if (bpp < 8) bpp = 8;
1695
1696 // Render
1697
1698 enum byte_order { RGB, RBG, BRG, BGR, GRB, GBR };
1699 byte_order b_o = RGB;
1700
1701 if (bpp >= 24)
1702 {
1703 if ((vi->red_mask > vi->green_mask) && (vi->green_mask > vi->blue_mask)) b_o = RGB;
1704 else if ((vi->red_mask > vi->blue_mask) && (vi->blue_mask > vi->green_mask)) b_o = RGB;
1705 else if ((vi->blue_mask > vi->red_mask) && (vi->red_mask > vi->green_mask)) b_o = BRG;
1706 else if ((vi->blue_mask > vi->green_mask) && (vi->green_mask > vi->red_mask)) b_o = BGR;
1707 else if ((vi->green_mask > vi->red_mask) && (vi->red_mask > vi->blue_mask)) b_o = GRB;
1708 else if ((vi->green_mask > vi->blue_mask) && (vi->blue_mask > vi->red_mask)) b_o = GBR;
1709 }
1710
1711 /*
1712 int r_mask = GetMaskRed();
1713 int g_mask = GetMaskGreen();
1714 int b_mask = GetMaskBlue();
1715 */
1716
1717 XColor colors[256];
1718 if (bpp == 8)
1719 {
1720 Colormap cmap = (Colormap) wxTheApp->GetMainColormap( dpy );
1721
1722 for (int i = 0; i < 256; i++) colors[i].pixel = i;
1723 XQueryColors( dpy, cmap, colors, 256 );
1724 }
1725
1726 unsigned char* data = GetData();
1727
1728 int index = 0;
1729 for (int y = 0; y < height; y++)
1730 {
1731 for (int x = 0; x < width; x++)
1732 {
1733 int r = data[index];
1734 index++;
1735 int g = data[index];
1736 index++;
1737 int b = data[index];
1738 index++;
1739
1740 /*
1741 if (HasMask())
1742 {
1743 if ((r == r_mask) && (b == b_mask) && (g == g_mask))
1744 gdk_image_put_pixel( mask_image, x, y, 1 );
1745 else
1746 gdk_image_put_pixel( mask_image, x, y, 0 );
1747 }
1748 */
1749
1750 switch (bpp)
1751 {
1752 case 8:
1753 {
1754 int pixel = -1;
1755 /*
1756 if (wxTheApp->m_colorCube)
1757 {
1758 pixel = wxTheApp->m_colorCube
1759 [ ((r & 0xf8) << 7) + ((g & 0xf8) << 2) + ((b & 0xf8) >> 3) ];
1760 }
1761 else
1762 {
1763 */
1764 int max = 3 * (65536);
1765 for (int i = 0; i < 256; i++)
1766 {
1767 int rdiff = (r << 8) - colors[i].red;
1768 int gdiff = (g << 8) - colors[i].green;
1769 int bdiff = (b << 8) - colors[i].blue;
1770 int sum = abs (rdiff) + abs (gdiff) + abs (bdiff);
1771 if (sum < max) { pixel = i; max = sum; }
1772 }
1773 /*
1774 }
1775 */
1776 XPutPixel( data_image, x, y, pixel );
1777 break;
1778 }
1779 case 15:
1780 {
1781 int pixel = ((r & 0xf8) << 7) | ((g & 0xf8) << 2) | ((b & 0xf8) >> 3);
1782 XPutPixel( data_image, x, y, pixel );
1783 break;
1784 }
1785 case 16:
1786 {
1787 int pixel = ((r & 0xf8) << 8) | ((g & 0xfc) << 3) | ((b & 0xf8) >> 3);
1788 XPutPixel( data_image, x, y, pixel );
1789 break;
1790 }
1791 case 32:
1792 case 24:
1793 {
1794 int pixel = 0;
1795 switch (b_o)
1796 {
1797 case RGB: pixel = (r << 16) | (g << 8) | b; break;
1798 case RBG: pixel = (r << 16) | (b << 8) | g; break;
1799 case BRG: pixel = (b << 16) | (r << 8) | g; break;
1800 case BGR: pixel = (b << 16) | (g << 8) | r; break;
1801 case GRB: pixel = (g << 16) | (r << 8) | b; break;
1802 case GBR: pixel = (g << 16) | (b << 8) | r; break;
1803 }
1804 XPutPixel( data_image, x, y, pixel );
1805 }
1806 default: break;
1807 }
1808 } // for
1809 } // for
1810
1811 // Blit picture
1812
1813 XGCValues gcvalues;
1814 gcvalues.foreground = BlackPixel( dpy, DefaultScreen( dpy ) );
1815 GC gc = XCreateGC( dpy, RootWindow ( dpy, DefaultScreen(dpy) ), GCForeground, &gcvalues );
1816 XPutImage( dpy, (Drawable)bitmap.GetPixmap(), gc, data_image, 0, 0, 0, 0, width, height );
1817
1818 XDestroyImage( data_image );
1819 XFreeGC( dpy, gc );
1820
1821 /*
1822 // Blit mask
1823
1824 if (HasMask())
1825 {
1826 GdkGC *mask_gc = gdk_gc_new( bitmap.GetMask()->GetBitmap() );
1827
1828 gdk_draw_image( bitmap.GetMask()->GetBitmap(), mask_gc, mask_image, 0, 0, 0, 0, width, height );
1829
1830 gdk_image_destroy( mask_image );
1831 gdk_gc_unref( mask_gc );
1832 }
1833 */
1834
1835 return bitmap;
1836 }
1837
1838 wxImage::wxImage( const wxBitmap &bitmap )
1839 {
1840 wxCHECK_RET( bitmap.Ok(), wxT("invalid bitmap") );
1841
1842 Display *dpy = (Display*) wxGetDisplay();
1843 Visual* vis = DefaultVisual( dpy, DefaultScreen( dpy ) );
1844 int bpp = DefaultDepth( dpy, DefaultScreen( dpy ) );
1845
1846 XImage *ximage = XGetImage( dpy,
1847 (Drawable)bitmap.GetPixmap(),
1848 0, 0,
1849 bitmap.GetWidth(), bitmap.GetHeight(),
1850 AllPlanes, ZPixmap );
1851
1852 wxCHECK_RET( ximage, wxT("couldn't create image") );
1853
1854 Create( bitmap.GetWidth(), bitmap.GetHeight() );
1855 char unsigned *data = GetData();
1856
1857 if (!data)
1858 {
1859 XDestroyImage( ximage );
1860 wxFAIL_MSG( wxT("couldn't create image") );
1861 return;
1862 }
1863
1864 /*
1865 GdkImage *gdk_image_mask = (GdkImage*) NULL;
1866 if (bitmap.GetMask())
1867 {
1868 gdk_image_mask = gdk_image_get( bitmap.GetMask()->GetBitmap(),
1869 0, 0,
1870 bitmap.GetWidth(), bitmap.GetHeight() );
1871
1872 SetMaskColour( 16, 16, 16 ); // anything unlikely and dividable
1873 }
1874 */
1875
1876 // Retrieve depth info
1877
1878 XVisualInfo vinfo_template;
1879 XVisualInfo *vi;
1880
1881 vinfo_template.visual = vis;
1882 vinfo_template.visualid = XVisualIDFromVisual( vis );
1883 vinfo_template.depth = bpp;
1884 int nitem = 0;
1885
1886 vi = XGetVisualInfo( dpy, VisualIDMask|VisualDepthMask, &vinfo_template, &nitem );
1887
1888 wxCHECK_RET( vi, wxT("no visual") );
1889
1890 if ((bpp == 16) && (vi->red_mask != 0xf800)) bpp = 15;
1891
1892 XFree( vi );
1893
1894 XColor colors[256];
1895 if (bpp == 8)
1896 {
1897 Colormap cmap = (Colormap)wxTheApp->GetMainColormap( dpy );
1898
1899 for (int i = 0; i < 256; i++) colors[i].pixel = i;
1900 XQueryColors( dpy, cmap, colors, 256 );
1901 }
1902
1903 long pos = 0;
1904 for (int j = 0; j < bitmap.GetHeight(); j++)
1905 {
1906 for (int i = 0; i < bitmap.GetWidth(); i++)
1907 {
1908 int pixel = XGetPixel( ximage, i, j );
1909 if (bpp <= 8)
1910 {
1911 data[pos] = colors[pixel].red >> 8;
1912 data[pos+1] = colors[pixel].green >> 8;
1913 data[pos+2] = colors[pixel].blue >> 8;
1914 } else if (bpp == 15)
1915 {
1916 data[pos] = (pixel >> 7) & 0xf8;
1917 data[pos+1] = (pixel >> 2) & 0xf8;
1918 data[pos+2] = (pixel << 3) & 0xf8;
1919 } else if (bpp == 16)
1920 {
1921 data[pos] = (pixel >> 8) & 0xf8;
1922 data[pos+1] = (pixel >> 3) & 0xfc;
1923 data[pos+2] = (pixel << 3) & 0xf8;
1924 } else
1925 {
1926 data[pos] = (pixel >> 16) & 0xff;
1927 data[pos+1] = (pixel >> 8) & 0xff;
1928 data[pos+2] = pixel & 0xff;
1929 }
1930
1931 /*
1932 if (gdk_image_mask)
1933 {
1934 int mask_pixel = gdk_image_get_pixel( gdk_image_mask, i, j );
1935 if (mask_pixel == 0)
1936 {
1937 data[pos] = 16;
1938 data[pos+1] = 16;
1939 data[pos+2] = 16;
1940 }
1941 }
1942 */
1943
1944 pos += 3;
1945 }
1946 }
1947
1948 XDestroyImage( ximage );
1949 /*
1950 if (gdk_image_mask) gdk_image_destroy( gdk_image_mask );
1951 */
1952 }
1953 #endif
1954
1955 #ifdef __WXPM__
1956 // OS/2 Presentation manager conversion routings
1957
1958 wxBitmap wxImage::ConvertToBitmap() const
1959 {
1960 if ( !Ok() )
1961 return wxNullBitmap;
1962 wxBitmap bitmap; // remove
1963 // TODO:
1964 /*
1965 int sizeLimit = 1024*768*3;
1966
1967 // width and height of the device-dependent bitmap
1968 int width = GetWidth();
1969 int bmpHeight = GetHeight();
1970
1971 // calc the number of bytes per scanline and padding
1972 int bytePerLine = width*3;
1973 int sizeDWORD = sizeof( DWORD );
1974 int lineBoundary = bytePerLine % sizeDWORD;
1975 int padding = 0;
1976 if( lineBoundary > 0 )
1977 {
1978 padding = sizeDWORD - lineBoundary;
1979 bytePerLine += padding;
1980 }
1981 // calc the number of DIBs and heights of DIBs
1982 int numDIB = 1;
1983 int hRemain = 0;
1984 int height = sizeLimit/bytePerLine;
1985 if( height >= bmpHeight )
1986 height = bmpHeight;
1987 else
1988 {
1989 numDIB = bmpHeight / height;
1990 hRemain = bmpHeight % height;
1991 if( hRemain >0 ) numDIB++;
1992 }
1993
1994 // set bitmap parameters
1995 wxBitmap bitmap;
1996 wxCHECK_MSG( Ok(), bitmap, wxT("invalid image") );
1997 bitmap.SetWidth( width );
1998 bitmap.SetHeight( bmpHeight );
1999 bitmap.SetDepth( wxDisplayDepth() );
2000
2001 // create a DIB header
2002 int headersize = sizeof(BITMAPINFOHEADER);
2003 LPBITMAPINFO lpDIBh = (BITMAPINFO *) malloc( headersize );
2004 wxCHECK_MSG( lpDIBh, bitmap, wxT("could not allocate memory for DIB header") );
2005 // Fill in the DIB header
2006 lpDIBh->bmiHeader.biSize = headersize;
2007 lpDIBh->bmiHeader.biWidth = (DWORD)width;
2008 lpDIBh->bmiHeader.biHeight = (DWORD)(-height);
2009 lpDIBh->bmiHeader.biSizeImage = bytePerLine*height;
2010 // the general formula for biSizeImage:
2011 // ( ( ( ((DWORD)width*24) +31 ) & ~31 ) >> 3 ) * height;
2012 lpDIBh->bmiHeader.biPlanes = 1;
2013 lpDIBh->bmiHeader.biBitCount = 24;
2014 lpDIBh->bmiHeader.biCompression = BI_RGB;
2015 lpDIBh->bmiHeader.biClrUsed = 0;
2016 // These seem not really needed for our purpose here.
2017 lpDIBh->bmiHeader.biClrImportant = 0;
2018 lpDIBh->bmiHeader.biXPelsPerMeter = 0;
2019 lpDIBh->bmiHeader.biYPelsPerMeter = 0;
2020 // memory for DIB data
2021 unsigned char *lpBits;
2022 lpBits = (unsigned char *)malloc( lpDIBh->bmiHeader.biSizeImage );
2023 if( !lpBits )
2024 {
2025 wxFAIL_MSG( wxT("could not allocate memory for DIB") );
2026 free( lpDIBh );
2027 return bitmap;
2028 }
2029
2030 // create and set the device-dependent bitmap
2031 HDC hdc = ::GetDC(NULL);
2032 HDC memdc = ::CreateCompatibleDC( hdc );
2033 HBITMAP hbitmap;
2034 hbitmap = ::CreateCompatibleBitmap( hdc, width, bmpHeight );
2035 ::SelectObject( memdc, hbitmap);
2036
2037 // copy image data into DIB data and then into DDB (in a loop)
2038 unsigned char *data = GetData();
2039 int i, j, n;
2040 int origin = 0;
2041 unsigned char *ptdata = data;
2042 unsigned char *ptbits;
2043
2044 for( n=0; n<numDIB; n++ )
2045 {
2046 if( numDIB > 1 && n == numDIB-1 && hRemain > 0 )
2047 {
2048 // redefine height and size of the (possibly) last smaller DIB
2049 // memory is not reallocated
2050 height = hRemain;
2051 lpDIBh->bmiHeader.biHeight = (DWORD)(-height);
2052 lpDIBh->bmiHeader.biSizeImage = bytePerLine*height;
2053 }
2054 ptbits = lpBits;
2055
2056 for( j=0; j<height; j++ )
2057 {
2058 for( i=0; i<width; i++ )
2059 {
2060 *(ptbits++) = *(ptdata+2);
2061 *(ptbits++) = *(ptdata+1);
2062 *(ptbits++) = *(ptdata );
2063 ptdata += 3;
2064 }
2065 for( i=0; i< padding; i++ ) *(ptbits++) = 0;
2066 }
2067 ::StretchDIBits( memdc, 0, origin, width, height,\
2068 0, 0, width, height, lpBits, lpDIBh, DIB_RGB_COLORS, SRCCOPY);
2069 origin += height;
2070 // if numDIB = 1, lines below can also be used
2071 // hbitmap = CreateDIBitmap( hdc, &(lpDIBh->bmiHeader), CBM_INIT, lpBits, lpDIBh, DIB_RGB_COLORS );
2072 // The above line is equivalent to the following two lines.
2073 // hbitmap = ::CreateCompatibleBitmap( hdc, width, height );
2074 // ::SetDIBits( hdc, hbitmap, 0, height, lpBits, lpDIBh, DIB_RGB_COLORS);
2075 // or the following lines
2076 // hbitmap = ::CreateCompatibleBitmap( hdc, width, height );
2077 // HDC memdc = ::CreateCompatibleDC( hdc );
2078 // ::SelectObject( memdc, hbitmap);
2079 // ::SetDIBitsToDevice( memdc, 0, 0, width, height,
2080 // 0, 0, 0, height, (void *)lpBits, lpDIBh, DIB_RGB_COLORS);
2081 // ::SelectObject( memdc, 0 );
2082 // ::DeleteDC( memdc );
2083 }
2084 bitmap.SetHBITMAP( (WXHBITMAP) hbitmap );
2085
2086 // similarly, created an mono-bitmap for the possible mask
2087 if( HasMask() )
2088 {
2089 hbitmap = ::CreateBitmap( (WORD)width, (WORD)bmpHeight, 1, 1, NULL );
2090 ::SelectObject( memdc, hbitmap);
2091 if( numDIB == 1 ) height = bmpHeight;
2092 else height = sizeLimit/bytePerLine;
2093 lpDIBh->bmiHeader.biHeight = (DWORD)(-height);
2094 lpDIBh->bmiHeader.biSizeImage = bytePerLine*height;
2095 origin = 0;
2096 unsigned char r = GetMaskRed();
2097 unsigned char g = GetMaskGreen();
2098 unsigned char b = GetMaskBlue();
2099 unsigned char zero = 0, one = 255;
2100 ptdata = data;
2101 for( n=0; n<numDIB; n++ )
2102 {
2103 if( numDIB > 1 && n == numDIB - 1 && hRemain > 0 )
2104 {
2105 // redefine height and size of the (possibly) last smaller DIB
2106 // memory is not reallocated
2107 height = hRemain;
2108 lpDIBh->bmiHeader.biHeight = (DWORD)(-height);
2109 lpDIBh->bmiHeader.biSizeImage = bytePerLine*height;
2110 }
2111 ptbits = lpBits;
2112 for( int j=0; j<height; j++ )
2113 {
2114 for(i=0; i<width; i++ )
2115 {
2116 if( (*(ptdata++)!=r) | (*(ptdata++)!=g) | (*(ptdata++)!=b) )
2117 {
2118 *(ptbits++) = one;
2119 *(ptbits++) = one;
2120 *(ptbits++) = one;
2121 }
2122 else
2123 {
2124 *(ptbits++) = zero;
2125 *(ptbits++) = zero;
2126 *(ptbits++) = zero;
2127 }
2128 }
2129 for( i=0; i< padding; i++ ) *(ptbits++) = zero;
2130 }
2131 ::StretchDIBits( memdc, 0, origin, width, height,\
2132 0, 0, width, height, lpBits, lpDIBh, DIB_RGB_COLORS, SRCCOPY);
2133 origin += height;
2134 }
2135 // create a wxMask object
2136 wxMask *mask = new wxMask();
2137 mask->SetMaskBitmap( (WXHBITMAP) hbitmap );
2138 bitmap.SetMask( mask );
2139 }
2140
2141 // free allocated resources
2142 ::SelectObject( memdc, 0 );
2143 ::DeleteDC( memdc );
2144 ::ReleaseDC(NULL, hdc);
2145 free(lpDIBh);
2146 free(lpBits);
2147
2148 // check the wxBitmap object
2149 if( bitmap.GetHBITMAP() )
2150 bitmap.SetOk( TRUE );
2151 else
2152 bitmap.SetOk( FALSE );
2153 */
2154 return bitmap;
2155 }
2156
2157 wxImage::wxImage( const wxBitmap &bitmap )
2158 {
2159 // check the bitmap
2160 if( !bitmap.Ok() )
2161 {
2162 wxFAIL_MSG( wxT("invalid bitmap") );
2163 return;
2164 }
2165
2166 // create an wxImage object
2167 int width = bitmap.GetWidth();
2168 int height = bitmap.GetHeight();
2169 Create( width, height );
2170 unsigned char *data = GetData();
2171 if( !data )
2172 {
2173 wxFAIL_MSG( wxT("could not allocate data for image") );
2174 return;
2175 }
2176
2177 // calc the number of bytes per scanline and padding in the DIB
2178 int bytePerLine = width*3;
2179 int sizeDWORD = sizeof( DWORD );
2180 int lineBoundary = bytePerLine % sizeDWORD;
2181 int padding = 0;
2182 if( lineBoundary > 0 )
2183 {
2184 padding = sizeDWORD - lineBoundary;
2185 bytePerLine += padding;
2186 }
2187 // TODO:
2188 /*
2189 // create a DIB header
2190 int headersize = sizeof(BITMAPINFOHEADER);
2191 LPBITMAPINFO lpDIBh = (BITMAPINFO *) malloc( headersize );
2192 if( !lpDIBh )
2193 {
2194 wxFAIL_MSG( wxT("could not allocate data for DIB header") );
2195 free( data );
2196 return;
2197 }
2198 // Fill in the DIB header
2199 lpDIBh->bmiHeader.biSize = headersize;
2200 lpDIBh->bmiHeader.biWidth = width;
2201 lpDIBh->bmiHeader.biHeight = -height;
2202 lpDIBh->bmiHeader.biSizeImage = bytePerLine * height;
2203 lpDIBh->bmiHeader.biPlanes = 1;
2204 lpDIBh->bmiHeader.biBitCount = 24;
2205 lpDIBh->bmiHeader.biCompression = BI_RGB;
2206 lpDIBh->bmiHeader.biClrUsed = 0;
2207 // These seem not really needed for our purpose here.
2208 lpDIBh->bmiHeader.biClrImportant = 0;
2209 lpDIBh->bmiHeader.biXPelsPerMeter = 0;
2210 lpDIBh->bmiHeader.biYPelsPerMeter = 0;
2211 // memory for DIB data
2212 unsigned char *lpBits;
2213 lpBits = (unsigned char *) malloc( lpDIBh->bmiHeader.biSizeImage );
2214 if( !lpBits )
2215 {
2216 wxFAIL_MSG( wxT("could not allocate data for DIB") );
2217 free( data );
2218 free( lpDIBh );
2219 return;
2220 }
2221
2222 // copy data from the device-dependent bitmap to the DIB
2223 HDC hdc = ::GetDC(NULL);
2224 HBITMAP hbitmap;
2225 hbitmap = (HBITMAP) bitmap.GetHBITMAP();
2226 ::GetDIBits( hdc, hbitmap, 0, height, lpBits, lpDIBh, DIB_RGB_COLORS );
2227
2228 // copy DIB data into the wxImage object
2229 int i, j;
2230 unsigned char *ptdata = data;
2231 unsigned char *ptbits = lpBits;
2232 for( i=0; i<height; i++ )
2233 {
2234 for( j=0; j<width; j++ )
2235 {
2236 *(ptdata++) = *(ptbits+2);
2237 *(ptdata++) = *(ptbits+1);
2238 *(ptdata++) = *(ptbits );
2239 ptbits += 3;
2240 }
2241 ptbits += padding;
2242 }
2243
2244 // similarly, set data according to the possible mask bitmap
2245 if( bitmap.GetMask() && bitmap.GetMask()->GetMaskBitmap() )
2246 {
2247 hbitmap = (HBITMAP) bitmap.GetMask()->GetMaskBitmap();
2248 // memory DC created, color set, data copied, and memory DC deleted
2249 HDC memdc = ::CreateCompatibleDC( hdc );
2250 ::SetTextColor( memdc, RGB( 0, 0, 0 ) );
2251 ::SetBkColor( memdc, RGB( 255, 255, 255 ) );
2252 ::GetDIBits( memdc, hbitmap, 0, height, lpBits, lpDIBh, DIB_RGB_COLORS );
2253 ::DeleteDC( memdc );
2254 // background color set to RGB(16,16,16) in consistent with wxGTK
2255 unsigned char r=16, g=16, b=16;
2256 ptdata = data;
2257 ptbits = lpBits;
2258 for( i=0; i<height; i++ )
2259 {
2260 for( j=0; j<width; j++ )
2261 {
2262 if( *ptbits != 0 )
2263 ptdata += 3;
2264 else
2265 {
2266 *(ptdata++) = r;
2267 *(ptdata++) = g;
2268 *(ptdata++) = b;
2269 }
2270 ptbits += 3;
2271 }
2272 ptbits += padding;
2273 }
2274 SetMaskColour( r, g, b );
2275 SetMask( TRUE );
2276 }
2277 else
2278 {
2279 SetMask( FALSE );
2280 }
2281 // free allocated resources
2282 ::ReleaseDC(NULL, hdc);
2283 free(lpDIBh);
2284 free(lpBits);
2285 */
2286 }
2287
2288 #endif
2289
2290 // A module to allow wxImage initialization/cleanup
2291 // without calling these functions from app.cpp or from
2292 // the user's application.
2293
2294 class wxImageModule: public wxModule
2295 {
2296 DECLARE_DYNAMIC_CLASS(wxImageModule)
2297 public:
2298 wxImageModule() {}
2299 bool OnInit() { wxImage::InitStandardHandlers(); return TRUE; };
2300 void OnExit() { wxImage::CleanUpHandlers(); };
2301 };
2302
2303 IMPLEMENT_DYNAMIC_CLASS(wxImageModule, wxModule)