]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/bitmap.cpp
Added OLE automation sample, changed BC++ makefiles
[wxWidgets.git] / src / gtk1 / bitmap.cpp
CommitLineData
c801d85f
KB
1/////////////////////////////////////////////////////////////////////////////
2// Name: bitmap.cpp
3// Purpose:
4// Author: Robert Roebling
6f65e337 5// RCS-ID: $Id$
01111366 6// Copyright: (c) 1998 Robert Roebling
c801d85f
KB
7// Licence: wxWindows licence
8/////////////////////////////////////////////////////////////////////////////
9
10#ifdef __GNUG__
11#pragma implementation "bitmap.h"
12#endif
13
14#include "wx/bitmap.h"
52cbfcf0 15#include "wx/icon.h"
fd0eed64 16#include "wx/filefn.h"
c801d85f 17#include "gdk/gdkprivate.h"
01111366 18#include "gdk/gdkx.h"
4bc67cc5 19#include "wx/image.h"
c801d85f
KB
20
21//-----------------------------------------------------------------------------
22// wxMask
23//-----------------------------------------------------------------------------
24
25IMPLEMENT_DYNAMIC_CLASS(wxMask,wxObject)
26
27wxMask::wxMask(void)
28{
fd0eed64 29 m_bitmap = (GdkBitmap *) NULL;
ff7b1510 30}
c801d85f
KB
31
32wxMask::wxMask( const wxBitmap& WXUNUSED(bitmap), const wxColour& WXUNUSED(colour) )
33{
ff7b1510 34}
c801d85f 35
debe6624 36wxMask::wxMask( const wxBitmap& WXUNUSED(bitmap), int WXUNUSED(paletteIndex) )
c801d85f 37{
ff7b1510 38}
c801d85f
KB
39
40wxMask::wxMask( const wxBitmap& WXUNUSED(bitmap) )
41{
ff7b1510 42}
c801d85f
KB
43
44wxMask::~wxMask(void)
45{
fd0eed64 46 if (m_bitmap) gdk_bitmap_unref( m_bitmap );
ff7b1510 47}
c801d85f
KB
48
49GdkBitmap *wxMask::GetBitmap(void) const
50{
fd0eed64 51 return m_bitmap;
ff7b1510 52}
c801d85f
KB
53
54//-----------------------------------------------------------------------------
55// wxBitmap
56//-----------------------------------------------------------------------------
57
58class wxBitmapRefData: public wxObjectRefData
59{
fd0eed64
RR
60public:
61 wxBitmapRefData(void);
62 ~wxBitmapRefData(void);
63
64 GdkPixmap *m_pixmap;
65 GdkBitmap *m_bitmap;
66 wxMask *m_mask;
67 int m_width;
68 int m_height;
69 int m_bpp;
70 wxPalette *m_palette;
c801d85f
KB
71};
72
73wxBitmapRefData::wxBitmapRefData(void)
74{
fd0eed64
RR
75 m_pixmap = (GdkPixmap *) NULL;
76 m_bitmap = (GdkBitmap *) NULL;
77 m_mask = (wxMask *) NULL;
78 m_width = 0;
79 m_height = 0;
80 m_bpp = 0;
81 m_palette = (wxPalette *) NULL;
ff7b1510 82}
c801d85f
KB
83
84wxBitmapRefData::~wxBitmapRefData(void)
85{
fd0eed64
RR
86 if (m_pixmap) gdk_pixmap_unref( m_pixmap );
87 if (m_bitmap) gdk_bitmap_unref( m_bitmap );
88 if (m_mask) delete m_mask;
89 if (m_palette) delete m_palette;
ff7b1510 90}
c801d85f
KB
91
92//-----------------------------------------------------------------------------
93
94#define M_BMPDATA ((wxBitmapRefData *)m_refData)
95
96IMPLEMENT_DYNAMIC_CLASS(wxBitmap,wxGDIObject)
97
98wxBitmap::wxBitmap(void)
99{
fd0eed64 100 if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this);
ff7b1510 101}
c801d85f 102
debe6624 103wxBitmap::wxBitmap( int width, int height, int depth )
c801d85f 104{
fd0eed64
RR
105 wxCHECK_RET( (width > 0) && (height > 0), "invalid bitmap size" )
106 wxCHECK_RET( (depth > 0) || (depth == -1), "invalid bitmap depth" )
fee04295 107
fd0eed64 108 m_refData = new wxBitmapRefData();
fee04295 109
fd0eed64 110 GdkWindow *parent = (GdkWindow*) &gdk_root_parent;
fee04295 111
fd0eed64
RR
112 M_BMPDATA->m_mask = (wxMask *) NULL;
113 M_BMPDATA->m_pixmap = gdk_pixmap_new( parent, width, height, depth );
114 M_BMPDATA->m_width = width;
115 M_BMPDATA->m_height = height;
116 M_BMPDATA->m_bpp = gdk_window_get_visual( parent )->depth;
c801d85f 117
fd0eed64 118 if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this);
ff7b1510 119}
c801d85f 120
e52f60e6
RR
121wxBitmap::wxBitmap( const char **bits )
122{
123 wxCHECK_RET( bits != NULL, "invalid bitmap data" )
124
125 m_refData = new wxBitmapRefData();
126
127 GdkBitmap *mask = (GdkBitmap*) NULL;
128 GdkWindow *parent = (GdkWindow*) &gdk_root_parent;
129
130 M_BMPDATA->m_pixmap = gdk_pixmap_create_from_xpm_d( parent, &mask, NULL, (gchar **) bits );
131
132 if (mask)
133 {
134 M_BMPDATA->m_mask = new wxMask();
135 M_BMPDATA->m_mask->m_bitmap = mask;
136 }
137
138 gdk_window_get_size( M_BMPDATA->m_pixmap, &(M_BMPDATA->m_width), &(M_BMPDATA->m_height) );
139
140 M_BMPDATA->m_bpp = gdk_window_get_visual( parent )->depth; // ?
141 if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this);
142}
143
c801d85f
KB
144wxBitmap::wxBitmap( char **bits )
145{
fd0eed64 146 wxCHECK_RET( bits != NULL, "invalid bitmap data" )
fee04295 147
fd0eed64 148 m_refData = new wxBitmapRefData();
c801d85f 149
fd0eed64
RR
150 GdkBitmap *mask = (GdkBitmap*) NULL;
151 GdkWindow *parent = (GdkWindow*) &gdk_root_parent;
219f895a 152
fd0eed64 153 M_BMPDATA->m_pixmap = gdk_pixmap_create_from_xpm_d( parent, &mask, NULL, (gchar **) bits );
219f895a 154
4bc67cc5
RR
155 wxCHECK_RET( M_BMPDATA->m_pixmap, "couldn't create pixmap" );
156
fd0eed64
RR
157 if (mask)
158 {
159 M_BMPDATA->m_mask = new wxMask();
160 M_BMPDATA->m_mask->m_bitmap = mask;
161 }
219f895a 162
fd0eed64 163 gdk_window_get_size( M_BMPDATA->m_pixmap, &(M_BMPDATA->m_width), &(M_BMPDATA->m_height) );
0180d5da 164
fd0eed64 165 M_BMPDATA->m_bpp = gdk_window_get_visual( parent )->depth; // ?
fd0eed64 166 if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this);
ff7b1510 167}
c801d85f
KB
168
169wxBitmap::wxBitmap( const wxBitmap& bmp )
170{
fd0eed64 171 Ref( bmp );
c801d85f 172
fd0eed64 173 if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this);
ff7b1510 174}
c801d85f
KB
175
176wxBitmap::wxBitmap( const wxBitmap* bmp )
177{
fd0eed64 178 if (bmp) Ref( *bmp );
c801d85f 179
fd0eed64 180 if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this);
ff7b1510 181}
6f65e337 182
debe6624 183wxBitmap::wxBitmap( const wxString &filename, int type )
c801d85f 184{
fd0eed64 185 LoadFile( filename, type );
ff7b1510 186
fd0eed64 187 if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this);
ff7b1510 188}
c801d85f 189
debe6624 190wxBitmap::wxBitmap( const char bits[], int width, int height, int WXUNUSED(depth))
6f65e337 191{
fd0eed64 192 m_refData = new wxBitmapRefData();
6f65e337 193
fd0eed64
RR
194 M_BMPDATA->m_mask = (wxMask *) NULL;
195 M_BMPDATA->m_bitmap =
196 gdk_bitmap_create_from_data( (GdkWindow*) &gdk_root_parent, (gchar *) bits, width, height );
197 M_BMPDATA->m_width = width;
198 M_BMPDATA->m_height = height;
199 M_BMPDATA->m_bpp = 1;
6f65e337 200
4bc67cc5
RR
201 wxCHECK_RET( M_BMPDATA->m_bitmap, "couldn't create bitmap" );
202
fd0eed64 203 if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this);
6f65e337
JS
204}
205
c801d85f
KB
206wxBitmap::~wxBitmap(void)
207{
fd0eed64 208 if (wxTheBitmapList) wxTheBitmapList->DeleteObject(this);
ff7b1510 209}
c801d85f
KB
210
211wxBitmap& wxBitmap::operator = ( const wxBitmap& bmp )
212{
fd0eed64
RR
213 if (*this == bmp) return (*this);
214 Ref( bmp );
215 return *this;
ff7b1510 216}
c801d85f
KB
217
218bool wxBitmap::operator == ( const wxBitmap& bmp )
219{
fd0eed64 220 return m_refData == bmp.m_refData;
ff7b1510 221}
c801d85f
KB
222
223bool wxBitmap::operator != ( const wxBitmap& bmp )
224{
fd0eed64 225 return m_refData != bmp.m_refData;
ff7b1510 226}
c801d85f
KB
227
228bool wxBitmap::Ok(void) const
229{
fd0eed64 230 return (m_refData != NULL);
ff7b1510 231}
c801d85f
KB
232
233int wxBitmap::GetHeight(void) const
234{
33a5bc52 235 wxCHECK_MSG( Ok(), -1, "invalid bitmap" );
e55ad60e 236
fd0eed64 237 return M_BMPDATA->m_height;
ff7b1510 238}
c801d85f
KB
239
240int wxBitmap::GetWidth(void) const
241{
33a5bc52 242 wxCHECK_MSG( Ok(), -1, "invalid bitmap" );
e55ad60e 243
fd0eed64 244 return M_BMPDATA->m_width;
ff7b1510 245}
c801d85f
KB
246
247int wxBitmap::GetDepth(void) const
248{
33a5bc52 249 wxCHECK_MSG( Ok(), -1, "invalid bitmap" );
e55ad60e 250
fd0eed64 251 return M_BMPDATA->m_bpp;
ff7b1510 252}
c801d85f 253
c801d85f
KB
254wxMask *wxBitmap::GetMask(void) const
255{
33a5bc52 256 wxCHECK_MSG( Ok(), (wxMask *) NULL, "invalid bitmap" );
219f895a 257
fd0eed64 258 return M_BMPDATA->m_mask;
ff7b1510 259}
c801d85f
KB
260
261void wxBitmap::SetMask( wxMask *mask )
262{
33a5bc52 263 wxCHECK_RET( Ok(), "invalid bitmap" );
219f895a 264
fd0eed64 265 if (M_BMPDATA->m_mask) delete M_BMPDATA->m_mask;
cf7a7e13 266
fd0eed64 267 M_BMPDATA->m_mask = mask;
ff7b1510 268}
c801d85f 269
fd0eed64 270bool wxBitmap::SaveFile( const wxString &name, int type, wxPalette *WXUNUSED(palette) )
c801d85f 271{
33a5bc52 272 wxCHECK_MSG( Ok(), FALSE, "invalid bitmap" );
e55ad60e 273
fd0eed64
RR
274 if (type == wxBITMAP_TYPE_PNG)
275 {
4bc67cc5 276 wxImage image( *this );
fd0eed64
RR
277 if (image.Ok()) return image.SaveFile( name, type );
278 }
279
280 return FALSE;
ff7b1510 281}
c801d85f 282
fd0eed64 283bool wxBitmap::LoadFile( const wxString &name, int type )
c801d85f 284{
fd0eed64
RR
285 UnRef();
286
287 if (!wxFileExists(name)) return FALSE;
288
289 if (type == wxBITMAP_TYPE_XPM)
290 {
291 m_refData = new wxBitmapRefData();
292
293 GdkBitmap *mask = (GdkBitmap*) NULL;
294 GdkWindow *parent = (GdkWindow*) &gdk_root_parent;
cf7a7e13 295
fd0eed64
RR
296 M_BMPDATA->m_pixmap = gdk_pixmap_create_from_xpm( parent, &mask, NULL, name );
297
298 if (mask)
299 {
300 M_BMPDATA->m_mask = new wxMask();
301 M_BMPDATA->m_mask->m_bitmap = mask;
302 }
303
304 gdk_window_get_size( M_BMPDATA->m_pixmap, &(M_BMPDATA->m_width), &(M_BMPDATA->m_height) );
305 M_BMPDATA->m_bpp = gdk_window_get_visual( parent )->depth;
306 }
307 else if (type == wxBITMAP_TYPE_PNG)
308 {
309 wxImage image;
310 image.LoadFile( name, type );
4bc67cc5 311 if (image.Ok()) *this = image.ConvertToBitmap();
fd0eed64
RR
312 }
313 else if (type == wxBITMAP_TYPE_BMP)
314 {
315 wxImage image;
316 image.LoadFile( name, type );
4bc67cc5 317 if (image.Ok()) *this = image.ConvertToBitmap();
fd0eed64
RR
318 }
319 else
320 return FALSE;
321
322 return TRUE;
ff7b1510 323}
c801d85f
KB
324
325wxPalette *wxBitmap::GetPalette(void) const
326{
fd0eed64 327 if (!Ok()) return (wxPalette *) NULL;
33a5bc52 328
fd0eed64 329 return M_BMPDATA->m_palette;
ff7b1510 330}
c801d85f 331
4bc67cc5
RR
332void wxBitmap::SetHeight( int height )
333{
334 if (!m_refData) m_refData = new wxBitmapRefData();
335
336 M_BMPDATA->m_height = height;
337}
338
339void wxBitmap::SetWidth( int width )
340{
341 if (!m_refData) m_refData = new wxBitmapRefData();
342
343 M_BMPDATA->m_width = width;
344}
345
346void wxBitmap::SetDepth( int depth )
347{
348 if (!m_refData) m_refData = new wxBitmapRefData();
349
350 M_BMPDATA->m_bpp = depth;
351}
352
353void wxBitmap::SetPixmap( GdkPixmap *pixmap )
354{
355 if (!m_refData) m_refData = new wxBitmapRefData();
356
357 M_BMPDATA->m_pixmap = pixmap;
358}
359
c801d85f
KB
360GdkPixmap *wxBitmap::GetPixmap(void) const
361{
33a5bc52 362 wxCHECK_MSG( Ok(), (GdkPixmap *) NULL, "invalid bitmap" );
cf7a7e13 363
fd0eed64 364 return M_BMPDATA->m_pixmap;
ff7b1510 365}
c801d85f 366
6f65e337
JS
367GdkBitmap *wxBitmap::GetBitmap(void) const
368{
33a5bc52 369 wxCHECK_MSG( Ok(), (GdkBitmap *) NULL, "invalid bitmap" );
219f895a 370
fd0eed64 371 return M_BMPDATA->m_bitmap;
ff7b1510 372}
6f65e337 373
4bc67cc5
RR
374//-----------------------------------------------------------------------------
375// wxImage
376//-----------------------------------------------------------------------------
377
378wxBitmap wxImage::ConvertToBitmap() const
219f895a 379{
4bc67cc5 380 wxBitmap bitmap;
219f895a 381
4bc67cc5 382 wxCHECK_MSG( Ok(), bitmap, "invalid image" );
cf7a7e13 383
4bc67cc5
RR
384 int width = GetWidth();
385 int height = GetHeight();
01111366 386
4bc67cc5
RR
387 bitmap.SetHeight( height );
388 bitmap.SetWidth( width );
389
fd0eed64 390 // Create picture
cf7a7e13 391
fd0eed64
RR
392 GdkImage *data_image =
393 gdk_image_new( GDK_IMAGE_FASTEST, gdk_visual_get_system(), width, height );
cf7a7e13 394
4bc67cc5 395 bitmap.SetPixmap( gdk_pixmap_new( (GdkWindow*)&gdk_root_parent, width, height, -1 ) );
01111366 396
fd0eed64 397 // Create mask
cf7a7e13 398
fd0eed64 399 GdkImage *mask_image = (GdkImage*) NULL;
cf7a7e13 400
4bc67cc5 401 if (HasMask())
fd0eed64
RR
402 {
403 unsigned char *mask_data = (unsigned char*)malloc( ((width >> 3)+8) * height );
01111366 404
fd0eed64 405 mask_image = gdk_image_new_bitmap( gdk_visual_get_system(), mask_data, width, height );
01111366 406
4bc67cc5
RR
407 wxMask *mask = new wxMask();
408 mask->m_bitmap = gdk_pixmap_new( (GdkWindow*)&gdk_root_parent, width, height, 1 );
409
410 bitmap.SetMask( mask );
fd0eed64 411 }
cf7a7e13 412
fd0eed64 413 // Retrieve depth
01111366 414
4bc67cc5 415 GdkVisual *visual = gdk_window_get_visual( bitmap.GetPixmap() );
e8fdc264
RR
416 if (visual == NULL) visual = gdk_window_get_visual( (GdkWindow*) &gdk_root_parent );
417 int bpp = visual->depth;
fb1585ae 418 if ((bpp == 16) && (visual->red_mask != 0xf800)) bpp = 15;
e8fdc264
RR
419 if (bpp < 8) bpp = 8;
420
fd0eed64 421 // Render
fee04295 422
fd0eed64
RR
423 enum byte_order { RGB, RBG, BRG, BGR, GRB, GBR };
424 byte_order b_o = RGB;
fee04295 425
e8fdc264 426 if (bpp >= 24)
fd0eed64
RR
427 {
428 GdkVisual *visual = gdk_visual_get_system();
429 if ((visual->red_mask > visual->green_mask) && (visual->green_mask > visual->blue_mask)) b_o = RGB;
430 else if ((visual->red_mask > visual->blue_mask) && (visual->blue_mask > visual->green_mask)) b_o = RGB;
431 else if ((visual->blue_mask > visual->red_mask) && (visual->red_mask > visual->green_mask)) b_o = BRG;
432 else if ((visual->blue_mask > visual->green_mask) && (visual->green_mask > visual->red_mask)) b_o = BGR;
433 else if ((visual->green_mask > visual->red_mask) && (visual->red_mask > visual->blue_mask)) b_o = GRB;
434 else if ((visual->green_mask > visual->blue_mask) && (visual->blue_mask > visual->red_mask)) b_o = GBR;
435 }
01111366 436
4bc67cc5
RR
437 int r_mask = GetMaskRed();
438 int g_mask = GetMaskGreen();
439 int b_mask = GetMaskBlue();
01111366 440
4bc67cc5 441 unsigned char* data = GetData();
01111366 442
fd0eed64
RR
443 int index = 0;
444 for (int y = 0; y < height; y++)
445 {
446 for (int x = 0; x < width; x++)
447 {
448 int r = data[index];
449 index++;
450 int g = data[index];
451 index++;
452 int b = data[index];
453 index++;
01111366 454
4bc67cc5 455 if (HasMask())
fd0eed64 456 {
df875e59 457 if ((r == r_mask) && (b == b_mask) && (g == g_mask))
fd0eed64 458 gdk_image_put_pixel( mask_image, x, y, 1 );
df875e59
RR
459 else
460 gdk_image_put_pixel( mask_image, x, y, 0 );
fd0eed64 461 }
01111366 462
e8fdc264 463 switch (bpp)
fd0eed64
RR
464 {
465 case 8:
466 {
467 GdkColormap *cmap = gtk_widget_get_default_colormap();
468 GdkColor *colors = cmap->colors;
469 int max = 3 * (65536);
470 int index = -1;
471
472 for (int i = 0; i < cmap->size; i++)
473 {
474 int rdiff = (r << 8) - colors[i].red;
475 int gdiff = (g << 8) - colors[i].green;
476 int bdiff = (b << 8) - colors[i].blue;
477 int sum = ABS (rdiff) + ABS (gdiff) + ABS (bdiff);
478 if (sum < max) { index = i; max = sum; }
479 }
01111366 480
fd0eed64 481 gdk_image_put_pixel( data_image, x, y, index );
01111366 482
fd0eed64
RR
483 break;
484 }
485 case 15:
486 {
487 guint32 pixel = ((r & 0xf8) << 7) | ((g & 0xf8) << 2) | ((b & 0xf8) >> 3);
488 gdk_image_put_pixel( data_image, x, y, pixel );
489 break;
490 }
491 case 16:
492 {
493 guint32 pixel = ((r & 0xf8) << 8) | ((g & 0xfc) << 3) | ((b & 0xf8) >> 3);
494 gdk_image_put_pixel( data_image, x, y, pixel );
495 break;
496 }
497 case 32:
498 case 24:
499 {
500 guint32 pixel = 0;
501 switch (b_o)
502 {
503 case RGB: pixel = (r << 16) | (g << 8) | b; break;
504 case RBG: pixel = (r << 16) | (b << 8) | g; break;
505 case BRG: pixel = (b << 16) | (r << 8) | g; break;
506 case BGR: pixel = (b << 16) | (g << 8) | r; break;
507 case GRB: pixel = (g << 16) | (r << 8) | b; break;
508 case GBR: pixel = (g << 16) | (b << 8) | r; break;
509 }
510 gdk_image_put_pixel( data_image, x, y, pixel );
511 }
512 default: break;
fee04295 513 }
fd0eed64
RR
514 } // for
515 } // for
516
517 // Blit picture
cf7a7e13 518
4bc67cc5 519 GdkGC *data_gc = gdk_gc_new( bitmap.GetPixmap() );
cf7a7e13 520
4bc67cc5 521 gdk_draw_image( bitmap.GetPixmap(), data_gc, data_image, 0, 0, 0, 0, width, height );
cf7a7e13 522
fd0eed64
RR
523 gdk_image_destroy( data_image );
524 gdk_gc_unref( data_gc );
cf7a7e13 525
fd0eed64 526 // Blit mask
cf7a7e13 527
4bc67cc5 528 if (HasMask())
fd0eed64 529 {
4bc67cc5 530 GdkGC *mask_gc = gdk_gc_new( bitmap.GetMask()->GetBitmap() );
cf7a7e13 531
4bc67cc5 532 gdk_draw_image( bitmap.GetMask()->GetBitmap(), mask_gc, mask_image, 0, 0, 0, 0, width, height );
cf7a7e13 533
fd0eed64
RR
534 gdk_image_destroy( mask_image );
535 gdk_gc_unref( mask_gc );
536 }
4bc67cc5
RR
537
538 return bitmap;
ff7b1510 539}
219f895a 540
4bc67cc5 541wxImage::wxImage( const wxBitmap &bitmap )
219f895a 542{
4bc67cc5 543 wxCHECK_RET( bitmap.Ok(), "invalid bitmap" );
219f895a 544
4bc67cc5
RR
545 GdkImage *gdk_image = gdk_image_get( bitmap.GetPixmap(),
546 0, 0,
547 bitmap.GetWidth(), bitmap.GetHeight() );
548
549 wxCHECK_RET( gdk_image, "couldn't create image" );
cf7a7e13 550
4bc67cc5
RR
551 Create( bitmap.GetWidth(), bitmap.GetHeight() );
552 char unsigned *data = GetData();
553
554 if (!data)
555 {
556 gdk_image_destroy( gdk_image );
557 wxFAIL_MSG( "couldn't create image" );
558 return;
559 }
560
561 GdkImage *gdk_image_mask = (GdkImage*) NULL;
562 if (bitmap.GetMask())
563 {
564 gdk_image_mask = gdk_image_get( bitmap.GetMask()->GetBitmap(),
565 0, 0,
566 bitmap.GetWidth(), bitmap.GetHeight() );
dc86cb34 567
4bc67cc5
RR
568 SetMaskColour( 16, 16, 16 ); // anything unlikely and dividable
569 }
570
571 GdkVisual *visual = gdk_window_get_visual( bitmap.GetPixmap() );
fd0eed64
RR
572 if (visual == NULL) visual = gdk_window_get_visual( (GdkWindow*) &gdk_root_parent );
573 int bpp = visual->depth;
fb1585ae 574 if ((bpp == 16) && (visual->red_mask != 0xf800)) bpp = 15;
e8fdc264 575
fd0eed64 576 GdkColormap *cmap = gtk_widget_get_default_colormap();
cf7a7e13 577
fd0eed64 578 long pos = 0;
4bc67cc5 579 for (int j = 0; j < bitmap.GetHeight(); j++)
01111366 580 {
4bc67cc5 581 for (int i = 0; i < bitmap.GetWidth(); i++)
fd0eed64
RR
582 {
583 int pixel = gdk_image_get_pixel( gdk_image, i, j );
584 if (bpp <= 8)
585 {
586 data[pos] = cmap->colors[pixel].red >> 8;
587 data[pos+1] = cmap->colors[pixel].green >> 8;
588 data[pos+2] = cmap->colors[pixel].blue >> 8;
589 } else if (bpp == 15)
590 {
591 data[pos] = (pixel >> 7) & 0xf8;
592 data[pos+1] = (pixel >> 2) & 0xf8;
593 data[pos+2] = (pixel << 3) & 0xf8;
594 } else if (bpp == 16)
595 {
596 data[pos] = (pixel >> 8) & 0xf8;
597 data[pos+1] = (pixel >> 3) & 0xfc;
598 data[pos+2] = (pixel << 3) & 0xf8;
599 } else
600 {
601 data[pos] = (pixel >> 16) & 0xff;
602 data[pos+1] = (pixel >> 8) & 0xff;
603 data[pos+2] = pixel & 0xff;
604 }
01111366 605
4bc67cc5
RR
606 if (gdk_image_mask)
607 {
608 int mask_pixel = gdk_image_get_pixel( gdk_image_mask, i, j );
609 if (mask_pixel == 0)
610 {
611 data[pos] = 16;
612 data[pos+1] = 16;
613 data[pos+2] = 16;
614 }
615 }
616
fd0eed64
RR
617 pos += 3;
618 }
01111366 619 }
219f895a 620
fd0eed64 621 gdk_image_destroy( gdk_image );
4bc67cc5 622 if (gdk_image_mask) gdk_image_destroy( gdk_image_mask );
ff7b1510 623}
219f895a
RR
624
625
01111366 626