Issues writing PNM P6 - c

I'm writing a program that takes in two duplicate PNM P6 files, puts the memory of the first file into a buffer, creates a yellow diagonal line over that, and writes the result to the second file. When I run it, the output file is corrupted and can't be displayed. I noticed when looking at the output that it's missing the three lines that should be at the top:
P6
1786 1344
255
I don't know how to programmatically ensure that those lines stay in the code -- I can't figure out why they're even being overwritten in the first place.
When I manually add those lines to the output, the file is no longer corrupted. However, no yellow diagonal line appears. Is this part of the same issue, or is there something else I should look into fixing?
My code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct
{
unsigned char red, green, blue;
} iPixel;
typedef struct
{
int x, y;
iPixel *data;
} Image;
Image *
ReadImage(char *filename)
{
FILE *f = fopen(filename, "rb");
Image *img = NULL;
char magicNum[128];
int width, height, maxval;
if (f == NULL)
{
fprintf(stderr, "Unable to open file %s\n", filename);
return NULL;
}
fscanf(f, "%s\n%d %d\n%d\n", magicNum, &width, &height, &maxval);
/*
printf("Magic num = %s width = %d, height = %d, maxval = %d\n",
magicNum, width, height, maxval)
;
*/
if (strcmp(magicNum, "P6") != 0)
{
fprintf(stderr, "Unable to read from file %s, because it is not a PNM file of type P6\n", filename);
return NULL;
}
img = (Image *) malloc(sizeof(Image));
img -> x = width;
img -> y = height;
img -> data = (iPixel*) malloc(img -> x * img -> y * sizeof(iPixel));
fread(img -> data, sizeof(iPixel), width*height, f);
fclose(f);
return img;
}
void WriteImage(Image *img, char *filename)
{
FILE *f = fopen(filename, "wb");
fwrite(img->data, sizeof(iPixel), img-> x * img-> y, f);
fclose(f);
}
Image *
YellowDiagonal(Image *input)
{
int i, j;
for (i = 0; i < input->x; i++)
{
for (j=0; j < input->y; j++)
{
if (i==j)
{
input->data[i].red=255;
input->data[i].green=255;
input->data[i].blue=0;
}
}
}
return input;
}
int main(int argc, char *argv[])
{
if (argc != 3)
{
fprintf(stderr, "Usage: ./3A_c.c <input image file> <output image file>\n");
exit(EXIT_FAILURE);
}
Image *img;
img = ReadImage(argv[1]);
fprintf(stderr, "Read.\n");
YellowDiagonal(img);
fprintf(stderr, "Diagonal line.\n");
WriteImage(img, argv[2]);
fprintf(stderr, "Write.\n");
}

Be careful to write exactly the same format you are reading. The PNM format is well defined, and you are reading it correctly. However, in the writing routine there were a couple of mistakes:
opening a file with "w" or "wb" truncates it to 0 bytes;
best practice is always to check if fopen succeeds;
reading actual ASCII data can be done with fscanf, binary data with fread. Similarly, writing ASCII should be done with fprintf and only binary data again with fwrite.
If you want to make sure you write the same data as you read in before, you need to save it somewhere. The maxval variable is read, but not saved, and so I cannot write it back. However, it is not a huge problem because the rest of your code assumes the image is R8G8B8 anyway, and so maxval should always be 255.
Here is an adjusted WriteImage that works.
void WriteImage(Image *img, char *filename)
{
FILE *f = fopen(filename, "wb");
if (f == NULL)
{
printf ("Unable to open '%s' for writing!\n", filename);
/* better would be: "return -1" to indicate an error, 0 otherwise */
return;
}
fprintf (f, "P6\n");
fprintf (f, "%d %d\n", img->x, img->y);
/* actually you need to write 'maxval' here */
fprintf (f, "%d\n", 255);
fwrite(img->data, sizeof(iPixel), img->x * img->y, f);
fclose(f);
}
With the above out of the way, you can now see your 'diagonal line' is not correct! I'm not going to fix that (I suppose not being able to see what happened stopped you in your tracks), but here are a few pointers just to guide you:
no need to return an Image * if you are changing data in situ anyway
no need to check every single pixel
check the coordinates of what pixels are changed ...

Related

