Skip to content

Instantly share code, notes, and snippets.

@mdukat
Last active March 6, 2019 14:18
Show Gist options
  • Save mdukat/7fc8973331545ad70327d9ba7533031a to your computer and use it in GitHub Desktop.
Save mdukat/7fc8973331545ad70327d9ba7533031a to your computer and use it in GitHub Desktop.
bmp generator (2x2)
#include <stdlib.h>
#include <stdio.h>
#define BITMAP_SIZE 16
struct __attribute__((__packed__)) bmpHeader {
short BM; //const "BM"
unsigned int fileSize; // oblicz se xD
unsigned int reserved; // byle co, nawet papiesz
unsigned int bitmapOffset;
};
struct dibHeader {
unsigned int dibSize;
int width;
int height;
short planes; // statyczne 1
short bps; // bits per pixel (24 git gud)
int BI_RGB; // kompresja (statyczne 0)
unsigned int bitmapDataSize; // w bajtach, surowe dane bitmapy
unsigned int DPI_horizontal;
unsigned int DPI_vertical;
unsigned int paletteColors;
unsigned int importantColors; // jesteś tak ważny, jak kolory w mojej palecie :3
};
int main(){
struct bmpHeader BMP_Header;
struct dibHeader DIB_Header;
// init BMP_Header;
BMP_Header.BM = 0x4D42;
BMP_Header.fileSize = (sizeof(struct bmpHeader) + sizeof(struct dibHeader) + BITMAP_SIZE) - 2;
BMP_Header.reserved = 0;
BMP_Header.bitmapOffset = (sizeof(struct bmpHeader) + sizeof(struct dibHeader));
// init DIB_Header;
DIB_Header.dibSize = sizeof(struct dibHeader);
DIB_Header.width = 2;
DIB_Header.height = 2; // fight me
DIB_Header.planes = 1;
DIB_Header.bps = 24;
DIB_Header.BI_RGB = 0;
// DIB_Header.bitmapDataSize (później)
DIB_Header.DPI_horizontal = 2835;
DIB_Header.DPI_vertical = 2835;
DIB_Header.paletteColors = 0;
DIB_Header.importantColors = 0;
// bitmap itself
char* bitmapArray = malloc(BITMAP_SIZE);
for(int i = 0; i<BITMAP_SIZE; i++){ // tymczasowo
bitmapArray[i] = 0xAB;
};
// calculate things
//BMP_Header.fileSize = (BMP_Header.bitmapOffset + BITMAP_SIZE) - 2;
DIB_Header.bitmapDataSize = BITMAP_SIZE;
// write to file
FILE* myFileObject = fopen("./test.bmp", "wb");
fwrite(&BMP_Header, sizeof(BMP_Header), 1, myFileObject);
fwrite(&DIB_Header, sizeof(DIB_Header), 1, myFileObject);
fwrite(bitmapArray, BITMAP_SIZE, 1, myFileObject);
fclose(myFileObject);
// cleanup
free(bitmapArray);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment