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