C: Writing structs of RGB values to file to create ppm image - premature end of file

The program i am creating a steganography program that hides a secret message inside a .ppm image by changing random red pixel values to ascii characters.
The program is based on code that is on stackoverflow for reading and writing ppm images (read PPM file and store it in an array; coded with C), all other code is my own work. I have completed all the necessary functions to do this such as writing,reading,encoding and decoding the files but i am struggling to grasp the fwrite function.
Currently when the program encodes an image it takes in the .ppm converts it to its rgb values in a struct. Then it hides the secret message by editing the red values to ascii characters. The issue arises when it comes to "printing" the image to a file. When the program has completed the image produced is around 90% of what it should be printing. Example show below:
Example of the unfinished image
I have checked it is storing all the values are being stored correctly by printing all the rgb values and it is. (used the showPPM method). Is there not enough memory to write the image? is the image to large for the write function? these are my guesses.
Any information on how i should go about changing the writePPM function so that i correctly print 100% of the image to the file would be great.
Here is the code below:
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
#include<time.h>
typedef struct {
unsigned char red,green,blue;
} PPMPixel;
typedef struct {
int x, y;
PPMPixel *data;
} PPMImage;
void writePPM(PPMImage *img);
static PPMImage *getPPM(const char *filename)
{
char buff[16];
PPMImage *img;
FILE *fp;
int c, rgb_comp_color;
//open PPM file for reading
fp = fopen(filename, "rb");
if (!fp) {
fprintf(stderr, "Unable to open file '%s'\n", filename);
exit(1);
}
//read image format
if (!fgets(buff, sizeof(buff), fp)) {
perror(filename);
exit(1);
}
//check the image format
if (buff[0] != 'P' || buff[1] != '3') {
fprintf(stderr, "Invalid image format (must be 'P3')\n");
exit(1);
}else{
printf("P3\n");
}
//alloc memory form image
img = (PPMImage *)malloc(sizeof(PPMImage));
if (!img) {
fprintf(stderr, "Unable to allocate memory\n");
exit(1);
}
c = getc(fp);
while (c == '#') {
while (getc(fp) != '\n') ;
c = getc(fp);
}
ungetc(c, fp);
//read image size information
if (fscanf(fp, "%d %d", &img->x, &img->y) != 2) {
fprintf(stderr, "Invalid image size (error loading '%s')\n", filename);
exit(1);
}else{
printf("Height: %d\n",img->x);
printf("Width: %d\n",img->y);
}
//read rgb component
if (fscanf(fp, "%d", &rgb_comp_color) != 1) {
fprintf(stderr, "Invalid rgb component (error loading '%s')\n", filename);
exit(1);
}else{
printf("%d\n",rgb_comp_color );
}
//check rgb component depth
if (rgb_comp_color!= 255) {
fprintf(stderr, "'%s' does not have 8-bits components\n", filename);
exit(1);
}
while (fgetc(fp) != '\n') ;
//memory allocation for pixel data
img->data = (PPMPixel*)malloc(24*img->x * img->y * sizeof(PPMPixel));
if (!img) {
fprintf(stderr, "Unable to allocate memory\n");
exit(1);
}
//read pixel data from file
if (fread(img->data, 10*img->x, img->y, fp) != img->y) {
fprintf(stderr, "Error loading image '%s'\n", filename);
exit(1);
}
fclose(fp);
return img;
}
struct PPMImage * encode(char * text, PPMImage * img)
{
//convert secret message to ascii code
int i,ascii,height,width;
int total = 0;
int rolling = 0;
int original = 0;
time_t t;
srand((unsigned) time(&t));
height=img->y;
width=img->x;
for(i = 0; text[i]; i++){
ascii = text[i];
//create random number between 0 and max the width
total = total + rand() % width;
original = total;
//printf("Random Number: %d\n",total);
if(total >= width){
rolling = rolling + 1;
total = total - width;
}
//printf("Before R: %d \n",img->data[0].red );
img->x=rolling;
img->y=total;
printf("X: %d ",rolling );
printf("Y: %d ",total );
//set img position
//at position random we set the red bit equal to ascii number
printf("Old R: %d ",img->data[i].red );
img->data[i].red=ascii;
printf("New R: %d\n ",img->data[i].red );
}
//take img then print it out
//setting the img values again for printing
img->x=width;
img->y=height;
writePPM(img);
}
void writePPM(PPMImage *img)
{
FILE *fp;
//open file to be written
fp = fopen("encoded.ppm", "wb");
if (!fp) {
fprintf(stderr, "Unable to open file \n");
exit(1);
}
//image format
fprintf(fp, "P3\n");
//comments
//need to store comments to be outputted
fprintf(fp, "# Created by Sean \n");
//image size
fprintf(fp,"%d %d\n",img->x,img->y);
// rgb component depth
fprintf(fp, "%d\n",255);
//write pixels currently not fully working
fwrite(img->data, sizeof(img->data), 3*img->y*img->x, fp);
//close file stream
fclose(fp);
}
void showPPM(PPMImage *img)
{
int i;
if(img){
for(i=-1;i<img->x*img->y;i++){
printf("Number: %d\n",i);
printf("R: %d ",img->data[i].red );
printf("G: %d ",img->data[i].green );
printf("B: %d\n ",img->data[i].blue );
}
}
}
char * decode(PPMImage * i1,PPMImage * i2){
//compare difference in number of bits in red pixels
//if there is a different then take the red pixel value from the encrypted image
//then translate it from ascii to chars then print.
printf("Decoding......\n");
int i;
for(i=-1;i<i1->x*i1->y;i++){
if(i1->data[i].red != i2->data[i].red){
printf("%c",i1->data[i].red );
}
}
//to be able to test and finish this need to write code for encoding
}
int main(int argc, char *argv[]){
//input statements
if(argc == 3){
PPMImage *image;
image = getPPM(argv[2]);
//uncomment the showPPM to display all rgb values in the encoded files
//showPPM(image);
if(argv[1] = "e"){
printf("Please enter your secret message to be encoded estimated max characters: %d\n",image->y);
//need to add user input
encode("test output!",image);
}
}else if(argc == 4){
PPMImage *i1;
PPMImage *i2;
i1 = getPPM(argv[2]);
i2 = getPPM(argv[3]);
if(argv[1] = "d"){
decode(i1,i2);
}
}else{
printf("Wrong arguments");
}
}
The problem is actually in the code for reading in the PPM, which you have modified in a way that appears to work, but actually doesn't because the file format is different to what you think it is.
The code that you linked to is for reading PPM files in the "raw" format. These files start with the "P6" code. In these files, each RGB value is stored as either 1 or 2 bytes (depending on whether the RGB component depth is less than 256). So, if the max value is 255, it's 1 bytes per value, so the file size is width * height * 3.
However, you have modified the code to read "plain" PPM files, which start with the "P3" code, by checking for P3 and reading in more data. These files don't store the RGB values as raw binary data, but as ASCII text specifying the value in decimal format, separated by whitespace. So for example if you had the value 93 in raw format, it would just be 1 byte with a value of 93, but in the "plain" format it would be 3 (or more) bytes: one or more bytes with the ASCII value for a space (or tab), then the ASCII value for "9" (which is 57) then the ASCII value for "3" (which is 51). It's impossible to calculate the size of the file based on the width and height because the whitespace can be variable, and each value could be represented between 1 and 3 digits.
Despite the fact that you are not parsing the data as ASCII-encoded text, your PPM-reading code seems to work because you are just reading in a chunk of data, (optionally) modifying a few random bytes and then writing it out again either totally or mostly unchanged.
So, your possible solutions are:
Change the getPPM code back to what it was and use an actual P6 file.
Write a PPM reader that parses the data properly as ASCII text containing whitespace-separated decimal numbers (you can write out as either P3 or P6).
More info: PPM Format Specification

C Display PPM image as Text [duplicate]

This question already has answers here:
read PPM file and store it in an array; coded with C
(2 answers)
Closed 6 years ago.
I am new to programming C and i am experimenting with file manipulation, i am trying to output a PPM file with its comments and rgb data values like so:
P3
# The same image with width 3 and height 2,
# using 0 or 1 per color (red, green, blue)
3 2 1
1 0 0 0 1 0 0 0 1
1 1 0 1 1 1 0 0 0
In this program i have been able to check that it has the correct format and read in to a struct but where i am stuck is how to collect the rgb data and then printing it out. Here is what i have so far. The method to display this information is the showPPM struct which i have started but do not know how to read the image struct and collect its rgb values and display it, any help would be great.
#include<stdio.h>
#include<stdlib.h>
typedef struct {
unsigned char red,green,blue;
} PPMPixel;
typedef struct {
int x, y;
PPMPixel *data;
} PPMImage;
static PPMImage *readPPM(const char *filename)
{
char buff[16];
PPMImage *img;
FILE *fp;
int c, rgb_comp_color;
//open PPM file for reading
fp = fopen(filename, "rb");
if (!fp) {
fprintf(stderr, "Unable to open file '%s'\n", filename);
exit(1);
}
//read image format
if (!fgets(buff, sizeof(buff), fp)) {
perror(filename);
exit(1);
}
//check the image format
if (buff[0] != 'P' || buff[1] != '3') {
fprintf(stderr, "Invalid image format (must be 'P6')\n");
exit(1);
}
//alloc memory form image
img = (PPMImage *)malloc(sizeof(PPMImage));
if (!img) {
fprintf(stderr, "Unable to allocate memory\n");
exit(1);
}
//check for comments
c = getc(fp);
while (c == '#') {
while (getc(fp) != '\n') ;
c = getc(fp);
}
ungetc(c, fp);
//read image size information
if (fscanf(fp, "%d %d", &img->x, &img->y) != 2) {
fprintf(stderr, "Invalid image size (error loading '%s')\n", filename);
exit(1);
}
while (fgetc(fp) != '\n') ;
//memory allocation for pixel data
img->data = (PPMPixel*)malloc(img->x * img->y * sizeof(PPMPixel));
if (!img) {
fprintf(stderr, "Unable to allocate memory\n");
exit(1);
}
//read pixel data from file
if (fread(img->data, 3 * img->x, img->y, fp) != img->y) {
fprintf(stderr, "Error loading image '%s'\n", filename);
exit(1);
}
fclose(fp);
return img;
}
void showPPM(struct * image){
int rgb_array[600][400];
int i;
int j;
for(i = 0; i<600; i++)
{
for(j = 0; j<400; j++)
{
printf("%d", rgb_array[i][j]);
}
}
}
int main(){
PPMImage *image;
image = readPPM("aab.ppm");
showPPM(image);
}
Your code looks suspiciously like:
read PPM file and store it in an array; coded with C
There were some really good comments and help on that as well.
So, I leave the other stuff below as further help:
Starting off somewhat vague here, to let you have chance to work through it yourself...
First, you need to read in the entire ppm file into a buffer. fread() is probably the function of choice for this. You can use ftell() to get the size of the file. Next, you can typecast your PPMImage structure on top of the buffer used by fread(), and from there, you should be able to access the data via the data field, maybe even using array notation if you wished. (I think this is the step you are missing right now...)
Once you can access the data, you should be able to iterate over the data and printf() or whatever you need to the console based on the input data.

Display rgb values of ppm image and conflict type error in C

I'm fairly new to C and have been tasked with creating a program that reads an image and displays that image. I've created (what I believe to be) a function to read in the files format/comments etc, and have attempted to create a function to display each pixels rgb values.
I cannot test it because I have an error in my program - 'conflicting types for getPPM'. Could anyone explain why I'm getting this error? and any possible fix to this would be great.
If anyone could suggest a way to be able to store the comments (maybe in array?) and print them later on that would also be really helpful! Thanks.
Also feel free to scrutinize the program as a whole and give me feedback, I'm not entirely sure if it will actually do anything once the above error is fixed.
Thanks very much!
#include <stdio.h>
#include <stdlib.h>
#define MAX_HEIGHT 600
#define MAX_WIDTH 400
int main(int argc, char ** argv){
// get image file
FILE *file;
file = fopen("aab(1).ppm", "r");
//if there is no file then return an error
if(file == NULL){
fprintf(stderr, "File is not valid");
return 0;
}
else {
struct PPM *newPPM = getPPM(file);
return 0;
}
}
struct PPM {
char format[2]; //PPM format code
int height, width; //image pixel height and width
int max; //max rgb colour value
} PPM;
struct PPM_Pixel {
//Create variables to hold the rgb pixel values
int red, green, blue;
} PPM_Pixel;
struct PPM *getPPM(FILE * fd){
struct PPM *image;
char buffer[16];
char c;
char commentArray[256];
fd = fopen(fd, "r");
struct PPM *newPPM = malloc(sizeof(PPM));
//checks if the file being read is valid/the correct file
if(fd == NULL){
exit(1);
}
//read the image of the format
if(!fgets(buffer, sizeOf(buffer), fd)){
exit(1);
}
//checks the format of the ppm file is correct
if(buffer[0] != 'P' || buffer[1] != '3'){
fprintf(stderr, "Invalid image format! \n");
exit(1);
}else {
printf("%s", newPPM->format);
}
//allocate memory for the image
image = malloc(sizeof(PPM));
if(!image){
fprintf(stderr, "Cannot allocate memory");
exit(1);
}
//checks whether the next character is a comment and store it in a linked list
c = getc(fd);
while(c == '#'){
while(getc(fd) != '\n'){
c = getc(fd);
}
}
if(fscaf(fd, "%d %d", &image->height, &image->width)!= 2){
fprintf(stderr, "Size of image is invalid");
exit(1);
}
fclose(fd);
return newPPM;
};
showPPM(struct PPM * image){
//two-dimensional array to show the rows and columns of pixels.
int rgb_array[MAX_HEIGHT][MAX_WIDTH];
int i;
int j;
for(i = 0; i<MAX_HEIGHT; i++){
for(j = 0; j<MAX_WIDTH; j++){
printf("%d", rgb_array[i][j]);
}
}
};
the following code
contains lots of comments about what is wrong and/or how to correct
performs appropriate error checking
corrects some function signatures
properly declares a prototype for each function
corrects some of the 'keypunch' errors
corrects the 'syntax' errors
moved the struct definitions to before those definitions are first referenced
Caveat: this code does not correct the problems with accessing the PPM image file.
Caveat: this code still does not pass the malloc'd memory areas to free(), so those statements still need to be added.
#include <stdio.h>
#include <stdlib.h>
#define MAX_HEIGHT 600
#define MAX_WIDTH 400
// 1) giving the struct tag name the same as an instance name is a bad idea
// 2) good programming practice is to define a struct separately
// from the instance of that struct
struct PPM
{
char format[2]; //PPM format code
int height, width; //image pixel height and width
int max; //max rgb colour value
};
struct PPM myPPM;
struct PPM_Pixel
{
//Create variables to hold the rgb pixel values
//int red, green, blue;
// follow the axiom:
// only one statement per line and (at most) one variable declaration per statement
int red;
int green;
int blue;
};
struct PPM_Pixel myPPM_Pixel;
// prototypes
struct PPM *getPPM(FILE * fp);
void showPPM(struct PPM * image);
// this line will cause the compiler to raise two warnings
// about unused variable 'argc' and 'argv'
//int main(int argc, char ** argv)
int main( void )
{
// get image file
//FILE *file;
//file = fopen("aab(1).ppm", "r");
//if there is no file then return an error
//if(file == NULL)
FILE *fp == NULL;
if( NUL == (fp = fopen("aab(1).ppm", "r") ) )
{
//fprintf(stderr, "File is not valid");
// should include system error message
perror( "fopen for read of aab(1).ppm failed" );
//return 0;
// need to indicate an error occurred
exit( EXIT_FAILURE );
}
// implied else, fopen was successful
// following statement will raise a compiler warning
// about: 'set but not used variable'
struct PPM *newPPM = getPPM(fp);
fclose( fp ); // moved to here from 'getPPM()'
} // end function: main
struct PPM *getPPM(FILE * fp)
{
struct PPM *image;
char buffer[16];
//char c;
// the returned value from 'getc()' is an integer, not a character
int c;
char commentArray[256];
// following line is nonsence and the passed in parameter is already open
//fd = fopen(fd, "r");
// when allocating memory, always check the returned value
// to assure the operation was successful
struct PPM *newPPM = NULL;
if( NULL == (newPPM = malloc( sizeof struct PPM ) ) )
{
perror( "malloc for struct PPM failed" );
exit( EXIT_FAILURE );
}
// implied else, malloc successful
// already done in main()
//checks if the file being read is valid/the correct file
//if(fd == NULL){
// exit(1);
//}
//read the image of the format
if(!fgets(buffer, sizeOf(buffer), fd)){
exit(1);
}
//checks the format of the ppm file is correct
if(buffer[0] != 'P' || buffer[1] != '3'){
fprintf(stderr, "Invalid image format! \n");
exit( EXIT_FAILURE );
}
// implied else, file format correct
printf("%s", newPPM->format);
//allocate memory for the image
// note: there is no item named 'PPM'
// image = malloc(sizeof(PPM));
//if(!image){
// best to keep things together
if( NULL == (image = malloc( sizeof struct PPM ) ) )
{
//fprintf(stderr, "Cannot allocate memory");
// should include the OS message for the failure
perror( "malloc for struct PPM failed" );
exit( EXIT_FAILURE );
}
// implied else, malloc successful
// the following code does not store a comment in a linked list
// and
// skips over 1/2 the characters in the line due to the double call
// to 'getc()' in the inner while loop
//checks whether the next character is a comment and store it in a linked list
//c = getc(fd);
//while(c == '#'){
// while(getc(fd) != '\n'){
// c = getc(fd);
// }
//}
// this line does not compile! did you actually mean to call: 'fscanf()'?
//if(fscaf(fd, "%d %d", &image->height, &image->width)!= 2)
if( 2 == fscanf( fp, "%d %d", &image->height, &image->width) )
{
// next line NOT true, what actually happened is the call to 'fscanf()' failed
//fprintf(stderr, "Size of image is invalid");
perror( "fscanf for image height and width failed" );
exit( EXIT_FAILURE );
}
// implied else, fscanf successful
// best practice to close a file, where it was opened.
//fclose(fd);
return newPPM;
//}; do not follow a function definition with a semicolon
}
// Note: the following function is never called
// showPPM(struct PPM * image) each function signature needs a return type, even if 'void'
void showPPM(struct PPM * image)
{
// Note: PPM images have the number of pixels in each row rounded up to a multiple of 4
// but this posted code fails to handle that
//two-dimensional array to show the rows and columns of pixels.
// bad idea, suggest passing the (already input) width and height
// and use those passed in values to define the array sizes
int rgb_array[MAX_HEIGHT][MAX_WIDTH];
int i;
int j;
for(i = 0; i<MAX_HEIGHT; i++)
{
for(j = 0; j<MAX_WIDTH; j++)
{
printf("%d", rgb_array[i][j]);
}
}
// }; do not follow a function definition with a semicolon
}

