|
| 1 | +#include "bmp.h" |
| 2 | + |
| 3 | +#define FOR_HEADER(FOR_FIELD) \ |
| 4 | + FOR_FIELD(uint16_t, bfType) \ |
| 5 | + FOR_FIELD(uint32_t, bfileSize) \ |
| 6 | + FOR_FIELD(uint32_t, bfReserved) \ |
| 7 | + FOR_FIELD(uint32_t, bOffBits) \ |
| 8 | + FOR_FIELD(uint32_t, biSize) \ |
| 9 | + FOR_FIELD(uint32_t, biWidth) \ |
| 10 | + FOR_FIELD(uint32_t, biHeight) \ |
| 11 | + FOR_FIELD(uint16_t, biPlanes) \ |
| 12 | + FOR_FIELD(uint16_t, biBitCount) \ |
| 13 | + FOR_FIELD(uint32_t, biCompression) \ |
| 14 | + FOR_FIELD(uint32_t, biSizeImage) \ |
| 15 | + FOR_FIELD(uint32_t, biXPelsPerMeter) \ |
| 16 | + FOR_FIELD(uint32_t, biYPelsPerMeter) \ |
| 17 | + FOR_FIELD(uint32_t, biClrUsed) \ |
| 18 | + FOR_FIELD(uint32_t, biClrImportant) |
| 19 | + |
| 20 | +#define DECLARE_FIELD(type, name) type name; |
| 21 | + |
| 22 | +struct __attribute__((packed)) header { |
| 23 | + FOR_HEADER(DECLARE_FIELD) |
| 24 | +}; |
| 25 | + |
| 26 | +#undef FOR_HEADER |
| 27 | +#undef DECLARE_FIELD |
| 28 | + |
| 29 | +static const uint16_t SIGNATURE = 0x4d42; |
| 30 | +static const uint32_t HEADER_INFO_SIZE = 40; |
| 31 | +static const uint16_t BITS_PER_PIXEL = 24; |
| 32 | + |
| 33 | +static inline uint32_t get_padding(uint32_t width) { return width % 4; } |
| 34 | + |
| 35 | +static struct header generate_header(const struct dimensions dims) { |
| 36 | + const uint32_t head_size = sizeof(struct header); |
| 37 | + const uint32_t img_size = sizeof(struct pixel) |
| 38 | + * dims.height * (dims.width + get_padding(dims.width)); |
| 39 | + const uint32_t file_size = head_size + img_size; |
| 40 | + return (struct header) { |
| 41 | + .bfType = SIGNATURE, |
| 42 | + .bfileSize = file_size, |
| 43 | + .bfReserved = 0, |
| 44 | + .bOffBits = head_size, |
| 45 | + .biSize = HEADER_INFO_SIZE, |
| 46 | + .biWidth = dims.width, |
| 47 | + .biHeight = dims.height, |
| 48 | + .biPlanes = 1, |
| 49 | + .biBitCount = BITS_PER_PIXEL, |
| 50 | + .biCompression = 0, |
| 51 | + .biSizeImage = img_size, |
| 52 | + .biXPelsPerMeter = 0, |
| 53 | + .biYPelsPerMeter = 0, |
| 54 | + .biClrUsed = 0, |
| 55 | + .biClrImportant = 0 |
| 56 | + }; |
| 57 | +} |
| 58 | + |
| 59 | +static enum read_status read_header(FILE *in, struct header *head) { |
| 60 | + enum read_status retval = READ_OK; |
| 61 | + if (fread(head, sizeof(struct header), 1, in) < 1 || |
| 62 | + head->biSize != HEADER_INFO_SIZE || head->biPlanes != 1 || |
| 63 | + head->biBitCount != BITS_PER_PIXEL || head->biCompression != 0) |
| 64 | + retval = READ_INVALID_HEADER; |
| 65 | + else if (head->bfType != SIGNATURE) |
| 66 | + retval = READ_INVALID_SIGNATURE; |
| 67 | + return retval; |
| 68 | +} |
| 69 | + |
| 70 | +static enum read_status read_image(FILE *in, struct image *img) { |
| 71 | + enum read_status retval = READ_OK; |
| 72 | + const struct dimensions dims = img->dims; |
| 73 | + for (uint32_t i = 0; i < dims.height; i++) { |
| 74 | + if (fread(&img->data[i * dims.width], sizeof(struct pixel), dims.width, in) |
| 75 | + < dims.width || fseek(in, get_padding(dims.width), SEEK_CUR) != 0) { |
| 76 | + retval = READ_INVALID_BITS; |
| 77 | + break; |
| 78 | + } |
| 79 | + } |
| 80 | + return retval; |
| 81 | +} |
| 82 | + |
| 83 | +enum read_status from_bmp(FILE *in, struct image *img) { |
| 84 | + enum read_status retval = READ_OK; |
| 85 | + /* read the header of bitmap file */ |
| 86 | + struct header head = {0}; |
| 87 | + if ((retval = read_header(in, &head)) != 0) |
| 88 | + return retval; |
| 89 | + /* set file pointer to data section of a bitmap file */ |
| 90 | + if (fseek(in, head.bOffBits, SEEK_SET) != 0) |
| 91 | + return READ_INVALID_BITS; |
| 92 | + /* read the image from the data section of a bitmap file */ |
| 93 | + *img = image_create(dimensions_create(head.biWidth, head.biHeight)); |
| 94 | + if ((retval = read_image(in, img)) != 0) |
| 95 | + image_destroy(img); |
| 96 | + return retval; |
| 97 | +} |
| 98 | + |
| 99 | +enum write_status to_bmp(FILE *out, struct image const *img) { |
| 100 | + enum write_status retval = WRITE_OK; |
| 101 | + /* write the header of bitmap file */ |
| 102 | + const struct header head = generate_header(img->dims); |
| 103 | + if (fwrite(&head, sizeof(struct header), 1, out) < 1) |
| 104 | + return WRITE_ERROR; |
| 105 | + /* write data section */ |
| 106 | + const struct dimensions dims = img->dims; |
| 107 | + for (uint32_t o = 0, i = 0; i < dims.height; i++) { |
| 108 | + if (fwrite(&img->data[i * dims.width], sizeof(struct pixel), dims.width, out) |
| 109 | + < dims.width || fwrite(&o, get_padding(dims.width), 1, out) < 1) { |
| 110 | + retval = WRITE_ERROR; |
| 111 | + break; |
| 112 | + } |
| 113 | + } |
| 114 | + return retval; |
| 115 | +} |
0 commit comments