16
#include <fs/file_format.h>
22
#define PALLETE_SIZE 256
28
#define FIND_SKIP(a) ( ( 4 - ( (a) % 4 ) ) & 0x03)
30
void bmp_unload(struct bmp *bmp_data) {
31
free(bmp_data->image);
43
uint32_t biCompression;
45
uint32_t biXPelsPerMeter;
46
uint32_t biYPelsPerMeter;
48
uint32_t biClrImportant;
57
static void read_pallete(int fd, struct color pallete[], int colors) {
61
lseek(fd, HEADER_SIZE - 1, SEEK_SET);
62
for (i = 0; i < colors; i++) {
64
pallete[i].red = tmp[1];
65
pallete[i].green = tmp[2];
66
pallete[i].blue = tmp[3];
70
int bmp_load(char *file_name, struct bmp *bmp_data) {
71
int j, i , k, tmp, skip, pos = 0;
73
struct color pallete[PALLETE_SIZE];
74
struct bmp_header bmp_head;
75
uint8_t signature[2], buff[4];
77
int fd = open(file_name, O_RDONLY);
79
printf("Error : can't open file.\n");
83
if (read(fd, signature, sizeof(signature)) != sizeof(signature)) {
84
printf("Error : can't read signature of file.\n");
89
if (raw_get_file_format(signature) != BMP_FILE) {
90
printf("Error : it's not BMP file\n");
95
if (read(fd, &bmp_head, sizeof(struct bmp_header)) != sizeof(struct bmp_header)) {
96
printf("Error : can't read header of bmp file\n");
101
bmp_data->bits_per_pixel = bmp_head.biBitCount;
102
bmp_data->width = (width = bmp_head.biWidth);
103
bmp_data->height = (height = bmp_head.biHeight);
104
bmp_data->image_size = width * height * 3;
106
bmp_data->image = (uint8_t*)malloc(bmp_data->image_size * sizeof(uint8_t));
108
switch (bmp_head.biBitCount) {
110
lseek(fd, bmp_head.bfOffBits, SEEK_SET);
111
for (i = 0; i < height; i++) {
112
for (j = 0; j < width; j++) {
114
bmp_data->image[pos] = buff[2];
115
bmp_data->image[pos + 1] = buff[1];
116
bmp_data->image[pos + 2] = buff[0];
123
lseek(fd, bmp_head.bfOffBits, SEEK_SET);
124
skip = FIND_SKIP(width * 3);
125
for (i = 0; i < height; i++) {
126
for (j = 0; j < width; j++) {
128
bmp_data->image[pos] = buff[2];
129
bmp_data->image[pos + 1] = buff[1];
130
bmp_data->image[pos + 2] = buff[0];
134
read(fd, buff, skip);
141
printf("Error : 16bit bmp not supported\n");
146
read_pallete(fd, pallete, (bmp_head.bfOffBits - HEADER_SIZE) / COLOR_SIZE);
147
lseek(fd, bmp_head.bfOffBits, SEEK_SET);
148
skip = FIND_SKIP(width);
149
for (i = 0; i < height; i++) {
150
for (j = 0; j < width; j++) {
152
bmp_data->image[pos] = pallete[buff[0]].blue;
153
bmp_data->image[pos + 1] = pallete[buff[0]].green;
154
bmp_data->image[pos + 2] = pallete[buff[0]].red;
158
read(fd, buff, skip);
164
read_pallete(fd, pallete, (bmp_head.bfOffBits - HEADER_SIZE) / COLOR_SIZE);
165
lseek(fd, bmp_head.bfOffBits, SEEK_SET);
166
skip = FIND_SKIP(width / 2 + width % 2);
167
for (i = 0; i < height; i++) {
168
for (j = 0; j < width / 2; j++) {
170
buff[1] = buff[0]>>4;
171
buff[2] = buff[0] & 0x0f;
172
bmp_data->image[pos + 5] = pallete[buff[1]].red;
173
bmp_data->image[pos + 4] = pallete[buff[1]].green;
174
bmp_data->image[pos + 3] = pallete[buff[1]].blue;
175
bmp_data->image[pos + 2] = pallete[buff[2]].red;
176
bmp_data->image[pos + 1] = pallete[buff[2]].green;
177
bmp_data->image[pos] = pallete[buff[2]].blue;
182
buff[1] = buff[0]>>4;
183
bmp_data->image[pos + 2] = pallete[buff[1]].red;
184
bmp_data->image[pos + 1] = pallete[buff[1]].green;
185
bmp_data->image[pos] = pallete[buff[1]].blue;
189
read(fd, buff, skip);
195
lseek(fd, bmp_head.bfOffBits, SEEK_SET);
196
skip = FIND_SKIP(width / 8 + (width % 8 ? 1 : 0));
197
for (i = 0; i < height; i++) {
198
for (j = 0; j < width / 8; j++) {
200
for (k = 0; k < 8; k++) {
201
if (buff[0] & 0x80) {
202
bmp_data->image[pos] = 255;
203
bmp_data->image[pos + 1] = 255;
204
bmp_data->image[pos + 2] = 255;
205
bmp_data->image[pos + 3] = 255;
208
bmp_data->image[pos] = 0;
209
bmp_data->image[pos + 1] = 0;
210
bmp_data->image[pos + 2] = 0;
213
buff[0] = buff[0]<<1;
219
for (k = 0; k < width % 8; k++) {
220
if (buff[0] & 0x80) {
221
bmp_data->image[pos] = 255;
222
bmp_data->image[pos + 1] = 255;
223
bmp_data->image[pos + 2] = 255;
226
bmp_data->image[pos] = 0;
227
bmp_data->image[pos + 1] = 0;
228
bmp_data->image[pos + 2] = 0;
231
buff[0] = buff[0]<<1;
236
read(fd, buff, skip);
242
printf("invalid bitcount\n");
248
for (i = 0; i < height / 2; i++) {
249
for (j = 0; j < width * 3; j++) {
250
tmp = bmp_data->image[width * 3 * (height - 1 - i) + j];
251
bmp_data->image[width * 3 * (height - 1 - i) + j] = bmp_data->image[width * 3 * i + j];
252
bmp_data->image[width * 3 * i + j] = tmp;