Unsigned Char is (null) after reading the binary file into the same

I have to open/read a file which is an ascii art file (image)
and requires me to return the width and height of the image to the main routine and then requires me to pass the picture data back via a pointer. Here is the function prototype that I MUST use:
unsigned char *foo(char *filename, int *width, int *height)
Inside of foo, I must use a dynamic array of chars
to store tha image data. I need to use fread() to read
that data. I must also account for the carriage return at the end of each line.
Once I have opened and read the data, pass it back to the main routine. The main routine must then create a dynamic 2D array to store the image, copy the 1D array
into the 2D array, and display the image on the screen.
Image File Name: data.txt
My Code:
#include <stdio.h>
#include <stdlib.h>
void readDimension(FILE *inFile, int *width, int *height)
{
int i;
for (i = 0; i < 2; i++)
{
if (i == 0)
{
fscanf(inFile, "%d", width);
}
if (i == 1)
{
fscanf(inFile, "%d", height);
}
}
}
unsigned char *foo(char *filename, int *width, int *height)
{
FILE *inFile = fopen(filename, "rb");
readDimension(inFile, width, height);
unsigned char *ret = malloc(*width * *height);
fread(ret, 1, *width * *height, inFile);
fclose(inFile);
return ret;
}
int main(int argc, char** argv)
{
FILE *inFile;
int width, height;
unsigned char art;
if (argc == 1)
{
printf("Please specify a file name.\n");
}
else if (argc == 2)
{
inFile = fopen(argv[1], "rb");
if (inFile != NULL)
{
fclose(inFile);
art = foo(argv[1], &width, &height);
int n = sizeof(art);
printf("Data in Array: \\%c \n", art);
printf("Size of Array: %d \n", n);
}
else
{
printf("Error: File Not Found %s", argv[1]);
}
}
printf("Width: %d\n", width); // Testing
printf("Height: %d\n", height); // Testing
}
The problem is that you are trying to get the size of art, which is a pointer. It has fixed size. Size should be computed as width*height:
printf("Size of Array: %d x %d (%d)\n", width, height, width*height);
You declared art as an unsigned char, but you are assigning it a pointer to unsigned char. This is incorrect: art should be declared with an asterisk.
You are also printing a single character %c while passing an array art. This is not going to print anything of interest. If you wish to print the first character, print art[0]. If you wish to print the entire picture, make a pair of nested loops that iterate over the width and height, print each character, and print '\n' after each line.
Here are a few additional notes:
You should not read the entire file in one go with fread, because it would introduce \ns into the data. Instead, you should read the file line by line.
You can read width and height in the same function that reads the data
You do not need a loop to read two integers. You can do it in a single line, like this: fscanf(inFile, "%d %d", width, height);

