]>
Commit | Line | Data |
---|---|---|
83df96d6 | 1 | ///////////////////////////////////////////////////////////////////////////// |
55034339 | 2 | // Name: src/x11/bitmap.cpp |
83df96d6 | 3 | // Purpose: wxBitmap |
a11672a4 | 4 | // Author: Julian Smart, Robert Roebling |
83df96d6 JS |
5 | // Modified by: |
6 | // Created: 17/09/98 | |
a11672a4 | 7 | // Copyright: (c) Julian Smart, Robert Roebling |
65571936 | 8 | // Licence: wxWindows licence |
83df96d6 JS |
9 | ///////////////////////////////////////////////////////////////////////////// |
10 | ||
55034339 WS |
11 | // for compilers that support precompilation, includes "wx.h". |
12 | #include "wx/wxprec.h" | |
13 | ||
83df96d6 | 14 | #include "wx/bitmap.h" |
e4db172a WS |
15 | |
16 | #ifndef WX_PRECOMP | |
17 | #include "wx/log.h" | |
670f9935 | 18 | #include "wx/app.h" |
f38924e8 | 19 | #include "wx/dcmemory.h" |
923d28da | 20 | #include "wx/icon.h" |
18680f86 | 21 | #include "wx/math.h" |
155ecd4c | 22 | #include "wx/image.h" |
e4db172a WS |
23 | #endif |
24 | ||
bc797f4c | 25 | #include "wx/x11/private.h" |
83df96d6 | 26 | |
c79a329d JS |
27 | /* No point in using libXPM for NanoX */ |
28 | #if wxUSE_NANOX | |
29 | #undef wxHAVE_LIB_XPM | |
30 | #define wxHAVE_LIB_XPM 0 | |
a20c04ab JS |
31 | |
32 | // Copy from the drawable to the wxImage | |
33 | bool wxGetImageFromDrawable(GR_DRAW_ID drawable, int srcX, int srcY, int width, int height, wxImage& image); | |
c79a329d JS |
34 | #endif |
35 | ||
04a8da5f VZ |
36 | static WXPixmap wxGetSubPixmap( WXDisplay* xdisplay, WXPixmap xpixmap, |
37 | int x, int y, int width, int height, | |
38 | int depth ); | |
39 | ||
707440dc | 40 | #if wxUSE_XPM |
83df96d6 | 41 | #if wxHAVE_LIB_XPM |
707440dc JS |
42 | #include <X11/xpm.h> |
43 | #else | |
44 | #include "wx/xpmdecod.h" | |
45 | #include "wx/wfstream.h" | |
46 | #endif | |
83df96d6 | 47 | #endif |
83df96d6 | 48 | |
a11672a4 RR |
49 | //----------------------------------------------------------------------------- |
50 | // wxMask | |
51 | //----------------------------------------------------------------------------- | |
83df96d6 | 52 | |
a11672a4 | 53 | IMPLEMENT_DYNAMIC_CLASS(wxMask,wxObject) |
83df96d6 | 54 | |
a11672a4 | 55 | wxMask::wxMask() |
83df96d6 | 56 | { |
a11672a4 RR |
57 | m_bitmap = NULL; |
58 | m_display = NULL; | |
83df96d6 JS |
59 | } |
60 | ||
04a8da5f VZ |
61 | wxMask::wxMask(const wxMask& mask) |
62 | { | |
63 | m_display = mask.m_display; | |
64 | if ( !mask.m_bitmap ) | |
65 | { | |
66 | m_bitmap = NULL; | |
67 | return; | |
68 | } | |
69 | ||
70 | m_size = mask.m_size; | |
71 | ||
72 | // Duplicate the mask bitmap using the existing wxGetSubPixmap() function. | |
73 | // There are probably/surely better ways to do it. | |
74 | m_bitmap = wxGetSubPixmap(m_display, mask.m_bitmap, | |
75 | 0, 0, m_size.x, m_size.y, | |
76 | 1); | |
77 | } | |
78 | ||
a11672a4 | 79 | wxMask::wxMask( const wxBitmap& bitmap, const wxColour& colour ) |
83df96d6 | 80 | { |
a11672a4 RR |
81 | m_bitmap = NULL; |
82 | Create( bitmap, colour ); | |
83df96d6 JS |
83 | } |
84 | ||
a11672a4 | 85 | wxMask::wxMask( const wxBitmap& bitmap, int paletteIndex ) |
83df96d6 | 86 | { |
a11672a4 RR |
87 | m_bitmap = NULL; |
88 | Create( bitmap, paletteIndex ); | |
83df96d6 JS |
89 | } |
90 | ||
a11672a4 | 91 | wxMask::wxMask( const wxBitmap& bitmap ) |
83df96d6 | 92 | { |
a11672a4 RR |
93 | m_bitmap = NULL; |
94 | Create( bitmap ); | |
83df96d6 JS |
95 | } |
96 | ||
a11672a4 | 97 | wxMask::~wxMask() |
83df96d6 | 98 | { |
a11672a4 RR |
99 | if (m_bitmap) |
100 | XFreePixmap( (Display*) m_display, (Pixmap) m_bitmap ); | |
83df96d6 JS |
101 | } |
102 | ||
a11672a4 RR |
103 | bool wxMask::Create( const wxBitmap& bitmap, |
104 | const wxColour& colour ) | |
83df96d6 | 105 | { |
04a8da5f VZ |
106 | m_size = bitmap.GetSize(); |
107 | ||
c79a329d | 108 | #if !wxUSE_NANOX |
a11672a4 RR |
109 | if (m_bitmap) |
110 | { | |
111 | XFreePixmap( (Display*) m_display, (Pixmap) m_bitmap ); | |
112 | m_bitmap = NULL; | |
113 | } | |
83df96d6 | 114 | |
a11672a4 | 115 | m_display = bitmap.GetDisplay(); |
83df96d6 | 116 | |
2b5f62a0 | 117 | wxImage image = bitmap.ConvertToImage(); |
a1b806b9 | 118 | if (!image.IsOk()) return false; |
0cad49b4 | 119 | |
a11672a4 | 120 | m_display = bitmap.GetDisplay(); |
0cad49b4 | 121 | |
a11672a4 | 122 | Display *xdisplay = (Display*) m_display; |
a11672a4 RR |
123 | int xscreen = DefaultScreen( xdisplay ); |
124 | Window xroot = RootWindow( xdisplay, xscreen ); | |
0cad49b4 | 125 | |
a11672a4 RR |
126 | m_bitmap = (WXPixmap) XCreatePixmap( xdisplay, xroot, image.GetWidth(), image.GetHeight(), 1 ); |
127 | GC gc = XCreateGC( xdisplay, (Pixmap) m_bitmap, 0, NULL ); | |
83df96d6 | 128 | |
a11672a4 RR |
129 | XSetForeground( xdisplay, gc, WhitePixel(xdisplay,xscreen) ); |
130 | XSetFillStyle( xdisplay, gc, FillSolid ); | |
131 | XFillRectangle( xdisplay, (Pixmap) m_bitmap, gc, 0, 0, image.GetWidth(), image.GetHeight() ); | |
83df96d6 | 132 | |
a11672a4 RR |
133 | unsigned char *data = image.GetData(); |
134 | int index = 0; | |
83df96d6 | 135 | |
a11672a4 RR |
136 | unsigned char red = colour.Red(); |
137 | unsigned char green = colour.Green(); | |
138 | unsigned char blue = colour.Blue(); | |
83df96d6 | 139 | |
9ce8d6a2 | 140 | int bpp = wxTheApp->GetVisualInfo(m_display)->m_visualDepth; |
0cad49b4 | 141 | |
a11672a4 RR |
142 | if (bpp == 15) |
143 | { | |
55034339 WS |
144 | red &= 0xf8; |
145 | green &= 0xf8; | |
146 | blue &= 0xf8; | |
a11672a4 RR |
147 | } else |
148 | if (bpp == 16) | |
149 | { | |
55034339 WS |
150 | red &= 0xf8; |
151 | green &= 0xfc; | |
152 | blue &= 0xf8; | |
a11672a4 RR |
153 | } else |
154 | if (bpp == 12) | |
155 | { | |
55034339 WS |
156 | red &= 0xf0; |
157 | green &= 0xf0; | |
158 | blue &= 0xf0; | |
a11672a4 | 159 | } |
83df96d6 | 160 | |
a11672a4 | 161 | XSetForeground( xdisplay, gc, BlackPixel(xdisplay,xscreen) ); |
83df96d6 | 162 | |
288efe84 JS |
163 | int width = image.GetWidth(); |
164 | int height = image.GetHeight(); | |
165 | for (int j = 0; j < height; j++) | |
a11672a4 RR |
166 | { |
167 | int start_x = -1; | |
168 | int i; | |
288efe84 | 169 | for (i = 0; i < width; i++) |
83df96d6 | 170 | { |
a11672a4 RR |
171 | if ((data[index] == red) && |
172 | (data[index+1] == green) && | |
173 | (data[index+2] == blue)) | |
174 | { | |
175 | if (start_x == -1) | |
176 | start_x = i; | |
177 | } | |
178 | else | |
179 | { | |
180 | if (start_x != -1) | |
181 | { | |
182 | XDrawLine( xdisplay, (Pixmap) m_bitmap, gc, start_x, j, i-1, j ); | |
183 | start_x = -1; | |
184 | } | |
185 | } | |
186 | index += 3; | |
83df96d6 | 187 | } |
a11672a4 RR |
188 | if (start_x != -1) |
189 | XDrawLine( xdisplay, (Pixmap) m_bitmap, gc, start_x, j, i, j ); | |
83df96d6 | 190 | } |
0cad49b4 | 191 | |
a11672a4 | 192 | XFreeGC( xdisplay, gc ); |
83df96d6 | 193 | |
55034339 | 194 | return true; |
c79a329d | 195 | #else |
55034339 | 196 | return false; |
c79a329d JS |
197 | #endif |
198 | // wxUSE_NANOX | |
83df96d6 JS |
199 | } |
200 | ||
a11672a4 | 201 | bool wxMask::Create( const wxBitmap& bitmap, int paletteIndex ) |
83df96d6 | 202 | { |
a11672a4 RR |
203 | unsigned char r,g,b; |
204 | wxPalette *pal = bitmap.GetPalette(); | |
83df96d6 | 205 | |
55034339 | 206 | wxCHECK_MSG( pal, false, wxT("Cannot create mask from bitmap without palette") ); |
83df96d6 | 207 | |
a11672a4 | 208 | pal->GetRGB(paletteIndex, &r, &g, &b); |
83df96d6 | 209 | |
a11672a4 | 210 | return Create(bitmap, wxColour(r, g, b)); |
83df96d6 JS |
211 | } |
212 | ||
a11672a4 | 213 | bool wxMask::Create( const wxBitmap& bitmap ) |
83df96d6 | 214 | { |
c79a329d | 215 | #if !wxUSE_NANOX |
a11672a4 RR |
216 | if (m_bitmap) |
217 | { | |
218 | XFreePixmap( (Display*) m_display, (Pixmap) m_bitmap ); | |
219 | m_bitmap = NULL; | |
83df96d6 JS |
220 | } |
221 | ||
a1b806b9 | 222 | if (!bitmap.IsOk()) return false; |
83df96d6 | 223 | |
55034339 | 224 | wxCHECK_MSG( bitmap.GetBitmap(), false, wxT("Cannot create mask from colour bitmap") ); |
0cad49b4 | 225 | |
a11672a4 | 226 | m_display = bitmap.GetDisplay(); |
83df96d6 | 227 | |
a11672a4 RR |
228 | int xscreen = DefaultScreen( (Display*) m_display ); |
229 | Window xroot = RootWindow( (Display*) m_display, xscreen ); | |
0cad49b4 | 230 | |
a11672a4 | 231 | m_bitmap = (WXPixmap) XCreatePixmap( (Display*) m_display, xroot, bitmap.GetWidth(), bitmap.GetHeight(), 1 ); |
83df96d6 | 232 | |
55034339 | 233 | if (!m_bitmap) return false; |
0cad49b4 | 234 | |
a11672a4 | 235 | GC gc = XCreateGC( (Display*) m_display, (Pixmap) m_bitmap, 0, NULL ); |
83df96d6 | 236 | |
a11672a4 RR |
237 | XCopyPlane( (Display*) m_display, (Pixmap) bitmap.GetBitmap(), (Pixmap) m_bitmap, |
238 | gc, 0, 0, bitmap.GetWidth(), bitmap.GetHeight(), 0, 0, 1 ); | |
0cad49b4 | 239 | |
a11672a4 | 240 | XFreeGC( (Display*) m_display, gc ); |
83df96d6 | 241 | |
55034339 | 242 | return true; |
c79a329d | 243 | #else |
55034339 | 244 | return false; |
c79a329d JS |
245 | #endif |
246 | // wxUSE_NANOX | |
83df96d6 JS |
247 | } |
248 | ||
a11672a4 | 249 | //----------------------------------------------------------------------------- |
732d8c74 | 250 | // wxBitmapRefData |
a11672a4 | 251 | //----------------------------------------------------------------------------- |
83df96d6 | 252 | |
8f884a0d | 253 | class wxBitmapRefData : public wxGDIRefData |
83df96d6 | 254 | { |
a11672a4 RR |
255 | public: |
256 | wxBitmapRefData(); | |
dd21c826 | 257 | wxBitmapRefData(const wxBitmapRefData& data); |
d3c7fc99 | 258 | virtual ~wxBitmapRefData(); |
a11672a4 | 259 | |
dd21c826 VZ |
260 | // shouldn't be called more than once as it doesn't free the existing data |
261 | bool Create(int width, int height, int depth); | |
262 | ||
8f884a0d VZ |
263 | virtual bool IsOk() const { return m_pixmap || m_bitmap; } |
264 | ||
dd21c826 VZ |
265 | Pixmap m_pixmap; |
266 | Pixmap m_bitmap; | |
267 | Display *m_display; | |
a11672a4 RR |
268 | wxMask *m_mask; |
269 | int m_width; | |
270 | int m_height; | |
271 | int m_bpp; | |
272 | wxPalette *m_palette; | |
273 | }; | |
83df96d6 | 274 | |
a11672a4 | 275 | wxBitmapRefData::wxBitmapRefData() |
83df96d6 | 276 | { |
dd21c826 VZ |
277 | m_pixmap = 0; |
278 | m_bitmap = 0; | |
a11672a4 | 279 | m_display = NULL; |
dd21c826 | 280 | m_mask = NULL; |
a11672a4 RR |
281 | m_width = 0; |
282 | m_height = 0; | |
283 | m_bpp = 0; | |
d3b9f782 | 284 | m_palette = NULL; |
83df96d6 JS |
285 | } |
286 | ||
dd21c826 VZ |
287 | wxBitmapRefData::wxBitmapRefData(const wxBitmapRefData& data) |
288 | { | |
289 | m_pixmap = 0; | |
290 | m_bitmap = 0; | |
291 | m_display = data.m_display; | |
292 | m_mask = NULL; // FIXME: should copy | |
293 | m_palette = NULL; // FIXME: should copy | |
294 | ||
295 | Create(data.m_width, data.m_height, data.m_bpp); | |
296 | } | |
297 | ||
298 | bool wxBitmapRefData::Create(int width, int height, int depth) | |
299 | { | |
300 | m_width = width; | |
301 | m_height = height; | |
302 | m_bpp = depth; | |
303 | ||
304 | m_display = wxGlobalDisplay(); | |
305 | ||
306 | wxCHECK_MSG( m_display, false, wxT("No display") ); | |
307 | ||
308 | int xscreen = DefaultScreen(m_display); | |
309 | int bpp = DefaultDepth(m_display, xscreen); | |
310 | if ( depth == -1 ) | |
311 | depth = bpp; | |
312 | ||
313 | wxCHECK_MSG( (depth == bpp) || (depth == 1), false, | |
314 | wxT("invalid bitmap depth") ); | |
315 | ||
316 | #if wxUSE_NANOX | |
317 | m_pixmap = (WXPixmap) GrNewPixmap(width, height, NULL); | |
318 | #else // !wxUSE_NANOX | |
319 | Window xroot = RootWindow(m_display, xscreen); | |
320 | ||
03647350 | 321 | *(depth == 1 ? &m_bitmap : &m_pixmap) = |
dd21c826 VZ |
322 | XCreatePixmap(m_display, xroot, width, height, depth); |
323 | #endif // wxUSE_NANOX/!wxUSE_NANOX | |
324 | ||
325 | wxCHECK_MSG( m_pixmap || m_bitmap, false, wxT("Bitmap creation failed") ); | |
326 | ||
327 | return true; | |
328 | } | |
329 | ||
a11672a4 | 330 | wxBitmapRefData::~wxBitmapRefData() |
83df96d6 | 331 | { |
dd21c826 VZ |
332 | if (m_pixmap) |
333 | XFreePixmap(m_display, m_pixmap); | |
334 | if (m_bitmap) | |
335 | XFreePixmap(m_display, m_bitmap); | |
336 | delete m_mask; | |
337 | delete m_palette; | |
83df96d6 JS |
338 | } |
339 | ||
a11672a4 | 340 | //----------------------------------------------------------------------------- |
83df96d6 | 341 | |
62ad3cb3 MB |
342 | // helper function |
343 | ||
344 | static WXPixmap wxGetSubPixmap( WXDisplay* xdisplay, WXPixmap xpixmap, | |
345 | int x, int y, int width, int height, | |
346 | int depth ) | |
347 | { | |
4c620d19 | 348 | Display * const dpy = (Display *)xdisplay; |
62ad3cb3 | 349 | |
4c620d19 VZ |
350 | int xscreen = DefaultScreen( dpy ); |
351 | Window xroot = RootWindow( dpy, xscreen ); | |
352 | Visual* xvisual = DefaultVisual( dpy, xscreen ); | |
353 | ||
354 | XImage* ximage = XCreateImage( dpy, xvisual, depth, | |
62ad3cb3 MB |
355 | ZPixmap, 0, 0, width, height, 32, 0 ); |
356 | ximage->data = (char*)malloc( ximage->bytes_per_line * ximage->height ); | |
4c620d19 | 357 | ximage = XGetSubImage( dpy, (Pixmap)xpixmap, |
0cad49b4 | 358 | x, y, width, height, |
62ad3cb3 MB |
359 | AllPlanes, ZPixmap, ximage, 0, 0 ); |
360 | ||
4c620d19 VZ |
361 | GC gc = XCreateGC( dpy, (Pixmap)xpixmap, 0, NULL ); |
362 | Pixmap ret = XCreatePixmap( dpy, xroot, | |
62ad3cb3 MB |
363 | width, height, depth ); |
364 | ||
4c620d19 | 365 | XPutImage( dpy, ret, gc, ximage, |
62ad3cb3 MB |
366 | 0, 0, 0, 0, width, height ); |
367 | XDestroyImage( ximage ); | |
4c620d19 | 368 | XFreeGC( dpy, gc ); |
62ad3cb3 MB |
369 | |
370 | return (WXPixmap)ret; | |
371 | } | |
372 | ||
83df96d6 | 373 | |
732d8c74 FM |
374 | //----------------------------------------------------------------------------- |
375 | // wxBitmap | |
376 | //----------------------------------------------------------------------------- | |
83df96d6 | 377 | |
732d8c74 | 378 | #define M_BMPDATA ((wxBitmapRefData *)m_refData) |
83df96d6 | 379 | |
732d8c74 | 380 | IMPLEMENT_DYNAMIC_CLASS(wxBitmap,wxGDIObject) |
83df96d6 | 381 | |
a11672a4 | 382 | bool wxBitmap::Create( int width, int height, int depth ) |
83df96d6 | 383 | { |
a11672a4 RR |
384 | UnRef(); |
385 | ||
637b7e4f | 386 | wxCHECK_MSG( (width > 0) && (height > 0), false, wxT("invalid bitmap size") ); |
a11672a4 RR |
387 | |
388 | m_refData = new wxBitmapRefData(); | |
0cad49b4 | 389 | |
dd21c826 | 390 | return M_BMPDATA->Create(width, height, depth); |
83df96d6 JS |
391 | } |
392 | ||
452418c4 | 393 | bool wxBitmap::Create(const void* data, wxBitmapType type, |
62ad3cb3 | 394 | int width, int height, int depth) |
83df96d6 | 395 | { |
a11672a4 RR |
396 | UnRef(); |
397 | ||
62ad3cb3 | 398 | wxBitmapHandler *handler = FindHandler(type); |
a11672a4 | 399 | |
62ad3cb3 | 400 | if ( handler == NULL ) { |
54385bdb | 401 | wxLogWarning(wxT("no data bitmap handler for type %ld defined."), |
62ad3cb3 | 402 | (long)type); |
83df96d6 | 403 | |
55034339 | 404 | return false; |
62ad3cb3 | 405 | } |
83df96d6 | 406 | |
62ad3cb3 MB |
407 | return handler->Create(this, data, type, width, height, depth); |
408 | } | |
83df96d6 | 409 | |
dd38c875 MB |
410 | bool wxBitmap::Create(WXPixmap pixmap) |
411 | { | |
412 | UnRef(); | |
413 | Pixmap xpixmap = (Pixmap)pixmap; | |
414 | Display* xdisplay = wxGlobalDisplay(); | |
415 | int xscreen = DefaultScreen( xdisplay ); | |
416 | Window xroot = RootWindow( xdisplay, xscreen ); | |
417 | ||
418 | // make a copy of the Pixmap | |
419 | Window root; | |
dd38c875 MB |
420 | int x, y; |
421 | unsigned width, height, border, depth; | |
422 | ||
423 | XGetGeometry( xdisplay, (Drawable)xpixmap, &root, &x, &y, | |
424 | &width, &height, &border, &depth ); | |
dd21c826 | 425 | Pixmap copy = XCreatePixmap( xdisplay, xroot, width, height, depth ); |
dd38c875 MB |
426 | |
427 | GC gc = XCreateGC( xdisplay, copy, 0, NULL ); | |
428 | XCopyArea( xdisplay, xpixmap, copy, gc, 0, 0, width, height, 0, 0 ); | |
429 | XFreeGC( xdisplay, gc ); | |
430 | ||
431 | // fill in ref data | |
432 | wxBitmapRefData* ref = new wxBitmapRefData(); | |
433 | ||
434 | if( depth == 1 ) | |
dd21c826 | 435 | ref->m_bitmap = copy; |
dd38c875 | 436 | else |
dd21c826 | 437 | ref->m_pixmap = copy; |
dd38c875 | 438 | |
dd21c826 | 439 | ref->m_display = xdisplay; |
dd38c875 MB |
440 | ref->m_width = width; |
441 | ref->m_height = height; | |
442 | ref->m_bpp = depth; | |
443 | ||
444 | m_refData = ref; | |
445 | ||
446 | return true; | |
447 | } | |
448 | ||
452418c4 | 449 | wxBitmap::wxBitmap(const char* const* bits) |
62ad3cb3 | 450 | { |
452418c4 | 451 | Create(bits, wxBITMAP_TYPE_XPM_DATA, 0, 0, 0); |
83df96d6 JS |
452 | } |
453 | ||
8f884a0d | 454 | wxGDIRefData *wxBitmap::CreateGDIRefData() const |
dd21c826 VZ |
455 | { |
456 | return new wxBitmapRefData; | |
457 | } | |
458 | ||
8f884a0d | 459 | wxGDIRefData *wxBitmap::CloneGDIRefData(const wxGDIRefData *data) const |
dd21c826 | 460 | { |
5c33522f | 461 | return new wxBitmapRefData(*static_cast<const wxBitmapRefData *>(data)); |
dd21c826 VZ |
462 | } |
463 | ||
a11672a4 | 464 | bool wxBitmap::CreateFromImage( const wxImage& image, int depth ) |
83df96d6 | 465 | { |
c79a329d | 466 | #if wxUSE_NANOX |
a1b806b9 | 467 | if (!image.IsOk()) |
c79a329d | 468 | { |
a1b806b9 | 469 | wxASSERT_MSG(image.IsOk(), wxT("Invalid wxImage passed to wxBitmap::CreateFromImage.")); |
55034339 | 470 | return false; |
c79a329d | 471 | } |
0cad49b4 | 472 | |
c79a329d JS |
473 | int w = image.GetWidth(); |
474 | int h = image.GetHeight(); | |
0cad49b4 | 475 | |
c79a329d | 476 | if (!Create(w, h, depth)) |
55034339 | 477 | return false; |
c79a329d | 478 | |
888d3c80 JS |
479 | // Unfortunately the mask has to be screen-depth since |
480 | // 1-bpp bitmaps don't seem to be supported | |
481 | // TODO: implement transparent drawing, presumably | |
482 | // by doing several blits as per the Windows | |
483 | // implementation because Nano-X doesn't support | |
484 | // XSetClipMask. | |
f22033e5 JS |
485 | // TODO: could perhaps speed this function up |
486 | // by making a buffer of pixel values, | |
487 | // and then calling GrArea to write that to the | |
488 | // pixmap. See demos/nxroach.c. | |
c79a329d | 489 | |
888d3c80 | 490 | bool hasMask = image.HasMask(); |
0cad49b4 | 491 | |
888d3c80 JS |
492 | GC pixmapGC = GrNewGC(); |
493 | Pixmap pixmap = (Pixmap) GetPixmap(); | |
494 | ||
495 | GC maskGC = 0; | |
496 | Pixmap maskPixmap = 0; | |
497 | ||
498 | unsigned char maskR = 0; | |
499 | unsigned char maskG = 0; | |
500 | unsigned char maskB = 0; | |
501 | ||
502 | if (hasMask) | |
503 | { | |
504 | maskR = image.GetMaskRed(); | |
505 | maskG = image.GetMaskGreen(); | |
506 | maskB = image.GetMaskBlue(); | |
0cad49b4 | 507 | |
888d3c80 JS |
508 | maskGC = GrNewGC(); |
509 | maskPixmap = GrNewPixmap(w, h, 0); | |
510 | if (!maskPixmap) | |
55034339 | 511 | hasMask = false; |
888d3c80 JS |
512 | else |
513 | { | |
514 | wxMask* mask = new wxMask; | |
515 | mask->SetBitmap((WXPixmap) maskPixmap); | |
516 | SetMask(mask); | |
517 | } | |
518 | } | |
519 | ||
520 | GR_COLOR lastPixmapColour = 0; | |
521 | GR_COLOR lastMaskColour = 0; | |
0cad49b4 | 522 | |
c79a329d JS |
523 | int i, j; |
524 | for (i = 0; i < w; i++) | |
525 | { | |
526 | for (j = 0; j < h; j++) | |
527 | { | |
528 | unsigned char red = image.GetRed(i, j); | |
529 | unsigned char green = image.GetGreen(i, j); | |
530 | unsigned char blue = image.GetBlue(i, j); | |
c79a329d | 531 | |
888d3c80 JS |
532 | GR_COLOR colour = GR_RGB(red, green, blue); |
533 | ||
534 | // Efficiency measure | |
535 | if (colour != lastPixmapColour || (i == 0 && j == 0)) | |
536 | { | |
537 | GrSetGCForeground(pixmapGC, colour); | |
538 | lastPixmapColour = colour; | |
539 | } | |
0cad49b4 | 540 | |
888d3c80 | 541 | GrPoint(pixmap, pixmapGC, i, j); |
0cad49b4 | 542 | |
c79a329d JS |
543 | if (hasMask) |
544 | { | |
545 | // scan the bitmap for the transparent colour and set the corresponding | |
546 | // pixels in the mask to BLACK and the rest to WHITE | |
547 | if (maskR == red && maskG == green && maskB == blue) | |
888d3c80 JS |
548 | { |
549 | colour = GR_RGB(0, 0, 0); | |
550 | } | |
c79a329d | 551 | else |
888d3c80 JS |
552 | { |
553 | colour = GR_RGB(255, 255, 255); | |
554 | } | |
555 | if (colour != lastMaskColour || (i == 0 && j == 0)) | |
556 | { | |
557 | GrSetGCForeground(maskGC, colour); | |
558 | lastMaskColour = colour; | |
559 | } | |
560 | GrPoint(maskPixmap, maskGC, i, j); | |
c79a329d | 561 | } |
c79a329d JS |
562 | } |
563 | } | |
888d3c80 JS |
564 | |
565 | GrDestroyGC(pixmapGC); | |
566 | if (hasMask) | |
567 | GrDestroyGC(maskGC); | |
0cad49b4 | 568 | |
55034339 | 569 | return true; |
c79a329d JS |
570 | #else |
571 | // !wxUSE_NANOX | |
0cad49b4 | 572 | |
a11672a4 | 573 | UnRef(); |
83df96d6 | 574 | |
a1b806b9 | 575 | wxCHECK_MSG( image.IsOk(), false, wxT("invalid image") ); |
637b7e4f | 576 | wxCHECK_MSG( depth == -1, false, wxT("invalid bitmap depth") ); |
83df96d6 | 577 | |
a11672a4 | 578 | m_refData = new wxBitmapRefData(); |
83df96d6 | 579 | |
a11672a4 | 580 | M_BMPDATA->m_display = wxGlobalDisplay(); |
0cad49b4 | 581 | |
a11672a4 | 582 | Display *xdisplay = (Display*) M_BMPDATA->m_display; |
0cad49b4 | 583 | |
a11672a4 RR |
584 | int xscreen = DefaultScreen( xdisplay ); |
585 | Window xroot = RootWindow( xdisplay, xscreen ); | |
586 | Visual* xvisual = DefaultVisual( xdisplay, xscreen ); | |
0cad49b4 | 587 | |
9ce8d6a2 | 588 | int bpp = wxTheApp->GetVisualInfo(M_BMPDATA->m_display)->m_visualDepth; |
0cad49b4 | 589 | |
a11672a4 RR |
590 | int width = image.GetWidth(); |
591 | int height = image.GetHeight(); | |
592 | M_BMPDATA->m_width = width; | |
593 | M_BMPDATA->m_height = height; | |
83df96d6 | 594 | |
a11672a4 RR |
595 | if (depth != 1) depth = bpp; |
596 | M_BMPDATA->m_bpp = depth; | |
0cad49b4 | 597 | |
a11672a4 RR |
598 | if (depth == 1) |
599 | { | |
54385bdb | 600 | wxFAIL_MSG( wxT("mono images later") ); |
a11672a4 RR |
601 | } |
602 | else | |
603 | { | |
604 | // Create image | |
0cad49b4 | 605 | |
a11672a4 RR |
606 | XImage *data_image = XCreateImage( xdisplay, xvisual, bpp, ZPixmap, 0, 0, width, height, 32, 0 ); |
607 | data_image->data = (char*) malloc( data_image->bytes_per_line * data_image->height ); | |
0cad49b4 | 608 | |
a11672a4 RR |
609 | if (data_image->data == NULL) |
610 | { | |
611 | wxLogError( wxT("Out of memory.") ); // TODO clean | |
55034339 | 612 | return false; |
a11672a4 | 613 | } |
83df96d6 | 614 | |
dd21c826 | 615 | M_BMPDATA->m_pixmap = XCreatePixmap( xdisplay, xroot, width, height, depth ); |
83df96d6 | 616 | |
16fc7c45 VZ |
617 | // Create mask if necessary |
618 | const bool hasMask = image.HasMask(); | |
83df96d6 | 619 | |
d3b9f782 | 620 | XImage *mask_image = NULL; |
16fc7c45 | 621 | if ( hasMask ) |
0cad49b4 | 622 | { |
a11672a4 RR |
623 | mask_image = XCreateImage( xdisplay, xvisual, 1, ZPixmap, 0, 0, width, height, 32, 0 ); |
624 | mask_image->data = (char*) malloc( mask_image->bytes_per_line * mask_image->height ); | |
0cad49b4 | 625 | |
a11672a4 RR |
626 | if (mask_image->data == NULL) |
627 | { | |
628 | wxLogError( wxT("Out of memory.") ); // TODO clean | |
55034339 | 629 | return false; |
a11672a4 | 630 | } |
0cad49b4 | 631 | |
a11672a4 RR |
632 | wxMask *mask = new wxMask(); |
633 | mask->SetDisplay( xdisplay ); | |
634 | mask->SetBitmap( (WXPixmap) XCreatePixmap( xdisplay, xroot, width, height, 1 ) ); | |
83df96d6 | 635 | |
a11672a4 RR |
636 | SetMask( mask ); |
637 | } | |
83df96d6 | 638 | |
a11672a4 | 639 | if (bpp < 8) bpp = 8; |
83df96d6 | 640 | |
a11672a4 | 641 | // Render |
83df96d6 | 642 | |
a11672a4 RR |
643 | enum byte_order { RGB, RBG, BRG, BGR, GRB, GBR }; |
644 | byte_order b_o = RGB; | |
83df96d6 | 645 | |
9ce8d6a2 MB |
646 | wxXVisualInfo* vi = wxTheApp->GetVisualInfo(M_BMPDATA->m_display); |
647 | unsigned long greenMask = vi->m_visualGreenMask, | |
648 | redMask = vi->m_visualRedMask, | |
649 | blueMask = vi->m_visualBlueMask; | |
650 | ||
a11672a4 RR |
651 | if (bpp > 8) |
652 | { | |
9ce8d6a2 MB |
653 | if ((redMask > greenMask) && (greenMask > blueMask)) b_o = RGB; |
654 | else if ((redMask > blueMask) && (blueMask > greenMask)) b_o = RBG; | |
655 | else if ((blueMask > redMask) && (redMask > greenMask)) b_o = BRG; | |
656 | else if ((blueMask > greenMask) && (greenMask > redMask))b_o = BGR; | |
657 | else if ((greenMask > redMask) && (redMask > blueMask)) b_o = GRB; | |
658 | else if ((greenMask > blueMask) && (blueMask > redMask)) b_o = GBR; | |
a11672a4 | 659 | } |
83df96d6 | 660 | |
a11672a4 RR |
661 | int r_mask = image.GetMaskRed(); |
662 | int g_mask = image.GetMaskGreen(); | |
663 | int b_mask = image.GetMaskBlue(); | |
83df96d6 | 664 | |
a11672a4 | 665 | unsigned char* data = image.GetData(); |
54385bdb | 666 | wxASSERT_MSG( data, wxT("No image data") ); |
0cad49b4 | 667 | |
9ce8d6a2 MB |
668 | unsigned char *colorCube = |
669 | wxTheApp->GetVisualInfo(M_BMPDATA->m_display)->m_colorCube; | |
83df96d6 | 670 | |
a11672a4 RR |
671 | int index = 0; |
672 | for (int y = 0; y < height; y++) | |
673 | { | |
674 | for (int x = 0; x < width; x++) | |
675 | { | |
676 | int r = data[index]; | |
677 | index++; | |
678 | int g = data[index]; | |
679 | index++; | |
680 | int b = data[index]; | |
681 | index++; | |
682 | ||
683 | if (hasMask) | |
684 | { | |
685 | if ((r == r_mask) && (b == b_mask) && (g == g_mask)) | |
686 | XPutPixel( mask_image, x, y, 0 ); | |
687 | else | |
688 | XPutPixel( mask_image, x, y, 1 ); | |
689 | } | |
83df96d6 | 690 | |
a11672a4 RR |
691 | switch (bpp) |
692 | { | |
693 | case 8: | |
694 | { | |
695 | int pixel = 0; | |
dc4025af | 696 | pixel = colorCube[ ((r & 0xf8) << 7) + ((g & 0xf8) << 2) + ((b & 0xf8) >> 3) ]; |
a11672a4 RR |
697 | XPutPixel( data_image, x, y, pixel ); |
698 | break; | |
699 | } | |
700 | case 12: // SGI only | |
701 | { | |
702 | int pixel = 0; | |
703 | switch (b_o) | |
704 | { | |
705 | case RGB: pixel = ((r & 0xf0) << 4) | (g & 0xf0) | ((b & 0xf0) >> 4); break; | |
706 | case RBG: pixel = ((r & 0xf0) << 4) | (b & 0xf0) | ((g & 0xf0) >> 4); break; | |
707 | case GRB: pixel = ((g & 0xf0) << 4) | (r & 0xf0) | ((b & 0xf0) >> 4); break; | |
708 | case GBR: pixel = ((g & 0xf0) << 4) | (b & 0xf0) | ((r & 0xf0) >> 4); break; | |
709 | case BRG: pixel = ((b & 0xf0) << 4) | (r & 0xf0) | ((g & 0xf0) >> 4); break; | |
710 | case BGR: pixel = ((b & 0xf0) << 4) | (g & 0xf0) | ((r & 0xf0) >> 4); break; | |
711 | } | |
712 | XPutPixel( data_image, x, y, pixel ); | |
713 | break; | |
714 | } | |
715 | case 15: | |
716 | { | |
717 | int pixel = 0; | |
718 | switch (b_o) | |
719 | { | |
720 | case RGB: pixel = ((r & 0xf8) << 7) | ((g & 0xf8) << 2) | ((b & 0xf8) >> 3); break; | |
721 | case RBG: pixel = ((r & 0xf8) << 7) | ((b & 0xf8) << 2) | ((g & 0xf8) >> 3); break; | |
722 | case GRB: pixel = ((g & 0xf8) << 7) | ((r & 0xf8) << 2) | ((b & 0xf8) >> 3); break; | |
723 | case GBR: pixel = ((g & 0xf8) << 7) | ((b & 0xf8) << 2) | ((r & 0xf8) >> 3); break; | |
724 | case BRG: pixel = ((b & 0xf8) << 7) | ((r & 0xf8) << 2) | ((g & 0xf8) >> 3); break; | |
725 | case BGR: pixel = ((b & 0xf8) << 7) | ((g & 0xf8) << 2) | ((r & 0xf8) >> 3); break; | |
726 | } | |
727 | XPutPixel( data_image, x, y, pixel ); | |
728 | break; | |
729 | } | |
730 | case 16: | |
731 | { | |
732 | // I actually don't know if for 16-bit displays, it is alway the green | |
733 | // component or the second component which has 6 bits. | |
734 | int pixel = 0; | |
735 | switch (b_o) | |
736 | { | |
737 | case RGB: pixel = ((r & 0xf8) << 8) | ((g & 0xfc) << 3) | ((b & 0xf8) >> 3); break; | |
738 | case RBG: pixel = ((r & 0xf8) << 8) | ((b & 0xfc) << 3) | ((g & 0xf8) >> 3); break; | |
739 | case GRB: pixel = ((g & 0xf8) << 8) | ((r & 0xfc) << 3) | ((b & 0xf8) >> 3); break; | |
740 | case GBR: pixel = ((g & 0xf8) << 8) | ((b & 0xfc) << 3) | ((r & 0xf8) >> 3); break; | |
741 | case BRG: pixel = ((b & 0xf8) << 8) | ((r & 0xfc) << 3) | ((g & 0xf8) >> 3); break; | |
742 | case BGR: pixel = ((b & 0xf8) << 8) | ((g & 0xfc) << 3) | ((r & 0xf8) >> 3); break; | |
743 | } | |
744 | XPutPixel( data_image, x, y, pixel ); | |
745 | break; | |
746 | } | |
747 | case 32: | |
748 | case 24: | |
749 | { | |
750 | int pixel = 0; | |
751 | switch (b_o) | |
752 | { | |
753 | case RGB: pixel = (r << 16) | (g << 8) | b; break; | |
754 | case RBG: pixel = (r << 16) | (b << 8) | g; break; | |
755 | case BRG: pixel = (b << 16) | (r << 8) | g; break; | |
756 | case BGR: pixel = (b << 16) | (g << 8) | r; break; | |
757 | case GRB: pixel = (g << 16) | (r << 8) | b; break; | |
758 | case GBR: pixel = (g << 16) | (b << 8) | r; break; | |
759 | } | |
760 | XPutPixel( data_image, x, y, pixel ); | |
761 | } | |
762 | default: break; | |
763 | } | |
764 | } // for | |
765 | } // for | |
83df96d6 | 766 | |
a11672a4 | 767 | // Blit picture |
83df96d6 | 768 | |
a11672a4 RR |
769 | GC gc = XCreateGC( xdisplay, (Pixmap) M_BMPDATA->m_pixmap, 0, NULL ); |
770 | XPutImage( xdisplay, (Pixmap) M_BMPDATA->m_pixmap, gc, data_image, 0, 0, 0, 0, width, height ); | |
a11672a4 RR |
771 | XDestroyImage( data_image ); |
772 | XFreeGC( xdisplay, gc ); | |
83df96d6 | 773 | |
a11672a4 | 774 | // Blit mask |
0cad49b4 | 775 | |
a11672a4 RR |
776 | if (image.HasMask()) |
777 | { | |
778 | GC gc = XCreateGC( xdisplay, (Pixmap) GetMask()->GetBitmap(), 0, NULL ); | |
011c1748 | 779 | XPutImage( xdisplay, (Pixmap) GetMask()->GetBitmap(), gc, mask_image, 0, 0, 0, 0, width, height ); |
a11672a4 RR |
780 | |
781 | XDestroyImage( mask_image ); | |
782 | XFreeGC( xdisplay, gc ); | |
783 | } | |
784 | } | |
83df96d6 | 785 | |
55034339 | 786 | return true; |
c79a329d JS |
787 | #endif |
788 | // wxUSE_NANOX | |
83df96d6 JS |
789 | } |
790 | ||
a11672a4 | 791 | wxImage wxBitmap::ConvertToImage() const |
83df96d6 | 792 | { |
a11672a4 | 793 | wxImage image; |
83df96d6 | 794 | |
a1b806b9 | 795 | wxCHECK_MSG( IsOk(), wxNullImage, wxT("invalid bitmap") ); |
83df96d6 | 796 | |
a11672a4 RR |
797 | Display *xdisplay = (Display*) M_BMPDATA->m_display; |
798 | wxASSERT_MSG( xdisplay, wxT("No display") ); | |
c79a329d JS |
799 | |
800 | #if wxUSE_NANOX | |
8601b2e1 | 801 | //int bpp = DefaultDepth(xdisplay, xscreen); |
f22033e5 | 802 | wxGetImageFromDrawable((Pixmap) GetPixmap(), 0, 0, GetWidth(), GetHeight(), image); |
c79a329d | 803 | return image; |
c79a329d JS |
804 | #else |
805 | // !wxUSE_NANOX | |
9ce8d6a2 | 806 | int bpp = wxTheApp->GetVisualInfo(M_BMPDATA->m_display)->m_visualDepth; |
a11672a4 RR |
807 | XImage *x_image = NULL; |
808 | if (GetPixmap()) | |
809 | { | |
810 | x_image = XGetImage( xdisplay, (Pixmap) GetPixmap(), | |
811 | 0, 0, | |
812 | GetWidth(), GetHeight(), | |
813 | AllPlanes, ZPixmap ); | |
814 | } else | |
815 | if (GetBitmap()) | |
816 | { | |
817 | x_image = XGetImage( xdisplay, (Pixmap) GetBitmap(), | |
818 | 0, 0, | |
819 | GetWidth(), GetHeight(), | |
820 | AllPlanes, ZPixmap ); | |
821 | } else | |
822 | { | |
823 | wxFAIL_MSG( wxT("Ill-formed bitmap") ); | |
824 | } | |
83df96d6 | 825 | |
a11672a4 | 826 | wxCHECK_MSG( x_image, wxNullImage, wxT("couldn't create image") ); |
83df96d6 | 827 | |
a11672a4 RR |
828 | image.Create( GetWidth(), GetHeight() ); |
829 | char unsigned *data = image.GetData(); | |
83df96d6 | 830 | |
a11672a4 RR |
831 | if (!data) |
832 | { | |
833 | XDestroyImage( x_image ); | |
834 | wxFAIL_MSG( wxT("couldn't create image") ); | |
835 | return wxNullImage; | |
836 | } | |
83df96d6 | 837 | |
a11672a4 RR |
838 | XImage *x_image_mask = NULL; |
839 | if (GetMask()) | |
840 | { | |
841 | x_image_mask = XGetImage( xdisplay, (Pixmap) GetMask()->GetBitmap(), | |
842 | 0, 0, | |
0cad49b4 | 843 | GetWidth(), GetHeight(), |
a11672a4 | 844 | AllPlanes, ZPixmap ); |
83df96d6 | 845 | |
a11672a4 RR |
846 | image.SetMaskColour( 16, 16, 16 ); // anything unlikely and dividable |
847 | } | |
83df96d6 | 848 | |
a11672a4 RR |
849 | int red_shift_right = 0; |
850 | int green_shift_right = 0; | |
851 | int blue_shift_right = 0; | |
852 | int red_shift_left = 0; | |
853 | int green_shift_left = 0; | |
854 | int blue_shift_left = 0; | |
55034339 | 855 | bool use_shift = false; |
83df96d6 | 856 | |
a11672a4 RR |
857 | if (GetPixmap()) |
858 | { | |
9ce8d6a2 MB |
859 | wxXVisualInfo* vi = wxTheApp->GetVisualInfo(M_BMPDATA->m_display); |
860 | ||
861 | red_shift_right = vi->m_visualRedShift; | |
862 | red_shift_left = 8 - vi->m_visualRedPrec; | |
863 | green_shift_right = vi->m_visualGreenShift; | |
864 | green_shift_left = 8 - vi->m_visualGreenPrec; | |
865 | blue_shift_right = vi->m_visualBlueShift; | |
866 | blue_shift_left = 8 - vi->m_visualBluePrec; | |
0cad49b4 | 867 | |
9ce8d6a2 MB |
868 | use_shift = (vi->m_visualType == GrayScale) || |
869 | (vi->m_visualType != PseudoColor); | |
a11672a4 | 870 | } |
0cad49b4 | 871 | |
a11672a4 RR |
872 | if (GetBitmap()) |
873 | { | |
874 | bpp = 1; | |
875 | } | |
83df96d6 | 876 | |
9ce8d6a2 MB |
877 | XColor *colors = (XColor*)wxTheApp-> |
878 | GetVisualInfo(M_BMPDATA->m_display)->m_visualColormap; | |
83df96d6 | 879 | |
288efe84 JS |
880 | int width = GetWidth(); |
881 | int height = GetHeight(); | |
a11672a4 | 882 | long pos = 0; |
288efe84 | 883 | for (int j = 0; j < height; j++) |
a11672a4 | 884 | { |
288efe84 | 885 | for (int i = 0; i < width; i++) |
a11672a4 RR |
886 | { |
887 | unsigned long pixel = XGetPixel( x_image, i, j ); | |
888 | if (bpp == 1) | |
889 | { | |
890 | if (pixel == 0) | |
891 | { | |
892 | data[pos] = 0; | |
893 | data[pos+1] = 0; | |
894 | data[pos+2] = 0; | |
895 | } | |
896 | else | |
897 | { | |
898 | data[pos] = 255; | |
899 | data[pos+1] = 255; | |
900 | data[pos+2] = 255; | |
901 | } | |
902 | } | |
903 | else if (use_shift) | |
904 | { | |
55034339 WS |
905 | data[pos] = (unsigned char)((pixel >> red_shift_right) << red_shift_left); |
906 | data[pos+1] = (unsigned char)((pixel >> green_shift_right) << green_shift_left); | |
907 | data[pos+2] = (unsigned char)((pixel >> blue_shift_right) << blue_shift_left); | |
a11672a4 | 908 | } |
dc4025af | 909 | else if (colors) |
a11672a4 | 910 | { |
55034339 WS |
911 | data[pos] = (unsigned char)(colors[pixel].red >> 8); |
912 | data[pos+1] = (unsigned char)(colors[pixel].green >> 8); | |
913 | data[pos+2] = (unsigned char)(colors[pixel].blue >> 8); | |
a11672a4 | 914 | } |
a11672a4 RR |
915 | else |
916 | { | |
917 | wxFAIL_MSG( wxT("Image conversion failed. Unknown visual type.") ); | |
918 | } | |
83df96d6 | 919 | |
a11672a4 RR |
920 | if (x_image_mask) |
921 | { | |
922 | int mask_pixel = XGetPixel( x_image_mask, i, j ); | |
923 | if (mask_pixel == 0) | |
924 | { | |
925 | data[pos] = 16; | |
926 | data[pos+1] = 16; | |
927 | data[pos+2] = 16; | |
928 | } | |
929 | } | |
83df96d6 | 930 | |
a11672a4 RR |
931 | pos += 3; |
932 | } | |
933 | } | |
83df96d6 | 934 | |
a11672a4 RR |
935 | XDestroyImage( x_image ); |
936 | if (x_image_mask) XDestroyImage( x_image_mask ); | |
a11672a4 | 937 | return image; |
c79a329d JS |
938 | #endif |
939 | // wxUSE_NANOX | |
83df96d6 JS |
940 | } |
941 | ||
62ad3cb3 | 942 | wxBitmap::wxBitmap( const wxString &filename, wxBitmapType type ) |
83df96d6 | 943 | { |
a11672a4 | 944 | LoadFile( filename, type ); |
83df96d6 JS |
945 | } |
946 | ||
62ad3cb3 | 947 | wxBitmap::wxBitmap( const char bits[], int width, int height, int depth ) |
83df96d6 | 948 | { |
62ad3cb3 | 949 | m_refData = new wxBitmapRefData; |
83df96d6 | 950 | |
452418c4 | 951 | (void) Create(bits, wxBITMAP_TYPE_XBM_DATA, width, height, depth); |
83df96d6 JS |
952 | } |
953 | ||
a11672a4 | 954 | wxBitmap::~wxBitmap() |
83df96d6 | 955 | { |
83df96d6 JS |
956 | } |
957 | ||
a11672a4 RR |
958 | int wxBitmap::GetHeight() const |
959 | { | |
a1b806b9 | 960 | wxCHECK_MSG( IsOk(), -1, wxT("invalid bitmap") ); |
83df96d6 | 961 | |
a11672a4 RR |
962 | return M_BMPDATA->m_height; |
963 | } | |
83df96d6 | 964 | |
a11672a4 RR |
965 | int wxBitmap::GetWidth() const |
966 | { | |
a1b806b9 | 967 | wxCHECK_MSG( IsOk(), -1, wxT("invalid bitmap") ); |
83df96d6 | 968 | |
a11672a4 RR |
969 | return M_BMPDATA->m_width; |
970 | } | |
83df96d6 | 971 | |
a11672a4 RR |
972 | int wxBitmap::GetDepth() const |
973 | { | |
a1b806b9 | 974 | wxCHECK_MSG( IsOk(), -1, wxT("invalid bitmap") ); |
83df96d6 | 975 | |
a11672a4 RR |
976 | return M_BMPDATA->m_bpp; |
977 | } | |
83df96d6 | 978 | |
a11672a4 RR |
979 | wxMask *wxBitmap::GetMask() const |
980 | { | |
a1b806b9 | 981 | wxCHECK_MSG( IsOk(), NULL, wxT("invalid bitmap") ); |
83df96d6 | 982 | |
a11672a4 RR |
983 | return M_BMPDATA->m_mask; |
984 | } | |
83df96d6 | 985 | |
a11672a4 RR |
986 | void wxBitmap::SetMask( wxMask *mask ) |
987 | { | |
a1b806b9 | 988 | wxCHECK_RET( IsOk(), wxT("invalid bitmap") ); |
83df96d6 | 989 | |
55ccdb93 | 990 | AllocExclusive(); |
a11672a4 | 991 | if (M_BMPDATA->m_mask) delete M_BMPDATA->m_mask; |
83df96d6 | 992 | |
a11672a4 RR |
993 | M_BMPDATA->m_mask = mask; |
994 | } | |
83df96d6 | 995 | |
a11672a4 RR |
996 | bool wxBitmap::CopyFromIcon(const wxIcon& icon) |
997 | { | |
998 | *this = icon; | |
55034339 | 999 | return true; |
a11672a4 | 1000 | } |
83df96d6 | 1001 | |
a11672a4 RR |
1002 | wxBitmap wxBitmap::GetSubBitmap( const wxRect& rect) const |
1003 | { | |
a1b806b9 | 1004 | wxCHECK_MSG( IsOk() && |
a11672a4 | 1005 | (rect.x >= 0) && (rect.y >= 0) && |
62ad3cb3 MB |
1006 | (rect.x+rect.width <= M_BMPDATA->m_width ) && |
1007 | (rect.y+rect.height <= M_BMPDATA->m_height), | |
a11672a4 | 1008 | wxNullBitmap, wxT("invalid bitmap or bitmap region") ); |
83df96d6 | 1009 | |
a11672a4 | 1010 | wxBitmap ret( rect.width, rect.height, M_BMPDATA->m_bpp ); |
a1b806b9 | 1011 | wxASSERT_MSG( ret.IsOk(), wxT("GetSubBitmap error") ); |
83df96d6 | 1012 | |
62ad3cb3 | 1013 | if( GetMask() ) |
83df96d6 | 1014 | { |
62ad3cb3 MB |
1015 | wxMask* mask = new wxMask(); |
1016 | mask->SetDisplay( GetMask()->GetDisplay() ); | |
1017 | mask->SetBitmap( wxGetSubPixmap( GetMask()->GetDisplay(), | |
1018 | GetMask()->GetBitmap(), | |
1019 | rect.x, rect.y, | |
1020 | rect.width, rect.height, | |
1021 | 1 ) ); | |
1022 | ||
1023 | ret.SetMask( mask ); | |
a11672a4 | 1024 | } |
62ad3cb3 MB |
1025 | |
1026 | if( GetPixmap() ) | |
a11672a4 | 1027 | { |
62ad3cb3 MB |
1028 | ret.SetPixmap( wxGetSubPixmap( GetDisplay(), |
1029 | GetPixmap(), | |
1030 | rect.x, rect.y, | |
1031 | rect.width, rect.height, | |
1032 | M_BMPDATA->m_bpp ) ); | |
83df96d6 JS |
1033 | } |
1034 | ||
62ad3cb3 | 1035 | if( GetBitmap() ) |
83df96d6 | 1036 | { |
62ad3cb3 MB |
1037 | ret.SetBitmap( wxGetSubPixmap( GetDisplay(), |
1038 | GetBitmap(), | |
1039 | rect.x, rect.y, | |
1040 | rect.width, rect.height, | |
1041 | 1 ) ); | |
83df96d6 JS |
1042 | } |
1043 | ||
a11672a4 RR |
1044 | return ret; |
1045 | } | |
83df96d6 | 1046 | |
62ad3cb3 MB |
1047 | bool wxBitmap::SaveFile( const wxString &name, wxBitmapType type, |
1048 | const wxPalette *palette ) const | |
a11672a4 | 1049 | { |
a1b806b9 | 1050 | wxCHECK_MSG( IsOk(), false, wxT("invalid bitmap") ); |
83df96d6 | 1051 | |
62ad3cb3 MB |
1052 | wxBitmapHandler *handler = FindHandler(type); |
1053 | ||
a11672a4 | 1054 | // Try to save the bitmap via wxImage handlers: |
62ad3cb3 | 1055 | if (handler == NULL) |
0cad49b4 | 1056 | { |
62ad3cb3 | 1057 | wxImage image(this->ConvertToImage()); |
a1b806b9 | 1058 | if (image.IsOk()) return image.SaveFile( name, type ); |
62ad3cb3 | 1059 | |
55034339 | 1060 | return false; |
a11672a4 | 1061 | } |
83df96d6 | 1062 | |
62ad3cb3 | 1063 | return handler->SaveFile(this, name, type, palette); |
a11672a4 | 1064 | } |
83df96d6 | 1065 | |
62ad3cb3 | 1066 | bool wxBitmap::LoadFile( const wxString &name, wxBitmapType type ) |
a11672a4 RR |
1067 | { |
1068 | UnRef(); | |
83df96d6 | 1069 | |
55034339 | 1070 | if (!wxFileExists(name)) return false; |
83df96d6 | 1071 | |
62ad3cb3 | 1072 | wxBitmapHandler *handler = FindHandler(type); |
83df96d6 | 1073 | |
62ad3cb3 | 1074 | if (handler == NULL) |
83df96d6 | 1075 | { |
62ad3cb3 MB |
1076 | wxImage image; |
1077 | if (!image.LoadFile( name, type )) | |
55034339 | 1078 | return false; |
83df96d6 | 1079 | |
a1b806b9 | 1080 | if (image.IsOk()) |
a11672a4 | 1081 | { |
62ad3cb3 | 1082 | *this = wxBitmap(image); |
55034339 | 1083 | return true; |
a11672a4 | 1084 | } |
55034339 | 1085 | else return false; |
83df96d6 | 1086 | } |
83df96d6 | 1087 | |
62ad3cb3 MB |
1088 | return handler->LoadFile(this, name, type, -1, -1); |
1089 | } | |
1090 | ||
1091 | void wxBitmap::SetPalette(const wxPalette& palette) | |
1092 | { | |
a1b806b9 | 1093 | wxCHECK_RET(IsOk(), wxT("invalid bitmap")); |
62ad3cb3 MB |
1094 | wxCHECK_RET(GetDepth() > 1 && GetDepth() <= 8, |
1095 | wxT("cannot set palette for bitmap of this depth")); | |
1096 | ||
55ccdb93 | 1097 | AllocExclusive(); |
5276b0a5 | 1098 | wxDELETE(M_BMPDATA->m_palette); |
62ad3cb3 | 1099 | |
a1b806b9 | 1100 | if (!palette.IsOk()) return; |
0cad49b4 | 1101 | |
62ad3cb3 | 1102 | M_BMPDATA->m_palette = new wxPalette(palette); |
83df96d6 JS |
1103 | } |
1104 | ||
a11672a4 | 1105 | wxPalette *wxBitmap::GetPalette() const |
83df96d6 | 1106 | { |
a1b806b9 | 1107 | if (!IsOk()) return NULL; |
83df96d6 | 1108 | |
a11672a4 RR |
1109 | return M_BMPDATA->m_palette; |
1110 | } | |
83df96d6 | 1111 | |
a11672a4 RR |
1112 | void wxBitmap::SetHeight( int height ) |
1113 | { | |
55ccdb93 | 1114 | AllocExclusive(); |
83df96d6 | 1115 | |
a11672a4 RR |
1116 | M_BMPDATA->m_height = height; |
1117 | } | |
83df96d6 | 1118 | |
a11672a4 RR |
1119 | void wxBitmap::SetWidth( int width ) |
1120 | { | |
55ccdb93 | 1121 | AllocExclusive(); |
83df96d6 | 1122 | |
a11672a4 RR |
1123 | M_BMPDATA->m_width = width; |
1124 | } | |
83df96d6 | 1125 | |
a11672a4 RR |
1126 | void wxBitmap::SetDepth( int depth ) |
1127 | { | |
55ccdb93 | 1128 | AllocExclusive(); |
83df96d6 | 1129 | |
a11672a4 RR |
1130 | M_BMPDATA->m_bpp = depth; |
1131 | } | |
83df96d6 | 1132 | |
a11672a4 RR |
1133 | void wxBitmap::SetPixmap( WXPixmap pixmap ) |
1134 | { | |
1135 | if (!m_refData) m_refData = new wxBitmapRefData(); | |
83df96d6 | 1136 | |
dd21c826 | 1137 | M_BMPDATA->m_pixmap = (Pixmap)pixmap; |
a11672a4 | 1138 | } |
83df96d6 | 1139 | |
a11672a4 RR |
1140 | void wxBitmap::SetBitmap( WXPixmap bitmap ) |
1141 | { | |
1142 | if (!m_refData) m_refData = new wxBitmapRefData(); | |
83df96d6 | 1143 | |
dd21c826 | 1144 | M_BMPDATA->m_bitmap = (Pixmap)bitmap; |
a11672a4 | 1145 | } |
83df96d6 | 1146 | |
a11672a4 RR |
1147 | WXPixmap wxBitmap::GetPixmap() const |
1148 | { | |
a1b806b9 | 1149 | wxCHECK_MSG( IsOk(), (WXPixmap) NULL, wxT("invalid bitmap") ); |
83df96d6 | 1150 | |
dd21c826 | 1151 | return (WXPixmap)M_BMPDATA->m_pixmap; |
a11672a4 | 1152 | } |
83df96d6 | 1153 | |
a11672a4 RR |
1154 | WXPixmap wxBitmap::GetBitmap() const |
1155 | { | |
a1b806b9 | 1156 | wxCHECK_MSG( IsOk(), (WXPixmap) NULL, wxT("invalid bitmap") ); |
83df96d6 | 1157 | |
dd21c826 | 1158 | return (WXPixmap)M_BMPDATA->m_bitmap; |
83df96d6 | 1159 | } |
7266b672 | 1160 | |
a29c6824 MB |
1161 | WXPixmap wxBitmap::GetDrawable() const |
1162 | { | |
a1b806b9 | 1163 | wxCHECK_MSG( IsOk(), (WXPixmap) NULL, wxT("invalid bitmap") ); |
a29c6824 | 1164 | |
dd21c826 VZ |
1165 | return (WXPixmap)(M_BMPDATA->m_bpp == 1 ? M_BMPDATA->m_bitmap |
1166 | : M_BMPDATA->m_pixmap); | |
a29c6824 MB |
1167 | } |
1168 | ||
a11672a4 | 1169 | WXDisplay *wxBitmap::GetDisplay() const |
7266b672 | 1170 | { |
a1b806b9 | 1171 | wxCHECK_MSG( IsOk(), NULL, wxT("invalid bitmap") ); |
b6ed4565 | 1172 | |
a11672a4 | 1173 | return M_BMPDATA->m_display; |
7266b672 | 1174 | } |
a11672a4 | 1175 | |
a20c04ab JS |
1176 | #if wxUSE_NANOX |
1177 | // Copy from the drawable to the wxImage | |
1178 | bool wxGetImageFromDrawable(GR_DRAW_ID drawable, int srcX, int srcY, int width, int height, wxImage& image) | |
1179 | { | |
f22033e5 | 1180 | GR_SCREEN_INFO sinfo; |
a20c04ab JS |
1181 | int x, y; |
1182 | GR_PIXELVAL *pixels; | |
a20c04ab | 1183 | GR_PALETTE* palette = NULL; |
f22033e5 | 1184 | unsigned char rgb[3], *pp; |
a20c04ab | 1185 | |
f22033e5 | 1186 | GrGetScreenInfo(&sinfo); |
a20c04ab | 1187 | |
f22033e5 | 1188 | if (sinfo.pixtype == MWPF_PALETTE) { |
0cad49b4 | 1189 | if(!(palette = (GR_PALETTE*) malloc(sizeof(GR_PALETTE)))) { |
55034339 | 1190 | return false; |
0cad49b4 VZ |
1191 | } |
1192 | GrGetSystemPalette(palette); | |
1193 | } | |
a20c04ab | 1194 | |
f22033e5 | 1195 | if(!(pixels = (GR_PIXELVAL*) malloc(sizeof(GR_PIXELVAL) * width * height))) |
a20c04ab | 1196 | { |
55034339 | 1197 | return false; |
a20c04ab JS |
1198 | } |
1199 | ||
f22033e5 | 1200 | image.Create(width, height); |
a20c04ab | 1201 | |
0cad49b4 VZ |
1202 | GrReadArea(drawable, srcX, srcY, width, height, |
1203 | pixels); | |
1204 | ||
1205 | ||
1206 | for(x = 0; x < sinfo.cols; x++) { | |
1207 | ||
1208 | pp = (unsigned char *)pixels + | |
1209 | ((x + (y * sinfo.cols)) * | |
1210 | sizeof(GR_PIXELVAL)); | |
1211 | ||
1212 | switch(sinfo.pixtype) { | |
1213 | /* FIXME: These may need modifying on big endian. */ | |
1214 | case MWPF_TRUECOLOR0888: | |
1215 | case MWPF_TRUECOLOR888: | |
1216 | rgb[0] = pp[2]; | |
1217 | rgb[1] = pp[1]; | |
1218 | rgb[2] = pp[0]; | |
1219 | break; | |
1220 | case MWPF_PALETTE: | |
1221 | rgb[0] = palette->palette[pp[0]].r; | |
1222 | rgb[1] = palette->palette[pp[0]].g; | |
1223 | rgb[2] = palette->palette[pp[0]].b; | |
1224 | break; | |
1225 | case MWPF_TRUECOLOR565: | |
1226 | rgb[0] = pp[1] & 0xf8; | |
1227 | rgb[1] = ((pp[1] & 0x07) << 5) | | |
1228 | ((pp[0] & 0xe0) >> 3); | |
1229 | rgb[2] = (pp[0] & 0x1f) << 3; | |
1230 | break; | |
1231 | case MWPF_TRUECOLOR555: | |
1232 | rgb[0] = (pp[1] & 0x7c) << 1; | |
1233 | rgb[1] = ((pp[1] & 0x03) << 6) | | |
1234 | ((pp[0] & 0xe0) >> 2); | |
1235 | rgb[2] = (pp[0] & 0x1f) << 3; | |
1236 | break; | |
1237 | case MWPF_TRUECOLOR332: | |
1238 | rgb[0] = pp[0] & 0xe0; | |
1239 | rgb[1] = (pp[0] & 0x1c) << 3; | |
1240 | rgb[2] = (pp[0] & 0x03) << 6; | |
1241 | break; | |
1242 | default: | |
1243 | fprintf(stderr, "Unsupported pixel " | |
1244 | "format\n"); | |
1245 | return 1; | |
1246 | } | |
1247 | ||
1248 | image.SetRGB(x, y, rgb[0], rgb[1], rgb[2]); | |
1249 | ||
1250 | } | |
a20c04ab JS |
1251 | |
1252 | free(pixels); | |
f22033e5 | 1253 | if(palette) free(palette); |
a20c04ab | 1254 | |
55034339 | 1255 | return true; |
a20c04ab JS |
1256 | } |
1257 | ||
1258 | #if 0 | |
1259 | int GrGetPixelColor(GR_SCREEN_INFO* sinfo, GR_PALETTE* palette, GR_PIXELVAL pixel, | |
1260 | unsigned char* red, unsigned char* green, unsigned char* blue) | |
1261 | { | |
0cad49b4 | 1262 | unsigned char rgb[3], *pp; |
a20c04ab JS |
1263 | |
1264 | pp = (unsigned char*) & pixel ; | |
1265 | ||
1266 | switch (sinfo.pixtype) | |
1267 | { | |
0cad49b4 VZ |
1268 | /* FIXME: These may need modifying on big endian. */ |
1269 | case MWPF_TRUECOLOR0888: | |
1270 | case MWPF_TRUECOLOR888: | |
1271 | rgb[0] = pp[2]; | |
1272 | rgb[1] = pp[1]; | |
1273 | rgb[2] = pp[0]; | |
1274 | break; | |
1275 | case MWPF_PALETTE: | |
1276 | rgb[0] = palette->palette[pp[0]].r; | |
1277 | rgb[1] = palette->palette[pp[0]].g; | |
1278 | rgb[2] = palette->palette[pp[0]].b; | |
1279 | break; | |
1280 | case MWPF_TRUECOLOR565: | |
1281 | rgb[0] = pp[1] & 0xf8; | |
1282 | rgb[1] = ((pp[1] & 0x07) << 5) | | |
1283 | ((pp[0] & 0xe0) >> 3); | |
1284 | rgb[2] = (pp[0] & 0x1f) << 3; | |
1285 | break; | |
1286 | case MWPF_TRUECOLOR555: | |
1287 | rgb[0] = (pp[1] & 0x7c) << 1; | |
1288 | rgb[1] = ((pp[1] & 0x03) << 6) | | |
1289 | ((pp[0] & 0xe0) >> 2); | |
1290 | rgb[2] = (pp[0] & 0x1f) << 3; | |
1291 | break; | |
1292 | case MWPF_TRUECOLOR332: | |
1293 | rgb[0] = pp[0] & 0xe0; | |
1294 | rgb[1] = (pp[0] & 0x1c) << 3; | |
1295 | rgb[2] = (pp[0] & 0x03) << 6; | |
1296 | break; | |
1297 | default: | |
1298 | fprintf(stderr, "Unsupported pixel format\n"); | |
1299 | return 0; | |
1300 | } | |
a20c04ab JS |
1301 | |
1302 | ||
1303 | *(red) = rgb[0]; | |
1304 | *(green) = rgb[1]; | |
1305 | *(blue) = rgb[2]; | |
1306 | return 1; | |
1307 | } | |
1308 | #endif | |
c978d361 | 1309 | |
a20c04ab JS |
1310 | #endif |
1311 | // wxUSE_NANOX | |
1312 | ||
62ad3cb3 MB |
1313 | // ============================================================================ |
1314 | // Bitmap handlers | |
1315 | // ============================================================================ | |
1316 | ||
62ad3cb3 MB |
1317 | #define M_BMPHANDLERDATA ((wxBitmapRefData *)bitmap->GetRefData()) |
1318 | ||
1319 | #if wxUSE_XPM | |
1320 | ||
1321 | #if wxHAVE_LIB_XPM || wxUSE_STREAMS | |
1322 | ||
1323 | // ---------------------------------------------------------------------------- | |
1324 | // wxXPMFileHandler | |
1325 | // ---------------------------------------------------------------------------- | |
1326 | ||
1327 | class wxXPMFileHandler : public wxBitmapHandler | |
1328 | { | |
62ad3cb3 MB |
1329 | public: |
1330 | wxXPMFileHandler() | |
1331 | { | |
2fd1cf7d MB |
1332 | SetName( wxT("XPM file") ); |
1333 | SetExtension( wxT("xpm") ); | |
1334 | SetType( wxBITMAP_TYPE_XPM ); | |
62ad3cb3 MB |
1335 | }; |
1336 | ||
545cb3fc VZ |
1337 | virtual bool LoadFile(wxBitmap *bitmap, const wxString& name, |
1338 | wxBitmapType flags, | |
62ad3cb3 MB |
1339 | int desiredWidth, int desiredHeight); |
1340 | ||
1341 | virtual bool SaveFile(const wxBitmap *bitmap, const wxString& name, | |
545cb3fc VZ |
1342 | wxBitmapType type, |
1343 | const wxPalette *palette = NULL) const; | |
1344 | ||
1345 | virtual bool Create(wxBitmap *WXUNUSED(bitmap), | |
1346 | const void* WXUNUSED(data), | |
1347 | wxBitmapType WXUNUSED(flags), | |
1348 | int WXUNUSED(width), | |
1349 | int WXUNUSED(height), | |
1350 | int WXUNUSED(depth) = 1) | |
55034339 | 1351 | { return false; } |
545cb3fc VZ |
1352 | |
1353 | DECLARE_DYNAMIC_CLASS(wxXPMFileHandler) | |
62ad3cb3 MB |
1354 | }; |
1355 | ||
e933b5bc | 1356 | IMPLEMENT_DYNAMIC_CLASS(wxXPMFileHandler, wxBitmapHandler) |
62ad3cb3 | 1357 | |
545cb3fc VZ |
1358 | bool wxXPMFileHandler::LoadFile(wxBitmap *bitmap, |
1359 | const wxString& name, | |
1360 | wxBitmapType WXUNUSED(flags), | |
1361 | int WXUNUSED(desiredWidth), | |
55034339 | 1362 | int WXUNUSED(desiredHeight)) |
62ad3cb3 MB |
1363 | { |
1364 | #if wxHAVE_LIB_XPM | |
1365 | if (!bitmap->GetRefData()) | |
1366 | bitmap->SetRefData( new wxBitmapRefData() ); | |
1367 | ||
1368 | M_BMPHANDLERDATA->m_display = wxGlobalDisplay(); | |
0cad49b4 | 1369 | |
62ad3cb3 | 1370 | Display *xdisplay = (Display*) M_BMPHANDLERDATA->m_display; |
0cad49b4 | 1371 | |
62ad3cb3 MB |
1372 | int xscreen = DefaultScreen( xdisplay ); |
1373 | Window xroot = RootWindow( xdisplay, xscreen ); | |
0cad49b4 | 1374 | |
62ad3cb3 MB |
1375 | int bpp = DefaultDepth( xdisplay, xscreen ); |
1376 | ||
1377 | XpmAttributes xpmAttr; | |
1378 | xpmAttr.valuemask = XpmReturnInfos; // nothing yet, but get infos back | |
1379 | ||
1380 | Pixmap pixmap; | |
1381 | Pixmap mask = 0; | |
0cad49b4 | 1382 | |
62ad3cb3 | 1383 | int ErrorStatus = XpmReadFileToPixmap( xdisplay, xroot, |
0f0223d3 | 1384 | (char*) ((const char*) name.c_str()), |
62ad3cb3 | 1385 | &pixmap, &mask, &xpmAttr); |
0cad49b4 | 1386 | |
62ad3cb3 MB |
1387 | if (ErrorStatus == XpmSuccess) |
1388 | { | |
1389 | M_BMPHANDLERDATA->m_width = xpmAttr.width; | |
1390 | M_BMPHANDLERDATA->m_height = xpmAttr.height; | |
1391 | ||
1392 | M_BMPHANDLERDATA->m_bpp = bpp; // mono as well? | |
1393 | ||
1394 | XpmFreeAttributes(&xpmAttr); | |
1395 | ||
b29aaf4a | 1396 | M_BMPHANDLERDATA->m_bitmap = (Pixmap) pixmap; |
0cad49b4 | 1397 | |
62ad3cb3 MB |
1398 | if (mask) |
1399 | { | |
1400 | M_BMPHANDLERDATA->m_mask = new wxMask; | |
1401 | M_BMPHANDLERDATA->m_mask->SetBitmap( (WXPixmap) mask ); | |
1402 | M_BMPHANDLERDATA->m_mask->SetDisplay( xdisplay ); | |
1403 | } | |
1404 | } | |
1405 | else | |
1406 | { | |
1407 | UnRef(); | |
0cad49b4 | 1408 | |
55034339 | 1409 | return false; |
62ad3cb3 MB |
1410 | } |
1411 | ||
55034339 | 1412 | return true; |
62ad3cb3 MB |
1413 | #elif wxUSE_STREAMS |
1414 | wxXPMDecoder decoder; | |
1415 | wxFileInputStream stream(name); | |
9690b006 | 1416 | if (stream.IsOk()) |
62ad3cb3 MB |
1417 | { |
1418 | wxImage image(decoder.ReadFile(stream)); | |
9690b006 | 1419 | return image.IsOk() && bitmap->CreateFromImage(image); |
62ad3cb3 | 1420 | } |
6cee3292 | 1421 | |
55034339 | 1422 | return false; |
0cad49b4 | 1423 | #else // !wxHAVE_LIB_XPM && !wxUSE_STREAMS |
55034339 | 1424 | return false; |
62ad3cb3 MB |
1425 | #endif // wxHAVE_LIB_XPM / wxUSE_STREAMS |
1426 | } | |
1427 | ||
545cb3fc VZ |
1428 | bool wxXPMFileHandler::SaveFile(const wxBitmap *bitmap, |
1429 | const wxString& name, | |
1430 | wxBitmapType type, | |
1431 | const wxPalette *WXUNUSED(palette)) const | |
62ad3cb3 MB |
1432 | { |
1433 | wxImage image(bitmap->ConvertToImage()); | |
a1b806b9 | 1434 | if (image.IsOk()) |
545cb3fc | 1435 | return image.SaveFile( name, type ); |
62ad3cb3 | 1436 | |
55034339 | 1437 | return false; |
62ad3cb3 MB |
1438 | } |
1439 | ||
1440 | #endif // wxHAVE_LIB_XPM || wxUSE_STREAMS | |
1441 | ||
1442 | // ---------------------------------------------------------------------------- | |
1443 | // wxXPMDataHandler | |
1444 | // ---------------------------------------------------------------------------- | |
1445 | ||
1446 | class wxXPMDataHandler : public wxBitmapHandler | |
1447 | { | |
bc21780e | 1448 | DECLARE_DYNAMIC_CLASS(wxXPMDataHandler) |
62ad3cb3 MB |
1449 | public: |
1450 | wxXPMDataHandler() | |
1451 | { | |
2fd1cf7d MB |
1452 | SetName( wxT("XPM data") ); |
1453 | SetExtension( wxT("xpm") ); | |
1454 | SetType( wxBITMAP_TYPE_XPM_DATA ); | |
62ad3cb3 MB |
1455 | }; |
1456 | ||
55034339 WS |
1457 | virtual bool LoadFile(wxBitmap *WXUNUSED(bitmap), |
1458 | const wxString& WXUNUSED(name), | |
85d98dfe | 1459 | wxBitmapType WXUNUSED(flags), |
55034339 WS |
1460 | int WXUNUSED(desiredWidth), |
1461 | int WXUNUSED(desiredHeight)) | |
1462 | { return false; } | |
62ad3cb3 | 1463 | |
55034339 WS |
1464 | virtual bool SaveFile(const wxBitmap *WXUNUSED(bitmap), |
1465 | const wxString& WXUNUSED(name), | |
85d98dfe | 1466 | wxBitmapType WXUNUSED(type), |
545cb3fc | 1467 | const wxPalette *WXUNUSED(palette) = NULL) const |
55034339 | 1468 | { return false; } |
62ad3cb3 | 1469 | |
85d98dfe | 1470 | virtual bool Create(wxBitmap *bitmap, const void* data, wxBitmapType flags, |
62ad3cb3 MB |
1471 | int width, int height, int depth = 1); |
1472 | }; | |
1473 | ||
e933b5bc | 1474 | IMPLEMENT_DYNAMIC_CLASS(wxXPMDataHandler, wxBitmapHandler) |
62ad3cb3 | 1475 | |
452418c4 | 1476 | bool wxXPMDataHandler::Create(wxBitmap *bitmap, const void* bits, |
85d98dfe | 1477 | wxBitmapType WXUNUSED(flags), |
55034339 | 1478 | int WXUNUSED(width), int WXUNUSED(height), int WXUNUSED(depth)) |
62ad3cb3 MB |
1479 | { |
1480 | #if wxHAVE_LIB_XPM | |
637b7e4f | 1481 | wxCHECK_MSG( bits != NULL, false, wxT("invalid bitmap data") ); |
62ad3cb3 MB |
1482 | |
1483 | if (!bitmap->GetRefData()) | |
1484 | bitmap->SetRefData( new wxBitmapRefData() ); | |
1485 | ||
1486 | M_BMPHANDLERDATA->m_display = wxGlobalDisplay(); | |
0cad49b4 | 1487 | |
62ad3cb3 | 1488 | Display *xdisplay = (Display*) M_BMPHANDLERDATA->m_display; |
0cad49b4 | 1489 | |
62ad3cb3 MB |
1490 | int xscreen = DefaultScreen( xdisplay ); |
1491 | Window xroot = RootWindow( xdisplay, xscreen ); | |
0cad49b4 | 1492 | |
62ad3cb3 MB |
1493 | int bpp = DefaultDepth( xdisplay, xscreen ); |
1494 | ||
1495 | XpmAttributes xpmAttr; | |
1496 | xpmAttr.valuemask = XpmReturnInfos; // nothing yet, but get infos back | |
1497 | ||
1498 | Pixmap pixmap = 0; | |
1499 | Pixmap mask = 0; | |
0cad49b4 | 1500 | |
62ad3cb3 MB |
1501 | int ErrorStatus = XpmCreatePixmapFromData( xdisplay, xroot, (char**) bits, |
1502 | &pixmap, &mask, &xpmAttr ); | |
0cad49b4 | 1503 | |
62ad3cb3 MB |
1504 | if (ErrorStatus == XpmSuccess) |
1505 | { | |
1506 | M_BMPHANDLERDATA->m_width = xpmAttr.width; | |
1507 | M_BMPHANDLERDATA->m_height = xpmAttr.height; | |
1508 | ||
1509 | M_BMPHANDLERDATA->m_bpp = bpp; // mono as well? | |
1510 | ||
4b6a582b | 1511 | #if wxDEBUG_LEVEL |
62ad3cb3 MB |
1512 | unsigned int depthRet; |
1513 | int xRet, yRet; | |
1514 | unsigned int widthRet, heightRet, borderWidthRet; | |
1515 | XGetGeometry( xdisplay, pixmap, &xroot, &xRet, &yRet, | |
1516 | &widthRet, &heightRet, &borderWidthRet, &depthRet); | |
1517 | ||
1518 | wxASSERT_MSG( bpp == (int)depthRet, wxT("colour depth mismatch") ); | |
4b6a582b | 1519 | #endif // wxDEBUG_LEVEL |
62ad3cb3 MB |
1520 | |
1521 | XpmFreeAttributes(&xpmAttr); | |
0cad49b4 | 1522 | |
b29aaf4a | 1523 | M_BMPHANDLERDATA->m_pixmap = (Pixmap) pixmap; |
0cad49b4 | 1524 | |
62ad3cb3 MB |
1525 | if (mask) |
1526 | { | |
1527 | M_BMPHANDLERDATA->m_mask = new wxMask; | |
1528 | M_BMPHANDLERDATA->m_mask->SetBitmap( (WXPixmap) mask ); | |
1529 | M_BMPHANDLERDATA->m_mask->SetDisplay( xdisplay ); | |
1530 | } | |
55034339 | 1531 | return true; |
62ad3cb3 MB |
1532 | } |
1533 | else | |
1534 | { | |
1535 | bitmap->UnRef(); | |
0cad49b4 | 1536 | |
55034339 | 1537 | return false; |
62ad3cb3 | 1538 | } |
0cad49b4 | 1539 | #else // !wxHAVE_LIB_XPM |
62ad3cb3 | 1540 | wxXPMDecoder decoder; |
0cad49b4 | 1541 | wxImage image(decoder.ReadData((const char **)bits)); |
a1b806b9 | 1542 | return image.IsOk() && bitmap->CreateFromImage(image); |
0cad49b4 | 1543 | #endif // wxHAVE_LIB_XPM/!wxHAVE_LIB_XPM |
62ad3cb3 MB |
1544 | } |
1545 | ||
1546 | #endif // wxUSE_XPM | |
1547 | ||
1548 | // ---------------------------------------------------------------------------- | |
1549 | // wxXBMDataHandler | |
1550 | // ---------------------------------------------------------------------------- | |
1551 | ||
1552 | class WXDLLEXPORT wxXBMDataHandler: public wxBitmapHandler | |
1553 | { | |
1554 | DECLARE_DYNAMIC_CLASS(wxXBMDataHandler) | |
1555 | public: | |
1556 | inline wxXBMDataHandler() | |
1557 | { | |
2fd1cf7d MB |
1558 | SetName( wxT("XBM data") ); |
1559 | SetExtension( wxT("xbm") ); | |
1560 | SetType( wxBITMAP_TYPE_XBM_DATA ); | |
62ad3cb3 MB |
1561 | }; |
1562 | ||
55034339 WS |
1563 | virtual bool LoadFile(wxBitmap *WXUNUSED(bitmap), |
1564 | const wxString& WXUNUSED(name), | |
545cb3fc | 1565 | wxBitmapType WXUNUSED(flags), |
55034339 WS |
1566 | int WXUNUSED(desiredWidth), |
1567 | int WXUNUSED(desiredHeight)) | |
1568 | { return false; } | |
62ad3cb3 | 1569 | |
55034339 WS |
1570 | virtual bool SaveFile(const wxBitmap *WXUNUSED(bitmap), |
1571 | const wxString& WXUNUSED(name), | |
545cb3fc VZ |
1572 | wxBitmapType WXUNUSED(type), |
1573 | const wxPalette *WXUNUSED(palette) = NULL) const | |
55034339 | 1574 | { return false; } |
62ad3cb3 | 1575 | |
545cb3fc | 1576 | virtual bool Create(wxBitmap *bitmap, const void* data, wxBitmapType type, |
62ad3cb3 MB |
1577 | int width, int height, int depth = 1); |
1578 | }; | |
1579 | ||
e933b5bc | 1580 | IMPLEMENT_DYNAMIC_CLASS(wxXBMDataHandler, wxBitmapHandler) |
62ad3cb3 | 1581 | |
452418c4 | 1582 | bool wxXBMDataHandler::Create( wxBitmap *bitmap, const void* bits, |
545cb3fc | 1583 | wxBitmapType WXUNUSED(type), |
7fc65a03 | 1584 | int width, int height, int WXUNUSED(depth)) |
62ad3cb3 MB |
1585 | { |
1586 | #if !wxUSE_NANOX | |
1587 | if (!bitmap->GetRefData()) | |
1588 | bitmap->SetRefData( new wxBitmapRefData() ); | |
1589 | ||
1590 | M_BMPHANDLERDATA->m_display = wxGlobalDisplay(); | |
1591 | ||
1592 | Display *xdisplay = (Display*) M_BMPHANDLERDATA->m_display; | |
0cad49b4 | 1593 | |
62ad3cb3 MB |
1594 | int xscreen = DefaultScreen( xdisplay ); |
1595 | Window xroot = RootWindow( xdisplay, xscreen ); | |
0cad49b4 | 1596 | |
d3b9f782 | 1597 | M_BMPHANDLERDATA->m_mask = NULL; |
62ad3cb3 | 1598 | M_BMPHANDLERDATA->m_bitmap = |
dd21c826 VZ |
1599 | XCreateBitmapFromData(xdisplay, xroot, |
1600 | (char *) bits, width, height ); | |
62ad3cb3 MB |
1601 | M_BMPHANDLERDATA->m_width = width; |
1602 | M_BMPHANDLERDATA->m_height = height; | |
1603 | M_BMPHANDLERDATA->m_bpp = 1; | |
1604 | ||
55034339 | 1605 | return true; |
d171743e | 1606 | #else |
55034339 | 1607 | wxCHECK_MSG( M_BMPHANDLERDATA->m_bitmap, false, |
62ad3cb3 | 1608 | wxT("couldn't create bitmap") ); |
d171743e | 1609 | #endif |
62ad3cb3 MB |
1610 | } |
1611 | ||
1612 | void wxBitmap::InitStandardHandlers() | |
1613 | { | |
1614 | AddHandler(new wxXBMDataHandler); | |
1615 | #if wxUSE_XPM | |
1616 | #if wxHAVE_LIB_XPM || wxUSE_STREAMS | |
1617 | AddHandler(new wxXPMFileHandler); | |
1618 | #endif | |
1619 | AddHandler(new wxXPMDataHandler); | |
1620 | #endif | |
1621 | } |