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