From b426877a4f3429cefdd3fe0f4824ecd3d0a8f548 Mon Sep 17 00:00:00 2001 From: Robin Stuart Date: Sat, 20 May 2017 23:37:50 +0100 Subject: [PATCH] Add new symbology UPNQR --- backend/library.c | 9 ++++- backend/qr.c | 97 +++++++++++++++++++++++++++++++++++++++++++++++ backend/zint.h | 1 + frontend/main.c | 2 +- 4 files changed, 107 insertions(+), 2 deletions(-) diff --git a/backend/library.c b/backend/library.c index 696f9677..3e6b7b19 100644 --- a/backend/library.c +++ b/backend/library.c @@ -205,6 +205,7 @@ extern int grid_matrix(struct zint_symbol *symbol, const unsigned char source[], extern int han_xin(struct zint_symbol * symbol, const unsigned char source[], int length); /* Han Xin */ extern int dotcode(struct zint_symbol * symbol, const unsigned char source[], int length); /* DotCode */ extern int codablock(struct zint_symbol * symbol, unsigned char source[], int length); /* Codablock */ +extern int upnqr(struct zint_symbol *symbol, const unsigned char source[], int length); /* UPNQR */ extern int plot_raster(struct zint_symbol *symbol, int rotate_angle, int file_type); /* Plot to PNG/BMP/PCX */ extern int render_plot(struct zint_symbol *symbol, float width, float height); /* Plot to gLabels */ @@ -459,6 +460,7 @@ static int is_matrix(const int symbology) { case BARCODE_GRIDMATRIX: case BARCODE_HANXIN: case BARCODE_DOTCODE: + case BARCODE_UPNQR: result = 1; break; } @@ -482,6 +484,7 @@ static int supports_eci(const int symbology) { case BARCODE_DOTCODE: case BARCODE_GRIDMATRIX: case BARCODE_HANXIN: + case BARCODE_UPNQR: result = 1; break; } @@ -584,6 +587,7 @@ int ZBarcode_ValidID(int symbol_id) { case BARCODE_HANXIN: case BARCODE_DOTCODE: case BARCODE_CODABLOCKF: + case BARCODE_UPNQR: result = 1; break; } @@ -604,6 +608,8 @@ static int extended_charset(struct zint_symbol *symbol, const unsigned char *sou break; case BARCODE_HANXIN: error_number = han_xin(symbol, source, length); break; + case BARCODE_UPNQR: error_number = upnqr(symbol, source, length); + break; } return error_number; @@ -958,7 +964,7 @@ int ZBarcode_Encode(struct zint_symbol *symbol, unsigned char *source, int lengt error_number = ZINT_WARN_INVALID_OPTION; } /* Everything from 128 up is Zint-specific */ - if (symbol->symbology >= 143) { + if (symbol->symbology >= 144) { strcpy(symbol->errtxt, "Symbology out of range, using Code 128 (B16)"); symbol->symbology = BARCODE_CODE128; error_number = ZINT_WARN_INVALID_OPTION; @@ -1017,6 +1023,7 @@ int ZBarcode_Encode(struct zint_symbol *symbol, unsigned char *source, int lengt case BARCODE_MICROQR: case BARCODE_GRIDMATRIX: case BARCODE_HANXIN: + case BARCODE_UPNQR: error_number = extended_charset(symbol, local_source, length); break; default: diff --git a/backend/qr.c b/backend/qr.c index 117a396a..25e8cf8f 100644 --- a/backend/qr.c +++ b/backend/qr.c @@ -2947,3 +2947,100 @@ int microqr(struct zint_symbol *symbol, const unsigned char source[], int length return 0; } + +/* For UPNQR the symbol size and error correction capacity is fixed */ +int upnqr(struct zint_symbol *symbol, const unsigned char source[], int length) { + int i, j, est_binlen; + int ecc_level, version, max_cw, target_binlen, blocks, size; + int bitmask; + +#ifndef _MSC_VER + int jisdata[length + 1]; + char mode[length + 1]; +#else + int* datastream; + int* fullstream; + unsigned char* grid; + int* jisdata = (int *) _alloca((length + 1) * sizeof (int)); + char* mode = (char *) _alloca(length + 1); +#endif + + switch(symbol->eci) { + case 3: + /* Convert input to ISO-8859-2 */ + strcpy(symbol->errtxt, "Not implemented yet!"); + return ZINT_ERROR_INVALID_DATA; + break; + case 4: + /* Input is already in ISO-8859-2 format */ + for (i = 0; i < length; i++) { + jisdata[i] = (int) source[i]; + mode[i] = 'B'; + } + break; + default: + strcpy(symbol->errtxt, "Input data should be in Unicode or ISO-8859-2 format"); + return ZINT_ERROR_INVALID_DATA; + break; + } + + symbol->eci = 4; + est_binlen = estimate_binary_length(mode, length, 0, symbol->eci); + ecc_level = LEVEL_M; + max_cw = 2334; + + if (est_binlen > (8 * max_cw)) { + strcpy(symbol->errtxt, "Input too long for selected error correction level (E61)"); + return ZINT_ERROR_TOO_LONG; + } + + version = 15; // 77 x 77 + + target_binlen = qr_data_codewords_M[version - 1]; + blocks = qr_blocks_M[version - 1]; + +#ifndef _MSC_VER + int datastream[target_binlen + 1]; + int fullstream[qr_total_codewords[version - 1] + 1]; +#else + datastream = (int *) _alloca((target_binlen + 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); + + size = qr_sizes[version - 1]; +#ifndef _MSC_VER + unsigned char grid[size * size]; +#else + grid = (unsigned char *) _alloca((size * size) * sizeof (unsigned char)); +#endif + + for (i = 0; i < size; i++) { + for (j = 0; j < size; j++) { + grid[(i * size) + j] = 0; + } + } + + setup_grid(grid, size, version); + populate_grid(grid, size, fullstream, qr_total_codewords[version - 1]); + + bitmask = apply_bitmask(grid, size, ecc_level); + + add_format_info(grid, size, ecc_level, bitmask); + + symbol->width = size; + symbol->rows = size; + + for (i = 0; i < size; i++) { + for (j = 0; j < size; j++) { + if (grid[(i * size) + j] & 0x01) { + set_module(symbol, i, j); + } + } + symbol->row_height[i] = 1; + } + + return 0; +} \ No newline at end of file diff --git a/backend/zint.h b/backend/zint.h index 4dd98e06..73a632b4 100644 --- a/backend/zint.h +++ b/backend/zint.h @@ -201,6 +201,7 @@ extern "C" { #define BARCODE_CHANNEL 140 #define BARCODE_CODEONE 141 #define BARCODE_GRIDMATRIX 142 +#define BARCODE_UPNQR 143 // Output options #define BARCODE_NO_ASCII 1 diff --git a/frontend/main.c b/frontend/main.c index 9d1c94c2..1e3acbdc 100644 --- a/frontend/main.c +++ b/frontend/main.c @@ -66,7 +66,7 @@ void types(void) { "38: UPC-E + Check 85: USPS OneCode 140: Channel Code\n" "40: Postnet 86: UK Plessey 141: Code One\n" "47: MSI Plessey 87: Telepen Numeric 142: Grid Matrix\n" - "49: FIM 89: ITF-14\n" + "49: FIM 89: ITF-14 143: UPNQR\n" ); }