在C中将P2 .pgm图像读入2D数组

时间:2016-06-19 17:24:39

标签: c arrays

我已经开始学习图像处理了,我正在尝试将.pgm文件读入C中的2D数组。我正在测试输入和输出,但是程序没有正常工作所以,我试着做一些改动。我怎样才能改进/修改代码以使其有效?

#include<stdio.h>
#include<stdlib.h>

struct PGMstructure 
{
    int maxVal;
    int width;
    int height;
    int data[800][800];
};


int main()
{
    FILE *imagein,*imageout;

    int row, col;

    int i,j;
    int ch_int;
    struct PGMstructure *imginfo;   
    char infpath[500],outfpath[500];

    printf("Enter PGM file path:");
    scanf("%s",infpath);
    imagein = fopen(infpath,"r+");

    if(imagein == NULL)
    {
        printf("Error opening first file");
        exit(8);
    }

    while(getc(imagein) != '\n');             

    while (getc(imagein) == '#')              
    {
        while (getc(imagein) != '\n');          
    }

    fseek(imagein, -1, SEEK_CUR);
    fscanf(imagein,"%d", &imginfo->width);
    fscanf(imagein,"%d", &imginfo->height);
    fscanf(imagein,"%d", &imginfo->maxVal);
    printf("\n width  = %d\n",imginfo->width);
    printf("\n height = %d\n",imginfo->height);
    printf("\n maxVal = %d\n",imginfo->maxVal);

    for (row=0; row<imginfo->height; row++){

        for (col=0; col < imginfo->width; col++)
        {    
            fscanf(imagein,"%d", &ch_int);
            imginfo->data[row][col] = ch_int;
        }
    }

    printf("Enter path of output file:");

    scanf("%s",outfpath);
    imageout = fopen(outfpath,"w+");


    for ( i = 0 ; i < row ; i++ )
    {
        for ( j = 0 ; j < col ; j++ )
        {
            fprintf( imageout,"%d" , imginfo->data[row][col] );
        }
        printf( "\n" ) ;
    }

    return 0;
}

2 个答案:

答案 0 :(得分:1)

您的代码似乎没问题,唯一的问题是您只是创建指针,而不是将其引用到内存中的实际结构。

您可以通过添加以下代码行来完成此操作,所有内容都应按预期工作。

imginfo = malloc(sizeof(struct PGMstructure));

答案 1 :(得分:1)

您的代码的主要问题是MiteshNinja强调的问题,即您忘记为您的结构分配空间。
当您不再需要结构时,请记住使用函数malloc释放您使用free分配的内存。这对于避免内存泄漏非常重要!

我认为值得指出的另一件事是,无论何时你完成一个文件,你都应该记得用fclose关闭它。

然后,您可以在下面的代码中找到许多琐碎的问题。

最后,也许this post可能会有所帮助。

#include<stdio.h>
#include<stdlib.h>

struct PGMstructure
{
    int maxVal;
    int width;
    int height;
    int data[800][800];
};


int main()
{
    FILE *imagein,*imageout;

    int row, col;

    int i,j;
    int ch_int;
//--- CHANGED ------ Start
    struct PGMstructure *imginfo = malloc(sizeof(struct PGMstructure));
//--- CHANGED ------ End
    char infpath[500],outfpath[500];

    printf("Enter PGM file path:");
    scanf("%s",infpath);
    imagein = fopen(infpath,"r+");

    if(imagein == NULL)
    {
        printf("Error opening first file");
        exit(8);
    }

//--- CHANGED ------ Start
    while(getc(imagein) != '\n');           // Ignore the first line in the input file

    if (getc(imagein) == '#' )              // If it is the case, ignore the second line in the input file
        {
        while(getc(imagein) != '\n');
        }
    else
        {
        fseek(imagein, -1, SEEK_CUR);
        }
//--- CHANGED ------ End

    fscanf(imagein,"%d", &imginfo->width);
    fscanf(imagein,"%d", &imginfo->height);
    fscanf(imagein,"%d", &imginfo->maxVal);
    printf("\n width  = %d\n",imginfo->width);
    printf("\n height = %d\n",imginfo->height);
    printf("\n maxVal = %d\n",imginfo->maxVal);

    for (row=0; row<imginfo->height; row++){

        for (col=0; col < imginfo->width; col++)
        {
            fscanf(imagein,"%d", &ch_int);
            imginfo->data[row][col] = ch_int;
        }
    }

//--- CHANGED ------ Start
    fclose(imagein);
//--- CHANGED ------ End

    printf("Enter path of output file:");

    scanf("%s",outfpath);
    imageout = fopen(outfpath,"w+");

//--- CHANGED ------ Start
    for ( i = 0 ; i < imginfo->height ; i++ )
    {
        for ( j = 0 ; j < imginfo->width ; j++ )
        {
            fprintf( imageout,"%d  " , imginfo->data[i][j] );
        }
            fprintf( imageout,"\n" );
    }

    fclose(imageout);
    free(imginfo);
//--- CHANGED ------ End

    return 0;
}