0

I have to read pixel data into a structure without padding. How can this be achieved?
Header data are also in structure, but there is no problem. The BMP picture is 24bpp.

struct pixel* read_data(FILE* 
stream, const struct 
bmp_header* header)
{
    if (stream == NULL)
    {
        return NULL;
    }
    int hei = header->height;
    int wid = header->width;
    int bpp = header->bpp;

    rewind(stream);
    struct pixel *rgb = NULL;

    rgb = callocc(wid*hei*bpp,
    sizeof(struct pixel));
    fseek(stream,54,SEEK_SET);
    fread(rgb,1,wid*hei*bpp ,stream);
    return rgb;
}

And struct is:

struct pixel* pixel{
    unit16 blue;
    unit16 green;
    unit16 red;
}
Dharman
  • 30,962
  • 25
  • 85
  • 135
Anonymous
  • 13
  • 4
  • regarding: `if (stream == NULL) { return NULL; }` Why call this function if the `stream` is NULL? That should have been checked when the .bmp file was originally opened. – user3629249 Apr 20 '20 at 16:06
  • regarding: `rgb = callocc(wid*hei*bpp, sizeof(struct pixel));` what is `callocc()` did you mean `calloc()`? – user3629249 Apr 20 '20 at 16:08
  • regarding: `fseek(stream,54,SEEK_SET);` That 54 is not necessarily correct. Suggest using the `file offset to pixel array` field of the `bitmapfileheader` struct to locate the pixel array. – user3629249 Apr 20 '20 at 16:12
  • Your question states that you do not want the padding in the pixel array. So cannot simply read the whole array in one gulp as each line may have padding bytes on its' end – user3629249 Apr 20 '20 at 16:14
  • [.bmp file format](https://en.wikipedia.org/wiki/BMP_file_format) – user3629249 Apr 20 '20 at 16:14
  • Please post a [mcve] so we can reproduce your problem and help you debug it – user3629249 Apr 20 '20 at 16:15
  • regarding: `const struct bmp_header* header)` What is the contents of the `bmp_header` struct? – user3629249 Apr 20 '20 at 16:20
  • Please read: [input pixel data](https://stackoverflow.com/questions/14279242/read-bitmap-file-into-structure) for some insight into how to read the pixel data of a bmp file – user3629249 Apr 20 '20 at 16:27

0 Answers0