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