I am trying to read pixel data from a PPM file. I have a function to read the header so I know it's pointing to the beginning of the pixel data. I tried using fgetc(file) but if the value is more than one digit it will not work. I also tried using an array and converting the array to an int or char but I have no way of knowing how many digits each value is. I'm also not sure if the values are separated by whitespace or not. Basically I need a way to extract the pixel data. (I'm using C.)
My code right now is:
char read_byte(FILE *ipt) {
int c, i=0, sum=0;
while (i<16) {
c=fgetc(ipt);
if((i%2)!=0 {
if(c&1) {
sum+=pow(2,i/2);
}
}
i++;
}
return (char)sum;
}
EDIT:
At first I thought the file was stored as the ASCII values, then I realized it's stored as binary. Right now I think I'm making it act like hex. I'm not sure if that's correct. I'm really stuck.
EDIT: changed my code a bit
char read_byte(FILE *ipt) {
int c, i=0, sum=0;
while(i<8) {
c = fgetc(ipt);
c=c&1;
sum+=c*pow(2,i);
i++;
}
return sum;
}
I print the sum as %c
Must you write this for an assignment, or is it for pleasure, or could you use someone else's code?
There is an Open Source solution.
"Netpbm is a package of graphics programs and a programming library. " which includes programs to read PPM at http://netpbm.sourceforge.net/doc/
Edit:
Have you got, or read the definition of the file format, e.g. http://netpbm.sourceforge.net/doc/ppm.html?
It looks like the data is either sequences of one byte RGB triples, or sequences of two byte RGB triples.
The program can detect which format is used from item 7 "The maximum color value (Maxval)". It says "If the Maxval is less than 256, it is 1 byte. Otherwise, it is 2 bytes."
So you code a function which reads one byte/component RGB data, then code another to read two byte/component RGB data.
The program can choose which to call once it has read the value of Maxval.
Edit {
According to the document at that link, the image data in a 'P6' ppm is binary.
So if MaxValue is <256, and hence the data for each colour component is one byte, then reading three bytes, with three calls of fgetc(fp) would return the binary value of one RGB pixel.
If the program has read the header, it has the values of width and height for the image data. So it could allocate an array for every row (width wide of RGB pixels), and an array of pointers to each allocated pixel row array. Then read the binary data into each row, and the program has something straightforward to operate on; a 2d array.
} end edit
My reading of your question suggests you already know how to read one byte data using fgetc.
Edit - it seems like this is irrelevant:
You can read two byte data by calling fgetc twice, and shifting and bit or-ing the data, e.g. (partly ignoring error checking):
int a = fgetc(fp);
int b = fgetc(fp);
if (a >= 0 && b >= 0) { // then all is okay
unsigned int twobyte = (a<<8) | b; // or (b<<8) | a; depending on byte order
// ...
Related
I wrote this function that performs a slightly modified variation of run-length encoding on text files in C.
I'm trying to generalize it to binary files but I have no experience working with them. I understand that, while I can compare bytes of binary data much the same way I can compare chars from a text file, I am not sure how to go about printing the number of occurrences of a byte to the compressed version like I do in the code below.
A note on the type of RLE I'm using: bytes that occur more than once in a row are duplicated to signal the next-to-come number is in fact the number of occurrences vs just a number following the character in the file. For occurrences longer than one digit, they are broken down into runs that are 9 occurrences long.
For example, aaaaaaaaaaabccccc becomes aa9aa2bcc5.
Here's my code:
char* encode(char* str)
{
char* ret = calloc(2 * strlen(str) + 1, 1);
size_t retIdx = 0, inIdx = 0;
while (str[inIdx]) {
size_t count = 1;
size_t contIdx = inIdx;
while (str[inIdx] == str[++contIdx]) {
count++;
}
size_t tmpCount = count;
// break down counts with 2 or more digits into counts ≤ 9
while (tmpCount > 9) {
tmpCount -= 9;
ret[retIdx++] = str[inIdx];
ret[retIdx++] = str[inIdx];
ret[retIdx++] = '9';
}
char tmp[2];
ret[retIdx++] = str[inIdx];
if (tmpCount > 1) {
// repeat character (this tells the decompressor that the next digit
// is in fact the # of consecutive occurrences of this char)
ret[retIdx++] = str[inIdx];
// convert single-digit count to string
snprintf(tmp, 2, "%ld", tmpCount);
ret[retIdx++] = tmp[0];
}
inIdx += count;
}
return ret;
}
What changes are in order to adapt this to a binary stream? The first problem I see is with the snprintf call since it's operating using a text format. Something that rings a bell is also the way I'm handling the multiple-digit occurrence runs. We're not working in base 10 anymore so that has to change, I'm just unsure how having almost never worked with binary data.
A few ideas that can be useful to you:
one simple method to generalize RLE to binary data is to use a bit-based compression. For example the bit sequence 00000000011111100111 can be translated to the sequence 0 9623. Since the binary alphabet is composed by only two symbols, you need to only store the first bit value (this can be as simple as storing it in the very first bit) and then the number of the contiguous equal values. Arbitrarily large integers can be stored in a binary format using Elias gamma coding. Extra padding can be added to fit the entire sequence nicely into an integer number of bytes. So using this method, the above sequence can be encoded like this:
00000000011111100111 -> 0 0001001 00110 010 011
^ ^ ^ ^ ^
first bit 9 6 2 3
If you want to keep it byte based, one idea is to consider all the even bytes frequencies (interpreted as an unsigned char) and all the odd bytes the values. If one byte occur more than 255 times, than you can just repeat it. This can be very inefficient, though, but it is definitively simple to implement, and it might be good enough if you can make some assumptions on the input.
Also, you can consider moving out from RLE and implement Huffman's coding or other sophisticated algorithms (e.g. LZW).
Implementation wise, i think tucuxi already gave you some hints.
You only have to address 2 problems:
you cannot use any str-related functions, because C strings do not deal well with '\0'. So for example, strlen will return the index of the 1st 0x0 byte in a string. The length of the input must be passed in as an additional parameter: char *encode(char *start, size_t length)
your output cannot have an implicit length of strlen(ret), because there may be extra 0-bytes sprinkled about in the output. You again need an extra parameter: size_t encode(char *start, size_t length, char *output) (this version would require the output buffer to be reserved externally, with a size of at least length*2, and return the length of the encoded string)
The rest of the code, assuming it was working before, should continue to work correctly now. If you want to go beyond base-10, and for instance use base-256 for greater compression, you would only need to change the constant in the break-things-up loop (from 9 to 255), and replace the snprintf as follows:
// before
snprintf(tmp, 2, "%ld", tmpCount);
ret[retIdx++] = tmp[0];
// after: much easier
ret[retIdx++] = tmpCount;
I tried to run this program through the terminal and this error showed up.
"Segmentation Fault: 11"
I would like to know why . What this program does is, it reads a .ppm file and it saves it's information in a matrix variable of type Pixel, so, a PPM file is basically composed by: the first line is going to be "P3" by default, second line the size of the matrix, and the third line the highest value possible for a Pixel attribute, the other lines will have 3 integers of maximum value of 255, so for each member of the matrix there will be a pixel R, G, B.
what I tried to do in the function save_image, first recognize if we are dealing with a ppm file(checking if there is a P3 in the first line), then read the number of rows and columns for the matrix, then it creates a new matrix using the malloc function, then it will save the data in the lines of the file to the .r and .g and .b of the variable myImg.
I am very new to debugging/programming so I'm sorry if this isn't enough information but I tried my best.
#include <stdio.h>
#include <stdlib.h>
typedef struct{
int r;
int g;
int b;
}Pixel;
void save_image(FILE* img, Pixel ** newImg) {
int i;
int j;
int fcount;
int scount;
int count;
int dcc;
char init[3];
fscanf(img,"%s",init);
if(init[0]=='P' && init[1]=='3'){
printf("worked!\n");
fscanf(img,"%d %d",&j,&i);
fscanf(img, "%d",&dcc);
*newImg = (Pixel*)malloc(sizeof(Pixel) * i);
for ( count = 0; count < i ; ++count)
{
newImg[count] = (Pixel*)malloc(sizeof(Pixel) * j);
}
for (fcount = 0; fcount <= i ; ++fcount)
{
for (scount = 0; scount <= j; ++scount)
{
fscanf(img,"%i %i %i",&newImg[i][j].r,&newImg[i][j].g,&newImg[i][j].b);
}
}
}
else
printf("Type of file not recognized\n");
fclose(img);
}
int main(int argc, char const *argv[])
{
FILE* image;
Pixel myImg;
Pixel** newImg;
**newImg = myImg;
image = fopen(argv[1],"r");
save_image(image,newImg);
return 0;
}
The program fails because the initial malloc for newImg[] is malloc'ing some multiple of the size of Pixel rather than size of pointer to Pixel combined with problems with the passing of the pointer to the newImg as a parameter to the save_image() function. See my comment about where the variable newImg should be defined and the desirable modification to the declaration of the save_image() function
Given the was the posted code is written, it seems to be expecting the 'plain' .ppm file format
and the posted code is failing to allow for any embedded comments within the file
given this description of the format of a .ppm file:
The format definition is as follows. You can use the libnetpbm C subroutine library to read and interpret the format conveniently and accurately.
A PPM file consists of a sequence of one or more PPM images. There are no data, delimiters, or padding before, after, or between images.
Each PPM image consists of the following:
A "magic number" for identifying the file type. A ppm image's magic number is the two characters "P6".
Whitespace (blanks, TABs, CRs, LFs).
A width, formatted as ASCII characters in decimal.
Whitespace.
A height, again in ASCII decimal.
Whitespace.
The maximum color value (Maxval), again in ASCII decimal. Must be less than 65536 and more than zero.
A single whitespace character (usually a newline).
A raster of Height rows, in order from top to bottom. Each row consists of Width pixels, in order from left to right. Each pixel is a triplet of red, green, and blue samples, in that order. Each sample is represented in pure binary by either 1 or 2 bytes. If the Maxval is less than 256, it is 1 byte. Otherwise, it is 2 bytes. The most significant byte is first.
A row of an image is horizontal. A column is vertical. The pixels in the image are square and contiguous.
In the raster, the sample values are "nonlinear." They are proportional to the intensity of the ITU-R Recommendation BT.709 red, green, and blue in the pixel, adjusted by the BT.709 gamma transfer function. (That transfer function specifies a gamma number of 2.2 and has a linear section for small intensities). A value of Maxval for all three samples represents CIE D65 white and the most intense color in the color universe of which the image is part (the color universe is all the colors in all images to which this image might be compared).
ITU-R Recommendation BT.709 is a renaming of the former CCIR Recommendation 709. When CCIR was absorbed into its parent organization, the ITU, ca. 2000, the standard was renamed. This document once referred to the standard as CIE Rec. 709, but it isn't clear now that CIE ever sponsored such a standard.
Note that another popular color space is the newer sRGB. A common variation on PPM is to substitute this color space for the one specified.
Note that a common variation on the PPM format is to have the sample values be "linear," i.e. as specified above except without the gamma adjustment. pnmgamma takes such a PPM variant as input and produces a true PPM as output.
Strings starting with "#" may be comments, the same as with PBM.
Note that you can use pamdepth to convert between a the format with 1 byte per sample and the one with 2 bytes per sample.
All characters referred to herein are encoded in ASCII. "newline" refers to the character known in ASCII as Line Feed or LF. A "white space" character is space, CR, LF, TAB, VT, or FF (I.e. what the ANSI standard C isspace() function calls white space).
Plain PPM
There is actually another version of the PPM format that is fairly rare: "plain" PPM format. The format above, which generally considered the normal one, is known as the "raw" PPM format. See pbm for some commentary on how plain and raw formats relate to one another and how to use them.
The difference in the plain format is:
There is exactly one image in a file.
The magic number is P3 instead of P6.
Each sample in the raster is represented as an ASCII decimal number (of arbitrary size).
Each sample in the raster has white space before and after it. There must be at least one character of white space between any two samples, but there is no maximum. There is no particular separation of one pixel from another -- just the required separation between the blue sample of one pixel from the red sample of the next pixel.
No line should be longer than 70 characters.
Here is an example of a small image in this format.
P3
# feep.ppm
4 4
15
0 0 0 0 0 0 0 0 0 15 0 15
0 0 0 0 15 7 0 0 0 0 0 0
0 0 0 0 0 0 0 15 7 0 0 0
15 0 15 0 0 0 0 0 0 0 0 0
There is a newline character at the end of each of these lines.
Programs that read this format should be as lenient as possible, accepting anything that looks remotely like a PPM image.
I am writing in C and I have to write code that will invert the RGB values for each pixel in an image. It's a simple process in that you take the max color value and subtract the actual RGB values. I have successfully read in the max color value, but when trying to invert the values, everything is returned as 0 and when written to a new file is not readable. Below is the code, any ideas?
Inverting the picture
int i,j;
for(i=0; i<myPic->rows;++i) {
//Moves up and down the columns reaching all Pixels
//Moves across left to right across all columns
for (j=0;j<myPic->cols;++j) {
//Inverstion requires the actual value to be subtracted from the max
myPic->pixels[i][j].red = myPic->colors - myPic->pixels[i][j].red;
myPic->pixels[i][j].green = myPic->colors - myPic->pixels[i][j].green;
myPic->pixels[i][j].blue = myPic->colors - myPic->pixels[i][j].blue;
}
}
return myPic;
}
Output of the image
fprintf(name,"P3\n%d %d\n%d\n",myPic->rows,myPic->cols,myPic->colors);
//The P3,rows,columns,and color values are all printed first
int i,j;
for(i=0; i< myPic->rows;++i) {
for(j=0; j<myPic->cols;++j) { //Each Pixel is printed one at a time
fprintf(name,"%d",myPic->pixels[i][j].red); //Red printed first
fprintf(name,"%d",myPic->pixels[i][j].green); //Green printed second
fprintf(name,"%d",myPic->pixels[i][j].blue); //Blue printed third
fprintf("\n");
}
}
}
Thanks for the help guys, this is what I'm using now and it works
When writing the pixel data, this line
myPic->pixels[i] = malloc(sizeof(Pixel) *myPic->cols);
overwrites the existing pointer, and makes it point to new (and more importantly, uninitialized) data.
Copy-paste programming (which you seem to have been doing) can work sometimes, but you have to take care to modify the copied code properly.
On an unrelated note, you don't print a newline after each line, so the resulting PPM file will actually not be correct.
#Joachim is correct. so do something like this:
int i,j;
for(i=0; i<myPic->rows;++i) {
Pixel[myPic->cols] temp = *myPic->pixels[i];
//Moves up and down the columns reaching all Pixels
myPic->pixels[i] = malloc(sizeof(Pixel) *myPic->cols);
//Moves across left to right across all columns
for (j=0;j<myPic->cols;++j) {
//Inverstion requires the actual value to be subtracted from the max
myPic->pixels[i][j].red = myPic->colors - temp[j].red;
myPic->pixels[i][j].green = myPic->colors - temp[j].green;
myPic->pixels[i][j].blue = myPic->colors - temp[j].blue;
}
}
return myPic;
}
Here's my code so far:
#include <stdio.h>
#include <stdint.h>
#define N_DET 432
#define N_PROJ 500
int readSinogram(uint16_t mess[],char filename[], int sample){
//init
FILE *fp;
int i;
//open file
fp=fopen(filename,"r");
//read sinogram
fseek(fp,sizeof(uint16_t)*N_DET*N_PROJ*sample*2,SEEK_SET); //move pointer
fread(mess,sizeof(uint16_t),N_DET*N_PROJ,fp); //read sinogram
return 0;
};
int main()
{
uint16_t mess[N_DET*N_PROJ];
char filename[]="C:\\Path\\MyFile.fxc";
double curr;
int i,DET,PROJ;
readSinogram(mess,filename,0); //read the data
printf("\nDET?"); //ask for input
scanf("%u", &DET);
printf("\nPROJ?");
scanf("%u", &PROJ);
curr=mess[DET+N_DET*PROJ]; //get the data
printf("Data: %f",curr); //print the data
return 0;
}
This reads in the .fxc file, which is just a binary file containing uint16_t formatted numbers. "readSinogram" reads one data set which contains N_DET*N_PROJ numbers. (The pointer is moved by twice the block size, because there are alternating blocks of two measurements in the file.)
After reading it, one can put in a DET and PROJ and have a look at the data at this point.
So far, everything works fine. The data is correct for a certain data range but when asking for too big DET and/or PROJ the data is incorrect.
I do have the same file read in in Matlab and can confirm that the data in it is fine.
To be more exact: every index above [DET+N_DET*PROJ] > 248835 will return 52428 instead of the correct value (ranging from 0 to 4088). The values up to those work fine. So there's got to be something wrong with the "mess" array above that index, I guess.
What am I doing wrong? Thanks in advance!
you need allocate a larger array to get to index value greater than 248835
currently you have the following defines
#define N_DET 432
#define N_PROJ 500
which leads to the array size of 432*500 = 216000
now if your indexing the array at value 248835 248835 > 216000 you would cause to access the memory out of the allocated memory for the array which would lead to undefined behavior. What you need is a bigger size array which can accommodate more than 248835 entries. a simple #define N_DET 500 would do that, but you have to make sure that is the requirement. A basic index check would be helpful to avoid having a out of range problem something on the lines of
#define MAX_RANGE 500*500
if((DET+N_DET*PROJ) < MAX_RANGE)
curr=mess[DET+N_DET*PROJ]; //get the data
else
//error handling
you also need error handling in fopen fseek and check the return for fread plus optionally you can close the opened file discriptor by close(fd)
Can anyone help me with some code i need to implement bit stuffing on an array of data? The program is for an AVR micro-controller (Tiny84A) using GNU C.
unsigned char datas[3] = {00011111,10000001,00000000};
To add 0 to each set of 5 one’s i.e. after every five consecutive 1′s appear a zero
Therefore data should be
00011111,10000001 becomes 00011111 01000000 10000000
I'm unsure where to start, an example would be great!
There are lots of examples on the web for bit stuffing (inserting a 0 after 5 1s). Unfortunately, many of them involve reading and writing strings of characters 0 and 1.
There are also examples of bit streams. It looks like you need to find examples of each and combine them.
You need to expand your question to explain more of the design decisions you have made.
In particular, it looks like you are expecting the input to be in an array instead of a stream. Are you expecting to write the output to the same array? That is a little bit trickier than writing to a different array. A stream of bits would be different again.
The basic idea is to keep counters of where you are in the input and output streams, and load and write bytes appropriately as you reach the end of 8 bits.
unsigned char datas[3] = {00011111,10000001,00000000};
int in_byte, in_bit, out_byte, out_bit;
void init()
{
in_byte = 0; in_bit = 0; out_byte = 0; out_bit = 0;
}
int get_bit()
{
int ret = 0;
if (datas[in_byte] & 1 << (7 - in_bit))
{
ret = 1;
{
++in_bit;
if (in_bit == 8)
{
in_bit = 0;
++in_byte;
}
return ret;
}
You have to write your own void put_bit(int bit), but it is similar using the "or" operator |.
You also need to make a function to do the loop that counts up to 5 and puts an extra 0. Look at the examples on the web for that.