pdf417_trace.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /* pdf417_trace.h - Trace routines for optimal PDF417 optimization algorithm */
  2. /*
  3. libzint - the open source barcode library
  4. Copyright (C) 2022-2023 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_PDF417_TRACE_H
  30. #define Z_PDF417_TRACE_H
  31. #ifndef PDF_TRACE
  32. #define PDF_TRACE_Edges(px, s, l, p, v)
  33. #define PDF_TRACE_AddEdge(s, l, es, p, v, t, e) do { (void)(s); (void)(l); } while (0)
  34. #define PDF_TRACE_NotAddEdge(s, l, es, p, v, t, e) do { (void)(s); (void)(l); } while (0)
  35. #else
  36. static 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. static void PDF_TRACE_EdgeToString(char *buf, const unsigned char *source, const int length, struct pdf_edge *edges,
  41. struct pdf_edge *edge) {
  42. int previousMode = PDF_TRACE_getPreviousMode(edges, edge);
  43. (void)length;
  44. if (buf) {
  45. sprintf(buf, "%d_%c %c(%d,%d) %d(%d,%d,%d) -> %d_%c",
  46. edge->from, pdf_smodes[previousMode], pdf_smodes[edge->mode], source[edge->from], edge->len, edge->size
  47. + edge->unit_size, edge->units, edge->unit_size, edge->size, edge->from + 1, pdf_smodes[edge->mode]);
  48. } else {
  49. printf("%d_%c %c(%d,%d) %d(%d,%d,%d) -> %d_%c",
  50. edge->from, pdf_smodes[previousMode], pdf_smodes[edge->mode], source[edge->from], edge->len, edge->size
  51. + edge->unit_size, edge->units, edge->unit_size, edge->size, edge->from + 1, pdf_smodes[edge->mode]);
  52. }
  53. }
  54. static void PDF_TRACE_Path(const unsigned char *source, const int length, struct pdf_edge *edges,
  55. struct pdf_edge *edge, char *result, const int result_size) {
  56. PDF_TRACE_EdgeToString(result, source, length, edges, edge);
  57. struct pdf_edge *current = PDF_PREVIOUS(edges, edge);
  58. while (current) {
  59. char s[256];
  60. char *pos;
  61. int len;
  62. PDF_TRACE_EdgeToString(s, source, length, edges, current);
  63. pos = strrchr(s, ' ');
  64. assert(pos);
  65. len = strlen(result);
  66. if ((pos - s) + 1 + len + 1 >= result_size) {
  67. result[result_size - 4] = '\0';
  68. strcat(result, "...");
  69. break;
  70. }
  71. memmove(result + (pos - s) + 1, result, len + 1);
  72. memcpy(result, s, (pos - s) + 1);
  73. current = PDF_PREVIOUS(edges, current);
  74. }
  75. puts(result);
  76. }
  77. static void PDF_TRACE_Edges(const char *prefix, const unsigned char *source, const int length,
  78. struct pdf_edge *edges, const int vertexIndex) {
  79. int i, j, e_i;
  80. char result[1024 * 2];
  81. if (vertexIndex) {
  82. printf(prefix, vertexIndex);
  83. } else {
  84. fputs(prefix, stdout);
  85. }
  86. for (i = vertexIndex; i <= length; i++) {
  87. e_i = i * PDF_NUM_MODES;
  88. for (j = 0; j < PDF_NUM_MODES; j++) {
  89. if (edges[e_i + j].mode) {
  90. fputs(" **** ", stdout);
  91. PDF_TRACE_Path(source, length, edges, edges + e_i + j, result, (int) ARRAY_SIZE(result));
  92. }
  93. }
  94. }
  95. }
  96. static void PDF_TRACE_AddEdge(const unsigned char *source, const int length, struct pdf_edge *edges,
  97. struct pdf_edge *previous, const int vertexIndex, const int t_table, struct pdf_edge *edge) {
  98. const int new_size = edge->size + edge->unit_size;
  99. const int v_ij = vertexIndex * PDF_NUM_MODES + edge->mode - 1;
  100. const int v_size = edges[v_ij].size + edges[v_ij].unit_size;
  101. (void)source; (void)length;
  102. 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",
  103. pdf_smodes[edge->mode], t_table, previous ? (int) (previous - edges) : 0, edge->from, edge->len, v_ij,
  104. v_size, edges[v_ij].units, edges[v_ij].unit_size, edges[v_ij].size,
  105. new_size, edge->units, edge->unit_size, edge->size);
  106. }
  107. static void PDF_TRACE_NotAddEdge(const unsigned char *source, const int length, struct pdf_edge *edges,
  108. struct pdf_edge *previous, const int vertexIndex, const int t_table, struct pdf_edge *edge) {
  109. const int new_size = edge->size + edge->unit_size;
  110. const int v_ij = vertexIndex * PDF_NUM_MODES + edge->mode - 1;
  111. const int v_size = edges[v_ij].size + edges[v_ij].unit_size;
  112. (void)source; (void)length;
  113. 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",
  114. pdf_smodes[edge->mode], t_table, previous ? (int) (previous - edges) : 0, edge->from, edge->len, v_ij,
  115. v_size, edges[v_ij].units, edges[v_ij].unit_size, edges[v_ij].size,
  116. new_size, edge->units, edge->unit_size, edge->size);
  117. }
  118. #endif /* PDF_TRACE */
  119. /* vim: set ts=4 sw=4 et : */
  120. #endif /* Z_PDF417_TRACE_H */