-
Notifications
You must be signed in to change notification settings - Fork 137
/
dmtxencodeoptimize.c
441 lines (387 loc) · 13.2 KB
/
dmtxencodeoptimize.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
/**
* libdmtx - Data Matrix Encoding/Decoding Library
* Copyright 2011 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 dmtxencodeoptimize.c
* \brief Logic for optimized (multiple scheme) encoding
*/
#define DUMPSTREAMS 0
enum SchemeState {
AsciiFull,
AsciiCompactOffset0, /* 0 offset from first regular input value */
AsciiCompactOffset1,
C40Offset0, /* 0 offset from first expanded C40 value */
C40Offset1,
C40Offset2,
TextOffset0, /* 0 offset from first expanded Text value */
TextOffset1,
TextOffset2,
X12Offset0, /* 0 offset from first expanded X12 value */
X12Offset1,
X12Offset2,
EdifactOffset0, /* 0 offset from first regular input value */
EdifactOffset1,
EdifactOffset2,
EdifactOffset3,
Base256,
SchemeStateCount
};
#if DUMPSTREAMS
static void DumpStreams(DmtxEncodeStream *streamBest)
{
enum SchemeState state;
char prefix[32];
fprintf(stdout, "----------------------------------------\n");
for(state = 0; state < SchemeStateCount; state++)
{
if(streamBest[state].status == DmtxStatusEncoding ||
streamBest[state].status == DmtxStatusComplete)
fprintf(stdout, "\"%c\" ", streamBest[state].input->b[streamBest[state].inputNext-1]);
else
fprintf(stdout, " ");
switch(streamBest[state].status) {
case DmtxStatusEncoding:
snprintf(prefix, sizeof(prefix), "%2d (%s): ", state, " encode ");
break;
case DmtxStatusComplete:
snprintf(prefix, sizeof(prefix), "%2d (%s): ", state, "complete");
break;
case DmtxStatusInvalid:
snprintf(prefix, sizeof(prefix), "%2d (%s): ", state, "invalid ");
break;
case DmtxStatusFatal:
snprintf(prefix, sizeof(prefix), "%2d (%s): ", state, " fatal ");
break;
}
dmtxByteListPrint(streamBest[state].output, prefix);
}
}
#endif
/**
*
*
*/
static int
EncodeOptimizeBest(DmtxByteList *input, DmtxByteList *output, int sizeIdxRequest, int fnc1)
{
enum SchemeState state;
int inputNext, c40ValueCount, textValueCount, x12ValueCount;
int sizeIdx;
DmtxEncodeStream *winner;
DmtxPassFail passFail;
DmtxEncodeStream streamsBest[SchemeStateCount];
DmtxEncodeStream streamsTemp[SchemeStateCount];
DmtxByte outputsBestStorage[SchemeStateCount][4096];
DmtxByte outputsTempStorage[SchemeStateCount][4096];
DmtxByte ctxTempStorage[4];
DmtxByteList outputsBest[SchemeStateCount];
DmtxByteList outputsTemp[SchemeStateCount];
DmtxByteList ctxTemp = dmtxByteListBuild(ctxTempStorage, sizeof(ctxTempStorage));
/* Initialize all streams with their own output storage */
for(state = 0; state < SchemeStateCount; state++)
{
outputsBest[state] = dmtxByteListBuild(outputsBestStorage[state], sizeof(outputsBestStorage[state]));
outputsTemp[state] = dmtxByteListBuild(outputsTempStorage[state], sizeof(outputsTempStorage[state]));
streamsBest[state] = StreamInit(input, &(outputsBest[state]));
streamsTemp[state] = StreamInit(input, &(outputsTemp[state]));
streamsBest[state].fnc1 = fnc1;
streamsTemp[state].fnc1 = fnc1;
}
c40ValueCount = textValueCount = x12ValueCount = 0;
for(inputNext = 0; inputNext < input->length; inputNext++)
{
StreamAdvanceFromBest(streamsTemp, streamsBest, AsciiFull, sizeIdxRequest);
AdvanceAsciiCompact(streamsTemp, streamsBest, AsciiCompactOffset0, inputNext, sizeIdxRequest);
AdvanceAsciiCompact(streamsTemp, streamsBest, AsciiCompactOffset1, inputNext, sizeIdxRequest);
AdvanceCTX(streamsTemp, streamsBest, C40Offset0, inputNext, c40ValueCount, sizeIdxRequest);
AdvanceCTX(streamsTemp, streamsBest, C40Offset1, inputNext, c40ValueCount, sizeIdxRequest);
AdvanceCTX(streamsTemp, streamsBest, C40Offset2, inputNext, c40ValueCount, sizeIdxRequest);
AdvanceCTX(streamsTemp, streamsBest, TextOffset0, inputNext, textValueCount, sizeIdxRequest);
AdvanceCTX(streamsTemp, streamsBest, TextOffset1, inputNext, textValueCount, sizeIdxRequest);
AdvanceCTX(streamsTemp, streamsBest, TextOffset2, inputNext, textValueCount, sizeIdxRequest);
AdvanceCTX(streamsTemp, streamsBest, X12Offset0, inputNext, x12ValueCount, sizeIdxRequest);
AdvanceCTX(streamsTemp, streamsBest, X12Offset1, inputNext, x12ValueCount, sizeIdxRequest);
AdvanceCTX(streamsTemp, streamsBest, X12Offset2, inputNext, x12ValueCount, sizeIdxRequest);
AdvanceEdifact(streamsTemp, streamsBest, EdifactOffset0, inputNext, sizeIdxRequest);
AdvanceEdifact(streamsTemp, streamsBest, EdifactOffset1, inputNext, sizeIdxRequest);
AdvanceEdifact(streamsTemp, streamsBest, EdifactOffset2, inputNext, sizeIdxRequest);
AdvanceEdifact(streamsTemp, streamsBest, EdifactOffset3, inputNext, sizeIdxRequest);
StreamAdvanceFromBest(streamsTemp, streamsBest, Base256, sizeIdxRequest);
/* Overwrite best streams with new results */
for(state = 0; state < SchemeStateCount; state++)
{
if(streamsBest[state].status != DmtxStatusComplete)
StreamCopy(&(streamsBest[state]), &(streamsTemp[state]));
}
dmtxByteListClear(&ctxTemp);
PushCTXValues(&ctxTemp, input->b[inputNext], DmtxSchemeC40, &passFail, fnc1);
c40ValueCount += ((passFail == DmtxPass) ? ctxTemp.length : 1);
dmtxByteListClear(&ctxTemp);
PushCTXValues(&ctxTemp, input->b[inputNext], DmtxSchemeText, &passFail, fnc1);
textValueCount += ((passFail == DmtxPass) ? ctxTemp.length : 1);
dmtxByteListClear(&ctxTemp);
PushCTXValues(&ctxTemp, input->b[inputNext], DmtxSchemeX12, &passFail, fnc1);
x12ValueCount += ((passFail == DmtxPass) ? ctxTemp.length : 1);
#if DUMPSTREAMS
DumpStreams(streamsBest);
#endif
}
/* Choose the overall winner */
winner = NULL;
for(state = 0; state < SchemeStateCount; state++)
{
if(streamsBest[state].status == DmtxStatusComplete)
{
if(winner == NULL || streamsBest[state].output->length < winner->output->length)
winner = &(streamsBest[state]);
}
}
/* Copy winner to output */
if(winner == NULL)
{
sizeIdx = DmtxUndefined;
}
else
{
dmtxByteListCopy(output, winner->output, &passFail);
sizeIdx = (passFail == DmtxPass) ? winner->sizeIdx : DmtxUndefined;
}
return sizeIdx;
}
/**
* It's safe to compare output length because all targetState combinations
* start on same input and encodes same number of inputs. Only difference
* is the number of latches/unlatches that are also encoded
*/
static void
StreamAdvanceFromBest(DmtxEncodeStream *streamsNext, DmtxEncodeStream *streamsBest,
int targetState, int sizeIdxRequest)
{
enum SchemeState fromState;
DmtxScheme targetScheme;
DmtxEncodeOption encodeOption;
DmtxByte outputTempStorage[4096];
DmtxByteList outputTemp = dmtxByteListBuild(outputTempStorage, sizeof(outputTempStorage));
DmtxEncodeStream streamTemp;
DmtxEncodeStream *targetStream = &(streamsNext[targetState]);
streamTemp.output = &outputTemp; /* Set directly instead of calling StreamInit() */
targetScheme = GetScheme(targetState);
if(targetState == AsciiFull)
encodeOption = DmtxEncodeFull;
else if(targetState == AsciiCompactOffset0 || targetState == AsciiCompactOffset1)
encodeOption = DmtxEncodeCompact;
else
encodeOption = DmtxEncodeNormal;
for(fromState = 0; fromState < SchemeStateCount; fromState++)
{
if(streamsBest[fromState].status != DmtxStatusEncoding ||
ValidStateSwitch(fromState, targetState) == DmtxFalse)
{
continue;
}
StreamCopy(&streamTemp, &(streamsBest[fromState]));
EncodeNextChunk(&streamTemp, targetScheme, encodeOption, sizeIdxRequest);
if(fromState == 0 || (streamTemp.status != DmtxStatusInvalid &&
streamTemp.output->length < targetStream->output->length))
{
StreamCopy(targetStream, &streamTemp);
}
}
}
/**
*
*/
static void
AdvanceAsciiCompact(DmtxEncodeStream *streamsNext, DmtxEncodeStream *streamsBest,
int targetState, int inputNext, int sizeIdxRequest)
{
DmtxEncodeStream *currentStream = &(streamsBest[targetState]);
DmtxEncodeStream *targetStream = &(streamsNext[targetState]);
DmtxBoolean isStartState;
switch(targetState)
{
case AsciiCompactOffset0:
isStartState = (inputNext % 2 == 0) ? DmtxTrue : DmtxFalse;
break;
case AsciiCompactOffset1:
isStartState = (inputNext % 2 == 1) ? DmtxTrue : DmtxFalse;
break;
default:
StreamMarkFatal(targetStream, DmtxErrorIllegalParameterValue);
return;
}
if(inputNext < currentStream->inputNext)
{
StreamCopy(targetStream, currentStream);
}
else if(isStartState == DmtxTrue)
{
StreamAdvanceFromBest(streamsNext, streamsBest, targetState, sizeIdxRequest);
}
else
{
StreamCopy(targetStream, currentStream);
StreamMarkInvalid(targetStream, DmtxErrorUnknown);
}
}
/**
*
*/
static void
AdvanceCTX(DmtxEncodeStream *streamsNext, DmtxEncodeStream *streamsBest,
int targetState, int inputNext, int ctxValueCount, int sizeIdxRequest)
{
DmtxEncodeStream *currentStream = &(streamsBest[targetState]);
DmtxEncodeStream *targetStream = &(streamsNext[targetState]);
DmtxBoolean isStartState;
/* we won't actually use inputNext here */
switch(targetState)
{
case C40Offset0:
case TextOffset0:
case X12Offset0:
isStartState = (ctxValueCount % 3 == 0) ? DmtxTrue : DmtxFalse;
break;
case C40Offset1:
case TextOffset1:
case X12Offset1:
isStartState = (ctxValueCount % 3 == 1) ? DmtxTrue : DmtxFalse;
break;
case C40Offset2:
case TextOffset2:
case X12Offset2:
isStartState = (ctxValueCount % 3 == 2) ? DmtxTrue : DmtxFalse;
break;
default:
StreamMarkFatal(targetStream, DmtxErrorIllegalParameterValue);
return;
}
if(inputNext < currentStream->inputNext)
{
StreamCopy(targetStream, currentStream);
}
else if(isStartState == DmtxTrue)
{
StreamAdvanceFromBest(streamsNext, streamsBest, targetState, sizeIdxRequest);
}
else
{
StreamCopy(targetStream, currentStream);
StreamMarkInvalid(targetStream, DmtxErrorUnknown);
}
}
/**
*
*/
static void
AdvanceEdifact(DmtxEncodeStream *streamsNext, DmtxEncodeStream *streamsBest,
int targetState, int inputNext, int sizeIdxRequest)
{
DmtxEncodeStream *currentStream = &(streamsBest[targetState]);
DmtxEncodeStream *targetStream = &(streamsNext[targetState]);
DmtxBoolean isStartState;
switch(targetState)
{
case EdifactOffset0:
isStartState = (inputNext % 4 == 0) ? DmtxTrue : DmtxFalse;
break;
case EdifactOffset1:
isStartState = (inputNext % 4 == 1) ? DmtxTrue : DmtxFalse;
break;
case EdifactOffset2:
isStartState = (inputNext % 4 == 2) ? DmtxTrue : DmtxFalse;
break;
case EdifactOffset3:
isStartState = (inputNext % 4 == 3) ? DmtxTrue : DmtxFalse;
break;
default:
StreamMarkFatal(targetStream, DmtxErrorIllegalParameterValue);
return;
}
if(isStartState == DmtxTrue)
{
StreamAdvanceFromBest(streamsNext, streamsBest, targetState, sizeIdxRequest);
}
else
{
StreamCopy(targetStream, currentStream);
if(currentStream->status == DmtxStatusEncoding && currentStream->currentScheme == DmtxSchemeEdifact)
EncodeNextChunk(targetStream, DmtxSchemeEdifact, DmtxEncodeNormal, sizeIdxRequest);
else
StreamMarkInvalid(targetStream, DmtxErrorUnknown);
}
}
/**
*
*
*/
static int
GetScheme(int state)
{
DmtxScheme scheme;
switch(state)
{
case AsciiFull:
case AsciiCompactOffset0:
case AsciiCompactOffset1:
scheme = DmtxSchemeAscii;
break;
case C40Offset0:
case C40Offset1:
case C40Offset2:
scheme = DmtxSchemeC40;
break;
case TextOffset0:
case TextOffset1:
case TextOffset2:
scheme = DmtxSchemeText;
break;
case X12Offset0:
case X12Offset1:
case X12Offset2:
scheme = DmtxSchemeX12;
break;
case EdifactOffset0:
case EdifactOffset1:
case EdifactOffset2:
case EdifactOffset3:
scheme = DmtxSchemeEdifact;
break;
case Base256:
scheme = DmtxSchemeBase256;
break;
default:
scheme = DmtxUndefined;
break;
}
return scheme;
}
/**
*
*
*/
static DmtxBoolean
ValidStateSwitch(int fromState, int targetState)
{
DmtxBoolean validStateSwitch;
DmtxScheme fromScheme = GetScheme(fromState);
DmtxScheme toScheme = GetScheme(targetState);
if(fromScheme == toScheme && fromState != targetState &&
fromState != AsciiFull && targetState != AsciiFull)
{
validStateSwitch = DmtxFalse;
}
else
{
validStateSwitch = DmtxTrue;
}
return validStateSwitch;
}