I'm working with bmp images, which I'm passing a a 2d array by reference to a function that opens the bmp and stores it using a struct. When the function exits, I have a reference to an address. I can add it to a new array to store the opened bmp, but as it is a reference all locations of the new array have the same reference.
see
https://www.codeproject.com/Questions/5248639/Why-is-my-array-index-values-all-the-same as a reference to my original problem.
So I need to de-reference and only insert the 2d array values. How do I go about doing this.
typedef struct pixel {
uint8_t r;
uint8_t g;
uint8_t b;
} pixel;
pixel** img;
struct pixel** images[num_images];
img = (pixel**)malloc (IMG_Y * sizeof (pixel*));
for (i = 0; i < IMG_Y; i++)
{
img[i] = (pixel*)malloc (IMG_X * sizeof (pixel));
}
for (i = 0; i < num_images; i++)
{
if (open_bmp (*(image_names + i), img) == EXIT_FAILURE)
{
return EXIT_FAILURE;
}
images[i] = img;
}
}
int open_bmp (char * file_name, pixel** img) {
long offset = 0;
unsigned char buff[100];
int x = 0, y = 0;
FILE *f = fopen (file_name, "rb");
fseek (f, offset, SEEK_SET); while ((fread (&buff, 3, 1, f) >= 1) && y < IMG_Y) {
img[y][x].r = buff[2];
img[y][x].g = buff[1];
img[y][x].b = buff[0];
x++; if (x >= IMG_X) { y++;
x = 0;
}
}
if (PRINTBMPDATA) {
for (y = 0; y < IMG_Y; y++) {
for (x = 0; x < IMG_X; x++) {
printf ("%02x%02x%02x ", img[y][x].b, img[y][x].g, img[y][x].r);
}
printf ("\n");
}
printf ("\n");
}
if (fclose (f)) {
printf ("Unable to close image file\n");
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
What I have tried:
What I have attempted to do was de-reference. Which only inserted a single value of 0.
images[i] = **img
the result was just a 1D array
Next I thought I should attempt to pass a 3D array, modify the open_bmp() and just do the same thing I'm doing now, but when passed to open_bmp() fill levels 2 and 3. here are the changes I made.
pixel*** img;
img = (pixel***)malloc (num_images * sizeof (pixel**));
for(i = 0; i < num_images; i++)
{
img[i] = (pixel**)malloc (IMG_Y * sizeof (pixel*));
for (k = 0; k < IMG_Y; i++)
img[i][k] = (pixel*)malloc (IMG_X * sizeof (pixel));
}
for (i = 0; i < num_images; i++)
{
if (open_bmp (*(image_names + i), img, i) == EXIT_FAILURE) {
return EXIT_FAILURE;
}
}
int open_bmp (char * file_name, pixel*** img, int idx) {
printf ("Index: %d\n", idx);
long offset = 0;
unsigned char buff[100];
int x = 0, y = 0;
FILE *f = fopen (file_name, "rb");
fseek (f, offset, SEEK_SET); while ((fread (&buff, 3, 1, f) >= 1) && y < IMG_Y) {
img[idx][y][x].r = buff[2];
img[idx][y][x].g = buff[1];
img[idx][y][x].b = buff[0];
x++; if (x >= IMG_X) { y++;
x = 0;
}
}
if (PRINTBMPDATA) {
for (y = 0; y < IMG_Y; y++) {
for (x = 0; x < IMG_X; x++) {
printf ("%02x%02x%02x ", img[idx][y][x].b, img[idx][y][x].g, img[idx][y][x].r);
}
printf ("\n");
}
printf ("\n");
}
if (fclose (f)) {
printf ("Unable to close image file\n");
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
while I wrote a test case and it works fine, I've found a lot of references stating 3d+ arrays are a bad idea in this manner. The other issue, is the actual program doesn't execute.