Writing a Targa file not working from an array

Hello, I wrote code that generates an image that writes its contents to a Targa file. Due to modifications that I wish to do, I decided to copy the value of each pixel as they are calculated to a dynamically allocated array before write it to a file. The problem is now that I all I see is a big black image.

Here I Will show the basic structure of the algorithm:

 a = new char [image.getSize()]; //image.getWidth()*image.getHeight()*3 
 int ctr=0, ctr2=0, ctr3=0;
 for (int y=0;  y<image.getHeight(); y++) {
     for (int x=0; x<image.getWidth(); x++) {
        ........
        ........
        ........
        ctr2=ctr;
        ctr2++;
        ctr3=ctr;
        ctr3+=2;
        a[ctr]  = min (blue2*255.0f,255.0f);
        a[ctr2] = min (green2*255.0f,255.0f);
        a[ctr3] = min (red2*255.0f,255.0f);
        tgaManager.fileWriter.put (a[ctr]);    //offstream object
        tgaManager.fileWriter.put (a[ctr2]);
        tgaManager.fileWriter.put (a[ctr3]);
        ctr+=3;
     }
 }

The code above still wrotes the pixels to a file while is doing the color calculations. It works fine. It writes each color component in a single loop iteration. That's why the size of a is three times its height * its width. SO the ctr variable is incremented three times each iteration so there is no overwriting of the values of a certain iteration in the next two. Ctr2 an three are incremented by one and two to write in the subsequent positions if a the values of green and red components

What does not work is this:

 a = new char [image.getSize()]; //image.getWidth()*image.getHeight()*image.getBitsPerPixel() 
 int ctr=0, ctr2=0, ctr3=0;
 for (int y=0;  y<image.getHeight(); y++) {
     for (int x=0; x<image.getWidth(); x++) {
        ........
        ........
        ........
        ctr2=ctr;
        ctr2++;
        ctr3=ctr;
        ctr3+=2;
        a[ctr]  = min (blue2*255.0f,255.0f);
        a[ctr2] = min (green2*255.0f,255.0f);
        a[ctr3] = min (red2*255.0f,255.0f);
        //tgaManager.fileWriter.put (a[ctr]);    //offstream object
        //tgaManager.fileWriter.put (a[ctr2]);
        //tgaManager.fileWriter.put (a[ctr3]);
        ctr+=3;
     }
 }
for (int i=0; i<image.getHeight()*image.getWidth(); i++) {
         int y=i;
         int z=i;
         y++;
         z+=2;
         tgaManager.fileWriter.put (a);
         tgaManager.fileWriter.put (a[y]);
         tgaManager.fileWriter.put (a[z]);
 }

The number of iterations of both loops is the same, I use three counters in the same way on both loops, so I think the problem lies perhaps is in the fact writing order to the file, it has to be in some specific order. Or perhaps there is error in the algorithm that I was not able to see.

Anyone has an idea. Many thanks.

One might note that the 1st code fragment (making the wild assumption that none of the obscured lines make any updates to the ctr variable), sets and writes three successive elements of the array a[] each time through the loop. This seems to be what you want to do, although it would probably be much more efficient to just have the loop set elements of the array and then use a single write of the entire array when the loop finishes.

The second code fragment sets elements of the array in about the same way in the first loop. But, the second loop writes elements 0, 1, and 2 of the array on the 1st pass; elements 1, 2, and 3 on the 2nd pass; 2, 3, and 4 on the 3rd pass; etc. To be logically equivalent to the first code fragment, you would need to change:

for (int i=0; i<image.getHeight()*image.getWidth(); i++) {

to something more like:

for (int i=0; i<image.getHeight()*image.getWidth(); i+=3) {

But, again, writing the entire array with a single write would seem to be much more efficient than writing the entire array one byte at a time.

One would expect that the file produced by the first code fragment you showed us is much smaller than the file produced by the second code fragment you showed us (by about a factor of 3). Maybe that would be a hint as to what might not be working???

1 Like

Don Cragun, your suggestion did not work at first, but is working now that I not only changed the counter increment from 1 to 3, but also changed the loop stop condition from

i<image.getHeight()*image.getWidth();

to

i<image.getHeight()*image.getWidth()*3;

I was writing the array one byte por pixel, because I was storing the contents on three different arrays (since I was not being able to do so with one), one for each color component, and in the writing phase I had to alternate between them.