]> git.saurik.com Git - wxWidgets.git/blame - src/x11/bitmap.cpp
don't return garbage from GetRawBitmap
[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
9ce8d6a2 117 int bpp = wxTheApp->GetVisualInfo(m_display)->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
9ce8d6a2 509 int bpp = wxTheApp->GetVisualInfo(M_BMPDATA->m_display)->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
9ce8d6a2
MB
566 wxXVisualInfo* vi = wxTheApp->GetVisualInfo(M_BMPDATA->m_display);
567 unsigned long greenMask = vi->m_visualGreenMask,
568 redMask = vi->m_visualRedMask,
569 blueMask = vi->m_visualBlueMask;
570
a11672a4
RR
571 if (bpp > 8)
572 {
9ce8d6a2
MB
573 if ((redMask > greenMask) && (greenMask > blueMask)) b_o = RGB;
574 else if ((redMask > blueMask) && (blueMask > greenMask)) b_o = RBG;
575 else if ((blueMask > redMask) && (redMask > greenMask)) b_o = BRG;
576 else if ((blueMask > greenMask) && (greenMask > redMask))b_o = BGR;
577 else if ((greenMask > redMask) && (redMask > blueMask)) b_o = GRB;
578 else if ((greenMask > blueMask) && (blueMask > redMask)) b_o = GBR;
a11672a4 579 }
83df96d6 580
a11672a4
RR
581 int r_mask = image.GetMaskRed();
582 int g_mask = image.GetMaskGreen();
583 int b_mask = image.GetMaskBlue();
83df96d6 584
a11672a4
RR
585 unsigned char* data = image.GetData();
586 wxASSERT_MSG( data, "No image data" );
dc4025af 587
9ce8d6a2
MB
588 unsigned char *colorCube =
589 wxTheApp->GetVisualInfo(M_BMPDATA->m_display)->m_colorCube;
83df96d6 590
a11672a4 591 bool hasMask = image.HasMask();
83df96d6 592
a11672a4
RR
593 int index = 0;
594 for (int y = 0; y < height; y++)
595 {
596 for (int x = 0; x < width; x++)
597 {
598 int r = data[index];
599 index++;
600 int g = data[index];
601 index++;
602 int b = data[index];
603 index++;
604
605 if (hasMask)
606 {
607 if ((r == r_mask) && (b == b_mask) && (g == g_mask))
608 XPutPixel( mask_image, x, y, 0 );
609 else
610 XPutPixel( mask_image, x, y, 1 );
611 }
83df96d6 612
a11672a4
RR
613 switch (bpp)
614 {
615 case 8:
616 {
617 int pixel = 0;
dc4025af 618 pixel = colorCube[ ((r & 0xf8) << 7) + ((g & 0xf8) << 2) + ((b & 0xf8) >> 3) ];
a11672a4
RR
619 XPutPixel( data_image, x, y, pixel );
620 break;
621 }
622 case 12: // SGI only
623 {
624 int pixel = 0;
625 switch (b_o)
626 {
627 case RGB: pixel = ((r & 0xf0) << 4) | (g & 0xf0) | ((b & 0xf0) >> 4); break;
628 case RBG: pixel = ((r & 0xf0) << 4) | (b & 0xf0) | ((g & 0xf0) >> 4); break;
629 case GRB: pixel = ((g & 0xf0) << 4) | (r & 0xf0) | ((b & 0xf0) >> 4); break;
630 case GBR: pixel = ((g & 0xf0) << 4) | (b & 0xf0) | ((r & 0xf0) >> 4); break;
631 case BRG: pixel = ((b & 0xf0) << 4) | (r & 0xf0) | ((g & 0xf0) >> 4); break;
632 case BGR: pixel = ((b & 0xf0) << 4) | (g & 0xf0) | ((r & 0xf0) >> 4); break;
633 }
634 XPutPixel( data_image, x, y, pixel );
635 break;
636 }
637 case 15:
638 {
639 int pixel = 0;
640 switch (b_o)
641 {
642 case RGB: pixel = ((r & 0xf8) << 7) | ((g & 0xf8) << 2) | ((b & 0xf8) >> 3); break;
643 case RBG: pixel = ((r & 0xf8) << 7) | ((b & 0xf8) << 2) | ((g & 0xf8) >> 3); break;
644 case GRB: pixel = ((g & 0xf8) << 7) | ((r & 0xf8) << 2) | ((b & 0xf8) >> 3); break;
645 case GBR: pixel = ((g & 0xf8) << 7) | ((b & 0xf8) << 2) | ((r & 0xf8) >> 3); break;
646 case BRG: pixel = ((b & 0xf8) << 7) | ((r & 0xf8) << 2) | ((g & 0xf8) >> 3); break;
647 case BGR: pixel = ((b & 0xf8) << 7) | ((g & 0xf8) << 2) | ((r & 0xf8) >> 3); break;
648 }
649 XPutPixel( data_image, x, y, pixel );
650 break;
651 }
652 case 16:
653 {
654 // I actually don't know if for 16-bit displays, it is alway the green
655 // component or the second component which has 6 bits.
656 int pixel = 0;
657 switch (b_o)
658 {
659 case RGB: pixel = ((r & 0xf8) << 8) | ((g & 0xfc) << 3) | ((b & 0xf8) >> 3); break;
660 case RBG: pixel = ((r & 0xf8) << 8) | ((b & 0xfc) << 3) | ((g & 0xf8) >> 3); break;
661 case GRB: pixel = ((g & 0xf8) << 8) | ((r & 0xfc) << 3) | ((b & 0xf8) >> 3); break;
662 case GBR: pixel = ((g & 0xf8) << 8) | ((b & 0xfc) << 3) | ((r & 0xf8) >> 3); break;
663 case BRG: pixel = ((b & 0xf8) << 8) | ((r & 0xfc) << 3) | ((g & 0xf8) >> 3); break;
664 case BGR: pixel = ((b & 0xf8) << 8) | ((g & 0xfc) << 3) | ((r & 0xf8) >> 3); break;
665 }
666 XPutPixel( data_image, x, y, pixel );
667 break;
668 }
669 case 32:
670 case 24:
671 {
672 int pixel = 0;
673 switch (b_o)
674 {
675 case RGB: pixel = (r << 16) | (g << 8) | b; break;
676 case RBG: pixel = (r << 16) | (b << 8) | g; break;
677 case BRG: pixel = (b << 16) | (r << 8) | g; break;
678 case BGR: pixel = (b << 16) | (g << 8) | r; break;
679 case GRB: pixel = (g << 16) | (r << 8) | b; break;
680 case GBR: pixel = (g << 16) | (b << 8) | r; break;
681 }
682 XPutPixel( data_image, x, y, pixel );
683 }
684 default: break;
685 }
686 } // for
687 } // for
83df96d6 688
a11672a4 689 // Blit picture
83df96d6 690
a11672a4
RR
691 GC gc = XCreateGC( xdisplay, (Pixmap) M_BMPDATA->m_pixmap, 0, NULL );
692 XPutImage( xdisplay, (Pixmap) M_BMPDATA->m_pixmap, gc, data_image, 0, 0, 0, 0, width, height );
a11672a4
RR
693 XDestroyImage( data_image );
694 XFreeGC( xdisplay, gc );
83df96d6 695
a11672a4
RR
696 // Blit mask
697
698 if (image.HasMask())
699 {
700 GC gc = XCreateGC( xdisplay, (Pixmap) GetMask()->GetBitmap(), 0, NULL );
011c1748 701 XPutImage( xdisplay, (Pixmap) GetMask()->GetBitmap(), gc, mask_image, 0, 0, 0, 0, width, height );
a11672a4
RR
702
703 XDestroyImage( mask_image );
704 XFreeGC( xdisplay, gc );
705 }
706 }
83df96d6 707
83df96d6 708 return TRUE;
c79a329d
JS
709#endif
710 // wxUSE_NANOX
83df96d6
JS
711}
712
a11672a4 713wxImage wxBitmap::ConvertToImage() const
83df96d6 714{
a11672a4 715 wxImage image;
83df96d6 716
a11672a4 717 wxCHECK_MSG( Ok(), wxNullImage, wxT("invalid bitmap") );
83df96d6 718
a11672a4
RR
719 Display *xdisplay = (Display*) M_BMPDATA->m_display;
720 wxASSERT_MSG( xdisplay, wxT("No display") );
c79a329d
JS
721
722#if wxUSE_NANOX
8601b2e1 723 //int bpp = DefaultDepth(xdisplay, xscreen);
f22033e5 724 wxGetImageFromDrawable((Pixmap) GetPixmap(), 0, 0, GetWidth(), GetHeight(), image);
c79a329d 725 return image;
c79a329d
JS
726#else
727 // !wxUSE_NANOX
9ce8d6a2 728 int bpp = wxTheApp->GetVisualInfo(M_BMPDATA->m_display)->m_visualDepth;
a11672a4
RR
729 XImage *x_image = NULL;
730 if (GetPixmap())
731 {
732 x_image = XGetImage( xdisplay, (Pixmap) GetPixmap(),
733 0, 0,
734 GetWidth(), GetHeight(),
735 AllPlanes, ZPixmap );
736 } else
737 if (GetBitmap())
738 {
739 x_image = XGetImage( xdisplay, (Pixmap) GetBitmap(),
740 0, 0,
741 GetWidth(), GetHeight(),
742 AllPlanes, ZPixmap );
743 } else
744 {
745 wxFAIL_MSG( wxT("Ill-formed bitmap") );
746 }
83df96d6 747
a11672a4 748 wxCHECK_MSG( x_image, wxNullImage, wxT("couldn't create image") );
83df96d6 749
a11672a4
RR
750 image.Create( GetWidth(), GetHeight() );
751 char unsigned *data = image.GetData();
83df96d6 752
a11672a4
RR
753 if (!data)
754 {
755 XDestroyImage( x_image );
756 wxFAIL_MSG( wxT("couldn't create image") );
757 return wxNullImage;
758 }
83df96d6 759
a11672a4
RR
760 XImage *x_image_mask = NULL;
761 if (GetMask())
762 {
763 x_image_mask = XGetImage( xdisplay, (Pixmap) GetMask()->GetBitmap(),
764 0, 0,
765 GetWidth(), GetHeight(),
766 AllPlanes, ZPixmap );
83df96d6 767
a11672a4
RR
768 image.SetMaskColour( 16, 16, 16 ); // anything unlikely and dividable
769 }
83df96d6 770
a11672a4
RR
771 int red_shift_right = 0;
772 int green_shift_right = 0;
773 int blue_shift_right = 0;
774 int red_shift_left = 0;
775 int green_shift_left = 0;
776 int blue_shift_left = 0;
777 bool use_shift = FALSE;
83df96d6 778
a11672a4
RR
779 if (GetPixmap())
780 {
9ce8d6a2
MB
781 wxXVisualInfo* vi = wxTheApp->GetVisualInfo(M_BMPDATA->m_display);
782
783 red_shift_right = vi->m_visualRedShift;
784 red_shift_left = 8 - vi->m_visualRedPrec;
785 green_shift_right = vi->m_visualGreenShift;
786 green_shift_left = 8 - vi->m_visualGreenPrec;
787 blue_shift_right = vi->m_visualBlueShift;
788 blue_shift_left = 8 - vi->m_visualBluePrec;
a11672a4 789
9ce8d6a2
MB
790 use_shift = (vi->m_visualType == GrayScale) ||
791 (vi->m_visualType != PseudoColor);
a11672a4 792 }
dc4025af 793
a11672a4
RR
794 if (GetBitmap())
795 {
796 bpp = 1;
797 }
83df96d6 798
9ce8d6a2
MB
799 XColor *colors = (XColor*)wxTheApp->
800 GetVisualInfo(M_BMPDATA->m_display)->m_visualColormap;
83df96d6 801
288efe84
JS
802 int width = GetWidth();
803 int height = GetHeight();
a11672a4 804 long pos = 0;
288efe84 805 for (int j = 0; j < height; j++)
a11672a4 806 {
288efe84 807 for (int i = 0; i < width; i++)
a11672a4
RR
808 {
809 unsigned long pixel = XGetPixel( x_image, i, j );
810 if (bpp == 1)
811 {
812 if (pixel == 0)
813 {
814 data[pos] = 0;
815 data[pos+1] = 0;
816 data[pos+2] = 0;
817 }
818 else
819 {
820 data[pos] = 255;
821 data[pos+1] = 255;
822 data[pos+2] = 255;
823 }
824 }
825 else if (use_shift)
826 {
827 data[pos] = (pixel >> red_shift_right) << red_shift_left;
828 data[pos+1] = (pixel >> green_shift_right) << green_shift_left;
829 data[pos+2] = (pixel >> blue_shift_right) << blue_shift_left;
830 }
dc4025af 831 else if (colors)
a11672a4 832 {
dc4025af
RR
833 data[pos] = colors[pixel].red >> 8;
834 data[pos+1] = colors[pixel].green >> 8;
835 data[pos+2] = colors[pixel].blue >> 8;
a11672a4 836 }
a11672a4
RR
837 else
838 {
839 wxFAIL_MSG( wxT("Image conversion failed. Unknown visual type.") );
840 }
83df96d6 841
a11672a4
RR
842 if (x_image_mask)
843 {
844 int mask_pixel = XGetPixel( x_image_mask, i, j );
845 if (mask_pixel == 0)
846 {
847 data[pos] = 16;
848 data[pos+1] = 16;
849 data[pos+2] = 16;
850 }
851 }
83df96d6 852
a11672a4
RR
853 pos += 3;
854 }
855 }
83df96d6 856
a11672a4
RR
857 XDestroyImage( x_image );
858 if (x_image_mask) XDestroyImage( x_image_mask );
a11672a4 859 return image;
c79a329d
JS
860#endif
861 // wxUSE_NANOX
83df96d6
JS
862}
863
a11672a4 864wxBitmap::wxBitmap( const wxBitmap& bmp )
83df96d6 865{
a11672a4 866 Ref( bmp );
83df96d6
JS
867}
868
62ad3cb3 869wxBitmap::wxBitmap( const wxString &filename, wxBitmapType type )
83df96d6 870{
a11672a4 871 LoadFile( filename, type );
83df96d6
JS
872}
873
62ad3cb3 874wxBitmap::wxBitmap( const char bits[], int width, int height, int depth )
83df96d6 875{
62ad3cb3 876 m_refData = new wxBitmapRefData;
83df96d6 877
62ad3cb3 878 (void) Create((void*) bits, wxBITMAP_TYPE_XBM_DATA, width, height, depth);
83df96d6
JS
879}
880
a11672a4 881wxBitmap::~wxBitmap()
83df96d6 882{
83df96d6
JS
883}
884
a11672a4 885wxBitmap& wxBitmap::operator = ( const wxBitmap& bmp )
83df96d6 886{
a11672a4
RR
887 if ( m_refData != bmp.m_refData )
888 Ref( bmp );
83df96d6 889
a11672a4
RR
890 return *this;
891}
83df96d6 892
a11672a4
RR
893bool wxBitmap::operator == ( const wxBitmap& bmp ) const
894{
895 return m_refData == bmp.m_refData;
896}
83df96d6 897
a11672a4
RR
898bool wxBitmap::operator != ( const wxBitmap& bmp ) const
899{
900 return m_refData != bmp.m_refData;
901}
83df96d6 902
a11672a4
RR
903bool wxBitmap::Ok() const
904{
905 return (m_refData != NULL);
906}
83df96d6 907
a11672a4
RR
908int wxBitmap::GetHeight() const
909{
910 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
83df96d6 911
a11672a4
RR
912 return M_BMPDATA->m_height;
913}
83df96d6 914
a11672a4
RR
915int wxBitmap::GetWidth() const
916{
917 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
83df96d6 918
a11672a4
RR
919 return M_BMPDATA->m_width;
920}
83df96d6 921
a11672a4
RR
922int wxBitmap::GetDepth() const
923{
924 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
83df96d6 925
a11672a4
RR
926 return M_BMPDATA->m_bpp;
927}
83df96d6 928
a11672a4
RR
929wxMask *wxBitmap::GetMask() const
930{
931 wxCHECK_MSG( Ok(), (wxMask *) NULL, wxT("invalid bitmap") );
83df96d6 932
a11672a4
RR
933 return M_BMPDATA->m_mask;
934}
83df96d6 935
a11672a4
RR
936void wxBitmap::SetMask( wxMask *mask )
937{
938 wxCHECK_RET( Ok(), wxT("invalid bitmap") );
83df96d6 939
a11672a4 940 if (M_BMPDATA->m_mask) delete M_BMPDATA->m_mask;
83df96d6 941
a11672a4
RR
942 M_BMPDATA->m_mask = mask;
943}
83df96d6 944
a11672a4
RR
945bool wxBitmap::CopyFromIcon(const wxIcon& icon)
946{
947 *this = icon;
948 return TRUE;
949}
83df96d6 950
a11672a4
RR
951wxBitmap wxBitmap::GetSubBitmap( const wxRect& rect) const
952{
953 wxCHECK_MSG( Ok() &&
954 (rect.x >= 0) && (rect.y >= 0) &&
62ad3cb3
MB
955 (rect.x+rect.width <= M_BMPDATA->m_width ) &&
956 (rect.y+rect.height <= M_BMPDATA->m_height),
a11672a4 957 wxNullBitmap, wxT("invalid bitmap or bitmap region") );
83df96d6 958
a11672a4
RR
959 wxBitmap ret( rect.width, rect.height, M_BMPDATA->m_bpp );
960 wxASSERT_MSG( ret.Ok(), wxT("GetSubBitmap error") );
83df96d6 961
62ad3cb3 962 if( GetMask() )
83df96d6 963 {
62ad3cb3
MB
964 wxMask* mask = new wxMask();
965 mask->SetDisplay( GetMask()->GetDisplay() );
966 mask->SetBitmap( wxGetSubPixmap( GetMask()->GetDisplay(),
967 GetMask()->GetBitmap(),
968 rect.x, rect.y,
969 rect.width, rect.height,
970 1 ) );
971
972 ret.SetMask( mask );
a11672a4 973 }
62ad3cb3
MB
974
975 if( GetPixmap() )
a11672a4 976 {
62ad3cb3
MB
977 ret.SetPixmap( wxGetSubPixmap( GetDisplay(),
978 GetPixmap(),
979 rect.x, rect.y,
980 rect.width, rect.height,
981 M_BMPDATA->m_bpp ) );
83df96d6
JS
982 }
983
62ad3cb3 984 if( GetBitmap() )
83df96d6 985 {
62ad3cb3
MB
986 ret.SetBitmap( wxGetSubPixmap( GetDisplay(),
987 GetBitmap(),
988 rect.x, rect.y,
989 rect.width, rect.height,
990 1 ) );
83df96d6
JS
991 }
992
a11672a4
RR
993 return ret;
994}
83df96d6 995
62ad3cb3
MB
996bool wxBitmap::SaveFile( const wxString &name, wxBitmapType type,
997 const wxPalette *palette ) const
a11672a4
RR
998{
999 wxCHECK_MSG( Ok(), FALSE, wxT("invalid bitmap") );
83df96d6 1000
62ad3cb3
MB
1001 wxBitmapHandler *handler = FindHandler(type);
1002
a11672a4 1003 // Try to save the bitmap via wxImage handlers:
62ad3cb3
MB
1004 if (handler == NULL)
1005 {
1006 wxImage image(this->ConvertToImage());
a11672a4 1007 if (image.Ok()) return image.SaveFile( name, type );
62ad3cb3
MB
1008
1009 return FALSE;
a11672a4 1010 }
83df96d6 1011
62ad3cb3 1012 return handler->SaveFile(this, name, type, palette);
a11672a4 1013}
83df96d6 1014
62ad3cb3 1015bool wxBitmap::LoadFile( const wxString &name, wxBitmapType type )
a11672a4
RR
1016{
1017 UnRef();
83df96d6 1018
a11672a4 1019 if (!wxFileExists(name)) return FALSE;
83df96d6 1020
62ad3cb3 1021 wxBitmapHandler *handler = FindHandler(type);
83df96d6 1022
62ad3cb3 1023 if (handler == NULL)
83df96d6 1024 {
62ad3cb3
MB
1025 wxImage image;
1026 if (!image.LoadFile( name, type ))
1027 return FALSE;
83df96d6 1028
62ad3cb3 1029 if (image.Ok())
a11672a4 1030 {
62ad3cb3
MB
1031 *this = wxBitmap(image);
1032 return TRUE;
a11672a4 1033 }
a11672a4 1034 else return FALSE;
83df96d6 1035 }
83df96d6 1036
62ad3cb3
MB
1037 return handler->LoadFile(this, name, type, -1, -1);
1038}
1039
1040void wxBitmap::SetPalette(const wxPalette& palette)
1041{
1042 wxCHECK_RET(Ok(), wxT("invalid bitmap"));
1043 wxCHECK_RET(GetDepth() > 1 && GetDepth() <= 8,
1044 wxT("cannot set palette for bitmap of this depth"));
1045
1046 delete M_BMPDATA->m_palette;
1047 M_BMPDATA->m_palette = NULL;
1048
1049 if (!palette.Ok()) return;
1050
1051 M_BMPDATA->m_palette = new wxPalette(palette);
83df96d6
JS
1052}
1053
a11672a4 1054wxPalette *wxBitmap::GetPalette() const
83df96d6 1055{
a11672a4 1056 if (!Ok()) return (wxPalette *) NULL;
83df96d6 1057
a11672a4
RR
1058 return M_BMPDATA->m_palette;
1059}
83df96d6 1060
a11672a4
RR
1061void wxBitmap::SetHeight( int height )
1062{
1063 if (!m_refData) m_refData = new wxBitmapRefData();
83df96d6 1064
a11672a4
RR
1065 M_BMPDATA->m_height = height;
1066}
83df96d6 1067
a11672a4
RR
1068void wxBitmap::SetWidth( int width )
1069{
1070 if (!m_refData) m_refData = new wxBitmapRefData();
83df96d6 1071
a11672a4
RR
1072 M_BMPDATA->m_width = width;
1073}
83df96d6 1074
a11672a4
RR
1075void wxBitmap::SetDepth( int depth )
1076{
1077 if (!m_refData) m_refData = new wxBitmapRefData();
83df96d6 1078
a11672a4
RR
1079 M_BMPDATA->m_bpp = depth;
1080}
83df96d6 1081
a11672a4
RR
1082void wxBitmap::SetPixmap( WXPixmap pixmap )
1083{
1084 if (!m_refData) m_refData = new wxBitmapRefData();
83df96d6 1085
a11672a4
RR
1086 M_BMPDATA->m_pixmap = pixmap;
1087}
83df96d6 1088
a11672a4
RR
1089void wxBitmap::SetBitmap( WXPixmap bitmap )
1090{
1091 if (!m_refData) m_refData = new wxBitmapRefData();
83df96d6 1092
a11672a4
RR
1093 M_BMPDATA->m_bitmap = bitmap;
1094}
83df96d6 1095
a11672a4
RR
1096WXPixmap wxBitmap::GetPixmap() const
1097{
1098 wxCHECK_MSG( Ok(), (WXPixmap) NULL, wxT("invalid bitmap") );
83df96d6 1099
a11672a4
RR
1100 return M_BMPDATA->m_pixmap;
1101}
83df96d6 1102
a11672a4
RR
1103WXPixmap wxBitmap::GetBitmap() const
1104{
1105 wxCHECK_MSG( Ok(), (WXPixmap) NULL, wxT("invalid bitmap") );
83df96d6 1106
a11672a4 1107 return M_BMPDATA->m_bitmap;
83df96d6 1108}
7266b672 1109
a29c6824
MB
1110WXPixmap wxBitmap::GetDrawable() const
1111{
1112 wxCHECK_MSG( Ok(), (WXPixmap) NULL, wxT("invalid bitmap") );
1113
1114 return M_BMPDATA->m_bpp == 1 ? M_BMPDATA->m_bitmap : M_BMPDATA->m_pixmap;
1115}
1116
a11672a4 1117WXDisplay *wxBitmap::GetDisplay() const
7266b672 1118{
a11672a4 1119 wxCHECK_MSG( Ok(), (WXDisplay*) NULL, wxT("invalid bitmap") );
b6ed4565 1120
a11672a4 1121 return M_BMPDATA->m_display;
7266b672 1122}
a11672a4 1123
a20c04ab
JS
1124#if wxUSE_NANOX
1125// Copy from the drawable to the wxImage
1126bool wxGetImageFromDrawable(GR_DRAW_ID drawable, int srcX, int srcY, int width, int height, wxImage& image)
1127{
f22033e5 1128 GR_SCREEN_INFO sinfo;
a20c04ab
JS
1129 int x, y;
1130 GR_PIXELVAL *pixels;
a20c04ab 1131 GR_PALETTE* palette = NULL;
f22033e5 1132 unsigned char rgb[3], *pp;
a20c04ab 1133
f22033e5 1134 GrGetScreenInfo(&sinfo);
a20c04ab 1135
f22033e5
JS
1136 if (sinfo.pixtype == MWPF_PALETTE) {
1137 if(!(palette = (GR_PALETTE*) malloc(sizeof(GR_PALETTE)))) {
a20c04ab
JS
1138 return FALSE;
1139 }
1140 GrGetSystemPalette(palette);
1141 }
1142
f22033e5 1143 if(!(pixels = (GR_PIXELVAL*) malloc(sizeof(GR_PIXELVAL) * width * height)))
a20c04ab
JS
1144 {
1145 return FALSE;
1146 }
1147
f22033e5 1148 image.Create(width, height);
a20c04ab
JS
1149
1150 GrReadArea(drawable, srcX, srcY, width, height,
1151 pixels);
1152
1153
1154 for(x = 0; x < sinfo.cols; x++) {
1155
1156 pp = (unsigned char *)pixels +
1157 ((x + (y * sinfo.cols)) *
1158 sizeof(GR_PIXELVAL));
1159
1160 switch(sinfo.pixtype) {
1161 /* FIXME: These may need modifying on big endian. */
1162 case MWPF_TRUECOLOR0888:
1163 case MWPF_TRUECOLOR888:
1164 rgb[0] = pp[2];
1165 rgb[1] = pp[1];
1166 rgb[2] = pp[0];
1167 break;
1168 case MWPF_PALETTE:
1169 rgb[0] = palette->palette[pp[0]].r;
1170 rgb[1] = palette->palette[pp[0]].g;
1171 rgb[2] = palette->palette[pp[0]].b;
1172 break;
1173 case MWPF_TRUECOLOR565:
1174 rgb[0] = pp[1] & 0xf8;
1175 rgb[1] = ((pp[1] & 0x07) << 5) |
1176 ((pp[0] & 0xe0) >> 3);
1177 rgb[2] = (pp[0] & 0x1f) << 3;
1178 break;
1179 case MWPF_TRUECOLOR555:
1180 rgb[0] = (pp[1] & 0x7c) << 1;
1181 rgb[1] = ((pp[1] & 0x03) << 6) |
1182 ((pp[0] & 0xe0) >> 2);
1183 rgb[2] = (pp[0] & 0x1f) << 3;
1184 break;
1185 case MWPF_TRUECOLOR332:
1186 rgb[0] = pp[0] & 0xe0;
1187 rgb[1] = (pp[0] & 0x1c) << 3;
1188 rgb[2] = (pp[0] & 0x03) << 6;
1189 break;
1190 default:
1191 fprintf(stderr, "Unsupported pixel "
1192 "format\n");
1193 return 1;
1194 }
1195
1196 image.SetRGB(x, y, rgb[0], rgb[1], rgb[2]);
1197
a20c04ab
JS
1198 }
1199
1200 free(pixels);
f22033e5 1201 if(palette) free(palette);
a20c04ab
JS
1202
1203 return TRUE;
1204}
1205
1206#if 0
1207int GrGetPixelColor(GR_SCREEN_INFO* sinfo, GR_PALETTE* palette, GR_PIXELVAL pixel,
1208 unsigned char* red, unsigned char* green, unsigned char* blue)
1209{
1210 unsigned char rgb[3], *pp;
1211
1212 pp = (unsigned char*) & pixel ;
1213
1214 switch (sinfo.pixtype)
1215 {
1216 /* FIXME: These may need modifying on big endian. */
1217 case MWPF_TRUECOLOR0888:
1218 case MWPF_TRUECOLOR888:
1219 rgb[0] = pp[2];
1220 rgb[1] = pp[1];
1221 rgb[2] = pp[0];
1222 break;
1223 case MWPF_PALETTE:
1224 rgb[0] = palette->palette[pp[0]].r;
1225 rgb[1] = palette->palette[pp[0]].g;
1226 rgb[2] = palette->palette[pp[0]].b;
1227 break;
1228 case MWPF_TRUECOLOR565:
1229 rgb[0] = pp[1] & 0xf8;
1230 rgb[1] = ((pp[1] & 0x07) << 5) |
1231 ((pp[0] & 0xe0) >> 3);
1232 rgb[2] = (pp[0] & 0x1f) << 3;
1233 break;
1234 case MWPF_TRUECOLOR555:
1235 rgb[0] = (pp[1] & 0x7c) << 1;
1236 rgb[1] = ((pp[1] & 0x03) << 6) |
1237 ((pp[0] & 0xe0) >> 2);
1238 rgb[2] = (pp[0] & 0x1f) << 3;
1239 break;
1240 case MWPF_TRUECOLOR332:
1241 rgb[0] = pp[0] & 0xe0;
1242 rgb[1] = (pp[0] & 0x1c) << 3;
1243 rgb[2] = (pp[0] & 0x03) << 6;
1244 break;
1245 default:
1246 fprintf(stderr, "Unsupported pixel format\n");
1247 return 0;
1248 }
1249
1250
1251 *(red) = rgb[0];
1252 *(green) = rgb[1];
1253 *(blue) = rgb[2];
1254 return 1;
1255}
1256#endif
c978d361 1257
a20c04ab
JS
1258#endif
1259 // wxUSE_NANOX
1260
62ad3cb3
MB
1261// ============================================================================
1262// Bitmap handlers
1263// ============================================================================
1264
1265IMPLEMENT_ABSTRACT_CLASS(wxBitmapHandler, wxBitmapHandlerBase);
1266
1267#define M_BMPHANDLERDATA ((wxBitmapRefData *)bitmap->GetRefData())
1268
1269#if wxUSE_XPM
1270
1271#if wxHAVE_LIB_XPM || wxUSE_STREAMS
1272
1273// ----------------------------------------------------------------------------
1274// wxXPMFileHandler
1275// ----------------------------------------------------------------------------
1276
1277class wxXPMFileHandler : public wxBitmapHandler
1278{
1279 DECLARE_DYNAMIC_CLASS(wxXPMFileHandler);
1280public:
1281 wxXPMFileHandler()
1282 {
1283 m_name = "XPM file";
1284 m_extension = "xpm";
1285 m_type = wxBITMAP_TYPE_XPM;
1286 };
1287
1288 virtual bool LoadFile(wxBitmap *bitmap, const wxString& name, long flags,
1289 int desiredWidth, int desiredHeight);
1290
1291 virtual bool SaveFile(const wxBitmap *bitmap, const wxString& name,
1292 int type, const wxPalette *palette = NULL);
1293
1294 virtual bool Create(wxBitmap *bitmap, void *data, long flags,
1295 int width, int height, int depth = 1)
1296 { return FALSE; }
1297};
1298
1299IMPLEMENT_DYNAMIC_CLASS(wxXPMFileHandler, wxBitmapHandler);
1300
1301bool wxXPMFileHandler::LoadFile(wxBitmap *bitmap, const wxString& name,
1302 long flags, int desiredWidth,
1303 int desiredHeight)
1304{
1305#if wxHAVE_LIB_XPM
1306 if (!bitmap->GetRefData())
1307 bitmap->SetRefData( new wxBitmapRefData() );
1308
1309 M_BMPHANDLERDATA->m_display = wxGlobalDisplay();
1310
1311 Display *xdisplay = (Display*) M_BMPHANDLERDATA->m_display;
1312
1313 int xscreen = DefaultScreen( xdisplay );
1314 Window xroot = RootWindow( xdisplay, xscreen );
1315
1316 int bpp = DefaultDepth( xdisplay, xscreen );
1317
1318 XpmAttributes xpmAttr;
1319 xpmAttr.valuemask = XpmReturnInfos; // nothing yet, but get infos back
1320
1321 Pixmap pixmap;
1322 Pixmap mask = 0;
1323
1324 int ErrorStatus = XpmReadFileToPixmap( xdisplay, xroot,
1325 (char*) name.c_str(),
1326 &pixmap, &mask, &xpmAttr);
1327
1328 if (ErrorStatus == XpmSuccess)
1329 {
1330 M_BMPHANDLERDATA->m_width = xpmAttr.width;
1331 M_BMPHANDLERDATA->m_height = xpmAttr.height;
1332
1333 M_BMPHANDLERDATA->m_bpp = bpp; // mono as well?
1334
1335 XpmFreeAttributes(&xpmAttr);
1336
1337 M_BMPHANDLERDATA->m_bitmap = (WXPixmap) pixmap;
1338
1339 if (mask)
1340 {
1341 M_BMPHANDLERDATA->m_mask = new wxMask;
1342 M_BMPHANDLERDATA->m_mask->SetBitmap( (WXPixmap) mask );
1343 M_BMPHANDLERDATA->m_mask->SetDisplay( xdisplay );
1344 }
1345 }
1346 else
1347 {
1348 UnRef();
1349
1350 return FALSE;
1351 }
1352
1353 return TRUE;
1354#elif wxUSE_STREAMS
1355 wxXPMDecoder decoder;
1356 wxFileInputStream stream(name);
1357 if (stream.Ok())
1358 {
1359 wxImage image(decoder.ReadFile(stream));
1360 if (image.Ok())
1361 return CreateFromImage(image);
1362 else
1363 return FALSE;
1364 }
1365 else
1366 return FALSE;
1367#endif // wxHAVE_LIB_XPM / wxUSE_STREAMS
1368}
1369
1370bool wxXPMFileHandler::SaveFile(const wxBitmap *bitmap, const wxString& name,
1371 int type,
1372 const wxPalette *WXUNUSED(palette))
1373{
1374 wxImage image(bitmap->ConvertToImage());
1375 if (image.Ok()) return image.SaveFile( name, (wxBitmapType)type );
1376
1377 return FALSE;
1378}
1379
1380#endif // wxHAVE_LIB_XPM || wxUSE_STREAMS
1381
1382// ----------------------------------------------------------------------------
1383// wxXPMDataHandler
1384// ----------------------------------------------------------------------------
1385
1386class wxXPMDataHandler : public wxBitmapHandler
1387{
1388 DECLARE_DYNAMIC_CLASS(wxXPMDataHandler);
1389public:
1390 wxXPMDataHandler()
1391 {
1392 m_name = "XPM data";
1393 m_extension = "xpm";
1394 m_type = wxBITMAP_TYPE_XPM_DATA;
1395 };
1396
1397 virtual bool LoadFile(wxBitmap *bitmap, const wxString& name, long flags,
1398 int desiredWidth, int desiredHeight)
1399 { return FALSE; }
1400
1401 virtual bool SaveFile(const wxBitmap *bitmap, const wxString& name,
1402 int type, const wxPalette *palette = NULL)
1403 { return FALSE; }
1404
1405 virtual bool Create(wxBitmap *bitmap, void *data, long flags,
1406 int width, int height, int depth = 1);
1407};
1408
1409IMPLEMENT_DYNAMIC_CLASS(wxXPMDataHandler, wxBitmapHandler);
1410
1411bool wxXPMDataHandler::Create(wxBitmap *bitmap, void *bits,
1412 long WXUNUSED(flags),
1413 int width, int height, int WXUNUSED(depth))
1414{
1415#if wxHAVE_LIB_XPM
1416 wxCHECK_MSG( bits != NULL, FALSE, wxT("invalid bitmap data") )
1417
1418 if (!bitmap->GetRefData())
1419 bitmap->SetRefData( new wxBitmapRefData() );
1420
1421 M_BMPHANDLERDATA->m_display = wxGlobalDisplay();
1422
1423 Display *xdisplay = (Display*) M_BMPHANDLERDATA->m_display;
1424
1425 int xscreen = DefaultScreen( xdisplay );
1426 Window xroot = RootWindow( xdisplay, xscreen );
1427
1428 int bpp = DefaultDepth( xdisplay, xscreen );
1429
1430 XpmAttributes xpmAttr;
1431 xpmAttr.valuemask = XpmReturnInfos; // nothing yet, but get infos back
1432
1433 Pixmap pixmap = 0;
1434 Pixmap mask = 0;
1435
1436 int ErrorStatus = XpmCreatePixmapFromData( xdisplay, xroot, (char**) bits,
1437 &pixmap, &mask, &xpmAttr );
1438
1439 if (ErrorStatus == XpmSuccess)
1440 {
1441 M_BMPHANDLERDATA->m_width = xpmAttr.width;
1442 M_BMPHANDLERDATA->m_height = xpmAttr.height;
1443
1444 M_BMPHANDLERDATA->m_bpp = bpp; // mono as well?
1445
1446#if __WXDEBUG__
1447 unsigned int depthRet;
1448 int xRet, yRet;
1449 unsigned int widthRet, heightRet, borderWidthRet;
1450 XGetGeometry( xdisplay, pixmap, &xroot, &xRet, &yRet,
1451 &widthRet, &heightRet, &borderWidthRet, &depthRet);
1452
1453 wxASSERT_MSG( bpp == (int)depthRet, wxT("colour depth mismatch") );
1454#endif
1455
1456 XpmFreeAttributes(&xpmAttr);
1457
1458 M_BMPHANDLERDATA->m_pixmap = (WXPixmap) pixmap;
1459
1460 if (mask)
1461 {
1462 M_BMPHANDLERDATA->m_mask = new wxMask;
1463 M_BMPHANDLERDATA->m_mask->SetBitmap( (WXPixmap) mask );
1464 M_BMPHANDLERDATA->m_mask->SetDisplay( xdisplay );
1465 }
1466 return TRUE;
1467 }
1468 else
1469 {
1470 bitmap->UnRef();
1471
1472 return FALSE;
1473 }
1474#else
1475 wxXPMDecoder decoder;
1476 wxImage image(decoder.ReadData(bits));
1477 if (image.Ok())
1478 return bitmap->CreateFromImage(image);
1479 else
1480 return FALSE;
1481#endif
1482}
1483
1484#endif // wxUSE_XPM
1485
1486// ----------------------------------------------------------------------------
1487// wxXBMDataHandler
1488// ----------------------------------------------------------------------------
1489
1490class WXDLLEXPORT wxXBMDataHandler: public wxBitmapHandler
1491{
1492 DECLARE_DYNAMIC_CLASS(wxXBMDataHandler)
1493public:
1494 inline wxXBMDataHandler()
1495 {
1496 m_name = "XBM data";
1497 m_extension = "xbm";
1498 m_type = wxBITMAP_TYPE_XBM_DATA;
1499 };
1500
1501 virtual bool LoadFile(wxBitmap *bitmap, const wxString& name, long flags,
1502 int desiredWidth, int desiredHeight)
1503 { return FALSE; }
1504
1505 virtual bool SaveFile(const wxBitmap *bitmap, const wxString& name,
1506 int type, const wxPalette *palette = NULL)
1507 { return FALSE; }
1508
1509 virtual bool Create(wxBitmap *bitmap, void *data, long flags,
1510 int width, int height, int depth = 1);
1511};
1512
1513IMPLEMENT_DYNAMIC_CLASS(wxXBMDataHandler, wxBitmapHandler);
1514
1515bool wxXBMDataHandler::Create( wxBitmap *bitmap, void *bits,
1516 long WXUNUSED(flags),
1517 int width, int height, int depth)
1518{
1519#if !wxUSE_NANOX
1520 if (!bitmap->GetRefData())
1521 bitmap->SetRefData( new wxBitmapRefData() );
1522
1523 M_BMPHANDLERDATA->m_display = wxGlobalDisplay();
1524
1525 Display *xdisplay = (Display*) M_BMPHANDLERDATA->m_display;
1526
1527 int xscreen = DefaultScreen( xdisplay );
1528 Window xroot = RootWindow( xdisplay, xscreen );
1529
1530 M_BMPHANDLERDATA->m_mask = (wxMask *) NULL;
1531 M_BMPHANDLERDATA->m_bitmap =
1532 (WXPixmap) XCreateBitmapFromData( xdisplay, xroot,
1533 (char *) bits, width, height );
1534 M_BMPHANDLERDATA->m_width = width;
1535 M_BMPHANDLERDATA->m_height = height;
1536 M_BMPHANDLERDATA->m_bpp = 1;
1537
1538 return TRUE;
1539#endif
1540 wxCHECK_MSG( M_BMPHANDLERDATA->m_bitmap, FALSE,
1541 wxT("couldn't create bitmap") );
1542}
1543
1544void wxBitmap::InitStandardHandlers()
1545{
1546 AddHandler(new wxXBMDataHandler);
1547#if wxUSE_XPM
1548#if wxHAVE_LIB_XPM || wxUSE_STREAMS
1549 AddHandler(new wxXPMFileHandler);
1550#endif
1551 AddHandler(new wxXPMDataHandler);
1552#endif
1553}