我对如何在main中调用showPPM函数感到困惑。如果有人可以帮助我,那就太好了!
该程序的目的是读取图像文件并显示像素rgb值。因此,我不会打乱这篇文章,我排除了getPPM函数,但是是否需要询问。
#include <stdio.h>
#include <stdlib.h>
#define MAX_HEIGHT 600
#define MAX_WIDTH 400
#define RGB_COMPONENT_COLOUR
struct PPM {
char format[4]; //PPM format code
int height;
int 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 ){
FILE *file;
// get image file
file = fopen("aab(1).ppm", "r");
//check if a file exists
if(file == NULL){
fprintf(stderr, "File does not exist\n");
return 0;
}
struct PPM *newPPM = getPPM(file);
fclose(file);
}
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;
}
}
}
}
最佳答案
呼叫showPPM()
后立即呼叫getPPM()
建议传递文件指针和指向PPM结构的指针。
来自:http://netpbm.sourceforge.net/doc/ppm.html
每个PPM映像均包含以下内容:
A two char "magic number" for identifying the file type.
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.
给定以上格式信息,编写代码以显示像素值相对简单。
读取/丢弃Maxval字段
循环直到EOF
读取三个值,可能使用
scanf()
打印三个值,可能还带有行/列指示
转到2
注意; “魔术”数字有两个可能的值:
P3
和P6
为了您的目的,请检查两个值,而不只是一个。
注意:
P6
格式具有按比例缩放的值,而Maxval
字段为完整比例。您可能想要也可能不想考虑注意:如果使用
P6
格式,则可能要使用height*width
的限制来限制显示的像素数,而不是寻找EOF。