1
0
mirror of https://git.code.sf.net/p/zint/code synced 2026-05-01 11:45:29 +00:00
Files
zint/backend/pcx.c
gitlost df64a0f237 debian: updated using https://salsa.debian.org/debian/zint as
template, props Dmitry Smirnov (ticket #147, props John Crisp);
  TODO: enable building with the GS1 Syntax Engine;
  this involved auditing copyrights which prompted the next item
  re "tif.c" and will prompt the next next item "pdf417.c"
tif.c: replaced LZW compression with tree-based one due to
  "tif_lzw.h"'s original BSD-TAHOE license which has the equivalent
  of the "advertising clause", new implementation (which is much
  faster anyway) props Harald Kuhr and Bob Montgomery
pdf417.c: TODO: Replace all code adapted from "pdf417.frm" as it
  was released under GPL v2.0 or later
pdf417.h: replace unnecessary "pdf417.frm" references with ISO/IEC
  standard as 1st step re above, some expanded comments
general: Codablock-F -> Codablock F (i.e. lose hyphen)
cmake: frontend/frontend_qt: go back to only setting RPATH on macOS
  as not Debian compatible and it seems it's not a good idea on
  Linux anyway; TODO: check BSD + other Unixes
CLI/GUI: make "--verbose" option official and document
tools/gen_eci_sb_h.php/gen_eci_mb_h.php: add SPDX Unicode-3.0;
  update haible.de/bruno tarball info - "GB18030.TXT" no longer in
  "jdk-1.4.2/" so mention "libiconv-1.11/" version instead (same)
general: add various SPDXs and normalize some Copyrights
reedsol.c: malloced `logt`/`alog` tables int -> short (slight
  performance improvement)
aztec.c: add copyrights, some code fiddling
BWIPP: update to latest
2026-03-18 15:08:32 +00:00

169 lines
7.1 KiB
C

/* pcx.c - Handles output to ZSoft PCX file */
/*
libzint - the open source barcode library
Copyright (C) 2009-2026 Robin Stuart <rstuart114@gmail.com>
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. Neither the name of the project nor the names of its contributors
may be used to endorse or promote products derived from this software
without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
*/
/* SPDX-License-Identifier: BSD-3-Clause */
#include <errno.h>
#include <math.h>
#include <stdio.h>
#include "common.h"
#include "filemem.h"
#include "output.h"
#include "pcx.h" /* PCX header structure */
/* ZSoft PCX File Format Technical Reference Manual http://bespin.org/~qz/pc-gpe/pcx.txt */
INTERNAL int zint_pcx_pixel_plot(struct zint_symbol *symbol, const unsigned char *pixelbuf) {
unsigned char fgred, fggrn, fgblu, fgalpha, bgred, bggrn, bgblu, bgalpha;
int row, column, i, colour;
int run_count;
struct filemem fm;
struct filemem *const fmp = &fm;
pcx_header_t header;
unsigned char previous;
const unsigned char *pb;
const int bytes_per_line = symbol->bitmap_width + (symbol->bitmap_width & 1); /* Must be even */
unsigned char *rle_row = (unsigned char *) z_alloca(bytes_per_line);
rle_row[bytes_per_line - 1] = 0; /* Will remain zero if bitmap_width odd */
(void) zint_out_colour_get_rgb(symbol->fgcolour, &fgred, &fggrn, &fgblu, &fgalpha);
(void) zint_out_colour_get_rgb(symbol->bgcolour, &bgred, &bggrn, &bgblu, &bgalpha);
header.manufacturer = 10; /* ZSoft */
header.version = 5; /* Version 3.0 */
header.encoding = 1; /* Run length encoding */
header.bits_per_pixel = 8; /* TODO: 1-bit monochrome black/white */
zint_out_le_u16(header.window_xmin, 0);
zint_out_le_u16(header.window_ymin, 0);
zint_out_le_u16(header.window_xmax, symbol->bitmap_width - 1);
zint_out_le_u16(header.window_ymax, symbol->bitmap_height - 1);
zint_out_le_u16(header.horiz_dpi, symbol->dpmm ? roundf(z_stripf(symbol->dpmm * 25.4f)) : 300);
header.vert_dpi = header.horiz_dpi;
for (i = 0; i < 48; i++) {
header.colourmap[i] = 0x00;
}
header.reserved = 0;
header.number_of_planes = 3 + (fgalpha != 0xFF || bgalpha != 0xFF); /* TODO: 1-bit monochrome black/white */
zint_out_le_u16(header.bytes_per_line, bytes_per_line);
zint_out_le_u16(header.palette_info, 1); /* Colour */
zint_out_le_u16(header.horiz_screen_size, 0);
zint_out_le_u16(header.vert_screen_size, 0);
for (i = 0; i < 54; i++) {
header.filler[i] = 0x00;
}
/* Open output file in binary mode */
if (!zint_fm_open(fmp, symbol, "wb")) {
return ZEXT z_errtxtf(ZINT_ERROR_FILE_ACCESS, symbol, 621, "Could not open PCX output file (%1$d: %2$s)",
fmp->err, strerror(fmp->err));
}
zint_fm_write(&header, sizeof(pcx_header_t), 1, fmp);
for (row = 0, pb = pixelbuf; row < symbol->bitmap_height; row++, pb += symbol->bitmap_width) {
for (colour = 0; colour < header.number_of_planes; colour++) {
for (column = 0; column < symbol->bitmap_width; column++) {
const unsigned char ch = pb[column];
switch (colour) {
case 0:
if (ch == '0' || ch == '1') {
rle_row[column] = ch != '0' ? fgred : bgred;
} else {
zint_out_colour_char_to_rgb(ch, &rle_row[column], NULL, NULL);
}
break;
case 1:
if (ch == '0' || ch == '1') {
rle_row[column] = ch != '0' ? fggrn : bggrn;
} else {
zint_out_colour_char_to_rgb(ch, NULL, &rle_row[column], NULL);
}
break;
case 2:
if (ch == '0' || ch == '1') {
rle_row[column] = ch != '0' ? fgblu : bgblu;
} else {
zint_out_colour_char_to_rgb(ch, NULL, NULL, &rle_row[column]);
}
break;
case 3:
rle_row[column] = ch != '0' ? fgalpha : bgalpha;
break;
}
}
/* Based on ImageMagick/coders/pcx.c PCXWritePixels()
* Copyright 1999-2020 ImageMagick Studio LLC */
/* SPDX-License-Identifier: ImageMagick */
previous = rle_row[0];
run_count = 1;
for (column = 1; column < bytes_per_line; column++) { /* Note going up to bytes_per_line */
if (previous == rle_row[column] && run_count < 63) {
run_count++;
} else {
if (run_count > 1 || (previous & 0xC0) == 0xC0) {
run_count += 0xC0;
zint_fm_putc(run_count, fmp);
}
zint_fm_putc(previous, fmp);
previous = rle_row[column];
run_count = 1;
}
}
if (run_count > 1 || (previous & 0xC0) == 0xC0) {
run_count += 0xC0;
zint_fm_putc(run_count, fmp);
}
zint_fm_putc(previous, fmp);
}
}
if (zint_fm_error(fmp)) {
ZEXT z_errtxtf(0, symbol, 622, "Incomplete write of PCX output (%1$d: %2$s)", fmp->err, strerror(fmp->err));
(void) zint_fm_close(fmp, symbol);
return ZINT_ERROR_FILE_WRITE;
}
if (!zint_fm_close(fmp, symbol)) {
return ZEXT z_errtxtf(ZINT_ERROR_FILE_WRITE, symbol, 624, "Failure on closing PCX output file (%1$d: %2$s)",
fmp->err, strerror(fmp->err));
}
return 0;
}
/* vim: set ts=4 sw=4 et : */