]> git.saurik.com Git - wxWidgets.git/blame - src/x11/bitmap.cpp
don't write the strings to the stream one char at a time, it's *horribly* slow
[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
a11672a4
RR
94 wxImage image( bitmap );
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
a11672a4 268#define M_BMPDATA ((wxBitmapRefData *)m_refData)
83df96d6 269
a11672a4 270IMPLEMENT_DYNAMIC_CLASS(wxBitmap,wxGDIObject)
83df96d6 271
a11672a4 272wxBitmap::wxBitmap()
83df96d6 273{
83df96d6
JS
274}
275
a11672a4 276wxBitmap::wxBitmap( int width, int height, int depth )
83df96d6 277{
a11672a4 278 Create( width, height, depth );
83df96d6
JS
279}
280
a11672a4 281bool wxBitmap::Create( int width, int height, int depth )
83df96d6 282{
a11672a4
RR
283 UnRef();
284
285 wxCHECK_MSG( (width > 0) && (height > 0), FALSE, wxT("invalid bitmap size") )
286
287 m_refData = new wxBitmapRefData();
288
289 M_BMPDATA->m_display = wxGlobalDisplay();
290
291 wxASSERT_MSG( M_BMPDATA->m_display, wxT("No display") );
292
293 int xscreen = DefaultScreen( (Display*) M_BMPDATA->m_display );
294 Window xroot = RootWindow( (Display*) M_BMPDATA->m_display, xscreen );
295
296 int bpp = DefaultDepth( (Display*) M_BMPDATA->m_display, xscreen );
297 if (depth == -1) depth = bpp;
298
299 wxCHECK_MSG( (depth == bpp) ||
300 (depth == 1), FALSE, wxT("invalid bitmap depth") )
301
302 M_BMPDATA->m_mask = (wxMask *) NULL;
303 M_BMPDATA->m_width = width;
304 M_BMPDATA->m_height = height;
c79a329d
JS
305
306#if wxUSE_NANOX
888d3c80 307 M_BMPDATA->m_pixmap = (WXPixmap) GrNewPixmap(width, height, NULL);
c79a329d
JS
308 M_BMPDATA->m_bpp = bpp;
309
888d3c80 310 wxASSERT_MSG( M_BMPDATA->m_pixmap, wxT("Bitmap creation failed") );
c79a329d 311#else
a11672a4 312 if (depth == 1)
83df96d6 313 {
a11672a4
RR
314 M_BMPDATA->m_bitmap = (WXPixmap) XCreatePixmap( (Display*) M_BMPDATA->m_display, xroot, width, height, 1 );
315
316 wxASSERT_MSG( M_BMPDATA->m_bitmap, wxT("Bitmap creation failed") );
317
318 M_BMPDATA->m_bpp = 1;
83df96d6
JS
319 }
320 else
83df96d6 321 {
a11672a4
RR
322 M_BMPDATA->m_pixmap = (WXPixmap) XCreatePixmap( (Display*) M_BMPDATA->m_display, xroot, width, height, depth );
323
324 wxASSERT_MSG( M_BMPDATA->m_pixmap, wxT("Pixmap creation failed") );
325
326 M_BMPDATA->m_bpp = depth;
83df96d6 327 }
c79a329d 328#endif
a11672a4 329 return Ok();
83df96d6
JS
330}
331
a11672a4 332bool wxBitmap::CreateFromXpm( const char **bits )
83df96d6 333{
707440dc
JS
334#if wxUSE_XPM
335#if wxHAVE_LIB_XPM
a11672a4
RR
336 UnRef();
337
338 wxCHECK_MSG( bits != NULL, FALSE, wxT("invalid bitmap data") )
339
340 m_refData = new wxBitmapRefData();
341
342 M_BMPDATA->m_display = wxGlobalDisplay();
343
344 Display *xdisplay = (Display*) M_BMPDATA->m_display;
345
346 int xscreen = DefaultScreen( xdisplay );
347 Window xroot = RootWindow( xdisplay, xscreen );
348
349 int bpp = DefaultDepth( xdisplay, xscreen );
350
351 XpmAttributes xpmAttr;
352 xpmAttr.valuemask = XpmReturnInfos; // nothing yet, but get infos back
353
354 Pixmap pixmap;
355 Pixmap mask = 0;
356
357 int ErrorStatus = XpmCreatePixmapFromData( xdisplay, xroot, (char**) bits, &pixmap, &mask, &xpmAttr );
358
359 if (ErrorStatus == XpmSuccess)
83df96d6 360 {
a11672a4
RR
361 M_BMPDATA->m_width = xpmAttr.width;
362 M_BMPDATA->m_height = xpmAttr.height;
83df96d6 363
a11672a4 364 M_BMPDATA->m_bpp = bpp; // mono as well?
83df96d6 365
a11672a4
RR
366#if __WXDEBUG__
367 unsigned int depthRet;
368 int xRet, yRet;
369 unsigned int widthRet, heightRet, borderWidthRet;
370 XGetGeometry( xdisplay, pixmap, &xroot, &xRet, &yRet,
371 &widthRet, &heightRet, &borderWidthRet, &depthRet);
83df96d6 372
63fd5f0b 373 wxASSERT_MSG( bpp == (int)depthRet, wxT("colour depth mismatch") );
a11672a4
RR
374#endif
375
376 XpmFreeAttributes(&xpmAttr);
377
378 M_BMPDATA->m_pixmap = (WXPixmap) pixmap;
379
380 if (mask)
381 {
382 M_BMPDATA->m_mask = new wxMask;
383 M_BMPDATA->m_mask->SetBitmap( (WXPixmap) mask );
384 M_BMPDATA->m_mask->SetDisplay( xdisplay );
385 }
707440dc 386 return TRUE;
a11672a4
RR
387 }
388 else
389 {
390 UnRef();
391
392 return FALSE;
393 }
707440dc
JS
394#else
395 wxXPMDecoder decoder;
396 wxImage image(decoder.ReadData(bits));
397 if (image.Ok())
398 return CreateFromImage(image);
399 else
400 return FALSE;
401#endif
402#endif
403 return FALSE;
83df96d6
JS
404}
405
a11672a4 406bool wxBitmap::CreateFromImage( const wxImage& image, int depth )
83df96d6 407{
c79a329d
JS
408#if wxUSE_NANOX
409 if (!image.Ok())
410 {
411 wxASSERT_MSG(image.Ok(), "Invalid wxImage passed to wxBitmap::CreateFromImage.");
412 return FALSE;
413 }
414
415 int w = image.GetWidth();
416 int h = image.GetHeight();
417
418 if (!Create(w, h, depth))
419 return FALSE;
420
888d3c80
JS
421 // Unfortunately the mask has to be screen-depth since
422 // 1-bpp bitmaps don't seem to be supported
423 // TODO: implement transparent drawing, presumably
424 // by doing several blits as per the Windows
425 // implementation because Nano-X doesn't support
426 // XSetClipMask.
f22033e5
JS
427 // TODO: could perhaps speed this function up
428 // by making a buffer of pixel values,
429 // and then calling GrArea to write that to the
430 // pixmap. See demos/nxroach.c.
c79a329d 431
888d3c80
JS
432 bool hasMask = image.HasMask();
433
434 GC pixmapGC = GrNewGC();
435 Pixmap pixmap = (Pixmap) GetPixmap();
436
437 GC maskGC = 0;
438 Pixmap maskPixmap = 0;
439
440 unsigned char maskR = 0;
441 unsigned char maskG = 0;
442 unsigned char maskB = 0;
443
444 if (hasMask)
445 {
446 maskR = image.GetMaskRed();
447 maskG = image.GetMaskGreen();
448 maskB = image.GetMaskBlue();
449
450 maskGC = GrNewGC();
451 maskPixmap = GrNewPixmap(w, h, 0);
452 if (!maskPixmap)
453 hasMask = FALSE;
454 else
455 {
456 wxMask* mask = new wxMask;
457 mask->SetBitmap((WXPixmap) maskPixmap);
458 SetMask(mask);
459 }
460 }
461
462 GR_COLOR lastPixmapColour = 0;
463 GR_COLOR lastMaskColour = 0;
c79a329d
JS
464
465 int i, j;
466 for (i = 0; i < w; i++)
467 {
468 for (j = 0; j < h; j++)
469 {
470 unsigned char red = image.GetRed(i, j);
471 unsigned char green = image.GetGreen(i, j);
472 unsigned char blue = image.GetBlue(i, j);
c79a329d 473
888d3c80
JS
474 GR_COLOR colour = GR_RGB(red, green, blue);
475
476 // Efficiency measure
477 if (colour != lastPixmapColour || (i == 0 && j == 0))
478 {
479 GrSetGCForeground(pixmapGC, colour);
480 lastPixmapColour = colour;
481 }
482
483 GrPoint(pixmap, pixmapGC, i, j);
c79a329d 484
c79a329d
JS
485 if (hasMask)
486 {
487 // scan the bitmap for the transparent colour and set the corresponding
488 // pixels in the mask to BLACK and the rest to WHITE
489 if (maskR == red && maskG == green && maskB == blue)
888d3c80
JS
490 {
491 colour = GR_RGB(0, 0, 0);
492 }
c79a329d 493 else
888d3c80
JS
494 {
495 colour = GR_RGB(255, 255, 255);
496 }
497 if (colour != lastMaskColour || (i == 0 && j == 0))
498 {
499 GrSetGCForeground(maskGC, colour);
500 lastMaskColour = colour;
501 }
502 GrPoint(maskPixmap, maskGC, i, j);
c79a329d 503 }
c79a329d
JS
504 }
505 }
888d3c80
JS
506
507 GrDestroyGC(pixmapGC);
508 if (hasMask)
509 GrDestroyGC(maskGC);
c79a329d
JS
510
511 return TRUE;
512#else
513 // !wxUSE_NANOX
514
a11672a4 515 UnRef();
83df96d6 516
a11672a4
RR
517 wxCHECK_MSG( image.Ok(), FALSE, wxT("invalid image") )
518 wxCHECK_MSG( depth == -1, FALSE, wxT("invalid bitmap depth") )
83df96d6 519
a11672a4 520 m_refData = new wxBitmapRefData();
83df96d6 521
a11672a4
RR
522 M_BMPDATA->m_display = wxGlobalDisplay();
523
524 Display *xdisplay = (Display*) M_BMPDATA->m_display;
525
526 int xscreen = DefaultScreen( xdisplay );
527 Window xroot = RootWindow( xdisplay, xscreen );
528 Visual* xvisual = DefaultVisual( xdisplay, xscreen );
529
dc4025af 530 int bpp = wxTheApp->m_visualDepth;
a11672a4
RR
531
532 int width = image.GetWidth();
533 int height = image.GetHeight();
534 M_BMPDATA->m_width = width;
535 M_BMPDATA->m_height = height;
83df96d6 536
a11672a4
RR
537 if (depth != 1) depth = bpp;
538 M_BMPDATA->m_bpp = depth;
539
540 if (depth == 1)
541 {
542 wxFAIL_MSG( "mono images later" );
543 }
544 else
545 {
546 // Create image
547
548 XImage *data_image = XCreateImage( xdisplay, xvisual, bpp, ZPixmap, 0, 0, width, height, 32, 0 );
549 data_image->data = (char*) malloc( data_image->bytes_per_line * data_image->height );
550
551 if (data_image->data == NULL)
552 {
553 wxLogError( wxT("Out of memory.") ); // TODO clean
554 return FALSE;
555 }
83df96d6 556
a11672a4 557 M_BMPDATA->m_pixmap = (WXPixmap) XCreatePixmap( xdisplay, xroot, width, height, depth );
83df96d6 558
a11672a4 559 // Create mask
83df96d6 560
a11672a4
RR
561 XImage *mask_image = (XImage*) NULL;
562 if (image.HasMask())
563 {
564 mask_image = XCreateImage( xdisplay, xvisual, 1, ZPixmap, 0, 0, width, height, 32, 0 );
565 mask_image->data = (char*) malloc( mask_image->bytes_per_line * mask_image->height );
566
567 if (mask_image->data == NULL)
568 {
569 wxLogError( wxT("Out of memory.") ); // TODO clean
570 return FALSE;
571 }
572
573 wxMask *mask = new wxMask();
574 mask->SetDisplay( xdisplay );
575 mask->SetBitmap( (WXPixmap) XCreatePixmap( xdisplay, xroot, width, height, 1 ) );
83df96d6 576
a11672a4
RR
577 SetMask( mask );
578 }
83df96d6 579
a11672a4 580 if (bpp < 8) bpp = 8;
83df96d6 581
a11672a4 582 // Render
83df96d6 583
a11672a4
RR
584 enum byte_order { RGB, RBG, BRG, BGR, GRB, GBR };
585 byte_order b_o = RGB;
83df96d6 586
a11672a4
RR
587 if (bpp > 8)
588 {
dc4025af
RR
589 if ((wxTheApp->m_visualRedMask > wxTheApp->m_visualGreenMask) && (wxTheApp->m_visualGreenMask > wxTheApp->m_visualBlueMask)) b_o = RGB;
590 else if ((wxTheApp->m_visualRedMask > wxTheApp->m_visualBlueMask) && (wxTheApp->m_visualBlueMask > wxTheApp->m_visualGreenMask)) b_o = RBG;
591 else if ((wxTheApp->m_visualBlueMask > wxTheApp->m_visualRedMask) && (wxTheApp->m_visualRedMask > wxTheApp->m_visualGreenMask)) b_o = BRG;
592 else if ((wxTheApp->m_visualBlueMask > wxTheApp->m_visualGreenMask) && (wxTheApp->m_visualGreenMask > wxTheApp->m_visualRedMask)) b_o = BGR;
593 else if ((wxTheApp->m_visualGreenMask > wxTheApp->m_visualRedMask) && (wxTheApp->m_visualRedMask > wxTheApp->m_visualBlueMask)) b_o = GRB;
594 else if ((wxTheApp->m_visualGreenMask > wxTheApp->m_visualBlueMask) && (wxTheApp->m_visualBlueMask > wxTheApp->m_visualRedMask)) b_o = GBR;
a11672a4 595 }
83df96d6 596
a11672a4
RR
597 int r_mask = image.GetMaskRed();
598 int g_mask = image.GetMaskGreen();
599 int b_mask = image.GetMaskBlue();
83df96d6 600
a11672a4
RR
601 unsigned char* data = image.GetData();
602 wxASSERT_MSG( data, "No image data" );
dc4025af
RR
603
604 unsigned char *colorCube = wxTheApp->m_colorCube;
83df96d6 605
a11672a4 606 bool hasMask = image.HasMask();
83df96d6 607
a11672a4
RR
608 int index = 0;
609 for (int y = 0; y < height; y++)
610 {
611 for (int x = 0; x < width; x++)
612 {
613 int r = data[index];
614 index++;
615 int g = data[index];
616 index++;
617 int b = data[index];
618 index++;
619
620 if (hasMask)
621 {
622 if ((r == r_mask) && (b == b_mask) && (g == g_mask))
623 XPutPixel( mask_image, x, y, 0 );
624 else
625 XPutPixel( mask_image, x, y, 1 );
626 }
83df96d6 627
a11672a4
RR
628 switch (bpp)
629 {
630 case 8:
631 {
632 int pixel = 0;
dc4025af 633 pixel = colorCube[ ((r & 0xf8) << 7) + ((g & 0xf8) << 2) + ((b & 0xf8) >> 3) ];
a11672a4
RR
634 XPutPixel( data_image, x, y, pixel );
635 break;
636 }
637 case 12: // SGI only
638 {
639 int pixel = 0;
640 switch (b_o)
641 {
642 case RGB: pixel = ((r & 0xf0) << 4) | (g & 0xf0) | ((b & 0xf0) >> 4); break;
643 case RBG: pixel = ((r & 0xf0) << 4) | (b & 0xf0) | ((g & 0xf0) >> 4); break;
644 case GRB: pixel = ((g & 0xf0) << 4) | (r & 0xf0) | ((b & 0xf0) >> 4); break;
645 case GBR: pixel = ((g & 0xf0) << 4) | (b & 0xf0) | ((r & 0xf0) >> 4); break;
646 case BRG: pixel = ((b & 0xf0) << 4) | (r & 0xf0) | ((g & 0xf0) >> 4); break;
647 case BGR: pixel = ((b & 0xf0) << 4) | (g & 0xf0) | ((r & 0xf0) >> 4); break;
648 }
649 XPutPixel( data_image, x, y, pixel );
650 break;
651 }
652 case 15:
653 {
654 int pixel = 0;
655 switch (b_o)
656 {
657 case RGB: pixel = ((r & 0xf8) << 7) | ((g & 0xf8) << 2) | ((b & 0xf8) >> 3); break;
658 case RBG: pixel = ((r & 0xf8) << 7) | ((b & 0xf8) << 2) | ((g & 0xf8) >> 3); break;
659 case GRB: pixel = ((g & 0xf8) << 7) | ((r & 0xf8) << 2) | ((b & 0xf8) >> 3); break;
660 case GBR: pixel = ((g & 0xf8) << 7) | ((b & 0xf8) << 2) | ((r & 0xf8) >> 3); break;
661 case BRG: pixel = ((b & 0xf8) << 7) | ((r & 0xf8) << 2) | ((g & 0xf8) >> 3); break;
662 case BGR: pixel = ((b & 0xf8) << 7) | ((g & 0xf8) << 2) | ((r & 0xf8) >> 3); break;
663 }
664 XPutPixel( data_image, x, y, pixel );
665 break;
666 }
667 case 16:
668 {
669 // I actually don't know if for 16-bit displays, it is alway the green
670 // component or the second component which has 6 bits.
671 int pixel = 0;
672 switch (b_o)
673 {
674 case RGB: pixel = ((r & 0xf8) << 8) | ((g & 0xfc) << 3) | ((b & 0xf8) >> 3); break;
675 case RBG: pixel = ((r & 0xf8) << 8) | ((b & 0xfc) << 3) | ((g & 0xf8) >> 3); break;
676 case GRB: pixel = ((g & 0xf8) << 8) | ((r & 0xfc) << 3) | ((b & 0xf8) >> 3); break;
677 case GBR: pixel = ((g & 0xf8) << 8) | ((b & 0xfc) << 3) | ((r & 0xf8) >> 3); break;
678 case BRG: pixel = ((b & 0xf8) << 8) | ((r & 0xfc) << 3) | ((g & 0xf8) >> 3); break;
679 case BGR: pixel = ((b & 0xf8) << 8) | ((g & 0xfc) << 3) | ((r & 0xf8) >> 3); break;
680 }
681 XPutPixel( data_image, x, y, pixel );
682 break;
683 }
684 case 32:
685 case 24:
686 {
687 int pixel = 0;
688 switch (b_o)
689 {
690 case RGB: pixel = (r << 16) | (g << 8) | b; break;
691 case RBG: pixel = (r << 16) | (b << 8) | g; break;
692 case BRG: pixel = (b << 16) | (r << 8) | g; break;
693 case BGR: pixel = (b << 16) | (g << 8) | r; break;
694 case GRB: pixel = (g << 16) | (r << 8) | b; break;
695 case GBR: pixel = (g << 16) | (b << 8) | r; break;
696 }
697 XPutPixel( data_image, x, y, pixel );
698 }
699 default: break;
700 }
701 } // for
702 } // for
83df96d6 703
a11672a4 704 // Blit picture
83df96d6 705
a11672a4
RR
706 GC gc = XCreateGC( xdisplay, (Pixmap) M_BMPDATA->m_pixmap, 0, NULL );
707 XPutImage( xdisplay, (Pixmap) M_BMPDATA->m_pixmap, gc, data_image, 0, 0, 0, 0, width, height );
a11672a4
RR
708 XDestroyImage( data_image );
709 XFreeGC( xdisplay, gc );
83df96d6 710
a11672a4
RR
711 // Blit mask
712
713 if (image.HasMask())
714 {
715 GC gc = XCreateGC( xdisplay, (Pixmap) GetMask()->GetBitmap(), 0, NULL );
011c1748 716 XPutImage( xdisplay, (Pixmap) GetMask()->GetBitmap(), gc, mask_image, 0, 0, 0, 0, width, height );
a11672a4
RR
717
718 XDestroyImage( mask_image );
719 XFreeGC( xdisplay, gc );
720 }
721 }
83df96d6 722
83df96d6 723 return TRUE;
c79a329d
JS
724#endif
725 // wxUSE_NANOX
83df96d6
JS
726}
727
a11672a4 728wxImage wxBitmap::ConvertToImage() const
83df96d6 729{
a11672a4 730 wxImage image;
83df96d6 731
a11672a4 732 wxCHECK_MSG( Ok(), wxNullImage, wxT("invalid bitmap") );
83df96d6 733
a11672a4
RR
734 Display *xdisplay = (Display*) M_BMPDATA->m_display;
735 wxASSERT_MSG( xdisplay, wxT("No display") );
c79a329d
JS
736
737#if wxUSE_NANOX
8601b2e1 738 //int bpp = DefaultDepth(xdisplay, xscreen);
f22033e5 739 wxGetImageFromDrawable((Pixmap) GetPixmap(), 0, 0, GetWidth(), GetHeight(), image);
c79a329d 740 return image;
c79a329d
JS
741#else
742 // !wxUSE_NANOX
8601b2e1 743 int bpp = wxTheApp->m_visualDepth;
a11672a4
RR
744 XImage *x_image = NULL;
745 if (GetPixmap())
746 {
747 x_image = XGetImage( xdisplay, (Pixmap) GetPixmap(),
748 0, 0,
749 GetWidth(), GetHeight(),
750 AllPlanes, ZPixmap );
751 } else
752 if (GetBitmap())
753 {
754 x_image = XGetImage( xdisplay, (Pixmap) GetBitmap(),
755 0, 0,
756 GetWidth(), GetHeight(),
757 AllPlanes, ZPixmap );
758 } else
759 {
760 wxFAIL_MSG( wxT("Ill-formed bitmap") );
761 }
83df96d6 762
a11672a4 763 wxCHECK_MSG( x_image, wxNullImage, wxT("couldn't create image") );
83df96d6 764
a11672a4
RR
765 image.Create( GetWidth(), GetHeight() );
766 char unsigned *data = image.GetData();
83df96d6 767
a11672a4
RR
768 if (!data)
769 {
770 XDestroyImage( x_image );
771 wxFAIL_MSG( wxT("couldn't create image") );
772 return wxNullImage;
773 }
83df96d6 774
a11672a4
RR
775 XImage *x_image_mask = NULL;
776 if (GetMask())
777 {
778 x_image_mask = XGetImage( xdisplay, (Pixmap) GetMask()->GetBitmap(),
779 0, 0,
780 GetWidth(), GetHeight(),
781 AllPlanes, ZPixmap );
83df96d6 782
a11672a4
RR
783 image.SetMaskColour( 16, 16, 16 ); // anything unlikely and dividable
784 }
83df96d6 785
a11672a4
RR
786 int red_shift_right = 0;
787 int green_shift_right = 0;
788 int blue_shift_right = 0;
789 int red_shift_left = 0;
790 int green_shift_left = 0;
791 int blue_shift_left = 0;
792 bool use_shift = FALSE;
83df96d6 793
a11672a4
RR
794 if (GetPixmap())
795 {
dc4025af
RR
796 red_shift_right = wxTheApp->m_visualRedShift;
797 red_shift_left = 8-wxTheApp->m_visualRedPrec;
798 green_shift_right = wxTheApp->m_visualGreenShift;
799 green_shift_left = 8-wxTheApp->m_visualGreenPrec;
800 blue_shift_right = wxTheApp->m_visualBlueShift;
801 blue_shift_left = 8-wxTheApp->m_visualBluePrec;
a11672a4 802
dc4025af 803 use_shift = (wxTheApp->m_visualType == GrayScale) || (wxTheApp->m_visualType != PseudoColor);
a11672a4 804 }
dc4025af 805
a11672a4
RR
806 if (GetBitmap())
807 {
808 bpp = 1;
809 }
83df96d6 810
dc4025af 811 XColor *colors = (XColor*) wxTheApp->m_visualColormap;
83df96d6 812
288efe84
JS
813 int width = GetWidth();
814 int height = GetHeight();
a11672a4 815 long pos = 0;
288efe84 816 for (int j = 0; j < height; j++)
a11672a4 817 {
288efe84 818 for (int i = 0; i < width; i++)
a11672a4
RR
819 {
820 unsigned long pixel = XGetPixel( x_image, i, j );
821 if (bpp == 1)
822 {
823 if (pixel == 0)
824 {
825 data[pos] = 0;
826 data[pos+1] = 0;
827 data[pos+2] = 0;
828 }
829 else
830 {
831 data[pos] = 255;
832 data[pos+1] = 255;
833 data[pos+2] = 255;
834 }
835 }
836 else if (use_shift)
837 {
838 data[pos] = (pixel >> red_shift_right) << red_shift_left;
839 data[pos+1] = (pixel >> green_shift_right) << green_shift_left;
840 data[pos+2] = (pixel >> blue_shift_right) << blue_shift_left;
841 }
dc4025af 842 else if (colors)
a11672a4 843 {
dc4025af
RR
844 data[pos] = colors[pixel].red >> 8;
845 data[pos+1] = colors[pixel].green >> 8;
846 data[pos+2] = colors[pixel].blue >> 8;
a11672a4 847 }
a11672a4
RR
848 else
849 {
850 wxFAIL_MSG( wxT("Image conversion failed. Unknown visual type.") );
851 }
83df96d6 852
a11672a4
RR
853 if (x_image_mask)
854 {
855 int mask_pixel = XGetPixel( x_image_mask, i, j );
856 if (mask_pixel == 0)
857 {
858 data[pos] = 16;
859 data[pos+1] = 16;
860 data[pos+2] = 16;
861 }
862 }
83df96d6 863
a11672a4
RR
864 pos += 3;
865 }
866 }
83df96d6 867
a11672a4
RR
868 XDestroyImage( x_image );
869 if (x_image_mask) XDestroyImage( x_image_mask );
a11672a4 870 return image;
c79a329d
JS
871#endif
872 // wxUSE_NANOX
83df96d6
JS
873}
874
a11672a4 875wxBitmap::wxBitmap( const wxBitmap& bmp )
83df96d6 876{
a11672a4 877 Ref( bmp );
83df96d6
JS
878}
879
a11672a4 880wxBitmap::wxBitmap( const wxString &filename, int type )
83df96d6 881{
a11672a4 882 LoadFile( filename, type );
83df96d6
JS
883}
884
a11672a4 885wxBitmap::wxBitmap( const char bits[], int width, int height, int WXUNUSED(depth) )
83df96d6 886{
c79a329d 887#if !wxUSE_NANOX
a11672a4 888 m_refData = new wxBitmapRefData();
83df96d6 889
a11672a4 890 M_BMPDATA->m_display = wxGlobalDisplay();
83df96d6 891
a11672a4
RR
892 Display *xdisplay = (Display*) M_BMPDATA->m_display;
893
894 int xscreen = DefaultScreen( xdisplay );
895 Window xroot = RootWindow( xdisplay, xscreen );
896
897 M_BMPDATA->m_mask = (wxMask *) NULL;
898 M_BMPDATA->m_bitmap = (WXPixmap) XCreateBitmapFromData( xdisplay, xroot, (char *) bits, width, height );
899 M_BMPDATA->m_width = width;
900 M_BMPDATA->m_height = height;
901 M_BMPDATA->m_bpp = 1;
c79a329d 902#endif
a11672a4 903 wxCHECK_RET( M_BMPDATA->m_bitmap, wxT("couldn't create bitmap") );
83df96d6
JS
904}
905
a11672a4 906wxBitmap::~wxBitmap()
83df96d6 907{
83df96d6
JS
908}
909
a11672a4 910wxBitmap& wxBitmap::operator = ( const wxBitmap& bmp )
83df96d6 911{
a11672a4
RR
912 if ( m_refData != bmp.m_refData )
913 Ref( bmp );
83df96d6 914
a11672a4
RR
915 return *this;
916}
83df96d6 917
a11672a4
RR
918bool wxBitmap::operator == ( const wxBitmap& bmp ) const
919{
920 return m_refData == bmp.m_refData;
921}
83df96d6 922
a11672a4
RR
923bool wxBitmap::operator != ( const wxBitmap& bmp ) const
924{
925 return m_refData != bmp.m_refData;
926}
83df96d6 927
a11672a4
RR
928bool wxBitmap::Ok() const
929{
930 return (m_refData != NULL);
931}
83df96d6 932
a11672a4
RR
933int wxBitmap::GetHeight() const
934{
935 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
83df96d6 936
a11672a4
RR
937 return M_BMPDATA->m_height;
938}
83df96d6 939
a11672a4
RR
940int wxBitmap::GetWidth() const
941{
942 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
83df96d6 943
a11672a4
RR
944 return M_BMPDATA->m_width;
945}
83df96d6 946
a11672a4
RR
947int wxBitmap::GetDepth() const
948{
949 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
83df96d6 950
a11672a4
RR
951 return M_BMPDATA->m_bpp;
952}
83df96d6 953
a11672a4
RR
954wxMask *wxBitmap::GetMask() const
955{
956 wxCHECK_MSG( Ok(), (wxMask *) NULL, wxT("invalid bitmap") );
83df96d6 957
a11672a4
RR
958 return M_BMPDATA->m_mask;
959}
83df96d6 960
a11672a4
RR
961void wxBitmap::SetMask( wxMask *mask )
962{
963 wxCHECK_RET( Ok(), wxT("invalid bitmap") );
83df96d6 964
a11672a4 965 if (M_BMPDATA->m_mask) delete M_BMPDATA->m_mask;
83df96d6 966
a11672a4
RR
967 M_BMPDATA->m_mask = mask;
968}
83df96d6 969
a11672a4
RR
970bool wxBitmap::CopyFromIcon(const wxIcon& icon)
971{
972 *this = icon;
973 return TRUE;
974}
83df96d6 975
a11672a4
RR
976wxBitmap wxBitmap::GetSubBitmap( const wxRect& rect) const
977{
978 wxCHECK_MSG( Ok() &&
979 (rect.x >= 0) && (rect.y >= 0) &&
980 (rect.x+rect.width <= M_BMPDATA->m_width) && (rect.y+rect.height <= M_BMPDATA->m_height),
981 wxNullBitmap, wxT("invalid bitmap or bitmap region") );
83df96d6 982
a11672a4
RR
983 wxBitmap ret( rect.width, rect.height, M_BMPDATA->m_bpp );
984 wxASSERT_MSG( ret.Ok(), wxT("GetSubBitmap error") );
83df96d6 985
a11672a4
RR
986
987 wxFAIL_MSG( "wxBitmap::GetSubBitmap not yet implemented" );
988
989#if 0
990 if (ret.GetPixmap())
83df96d6 991 {
a11672a4
RR
992 GdkGC *gc = gdk_gc_new( ret.GetPixmap() );
993 gdk_draw_pixmap( ret.GetPixmap(), gc, GetPixmap(), rect.x, rect.y, 0, 0, rect.width, rect.height );
994 gdk_gc_destroy( gc );
995 }
996 else
997 {
998 GdkGC *gc = gdk_gc_new( ret.GetBitmap() );
999 gdk_wx_draw_bitmap( ret.GetBitmap(), gc, GetBitmap(), rect.x, rect.y, 0, 0, rect.width, rect.height );
1000 gdk_gc_destroy( gc );
83df96d6
JS
1001 }
1002
a11672a4 1003 if (GetMask())
83df96d6 1004 {
a11672a4
RR
1005 wxMask *mask = new wxMask;
1006 mask->m_bitmap = gdk_pixmap_new( wxGetRootWindow()->window, rect.width, rect.height, 1 );
1007
1008 GdkGC *gc = gdk_gc_new( mask->m_bitmap );
1009 gdk_wx_draw_bitmap( mask->m_bitmap, gc, M_BMPDATA->m_mask->m_bitmap, 0, 0, rect.x, rect.y, rect.width, rect.height );
1010 gdk_gc_destroy( gc );
83df96d6 1011
a11672a4 1012 ret.SetMask( mask );
83df96d6 1013 }
a11672a4 1014#endif
83df96d6 1015
a11672a4
RR
1016 return ret;
1017}
83df96d6 1018
a11672a4
RR
1019bool wxBitmap::SaveFile( const wxString &name, int type, wxPalette *WXUNUSED(palette) )
1020{
1021 wxCHECK_MSG( Ok(), FALSE, wxT("invalid bitmap") );
83df96d6 1022
a11672a4 1023 // Try to save the bitmap via wxImage handlers:
83df96d6 1024 {
a11672a4
RR
1025 wxImage image( *this );
1026 if (image.Ok()) return image.SaveFile( name, type );
1027 }
83df96d6 1028
a11672a4
RR
1029 return FALSE;
1030}
83df96d6 1031
a11672a4
RR
1032bool wxBitmap::LoadFile( const wxString &name, int type )
1033{
1034 UnRef();
83df96d6 1035
a11672a4 1036 if (!wxFileExists(name)) return FALSE;
83df96d6 1037
83df96d6 1038
a11672a4 1039 if (type == wxBITMAP_TYPE_XPM)
83df96d6 1040 {
707440dc
JS
1041#if wxUSE_XPM
1042#if wxHAVE_LIB_XPM
a11672a4 1043 m_refData = new wxBitmapRefData();
83df96d6 1044
a11672a4
RR
1045 M_BMPDATA->m_display = wxGlobalDisplay();
1046
1047 Display *xdisplay = (Display*) M_BMPDATA->m_display;
1048
1049 int xscreen = DefaultScreen( xdisplay );
1050 Window xroot = RootWindow( xdisplay, xscreen );
1051
1052 int bpp = DefaultDepth( xdisplay, xscreen );
83df96d6 1053
a11672a4
RR
1054 XpmAttributes xpmAttr;
1055 xpmAttr.valuemask = XpmReturnInfos; // nothing yet, but get infos back
83df96d6 1056
a11672a4
RR
1057 Pixmap pixmap;
1058 Pixmap mask = 0;
1059
1060 int ErrorStatus = XpmReadFileToPixmap( xdisplay, xroot, (char*) name.c_str(), &pixmap, &mask, &xpmAttr);
1061
1062 if (ErrorStatus == XpmSuccess)
1063 {
1064 M_BMPDATA->m_width = xpmAttr.width;
1065 M_BMPDATA->m_height = xpmAttr.height;
83df96d6 1066
a11672a4 1067 M_BMPDATA->m_bpp = bpp; // mono as well?
83df96d6 1068
a11672a4
RR
1069 XpmFreeAttributes(&xpmAttr);
1070
1071 M_BMPDATA->m_bitmap = (WXPixmap) pixmap;
1072
1073 if (mask)
1074 {
1075 M_BMPDATA->m_mask = new wxMask;
1076 M_BMPDATA->m_mask->SetBitmap( (WXPixmap) mask );
1077 M_BMPDATA->m_mask->SetDisplay( xdisplay );
1078 }
1079 }
1080 else
1081 {
1082 UnRef();
1083
1084 return FALSE;
1085 }
707440dc
JS
1086#else
1087#if wxUSE_STREAMS
1088 wxXPMDecoder decoder;
1089 wxFileInputStream stream(name);
1090 if (stream.Ok())
1091 {
c79a329d 1092 wxImage image(decoder.ReadFile(stream));
707440dc
JS
1093 if (image.Ok())
1094 return CreateFromImage(image);
1095 else
1096 return FALSE;
1097 }
1098 else
1099 return FALSE;
1100#else
1101 return FALSE;
1102#endif
1103 // wxUSE_STREAMS
1104#endif
1105 // wxHAVE_LIB_XPM
1106#endif
1107 // wxUSE_XPM
1108 return FALSE;
a11672a4
RR
1109 }
1110 else // try if wxImage can load it
1111 {
1112 wxImage image;
1113 if (!image.LoadFile( name, type )) return FALSE;
1114 if (image.Ok()) *this = image.ConvertToBitmap();
1115 else return FALSE;
83df96d6 1116 }
83df96d6
JS
1117
1118 return TRUE;
1119}
1120
a11672a4 1121wxPalette *wxBitmap::GetPalette() const
83df96d6 1122{
a11672a4 1123 if (!Ok()) return (wxPalette *) NULL;
83df96d6 1124
a11672a4
RR
1125 return M_BMPDATA->m_palette;
1126}
83df96d6 1127
a11672a4
RR
1128void wxBitmap::SetHeight( int height )
1129{
1130 if (!m_refData) m_refData = new wxBitmapRefData();
83df96d6 1131
a11672a4
RR
1132 M_BMPDATA->m_height = height;
1133}
83df96d6 1134
a11672a4
RR
1135void wxBitmap::SetWidth( int width )
1136{
1137 if (!m_refData) m_refData = new wxBitmapRefData();
83df96d6 1138
a11672a4
RR
1139 M_BMPDATA->m_width = width;
1140}
83df96d6 1141
a11672a4
RR
1142void wxBitmap::SetDepth( int depth )
1143{
1144 if (!m_refData) m_refData = new wxBitmapRefData();
83df96d6 1145
a11672a4
RR
1146 M_BMPDATA->m_bpp = depth;
1147}
83df96d6 1148
a11672a4
RR
1149void wxBitmap::SetPixmap( WXPixmap pixmap )
1150{
1151 if (!m_refData) m_refData = new wxBitmapRefData();
83df96d6 1152
a11672a4
RR
1153 M_BMPDATA->m_pixmap = pixmap;
1154}
83df96d6 1155
a11672a4
RR
1156void wxBitmap::SetBitmap( WXPixmap bitmap )
1157{
1158 if (!m_refData) m_refData = new wxBitmapRefData();
83df96d6 1159
a11672a4
RR
1160 M_BMPDATA->m_bitmap = bitmap;
1161}
83df96d6 1162
a11672a4
RR
1163WXPixmap wxBitmap::GetPixmap() const
1164{
1165 wxCHECK_MSG( Ok(), (WXPixmap) NULL, wxT("invalid bitmap") );
83df96d6 1166
a11672a4
RR
1167 return M_BMPDATA->m_pixmap;
1168}
83df96d6 1169
a11672a4
RR
1170WXPixmap wxBitmap::GetBitmap() const
1171{
1172 wxCHECK_MSG( Ok(), (WXPixmap) NULL, wxT("invalid bitmap") );
83df96d6 1173
a11672a4 1174 return M_BMPDATA->m_bitmap;
83df96d6 1175}
7266b672 1176
a11672a4 1177WXDisplay *wxBitmap::GetDisplay() const
7266b672 1178{
a11672a4 1179 wxCHECK_MSG( Ok(), (WXDisplay*) NULL, wxT("invalid bitmap") );
b6ed4565 1180
a11672a4 1181 return M_BMPDATA->m_display;
7266b672 1182}
a11672a4 1183
a20c04ab
JS
1184#if wxUSE_NANOX
1185// Copy from the drawable to the wxImage
1186bool wxGetImageFromDrawable(GR_DRAW_ID drawable, int srcX, int srcY, int width, int height, wxImage& image)
1187{
f22033e5 1188 GR_SCREEN_INFO sinfo;
a20c04ab
JS
1189 int x, y;
1190 GR_PIXELVAL *pixels;
a20c04ab 1191 GR_PALETTE* palette = NULL;
f22033e5 1192 unsigned char rgb[3], *pp;
a20c04ab 1193
f22033e5 1194 GrGetScreenInfo(&sinfo);
a20c04ab 1195
f22033e5
JS
1196 if (sinfo.pixtype == MWPF_PALETTE) {
1197 if(!(palette = (GR_PALETTE*) malloc(sizeof(GR_PALETTE)))) {
a20c04ab
JS
1198 return FALSE;
1199 }
1200 GrGetSystemPalette(palette);
1201 }
1202
f22033e5 1203 if(!(pixels = (GR_PIXELVAL*) malloc(sizeof(GR_PIXELVAL) * width * height)))
a20c04ab
JS
1204 {
1205 return FALSE;
1206 }
1207
f22033e5 1208 image.Create(width, height);
a20c04ab
JS
1209
1210 GrReadArea(drawable, srcX, srcY, width, height,
1211 pixels);
1212
1213
1214 for(x = 0; x < sinfo.cols; x++) {
1215
1216 pp = (unsigned char *)pixels +
1217 ((x + (y * sinfo.cols)) *
1218 sizeof(GR_PIXELVAL));
1219
1220 switch(sinfo.pixtype) {
1221 /* FIXME: These may need modifying on big endian. */
1222 case MWPF_TRUECOLOR0888:
1223 case MWPF_TRUECOLOR888:
1224 rgb[0] = pp[2];
1225 rgb[1] = pp[1];
1226 rgb[2] = pp[0];
1227 break;
1228 case MWPF_PALETTE:
1229 rgb[0] = palette->palette[pp[0]].r;
1230 rgb[1] = palette->palette[pp[0]].g;
1231 rgb[2] = palette->palette[pp[0]].b;
1232 break;
1233 case MWPF_TRUECOLOR565:
1234 rgb[0] = pp[1] & 0xf8;
1235 rgb[1] = ((pp[1] & 0x07) << 5) |
1236 ((pp[0] & 0xe0) >> 3);
1237 rgb[2] = (pp[0] & 0x1f) << 3;
1238 break;
1239 case MWPF_TRUECOLOR555:
1240 rgb[0] = (pp[1] & 0x7c) << 1;
1241 rgb[1] = ((pp[1] & 0x03) << 6) |
1242 ((pp[0] & 0xe0) >> 2);
1243 rgb[2] = (pp[0] & 0x1f) << 3;
1244 break;
1245 case MWPF_TRUECOLOR332:
1246 rgb[0] = pp[0] & 0xe0;
1247 rgb[1] = (pp[0] & 0x1c) << 3;
1248 rgb[2] = (pp[0] & 0x03) << 6;
1249 break;
1250 default:
1251 fprintf(stderr, "Unsupported pixel "
1252 "format\n");
1253 return 1;
1254 }
1255
1256 image.SetRGB(x, y, rgb[0], rgb[1], rgb[2]);
1257
a20c04ab
JS
1258 }
1259
1260 free(pixels);
f22033e5 1261 if(palette) free(palette);
a20c04ab
JS
1262
1263 return TRUE;
1264}
1265
1266#if 0
1267int GrGetPixelColor(GR_SCREEN_INFO* sinfo, GR_PALETTE* palette, GR_PIXELVAL pixel,
1268 unsigned char* red, unsigned char* green, unsigned char* blue)
1269{
1270 unsigned char rgb[3], *pp;
1271
1272 pp = (unsigned char*) & pixel ;
1273
1274 switch (sinfo.pixtype)
1275 {
1276 /* FIXME: These may need modifying on big endian. */
1277 case MWPF_TRUECOLOR0888:
1278 case MWPF_TRUECOLOR888:
1279 rgb[0] = pp[2];
1280 rgb[1] = pp[1];
1281 rgb[2] = pp[0];
1282 break;
1283 case MWPF_PALETTE:
1284 rgb[0] = palette->palette[pp[0]].r;
1285 rgb[1] = palette->palette[pp[0]].g;
1286 rgb[2] = palette->palette[pp[0]].b;
1287 break;
1288 case MWPF_TRUECOLOR565:
1289 rgb[0] = pp[1] & 0xf8;
1290 rgb[1] = ((pp[1] & 0x07) << 5) |
1291 ((pp[0] & 0xe0) >> 3);
1292 rgb[2] = (pp[0] & 0x1f) << 3;
1293 break;
1294 case MWPF_TRUECOLOR555:
1295 rgb[0] = (pp[1] & 0x7c) << 1;
1296 rgb[1] = ((pp[1] & 0x03) << 6) |
1297 ((pp[0] & 0xe0) >> 2);
1298 rgb[2] = (pp[0] & 0x1f) << 3;
1299 break;
1300 case MWPF_TRUECOLOR332:
1301 rgb[0] = pp[0] & 0xe0;
1302 rgb[1] = (pp[0] & 0x1c) << 3;
1303 rgb[2] = (pp[0] & 0x03) << 6;
1304 break;
1305 default:
1306 fprintf(stderr, "Unsupported pixel format\n");
1307 return 0;
1308 }
1309
1310
1311 *(red) = rgb[0];
1312 *(green) = rgb[1];
1313 *(blue) = rgb[2];
1314 return 1;
1315}
1316#endif
c978d361 1317
a20c04ab
JS
1318#endif
1319 // wxUSE_NANOX
1320