[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

Re: Access image in a GdkPixmap




On Tue, 22 Jun 1999, David Sagnol wrote:
>             valPixel = gdk_image_get_pixel(image, j,i);
>             row[j*3+0] = (guchar)((valPixel << 8) >>24);
>             row[j*3+1] = (guchar)((valPixel <<16) >>24);
>             row[j*3+2] = (guchar)((valPixel <<24) >>24);

The pixel is the 'pixel' member of a GdkColor; basically it's a colormap
entry. Its meaning varies depending on the visual. For example, on an
8-bit visual I think it's a number from 0 to 255 indexing the 8-bit
colormap; on a 24-bit visual then it's likely to be one-byte RGB packed
values; perhaps you have a 16-bit visual (thus you get 255 255, i.e. 16
bits set to 1 for white).

In the Xlib Programming Manual, you might check out the "Color" chapter,
for example the sample code on page 230. Unfortunately there's no
Gdk-specific docs yet.

The implementation of the Gdk function gdk_rgb_xpixel_from_rgb() might be
instructive too (even though it's going the wrong way).

You might also look at the Imlib function to do this; it's
gdk_imlib_create_image_from_drawable(). The relevant section:

     switch (xatt.depth)
        {
        case 0:
        case 1:
        case 2:
        case 3:
        case 4:
        case 5:
        case 6:
        case 7:
        case 8:
          for (yy = 0; yy < height; yy++)
            {
              for (xx = 0; xx < width; xx++)
                {
                  pixel = XGetPixel(xim, xx, yy);
                  r = ctab[pixel & 0xff].r;
                 g = ctab[pixel & 0xff].g;
                  b = ctab[pixel & 0xff].b;
                  *ptr++ = r;
                  *ptr++ = g;
                  *ptr++ = b;
                }
            }
          break;
        case 16:
        case 15:
          if (id->x.render_depth == 16)
            {
              for (yy = 0; yy < height; yy++)
                {
                  for (xx = 0; xx < width; xx++)
                    {
                      pixel = XGetPixel(xim, xx, yy);
                      r = (pixel >> 8) & 0xf8;
                      g = (pixel >> 4) & 0xfc;
                      b = (pixel << 3) & 0xf8;
                      *ptr++ = r;
                      *ptr++ = g;
                      *ptr++ = b;
                   }
                }
            }
          else
            {
              for (yy = 0; yy < height; yy++)
                {
                  for (xx = 0; xx < width; xx++)
                    {
                      pixel = XGetPixel(xim, xx, yy);
                      r = (pixel >> 7) & 0xf8;
                      g = (pixel >> 2) & 0xf8;
                      b = (pixel << 3) & 0xf8;
                      *ptr++ = r;
                      *ptr++ = g;
                      *ptr++ = b;
                    }
                }
            }
          break;
        case 24:
        case 32:
          for (yy = 0; yy < height; yy++)
             {
              for (xx = 0; xx < width; xx++)
                {
                  pixel = XGetPixel(xim, xx, yy);
                  r = (pixel >> 16) & 0xff;
                  g = (pixel >> 8) & 0xff;
                  b = pixel & 0xff;
                  *ptr++ = r;
                  *ptr++ = g;
                  *ptr++ = b;
                }
            }
          break;
        default:
          for (yy = 0; yy < height; yy++)
            {
              for (xx = 0; xx < width; xx++)
                {
                  r = rand();
                  g = rand();
                  b = rand();
                  *ptr++ = r;
                  *ptr++ = g;
                  *ptr++ = b;
                }
            }
          break;
        }
    }

Clearly this is unpleasant and nontrivial, and for printing probably won't
even look nice when you're done (since it will be all pixelated).
PostScript will be nicer, but harder to implement. Maybe you can just use
the Imlib function, or cut-and-paste it.

Oh, the default case in the switch statement appears to be a joke. :-)
(At least, I can't figure it out.)

Havoc






[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]