简体   繁体   English

ppm文件的图像卷积

[英]image convolution from ppm file

Good afternoon to everyone. 大家下午好。 Firstly, i would like to apologize for my code - i am a real beginner in C. My problem is - i am given ppm file and i would need to store values from there into an array. 首先,我想为我的代码道歉-我是C语言的真正初学者。我的问题是-给我ppm文件,我需要将其中的值存储到数组中。 I have already stored height , width and max value of color, now my idea to store a values would be the as shown at the picture - multiplying by three because it is in RGB format. 我已经存储了color的height,width和max值,现在我存储一个值的想法是如图所示-乘以三,因为它是RGB格式。 thank you for your help and please concider the fact i am a real beginner in C. 感谢您的帮助,请考虑一下我是C语言的真正初学者的事实。

my code and output 我的代码和输出

#include <stdio.h>
int main(int argc, char** argv) {
int i = 0;
int j = 0;
FILE *fp;
fp = fopen(argv[1], "r");
printf(" %s ", argv[1]);
printf("\n");
int firstLine[2];
int width;
int next;
int enter;
int loop;
int height;
int max_color;
int pix[width][height];
int mask[3][3] = {// inicializting our given mask
    {0, -1, 0},
    {-1, 5, -1},
    {0, -1, 0}
};

for (i = 0; i < 3; i++) {
    for (j = 0; j < 3; j++) {
        printf("%d ", mask[i][j]);

    }
    printf("\n");
}

fscanf(fp, "%s", &firstLine);
fscanf(fp, "%d", &height);
fscanf(fp, "%d", &width);
fscanf(fp, "%d", &max_color);


printf("%p", firstLine);
printf("\n");
printf("%d ", width);
printf("\n");
printf("%d", height);
printf("\n");
printf("%d", max_color);
printf("\n");


for (i = 0; i < width * 3; i++) {
    for (j = 0; j < height * 3; j++) {
        loop = fscanf(fp, "%d", &enter);
        pix[i][j] = enter;
        printf("%d ", enter);
    }
}

// fclose(fp);


return (EXIT_SUCCESS);

} }

You cannot define array with unknown size, esp height and width are not initialized. 您无法定义大小未知的数组,尤其是高度和宽度未初始化。 You should use dynamic allocated array here, like this: 您应该在此处使用动态分配的数组,如下所示:

int ***pix;
pix = malloc(height * sizeof(int**));
for (i = 0; i < height; i++) {
    pix[i] = malloc(width * sizeof(int**));
    for (j = 0; j < width; j++) {
        pix[i][j] = malloc(3 * sizeof(int));
    }
}

To correct parse the binary, you cannot use formatted input because they are for strings. 要正确解析二进制文件,您不能使用格式化的输入,因为它们用于字符串。 You can use fread instead, eg to read the width: 您可以改用fread,例如读取宽度:

fread(&width, sizeof(int), 1, fp);

Then to fill this array: 然后填充此数组:

for (i = 0; i < height; i++) {
    for (j = 0; j < width; j++) {
        for (k = 0; k < 3; k++) {
            fread(&enter, sizeof(int), 1, fp);
            pix[i][j][k] = enter;
        }
    }
}

This only works if what you said about ppm file's format is correct of course. 当然,这仅在您对ppm文件格式所说的正确的情况下有效。

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

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