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
如果你的程序應該是乾淨的c代碼,你可能不會使用'new'。在C中,它是通過'malloc()'(包括'stdlib.h')完成的。 – Scheff
'main'中'pixelArray'的類型是錯誤的。它必須是'unsigned char * pixelArray'(不含'[size]')。 – Scheff
我已經成功地將標題(P6,650,652和255)存儲在它們的正確變量中(我知道是因爲我試圖打印它們的值,並且它們出來了)。我的輸出如下所示。我想知道那些低於最大值的無意義字符是什麼。那些是二進制像素數據嗎?如果是這樣,那麼我只需要寫我的writePPM函數。 – asilvester635