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
dmtxplacemod.c
Go to the documentation of this file.
1
17#include <assert.h>
18
19#include "dmtx.h"
20#include "dmtxstatic.h"
21
30int dmtxSymbolModuleStatus(DmtxMessage *message, int sizeIdx, int symbolRow, int symbolCol)
31{
32 int symbolRowReverse;
33 int mappingRow, mappingCol;
34 int dataRegionRows, dataRegionCols;
35 int symbolRows, mappingCols;
36
37 dataRegionRows = dmtxGetSymbolAttribute(DmtxSymAttribDataRegionRows, sizeIdx);
38 dataRegionCols = dmtxGetSymbolAttribute(DmtxSymAttribDataRegionCols, sizeIdx);
41
42 symbolRowReverse = symbolRows - symbolRow - 1;
43 mappingRow = symbolRowReverse - 1 - 2 * (symbolRowReverse / (dataRegionRows + 2));
44 mappingCol = symbolCol - 1 - 2 * (symbolCol / (dataRegionCols + 2));
45
46 /* Solid portion of alignment patterns */
47 if (symbolRow % (dataRegionRows + 2) == 0 || symbolCol % (dataRegionCols + 2) == 0) {
48 return (DmtxModuleOnRGB | (!DmtxModuleData));
49 }
50
51 /* Horinzontal calibration bars */
52 if ((symbolRow + 1) % (dataRegionRows + 2) == 0) {
53 return (((symbolCol & 0x01) ? 0 : DmtxModuleOnRGB) | (!DmtxModuleData));
54 }
55
56 /* Vertical calibration bars */
57 if ((symbolCol + 1) % (dataRegionCols + 2) == 0) {
58 return (((symbolRow & 0x01) ? 0 : DmtxModuleOnRGB) | (!DmtxModuleData));
59 }
60
61 /* Data modules */
62 return (message->array[mappingRow * mappingCols + mappingCol] | DmtxModuleData);
63}
64
73static int modulePlacementEcc200(INOUT unsigned char *modules, OUT unsigned char *codewords, int sizeIdx,
74 int moduleOnColor)
75{
76 int row, col, chr;
77 int mappingRows, mappingCols;
78
80
83
84 /* 初始化:寻找第一个字符的第8位的起始位置 */
85 chr = 0;
86 row = 4;
87 col = 0;
88
89 do {
90 /* 检查并处理四个特殊角落的码字排布模式 */
91 if ((row == mappingRows) && (col == 0)) {
92 patternShapeSpecial1(modules, mappingRows, mappingCols, &(codewords[chr++]), moduleOnColor);
93 } else if ((row == mappingRows - 2) && (col == 0) && (mappingCols % 4 != 0)) {
94 patternShapeSpecial2(modules, mappingRows, mappingCols, &(codewords[chr++]), moduleOnColor);
95 } else if ((row == mappingRows - 2) && (col == 0) && (mappingCols % 8 == 4)) {
96 patternShapeSpecial3(modules, mappingRows, mappingCols, &(codewords[chr++]), moduleOnColor);
97 } else if ((row == mappingRows + 4) && (col == 2) && (mappingCols % 8 == 0)) {
98 patternShapeSpecial4(modules, mappingRows, mappingCols, &(codewords[chr++]), moduleOnColor);
99 }
100
101 /* 以对角线方式斜向上扫描并插入字符 */
102 do {
103 if ((row < mappingRows) && (col >= 0) && !(modules[row * mappingCols + col] & DmtxModuleVisited)) {
104 patternShapeStandard(modules, mappingRows, mappingCols, row, col, &(codewords[chr++]), moduleOnColor);
105 }
106 row -= 2;
107 col += 2;
108 } while ((row >= 0) && (col < mappingCols));
109 row += 1;
110 col += 3;
111
112 /* 同样以对角线方式向下扫描并插入字符 */
113 do {
114 if ((row >= 0) && (col < mappingCols) && !(modules[row * mappingCols + col] & DmtxModuleVisited)) {
115 patternShapeStandard(modules, mappingRows, mappingCols, row, col, &(codewords[chr++]), moduleOnColor);
116 }
117 row += 2;
118 col -= 2;
119 } while ((row < mappingRows) && (col >= 0));
120 row += 3;
121 col += 1;
122 /* 重复此过程,直到扫描完整个modules数组 */
123 } while ((row < mappingRows) || (col < mappingCols));
124
125 /* 处理右下角的固定模式 */
126 if (!(modules[mappingRows * mappingCols - 1] & DmtxModuleVisited)) {
127 modules[mappingRows * mappingCols - 1] |= moduleOnColor;
128 modules[(mappingRows * mappingCols) - mappingCols - 2] |= moduleOnColor;
129 } /* XXX should this fixed pattern also be used in reading somehow? */
130
131 /* XXX compare that chr == region->dataSize here */
132 return chr; /* XXX number of codewords read off */
133}
134
152static void patternShapeStandard(unsigned char *modules, int mappingRows, int mappingCols, int row, int col,
153 unsigned char *codeword, int moduleOnColor)
154{
155 placeModule(modules, mappingRows, mappingCols, row - 2, col - 2, codeword, DmtxMaskBit1, moduleOnColor);
156 placeModule(modules, mappingRows, mappingCols, row - 2, col - 1, codeword, DmtxMaskBit2, moduleOnColor);
157 placeModule(modules, mappingRows, mappingCols, row - 1, col - 2, codeword, DmtxMaskBit3, moduleOnColor);
158 placeModule(modules, mappingRows, mappingCols, row - 1, col - 1, codeword, DmtxMaskBit4, moduleOnColor);
159 placeModule(modules, mappingRows, mappingCols, row - 1, col, codeword, DmtxMaskBit5, moduleOnColor);
160 placeModule(modules, mappingRows, mappingCols, row, col - 2, codeword, DmtxMaskBit6, moduleOnColor);
161 placeModule(modules, mappingRows, mappingCols, row, col - 1, codeword, DmtxMaskBit7, moduleOnColor);
162 placeModule(modules, mappingRows, mappingCols, row, col, codeword, DmtxMaskBit8, moduleOnColor);
163}
164
186static void patternShapeSpecial1(unsigned char *modules, int mappingRows, int mappingCols, unsigned char *codeword,
187 int moduleOnColor)
188{
189 placeModule(modules, mappingRows, mappingCols, mappingRows - 1, 0, codeword, DmtxMaskBit1, moduleOnColor);
190 placeModule(modules, mappingRows, mappingCols, mappingRows - 1, 1, codeword, DmtxMaskBit2, moduleOnColor);
191 placeModule(modules, mappingRows, mappingCols, mappingRows - 1, 2, codeword, DmtxMaskBit3, moduleOnColor);
192 placeModule(modules, mappingRows, mappingCols, 0, mappingCols - 2, codeword, DmtxMaskBit4, moduleOnColor);
193 placeModule(modules, mappingRows, mappingCols, 0, mappingCols - 1, codeword, DmtxMaskBit5, moduleOnColor);
194 placeModule(modules, mappingRows, mappingCols, 1, mappingCols - 1, codeword, DmtxMaskBit6, moduleOnColor);
195 placeModule(modules, mappingRows, mappingCols, 2, mappingCols - 1, codeword, DmtxMaskBit7, moduleOnColor);
196 placeModule(modules, mappingRows, mappingCols, 3, mappingCols - 1, codeword, DmtxMaskBit8, moduleOnColor);
197}
198
221static void patternShapeSpecial2(unsigned char *modules, int mappingRows, int mappingCols, unsigned char *codeword,
222 int moduleOnColor)
223{
224 placeModule(modules, mappingRows, mappingCols, mappingRows - 3, 0, codeword, DmtxMaskBit1, moduleOnColor);
225 placeModule(modules, mappingRows, mappingCols, mappingRows - 2, 0, codeword, DmtxMaskBit2, moduleOnColor);
226 placeModule(modules, mappingRows, mappingCols, mappingRows - 1, 0, codeword, DmtxMaskBit3, moduleOnColor);
227 placeModule(modules, mappingRows, mappingCols, 0, mappingCols - 4, codeword, DmtxMaskBit4, moduleOnColor);
228 placeModule(modules, mappingRows, mappingCols, 0, mappingCols - 3, codeword, DmtxMaskBit5, moduleOnColor);
229 placeModule(modules, mappingRows, mappingCols, 0, mappingCols - 2, codeword, DmtxMaskBit6, moduleOnColor);
230 placeModule(modules, mappingRows, mappingCols, 0, mappingCols - 1, codeword, DmtxMaskBit7, moduleOnColor);
231 placeModule(modules, mappingRows, mappingCols, 1, mappingCols - 1, codeword, DmtxMaskBit8, moduleOnColor);
232}
233
257static void patternShapeSpecial3(unsigned char *modules, int mappingRows, int mappingCols, unsigned char *codeword,
258 int moduleOnColor)
259{
260 placeModule(modules, mappingRows, mappingCols, mappingRows - 3, 0, codeword, DmtxMaskBit1, moduleOnColor);
261 placeModule(modules, mappingRows, mappingCols, mappingRows - 2, 0, codeword, DmtxMaskBit2, moduleOnColor);
262 placeModule(modules, mappingRows, mappingCols, mappingRows - 1, 0, codeword, DmtxMaskBit3, moduleOnColor);
263 placeModule(modules, mappingRows, mappingCols, 0, mappingCols - 2, codeword, DmtxMaskBit4, moduleOnColor);
264 placeModule(modules, mappingRows, mappingCols, 0, mappingCols - 1, codeword, DmtxMaskBit5, moduleOnColor);
265 placeModule(modules, mappingRows, mappingCols, 1, mappingCols - 1, codeword, DmtxMaskBit6, moduleOnColor);
266 placeModule(modules, mappingRows, mappingCols, 2, mappingCols - 1, codeword, DmtxMaskBit7, moduleOnColor);
267 placeModule(modules, mappingRows, mappingCols, 3, mappingCols - 1, codeword, DmtxMaskBit8, moduleOnColor);
268}
269
295static void patternShapeSpecial4(unsigned char *modules, int mappingRows, int mappingCols, unsigned char *codeword,
296 int moduleOnColor)
297{
298 placeModule(modules, mappingRows, mappingCols, mappingRows - 1, 0, codeword, DmtxMaskBit1, moduleOnColor);
299 placeModule(modules, mappingRows, mappingCols, mappingRows - 1, mappingCols - 1, codeword, DmtxMaskBit2,
300 moduleOnColor);
301 placeModule(modules, mappingRows, mappingCols, 0, mappingCols - 3, codeword, DmtxMaskBit3, moduleOnColor);
302 placeModule(modules, mappingRows, mappingCols, 0, mappingCols - 2, codeword, DmtxMaskBit4, moduleOnColor);
303 placeModule(modules, mappingRows, mappingCols, 0, mappingCols - 1, codeword, DmtxMaskBit5, moduleOnColor);
304 placeModule(modules, mappingRows, mappingCols, 1, mappingCols - 3, codeword, DmtxMaskBit6, moduleOnColor);
305 placeModule(modules, mappingRows, mappingCols, 1, mappingCols - 2, codeword, DmtxMaskBit7, moduleOnColor);
306 placeModule(modules, mappingRows, mappingCols, 1, mappingCols - 1, codeword, DmtxMaskBit8, moduleOnColor);
307}
308
324static void placeModule(unsigned char *modules, int mappingRows, int mappingCols, int row, int col,
325 unsigned char *codeword, int mask, int moduleOnColor)
326{
327 if (row < 0) {
328 row += mappingRows;
329 col += 4 - ((mappingRows + 4) % 8);
330 }
331 if (col < 0) {
332 col += mappingCols;
333 row += 4 - ((mappingCols + 4) % 8);
334 }
335
336 if ((modules[row * mappingCols + col] & DmtxModuleAssigned) != 0) { /* 解码 */
337 if ((modules[row * mappingCols + col] & moduleOnColor) != 0) {
338 *codeword |= mask;
339 } else {
340 *codeword &= (0xff ^ mask);
341 }
342 } else { /* 编码 */
343 if ((*codeword & mask) != 0x00) {
344 modules[row * mappingCols + col] |= moduleOnColor;
345 }
346
347 modules[row * mappingCols + col] |= DmtxModuleAssigned;
348 }
349
350 modules[row * mappingCols + col] |= DmtxModuleVisited;
351}
libdmtx - Data Matrix Encoding/Decoding Library Copyright 2008, 2009 Mike Laughton.
int dmtxGetSymbolAttribute(int attribute, int sizeIdx)
根据规格索引返回二维码规格各个参数
Definition dmtxsymbol.c:45
#define DmtxModuleOnRed
Definition dmtx.h:57
#define DmtxModuleAssigned
已分配
Definition dmtx.h:63
#define DmtxModuleData
Definition dmtx.h:65
@ DmtxSymAttribSymbolRows
二维码码元总行数(包括L形框和点线)
Definition dmtx.h:154
@ DmtxSymAttribDataRegionCols
单区块二维码数据区码元列数(不包括L形框和点线)
Definition dmtx.h:157
@ DmtxSymAttribMappingMatrixRows
二维码数据区码元总行数(不包括L形框和点线)
Definition dmtx.h:160
@ DmtxSymAttribDataRegionRows
单区块二维码数据区码元行数(不包括L形框和点线)
Definition dmtx.h:156
@ DmtxSymAttribMappingMatrixCols
二维码数据区码元总列数(不包括L形框和点线)
Definition dmtx.h:161
#define DmtxModuleOnBlue
Definition dmtx.h:59
#define INOUT
Definition dmtx.h:76
#define DmtxModuleOnRGB
OnRed | OnGreen | OnBlue.
Definition dmtx.h:60
#define DmtxModuleVisited
已访问
Definition dmtx.h:64
#define DmtxModuleOnGreen
绿
Definition dmtx.h:58
#define OUT
Definition dmtx.h:75
int dmtxSymbolModuleStatus(DmtxMessage *message, int sizeIdx, int symbolRow, int symbolCol)
receives symbol row and col and returns status DmtxModuleOn / !DmtxModuleOn (DmtxModuleOff) DmtxModul...
static void patternShapeStandard(unsigned char *modules, int mappingRows, int mappingCols, int row, int col, unsigned char *codeword, int moduleOnColor)
将标准码字放置到指定的模块位置
static void placeModule(unsigned char *modules, int mappingRows, int mappingCols, int row, int col, unsigned char *codeword, int mask, int moduleOnColor)
位模块放置
static void patternShapeSpecial3(unsigned char *modules, int mappingRows, int mappingCols, unsigned char *codeword, int moduleOnColor)
特殊排布3
static void patternShapeSpecial4(unsigned char *modules, int mappingRows, int mappingCols, unsigned char *codeword, int moduleOnColor)
特殊排布4
static void patternShapeSpecial2(unsigned char *modules, int mappingRows, int mappingCols, unsigned char *codeword, int moduleOnColor)
特殊排布2
static int modulePlacementEcc200(INOUT unsigned char *modules, OUT unsigned char *codewords, int sizeIdx, int moduleOnColor)
通过DataMatrix数据区的二进制矩阵,根据DataMatrix的排列规则,得到码字(codewords)
static void patternShapeSpecial1(unsigned char *modules, int mappingRows, int mappingCols, unsigned char *codeword, int moduleOnColor)
特殊排布1
libdmtx - Data Matrix Encoding/Decoding Library Copyright 2008, 2009 Mike Laughton.
@ DmtxMaskBit7
Definition dmtxstatic.h:130
@ DmtxMaskBit2
Definition dmtxstatic.h:135
@ DmtxMaskBit1
Definition dmtxstatic.h:136
@ DmtxMaskBit4
Definition dmtxstatic.h:133
@ DmtxMaskBit5
Definition dmtxstatic.h:132
@ DmtxMaskBit3
Definition dmtxstatic.h:134
@ DmtxMaskBit8
Definition dmtxstatic.h:129
@ DmtxMaskBit6
Definition dmtxstatic.h:131
#define DmtxAssert(expr)
Definition dmtxstatic.h:96
DataMatrix编码内容
Definition dmtx.h:421
unsigned char * array
指向DataMatrix数据区二进制矩阵的指针
Definition dmtx.h:428