-
Notifications
You must be signed in to change notification settings - Fork 139
/
dmtxencode.c
525 lines (446 loc) · 15.7 KB
/
dmtxencode.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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
/**
* 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 dmtxencode.c
* \brief Base encoding logic
*/
#undef ISDIGIT
#define ISDIGIT(n) (n > 47 && n < 58)
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
/**
* \brief Initialize encode struct with default values
* \return Initialized DmtxEncode struct
*/
extern DmtxEncode *
dmtxEncodeCreate(void)
{
DmtxEncode *enc;
enc = (DmtxEncode *)calloc(1, sizeof(DmtxEncode));
if(enc == NULL)
return NULL;
enc->scheme = DmtxSchemeAscii;
enc->sizeIdxRequest = DmtxSymbolSquareAuto;
enc->marginSize = 10;
enc->moduleSize = 5;
enc->pixelPacking = DmtxPack24bppRGB;
enc->imageFlip = DmtxFlipNone;
enc->rowPadBytes = 0;
enc->fnc1 = DmtxUndefined;
/* Initialize background color to white */
/* enc.region.gradient.ray.p.R = 255.0;
enc.region.gradient.ray.p.G = 255.0;
enc.region.gradient.ray.p.B = 255.0; */
/* Initialize foreground color to black */
/* enc.region.gradient.tMin = 0.0;
enc.region.gradient.tMax = xyz; */
dmtxMatrix3Identity(enc->xfrm);
return enc;
}
/**
* \brief Deinitialize encode struct
* \param enc
* \return void
*/
extern DmtxPassFail
dmtxEncodeDestroy(DmtxEncode **enc)
{
if(enc == NULL || *enc == NULL)
return DmtxFail;
/* Free pixel array allocated in dmtxEncodeDataMatrix() */
if((*enc)->image != NULL && (*enc)->image->pxl != NULL) {
free((*enc)->image->pxl);
(*enc)->image->pxl = NULL;
}
dmtxImageDestroy(&((*enc)->image));
dmtxMessageDestroy(&((*enc)->message));
free(*enc);
*enc = NULL;
return DmtxPass;
}
/**
* \brief Set encoding behavior property
* \param enc
* \param prop
* \param value
* \return DmtxPass | DmtxFail
*/
extern DmtxPassFail
dmtxEncodeSetProp(DmtxEncode *enc, int prop, int value)
{
switch(prop) {
/* Encoding details */
case DmtxPropScheme:
enc->scheme = value;
break;
case DmtxPropSizeRequest:
if(value == DmtxSymbolShapeAuto)
return DmtxFail;
enc->sizeIdxRequest = value;
break;
case DmtxPropFnc1:
enc->fnc1 = value;
break;
/* Presentation details */
case DmtxPropMarginSize:
enc->marginSize = value;
break;
case DmtxPropModuleSize:
enc->moduleSize = value;
break;
/* Image properties */
case DmtxPropPixelPacking:
enc->pixelPacking = value;
break;
case DmtxPropImageFlip:
enc->imageFlip = value;
break;
case DmtxPropRowPadBytes:
enc->rowPadBytes = value;
default:
break;
}
return DmtxPass;
}
/**
* \brief Get encoding behavior property
* \param enc
* \param prop
* \return value
*/
extern int
dmtxEncodeGetProp(DmtxEncode *enc, int prop)
{
switch(prop) {
case DmtxPropMarginSize:
return enc->marginSize;
case DmtxPropModuleSize:
return enc->moduleSize;
case DmtxPropScheme:
return enc->scheme;
case DmtxPropFnc1:
return enc->fnc1;
default:
break;
}
return DmtxUndefined;
}
/**
* \brief Convert message into Data Matrix image
* \param enc
* \param inputSize
* \param inputString
* \param sizeIdxRequest
* \param bReaderProgramming
* \return DmtxPass | DmtxFail
*/
#ifdef HAVE_READER_PROGRAMMING
extern DmtxPassFail
dmtxEncodeDataMatrix(DmtxEncode *enc, int inputSize, unsigned char *inputString, DmtxBoolean bReaderProgramming)
#else
extern DmtxPassFail
dmtxEncodeDataMatrix(DmtxEncode *enc, int inputSize, unsigned char *inputString)
#endif
{
int sizeIdx;
int width, height, bitsPerPixel;
unsigned char *pxl;
DmtxByte outputStorage[4096];
DmtxByteList output = dmtxByteListBuild(outputStorage, sizeof(outputStorage));
DmtxByteList input = dmtxByteListBuild(inputString, inputSize);
input.length = inputSize;
/* Future: stream = StreamInit() ... */
/* Future: EncodeDataCodewords(&stream) ... */
/* Encode input string into data codewords */
#ifdef HAVE_READER_PROGRAMMING
sizeIdx = EncodeDataCodewords(&input, &output, enc->sizeIdxRequest, enc->scheme, enc->fnc1, bReaderProgramming);
#else
sizeIdx = EncodeDataCodewords(&input, &output, enc->sizeIdxRequest, enc->scheme, enc->fnc1);
#endif
if(sizeIdx == DmtxUndefined || output.length <= 0)
return DmtxFail;
/* EncodeDataCodewords() should have updated any auto sizeIdx to a real one */
assert(sizeIdx != DmtxSymbolSquareAuto && sizeIdx != DmtxSymbolRectAuto);
/* XXX we can remove a lot of this redundant data */
enc->region.sizeIdx = sizeIdx;
enc->region.symbolRows = dmtxGetSymbolAttribute(DmtxSymAttribSymbolRows, sizeIdx);
enc->region.symbolCols = dmtxGetSymbolAttribute(DmtxSymAttribSymbolCols, sizeIdx);
enc->region.mappingRows = dmtxGetSymbolAttribute(DmtxSymAttribMappingMatrixRows, sizeIdx);
enc->region.mappingCols = dmtxGetSymbolAttribute(DmtxSymAttribMappingMatrixCols, sizeIdx);
/* Allocate memory for message and array */
enc->message = dmtxMessageCreate(sizeIdx, DmtxFormatMatrix);
enc->message->padCount = 0; /* XXX this needs to be added back */
memcpy(enc->message->code, output.b, output.length);
/* Generate error correction codewords */
RsEncode(enc->message, enc->region.sizeIdx);
/* Module placement in region */
ModulePlacementEcc200(enc->message->array, enc->message->code,
enc->region.sizeIdx, DmtxModuleOnRGB);
width = 2 * enc->marginSize + (enc->region.symbolCols * enc->moduleSize);
height = 2 * enc->marginSize + (enc->region.symbolRows * enc->moduleSize);
bitsPerPixel = GetBitsPerPixel(enc->pixelPacking);
if(bitsPerPixel == DmtxUndefined)
return DmtxFail;
assert(bitsPerPixel % 8 == 0);
/* Allocate memory for the image to be generated */
pxl = (unsigned char *)malloc((width * bitsPerPixel / 8 + enc->rowPadBytes) * height);
if(pxl == NULL) {
perror("pixel malloc error");
return DmtxFail;
}
enc->image = dmtxImageCreate(pxl, width, height, enc->pixelPacking);
if(enc->image == NULL) {
perror("image malloc error");
return DmtxFail;
}
dmtxImageSetProp(enc->image, DmtxPropImageFlip, enc->imageFlip);
dmtxImageSetProp(enc->image, DmtxPropRowPadBytes, enc->rowPadBytes);
/* Insert finder and aligment pattern modules */
PrintPattern(enc);
return DmtxPass;
}
/**
* \brief Convert message into Data Mosaic image
*
* 1) count how many codewords it would take to encode the whole thing
* 2) take ceiling N of codeword count divided by 3
* 3) using minimum symbol size that can accomodate N codewords:
* 4) create several barcodes over iterations of increasing numbers of
* input codewords until you go one too far
* 5) if codewords remain after filling R, G, and B barcodes then go back
* to 3 and try with next larger size
* 6) take the 3 different images you created and write out a new barcode
*
* \param enc
* \param inputSize
* \param inputString
* \param sizeIdxRequest
* \return DmtxPass | DmtxFail
*/
extern DmtxPassFail
dmtxEncodeDataMosaic(DmtxEncode *enc, int inputSize, unsigned char *inputString)
{
unsigned char *inputStringR, *inputStringG, *inputStringB;
int tmpInputSize;
int inputSizeR, inputSizeG, inputSizeB;
int sizeIdxAttempt, sizeIdxFirst, sizeIdxLast;
int row, col, mappingRows, mappingCols;
DmtxEncode *encR, *encG, *encB;
/* Use 1/3 (ceiling) of inputSize establish input size target */
tmpInputSize = (inputSize + 2) / 3;
inputSizeR = tmpInputSize;
inputSizeG = tmpInputSize;
inputSizeB = inputSize - (inputSizeR + inputSizeG);
inputStringR = inputString;
inputStringG = inputStringR + inputSizeR;
inputStringB = inputStringG + inputSizeG;
/* Use 1/3 (floor) of dataWordCount establish first symbol size attempt */
sizeIdxFirst = FindSymbolSize(tmpInputSize, enc->sizeIdxRequest);
if(sizeIdxFirst == DmtxUndefined)
return DmtxFail;
/* Set the last possible symbol size for this symbol shape or specific size request */
if(enc->sizeIdxRequest == DmtxSymbolSquareAuto)
sizeIdxLast = DmtxSymbolSquareCount - 1;
else if(enc->sizeIdxRequest == DmtxSymbolRectAuto)
sizeIdxLast = DmtxSymbolSquareCount + DmtxSymbolRectCount - 1;
else
sizeIdxLast = sizeIdxFirst;
encR = encG = encB = NULL;
/* Try increasing symbol sizes until 3 of them can hold all input values */
for(sizeIdxAttempt = sizeIdxFirst; sizeIdxAttempt <= sizeIdxLast; sizeIdxAttempt++)
{
dmtxEncodeDestroy(&encR);
dmtxEncodeDestroy(&encG);
dmtxEncodeDestroy(&encB);
encR = dmtxEncodeCreate();
encG = dmtxEncodeCreate();
encB = dmtxEncodeCreate();
/* Copy all settings from master DmtxEncode, including pointer to image
and message, which is initially null */
*encR = *encG = *encB = *enc;
dmtxEncodeSetProp(encR, DmtxPropSizeRequest, sizeIdxAttempt);
dmtxEncodeSetProp(encG, DmtxPropSizeRequest, sizeIdxAttempt);
dmtxEncodeSetProp(encB, DmtxPropSizeRequest, sizeIdxAttempt);
/* RED LAYER - Holds temporary copy */
#ifdef HAVE_READER_PROGRAMMING
dmtxEncodeDataMatrix(encR, inputSizeR, inputStringR, DmtxFalse);
#else
dmtxEncodeDataMatrix(encR, inputSizeR, inputStringR);
#endif
if(encR->region.sizeIdx != sizeIdxAttempt)
continue;
/* GREEN LAYER - Holds temporary copy */
#ifdef HAVE_READER_PROGRAMMING
dmtxEncodeDataMatrix(encG, inputSizeG, inputStringG, DmtxFalse);
#else
dmtxEncodeDataMatrix(encG, inputSizeG, inputStringG);
#endif
if(encG->region.sizeIdx != sizeIdxAttempt)
continue;
/* BLUE LAYER - Holds temporary copy */
#ifdef HAVE_READER_PROGRAMMING
dmtxEncodeDataMatrix(encB, inputSizeB, inputStringB, DmtxFalse);
#else
dmtxEncodeDataMatrix(encB, inputSizeB, inputStringB);
#endif
if(encB->region.sizeIdx != sizeIdxAttempt)
continue;
/* If we get this far we found a fit */
break;
}
if(encR == NULL || encG == NULL || encB == NULL)
{
dmtxEncodeDestroy(&encR);
dmtxEncodeDestroy(&encG);
dmtxEncodeDestroy(&encB);
return DmtxFail;
}
/* Now we have the correct sizeIdxAttempt, and they all fit into the desired size */
/* Perform the red portion of the final encode to set internals correctly */
dmtxEncodeSetProp(enc, DmtxPropSizeRequest, sizeIdxAttempt);
#ifdef HAVE_READER_PROGRAMMING
dmtxEncodeDataMatrix(enc, inputSizeR, inputStringR, DmtxFalse);
#else
dmtxEncodeDataMatrix(enc, inputSizeR, inputStringR);
#endif
/* Zero out the array and overwrite the bits in 3 passes */
mappingRows = dmtxGetSymbolAttribute(DmtxSymAttribMappingMatrixRows, sizeIdxAttempt);
mappingCols = dmtxGetSymbolAttribute(DmtxSymAttribMappingMatrixCols, sizeIdxAttempt);
memset(enc->message->array, 0x00, sizeof(unsigned char) *
enc->region.mappingRows * enc->region.mappingCols);
ModulePlacementEcc200(enc->message->array, encR->message->code, sizeIdxAttempt, DmtxModuleOnRed);
/* Reset DmtxModuleAssigned and DMX_MODULE_VISITED bits */
for(row = 0; row < mappingRows; row++) {
for(col = 0; col < mappingCols; col++) {
enc->message->array[row*mappingCols+col] &= (0xff ^ (DmtxModuleAssigned | DmtxModuleVisited));
}
}
ModulePlacementEcc200(enc->message->array, encG->message->code, sizeIdxAttempt, DmtxModuleOnGreen);
/* Reset DmtxModuleAssigned and DMX_MODULE_VISITED bits */
for(row = 0; row < mappingRows; row++) {
for(col = 0; col < mappingCols; col++) {
enc->message->array[row*mappingCols+col] &= (0xff ^ (DmtxModuleAssigned | DmtxModuleVisited));
}
}
ModulePlacementEcc200(enc->message->array, encB->message->code, sizeIdxAttempt, DmtxModuleOnBlue);
/* Destroy encR, encG, and encB */
dmtxEncodeDestroy(&encR);
dmtxEncodeDestroy(&encG);
dmtxEncodeDestroy(&encB);
PrintPattern(enc);
return DmtxPass;
}
/**
* \brief Convert input into message using specific encodation scheme
* \param buf
* \param inputString
* \param inputSize
* \param scheme
* \param sizeIdx
* \param bReaderProgramming
* \return Count of encoded data words
*
* Future: pass DmtxEncode to this function with an error reason field, which
* goes to EncodeSingle... too
*/
#ifdef HAVE_READER_PROGRAMMING
static int
EncodeDataCodewords(DmtxByteList *input, DmtxByteList *output, int sizeIdxRequest, DmtxScheme scheme, int fnc1, DmtxBoolean bReaderProgramming)
#else
static int
EncodeDataCodewords(DmtxByteList *input, DmtxByteList *output, int sizeIdxRequest, DmtxScheme scheme, int fnc1)
#endif
{
int sizeIdx;
/* Encode input string into data codewords */
switch(scheme)
{
case DmtxSchemeAutoBest:
sizeIdx = EncodeOptimizeBest(input, output, sizeIdxRequest, fnc1);
break;
case DmtxSchemeAutoFast:
sizeIdx = DmtxUndefined; /* EncodeAutoFast(input, output, sizeIdxRequest, passFail); */
break;
default:
#ifdef HAVE_READER_PROGRAMMING
sizeIdx = EncodeSingleScheme(input, output, sizeIdxRequest, scheme, fnc1, bReaderProgramming);
#else
sizeIdx = EncodeSingleScheme(input, output, sizeIdxRequest, scheme, fnc1);
#endif
break;
}
return sizeIdx;
}
/**
* \brief Write encoded message to image
* \param enc
* \return void
*/
static void
PrintPattern(DmtxEncode *enc)
{
int i, j;
int symbolRow, symbolCol;
int pixelRow, pixelCol;
int moduleStatus;
size_t rowSize, height;
int rgb[3];
double sxy, txy;
DmtxMatrix3 m1, m2;
DmtxVector2 vIn, vOut;
txy = enc->marginSize;
sxy = 1.0/enc->moduleSize;
dmtxMatrix3Translate(m1, -txy, -txy);
dmtxMatrix3Scale(m2, sxy, -sxy);
dmtxMatrix3Multiply(enc->xfrm, m1, m2);
dmtxMatrix3Translate(m1, txy, txy);
dmtxMatrix3Scale(m2, enc->moduleSize, enc->moduleSize);
dmtxMatrix3Multiply(enc->rxfrm, m2, m1);
rowSize = dmtxImageGetProp(enc->image, DmtxPropRowSizeBytes);
height = dmtxImageGetProp(enc->image, DmtxPropHeight);
memset(enc->image->pxl, 0xff, rowSize * height);
for(symbolRow = 0; symbolRow < enc->region.symbolRows; symbolRow++) {
for(symbolCol = 0; symbolCol < enc->region.symbolCols; symbolCol++) {
vIn.X = symbolCol;
vIn.Y = symbolRow;
dmtxMatrix3VMultiply(&vOut, &vIn, enc->rxfrm);
pixelCol = (int)(vOut.X);
pixelRow = (int)(vOut.Y);
moduleStatus = dmtxSymbolModuleStatus(enc->message,
enc->region.sizeIdx, symbolRow, symbolCol);
if (enc->image->bytesPerPixel == 1)
{
for(i = pixelRow; i < pixelRow + enc->moduleSize; i++) {
for(j = pixelCol; j < pixelCol + enc->moduleSize; j++) {
rgb[0] = ((moduleStatus & DmtxModuleOnRed) != 0x00) ? 0 : 255;
dmtxImageSetPixelValue(enc->image, j, i, 0, rgb[0]);
}
}
}
else
{
for(i = pixelRow; i < pixelRow + enc->moduleSize; i++) {
for(j = pixelCol; j < pixelCol + enc->moduleSize; j++) {
rgb[0] = ((moduleStatus & DmtxModuleOnRed) != 0x00) ? 0 : 255;
rgb[1] = ((moduleStatus & DmtxModuleOnGreen) != 0x00) ? 0 : 255;
rgb[2] = ((moduleStatus & DmtxModuleOnBlue) != 0x00) ? 0 : 255;
/* dmtxImageSetRgb(enc->image, j, i, rgb); */
dmtxImageSetPixelValue(enc->image, j, i, 0, rgb[0]);
dmtxImageSetPixelValue(enc->image, j, i, 1, rgb[1]);
dmtxImageSetPixelValue(enc->image, j, i, 2, rgb[2]);
}
}
}
}
}
}