P6
650 652
255
P6表示是PPM图片。接下来的两个字段是图像的宽度和高度。最后一个字段给出最大像素值。 header 的末尾是一个\n,然后是二进制像素数据。图像是彩色的,所以有三个字节(红、绿、蓝)。我的 readPPM 函数的目标是返回一维无符号字符数组中的像素数据,以及图像的宽度和高度。我的 writePPM 函数(我还没有为该函数做任何事情)的目标是根据我的 readPPM 函数返回的给定信息将 PPM 格式图像写入一个空文件。我仍然不确定如何让这个程序工作,所以我不存储宽度为 650 和高度为 652 的数据。一旦我真正可以读写文本文件,我就会担心这个问题。
更新:我已经成功地将 header (P6、650、652 和 255)存储在它们正确的变量中(我知道是因为我尝试打印它们的值并且结果是正确的)。我的输出如下所示。我想知道那些低于最大值的无意义字符是什么。那些是二进制像素数据吗?如果是这样,那么我只需要编写我的 writePPM 函数。
输出: P6: P6
宽度:650
高度:652
最大:255
?ɡ????????????????? ? ʧ?Ģ?ǥ?Ƥ?Ģ? ??? ?????????? ??????ħ?Ũ????????©?Ǯ??????????????Ǩ?˭?ū???????????????? ???????? ize? ?ѯ?ѯ?Ϭ?̭?˰?Ͷ?͵?˳?ʱ?Ǯ?Ş?ε?ӷ??̯?ɬ?̫?ͬ?ͭ?˪?‰?ͮ?ː?Ӵ?Ӷ ? ?ʫ?ͮ?ղ?ͯ?Ӳ?ֵ?ʹ?ь?ϲ?ˮ?₩?˰?ˮ?ˮ?ͯ?̮?̬?ͯ?ˮ?̱?˲?Ş?ƭ?Ư?Ů ®?¯???????????????????????????? ??????????????????????? ??????ĭ?Ů?®????????????©????????????? ʬ?ʮ?˯?˯?˯?˯?₩?₩?ɫ?̮?ͯ?ϱ?ь?ӵ?Ѵ?б?ϰ?ϭ?♬?Ѯ?у?Ӱ?Э?Ϭ?ͯ? ¡ ? ???????????????????????????????????? ?????????????????????????????? ?ë????????£?Ʀ?Ʀ?ƥ?Ť?Ť?ƥ?ɧ?ʨ?ͫ?ì?ʩ?Ť?â?ţ?Ǩ?ť?Ƨ?ˬ?ɭ?¦ ???????????????????????????????C?????????????????? ???????????????????????????????????????? ??????????????????????????????????????° ?ñ??????????????????????????????????????憨憨憨䯽汭곮췱洫ᱧద赫緭䴪᯦᯦朴洫鴬鴬鴬ツ긭긭踩踏깫컭궩糦뵩뷩켨컪鸧絧絧鷮燕彬彬???긭踩긭캱캳깲綯㳩ᱧ㳧ㄩㄥ᳦᳤ⴥ缱缱罼轼??
main.cc
int main() {
char fileName[50] = "binary_pixels.txt";
int width = 0; // width of the image
int height = 0; // heigt of the image
int size = 128; // size of the array
// read the PPM file and store its contents inside an array and return
// the pointer to that array to pixelArray
unsigned char* pixelArray[size] = readPPM(fileName, &width, &height);
// print the contents of the array
for (int i = 0; i < 128; i++) {
printf("%s\n", pixelArray[i]);
} // end of for loop
} // end of main
readPPM.cc
unsigned char readPPM(const char* fileName, char* pSix, int* width,
int* height, int* maximum) {
// open the file to read just the header reading
FILE* fr = fopen(fileName, "r");
// formatted read of header
fscanf(fr, "%s", pSix);
// check to see if it's a PPM image file
if (strncmp(pSix, "p6" , 10) != 0) {
printf("They are not the same\n");
} else {
printf("They are the same\n");
}
// read the rest of header
fscanf(fr, "%d\n %d\n", width, height);
fscanf(fr, "%d\n", maximum);
// check to see if they were stored properly
printf("PSix: %s\n", pSix);
printf("Width: %d\n", *width);
printf("Height: %d\n", *height);
printf("maximum: %d\n", *maximum);
// allocate array for pixels
unsigned char* pixels = new unsigned char[width * height];
// unformatted read of binary pixel data
while (fread(pixels, sizeof(pixel), 128, fr)) {
printf("%s", pixels);
} // end of for loop
// close file
fclose(fr);
// return the array
return pixels;
} // end of readPPM
最佳答案
//struct to hold a pixel.
struct rgb
{
char r;
char g;
char b;
};
//struct to hold the image and its info.
struct image
{
char p;
int format;
int width;
int height;
int intensity;
//struct rgb **rgb;
unsigned char *pixels;
};
int main(void)
{
struct image m;
FILE *fp = //fopen(...);
fscanf(fp, "%c%d\n", &m.p, &m.format);
fscanf(fp, "%d %d\n", &m.width, &m.height);
fscanf(fp, "%d\n", &m.intensity);
printf("%c%d\n", m.p, m.format);
printf("%d %d\n", m.width, m.height);
printf("%d\n", m.intensity);
//allocate array to hold the pixels
m.pixel = (unsigned char*)malloc(m.width*m.heigth*3*sizeof(unsigned char));
//read pixels into m.pixel
return 0;
}
关于c - 读取 PPM 格式图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42290419/