aboutsummaryrefslogtreecommitdiffstats
path: root/src/ft_save_to_bmp.c
blob: 24d28d8b7318188d0801e1806005db094b638dbc (plain)
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
/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   ft_save_to_bmp.c                                   :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: rbousset <marvin@42.fr>                    +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2020/02/29 20:50:35 by rbousset          #+#    #+#             */
/*   Updated: 2020/02/29 20:50:37 by rbousset         ###   ########lyon.fr   */
/*                                                                            */
/* ************************************************************************** */

#include <libft.h>
#include <cub3d.h>
#include <stddef.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdint.h>

static char
	*ft_set_file_name(t_map *ml)
{
	char	*str;

	if (ft_strrchr(ml->filename, '/'))
	{
		if (!(str = (char*)malloc((ft_strlen(FT_BMP_SAVE_DIR) +
								ft_strlen(ft_strrchr(ml->filename, '/') + 1)
								+ 1) * sizeof(char))))
			return (NULL);
		ft_sprintf(str, "%s%s", FT_BMP_SAVE_DIR,
					ft_strrchr(ml->filename, '/') + 1);
	}
	else
	{
		if (!(str = (char*)malloc((ft_strlen(FT_BMP_SAVE_DIR) +
				ft_strlen(ml->filename) + 1) * sizeof(char))))
			return (NULL);
		ft_sprintf(str, "%s%s", FT_BMP_SAVE_DIR, ml->filename);
	}
	ft_memcpy(ft_strrchr(str, '.') + 1, "bmp", 3);
	return (str);
}

static uint8_t
	*ft_convert_image_to_bmp(t_img img, t_cub *cl)
{
	uint8_t		*bmp;
	t_bmp_rgb	px;
	uint32_t	y;
	uint32_t	x;
	uint32_t	i;

	(void)img;
	if (!(bmp = (uint8_t*)ft_calloc(3 * cl->wlist.x_size
		* cl->wlist.y_size, sizeof(uint8_t))))
		return (NULL);
	y = cl->wlist.y_size;
	i = 0;
	while (--y > 0)
	{
		x = -1;
		while (++x < cl->wlist.x_size)
		{
			px = ft_hex_to_rgb(*(int*)(cl->img.ptr
				+ (x * 4 + (y * cl->img.sizeline))));
			*(bmp + i++) = (uint8_t)px.b;
			*(bmp + i++) = (uint8_t)px.g;
			*(bmp + i++) = (uint8_t)px.r;
		}
	}
	return (bmp);
}

static int8_t
	ft_write_bmp(const char *fname, uint32_t h, uint32_t w, uint8_t *bmp)
{
	t_bmp_file		bmp_file;
	t_bmp_info		bmp_info;
	uint32_t		size;
	int				fd;

	size = w * h * 3;
	bmp_file = ft_init_bmp();
	bmp_info = ft_init_bmp_info();
	bmp_file.file_size = size + sizeof(t_bmp_file) + sizeof(t_bmp_info);
	bmp_file.offset_data = bmp_file.file_size - size;
	bmp_info.size = 40;
	bmp_info.width = w;
	bmp_info.height = h;
	bmp_info.bit_count = 24;
	bmp_info.size_image = size;
	if (!(fd = open(fname, O_RDWR | O_CREAT, 0644)))
		return (-1);
	if (!write(fd, &bmp_file, sizeof(t_bmp_file)) ||
		!write(fd, &bmp_info, sizeof(t_bmp_info)) ||
		!write(fd, bmp, size))
		return (-1);
	close(fd);
	return (0);
}

int8_t
	ft_save_to_bmp(t_cub *cl)
{
	uint8_t			*bmp;
	char			*fname;

	if (!(fname = ft_set_file_name(&cl->mlist)))
		return (-1);
	if (!(bmp = ft_convert_image_to_bmp(cl->img, cl)))
	{
		ft_memdel((void*)&fname);
		return (-1);
	}
	if (ft_write_bmp(fname, cl->wlist.y_size, cl->wlist.x_size, bmp) < 0)
	{
		ft_memdel((void*)&bmp);
		ft_memdel((void*)&fname);
		return (-1);
	}
	ft_memdel((void*)&bmp);
	ft_memdel((void*)&fname);
	return (0);
}