2017-02-17 119 views
1

P6读PPM格式图像

P6表示它是一个PPM图像。接下来的两个字段是图像的宽度和高度。最后一个字段给出最大像素值。在头部的末尾是\ n,然后是二进制像素数据。图像是彩色的,所以有三个字节(红色,绿色,蓝色)。 readPPM函数的目标是将像素数据返回到无符号字符的一维数组中,再加上图像的宽度和高度。我的writePPM函数(我还没有为这个函数做任何事情)的目标是从我的readPPM函数返回的给定信息中将PPM格式图像写入一个空文件。我仍然不确定如何使这个程序工作,所以我不存储650宽度和652高度。一旦我真的可以读写文本文件,我会担心这一点。 (我知道,因为我试图打印他们的值,他们出来右)成功地存储头(P6,650,652和255))在他们的右变量。我的输出如下所示。我想知道那些低于最大值的无意义字符是什么。那些是二进制像素数据吗?如果是这样,那么我只需要写我的writePPM函数。

输出: PSix:P6

宽度:650

高度:652

最大:??????255

ɡƞɢˤɢS' G·Ǡ?ʤ?ɣ?ɡ?ɡ?ǟ·G·S'ƞ?ȡ?ʣ?ʣ?ȡ?s?s?Ȟ?ʠ?起赔?ƞ2 O 3ʤ?ʦ?ʦ?ɧ? ƥ?ǥ?埃?ɩ?ʪ?ʫ?ʫ?ʪ?ʨ?ɤ?Ǡ?ʢ?起赔?ȡ?ȡ?ʥ?ɥ?ʧ·G·ǥ?ƥ·G· ????ƥ????ǥ?ʨ?ɧ?ʨ?ɧ?ɧ?ͨ?ϧ?ʟ?ˠ?Х?Φ?ͥ?ˣ?ǟ12 O?ʦ?ɥ微克??? ?????????? ????? H +ü???????©?Ǯ?????????????改签?˭?ū?????????????? ???????????鴢紣㴢ⳡ޴?ܴᷟ?⺡㻢Ḥ⹥޴?ݳ1&ܲα1خ?׭?ٯ?֮?ѫ?Ъ?̨?մ? յ?Ǫ?ٿ?һ????ú?ɳ?ɳ?ɱ?ɱ?˰?ˮ?ɪ?埃?ͫ?ֵ?ұT&¡?ʩ?Ӳ?Ϯ?ͫ?ϭ ?ѯ?ѯ?Ϭ?̭?˰?Ͷ?͵?˳?ʱ?Ǯ?ȯ?ε?ӷ?ϳ?̯?ɬ?̫?ͬ?ͭ?˪?ΰ?ͮ?ί?Ӵ?ӷ ?ς?ˮ?ɬ?ʭ?Ѵ?ҵ?г?ς?ˮ?ȫ?ɬ?改签?ρ?в?˭?ʬ?˭?̭?ί?ͮ?ͮ?ͮ?ί?ˬ?ȩ ?ʫ?ͮ?ղ?ү?Ӳ?ֵ?Ҵ?ѳ?ς?ˮ?ȭ?˰?ˮ?ˮ?ͯ?̮?̬?ͯ?ˮ?˲?ȯ?Ƭ?ü?ü ?®?¯?????????????????????????????????????¨?é?é?ū?¨ ???????????????????????????????ī?ī?ī?ü?ú?ī?ū?ū?é? ?????我?ú?®???????????©??????????ȫ?ȫ?Ȫ?ɫ?̭?̭?˭?ʬ?ɫ? ʬ?ʮ?˯?˯?˯?˯?Ȭ?Ȫ?ɫ?̮?ͯ?ρ?ѳ?ӵ?Ѵ?б?κ?ϭ?ά?Ѯ?Ա?Ӱ?Э?Ϭ?ү? ү?Э?ά?ѯ?וי?ӭ?ϩ?ͧ?ϧ?ѩ?Ԭ?ذ?խ?ѩ?Ϧ?Ϧ?Ч?ҩ?ҩ?ҩ?ת?ث?׭?خ?ׯ? ֮?կ?կ?Ԭ?խ?ج?ج?ܬ?߰?㳩䴨峨ݫ→7→ެ?䲧峨䲧䲧Ⱗ䲩붮?س국굯???????걦뱥جمي??? ????????????????????????????????????±???? d ???????? ?????????????????????????????????????????æ?ú?C?é?我北京时间???????£?Ʀ?Ʀ?ƥ?吨???????????????吨ƥɧʨͫάʩ吨牛逼改签ŤƧˬɭ|??????????????????? ?????????????C???????????????????????????????????? ?????????????????????????????????????????????????? ?????????????????????????????ñ?°3 N ??????????î????? ???????????????????鲯豮汭尬䯫汭곮췱洫ᱧద嵫緭䴪᯦᯦䲩洫鴬鴬鴬국국贩춬??????뵩괪??? ????????겱⪭????겳???뵫贩贩趫긭긭긭跩綨깫컭궩糦뵩뷩켨컪鸧綨浧絬鷮燕峬洫???긭跩긭캱캳깲綯㳩ᱧ㳧嵩㵨᳦᳤ჵ乩缬轭轭缬ߴ?0?ߴ?帱

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 
+2

如果你的程序应该是干净的c代码,你可能不会使用'new'。在C中,它是通过'malloc()'(包括'stdlib.h')完成的。 – Scheff

+0

'main'中'pixelArray'的类型是错误的。它必须是'unsigned char * pixelArray'(不含'[size]')。 – Scheff

+0

我已经成功地将标题(P6,650,652和255)存储在它们的正确变量中(我知道是因为我试图打印它们的值,并且它们出来了)。我的输出如下所示。我想知道那些低于最大值的无意义字符是什么。那些是二进制像素数据吗?如果是这样,那么我只需要写我的writePPM函数。 – asilvester635

回答

0
//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; 
} 
+0

我已经成功地将头部(P6,650,652和255))存储在它们的右变量中(我知道是因为我试过印刷他们的价值观,他们出来了)。我的输出如下所示。我想知道那些低于最大值的无意义字符是什么。那些是二进制像素数据吗?如果是这样,那么我只需要写我的writePPM函数。我也更新了我的代码。 – asilvester635

0

您是读取字符到正确的位置。pixels已经是一个指向char如此的,你应该把它当作是fscanf

fscanf(fr, "%c", pixels) 

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

+0

我已经成功地将头部(P6,650,652和255)存储在它们的右变量中(我知道是因为我试图打印它们的值,并且它们出来了)。我的输出如下所示。我想知道那些低于最大值的无意义字符是什么。那些是二进制像素数据吗?如果是这样,那么我只需要写我的writePPM函数。我也更新了我的代码。 – asilvester635

+0

当您找到问题的答案时,您应该通过选择正确的答案来解决问题 - 如果需要,可以自己发布 - 并关闭问题。 *不*改变问题。 – kmkaplan