Raster scan - raw file into image in C - c

I have a file with the following structure:
"width" "height" "gray_levels" "pix1" "pix1_length" "pixn" "pixn_length"
Basically, I have to convert this raw data of a raster scanned image back into an image.
Also depending on the amount of gray levels used there will be different character for each gray level. My problem is that I don't really know where to begin. I know that it's better if I have a 2D array, to which I would enter the integer value of different character for the gray level. So i.e. 0 = # (and then I would enter the ASCII value of #)
fscanf(inputfile,"%i %i %i", &x, &y, &gray_levels);
This line reads the dimension of the image for later processing, but I have no idea how to use it without creating a forest i.e. like 10 loops inside one another. I think the main problem is how to program it so that e.g. when first pixel is length 300, I make it to go to the next line in the array.
Also, I shouldn't use malloc because I haven't covered that topic yet. I need to create the size of the array at runtime, so I just created an array with the maximum size of 80*100.

Related

Controlling text alignment when writing a text file

I need to create text files and have the ability to control if the text is left or right justified using Go.
I found tab writer but I don't want columns. The text needs to flow freely.
Any suggestions?
You are very limited in the kinds of formatting you can perform within an ASCII text file. There are no ASCII control characters to say that a block of text will be justified in a certain way. You are either relying on a text viewer to interpret a custom syntax as formatting (see the Markdown format) or you are adding spaces to explicitly create the formatting you want on each line.
For the latter, you can insert spaces in front of each line to simulate justification. To do this, you'll need to pick a fixed number of characters per line (e.g. 40 characters) as the basis of your formatting. Note that this maximum line width won't necessarily match the size of the screen in whatever text viewing app your user has.
The left-justification algorithm is basically a word-wrapping algorithm. See Best word wrap algorithm? for that.
The right-justification algorithm is word-wrapping again, but with an intermediate step: Have the word-wrapping function return your text split into word-wrapped lines first. And then pad the start of each line with a count of spaces equal to the count of characters you had remaining to fit inside of the maximum line width.
So say your source text is "There is no justification for this statement!" and your maximum line width is 15 characters. The left-justification algorithm will output this:
There is no
justification
for this
statement!
...and the right-justification algorithm will output this:
There is no
justification
for this
statement!
If you want to change the maximum line width, then you need to run the algorithm again to reflow the text with a new maximum line width.

Storing and replacing values in array continuously

I'm trying to read amplitude from a waveform and shine a green, yellow or red light depending on the amplitude of the signal. I'm fairly new to labVIEW and couldnt get my idea that wouldve worked with any other programming language I know to work. What I'm trying to do is take the value of the signal and for everytime it updates I'll store the value of the amplitude into an index of a large array. With each measurement being stored in the n+1 index of the array.
After a certain amount of data points I want to start over and replace values in the array (I use the formula node with the modulus for this). By keeping a finite amount of indexes to check for max value I restrict my amplitude check to a certain time period.
However my problem is that whenever I use the replace array subset to insert a new value into index n, all the other index points get erased. Rendering it pretty much useless. I was thinking its the Initialize array causing problems but I just cant seem to wrap my head around what to do here.
I tried creating just basic arrays in the front panel, but those either are control or indicator arrays and can't seem to be both written and read from, its either control (read but not write) or indicate(write but not read)?. Maybe its just not possible to do what I had in mind in an eloquent way in LabVIEW. If its not possible to do this with arrays in LabVIEW I will look for a different way to do it.
I'm pretty sure I got most of the rest of the code down except for an unfinished part here and there. Its just my issue with the arrays not working as I want them too.
I expected the array to retain its previously inputted data for index n-1 when index n is inputted. And only to be replaced once the index has come back to that specific point.
Instead its like a new array is initialized every time a new index is input.
download link for the VI
What you want to do:
Transport the content of the modified array into the next iteration of the WHILE loop.
What happens:
On each iteration, the content of the array is the same. It is the content of the initial array you created outside.
To solve this, right-click the orange square on the left border of the loop, and make it a "shift register". The symbol changes, and a similar symbol appears on the right border. Now, wire the modified array to the symbol on the right. What flows out into that symbol on the right, comes in from the left symbol on the next iteration.
Edit:
I have optimized your code a little. There is a modulo function, and an IF clause can handle ranges. ..3 means "values lower or equal 3". The next case is "Default", the next "7..". Unfortunately, this only works for integers. Otherwise, one would use nested IF clauses with the < comparator or similar.

