]> git.saurik.com Git - wxWidgets.git/blobdiff - src/x11/bitmap.cpp
A bit of scrolling works under GTK 2.0
[wxWidgets.git] / src / x11 / bitmap.cpp
index d4fa21be4bfe45939a461ae6a3bc88c73f301f87..31fd1858f0c766e61d22a855df0eb3cbaf08795f 100644 (file)
@@ -28,6 +28,9 @@
 #if wxUSE_NANOX
 #undef wxHAVE_LIB_XPM
 #define wxHAVE_LIB_XPM 0
+
+// Copy from the drawable to the wxImage
+bool wxGetImageFromDrawable(GR_DRAW_ID drawable, int srcX, int srcY, int width, int height, wxImage& image);
 #endif
 
 #if wxUSE_XPM
@@ -147,11 +150,13 @@ bool wxMask::Create( const wxBitmap& bitmap,
 
     XSetForeground( xdisplay, gc, BlackPixel(xdisplay,xscreen) );
 
-    for (int j = 0; j < image.GetHeight(); j++)
+    int width = image.GetWidth();
+    int height = image.GetHeight();
+    for (int j = 0; j < height; j++)
     {
         int start_x = -1;
         int i;
-        for (i = 0; i < image.GetWidth(); i++)
+        for (i = 0; i < width; i++)
         {
             if ((data[index] == red) &&
                 (data[index+1] == green) &&
@@ -432,6 +437,10 @@ bool wxBitmap::CreateFromImage( const wxImage& image, int depth )
     // by doing several blits as per the Windows
     // implementation because Nano-X doesn't support
     // XSetClipMask.
+    // TODO: could perhaps speed this function up
+    // by making a buffer of pixel values,
+    // and then calling GrArea to write that to the
+    // pixmap. See demos/nxroach.c.
 
     bool hasMask = image.HasMask();
     
@@ -800,34 +809,8 @@ wxImage wxBitmap::ConvertToImage() const
     int bpp = DefaultDepth( xdisplay, xscreen );
 
 #if wxUSE_NANOX
-    int w = image.GetWidth();
-    int h = image.GetHeight();
-    
-    wxMemoryDC memDC;
-    memDC.SelectObject(*this);
-
-    wxColour pixelCol;
-
-    // Warning: this is very inefficient.
-    // TODO: use GrReadArea to get an array of pixels
-    int i, j;
-    for (i = 0; i < w; i++)
-    {
-        for (j = 0; j < h; j++)
-        {
-            memDC.GetPixel(i, j, & pixelCol);
-
-            // TODO: make wxColour accessors more efficient
-            // by inlining, if possible
-            image.SetRGB(i, j,
-                         pixelCol.Red(), pixelCol.Green(),
-                         pixelCol.Blue());
-        }
-    }
-    memDC.SelectObject(wxNullBitmap);
-
+    wxGetImageFromDrawable((Pixmap) GetPixmap(), 0, 0, GetWidth(), GetHeight(), image);
     return image;
-    
 #else
     // !wxUSE_NANOX
     XImage *x_image = NULL;
@@ -925,10 +908,12 @@ wxImage wxBitmap::ConvertToImage() const
 
 //    GdkColormap *cmap = gtk_widget_get_default_colormap();
 
+    int width = GetWidth();
+    int height = GetHeight();
     long pos = 0;
-    for (int j = 0; j < GetHeight(); j++)
+    for (int j = 0; j < height; j++)
     {
-        for (int i = 0; i < GetWidth(); i++)
+        for (int i = 0; i < width; i++)
         {
             unsigned long pixel = XGetPixel( x_image, i, j );
             if (bpp == 1)
@@ -1296,3 +1281,140 @@ WXDisplay *wxBitmap::GetDisplay() const
     return M_BMPDATA->m_display;
 }
 
+#if wxUSE_NANOX
+// Copy from the drawable to the wxImage
+bool wxGetImageFromDrawable(GR_DRAW_ID drawable, int srcX, int srcY, int width, int height, wxImage& image)
+{
+    GR_SCREEN_INFO sinfo;
+    int x, y;
+    GR_PIXELVAL *pixels;
+    GR_PALETTE* palette = NULL;
+    unsigned char rgb[3], *pp;
+
+    GrGetScreenInfo(&sinfo);
+
+    if (sinfo.pixtype == MWPF_PALETTE) {
+               if(!(palette = (GR_PALETTE*) malloc(sizeof(GR_PALETTE)))) {
+                       return FALSE;
+               }
+               GrGetSystemPalette(palette);
+       }
+
+    if(!(pixels = (GR_PIXELVAL*) malloc(sizeof(GR_PIXELVAL) * width * height)))
+    {
+        return FALSE;
+    }
+
+    image.Create(width, height);
+
+       GrReadArea(drawable, srcX, srcY, width, height,
+                                               pixels);
+
+
+               for(x = 0; x < sinfo.cols; x++) {
+
+                       pp = (unsigned char *)pixels +
+                               ((x + (y * sinfo.cols)) *
+                               sizeof(GR_PIXELVAL));
+
+                       switch(sinfo.pixtype) {
+                       /* FIXME: These may need modifying on big endian. */
+                               case MWPF_TRUECOLOR0888:
+                               case MWPF_TRUECOLOR888:
+                                       rgb[0] = pp[2];
+                                       rgb[1] = pp[1];
+                                       rgb[2] = pp[0];
+                                       break;
+                               case MWPF_PALETTE:
+                                       rgb[0] = palette->palette[pp[0]].r;
+                                       rgb[1] = palette->palette[pp[0]].g;
+                                       rgb[2] = palette->palette[pp[0]].b;
+                                       break;
+                               case MWPF_TRUECOLOR565:
+                                       rgb[0] = pp[1] & 0xf8;
+                                       rgb[1] = ((pp[1] & 0x07) << 5) |
+                                               ((pp[0] & 0xe0) >> 3);
+                                       rgb[2] = (pp[0] & 0x1f) << 3;
+                                       break;
+                               case MWPF_TRUECOLOR555:
+                                       rgb[0] = (pp[1] & 0x7c) << 1;
+                                       rgb[1] = ((pp[1] & 0x03) << 6) |
+                                               ((pp[0] & 0xe0) >> 2);
+                                       rgb[2] = (pp[0] & 0x1f) << 3;
+                                       break;
+                               case MWPF_TRUECOLOR332:
+                                       rgb[0] = pp[0] & 0xe0;
+                                       rgb[1] = (pp[0] & 0x1c) << 3;
+                                       rgb[2] = (pp[0] & 0x03) << 6;
+                                       break;
+                               default:
+                                       fprintf(stderr, "Unsupported pixel "
+                                                       "format\n");
+                                       return 1;
+                       }
+
+            image.SetRGB(x, y, rgb[0], rgb[1], rgb[2]);
+
+       }
+
+    free(pixels);
+    if(palette) free(palette);
+
+    return TRUE;
+}
+
+#if 0
+int GrGetPixelColor(GR_SCREEN_INFO* sinfo, GR_PALETTE* palette, GR_PIXELVAL pixel,
+    unsigned char* red, unsigned char* green, unsigned char* blue)
+{
+       unsigned char rgb[3], *pp;
+
+    pp = (unsigned char*) & pixel ;
+
+    switch (sinfo.pixtype)
+    {
+                       /* FIXME: These may need modifying on big endian. */
+                               case MWPF_TRUECOLOR0888:
+                               case MWPF_TRUECOLOR888:
+                                       rgb[0] = pp[2];
+                                       rgb[1] = pp[1];
+                                       rgb[2] = pp[0];
+                                       break;
+                               case MWPF_PALETTE:
+                                       rgb[0] = palette->palette[pp[0]].r;
+                                       rgb[1] = palette->palette[pp[0]].g;
+                                       rgb[2] = palette->palette[pp[0]].b;
+                                       break;
+                               case MWPF_TRUECOLOR565:
+                                       rgb[0] = pp[1] & 0xf8;
+                                       rgb[1] = ((pp[1] & 0x07) << 5) |
+                                               ((pp[0] & 0xe0) >> 3);
+                                       rgb[2] = (pp[0] & 0x1f) << 3;
+                                       break;
+                               case MWPF_TRUECOLOR555:
+                                       rgb[0] = (pp[1] & 0x7c) << 1;
+                                       rgb[1] = ((pp[1] & 0x03) << 6) |
+                                               ((pp[0] & 0xe0) >> 2);
+                                       rgb[2] = (pp[0] & 0x1f) << 3;
+                                       break;
+                               case MWPF_TRUECOLOR332:
+                                       rgb[0] = pp[0] & 0xe0;
+                                       rgb[1] = (pp[0] & 0x1c) << 3;
+                                       rgb[2] = (pp[0] & 0x03) << 6;
+                                       break;
+                               default:
+                                       fprintf(stderr, "Unsupported pixel format\n");
+                                       return 0;
+       }
+
+
+    *(red) = rgb[0];
+    *(green) = rgb[1];
+    *(blue) = rgb[2];
+    return 1;
+}
+#endif
+
+#endif
+  // wxUSE_NANOX
+