comparison mupdf-source/thirdparty/zint/backend/dmatrix_trace.h @ 2:b50eed0cc0ef upstream

ADD: MuPDF v1.26.7: the MuPDF source as downloaded by a default build of PyMuPDF 1.26.4. The directory name has changed: no version number in the expanded directory now.
author Franz Glasner <fzglas.hg@dom66.de>
date Mon, 15 Sep 2025 11:43:07 +0200
parents
children
comparison
equal deleted inserted replaced
1:1d09e1dec1d9 2:b50eed0cc0ef
1 /* dmatrix_trace.h - Trace routines for DM_COMPRESSION algorithm */
2 /*
3 libzint - the open source barcode library
4 Copyright (C) 2021-2022 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
36 #ifndef DM_TRACE
37 #define DM_TRACE_Edges(px, s, l, p, v)
38 #define DM_TRACE_AddEdge(s, l, es, p, v, e)
39 #define DM_TRACE_NotAddEdge(s, l, es, p, v, ij, e)
40 #else
41
42 static int DM_TRACE_getPreviousMode(struct dm_edge *edges, struct dm_edge *edge) {
43 struct dm_edge *previous = DM_PREVIOUS(edges, edge);
44 return previous == NULL ? DM_ASCII : previous->endMode;
45 }
46
47 static void DM_TRACE_VertexToString(const unsigned char *source, const int length, const int position,
48 struct dm_edge *edge) {
49 if (position >= length) {
50 printf("end mode %s", dm_smodes[edge->mode]);
51 } else {
52 printf("char '%c' at %d mode %s", source[position], position, dm_smodes[edge->mode]);
53 }
54 }
55
56 static void DM_TRACE_EdgeToString(char *buf, const unsigned char *source, const int length, struct dm_edge *edges,
57 struct dm_edge *edge) {
58 int previousMode = DM_TRACE_getPreviousMode(edges, edge);
59 (void)length;
60 if (buf) {
61 sprintf(buf, "%d_%s %s(%.*s) (%d) --> %d_%s",
62 edge->from, dm_smodes[previousMode], dm_smodes[edge->mode], edge->len, source + edge->from, edge->size,
63 edge->from + edge->len, dm_smodes[edge->mode]);
64 } else {
65 printf("%d_%s %s(%.*s) (%d) --> %d_%s",
66 edge->from, dm_smodes[previousMode], dm_smodes[edge->mode], edge->len, source + edge->from, edge->size,
67 edge->from + edge->len, dm_smodes[edge->mode]);
68 }
69 }
70
71 static void DM_TRACE_Path(const unsigned char *source, const int length, struct dm_edge *edges,
72 struct dm_edge *edge, char *result, const int result_size) {
73 DM_TRACE_EdgeToString(result, source, length, edges, edge);
74 struct dm_edge *current = DM_PREVIOUS(edges, edge);
75 while (current) {
76 char s[256];
77 char *pos;
78 int len;
79 DM_TRACE_EdgeToString(s, source, length, edges, current);
80 pos = strrchr(s, ' ');
81 assert(pos);
82 len = strlen(result);
83 if ((pos - s) + 1 + len + 1 >= result_size) {
84 result[result_size - 4] = '\0';
85 strcat(result, "...");
86 break;
87 }
88 memmove(result + (pos - s) + 1, result, len + 1);
89 memcpy(result, s, (pos - s) + 1);
90 current = DM_PREVIOUS(edges, current);
91 }
92 puts(result);
93 }
94
95 static void DM_TRACE_Edges(const char *prefix, const unsigned char *source, const int length,
96 struct dm_edge *edges, const int vertexIndex) {
97 int i, j, e_i;
98 char result[1024 * 2];
99 if (vertexIndex) {
100 printf(prefix, vertexIndex);
101 } else {
102 fputs(prefix, stdout);
103 }
104 for (i = vertexIndex; i <= length; i++) {
105 e_i = i * DM_NUM_MODES;
106 for (j = 0; j < DM_NUM_MODES; j++) {
107 if (edges[e_i + j].mode) {
108 fputs("DEBUG ", stdout);
109 DM_TRACE_Path(source, length, edges, edges + e_i + j, result, (int) ARRAY_SIZE(result));
110 }
111 }
112 }
113 }
114
115 static void DM_TRACE_AddEdge(const unsigned char *source, const int length, struct dm_edge *edges,
116 struct dm_edge *previous, const int vertexIndex, struct dm_edge *edge) {
117 if (previous == NULL) {
118 fputs("DEBUG add ", stdout);
119 DM_TRACE_EdgeToString(NULL, source, length, edges, edge);
120 printf(" from %d to %d size %d\n", edge->from, vertexIndex, edge->size);
121 } else {
122 fputs("DEBUG add ", stdout);
123 DM_TRACE_EdgeToString(NULL, source, length, edges, edge);
124 fputs(" from ", stdout);
125 DM_TRACE_VertexToString(source, length, previous->from, previous);
126 fputs(" to ", stdout);
127 DM_TRACE_VertexToString(source, length, vertexIndex, edge);
128 printf(" size %d\n", edge->size);
129 }
130 }
131
132 static void DM_TRACE_NotAddEdge(const unsigned char *source, const int length, struct dm_edge *edges,
133 struct dm_edge *previous, const int vertexIndex, const int e_ij, struct dm_edge *edge) {
134 if (previous == NULL) {
135 fputs("DEBUG not add ", stdout);
136 DM_TRACE_EdgeToString(NULL, source, length, edges, edge);
137 printf(" from %d to %d size %d since ", edge->from, vertexIndex, edge->size);
138 DM_TRACE_EdgeToString(NULL, source, length, edges, edges + e_ij);
139 printf(" < size %d\n", edges[e_ij].size);
140 } else {
141 fputs("DEBUG not add ", stdout);
142 DM_TRACE_EdgeToString(NULL, source, length, edges, edge);
143 fputs(" from ", stdout);
144 DM_TRACE_VertexToString(source, length, previous->from, previous);
145 fputs(" to ", stdout);
146 DM_TRACE_VertexToString(source, length, vertexIndex, edge);
147 printf(" size %d since ", edge->size);
148 DM_TRACE_EdgeToString(NULL, source, length, edges, edges + e_ij);
149 printf(" < size %d\n", edges[e_ij].size);
150 }
151 }
152
153 #endif /* DM_TRACE */
154 /* vim: set ts=4 sw=4 et : */
155 #endif /* Z_DMATRIX_TRACE_H */