[英]Given an image file and pixel_array_offset, how to initialize the actual struct pixel values?
Following this requirement 遵循此要求
I also need to return the address of the first "struct pixel *". 我还需要返回第一个“结构像素*”的地址。 Not sure whether I'm implementing it correctly.
不确定我是否正确实施了它。
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;
}
You're going to want to figure out how you want to store the pixel data in RAM. 您将要弄清楚如何将像素数据存储在RAM中。 Specifically, do you want:
具体来说,您是否想要:
Your code looks like it's trying to do the first option (and looks like it's doing it wrong - eg malloc(height * sizeof(struct pixel));
when it should be malloc(height * sizeof(struct pixel *));
. However your code also looks like it's trying to do the second option (eg ptrs[i][j]->blue
and not row = ptrs[i];
and row[j]->blue
). 您的代码似乎正在尝试执行第一个选项(并且看起来做错了-例如
malloc(height * sizeof(struct pixel));
应该是malloc(height * sizeof(struct pixel *));
;。您的代码也似乎正在尝试执行第二个选项(例如ptrs[i][j]->blue
而不是row = ptrs[i];
和row[j]->blue
)。
For the first option (1 dimensional array of pointers) it'd be like: 对于第一种选择(一维指针数组),它就像:
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;
}
Myself, I'd use the second option (one 2D array) because it's easier and faster. 我自己,我会使用第二个选项(一个2D数组),因为它更容易,更快。 The problem is that C doesn't know the dimensions at compile time, so you need to cheat and use "2D array implemented as 1D array".
问题是C在编译时不知道尺寸,因此您需要作弊并使用“将2D数组实现为1D数组”。 In that case it'd be like:
在这种情况下,它将像:
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;
}
NOTE: Neither of these examples were tested and neither can be assumed "correct" without testing. 注意:这些示例均未经过测试,未经测试均不能视为“正确”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.