-
Notifications
You must be signed in to change notification settings - Fork 0
/
d_img.c
420 lines (351 loc) · 14 KB
/
d_img.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
#define NO_DEBUG_LOG
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <assert.h>
#include <math.h>
#include "d_img.h"
#include "d_memory.h"
#include "d_logger.h"
struct bitmap_header {
char type[2];
int32_t file_size;
int16_t _reserved1;
int16_t _reserved2;
int32_t data_offset;
};
struct bitmap_info_header {
int32_t header_size;
int32_t width;
int32_t height;
int16_t nb_color_planes;
int16_t bits_per_pixel;
int32_t compression_method;
int32_t raw_bitmap_data_size;
int32_t horizontal_pixels_per_meter;
int32_t vertical_pixels_per_meter;
int32_t nb_colors_in_palette;
int32_t nb_important_colors;
/* V2 */
uint32_t R_mask;
uint32_t G_mask;
uint32_t B_mask;
/* V3 */
uint32_t A_mask;
};
enum {
BI_RGB = 0,
BI_BITFIELD = 3,
BI_ALPHABITFIELDS = 6
};
DImg* d_img_new_image(int width, int height, int color_format) {
DImg* image = d_malloc(sizeof (DImg));
image->width = width;
image->height = height;
image->color_format = color_format;
image->pitch = ((width * color_format + 3) / 4) * 4 ;
image->pixels = d_malloc(image->pitch * height * color_format * sizeof (char) );
return image;
}
void d_img_free(DImg* image) {
if (image->pixels) free(image->pixels);
free(image);
}
DImg* d_img_load_from_bmp_file(char* filepath, DError** error) {
DImg* new_image = NULL;
char* conversion_buffer = NULL;
FILE* fd = fopen(filepath, "rb");
if (fd == NULL) {
if (error != NULL)
*error = DERROR(strerror(errno));
goto error;
}
struct bitmap_header bh;
int fread_count = fread(&bh, sizeof (struct bitmap_header), 1, fd);
if (fread_count != 1) {
if (error != NULL)
*error = DERROR("IO error while reading bitmap header");
goto error;
}
if (!(bh.type[0] == 'B' && bh.type[1] == 'M')) {
if (error != NULL)
*error = DERROR("Unsuported bitmap type ( Should be windows type )");
goto error;
}
struct bitmap_info_header bih;
fseek(fd, 14, SEEK_SET);
fread_count = fread(&bih, sizeof (struct bitmap_info_header), 1, fd);
if (fread_count != 1) {
if (error != NULL)
*error = DERROR("IO error while reading bitmap info header");
goto error;
}
if (bih.compression_method != BI_RGB && bih.compression_method != BI_BITFIELD && bih.compression_method != BI_ALPHABITFIELDS) {
if (error != NULL)
*error = DERROR("Unsuported bitmap compression method");
goto error;
}
if (bih.nb_colors_in_palette != 0) {
if (error != NULL)
*error = DERROR("bitmap color palette unsupported");
goto error;
}
DLOGD("BIH header size : %d", bih.header_size);
DLOGD("Compresion method : %d", bih.compression_method);
DLOGD("Application codes : %d,%d", bh._reserved1, bh._reserved2);
DLOGD("bits per pixel : %d", bih.bits_per_pixel);
DLOGD("number important color : %d", bih.nb_important_colors);
DLOGD("Number of colors in palette %d", bih.nb_colors_in_palette);
DLOGD("R_Mask %08x", bih.R_mask);
DLOGD("G_Mask %08x", bih.G_mask);
DLOGD("B_Mask %08x", bih.B_mask);
DLOGD("A_Mask %08x", bih.A_mask);
if (bih.compression_method == BI_RGB) {
if (bih.bits_per_pixel == 32) {
bih.R_mask = 0xFF000000;
bih.G_mask = 0x00FF0000;
bih.B_mask = 0x0000FF00;
bih.A_mask = 0x000000FF;
} else if (bih.bits_per_pixel == 24) {
bih.R_mask = 0x00FF0000;
bih.G_mask = 0x0000FF00;
bih.B_mask = 0x000000FF;
bih.A_mask = 0;
} else if (bih.bits_per_pixel == 16) {
if (error != NULL)
*error = DERROR("Bitmap file invalid : compression method is BI_RGB but masks must be defined for 16bits");
goto error;
}
}
DLOGD("R_Mask %08x", bih.R_mask);
DLOGD("G_Mask %08x", bih.G_mask);
DLOGD("B_Mask %08x", bih.B_mask);
DLOGD("A_Mask %08x", bih.A_mask);
int R_mask_bit_shift = 0;
int R_bit_depth = 0;
float R_scale_factor = 1;
int G_mask_bit_shift = 0;
int G_bit_depth = 0;
float G_scale_factor = 1;
int B_mask_bit_shift = 0;
int B_bit_depth = 0;
float B_scale_factor = 1;
int A_mask_bit_shift = 0;
int A_bit_depth = 0;
float A_scale_factor = 1;
/* Determine bit shifth and bit depth for each color */
int k = 0;
for (; k < 31; k++) {
if ((bih.R_mask & 1 << k) != 0) {
R_mask_bit_shift = k;
break;
}
}
if (k == 31)
R_bit_depth = 0;
else
for (; k < 33; k++) {
if ((bih.R_mask & 1 << k) == 0) {
R_bit_depth = k - R_mask_bit_shift;
break;
}
}
R_scale_factor = 255.0f / (pow(2, R_bit_depth) - 1);
k = 0;
for (; k < 31; k++) {
if ((bih.G_mask & 1 << k) != 0) {
G_mask_bit_shift = k;
break;
}
}
if (k == 31)
G_bit_depth = 0;
else
for (; k < 33; k++) {
if ((bih.G_mask & 1 << k) == 0) {
G_bit_depth = k - G_mask_bit_shift;
break;
}
}
G_scale_factor = 255.0f / (pow(2, G_bit_depth) - 1);
k = 0;
for (; k < 31; k++) {
if ((bih.B_mask & 1 << k) != 0) {
B_mask_bit_shift = k;
break;
}
}
if (k == 31)
B_bit_depth = 0;
else
for (; k < 33; k++) {
if ((bih.B_mask & 1 << k) == 0) {
B_bit_depth = k - B_mask_bit_shift;
break;
}
}
B_scale_factor = 255.0f / (pow(2, B_bit_depth) - 1);
k = 0;
for (; k < 31; k++) {
if ((bih.A_mask & 1 << k) != 0) {
A_mask_bit_shift = k;
break;
}
}
if (k == 31)
A_bit_depth = 0;
else
for (; k < 33; k++) {
if ((bih.A_mask & 1 << k) == 0) {
A_bit_depth = k - A_mask_bit_shift;
break;
}
}
A_scale_factor = 255.0f / (pow(2, A_bit_depth) - 1);
DLOGD("Red bit shift : %d; bit depth %d", R_mask_bit_shift, R_bit_depth);
DLOGD("Green bit shift : %d; bit depth %d", G_mask_bit_shift, G_bit_depth);
DLOGD("Blue bit shift : %d; bit depth %d", B_mask_bit_shift, B_bit_depth);
DLOGD("Alpha bit shift : %d; bit depth %d", A_mask_bit_shift, A_bit_depth);
/* Load pixels data*/
int data_offset = 14 + bih.header_size;
fseek(fd, data_offset, SEEK_SET);
if (bih.bits_per_pixel == 32) {
new_image = d_img_new_image(bih.width, bih.height, DIMG_COLOR_FORMAT_RGBA);
conversion_buffer = d_malloc(new_image->pitch * sizeof (char));
for (int h = 0; h < new_image->height; h++) {
if (fread(conversion_buffer, new_image->pitch * sizeof (char), 1, fd) == 0) {
if (error != NULL) {
*error = DERROR("IO Error while reading pixels data");
goto error;
}
}
for (int w = 0; w < new_image->width; w++) {
int32_t* pixel = (int32_t*) (conversion_buffer + 4 * w);
PIXEL_R(new_image, w, new_image->height - 1 - h) = ((*pixel & bih.R_mask) >> R_mask_bit_shift) * R_scale_factor;
PIXEL_G(new_image, w, new_image->height - 1 - h) = ((*pixel & bih.G_mask) >> G_mask_bit_shift) * G_scale_factor;
PIXEL_B(new_image, w, new_image->height - 1 - h) = ((*pixel & bih.B_mask) >> B_mask_bit_shift) * B_scale_factor;
PIXEL_A(new_image, w, new_image->height - 1 - h) = ((*pixel & bih.A_mask) >> A_mask_bit_shift) * A_scale_factor;
}
}
} else if (bih.bits_per_pixel == 24) {
new_image = d_img_new_image(bih.width, bih.height, DIMG_COLOR_FORMAT_RGB);
conversion_buffer = d_malloc(new_image->pitch * sizeof (char));
for (int h = 0; h < new_image->height; h++) {
if (fread(conversion_buffer, new_image->pitch * sizeof (char), 1, fd) == 0) {
if (error != NULL) {
*error = DERROR("IO Error while reading pixels data");
goto error;
}
}
for (int w = 0; w < new_image->width; w++) {
int32_t* pixel = (int32_t*) (conversion_buffer + 3 * w);
PIXEL_R(new_image, w, new_image->height - 1 - h) = ((*pixel & bih.R_mask) >> R_mask_bit_shift) * R_scale_factor;
PIXEL_G(new_image, w, new_image->height - 1 - h) = ((*pixel & bih.G_mask) >> G_mask_bit_shift) * G_scale_factor;
PIXEL_B(new_image, w, new_image->height - 1 - h) = ((*pixel & bih.B_mask) >> B_mask_bit_shift) * B_scale_factor;
}
}
} else if (bih.bits_per_pixel == 16) {
if (A_bit_depth > 0) {
new_image = d_img_new_image(bih.width, bih.height, DIMG_COLOR_FORMAT_RGBA);
} else {
new_image = d_img_new_image(bih.width, bih.height, DIMG_COLOR_FORMAT_RGB);
}
int pitch = ((new_image->width * 2 + 3) / 4) * 4;
conversion_buffer = d_malloc(pitch * sizeof (char));
for (int h = 0; h < new_image->height; h++) {
if (fread(conversion_buffer, pitch * sizeof (char), 1, fd) == 0) {
if (error != NULL) {
*error = DERROR("IO Error while reading pixels data");
goto error;
}
}
for (int w = 0; w < new_image->width; w++) {
PIXEL_R(new_image, w, new_image->height - 1 - h) = ((*(((int16_t*) conversion_buffer) + w) & bih.R_mask) >> R_mask_bit_shift) * R_scale_factor;
PIXEL_G(new_image, w, new_image->height - 1 - h) = ((*(((int16_t*) conversion_buffer) + w) & bih.G_mask) >> G_mask_bit_shift) * G_scale_factor;
PIXEL_B(new_image, w, new_image->height - 1 - h) = ((*(((int16_t*) conversion_buffer) + w) & bih.B_mask) >> B_mask_bit_shift) * B_scale_factor;
if (A_bit_depth > 0)
PIXEL_A(new_image, w, new_image->height - 1 - h) = ((*(((int16_t*) conversion_buffer) + w) & bih.A_mask) >> A_mask_bit_shift) * A_scale_factor;
}
}
} else {
if (error != NULL)
*error = DERROR("Unsuported bits depth");
goto error;
}
if ( fd ) fclose(fd);
if (conversion_buffer) free(conversion_buffer);
return new_image;
error:
if ( fd ) fclose(fd);
if (conversion_buffer) free(conversion_buffer);
if (new_image) d_img_free(new_image);
return NULL;
}
enum {
RED,
GREEN,
BLUE,
ALPHA
};
static inline unsigned char get_pixel_clamped(DImg* img, int color, int x, int y) {
if (x > 0)
if (x < img->width)
if (y > 0)
if (y < img->height)
return img->pixels[x * img->color_format + y * img->pitch + color];
else
return img->pixels[x * img->color_format + (img->height - 1) * img->pitch + color];
else
return img->pixels[x * img->color_format + 0 * img->pitch + color];
else
if (y > 0)
if (y < img->height)
return img->pixels[(img->width - 1) * img->color_format + y * img->pitch + color];
else
return img->pixels[(img->width - 1) * img->color_format + (img->height - 1) * img->pitch + color];
else
return img->pixels[(img->width - 1) * img->color_format + 0 * img->pitch + color];
else
if (y > 0)
if (y < img->height)
return img->pixels[0 * img->color_format + y * img->pitch + color];
else
return img->pixels[0 * img->color_format + (img->height - 1) * img->pitch + color];
else
return img->pixels[color];
}
static inline unsigned char get_interpolated_pixel_bilinear(DImg* img, int color, float x, float y) {
int rounded_x_1 = x;
int rounded_x_2 = x + 1;
int rounded_y_1 = y;
int rounded_y_2 = y + 1;
float ex = x - rounded_x_1;
float ey = y - rounded_y_1;
float interpol_x_1 = (1 - ex) * get_pixel_clamped(img, color, rounded_x_1, rounded_y_1) + (ex) * get_pixel_clamped(img, color, rounded_x_2, rounded_y_1);
float interpol_x_2 = (1 - ex) * get_pixel_clamped(img, color, rounded_x_1, rounded_y_2) + (ex) * get_pixel_clamped(img, color, rounded_x_2, rounded_y_2);
return ( 1.0f - ey) * interpol_x_1 + (ey) * interpol_x_2;
}
DImg* d_img_resize(DImg* img, int width, int height) {
DImg* new_image = d_img_new_image(width, height, img->color_format);
float scale_factor_x = (float) img->width / (float) width;
float scale_factor_y = (float) img->height / (float) height;
if (img->color_format == DIMG_COLOR_FORMAT_RGBA) {
for (int j = 0; j < height; j++)
for (int i = 0; i < width; i++) {
PIXEL_R(new_image, i, j) = get_interpolated_pixel_bilinear(img, RED, (float) i*scale_factor_x, (float) j * scale_factor_y);
PIXEL_G(new_image, i, j) = get_interpolated_pixel_bilinear(img, GREEN, (float) i*scale_factor_x, (float) j * scale_factor_y);
PIXEL_B(new_image, i, j) = get_interpolated_pixel_bilinear(img, BLUE, (float) i*scale_factor_x, (float) j * scale_factor_y);
PIXEL_A(new_image, i, j) = get_interpolated_pixel_bilinear(img, ALPHA, (float) i*scale_factor_x, (float) j * scale_factor_y);
}
} else {
for (int j = 0; j < height; j++)
for (int i = 0; i < width; i++) {
PIXEL_R(new_image, i, j) = get_interpolated_pixel_bilinear(img, RED, (float) i*scale_factor_x, (float) j * scale_factor_y);
PIXEL_G(new_image, i, j) = get_interpolated_pixel_bilinear(img, GREEN, (float) i*scale_factor_x, (float) j * scale_factor_y);
PIXEL_B(new_image, i, j) = get_interpolated_pixel_bilinear(img, BLUE, (float) i*scale_factor_x, (float) j * scale_factor_y);
}
}
return new_image;
}