Performance improvements for linear encoding and raster output

- use fixed-length string tables (mostly) instead of (char *) pointer ones
   (saves ~40K)
 - re-use C128Table for CODABLOCKF and CODE16K
   (required removal of Stop character and extra CODE16K-only entry)
 - use pointer to destination and copy (memcpy/strcpy(), bin_append_posn())
   instead of concatenating (strcat()) (mostly)
 - replace last remaining bin_append()s with bin_append_posn();
   bin_append() removed
 - add length arg to toupper() and expand() (avoids strlen())
 - change is_sane() to use table-based flags (avoids an iteration)
 - rename lookup() to is_sane_lookup() and change to check and return posns
   and use in pointer to destination loops (avoids strcat()s)
 - remove special case PHARMA in expand() (dealt with in pharma())
 - make #define SILVER/CALCIUM/TECHNETIUM/KRSET etc static strings
 - replace strchr() -> posn()
 - CODE128: populate destination once in checksum loop; re-use and export
   some more routines (c128_set_a/b/c(), c128_put_in_set()) for sharing;
   prefix defines (SHIFTA -> C128_SHIFTA etc) and existing exported routines
 - use factor XOR toggle trick in checksum calcs (avoids branch)
 - raster.c: fill out single 1-pixel row and copy using new draw_bar_line(),
   copy_bar_line() routines; similarly in buffer_plot compare previous line &
   copy if same (same technique as used to improve non-half-integer scaling,
   significant performance increase, (c) codemonkey82);
   also done for PNG (BMP/GIF/PCX/TIFF not done)
 - raster/vector/output.c: shorten "output_" prefix -> "out_";
   sync vector to other raster changes to try to keep source files similar
 - 2of5.c: prefix "c25_"
JAPANPOST: return error if input data truncated (backward incompatible)
DAFT: max chars 50 -> 100
common.c: istwodigit() -> is_twodigit()
common.c/emf.c/output.c: use some further stripf()s (MSVC6 float variations)
library.c: new check_output_args() helper
zint.h: add BARCODE_LAST marker and use in library.c
QRCODE: remove a NOLINT (requires clang-tidy-13), one remaining
CMake: separate no-optimize from ZINT_DEBUG into new ZINT_NOOPT option
This commit is contained in:
gitlost
2021-10-20 23:05:30 +01:00
parent e8b59aa696
commit fab7435fac
72 changed files with 3501 additions and 2380 deletions

View File

