Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

Hello so I was hoping someone knew how to move or change color and position actual image pixels and could explain and show the code to do so.

I know how to write pixels on a surface or screen-surface

usigned int *ptr = static_cast <unsigned int *> (screen-pixels);

int offset = y * (screen->pitch / sizeof(unsigned int));

ptr[offset + x] = color;

But I don't know how to alter or manipulate a image pixel of a png image my thoughts on this was

How do I get the values and locations of pixels and what do I have to write to make it happen?

Then how do I actually change the values or locations of those gotten pixels and how do I make that happen?

any ideas tip suggestions are also welcome!

  int main(int argc , char *argv[])
    {

        SDL_Surface *Screen = SDL_SetVideoMode(640,480,32,SDL_SWSURFACE);

        SDL_Surface *Image;

        Image = IMG_Load("image.png");

        bool done = false;

        SDL_Event event;

        while(!done)
        {

            SDL_FillRect(Screen,NULL,(0,0,0));

            SDL_BlitSurface(Image,NULL,Screen,NULL);

                while(SDL_PollEvent(&event))
                {

                    switch(event.type)
                    {

                            case SDL_QUIT:
                            return 0;
                            break;

                    }

                }

                SDL_Flip(Screen);

        }

    return 0;

}
share|improve this question
    
Do you want to persist the changes in a png file to your hard drive? –  Asakeron Oct 29 '12 at 11:45
    
just in the program or game but if you could show the code for doing it in a png file that would also be very cool to watch and see for that matter :) –  NoobScratcher Oct 29 '12 at 11:49
    
Both Screen and Image are of type SDL_Surface*. You are probably able to access their pixel data in the same way. What is not working? –  Asakeron Oct 29 '12 at 12:00
    
the idea and code to change the value and location of image pixels where do I start , what do I write, how do I understand it , technicality is something I like to stay away from as much as possible unless impossible otherwise , I'm crazy like that :D. –  NoobScratcher Oct 29 '12 at 12:06
    
So, you want to retrieve a pixel color value by specifying a coordinate and also be able to modify it? –  Asakeron Oct 29 '12 at 12:16

1 Answer 1

I will assume you are reading a 32 bit RGBA surface, which means each pixel will have 8 bit for Red, 8 bit for Green, 8 bit for Blue and 8 bit for Alpha(transparency).

Pixels indices:

Pixels are stored in an one-dimensional array, so you can't simply say pixels[x][y].

As they are stored in order from left to right and from top to bottom, determining an individual pixel's index in the array is as simples as pixels[x + (w * y)] where wis the surface's width, and x , y are the coordinates.

I suggest defining a helper function for that:

int get_pixel_index(int x, int y, int w)
{
    return w * y + x;
}

Getting individual pixels:

Accessing a single pixel by specifying it coordinates (x,y) in the surface can be achieved with the following code:

int index = get_pixel_index(x, y, Image->w);
Uint32 *all_pixels = (Uint32*) Image->pixels; //Get all pixels from surface.
Uint32 pixel = pixels[index]; //Get specific pixel

We use Uint32 because we are reading from 32 bit surface.

Reading pixel color information:

The pixel variable holds the color information for the requested pixel.

If you want to access the RGBA componnents indivudally you will need some bit twiddling or the SDL_GetRBA function:

SDL_GetRGBA(pixel, Image->format, &red, &green, &blue, &alpha);

Setting pixels:

You can set a pixel color as easily as:

pixel = color;

Where color is a Uint8 representing a RGBA color.

Alternatively, you can use SDL_MapRGBA which let you specify color components individualy:

pixel = SDL_MapRGBA(Image->format, red, green, blue, alpha);
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.