Skip to content

Instantly share code, notes, and snippets.

@dsamarin
Created March 20, 2012 02:50
Show Gist options
  • Save dsamarin/2130473 to your computer and use it in GitHub Desktop.
Save dsamarin/2130473 to your computer and use it in GitHub Desktop.
BMP file dimensions
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <inttypes.h>
int main(int argc, char* argv[]) {
int width, height;
unsigned char data[8];
FILE *file;
if (argc < 2) {
fprintf (stderr, "Missing arguments\n");
return EXIT_FAILURE;
}
file = fopen(argv[1], "r");
if (!file) {
fprintf (stderr, "Could not open '%s'.\n", argv[1]);
return EXIT_FAILURE;
}
if (fseek (file, 16, SEEK_SET)) {
fprintf (stderr, "Could not seek\n");
fclose (file);
return EXIT_FAILURE;
}
if (fread (data, 8, 1, file) != 1) {
fprintf (stderr, "Could not read dimensions.\n");
fclose (file);
return EXIT_FAILURE;
}
width = data[3] + (data[2] << 8) + (data[1] << 16) + (data[0] << 24);
height = data[7] + (data[6] << 8) + (data[5] << 16) + (data[4] << 24);
printf ("Width: %d\nHeight: %d\n", width, height);
fclose (file);
return EXIT_SUCCESS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment