繁体   English   中英

从ppm文件C中的像素获取0-255的值

[英]Getting the 0-255 values from a pixel in a ppm file C

好的,所以我有代码将从ppm图像中读取标题,为图像的大小分配内存,并将为每个像素成功打印空白点(在终端中重复的随机数)。 我需要知道的是如何读取每个像素的红色绿色和蓝色值(3个独立的值,范围从0-255)。 我不知道如何在每个像素内访问此数据。 到目前为止,这是我的代码:

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

int subscript(int row,int column,int numberColumns);
int sub(int rd, int gr, int bl);
//table of contents for the subscript
int main(void){
        char header[5];
        scanf("%s",header);
        printf("%s",header);
        printf("\n");
        int width, height, depth;
        scanf("%d %d %d\n", &width, &height, &depth);
        printf("%d %d %d\n", width, height, depth);
        int red = 0;
        int green = 0;
        int blue = 0;
        int sm;
        int r = 0;
        int c = 0;
        //allocate memory for bit to be used throughout main
        unsigned char *bit = malloc(width*height);
        int *rgb = malloc(3*depth);
        //loops to read in table and print it
        while(r < height){
                while(c < width)
                        {
                        int col;
                        scanf("%d",&col);
                        //brings in allocated memory and values
                        bit[subscript(r,c,width)] = col;
                        int clr;
                        rgb[sub(red,green,blue)] = clr; 
                        int color = clr + col;
                        printf(" %d",clr);
                        c=c+1;
                        }
                printf("\n");
                r = r + 1;
                c = 0;
                }


        free(bit);

}
int subscript(int row,int column, int numberColumns)
        {
        return row * numberColumns + column;
        //retuns items for subscript
}

int sub(int rd, int gr, int bl)
        {
        return rd+gr+bl;
}

如果标题中的最大颜色值字段小于256,则PPM文件将颜色存储为字节的三倍(每个字节分别用于r,g和b),如果颜色小于256,则存储为两个字节的三倍(如果大于或等于256,则为两个字节) r,g和b中的每一个的字节数,也按该顺序)。

在每通道两个字节的情况下,字节是MSB优先(大端)。


scanf("%c", &byte_var); 会将您读取的单个字符(字节)读入byte_var ,这应该是适当的类型。 然后,您可以对其进行相应处理。 scanf("%hhu", &byte_var); 如果使用的是C99,它将读取一个无符号字符-如果使用的是C89,则应查找getchar

现在, scanf返回成功转换的参数或EOF的数量,因此您应该检查错误输入和输入结束的结果。 例如:

int n;
char c;

while ((n = scanf("%c", &c)) != EOF && 1 == n) {
    // do something
}

一个示例用法是:

// Read width, height, work out how many bytes you need per pixel.
...

// Now read some pixels
// Here I assume one byte per pixel; this would be a good situation
// to use a function for reading different channel widths

w = 0, h = 0;
while ((n = scanf("%hhu%hhu%hhu", &r, &g, &b)) != EOF && 3 == n
        && w < width && h < height) {
  // now do something with r, g, b
  // e.g. store them in an appropriate pixels array at pixels[w][h] 
  ...

  ++w;
  if (w == width) {
    w = 0, ++h;
  }
}

我还看到您正在从stdin中读取内容,由于标题表明您正在使用文件,因此我发现它有点不寻常。 您可以使用fopen打开文件(不要忘记检查结果),使用fscanf读取文件,然后使用fclose将其关闭。

但是,我建议将整个文件读入内存并在那里进行处理,因为一次读取几个字节的文件速度很慢。


最后,我注意到您仅在需要bytes_per_channel * num_channels * width * height时才为bit分配width * height字节,而不检查malloc是否失败,也不释放rgb 您应该修复这些问题。

暂无
暂无

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

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