@ -35,32 +35,31 @@
#include "output.h"
#include "font.h"
#define SSET "0123456789ABCDEF"
#define SSET_F (IS_NUM_F | IS_UHX_F) /* SSET "0123456789ABCDEF" */
/* Check colour options are good. Note: using raster.c error nos 651-654 */
INTERNAL int output_check_colour_options(struct zint_symbol *symbol) {
int error_number;
INTERNAL int out_check_colour_options(struct zint_symbol *symbol) {
int fg_len = (int) strlen(symbol->fgcolour);
int bg_len = (int) strlen(symbol->bgcolour);
if ((strlen(symbol->fgcolour) != 6) && (strlen(symbol->fgcolour) != 8)) {
if ((fg_len != 6) && (fg_len != 8)) {
strcpy(symbol->errtxt, "651: Malformed foreground colour target");
return ZINT_ERROR_INVALID_OPTION;
}
if ((strlen(symbol->bgcolour) != 6) && (strlen(symbol->bgcolour) != 8)) {
if ((bg_len != 6) && (bg_len != 8)) {
strcpy(symbol->errtxt, "652: Malformed background colour target");
return ZINT_ERROR_INVALID_OPTION;
}
to_upper((unsigned char *) symbol->fgcolour);
to_upper((unsigned char *) symbol->bgcolour);
to_upper((unsigned char *) symbol->fgcolour, fg_len);
to_upper((unsigned char *) symbol->bgcolour, bg_len);
error_number = is_sane(SSET, (unsigned char *) symbol->fgcolour, (int) strlen(symbol->fgcolour));
if (error_number == ZINT_ERROR_INVALID_DATA) {
if (!is_sane(SSET_F, (unsigned char *) symbol->fgcolour, fg_len)) {
strcpy(symbol->errtxt, "653: Malformed foreground colour target");
return ZINT_ERROR_INVALID_OPTION;
}
error_number = is_sane(SSET, (unsigned char *) symbol->bgcolour, (int) strlen(symbol->bgcolour));
if (error_number == ZINT_ERROR_INVALID_DATA) {
if (!is_sane(SSET_F, (unsigned char *) symbol->bgcolour, bg_len)) {
strcpy(symbol->errtxt, "654: Malformed background colour target");
return ZINT_ERROR_INVALID_OPTION;
}
@ -69,7 +68,7 @@ INTERNAL int output_check_colour_options(struct zint_symbol *symbol) {
}
/* Return minimum quiet zones for each symbology */
STATIC_UNLESS_ZINT_TEST int quiet_zones(const struct zint_symbol *symbol, const int hide_text,
STATIC_UNLESS_ZINT_TEST int out_quiet_zones(const struct zint_symbol *symbol, const int hide_text,
float *left, float *right, float *top, float *bottom) {
int done = 0;
@ -192,7 +191,7 @@ STATIC_UNLESS_ZINT_TEST int quiet_zones(const struct zint_symbol *symbol, const
/* Only do others if flag set */
if (!(symbol->output_options & BARCODE_QUIET_ZONES) || (symbol->output_options & BARCODE_NO_QUIET_ZONES)) {
return done;
return 0;
}
switch (symbol->symbology) {
@ -471,12 +470,12 @@ STATIC_UNLESS_ZINT_TEST int quiet_zones(const struct zint_symbol *symbol, const
}
/* Set left (x), top (y), right and bottom offsets for whitespace */
INTERNAL void output_set_whitespace_offsets(const struct zint_symbol *symbol, const int hide_text,
INTERNAL void out_set_whitespace_offsets(const struct zint_symbol *symbol, const int hide_text,
float *xoffset, float *yoffset, float *roffset, float *boffset, const float scaler,
int *xoffset_si, int *yoffset_si, int *roffset_si, int *boffset_si) {
float qz_left, qz_right, qz_top, qz_bottom;
quiet_zones(symbol, hide_text, &qz_left, &qz_right, &qz_top, &qz_bottom);
out_quiet_zones(symbol, hide_text, &qz_left, &qz_right, &qz_top, &qz_bottom);
*xoffset = symbol->whitespace_width + qz_left;
*roffset = symbol->whitespace_width + qz_right;
@ -510,7 +509,7 @@ INTERNAL void output_set_whitespace_offsets(const struct zint_symbol *symbol, co
/* Set composite offset and main width excluding addon (for start of addon calc) and addon text, returning
UPC/EAN type */
INTERNAL int output_process_upcean(const struct zint_symbol *symbol, int *p_main_width, int *p_comp_xoffset,
INTERNAL int out_process_upcean(const struct zint_symbol *symbol, int *p_main_width, int *p_comp_xoffset,
unsigned char addon[6], int *p_addon_gap) {
int main_width; /* Width of main linear symbol, excluding addon */
int comp_xoffset; /* Whitespace offset (if any) of main linear symbol due to having composite */
@ -588,9 +587,9 @@ INTERNAL int output_process_upcean(const struct zint_symbol *symbol, int *p_main
}
/* Calculate large bar height i.e. linear bars with zero row height that respond to the symbol height.
If scaler `si` non-zero (raster), then large_bar_height if non-zero or else row heights will be rounded to nearest
pixel and symbol height adjusted */
INTERNAL float output_large_bar_height(struct zint_symbol *symbol, int si) {
If scaler `si` non-zero (raster), then large_bar_height if non-zero or else row heights will be rounded
to nearest pixel and symbol height adjusted */
INTERNAL float out_large_bar_height(struct zint_symbol *symbol, int si) {
float fixed_height = 0.0f;
int zero_count = 0;
int round_rows = 0;
@ -609,15 +608,14 @@ INTERNAL float output_large_bar_height(struct zint_symbol *symbol, int si) {
}
if (zero_count) {
large_bar_height = (symbol->height - fixed_height) / zero_count;
large_bar_height = stripf((symbol->height - fixed_height) / zero_count);
if (large_bar_height <= 0.0f) { /* Shouldn't happen but protect against memory access violations */
large_bar_height = 0.0078125f; /* Token positive value (exact float 2**-6) */
symbol->height = large_bar_height * zero_count + fixed_height;
}
if (si && !isfintf(large_bar_height * si)) {
large_bar_height = roundf(large_bar_height * si) / si;
symbol->height = large_bar_height * zero_count + fixed_height;
large_bar_height = stripf(roundf(large_bar_height * si) / si);
}
symbol->height = stripf(large_bar_height * zero_count + fixed_height);
/* Note should never happen that have both zero_count and round_rows */
} else {
large_bar_height = 0.0f; /* Not used if zero_count zero */
@ -629,7 +627,7 @@ INTERNAL float output_large_bar_height(struct zint_symbol *symbol, int si) {
}
fixed_height += symbol->row_height[i];
}
symbol->height = fixed_height;
symbol->height = stripf(fixed_height);
}
}
@ -637,7 +635,7 @@ INTERNAL float output_large_bar_height(struct zint_symbol *symbol, int si) {
}
/* Split UPC/EAN add-on text into various constituents */
INTERNAL void output_upcean_split_text(int upceanflag, unsigned char text[],
INTERNAL void out_upcean_split_text(int upceanflag, unsigned char text[],
unsigned char textpart1[5], unsigned char textpart2[7], unsigned char textpart3[7],
unsigned char textpart4[2]) {
int i;