|
|
@ -3,25 +3,21 @@
|
|
|
|
#include "images.h"
|
|
|
|
#include "images.h"
|
|
|
|
|
|
|
|
|
|
|
|
// write an int to a byte array
|
|
|
|
// write an int to a byte array
|
|
|
|
void bmp__write_int(char* bytes, int num) {
|
|
|
|
void bmp__write_int(unsigned char* bytes, int num) {
|
|
|
|
for (int i = 0; i < 4; i++) {
|
|
|
|
for (int i = 0; i < 4; i++) {
|
|
|
|
bytes[i] = num >> (i * 8);
|
|
|
|
bytes[i] = num >> (i * 8);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void bmp__write_byte(char* bytes, int num) {
|
|
|
|
void bmp__write_16bit(unsigned char* bytes, int num) {
|
|
|
|
bytes[0] = (char) num;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void bmp__write_16bit(char* bytes, int num) {
|
|
|
|
|
|
|
|
for (int i = 0; i < 2; i++) {
|
|
|
|
for (int i = 0; i < 2; i++) {
|
|
|
|
bytes[i] = num >> (i * 8);
|
|
|
|
bytes[i] = num >> (i * 8);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int bmp__align_to_four_bytes(int size) {
|
|
|
|
int bmp__align_to_four_bytes(int row_length) {
|
|
|
|
int os = size % 4;
|
|
|
|
int os =row_length % 4;
|
|
|
|
return os == 0 ? size : size + (4 - os);
|
|
|
|
return os == 0 ?row_length : row_length + (4 - os);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int bmp__image_byte_size(Image image) {
|
|
|
|
int bmp__image_byte_size(Image image) {
|
|
|
@ -30,9 +26,9 @@ int bmp__image_byte_size(Image image) {
|
|
|
|
// BITMAPCOREHEADER = 12 bytes
|
|
|
|
// BITMAPCOREHEADER = 12 bytes
|
|
|
|
int size = 14 + 12;
|
|
|
|
int size = 14 + 12;
|
|
|
|
// width must be padded to align to 4 bytes
|
|
|
|
// width must be padded to align to 4 bytes
|
|
|
|
int width_bytes = bmp__align_to_four_bytes(image.width * 3); // 3 bytes per pixel
|
|
|
|
int row_bytes = bmp__align_to_four_bytes(image.width * 3); // 3 bytes per pixel
|
|
|
|
// add width_bytes * height to total size
|
|
|
|
// add row_bytes * height to total size
|
|
|
|
size += width_bytes * image.height;
|
|
|
|
size += row_bytes * image.height;
|
|
|
|
|
|
|
|
|
|
|
|
return size;
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -50,22 +46,30 @@ int bmp__image_byte_size(Image image) {
|
|
|
|
// 0x01 0x00 number of color planes (must be 1)
|
|
|
|
// 0x01 0x00 number of color planes (must be 1)
|
|
|
|
// 0x18 0x00 number of bits per pixel (3 * 8 = 24)
|
|
|
|
// 0x18 0x00 number of bits per pixel (3 * 8 = 24)
|
|
|
|
|
|
|
|
|
|
|
|
int image_save_bmp(Image image, char* path) {
|
|
|
|
|
|
|
|
|
|
|
|
// char* bytes;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// bytes + 2 => zeigt auf offset 2 (ist char*)
|
|
|
|
|
|
|
|
// bytes[2] => wert an offset 2 (ist char)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int image_save_bmp(Image image, const char* path) {
|
|
|
|
int size = bmp__image_byte_size(image);
|
|
|
|
int size = bmp__image_byte_size(image);
|
|
|
|
char* bytes = calloc(sizeof(char), size);
|
|
|
|
unsigned char* bytes = calloc(sizeof(char), size);
|
|
|
|
// magic bytes, signature "BM"
|
|
|
|
// magic bytes, signature "BM"
|
|
|
|
bytes[0] = 0x42;
|
|
|
|
// bmp header:
|
|
|
|
bytes[1] = 0x4d;
|
|
|
|
bytes[0] = 'B';
|
|
|
|
|
|
|
|
bytes[1] = 'M';
|
|
|
|
// write size to header
|
|
|
|
// write size to header
|
|
|
|
bmp__write_int(bytes + 2, size);
|
|
|
|
bmp__write_int(bytes + 2, size);
|
|
|
|
// the next 4 byte are zeros
|
|
|
|
// the next 4 byte are zeros
|
|
|
|
// piyel array offset
|
|
|
|
// pixel array offset
|
|
|
|
bmp__write_int(bytes + 10, 14 + 12);
|
|
|
|
bmp__write_int(bytes + 10, 14 + 12);
|
|
|
|
bmp__write_int(bytes + 14, 12);
|
|
|
|
// DIB header:
|
|
|
|
|
|
|
|
bmp__write_int(bytes + 14, 12); // header size
|
|
|
|
bmp__write_16bit(bytes + 18, image.width); // width
|
|
|
|
bmp__write_16bit(bytes + 18, image.width); // width
|
|
|
|
bmp__write_16bit(bytes + 20, image.height); // height
|
|
|
|
bmp__write_16bit(bytes + 20, image.height); // height
|
|
|
|
bmp__write_16bit(bytes + 22, 1); // color planes
|
|
|
|
bmp__write_16bit(bytes + 22, 1); // color planes
|
|
|
|
bmp__write_16bit(bytes + 24, 3 * 8); // bits per pixel
|
|
|
|
bmp__write_16bit(bytes + 24, 3 * 8); // bits per pixel
|
|
|
|
|
|
|
|
|
|
|
|
int byte_offset = 26;
|
|
|
|
int byte_offset = 26;
|
|
|
|
int byte_pos = 0;
|
|
|
|
int byte_pos = 0;
|
|
|
|