-
Notifications
You must be signed in to change notification settings - Fork 0
/
make_bmp.c
85 lines (76 loc) · 2.49 KB
/
make_bmp.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* make_bmp.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mehtel <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/03/23 20:10:10 by mehtel #+# #+# */
/* Updated: 2021/03/23 20:11:08 by mehtel ### ########.fr */
/* */
/* ************************************************************************** */
#include "cub3d.h"
void write_bytes_to_fd(int fd, int num, int len)
{
unsigned char byte;
int i;
i = -1;
while (++i < len)
{
byte = (unsigned char)(num >> (i * 8));
ft_putchar_fd(byte, fd);
}
}
void put_header(int fd, t_params params)
{
int filesize;
filesize = 54 + params.width * params.height * (params.bpp >> 3);
write(fd, "BM", 2);
write_bytes_to_fd(fd, filesize, 4);
write_bytes_to_fd(fd, 0, 4);
write_bytes_to_fd(fd, 54, 4);
write_bytes_to_fd(fd, 40, 4);
write_bytes_to_fd(fd, params.width, 4);
write_bytes_to_fd(fd, params.height, 4);
write_bytes_to_fd(fd, 1, 2);
write_bytes_to_fd(fd, params.bpp, 2);
write_bytes_to_fd(fd, 0, 4);
write_bytes_to_fd(fd, 0, 4);
write_bytes_to_fd(fd, 0, 4);
write_bytes_to_fd(fd, 0, 4);
write_bytes_to_fd(fd, 0, 4);
write_bytes_to_fd(fd, 0, 4);
}
void invert_img(char *img, char *inverted, t_params params)
{
int y;
int x;
int line_len;
line_len = params.width * (params.bpp >> 3);
y = -1;
while (++y < params.height)
{
x = -1;
while (++x < line_len)
*(inverted + (params.height - 1 - y) * line_len + x) =
*(img + y * line_len + x);
}
}
void create_bmp_file(char *img, char *filename,
int *size, int bpp)
{
int fd;
char *inverted;
t_params params;
if (!(inverted = ft_calloc(sizeof(char), size[X] * size[Y] * (bpp >> 3))))
return ;
params.bpp = bpp;
params.height = size[X];
params.width = size[Y];
fd = open(filename, O_CREAT | O_WRONLY | O_TRUNC, 0777);
put_header(fd, params);
invert_img(img, inverted, params);
write(fd, inverted, params.height * params.width * (bpp >> 3));
free(inverted);
close(fd);
}