]>
Commit | Line | Data |
---|---|---|
c801d85f | 1 | ///////////////////////////////////////////////////////////////////////////// |
3cbab641 | 2 | // Name: src/gtk1/bitmap.cpp |
c801d85f KB |
3 | // Purpose: |
4 | // Author: Robert Roebling | |
01111366 | 5 | // Copyright: (c) 1998 Robert Roebling |
65571936 | 6 | // Licence: wxWindows licence |
c801d85f KB |
7 | ///////////////////////////////////////////////////////////////////////////// |
8 | ||
14f355c2 VS |
9 | // For compilers that support precompilation, includes "wx.h". |
10 | #include "wx/wxprec.h" | |
11 | ||
c801d85f | 12 | #include "wx/bitmap.h" |
670f9935 WS |
13 | |
14 | #ifndef WX_PRECOMP | |
15 | #include "wx/app.h" | |
f38924e8 | 16 | #include "wx/dcmemory.h" |
559a723c | 17 | #include "wx/palette.h" |
923d28da | 18 | #include "wx/icon.h" |
18680f86 | 19 | #include "wx/math.h" |
155ecd4c | 20 | #include "wx/image.h" |
670f9935 WS |
21 | #endif // WX_PRECOMP |
22 | ||
fd0eed64 | 23 | #include "wx/filefn.h" |
83624f79 | 24 | |
20e05ffb | 25 | #include <gdk/gdk.h> |
d76fe38b | 26 | #include <gtk/gtk.h> |
b5f01ae0 VS |
27 | #include <gdk/gdkx.h> |
28 | ||
3cbab641 | 29 | #include <gdk/gdkrgb.h> |
13111b2a | 30 | |
3cbab641 MR |
31 | extern |
32 | void gdk_wx_draw_bitmap (GdkDrawable *drawable, | |
33 | GdkGC *gc, | |
34 | GdkDrawable *src, | |
35 | gint xsrc, | |
36 | gint ysrc, | |
37 | gint xdest, | |
38 | gint ydest, | |
39 | gint width, | |
40 | gint height); | |
f6bcfd97 | 41 | |
d76fe38b RR |
42 | //----------------------------------------------------------------------------- |
43 | // data | |
44 | //----------------------------------------------------------------------------- | |
45 | ||
c2fa61e8 | 46 | extern GtkWidget *wxGetRootWindow(); |
c801d85f KB |
47 | |
48 | //----------------------------------------------------------------------------- | |
49 | // wxMask | |
50 | //----------------------------------------------------------------------------- | |
51 | ||
52 | IMPLEMENT_DYNAMIC_CLASS(wxMask,wxObject) | |
53 | ||
8bbe427f | 54 | wxMask::wxMask() |
c801d85f | 55 | { |
d3b9f782 | 56 | m_bitmap = NULL; |
ff7b1510 | 57 | } |
c801d85f | 58 | |
91b8de8d | 59 | wxMask::wxMask( const wxBitmap& bitmap, const wxColour& colour ) |
c801d85f | 60 | { |
d3b9f782 | 61 | m_bitmap = NULL; |
91b8de8d | 62 | Create( bitmap, colour ); |
ff7b1510 | 63 | } |
c801d85f | 64 | |
0b04c4e0 | 65 | #if wxUSE_PALETTE |
91b8de8d | 66 | wxMask::wxMask( const wxBitmap& bitmap, int paletteIndex ) |
c801d85f | 67 | { |
d3b9f782 | 68 | m_bitmap = NULL; |
91b8de8d | 69 | Create( bitmap, paletteIndex ); |
ff7b1510 | 70 | } |
0b04c4e0 | 71 | #endif // wxUSE_PALETTE |
c801d85f | 72 | |
91b8de8d | 73 | wxMask::wxMask( const wxBitmap& bitmap ) |
c801d85f | 74 | { |
d3b9f782 | 75 | m_bitmap = NULL; |
91b8de8d | 76 | Create( bitmap ); |
ff7b1510 | 77 | } |
c801d85f | 78 | |
8bbe427f | 79 | wxMask::~wxMask() |
c801d85f | 80 | { |
13111b2a | 81 | if (m_bitmap) |
72a7edf0 | 82 | gdk_bitmap_unref( m_bitmap ); |
ff7b1510 | 83 | } |
c801d85f | 84 | |
1fb4de31 RR |
85 | bool wxMask::Create( const wxBitmap& bitmap, |
86 | const wxColour& colour ) | |
91b8de8d RR |
87 | { |
88 | if (m_bitmap) | |
284b4c88 | 89 | { |
91b8de8d | 90 | gdk_bitmap_unref( m_bitmap ); |
d3b9f782 | 91 | m_bitmap = NULL; |
91b8de8d | 92 | } |
13111b2a | 93 | |
368d59f0 | 94 | wxImage image = bitmap.ConvertToImage(); |
a1b806b9 | 95 | if (!image.IsOk()) return false; |
13111b2a | 96 | |
c2fa61e8 | 97 | m_bitmap = gdk_pixmap_new( wxGetRootWindow()->window, image.GetWidth(), image.GetHeight(), 1 ); |
f9ee644e | 98 | GdkGC *gc = gdk_gc_new( m_bitmap ); |
13111b2a | 99 | |
f9ee644e RR |
100 | GdkColor color; |
101 | color.red = 65000; | |
102 | color.green = 65000; | |
103 | color.blue = 65000; | |
104 | color.pixel = 1; | |
105 | gdk_gc_set_foreground( gc, &color ); | |
106 | gdk_gc_set_fill( gc, GDK_SOLID ); | |
107 | gdk_draw_rectangle( m_bitmap, gc, TRUE, 0, 0, image.GetWidth(), image.GetHeight() ); | |
13111b2a | 108 | |
1fb4de31 RR |
109 | unsigned char *data = image.GetData(); |
110 | int index = 0; | |
13111b2a | 111 | |
1fb4de31 RR |
112 | unsigned char red = colour.Red(); |
113 | unsigned char green = colour.Green(); | |
114 | unsigned char blue = colour.Blue(); | |
13111b2a | 115 | |
005f5d18 | 116 | GdkVisual *visual = wxTheApp->GetGdkVisual(); |
c2fa61e8 | 117 | |
1fb4de31 | 118 | int bpp = visual->depth; |
2eefae6e VZ |
119 | if ((bpp == 16) && (visual->red_mask != 0xf800)) |
120 | bpp = 15; | |
1fb4de31 RR |
121 | if (bpp == 15) |
122 | { | |
123 | red = red & 0xf8; | |
1fb4de31 | 124 | green = green & 0xf8; |
f6bcfd97 | 125 | blue = blue & 0xf8; |
2eefae6e VZ |
126 | } |
127 | else if (bpp == 16) | |
1fb4de31 RR |
128 | { |
129 | red = red & 0xf8; | |
f6bcfd97 BP |
130 | green = green & 0xfc; |
131 | blue = blue & 0xf8; | |
2eefae6e VZ |
132 | } |
133 | else if (bpp == 12) | |
005f5d18 RR |
134 | { |
135 | red = red & 0xf0; | |
136 | green = green & 0xf0; | |
137 | blue = blue & 0xf0; | |
1fb4de31 | 138 | } |
13111b2a | 139 | |
f9ee644e RR |
140 | color.red = 0; |
141 | color.green = 0; | |
142 | color.blue = 0; | |
143 | color.pixel = 0; | |
144 | gdk_gc_set_foreground( gc, &color ); | |
13111b2a | 145 | |
1fb4de31 | 146 | for (int j = 0; j < image.GetHeight(); j++) |
f9ee644e | 147 | { |
f2593d0d RR |
148 | int start_x = -1; |
149 | int i; | |
150 | for (i = 0; i < image.GetWidth(); i++) | |
1fb4de31 | 151 | { |
13111b2a VZ |
152 | if ((data[index] == red) && |
153 | (data[index+1] == green) && | |
154 | (data[index+2] == blue)) | |
155 | { | |
156 | if (start_x == -1) | |
157 | start_x = i; | |
158 | } | |
159 | else | |
160 | { | |
161 | if (start_x != -1) | |
162 | { | |
163 | gdk_draw_line( m_bitmap, gc, start_x, j, i-1, j ); | |
164 | start_x = -1; | |
165 | } | |
f9ee644e RR |
166 | } |
167 | index += 3; | |
168 | } | |
169 | if (start_x != -1) | |
170 | gdk_draw_line( m_bitmap, gc, start_x, j, i, j ); | |
171 | } | |
1fb4de31 | 172 | |
f9ee644e | 173 | gdk_gc_unref( gc ); |
1fb4de31 | 174 | |
902725ee | 175 | return true; |
91b8de8d RR |
176 | } |
177 | ||
0b04c4e0 | 178 | #if wxUSE_PALETTE |
b5f01ae0 | 179 | bool wxMask::Create( const wxBitmap& bitmap, int paletteIndex ) |
91b8de8d | 180 | { |
b5f01ae0 VS |
181 | unsigned char r,g,b; |
182 | wxPalette *pal = bitmap.GetPalette(); | |
284b4c88 | 183 | |
902725ee | 184 | wxCHECK_MSG( pal, false, wxT("Cannot create mask from bitmap without palette") ); |
c2fa61e8 | 185 | |
b5f01ae0 | 186 | pal->GetRGB(paletteIndex, &r, &g, &b); |
284b4c88 | 187 | |
b5f01ae0 | 188 | return Create(bitmap, wxColour(r, g, b)); |
91b8de8d | 189 | } |
0b04c4e0 | 190 | #endif // wxUSE_PALETTE |
91b8de8d RR |
191 | |
192 | bool wxMask::Create( const wxBitmap& bitmap ) | |
193 | { | |
194 | if (m_bitmap) | |
284b4c88 | 195 | { |
91b8de8d | 196 | gdk_bitmap_unref( m_bitmap ); |
d3b9f782 | 197 | m_bitmap = NULL; |
91b8de8d | 198 | } |
284b4c88 | 199 | |
a1b806b9 | 200 | if (!bitmap.IsOk()) return false; |
284b4c88 | 201 | |
902725ee | 202 | wxCHECK_MSG( bitmap.GetBitmap(), false, wxT("Cannot create mask from colour bitmap") ); |
284b4c88 | 203 | |
c2fa61e8 | 204 | m_bitmap = gdk_pixmap_new( wxGetRootWindow()->window, bitmap.GetWidth(), bitmap.GetHeight(), 1 ); |
284b4c88 | 205 | |
902725ee | 206 | if (!m_bitmap) return false; |
284b4c88 | 207 | |
91b8de8d | 208 | GdkGC *gc = gdk_gc_new( m_bitmap ); |
284b4c88 | 209 | |
f6bcfd97 | 210 | gdk_wx_draw_bitmap( m_bitmap, gc, bitmap.GetBitmap(), 0, 0, 0, 0, bitmap.GetWidth(), bitmap.GetHeight() ); |
284b4c88 | 211 | |
91b8de8d | 212 | gdk_gc_unref( gc ); |
284b4c88 | 213 | |
902725ee | 214 | return true; |
91b8de8d RR |
215 | } |
216 | ||
217 | GdkBitmap *wxMask::GetBitmap() const | |
c801d85f | 218 | { |
fd0eed64 | 219 | return m_bitmap; |
ff7b1510 | 220 | } |
8bbe427f | 221 | |
732d8c74 | 222 | |
c801d85f | 223 | //----------------------------------------------------------------------------- |
732d8c74 | 224 | // wxBitmapRefData |
c801d85f KB |
225 | //----------------------------------------------------------------------------- |
226 | ||
8f884a0d | 227 | class wxBitmapRefData : public wxGDIRefData |
c801d85f | 228 | { |
fd0eed64 | 229 | public: |
f2593d0d | 230 | wxBitmapRefData(); |
39b0ab4a VZ |
231 | wxBitmapRefData(const wxBitmapRefData& data); |
232 | bool Create(int width, int height, int bpp); | |
d3c7fc99 | 233 | virtual ~wxBitmapRefData(); |
f2593d0d | 234 | |
8f884a0d VZ |
235 | virtual bool IsOk() const { return m_pixmap || m_bitmap; } |
236 | ||
f2593d0d RR |
237 | GdkPixmap *m_pixmap; |
238 | GdkBitmap *m_bitmap; | |
239 | wxMask *m_mask; | |
240 | int m_width; | |
241 | int m_height; | |
242 | int m_bpp; | |
39b0ab4a | 243 | #if wxUSE_PALETTE |
f2593d0d | 244 | wxPalette *m_palette; |
39b0ab4a | 245 | #endif // wxUSE_PALETTE |
c801d85f KB |
246 | }; |
247 | ||
8bbe427f | 248 | wxBitmapRefData::wxBitmapRefData() |
c801d85f | 249 | { |
d3b9f782 VZ |
250 | m_pixmap = NULL; |
251 | m_bitmap = NULL; | |
252 | m_mask = NULL; | |
fd0eed64 RR |
253 | m_width = 0; |
254 | m_height = 0; | |
255 | m_bpp = 0; | |
39b0ab4a | 256 | #if wxUSE_PALETTE |
d3b9f782 | 257 | m_palette = NULL; |
39b0ab4a VZ |
258 | #endif // wxUSE_PALETTE |
259 | } | |
260 | ||
261 | wxBitmapRefData::wxBitmapRefData(const wxBitmapRefData& data) | |
262 | { | |
263 | Create(data.m_width, data.m_height, data.m_bpp); | |
264 | ||
265 | m_mask = data.m_mask ? new wxMask(*data.m_mask) : NULL; | |
266 | ||
267 | #if wxUSE_PALETTE | |
268 | wxASSERT_MSG( !data.m_palette, | |
9a83f860 | 269 | wxT("copying bitmaps palette not implemented") ); |
39b0ab4a VZ |
270 | #endif // wxUSE_PALETTE |
271 | ||
272 | ||
273 | // copy the bitmap data by simply drawing the source bitmap on this one | |
274 | GdkPixmap **dst; | |
275 | if ( data.m_pixmap ) | |
276 | { | |
277 | dst = &m_pixmap; | |
278 | } | |
279 | else if ( data.m_bitmap ) | |
280 | { | |
281 | dst = &m_bitmap; | |
282 | } | |
283 | else // invalid bitmap? | |
284 | { | |
285 | return; | |
286 | } | |
287 | ||
288 | GdkGC *gc = gdk_gc_new(*dst); | |
289 | if ( m_bpp == 1 ) | |
290 | { | |
291 | gdk_wx_draw_bitmap(m_bitmap, gc, data.m_bitmap, 0, 0, 0, 0, -1, -1); | |
292 | } | |
293 | else // colour pixmap | |
294 | { | |
295 | gdk_draw_pixmap(m_pixmap, gc, data.m_pixmap, 0, 0, 0, 0, -1, -1); | |
296 | } | |
297 | ||
298 | gdk_gc_unref(gc); | |
299 | } | |
300 | ||
301 | bool wxBitmapRefData::Create(int width, int height, int bpp) | |
302 | { | |
303 | m_width = width; | |
304 | m_height = height; | |
305 | m_bpp = bpp; | |
306 | ||
307 | m_mask = NULL; | |
308 | #if wxUSE_PALETTE | |
309 | m_palette = NULL; | |
310 | #endif | |
311 | ||
312 | // to understand how this compiles you should know that GdkPixmap and | |
313 | // GdkBitmap are one and the same type in GTK+ 1 | |
314 | GdkPixmap **ppix; | |
315 | if ( m_bpp != 1 ) | |
316 | { | |
317 | const GdkVisual * const visual = wxTheApp->GetGdkVisual(); | |
318 | ||
319 | wxCHECK_MSG( (bpp == -1) || (bpp == visual->depth) || (bpp == 32), false, | |
320 | wxT("invalid bitmap depth") ); | |
321 | ||
322 | m_bpp = visual->depth; | |
323 | ||
324 | ppix = &m_pixmap; | |
325 | m_bitmap = NULL; | |
326 | } | |
327 | else // mono bitmap | |
328 | { | |
329 | ppix = &m_bitmap; | |
330 | m_pixmap = NULL; | |
331 | } | |
332 | ||
333 | *ppix = gdk_pixmap_new( wxGetRootWindow()->window, width, height, m_bpp ); | |
334 | ||
335 | return *ppix != NULL; | |
ff7b1510 | 336 | } |
c801d85f | 337 | |
8bbe427f | 338 | wxBitmapRefData::~wxBitmapRefData() |
c801d85f | 339 | { |
3ebcd89d VZ |
340 | if (m_pixmap) |
341 | gdk_pixmap_unref( m_pixmap ); | |
342 | if (m_bitmap) | |
343 | gdk_bitmap_unref( m_bitmap ); | |
344 | delete m_mask; | |
0b04c4e0 | 345 | #if wxUSE_PALETTE |
3ebcd89d | 346 | delete m_palette; |
0b04c4e0 | 347 | #endif // wxUSE_PALETTE |
ff7b1510 | 348 | } |
c801d85f | 349 | |
732d8c74 FM |
350 | |
351 | //----------------------------------------------------------------------------- | |
352 | // wxBitmap | |
c801d85f KB |
353 | //----------------------------------------------------------------------------- |
354 | ||
355 | #define M_BMPDATA ((wxBitmapRefData *)m_refData) | |
356 | ||
357 | IMPLEMENT_DYNAMIC_CLASS(wxBitmap,wxGDIObject) | |
358 | ||
8f884a0d | 359 | wxGDIRefData *wxBitmap::CreateGDIRefData() const |
39b0ab4a VZ |
360 | { |
361 | return new wxBitmapRefData; | |
362 | } | |
363 | ||
8f884a0d | 364 | wxGDIRefData *wxBitmap::CloneGDIRefData(const wxGDIRefData *data) const |
39b0ab4a | 365 | { |
5c33522f | 366 | return new wxBitmapRefData(*static_cast<const wxBitmapRefData *>(data)); |
39b0ab4a VZ |
367 | } |
368 | ||
c826213d RR |
369 | bool wxBitmap::Create( int width, int height, int depth ) |
370 | { | |
371 | UnRef(); | |
372 | ||
3ebcd89d VZ |
373 | if ( width <= 0 || height <= 0 ) |
374 | { | |
375 | return false; | |
376 | } | |
284b4c88 | 377 | |
eefa26be | 378 | m_refData = new wxBitmapRefData(); |
39b0ab4a | 379 | return M_BMPDATA->Create(width, height, depth); |
ff7b1510 | 380 | } |
b5f01ae0 | 381 | |
452418c4 | 382 | wxBitmap::wxBitmap(const char* const* bits) |
e52f60e6 | 383 | { |
452418c4 | 384 | wxCHECK2_MSG(bits != NULL, return, wxT("invalid bitmap data")); |
8bbe427f | 385 | |
005f5d18 | 386 | GdkVisual *visual = wxTheApp->GetGdkVisual(); |
c2fa61e8 | 387 | |
e52f60e6 RR |
388 | m_refData = new wxBitmapRefData(); |
389 | ||
d3b9f782 | 390 | GdkBitmap *mask = NULL; |
8bbe427f | 391 | |
c2fa61e8 | 392 | M_BMPDATA->m_pixmap = gdk_pixmap_create_from_xpm_d( wxGetRootWindow()->window, &mask, NULL, (gchar **) bits ); |
8bbe427f | 393 | |
452418c4 | 394 | wxCHECK2_MSG(M_BMPDATA->m_pixmap, return, wxT("couldn't create pixmap")); |
8bbe427f | 395 | |
fd0eed64 RR |
396 | if (mask) |
397 | { | |
398 | M_BMPDATA->m_mask = new wxMask(); | |
399 | M_BMPDATA->m_mask->m_bitmap = mask; | |
400 | } | |
8bbe427f | 401 | |
fd0eed64 | 402 | gdk_window_get_size( M_BMPDATA->m_pixmap, &(M_BMPDATA->m_width), &(M_BMPDATA->m_height) ); |
2eefae6e | 403 | |
8cce8940 | 404 | M_BMPDATA->m_bpp = visual->depth; // Can we get a different depth from create_from_xpm_d() ? |
ff7b1510 | 405 | } |
b5f01ae0 | 406 | |
783da845 RR |
407 | wxBitmap wxBitmap::Rescale( int clipx, int clipy, int clipwidth, int clipheight, int newx, int newy ) |
408 | { | |
a1b806b9 | 409 | wxCHECK_MSG( IsOk(), wxNullBitmap, wxT("invalid bitmap") ); |
783da845 RR |
410 | |
411 | if (newy==M_BMPDATA->m_width && newy==M_BMPDATA->m_height) | |
412 | return *this; | |
902725ee | 413 | |
783da845 RR |
414 | int width = wxMax(newx, 1); |
415 | int height = wxMax(newy, 1); | |
416 | width = wxMin(width, clipwidth); | |
417 | height = wxMin(height, clipheight); | |
902725ee | 418 | |
feac7937 | 419 | wxBitmap bmp; |
783da845 | 420 | |
d3b9f782 | 421 | GdkImage *img = NULL; |
3cbab641 MR |
422 | if (GetPixmap()) |
423 | img = gdk_image_get( GetPixmap(), 0, 0, GetWidth(), GetHeight() ); | |
424 | else if (GetBitmap()) | |
425 | img = gdk_image_get( GetBitmap(), 0, 0, GetWidth(), GetHeight() ); | |
426 | else | |
427 | wxFAIL_MSG( wxT("Ill-formed bitmap") ); | |
428 | ||
429 | wxCHECK_MSG( img, wxNullBitmap, wxT("couldn't create image") ); | |
430 | ||
431 | int bpp = -1; | |
432 | ||
433 | ||
434 | GdkGC *gc = NULL; | |
435 | GdkPixmap *dstpix = NULL; | |
436 | if (GetPixmap()) | |
783da845 | 437 | { |
3cbab641 MR |
438 | GdkVisual *visual = gdk_window_get_visual( GetPixmap() ); |
439 | if (visual == NULL) | |
440 | visual = wxTheApp->GetGdkVisual(); | |
441 | ||
442 | bpp = visual->depth; | |
443 | bmp = wxBitmap(width,height,bpp); | |
444 | dstpix = bmp.GetPixmap(); | |
445 | gc = gdk_gc_new( dstpix ); | |
783da845 | 446 | } |
3cbab641 MR |
447 | |
448 | char *dst = NULL; | |
449 | long dstbyteperline = 0; | |
450 | ||
451 | if (GetBitmap()) | |
783da845 | 452 | { |
3cbab641 MR |
453 | bpp = 1; |
454 | dstbyteperline = width/8*M_BMPDATA->m_bpp; | |
455 | if (width*M_BMPDATA->m_bpp % 8 != 0) | |
456 | dstbyteperline++; | |
457 | dst = (char*) malloc(dstbyteperline*height); | |
458 | } | |
feac7937 | 459 | |
3cbab641 MR |
460 | // be careful to use the right scaling factor |
461 | float scx = (float)M_BMPDATA->m_width/(float)newx; | |
462 | float scy = (float)M_BMPDATA->m_height/(float)newy; | |
463 | // prepare accel-tables | |
464 | int *tablex = (int *)calloc(width,sizeof(int)); | |
465 | int *tabley = (int *)calloc(height,sizeof(int)); | |
783da845 | 466 | |
3cbab641 MR |
467 | // accel table filled with clipped values |
468 | for (int x = 0; x < width; x++) | |
469 | tablex[x] = (int) (scx * (x+clipx)); | |
470 | for (int y = 0; y < height; y++) | |
471 | tabley[y] = (int) (scy * (y+clipy)); | |
feac7937 | 472 | |
3cbab641 MR |
473 | // Main rescaling routine starts here |
474 | for (int h = 0; h < height; h++) | |
475 | { | |
476 | char outbyte = 0; | |
477 | int old_x = -1; | |
478 | guint32 old_pixval = 0; | |
902725ee | 479 | |
3cbab641 | 480 | for (int w = 0; w < width; w++) |
783da845 | 481 | { |
3cbab641 MR |
482 | guint32 pixval; |
483 | int x = tablex[w]; | |
484 | if (x == old_x) | |
485 | pixval = old_pixval; | |
486 | else | |
487 | { | |
488 | pixval = gdk_image_get_pixel( img, x, tabley[h] ); | |
489 | old_pixval = pixval; | |
490 | old_x = x; | |
491 | } | |
783da845 | 492 | |
3cbab641 MR |
493 | if (bpp == 1) |
494 | { | |
495 | if (!pixval) | |
496 | { | |
497 | char bit=1; | |
498 | char shift = bit << (w % 8); | |
499 | outbyte |= shift; | |
500 | } | |
902725ee | 501 | |
3cbab641 MR |
502 | if ((w+1)%8==0) |
503 | { | |
504 | dst[h*dstbyteperline+w/8] = outbyte; | |
505 | outbyte = 0; | |
506 | } | |
507 | } | |
508 | else | |
509 | { | |
510 | GdkColor col; | |
511 | col.pixel = pixval; | |
512 | gdk_gc_set_foreground( gc, &col ); | |
513 | gdk_draw_point( dstpix, gc, w, h); | |
514 | } | |
feac7937 | 515 | } |
902725ee | 516 | |
3cbab641 MR |
517 | // do not forget the last byte |
518 | if ((bpp == 1) && (width % 8 != 0)) | |
519 | dst[h*dstbyteperline+width/8] = outbyte; | |
520 | } | |
feac7937 | 521 | |
3cbab641 MR |
522 | gdk_image_destroy( img ); |
523 | if (gc) gdk_gc_unref( gc ); | |
524 | ||
525 | if (bpp == 1) | |
526 | { | |
527 | bmp = wxBitmap( (const char *)dst, width, height, 1 ); | |
528 | free( dst ); | |
529 | } | |
530 | ||
531 | if (GetMask()) | |
532 | { | |
533 | dstbyteperline = width/8; | |
534 | if (width % 8 != 0) | |
535 | dstbyteperline++; | |
536 | dst = (char*) malloc(dstbyteperline*height); | |
537 | img = gdk_image_get( GetMask()->GetBitmap(), 0, 0, GetWidth(), GetHeight() ); | |
783da845 RR |
538 | |
539 | for (int h = 0; h < height; h++) | |
540 | { | |
541 | char outbyte = 0; | |
f9ebac93 | 542 | int old_x = -1; |
b63b07a8 | 543 | guint32 old_pixval = 0; |
feac7937 | 544 | |
f9ebac93 | 545 | for (int w = 0; w < width; w++) |
783da845 | 546 | { |
f9ebac93 RR |
547 | guint32 pixval; |
548 | int x = tablex[w]; | |
549 | if (x == old_x) | |
550 | pixval = old_pixval; | |
551 | else | |
552 | { | |
553 | pixval = gdk_image_get_pixel( img, x, tabley[h] ); | |
554 | old_pixval = pixval; | |
555 | old_x = x; | |
556 | } | |
902725ee | 557 | |
3cbab641 | 558 | if (pixval) |
783da845 | 559 | { |
3cbab641 MR |
560 | char bit=1; |
561 | char shift = bit << (w % 8); | |
562 | outbyte |= shift; | |
783da845 | 563 | } |
3cbab641 MR |
564 | |
565 | if ((w+1)%8 == 0) | |
783da845 | 566 | { |
3cbab641 MR |
567 | dst[h*dstbyteperline+w/8] = outbyte; |
568 | outbyte = 0; | |
783da845 RR |
569 | } |
570 | } | |
902725ee | 571 | |
783da845 | 572 | // do not forget the last byte |
3cbab641 | 573 | if (width % 8 != 0) |
f9ebac93 | 574 | dst[h*dstbyteperline+width/8] = outbyte; |
783da845 | 575 | } |
3cbab641 MR |
576 | wxMask* mask = new wxMask; |
577 | mask->m_bitmap = gdk_bitmap_create_from_data( wxGetRootWindow()->window, (gchar *) dst, width, height ); | |
578 | bmp.SetMask(mask); | |
902725ee | 579 | |
3cbab641 | 580 | free( dst ); |
783da845 | 581 | gdk_image_destroy( img ); |
feac7937 | 582 | } |
902725ee | 583 | |
3cbab641 MR |
584 | free( tablex ); |
585 | free( tabley ); | |
586 | ||
902725ee | 587 | return bmp; |
783da845 RR |
588 | } |
589 | ||
feac7937 | 590 | bool wxBitmap::CreateFromImage(const wxImage& image, int depth) |
b5f01ae0 | 591 | { |
a11672a4 RR |
592 | UnRef(); |
593 | ||
a1b806b9 | 594 | wxCHECK_MSG( image.IsOk(), false, wxT("invalid image") ); |
637b7e4f | 595 | wxCHECK_MSG( depth == -1 || depth == 1, false, wxT("invalid bitmap depth") ); |
b5f01ae0 | 596 | |
feac7937 VS |
597 | if (image.GetWidth() <= 0 || image.GetHeight() <= 0) |
598 | return false; | |
902725ee | 599 | |
feac7937 | 600 | m_refData = new wxBitmapRefData(); |
3ebcd89d | 601 | |
feac7937 | 602 | if (depth == 1) |
3ebcd89d | 603 | { |
feac7937 | 604 | return CreateFromImageAsBitmap(image); |
3ebcd89d | 605 | } |
feac7937 VS |
606 | else |
607 | { | |
feac7937 VS |
608 | return CreateFromImageAsPixmap(image); |
609 | } | |
610 | } | |
3ebcd89d | 611 | |
feac7937 VS |
612 | // conversion to mono bitmap: |
613 | bool wxBitmap::CreateFromImageAsBitmap(const wxImage& img) | |
614 | { | |
615 | // convert alpha channel to mask, if it is present: | |
616 | wxImage image(img); | |
617 | image.ConvertAlphaToMask(); | |
902725ee | 618 | |
feac7937 VS |
619 | int width = image.GetWidth(); |
620 | int height = image.GetHeight(); | |
c2fa61e8 | 621 | |
3ebcd89d VZ |
622 | SetHeight( height ); |
623 | SetWidth( width ); | |
624 | ||
feac7937 | 625 | SetBitmap( gdk_pixmap_new( wxGetRootWindow()->window, width, height, 1 ) ); |
b5f01ae0 | 626 | |
feac7937 | 627 | SetDepth( 1 ); |
2eefae6e | 628 | |
feac7937 | 629 | GdkVisual *visual = wxTheApp->GetGdkVisual(); |
b5f01ae0 | 630 | |
feac7937 | 631 | // Create picture image |
b5f01ae0 | 632 | |
feac7937 | 633 | unsigned char *data_data = (unsigned char*)malloc( ((width >> 3)+8) * height ); |
b5f01ae0 | 634 | |
feac7937 VS |
635 | GdkImage *data_image = |
636 | gdk_image_new_bitmap( visual, data_data, width, height ); | |
b5f01ae0 | 637 | |
feac7937 | 638 | // Create mask image |
b5f01ae0 | 639 | |
d3b9f782 | 640 | GdkImage *mask_image = NULL; |
b5f01ae0 | 641 | |
feac7937 VS |
642 | if (image.HasMask()) |
643 | { | |
644 | unsigned char *mask_data = (unsigned char*)malloc( ((width >> 3)+8) * height ); | |
b5f01ae0 | 645 | |
feac7937 | 646 | mask_image = gdk_image_new_bitmap( visual, mask_data, width, height ); |
b5f01ae0 | 647 | |
feac7937 VS |
648 | wxMask *mask = new wxMask(); |
649 | mask->m_bitmap = gdk_pixmap_new( wxGetRootWindow()->window, width, height, 1 ); | |
b5f01ae0 | 650 | |
feac7937 VS |
651 | SetMask( mask ); |
652 | } | |
b5f01ae0 | 653 | |
feac7937 VS |
654 | int r_mask = image.GetMaskRed(); |
655 | int g_mask = image.GetMaskGreen(); | |
656 | int b_mask = image.GetMaskBlue(); | |
b5f01ae0 | 657 | |
feac7937 | 658 | unsigned char* data = image.GetData(); |
b5f01ae0 | 659 | |
feac7937 VS |
660 | int index = 0; |
661 | for (int y = 0; y < height; y++) | |
662 | { | |
663 | for (int x = 0; x < width; x++) | |
b5f01ae0 | 664 | { |
feac7937 VS |
665 | int r = data[index]; |
666 | index++; | |
667 | int g = data[index]; | |
668 | index++; | |
669 | int b = data[index]; | |
670 | index++; | |
671 | ||
672 | if (image.HasMask()) | |
b5f01ae0 | 673 | { |
feac7937 VS |
674 | if ((r == r_mask) && (b == b_mask) && (g == g_mask)) |
675 | gdk_image_put_pixel( mask_image, x, y, 1 ); | |
b5f01ae0 | 676 | else |
feac7937 VS |
677 | gdk_image_put_pixel( mask_image, x, y, 0 ); |
678 | } | |
b5f01ae0 | 679 | |
feac7937 VS |
680 | if ((r == 255) && (b == 255) && (g == 255)) |
681 | gdk_image_put_pixel( data_image, x, y, 1 ); | |
682 | else | |
683 | gdk_image_put_pixel( data_image, x, y, 0 ); | |
b5f01ae0 | 684 | |
feac7937 VS |
685 | } // for |
686 | } // for | |
b5f01ae0 | 687 | |
feac7937 | 688 | // Blit picture |
b5f01ae0 | 689 | |
feac7937 | 690 | GdkGC *data_gc = gdk_gc_new( GetBitmap() ); |
b5f01ae0 | 691 | |
feac7937 | 692 | gdk_draw_image( GetBitmap(), data_gc, data_image, 0, 0, 0, 0, width, height ); |
b5f01ae0 | 693 | |
feac7937 VS |
694 | gdk_image_destroy( data_image ); |
695 | gdk_gc_unref( data_gc ); | |
b5f01ae0 | 696 | |
feac7937 | 697 | // Blit mask |
b5f01ae0 | 698 | |
feac7937 VS |
699 | if (image.HasMask()) |
700 | { | |
701 | GdkGC *mask_gc = gdk_gc_new( GetMask()->GetBitmap() ); | |
b5f01ae0 | 702 | |
feac7937 VS |
703 | gdk_draw_image( GetMask()->GetBitmap(), mask_gc, mask_image, 0, 0, 0, 0, width, height ); |
704 | ||
705 | gdk_image_destroy( mask_image ); | |
706 | gdk_gc_unref( mask_gc ); | |
b5f01ae0 | 707 | } |
c2fa61e8 | 708 | |
feac7937 VS |
709 | return true; |
710 | } | |
902725ee | 711 | |
feac7937 VS |
712 | // conversion to colour bitmap: |
713 | bool wxBitmap::CreateFromImageAsPixmap(const wxImage& img) | |
714 | { | |
715 | // convert alpha channel to mask, if it is present: | |
716 | wxImage image(img); | |
717 | image.ConvertAlphaToMask(); | |
902725ee | 718 | |
feac7937 VS |
719 | int width = image.GetWidth(); |
720 | int height = image.GetHeight(); | |
721 | ||
722 | SetHeight( height ); | |
723 | SetWidth( width ); | |
724 | ||
725 | SetPixmap( gdk_pixmap_new( wxGetRootWindow()->window, width, height, -1 ) ); | |
b5f01ae0 | 726 | |
feac7937 | 727 | GdkVisual *visual = wxTheApp->GetGdkVisual(); |
b5f01ae0 | 728 | |
feac7937 | 729 | int bpp = visual->depth; |
b5f01ae0 | 730 | |
feac7937 | 731 | SetDepth( bpp ); |
b5f01ae0 | 732 | |
feac7937 VS |
733 | if ((bpp == 16) && (visual->red_mask != 0xf800)) |
734 | bpp = 15; | |
735 | else if (bpp < 8) | |
736 | bpp = 8; | |
2eefae6e | 737 | |
feac7937 VS |
738 | // We handle 8-bit bitmaps ourselves using the colour cube, 12-bit |
739 | // visuals are not supported by GDK so we do these ourselves, too. | |
740 | // 15-bit and 16-bit should actually work and 24-bit certainly does. | |
9b72a74d | 741 | #ifdef __sgi |
feac7937 | 742 | if (!image.HasMask() && (bpp > 16)) |
9b72a74d | 743 | #else |
feac7937 | 744 | if (!image.HasMask() && (bpp > 12)) |
9b72a74d | 745 | #endif |
feac7937 | 746 | { |
902725ee | 747 | static bool s_hasInitialized = false; |
b5f01ae0 | 748 | |
feac7937 VS |
749 | if (!s_hasInitialized) |
750 | { | |
751 | gdk_rgb_init(); | |
902725ee | 752 | s_hasInitialized = true; |
feac7937 | 753 | } |
b5f01ae0 | 754 | |
feac7937 | 755 | GdkGC *gc = gdk_gc_new( GetPixmap() ); |
b5f01ae0 | 756 | |
feac7937 VS |
757 | gdk_draw_rgb_image( GetPixmap(), |
758 | gc, | |
759 | 0, 0, | |
760 | width, height, | |
761 | GDK_RGB_DITHER_NONE, | |
762 | image.GetData(), | |
763 | width*3 ); | |
b5f01ae0 | 764 | |
feac7937 | 765 | gdk_gc_unref( gc ); |
902725ee | 766 | return true; |
feac7937 | 767 | } |
b5f01ae0 | 768 | |
feac7937 | 769 | // Create picture image |
b5f01ae0 | 770 | |
feac7937 VS |
771 | GdkImage *data_image = |
772 | gdk_image_new( GDK_IMAGE_FASTEST, visual, width, height ); | |
b5f01ae0 | 773 | |
feac7937 | 774 | // Create mask image |
b5f01ae0 | 775 | |
d3b9f782 | 776 | GdkImage *mask_image = NULL; |
b5f01ae0 | 777 | |
feac7937 VS |
778 | if (image.HasMask()) |
779 | { | |
780 | unsigned char *mask_data = (unsigned char*)malloc( ((width >> 3)+8) * height ); | |
b5f01ae0 | 781 | |
feac7937 | 782 | mask_image = gdk_image_new_bitmap( visual, mask_data, width, height ); |
b5f01ae0 | 783 | |
feac7937 VS |
784 | wxMask *mask = new wxMask(); |
785 | mask->m_bitmap = gdk_pixmap_new( wxGetRootWindow()->window, width, height, 1 ); | |
b5f01ae0 | 786 | |
feac7937 VS |
787 | SetMask( mask ); |
788 | } | |
b5f01ae0 | 789 | |
feac7937 | 790 | // Render |
b5f01ae0 | 791 | |
feac7937 VS |
792 | enum byte_order { RGB, RBG, BRG, BGR, GRB, GBR }; |
793 | byte_order b_o = RGB; | |
b5f01ae0 | 794 | |
feac7937 VS |
795 | if (bpp > 8) |
796 | { | |
797 | if ((visual->red_mask > visual->green_mask) && (visual->green_mask > visual->blue_mask)) b_o = RGB; | |
798 | else if ((visual->red_mask > visual->blue_mask) && (visual->blue_mask > visual->green_mask)) b_o = RBG; | |
799 | else if ((visual->blue_mask > visual->red_mask) && (visual->red_mask > visual->green_mask)) b_o = BRG; | |
800 | else if ((visual->blue_mask > visual->green_mask) && (visual->green_mask > visual->red_mask)) b_o = BGR; | |
801 | else if ((visual->green_mask > visual->red_mask) && (visual->red_mask > visual->blue_mask)) b_o = GRB; | |
802 | else if ((visual->green_mask > visual->blue_mask) && (visual->blue_mask > visual->red_mask)) b_o = GBR; | |
803 | } | |
b5f01ae0 | 804 | |
feac7937 VS |
805 | int r_mask = image.GetMaskRed(); |
806 | int g_mask = image.GetMaskGreen(); | |
807 | int b_mask = image.GetMaskBlue(); | |
b5f01ae0 | 808 | |
feac7937 | 809 | unsigned char* data = image.GetData(); |
b5f01ae0 | 810 | |
feac7937 VS |
811 | int index = 0; |
812 | for (int y = 0; y < height; y++) | |
813 | { | |
814 | for (int x = 0; x < width; x++) | |
b5f01ae0 | 815 | { |
feac7937 VS |
816 | int r = data[index]; |
817 | index++; | |
818 | int g = data[index]; | |
819 | index++; | |
820 | int b = data[index]; | |
821 | index++; | |
822 | ||
823 | if (image.HasMask()) | |
b5f01ae0 | 824 | { |
feac7937 VS |
825 | if ((r == r_mask) && (b == b_mask) && (g == g_mask)) |
826 | gdk_image_put_pixel( mask_image, x, y, 1 ); | |
827 | else | |
828 | gdk_image_put_pixel( mask_image, x, y, 0 ); | |
829 | } | |
b5f01ae0 | 830 | |
feac7937 VS |
831 | switch (bpp) |
832 | { | |
833 | case 8: | |
b5f01ae0 | 834 | { |
feac7937 VS |
835 | int pixel = -1; |
836 | if (wxTheApp->m_colorCube) | |
b5f01ae0 | 837 | { |
feac7937 VS |
838 | pixel = wxTheApp->m_colorCube[ ((r & 0xf8) << 7) + ((g & 0xf8) << 2) + ((b & 0xf8) >> 3) ]; |
839 | } | |
840 | else | |
841 | { | |
842 | GdkColormap *cmap = gtk_widget_get_default_colormap(); | |
843 | GdkColor *colors = cmap->colors; | |
844 | int max = 3 * (65536); | |
845 | ||
846 | for (int i = 0; i < cmap->size; i++) | |
b5f01ae0 | 847 | { |
feac7937 VS |
848 | int rdiff = (r << 8) - colors[i].red; |
849 | int gdiff = (g << 8) - colors[i].green; | |
850 | int bdiff = (b << 8) - colors[i].blue; | |
851 | int sum = ABS (rdiff) + ABS (gdiff) + ABS (bdiff); | |
852 | if (sum < max) { pixel = i; max = sum; } | |
b5f01ae0 | 853 | } |
feac7937 | 854 | } |
2eefae6e | 855 | |
feac7937 | 856 | gdk_image_put_pixel( data_image, x, y, pixel ); |
2eefae6e | 857 | |
feac7937 VS |
858 | break; |
859 | } | |
860 | case 12: // SGI only | |
861 | { | |
862 | guint32 pixel = 0; | |
863 | switch (b_o) | |
b5f01ae0 | 864 | { |
feac7937 VS |
865 | case RGB: pixel = ((r & 0xf0) << 4) | (g & 0xf0) | ((b & 0xf0) >> 4); break; |
866 | case RBG: pixel = ((r & 0xf0) << 4) | (b & 0xf0) | ((g & 0xf0) >> 4); break; | |
867 | case GRB: pixel = ((g & 0xf0) << 4) | (r & 0xf0) | ((b & 0xf0) >> 4); break; | |
868 | case GBR: pixel = ((g & 0xf0) << 4) | (b & 0xf0) | ((r & 0xf0) >> 4); break; | |
869 | case BRG: pixel = ((b & 0xf0) << 4) | (r & 0xf0) | ((g & 0xf0) >> 4); break; | |
870 | case BGR: pixel = ((b & 0xf0) << 4) | (g & 0xf0) | ((r & 0xf0) >> 4); break; | |
b5f01ae0 | 871 | } |
feac7937 VS |
872 | gdk_image_put_pixel( data_image, x, y, pixel ); |
873 | break; | |
874 | } | |
875 | case 15: | |
876 | { | |
877 | guint32 pixel = 0; | |
878 | switch (b_o) | |
b5f01ae0 | 879 | { |
feac7937 VS |
880 | case RGB: pixel = ((r & 0xf8) << 7) | ((g & 0xf8) << 2) | ((b & 0xf8) >> 3); break; |
881 | case RBG: pixel = ((r & 0xf8) << 7) | ((b & 0xf8) << 2) | ((g & 0xf8) >> 3); break; | |
882 | case GRB: pixel = ((g & 0xf8) << 7) | ((r & 0xf8) << 2) | ((b & 0xf8) >> 3); break; | |
883 | case GBR: pixel = ((g & 0xf8) << 7) | ((b & 0xf8) << 2) | ((r & 0xf8) >> 3); break; | |
884 | case BRG: pixel = ((b & 0xf8) << 7) | ((r & 0xf8) << 2) | ((g & 0xf8) >> 3); break; | |
885 | case BGR: pixel = ((b & 0xf8) << 7) | ((g & 0xf8) << 2) | ((r & 0xf8) >> 3); break; | |
b5f01ae0 | 886 | } |
feac7937 VS |
887 | gdk_image_put_pixel( data_image, x, y, pixel ); |
888 | break; | |
889 | } | |
890 | case 16: | |
891 | { | |
892 | // I actually don't know if for 16-bit displays, it is alway the green | |
893 | // component or the second component which has 6 bits. | |
894 | guint32 pixel = 0; | |
895 | switch (b_o) | |
b5f01ae0 | 896 | { |
feac7937 VS |
897 | case RGB: pixel = ((r & 0xf8) << 8) | ((g & 0xfc) << 3) | ((b & 0xf8) >> 3); break; |
898 | case RBG: pixel = ((r & 0xf8) << 8) | ((b & 0xfc) << 3) | ((g & 0xf8) >> 3); break; | |
899 | case GRB: pixel = ((g & 0xf8) << 8) | ((r & 0xfc) << 3) | ((b & 0xf8) >> 3); break; | |
900 | case GBR: pixel = ((g & 0xf8) << 8) | ((b & 0xfc) << 3) | ((r & 0xf8) >> 3); break; | |
901 | case BRG: pixel = ((b & 0xf8) << 8) | ((r & 0xfc) << 3) | ((g & 0xf8) >> 3); break; | |
902 | case BGR: pixel = ((b & 0xf8) << 8) | ((g & 0xfc) << 3) | ((r & 0xf8) >> 3); break; | |
8ab696e0 | 903 | } |
feac7937 VS |
904 | gdk_image_put_pixel( data_image, x, y, pixel ); |
905 | break; | |
906 | } | |
907 | case 32: | |
908 | case 24: | |
909 | { | |
910 | guint32 pixel = 0; | |
911 | switch (b_o) | |
8ab696e0 | 912 | { |
feac7937 VS |
913 | case RGB: pixel = (r << 16) | (g << 8) | b; break; |
914 | case RBG: pixel = (r << 16) | (b << 8) | g; break; | |
915 | case BRG: pixel = (b << 16) | (r << 8) | g; break; | |
916 | case BGR: pixel = (b << 16) | (g << 8) | r; break; | |
917 | case GRB: pixel = (g << 16) | (r << 8) | b; break; | |
918 | case GBR: pixel = (g << 16) | (b << 8) | r; break; | |
b5f01ae0 | 919 | } |
feac7937 | 920 | gdk_image_put_pixel( data_image, x, y, pixel ); |
902725ee | 921 | break; |
b5f01ae0 | 922 | } |
feac7937 VS |
923 | default: break; |
924 | } | |
925 | } // for | |
926 | } // for | |
b5f01ae0 | 927 | |
feac7937 | 928 | // Blit picture |
b5f01ae0 | 929 | |
feac7937 | 930 | GdkGC *data_gc = gdk_gc_new( GetPixmap() ); |
b5f01ae0 | 931 | |
feac7937 | 932 | gdk_draw_image( GetPixmap(), data_gc, data_image, 0, 0, 0, 0, width, height ); |
b5f01ae0 | 933 | |
feac7937 VS |
934 | gdk_image_destroy( data_image ); |
935 | gdk_gc_unref( data_gc ); | |
b5f01ae0 | 936 | |
feac7937 | 937 | // Blit mask |
b5f01ae0 | 938 | |
feac7937 VS |
939 | if (image.HasMask()) |
940 | { | |
941 | GdkGC *mask_gc = gdk_gc_new( GetMask()->GetBitmap() ); | |
b5f01ae0 | 942 | |
feac7937 | 943 | gdk_draw_image( GetMask()->GetBitmap(), mask_gc, mask_image, 0, 0, 0, 0, width, height ); |
b5f01ae0 | 944 | |
feac7937 VS |
945 | gdk_image_destroy( mask_image ); |
946 | gdk_gc_unref( mask_gc ); | |
b5f01ae0 VS |
947 | } |
948 | ||
feac7937 | 949 | return true; |
b5f01ae0 VS |
950 | } |
951 | ||
feac7937 VS |
952 | wxImage wxBitmap::ConvertToImage() const |
953 | { | |
954 | wxImage image; | |
c2fa61e8 | 955 | |
a1b806b9 | 956 | wxCHECK_MSG( IsOk(), wxNullImage, wxT("invalid bitmap") ); |
902725ee | 957 | |
feac7937 VS |
958 | image.Create(GetWidth(), GetHeight()); |
959 | unsigned char *data = image.GetData(); | |
2eefae6e | 960 | |
b5f01ae0 VS |
961 | if (!data) |
962 | { | |
b5f01ae0 VS |
963 | wxFAIL_MSG( wxT("couldn't create image") ); |
964 | return wxNullImage; | |
965 | } | |
966 | ||
3cbab641 MR |
967 | // the colour used as transparent one in wxImage and the one it is |
968 | // replaced with when it really occurs in the bitmap | |
969 | static const int MASK_RED = 1; | |
970 | static const int MASK_GREEN = 2; | |
971 | static const int MASK_BLUE = 3; | |
972 | static const int MASK_BLUE_REPLACEMENT = 2; | |
b5f01ae0 | 973 | |
d3b9f782 | 974 | GdkImage *gdk_image = NULL; |
b5f01ae0 | 975 | |
3cbab641 MR |
976 | if (HasPixmap()) |
977 | { | |
978 | gdk_image = gdk_image_get( GetPixmap(), | |
979 | 0, 0, | |
980 | GetWidth(), GetHeight() ); | |
981 | } | |
982 | else if (GetBitmap()) | |
983 | { | |
984 | gdk_image = gdk_image_get( GetBitmap(), | |
985 | 0, 0, | |
986 | GetWidth(), GetHeight() ); | |
b5f01ae0 | 987 | } |
feac7937 | 988 | else |
b5f01ae0 | 989 | { |
3cbab641 MR |
990 | wxFAIL_MSG( wxT("Ill-formed bitmap") ); |
991 | } | |
b5f01ae0 | 992 | |
3cbab641 | 993 | wxCHECK_MSG( gdk_image, wxNullImage, wxT("couldn't create image") ); |
feac7937 | 994 | |
d3b9f782 | 995 | GdkImage *gdk_image_mask = NULL; |
3cbab641 MR |
996 | if (GetMask()) |
997 | { | |
998 | gdk_image_mask = gdk_image_get( GetMask()->GetBitmap(), | |
999 | 0, 0, | |
1000 | GetWidth(), GetHeight() ); | |
feac7937 | 1001 | |
3cbab641 MR |
1002 | image.SetMaskColour( MASK_RED, MASK_GREEN, MASK_BLUE ); |
1003 | } | |
feac7937 | 1004 | |
3cbab641 MR |
1005 | int bpp = -1; |
1006 | int red_shift_right = 0; | |
1007 | int green_shift_right = 0; | |
1008 | int blue_shift_right = 0; | |
1009 | int red_shift_left = 0; | |
1010 | int green_shift_left = 0; | |
1011 | int blue_shift_left = 0; | |
1012 | bool use_shift = false; | |
feac7937 | 1013 | |
3cbab641 MR |
1014 | if (GetPixmap()) |
1015 | { | |
1016 | GdkVisual *visual = gdk_window_get_visual( GetPixmap() ); | |
1017 | if (visual == NULL) | |
1018 | visual = wxTheApp->GetGdkVisual(); | |
1019 | ||
1020 | bpp = visual->depth; | |
1021 | if (bpp == 16) | |
1022 | bpp = visual->red_prec + visual->green_prec + visual->blue_prec; | |
1023 | red_shift_right = visual->red_shift; | |
1024 | red_shift_left = 8-visual->red_prec; | |
1025 | green_shift_right = visual->green_shift; | |
1026 | green_shift_left = 8-visual->green_prec; | |
1027 | blue_shift_right = visual->blue_shift; | |
1028 | blue_shift_left = 8-visual->blue_prec; | |
1029 | ||
1030 | use_shift = (visual->type == GDK_VISUAL_TRUE_COLOR) || (visual->type == GDK_VISUAL_DIRECT_COLOR); | |
1031 | } | |
1032 | if (GetBitmap()) | |
1033 | { | |
1034 | bpp = 1; | |
1035 | } | |
feac7937 VS |
1036 | |
1037 | ||
3cbab641 | 1038 | GdkColormap *cmap = gtk_widget_get_default_colormap(); |
feac7937 | 1039 | |
3cbab641 MR |
1040 | long pos = 0; |
1041 | for (int j = 0; j < GetHeight(); j++) | |
1042 | { | |
1043 | for (int i = 0; i < GetWidth(); i++) | |
feac7937 | 1044 | { |
3cbab641 MR |
1045 | wxUint32 pixel = gdk_image_get_pixel( gdk_image, i, j ); |
1046 | if (bpp == 1) | |
8ab696e0 | 1047 | { |
3cbab641 | 1048 | if (pixel == 0) |
8ab696e0 | 1049 | { |
3cbab641 MR |
1050 | data[pos] = 0; |
1051 | data[pos+1] = 0; | |
1052 | data[pos+2] = 0; | |
2eefae6e | 1053 | } |
feac7937 | 1054 | else |
2eefae6e | 1055 | { |
3cbab641 MR |
1056 | data[pos] = 255; |
1057 | data[pos+1] = 255; | |
1058 | data[pos+2] = 255; | |
b5f01ae0 | 1059 | } |
3cbab641 MR |
1060 | } |
1061 | else if (use_shift) | |
1062 | { | |
1063 | data[pos] = (pixel >> red_shift_right) << red_shift_left; | |
1064 | data[pos+1] = (pixel >> green_shift_right) << green_shift_left; | |
1065 | data[pos+2] = (pixel >> blue_shift_right) << blue_shift_left; | |
1066 | } | |
1067 | else if (cmap->colors) | |
1068 | { | |
1069 | data[pos] = cmap->colors[pixel].red >> 8; | |
1070 | data[pos+1] = cmap->colors[pixel].green >> 8; | |
1071 | data[pos+2] = cmap->colors[pixel].blue >> 8; | |
1072 | } | |
1073 | else | |
1074 | { | |
1075 | wxFAIL_MSG( wxT("Image conversion failed. Unknown visual type.") ); | |
1076 | } | |
b5f01ae0 | 1077 | |
3cbab641 MR |
1078 | if (gdk_image_mask) |
1079 | { | |
1080 | int mask_pixel = gdk_image_get_pixel( gdk_image_mask, i, j ); | |
1081 | if (mask_pixel == 0) | |
feac7937 | 1082 | { |
3cbab641 MR |
1083 | data[pos] = MASK_RED; |
1084 | data[pos+1] = MASK_GREEN; | |
1085 | data[pos+2] = MASK_BLUE; | |
1086 | } | |
1087 | else if ( data[pos] == MASK_RED && | |
1088 | data[pos+1] == MASK_GREEN && | |
1089 | data[pos+2] == MASK_BLUE ) | |
1090 | { | |
1091 | data[pos+2] = MASK_BLUE_REPLACEMENT; | |
feac7937 | 1092 | } |
feac7937 | 1093 | } |
b5f01ae0 | 1094 | |
3cbab641 MR |
1095 | pos += 3; |
1096 | } | |
feac7937 | 1097 | } |
b5f01ae0 | 1098 | |
3cbab641 MR |
1099 | gdk_image_destroy( gdk_image ); |
1100 | if (gdk_image_mask) gdk_image_destroy( gdk_image_mask ); | |
1101 | ||
b5f01ae0 VS |
1102 | return image; |
1103 | } | |
1104 | ||
4611dd06 | 1105 | wxBitmap::wxBitmap( const wxString &filename, wxBitmapType type ) |
c801d85f | 1106 | { |
fd0eed64 | 1107 | LoadFile( filename, type ); |
ff7b1510 | 1108 | } |
c801d85f | 1109 | |
debe6624 | 1110 | wxBitmap::wxBitmap( const char bits[], int width, int height, int WXUNUSED(depth)) |
6f65e337 | 1111 | { |
3ebcd89d VZ |
1112 | if ( width > 0 && height > 0 ) |
1113 | { | |
1114 | m_refData = new wxBitmapRefData(); | |
6f65e337 | 1115 | |
d3b9f782 | 1116 | M_BMPDATA->m_mask = NULL; |
3ebcd89d VZ |
1117 | M_BMPDATA->m_bitmap = gdk_bitmap_create_from_data |
1118 | ( | |
1119 | wxGetRootWindow()->window, | |
1120 | (gchar *) bits, | |
1121 | width, | |
1122 | height | |
1123 | ); | |
1124 | M_BMPDATA->m_width = width; | |
1125 | M_BMPDATA->m_height = height; | |
1126 | M_BMPDATA->m_bpp = 1; | |
6f65e337 | 1127 | |
3ebcd89d VZ |
1128 | wxASSERT_MSG( M_BMPDATA->m_bitmap, wxT("couldn't create bitmap") ); |
1129 | } | |
6f65e337 | 1130 | } |
8bbe427f VZ |
1131 | |
1132 | wxBitmap::~wxBitmap() | |
c801d85f | 1133 | { |
ff7b1510 | 1134 | } |
8bbe427f | 1135 | |
91b8de8d | 1136 | int wxBitmap::GetHeight() const |
c801d85f | 1137 | { |
a1b806b9 | 1138 | wxCHECK_MSG( IsOk(), -1, wxT("invalid bitmap") ); |
e55ad60e | 1139 | |
fd0eed64 | 1140 | return M_BMPDATA->m_height; |
ff7b1510 | 1141 | } |
c801d85f | 1142 | |
91b8de8d | 1143 | int wxBitmap::GetWidth() const |
c801d85f | 1144 | { |
a1b806b9 | 1145 | wxCHECK_MSG( IsOk(), -1, wxT("invalid bitmap") ); |
8bbe427f | 1146 | |
fd0eed64 | 1147 | return M_BMPDATA->m_width; |
ff7b1510 | 1148 | } |
c801d85f | 1149 | |
91b8de8d | 1150 | int wxBitmap::GetDepth() const |
c801d85f | 1151 | { |
a1b806b9 | 1152 | wxCHECK_MSG( IsOk(), -1, wxT("invalid bitmap") ); |
8bbe427f | 1153 | |
fd0eed64 | 1154 | return M_BMPDATA->m_bpp; |
ff7b1510 | 1155 | } |
c801d85f | 1156 | |
91b8de8d | 1157 | wxMask *wxBitmap::GetMask() const |
c801d85f | 1158 | { |
a1b806b9 | 1159 | wxCHECK_MSG( IsOk(), NULL, wxT("invalid bitmap") ); |
8bbe427f | 1160 | |
fd0eed64 | 1161 | return M_BMPDATA->m_mask; |
ff7b1510 | 1162 | } |
c801d85f KB |
1163 | |
1164 | void wxBitmap::SetMask( wxMask *mask ) | |
1165 | { | |
a1b806b9 | 1166 | wxCHECK_RET( IsOk(), wxT("invalid bitmap") ); |
8bbe427f | 1167 | |
55ccdb93 | 1168 | AllocExclusive(); |
fd0eed64 | 1169 | if (M_BMPDATA->m_mask) delete M_BMPDATA->m_mask; |
8bbe427f | 1170 | |
fd0eed64 | 1171 | M_BMPDATA->m_mask = mask; |
ff7b1510 | 1172 | } |
c801d85f | 1173 | |
db0aec83 VS |
1174 | bool wxBitmap::CopyFromIcon(const wxIcon& icon) |
1175 | { | |
1176 | *this = icon; | |
902725ee | 1177 | return true; |
db0aec83 VS |
1178 | } |
1179 | ||
17bec151 RR |
1180 | wxBitmap wxBitmap::GetSubBitmap( const wxRect& rect) const |
1181 | { | |
a1b806b9 | 1182 | wxCHECK_MSG( IsOk() && |
13111b2a VZ |
1183 | (rect.x >= 0) && (rect.y >= 0) && |
1184 | (rect.x+rect.width <= M_BMPDATA->m_width) && (rect.y+rect.height <= M_BMPDATA->m_height), | |
17bec151 | 1185 | wxNullBitmap, wxT("invalid bitmap or bitmap region") ); |
13111b2a | 1186 | |
17bec151 | 1187 | wxBitmap ret( rect.width, rect.height, M_BMPDATA->m_bpp ); |
a1b806b9 | 1188 | wxASSERT_MSG( ret.IsOk(), wxT("GetSubBitmap error") ); |
13111b2a | 1189 | |
3cbab641 | 1190 | if (ret.GetPixmap()) |
17bec151 | 1191 | { |
3cbab641 MR |
1192 | GdkGC *gc = gdk_gc_new( ret.GetPixmap() ); |
1193 | gdk_draw_pixmap( ret.GetPixmap(), gc, GetPixmap(), rect.x, rect.y, 0, 0, rect.width, rect.height ); | |
1194 | gdk_gc_destroy( gc ); | |
17bec151 RR |
1195 | } |
1196 | else | |
1197 | { | |
3cbab641 MR |
1198 | GdkGC *gc = gdk_gc_new( ret.GetBitmap() ); |
1199 | GdkColor col; | |
1200 | col.pixel = 0xFFFFFF; | |
1201 | gdk_gc_set_foreground( gc, &col ); | |
1202 | col.pixel = 0; | |
1203 | gdk_gc_set_background( gc, &col ); | |
1204 | gdk_wx_draw_bitmap( ret.GetBitmap(), gc, GetBitmap(), rect.x, rect.y, 0, 0, rect.width, rect.height ); | |
1205 | gdk_gc_destroy( gc ); | |
17bec151 | 1206 | } |
13111b2a | 1207 | |
17bec151 RR |
1208 | if (GetMask()) |
1209 | { | |
1210 | wxMask *mask = new wxMask; | |
c2fa61e8 | 1211 | mask->m_bitmap = gdk_pixmap_new( wxGetRootWindow()->window, rect.width, rect.height, 1 ); |
13111b2a | 1212 | |
17bec151 | 1213 | GdkGC *gc = gdk_gc_new( mask->m_bitmap ); |
f9ebac93 RR |
1214 | GdkColor col; |
1215 | col.pixel = 0xFFFFFF; | |
1216 | gdk_gc_set_foreground( gc, &col ); | |
1217 | col.pixel = 0; | |
1218 | gdk_gc_set_background( gc, &col ); | |
1219 | gdk_wx_draw_bitmap( mask->m_bitmap, gc, M_BMPDATA->m_mask->m_bitmap, rect.x, rect.y, 0, 0, rect.width, rect.height ); | |
13111b2a VZ |
1220 | gdk_gc_destroy( gc ); |
1221 | ||
1222 | ret.SetMask( mask ); | |
17bec151 | 1223 | } |
13111b2a | 1224 | |
17bec151 RR |
1225 | return ret; |
1226 | } | |
1227 | ||
4611dd06 | 1228 | bool wxBitmap::SaveFile( const wxString &name, wxBitmapType type, const wxPalette *WXUNUSED(palette) ) const |
c801d85f | 1229 | { |
a1b806b9 | 1230 | wxCHECK_MSG( IsOk(), false, wxT("invalid bitmap") ); |
8bbe427f | 1231 | |
b75dd496 | 1232 | // Try to save the bitmap via wxImage handlers: |
fd0eed64 | 1233 | { |
368d59f0 | 1234 | wxImage image = ConvertToImage(); |
a1b806b9 | 1235 | if (image.IsOk()) return image.SaveFile( name, type ); |
fd0eed64 | 1236 | } |
8bbe427f | 1237 | |
902725ee | 1238 | return false; |
ff7b1510 | 1239 | } |
c801d85f | 1240 | |
4611dd06 | 1241 | bool wxBitmap::LoadFile( const wxString &name, wxBitmapType type ) |
c801d85f | 1242 | { |
fd0eed64 | 1243 | UnRef(); |
8bbe427f | 1244 | |
3ebcd89d | 1245 | if (!wxFileExists(name)) |
902725ee | 1246 | return false; |
8bbe427f | 1247 | |
005f5d18 | 1248 | GdkVisual *visual = wxTheApp->GetGdkVisual(); |
c2fa61e8 | 1249 | |
fd0eed64 RR |
1250 | if (type == wxBITMAP_TYPE_XPM) |
1251 | { | |
1252 | m_refData = new wxBitmapRefData(); | |
8bbe427f | 1253 | |
d3b9f782 | 1254 | GdkBitmap *mask = NULL; |
8bbe427f | 1255 | |
3ebcd89d VZ |
1256 | M_BMPDATA->m_pixmap = gdk_pixmap_create_from_xpm |
1257 | ( | |
1258 | wxGetRootWindow()->window, | |
1259 | &mask, | |
1260 | NULL, | |
1261 | name.fn_str() | |
1262 | ); | |
8bbe427f | 1263 | |
fd0eed64 RR |
1264 | if (mask) |
1265 | { | |
1266 | M_BMPDATA->m_mask = new wxMask(); | |
1267 | M_BMPDATA->m_mask->m_bitmap = mask; | |
1268 | } | |
8bbe427f | 1269 | |
fd0eed64 | 1270 | gdk_window_get_size( M_BMPDATA->m_pixmap, &(M_BMPDATA->m_width), &(M_BMPDATA->m_height) ); |
c2fa61e8 | 1271 | |
103aab26 | 1272 | M_BMPDATA->m_bpp = visual->depth; |
fd0eed64 | 1273 | } |
b75dd496 | 1274 | else // try if wxImage can load it |
fd0eed64 RR |
1275 | { |
1276 | wxImage image; | |
a1b806b9 | 1277 | if ( !image.LoadFile( name, type ) || !image.IsOk() ) |
902725ee | 1278 | return false; |
3ebcd89d VZ |
1279 | |
1280 | *this = wxBitmap(image); | |
fd0eed64 | 1281 | } |
8bbe427f | 1282 | |
902725ee | 1283 | return true; |
ff7b1510 | 1284 | } |
8bbe427f | 1285 | |
0b04c4e0 | 1286 | #if wxUSE_PALETTE |
91b8de8d | 1287 | wxPalette *wxBitmap::GetPalette() const |
c801d85f | 1288 | { |
a1b806b9 | 1289 | if (!IsOk()) |
d3b9f782 | 1290 | return NULL; |
8bbe427f | 1291 | |
fd0eed64 | 1292 | return M_BMPDATA->m_palette; |
ff7b1510 | 1293 | } |
c801d85f | 1294 | |
4611dd06 RR |
1295 | void wxBitmap::SetPalette(const wxPalette& WXUNUSED(palette)) |
1296 | { | |
1297 | // TODO | |
1298 | } | |
0b04c4e0 | 1299 | #endif // wxUSE_PALETTE |
4611dd06 | 1300 | |
4bc67cc5 RR |
1301 | void wxBitmap::SetHeight( int height ) |
1302 | { | |
55ccdb93 | 1303 | AllocExclusive(); |
4bc67cc5 RR |
1304 | M_BMPDATA->m_height = height; |
1305 | } | |
1306 | ||
1307 | void wxBitmap::SetWidth( int width ) | |
1308 | { | |
55ccdb93 | 1309 | AllocExclusive(); |
4bc67cc5 RR |
1310 | M_BMPDATA->m_width = width; |
1311 | } | |
1312 | ||
1313 | void wxBitmap::SetDepth( int depth ) | |
1314 | { | |
55ccdb93 | 1315 | AllocExclusive(); |
4bc67cc5 RR |
1316 | M_BMPDATA->m_bpp = depth; |
1317 | } | |
1318 | ||
1319 | void wxBitmap::SetPixmap( GdkPixmap *pixmap ) | |
1320 | { | |
3ebcd89d VZ |
1321 | if (!m_refData) |
1322 | m_refData = new wxBitmapRefData(); | |
4bc67cc5 RR |
1323 | |
1324 | M_BMPDATA->m_pixmap = pixmap; | |
1325 | } | |
1326 | ||
82ea63e6 RR |
1327 | void wxBitmap::SetBitmap( GdkPixmap *bitmap ) |
1328 | { | |
3ebcd89d VZ |
1329 | if (!m_refData) |
1330 | m_refData = new wxBitmapRefData(); | |
82ea63e6 RR |
1331 | |
1332 | M_BMPDATA->m_bitmap = bitmap; | |
1333 | } | |
1334 | ||
91b8de8d | 1335 | GdkPixmap *wxBitmap::GetPixmap() const |
c801d85f | 1336 | { |
a1b806b9 | 1337 | wxCHECK_MSG( IsOk(), NULL, wxT("invalid bitmap") ); |
8bbe427f | 1338 | |
fd0eed64 | 1339 | return M_BMPDATA->m_pixmap; |
ff7b1510 | 1340 | } |
8bbe427f | 1341 | |
feac7937 VS |
1342 | bool wxBitmap::HasPixmap() const |
1343 | { | |
a1b806b9 | 1344 | wxCHECK_MSG( IsOk(), false, wxT("invalid bitmap") ); |
feac7937 VS |
1345 | |
1346 | return M_BMPDATA->m_pixmap != NULL; | |
1347 | } | |
1348 | ||
91b8de8d | 1349 | GdkBitmap *wxBitmap::GetBitmap() const |
6f65e337 | 1350 | { |
a1b806b9 | 1351 | wxCHECK_MSG( IsOk(), NULL, wxT("invalid bitmap") ); |
8bbe427f | 1352 | |
fd0eed64 | 1353 | return M_BMPDATA->m_bitmap; |
ff7b1510 | 1354 | } |
feac7937 | 1355 | |
89954433 | 1356 | void *wxBitmap::GetRawData(wxPixelDataBase& WXUNUSED(data), int WXUNUSED(bpp)) |
284f2b59 | 1357 | { |
284f2b59 | 1358 | return NULL; |
284f2b59 RR |
1359 | } |
1360 | ||
17a1ebd1 | 1361 | void wxBitmap::UngetRawData(wxPixelDataBase& WXUNUSED(data)) |
284f2b59 RR |
1362 | { |
1363 | } | |
1364 | ||
902725ee | 1365 | bool wxBitmap::HasAlpha() const |
0ff2a74d | 1366 | { |
0ff2a74d | 1367 | return false; |
0ff2a74d RR |
1368 | } |
1369 | ||
4b61c88d RR |
1370 | /* static */ void wxBitmap::InitStandardHandlers() |
1371 | { | |
1372 | // TODO: Insert handler based on GdkPixbufs handler later | |
1373 | } |