Tchiggy
Tchiggy

Reputation: 131

Reading data from BMP file in C

I have a problem with reading pixels from bmp file. It might be something with padding at the end of row or the base64 padding. I have no clue. I've been struggling with this for some days and can't move on because the next task requires this one to be solved. I only share important parts of the code, since reading bmp header worked fine (tests had 0 failures).

bmp.c

struct pixel* read_data(FILE* stream, const struct bmp_header* header){
  if(stream == NULL || header == NULL){
    return 0;
  }
  // w == 1 && p == 1;   w == 2 && p == 2;   w == 3 && p == 3;   w == 4 && p == 0  
  int padding = header->width % 4; 
  int num_of_pixels = header->width * header->height;
  struct pixel* Pixel[num_of_pixels];

  fseek(stream, 54, SEEK_SET); //move 54B (header size)
  int index_p = 0;
  for(int i = 0; i < header->height; i++){
    for(int j = 0; j < header->width; j++){
      Pixel[index_p] = malloc(sizeof(struct pixel));
      fread(&(Pixel[index_p]->blue), 1, 1, stream); 
      fread(&(Pixel[index_p]->green), 1, 1, stream); 
      fread(&(Pixel[index_p]->red), 1, 1, stream); 
      index_p++;
    }
    fseek(stream, padding, SEEK_CUR);  //padding at the end of row
  }
  return *Pixel;
}

bmp.h

struct pixel {
    uint8_t blue;
    uint8_t green;
    uint8_t red;
    //uint8_t alpha;
} __attribute__((__packed__));

/**
 * Read the pixels
 *
 * Reads the data (pixels) from stream representing the image. If the stream
 * is not open or header is not provided, returns `NULL`.
 *
 * @param stream opened stream, where the image data are located
 * @param header the BMP header structure
 * @return the pixels of the image or `NULL` if stream or header are broken
 */
struct pixel* read_data(FILE* stream, const struct bmp_header* header);

header if needed (basically we use only 24bit color)

struct bmp_header{
    uint16_t type;              // "BM" (0x42, 0x4D)
    uint32_t size;              // file size
    uint16_t reserved1;         // not used (0)
    uint16_t reserved2;         // not used (0)
    uint32_t offset;            // offset to image data (54B)
    uint32_t dib_size;          // DIB header size (40B)
    uint32_t width;             // width in pixels
    uint32_t height;            // height in pixels
    uint16_t planes;            // 1
    uint16_t bpp;               // bits per pixel (24)
    uint32_t compression;       // compression type (0/1/2) 0
    uint32_t image_size;        // size of picture in bytes, 0
    uint32_t x_ppm;             // X Pixels per meter (0)
    uint32_t y_ppm;             // X Pixels per meter (0)
    uint32_t num_colors;        // number of colors (0)
    uint32_t important_colors;  // important colors (0)
} __attribute__((__packed__));

main.c I do not need to assign any variables to called functions because we have a program for testing this, I just have to call them in main

int main(){

  struct bmp_header* header;
  FILE *stream = fopen("./assets/square.2x3.bmp", "rb");
  header = read_bmp_header(stream);
  read_data(stream, header);
  read_bmp(stream);
  struct bmp_image* image;
  image = malloc(sizeof(struct bmp_image));
  free_bmp_image(image);  
  fclose(stream);
  return 0;
}

testing (there are more tests, but this should be enough)

1: 
FILE* stream = "Qk0+AAAAAAAAADYAAAAoAAAAAgAAAAEAAAABABgAAAAAAAgAAAAjLgAAIy4AAAAAAAAAAAAA/wAAAP8AAAA="; // base64 encoded stream
struct bmp_header* header = read_bmp_header(stream);
fseek(stream, offset, SEEK_SET);
Assertion 'read_data(stream, header) == "/wAAAP8A"' failed. [got "/wAAFctV"]

2: 
FILE* stream = "Qk1GAAAAAAAAADYAAAAoAAAAAgAAAAIAAAABABgAAAAAABAAAAAjLgAAIy4AAAAAAAAAAAAA/wAAAAAAAAAAAP8A/wAAAA=="; // base64 encoded stream
struct bmp_header* header = read_bmp_header(stream);
fseek(stream, offset, SEEK_SET);
Assertion 'read_data(stream, header) == "/wAAAAAAAAD/AP8A"' failed. [got "/wAAAAAAAAAAAAAA"]

So after the "==" is expected result and in the brackets is my the result from my code. As I mentioned, it might be something with padding, since it starts well but doesn't end well. Thanks for help.

Upvotes: 0

Views: 6070

Answers (1)

NoShady420
NoShady420

Reputation: 941

Short Answer: Set padding to (4-((3*width)%4))%4

Long answer:

Your code included:

int padding = header->width % 4; 

//Some lines of code

fseek(stream, padding, SEEK_CUR);

In a bitmap, padding is added until each row is a multiple of 4 bytes. You took padding as width % 4.

First off, each pixel takes 3 bytes(for rgb). So it should be (3*width)%4. Next, we need to subtract it from 4 bytes (Since padding is 4-pixels occupied). So padding would be 4-((3*width)%4). Another small modification, if (3*width)%4==0 then padding would come to be 4 (whereas, we expect it to be 0). So we take another mod4 just to be sure

So padding would come out to be (4-((3*width)%4))%4

EDIT:

As pointed out by user Craig Estey in the comments, its better to use sizeof(struct pixel) instead of 3

Upvotes: 2

Related Questions