wxMaxima
Loading...
Searching...
No Matches
dmatrix_trace.h
1/* dmatrix_trace.h - Trace routines for DM_COMPRESSION algorithm */
2/*
3 libzint - the open source barcode library
4 Copyright (C) 2021-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_DMATRIX_TRACE_H
34#define Z_DMATRIX_TRACE_H
35
36static int DM_TRACE_getPreviousMode(struct dm_edge *edges, struct dm_edge *edge) {
37 struct dm_edge *previous = DM_PREVIOUS(edges, edge);
38 return previous == NULL ? DM_ASCII : previous->endMode;
39}
40
41static void DM_TRACE_VertexToString(const unsigned char *source, const int length, const int position,
42 struct dm_edge *edge) {
43 if (position >= length) {
44 printf("end mode %s", dm_smodes[edge->mode]);
45 } else {
46 printf("char '%c' at %d mode %s", source[position], position, dm_smodes[edge->mode]);
47 }
48}
49
50static void DM_TRACE_EdgeToString(char *buf, const unsigned char *source, const int length, struct dm_edge *edges,
51 struct dm_edge *edge) {
52 int previousMode = DM_TRACE_getPreviousMode(edges, edge);
53 (void)length;
54 if (buf) {
55 sprintf(buf, "%d_%s %s(%.*s) (%d) --> %d_%s",
56 edge->from, dm_smodes[previousMode], dm_smodes[edge->mode], edge->len, source + edge->from, edge->size,
57 edge->from + edge->len, dm_smodes[edge->mode]);
58 } else {
59 printf("%d_%s %s(%.*s) (%d) --> %d_%s",
60 edge->from, dm_smodes[previousMode], dm_smodes[edge->mode], edge->len, source + edge->from, edge->size,
61 edge->from + edge->len, dm_smodes[edge->mode]);
62 }
63}
64
65static void DM_TRACE_Path(const unsigned char *source, const int length, struct dm_edge *edges,
66 struct dm_edge *edge, char *result, const int result_size) {
67 struct dm_edge *current;
68 DM_TRACE_EdgeToString(result, source, length, edges, edge);
69 current = DM_PREVIOUS(edges, edge);
70 while (current) {
71 char s[256];
72 char *pos;
73 int len;
74 DM_TRACE_EdgeToString(s, source, length, edges, current);
75 pos = strrchr(s, ' ');
76 assert(pos);
77 len = strlen(result);
78 if ((pos - s) + 1 + len + 1 >= result_size) {
79 memcpy(result + result_size - 4, "...", 4); /* Include terminating NUL */
80 break;
81 }
82 memmove(result + (pos - s) + 1, result, len + 1);
83 memcpy(result, s, (pos - s) + 1);
84 current = DM_PREVIOUS(edges, current);
85 }
86 puts(result);
87}
88
89static void DM_TRACE_Edges(const char *prefix, const unsigned char *source, const int length,
90 struct dm_edge *edges, const int vertexIndex) {
91 int i, j, e_i;
92 char result[1024 * 2];
93 if (vertexIndex) {
94 printf(prefix, vertexIndex);
95 } else {
96 fputs(prefix, stdout);
97 }
98 for (i = vertexIndex; i <= length; i++) {
99 e_i = i * DM_NUM_MODES;
100 for (j = 0; j < DM_NUM_MODES; j++) {
101 if (edges[e_i + j].mode) {
102 fputs("DEBUG ", stdout);
103 DM_TRACE_Path(source, length, edges, edges + e_i + j, result, (int) ARRAY_SIZE(result));
104 }
105 }
106 }
107}
108
109static void DM_TRACE_AddEdge(const unsigned char *source, const int length, struct dm_edge *edges,
110 struct dm_edge *previous, const int vertexIndex, struct dm_edge *edge) {
111 if (previous == NULL) {
112 fputs("DEBUG add ", stdout);
113 DM_TRACE_EdgeToString(NULL, source, length, edges, edge);
114 printf(" from %d to %d size %d\n", edge->from, vertexIndex, edge->size);
115 } else {
116 fputs("DEBUG add ", stdout);
117 DM_TRACE_EdgeToString(NULL, source, length, edges, edge);
118 fputs(" from ", stdout);
119 DM_TRACE_VertexToString(source, length, previous->from, previous);
120 fputs(" to ", stdout);
121 DM_TRACE_VertexToString(source, length, vertexIndex, edge);
122 printf(" size %d\n", edge->size);
123 }
124}
125
126static void DM_TRACE_NotAddEdge(const unsigned char *source, const int length, struct dm_edge *edges,
127 struct dm_edge *previous, const int vertexIndex, const int e_ij, struct dm_edge *edge) {
128 if (previous == NULL) {
129 fputs("DEBUG not add ", stdout);
130 DM_TRACE_EdgeToString(NULL, source, length, edges, edge);
131 printf(" from %d to %d size %d since ", edge->from, vertexIndex, edge->size);
132 DM_TRACE_EdgeToString(NULL, source, length, edges, edges + e_ij);
133 printf(" < size %d\n", edges[e_ij].size);
134 } else {
135 fputs("DEBUG not add ", stdout);
136 DM_TRACE_EdgeToString(NULL, source, length, edges, edge);
137 fputs(" from ", stdout);
138 DM_TRACE_VertexToString(source, length, previous->from, previous);
139 fputs(" to ", stdout);
140 DM_TRACE_VertexToString(source, length, vertexIndex, edge);
141 printf(" size %d since ", edge->size);
142 DM_TRACE_EdgeToString(NULL, source, length, edges, edges + e_ij);
143 printf(" < size %d\n", edges[e_ij].size);
144 }
145}
146
147/* vim: set ts=4 sw=4 et : */
148#endif /* Z_DMATRIX_TRACE_H */
Definition: dmatrix.c:696