summaryrefslogtreecommitdiffstats
path: root/src/save/save.cpp
blob: 9288243e4022bf92364405d534008c67abf17c1e (plain) (blame)
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
/******************************************************************************
 *	Copyright (C) 2018	Alejandro Colomar Andrés		      *
 *	SPDX-License-Identifier:	GPL-2.0-only			      *
 ******************************************************************************/


/******************************************************************************
 ******* headers **************************************************************
 ******************************************************************************/
#include "vision-artificial/save/save.hpp"

#include <cerrno>
#include <cstdio>
#include <cstdlib>
#include <cstring>

#include <sys/stat.h>

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>

#define ALX_NO_PREFIX
#include <libalx/base/compiler.hxx>
#include <libalx/base/errno.hxx>
#include <libalx/base/stdio.hxx>
#include <libalx/extra/cv/cv.hxx>

#include "vision-artificial/user/iface.hpp"


/******************************************************************************
 ******* macros ***************************************************************
 ******************************************************************************/


/******************************************************************************
 ******* enums ****************************************************************
 ******************************************************************************/


/******************************************************************************
 ******* structs / unions *****************************************************
 ******************************************************************************/


/******************************************************************************
 ******* variables ************************************************************
 ******************************************************************************/
class cv::Mat	image;
char		home_path[FILENAME_MAX];
char		user_prog_path[FILENAME_MAX];
char		saved_path[FILENAME_MAX];
char		saved_name[FILENAME_MAX];


/******************************************************************************
 ******* static functions (prototypes) ****************************************
 ******************************************************************************/


/******************************************************************************
 ******* global functions *****************************************************
 ******************************************************************************/
void	save_init	(void)
{

	if (sbprintf(home_path, NULL, "%s/", getenv(ENV_HOME)))
		goto err;
	if (sbprintf(user_prog_path, NULL, "%s/%s/",
					home_path, USER_PROG_DIR))
		goto err;
	if (sbprintf(saved_path, NULL, "%s/%s/", home_path, USER_SAVED_DIR))
		goto err;
	saved_name[0]	= '\0';

	if (mkdir(user_prog_path, 0700)) {
		if (errno != EEXIST)
			errorx(EXIT_FAILURE, user_prog_path);
	}
	mkdir(saved_path, 0700);

	return;
err:
	errorx(EXIT_FAILURE, "Path is too large and has been truncated\n");
}

void	save_reset_fpath(void)
{

	if (sbprintf(saved_path, NULL, "%s/%s/", home_path, USER_SAVED_DIR))
		goto err;
	return;
err:
	printf("Path is too large and has been truncated\n");
}

void	load_image_file	(const char *restrict fpath,
			 const char *restrict fname)
{
	char	file_path[FILENAME_MAX];
	char	file_name[FILENAME_MAX];

	image.release();

	/* Set file_path */
	if (!fpath) {
		/* Default path */
		save_reset_fpath();
		ALX_UNUSED(sbprintf(file_path, NULL, "%s", saved_path));
	} else {
		ALX_UNUSED(sbprintf(file_path, NULL, "%s", fpath));
	}

	/* Set file_name */
	if (!fname)
		user_iface_fname(file_path, saved_name);
	else
		ALX_UNUSED(sbprintf(saved_name, NULL, "%s", fname));

	/* File name */
	if (sbprintf(file_name, NULL, "%s/%s", file_path, saved_name))
		goto err_path;

	alx::CV::imread(&image, file_name);
	if (image.empty())
		goto err_img;

	return;
err_img:
	printf("Could not load file: %s\n", file_name);
	return;
err_path:
	printf("Path is too large and has been truncated\n");
}

void	save_cleanup	(void)
{

	image.release();
}

void	save_image_file	(const char *restrict fpath,
			 const char *restrict save_as)
{
	char	file_path[FILENAME_MAX];
	char	file_name[FILENAME_MAX];
	FILE	*fp;

	/* Set file_path */
	if (!fpath) {
		/* Default path */
		save_reset_fpath();
		ALX_UNUSED(sbprintf(file_path, NULL, "%s", saved_path));
	} else {
		ALX_UNUSED(sbprintf(file_path, NULL, "%s", fpath));
	}

	/* Set file_name */
	if (!save_as) {
		ALX_UNUSED(sbprintf(saved_name,NULL, "%s", SAVED_NAME_DEFAULT));
		user_iface_fname(saved_path, saved_name);
	} else {
		ALX_UNUSED(sbprintf(saved_name, NULL, "%s", save_as));
	}

	/* Prepend the path */
	if (sbprintf(file_name, NULL, "%s/%s", file_path, saved_name))
		goto err_path;

	fp =	fopen(file_name, "r");
	if (fp) {
		/* Name in use;  ask once more */
		fclose(fp);
		user_iface_fname(saved_path, saved_name);
		if (sbprintf(file_name,NULL,"%s/%s", file_path, saved_name))
			goto err_path;
	}

	user_iface_log_write(2, saved_name);

	cv::imwrite(file_name, image);

	return;
err_path:
	printf("Path is too large and has been truncated\n");
}


/******************************************************************************
 ******* static functions (definitions) ***************************************
 ******************************************************************************/


/******************************************************************************
 ******* end of file **********************************************************
 ******************************************************************************/