Re: question for GDK



4201785 gold sdsmt edu writes:
> When I use the gdk_image_get() to get the image data from the
> drawable, in UNIX, the image data is 32 bits per pixel, while on
> Linux is 16 bits per pixel, Do you know how to set up to get 32/24
> bits per pixel on Linux?

Hi,

  gdk_image_get (and the general case of reading pixel data from
drawables) might be less useful than you think.  Often there's a
better way of managing whatever you're actually trying to do.  Having
said that, there are times when nothing else works.  When I needed it
in the past, I wrote this function (gdk_get_rgb_image) -- it's
intended as exactly the opposite of gdk_draw_rgb_image, and so returns
the image in the standard gdkrgb format (24 bits per pixel), whatever
depth the drawable is.

  Remember that prior dithering, limited visual depth, obscured window
regions, and other factors will all affect the result of this
operation.

  By the way, I haven't been able to test this function on every
visual type.  I have had no problems on 8, 15 and 24-bit pseudo-colour
and true-colour displays, but I'd appreciate bug reports from anyone
who finds that it does the Wrong Thing on their visuals.

Cheers,
Gary.

void
gdk_get_rgb_image( GdkDrawable *drawable,
		   GdkColormap *colormap,
		   gint x,
		   gint y,
		   gint width,
		   gint height,
		   guchar *rgb_buf,
		   gint rowstride ) {
    
    GdkImage *image;
    gint xpix, ypix;
    guchar *p;
    guint32 pixel;
    GdkVisual *visual;
    int r, g, b, dwidth, dheight;

    gdk_window_get_geometry( drawable, NULL, NULL, &dwidth, &dheight, NULL );
    
    if( x <= -width || x >= dwidth )
	return;
    else if( x < 0 ) {
	rgb_buf -= 3 * x;
	width += x;
	x = 0;
    } else if( x + width > dwidth )
	width = dwidth - x;

    if( y <= -height || y >= dheight )
	return;
    else if( y < 0 ) {
	rgb_buf -= rowstride * y;
	height += y;
	y = 0;
    } else if( y + height > dheight )
	height = dheight - y;
    
    visual = gdk_colormap_get_visual( colormap );

    if( visual->type == GDK_VISUAL_TRUE_COLOR ) {
	r = visual->red_shift + visual->red_prec - 8;
	g = visual->green_shift + visual->green_prec - 8;
	b = visual->blue_shift + visual->blue_prec - 8;
    }
    
    image = gdk_image_get( drawable, x, y, width, height );

    for( ypix = 0; ypix < height; ypix++ ) {
	p = rgb_buf;
	
	for( xpix = 0; xpix < width; xpix++ ) {
	    pixel = gdk_image_get_pixel( image, xpix, ypix );

	    switch( visual->type ) {
	    case GDK_VISUAL_STATIC_GRAY:
	    case GDK_VISUAL_GRAYSCALE:
	    case GDK_VISUAL_STATIC_COLOR:
	    case GDK_VISUAL_PSEUDO_COLOR:
		*p++ = colormap->colors[ pixel ].red >> 8;
		*p++ = colormap->colors[ pixel ].green >> 8;
		*p++ = colormap->colors[ pixel ].blue >> 8;
		break;
		
	    case GDK_VISUAL_TRUE_COLOR:
		*p++ = r > 0 ? ( pixel & visual->red_mask ) >> r :
		    ( pixel & visual->red_mask ) << -r;
		*p++ = g > 0 ? ( pixel & visual->green_mask ) >> g :
		    ( pixel & visual->green_mask ) << -g;
		*p++ = b > 0 ? ( pixel & visual->blue_mask ) >> b :
		    ( pixel & visual->blue_mask ) << -b;
		break;
		
	    case GDK_VISUAL_DIRECT_COLOR:
		*p++ = colormap->colors[ ( pixel & visual->red_mask )
				       >> visual->red_shift ].red >> 8;
		*p++ = colormap->colors[ ( pixel & visual->green_mask )
				       >> visual->green_shift ].green >> 8;
		*p++ = colormap->colors[ ( pixel & visual->blue_mask )
				       >> visual->blue_shift ].blue >> 8;
		break;
	    }
	}

	rgb_buf += rowstride;
    }
}
-- 
  Gary Wong    Consultant, Dependable Distributed Computing, AT&T Shannon Labs
        gtw research att com            http://www.cs.arizona.edu/~gary/




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