[英]PPM Image format, height and width not displaying - C
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. 我打算读取和显示PPM图像的程序未打印要读取的图像的实际格式,高度或宽度。 It's probably a really basic mistake, but I've not been using C for very long. 这可能是一个非常基本的错误,但是我已经很长时间没有使用C了。
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. 编辑:我实际上只是注意到我在哪里检查图像格式是否有效,我正在检查它是否== 2,但是应该是!= 2(如果我输入错了,请更正我),所以它说我的图像格式是无效。 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 尽管您忘记了包含showPPM()
的代码, showPPM()
他的原型是
void showPPM(struct PPM * image);
and you are passing a FILE *
: 并且您正在传递FILE *
:
FILE *file;
...
showPPM(file);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.