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