QR optimize encoding modes

This commit is contained in:
gitlost 2019-12-16 17:31:52 +00:00
parent 74082e4d1b
commit e331fd1e90
11 changed files with 1944 additions and 971 deletions

View File

@ -36,7 +36,7 @@ if (CMAKE_COMPILER_IS_GNUCXX OR CMAKE_COMPILER_IS_GNUCC)
if (ZINT_SANITIZE)
add_compile_options("-fsanitize=undefined")
add_compile_options("-fsanitize=address")
set (CMAKE_EXE_LINKER_FLAGS "-fsanitize=undefined -fsanitize=address")
set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fsanitize=undefined -fsanitize=address")
endif (ZINT_SANITIZE)
endif (CMAKE_COMPILER_IS_GNUCXX OR CMAKE_COMPILER_IS_GNUCC)

View File

@ -459,3 +459,17 @@ void pn_define_mode(char* mode, const unsigned int data[], const size_t length,
printf(" Mode: %.*s\n", (int)length, mode);
}
}
/* Dumps hex-formatted codewords in symbol->errtxt (for use in testing) */
void debug_test_codeword_dump(struct zint_symbol *symbol, unsigned char* codewords, int length) {
int i, max = length, cnt_len = 0;
if (length > 30) { /* 30*3 < errtxt 92 (100 - "Warning ") chars */
sprintf(symbol->errtxt, "(%d) ", length); /* Place the number of codewords at the front */
cnt_len = strlen(symbol->errtxt);
max = 30 - (cnt_len + 2) / 3;
}
for (i = 0; i < max; i++) {
sprintf(symbol->errtxt + cnt_len + i * 3, "%02X ", codewords[i]);
}
symbol->errtxt[strlen(symbol->errtxt) - 1] = '\0'; /* Zap last space */
}

View File

@ -68,7 +68,6 @@ extern "C" {
extern int module_is_set(const struct zint_symbol *symbol, const int y_coord, const int x_coord);
extern void set_module(struct zint_symbol *symbol, const int y_coord, const int x_coord);
extern int istwodigits(const unsigned char source[], const size_t position);
extern int parunmodd(const unsigned char llyth);
extern void expand(struct zint_symbol *symbol, const char data[]);
extern void unset_module(struct zint_symbol *symbol, const int y_coord, const int x_coord);
extern int is_stackable(const int symbology);
@ -79,11 +78,14 @@ extern "C" {
extern void set_minimum_height(struct zint_symbol *symbol, const int min_height);
typedef unsigned int* (*pn_head_costs)(unsigned int state[]);
typedef unsigned int (*pn_switch_cost)(unsigned int state[], const int j, const int k);
typedef unsigned int (*pn_switch_cost)(unsigned int state[], const int k, const int j);
typedef unsigned int (*pn_eod_cost)(unsigned int state[], const int k);
typedef void (*pn_cur_cost)(unsigned int state[], const unsigned int data[], const size_t length, const int i, char* char_modes, unsigned int prev_costs[], unsigned int cur_costs[]);
extern void pn_define_mode(char* mode, const unsigned int data[], const size_t length, const int debug,
unsigned int state[], const char mode_types[], const int num_modes, pn_head_costs head_costs, pn_switch_cost switch_cost, pn_eod_cost eod_cost, pn_cur_cost cur_cost);
extern void debug_test_codeword_dump(struct zint_symbol *symbol, unsigned char* codewords, int length);
#ifdef __cplusplus
}
#endif /* __cplusplus */

View File

@ -131,8 +131,8 @@ static unsigned int* gm_head_costs(unsigned int state[]) {
return head_costs;
}
/* Cost of switching modes - see AIMD014 Rev. 1.63 Table 9 Type conversion codes */
static unsigned int gm_switch_cost(unsigned int state[], const int j, const int k) {
/* Cost of switching modes from k to j - see AIMD014 Rev. 1.63 Table 9 Type conversion codes */
static unsigned int gm_switch_cost(unsigned int state[], const int k, const int j) {
static unsigned int switch_costs[GM_NUM_MODES][GM_NUM_MODES] = {
/* H N L U M B */
/*H*/ { 0, (13 + 2) * GM_MULT, 13 * GM_MULT, 13 * GM_MULT, 13 * GM_MULT, (13 + 9) * GM_MULT },
@ -143,7 +143,7 @@ static unsigned int gm_switch_cost(unsigned int state[], const int j, const int
/*B*/ { 4 * GM_MULT, (4 + 2) * GM_MULT, 4 * GM_MULT, 4 * GM_MULT, 4 * GM_MULT, 0 },
};
return switch_costs[j][k];
return switch_costs[k][j];
}
/* Final end-of-data cost - see AIMD014 Rev. 1.63 Table 9 Type conversion codes */
@ -232,7 +232,7 @@ static void add_byte_count(char binary[], const size_t byte_count_posn, const in
}
/* Add a control character to the data stream */
void add_shift_char(char binary[], int shifty, int debug) {
static void add_shift_char(char binary[], int shifty, int debug) {
int i;
int glyph = 0;
@ -716,24 +716,11 @@ static int gm_encode(unsigned int gbdata[], const size_t length, char binary[],
return 0;
}
static void gm_test_codeword_dump(struct zint_symbol *symbol, int* codewords, int length) {
int i, max = length, cnt_len = 0;
if (length > 33) {
sprintf(symbol->errtxt, "(%d) ", length); /* Place the number of codewords at the front */
cnt_len = strlen(symbol->errtxt);
max = 33 - (cnt_len + 2) / 3;
}
for (i = 0; i < max; i++) { /* 33*3 < errtxt 100 chars */
sprintf(symbol->errtxt + cnt_len + i * 3, "%02X ", codewords[i]);
}
symbol->errtxt[strlen(symbol->errtxt) - 1] = '\0'; /* Zap last space */
}
static void gm_add_ecc(const char binary[], const size_t data_posn, const int layers, const int ecc_level, int word[]) {
static void gm_add_ecc(const char binary[], const size_t data_posn, const int layers, const int ecc_level, unsigned char word[]) {
int data_cw, i, j, wp, p;
int n1, b1, n2, b2, e1, b3, e2;
int block_size, ecc_size;
int data[1320], block[130];
unsigned char data[1320], block[130];
unsigned char data_block[115], ecc_block[70];
data_cw = gm_data_codewords[((layers - 1) * 5) + (ecc_level - 1)];
@ -816,7 +803,7 @@ static void gm_add_ecc(const char binary[], const size_t data_posn, const int la
}
}
void place_macromodule(char grid[], int x, int y, int word1, int word2, int size) {
static void place_macromodule(char grid[], int x, int y, int word1, int word2, int size) {
int i, j;
i = (x * 6) + 1;
@ -866,7 +853,7 @@ void place_macromodule(char grid[], int x, int y, int word1, int word2, int size
}
}
void place_data_in_grid(int word[], char grid[], int modules, int size) {
static void place_data_in_grid(unsigned char word[], char grid[], int modules, int size) {
int x, y, macromodule, offset;
offset = 13 - ((modules - 1) / 2);
@ -879,7 +866,7 @@ void place_data_in_grid(int word[], char grid[], int modules, int size) {
}
/* Place the layer ID into each macromodule */
void place_layer_id(char* grid, int size, int layers, int modules, int ecc_level) {
static void place_layer_id(char* grid, int size, int layers, int modules, int ecc_level) {
int i, j, layer, start, stop;
#ifndef _MSC_VER
@ -938,7 +925,8 @@ int grid_matrix(struct zint_symbol *symbol, const unsigned char source[], size_t
int x, y, i;
char binary[9300];
int data_cw, input_latch = 0;
int word[1460], data_max, reader = 0;
unsigned char word[1460];
int data_max, reader = 0;
#ifndef _MSC_VER
unsigned int gbdata[length + 1];
@ -1070,7 +1058,7 @@ int grid_matrix(struct zint_symbol *symbol, const unsigned char source[], size_t
}
gm_add_ecc(binary, data_cw, layers, ecc_level, word);
if (symbol->debug & ZINT_DEBUG_TEST) gm_test_codeword_dump(symbol, word, data_cw);
if (symbol->debug & ZINT_DEBUG_TEST) debug_test_codeword_dump(symbol, word, data_cw);
size = 6 + (layers * 12);
modules = 1 + (layers * 2);
@ -1123,5 +1111,3 @@ int grid_matrix(struct zint_symbol *symbol, const unsigned char source[], size_t
return 0;
}

View File

@ -356,8 +356,8 @@ static unsigned int* hx_head_costs(unsigned int state[]) {
return head_costs;
}
/* Costs of switching modes */
static unsigned int hx_switch_cost(unsigned int state[], const int j, const int k) {
/* Cost of switching modes from k to j */
static unsigned int hx_switch_cost(unsigned int state[], const int k, const int j) {
static unsigned int switch_costs[HX_NUM_MODES][HX_NUM_MODES] = {
/* N T B 1 2 D F */
/*N*/ { 0, (10 + 4) * HX_MULT, (10 + 4 + 13) * HX_MULT, (10 + 4) * HX_MULT, (10 + 4) * HX_MULT, (10 + 4) * HX_MULT, 10 * HX_MULT },
@ -369,7 +369,7 @@ static unsigned int hx_switch_cost(unsigned int state[], const int j, const int
/*F*/ { 4 * HX_MULT, 4 * HX_MULT, (4 + 13) * HX_MULT, 4 * HX_MULT, 4 * HX_MULT, 4 * HX_MULT, 0 },
};
return switch_costs[j][k];
return switch_costs[k][j];
}
/* Final end-of-data costs */
@ -1369,19 +1369,6 @@ static int hx_apply_bitmask(unsigned char *grid, int size) {
return best_pattern;
}
static void hx_test_codeword_dump(struct zint_symbol *symbol, unsigned char* codewords, int length) {
int i, max = length, cnt_len = 0;
if (length > 33) {
sprintf(symbol->errtxt, "(%d) ", length); /* Place the number of codewords at the front */
cnt_len = strlen(symbol->errtxt);
max = 33 - (cnt_len + 2) / 3;
}
for (i = 0; i < max; i++) { /* 33*3 < errtxt 100 chars */
sprintf(symbol->errtxt + cnt_len + i * 3, "%02X ", codewords[i]);
}
symbol->errtxt[strlen(symbol->errtxt) - 1] = '\0'; /* Zap last space */
}
/* Han Xin Code - main */
int han_xin(struct zint_symbol *symbol, const unsigned char source[], size_t length) {
int est_binlen;
@ -1555,7 +1542,7 @@ int han_xin(struct zint_symbol *symbol, const unsigned char source[], size_t len
}
printf("\n");
}
if (symbol->debug & ZINT_DEBUG_TEST) hx_test_codeword_dump(symbol, datastream, data_codewords);
if (symbol->debug & ZINT_DEBUG_TEST) debug_test_codeword_dump(symbol, datastream, data_codewords);
hx_setup_grid(grid, size, version);

File diff suppressed because it is too large Load Diff

View File

@ -38,6 +38,7 @@
#define RHODIUM "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ $%*+-./:"
#define RMQR_VERSION 100
#define MICROQR_VERSION 200
/* From ISO/IEC 18004:2006 Table 7 */
static const unsigned short int qr_data_codewords_L[] = {

View File

@ -27,7 +27,7 @@ if (${CMAKE_CXX_COMPILER_ID} STREQUAL "GNU")
if (ZINT_SANITIZE)
add_compile_options("-fsanitize=undefined")
add_compile_options("-fsanitize=address")
set (CMAKE_EXE_LINKER_FLAGS "-fsanitize=undefined -fsanitize=address")
set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fsanitize=undefined -fsanitize=address")
endif (ZINT_SANITIZE)
endif ()

View File

@ -153,11 +153,11 @@ static void test_input(void)
/* 40*/ { UNICODE_MODE, 0, " 200mA至", 0, 0, "2F 60 40 00 60 2B 78 63 41 7F 40", "M6 H1 (GB 2312)" },
/* 41*/ { UNICODE_MODE, 0, "2A tel:86 019 82512738", 0, 0, "28 22 5F 4F 29 48 5F 6D 7E 6F 55 57 1F 28 63 0F 5A 11 64 0F 74", "M2 L5(with control) N15 (ASCII)" },
/* 42*/ { UNICODE_MODE, 0, "至2A tel:86 019 82512738", 0, 0, "30 07 56 60 4C 48 13 6A 32 17 7B 3F 5B 75 35 67 6A 18 63 76 44 39 03 7D 00", "B4 L5(with control) N15 (GB 2312)" },
/* 43*/ { UNICODE_MODE, 0, "AAT2556 电池充电器+降压转换器 200mA至2A tel:86 019 82512738", 0, 0, "(62) 29 22 22 1C 4E 41 42 7E 0A 40 14 00 37 7E 6F 00 62 7E 2C 00 1C 7E 4B 00 41 7E 18 00 42 7E 61", "M8 H11 M6 B4 L5(with control) N15 (GB 2312) (*NOT SAME* as D3 example, M8 H11 M6 H1 M3 L4(with control) N15, which uses a few more bits)" },
/* 44*/ { UNICODE_MODE, 0, "::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::", 0, 0, "(588) 37 68 68 68 68 68 74 7E 74 74 74 74 74 3A 3A 3A 3A 3A 3A 3A 1D 1D 1D 1D 1D 1D 1D 0E 0E 0E 0E", "B512 (ASCII)" },
/* 45*/ { UNICODE_MODE, 0, "::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::\177", 0, 0, "(591) 37 68 68 68 68 68 74 7E 74 74 74 74 74 3A 3A 3A 3A 3A 3A 3A 1D 1D 1D 1D 1D 1D 1D 0E 0E 0E 0E", "B513 (ASCII)" },
/* 46*/ { UNICODE_MODE, 0, ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::至", 0, 0, "(591) 37 68 68 68 68 68 74 7C 74 74 74 74 74 3A 3A 3A 3A 3A 3A 3A 1D 1D 1D 1D 1D 1D 1D 0E 0E 0E 0E", "B511 H1 (GB 2312)" },
/* 47*/ { UNICODE_MODE, 0, ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::至:", 0, 0, "(592) 37 68 68 68 68 68 74 7E 74 74 74 74 74 3A 3A 3A 3A 3A 3A 3A 1D 1D 1D 1D 1D 1D 1D 0E 0E 0E 0E", "B513 (GB 2312)" },
/* 43*/ { UNICODE_MODE, 0, "AAT2556 电池充电器+降压转换器 200mA至2A tel:86 019 82512738", 0, 0, "(62) 29 22 22 1C 4E 41 42 7E 0A 40 14 00 37 7E 6F 00 62 7E 2C 00 1C 7E 4B 00 41 7E 18 00", "M8 H11 M6 B4 L5(with control) N15 (GB 2312) (*NOT SAME* as D3 example, M8 H11 M6 H1 M3 L4(with control) N15, which uses a few more bits)" },
/* 44*/ { UNICODE_MODE, 0, "::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::", 0, 0, "(588) 37 68 68 68 68 68 74 7E 74 74 74 74 74 3A 3A 3A 3A 3A 3A 3A 1D 1D 1D 1D 1D 1D 1D 0E", "B512 (ASCII)" },
/* 45*/ { UNICODE_MODE, 0, "::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::\177", 0, 0, "(591) 37 68 68 68 68 68 74 7E 74 74 74 74 74 3A 3A 3A 3A 3A 3A 3A 1D 1D 1D 1D 1D 1D 1D 0E", "B513 (ASCII)" },
/* 46*/ { UNICODE_MODE, 0, ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::至", 0, 0, "(591) 37 68 68 68 68 68 74 7C 74 74 74 74 74 3A 3A 3A 3A 3A 3A 3A 1D 1D 1D 1D 1D 1D 1D 0E", "B511 H1 (GB 2312)" },
/* 47*/ { UNICODE_MODE, 0, ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::至:", 0, 0, "(592) 37 68 68 68 68 68 74 7E 74 74 74 74 74 3A 3A 3A 3A 3A 3A 3A 1D 1D 1D 1D 1D 1D 1D 0E", "B513 (GB 2312)" },
};
int data_size = sizeof(data) / sizeof(struct item);

View File

@ -34,7 +34,7 @@
#include "testcommon.h"
#define TEST_IMAIL_CSV_MAX 1000
#define TEST_IMAIL_CSV_MAX 500
static void test_csv(void)
{

File diff suppressed because it is too large Load Diff