forked from dmtx/libdmtx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dmtxscangrid.c
178 lines (146 loc) · 4.51 KB
/
dmtxscangrid.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
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
/**
* libdmtx - Data Matrix Encoding/Decoding Library
* Copyright 2008, 2009 Mike Laughton. All rights reserved.
* Copyright 2012-2016 Vadim A. Misbakh-Soloviov. All rights reserved.
*
* See LICENSE file in the main project directory for full
* terms of use and distribution.
*
* Contact:
* Vadim A. Misbakh-Soloviov <[email protected]>
* Mike Laughton <[email protected]>
*
* \file dmtxscangrid.c
* \brief Scan grid tracking
*/
/**
* \brief Initialize scan grid pattern
* \param dec
* \return Initialized grid
*/
static DmtxScanGrid
InitScanGrid(DmtxDecode *dec)
{
int scale, smallestFeature;
int xExtent, yExtent, maxExtent;
int extent;
DmtxScanGrid grid;
memset(&grid, 0x00, sizeof(DmtxScanGrid));
scale = dmtxDecodeGetProp(dec, DmtxPropScale);
smallestFeature = dmtxDecodeGetProp(dec, DmtxPropScanGap) / scale;
grid.xMin = dmtxDecodeGetProp(dec, DmtxPropXmin);
grid.xMax = dmtxDecodeGetProp(dec, DmtxPropXmax);
grid.yMin = dmtxDecodeGetProp(dec, DmtxPropYmin);
grid.yMax = dmtxDecodeGetProp(dec, DmtxPropYmax);
/* Values that get set once */
xExtent = grid.xMax - grid.xMin;
yExtent = grid.yMax - grid.yMin;
maxExtent = (xExtent > yExtent) ? xExtent : yExtent;
assert(maxExtent > 1);
for(extent = 1; extent < maxExtent; extent = ((extent + 1) * 2) - 1)
if(extent <= smallestFeature)
grid.minExtent = extent;
grid.maxExtent = extent;
grid.xOffset = (grid.xMin + grid.xMax - grid.maxExtent) / 2;
grid.yOffset = (grid.yMin + grid.yMax - grid.maxExtent) / 2;
/* Values that get reset for every level */
grid.total = 1;
grid.extent = grid.maxExtent;
SetDerivedFields(&grid);
return grid;
}
/**
* \brief Return the next good location (which may be the current location),
* and advance grid progress one position beyond that. If no good
* locations remain then return DmtxRangeEnd.
* \param grid
* \return void
*/
static int
PopGridLocation(DmtxScanGrid *grid, DmtxPixelLoc *locPtr)
{
int locStatus;
do {
locStatus = GetGridCoordinates(grid, locPtr);
/* Always leave grid pointing at next available location */
grid->pixelCount++;
} while(locStatus == DmtxRangeBad);
return locStatus;
}
/**
* \brief Extract current grid position in pixel coordinates and return
* whether location is good, bad, or end
* \param grid
* \return Pixel location
*/
static int
GetGridCoordinates(DmtxScanGrid *grid, DmtxPixelLoc *locPtr)
{
int count, half, quarter;
DmtxPixelLoc loc;
/* Initially pixelCount may fall beyond acceptable limits. Update grid
* state before testing coordinates */
/* Jump to next cross pattern horizontally if current column is done */
if(grid->pixelCount >= grid->pixelTotal) {
grid->pixelCount = 0;
grid->xCenter += grid->jumpSize;
}
/* Jump to next cross pattern vertically if current row is done */
if(grid->xCenter > grid->maxExtent) {
grid->xCenter = grid->startPos;
grid->yCenter += grid->jumpSize;
}
/* Increment level when vertical step goes too far */
if(grid->yCenter > grid->maxExtent) {
grid->total *= 4;
grid->extent /= 2;
SetDerivedFields(grid);
}
if(grid->extent == 0 || grid->extent < grid->minExtent) {
locPtr->X = locPtr->Y = -1;
return DmtxRangeEnd;
}
count = grid->pixelCount;
assert(count < grid->pixelTotal);
if(count == grid->pixelTotal - 1) {
/* center pixel */
loc.X = grid->xCenter;
loc.Y = grid->yCenter;
}
else {
half = grid->pixelTotal / 2;
quarter = half / 2;
/* horizontal portion */
if(count < half) {
loc.X = grid->xCenter + ((count < quarter) ? (count - quarter) : (half - count));
loc.Y = grid->yCenter;
}
/* vertical portion */
else {
count -= half;
loc.X = grid->xCenter;
loc.Y = grid->yCenter + ((count < quarter) ? (count - quarter) : (half - count));
}
}
loc.X += grid->xOffset;
loc.Y += grid->yOffset;
*locPtr = loc;
if(loc.X < grid->xMin || loc.X > grid->xMax ||
loc.Y < grid->yMin || loc.Y > grid->yMax)
return DmtxRangeBad;
return DmtxRangeGood;
}
/**
* \brief Update derived fields based on current state
* \param grid
* \return void
*/
static void
SetDerivedFields(DmtxScanGrid *grid)
{
grid->jumpSize = grid->extent + 1;
grid->pixelTotal = 2 * grid->extent - 1;
grid->startPos = grid->extent / 2;
grid->pixelCount = 0;
grid->xCenter = grid->yCenter = grid->startPos;
}