我对如何在 main.c 中调用 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;
}
}
}
}
最佳答案
调用 getPPM()
后立即调用 showPPM()
建议传递文件指针和指向 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 字段
- 循环直到文件结束
- 读取三个值,可能使用
scanf()
- 打印三个值,可能还带有行/列指示
- 转到2
注意; “魔数(Magic Number)”有两个可能的值:P3
和 P6
为了您的目的,请检查两个值,而不仅仅是一个值。
注意:P6
格式的值随着 Maxval
字段为满刻度而缩放。您可能想也可能不想考虑这一点
注意:如果使用 P6
格式,则可能需要使用 height*width
的限制来限制显示的像素数,而不是查找 EOF。
关于在 C 中调用 showPPM 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35353149/