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

@ -34,50 +34,63 @@
/* Telepen Barcode Symbology information and History (BSiH)
https://telepen.co.uk/wp-content/uploads/2018/10/Barcode-Symbology-information-and-History.pdf */
#define SODIUM "0123456789X"
#define SODIUM_X_F (IS_NUM_F | IS_UX__F) /* SODIUM "0123456789X" */
#include <stdio.h>
#include "common.h"
static char *TeleTable[] = {
"31313131", "1131313111", "33313111", "1111313131",
"3111313111", "11333131", "13133131", "111111313111",
"31333111", "1131113131", "33113131", "1111333111",
"3111113131", "1113133111", "1311133111", "111111113131",
"3131113111", "11313331", "333331", "111131113111",
"31113331", "1133113111", "1313113111", "1111113331",
"31131331", "113111113111", "3311113111", "1111131331",
"311111113111", "1113111331", "1311111331", "11111111113111",
"31313311", "1131311131", "33311131", "1111313311",
"3111311131", "11333311", "13133311", "111111311131",
"31331131", "1131113311", "33113311", "1111331131",
"3111113311", "1113131131", "1311131131", "111111113311",
"3131111131", "1131131311", "33131311", "111131111131",
"3111131311", "1133111131", "1313111131", "111111131311",
"3113111311", "113111111131", "3311111131", "111113111311",
"311111111131", "111311111311", "131111111311", "11111111111131",
"3131311111", "11313133", "333133", "111131311111",
"31113133", "1133311111", "1313311111", "1111113133",
"313333", "113111311111", "3311311111", "11113333",
"311111311111", "11131333", "13111333", "11111111311111",
"31311133", "1131331111", "33331111", "1111311133",
"3111331111", "11331133", "13131133", "111111331111",
"3113131111", "1131111133", "33111133", "111113131111",
"3111111133", "111311131111", "131111131111", "111111111133",
"31311313", "113131111111", "3331111111", "1111311313",
"311131111111", "11331313", "13131313", "11111131111111",
"3133111111", "1131111313", "33111313", "111133111111",
"3111111313", "111313111111", "131113111111", "111111111313",
"313111111111", "1131131113", "33131113", "11113111111111",
"3111131113", "113311111111", "131311111111", "111111131113",
"3113111113", "11311111111111", "331111111111", "111113111113",
"31111111111111", "111311111113", "131111111113", "1111111111111111",
static char TeleTable[128][16] = {
{ "31313131" }, { "1131313111" }, { "33313111" }, { "1111313131" },
{ "3111313111" }, { "11333131" }, { "13133131" }, { "111111313111" },
{ "31333111" }, { "1131113131" }, { "33113131" }, { "1111333111" },
{ "3111113131" }, { "1113133111" }, { "1311133111" }, { "111111113131" },
{ "3131113111" }, { "11313331" }, { "333331" }, { "111131113111" },
{ "31113331" }, { "1133113111" }, { "1313113111" }, { "1111113331" },
{ "31131331" }, { "113111113111" }, { "3311113111" }, { "1111131331" },
{ "311111113111" }, { "1113111331" }, { "1311111331" }, { "11111111113111" },
{ "31313311" }, { "1131311131" }, { "33311131" }, { "1111313311" },
{ "3111311131" }, { "11333311" }, { "13133311" }, { "111111311131" },
{ "31331131" }, { "1131113311" }, { "33113311" }, { "1111331131" },
{ "3111113311" }, { "1113131131" }, { "1311131131" }, { "111111113311" },
{ "3131111131" }, { "1131131311" }, { "33131311" }, { "111131111131" },
{ "3111131311" }, { "1133111131" }, { "1313111131" }, { "111111131311" },
{ "3113111311" }, { "113111111131" }, { "3311111131" }, { "111113111311" },
{ "311111111131" }, { "111311111311" }, { "131111111311" }, { "11111111111131" },
{ "3131311111" }, { "11313133" }, { "333133" }, { "111131311111" },
{ "31113133" }, { "1133311111" }, { "1313311111" }, { "1111113133" },
{ "313333" }, { "113111311111" }, { "3311311111" }, { "11113333" },
{ "311111311111" }, { "11131333" }, { "13111333" }, { "11111111311111" },
{ "31311133" }, { "1131331111" }, { "33331111" }, { "1111311133" },
{ "3111331111" }, { "11331133" }, { "13131133" }, { "111111331111" },
{ "3113131111" }, { "1131111133" }, { "33111133" }, { "111113131111" },
{ "3111111133" }, { "111311131111" }, { "131111131111" }, { "111111111133" },
{ "31311313" }, { "113131111111" }, { "3331111111" }, { "1111311313" },
{ "311131111111" }, { "11331313" }, { "13131313" }, { "11111131111111" },
{ "3133111111" }, { "1131111313" }, { "33111313" }, { "111133111111" },
{ "3111111313" }, { "111313111111" }, { "131113111111" }, { "111111111313" },
{ "313111111111" }, { "1131131113" }, { "33131113" }, { "11113111111111" },
{ "3111131113" }, { "113311111111" }, { "131311111111" }, { "111111131113" },
{ "3113111113" }, { "11311111111111" }, { "331111111111" }, { "111113111113" },
{ "31111111111111" }, { "111311111113" }, { "131111111113" },
{'1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1'},
};
/* Generated by "backend/tests/test_telepen -f generate_lens -g" */
static const char TeleLens[128] = {
8, 10, 8, 10, 10, 8, 8, 12, 8, 10, 8, 10, 10, 10, 10, 12,
10, 8, 6, 12, 8, 10, 10, 10, 8, 12, 10, 10, 12, 10, 10, 14,
8, 10, 8, 10, 10, 8, 8, 12, 8, 10, 8, 10, 10, 10, 10, 12,
10, 10, 8, 12, 10, 10, 10, 12, 10, 12, 10, 12, 12, 12, 12, 14,
10, 8, 6, 12, 8, 10, 10, 10, 6, 12, 10, 8, 12, 8, 8, 14,
8, 10, 8, 10, 10, 8, 8, 12, 10, 10, 8, 12, 10, 12, 12, 12,
8, 12, 10, 10, 12, 8, 8, 14, 10, 10, 8, 12, 10, 12, 12, 12,
12, 10, 8, 14, 10, 12, 12, 12, 10, 14, 12, 12, 14, 12, 12, 16
};
INTERNAL int telepen(struct zint_symbol *symbol, unsigned char source[], int src_len) {
int i, count, check_digit;
int error_number;
char dest[521]; /* 12 (start) + 30 * 16 (max for DELs) + 16 (check digit) + 12 (stop) + 1 = 521 */
char *d = dest;
error_number = 0;
@ -88,7 +101,8 @@ INTERNAL int telepen(struct zint_symbol *symbol, unsigned char source[], int src
return ZINT_ERROR_TOO_LONG;
}
/* Start character */
strcpy(dest, TeleTable['_']);
memcpy(d, TeleTable['_'], 12);
d += 12;
for (i = 0; i < src_len; i++) {
if (source[i] > 127) {
@ -96,7 +110,8 @@ INTERNAL int telepen(struct zint_symbol *symbol, unsigned char source[], int src
strcpy(symbol->errtxt, "391: Invalid character in input data, extended ASCII not allowed");
return ZINT_ERROR_INVALID_DATA;
}
strcat(dest, TeleTable[source[i]]);
memcpy(d, TeleTable[source[i]], TeleLens[source[i]]);
d += TeleLens[source[i]];
count += source[i];
}
@ -104,14 +119,16 @@ INTERNAL int telepen(struct zint_symbol *symbol, unsigned char source[], int src
if (check_digit == 127) {
check_digit = 0;
}
strcat(dest, TeleTable[check_digit]);
memcpy(d, TeleTable[check_digit], TeleLens[check_digit]);
d += TeleLens[check_digit];
if (symbol->debug & ZINT_DEBUG_PRINT) printf("Check digit: %d\n", check_digit);
/* Stop character */
strcat(dest, TeleTable['z']);
memcpy(d, TeleTable['z'], 14);
d += 14;
expand(symbol, dest);
expand(symbol, dest, d - dest);
if (symbol->output_options & COMPLIANT_HEIGHT) {
/* Default height from various Telepen docs is based on default 26pt at X 0.01125"
@ -137,6 +154,7 @@ INTERNAL int telepen_num(struct zint_symbol *symbol, unsigned char source[], int
int error_number = 0;
int i;
char dest[521]; /* 12 (start) + 30 * 16 (max for DELs) + 16 (check digit) + 12 (stop) + 1 = 521 */
char *d = dest;
unsigned char temp[64];
count = 0;
@ -146,8 +164,8 @@ INTERNAL int telepen_num(struct zint_symbol *symbol, unsigned char source[], int
return ZINT_ERROR_TOO_LONG;
}
ustrcpy(temp, source);
to_upper(temp);
if (is_sane(SODIUM, temp, src_len) != 0) {
to_upper(temp, src_len);
if (!is_sane(SODIUM_X_F, temp, src_len)) {
strcpy(symbol->errtxt, "393: Invalid character in data (digits and \"X\" only)");
return ZINT_ERROR_INVALID_DATA;
}
@ -161,7 +179,8 @@ INTERNAL int telepen_num(struct zint_symbol *symbol, unsigned char source[], int
}
/* Start character */
strcpy(dest, TeleTable['_']);
memcpy(d, TeleTable['_'], 12);
d += 12;
for (i = 0; i < src_len; i += 2) {
if (temp[i] == 'X') {
@ -177,21 +196,24 @@ INTERNAL int telepen_num(struct zint_symbol *symbol, unsigned char source[], int
glyph += 27;
count += glyph;
}
strcat(dest, TeleTable[glyph]);
memcpy(d, TeleTable[glyph], TeleLens[glyph]);
d += TeleLens[glyph];
}
check_digit = 127 - (count % 127);
if (check_digit == 127) {
check_digit = 0;
}
strcat(dest, TeleTable[check_digit]);
memcpy(d, TeleTable[check_digit], TeleLens[check_digit]);
d += TeleLens[check_digit];
if (symbol->debug & ZINT_DEBUG_PRINT) printf("Check digit: %d\n", check_digit);
/* Stop character */
strcat(dest, TeleTable['z']);
memcpy(d, TeleTable['z'], 14);
d += 14;
expand(symbol, dest);
expand(symbol, dest, d - dest);
if (symbol->output_options & COMPLIANT_HEIGHT) {
(void) set_height(symbol, 0.0f, 32.0f, 0, 1 /*no_errtxt*/); /* Same as alphanumeric Telepen */