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