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