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