Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/3rdparty/quirc/src/quirc.c
16337 views
1
/* quirc -- QR-code recognition library
2
* Copyright (C) 2010-2012 Daniel Beer <[email protected]>
3
*
4
* Permission to use, copy, modify, and/or distribute this software for any
5
* purpose with or without fee is hereby granted, provided that the above
6
* copyright notice and this permission notice appear in all copies.
7
*
8
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15
*/
16
17
#include <stdlib.h>
18
#include <string.h>
19
#include <quirc_internal.h>
20
21
const char *quirc_version(void)
22
{
23
return "1.0";
24
}
25
26
struct quirc *quirc_new(void)
27
{
28
struct quirc *q = malloc(sizeof(*q));
29
30
if (!q)
31
return NULL;
32
33
memset(q, 0, sizeof(*q));
34
return q;
35
}
36
37
void quirc_destroy(struct quirc *q)
38
{
39
free(q->image);
40
/* q->pixels may alias q->image when their type representation is of the
41
same size, so we need to be careful here to avoid a double free */
42
if (sizeof(*q->image) != sizeof(*q->pixels))
43
free(q->pixels);
44
free(q->row_average);
45
free(q);
46
}
47
48
int quirc_resize(struct quirc *q, int w, int h)
49
{
50
uint8_t *image = NULL;
51
quirc_pixel_t *pixels = NULL;
52
int *row_average = NULL;
53
54
/*
55
* XXX: w and h should be size_t (or at least unsigned) as negatives
56
* values would not make much sense. The downside is that it would break
57
* both the API and ABI. Thus, at the moment, let's just do a sanity
58
* check.
59
*/
60
if (w < 0 || h < 0)
61
goto fail;
62
63
/*
64
* alloc a new buffer for q->image. We avoid realloc(3) because we want
65
* on failure to be leave `q` in a consistant, unmodified state.
66
*/
67
image = calloc(w, h);
68
if (!image)
69
goto fail;
70
71
/* compute the "old" (i.e. currently allocated) and the "new"
72
(i.e. requested) image dimensions */
73
size_t olddim = q->w * q->h;
74
size_t newdim = w * h;
75
size_t min = (olddim < newdim ? olddim : newdim);
76
77
/*
78
* copy the data into the new buffer, avoiding (a) to read beyond the
79
* old buffer when the new size is greater and (b) to write beyond the
80
* new buffer when the new size is smaller, hence the min computation.
81
*/
82
(void)memcpy(image, q->image, min);
83
84
/* alloc a new buffer for q->pixels if needed */
85
if (sizeof(*q->image) != sizeof(*q->pixels)) {
86
pixels = calloc(newdim, sizeof(quirc_pixel_t));
87
if (!pixels)
88
goto fail;
89
}
90
91
/* alloc a new buffer for q->row_average */
92
row_average = calloc(w, sizeof(int));
93
if (!row_average)
94
goto fail;
95
96
/* alloc succeeded, update `q` with the new size and buffers */
97
q->w = w;
98
q->h = h;
99
free(q->image);
100
q->image = image;
101
if (sizeof(*q->image) != sizeof(*q->pixels)) {
102
free(q->pixels);
103
q->pixels = pixels;
104
}
105
free(q->row_average);
106
q->row_average = row_average;
107
108
return 0;
109
/* NOTREACHED */
110
fail:
111
free(image);
112
free(pixels);
113
free(row_average);
114
115
return -1;
116
}
117
118
int quirc_count(const struct quirc *q)
119
{
120
return q->num_grids;
121
}
122
123
static const char *const error_table[] = {
124
[QUIRC_SUCCESS] = "Success",
125
[QUIRC_ERROR_INVALID_GRID_SIZE] = "Invalid grid size",
126
[QUIRC_ERROR_INVALID_VERSION] = "Invalid version",
127
[QUIRC_ERROR_FORMAT_ECC] = "Format data ECC failure",
128
[QUIRC_ERROR_DATA_ECC] = "ECC failure",
129
[QUIRC_ERROR_UNKNOWN_DATA_TYPE] = "Unknown data type",
130
[QUIRC_ERROR_DATA_OVERFLOW] = "Data overflow",
131
[QUIRC_ERROR_DATA_UNDERFLOW] = "Data underflow"
132
};
133
134
const char *quirc_strerror(quirc_decode_error_t err)
135
{
136
if ((int)err < 8) { return error_table[err]; }
137
else { return "Unknown error"; }
138
}
139
140