-
Notifications
You must be signed in to change notification settings - Fork 138
/
dmtxplacemod.c
282 lines (256 loc) · 11.7 KB
/
dmtxplacemod.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/**
* 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 dmtxplacemod.c
* \brief Data Matrix module placement
*/
/**
* receives symbol row and col and returns status
* DmtxModuleOn / !DmtxModuleOn (DmtxModuleOff)
* DmtxModuleAssigned
* DmtxModuleVisited
* DmtxModuleData / !DmtxModuleData (DmtxModuleAlignment)
* row and col are expressed in symbol coordinates, so (0,0) is the intersection of the "L"
*/
int
dmtxSymbolModuleStatus(DmtxMessage *message, int sizeIdx, int symbolRow, int symbolCol)
{
int symbolRowReverse;
int mappingRow, mappingCol;
int dataRegionRows, dataRegionCols;
int symbolRows, mappingCols;
dataRegionRows = dmtxGetSymbolAttribute(DmtxSymAttribDataRegionRows, sizeIdx);
dataRegionCols = dmtxGetSymbolAttribute(DmtxSymAttribDataRegionCols, sizeIdx);
symbolRows = dmtxGetSymbolAttribute(DmtxSymAttribSymbolRows, sizeIdx);
mappingCols = dmtxGetSymbolAttribute(DmtxSymAttribMappingMatrixCols, sizeIdx);
symbolRowReverse = symbolRows - symbolRow - 1;
mappingRow = symbolRowReverse - 1 - 2 * (symbolRowReverse / (dataRegionRows+2));
mappingCol = symbolCol - 1 - 2 * (symbolCol / (dataRegionCols+2));
/* Solid portion of alignment patterns */
if(symbolRow % (dataRegionRows+2) == 0 ||
symbolCol % (dataRegionCols+2) == 0)
return (DmtxModuleOnRGB | (!DmtxModuleData));
/* Horinzontal calibration bars */
if((symbolRow+1) % (dataRegionRows+2) == 0)
return (((symbolCol & 0x01) ? 0 : DmtxModuleOnRGB) | (!DmtxModuleData));
/* Vertical calibration bars */
if((symbolCol+1) % (dataRegionCols+2) == 0)
return (((symbolRow & 0x01) ? 0 : DmtxModuleOnRGB) | (!DmtxModuleData));
/* Data modules */
return (message->array[mappingRow * mappingCols + mappingCol] | DmtxModuleData);
}
/**
* \brief Logical relationship between bit and module locations
* \param modules
* \param codewords
* \param sizeIdx
* \param moduleOnColor
* \return Number of codewords read
*/
static int
ModulePlacementEcc200(unsigned char *modules, unsigned char *codewords, int sizeIdx, int moduleOnColor)
{
int row, col, chr;
int mappingRows, mappingCols;
assert(moduleOnColor & (DmtxModuleOnRed | DmtxModuleOnGreen | DmtxModuleOnBlue));
mappingRows = dmtxGetSymbolAttribute(DmtxSymAttribMappingMatrixRows, sizeIdx);
mappingCols = dmtxGetSymbolAttribute(DmtxSymAttribMappingMatrixCols, sizeIdx);
/* Start in the nominal location for the 8th bit of the first character */
chr = 0;
row = 4;
col = 0;
do {
/* Repeatedly first check for one of the special corner cases */
if((row == mappingRows) && (col == 0))
PatternShapeSpecial1(modules, mappingRows, mappingCols, &(codewords[chr++]), moduleOnColor);
else if((row == mappingRows-2) && (col == 0) && (mappingCols%4 != 0))
PatternShapeSpecial2(modules, mappingRows, mappingCols, &(codewords[chr++]), moduleOnColor);
else if((row == mappingRows-2) && (col == 0) && (mappingCols%8 == 4))
PatternShapeSpecial3(modules, mappingRows, mappingCols, &(codewords[chr++]), moduleOnColor);
else if((row == mappingRows+4) && (col == 2) && (mappingCols%8 == 0))
PatternShapeSpecial4(modules, mappingRows, mappingCols, &(codewords[chr++]), moduleOnColor);
/* Sweep upward diagonally, inserting successive characters */
do {
if((row < mappingRows) && (col >= 0) &&
!(modules[row*mappingCols+col] & DmtxModuleVisited))
PatternShapeStandard(modules, mappingRows, mappingCols, row, col, &(codewords[chr++]), moduleOnColor);
row -= 2;
col += 2;
} while ((row >= 0) && (col < mappingCols));
row += 1;
col += 3;
/* Sweep downward diagonally, inserting successive characters */
do {
if((row >= 0) && (col < mappingCols) &&
!(modules[row*mappingCols+col] & DmtxModuleVisited))
PatternShapeStandard(modules, mappingRows, mappingCols, row, col, &(codewords[chr++]), moduleOnColor);
row += 2;
col -= 2;
} while ((row < mappingRows) && (col >= 0));
row += 3;
col += 1;
/* ... until the entire modules array is scanned */
} while ((row < mappingRows) || (col < mappingCols));
/* If lower righthand corner is untouched then fill in the fixed pattern */
if(!(modules[mappingRows * mappingCols - 1] &
DmtxModuleVisited)) {
modules[mappingRows * mappingCols - 1] |= moduleOnColor;
modules[(mappingRows * mappingCols) - mappingCols - 2] |= moduleOnColor;
} /* XXX should this fixed pattern also be used in reading somehow? */
/* XXX compare that chr == region->dataSize here */
return chr; /* XXX number of codewords read off */
}
/**
* \brief XXX
* \param modules
* \param mappingRows
* \param mappingCols
* \param row
* \param col
* \param codeword
* \param moduleOnColor
* \return void
*/
static void
PatternShapeStandard(unsigned char *modules, int mappingRows, int mappingCols, int row, int col, unsigned char *codeword, int moduleOnColor)
{
PlaceModule(modules, mappingRows, mappingCols, row-2, col-2, codeword, DmtxMaskBit1, moduleOnColor);
PlaceModule(modules, mappingRows, mappingCols, row-2, col-1, codeword, DmtxMaskBit2, moduleOnColor);
PlaceModule(modules, mappingRows, mappingCols, row-1, col-2, codeword, DmtxMaskBit3, moduleOnColor);
PlaceModule(modules, mappingRows, mappingCols, row-1, col-1, codeword, DmtxMaskBit4, moduleOnColor);
PlaceModule(modules, mappingRows, mappingCols, row-1, col, codeword, DmtxMaskBit5, moduleOnColor);
PlaceModule(modules, mappingRows, mappingCols, row, col-2, codeword, DmtxMaskBit6, moduleOnColor);
PlaceModule(modules, mappingRows, mappingCols, row, col-1, codeword, DmtxMaskBit7, moduleOnColor);
PlaceModule(modules, mappingRows, mappingCols, row, col, codeword, DmtxMaskBit8, moduleOnColor);
}
/**
* \brief XXX
* \param modules
* \param mappingRows
* \param mappingCols
* \param codeword
* \param moduleOnColor
* \return void
*/
static void
PatternShapeSpecial1(unsigned char *modules, int mappingRows, int mappingCols, unsigned char *codeword, int moduleOnColor)
{
PlaceModule(modules, mappingRows, mappingCols, mappingRows-1, 0, codeword, DmtxMaskBit1, moduleOnColor);
PlaceModule(modules, mappingRows, mappingCols, mappingRows-1, 1, codeword, DmtxMaskBit2, moduleOnColor);
PlaceModule(modules, mappingRows, mappingCols, mappingRows-1, 2, codeword, DmtxMaskBit3, moduleOnColor);
PlaceModule(modules, mappingRows, mappingCols, 0, mappingCols-2, codeword, DmtxMaskBit4, moduleOnColor);
PlaceModule(modules, mappingRows, mappingCols, 0, mappingCols-1, codeword, DmtxMaskBit5, moduleOnColor);
PlaceModule(modules, mappingRows, mappingCols, 1, mappingCols-1, codeword, DmtxMaskBit6, moduleOnColor);
PlaceModule(modules, mappingRows, mappingCols, 2, mappingCols-1, codeword, DmtxMaskBit7, moduleOnColor);
PlaceModule(modules, mappingRows, mappingCols, 3, mappingCols-1, codeword, DmtxMaskBit8, moduleOnColor);
}
/**
* \brief XXX
* \param modules
* \param mappingRows
* \param mappingCols
* \param codeword
* \param moduleOnColor
* \return void
*/
static void
PatternShapeSpecial2(unsigned char *modules, int mappingRows, int mappingCols, unsigned char *codeword, int moduleOnColor)
{
PlaceModule(modules, mappingRows, mappingCols, mappingRows-3, 0, codeword, DmtxMaskBit1, moduleOnColor);
PlaceModule(modules, mappingRows, mappingCols, mappingRows-2, 0, codeword, DmtxMaskBit2, moduleOnColor);
PlaceModule(modules, mappingRows, mappingCols, mappingRows-1, 0, codeword, DmtxMaskBit3, moduleOnColor);
PlaceModule(modules, mappingRows, mappingCols, 0, mappingCols-4, codeword, DmtxMaskBit4, moduleOnColor);
PlaceModule(modules, mappingRows, mappingCols, 0, mappingCols-3, codeword, DmtxMaskBit5, moduleOnColor);
PlaceModule(modules, mappingRows, mappingCols, 0, mappingCols-2, codeword, DmtxMaskBit6, moduleOnColor);
PlaceModule(modules, mappingRows, mappingCols, 0, mappingCols-1, codeword, DmtxMaskBit7, moduleOnColor);
PlaceModule(modules, mappingRows, mappingCols, 1, mappingCols-1, codeword, DmtxMaskBit8, moduleOnColor);
}
/**
* \brief XXX
* \param modules
* \param mappingRows
* \param mappingCols
* \param codeword
* \param moduleOnColor
* \return void
*/
static void
PatternShapeSpecial3(unsigned char *modules, int mappingRows, int mappingCols, unsigned char *codeword, int moduleOnColor)
{
PlaceModule(modules, mappingRows, mappingCols, mappingRows-3, 0, codeword, DmtxMaskBit1, moduleOnColor);
PlaceModule(modules, mappingRows, mappingCols, mappingRows-2, 0, codeword, DmtxMaskBit2, moduleOnColor);
PlaceModule(modules, mappingRows, mappingCols, mappingRows-1, 0, codeword, DmtxMaskBit3, moduleOnColor);
PlaceModule(modules, mappingRows, mappingCols, 0, mappingCols-2, codeword, DmtxMaskBit4, moduleOnColor);
PlaceModule(modules, mappingRows, mappingCols, 0, mappingCols-1, codeword, DmtxMaskBit5, moduleOnColor);
PlaceModule(modules, mappingRows, mappingCols, 1, mappingCols-1, codeword, DmtxMaskBit6, moduleOnColor);
PlaceModule(modules, mappingRows, mappingCols, 2, mappingCols-1, codeword, DmtxMaskBit7, moduleOnColor);
PlaceModule(modules, mappingRows, mappingCols, 3, mappingCols-1, codeword, DmtxMaskBit8, moduleOnColor);
}
/**
* \brief XXX
* \param modules
* \param mappingRows
* \param mappingCols
* \param codeword
* \param moduleOnColor
* \return void
*/
static void
PatternShapeSpecial4(unsigned char *modules, int mappingRows, int mappingCols, unsigned char *codeword, int moduleOnColor)
{
PlaceModule(modules, mappingRows, mappingCols, mappingRows-1, 0, codeword, DmtxMaskBit1, moduleOnColor);
PlaceModule(modules, mappingRows, mappingCols, mappingRows-1, mappingCols-1, codeword, DmtxMaskBit2, moduleOnColor);
PlaceModule(modules, mappingRows, mappingCols, 0, mappingCols-3, codeword, DmtxMaskBit3, moduleOnColor);
PlaceModule(modules, mappingRows, mappingCols, 0, mappingCols-2, codeword, DmtxMaskBit4, moduleOnColor);
PlaceModule(modules, mappingRows, mappingCols, 0, mappingCols-1, codeword, DmtxMaskBit5, moduleOnColor);
PlaceModule(modules, mappingRows, mappingCols, 1, mappingCols-3, codeword, DmtxMaskBit6, moduleOnColor);
PlaceModule(modules, mappingRows, mappingCols, 1, mappingCols-2, codeword, DmtxMaskBit7, moduleOnColor);
PlaceModule(modules, mappingRows, mappingCols, 1, mappingCols-1, codeword, DmtxMaskBit8, moduleOnColor);
}
/**
* \brief XXX
* \param modules
* \param mappingRows
* \param mappingCols
* \param row
* \param col
* \param codeword
* \param mask
* \param moduleOnColor
* \return void
*/
static void
PlaceModule(unsigned char *modules, int mappingRows, int mappingCols, int row, int col, unsigned char *codeword, int mask, int moduleOnColor)
{
if(row < 0) {
row += mappingRows;
col += 4 - ((mappingRows+4)%8);
}
if(col < 0) {
col += mappingCols;
row += 4 - ((mappingCols+4)%8);
}
/* If module has already been assigned then we are decoding the pattern into codewords */
if((modules[row*mappingCols+col] & DmtxModuleAssigned) != 0) {
if((modules[row*mappingCols+col] & moduleOnColor) != 0)
*codeword |= mask;
else
*codeword &= (0xff ^ mask);
}
/* Otherwise we are encoding the codewords into a pattern */
else {
if((*codeword & mask) != 0x00)
modules[row*mappingCols+col] |= moduleOnColor;
modules[row*mappingCols+col] |= DmtxModuleAssigned;
}
modules[row*mappingCols+col] |= DmtxModuleVisited;
}