My program that is meant to read and display a PPM image is not printing the actual format or height or width of the image being read. It's probably a really basic mistake, but I've not been using C for very long.
EDIT: I've actually just noticed where I check the image format is valid, I'm checking whether it == 2, but it should be != 2 (correct me if I'm wrong) so it says my image format is invalid either way. I'll try run my code on another image.
If anyone can help that would be great.
Current output:
PPM FILE PROGRAM
Memory allocation successful
File format is correct
89~
Image size: 3680602 544108293
Desired output:
PPM FILE PROGRAM
Memory allocation successful
File format is correct
P3
Image size: 125 100
Code:
#include <stdio.h>
#include <stdlib.h>
#define MAX_HEIGHT 600
#define MAX_WIDTH 400
struct PPM {
char format[3]; //PPM format code
int height, width; //image pixel height and width
int max; //max rgb colour value
};
struct PPM_Pixel {
//Create variables to hold the rgb pixel values
int red;
int green;
int blue;
};
struct PPM *getPPM(FILE * file);
void showPPM(struct PPM * image);
int main( void ){
printf("PPM FILE PROGRAM \n");
FILE *file;
// 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 does not exist\n");
return 0;
}
struct PPM *newPPM = getPPM(file);
showPPM(file);
fclose(file);
}
struct PPM *getPPM(FILE * file){
char buffer[3];
int c;
struct PPM *image = NULL;
if(NULL == (image = malloc(sizeof(struct PPM)))){
perror("memory allocation for PPM file failed\n");
exit(1);
}
else {
printf("Memory allocation succesful\n");
}
//read the image of the format
if(!fgets(buffer, sizeof(buffer), file)){
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("File format is correct\n");
printf("%s\n", image->format);
}
//checks whether the next character is a comment and skips it
c = getc(file);
while(c == '#'){
while(getc(file) != '\n'){
c = getc(file);
}
}
//check the image size is valid
if(fscanf(file, "%d %d", &image->height, &image->width) == 2){
printf("Invalid imaze size\n");
exit(1);
}else{
printf("Image size: %d %d ", image->height, image->width);
}
return image;
}
void showPPM(struct PPM * image){
struct PPM_Pixel rgb_array[MAX_HEIGHT][MAX_WIDTH];
int i;
int j;
for(i = 0; i<MAX_HEIGHT; i++){
for(j = 0; j<MAX_WIDTH; j++){
struct PPM_Pixel newPPM_Pixel;
if(fscanf(image, "%d %d %d", &newPPM_Pixel.red, &newPPM_Pixel.green, &newPPM_Pixel.blue) == 3){
rgb_array[i][j] = newPPM_Pixel;
}
}
}
}
Apologies, I'm not sure how to change the output text to just text.
Although you forget to include the code of showPPM()
, his prototype is
void showPPM(struct PPM * image);
and you are passing a FILE *
:
FILE *file;
...
showPPM(file);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.