结构初始化和填充矩阵 - C

时间:2018-03-27 21:06:40

标签: c matrix struct

我需要创建一个类型为

的结构矩阵
struct colorPixelData_t {
    int red;
    int green;
    int blue;
};

我正试图像这样初始化我的价值观:

struct colorPixelData_t **initColorMatrix(struct colorPixelData_t **b, int height, int width)
{
        int i, j;
        b = malloc(height * width * sizeof(struct colorPixelData_t));
        assert(b);
        for (i = 0; i < height; i++) {
                for (j = 0; j < width; j++)
                        b[i][j] = malloc(sizeof(struct colorPixelData_t));
        }
        return b;
}

我正试图操纵下面的数据。基本上,我正在读取一个bmp文件,部分格式是像素数据,我将其读入void *数组,然后将其memcpy转换为unsigned char *数组,然后将unsigned char *数组转换为结构矩阵。每个3个字节每个像素保存一个rgb值,即字节0-2可能保持200,0,0,形成像素[0,0]的rgb(200,0,0) - 因此结构矩阵[像素x ,像素y] - &gt;红色,绿色,蓝色。

struct colorPixelData_t **getColorPixelInfo(struct colorPixelData_t **colorPixelData, void *colorPixelDataArr, int imageSize, int height, int width)
{
        unsigned char *unsCharArr = malloc(imageSize);
        assert(unsCharArr);
        memcpy(unsCharArr, colorPixelDataArr, imageSize);

        int counter = 0;
        int iteratorByThree = 0;
        for (int i = height - 1; i >= 0; i--) {
                for (int j = 0; j < width; j++) {
                        if (iteratorByThree == 0)
                                colorPixelData[i][j] -> red = (int) unsCharArr[counter];
                        else if (iteratorByThree == 1)
                                colorPixelData[i][j] -> green = (int) unsCharArr[counter];
                        else {
                                colorPixelData[i][j] -> blue = (int) unsCharArr[counter];
                                iteratorByThree = 0;
                        }
                        counter++;
                        iteratorByThree++;
                }
        }
        return colorPixelData;
}

当我编译它时,它给了我这些错误:

bmp.c: In function ‘initColorMatrix’:
bmp.c:678:33: error: incompatible types when assigning to type ‘struct colorPixelData_t’ from type ‘void *’
                         b[i][j] = malloc(sizeof(struct colorPixelData_t));
                                 ^
bmp.c: In function ‘getColorPixelInfo’:
bmp.c:694:54: error: invalid type argument of ‘->’ (have ‘struct colorPixelData_t’)
                                 colorPixelData[i][j] -> red = (int) unsCharArr[counter];
                                                      ^~
bmp.c:696:54: error: invalid type argument of ‘->’ (have ‘struct colorPixelData_t’)
                                 colorPixelData[i][j] -> green = (int) unsCharArr[counter];
                                                      ^~
bmp.c:698:54: error: invalid type argument of ‘->’ (have ‘struct colorPixelData_t’)
                                 colorPixelData[i][j] -> blue = (int) unsCharArr[counter];

谁能告诉我我做错了什么?

0 个答案:

没有答案
相关问题