dmatrix_trace.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. Redistribution and use in source and binary forms, with or without
  6. modification, are permitted provided that the following conditions
  7. are met:
  8. 1. Redistributions of source code must retain the above copyright
  9. notice, this list of conditions and the following disclaimer.
  10. 2. Redistributions in binary form must reproduce the above copyright
  11. notice, this list of conditions and the following disclaimer in the
  12. documentation and/or other materials provided with the distribution.
  13. 3. Neither the name of the project nor the names of its contributors
  14. may be used to endorse or promote products derived from this software
  15. without specific prior written permission.
  16. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  17. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19. ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
  20. FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  22. OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  23. HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  24. LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  25. OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  26. SUCH DAMAGE.
  27. */
  28. /* SPDX-License-Identifier: BSD-3-Clause */
  29. #ifndef Z_DMATRIX_TRACE_H
  30. #define Z_DMATRIX_TRACE_H
  31. #ifndef DM_TRACE
  32. #define DM_TRACE_Edges(px, s, l, p, v)
  33. #define DM_TRACE_AddEdge(s, l, es, p, v, e)
  34. #define DM_TRACE_NotAddEdge(s, l, es, p, v, ij, e)
  35. #else
  36. static 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. static void DM_TRACE_VertexToString(const unsigned char *source, const int length, const int position,
  41. struct dm_edge *edge) {
  42. if (position >= length) {
  43. printf("end mode %s", dm_smodes[edge->mode]);
  44. } else {
  45. printf("char '%c' at %d mode %s", source[position], position, dm_smodes[edge->mode]);
  46. }
  47. }
  48. static void DM_TRACE_EdgeToString(char *buf, const unsigned char *source, const int length, struct dm_edge *edges,
  49. struct dm_edge *edge) {
  50. int previousMode = DM_TRACE_getPreviousMode(edges, edge);
  51. (void)length;
  52. if (buf) {
  53. sprintf(buf, "%d_%s %s(%.*s) (%d) --> %d_%s",
  54. edge->from, dm_smodes[previousMode], dm_smodes[edge->mode], edge->len, source + edge->from, edge->size,
  55. edge->from + edge->len, dm_smodes[edge->mode]);
  56. } else {
  57. printf("%d_%s %s(%.*s) (%d) --> %d_%s",
  58. edge->from, dm_smodes[previousMode], dm_smodes[edge->mode], edge->len, source + edge->from, edge->size,
  59. edge->from + edge->len, dm_smodes[edge->mode]);
  60. }
  61. }
  62. static void DM_TRACE_Path(const unsigned char *source, const int length, struct dm_edge *edges,
  63. struct dm_edge *edge, char *result, const int result_size) {
  64. DM_TRACE_EdgeToString(result, source, length, edges, edge);
  65. struct dm_edge *current = DM_PREVIOUS(edges, edge);
  66. while (current) {
  67. char s[256];
  68. char *pos;
  69. int len;
  70. DM_TRACE_EdgeToString(s, source, length, edges, current);
  71. pos = strrchr(s, ' ');
  72. assert(pos);
  73. len = strlen(result);
  74. if ((pos - s) + 1 + len + 1 >= result_size) {
  75. result[result_size - 4] = '\0';
  76. strcat(result, "...");
  77. break;
  78. }
  79. memmove(result + (pos - s) + 1, result, len + 1);
  80. memcpy(result, s, (pos - s) + 1);
  81. current = DM_PREVIOUS(edges, current);
  82. }
  83. puts(result);
  84. }
  85. static void DM_TRACE_Edges(const char *prefix, const unsigned char *source, const int length,
  86. struct dm_edge *edges, const int vertexIndex) {
  87. int i, j, e_i;
  88. char result[1024 * 2];
  89. if (vertexIndex) {
  90. printf(prefix, vertexIndex);
  91. } else {
  92. fputs(prefix, stdout);
  93. }
  94. for (i = vertexIndex; i <= length; i++) {
  95. e_i = i * DM_NUM_MODES;
  96. for (j = 0; j < DM_NUM_MODES; j++) {
  97. if (edges[e_i + j].mode) {
  98. fputs("DEBUG ", stdout);
  99. DM_TRACE_Path(source, length, edges, edges + e_i + j, result, (int) ARRAY_SIZE(result));
  100. }
  101. }
  102. }
  103. }
  104. static void DM_TRACE_AddEdge(const unsigned char *source, const int length, struct dm_edge *edges,
  105. struct dm_edge *previous, const int vertexIndex, struct dm_edge *edge) {
  106. if (previous == NULL) {
  107. fputs("DEBUG add ", stdout);
  108. DM_TRACE_EdgeToString(NULL, source, length, edges, edge);
  109. printf(" from %d to %d size %d\n", edge->from, vertexIndex, edge->size);
  110. } else {
  111. fputs("DEBUG add ", stdout);
  112. DM_TRACE_EdgeToString(NULL, source, length, edges, edge);
  113. fputs(" from ", stdout);
  114. DM_TRACE_VertexToString(source, length, previous->from, previous);
  115. fputs(" to ", stdout);
  116. DM_TRACE_VertexToString(source, length, vertexIndex, edge);
  117. printf(" size %d\n", edge->size);
  118. }
  119. }
  120. static void DM_TRACE_NotAddEdge(const unsigned char *source, const int length, struct dm_edge *edges,
  121. struct dm_edge *previous, const int vertexIndex, const int e_ij, struct dm_edge *edge) {
  122. if (previous == NULL) {
  123. fputs("DEBUG not add ", stdout);
  124. DM_TRACE_EdgeToString(NULL, source, length, edges, edge);
  125. printf(" from %d to %d size %d since ", edge->from, vertexIndex, edge->size);
  126. DM_TRACE_EdgeToString(NULL, source, length, edges, edges + e_ij);
  127. printf(" < size %d\n", edges[e_ij].size);
  128. } else {
  129. fputs("DEBUG not add ", stdout);
  130. DM_TRACE_EdgeToString(NULL, source, length, edges, edge);
  131. fputs(" from ", stdout);
  132. DM_TRACE_VertexToString(source, length, previous->from, previous);
  133. fputs(" to ", stdout);
  134. DM_TRACE_VertexToString(source, length, vertexIndex, edge);
  135. printf(" size %d since ", edge->size);
  136. DM_TRACE_EdgeToString(NULL, source, length, edges, edges + e_ij);
  137. printf(" < size %d\n", edges[e_ij].size);
  138. }
  139. }
  140. #endif /* DM_TRACE */
  141. /* vim: set ts=4 sw=4 et : */
  142. #endif /* Z_DMATRIX_TRACE_H */