我正在使用文件pgm.c和pgm.h來讀取PGM圖像文件,然後我的程序將操縱文件(灰度,旋轉等)。在C中讀取PGM圖像(沒有正確讀取文件)
但是,我不能得到的文件正確讀取。當我使用:
PGMImage* img = (PGMImage*)malloc(sizeof(PGMImage));
char iName1[256];
printf("Enter the filename of your PGM image:\n");
scanf("%s", iName1);
getPGMfile(iName1, &img);
控制檯返回圖像的類型,寬度,高度和最大值。但是,寬度永遠不會正確。無論我使用的是我在網上找到的.pgm文件還是我自己創建的一個.pgm文件,它通常都是0(而且它是56的一次)。我甚至試圖對圖像的寬度進行硬編碼,但即使如此,它也會導致程序崩潰。寬度應在pgm.c被讀取時,getPGMfile作用下:
void getPGMfile (char filename[], PGMImage *img)
{
FILE *in_file;
char ch;
int row, col, type;
int ch_int;
in_file = fopen(filename, "r");
if (in_file == NULL)
{
fprintf(stderr, "Error: Unable to open file %s\n\n", filename);
exit(8);
}
printf("\nReading image file: %s\n", filename);
/*determine pgm image type (only type three can be used)*/
ch = getc(in_file);
if(ch != 'P')
{
printf("ERROR(1): Not valid pgm/ppm file type\n");
exit(1);
}
ch = getc(in_file);
/*convert the one digit integer currently represented as a character to
an integer(48 == '0')*/
type = ch - 48;
printf("Type: %d", type);
if((type != 2) && (type != 3) && (type != 5) && (type != 6))
{
printf("ERROR(2): Not valid pgm/ppm file type\n");
exit(1);
}
while(getc(in_file) != '\n'); /* skip to end of line*/
while (getc(in_file) == '#') /* skip comment lines */
{
while (getc(in_file) != '\n'); /* skip to end of comment line */
}
/*there seems to be a difference between color and b/w. This line is needed
by b/w but doesn't effect color reading...*/
fseek(in_file, -1, SEEK_CUR); /* backup one character*/
fscanf(in_file,"%d", &((*img).width));
fscanf(in_file,"%d", &((*img).height));
fscanf(in_file,"%d", &((*img).maxVal));
//I omitted the rest, but it can be seen in the link above
我不知道是什麼導致了這些錯誤。我在Windows 7(64位)上使用Eclipse和MinGW GCC。我的導師不能幫我解決這個問題,所以我希望你能! :)
總結:在PGM圖像的寬度總是讀錯,任何與該文件進行事後導致可執行停止工作。對寬度進行硬編碼還會導致可執行文件停止工作。
getpgm不應該通過指針的地址,而是隻是根據頭文件指針:getPGMfile(iName1,IMG) – Magn3s1um
你是不允許使用的NetPBM的http://netpbm.sourceforge.net/doc/libnetpbm.html –
@ Magn3s1um提供的函數調用:+1,除非它是正確的答案。 – rici