首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >读取PPM格式图像

读取PPM格式图像
EN

Stack Overflow用户
提问于 2017-02-17 05:55:51
回答 2查看 5.4K关注 0票数 1

P6

六百五十六五二

255

P6表示它是一个PPM映像。下两个字段是图像的宽度和高度。最后一个字段给出了最大像素值。在标题的末尾是一个\n,然后是二进制像素数据。图像是彩色的,因此有三个字节(红色、绿色、蓝色)。我的readPPM函数的目标是返回一维无符号字符数组中的像素数据,再加上图像的宽度和高度。我的writePPM函数(我还没有为这个函数做任何事情)的目标是从我的readPPM函数返回的给定信息中将PPM格式映像写入一个空文件。我仍然不知道如何使这个程序工作,所以我不存储650的宽度和652的高度。一旦我真的能够读写文本文件,我就会担心这个问题。

更新:我已经成功地将标题(P6、650、652和255)存储在它们的正确变量中(我知道,因为我试图打印它们的值,它们得到了正确的结果)。我的输出如下所示。我想知道那些低于最大值的无稽之谈是什么。这些是二进制像素数据吗?如果是这样,那么我只需要编写我的writePPM函数。

输出: PSix: P6

宽度: 650

身高: 652

最高限额: 255

?ɡ?Ƞ?ɢ?ˤ?ɢ?Ş?ĝ?Ǡ?ʤ?ɣ?ɡ?ɡ?ǟ?Ĝ?ŝ?Ƞ?ȡ?ʣ?ʣ?ȡ?Ŝ?Ŝ?Ȟ?ʠ?̤?ƞ?Ơ?ʤ?ʦ?ʦ?ɧ?Ƥ?ǥ?Ȧ?ɩ?ʪ?ʫ?ʫ?ʪ?ʨ?ɤ?Ǡ?ʢ?̤?ȡ?ȡ?ʥ?ɥ?ʧ?Ģ?ǥ?Ƥ?Ģ?Ƥ????ǥ?ʨ?ɧ?ʨ?ɧ?ɧ?ͨ?ϧ?ʟ?ˠ?Х?Φ?ͥ?ˣ?ǟ??Ơ?ʦ?ɥ?Ġ??????????????????ħ?Ũ???????©?Ǯ?????????????Ǩ?˭?ū?????????????????????????鴢紣㴢ⳡ޴?ܴᷟ?⺡㻢Ḥ⹥޴?ݳ?۱?ܲ?۱?خ?׭?ٯ?֮?ѫ?Ъ?̨?մ?յ?Ǫ?̲?ٿ?һ????ư?ɳ?ɳ?ɱ?ɱ?˰?ˮ?ɪ?Ȧ?ͫ?ֵ?ұ?Ť?¡?ʩ?Ӳ?Ϯ?ͫ?ϭ?ѯ?ѯ?Ϭ?̭?˰?Ͷ?͵?˳?ʱ?Ǯ?ȯ?ε?ӷ?ϳ?̯?ɬ?̫?ͬ?ͭ?˪?ΰ?ͮ?ί?Ӵ?Ӷ?ϲ?ˮ?ɬ?ʭ?Ѵ?ҵ?г?ϲ?ˮ?ȫ?ɬ?ǩ?ϱ?в?˭?ʬ?˭?̭?ί?ͮ?ͮ?ͮ?ί?ˬ?ȩ?ʫ?ͮ?ղ?ү?Ӳ?ֵ?Ҵ?ѳ?ϲ?ˮ?ȭ?˰?ˮ?ˮ?ͯ?̮?̬?ͯ?ˮ?̱?˲?ȯ?ƭ?Ư?Ů?®?¯?????????????????????????????????????¨?é?é?ū?¨???????????????????????????????ī?ī?ī?Ŭ?Ŭ?ī?ū?ū?é???????ĭ?Ů?®???????????©??????????ȫ?ȫ?Ȫ?ɫ?̭?̭?˭?ʬ?ɫ?ʬ?ʮ?˯?˯?˯?˯?Ȭ?Ȫ?ɫ?̮?ͯ?ϱ?ѳ?ӵ?Ѵ?б?ϰ?ϭ?ά?Ѯ?Ա?Ӱ?Э?Ϭ?ү?ү?Э?ά?ѯ?ױ?ӭ?ϩ?ͧ?ϧ?ѩ?Ԭ?ذ?խ?ѩ?Ϧ?Ϧ?Ч?ҩ?ҩ?ҩ?ת?ث?׭?خ?ׯ?֮?կ?կ?Ԭ?խ?ج?ج?ܬ?߰?㳩䴨峨ݫ?٧?ެ?䲧峨䲧䲧Ⱗ䲩붮?ﺲ국굯?걦뱥ﶥ???????????????????????????????????????±????ð?????????????????????????????????????????????????æ?ũ?ç?é?Ī?ë???????£?Ʀ?Ʀ?ƥ?Ť?Ť?ƥ?ɧ?ʨ?ͫ?ά?ʩ?Ť?â?ţ?Ǩ?ť?Ƨ?ˬ?ɭ?¦???????????????????????????????ç???????????????????????????????????????????????????????????????????????????????????????????????????????????????????ñ?°?ñ??????????î????????????????????????鲯豮汭尬䯫汭곮췱洫ᱧద嵫緭䴪᯦᯦䲩洫鴬鴬鴬국국贩춬?뵩괪???????????겱⪭?겳?뵫贩贩趫긭긭긭跩綨깫컭궩糦뵩뷩켨컪鸧綨浧絬鷮鷰峬洫???긭跩긭캱캳깲綯㳩ᱧ㳧嵩㵨᳦᳤ⴥ乩缬轭轭缬ߴ?·?ߴ?幬

main.cc

代码语言:javascript
复制
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

代码语言:javascript
复制
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
EN

回答 2

Stack Overflow用户

发布于 2017-02-17 07:15:09

代码语言:javascript
复制
//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;
}
票数 2
EN

Stack Overflow用户

发布于 2017-02-17 07:26:11

您没有将字符读入正确的位置。pixels已经是指向char的指针,所以您应该像传递给fscanf一样传递它。

代码语言:javascript
复制
fscanf(fr, "%c", pixels)

当然还有其他的问题,但是我这应该让你通过这个路障。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42290419

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档