Sponsored Content
Top Forums Programming Writing a Targa file not working from an array Post 303016281 by colt on Monday 23rd of April 2018 06:39:02 PM
Old 04-23-2018
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:

Code:
 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:

Code:
 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[i]);
         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.
 

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Writing into shared memory Array

Hi All, How do I write into shared memory blocks using multiple threads? For example, I have created 50 threads to write into shared memory and 50 threads to read from shared memory. I have already created a shared memory and I want to write into shared memory as follows: ... (0 Replies)
Discussion started by: kumars
0 Replies

2. UNIX for Advanced & Expert Users

Planning on writing a Guide to Working with Large Datasets

In a recent research experiment I was handling, I faced this task of managing huge amounts of data to the order of Terabytes and with the help of many people here, I managed to learn quite a lot of things in the whole process. I am sure that many people will keep facing these situations quite often... (2 Replies)
Discussion started by: Legend986
2 Replies

3. Programming

C programming working with multidimensional array

Hi, I have the following variable declaration which looks like a 3d array or N matrixs KxK of floats float (*table); I have to pass to a function only the first table. How can I do it?? Thanks (6 Replies)
Discussion started by: littleboyblu
6 Replies

4. Shell Programming and Scripting

Array not working

Hi My below code to display the contents in array is not working. Pls correct my code. cat $file | while read file1 do filenames=$file1 let i=$i+1 echo $i echo ${filenames} done let j=1 while do echo "${filenames}" let j=$j+1 done (5 Replies)
Discussion started by: Sekar1
5 Replies

5. Shell Programming and Scripting

Compare file to array, replace with corresponding second array

ok, so here is the issue, I have 2 arrays. I need to be able to create a loop that will find ${ARRAY1 in the text doc, and replace it with ${ARRAY2 then write the results. I already have that working. The problem is, I need it to do that same result across however many items are in the 2... (2 Replies)
Discussion started by: gentlefury
2 Replies

6. Shell Programming and Scripting

working with null elements in an array

i have an array (with each element length "n") which is dynamic and has alphanumeric characters. i want to check if any of the elements of the array are null and replace it with a string of "n" zeros for that element. can you suggest me a code for the same. (1 Reply)
Discussion started by: mumbaiguy07
1 Replies

7. Shell Programming and Scripting

Print arguments using array elements not working?

Hey guys, I'm new to shell scripting and I'm trying to write a script that takes user input and copies the specified columns from a data file to a new one. In order to account for the possibility of a variable number of columns to copy I wrote a loop that encodes the user's choices in an array... (16 Replies)
Discussion started by: shaner
16 Replies

8. Shell Programming and Scripting

Writing HTML with variables from an array or other means

Hello! I wish to create a script that will automate the creation of HTML files... for example, if my HTML starts out containing: <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>$TITLE</title> </head> I want to put that into something that I can read and write later on. My... (4 Replies)
Discussion started by: tntelle
4 Replies

9. Shell Programming and Scripting

Need help writing array for the specific need of shell script

I need shell script for the following specfic need, I'll get following output after running my program. I wanted every 50th row in the coloumn and take into array. For example input ============== 03 03 03 03 05 05 05 05 07 07 07 07 I wanted to extract 3,5,7 and store it in... (12 Replies)
Discussion started by: JITHENDER
12 Replies

10. Shell Programming and Scripting

Bash question: working with an array of previously set variable strings

while i've used arrays to work with variables, i've never used them to loop through a set of strings and wanted to ask the community for some feedback or assistance. let me be specific. here's my code: # URL port Variables port2195=`nc -z $url2195 2195` port2196=`nc -z $url2196 2196`... (5 Replies)
Discussion started by: hungryd
5 Replies
RA_T8(1)						      General Commands Manual							  RA_T8(1)

NAME
ra_t8 - convert RADIANCE picture to/from Targa 8-bit image file SYNOPSIS
ra_t8 [ -d ][ -b ][ -c ncolors ][ -g gamma ][ -e +/-stops ][ -n sampfac ] input [ output ] ra_t8 -r [ -g gamma ][ -e +/-stops ] [ input [ output ] ] DESCRIPTION
Ra_t8 converts between RADIANCE and Targa 8-bit color-mapped image files (type 1 in Targa's documentation). In the default mode, a RADI- ANCE picture is converted to a color-mapped Targa file of the same horizontal and vertical dimensions with 8-bits per pixel. The -d option turns off dithering. The -b option converts the image to black and white. Only with this option can the input be taken from stdin. The -c option allows fewer than 256 colors. The -g option specifies the exponent used in gamma correction; the default value is 2.2. An expo- nent of 1.0 turns gamma correction off. The -e option specifies an exposure compensation in f-stops (powers of two). Only integer stops are allowed, for efficiency. The -n option specifies a sampling factor for neural network color quantization. This value should be between 1 and 80, where 1 takes the longest and produces the best results in small areas of the image. If no value is given, a faster median cut algorithm is used. The -r option invokes a reverse conversion, from an 8-bit Targa file to a RADIANCE picture. If the output file is missing, the standard output is used. AUTHORS
Greg Ward Anthony Dekker provided the code for neural network color quantization BUGS
Run-length encoded files can be read but not written with this program. SEE ALSO
pfilt(1), ra_bmp(1), ra_bn(1), ra_ppm(1), ra_pr(1), ra_pr24(1), ra_t16(1), ra_tiff(1), ximage(1) RADIANCE
5/30/95 RA_T8(1)
All times are GMT -4. The time now is 05:24 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy