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,51 @@
#include <stdio.h>
#include "common.h"
#define SSET "0123456789ABCDEF"
#define SSET_F (IS_NUM_F | IS_UHX_F) /* SSET "0123456789ABCDEF" */
static const char *PlessTable[16] = {
"13131313", "31131313", "13311313", "31311313",
"13133113", "31133113", "13313113", "31313113",
"13131331", "31131331", "13311331", "31311331",
"13133131", "31133131", "13313131", "31313131"
static const char PlessTable[16][8] = {
{'1','3','1','3','1','3','1','3'}, {'3','1','1','3','1','3','1','3'}, {'1','3','3','1','1','3','1','3'},
{'3','1','3','1','1','3','1','3'}, {'1','3','1','3','3','1','1','3'}, {'3','1','1','3','3','1','1','3'},
{'1','3','3','1','3','1','1','3'}, {'3','1','3','1','3','1','1','3'}, {'1','3','1','3','1','3','3','1'},
{'3','1','1','3','1','3','3','1'}, {'1','3','3','1','1','3','3','1'}, {'3','1','3','1','1','3','3','1'},
{'1','3','1','3','3','1','3','1'}, {'3','1','1','3','3','1','3','1'}, {'1','3','3','1','3','1','3','1'},
{'3','1','3','1','3','1','3','1'}
};
static const char *MSITable[10] = {
"12121212", "12121221", "12122112", "12122121", "12211212",
"12211221", "12212112", "12212121", "21121212", "21121221"
static const char MSITable[10][8] = {
{'1','2','1','2','1','2','1','2'}, {'1','2','1','2','1','2','2','1'}, {'1','2','1','2','2','1','1','2'},
{'1','2','1','2','2','1','2','1'}, {'1','2','2','1','1','2','1','2'}, {'1','2','2','1','1','2','2','1'},
{'1','2','2','1','2','1','1','2'}, {'1','2','2','1','2','1','2','1'}, {'2','1','1','2','1','2','1','2'},
{'2','1','1','2','1','2','2','1'}
};
/* Not MSI/Plessey but the older Plessey standard */
INTERNAL int plessey(struct zint_symbol *symbol, unsigned char source[], int length) {
int i;
unsigned char *checkptr;
unsigned char checkptr[65 * 4 + 8] = {0};
static const char grid[9] = {1, 1, 1, 1, 0, 1, 0, 0, 1};
char dest[554]; /* 8 + 65 * 8 + 8 * 2 + 9 + 1 = 554 */
char *d = dest;
int error_number = 0;
if (length > 65) {
strcpy(symbol->errtxt, "370: Input too long (65 character maximum)");
return ZINT_ERROR_TOO_LONG;
}
if (is_sane(SSET, source, length) != 0) {
if (!is_sane(SSET_F, source, length)) {
strcpy(symbol->errtxt, "371: Invalid character in data (digits and \"ABCDEF\" only)");
return ZINT_ERROR_INVALID_DATA;
}
if (!(checkptr = (unsigned char *) calloc(1, length * 4 + 8))) {
strcpy(symbol->errtxt, "373: Insufficient memory for check digit CRC buffer");
return ZINT_ERROR_MEMORY;
}
/* Start character */
strcpy(dest, "31311331");
memcpy(d, "31311331", 8);
d += 8;
/* Data area */
for (i = 0; i < length; i++) {
unsigned int check = posn(SSET, source[i]);
lookup(SSET, PlessTable, source[i], dest);
for (i = 0; i < length; i++, d += 8) {
unsigned int check = source[i] - '0' - (source[i] >> 6) * 7;
memcpy(d, PlessTable[check], 8);
checkptr[4 * i] = check & 1;
checkptr[4 * i + 1] = (check >> 1) & 1;
checkptr[4 * i + 2] = (check >> 2) & 1;
@ -97,24 +98,26 @@ INTERNAL int plessey(struct zint_symbol *symbol, unsigned char source[], int len
for (i = 0; i < 8; i++) {
switch (checkptr[length * 4 + i]) {
case 0: strcat(dest, "13");
case 0: memcpy(d, "13", 2);
d += 2;
break;
case 1: strcat(dest, "31");
case 1: memcpy(d, "31", 2);
d += 2;
break;
}
}
/* Stop character */
strcat(dest, "331311313");
memcpy(d, "331311313", 9);
d += 9;
expand(symbol, dest);
expand(symbol, dest, d - dest);
// TODO: Find documentation on BARCODE_PLESSEY dimensions/height
symbol->text[0] = '\0';
ustrncat(symbol->text, source, length);
free(checkptr);
return error_number;
}
@ -154,35 +157,38 @@ static char msi_check_digit_mod11(const unsigned char source[], const int length
}
/* Plain MSI Plessey - does not calculate any check character */
static void msi_plessey_nomod(struct zint_symbol *symbol, const unsigned char source[], const int length,
char dest[]) {
static char *msi_plessey_nomod(struct zint_symbol *symbol, const unsigned char source[], const int length,
char *d) {
int i;
for (i = 0; i < length; i++) {
lookup(NEON, MSITable, source[i], dest);
for (i = 0; i < length; i++, d += 8) {
memcpy(d, MSITable[source[i] - '0'], 8);
}
symbol->text[0] = '\0';
ustrncat(symbol->text, source, length);
return d;
}
/* MSI Plessey with Modulo 10 check digit */
static void msi_plessey_mod10(struct zint_symbol *symbol, const unsigned char source[], const int length,
const int no_checktext, char dest[]) {
static char *msi_plessey_mod10(struct zint_symbol *symbol, const unsigned char source[], const int length,
const int no_checktext, char *d) {
int i;
char check_digit;
/* draw data section */
for (i = 0; i < length; i++) {
lookup(NEON, MSITable, source[i], dest);
for (i = 0; i < length; i++, d += 8) {
memcpy(d, MSITable[source[i] - '0'], 8);
}
/* calculate check digit */
check_digit = msi_check_digit_mod10(source, length);
/* draw check digit */
lookup(NEON, MSITable, check_digit, dest);
memcpy(d, MSITable[check_digit - '0'], 8);
d += 8;
symbol->text[0] = '\0';
ustrncat(symbol->text, source, length);
@ -190,11 +196,13 @@ static void msi_plessey_mod10(struct zint_symbol *symbol, const unsigned char so
symbol->text[length] = check_digit;
symbol->text[length + 1] = '\0';
}
return d;
}
/* MSI Plessey with two Modulo 10 check digits */
static void msi_plessey_mod1010(struct zint_symbol *symbol, const unsigned char source[], const int length,
const int no_checktext, char dest[]) {
static char *msi_plessey_mod1010(struct zint_symbol *symbol, const unsigned char source[], const int length,
const int no_checktext, char *d) {
int i;
unsigned char temp[65 + 2 + 1];
@ -207,8 +215,8 @@ static void msi_plessey_mod1010(struct zint_symbol *symbol, const unsigned char
temp[length + 2] = '\0';
/* draw data section */
for (i = 0; i < length + 2; i++) {
lookup(NEON, MSITable, temp[i], dest);
for (i = 0; i < length + 2; i++, d += 8) {
memcpy(d, MSITable[temp[i] - '0'], 8);
}
if (no_checktext) {
@ -217,27 +225,32 @@ static void msi_plessey_mod1010(struct zint_symbol *symbol, const unsigned char
} else {
ustrcpy(symbol->text, temp);
}
return d;
}
/* MSI Plessey with Modulo 11 check digit */
static void msi_plessey_mod11(struct zint_symbol *symbol, const unsigned char source[], const int length,
const int no_checktext, const int wrap, char dest[]) {
static char *msi_plessey_mod11(struct zint_symbol *symbol, const unsigned char source[], const int length,
const int no_checktext, const int wrap, char *d) {
/* Uses the IBM weight system if wrap = 7, and the NCR system if wrap = 9 */
int i;
char check_digit;
/* draw data section */
for (i = 0; i < length; i++) {
lookup(NEON, MSITable, source[i], dest);
for (i = 0; i < length; i++, d += 8) {
memcpy(d, MSITable[source[i] - '0'], 8);
}
/* Append check digit */
check_digit = msi_check_digit_mod11(source, length, wrap);
if (check_digit == 'A') {
lookup(NEON, MSITable, '1', dest);
lookup(NEON, MSITable, '0', dest);
memcpy(d, MSITable[1], 8);
d += 8;
memcpy(d, MSITable[0], 8);
d += 8;
} else {
lookup(NEON, MSITable, check_digit, dest);
memcpy(d, MSITable[check_digit - '0'], 8);
d += 8;
}
symbol->text[0] = '\0';
@ -250,11 +263,13 @@ static void msi_plessey_mod11(struct zint_symbol *symbol, const unsigned char so
symbol->text[length + 1] = '\0';
}
}
return d;
}
/* MSI Plessey with Modulo 11 check digit and Modulo 10 check digit */
static void msi_plessey_mod1110(struct zint_symbol *symbol, const unsigned char source[], const int length,
const int no_checktext, const int wrap, char dest[]) {
static char *msi_plessey_mod1110(struct zint_symbol *symbol, const unsigned char source[], const int length,
const int no_checktext, const int wrap, char *d) {
/* Uses the IBM weight system if wrap = 7, and the NCR system if wrap = 9 */
int i;
char check_digit;
@ -278,8 +293,8 @@ static void msi_plessey_mod1110(struct zint_symbol *symbol, const unsigned char
temp[++temp_len] = '\0';
/* draw data section */
for (i = 0; i < temp_len; i++) {
lookup(NEON, MSITable, temp[i], dest);
for (i = 0; i < temp_len; i++, d += 8) {
memcpy(d, MSITable[temp[i] - '0'], 8);
}
if (no_checktext) {
@ -288,11 +303,14 @@ static void msi_plessey_mod1110(struct zint_symbol *symbol, const unsigned char
} else {
ustrcpy(symbol->text, temp);
}
return d;
}
INTERNAL int msi_plessey(struct zint_symbol *symbol, unsigned char source[], int length) {
int error_number = 0;
char dest[550]; /* 2 + 65 * 8 + 3 * 8 + 3 + 1 = 550 */
char *d = dest;
int check_option = symbol->option_2;
int no_checktext = 0;
@ -300,7 +318,7 @@ INTERNAL int msi_plessey(struct zint_symbol *symbol, unsigned char source[], int
strcpy(symbol->errtxt, "372: Input too long (65 character maximum)");
return ZINT_ERROR_TOO_LONG;
}
if (is_sane(NEON, source, length) != 0) {
if (!is_sane(NEON_F, source, length)) {
strcpy(symbol->errtxt, "377: Invalid character in data (digits only)");
return ZINT_ERROR_INVALID_DATA;
}
@ -314,29 +332,31 @@ INTERNAL int msi_plessey(struct zint_symbol *symbol, unsigned char source[], int
}
/* Start character */
strcpy(dest, "21");
memcpy(d, "21", 2);
d += 2;
switch (check_option) {
case 0: msi_plessey_nomod(symbol, source, length, dest);
case 0: d = msi_plessey_nomod(symbol, source, length, d);
break;
case 1: msi_plessey_mod10(symbol, source, length, no_checktext, dest);
case 1: d = msi_plessey_mod10(symbol, source, length, no_checktext, d);
break;
case 2: msi_plessey_mod1010(symbol, source, length, no_checktext, dest);
case 2: d = msi_plessey_mod1010(symbol, source, length, no_checktext, d);
break;
case 3: msi_plessey_mod11(symbol, source, length, no_checktext, 7 /*IBM wrap*/, dest);
case 3: d = msi_plessey_mod11(symbol, source, length, no_checktext, 7 /*IBM wrap*/, d);
break;
case 4: msi_plessey_mod1110(symbol, source, length, no_checktext, 7 /*IBM wrap*/, dest);
case 4: d = msi_plessey_mod1110(symbol, source, length, no_checktext, 7 /*IBM wrap*/, d);
break;
case 5: msi_plessey_mod11(symbol, source, length, no_checktext, 9 /*NCR wrap*/, dest);
case 5: d = msi_plessey_mod11(symbol, source, length, no_checktext, 9 /*NCR wrap*/, d);
break;
case 6: msi_plessey_mod1110(symbol, source, length, no_checktext, 9 /*NCR wrap*/, dest);
case 6: d = msi_plessey_mod1110(symbol, source, length, no_checktext, 9 /*NCR wrap*/, d);
break;
}
/* Stop character */
strcat(dest, "121");
memcpy(d, "121", 3);
d += 3;
expand(symbol, dest);
expand(symbol, dest, d - dest);
// TODO: Find documentation on BARCODE_MSI_PLESSEY dimensions/height