Weird reflection of image elements with a targa image

Hi, I am currently doing a program that reads an image in the targa format, does some processing on it and then write back in a new image. If I just read and write it without any changes, it work fine.
If I try to do any kind of processing, I end with a bizarre image with the elements of the image appertaining several times. At first I thought that the problem was in the algorithm I was applying, a mean filter that was causing it.

Here two images in their original form:

http://s72.photobucket.com/user/john_smith140/media/input_zpsz2cfhrc7.jpeg.html?sort=3&o=1
http://s72.photobucket.com/user/john_smith140/media/gp4_zpstafhejk5.jpg.html?sort=3&o=0

And here after the processing:

http://s72.photobucket.com/user/john_smith140/media/triangles_zps8lsblfua.jpeg.html?sort=3&o=3
http://s72.photobucket.com/user/john_smith140/media/racing_zps91arv3ie.jpeg.html?sort=3&o=2

But then I decided to simplify the image to try to see what is happening. So I had the idea of "erase" it, making it all white, with a single black square in part of the image to see if it would show up as expected. That's the simplest thing I could think. This is the code that does this square drawing:

1
2
3
4
5
6
7
8
9
10
11
for (int x=0; x<width; x++)
        for (int y=0; y<height; y++) {
            image [3* (x*height + y)] = 255;
            image [3* (x*height + y) + 1] = 255;
            image [3* (x*height + y) + 2] = 255;
            if (x>=200 && x<=400 && y>=200 && y<=400) {
                image [3* (x*height + y)] = 0;
                image [3* (x*height + y) + 1] = 0;
                image [3* (x*height + y) + 2] = 0;
            }
        } 
Image being a char image [size];

I ended with this abomination:

http://s72.photobucket.com/user/john_smith140/media/z_zpsj5iw9hme.jpeg.html?o=1

I tested with a 2-dimension vector and the result was the same.
I think the reason may be related with targa format, which is why I am thinking about trying another file format. Outside that I have no idea about how I can figure out what's happening and would appreciate any suggestions about how I can try to find out what's going on.

If more code is necessary, just ask for it.
Last edited on
Access an RGB triplet:

1
2
3
4
int index( int width, int x, int y )
{
  return 3 * (y * width + x);
}


So:

1
2
3
4
5
6
7
8
9
10
11
12
// Fill image with white
fill_n( image, 3 * width * height, 255 );

// Draw a pretty green (RGB = #73C36C) square at (left, top, right, bottom)
for (int x = left; x <= right; x++)
for (int y = top; y <= bottom; 
{
  int n = index( width, x, y );
  image[ n + 0 ] = 115;
  image[ n + 1 ] = 195;
  image[ n + 2 ] = 108;
}

Hope this helps.

[edit] Fixed stupid mistake.
Last edited on
Ok, it worked. Thanks a lot. But my doubt remains....... Why wasn't my code working?

I was working with the image's pixels in a row-column order. Your code deals with them in a column-row order. Both should work, but only the second one indeed works. Why? ANyone has an idea?
No, only one will work, because the data is laid out in memory by rows. So you have to address it by rows, then by column.

Hope this helps. :O)
Topic archived. No new replies allowed.