libdmtx 0.7.8.7
libdmtx is a software library that enables programs to read and write Data Matrix barcodes of the modern ECC200 variety.
Loading...
Searching...
No Matches
dmtxscangrid.c
Go to the documentation of this file.
1
17#include <assert.h>
18#include <string.h>
19
20#include "dmtx.h"
21#include "dmtxstatic.h"
22
27{
28 int scale, smallestFeature;
29 int xExtent, yExtent, maxExtent;
30 int extent;
31 DmtxScanGrid grid;
32
33 memset(&grid, 0x00, sizeof(DmtxScanGrid));
34
35 scale = dmtxDecodeGetProp(dec, DmtxPropScale);
36 smallestFeature = dmtxDecodeGetProp(dec, DmtxPropScanGap) / scale;
37
38 grid.xMin = dmtxDecodeGetProp(dec, DmtxPropXmin);
39 grid.xMax = dmtxDecodeGetProp(dec, DmtxPropXmax);
40 grid.yMin = dmtxDecodeGetProp(dec, DmtxPropYmin);
41 grid.yMax = dmtxDecodeGetProp(dec, DmtxPropYmax);
42
43 /* Values that get set once */
44 xExtent = grid.xMax - grid.xMin;
45 yExtent = grid.yMax - grid.yMin;
46 maxExtent = max(xExtent, yExtent);
47
48 DmtxAssert(maxExtent > 1);
49
50 for (extent = 1; extent < maxExtent; extent = ((extent + 1) * 2) - 1) {
51 if (extent <= smallestFeature) {
52 grid.minExtent = extent;
53 }
54 }
55
56 grid.maxExtent = extent;
57
58 grid.xOffset = (grid.xMin + grid.xMax - grid.maxExtent) / 2;
59 grid.yOffset = (grid.yMin + grid.yMax - grid.maxExtent) / 2;
60
61 /* Values that get reset for every level */
62 grid.total = 1;
63 grid.extent = grid.maxExtent;
64
65 setDerivedFields(&grid);
66
67 return grid;
68}
69
75static int popGridLocation(DmtxScanGrid *grid, DmtxPixelLoc *locPtr)
76{
77 int locStatus;
78
79 do {
80 locStatus = getGridCoordinates(grid, locPtr);
81
82 /* Always leave grid pointing at next available location */
83 grid->pixelCount++;
84
85 } while (locStatus == DmtxRangeBad);
86
87 return locStatus;
88}
89
96{
97 int count, half, quarter;
98 DmtxPixelLoc loc;
99
100 /* Initially pixelCount may fall beyond acceptable limits. Update grid
101 * state before testing coordinates */
102
103 /* Jump to next cross pattern horizontally if current column is done */
104 if (grid->pixelCount >= grid->pixelTotal) {
105 grid->pixelCount = 0;
106 grid->xCenter += grid->jumpSize;
107 }
108
109 /* Jump to next cross pattern vertically if current row is done */
110 if (grid->xCenter > grid->maxExtent) {
111 grid->xCenter = grid->startPos;
112 grid->yCenter += grid->jumpSize;
113 }
114
115 /* Increment level when vertical step goes too far */
116 if (grid->yCenter > grid->maxExtent) {
117 grid->total *= 4;
118 grid->extent /= 2;
119 setDerivedFields(grid);
120 }
121
122 if (grid->extent == 0 || grid->extent < grid->minExtent) {
123 locPtr->x = locPtr->y = -1;
124 return DmtxRangeEnd;
125 }
126
127 count = grid->pixelCount;
128
129 DmtxAssert(count < grid->pixelTotal);
130
131 if (count == grid->pixelTotal - 1) {
132 /* center pixel */
133 loc.x = grid->xCenter;
134 loc.y = grid->yCenter;
135 } else {
136 half = grid->pixelTotal / 2;
137 quarter = half / 2;
138
139 /* horizontal portion */
140 if (count < half) {
141 loc.x = grid->xCenter + ((count < quarter) ? (count - quarter) : (half - count));
142 loc.y = grid->yCenter;
143 }
144 /* vertical portion */
145 else {
146 count -= half;
147 loc.x = grid->xCenter;
148 loc.y = grid->yCenter + ((count < quarter) ? (count - quarter) : (half - count));
149 }
150 }
151
152 loc.x += grid->xOffset;
153 loc.y += grid->yOffset;
154
155 *locPtr = loc;
156
157 if (loc.x < grid->xMin || loc.x > grid->xMax || loc.y < grid->yMin || loc.y > grid->yMax) {
158 return DmtxRangeBad;
159 }
160
161 return DmtxRangeGood;
162}
163
168{
169 grid->jumpSize = grid->extent + 1;
170 grid->pixelTotal = 2 * grid->extent - 1;
171 grid->startPos = grid->extent / 2;
172 grid->pixelCount = 0;
173 grid->xCenter = grid->yCenter = grid->startPos;
174}
libdmtx - Data Matrix Encoding/Decoding Library Copyright 2008, 2009 Mike Laughton.
@ DmtxPropXmax
ROI X坐标最大值(如果未设置则为图像宽度-1)
Definition dmtx.h:208
@ DmtxPropXmin
ROI X坐标最小值(如果未设置则为0)
Definition dmtx.h:207
@ DmtxPropYmin
ROI Y坐标最小值(如果未设置则为0)
Definition dmtx.h:209
@ DmtxPropScale
图像缩放比例
Definition dmtx.h:211
@ DmtxPropYmax
ROI Y坐标最大值(如果未设置则为图像高度-1)
Definition dmtx.h:210
@ DmtxPropScanGap
Definition dmtx.h:190
int dmtxDecodeGetProp(DmtxDecode *dec, int prop)
Get decoding behavior property.
Definition dmtxdecode.c:166
static DmtxScanGrid initScanGrid(DmtxDecode *dec)
初始化扫描网格
static int getGridCoordinates(DmtxScanGrid *grid, DmtxPixelLoc *locPtr)
Extract current grid position in pixel coordinates and return whether location is good,...
static int popGridLocation(DmtxScanGrid *grid, DmtxPixelLoc *locPtr)
Return the next good location (which may be the current location), and advance grid progress one posi...
static void setDerivedFields(DmtxScanGrid *grid)
Update derived fields based on current state.
libdmtx - Data Matrix Encoding/Decoding Library Copyright 2008, 2009 Mike Laughton.
#define DmtxAssert(expr)
Definition dmtxstatic.h:96
#define max(X, Y)
Definition dmtxstatic.h:65
@ DmtxRangeBad
Definition dmtxstatic.h:115
@ DmtxRangeEnd
Definition dmtxstatic.h:116
@ DmtxRangeGood
Definition dmtxstatic.h:114
DmtxDecode.
像素坐标
Definition dmtx.h:268
DmtxScanGrid.