2016-02-17 23:52:48 +13:00
|
|
|
/* png.c - Handles output to PNG file */
|
|
|
|
/*
|
|
|
|
libzint - the open source barcode library
|
- `zint_symbol->fgcolour` & `bgcolour` buffer lengths extended 10
-> 16 to allow for "C,M,Y,K" comma-separated decimal percentage
strings
- API/CLI/GUI: allow foreground/background colours to be specified
as comma-separated decimal "C,M,Y,K" strings where "C", "M" etc.
are percentages (0-100) (ticket #281, 3rd point)
- output.c: new funcs `out_colour_get_rgb()` & `out_colour_get_cmyk()`
and use in bmp/emf/gif etc.
- PCX: add alpha support
- GUI: fix fg/gbcolor icon background not being reset on zap
- GUI: Rearrange some Appearance tab inputs (Border Type <-> Width,
Show Text <-> Font, Text/Font <-> Printing Scale/Size) to flow
more naturally (hopefully)
- GUI: save button "Save As..." -> "Save..." and add icon
- CLI: add --bgcolor/colour & --fgcolor/colour synonyms
2023-01-30 08:51:11 +13:00
|
|
|
Copyright (C) 2009-2023 Robin Stuart <rstuart114@gmail.com>
|
2016-02-17 23:52:48 +13:00
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions
|
|
|
|
are met:
|
|
|
|
|
2017-10-24 08:37:52 +13:00
|
|
|
1. Redistributions of source code must retain the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer.
|
2016-02-17 23:52:48 +13:00
|
|
|
2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer in the
|
2017-10-24 08:37:52 +13:00
|
|
|
documentation and/or other materials provided with the distribution.
|
2016-02-17 23:52:48 +13:00
|
|
|
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
|
2017-10-24 08:37:52 +13:00
|
|
|
without specific prior written permission.
|
2016-02-17 23:52:48 +13:00
|
|
|
|
|
|
|
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
|
2017-10-24 08:37:52 +13:00
|
|
|
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
2016-02-17 23:52:48 +13:00
|
|
|
SUCH DAMAGE.
|
2016-02-20 23:50:15 +13:00
|
|
|
*/
|
RMQR: update to ISO/IEC 23941:2022 - R13x77 numeric cclens change 8 -> 7
QRCODE: use stricter interpretation of ZINT_FULL_MULTIBYTE, excluding
certain trailing bytes
libzint: fix some confusing error messages introduced by segment stuff
general: new escape chars \U, \d and \o
backend_qt: fudge rendering of border rectangles due to scaling/translation
rounding errors TODO: better fudge
GUI: foreground/background colours -> text boxes and icon buttons, add swap
button, independently movable picker (NULL parent), preview colour changes,
preview Data Window changes, add clear data (del) buttons, add zap button
and Factory Reset menu option, various other fixes
libzint: remove STATIC_UNLESS_ZINT_TEST, use wrappers
CMake: add find package QtSvg, remove QtXml
manual: split symbology and general specs and sort, move DAFT to 4-state,
UPC/EAN -> EAN/UPC, DataBar -> GS1 DataBar always, expand MAILMARK info,
various other fiddlings
man page: options or -> |, expand MSI Plessey check digit options
README.linux: add packages info
license: add SPDX-License-Identifier to touched files
2022-06-10 08:52:02 +12:00
|
|
|
/* SPDX-License-Identifier: BSD-3-Clause */
|
2016-02-17 23:52:48 +13:00
|
|
|
|
2022-11-25 03:18:31 +13:00
|
|
|
#ifndef ZINT_NO_PNG
|
2021-06-10 22:15:39 +12:00
|
|
|
|
|
|
|
#include <errno.h>
|
- API: add new zint_symbol `dpmm` field for output resolution (BMP/
EMF/PCX/PNG/TIF only, i.e. excluding EPS, GIF & SVG)
- Add support for specifying scale by X-dimension and resolution
with new option `--scalexdimdp` for CLI/Tcl & new API function
`ZBarcode_Scale_From_XdimDp()` (+ `ZBarcode_XdimDp_From_Scale()`
& `ZBarcode_Default_Xdim()`) and new GUI popup; manual: document
- BMP/EMF/PCX/PNG/TIF: use new `dpmm` resolution field (for EMF
following Inkscape)
- backend_qt: add `dpmm()`, `vectorWidth()`, `vectorHeight()`,
`noPng()`, `getVersion()`, `takesGS1AIData()`, & `XdimDp` stuff
incl. new `QZintXdimDp` struct for passing around scale vars &
use in `getAsCLI()`; add comments
- Raise `scale` limit to 200 (from 100) to allow for large dpmm
- output: create directories & subdirectories as necessary for
output path using new function `out_fopen()` and use in BMP/EMF/
EPS/GIF/PCX/PNG/SVG/TIF
- DPLEIT/DPIDENT: format HRT according to (incomplete)
documentation, and set default height to 72X (from 50X)
- CODE128B renamed to CODE128AB as can use subsets A and/or B
- CODABAR: fix minimum height calc
- EMF: fix indexing of handles (zero-based not 1-based)
- GUI: fix symbology zap (previous technique of clearing and
re-loading settings without doing a sync no longer works);
fix UPCEAN guard descent enable
- MAILMARK: better error message if input < 14 characters
- GUI: add "Default" button for DAFT tracker ratio & enable/disable
various default buttons; use new `takesGS1AIData()` to
enable/disable GS1-specific checkboxes
- CLI: use new `validate_float()` to parse float options (7
significant digits allowed only, no scientific notation)
- DATAMATRIX/GRIDMATRIX/PDF417/QR/ULTRA: micro-optimize structapp
ID parse
- library/CLI: fiddle with static asserts (make CHAR_BIT sensitive,
supposedly)
- win32/README: update building libpng (assembly removed)
- README.linux: document incompatibility of Qt6 >= 6.3
- manual: expand Barcode Studio waffle
- test suite: change range separator to hyphen and allow multiple
excludes
2022-12-03 10:39:01 +13:00
|
|
|
#include <math.h>
|
2016-02-17 23:52:48 +13:00
|
|
|
#include <stdio.h>
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <io.h>
|
|
|
|
#endif
|
|
|
|
#include <png.h>
|
|
|
|
#include <zlib.h>
|
|
|
|
#include <setjmp.h>
|
2022-09-14 09:08:08 +12:00
|
|
|
#include "common.h"
|
- API: add new zint_symbol `dpmm` field for output resolution (BMP/
EMF/PCX/PNG/TIF only, i.e. excluding EPS, GIF & SVG)
- Add support for specifying scale by X-dimension and resolution
with new option `--scalexdimdp` for CLI/Tcl & new API function
`ZBarcode_Scale_From_XdimDp()` (+ `ZBarcode_XdimDp_From_Scale()`
& `ZBarcode_Default_Xdim()`) and new GUI popup; manual: document
- BMP/EMF/PCX/PNG/TIF: use new `dpmm` resolution field (for EMF
following Inkscape)
- backend_qt: add `dpmm()`, `vectorWidth()`, `vectorHeight()`,
`noPng()`, `getVersion()`, `takesGS1AIData()`, & `XdimDp` stuff
incl. new `QZintXdimDp` struct for passing around scale vars &
use in `getAsCLI()`; add comments
- Raise `scale` limit to 200 (from 100) to allow for large dpmm
- output: create directories & subdirectories as necessary for
output path using new function `out_fopen()` and use in BMP/EMF/
EPS/GIF/PCX/PNG/SVG/TIF
- DPLEIT/DPIDENT: format HRT according to (incomplete)
documentation, and set default height to 72X (from 50X)
- CODE128B renamed to CODE128AB as can use subsets A and/or B
- CODABAR: fix minimum height calc
- EMF: fix indexing of handles (zero-based not 1-based)
- GUI: fix symbology zap (previous technique of clearing and
re-loading settings without doing a sync no longer works);
fix UPCEAN guard descent enable
- MAILMARK: better error message if input < 14 characters
- GUI: add "Default" button for DAFT tracker ratio & enable/disable
various default buttons; use new `takesGS1AIData()` to
enable/disable GS1-specific checkboxes
- CLI: use new `validate_float()` to parse float options (7
significant digits allowed only, no scientific notation)
- DATAMATRIX/GRIDMATRIX/PDF417/QR/ULTRA: micro-optimize structapp
ID parse
- library/CLI: fiddle with static asserts (make CHAR_BIT sensitive,
supposedly)
- win32/README: update building libpng (assembly removed)
- README.linux: document incompatibility of Qt6 >= 6.3
- manual: expand Barcode Studio waffle
- test suite: change range separator to hyphen and allow multiple
excludes
2022-12-03 10:39:01 +13:00
|
|
|
#include "output.h"
|
2016-07-19 07:14:56 +12:00
|
|
|
|
2021-07-27 02:29:05 +12:00
|
|
|
/* Note if change this need to change "backend/tests/test_png.c" definition also */
|
|
|
|
struct wpng_error_type {
|
|
|
|
struct zint_symbol *symbol;
|
2016-02-17 23:52:48 +13:00
|
|
|
jmp_buf jmpbuf;
|
|
|
|
};
|
|
|
|
|
RMQR: update to ISO/IEC 23941:2022 - R13x77 numeric cclens change 8 -> 7
QRCODE: use stricter interpretation of ZINT_FULL_MULTIBYTE, excluding
certain trailing bytes
libzint: fix some confusing error messages introduced by segment stuff
general: new escape chars \U, \d and \o
backend_qt: fudge rendering of border rectangles due to scaling/translation
rounding errors TODO: better fudge
GUI: foreground/background colours -> text boxes and icon buttons, add swap
button, independently movable picker (NULL parent), preview colour changes,
preview Data Window changes, add clear data (del) buttons, add zap button
and Factory Reset menu option, various other fixes
libzint: remove STATIC_UNLESS_ZINT_TEST, use wrappers
CMake: add find package QtSvg, remove QtXml
manual: split symbology and general specs and sort, move DAFT to 4-state,
UPC/EAN -> EAN/UPC, DataBar -> GS1 DataBar always, expand MAILMARK info,
various other fiddlings
man page: options or -> |, expand MSI Plessey check digit options
README.linux: add packages info
license: add SPDX-License-Identifier to touched files
2022-06-10 08:52:02 +12:00
|
|
|
static void wpng_error_handler(png_structp png_ptr, png_const_charp msg) {
|
2021-07-27 02:29:05 +12:00
|
|
|
struct wpng_error_type *wpng_error_ptr;
|
2016-02-17 23:52:48 +13:00
|
|
|
|
2021-07-27 02:29:05 +12:00
|
|
|
wpng_error_ptr = (struct wpng_error_type *) png_get_error_ptr(png_ptr);
|
|
|
|
if (wpng_error_ptr == NULL) {
|
2016-02-20 23:50:15 +13:00
|
|
|
/* we are completely hosed now */
|
2021-07-27 02:29:05 +12:00
|
|
|
fprintf(stderr, "Error 636: libpng error: %s\n", msg ? msg : "<NULL>");
|
|
|
|
fprintf(stderr, "Error 637: jmpbuf not recoverable, terminating\n");
|
2016-02-17 23:52:48 +13:00
|
|
|
fflush(stderr);
|
2021-07-27 02:29:05 +12:00
|
|
|
return; /* libpng will call abort() */
|
2016-02-17 23:52:48 +13:00
|
|
|
}
|
2021-07-27 02:29:05 +12:00
|
|
|
sprintf(wpng_error_ptr->symbol->errtxt, "635: libpng error: %.60s", msg ? msg : "<NULL>");
|
|
|
|
longjmp(wpng_error_ptr->jmpbuf, 1);
|
2016-02-17 23:52:48 +13:00
|
|
|
}
|
|
|
|
|
RMQR: update to ISO/IEC 23941:2022 - R13x77 numeric cclens change 8 -> 7
QRCODE: use stricter interpretation of ZINT_FULL_MULTIBYTE, excluding
certain trailing bytes
libzint: fix some confusing error messages introduced by segment stuff
general: new escape chars \U, \d and \o
backend_qt: fudge rendering of border rectangles due to scaling/translation
rounding errors TODO: better fudge
GUI: foreground/background colours -> text boxes and icon buttons, add swap
button, independently movable picker (NULL parent), preview colour changes,
preview Data Window changes, add clear data (del) buttons, add zap button
and Factory Reset menu option, various other fixes
libzint: remove STATIC_UNLESS_ZINT_TEST, use wrappers
CMake: add find package QtSvg, remove QtXml
manual: split symbology and general specs and sort, move DAFT to 4-state,
UPC/EAN -> EAN/UPC, DataBar -> GS1 DataBar always, expand MAILMARK info,
various other fiddlings
man page: options or -> |, expand MSI Plessey check digit options
README.linux: add packages info
license: add SPDX-License-Identifier to touched files
2022-06-10 08:52:02 +12:00
|
|
|
#ifdef ZINT_TEST /* Wrapper for direct testing */
|
|
|
|
INTERNAL void wpng_error_handler_test(png_structp png_ptr, png_const_charp msg) {
|
2023-06-12 12:25:55 +12:00
|
|
|
wpng_error_handler(png_ptr, msg);
|
RMQR: update to ISO/IEC 23941:2022 - R13x77 numeric cclens change 8 -> 7
QRCODE: use stricter interpretation of ZINT_FULL_MULTIBYTE, excluding
certain trailing bytes
libzint: fix some confusing error messages introduced by segment stuff
general: new escape chars \U, \d and \o
backend_qt: fudge rendering of border rectangles due to scaling/translation
rounding errors TODO: better fudge
GUI: foreground/background colours -> text boxes and icon buttons, add swap
button, independently movable picker (NULL parent), preview colour changes,
preview Data Window changes, add clear data (del) buttons, add zap button
and Factory Reset menu option, various other fixes
libzint: remove STATIC_UNLESS_ZINT_TEST, use wrappers
CMake: add find package QtSvg, remove QtXml
manual: split symbology and general specs and sort, move DAFT to 4-state,
UPC/EAN -> EAN/UPC, DataBar -> GS1 DataBar always, expand MAILMARK info,
various other fiddlings
man page: options or -> |, expand MSI Plessey check digit options
README.linux: add packages info
license: add SPDX-License-Identifier to touched files
2022-06-10 08:52:02 +12:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2021-02-27 09:45:08 +13:00
|
|
|
/* Guestimate best compression strategy */
|
- `zint_symbol->fgcolour` & `bgcolour` buffer lengths extended 10
-> 16 to allow for "C,M,Y,K" comma-separated decimal percentage
strings
- API/CLI/GUI: allow foreground/background colours to be specified
as comma-separated decimal "C,M,Y,K" strings where "C", "M" etc.
are percentages (0-100) (ticket #281, 3rd point)
- output.c: new funcs `out_colour_get_rgb()` & `out_colour_get_cmyk()`
and use in bmp/emf/gif etc.
- PCX: add alpha support
- GUI: fix fg/gbcolor icon background not being reset on zap
- GUI: Rearrange some Appearance tab inputs (Border Type <-> Width,
Show Text <-> Font, Text/Font <-> Printing Scale/Size) to flow
more naturally (hopefully)
- GUI: save button "Save As..." -> "Save..." and add icon
- CLI: add --bgcolor/colour & --fgcolor/colour synonyms
2023-01-30 08:51:11 +13:00
|
|
|
static int guess_compression_strategy(struct zint_symbol *symbol, const unsigned char *pixelbuf) {
|
2021-02-27 09:45:08 +13:00
|
|
|
(void)pixelbuf;
|
|
|
|
|
|
|
|
/* TODO: Do properly */
|
|
|
|
|
|
|
|
/* It seems the best choice for typical barcode pngs is one of Z_DEFAULT_STRATEGY and Z_FILTERED */
|
|
|
|
|
|
|
|
/* Some guesses */
|
|
|
|
if (symbol->symbology == BARCODE_MAXICODE) {
|
|
|
|
return Z_DEFAULT_STRATEGY;
|
|
|
|
}
|
|
|
|
if (symbol->symbology == BARCODE_AZTEC && symbol->bitmap_width <= 30) {
|
|
|
|
return Z_DEFAULT_STRATEGY;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Z_FILTERED seems to work better for slightly more barcodes+data so default to that */
|
|
|
|
return Z_FILTERED;
|
|
|
|
}
|
|
|
|
|
- `zint_symbol->fgcolour` & `bgcolour` buffer lengths extended 10
-> 16 to allow for "C,M,Y,K" comma-separated decimal percentage
strings
- API/CLI/GUI: allow foreground/background colours to be specified
as comma-separated decimal "C,M,Y,K" strings where "C", "M" etc.
are percentages (0-100) (ticket #281, 3rd point)
- output.c: new funcs `out_colour_get_rgb()` & `out_colour_get_cmyk()`
and use in bmp/emf/gif etc.
- PCX: add alpha support
- GUI: fix fg/gbcolor icon background not being reset on zap
- GUI: Rearrange some Appearance tab inputs (Border Type <-> Width,
Show Text <-> Font, Text/Font <-> Printing Scale/Size) to flow
more naturally (hopefully)
- GUI: save button "Save As..." -> "Save..." and add icon
- CLI: add --bgcolor/colour & --fgcolor/colour synonyms
2023-01-30 08:51:11 +13:00
|
|
|
INTERNAL int png_pixel_plot(struct zint_symbol *symbol, const unsigned char *pixelbuf) {
|
2021-07-27 02:29:05 +12:00
|
|
|
struct wpng_error_type wpng_error;
|
|
|
|
FILE *outfile;
|
2016-02-20 23:50:15 +13:00
|
|
|
png_structp png_ptr;
|
|
|
|
png_infop info_ptr;
|
2021-02-27 09:45:08 +13:00
|
|
|
int i;
|
2020-11-02 07:32:55 +13:00
|
|
|
int row, column;
|
2021-02-27 09:45:08 +13:00
|
|
|
png_color bg, fg;
|
|
|
|
unsigned char bg_alpha, fg_alpha;
|
|
|
|
unsigned char map[128];
|
|
|
|
png_color palette[32];
|
2021-03-12 00:59:30 +13:00
|
|
|
int num_palette;
|
2021-02-27 09:45:08 +13:00
|
|
|
unsigned char trans_alpha[32];
|
2021-03-12 00:59:30 +13:00
|
|
|
int num_trans;
|
2021-02-27 09:45:08 +13:00
|
|
|
int bit_depth;
|
|
|
|
int compression_strategy;
|
- `zint_symbol->fgcolour` & `bgcolour` buffer lengths extended 10
-> 16 to allow for "C,M,Y,K" comma-separated decimal percentage
strings
- API/CLI/GUI: allow foreground/background colours to be specified
as comma-separated decimal "C,M,Y,K" strings where "C", "M" etc.
are percentages (0-100) (ticket #281, 3rd point)
- output.c: new funcs `out_colour_get_rgb()` & `out_colour_get_cmyk()`
and use in bmp/emf/gif etc.
- PCX: add alpha support
- GUI: fix fg/gbcolor icon background not being reset on zap
- GUI: Rearrange some Appearance tab inputs (Border Type <-> Width,
Show Text <-> Font, Text/Font <-> Printing Scale/Size) to flow
more naturally (hopefully)
- GUI: save button "Save As..." -> "Save..." and add icon
- CLI: add --bgcolor/colour & --fgcolor/colour synonyms
2023-01-30 08:51:11 +13:00
|
|
|
const unsigned char *pb;
|
2021-07-27 02:29:05 +12:00
|
|
|
const int output_to_stdout = symbol->output_options & BARCODE_STDOUT;
|
2022-07-15 03:01:30 +12:00
|
|
|
unsigned char *outdata = (unsigned char *) z_alloca(symbol->bitmap_width);
|
2016-02-17 23:52:48 +13:00
|
|
|
|
2021-07-27 02:29:05 +12:00
|
|
|
wpng_error.symbol = symbol;
|
2016-02-20 23:50:15 +13:00
|
|
|
|
- `zint_symbol->fgcolour` & `bgcolour` buffer lengths extended 10
-> 16 to allow for "C,M,Y,K" comma-separated decimal percentage
strings
- API/CLI/GUI: allow foreground/background colours to be specified
as comma-separated decimal "C,M,Y,K" strings where "C", "M" etc.
are percentages (0-100) (ticket #281, 3rd point)
- output.c: new funcs `out_colour_get_rgb()` & `out_colour_get_cmyk()`
and use in bmp/emf/gif etc.
- PCX: add alpha support
- GUI: fix fg/gbcolor icon background not being reset on zap
- GUI: Rearrange some Appearance tab inputs (Border Type <-> Width,
Show Text <-> Font, Text/Font <-> Printing Scale/Size) to flow
more naturally (hopefully)
- GUI: save button "Save As..." -> "Save..." and add icon
- CLI: add --bgcolor/colour & --fgcolor/colour synonyms
2023-01-30 08:51:11 +13:00
|
|
|
(void) out_colour_get_rgb(symbol->fgcolour, &fg.red, &fg.green, &fg.blue, &fg_alpha);
|
|
|
|
(void) out_colour_get_rgb(symbol->bgcolour, &bg.red, &bg.green, &bg.blue, &bg_alpha);
|
2021-02-27 09:45:08 +13:00
|
|
|
|
2021-03-17 12:38:47 +13:00
|
|
|
num_trans = 0;
|
2021-02-27 09:45:08 +13:00
|
|
|
if (symbol->symbology == BARCODE_ULTRA) {
|
|
|
|
static const int ultra_chars[8] = { 'W', 'C', 'B', 'M', 'R', 'Y', 'G', 'K' };
|
2021-07-27 02:29:05 +12:00
|
|
|
static const png_color ultra_colours[8] = {
|
2021-02-27 09:45:08 +13:00
|
|
|
{ 0xff, 0xff, 0xff, }, /* White */
|
|
|
|
{ 0, 0xff, 0xff, }, /* Cyan */
|
|
|
|
{ 0, 0, 0xff, }, /* Blue */
|
|
|
|
{ 0xff, 0, 0xff, }, /* Magenta */
|
|
|
|
{ 0xff, 0, 0, }, /* Red */
|
|
|
|
{ 0xff, 0xff, 0, }, /* Yellow */
|
|
|
|
{ 0, 0xff, 0, }, /* Green */
|
|
|
|
{ 0, 0, 0, }, /* Black */
|
|
|
|
};
|
|
|
|
for (i = 0; i < 8; i++) {
|
|
|
|
map[ultra_chars[i]] = i;
|
|
|
|
palette[i] = ultra_colours[i];
|
|
|
|
if (fg_alpha != 0xff) {
|
|
|
|
trans_alpha[i] = fg_alpha;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
num_palette = 8;
|
|
|
|
if (fg_alpha != 0xff) {
|
|
|
|
num_trans = 8;
|
|
|
|
}
|
|
|
|
|
2021-05-29 01:37:57 +12:00
|
|
|
/* For Ultracode, have foreground only if have bind/box */
|
2022-11-11 11:13:41 +13:00
|
|
|
if (symbol->border_width > 0 && (symbol->output_options & (BARCODE_BIND | BARCODE_BOX | BARCODE_BIND_TOP))) {
|
2021-05-29 01:37:57 +12:00
|
|
|
/* Check whether can re-use black */
|
|
|
|
if (fg.red == 0 && fg.green == 0 && fg.blue == 0) {
|
|
|
|
map['1'] = 7; /* Re-use black */
|
|
|
|
} else {
|
|
|
|
map['1'] = num_palette;
|
|
|
|
palette[num_palette++] = fg;
|
|
|
|
if (fg_alpha != 0xff) {
|
|
|
|
trans_alpha[num_trans++] = fg_alpha;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-27 09:45:08 +13:00
|
|
|
/* For Ultracode, have background only if have whitespace/quiet zones */
|
2021-09-25 00:21:24 +12:00
|
|
|
if (symbol->whitespace_width > 0 || symbol->whitespace_height > 0
|
|
|
|
|| ((symbol->output_options & BARCODE_QUIET_ZONES)
|
|
|
|
&& !(symbol->output_options & BARCODE_NO_QUIET_ZONES))) {
|
2021-02-27 09:45:08 +13:00
|
|
|
/* Check whether can re-use white */
|
|
|
|
if (bg.red == 0xff && bg.green == 0xff && bg.blue == 0xff && bg_alpha == fg_alpha) {
|
|
|
|
map['0'] = 0; /* Re-use white */
|
|
|
|
} else {
|
|
|
|
if (bg_alpha == 0xff || fg_alpha != 0xff) {
|
|
|
|
/* No alpha or have foreground alpha - add to end */
|
|
|
|
map['0'] = num_palette;
|
|
|
|
palette[num_palette++] = bg;
|
|
|
|
} else {
|
|
|
|
/* Alpha and no foreground alpha - add to front & move white to end */
|
|
|
|
map['0'] = 0;
|
|
|
|
palette[0] = bg;
|
|
|
|
map['W'] = num_palette;
|
|
|
|
palette[num_palette++] = ultra_colours[0];
|
|
|
|
}
|
|
|
|
if (bg_alpha != 0xff) {
|
|
|
|
trans_alpha[num_trans++] = bg_alpha;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
int bg_idx = 0, fg_idx = 1;
|
|
|
|
/* Do alphas first so can swop indexes if background not alpha */
|
|
|
|
if (bg_alpha != 0xff) {
|
|
|
|
trans_alpha[num_trans++] = bg_alpha;
|
|
|
|
}
|
|
|
|
if (fg_alpha != 0xff) {
|
|
|
|
trans_alpha[num_trans++] = fg_alpha;
|
|
|
|
if (num_trans == 1) {
|
|
|
|
/* Only foreground has alpha so swop indexes - saves a byte! */
|
|
|
|
bg_idx = 1;
|
|
|
|
fg_idx = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
map['0'] = bg_idx;
|
|
|
|
palette[bg_idx] = bg;
|
|
|
|
map['1'] = fg_idx;
|
|
|
|
palette[fg_idx] = fg;
|
|
|
|
num_palette = 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (num_palette <= 2) {
|
|
|
|
bit_depth = 1;
|
|
|
|
} else {
|
2021-07-27 02:29:05 +12:00
|
|
|
bit_depth = 4;
|
2020-08-03 09:26:39 +12:00
|
|
|
}
|
|
|
|
|
2016-02-20 23:50:15 +13:00
|
|
|
/* Open output file in binary mode */
|
2021-07-27 02:29:05 +12:00
|
|
|
if (output_to_stdout) {
|
2016-02-17 23:52:48 +13:00
|
|
|
#ifdef _MSC_VER
|
2016-02-20 23:50:15 +13:00
|
|
|
if (-1 == _setmode(_fileno(stdout), _O_BINARY)) {
|
2021-07-27 02:29:05 +12:00
|
|
|
sprintf(symbol->errtxt, "631: Could not set stdout to binary (%d: %.30s)", errno, strerror(errno));
|
2016-02-20 23:50:15 +13:00
|
|
|
return ZINT_ERROR_FILE_ACCESS;
|
|
|
|
}
|
2016-02-17 23:52:48 +13:00
|
|
|
#endif
|
2021-07-27 02:29:05 +12:00
|
|
|
outfile = stdout;
|
2016-02-20 23:50:15 +13:00
|
|
|
} else {
|
- API: add new zint_symbol `dpmm` field for output resolution (BMP/
EMF/PCX/PNG/TIF only, i.e. excluding EPS, GIF & SVG)
- Add support for specifying scale by X-dimension and resolution
with new option `--scalexdimdp` for CLI/Tcl & new API function
`ZBarcode_Scale_From_XdimDp()` (+ `ZBarcode_XdimDp_From_Scale()`
& `ZBarcode_Default_Xdim()`) and new GUI popup; manual: document
- BMP/EMF/PCX/PNG/TIF: use new `dpmm` resolution field (for EMF
following Inkscape)
- backend_qt: add `dpmm()`, `vectorWidth()`, `vectorHeight()`,
`noPng()`, `getVersion()`, `takesGS1AIData()`, & `XdimDp` stuff
incl. new `QZintXdimDp` struct for passing around scale vars &
use in `getAsCLI()`; add comments
- Raise `scale` limit to 200 (from 100) to allow for large dpmm
- output: create directories & subdirectories as necessary for
output path using new function `out_fopen()` and use in BMP/EMF/
EPS/GIF/PCX/PNG/SVG/TIF
- DPLEIT/DPIDENT: format HRT according to (incomplete)
documentation, and set default height to 72X (from 50X)
- CODE128B renamed to CODE128AB as can use subsets A and/or B
- CODABAR: fix minimum height calc
- EMF: fix indexing of handles (zero-based not 1-based)
- GUI: fix symbology zap (previous technique of clearing and
re-loading settings without doing a sync no longer works);
fix UPCEAN guard descent enable
- MAILMARK: better error message if input < 14 characters
- GUI: add "Default" button for DAFT tracker ratio & enable/disable
various default buttons; use new `takesGS1AIData()` to
enable/disable GS1-specific checkboxes
- CLI: use new `validate_float()` to parse float options (7
significant digits allowed only, no scientific notation)
- DATAMATRIX/GRIDMATRIX/PDF417/QR/ULTRA: micro-optimize structapp
ID parse
- library/CLI: fiddle with static asserts (make CHAR_BIT sensitive,
supposedly)
- win32/README: update building libpng (assembly removed)
- README.linux: document incompatibility of Qt6 >= 6.3
- manual: expand Barcode Studio waffle
- test suite: change range separator to hyphen and allow multiple
excludes
2022-12-03 10:39:01 +13:00
|
|
|
if (!(outfile = out_fopen(symbol->outfile, "wb"))) {
|
2021-07-27 02:29:05 +12:00
|
|
|
sprintf(symbol->errtxt, "632: Could not open output file (%d: %.30s)", errno, strerror(errno));
|
2016-02-20 23:50:15 +13:00
|
|
|
return ZINT_ERROR_FILE_ACCESS;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set up error handling routine as proc() above */
|
2021-07-27 02:29:05 +12:00
|
|
|
png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, &wpng_error, wpng_error_handler, NULL);
|
2016-02-20 23:50:15 +13:00
|
|
|
if (!png_ptr) {
|
2021-07-27 02:29:05 +12:00
|
|
|
strcpy(symbol->errtxt, "633: Insufficient memory for PNG write structure buffer");
|
|
|
|
if (!output_to_stdout) {
|
2022-12-20 05:28:15 +13:00
|
|
|
(void) fclose(outfile);
|
2021-07-27 02:29:05 +12:00
|
|
|
}
|
2016-02-20 23:50:15 +13:00
|
|
|
return ZINT_ERROR_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
info_ptr = png_create_info_struct(png_ptr);
|
|
|
|
if (!info_ptr) {
|
|
|
|
png_destroy_write_struct(&png_ptr, NULL);
|
2021-07-27 02:29:05 +12:00
|
|
|
strcpy(symbol->errtxt, "634: Insufficient memory for PNG info structure buffer");
|
|
|
|
if (!output_to_stdout) {
|
2022-12-20 05:28:15 +13:00
|
|
|
(void) fclose(outfile);
|
2021-07-27 02:29:05 +12:00
|
|
|
}
|
2016-02-20 23:50:15 +13:00
|
|
|
return ZINT_ERROR_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* catch jumping here */
|
2021-07-27 02:29:05 +12:00
|
|
|
if (setjmp(wpng_error.jmpbuf)) {
|
2016-02-20 23:50:15 +13:00
|
|
|
png_destroy_write_struct(&png_ptr, &info_ptr);
|
2021-07-27 02:29:05 +12:00
|
|
|
if (!output_to_stdout) {
|
2022-12-20 05:28:15 +13:00
|
|
|
(void) fclose(outfile);
|
2021-07-27 02:29:05 +12:00
|
|
|
}
|
2016-02-20 23:50:15 +13:00
|
|
|
return ZINT_ERROR_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* open output file with libpng */
|
2021-07-27 02:29:05 +12:00
|
|
|
png_init_io(png_ptr, outfile);
|
2016-02-20 23:50:15 +13:00
|
|
|
|
|
|
|
/* set compression */
|
|
|
|
png_set_compression_level(png_ptr, 9);
|
|
|
|
|
2021-02-27 09:45:08 +13:00
|
|
|
/* Compression strategy can make a difference */
|
|
|
|
compression_strategy = guess_compression_strategy(symbol, pixelbuf);
|
|
|
|
if (compression_strategy != Z_DEFAULT_STRATEGY) {
|
|
|
|
png_set_compression_strategy(png_ptr, compression_strategy);
|
|
|
|
}
|
|
|
|
|
- API: add new zint_symbol `dpmm` field for output resolution (BMP/
EMF/PCX/PNG/TIF only, i.e. excluding EPS, GIF & SVG)
- Add support for specifying scale by X-dimension and resolution
with new option `--scalexdimdp` for CLI/Tcl & new API function
`ZBarcode_Scale_From_XdimDp()` (+ `ZBarcode_XdimDp_From_Scale()`
& `ZBarcode_Default_Xdim()`) and new GUI popup; manual: document
- BMP/EMF/PCX/PNG/TIF: use new `dpmm` resolution field (for EMF
following Inkscape)
- backend_qt: add `dpmm()`, `vectorWidth()`, `vectorHeight()`,
`noPng()`, `getVersion()`, `takesGS1AIData()`, & `XdimDp` stuff
incl. new `QZintXdimDp` struct for passing around scale vars &
use in `getAsCLI()`; add comments
- Raise `scale` limit to 200 (from 100) to allow for large dpmm
- output: create directories & subdirectories as necessary for
output path using new function `out_fopen()` and use in BMP/EMF/
EPS/GIF/PCX/PNG/SVG/TIF
- DPLEIT/DPIDENT: format HRT according to (incomplete)
documentation, and set default height to 72X (from 50X)
- CODE128B renamed to CODE128AB as can use subsets A and/or B
- CODABAR: fix minimum height calc
- EMF: fix indexing of handles (zero-based not 1-based)
- GUI: fix symbology zap (previous technique of clearing and
re-loading settings without doing a sync no longer works);
fix UPCEAN guard descent enable
- MAILMARK: better error message if input < 14 characters
- GUI: add "Default" button for DAFT tracker ratio & enable/disable
various default buttons; use new `takesGS1AIData()` to
enable/disable GS1-specific checkboxes
- CLI: use new `validate_float()` to parse float options (7
significant digits allowed only, no scientific notation)
- DATAMATRIX/GRIDMATRIX/PDF417/QR/ULTRA: micro-optimize structapp
ID parse
- library/CLI: fiddle with static asserts (make CHAR_BIT sensitive,
supposedly)
- win32/README: update building libpng (assembly removed)
- README.linux: document incompatibility of Qt6 >= 6.3
- manual: expand Barcode Studio waffle
- test suite: change range separator to hyphen and allow multiple
excludes
2022-12-03 10:39:01 +13:00
|
|
|
if (symbol->dpmm) {
|
|
|
|
int resolution = (int) roundf(stripf(symbol->dpmm * 1000.0f)); /* pixels per metre */
|
|
|
|
png_set_pHYs(png_ptr, info_ptr, resolution, resolution, PNG_RESOLUTION_METER);
|
|
|
|
}
|
|
|
|
|
2016-02-20 23:50:15 +13:00
|
|
|
/* set Header block */
|
2021-07-27 02:29:05 +12:00
|
|
|
png_set_IHDR(png_ptr, info_ptr, symbol->bitmap_width, symbol->bitmap_height,
|
2021-02-27 09:45:08 +13:00
|
|
|
bit_depth, PNG_COLOR_TYPE_PALETTE, PNG_INTERLACE_NONE,
|
|
|
|
PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
|
|
|
|
|
|
|
|
png_set_PLTE(png_ptr, info_ptr, palette, num_palette);
|
|
|
|
if (num_trans) {
|
|
|
|
png_set_tRNS(png_ptr, info_ptr, trans_alpha, num_trans, NULL);
|
|
|
|
}
|
|
|
|
|
2016-02-20 23:50:15 +13:00
|
|
|
/* write all chunks up to (but not including) first IDAT */
|
|
|
|
png_write_info(png_ptr, info_ptr);
|
|
|
|
|
|
|
|
/* Pixel Plotting */
|
2021-03-06 06:27:11 +13:00
|
|
|
pb = pixelbuf;
|
2021-02-27 09:45:08 +13:00
|
|
|
if (bit_depth == 1) {
|
|
|
|
for (row = 0; row < symbol->bitmap_height; row++) {
|
2021-10-21 11:05:30 +13:00
|
|
|
if (row && memcmp(pb, pb - symbol->bitmap_width, symbol->bitmap_width) == 0) {
|
|
|
|
pb += symbol->bitmap_width;
|
|
|
|
} else {
|
|
|
|
unsigned char *image_data = outdata;
|
|
|
|
for (column = 0; column < symbol->bitmap_width; column += 8, image_data++) {
|
|
|
|
unsigned char byte = 0;
|
|
|
|
for (i = 0; i < 8 && column + i < symbol->bitmap_width; i++, pb++) {
|
|
|
|
byte |= map[*pb] << (7 - i);
|
|
|
|
}
|
|
|
|
*image_data = byte;
|
2021-02-27 09:45:08 +13:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/* write row contents to file */
|
|
|
|
png_write_row(png_ptr, outdata);
|
|
|
|
}
|
2021-07-27 02:29:05 +12:00
|
|
|
} else { /* Bit depth 4 */
|
2021-02-27 09:45:08 +13:00
|
|
|
for (row = 0; row < symbol->bitmap_height; row++) {
|
2021-10-21 11:05:30 +13:00
|
|
|
if (row && memcmp(pb, pb - symbol->bitmap_width, symbol->bitmap_width) == 0) {
|
|
|
|
pb += symbol->bitmap_width;
|
|
|
|
} else {
|
|
|
|
unsigned char *image_data = outdata;
|
|
|
|
for (column = 0; column < symbol->bitmap_width; column += 2, image_data++) {
|
|
|
|
unsigned char byte = map[*pb++] << 4;
|
|
|
|
if (column + 1 < symbol->bitmap_width) {
|
|
|
|
byte |= map[*pb++];
|
|
|
|
}
|
|
|
|
*image_data = byte;
|
2021-02-27 09:45:08 +13:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/* write row contents to file */
|
|
|
|
png_write_row(png_ptr, outdata);
|
|
|
|
}
|
2016-02-20 23:50:15 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
/* End the file */
|
|
|
|
png_write_end(png_ptr, NULL);
|
|
|
|
|
|
|
|
/* make sure we have disengaged */
|
2021-07-27 02:29:05 +12:00
|
|
|
png_destroy_write_struct(&png_ptr, &info_ptr);
|
|
|
|
|
2022-12-20 05:28:15 +13:00
|
|
|
if (ferror(outfile)) {
|
|
|
|
sprintf(symbol->errtxt, "638: Incomplete write to output (%d: %.30s)", errno, strerror(errno));
|
|
|
|
if (!output_to_stdout) {
|
|
|
|
(void) fclose(outfile);
|
|
|
|
}
|
|
|
|
return ZINT_ERROR_FILE_WRITE;
|
|
|
|
}
|
|
|
|
|
2021-07-27 02:29:05 +12:00
|
|
|
if (output_to_stdout) {
|
2022-12-20 05:28:15 +13:00
|
|
|
if (fflush(outfile) != 0) {
|
|
|
|
sprintf(symbol->errtxt, "639: Incomplete flush to output (%d: %.30s)", errno, strerror(errno));
|
|
|
|
return ZINT_ERROR_FILE_WRITE;
|
|
|
|
}
|
2016-02-20 23:50:15 +13:00
|
|
|
} else {
|
2022-12-20 05:28:15 +13:00
|
|
|
if (fclose(outfile) != 0) {
|
|
|
|
sprintf(symbol->errtxt, "960: Failure on closing output file (%d: %.30s)", errno, strerror(errno));
|
|
|
|
return ZINT_ERROR_FILE_WRITE;
|
|
|
|
}
|
2016-02-20 23:50:15 +13:00
|
|
|
}
|
2021-02-27 09:45:08 +13:00
|
|
|
|
2016-02-20 23:50:15 +13:00
|
|
|
return 0;
|
2016-02-17 23:52:48 +13:00
|
|
|
}
|
RMQR: update to ISO/IEC 23941:2022 - R13x77 numeric cclens change 8 -> 7
QRCODE: use stricter interpretation of ZINT_FULL_MULTIBYTE, excluding
certain trailing bytes
libzint: fix some confusing error messages introduced by segment stuff
general: new escape chars \U, \d and \o
backend_qt: fudge rendering of border rectangles due to scaling/translation
rounding errors TODO: better fudge
GUI: foreground/background colours -> text boxes and icon buttons, add swap
button, independently movable picker (NULL parent), preview colour changes,
preview Data Window changes, add clear data (del) buttons, add zap button
and Factory Reset menu option, various other fixes
libzint: remove STATIC_UNLESS_ZINT_TEST, use wrappers
CMake: add find package QtSvg, remove QtXml
manual: split symbology and general specs and sort, move DAFT to 4-state,
UPC/EAN -> EAN/UPC, DataBar -> GS1 DataBar always, expand MAILMARK info,
various other fiddlings
man page: options or -> |, expand MSI Plessey check digit options
README.linux: add packages info
license: add SPDX-License-Identifier to touched files
2022-06-10 08:52:02 +12:00
|
|
|
/* vim: set ts=4 sw=4 et : */
|
2022-11-25 03:18:31 +13:00
|
|
|
#endif /* ZINT_NO_PNG */
|