Initial implementation of rMQR

According to draft dated June 2018
This commit is contained in:
Robin Stuart 2019-11-25 19:08:25 +00:00
parent 385a0a246f
commit ad28de1819
6 changed files with 783 additions and 211 deletions

View file

@ -1,8 +1,7 @@
/* qr.c Handles QR Code */
/* qr.c Handles QR Code, Micro QR Code, UPNQR and rMQR
/*
libzint - the open source barcode library
Copyright (C) 2009 -2017 Robin Stuart <rstuart114@gmail.com>
Copyright (C) 2009 - 2019 Robin Stuart <rstuart114@gmail.com>
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
@ -147,7 +146,7 @@ static int tribus(const int version,const int a,const int b,const int c) {
}
/* Convert input data to a binary stream and add padding */
static void qr_binary(int datastream[], const int version, const int target_binlen, const char mode[], const int jisdata[], const size_t length, const int gs1, const int eci, const int est_binlen,const int debug) {
static void qr_binary(int datastream[], const int version, const int target_codewords, const char mode[], const int jisdata[], const size_t length, const int gs1, const int eci, const int est_binlen,const int debug) {
int position = 0;
int i;
char padbits;
@ -163,10 +162,14 @@ static void qr_binary(int datastream[], const int version, const int target_binl
strcpy(binary, "");
if (gs1) {
strcat(binary, "0101"); /* FNC1 */
if (version < RMQR_VERSION) {
strcat(binary, "0101"); /* FNC1 */
} else {
strcat(binary, "101");
}
}
if (eci != 0) {
if (eci != 0) { /* Not applicable to RMQR */
strcat(binary, "0111"); /* ECI (Table 4) */
if (eci <= 127) {
bin_append(eci, 8, binary); /* 000000 to 000127 */
@ -198,10 +201,18 @@ static void qr_binary(int datastream[], const int version, const int target_binl
case 'K':
/* Kanji mode */
/* Mode indicator */
strcat(binary, "1000");
if (version < RMQR_VERSION) {
strcat(binary, "1000");
} else {
strcat(binary, "100");
}
/* Character count indicator */
bin_append(short_data_block_length, tribus(version, 8, 10, 12), binary);
if (version < RMQR_VERSION) {
bin_append(short_data_block_length, tribus(version, 8, 10, 12), binary);
} else {
bin_append(short_data_block_length, rmqr_kanji_cci[version - RMQR_VERSION], binary);
}
if (debug) {
printf("Kanji block (length %d)\n\t", short_data_block_length);
@ -235,10 +246,18 @@ static void qr_binary(int datastream[], const int version, const int target_binl
case 'B':
/* Byte mode */
/* Mode indicator */
strcat(binary, "0100");
if (version < RMQR_VERSION) {
strcat(binary, "0100");
} else {
strcat(binary, "011");
}
/* Character count indicator */
bin_append(short_data_block_length, tribus(version, 8, 16, 16), binary);
if (version < RMQR_VERSION) {
bin_append(short_data_block_length, tribus(version, 8, 16, 16), binary);
} else {
bin_append(short_data_block_length, rmqr_byte_cci[version - RMQR_VERSION], binary);
}
if (debug) {
printf("Byte block (length %d)\n\t", short_data_block_length);
@ -267,7 +286,11 @@ static void qr_binary(int datastream[], const int version, const int target_binl
case 'A':
/* Alphanumeric mode */
/* Mode indicator */
strcat(binary, "0010");
if (version < RMQR_VERSION) {
strcat(binary, "0010");
} else {
strcat(binary, "010");
}
percent_count = 0;
for (i = 0; i < short_data_block_length; i++) {
@ -275,9 +298,13 @@ static void qr_binary(int datastream[], const int version, const int target_binl
percent_count++;
}
}
/* Character count indicator */
bin_append(short_data_block_length + percent_count, tribus(version, 9, 11, 13), binary);
if (version < RMQR_VERSION) {
bin_append(short_data_block_length + percent_count, tribus(version, 9, 11, 13), binary);
} else {
bin_append(short_data_block_length + percent_count, rmqr_alphanum_cci[version - RMQR_VERSION], binary);
}
if (debug) {
printf("Alpha block (length %d)\n\t", short_data_block_length + percent_count);
@ -365,10 +392,18 @@ static void qr_binary(int datastream[], const int version, const int target_binl
case 'N':
/* Numeric mode */
/* Mode indicator */
strcat(binary, "0001");
if (version >= RMQR_VERSION) {
strcat(binary, "0001");
} else {
strcat(binary, "001");
}
/* Character count indicator */
bin_append(short_data_block_length, tribus(version, 10, 12, 14), binary);
if (version < RMQR_VERSION) {
bin_append(short_data_block_length, tribus(version, 10, 12, 14), binary);
} else {
bin_append(short_data_block_length, rmqr_numeric_cci[version - RMQR_VERSION], binary);
}
if (debug) {
printf("Number block (length %d)\n\t", short_data_block_length);
@ -416,7 +451,11 @@ static void qr_binary(int datastream[], const int version, const int target_binl
} while (position < length);
/* Terminator */
strcat(binary, "0000");
if (version < RMQR_VERSION) {
strcat(binary, "0000");
} else {
strcat(binary, "000");
}
current_binlen = (int)strlen(binary);
padbits = 8 - (current_binlen % 8);
@ -443,7 +482,7 @@ static void qr_binary(int datastream[], const int version, const int target_binl
/* Add pad codewords */
toggle = 0;
for (i = current_bytes; i < target_binlen; i++) {
for (i = current_bytes; i < target_codewords; i++) {
if (toggle == 0) {
datastream[i] = 0xec;
toggle = 1;
@ -455,7 +494,7 @@ static void qr_binary(int datastream[], const int version, const int target_binl
if (debug) {
printf("Resulting codewords:\n\t");
for (i = 0; i < target_binlen; i++) {
for (i = 0; i < target_codewords; i++) {
printf("0x%2X ", datastream[i]);
}
printf("\n");
@ -463,14 +502,20 @@ static void qr_binary(int datastream[], const int version, const int target_binl
}
/* Split data into blocks, add error correction and then interleave the blocks and error correction data */
static void add_ecc(int fullstream[],const int datastream[],const int version,const int data_cw,const int blocks) {
int ecc_cw = qr_total_codewords[version - 1] - data_cw;
static void add_ecc(int fullstream[],const int datastream[],const int version,const int data_cw,const int blocks,int debug) {
int ecc_cw;
if (version < RMQR_VERSION) {
ecc_cw = qr_total_codewords[version - 1] - data_cw;
} else {
ecc_cw = rmqr_total_codewords[version - RMQR_VERSION] - data_cw;
}
int short_data_block_length = data_cw / blocks;
int qty_long_blocks = data_cw % blocks;
int qty_short_blocks = blocks - qty_long_blocks;
int ecc_block_length = ecc_cw / blocks;
int i, j, length_this_block, posn, debug = 0;
int i, j, length_this_block, posn;
#ifndef _MSC_VER
unsigned char data_block[short_data_block_length + 2];
@ -670,35 +715,36 @@ static int cwbit(const int* fullstream,const int i) {
return resultant;
}
static void populate_grid(unsigned char* grid,const int size,const int* fullstream,const int cw) {
static void populate_grid(unsigned char* grid,const int h_size,const int v_size,const int* fullstream,const int cw) {
int direction = 1; /* up */
int row = 0; /* right hand side */
int i, n, y;
n = cw * 8;
y = size - 1;
y = v_size - 1;
i = 0;
do {
int x = (size - 2) - (row * 2);
if (x < 6)
int x = (h_size - 2) - (row * 2);
if ((x < 6) && (v_size == h_size))
x--; /* skip over vertical timing pattern */
if (!(grid[(y * size) + (x + 1)] & 0xf0)) {
if (!(grid[(y * h_size) + (x + 1)] & 0xf0)) {
if (cwbit(fullstream, i)) {
grid[(y * size) + (x + 1)] = 0x01;
grid[(y * h_size) + (x + 1)] = 0x01;
} else {
grid[(y * size) + (x + 1)] = 0x00;
grid[(y * h_size) + (x + 1)] = 0x00;
}
i++;
}
if (i < n) {
if (!(grid[(y * size) + x] & 0xf0)) {
if (!(grid[(y * h_size) + x] & 0xf0)) {
if (cwbit(fullstream, i)) {
grid[(y * size) + x] = 0x01;
grid[(y * h_size) + x] = 0x01;
} else {
grid[(y * size) + x] = 0x00;
grid[(y * h_size) + x] = 0x00;
}
i++;
}
@ -715,10 +761,10 @@ static void populate_grid(unsigned char* grid,const int size,const int* fullstre
y = 0;
direction = 0;
}
if (y == size) {
if (y == v_size) {
/* reached the bottom */
row++;
y = size - 1;
y = v_size - 1;
direction = 1;
}
} while (i < n);
@ -1335,11 +1381,15 @@ static int getBinaryLength(const int version,char inputMode[],const int inputDat
currentMode = ' '; // Null
if (gs1 == 1) {
count += 4;
if (version < RMQR_VERSION) {
count += 4;
} else {
count += 3;
}
}
if (eci != 0) {
count += 12;
count += 12; // RMQR does not support ECI
}
for (i = 0; i < inputLength; i++) {
@ -1347,11 +1397,19 @@ static int getBinaryLength(const int version,char inputMode[],const int inputDat
count += 4;
switch (inputMode[i]) {
case 'K':
count += tribus(version, 8, 10, 12);
if (version < RMQR_VERSION) {
count += tribus(version, 8, 10, 12);
} else {
count += 3 + rmqr_kanji_cci[version - RMQR_VERSION];
}
count += (blockLength(i, inputMode, inputLength) * 13);
break;
case 'B':
count += tribus(version, 8, 16, 16);
if (version < RMQR_VERSION) {
count += tribus(version, 8, 16, 16);
} else {
count += 3 + rmqr_byte_cci[version - RMQR_VERSION];
}
for (j = i; j < (i + blockLength(i, inputMode, inputLength)); j++) {
if (inputData[j] > 0xff) {
count += 16;
@ -1361,7 +1419,11 @@ static int getBinaryLength(const int version,char inputMode[],const int inputDat
}
break;
case 'A':
count += tribus(version, 9, 11, 13);
if (version < RMQR_VERSION) {
count += tribus(version, 9, 11, 13);
} else {
count += 3 + rmqr_alphanum_cci[version - RMQR_VERSION];
}
alphalength = blockLength(i, inputMode, inputLength);
// In alphanumeric mode % becomes %%
for (j = i; j < (i + alphalength); j++) {
@ -1381,7 +1443,11 @@ static int getBinaryLength(const int version,char inputMode[],const int inputDat
}
break;
case 'N':
count += tribus(version, 10, 12, 14);
if (version < RMQR_VERSION) {
count += tribus(version, 10, 12, 14);
} else {
count += 3 + rmqr_numeric_cci[version - RMQR_VERSION];
}
switch (blockLength(i, inputMode, inputLength) % 3) {
case 0:
count += (blockLength(i, inputMode, inputLength) / 3) * 10;
@ -1406,7 +1472,7 @@ static int getBinaryLength(const int version,char inputMode[],const int inputDat
int qr_code(struct zint_symbol *symbol, const unsigned char source[], size_t length) {
int i, j, est_binlen;
int ecc_level, autosize, version, max_cw, target_binlen, blocks, size;
int ecc_level, autosize, version, max_cw, target_codewords, blocks, size;
int bitmask, gs1;
int canShrink;
@ -1569,7 +1635,7 @@ int qr_code(struct zint_symbol *symbol, const unsigned char source[], size_t len
return ZINT_ERROR_TOO_LONG;
}
}
/* Ensure maxium error correction capacity */
if (est_binlen <= qr_data_codewords_M[version - 1] * 8) {
ecc_level = LEVEL_M;
@ -1581,30 +1647,30 @@ int qr_code(struct zint_symbol *symbol, const unsigned char source[], size_t len
ecc_level = LEVEL_H;
}
target_binlen = qr_data_codewords_L[version - 1];
target_codewords = qr_data_codewords_L[version - 1];
blocks = qr_blocks_L[version - 1];
switch (ecc_level) {
case LEVEL_M: target_binlen = qr_data_codewords_M[version - 1];
case LEVEL_M: target_codewords = qr_data_codewords_M[version - 1];
blocks = qr_blocks_M[version - 1];
break;
case LEVEL_Q: target_binlen = qr_data_codewords_Q[version - 1];
case LEVEL_Q: target_codewords = qr_data_codewords_Q[version - 1];
blocks = qr_blocks_Q[version - 1];
break;
case LEVEL_H: target_binlen = qr_data_codewords_H[version - 1];
case LEVEL_H: target_codewords = qr_data_codewords_H[version - 1];
blocks = qr_blocks_H[version - 1];
break;
}
#ifndef _MSC_VER
int datastream[target_binlen + 1];
int datastream[target_codewords + 1];
int fullstream[qr_total_codewords[version - 1] + 1];
#else
datastream = (int *) _alloca((target_binlen + 1) * sizeof (int));
datastream = (int *) _alloca((target_codewords + 1) * sizeof (int));
fullstream = (int *) _alloca((qr_total_codewords[version - 1] + 1) * sizeof (int));
#endif
qr_binary(datastream, version, target_binlen, mode, jisdata, length, gs1, symbol->eci, est_binlen, symbol->debug);
add_ecc(fullstream, datastream, version, target_binlen, blocks);
qr_binary(datastream, version, target_codewords, mode, jisdata, length, gs1, symbol->eci, est_binlen, symbol->debug);
add_ecc(fullstream, datastream, version, target_codewords, blocks, symbol->debug);
size = qr_sizes[version - 1];
#ifndef _MSC_VER
@ -1620,7 +1686,7 @@ int qr_code(struct zint_symbol *symbol, const unsigned char source[], size_t len
}
setup_grid(grid, size, version);
populate_grid(grid, size, fullstream, qr_total_codewords[version - 1]);
populate_grid(grid, size, size, fullstream, qr_total_codewords[version - 1]);
if (version >= 7) {
add_version_info(grid, size, version);
@ -1647,8 +1713,6 @@ int qr_code(struct zint_symbol *symbol, const unsigned char source[], size_t len
return 0;
}
/* NOTE: From this point forward concerns Micro QR Code only */
static int micro_qr_intermediate(char binary[], const int jisdata[], const char mode[], const size_t length, int *kanji_used, int *alphanum_used, int *byte_used,const int debug) {
/* Convert input data to an "intermediate stage" where data is binary encoded but
control information is not */
@ -2901,7 +2965,7 @@ int microqr(struct zint_symbol *symbol, const unsigned char source[], size_t len
/* For UPNQR the symbol size and error correction capacity is fixed */
int upnqr(struct zint_symbol *symbol, const unsigned char source[], size_t length) {
int i, j, est_binlen;
int ecc_level, version, target_binlen, blocks, size;
int ecc_level, version, target_codewords, blocks, size;
int bitmask, error_number;
#ifndef _MSC_VER
@ -2958,19 +3022,19 @@ int upnqr(struct zint_symbol *symbol, const unsigned char source[], size_t lengt
version = 15; // 77 x 77
target_binlen = qr_data_codewords_M[version - 1];
target_codewords = qr_data_codewords_M[version - 1];
blocks = qr_blocks_M[version - 1];
#ifndef _MSC_VER
int datastream[target_binlen + 1];
int datastream[target_codewords + 1];
int fullstream[qr_total_codewords[version - 1] + 1];
#else
datastream = (int *) _alloca((target_binlen + 1) * sizeof (int));
datastream = (int *) _alloca((target_codewords + 1) * sizeof (int));
fullstream = (int *) _alloca((qr_total_codewords[version - 1] + 1) * sizeof (int));
#endif
qr_binary(datastream, version, target_binlen, mode, jisdata, length, 0, symbol->eci, est_binlen, symbol->debug);
add_ecc(fullstream, datastream, version, target_binlen, blocks);
qr_binary(datastream, version, target_codewords, mode, jisdata, length, 0, symbol->eci, est_binlen, symbol->debug);
add_ecc(fullstream, datastream, version, target_codewords, blocks, symbol->debug);
size = qr_sizes[version - 1];
#ifndef _MSC_VER
@ -2986,7 +3050,7 @@ int upnqr(struct zint_symbol *symbol, const unsigned char source[], size_t lengt
}
setup_grid(grid, size, version);
populate_grid(grid, size, fullstream, qr_total_codewords[version - 1]);
populate_grid(grid, size, size, fullstream, qr_total_codewords[version - 1]);
add_version_info(grid, size, version);
@ -3009,4 +3073,367 @@ int upnqr(struct zint_symbol *symbol, const unsigned char source[], size_t lengt
return 0;
}
static void setup_rmqr_grid(unsigned char* grid,const int h_size,const int v_size,const int version) {
int i, j;
char alignment[] = {0x1F, 0x11, 0x15, 0x11, 0x1F};
int h_version, finder_position;
/* Add timing patterns - top and bottom */
for (i = 0; i < h_size; i++) {
if (i % 2) {
grid[i] = 0x20;
grid[((v_size - 1) * h_size) + i] = 0x20;
} else {
grid[i] = 0x21;
grid[((v_size - 1) * h_size) + i] = 0x21;
}
}
/* Add timing patterns - left and right */
for (i = 0; i < v_size; i++) {
if (i % 2) {
grid[i * h_size] = 0x20;
grid[(i * h_size) + (h_size - 1)] = 0x20;
} else {
grid[i * h_size] = 0x21;
grid[(i * h_size) + (h_size - 1)] = 0x21;
}
}
/* Add finder pattern */
place_finder(grid, h_size, 0, 0); // This works because finder is always top left
/* Add finder sub-pattern to bottom right */
for (i = 0; i < 5; i++) {
for (j = 0; j < 5; j++) {
if (alignment[j] & 0x10 >> i) {
grid[((v_size - 5) * h_size) + (h_size * i) + (h_size - 5) + j] = 0x11;
} else {
grid[((v_size - 5) * h_size) + (h_size * i) + (h_size - 5) + j] = 0x10;
}
}
}
/* Add corner finder pattern - bottom left */
grid[(v_size - 2) * h_size] = 0x11;
grid[((v_size - 2) * h_size) + 1] = 0x10;
grid[((v_size - 1) * h_size) + 1] = 0x11;
/* Add corner finder pattern - top right */
grid[h_size - 2] = 0x11;
grid[(h_size * 2) - 2] = 0x10;
grid[(h_size * 2) - 1] = 0x11;
/* Add seperator */
for (i = 0; i < 7; i++) {
grid[(i * h_size) + 7] = 0x20;
}
if (v_size > 7) {
// Note for v_size = 9 this overrides the bottom right corner finder pattern
for(i = 0; i < 8; i++) {
grid[(7 * h_size) + i] = 0x20;
}
}
/* Add alignment patterns */
if (h_size > 27) {
for(i = 0; i < 5; i++) {
if (h_size == rmqr_width[i]) {
h_version = i;
}
}
for(i = 0; i < 4; i++) {
finder_position = rmqr_table_d1[(h_version * 4) + i];
if (finder_position != 0) {
for (j = 0; j < v_size; j++) {
if (j % 2) {
grid[(j * h_size) + finder_position] = 0x10;
} else {
grid[(j * h_size) + finder_position] = 0x11;
}
}
// Top square
grid[h_size + finder_position - 1] = 0x11;
grid[(h_size * 2) + finder_position - 1] = 0x11;
grid[h_size + finder_position + 1] = 0x11;
grid[(h_size * 2) + finder_position + 1] = 0x11;
// Bottom square
grid[(h_size * (v_size - 3)) + finder_position - 1] = 0x11;
grid[(h_size * (v_size - 2)) + finder_position - 1] = 0x11;
grid[(h_size * (v_size - 3)) + finder_position + 1] = 0x11;
grid[(h_size * (v_size - 2)) + finder_position + 1] = 0x11;
}
}
}
/* Reserve space for format information */
for (i = 0; i < 5; i++) {
for (j = 0; j < 3; j++) {
grid[(h_size * (i + 1)) + j + 8] = 0x20;
grid[(h_size * (v_size - 6)) + (h_size * i) + j + (h_size - 8)] = 0x20;
}
}
grid[(h_size * 1) + 11] = 0x20;
grid[(h_size * 2) + 11] = 0x20;
grid[(h_size * 3) + 11] = 0x20;
grid[(h_size * (v_size - 6)) + (h_size - 5)] = 0x20;
grid[(h_size * (v_size - 6)) + (h_size - 4)] = 0x20;
grid[(h_size * (v_size - 6)) + (h_size - 3)] = 0x20;
}
/* rMQR according to 2018 draft standard */
int rmqr(struct zint_symbol *symbol, const unsigned char source[], size_t length) {
int i, j, est_binlen;
int ecc_level, autosize, version, max_cw, target_codewords, blocks, h_size, v_size;
int gs1;
int footprint, best_footprint, format_data;
unsigned int left_format_info, right_format_info;
#ifndef _MSC_VER
int utfdata[length + 1];
int jisdata[length + 1];
char mode[length + 1];
#else
int* datastream;
int* fullstream;
unsigned char* grid;
int* utfdata = (int *) _alloca((length + 1) * sizeof (int));
int* jisdata = (int *) _alloca((length + 1) * sizeof (int));
char* mode = (char *) _alloca(length + 1);
#endif
gs1 = (symbol->input_mode == GS1_MODE);
if ((symbol->input_mode == DATA_MODE) || (symbol->eci != 0)) {
for (i = 0; i < length; i++) {
jisdata[i] = (int) source[i];
}
} else {
/* Convert Unicode input to Shift-JIS */
int error_number = utf8toutf16(symbol, source, utfdata, &length);
if (error_number != 0) {
return error_number;
}
for (i = 0; i < length; i++) {
if (utfdata[i] <= 0xff) {
jisdata[i] = utfdata[i];
} else {
int glyph = 0;
j = 0;
do {
if (sjis_lookup[j * 2] == utfdata[i]) {
glyph = sjis_lookup[(j * 2) + 1];
}
j++;
} while ((j < 6843) && (glyph == 0));
if (glyph == 0) {
strcpy(symbol->errtxt, "575: Invalid character in input data");
return ZINT_ERROR_INVALID_DATA;
}
jisdata[i] = glyph;
}
}
}
define_mode(mode, jisdata, length, gs1);
est_binlen = getBinaryLength(RMQR_VERSION + 31, mode, jisdata, length, gs1, symbol->eci);
ecc_level = LEVEL_M;
max_cw = 152;
if (symbol->option_1 == 1) {
strcpy(symbol->errtxt, "576: Error correction level L not available in rMQR");
return ZINT_ERROR_INVALID_OPTION;
}
if (symbol->option_1 == 3) {
strcpy(symbol->errtxt, "577: Error correction level Q not available in rMQR");
return ZINT_ERROR_INVALID_OPTION;
}
if (symbol->option_1 == 4) {
ecc_level = LEVEL_H;
max_cw = 76;
}
if (est_binlen > (8 * max_cw)) {
strcpy(symbol->errtxt, "578: Input too long for selected error correction level");
return ZINT_ERROR_TOO_LONG;
}
if ((symbol->option_2 < 0) || (symbol->option_2 > 38)) {
strcpy(symbol->errtxt, "579: Invalid rMQR symbol size");
return ZINT_ERROR_INVALID_OPTION;
}
version = 31; // Set default to keep compiler happy
if (symbol->option_2 == 0) {
// Automatic symbol size
autosize = 31;
best_footprint = rmqr_height[31] * rmqr_width[31];
for (version = 30; version >= 0; version--) {
est_binlen = getBinaryLength(RMQR_VERSION + version, mode, jisdata, length, gs1, symbol->eci);
footprint = rmqr_height[version] * rmqr_width[version];
if (ecc_level == LEVEL_M) {
if (rmqr_data_codewords_M[version] >= (est_binlen / 8)) {
if (footprint < best_footprint) {
autosize = version;
best_footprint = footprint;
}
}
} else {
if (rmqr_data_codewords_H[version] >= (est_binlen / 8)) {
if (footprint < best_footprint) {
autosize = version;
best_footprint = footprint;
}
}
}
}
version = autosize;
est_binlen = getBinaryLength(RMQR_VERSION + version, mode, jisdata, length, gs1, symbol->eci);
}
if ((symbol->option_2 >= 1) && (symbol->option_2 <= 32)) {
// User specified symbol size
version = symbol->option_2 - 1;
}
if (symbol->option_2 >= 33) {
// User has specified symbol height only
version = rmqr_fixed_height_upper_bound[symbol->option_2 - 32];
for(i = version - 1; i > rmqr_fixed_height_upper_bound[symbol->option_2 - 33]; i--) {
est_binlen = getBinaryLength(RMQR_VERSION + i, mode, jisdata, length, gs1, symbol->eci);
if (ecc_level == LEVEL_M) {
if (rmqr_data_codewords_M[i] >= (est_binlen / 8)) {
version = i;
}
} else {
if (rmqr_data_codewords_H[i] >= (est_binlen / 8)) {
version = i;
}
}
}
est_binlen = getBinaryLength(RMQR_VERSION + version, mode, jisdata, length, gs1, symbol->eci);
}
if (symbol->option_1 == -1) {
// Detect if there is enough free space to increase ECC level
if (est_binlen < (rmqr_data_codewords_H[version] * 8)) {
ecc_level = LEVEL_H;
}
}
if (ecc_level == LEVEL_M) {
target_codewords = rmqr_data_codewords_M[version];
blocks = rmqr_blocks_M[version];
} else {
target_codewords = rmqr_data_codewords_H[version];
blocks = rmqr_blocks_H[version];
}
if (est_binlen > (target_codewords * 8)) {
// User has selected a symbol too small for the data
strcpy(symbol->errtxt, "580: Input too long for selected symbol size");
return ZINT_ERROR_TOO_LONG;
}
if (symbol->debug) {
printf("Minimum codewords = %d\n", est_binlen / 8);
printf("Selected version: %d = R%dx%d-", (version + 1), rmqr_height[version], rmqr_width[version]);
if (ecc_level == LEVEL_M) {
printf("M\n");
} else {
printf("H\n");
}
printf("Number of data codewords in symbol = %d\n", target_codewords);
printf("Number of ECC blocks = %d\n", blocks);
}
#ifndef _MSC_VER
int datastream[target_codewords + 1];
int fullstream[rmqr_total_codewords[version] + 1];
#else
datastream = (int *) _alloca((target_codewords + 1) * sizeof (int));
fullstream = (int *) _alloca((rmqr_total_codewords[version] + 1) * sizeof (int));
#endif
qr_binary(datastream, RMQR_VERSION + version, target_codewords, mode, jisdata, length, gs1, symbol->eci, est_binlen, symbol->debug);
add_ecc(fullstream, datastream, RMQR_VERSION + version, target_codewords, blocks, symbol->debug);
h_size = rmqr_width[version];
v_size = rmqr_height[version];
#ifndef _MSC_VER
unsigned char grid[h_size * v_size];
#else
grid = (unsigned char *) _alloca((h_size * v_size) * sizeof (unsigned char));
#endif
for (i = 0; i < v_size; i++) {
for (j = 0; j < h_size; j++) {
grid[(i * h_size) + j] = 0;
}
}
setup_rmqr_grid(grid, h_size, v_size, version);
populate_grid(grid, h_size, v_size, fullstream, rmqr_total_codewords[version]);
/* apply bitmask */
for (i = 0; i < v_size; i++) {
for (j = 0; j < h_size; j++) {
if ((grid[(i * h_size) + j] & 0xf0) == 0) {
// This is a data module
if (((i / 2) + (j / 3)) % 2 == 0) { // < This is the data mask from section 7.8.2
// This module needs to be changed
if (grid[(i * h_size) + j] == 0x01) {
grid[(i * h_size) + j] = 0x00;
} else {
grid[(i * h_size) + j] = 0x01;
}
}
}
}
}
/* add format information */
format_data = version;
if (ecc_level == LEVEL_H) {
format_data += 32;
}
left_format_info = rmqr_format_info_left[format_data];
right_format_info = rmqr_format_info_right[format_data];
for (i = 0; i < 5; i++) {
for (j = 0; j < 3; j++) {
grid[(h_size * (i + 1)) + j + 8] = (left_format_info >> ((j * 5) + i)) & 0x01;
grid[(h_size * (v_size - 6)) + (h_size * i) + j + (h_size - 8)] = (right_format_info >> ((j * 5) + i)) & 0x01;
}
}
grid[(h_size * 1) + 11] = (left_format_info >> 15) & 0x01;
grid[(h_size * 2) + 11] = (left_format_info >> 16) & 0x01;
grid[(h_size * 3) + 11] = (left_format_info >> 17) & 0x01;
grid[(h_size * (v_size - 6)) + (h_size - 5)] = (right_format_info >> 15) & 0x01;
grid[(h_size * (v_size - 6)) + (h_size - 4)] = (right_format_info >> 16) & 0x01;
grid[(h_size * (v_size - 6)) + (h_size - 3)] = (right_format_info >> 17) & 0x01;
symbol->width = h_size;
symbol->rows = v_size;
for (i = 0; i < v_size; i++) {
for (j = 0; j < h_size; j++) {
if (grid[(i * h_size) + j] & 0x01) {
set_module(symbol, i, j);
}
}
symbol->row_height[i] = 1;
}
return 0;
}