Why am I getting "Segmentation Fault" when I run my program?

My program decodes an image that is covered by random pixels, to decode the image, I have to multiply each pixel's red color component by 10. The green and blue color components are the same values as the new red component. I've created multiple helper functions, to make the code easier to read in main, but when I try to run my a.out, I keep getting "Segmentation Fault". I can't seem to find my mistakes! Help is appreciated.
void check_argument(int arg_list)
{
if (arg_list < 2)
{
perror("usage: a.out <input file>\n");
}
}
void print_pixel(int a, FILE *out)
{
int r, g, b;
r = a * 10;
if (r > 255)
{
r = 255;
}
g = r;
b = r;
fprintf(out, "%d\n", r);
fprintf(out, "%d\n", g);
fprintf(out, "%d\n", b);
}
void read_header(FILE *in)
{
char str[20];
for (int i = 0; i < 3; i++)
{
fgets(str, 20, in);
}
}
FILE* open_files(FILE *infile, char *input[])
{
infile = fopen(input[1], "r");
if (infile == NULL)
{
perror("Error: Cannot read file.\n");
}
return infile;
}
void decode(int arg_list, char *in[])
{
FILE *input, *output;
int check, red, green, blue;
open_files(input, in);
output = fopen("hidden.ppm", "w");
fprintf(output, "P3\n");
fprintf(output, "%d %d\n", 500, 375);
fprintf(output, "255\n");
read_header(input);
check = fscanf(input, "%d %d %d", &red, &green, &blue);
while (check != EOF)
{
print_pixel(red, output);
check = fscanf(input, "%d %d %d", &red, &green, &blue);
}
fclose(input);
fclose(output);
}
int main(int argc, char *argv[])
{
check_argument(argc);
decode(argc, argv);
}
After calling open_files(input, in); you will not have the file handle in input.
As this is supposed to be homework, I'll try to show you the some common source of bugs and how to find them.
Variables which are used must (should) be assigned to before that. This counts especially for pointers, e. g. FILE *.
If a function (e. g. fopen()) fails, it normally indicates this by returning a special value which must be checked before continuing.
To check which value a variable has, you can use printf() to show it.
This is for finding principal errors such as segfaults.
But logical errors are hard to find as well: if you read 3 values and store them into variables, it might be more useful to use them all instead of only one of them. (But maybe this one is not yet the goal of this exercise.)
I wrote the lines before this before I learned that it is not the task to search for bugs in a given program, but to write a program by yourself, so I'll get a little more concrete by now.
A FILE * is something returned by fopen(). You can return it or you can write it to a variable or another memory location indirectly pointed to by a pointer "one level deeper".
So you should rewrite your open_files() (BTW: why file*s*? It's currently only one...):
either for returning the value (preferrable):
FILE* open_files(char *input[])
{
FILE *infile = fopen(input[1], "r");
if (infile == NULL)
{
perror("Error: Cannot read file.\n");
}
return infile;
}
and call it with
input = open_files(input);
or with "pass by reference":
void open_files(FILE **infile, char *input[])
{
*infile = fopen(input[1], "r");
if (*infile == NULL)
{
perror("Error: Cannot read file.\n");
}
return *infile;
}
and call it with
open_files(&input, in);
Only doing that you'll have your variable input at the caller's site really written to.

Resources