pcx.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /* pcx.c - Handles output to ZSoft PCX file */
  2. /*
  3. libzint - the open source barcode library
  4. Copyright (C) 2009-2024 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. #include <errno.h>
  30. #include <math.h>
  31. #include <stdio.h>
  32. #include "common.h"
  33. #include "filemem.h"
  34. #include "output.h"
  35. #include "pcx.h" /* PCX header structure */
  36. /* ZSoft PCX File Format Technical Reference Manual http://bespin.org/~qz/pc-gpe/pcx.txt */
  37. INTERNAL int pcx_pixel_plot(struct zint_symbol *symbol, const unsigned char *pixelbuf) {
  38. unsigned char fgred, fggrn, fgblu, fgalpha, bgred, bggrn, bgblu, bgalpha;
  39. int row, column, i, colour;
  40. int run_count;
  41. struct filemem fm;
  42. struct filemem *const fmp = &fm;
  43. pcx_header_t header;
  44. unsigned char previous;
  45. const unsigned char *pb;
  46. const int bytes_per_line = symbol->bitmap_width + (symbol->bitmap_width & 1); /* Must be even */
  47. unsigned char *rle_row = (unsigned char *) z_alloca(bytes_per_line);
  48. rle_row[bytes_per_line - 1] = 0; /* Will remain zero if bitmap_width odd */
  49. (void) out_colour_get_rgb(symbol->fgcolour, &fgred, &fggrn, &fgblu, &fgalpha);
  50. (void) out_colour_get_rgb(symbol->bgcolour, &bgred, &bggrn, &bgblu, &bgalpha);
  51. header.manufacturer = 10; /* ZSoft */
  52. header.version = 5; /* Version 3.0 */
  53. header.encoding = 1; /* Run length encoding */
  54. header.bits_per_pixel = 8; /* TODO: 1-bit monochrome black/white */
  55. out_le_u16(header.window_xmin, 0);
  56. out_le_u16(header.window_ymin, 0);
  57. out_le_u16(header.window_xmax, symbol->bitmap_width - 1);
  58. out_le_u16(header.window_ymax, symbol->bitmap_height - 1);
  59. out_le_u16(header.horiz_dpi, symbol->dpmm ? roundf(stripf(symbol->dpmm * 25.4f)) : 300);
  60. header.vert_dpi = header.horiz_dpi;
  61. for (i = 0; i < 48; i++) {
  62. header.colourmap[i] = 0x00;
  63. }
  64. header.reserved = 0;
  65. header.number_of_planes = 3 + (fgalpha != 0xFF || bgalpha != 0xFF); /* TODO: 1-bit monochrome black/white */
  66. out_le_u16(header.bytes_per_line, bytes_per_line);
  67. out_le_u16(header.palette_info, 1); /* Colour */
  68. out_le_u16(header.horiz_screen_size, 0);
  69. out_le_u16(header.vert_screen_size, 0);
  70. for (i = 0; i < 54; i++) {
  71. header.filler[i] = 0x00;
  72. }
  73. /* Open output file in binary mode */
  74. if (!fm_open(fmp, symbol, "wb")) {
  75. return errtxtf(ZINT_ERROR_FILE_ACCESS, symbol, 621, "Could not open PCX output file (%1$d: %2$s)", fmp->err,
  76. strerror(fmp->err));
  77. }
  78. fm_write(&header, sizeof(pcx_header_t), 1, fmp);
  79. for (row = 0, pb = pixelbuf; row < symbol->bitmap_height; row++, pb += symbol->bitmap_width) {
  80. for (colour = 0; colour < header.number_of_planes; colour++) {
  81. for (column = 0; column < symbol->bitmap_width; column++) {
  82. const unsigned char ch = pb[column];
  83. switch (colour) {
  84. case 0:
  85. if (ch == '0' || ch == '1') {
  86. rle_row[column] = ch != '0' ? fgred : bgred;
  87. } else {
  88. out_colour_char_to_rgb(ch, &rle_row[column], NULL, NULL);
  89. }
  90. break;
  91. case 1:
  92. if (ch == '0' || ch == '1') {
  93. rle_row[column] = ch != '0' ? fggrn : bggrn;
  94. } else {
  95. out_colour_char_to_rgb(ch, NULL, &rle_row[column], NULL);
  96. }
  97. break;
  98. case 2:
  99. if (ch == '0' || ch == '1') {
  100. rle_row[column] = ch != '0' ? fgblu : bgblu;
  101. } else {
  102. out_colour_char_to_rgb(ch, NULL, NULL, &rle_row[column]);
  103. }
  104. break;
  105. case 3:
  106. rle_row[column] = ch != '0' ? fgalpha : bgalpha;
  107. break;
  108. }
  109. }
  110. /* Based on ImageMagick/coders/pcx.c PCXWritePixels()
  111. * Copyright 1999-2020 ImageMagick Studio LLC */
  112. previous = rle_row[0];
  113. run_count = 1;
  114. for (column = 1; column < bytes_per_line; column++) { /* Note going up to bytes_per_line */
  115. if ((previous == rle_row[column]) && (run_count < 63)) {
  116. run_count++;
  117. } else {
  118. if (run_count > 1 || (previous & 0xc0) == 0xc0) {
  119. run_count += 0xc0;
  120. fm_putc(run_count, fmp);
  121. }
  122. fm_putc(previous, fmp);
  123. previous = rle_row[column];
  124. run_count = 1;
  125. }
  126. }
  127. if (run_count > 1 || (previous & 0xc0) == 0xc0) {
  128. run_count += 0xc0;
  129. fm_putc(run_count, fmp);
  130. }
  131. fm_putc(previous, fmp);
  132. }
  133. }
  134. if (fm_error(fmp)) {
  135. errtxtf(0, symbol, 622, "Incomplete write of PCX output (%1$d: %2$s)", fmp->err, strerror(fmp->err));
  136. (void) fm_close(fmp, symbol);
  137. return ZINT_ERROR_FILE_WRITE;
  138. }
  139. if (!fm_close(fmp, symbol)) {
  140. return errtxtf(ZINT_ERROR_FILE_WRITE, symbol, 624, "Failure on closing PCX output file (%1$d: %2$s)",
  141. fmp->err, strerror(fmp->err));
  142. }
  143. return 0;
  144. }
  145. /* vim: set ts=4 sw=4 et : */