Added save_bmp function to main.c to save the image array generated as a .bmp file
This commit is contained in:
+77
-1
@@ -1,5 +1,81 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
void save_bmp(const char *filename, unsigned char *img, size_t width, ssize_t height);
|
||||||
|
|
||||||
int main(int argc, char const *argv[])
|
int main(int argc, char const *argv[])
|
||||||
{
|
{
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void save_bmp(const char *filename, unsigned char *img, size_t width, ssize_t height) {
|
||||||
|
FILE *fptr = fopen(filename,"wb");
|
||||||
|
if(fptr == NULL) {
|
||||||
|
fprintf(stderr,"ERROR: Could not open output file at %s\n.",filename);
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
int actual_height = (height < 0) ? -height : height;
|
||||||
|
size_t row_padding = (4 - (width) * 3 % 4) % 4;
|
||||||
|
size_t row_width = width * 3 + row_padding;
|
||||||
|
|
||||||
|
size_t image_size = actual_height * row_width;
|
||||||
|
size_t file_size = image_size + 54;
|
||||||
|
|
||||||
|
unsigned char file_header[14] = {
|
||||||
|
'B', 'M',
|
||||||
|
0, 0, 0, 0,
|
||||||
|
0, 0, 0, 0,
|
||||||
|
54, 0, 0, 0
|
||||||
|
};
|
||||||
|
|
||||||
|
file_header[2] = (unsigned char)(file_size);
|
||||||
|
file_header[3] = (unsigned char)(file_size >> 8);
|
||||||
|
file_header[4] = (unsigned char)(file_size >> 16);
|
||||||
|
file_header[5] = (unsigned char)(file_size >> 24);
|
||||||
|
|
||||||
|
unsigned char info_header[40] = {
|
||||||
|
40, 0, 0, 0,
|
||||||
|
0, 0, 0, 0,
|
||||||
|
0, 0, 0, 0,
|
||||||
|
1, 0,
|
||||||
|
24, 0,
|
||||||
|
0, 0, 0, 0,
|
||||||
|
0, 0, 0, 0,
|
||||||
|
0x13, 0x0B, 0, 0,
|
||||||
|
0x13, 0x0B, 0, 0,
|
||||||
|
0, 0, 0, 0,
|
||||||
|
0, 0, 0, 0
|
||||||
|
};
|
||||||
|
|
||||||
|
info_header[4] = (unsigned char)(width);
|
||||||
|
info_header[5] = (unsigned char)(width >> 8);
|
||||||
|
info_header[6] = (unsigned char)(width >> 16);
|
||||||
|
info_header[7] = (unsigned char)(width >> 24);
|
||||||
|
|
||||||
|
uint32_t h = (uint32_t)height;
|
||||||
|
info_header[8] = (unsigned char)(h);
|
||||||
|
info_header[9] = (unsigned char)(h >> 8);
|
||||||
|
info_header[10] = (unsigned char)(h >> 16);
|
||||||
|
info_header[11] = (unsigned char)(h >> 24);
|
||||||
|
|
||||||
|
info_header[20] = (unsigned char)(image_size);
|
||||||
|
info_header[21] = (unsigned char)(image_size >> 8);
|
||||||
|
info_header[22] = (unsigned char)(image_size >> 16);
|
||||||
|
info_header[23] = (unsigned char)(image_size >> 24);
|
||||||
|
|
||||||
|
fwrite(file_header,1,14,fptr);
|
||||||
|
fwrite(info_header,1,40,fptr);
|
||||||
|
|
||||||
|
unsigned char padding_bytes[3] = {0, 0, 0};
|
||||||
|
|
||||||
|
for(int i = 0; i < actual_height; i++) {
|
||||||
|
fwrite(img + (i * width * 3), 3, width, fptr);
|
||||||
|
if(row_padding > 0) {
|
||||||
|
fwrite(padding_bytes,1,row_padding,fptr);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fclose(fptr);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user