• ### Popular Now

• 15
• 15
• 11
• 9
• 10

#### Archived

This topic is now archived and is closed to further replies.

# My screenshots are all grayscale... why?

This topic is 5244 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I am have a program that needs to dump the image in a window to a pic file. I have it all set up and its writing the image ok, but i cant seem to get the coloring right. Its either all red, gree, or blue or grayscale. What am i doing wrong? Here is the code:
  png_uint_32 y, x;
png_byte image[height][width*8];
png_bytep row_pointers[height];
unsigned long pixel;

XImage *ximg;
ximg = XGetImage( display, win, 0, 0, width, height, 0xffffffff, XYPixmap );

for( y = 0; y < height; y++ )
{
for( x = 0; x < width; x++ )
{
// Now get the pixel from the incoming image and copy it to
// the PNG row.
// FIXME Not working
pixel = XGetPixel( ximg, x, y );
//fprintf( stdout, "%x\n", pixel );
r = 0x00ff & pixel;
g = 0x0000ff & pixel;
b = 0x000000ff & pixel;
image[y][x*3] = r;
image[y][x*3+1] = g;
image[y][x*3+2] = b;
//	  image[y][x*3] = pixel; // This make the image RED scale

}


##### Share on other sites
I''m not familiar with XGetImage and XGetPixel, so I can''t tell you whether you''re using them correctly or not, but the line "png_byte image[height][width*8];" looks suspicious. Why width*8? If anything, I think it should be width*3...

Michael K.,
Co-designer and Graphics Programmer of "The Keepers"

We come in peace... surrender or die!

##### Share on other sites
quote:
r = 0x00ff & pixel;
g = 0x0000ff & pixel;
b = 0x000000ff & pixel;

This looks wrong.

0x00ff, 0x0000ff and 0x000000ff all equal 255.

I think you mean.

0x000000FF, 0x0000FF00 and 0x00FF0000

Which will mask the colours correctly.

##### Share on other sites
It needs to be width*8 because its just an array of bytes, so we need *8 so that there is room for all the colors. .PNG files are a pain in the arse, but that is what i have to work with.

I have this code that works fine:
  png_uint_32 y, x;  png_byte image[height][width*8];   png_bytep row_pointers[height];  for( y = 0; y < height; y++ )    {      for( x = 0; x < width; x++ )	{	  // Now get the pixel from the incoming image and copy it to	  // the PNG row.	  	  // Get the pixel from the image	  r = getmetapixel( imgptr, x, y, 0, width, height, channels );	  g = getmetapixel( imgptr, x, y, 1, width, height, channels );	  b = getmetapixel( imgptr, x, y, 2, width, height, channels );/* 	  if( r || g || b ) *//* 	  fprintf( stdout, "r:%x\ng:%x\nb:%x\n", r, g, b ); */	            image[y][x*3] =r;          image[y][x*3+1] =g;          image[y][x*3+2] =b;	}

Where getmetapixel returns an int value of the r, g or b color in the pixel.
Unfortunatley i cant use that function on the image i am trying to take from the ximage struct. the XGetPixel is returning an unsigned long. I think it must have something to do with the way i am extracting the r,g,b color bits from it, but im not sure.

##### Share on other sites
quote:
Original post by Sphet
quote:
r = 0x00ff & pixel;
g = 0x0000ff & pixel;
b = 0x000000ff & pixel;

This looks wrong.

0x00ff, 0x0000ff and 0x000000ff all equal 255.

I think you mean.

0x000000FF, 0x0000FF00 and 0x00FF0000

Which will mask the colours correctly.

I changed to this, and the image that is output is all red-scale now.

##### Share on other sites
You''re not scaling the color components to the 0-255 range. If you just take (pixel & 0xff0000) the result is going to be from 0x10000 to 0xff0000. You must bit shift it to the right.

For example:
r = (0xff0000 & pixel)>>16;
g = (0xff00 & pixel)>>8;
b = 0xff & pixel;

##### Share on other sites
quote:
Original post by Fingers_
You''re not scaling the color components to the 0-255 range. If you just take (pixel & 0xff0000) the result is going to be from 0x10000 to 0xff0000. You must bit shift it to the right.

For example:
r = (0xff0000 & pixel)>>16;
g = (0xff00 & pixel)>>8;
b = 0xff & pixel;