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