How can I printf first in columns then in lines?

maybe this question seems stupid... I would like to fprintf a dataset formatted in rows and columns. I know the procedure so far
for(i=0;i<number_of_rows;i++)
{
for(j=0;j<number_of_columns;j++)
fprintf(file,"%g\t",array[i][j]);
fprintf(file,"\n");
}
What essentially does this code is to fill up first the rows and then the columns. I have datasets that are created by columns. Thus, I want to fill up every column before I pass to the next one. I don't know before the runtime the length of the output data in order to make a proper 2D array. And I need to print out first a whole column, then the other. How can I do this?
And I need to print out first a whole column, then the other. How can I do this?
That would involve appending to a previously printed line. And, as you may know, adding content into a file involves rewriting the entire rest of the file. You can probably guess that would be incredibly inefficient and also complicated to implement.
I don't know before the runtime the length of the output data in order to make a proper 2D array
If your problem is the length of the data, then I assume that by proper 2D array you mean that you intend to pad each cell of a column to be the same width.
In that case, I recommend not to waste time on the idea of writing column first. Instead, calculate the widths at runtime. First sprintf into a matrix of strings and calculate the length of each cell. Then print the matrix of strings using the calculated lengths.

How to Access a Moving Portion of an Array

I'm at a loss for what to do in my program (written in C). There is a large matrix of numbers (an image) that I am processing. The processing happens one line at a time, with reference to the previous line, so I only need to access two lines of numbers at a time.
Originally, I tried a 2 by X array but once I save the information for the third line, the array is upside down with the third line of the image in the first row of the array and the second line of the image in the second row of the image.
Is there a better way to correct this other than simply copying the second row of the array to the first row? Maybe it wouldn't be so bad, but I would imagine doing that for every other line of the image would be expensive. Should I use two pointers on the image instead?
I apologize if this is a common thing that can be easily looked up but I couldn't figure out how to begin looking. If anyone needs clarification, let me know. Thank you very much!
Diagram of what numbers I need access to:
http://www.gliffy.com/go/publish/5968966
I suppose that you are processing the image as you read it, or as you decompress it, or some such, for if you already had the whole thing in memory in usable form then you would just use that.
I see two reasonably good alternatives:
Instead of hard-coding the indices of the earlier and later lines in your 2 by x array, use variables to track which row contains which line.
Use a 1-D array for each line, and use pointers to track which one contains the current line and which one contains the previous line.
(Though really, those boil down to pretty much the same thing.) Either way, you can avoid needless copying.
Let's assume you have:
struct rbg_t bitmap[X][Y];
To get a window of dimensions X by 2, it is like this:
struct rgb_t *first_line = bitmap[0], *second_line=bitmap[1];
Then you can process the two lines like so:
for(int i=0;i<X;++i)
{
do_work(first_line[i], second_line[i]);
}
To shift the window down by one, you can do this:
first_line+=sizeof(struct rgb_t)*X;
second_line+=sizeof(struct rgb_t)*X;
Where X is the width of the bitmap

Read a mat data file and to draw 2D vector out of it

Here is a bit of detail, I am from C++ old language background, while resolving Matlab question asked by my teacher.
I am given a mat file which contains real data, and I need to read it, assign the values to 2-D array, as a spike of waveform is a 2-D array. Plot it on X and Y axis.
Then I need to make a threshold by looking at spikes that most spikes are between, e.g, a range of this number so chop of the extra bit over, and take only spike (2-D array) which have under a certain threshold.
Spike means a simple signal which you see when a patient is sick and its heart beat is showing on screen.
My data file is 313 Mb in size. So can anyone guide me how to deal with this big file as well.
So any help code would be great.
Type the following in Matlab:
help load
Read it. Then type:
help plot
Read it. Make a start on your problem, then come back for help.
First load your .mat file into the current workspace:
load(filename)
filename would be something like 'data.mat'
After this you should have your 2D array in the workspace...let's assume it's named 'data'. If the first row is the X axis and the second row is the Y axis, then use:
plot(data(1,:), data(2,:))
The ':' in MATLAB selects every column in the 2D array. You can then use the following to find all indexes of values over your threshold:
indexes = find(data(1,:) > threshold)
If you want to saturate these values at your threshold, then do:
data(1,indexes) = threshold
The size of your .mat file shouldn't change anything other than how long each function takes to complete.
Edit: You were vague and unclear in your problem statement, so hopefully I understood you correctly. Let me know if I didn't understand what you wanted.

Resources