繁体   English   中英

给定一个图像文件和pixel_array_offset,如何初始化实际的结构像素值?

[英]Given an image file and pixel_array_offset, how to initialize the actual struct pixel values?

遵循此要求

我还需要返回第一个“结构像素*”的地址。 不确定我是否正确实施了它。

struct pixel **read_pixel_array(FILE *image, int pixel_array_offset, int width, int height) {

    struct pixel **ptrs = malloc(height * sizeof(struct pixel));

    struct pixel *first_pixel;
    fseek(image, pixel_array_offset, SEEK_SET);

    for (int i=0, i<height, i++) {
        ptrs[i] = malloc(width * sizeof(struct pixel));
        for (int j=0, j<width, j++) {
            fread(ptrs[i][j]->blue, 1, 1, image);
            fread(ptrs[i][j]->green, 1, 1, image);
            fread(ptrs[i][j]->red, 1, 1, image);
        }

    }

    fseek(image, pixel_array_offset, SEEK_SET);
    fread(first_pixel, 3, 1, image);

    return &first_pixel;

}

您将要弄清楚如何将像素数据存储在RAM中。 具体来说,您是否想要:

  • 每行一个“指向像素的一维数组的指针”的一维数组
  • 二维像素阵列
  • 其他的东西

您的代码似乎正在尝试执行第一个选项(并且看起来做错了-例如malloc(height * sizeof(struct pixel));应该是malloc(height * sizeof(struct pixel *)); ;。您的代码也似乎正在尝试执行第二个选项(例如ptrs[i][j]->blue而不是row = ptrs[i];row[j]->blue )。

对于第一种选择(一维指针数组),它就像:

struct pixel **read_pixel_array(FILE *image, int pixel_array_offset, int width, int height) {

    struct pixel **ptrs;
    struct pixel *row;

    ptrs = malloc(height * sizeof(struct pixel *));
    if(ptrs == NULL) {
        // Failed to allocate memory for array of pointers
        return NULL;
    }

    fseek(image, pixel_array_offset, SEEK_SET);

    for (int i=0, i<height, i++) {
        row = malloc(width * sizeof(struct pixel));
        if(row == NULL) {
            // Failed to allocate memory for row. Need to free everything
            //   that was previously allocated to avoid memory leaks
            while(i > 0) {
                i--;
                free(ptrs[i]);
            }
            free(ptrs);
            return NULL;
        }
        ptrs[i] = row;
        for (int j=0, j<width, j++) {
            fread(row[j]->blue, 1, 1, image);
            fread(row[j]->green, 1, 1, image);
            fread(row[j]->red, 1, 1, image);
        }
    }
    return ptrs;
}

我自己,我会使用第二个选项(一个2D数组),因为它更容易,更快。 问题是C在编译时不知道尺寸,因此您需要作弊并使用“将2D数组实现为1D数组”。 在这种情况下,它将像:

struct pixel *read_pixel_array(FILE *image, int pixel_array_offset, int width, int height) {
    struct pixel *myArray;

    myArray = malloc(height * width * sizeof(struct pixel));
    if(myArray == NULL) {
        // Failed to allocate memory
        return NULL;
    }

    fseek(image, pixel_array_offset, SEEK_SET);

    for (int i=0, i<height, i++) {
        for (int j=0, j<width, j++) {
            fread(myArray [i*width + j]->blue, 1, 1, image);
            fread(myArray [i*width + j]->green, 1, 1, image);
            fread(myArray [i*width + j]->red, 1, 1, image);
        }
    }
    return myArray;
}

注意:这些示例均未经过测试,未经测试均不能视为“正确”。

暂无
暂无

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

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