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