Skip to content

VAD reader C example

Raresh Nistor edited this page Aug 31, 2019 · 1 revision

The code below was frugally written in a web browser. Use at own risk.

#include <stdlib.h>
#include <stdint.h>

typedef struct TGRVBitmap {
  uint16_t width;
  uint16_t height;
  uint8_t* data;
} Bitmap;

TGRVBitmap* loadBitmap(char* filename) {
  FILE* fp = NULL;
  Bitmap* bm = NULL;
  uint8_t* bmData = NULL;
  char header[5] = { 0 };
  size_t index = 0, charCount = 0;

  // create a place for the bitmap data in memory
  bm = calloc(1, sizeof(Bitmap));
  
  // load the file and read the header data
  fp = fopen(filename, "rb");
  fread(header, 1, 4, fp);
  fread(&bm->width,  sizeof(uint16_t), 1, fp);
  fread(&bm->height, sizeof(uint16_t), 1, fp);
  charCount = bm->width * bm->height;
  
  // load the pixel data
  bmData = malloc(sizeof(uint8_t) * charCount);
  fread(bmData, sizeof(uint8_t), charCount, fp);
  bm->data = bmData;

  // done
  return bm;
}
Clone this wiki locally