wxMaxima
Loading...
Searching...
No Matches
pdf417_trace.h
1/* pdf417_trace.h - Trace routines for optimal PDF417 optimization algorithm */
2/*
3 libzint - the open source barcode library
4 Copyright (C) 2022-2025 Robin Stuart <rstuart114@gmail.com>
5
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions
8 are met:
9
10 1. Redistributions of source code must retain the above copyright
11 notice, this list of conditions and the following disclaimer.
12 2. Redistributions in binary form must reproduce the above copyright
13 notice, this list of conditions and the following disclaimer in the
14 documentation and/or other materials provided with the distribution.
15 3. Neither the name of the project nor the names of its contributors
16 may be used to endorse or promote products derived from this software
17 without specific prior written permission.
18
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
23 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 SUCH DAMAGE.
30 */
31/* SPDX-License-Identifier: BSD-3-Clause */
32
33#ifndef Z_PDF417_TRACE_H
34#define Z_PDF417_TRACE_H
35
36static int PDF_TRACE_getPreviousMode(struct pdf_edge *edges, struct pdf_edge *edge) {
37 struct pdf_edge *previous = PDF_PREVIOUS(edges, edge);
38 return previous == NULL ? PDF_ALP : previous->mode;
39}
40
41static void PDF_TRACE_EdgeToString(char *buf, const unsigned char *source, const int length, struct pdf_edge *edges,
42 struct pdf_edge *edge) {
43 int previousMode = PDF_TRACE_getPreviousMode(edges, edge);
44 (void)length;
45 if (buf) {
46 sprintf(buf, "%d_%c %c(%d,%d) %d(%d,%d,%d) -> %d_%c",
47 edge->from, pdf_smodes[previousMode], pdf_smodes[edge->mode], source[edge->from], edge->len, edge->size
48 + edge->unit_size, edge->units, edge->unit_size, edge->size, edge->from + 1, pdf_smodes[edge->mode]);
49 } else {
50 printf("%d_%c %c(%d,%d) %d(%d,%d,%d) -> %d_%c",
51 edge->from, pdf_smodes[previousMode], pdf_smodes[edge->mode], source[edge->from], edge->len, edge->size
52 + edge->unit_size, edge->units, edge->unit_size, edge->size, edge->from + 1, pdf_smodes[edge->mode]);
53 }
54}
55
56static void PDF_TRACE_Path(const unsigned char *source, const int length, struct pdf_edge *edges,
57 struct pdf_edge *edge, char *result, const int result_size) {
58 struct pdf_edge *current;
59 PDF_TRACE_EdgeToString(result, source, length, edges, edge);
60 current = PDF_PREVIOUS(edges, edge);
61 while (current) {
62 char s[256];
63 char *pos;
64 int len;
65 PDF_TRACE_EdgeToString(s, source, length, edges, current);
66 pos = strrchr(s, ' ');
67 assert(pos);
68 len = strlen(result);
69 if ((pos - s) + 1 + len + 1 >= result_size) {
70 memcpy(result + result_size - 4, "...", 4); /* Include terminating NUL */
71 break;
72 }
73 memmove(result + (pos - s) + 1, result, len + 1);
74 memcpy(result, s, (pos - s) + 1);
75 current = PDF_PREVIOUS(edges, current);
76 }
77 puts(result);
78}
79
80static void PDF_TRACE_Edges(const char *prefix, const unsigned char *source, const int length,
81 struct pdf_edge *edges, const int vertexIndex) {
82 int i, j, e_i;
83 char result[1024 * 2];
84 if (vertexIndex) {
85 printf(prefix, vertexIndex);
86 } else {
87 fputs(prefix, stdout);
88 }
89 for (i = vertexIndex; i <= length; i++) {
90 e_i = i * PDF_NUM_MODES;
91 for (j = 0; j < PDF_NUM_MODES; j++) {
92 if (edges[e_i + j].mode) {
93 fputs(" **** ", stdout);
94 PDF_TRACE_Path(source, length, edges, edges + e_i + j, result, (int) ARRAY_SIZE(result));
95 }
96 }
97 }
98}
99
100static void PDF_TRACE_AddEdge(const unsigned char *source, const int length, struct pdf_edge *edges,
101 struct pdf_edge *previous, const int vertexIndex, const int t_table, struct pdf_edge *edge) {
102 const int new_size = edge->size + edge->unit_size;
103 const int v_ij = vertexIndex * PDF_NUM_MODES + edge->mode - 1;
104 const int v_size = edges[v_ij].size + edges[v_ij].unit_size;
105
106 (void)source; (void)length;
107
108 printf("add mode %c, t_table 0x%X, previous %d, from %d, len %d, v_ij %d, %d(%d,%d,%d) > %d(%d,%d,%d)\n",
109 pdf_smodes[edge->mode], t_table, previous ? (int) (previous - edges) : 0, edge->from, edge->len, v_ij,
110 v_size, edges[v_ij].units, edges[v_ij].unit_size, edges[v_ij].size,
111 new_size, edge->units, edge->unit_size, edge->size);
112}
113
114static void PDF_TRACE_NotAddEdge(const unsigned char *source, const int length, struct pdf_edge *edges,
115 struct pdf_edge *previous, const int vertexIndex, const int t_table, struct pdf_edge *edge) {
116 const int new_size = edge->size + edge->unit_size;
117 const int v_ij = vertexIndex * PDF_NUM_MODES + edge->mode - 1;
118 const int v_size = edges[v_ij].size + edges[v_ij].unit_size;
119
120 (void)source; (void)length;
121
122 printf("NOT mode %c, t_table %d, previous %d, from %d, len %d, v_ij %d, %d(%d,%d,%d) <= %d(%d,%d,%d)\n",
123 pdf_smodes[edge->mode], t_table, previous ? (int) (previous - edges) : 0, edge->from, edge->len, v_ij,
124 v_size, edges[v_ij].units, edges[v_ij].unit_size, edges[v_ij].size,
125 new_size, edge->units, edge->unit_size, edge->size);
126}
127
128/* vim: set ts=4 sw=4 et : */
129#endif /* Z_PDF417_TRACE_H */
Definition: pdf417.c:694