summaryrefslogtreecommitdiffstats
path: root/src/main.c
blob: c1a5c4cb10b9b20d9e31e12cd53aff592be0d967 (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
/******************************************************************************
 *	Copyright (C) 2020	Alejandro Colomar Andrés		      *
 *	SPDX-License-Identifier:	GPL-2.0-only			      *
 ******************************************************************************/


/******************************************************************************
 ******* headers **************************************************************
 ******************************************************************************/
#include <stddef.h>
#include <stdio.h>

#define ALX_NO_PREFIX
#include <libalx/base/compiler.h>
#include <libalx/base/stdio.h>
#include <libalx/base/stdlib.h>
#include <libalx/extra/cv/cv.h>

#include "dbg.h"
#include "label.h"
#include "symbols.h"
#include "templates/base.h"
#include "templates/templates.h"


/******************************************************************************
 ******* macro ****************************************************************
 ******************************************************************************/
#define ENV_IMG_FNAME		"IMG_FNAME"


/******************************************************************************
 ******* enum / struct / union ************************************************
 ******************************************************************************/


/******************************************************************************
 ******* static functions (prototypes) ****************************************
 ******************************************************************************/
static
int	init	(img_s **restrict img);
static
void	deinit	(img_s *restrict img);


/******************************************************************************
 ******* main *****************************************************************
 ******************************************************************************/
int	main	(int argc, char *argv[])
{
	const char	*fname;
	img_s		*img;
	uint32_t	code;
	int		status;

	status	= 1;
	if (argc != 2)
		return	1;
	fname	= argv[1];
	status++;
	if (init(&img))
		goto err0;

	status++;
	if (load_templates())
		goto err;
	status++;
	if (alx_cv_imread(img, fname))
		goto err;
	status++;
	if (find_label(img))
		goto err;
	status++;
	if (find_symbols_vertically(img))
		goto err;
	status++;
	if (find_symbols_horizontally(img))
		goto err;
	status++;
	if (align_symbols(img))
		goto err;
	status++;
	if (extract_symbols(img))
		goto err;
	status++;
	for (ptrdiff_t i = 0; i < nsyms; i++) {
		code	= 0;
		if (clean_symbol(symbols[i]))
			goto err;
		if (match_t_base(symbols[i], &code, i))
			goto err;
		if (match_t_inner(symbols[i], &code) < 0)
			goto err;
		if (match_t_outer(symbols[i], &code) < 0)
			goto err;
		print_code(code);
	}

	alx_cv_imwrite(img, "/tmp/wash.png");

	deinit(img);
	return	0;
err:
	deinit(img);
err0:
	fprintf(stderr, "Error reading label\n");
	return	status;
}


/******************************************************************************
 ******* static functions (definitions) ***************************************
 ******************************************************************************/
static
int	init	(img_s **restrict img)
{

	if (alx_cv_init_img(img))
		return	-1;
	if (init_symbols())
		goto err0;
	if (init_templates())
		goto err1;
	if (DBG)
		alx_cv_named_window("dbg", ALX_CV_WINDOW_NORMAL);

	return	0;

err1:	deinit_symbols();
err0:	alx_cv_deinit_img(*img);
	return	-1;
}

static
void	deinit	(img_s *restrict img)
{

	if (DBG)
		alx_cv_destroy_all_windows();
	deinit_templates();
	deinit_symbols();
	alx_cv_deinit_img(img);
}


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