Skip to content

Instantly share code, notes, and snippets.

@harieamjari
Last active June 10, 2020 06:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save harieamjari/b1991624866556ab3e6baaf1ae79a313 to your computer and use it in GitHub Desktop.
Save harieamjari/b1991624866556ab3e6baaf1ae79a313 to your computer and use it in GitHub Desktop.
A simple bmp writer in C using the standard library. This renders a 24-bit image of color red, green, and blue, from bottom to top, respectively.
/*
DO WHAT THE FUCK YOU WANT TO BUT IT'S NOT MY FAULT PUBLIC LICENSE
Version 1, October 2013
Copyright (c) 2020 Al-buharie Amjari <healer.harie@gmail.com>
Everyone is permitted to copy and distribute verbatim or modified copies
of this license document, and changing it is allowed as long as the name
is changed.
DO WHAT THE FUCK YOU WANT TO BUT IT'S NOT MY FAULT PUBLIC LICENSE TERMS
AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION:
0. You just DO WHAT THE FUCK YOU WANT TO.
1. Do not hold the author(s), creator(s), developer(s) or distributor(s)
liable for anything that happens or goes wrong with your use of the work.
*/
//Memo: http://www.ece.ualberta.ca/~elliott/ee552/studentAppNotes/2003_w/misc/bmp_file_format/bmp_file_format.htm
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct __attribute__((__packed__)) {
char Signature[2];
uint32_t FileSize;
uint32_t Reserved;
uint32_t DataOffset;
} bmp_header_struct;
typedef struct __attribute__((__packed__)) {
uint32_t Size;
uint32_t Width;
uint32_t Height;
uint16_t Planes;
uint16_t BitsPerPixel;
uint32_t Compression;
uint32_t ImageSize;
uint32_t XPixelsPerMeter;
uint32_t YPixelsPerMeter;
uint32_t ColorsUsed;
uint32_t ImportantColors;
} bmp_infoheader_struct;
enum { red = 0x00ff0000, green = 0x0000ff00, blue = 0x000000ff };
int main() {
FILE *bmp_file = fopen("write.bmp", "wb+");
if (!bmp_file) {
fprintf(stderr, "Couldn't open file!\n");
return 127;
}
// uint24_t *pixel_x;
// uint24_t *pixel_y;
uint32_t *pixel_all;
bmp_header_struct bmp_header0;
bmp_infoheader_struct bmp_infoheader0;
strncpy(bmp_header0.Signature, "BM", 2);
bmp_header0.FileSize = 0;
bmp_header0.Reserved = 0;
bmp_header0.DataOffset = 36;
bmp_infoheader0.Size = 40;
bmp_infoheader0.Width = 400;
bmp_infoheader0.Height = 1000;
bmp_infoheader0.Planes = 1;
bmp_infoheader0.BitsPerPixel = 24;
bmp_infoheader0.Compression = 0;
bmp_infoheader0.ImageSize = 0;
bmp_infoheader0.XPixelsPerMeter = 0;
bmp_infoheader0.YPixelsPerMeter = 0;
bmp_infoheader0.ColorsUsed = 0;
bmp_infoheader0.ImportantColors = 0;
fwrite(&bmp_header0, sizeof(bmp_header0), 1, bmp_file);
fwrite(&bmp_infoheader0, sizeof(bmp_infoheader0), 1, bmp_file);
pixel_all =
malloc(sizeof(uint32_t) * bmp_infoheader0.Width * bmp_infoheader0.Height);
for (uint32_t y = 0; y < bmp_infoheader0.Height; y++) {
for (uint32_t x = 0; x < bmp_infoheader0.Width; x++) {
if (y < 1000)
pixel_all[y] = blue;
if (y < 620)
pixel_all[y] = green;
if (y < 320)
pixel_all[y] = red;
fwrite(pixel_all + y, 3, 1, bmp_file);
}
}
uint32_t FileSize = ftell(bmp_file);
fseek(bmp_file, 2, SEEK_SET);
fwrite(&FileSize, sizeof(uint32_t), 1, bmp_file);
free(pixel_all);
fclose(bmp_file);
printf("writing successful\n");
return 0;
}
@harieamjari
Copy link
Author

harieamjari commented Jun 5, 2020

Note: fwrite at line 93 makes use of the endianess of a system. Instead of being written as 0x00ff0000, we get 0x0000ff; it gets truncated because fwrite is directed to only write 3 bytes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment