-
Notifications
You must be signed in to change notification settings - Fork 0
/
shot.c
103 lines (95 loc) · 2.77 KB
/
shot.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* shot.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hgrissen <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/02/10 16:36:39 by hgrissen #+# #+# */
/* Updated: 2021/02/11 17:45:40 by hgrissen ### ########.fr */
/* */
/* ************************************************************************** */
#include "cub3d.h"
void header_init(void)
{
g_bmp.type[0] = 0x42;
g_bmp.type[1] = 0x4D;
g_bmp.filesize = (g_prm.w * g_prm.h * 4) + 54;
g_bmp.reserved1 = 0x00000000;
g_bmp.reserved2 = 0x00000000;
g_bmp.pxdataoff = 0x36;
g_bmp.headersize = 40;
g_bmp.width = g_prm.w;
g_bmp.height = g_prm.h * -1;
g_bmp.planes = 1;
g_bmp.bpp = 32;
g_bmp.compression = 0;
g_bmp.imagesize = g_prm.w * g_prm.h * 4;
g_bmp.xpermeter = 2835;
g_bmp.ypermeter = 2835;
g_bmp.totalcolors = 0;
g_bmp.importantcolors = 0;
}
void header_write(int fd)
{
int r;
r = 0;
r = write(fd, &g_bmp.type, 2);
r = write(fd, &g_bmp.filesize, 4);
r = write(fd, &g_bmp.reserved1, 2);
r = write(fd, &g_bmp.reserved2, 2);
r = write(fd, &g_bmp.pxdataoff, 4);
r = write(fd, &g_bmp.headersize, 4);
r = write(fd, &g_bmp.width, 4);
r = write(fd, &g_bmp.height, 4);
r = write(fd, &g_bmp.planes, 2);
r = write(fd, &g_bmp.bpp, 2);
r = write(fd, &g_bmp.compression, 4);
r = write(fd, &g_bmp.imagesize, 4);
r = write(fd, &g_bmp.xpermeter, 4);
r = write(fd, &g_bmp.ypermeter, 4);
r = write(fd, &g_bmp.totalcolors, 4);
r = write(fd, &g_bmp.importantcolors, 4);
(void)r;
}
void write_colors(int fd)
{
char *colors;
int i;
int j;
int *color;
i = 0;
j = 0;
color = (int*)g_img.addr;
if (!(colors = malloc(g_bmp.imagesize * sizeof(char))))
{
printf("Error:\nscreenshot issue");
exit(0);
}
while (i < (int)(g_bmp.imagesize / 4))
{
colors[j++] = color[i] & 255;
colors[j++] = (color[i] & 255 << 8) >> 8;
colors[j++] = (color[i] & 255 << 16) >> 16;
colors[j++] = 0;
i++;
}
j = write(fd, colors, g_bmp.imagesize);
free(colors);
}
void save_bmp(void)
{
int fd;
if (!((fd = open("screenshot.bmp", O_WRONLY | O_CREAT |
O_TRUNC, S_IRUSR | S_IWUSR)) > 0))
{
printf("Error:\nscreenshot problem");
exit(0);
}
ft_bzero(&g_bmp, sizeof(t_bmp_file));
header_init();
header_write(fd);
write_colors(fd);
close(fd);
(void)g_bmp;
}