Sign in to follow this  

[XNA] How to run through ever pixel in an image?

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello,

After looking into some image manipulation with xna, i've come out slightley confused.

One task I was wanting to do would involve running through all the pixels in a given image, and checking each ones color.

After my searching, I feel that I would be somehow using both Texture2D.GetData and some kind of PixelGrabber (The examples I saw for pixel grabbers were in java)

However, the programs that grabbed pixels appeared to shove the whole images pixels into a 1 dimensional array.

If that were the case, how would they go about reconstructing the image if they wanted, or knowing which pixel they were currentley viewing? Isn't it all basically on one line now?

How do I go about getting the pixels? And how does this process work?

Thanks,

-Minty

Share this post


Link to post
Share on other sites
Having a single-dimensional array doesn't mean it's all "on one line". You just wrap the lines around again, depending on a value of your choosing. When it comes to things like rectangular image data, the number to wrap on is the width of the image.

You access specific pixels from a one-dimensional array using the following algorithm:

[font="Courier New"]pixel = texturedata [ x * texture_width + y ];
texturedata [ x * texture_width + y ] = newpixel;[/font]


Use Texture2D.GetData() to get the array of pixels, change them to your liking using the math above, and then use Texture2D.SetData() to commit the changes back to the texture.

Here's some sample code:
[code]// Create new 32x32 texture, and retrieve its data into an array
Texture2D newTexture = new Texture2D(GraphicsDevice, 32, 32);
Color[] textureData;
newTexture.GetData<Color>(textureData);

// Fill texture with white
for (int i = 0; i < 32; i++)
{
for (int j = 0; j < 32; j++)
{
textureData[i * newTexture.Width + j] = Color.White;
}
}

// Fill center pixel of image with Black
textureData[16 * newTexture + 16] = Color.Black;

// Commit changes to texture
newTexture.SetData<Color>(textureData);[/code]

Share this post


Link to post
Share on other sites

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

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this