简体   繁体   English

C 中的 PGM 文件处理

[英]PGM File Processing in C

New to C, I'm trying to read an input PGM file that's either P2, P5, or P6 along with an output file from the command prompt, then read the data to a buffer, and finally write it to the output file. C 的新手,我正在尝试从命令提示符读取 P2、P5 或 P6 以及 output 文件的输入 PGM 文件,然后将数据读取到缓冲区,最后将其写入 Z148E6221F3983 文件。 Everything is compiling but when I view the output file only P2 looks like the original file.一切都在编译,但是当我查看 output 文件时,只有 P2 看起来像原始文件。 P5 and P6 look like a rainbow mess. P5 和 P6 看起来像一团乱七八糟的东西。 I'm including my code.我包括我的代码。

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

unsigned char* getPGMfile(char filename[], int *lwidth, int *lheight, int *lchannel);
int save(char filename[], unsigned char*data, int lwidth, int lheight, int lchannel);

int main(int argc, char **argv){
    int lwidth, lheight, lchannel;
    getPGMfile(argv[1], &lwidth, &lheight, &lchannel);
    unsigned char* data=(getPGMfile(argv[1], &lwidth, &lheight, &lchannel));
    printf("width, height, channel: %d %d %d\n",lwidth,lheight,lchannel);
    save(argv[2], data, lwidth, lheight, lchannel);
    return 0;
}

unsigned char* getPGMfile (char filename[], int *lwidth, int *lheight, int *lchannel){
        FILE *in_file;
        char ch;
        int row, col, type;
        int ch_int;
        in_file = fopen(filename, "r");
        if (in_file == NULL){
                fprintf(stderr, "Unable to open file %s\n\n", filename);
                exit(8);
        }
        printf("\nReading image file: %s\n", filename);
        ch = getc(in_file);
        if(ch != 'P'){
                printf("Not valid pgm/ppm file type\n");
                exit(1);
        }
        ch = getc(in_file);
        type = ch - 48;
        if((type != 2) && (type != 5) && (type != 6)){
                printf("Not valid pgm/ppm file type\n");
                exit(1);
        }
        while(getc(in_file) != '\n');
    while (getc(in_file) == '#'){
                while (getc(in_file) != '\n');
        }
        fseek(in_file, -1, SEEK_CUR);
    int width, height;
        fscanf(in_file,"%d%d", &width, &height);
    printf("width and heigth: %d %d\n", width, height);
    int intensity;
    fscanf(in_file,"%d", &intensity);
    printf("intensity: %d\n", intensity);

        unsigned char *data; 
        if(type == 2){
        *lchannel=5;
        int k=0;
            data = (unsigned char *) malloc((width*height)*sizeof(unsigned char));
                for (row=height-1; row >=0; row--)
                for (col=0; col<width; col++){
                        fscanf(in_file,"%d", &ch_int);
            data[k]=ch_int;k++;
 }
        }
        else if(type == 5){
        *lchannel=6;
                data=(unsigned char *) malloc(width*height*sizeof(unsigned char));
        fread(data, 1, (width*height), in_file);
        }
    else if(type == 6){
        *lchannel=6;
                data=(unsigned char *) malloc(3*(width*height)*sizeof(unsigned char));
        fread(data, 1, (3*(width*height)), in_file);
        }
        fclose(in_file);
        printf("\nDone reading file\n");
    *lwidth=width;
    *lheight=height;
    return data;
}

int save(char filename[],unsigned char *data, int lwidth, int lheight, int lchannel){
        FILE *fp;
    fp = fopen(filename, "wb");
    const int dimx = 800, dimy = 800;
    int i,j;
        if(lchannel == 5){
                fprintf(fp,"P5\n%u %u\n255\n", lwidth, lheight);
        fwrite(data,1,(lwidth*lheight),fp);
        printf("File %s saved.\n",filename);
        }
        else if(lchannel == 6){
        fprintf(fp,"P6\n%u %u\n255\n", lwidth, lheight);
        for(j=0;j<dimy;++j){
            for(i=0;i<dimx;++i){
                static unsigned char color[3];
                color[0]=i%256;
                color[1]=j%256;
                color[2]=(i*j)%256;
                fwrite(color,1,3,fp);
            }
        }
        printf("File %s saved.\n",filename);
        }
        fclose(fp);
    return(0);
    free(data);
}

The fread size should be sizeof(unsigned char) rather than 1. fread 大小应该是 sizeof(unsigned char) 而不是 1。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM