From 2e59058615252519cf3b6fedade55582e939ef27 Mon Sep 17 00:00:00 2001 From: Robin Stuart Date: Wed, 17 Feb 2016 10:37:20 +0000 Subject: [PATCH] Avoid possible confusion with Windows error code Bugfix by Oxy Genic Ref: https://sourceforge.net/p/zint/mailman/message/34857131/ --- backend/2of5.c | 32 +- backend/auspost.c | 10 +- backend/aztec.c | 22 +- backend/code.c | 32 +- backend/code1.c | 14 +- backend/code128.c | 18 +- backend/code16k.c | 6 +- backend/code49.c | 6 +- backend/common.c | 10 +- backend/composite.c | 52 +- backend/dmatrix.c | 8 +- backend/gridmtx.c | 8 +- backend/gs1.c | 24 +- backend/imail.c | 8 +- backend/library.c | 54 +- backend/maxicode.c | 16 +- backend/medical.c | 24 +- backend/pdf417.c | 24 +- backend/plessey.c | 16 +- backend/png.c | 2322 +++++++-------- backend/postal.c | 34 +- backend/ps.c | 14 +- backend/qr.c | 4888 ++++++++++++++++--------------- backend/rss.c | 16 +- backend/svg.c | 14 +- backend/telepen.c | 10 +- backend/upcean.c | 32 +- backend/zint.h | 16 +- backend_qt4/qzint.cpp | 6 +- frontend/main.c | 16 +- frontend_qt4/sequencewindow.cpp | 4 +- 31 files changed, 3878 insertions(+), 3878 deletions(-) diff --git a/backend/2of5.c b/backend/2of5.c index f71a9f43..9e550cc1 100644 --- a/backend/2of5.c +++ b/backend/2of5.c @@ -63,10 +63,10 @@ int matrix_two_of_five(struct zint_symbol *symbol, unsigned char source[], int l if(length > 80) { strcpy(symbol->errtxt, "Input too long"); - return ERROR_TOO_LONG; + return ZINT_ERROR_TOO_LONG; } error_number = is_sane(NEON, source, length); - if(error_number == ERROR_INVALID_DATA) { + if(error_number == ZINT_ERROR_INVALID_DATA) { strcpy(symbol->errtxt, "Invalid characters in data"); return error_number; } @@ -96,10 +96,10 @@ int industrial_two_of_five(struct zint_symbol *symbol, unsigned char source[], i if(length > 45) { strcpy(symbol->errtxt, "Input too long"); - return ERROR_TOO_LONG; + return ZINT_ERROR_TOO_LONG; } error_number = is_sane(NEON, source, length); - if(error_number == ERROR_INVALID_DATA) { + if(error_number == ZINT_ERROR_INVALID_DATA) { strcpy(symbol->errtxt, "Invalid character in data"); return error_number; } @@ -128,10 +128,10 @@ int iata_two_of_five(struct zint_symbol *symbol, unsigned char source[], int len if(length > 45) { strcpy(symbol->errtxt, "Input too long"); - return ERROR_TOO_LONG; + return ZINT_ERROR_TOO_LONG; } error_number = is_sane(NEON, source, length); - if(error_number == ERROR_INVALID_DATA) { + if(error_number == ZINT_ERROR_INVALID_DATA) { strcpy(symbol->errtxt, "Invalid characters in data"); return error_number; } @@ -161,10 +161,10 @@ int logic_two_of_five(struct zint_symbol *symbol, unsigned char source[], int le if(length > 80) { strcpy(symbol->errtxt, "Input too long"); - return ERROR_TOO_LONG; + return ZINT_ERROR_TOO_LONG; } error_number = is_sane(NEON, source, length); - if(error_number == ERROR_INVALID_DATA) { + if(error_number == ZINT_ERROR_INVALID_DATA) { strcpy(symbol->errtxt, "Invalid characters in data"); return error_number; } @@ -199,10 +199,10 @@ int interleaved_two_of_five(struct zint_symbol *symbol, unsigned char source[], if(length > 89) { strcpy(symbol->errtxt, "Input too long"); - return ERROR_TOO_LONG; + return ZINT_ERROR_TOO_LONG; } error_number = is_sane(NEON, source, length); - if (error_number == ERROR_INVALID_DATA) { + if (error_number == ZINT_ERROR_INVALID_DATA) { strcpy(symbol->errtxt, "Invalid characters in data"); return error_number; } @@ -260,11 +260,11 @@ int itf14(struct zint_symbol *symbol, unsigned char source[], int length) if(length > 13) { strcpy(symbol->errtxt, "Input too long"); - return ERROR_TOO_LONG; + return ZINT_ERROR_TOO_LONG; } error_number = is_sane(NEON, source, length); - if(error_number == ERROR_INVALID_DATA) { + if(error_number == ZINT_ERROR_INVALID_DATA) { strcpy(symbol->errtxt, "Invalid character in data"); return error_number; } @@ -303,10 +303,10 @@ int dpleit(struct zint_symbol *symbol, unsigned char source[], int length) count = 0; if(length > 13) { strcpy(symbol->errtxt, "Input wrong length"); - return ERROR_TOO_LONG; + return ZINT_ERROR_TOO_LONG; } error_number = is_sane(NEON, source, length); - if(error_number == ERROR_INVALID_DATA) { + if(error_number == ZINT_ERROR_INVALID_DATA) { strcpy(symbol->errtxt, "Invalid characters in data"); return error_number; } @@ -340,10 +340,10 @@ int dpident(struct zint_symbol *symbol, unsigned char source[], int length) count = 0; if(length > 11) { strcpy(symbol->errtxt, "Input wrong length"); - return ERROR_TOO_LONG; + return ZINT_ERROR_TOO_LONG; } error_number = is_sane(NEON, source, length); - if(error_number == ERROR_INVALID_DATA) { + if(error_number == ZINT_ERROR_INVALID_DATA) { strcpy(symbol->errtxt, "Invalid characters in data"); return error_number; } diff --git a/backend/auspost.c b/backend/auspost.c index 18399b85..ce697615 100644 --- a/backend/auspost.c +++ b/backend/auspost.c @@ -140,16 +140,16 @@ int australia_post(struct zint_symbol *symbol, unsigned char source[], int lengt break; default: strcpy(symbol->errtxt, "Auspost input is wrong length"); - return ERROR_TOO_LONG; + return ZINT_ERROR_TOO_LONG; } - if(error_number == ERROR_INVALID_DATA) { + if(error_number == ZINT_ERROR_INVALID_DATA) { strcpy(symbol->errtxt, "Invalid characters in data"); return error_number; } } else { if (length > 8) { strcpy(symbol->errtxt, "Auspost input is too long"); - return ERROR_TOO_LONG; + return ZINT_ERROR_TOO_LONG; } switch(symbol->symbology) { case BARCODE_AUSREPLY: strcpy(fcc, "45"); break; @@ -166,7 +166,7 @@ int australia_post(struct zint_symbol *symbol, unsigned char source[], int lengt concat(localstr, (char*)source); h = strlen(localstr); error_number = is_sane(GDSET, (unsigned char *)localstr, h); - if(error_number == ERROR_INVALID_DATA) { + if(error_number == ZINT_ERROR_INVALID_DATA) { strcpy(symbol->errtxt, "Invalid characters in data"); return error_number; } @@ -175,7 +175,7 @@ int australia_post(struct zint_symbol *symbol, unsigned char source[], int lengt memcpy(dpid, localstr, 8); dpid[8] = '\0'; error_number = is_sane(NEON, (unsigned char *)dpid, strlen(dpid)); - if(error_number == ERROR_INVALID_DATA) { + if(error_number == ZINT_ERROR_INVALID_DATA) { strcpy(symbol->errtxt, "Invalid characters in DPID"); return error_number; } diff --git a/backend/aztec.c b/backend/aztec.c index 883a40e5..5895f00b 100644 --- a/backend/aztec.c +++ b/backend/aztec.c @@ -588,7 +588,7 @@ int aztec_text_process(unsigned char source[], const unsigned int src_len, char bytes--; if(bytes > 2079) { - return ERROR_TOO_LONG; + return ZINT_ERROR_TOO_LONG; } if(bytes > 31) { /* Put 00000 followed by 11-bit number of bytes less 31 */ @@ -660,7 +660,7 @@ int aztec_text_process(unsigned char source[], const unsigned int src_len, char if(debug) printf("\n"); if(strlen(binary_string) > 14970) { - return ERROR_TOO_LONG; + return ZINT_ERROR_TOO_LONG; } return 0; @@ -847,7 +847,7 @@ int aztec(struct zint_symbol *symbol, unsigned char source[], int length) if(symbol->output_options & READER_INIT) { reader = 1; comp_loop = 1; } if((gs1 == 1) && (reader == 1)) { strcpy(symbol->errtxt, "Cannot encode in GS1 and Reader Initialisation mode at the same time"); - return ERROR_INVALID_OPTION; + return ZINT_ERROR_INVALID_OPTION; } switch(symbol->input_mode) { @@ -872,7 +872,7 @@ int aztec(struct zint_symbol *symbol, unsigned char source[], int length) if(!((symbol->option_1 >= -1) && (symbol->option_1 <= 4))) { strcpy(symbol->errtxt, "Invalid error correction level - using default instead"); - err_code = WARN_INVALID_OPTION; + err_code = ZINT_WARN_INVALID_OPTION; symbol->option_1 = -1; } @@ -960,7 +960,7 @@ int aztec(struct zint_symbol *symbol, unsigned char source[], int length) if(layers == 0) { /* Couldn't find a symbol which fits the data */ strcpy(symbol->errtxt, "Input too long (too many bits for selected ECC)"); - return ERROR_TOO_LONG; + return ZINT_ERROR_TOO_LONG; } /* Determine codeword bitlength - Table 3 */ @@ -1053,7 +1053,7 @@ int aztec(struct zint_symbol *symbol, unsigned char source[], int length) } if((symbol->option_2 < 0) || (symbol->option_2 > 36)) { strcpy(symbol->errtxt, "Invalid Aztec Code size"); - return ERROR_INVALID_OPTION; + return ZINT_ERROR_INVALID_OPTION; } /* Determine codeword bitlength - Table 3 */ @@ -1124,7 +1124,7 @@ int aztec(struct zint_symbol *symbol, unsigned char source[], int length) if(adjusted_length > data_maxsize) { strcpy(symbol->errtxt, "Data too long for specified Aztec Code symbol size"); - return ERROR_TOO_LONG; + return ZINT_ERROR_TOO_LONG; } if(debug) { @@ -1141,7 +1141,7 @@ int aztec(struct zint_symbol *symbol, unsigned char source[], int length) if(reader && (layers > 22)) { strcpy(symbol->errtxt, "Data too long for reader initialisation symbol"); - return ERROR_TOO_LONG; + return ZINT_ERROR_TOO_LONG; } data_blocks = adjusted_length / codeword_size; @@ -1437,12 +1437,12 @@ int aztec_runes(struct zint_symbol *symbol, unsigned char source[], int length) input_value = 0; if(length > 3) { strcpy(symbol->errtxt, "Input too large"); - return ERROR_INVALID_DATA; + return ZINT_ERROR_INVALID_DATA; } error_number = is_sane(NEON, source, length); if(error_number != 0) { strcpy(symbol->errtxt, "Invalid characters in input"); - return ERROR_INVALID_DATA; + return ZINT_ERROR_INVALID_DATA; } switch(length) { case 3: input_value = 100 * ctoi(source[0]); @@ -1458,7 +1458,7 @@ int aztec_runes(struct zint_symbol *symbol, unsigned char source[], int length) if(input_value > 255) { strcpy(symbol->errtxt, "Input too large"); - return ERROR_INVALID_DATA; + return ZINT_ERROR_INVALID_DATA; } strcpy(binary_string, ""); diff --git a/backend/code.c b/backend/code.c index bc039e2f..1a649f9b 100644 --- a/backend/code.c +++ b/backend/code.c @@ -109,10 +109,10 @@ int code_11(struct zint_symbol *symbol, unsigned char source[], int length) if(length > 121) { strcpy(symbol->errtxt, "Input too long"); - return ERROR_TOO_LONG; + return ZINT_ERROR_TOO_LONG; } error_number = is_sane(SODIUM, source, length); - if(error_number == ERROR_INVALID_DATA) { + if(error_number == ZINT_ERROR_INVALID_DATA) { strcpy(symbol->errtxt, "Invalid characters in data"); return error_number; } @@ -193,14 +193,14 @@ int c39(struct zint_symbol *symbol, unsigned char source[], int length) if((symbol->symbology == BARCODE_LOGMARS) && (length > 59)) { strcpy(symbol->errtxt, "Input too long"); - return ERROR_TOO_LONG; + return ZINT_ERROR_TOO_LONG; } else if(length > 74) { strcpy(symbol->errtxt, "Input too long"); - return ERROR_TOO_LONG; + return ZINT_ERROR_TOO_LONG; } to_upper(source); error_number = is_sane(SILVER , source, length); - if(error_number == ERROR_INVALID_DATA) { + if(error_number == ZINT_ERROR_INVALID_DATA) { strcpy(symbol->errtxt, "Invalid characters in data"); return error_number; } @@ -284,10 +284,10 @@ int pharmazentral(struct zint_symbol *symbol, unsigned char source[], int length count = 0; if(length > 6) { strcpy(symbol->errtxt, "Input wrong length"); - return ERROR_TOO_LONG; + return ZINT_ERROR_TOO_LONG; } error_number = is_sane(NEON, source, length); - if(error_number == ERROR_INVALID_DATA) { + if(error_number == ZINT_ERROR_INVALID_DATA) { strcpy(symbol->errtxt, "Invalid characters in data"); return error_number; } @@ -308,7 +308,7 @@ int pharmazentral(struct zint_symbol *symbol, unsigned char source[], int length localstr[8] = '\0'; if(localstr[7] == 'A') { strcpy(symbol->errtxt, "Invalid PZN Data"); - return ERROR_INVALID_DATA; + return ZINT_ERROR_INVALID_DATA; } error_number = c39(symbol, (unsigned char *)localstr, strlen(localstr)); ustrcpy(symbol->text, (unsigned char *)"PZN"); @@ -330,7 +330,7 @@ int ec39(struct zint_symbol *symbol, unsigned char source[], int length) if(length > 74) { strcpy(symbol->errtxt, "Input too long"); - return ERROR_TOO_LONG; + return ZINT_ERROR_TOO_LONG; } /* Creates a buffer string and places control characters into it */ @@ -338,7 +338,7 @@ int ec39(struct zint_symbol *symbol, unsigned char source[], int length) if(source[i] > 127) { /* Cannot encode extended ASCII */ strcpy(symbol->errtxt, "Invalid characters in input data"); - return ERROR_INVALID_DATA; + return ZINT_ERROR_INVALID_DATA; } concat((char*)buffer, EC39Ctrl[source[i]]); } @@ -373,7 +373,7 @@ int c93(struct zint_symbol *symbol, unsigned char source[], int length) if(length > 107) { strcpy(symbol->errtxt, "Input too long"); - return ERROR_TOO_LONG; + return ZINT_ERROR_TOO_LONG; } /* Message Content */ @@ -381,7 +381,7 @@ int c93(struct zint_symbol *symbol, unsigned char source[], int length) if (source[i] > 127) { /* Cannot encode extended ASCII */ strcpy(symbol->errtxt, "Invalid characters in input data"); - return ERROR_INVALID_DATA; + return ZINT_ERROR_INVALID_DATA; } concat(buffer, C93Ctrl[source[i]]); symbol->text[i] = source[i] ? source[i] : ' '; @@ -391,7 +391,7 @@ int c93(struct zint_symbol *symbol, unsigned char source[], int length) h = strlen(buffer); if (h > 107) { strcpy(symbol->errtxt, "Input too long"); - return ERROR_TOO_LONG; + return ZINT_ERROR_TOO_LONG; } for (i = 0; i < h; i++) { @@ -506,10 +506,10 @@ int channel_code(struct zint_symbol *symbol, unsigned char source[], int length) if(length > 7) { strcpy(symbol->errtxt, "Input too long"); - return ERROR_TOO_LONG; + return ZINT_ERROR_TOO_LONG; } error_number = is_sane(NEON, source, length); - if(error_number == ERROR_INVALID_DATA) { + if(error_number == ZINT_ERROR_INVALID_DATA) { strcpy(symbol->errtxt, "Invalid characters in data"); return error_number; } @@ -533,7 +533,7 @@ int channel_code(struct zint_symbol *symbol, unsigned char source[], int length) } if(range) { strcpy(symbol->errtxt, "Value out of range"); - return ERROR_INVALID_DATA; + return ZINT_ERROR_INVALID_DATA; } for(i = 0; i < 11; i++) { B[i] = 0; S[i] = 0; } diff --git a/backend/code1.c b/backend/code1.c index 9409e619..64e87ba2 100644 --- a/backend/code1.c +++ b/backend/code1.c @@ -1031,7 +1031,7 @@ int code_one(struct zint_symbol *symbol, unsigned char source[], int length) if((symbol->option_2 < 0) || (symbol->option_2 > 10)) { strcpy(symbol->errtxt, "Invalid symbol size"); - return ERROR_INVALID_OPTION; + return ZINT_ERROR_INVALID_OPTION; } if(symbol->option_2 == 9) { @@ -1044,11 +1044,11 @@ int code_one(struct zint_symbol *symbol, unsigned char source[], int length) if(length > 18) { strcpy(symbol->errtxt, "Input data too long"); - return ERROR_TOO_LONG; + return ZINT_ERROR_TOO_LONG; } - if(is_sane(NEON, source, length) == ERROR_INVALID_DATA) { + if(is_sane(NEON, source, length) == ZINT_ERROR_INVALID_DATA) { strcpy(symbol->errtxt, "Invalid input data (Version S encodes numeric input only)"); - return ERROR_INVALID_DATA; + return ZINT_ERROR_INVALID_DATA; } sub_version = 3; codewords = 12; block_width = 6; /* Version S-30 */ @@ -1120,12 +1120,12 @@ int code_one(struct zint_symbol *symbol, unsigned char source[], int length) data_length = c1_encode(symbol, source, data, length); if(data_length == 0) { - return ERROR_TOO_LONG; + return ZINT_ERROR_TOO_LONG; } if(data_length > 38) { strcpy(symbol->errtxt, "Input data too long"); - return ERROR_TOO_LONG; + return ZINT_ERROR_TOO_LONG; } size = 10; @@ -1187,7 +1187,7 @@ int code_one(struct zint_symbol *symbol, unsigned char source[], int length) data_length = c1_encode(symbol, source, data, length); if(data_length == 0) { - return ERROR_TOO_LONG; + return ZINT_ERROR_TOO_LONG; } for(i = 7; i >= 0; i--) { diff --git a/backend/code128.c b/backend/code128.c index 3bfd9f96..2fd0be34 100644 --- a/backend/code128.c +++ b/backend/code128.c @@ -236,7 +236,7 @@ int code_128(struct zint_symbol *symbol, unsigned char source[], int length) /* This only blocks rediculously long input - the actual length of the resulting barcode depends on the type of data, so this is trapped later */ strcpy(symbol->errtxt, "Input too long"); - return ERROR_TOO_LONG; + return ZINT_ERROR_TOO_LONG; } /* Detect extended ASCII characters */ @@ -403,7 +403,7 @@ int code_128(struct zint_symbol *symbol, unsigned char source[], int length) } if(glyph_count > 80.0) { strcpy(symbol->errtxt, "Input too long"); - return ERROR_TOO_LONG; + return ZINT_ERROR_TOO_LONG; } /* So now we know what start character to use - we can get on with it! */ @@ -635,13 +635,13 @@ int ean_128(struct zint_symbol *symbol, unsigned char source[], int length) /* This only blocks rediculously long input - the actual length of the resulting barcode depends on the type of data, so this is trapped later */ strcpy(symbol->errtxt, "Input too long"); - return ERROR_TOO_LONG; + return ZINT_ERROR_TOO_LONG; } for(i = 0; i < length; i++) { if(source[i] == '\0') { /* Null characters not allowed! */ strcpy(symbol->errtxt, "NULL character in input data"); - return ERROR_INVALID_DATA; + return ZINT_ERROR_INVALID_DATA; } } @@ -766,7 +766,7 @@ int ean_128(struct zint_symbol *symbol, unsigned char source[], int length) } if(glyph_count > 80.0) { strcpy(symbol->errtxt, "Input too long"); - return ERROR_TOO_LONG; + return ZINT_ERROR_TOO_LONG; } /* So now we know what start character to use - we can get on with it! */ @@ -939,11 +939,11 @@ int nve_18(struct zint_symbol *symbol, unsigned char source[], int length) if(sourcelen > 17) { strcpy(symbol->errtxt, "Input too long"); - return ERROR_TOO_LONG; + return ZINT_ERROR_TOO_LONG; } error_number = is_sane(NEON, source, length); - if(error_number == ERROR_INVALID_DATA) { + if(error_number == ZINT_ERROR_INVALID_DATA) { strcpy(symbol->errtxt, "Invalid characters in data"); return error_number; } @@ -980,11 +980,11 @@ int ean_14(struct zint_symbol *symbol, unsigned char source[], int length) if(length > 13) { strcpy(symbol->errtxt, "Input wrong length"); - return ERROR_TOO_LONG; + return ZINT_ERROR_TOO_LONG; } error_number = is_sane(NEON, source, length); - if(error_number == ERROR_INVALID_DATA) { + if(error_number == ZINT_ERROR_INVALID_DATA) { strcpy(symbol->errtxt, "Invalid character in data"); return error_number; } diff --git a/backend/code16k.c b/backend/code16k.c index 1d0b281c..89805575 100644 --- a/backend/code16k.c +++ b/backend/code16k.c @@ -200,7 +200,7 @@ int code16k(struct zint_symbol *symbol, unsigned char source[], int length) if(input_length > 157) { strcpy(symbol->errtxt, "Input too long"); - return ERROR_TOO_LONG; + return ZINT_ERROR_TOO_LONG; } bar_characters = 0; @@ -390,7 +390,7 @@ int code16k(struct zint_symbol *symbol, unsigned char source[], int length) if(glyph_count > 77.0) { strcpy(symbol->errtxt, "Input too long"); - return ERROR_TOO_LONG; + return ZINT_ERROR_TOO_LONG; } /* Calculate how tall the symbol will be */ @@ -415,7 +415,7 @@ int code16k(struct zint_symbol *symbol, unsigned char source[], int length) if(m == 2) { m = 5; } if(gs1) { strcpy(symbol->errtxt, "Cannot use both GS1 mode and Reader Initialisation"); - return ERROR_INVALID_OPTION; + return ZINT_ERROR_INVALID_OPTION; } else { if((set[0] == 'B') && (set[1] == 'C')) { m = 6; } } diff --git a/backend/code49.c b/backend/code49.c index 10b559f2..2c8f5068 100644 --- a/backend/code49.c +++ b/backend/code49.c @@ -52,7 +52,7 @@ int code_49(struct zint_symbol *symbol, unsigned char source[], int length) if(length > 81) { strcpy(symbol->errtxt, "Input too long"); - return ERROR_TOO_LONG; + return ZINT_ERROR_TOO_LONG; } if(symbol->input_mode == GS1_MODE) { gs1 = 1; } else { gs1 = 0; } @@ -60,7 +60,7 @@ int code_49(struct zint_symbol *symbol, unsigned char source[], int length) for(i = 0; i < length; i++) { if(source[i] > 127) { strcpy(symbol->errtxt, "Invalid characters in input data"); - return ERROR_INVALID_DATA; + return ZINT_ERROR_INVALID_DATA; } if(gs1 && (source[i] == '[')) concat(intermediate, "*"); /* FNC1 */ @@ -206,7 +206,7 @@ int code_49(struct zint_symbol *symbol, unsigned char source[], int length) if(codeword_count > 49) { strcpy(symbol->errtxt, "Input too long"); - return ERROR_TOO_LONG; + return ZINT_ERROR_TOO_LONG; } /* Place codewords in code character array (c grid) */ diff --git a/backend/common.c b/backend/common.c index 00ce3928..ba977675 100644 --- a/backend/common.c +++ b/backend/common.c @@ -112,7 +112,7 @@ int is_sane(char test_string[], unsigned char source[], int length) } } if (!(latch)) { - return ERROR_INVALID_DATA; + return ZINT_ERROR_INVALID_DATA; } } @@ -321,7 +321,7 @@ int latin1_process(struct zint_symbol *symbol, unsigned char source[], unsigned } if(next == -1) { strcpy(symbol->errtxt, "error: Invalid character in input string (only Latin-1 characters supported)"); - return ERROR_INVALID_DATA; + return ZINT_ERROR_INVALID_DATA; } i = next; } while(i < *length); @@ -350,11 +350,11 @@ int utf8toutf16(struct zint_symbol *symbol, unsigned char source[], int vals[], } else { if((source[bpos] >= 0x80) && (source[bpos] <= 0xbf)) { strcpy(symbol->errtxt, "Corrupt Unicode data"); - return ERROR_INVALID_DATA; + return ZINT_ERROR_INVALID_DATA; } if((source[bpos] >= 0xc0) && (source[bpos] <= 0xc1)) { strcpy(symbol->errtxt, "Overlong encoding not supported"); - return ERROR_INVALID_DATA; + return ZINT_ERROR_INVALID_DATA; } if((source[bpos] >= 0xc2) && (source[bpos] <= 0xdf)) { @@ -371,7 +371,7 @@ int utf8toutf16(struct zint_symbol *symbol, unsigned char source[], int vals[], } else if(source[bpos] >= 0xf0) { strcpy(symbol->errtxt, "Unicode sequences of more than 3 bytes not supported"); - return ERROR_INVALID_DATA; + return ZINT_ERROR_INVALID_DATA; } } diff --git a/backend/composite.c b/backend/composite.c index 62fb7b95..e0cc2b88 100644 --- a/backend/composite.c +++ b/backend/composite.c @@ -863,7 +863,7 @@ int cc_binary_string(struct zint_symbol *symbol, const char source[], char binar if((ninety[i] != '*') && (ninety[i] != ',') && (ninety[i] != '-') && (ninety[i] != '.') && (ninety[i] != '/')) { /* An Invalid AI 90 character */ strcpy(symbol->errtxt, "Invalid AI 90 data"); - return ERROR_INVALID_DATA; + return ZINT_ERROR_INVALID_DATA; } } } @@ -1186,7 +1186,7 @@ int cc_binary_string(struct zint_symbol *symbol, const char source[], char binar if(latch == 1) { /* Invalid characters in input data */ strcpy(symbol->errtxt, "Invalid characters in input data"); - return ERROR_INVALID_DATA; + return ZINT_ERROR_INVALID_DATA; } for(i = 0; i < strlen(general_field); i++) { @@ -1382,7 +1382,7 @@ int cc_binary_string(struct zint_symbol *symbol, const char source[], char binar /* CC-A 2D component - calculate remaining space */ switch(*(cc_width)) { case 2: - if(binary_length > 167) { return ERROR_TOO_LONG; } + if(binary_length > 167) { return ZINT_ERROR_TOO_LONG; } if(binary_length <= 167) { target_bitsize = 167; } if(binary_length <= 138) { target_bitsize = 138; } if(binary_length <= 118) { target_bitsize = 118; } @@ -1392,7 +1392,7 @@ int cc_binary_string(struct zint_symbol *symbol, const char source[], char binar if(binary_length <= 59) { target_bitsize = 59; } break; case 3: - if(binary_length > 167) { return ERROR_TOO_LONG; } + if(binary_length > 167) { return ZINT_ERROR_TOO_LONG; } if(binary_length <= 167) { target_bitsize = 167; } if(binary_length <= 138) { target_bitsize = 138; } if(binary_length <= 118) { target_bitsize = 118; } @@ -1400,7 +1400,7 @@ int cc_binary_string(struct zint_symbol *symbol, const char source[], char binar if(binary_length <= 78) { target_bitsize = 78; } break; case 4: - if(binary_length > 197) { return ERROR_TOO_LONG; } + if(binary_length > 197) { return ZINT_ERROR_TOO_LONG; } if(binary_length <= 197) { target_bitsize = 197; } if(binary_length <= 167) { target_bitsize = 167; } if(binary_length <= 138) { target_bitsize = 138; } @@ -1414,7 +1414,7 @@ int cc_binary_string(struct zint_symbol *symbol, const char source[], char binar /* CC-B 2D component - calculated from ISO/IEC 24728 Table 1 */ switch(*(cc_width)) { case 2: - if(binary_length > 336) { return ERROR_TOO_LONG; } + if(binary_length > 336) { return ZINT_ERROR_TOO_LONG; } if(binary_length <= 336) { target_bitsize = 336; } if(binary_length <= 296) { target_bitsize = 296; } if(binary_length <= 256) { target_bitsize = 256; } @@ -1424,7 +1424,7 @@ int cc_binary_string(struct zint_symbol *symbol, const char source[], char binar if(binary_length <= 56) { target_bitsize = 56; } break; case 3: - if(binary_length > 768) { return ERROR_TOO_LONG; } + if(binary_length > 768) { return ZINT_ERROR_TOO_LONG; } if(binary_length <= 768) { target_bitsize = 768; } if(binary_length <= 648) { target_bitsize = 648; } if(binary_length <= 536) { target_bitsize = 536; } @@ -1437,7 +1437,7 @@ int cc_binary_string(struct zint_symbol *symbol, const char source[], char binar if(binary_length <= 32) { target_bitsize = 32; } break; case 4: - if(binary_length > 1184) { return ERROR_TOO_LONG; } + if(binary_length > 1184) { return ZINT_ERROR_TOO_LONG; } if(binary_length <= 1184) { target_bitsize = 1184; } if(binary_length <= 1016) { target_bitsize = 1016; } if(binary_length <= 840) { target_bitsize = 840; } @@ -1480,7 +1480,7 @@ int cc_binary_string(struct zint_symbol *symbol, const char source[], char binar codewords_used += 3; if(codewords_used > symbol->option_3) { - return ERROR_TOO_LONG; + return ZINT_ERROR_TOO_LONG; } /* *(cc_width) = 0.5 + sqrt((codewords_used) / 3); */ *(cc_width) = (lin_width - 62) / 17; @@ -1545,7 +1545,7 @@ int cc_binary_string(struct zint_symbol *symbol, const char source[], char binar if(strlen(binary_string) > 11805) { /* (2361 * 5) */ strcpy(symbol->errtxt, "Input too long"); - return ERROR_TOO_LONG; + return ZINT_ERROR_TOO_LONG; } /* all the code below is repeated from above - it needs to be calculated again because the @@ -1556,7 +1556,7 @@ int cc_binary_string(struct zint_symbol *symbol, const char source[], char binar /* CC-A 2D component - calculate padding required */ switch(*(cc_width)) { case 2: - if(binary_length > 167) { return ERROR_TOO_LONG; } + if(binary_length > 167) { return ZINT_ERROR_TOO_LONG; } if(binary_length <= 167) { target_bitsize = 167; } if(binary_length <= 138) { target_bitsize = 138; } if(binary_length <= 118) { target_bitsize = 118; } @@ -1566,7 +1566,7 @@ int cc_binary_string(struct zint_symbol *symbol, const char source[], char binar if(binary_length <= 59) { target_bitsize = 59; } break; case 3: - if(binary_length > 167) { return ERROR_TOO_LONG; } + if(binary_length > 167) { return ZINT_ERROR_TOO_LONG; } if(binary_length <= 167) { target_bitsize = 167; } if(binary_length <= 138) { target_bitsize = 138; } if(binary_length <= 118) { target_bitsize = 118; } @@ -1574,7 +1574,7 @@ int cc_binary_string(struct zint_symbol *symbol, const char source[], char binar if(binary_length <= 78) { target_bitsize = 78; } break; case 4: - if(binary_length > 197) { return ERROR_TOO_LONG; } + if(binary_length > 197) { return ZINT_ERROR_TOO_LONG; } if(binary_length <= 197) { target_bitsize = 197; } if(binary_length <= 167) { target_bitsize = 167; } if(binary_length <= 138) { target_bitsize = 138; } @@ -1588,7 +1588,7 @@ int cc_binary_string(struct zint_symbol *symbol, const char source[], char binar /* CC-B 2D component */ switch(*(cc_width)) { case 2: - if(binary_length > 336) { return ERROR_TOO_LONG; } + if(binary_length > 336) { return ZINT_ERROR_TOO_LONG; } if(binary_length <= 336) { target_bitsize = 336; } if(binary_length <= 296) { target_bitsize = 296; } if(binary_length <= 256) { target_bitsize = 256; } @@ -1598,7 +1598,7 @@ int cc_binary_string(struct zint_symbol *symbol, const char source[], char binar if(binary_length <= 56) { target_bitsize = 56; } break; case 3: - if(binary_length > 768) { return ERROR_TOO_LONG; } + if(binary_length > 768) { return ZINT_ERROR_TOO_LONG; } if(binary_length <= 768) { target_bitsize = 768; } if(binary_length <= 648) { target_bitsize = 648; } if(binary_length <= 536) { target_bitsize = 536; } @@ -1611,7 +1611,7 @@ int cc_binary_string(struct zint_symbol *symbol, const char source[], char binar if(binary_length <= 32) { target_bitsize = 32; } break; case 4: - if(binary_length > 1184) { return ERROR_TOO_LONG; } + if(binary_length > 1184) { return ZINT_ERROR_TOO_LONG; } if(binary_length <= 1184) { target_bitsize = 1184; } if(binary_length <= 1016) { target_bitsize = 1016; } if(binary_length <= 840) { target_bitsize = 840; } @@ -1654,7 +1654,7 @@ int cc_binary_string(struct zint_symbol *symbol, const char source[], char binar codewords_used += 3; if(codewords_used > symbol->option_3) { - return ERROR_TOO_LONG; + return ZINT_ERROR_TOO_LONG; } /* *(cc_width) = 0.5 + sqrt((codewords_used) / 3); */ *(cc_width) = (lin_width - 62) / 17; @@ -1766,12 +1766,12 @@ int composite(struct zint_symbol *symbol, unsigned char source[], int length) pri_len = strlen(symbol->primary); if(pri_len == 0) { strcpy(symbol->errtxt, "No primary (linear) message in 2D composite"); - return ERROR_INVALID_OPTION; + return ZINT_ERROR_INVALID_OPTION; } if(length > 2990) { strcpy(symbol->errtxt, "2D component input data too long"); - return ERROR_TOO_LONG; + return ZINT_ERROR_TOO_LONG; } linear = ZBarcode_Create(); /* Symbol contains the 2D component and Linear contains the rest */ @@ -1784,7 +1784,7 @@ int composite(struct zint_symbol *symbol, unsigned char source[], int length) if((cc_mode == 3) && (symbol->symbology != BARCODE_EAN128_CC)) { /* CC-C can only be used with a GS1-128 linear part */ strcpy(symbol->errtxt, "Invalid mode (CC-C only valid with GS1-128 linear component)"); - return ERROR_INVALID_OPTION; + return ZINT_ERROR_INVALID_OPTION; } linear->symbology = symbol->symbology; @@ -1849,16 +1849,16 @@ int composite(struct zint_symbol *symbol, unsigned char source[], int length) if(cc_mode == 1) { i = cc_binary_string(symbol, reduced, binary_string, cc_mode, &cc_width, &ecc_level, linear->width); - if (i == ERROR_TOO_LONG) { + if (i == ZINT_ERROR_TOO_LONG) { cc_mode = 2; } } if(cc_mode == 2) { /* If the data didn't fit into CC-A it is recalculated for CC-B */ i = cc_binary_string(symbol, reduced, binary_string, cc_mode, &cc_width, &ecc_level, linear->width); - if (i == ERROR_TOO_LONG) { + if (i == ZINT_ERROR_TOO_LONG) { if(symbol->symbology != BARCODE_EAN128_CC) { - return ERROR_TOO_LONG; + return ZINT_ERROR_TOO_LONG; } else { cc_mode = 3; } @@ -1868,8 +1868,8 @@ int composite(struct zint_symbol *symbol, unsigned char source[], int length) if(cc_mode == 3) { /* If the data didn't fit in CC-B (and linear part is GS1-128) it is recalculated for CC-C */ i = cc_binary_string(symbol, reduced, binary_string, cc_mode, &cc_width, &ecc_level, linear->width); - if (i == ERROR_TOO_LONG) { - return ERROR_TOO_LONG; + if (i == ZINT_ERROR_TOO_LONG) { + return ZINT_ERROR_TOO_LONG; } } @@ -1880,7 +1880,7 @@ int composite(struct zint_symbol *symbol, unsigned char source[], int length) } if(error_number != 0) { - return ERROR_ENCODING_PROBLEM; + return ZINT_ERROR_ENCODING_PROBLEM; } /* Merge the linear component with the 2D component */ diff --git a/backend/dmatrix.c b/backend/dmatrix.c index 32820599..571e7c9f 100755 --- a/backend/dmatrix.c +++ b/backend/dmatrix.c @@ -553,7 +553,7 @@ int dm200encode(struct zint_symbol *symbol, unsigned char source[], unsigned cha if(symbol->output_options & READER_INIT) { if(gs1) { strcpy(symbol->errtxt, "Cannot encode in GS1 mode and Reader Initialisation at the same time"); - return ERROR_INVALID_OPTION; + return ZINT_ERROR_INVALID_OPTION; } else { target[tp] = 234; tp++; /* Reader Programming */ concat(binary, " "); @@ -1075,7 +1075,7 @@ int data_matrix_200(struct zint_symbol *symbol, unsigned char source[], int leng if(binlen == 0) { strcpy(symbol->errtxt, "Data too long to fit in symbol"); - return ERROR_TOO_LONG; + return ZINT_ERROR_TOO_LONG; } if((symbol->option_2 >= 1) && (symbol->option_2 <= DMSIZESCOUNT)) { @@ -1102,7 +1102,7 @@ int data_matrix_200(struct zint_symbol *symbol, unsigned char source[], int leng symbolsize = calcsize; if(optionsize != -1) { /* flag an error */ - error_number = WARN_INVALID_OPTION; + error_number = ZINT_WARN_INVALID_OPTION; strcpy(symbol->errtxt, "Data does not fit in selected symbol size"); } } @@ -1212,7 +1212,7 @@ int dmatrix(struct zint_symbol *symbol, unsigned char source[], int length) } else { /* ECC 000 - 140 */ strcpy(symbol->errtxt, "Older Data Matrix standards are no longer supported"); - error_number = ERROR_INVALID_OPTION; + error_number = ZINT_ERROR_INVALID_OPTION; } return error_number; diff --git a/backend/gridmtx.c b/backend/gridmtx.c index 865fd882..bc5e4963 100644 --- a/backend/gridmtx.c +++ b/backend/gridmtx.c @@ -699,7 +699,7 @@ int gm_encode(int gbdata[], int length, char binary[], int reader) break; } if(strlen(binary) > 9191) { - return ERROR_TOO_LONG; + return ZINT_ERROR_TOO_LONG; } } while(sp < length); @@ -736,7 +736,7 @@ int gm_encode(int gbdata[], int length, char binary[], int reader) } if(strlen(binary) > 9191) { - return ERROR_TOO_LONG; + return ZINT_ERROR_TOO_LONG; } return 0; } @@ -961,7 +961,7 @@ int grid_matrix(struct zint_symbol *symbol, unsigned char source[], int length) } while ((j < 7445) && (glyph == 0)); if(glyph == 0) { strcpy(symbol->errtxt, "Invalid character in input data"); - return ERROR_INVALID_DATA; + return ZINT_ERROR_INVALID_DATA; } gbdata[i] = glyph; } @@ -1041,7 +1041,7 @@ int grid_matrix(struct zint_symbol *symbol, unsigned char source[], int length) if(data_cw > data_max) { strcpy(symbol->errtxt, "Input data too long"); - return ERROR_TOO_LONG; + return ZINT_ERROR_TOO_LONG; } gm_add_ecc(binary, data_cw, layers, ecc_level, word); diff --git a/backend/gs1.c b/backend/gs1.c index 3a7169ca..bf467a37 100644 --- a/backend/gs1.c +++ b/backend/gs1.c @@ -76,17 +76,17 @@ int gs1_verify(struct zint_symbol *symbol, unsigned char source[], const unsigne for(i = 0; i < src_len; i++) { if(source[i] >=128) { strcpy(symbol->errtxt, "Extended ASCII characters are not supported by GS1"); - return ERROR_INVALID_DATA; + return ZINT_ERROR_INVALID_DATA; } if(source[i] < 32) { strcpy(symbol->errtxt, "Control characters are not supported by GS1"); - return ERROR_INVALID_DATA; + return ZINT_ERROR_INVALID_DATA; } } if(source[0] != '[') { strcpy(symbol->errtxt, "Data does not start with an AI"); - return ERROR_INVALID_DATA; + return ZINT_ERROR_INVALID_DATA; } /* Check the position of the brackets */ @@ -115,31 +115,31 @@ int gs1_verify(struct zint_symbol *symbol, unsigned char source[], const unsigne if(bracket_level != 0) { /* Not all brackets are closed */ strcpy(symbol->errtxt, "Malformed AI in input data (brackets don\'t match)"); - return ERROR_INVALID_DATA; + return ZINT_ERROR_INVALID_DATA; } if(max_bracket_level > 1) { /* Nested brackets */ strcpy(symbol->errtxt, "Found nested brackets in input data"); - return ERROR_INVALID_DATA; + return ZINT_ERROR_INVALID_DATA; } if(max_ai_length > 4) { /* AI is too long */ strcpy(symbol->errtxt, "Invalid AI in input data (AI too long)"); - return ERROR_INVALID_DATA; + return ZINT_ERROR_INVALID_DATA; } if(min_ai_length <= 1) { /* AI is too short */ strcpy(symbol->errtxt, "Invalid AI in input data (AI too short)"); - return ERROR_INVALID_DATA; + return ZINT_ERROR_INVALID_DATA; } if(ai_latch == 1) { /* Non-numeric data in AI */ strcpy(symbol->errtxt, "Invalid AI in input data (non-numeric characters in AI)"); - return ERROR_INVALID_DATA; + return ZINT_ERROR_INVALID_DATA; } ai_count = 0; @@ -172,7 +172,7 @@ int gs1_verify(struct zint_symbol *symbol, unsigned char source[], const unsigne if(data_length[i] == 0) { /* No data for given AI */ strcpy(symbol->errtxt, "Empty data field in input data"); - return ERROR_INVALID_DATA; + return ZINT_ERROR_INVALID_DATA; } } @@ -253,13 +253,13 @@ int gs1_verify(struct zint_symbol *symbol, unsigned char source[], const unsigne if(error_latch == 5) { strcpy(symbol->errtxt, "Invalid data length for AI "); concat(symbol->errtxt, ai_string); - return ERROR_INVALID_DATA; + return ZINT_ERROR_INVALID_DATA; } if(error_latch == 6) { strcpy(symbol->errtxt, "Invalid AI value "); concat(symbol->errtxt, ai_string); - return ERROR_INVALID_DATA; + return ZINT_ERROR_INVALID_DATA; } /* Resolve AI data - put resulting string in 'reduced' */ @@ -318,5 +318,5 @@ int ugs1_verify(struct zint_symbol *symbol, unsigned char source[], const unsign return 0; } strcpy(symbol->errtxt, "ugs1_verify overflow"); - return ERROR_INVALID_DATA; + return ZINT_ERROR_INVALID_DATA; } diff --git a/backend/imail.c b/backend/imail.c index 6268a23b..2d458cf4 100644 --- a/backend/imail.c +++ b/backend/imail.c @@ -334,10 +334,10 @@ int imail(struct zint_symbol *symbol, unsigned char source[], int length) if(length > 32) { strcpy(symbol->errtxt, "Input too long"); - return ERROR_TOO_LONG; + return ZINT_ERROR_TOO_LONG; } error_number = is_sane(SODIUM, source, length); - if(error_number == ERROR_INVALID_DATA) { + if(error_number == ZINT_ERROR_INVALID_DATA) { strcpy(symbol->errtxt, "Invalid characters in data"); return error_number; } @@ -374,11 +374,11 @@ int imail(struct zint_symbol *symbol, unsigned char source[], int length) if(strlen(tracker) != 20) { strcpy(symbol->errtxt, "Invalid length tracking code"); - return ERROR_INVALID_DATA; + return ZINT_ERROR_INVALID_DATA; } if(strlen(zip) > 11) { strcpy(symbol->errtxt, "Invalid ZIP code"); - return ERROR_INVALID_DATA; + return ZINT_ERROR_INVALID_DATA; } /* *** Step 1 - Conversion of Data Fields into Binary Data *** */ diff --git a/backend/library.c b/backend/library.c index 6889de40..8b46ca1f 100755 --- a/backend/library.c +++ b/backend/library.c @@ -224,7 +224,7 @@ int dump_plot(struct zint_symbol *symbol) f = fopen(symbol->outfile, "w"); if(!f) { strcpy(symbol->errtxt, "Could not open output file"); - return ERROR_FILE_ACCESS; + return ZINT_ERROR_FILE_ACCESS; } } @@ -254,11 +254,11 @@ int hibc(struct zint_symbol *symbol, unsigned char source[], int length) if(length > 36) { strcpy(symbol->errtxt, "Data too long for HIBC LIC"); - return ERROR_TOO_LONG; + return ZINT_ERROR_TOO_LONG; } to_upper(source); error_number = is_sane(TECHNETIUM , source, length); - if(error_number == ERROR_INVALID_DATA) { + if(error_number == ZINT_ERROR_INVALID_DATA) { strcpy(symbol->errtxt, "Invalid characters in data"); return error_number; } @@ -606,8 +606,8 @@ int ZBarcode_Encode(struct zint_symbol *symbol, unsigned char *source, int lengt } if(length == 0) { strcpy(symbol->errtxt, "No input data"); - error_tag(symbol->errtxt, ERROR_INVALID_DATA); - return ERROR_INVALID_DATA; + error_tag(symbol->errtxt, ZINT_ERROR_INVALID_DATA); + return ZINT_ERROR_INVALID_DATA; } if(strcmp(symbol->outfile, "") == 0) { @@ -620,32 +620,32 @@ int ZBarcode_Encode(struct zint_symbol *symbol, unsigned char *source, int lengt #endif /* First check the symbology field */ - if(symbol->symbology < 1) { strcpy(symbol->errtxt, "Symbology out of range, using Code 128"); symbol->symbology = BARCODE_CODE128; error_number = WARN_INVALID_OPTION; } + if(symbol->symbology < 1) { strcpy(symbol->errtxt, "Symbology out of range, using Code 128"); symbol->symbology = BARCODE_CODE128; error_number = ZINT_WARN_INVALID_OPTION; } /* symbol->symbologys 1 to 86 are defined by tbarcode */ if(symbol->symbology == 5) { symbol->symbology = BARCODE_C25MATRIX; } if((symbol->symbology >= 10) && (symbol->symbology <= 12)) { symbol->symbology = BARCODE_EANX; } if((symbol->symbology == 14) || (symbol->symbology == 15)) { symbol->symbology = BARCODE_EANX; } if(symbol->symbology == 17) { symbol->symbology = BARCODE_UPCA; } - if(symbol->symbology == 19) { strcpy(symbol->errtxt, "Codabar 18 not supported, using Codabar"); symbol->symbology = BARCODE_CODABAR; error_number = WARN_INVALID_OPTION; } + if(symbol->symbology == 19) { strcpy(symbol->errtxt, "Codabar 18 not supported, using Codabar"); symbol->symbology = BARCODE_CODABAR; error_number = ZINT_WARN_INVALID_OPTION; } if(symbol->symbology == 26) { symbol->symbology = BARCODE_UPCA; } - if(symbol->symbology == 27) { strcpy(symbol->errtxt, "UPCD1 not supported"); error_number = ERROR_INVALID_OPTION; } + if(symbol->symbology == 27) { strcpy(symbol->errtxt, "UPCD1 not supported"); error_number = ZINT_ERROR_INVALID_OPTION; } if(symbol->symbology == 33) { symbol->symbology = BARCODE_EAN128; } if((symbol->symbology == 35) || (symbol->symbology == 36)) { symbol->symbology = BARCODE_UPCA; } if((symbol->symbology == 38) || (symbol->symbology == 39)) { symbol->symbology = BARCODE_UPCE; } if((symbol->symbology >= 41) && (symbol->symbology <= 45)) { symbol->symbology = BARCODE_POSTNET; } if(symbol->symbology == 46) { symbol->symbology = BARCODE_PLESSEY; } if(symbol->symbology == 48) { symbol->symbology = BARCODE_NVE18; } - if(symbol->symbology == 54) { strcpy(symbol->errtxt, "General Parcel Code not supported, using Code 128"); symbol->symbology = BARCODE_CODE128; error_number = WARN_INVALID_OPTION; } + if(symbol->symbology == 54) { strcpy(symbol->errtxt, "General Parcel Code not supported, using Code 128"); symbol->symbology = BARCODE_CODE128; error_number = ZINT_WARN_INVALID_OPTION; } if((symbol->symbology == 59) || (symbol->symbology == 61)) { symbol->symbology = BARCODE_CODE128; } if(symbol->symbology == 62) { symbol->symbology = BARCODE_CODE93; } if((symbol->symbology == 64) || (symbol->symbology == 65)) { symbol->symbology = BARCODE_AUSPOST; } - if(symbol->symbology == 73) { strcpy(symbol->errtxt, "Codablock E not supported"); error_number = ERROR_INVALID_OPTION; } + if(symbol->symbology == 73) { strcpy(symbol->errtxt, "Codablock E not supported"); error_number = ZINT_ERROR_INVALID_OPTION; } if(symbol->symbology == 78) { symbol->symbology = BARCODE_RSS14; } if(symbol->symbology == 83) { symbol->symbology = BARCODE_PLANET; } if(symbol->symbology == 88) { symbol->symbology = BARCODE_EAN128; } - if(symbol->symbology == 91) { strcpy(symbol->errtxt, "Symbology out of range, using Code 128"); symbol->symbology = BARCODE_CODE128; error_number = WARN_INVALID_OPTION; } - if((symbol->symbology >= 94) && (symbol->symbology <= 96)) { strcpy(symbol->errtxt, "Symbology out of range, using Code 128"); symbol->symbology = BARCODE_CODE128; error_number = WARN_INVALID_OPTION; } + if(symbol->symbology == 91) { strcpy(symbol->errtxt, "Symbology out of range, using Code 128"); symbol->symbology = BARCODE_CODE128; error_number = ZINT_WARN_INVALID_OPTION; } + if((symbol->symbology >= 94) && (symbol->symbology <= 96)) { strcpy(symbol->errtxt, "Symbology out of range, using Code 128"); symbol->symbology = BARCODE_CODE128; error_number = ZINT_WARN_INVALID_OPTION; } if(symbol->symbology == 100) { symbol->symbology = BARCODE_HIBC_128; } if(symbol->symbology == 101) { symbol->symbology = BARCODE_HIBC_39; } if(symbol->symbology == 103) { symbol->symbology = BARCODE_HIBC_DM; } @@ -653,10 +653,10 @@ int ZBarcode_Encode(struct zint_symbol *symbol, unsigned char *source, int lengt if(symbol->symbology == 107) { symbol->symbology = BARCODE_HIBC_PDF; } if(symbol->symbology == 109) { symbol->symbology = BARCODE_HIBC_MICPDF; } if(symbol->symbology == 111) { symbol->symbology = BARCODE_HIBC_BLOCKF; } - if((symbol->symbology >= 113) && (symbol->symbology <= 127)) { strcpy(symbol->errtxt, "Symbology out of range, using Code 128"); symbol->symbology = BARCODE_CODE128; error_number = WARN_INVALID_OPTION; } + if((symbol->symbology >= 113) && (symbol->symbology <= 127)) { strcpy(symbol->errtxt, "Symbology out of range, using Code 128"); symbol->symbology = BARCODE_CODE128; error_number = ZINT_WARN_INVALID_OPTION; } /* Everything from 128 up is Zint-specific */ - if(symbol->symbology >= 143) { strcpy(symbol->errtxt, "Symbology out of range, using Code 128"); symbol->symbology = BARCODE_CODE128; error_number = WARN_INVALID_OPTION; } - if((symbol->symbology == BARCODE_CODABLOCKF) || (symbol->symbology == BARCODE_HIBC_BLOCKF)) { strcpy(symbol->errtxt, "Codablock F not supported"); error_number = ERROR_INVALID_OPTION; } + if(symbol->symbology >= 143) { strcpy(symbol->errtxt, "Symbology out of range, using Code 128"); symbol->symbology = BARCODE_CODE128; error_number = ZINT_WARN_INVALID_OPTION; } + if((symbol->symbology == BARCODE_CODABLOCKF) || (symbol->symbology == BARCODE_HIBC_BLOCKF)) { strcpy(symbol->errtxt, "Codablock F not supported"); error_number = ZINT_ERROR_INVALID_OPTION; } if(error_number > 4) { error_tag(symbol->errtxt, error_number); @@ -671,7 +671,7 @@ int ZBarcode_Encode(struct zint_symbol *symbol, unsigned char *source, int lengt for(i = 0; i < length; i++) { if(source[i] == '\0') { strcpy(symbol->errtxt, "NULL characters not permitted in GS1 mode"); - return ERROR_INVALID_DATA; + return ZINT_ERROR_INVALID_DATA; } } if(gs1_compliant(symbol->symbology) == 1) { @@ -680,7 +680,7 @@ int ZBarcode_Encode(struct zint_symbol *symbol, unsigned char *source, int lengt length = ustrlen(local_source); } else { strcpy(symbol->errtxt, "Selected symbology does not support GS1 mode"); - return ERROR_INVALID_OPTION; + return ZINT_ERROR_INVALID_OPTION; } } else { memcpy(local_source, source, length); @@ -729,7 +729,7 @@ int ZBarcode_Print(struct zint_symbol *symbol, int rotate_angle) break; default: strcpy(symbol->errtxt, "Invalid rotation angle"); - return ERROR_INVALID_OPTION; + return ZINT_ERROR_INVALID_OPTION; } if(strlen(symbol->outfile) > 3) { @@ -756,13 +756,13 @@ int ZBarcode_Print(struct zint_symbol *symbol, int rotate_angle) } else { strcpy(symbol->errtxt, "Unknown output format"); - error_tag(symbol->errtxt, ERROR_INVALID_OPTION); - return ERROR_INVALID_OPTION; + error_tag(symbol->errtxt, ZINT_ERROR_INVALID_OPTION); + return ZINT_ERROR_INVALID_OPTION; } } else { strcpy(symbol->errtxt, "Unknown output format"); - error_tag(symbol->errtxt, ERROR_INVALID_OPTION); - return ERROR_INVALID_OPTION; + error_tag(symbol->errtxt, ZINT_ERROR_INVALID_OPTION); + return ZINT_ERROR_INVALID_OPTION; } error_tag(symbol->errtxt, error_number); @@ -781,7 +781,7 @@ int ZBarcode_Buffer(struct zint_symbol *symbol, int rotate_angle) break; default: strcpy(symbol->errtxt, "Invalid rotation angle"); - return ERROR_INVALID_OPTION; + return ZINT_ERROR_INVALID_OPTION; } error_number = bmp_handle(symbol, rotate_angle); @@ -834,7 +834,7 @@ int ZBarcode_Encode_File(struct zint_symbol *symbol, char *filename) file = fopen(filename, "rb"); if (!file) { strcpy(symbol->errtxt, "Unable to read input file"); - return ERROR_INVALID_DATA; + return ZINT_ERROR_INVALID_DATA; } /* Get file length */ @@ -846,7 +846,7 @@ int ZBarcode_Encode_File(struct zint_symbol *symbol, char *filename) /* The largest amount of data that can be encoded is 7089 numeric digits in QR Code */ strcpy(symbol->errtxt, "Input file too long"); fclose(file); - return ERROR_INVALID_DATA; + return ZINT_ERROR_INVALID_DATA; } } @@ -856,7 +856,7 @@ int ZBarcode_Encode_File(struct zint_symbol *symbol, char *filename) strcpy(symbol->errtxt, "Internal memory error"); if (strcmp(filename, "-")) fclose(file); - return ERROR_MEMORY; + return ZINT_ERROR_MEMORY; } /* Read file contents into buffer */ @@ -868,7 +868,7 @@ int ZBarcode_Encode_File(struct zint_symbol *symbol, char *filename) { strcpy(symbol->errtxt, strerror(errno)); nRead = 0; - return ERROR_INVALID_DATA; + return ZINT_ERROR_INVALID_DATA; } nRead += n; } while (!feof(file) && (0 < n) && (nRead < fileLen)); diff --git a/backend/maxicode.c b/backend/maxicode.c index 3dcb6da8..a60d03e2 100644 --- a/backend/maxicode.c +++ b/backend/maxicode.c @@ -139,7 +139,7 @@ int maxi_text_process(int mode, unsigned char source[], int length) int set[144], character[144], i, j, done, count, current_set; if(length > 138) { - return ERROR_TOO_LONG; + return ZINT_ERROR_TOO_LONG; } for(i = 0; i < 144; i++) { @@ -459,15 +459,15 @@ int maxi_text_process(int mode, unsigned char source[], int length) } while (i <= 143); if(((mode ==2) || (mode == 3)) && (length > 84)) { - return ERROR_TOO_LONG; + return ZINT_ERROR_TOO_LONG; } if(((mode == 4) || (mode == 6)) && (length > 93)) { - return ERROR_TOO_LONG; + return ZINT_ERROR_TOO_LONG; } if((mode == 5) && (length > 77)) { - return ERROR_TOO_LONG; + return ZINT_ERROR_TOO_LONG; } @@ -605,7 +605,7 @@ int maxicode(struct zint_symbol *symbol, unsigned char source[], int length) if((mode < 2) || (mode > 6)) { /* Only codes 2 to 6 supported */ strcpy(symbol->errtxt, "Invalid Maxicode Mode"); - return ERROR_INVALID_OPTION; + return ZINT_ERROR_INVALID_OPTION; } if((mode == 2) || (mode == 3)) { /* Modes 2 and 3 need data in symbol->primary */ @@ -614,13 +614,13 @@ int maxicode(struct zint_symbol *symbol, unsigned char source[], int length) } if(lp != 15) { strcpy(symbol->errtxt, "Invalid Primary String"); - return ERROR_INVALID_DATA; + return ZINT_ERROR_INVALID_DATA; } for(i = 9; i < 15; i++) { /* check that country code and service are numeric */ if((symbol->primary[i] < '0') || (symbol->primary[i] > '9')) { strcpy(symbol->errtxt, "Invalid Primary String"); - return ERROR_INVALID_DATA; + return ZINT_ERROR_INVALID_DATA; } } @@ -656,7 +656,7 @@ int maxicode(struct zint_symbol *symbol, unsigned char source[], int length) } i = maxi_text_process(mode, local_source, length); - if(i == ERROR_TOO_LONG ) { + if(i == ZINT_ERROR_TOO_LONG ) { strcpy(symbol->errtxt, "Input data too long"); return i; } diff --git a/backend/medical.c b/backend/medical.c index 6df9f227..77c10798 100644 --- a/backend/medical.c +++ b/backend/medical.c @@ -67,10 +67,10 @@ int pharma_one(struct zint_symbol *symbol, unsigned char source[], int length) if(length > 6) { strcpy(symbol->errtxt, "Input too long"); - return ERROR_TOO_LONG; + return ZINT_ERROR_TOO_LONG; } error_number = is_sane(NEON, source, length); - if(error_number == ERROR_INVALID_DATA) { + if(error_number == ZINT_ERROR_INVALID_DATA) { strcpy(symbol->errtxt, "Invalid characters in data"); return error_number; } @@ -79,7 +79,7 @@ int pharma_one(struct zint_symbol *symbol, unsigned char source[], int length) if((tester < 3) || (tester > 131070)) { strcpy(symbol->errtxt, "Data out of range"); - return ERROR_INVALID_DATA; + return ZINT_ERROR_INVALID_DATA; } do @@ -126,7 +126,7 @@ int pharma_two_calc(struct zint_symbol *symbol, unsigned char source[], char des if((tester < 4) || (tester > 64570080)) { strcpy(symbol->errtxt, "Data out of range"); - return ERROR_INVALID_DATA; + return ZINT_ERROR_INVALID_DATA; } error_number = 0; strcpy(inter, ""); @@ -170,10 +170,10 @@ int pharma_two(struct zint_symbol *symbol, unsigned char source[], int length) if(length > 8) { strcpy(symbol->errtxt, "Input too long"); - return ERROR_TOO_LONG; + return ZINT_ERROR_TOO_LONG; } error_number = is_sane(NEON, source, length); - if(error_number == ERROR_INVALID_DATA) { + if(error_number == ZINT_ERROR_INVALID_DATA) { strcpy(symbol->errtxt, "Invalid characters in data"); return error_number; } @@ -214,11 +214,11 @@ int codabar(struct zint_symbol *symbol, unsigned char source[], int length) if(length > 60) { /* No stack smashing please */ strcpy(symbol->errtxt, "Input too long"); - return ERROR_TOO_LONG; + return ZINT_ERROR_TOO_LONG; } to_upper(source); error_number = is_sane(CALCIUM, source, length); - if(error_number == ERROR_INVALID_DATA) { + if(error_number == ZINT_ERROR_INVALID_DATA) { strcpy(symbol->errtxt, "Invalid characters in data"); return error_number; } @@ -226,14 +226,14 @@ int codabar(struct zint_symbol *symbol, unsigned char source[], int length) if((source[0] != 'A') && (source[0] != 'B') && (source[0] != 'C') && (source[0] != 'D')) { strcpy(symbol->errtxt, "Invalid characters in data"); - return ERROR_INVALID_DATA; + return ZINT_ERROR_INVALID_DATA; } if((source[length - 1] != 'A') && (source[length - 1] != 'B') && (source[length - 1] != 'C') && (source[length - 1] != 'D')) { strcpy(symbol->errtxt, "Invalid characters in data"); - return ERROR_INVALID_DATA; + return ZINT_ERROR_INVALID_DATA; } for(i = 0; i < length; i++) @@ -257,10 +257,10 @@ int code32(struct zint_symbol *symbol, unsigned char source[], int length) /* Validate the input */ if(length > 8) { strcpy(symbol->errtxt, "Input too long"); - return ERROR_TOO_LONG; + return ZINT_ERROR_TOO_LONG; } error_number = is_sane(NEON, source, length); - if(error_number == ERROR_INVALID_DATA) { + if(error_number == ZINT_ERROR_INVALID_DATA) { strcpy(symbol->errtxt, "Invalid characters in data"); return error_number; } diff --git a/backend/pdf417.c b/backend/pdf417.c index ddb80a61..20f49b0e 100644 --- a/backend/pdf417.c +++ b/backend/pdf417.c @@ -707,12 +707,12 @@ int pdf417enc(struct zint_symbol *symbol, unsigned char source[], int length) if((symbol->option_1 < -1) || (symbol->option_1 > 8)) { strcpy(symbol->errtxt, "Security value out of range"); symbol->option_1 = -1; - error_number = WARN_INVALID_OPTION; + error_number = ZINT_WARN_INVALID_OPTION; } if((symbol->option_2 < 0) || (symbol->option_2 > 30)) { strcpy(symbol->errtxt, "Number of columns out of range"); symbol->option_2 = 0; - error_number = WARN_INVALID_OPTION; + error_number = ZINT_WARN_INVALID_OPTION; } /* 349 */ @@ -723,23 +723,23 @@ int pdf417enc(struct zint_symbol *symbol, unsigned char source[], int length) switch(codeerr) { case 1: strcpy(symbol->errtxt, "No such file or file unreadable"); - error_number = ERROR_INVALID_OPTION; + error_number = ZINT_ERROR_INVALID_OPTION; break; case 2: strcpy(symbol->errtxt, "Input string too long"); - error_number = ERROR_TOO_LONG; + error_number = ZINT_ERROR_TOO_LONG; break; case 3: strcpy(symbol->errtxt, "Number of codewords per row too small"); - error_number = WARN_INVALID_OPTION; + error_number = ZINT_WARN_INVALID_OPTION; break; case 4: strcpy(symbol->errtxt, "Data too long for specified number of columns"); - error_number = ERROR_TOO_LONG; + error_number = ZINT_ERROR_TOO_LONG; break; default: strcpy(symbol->errtxt, "Something strange happened"); - error_number = ERROR_ENCODING_PROBLEM; + error_number = ZINT_ERROR_ENCODING_PROBLEM; break; } } @@ -825,12 +825,12 @@ int micro_pdf417(struct zint_symbol *symbol, unsigned char chaine[], int length) if(mclength > 126) { strcpy(symbol->errtxt, "Input data too long"); - return ERROR_TOO_LONG; + return ZINT_ERROR_TOO_LONG; } if(symbol->option_2 > 4) { strcpy(symbol->errtxt, "Specified width out of range"); symbol->option_2 = 0; - codeerr = WARN_INVALID_OPTION; + codeerr = ZINT_WARN_INVALID_OPTION; } if(debug) { @@ -849,21 +849,21 @@ int micro_pdf417(struct zint_symbol *symbol, unsigned char chaine[], int length) /* the user specified 1 column but the data doesn't fit - go to automatic */ symbol->option_2 = 0; strcpy(symbol->errtxt, "Specified symbol size too small for data"); - codeerr = WARN_INVALID_OPTION; + codeerr = ZINT_WARN_INVALID_OPTION; } if((symbol->option_2 == 2) && (mclength > 37)) { /* the user specified 2 columns but the data doesn't fit - go to automatic */ symbol->option_2 = 0; strcpy(symbol->errtxt, "Specified symbol size too small for data"); - codeerr = WARN_INVALID_OPTION; + codeerr = ZINT_WARN_INVALID_OPTION; } if((symbol->option_2 == 3) && (mclength > 82)) { /* the user specified 3 columns but the data doesn't fit - go to automatic */ symbol->option_2 = 0; strcpy(symbol->errtxt, "Specified symbol size too small for data"); - codeerr = WARN_INVALID_OPTION; + codeerr = ZINT_WARN_INVALID_OPTION; } if(symbol->option_2 == 1) { diff --git a/backend/plessey.c b/backend/plessey.c index 883448f1..f62b7a0d 100644 --- a/backend/plessey.c +++ b/backend/plessey.c @@ -58,10 +58,10 @@ int plessey(struct zint_symbol *symbol, unsigned char source[], int length) if(length > 65) { strcpy(symbol->errtxt, "Input too long"); - return ERROR_TOO_LONG; + return ZINT_ERROR_TOO_LONG; } error_number = is_sane(SSET, source, length); - if(error_number == ERROR_INVALID_DATA) { + if(error_number == ZINT_ERROR_INVALID_DATA) { strcpy(symbol->errtxt, "Invalid characters in data"); return error_number; } @@ -116,7 +116,7 @@ int msi_plessey(struct zint_symbol *symbol, unsigned char source[], int length) if(length > 55) { strcpy(symbol->errtxt, "Input too long"); - return ERROR_TOO_LONG; + return ZINT_ERROR_TOO_LONG; } /* start character */ @@ -148,7 +148,7 @@ int msi_plessey_mod10(struct zint_symbol *symbol, unsigned char source[], int le if(length > 18) { strcpy(symbol->errtxt, "Input too long"); - return ERROR_TOO_LONG; + return ZINT_ERROR_TOO_LONG; } /* start character */ @@ -219,7 +219,7 @@ int msi_plessey_mod1010(struct zint_symbol *symbol, unsigned char source[], cons if(src_len > 18) { /* No Entry Stack Smashers! limit because of str->number conversion*/ strcpy(symbol->errtxt, "Input too long"); - return ERROR_TOO_LONG; + return ZINT_ERROR_TOO_LONG; } /* start character */ @@ -332,7 +332,7 @@ int msi_plessey_mod11(struct zint_symbol *symbol, unsigned char source[], const if(src_len > 55) { strcpy(symbol->errtxt, "Input too long"); - return ERROR_TOO_LONG; + return ZINT_ERROR_TOO_LONG; } /* start character */ @@ -397,7 +397,7 @@ int msi_plessey_mod1110(struct zint_symbol *symbol, unsigned char source[], cons if(src_len > 18) { strcpy(symbol->errtxt, "Input too long"); - return ERROR_TOO_LONG; + return ZINT_ERROR_TOO_LONG; } /* start character */ @@ -488,7 +488,7 @@ int msi_handle(struct zint_symbol *symbol, unsigned char source[], int length) { error_number = is_sane(NEON, source, length); if(error_number != 0) { strcpy(symbol->errtxt, "Invalid characters in input data"); - return ERROR_INVALID_DATA; + return ZINT_ERROR_INVALID_DATA; } diff --git a/backend/png.c b/backend/png.c index a06c20b7..2ac2f5e7 100644 --- a/backend/png.c +++ b/backend/png.c @@ -1,1165 +1,1165 @@ -/* png.c - Handles output to PNG file */ - -/* - libzint - the open source barcode library - Copyright (C) 2009 Robin Stuart - - Redistribution and use in source and binary forms, with or without - modification, are permitted provided that the following conditions - are met: - - 1. Redistributions of source code must retain the above copyright - notice, this list of conditions and the following disclaimer. - 2. Redistributions in binary form must reproduce the above copyright - notice, this list of conditions and the following disclaimer in the - documentation and/or other materials provided with the distribution. - 3. Neither the name of the project nor the names of its contributors - may be used to endorse or promote products derived from this software - without specific prior written permission. - - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND - ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE - FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - SUCH DAMAGE. -*/ - -#include -#ifdef _MSC_VER -#include -#include -#endif -#include -#include -#include "common.h" - -#ifdef _MSC_VER -#include -#endif /* _MSC_VER */ - -#ifndef NO_PNG -#include -#include -#include -#endif /* NO_PNG */ -#include "maxipng.h" /* Maxicode shapes */ - -#include "font.h" /* Font for human readable text */ - -#define SSET "0123456789ABCDEF" - -#define PNG_DATA 100 -#define BMP_DATA 200 - -#ifndef NO_PNG -struct mainprog_info_type { - long width; - long height; - FILE *outfile; - jmp_buf jmpbuf; -}; - -static void writepng_error_handler(png_structp png_ptr, png_const_charp msg) -{ - struct mainprog_info_type *graphic; - - fprintf(stderr, "writepng libpng error: %s\n", msg); - fflush(stderr); - - graphic = (struct mainprog_info_type*)png_get_error_ptr(png_ptr); - if (graphic == NULL) { /* we are completely hosed now */ - fprintf(stderr, - "writepng severe error: jmpbuf not recoverable; terminating.\n"); - fflush(stderr); - return; - } - longjmp(graphic->jmpbuf, 1); -} - -int png_pixel_plot(struct zint_symbol *symbol, int image_height, int image_width, char *pixelbuf, int rotate_angle) -{ - struct mainprog_info_type wpng_info; - struct mainprog_info_type *graphic; - png_structp png_ptr; - png_infop info_ptr; - unsigned char *image_data; - int i, row, column, errno; - int fgred, fggrn, fgblu, bgred, bggrn, bgblu; - -#ifndef _MSC_VER - unsigned char outdata[image_width * 3]; -#else - unsigned char* outdata = (unsigned char*)_alloca(image_width * 3); -#endif - - graphic = &wpng_info; - - switch(rotate_angle) { - case 0: - case 180: - graphic->width = image_width; - graphic->height = image_height; - break; - case 90: - case 270: - graphic->width = image_height; - graphic->height = image_width; - break; - } - - /* sort out colour options */ - to_upper((unsigned char*)symbol->fgcolour); - to_upper((unsigned char*)symbol->bgcolour); - - if(strlen(symbol->fgcolour) != 6) { - strcpy(symbol->errtxt, "Malformed foreground colour target"); - return ERROR_INVALID_OPTION; - } - if(strlen(symbol->bgcolour) != 6) { - strcpy(symbol->errtxt, "Malformed background colour target"); - return ERROR_INVALID_OPTION; - } - errno = is_sane(SSET, (unsigned char*)symbol->fgcolour, strlen(symbol->fgcolour)); - if (errno == ERROR_INVALID_DATA) { - strcpy(symbol->errtxt, "Malformed foreground colour target"); - return ERROR_INVALID_OPTION; - } - errno = is_sane(SSET, (unsigned char*)symbol->bgcolour, strlen(symbol->bgcolour)); - if (errno == ERROR_INVALID_DATA) { - strcpy(symbol->errtxt, "Malformed background colour target"); - return ERROR_INVALID_OPTION; - } - - fgred = (16 * ctoi(symbol->fgcolour[0])) + ctoi(symbol->fgcolour[1]); - fggrn = (16 * ctoi(symbol->fgcolour[2])) + ctoi(symbol->fgcolour[3]); - fgblu = (16 * ctoi(symbol->fgcolour[4])) + ctoi(symbol->fgcolour[5]); - bgred = (16 * ctoi(symbol->bgcolour[0])) + ctoi(symbol->bgcolour[1]); - bggrn = (16 * ctoi(symbol->bgcolour[2])) + ctoi(symbol->bgcolour[3]); - bgblu = (16 * ctoi(symbol->bgcolour[4])) + ctoi(symbol->bgcolour[5]); - - /* Open output file in binary mode */ - if((symbol->output_options & BARCODE_STDOUT) != 0) { -#ifdef _MSC_VER - if (-1 == _setmode(_fileno(stdout), _O_BINARY)) { - strcpy(symbol->errtxt, "Can't open output file"); - return ERROR_FILE_ACCESS; - } -#endif - graphic->outfile = stdout; - } else { - if (!(graphic->outfile = fopen(symbol->outfile, "wb"))) { - strcpy(symbol->errtxt, "Can't open output file"); - return ERROR_FILE_ACCESS; - } - } - - /* Set up error handling routine as proc() above */ - png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, graphic, writepng_error_handler, NULL); - if (!png_ptr) { - strcpy(symbol->errtxt, "Out of memory"); - return ERROR_MEMORY; - } - - info_ptr = png_create_info_struct(png_ptr); - if (!info_ptr) { - png_destroy_write_struct(&png_ptr, NULL); - strcpy(symbol->errtxt, "Out of memory"); - return ERROR_MEMORY; - } - - /* catch jumping here */ - if (setjmp(graphic->jmpbuf)) { - png_destroy_write_struct(&png_ptr, &info_ptr); - strcpy(symbol->errtxt, "libpng error occurred"); - return ERROR_MEMORY; - } - - /* open output file with libpng */ - png_init_io(png_ptr, graphic->outfile); - - /* set compression */ - png_set_compression_level(png_ptr,9); - - /* set Header block */ - png_set_IHDR(png_ptr, info_ptr, graphic->width, graphic->height, - 8, PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE, - PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT); - - /* write all chunks up to (but not including) first IDAT */ - png_write_info(png_ptr, info_ptr); - - /* set up the transformations: for now, just pack low-bit-depth pixels - into bytes (one, two or four pixels per byte) */ - png_set_packing(png_ptr); - - /* Pixel Plotting */ - - switch(rotate_angle) { - case 0: /* Plot the right way up */ - for(row = 0; row < image_height; row++) { - for(column = 0; column < image_width; column++) { - i = column * 3; - switch(*(pixelbuf + (image_width * row) + column)) - { - case '1': - outdata[i] = fgred; - outdata[i + 1] = fggrn; - outdata[i + 2] = fgblu; - break; - default: - outdata[i] = bgred; - outdata[i + 1] = bggrn; - outdata[i + 2] = bgblu; - break; - - } - } - /* write row contents to file */ - image_data = outdata; - png_write_row(png_ptr, image_data); - } - break; - case 90: /* Plot 90 degrees clockwise */ - for(row = 0; row < image_width; row++) { - for(column = 0; column < image_height; column++) { - i = column * 3; - switch(*(pixelbuf + (image_width * (image_height - column - 1)) + row)) - { - case '1': - outdata[i] = fgred; - outdata[i + 1] = fggrn; - outdata[i + 2] = fgblu; - break; - default: - outdata[i] = bgred; - outdata[i + 1] = bggrn; - outdata[i + 2] = bgblu; - break; - - } - } - - /* write row contents to file */ - image_data = outdata; - png_write_row(png_ptr, image_data); - } - break; - case 180: /* Plot upside down */ - for(row = 0; row < image_height; row++) { - for(column = 0; column < image_width; column++) { - i = column * 3; - switch(*(pixelbuf + (image_width * (image_height - row - 1)) + (image_width - column - 1))) - { - case '1': - outdata[i] = fgred; - outdata[i + 1] = fggrn; - outdata[i + 2] = fgblu; - break; - default: - outdata[i] = bgred; - outdata[i + 1] = bggrn; - outdata[i + 2] = bgblu; - break; - - } - } - - /* write row contents to file */ - image_data = outdata; - png_write_row(png_ptr, image_data); - } - break; - case 270: /* Plot 90 degrees anti-clockwise */ - for(row = 0; row < image_width; row++) { - for(column = 0; column < image_height; column++) { - i = column * 3; - switch(*(pixelbuf + (image_width * column) + (image_width - row - 1))) - { - case '1': - outdata[i] = fgred; - outdata[i + 1] = fggrn; - outdata[i + 2] = fgblu; - break; - default: - outdata[i] = bgred; - outdata[i + 1] = bggrn; - outdata[i + 2] = bgblu; - break; - - } - } - - /* write row contents to file */ - image_data = outdata; - png_write_row(png_ptr, image_data); - } - break; - } - - /* End the file */ - png_write_end(png_ptr, NULL); - - /* make sure we have disengaged */ - if (png_ptr && info_ptr) png_destroy_write_struct(&png_ptr, &info_ptr); - if(symbol->output_options & BARCODE_STDOUT) { - fflush(wpng_info.outfile); - } else { - fclose(wpng_info.outfile); - } - return 0; -} -#endif /* NO_PNG */ - -int bmp_pixel_plot(struct zint_symbol *symbol, int image_height, int image_width, char *pixelbuf, int rotate_angle) -{ - int i, row, column, errno; - int fgred, fggrn, fgblu, bgred, bggrn, bgblu; - - switch(rotate_angle) { - case 0: - case 180: - symbol->bitmap_width = image_width; - symbol->bitmap_height = image_height; - break; - case 90: - case 270: - symbol->bitmap_width = image_height; - symbol->bitmap_height = image_width; - break; - } - - if (symbol->bitmap != NULL) - free(symbol->bitmap); - - symbol->bitmap = (char *) malloc(image_width * image_height * 3); - - - /* sort out colour options */ - to_upper((unsigned char*)symbol->fgcolour); - to_upper((unsigned char*)symbol->bgcolour); - - if(strlen(symbol->fgcolour) != 6) { - strcpy(symbol->errtxt, "Malformed foreground colour target"); - return ERROR_INVALID_OPTION; - } - if(strlen(symbol->bgcolour) != 6) { - strcpy(symbol->errtxt, "Malformed background colour target"); - return ERROR_INVALID_OPTION; - } - errno = is_sane(SSET, (unsigned char*)symbol->fgcolour, strlen(symbol->fgcolour)); - if (errno == ERROR_INVALID_DATA) { - strcpy(symbol->errtxt, "Malformed foreground colour target"); - return ERROR_INVALID_OPTION; - } - errno = is_sane(SSET, (unsigned char*)symbol->bgcolour, strlen(symbol->fgcolour)); - if (errno == ERROR_INVALID_DATA) { - strcpy(symbol->errtxt, "Malformed background colour target"); - return ERROR_INVALID_OPTION; - } - - fgred = (16 * ctoi(symbol->fgcolour[0])) + ctoi(symbol->fgcolour[1]); - fggrn = (16 * ctoi(symbol->fgcolour[2])) + ctoi(symbol->fgcolour[3]); - fgblu = (16 * ctoi(symbol->fgcolour[4])) + ctoi(symbol->fgcolour[5]); - bgred = (16 * ctoi(symbol->bgcolour[0])) + ctoi(symbol->bgcolour[1]); - bggrn = (16 * ctoi(symbol->bgcolour[2])) + ctoi(symbol->bgcolour[3]); - bgblu = (16 * ctoi(symbol->bgcolour[4])) + ctoi(symbol->bgcolour[5]); - - /* Pixel Plotting */ - i = 0; - switch(rotate_angle) { - case 0: /* Plot the right way up */ - for(row = 0; row < image_height; row++) { - for(column = 0; column < image_width; column++) { - switch(*(pixelbuf + (image_width * row) + column)) - { - case '1': - symbol->bitmap[i++] = fgred; - symbol->bitmap[i++] = fggrn; - symbol->bitmap[i++] = fgblu; - break; - default: - symbol->bitmap[i++] = bgred; - symbol->bitmap[i++] = bggrn; - symbol->bitmap[i++] = bgblu; - break; - - } - } - } - break; - case 90: /* Plot 90 degrees clockwise */ - for(row = 0; row < image_width; row++) { - for(column = 0; column < image_height; column++) { - switch(*(pixelbuf + (image_width * (image_height - column - 1)) + row)) - { - case '1': - symbol->bitmap[i++] = fgred; - symbol->bitmap[i++] = fggrn; - symbol->bitmap[i++] = fgblu; - break; - default: - symbol->bitmap[i++] = bgred; - symbol->bitmap[i++] = bggrn; - symbol->bitmap[i++] = bgblu; - break; - - } - } - } - break; - case 180: /* Plot upside down */ - for(row = 0; row < image_height; row++) { - for(column = 0; column < image_width; column++) { - switch(*(pixelbuf + (image_width * (image_height - row - 1)) + (image_width - column - 1))) - { - case '1': - symbol->bitmap[i++] = fgred; - symbol->bitmap[i++] = fggrn; - symbol->bitmap[i++] = fgblu; - break; - default: - symbol->bitmap[i++] = bgred; - symbol->bitmap[i++] = bggrn; - symbol->bitmap[i++] = bgblu; - break; - - } - } - } - break; - case 270: /* Plot 90 degrees anti-clockwise */ - for(row = 0; row < image_width; row++) { - for(column = 0; column < image_height; column++) { - switch(*(pixelbuf + (image_width * column) + (image_width - row - 1))) - { - case '1': - symbol->bitmap[i++] = fgred; - symbol->bitmap[i++] = fggrn; - symbol->bitmap[i++] = fgblu; - break; - default: - symbol->bitmap[i++] = bgred; - symbol->bitmap[i++] = bggrn; - symbol->bitmap[i++] = bgblu; - break; - - } - } - } - break; - } - - return 0; -} - -int png_to_file(struct zint_symbol *symbol, int image_height, int image_width, char *pixelbuf, int rotate_angle, int image_type) -{ - int error_number; - float scaler = symbol->scale; - char *scaled_pixelbuf; - int horiz, vert, i; - int scale_width, scale_height; - - if(scaler == 0) { scaler = 0.5; } - scale_width = image_width * scaler; - scale_height = image_height * scaler; - - /* Apply scale options by creating another pixel buffer */ - if (!(scaled_pixelbuf = (char *) malloc(scale_width * scale_height))) { - printf("Insufficient memory for pixel buffer"); - return ERROR_ENCODING_PROBLEM; - } else { - for(i = 0; i < (scale_width * scale_height); i++) { - *(scaled_pixelbuf + i) = '0'; - } - } - - for(vert = 0; vert < scale_height; vert++) { - for(horiz = 0; horiz < scale_width; horiz++) { - *(scaled_pixelbuf + (vert * scale_width) + horiz) = *(pixelbuf + ((int)(vert / scaler) * image_width) + (int)(horiz / scaler)); - } - } - - if(image_type == PNG_DATA) { -#ifndef NO_PNG - error_number = png_pixel_plot(symbol, scale_height, scale_width, scaled_pixelbuf, rotate_angle); -#else - return ERROR_INVALID_OPTION; -#endif - } else { - error_number = bmp_pixel_plot(symbol, scale_height, scale_width, scaled_pixelbuf, rotate_angle); - } - - free(scaled_pixelbuf); - - return error_number; -} - -void draw_bar(char *pixelbuf, int xpos, int xlen, int ypos, int ylen, int image_width, int image_height) -{ - /* Draw a rectangle */ - int i, j, png_ypos; - - png_ypos = image_height - ypos - ylen; - /* This fudge is needed because EPS measures height from the bottom up but - PNG measures y position from the top down */ - - for(i = (xpos); i < (xpos + xlen); i++) { - for( j = (png_ypos); j < (png_ypos + ylen); j++) { - *(pixelbuf + (image_width * j) + i) = '1'; - } - } -} - -int bullseye_pixel(int row, int col) { - int block_val, block_pos, return_val; - - block_val = bullseye_compressed[(row * 12) + (col / 8)]; - return_val = 0; - block_pos = col % 8; - - switch(block_pos) { - case 0: if((block_val & 0x80) != 0) { return_val = 1; } break; - case 1: if((block_val & 0x40) != 0) { return_val = 1; } break; - case 2: if((block_val & 0x20) != 0) { return_val = 1; } break; - case 3: if((block_val & 0x10) != 0) { return_val = 1; } break; - case 4: if((block_val & 0x08) != 0) { return_val = 1; } break; - case 5: if((block_val & 0x04) != 0) { return_val = 1; } break; - case 6: if((block_val & 0x02) != 0) { return_val = 1; } break; - case 7: if((block_val & 0x01) != 0) { return_val = 1; } break; - } - - return return_val; -} - -void draw_bullseye(char *pixelbuf, int image_width, int xoffset, int yoffset) -{ - /* Central bullseye in Maxicode symbols */ - int i, j; - - for(j = 103; j < 196; j++) { - for(i = 0; i < 93; i++) { - if(bullseye_pixel(j - 103, i)) { - /* if(bullseye[(((j - 103) * 93) + i)] == 1) { */ - *(pixelbuf + (image_width * j) + (image_width * yoffset) + i + 99 + xoffset) = '1'; - } - } - } -} - -void draw_hexagon(char *pixelbuf, int image_width, int xposn, int yposn) -{ - /* Put a hexagon into the pixel buffer */ - int i, j; - - for(i = 0; i < 12; i++) { - for(j = 0; j < 10; j++) { - if(hexagon[(i * 10) + j] == 1) { - *(pixelbuf + (image_width * i) + (image_width * yposn) + xposn + j) = '1'; - } - } - } -} - -void draw_letter(char *pixelbuf, unsigned char letter, int xposn, int yposn, int smalltext, int image_width, int image_height) -{ - /* Put a letter into a position */ - int skip, i, j, glyph_no, alphabet; - - skip = 0; - alphabet = 0; - - if(letter < 33) { skip = 1; } - if((letter > 127) && (letter < 161)) { skip = 1; } - - if(skip == 0) { - if(letter > 128) { - alphabet = 1; - glyph_no = letter - 161; - } else { - glyph_no = letter - 33; - } - - if(smalltext) { - for(i = 0; i <= 8; i++) { - for(j = 0; j < 5; j++) { - if(alphabet == 0) { - if(small_font[(glyph_no * 5) + (i * 475) + j - 1] == 1) { - *(pixelbuf + (i * image_width) + (yposn * image_width) + xposn + j) = '1'; - } - } else { - if(small_font_extended[(glyph_no * 5) + (i * 475) + j - 1] == 1) { - *(pixelbuf + (i * image_width) + (yposn * image_width) + xposn + j) = '1'; - } - } - } - } - } else { - for(i = 0; i <= 13; i++) { - for(j = 0; j < 7 ; j++) { - if(alphabet == 0) { - if(ascii_font[(glyph_no * 7) + (i * 665) + j - 1] == 1) { - *(pixelbuf + (i * image_width) + (yposn * image_width) + xposn + j) = '1'; - } - } else { - if(ascii_ext_font[(glyph_no * 7) + (i * 665) + j - 1] == 1) { - *(pixelbuf + (i * image_width) + (yposn * image_width) + xposn + j) = '1'; - } - } - } - } - } - } -} - -void draw_string(char *pixbuf, char input_string[], int xposn, int yposn, int smalltext, int image_width, int image_height) -{ - /* Plot a string into the pixel buffer */ - int i, string_length, string_left_hand; - - string_length = strlen(input_string); - string_left_hand = xposn - ((7 * string_length) / 2); - - for(i = 0; i < string_length; i++) { - draw_letter(pixbuf, input_string[i], string_left_hand + (i * 7), yposn, smalltext, image_width, image_height); - } - -} - -int maxi_png_plot(struct zint_symbol *symbol, int rotate_angle, int data_type) -{ - int i, row, column, xposn, yposn; - int image_height, image_width; - char *pixelbuf; - int error_number; - int xoffset, yoffset; - - xoffset = symbol->border_width + symbol->whitespace_width; - yoffset = symbol->border_width; - image_width = 300 + (2 * xoffset * 2); - image_height = 300 + (2 * yoffset * 2); - - if (!(pixelbuf = (char *) malloc(image_width * image_height))) { - printf("Insifficient memory for pixel buffer"); - return ERROR_ENCODING_PROBLEM; - } else { - for(i = 0; i < (image_width * image_height); i++) { - *(pixelbuf + i) = '0'; - } - } - - draw_bullseye(pixelbuf, image_width, (2 * xoffset), (2 * yoffset)); - - for(row = 0; row < symbol->rows; row++) { - yposn = row * 9; - for(column = 0; column < symbol->width; column++) { - xposn = column * 10; - if(module_is_set(symbol, row, column)) { - if(row & 1) { - /* Odd (reduced) row */ - xposn += 5; - draw_hexagon(pixelbuf, image_width, xposn + (2 * xoffset), yposn + (2 * yoffset)); - } else { - /* Even (full) row */ - draw_hexagon(pixelbuf, image_width, xposn + (2 * xoffset), yposn + (2 * yoffset)); - } - } - } - } - - if(((symbol->output_options & BARCODE_BOX) != 0) || ((symbol->output_options & BARCODE_BIND) != 0)) { - /* boundary bars */ - draw_bar(pixelbuf, 0, image_width, 0, symbol->border_width * 2, image_width, image_height); - draw_bar(pixelbuf, 0, image_width, 300 + (symbol->border_width * 2), symbol->border_width * 2, image_width, image_height); - } - - if((symbol->output_options & BARCODE_BOX) != 0) { - /* side bars */ - draw_bar(pixelbuf, 0, symbol->border_width * 2, 0, image_height, image_width, image_height); - draw_bar(pixelbuf, 300 + ((symbol->border_width + symbol->whitespace_width + symbol->whitespace_width) * 2), symbol->border_width * 2, 0, image_height, image_width, image_height); - } - - error_number=png_to_file(symbol, image_height, image_width, pixelbuf, rotate_angle, data_type); - free(pixelbuf); - return error_number; -} - -/* Convert UTF-8 to Latin1 Codepage for the interpretation line */ -void to_latin1(unsigned char source[], unsigned char preprocessed[]) -{ - int j, i, input_length; - - input_length = ustrlen(source); - - j = 0; - i = 0; - while (i < input_length) { - switch (source[i]) { - case 0xC2: +/* png.c - Handles output to PNG file */ + +/* + libzint - the open source barcode library + Copyright (C) 2009 Robin Stuart + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + 3. Neither the name of the project nor the names of its contributors + may be used to endorse or promote products derived from this software + without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + SUCH DAMAGE. +*/ + +#include +#ifdef _MSC_VER +#include +#include +#endif +#include +#include +#include "common.h" + +#ifdef _MSC_VER +#include +#endif /* _MSC_VER */ + +#ifndef NO_PNG +#include +#include +#include +#endif /* NO_PNG */ +#include "maxipng.h" /* Maxicode shapes */ + +#include "font.h" /* Font for human readable text */ + +#define SSET "0123456789ABCDEF" + +#define PNG_DATA 100 +#define BMP_DATA 200 + +#ifndef NO_PNG +struct mainprog_info_type { + long width; + long height; + FILE *outfile; + jmp_buf jmpbuf; +}; + +static void writepng_error_handler(png_structp png_ptr, png_const_charp msg) +{ + struct mainprog_info_type *graphic; + + fprintf(stderr, "writepng libpng error: %s\n", msg); + fflush(stderr); + + graphic = (struct mainprog_info_type*)png_get_error_ptr(png_ptr); + if (graphic == NULL) { /* we are completely hosed now */ + fprintf(stderr, + "writepng severe error: jmpbuf not recoverable; terminating.\n"); + fflush(stderr); + return; + } + longjmp(graphic->jmpbuf, 1); +} + +int png_pixel_plot(struct zint_symbol *symbol, int image_height, int image_width, char *pixelbuf, int rotate_angle) +{ + struct mainprog_info_type wpng_info; + struct mainprog_info_type *graphic; + png_structp png_ptr; + png_infop info_ptr; + unsigned char *image_data; + int i, row, column, errno; + int fgred, fggrn, fgblu, bgred, bggrn, bgblu; + +#ifndef _MSC_VER + unsigned char outdata[image_width * 3]; +#else + unsigned char* outdata = (unsigned char*)_alloca(image_width * 3); +#endif + + graphic = &wpng_info; + + switch(rotate_angle) { + case 0: + case 180: + graphic->width = image_width; + graphic->height = image_height; + break; + case 90: + case 270: + graphic->width = image_height; + graphic->height = image_width; + break; + } + + /* sort out colour options */ + to_upper((unsigned char*)symbol->fgcolour); + to_upper((unsigned char*)symbol->bgcolour); + + if(strlen(symbol->fgcolour) != 6) { + strcpy(symbol->errtxt, "Malformed foreground colour target"); + return ZINT_ERROR_INVALID_OPTION; + } + if(strlen(symbol->bgcolour) != 6) { + strcpy(symbol->errtxt, "Malformed background colour target"); + return ZINT_ERROR_INVALID_OPTION; + } + errno = is_sane(SSET, (unsigned char*)symbol->fgcolour, strlen(symbol->fgcolour)); + if (errno == ZINT_ERROR_INVALID_DATA) { + strcpy(symbol->errtxt, "Malformed foreground colour target"); + return ZINT_ERROR_INVALID_OPTION; + } + errno = is_sane(SSET, (unsigned char*)symbol->bgcolour, strlen(symbol->bgcolour)); + if (errno == ZINT_ERROR_INVALID_DATA) { + strcpy(symbol->errtxt, "Malformed background colour target"); + return ZINT_ERROR_INVALID_OPTION; + } + + fgred = (16 * ctoi(symbol->fgcolour[0])) + ctoi(symbol->fgcolour[1]); + fggrn = (16 * ctoi(symbol->fgcolour[2])) + ctoi(symbol->fgcolour[3]); + fgblu = (16 * ctoi(symbol->fgcolour[4])) + ctoi(symbol->fgcolour[5]); + bgred = (16 * ctoi(symbol->bgcolour[0])) + ctoi(symbol->bgcolour[1]); + bggrn = (16 * ctoi(symbol->bgcolour[2])) + ctoi(symbol->bgcolour[3]); + bgblu = (16 * ctoi(symbol->bgcolour[4])) + ctoi(symbol->bgcolour[5]); + + /* Open output file in binary mode */ + if((symbol->output_options & BARCODE_STDOUT) != 0) { +#ifdef _MSC_VER + if (-1 == _setmode(_fileno(stdout), _O_BINARY)) { + strcpy(symbol->errtxt, "Can't open output file"); + return ZINT_ERROR_FILE_ACCESS; + } +#endif + graphic->outfile = stdout; + } else { + if (!(graphic->outfile = fopen(symbol->outfile, "wb"))) { + strcpy(symbol->errtxt, "Can't open output file"); + return ZINT_ERROR_FILE_ACCESS; + } + } + + /* Set up error handling routine as proc() above */ + png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, graphic, writepng_error_handler, NULL); + if (!png_ptr) { + strcpy(symbol->errtxt, "Out of memory"); + return ZINT_ERROR_MEMORY; + } + + info_ptr = png_create_info_struct(png_ptr); + if (!info_ptr) { + png_destroy_write_struct(&png_ptr, NULL); + strcpy(symbol->errtxt, "Out of memory"); + return ZINT_ERROR_MEMORY; + } + + /* catch jumping here */ + if (setjmp(graphic->jmpbuf)) { + png_destroy_write_struct(&png_ptr, &info_ptr); + strcpy(symbol->errtxt, "libpng error occurred"); + return ZINT_ERROR_MEMORY; + } + + /* open output file with libpng */ + png_init_io(png_ptr, graphic->outfile); + + /* set compression */ + png_set_compression_level(png_ptr,9); + + /* set Header block */ + png_set_IHDR(png_ptr, info_ptr, graphic->width, graphic->height, + 8, PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE, + PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT); + + /* write all chunks up to (but not including) first IDAT */ + png_write_info(png_ptr, info_ptr); + + /* set up the transformations: for now, just pack low-bit-depth pixels + into bytes (one, two or four pixels per byte) */ + png_set_packing(png_ptr); + + /* Pixel Plotting */ + + switch(rotate_angle) { + case 0: /* Plot the right way up */ + for(row = 0; row < image_height; row++) { + for(column = 0; column < image_width; column++) { + i = column * 3; + switch(*(pixelbuf + (image_width * row) + column)) + { + case '1': + outdata[i] = fgred; + outdata[i + 1] = fggrn; + outdata[i + 2] = fgblu; + break; + default: + outdata[i] = bgred; + outdata[i + 1] = bggrn; + outdata[i + 2] = bgblu; + break; + + } + } + /* write row contents to file */ + image_data = outdata; + png_write_row(png_ptr, image_data); + } + break; + case 90: /* Plot 90 degrees clockwise */ + for(row = 0; row < image_width; row++) { + for(column = 0; column < image_height; column++) { + i = column * 3; + switch(*(pixelbuf + (image_width * (image_height - column - 1)) + row)) + { + case '1': + outdata[i] = fgred; + outdata[i + 1] = fggrn; + outdata[i + 2] = fgblu; + break; + default: + outdata[i] = bgred; + outdata[i + 1] = bggrn; + outdata[i + 2] = bgblu; + break; + + } + } + + /* write row contents to file */ + image_data = outdata; + png_write_row(png_ptr, image_data); + } + break; + case 180: /* Plot upside down */ + for(row = 0; row < image_height; row++) { + for(column = 0; column < image_width; column++) { + i = column * 3; + switch(*(pixelbuf + (image_width * (image_height - row - 1)) + (image_width - column - 1))) + { + case '1': + outdata[i] = fgred; + outdata[i + 1] = fggrn; + outdata[i + 2] = fgblu; + break; + default: + outdata[i] = bgred; + outdata[i + 1] = bggrn; + outdata[i + 2] = bgblu; + break; + + } + } + + /* write row contents to file */ + image_data = outdata; + png_write_row(png_ptr, image_data); + } + break; + case 270: /* Plot 90 degrees anti-clockwise */ + for(row = 0; row < image_width; row++) { + for(column = 0; column < image_height; column++) { + i = column * 3; + switch(*(pixelbuf + (image_width * column) + (image_width - row - 1))) + { + case '1': + outdata[i] = fgred; + outdata[i + 1] = fggrn; + outdata[i + 2] = fgblu; + break; + default: + outdata[i] = bgred; + outdata[i + 1] = bggrn; + outdata[i + 2] = bgblu; + break; + + } + } + + /* write row contents to file */ + image_data = outdata; + png_write_row(png_ptr, image_data); + } + break; + } + + /* End the file */ + png_write_end(png_ptr, NULL); + + /* make sure we have disengaged */ + if (png_ptr && info_ptr) png_destroy_write_struct(&png_ptr, &info_ptr); + if(symbol->output_options & BARCODE_STDOUT) { + fflush(wpng_info.outfile); + } else { + fclose(wpng_info.outfile); + } + return 0; +} +#endif /* NO_PNG */ + +int bmp_pixel_plot(struct zint_symbol *symbol, int image_height, int image_width, char *pixelbuf, int rotate_angle) +{ + int i, row, column, errno; + int fgred, fggrn, fgblu, bgred, bggrn, bgblu; + + switch(rotate_angle) { + case 0: + case 180: + symbol->bitmap_width = image_width; + symbol->bitmap_height = image_height; + break; + case 90: + case 270: + symbol->bitmap_width = image_height; + symbol->bitmap_height = image_width; + break; + } + + if (symbol->bitmap != NULL) + free(symbol->bitmap); + + symbol->bitmap = (char *) malloc(image_width * image_height * 3); + + + /* sort out colour options */ + to_upper((unsigned char*)symbol->fgcolour); + to_upper((unsigned char*)symbol->bgcolour); + + if(strlen(symbol->fgcolour) != 6) { + strcpy(symbol->errtxt, "Malformed foreground colour target"); + return ZINT_ERROR_INVALID_OPTION; + } + if(strlen(symbol->bgcolour) != 6) { + strcpy(symbol->errtxt, "Malformed background colour target"); + return ZINT_ERROR_INVALID_OPTION; + } + errno = is_sane(SSET, (unsigned char*)symbol->fgcolour, strlen(symbol->fgcolour)); + if (errno == ZINT_ERROR_INVALID_DATA) { + strcpy(symbol->errtxt, "Malformed foreground colour target"); + return ZINT_ERROR_INVALID_OPTION; + } + errno = is_sane(SSET, (unsigned char*)symbol->bgcolour, strlen(symbol->fgcolour)); + if (errno == ZINT_ERROR_INVALID_DATA) { + strcpy(symbol->errtxt, "Malformed background colour target"); + return ZINT_ERROR_INVALID_OPTION; + } + + fgred = (16 * ctoi(symbol->fgcolour[0])) + ctoi(symbol->fgcolour[1]); + fggrn = (16 * ctoi(symbol->fgcolour[2])) + ctoi(symbol->fgcolour[3]); + fgblu = (16 * ctoi(symbol->fgcolour[4])) + ctoi(symbol->fgcolour[5]); + bgred = (16 * ctoi(symbol->bgcolour[0])) + ctoi(symbol->bgcolour[1]); + bggrn = (16 * ctoi(symbol->bgcolour[2])) + ctoi(symbol->bgcolour[3]); + bgblu = (16 * ctoi(symbol->bgcolour[4])) + ctoi(symbol->bgcolour[5]); + + /* Pixel Plotting */ + i = 0; + switch(rotate_angle) { + case 0: /* Plot the right way up */ + for(row = 0; row < image_height; row++) { + for(column = 0; column < image_width; column++) { + switch(*(pixelbuf + (image_width * row) + column)) + { + case '1': + symbol->bitmap[i++] = fgred; + symbol->bitmap[i++] = fggrn; + symbol->bitmap[i++] = fgblu; + break; + default: + symbol->bitmap[i++] = bgred; + symbol->bitmap[i++] = bggrn; + symbol->bitmap[i++] = bgblu; + break; + + } + } + } + break; + case 90: /* Plot 90 degrees clockwise */ + for(row = 0; row < image_width; row++) { + for(column = 0; column < image_height; column++) { + switch(*(pixelbuf + (image_width * (image_height - column - 1)) + row)) + { + case '1': + symbol->bitmap[i++] = fgred; + symbol->bitmap[i++] = fggrn; + symbol->bitmap[i++] = fgblu; + break; + default: + symbol->bitmap[i++] = bgred; + symbol->bitmap[i++] = bggrn; + symbol->bitmap[i++] = bgblu; + break; + + } + } + } + break; + case 180: /* Plot upside down */ + for(row = 0; row < image_height; row++) { + for(column = 0; column < image_width; column++) { + switch(*(pixelbuf + (image_width * (image_height - row - 1)) + (image_width - column - 1))) + { + case '1': + symbol->bitmap[i++] = fgred; + symbol->bitmap[i++] = fggrn; + symbol->bitmap[i++] = fgblu; + break; + default: + symbol->bitmap[i++] = bgred; + symbol->bitmap[i++] = bggrn; + symbol->bitmap[i++] = bgblu; + break; + + } + } + } + break; + case 270: /* Plot 90 degrees anti-clockwise */ + for(row = 0; row < image_width; row++) { + for(column = 0; column < image_height; column++) { + switch(*(pixelbuf + (image_width * column) + (image_width - row - 1))) + { + case '1': + symbol->bitmap[i++] = fgred; + symbol->bitmap[i++] = fggrn; + symbol->bitmap[i++] = fgblu; + break; + default: + symbol->bitmap[i++] = bgred; + symbol->bitmap[i++] = bggrn; + symbol->bitmap[i++] = bgblu; + break; + + } + } + } + break; + } + + return 0; +} + +int png_to_file(struct zint_symbol *symbol, int image_height, int image_width, char *pixelbuf, int rotate_angle, int image_type) +{ + int error_number; + float scaler = symbol->scale; + char *scaled_pixelbuf; + int horiz, vert, i; + int scale_width, scale_height; + + if(scaler == 0) { scaler = 0.5; } + scale_width = image_width * scaler; + scale_height = image_height * scaler; + + /* Apply scale options by creating another pixel buffer */ + if (!(scaled_pixelbuf = (char *) malloc(scale_width * scale_height))) { + printf("Insufficient memory for pixel buffer"); + return ZINT_ERROR_ENCODING_PROBLEM; + } else { + for(i = 0; i < (scale_width * scale_height); i++) { + *(scaled_pixelbuf + i) = '0'; + } + } + + for(vert = 0; vert < scale_height; vert++) { + for(horiz = 0; horiz < scale_width; horiz++) { + *(scaled_pixelbuf + (vert * scale_width) + horiz) = *(pixelbuf + ((int)(vert / scaler) * image_width) + (int)(horiz / scaler)); + } + } + + if(image_type == PNG_DATA) { +#ifndef NO_PNG + error_number = png_pixel_plot(symbol, scale_height, scale_width, scaled_pixelbuf, rotate_angle); +#else + return ZINT_ERROR_INVALID_OPTION; +#endif + } else { + error_number = bmp_pixel_plot(symbol, scale_height, scale_width, scaled_pixelbuf, rotate_angle); + } + + free(scaled_pixelbuf); + + return error_number; +} + +void draw_bar(char *pixelbuf, int xpos, int xlen, int ypos, int ylen, int image_width, int image_height) +{ + /* Draw a rectangle */ + int i, j, png_ypos; + + png_ypos = image_height - ypos - ylen; + /* This fudge is needed because EPS measures height from the bottom up but + PNG measures y position from the top down */ + + for(i = (xpos); i < (xpos + xlen); i++) { + for( j = (png_ypos); j < (png_ypos + ylen); j++) { + *(pixelbuf + (image_width * j) + i) = '1'; + } + } +} + +int bullseye_pixel(int row, int col) { + int block_val, block_pos, return_val; + + block_val = bullseye_compressed[(row * 12) + (col / 8)]; + return_val = 0; + block_pos = col % 8; + + switch(block_pos) { + case 0: if((block_val & 0x80) != 0) { return_val = 1; } break; + case 1: if((block_val & 0x40) != 0) { return_val = 1; } break; + case 2: if((block_val & 0x20) != 0) { return_val = 1; } break; + case 3: if((block_val & 0x10) != 0) { return_val = 1; } break; + case 4: if((block_val & 0x08) != 0) { return_val = 1; } break; + case 5: if((block_val & 0x04) != 0) { return_val = 1; } break; + case 6: if((block_val & 0x02) != 0) { return_val = 1; } break; + case 7: if((block_val & 0x01) != 0) { return_val = 1; } break; + } + + return return_val; +} + +void draw_bullseye(char *pixelbuf, int image_width, int xoffset, int yoffset) +{ + /* Central bullseye in Maxicode symbols */ + int i, j; + + for(j = 103; j < 196; j++) { + for(i = 0; i < 93; i++) { + if(bullseye_pixel(j - 103, i)) { + /* if(bullseye[(((j - 103) * 93) + i)] == 1) { */ + *(pixelbuf + (image_width * j) + (image_width * yoffset) + i + 99 + xoffset) = '1'; + } + } + } +} + +void draw_hexagon(char *pixelbuf, int image_width, int xposn, int yposn) +{ + /* Put a hexagon into the pixel buffer */ + int i, j; + + for(i = 0; i < 12; i++) { + for(j = 0; j < 10; j++) { + if(hexagon[(i * 10) + j] == 1) { + *(pixelbuf + (image_width * i) + (image_width * yposn) + xposn + j) = '1'; + } + } + } +} + +void draw_letter(char *pixelbuf, unsigned char letter, int xposn, int yposn, int smalltext, int image_width, int image_height) +{ + /* Put a letter into a position */ + int skip, i, j, glyph_no, alphabet; + + skip = 0; + alphabet = 0; + + if(letter < 33) { skip = 1; } + if((letter > 127) && (letter < 161)) { skip = 1; } + + if(skip == 0) { + if(letter > 128) { + alphabet = 1; + glyph_no = letter - 161; + } else { + glyph_no = letter - 33; + } + + if(smalltext) { + for(i = 0; i <= 8; i++) { + for(j = 0; j < 5; j++) { + if(alphabet == 0) { + if(small_font[(glyph_no * 5) + (i * 475) + j - 1] == 1) { + *(pixelbuf + (i * image_width) + (yposn * image_width) + xposn + j) = '1'; + } + } else { + if(small_font_extended[(glyph_no * 5) + (i * 475) + j - 1] == 1) { + *(pixelbuf + (i * image_width) + (yposn * image_width) + xposn + j) = '1'; + } + } + } + } + } else { + for(i = 0; i <= 13; i++) { + for(j = 0; j < 7 ; j++) { + if(alphabet == 0) { + if(ascii_font[(glyph_no * 7) + (i * 665) + j - 1] == 1) { + *(pixelbuf + (i * image_width) + (yposn * image_width) + xposn + j) = '1'; + } + } else { + if(ascii_ext_font[(glyph_no * 7) + (i * 665) + j - 1] == 1) { + *(pixelbuf + (i * image_width) + (yposn * image_width) + xposn + j) = '1'; + } + } + } + } + } + } +} + +void draw_string(char *pixbuf, char input_string[], int xposn, int yposn, int smalltext, int image_width, int image_height) +{ + /* Plot a string into the pixel buffer */ + int i, string_length, string_left_hand; + + string_length = strlen(input_string); + string_left_hand = xposn - ((7 * string_length) / 2); + + for(i = 0; i < string_length; i++) { + draw_letter(pixbuf, input_string[i], string_left_hand + (i * 7), yposn, smalltext, image_width, image_height); + } + +} + +int maxi_png_plot(struct zint_symbol *symbol, int rotate_angle, int data_type) +{ + int i, row, column, xposn, yposn; + int image_height, image_width; + char *pixelbuf; + int error_number; + int xoffset, yoffset; + + xoffset = symbol->border_width + symbol->whitespace_width; + yoffset = symbol->border_width; + image_width = 300 + (2 * xoffset * 2); + image_height = 300 + (2 * yoffset * 2); + + if (!(pixelbuf = (char *) malloc(image_width * image_height))) { + printf("Insifficient memory for pixel buffer"); + return ZINT_ERROR_ENCODING_PROBLEM; + } else { + for(i = 0; i < (image_width * image_height); i++) { + *(pixelbuf + i) = '0'; + } + } + + draw_bullseye(pixelbuf, image_width, (2 * xoffset), (2 * yoffset)); + + for(row = 0; row < symbol->rows; row++) { + yposn = row * 9; + for(column = 0; column < symbol->width; column++) { + xposn = column * 10; + if(module_is_set(symbol, row, column)) { + if(row & 1) { + /* Odd (reduced) row */ + xposn += 5; + draw_hexagon(pixelbuf, image_width, xposn + (2 * xoffset), yposn + (2 * yoffset)); + } else { + /* Even (full) row */ + draw_hexagon(pixelbuf, image_width, xposn + (2 * xoffset), yposn + (2 * yoffset)); + } + } + } + } + + if(((symbol->output_options & BARCODE_BOX) != 0) || ((symbol->output_options & BARCODE_BIND) != 0)) { + /* boundary bars */ + draw_bar(pixelbuf, 0, image_width, 0, symbol->border_width * 2, image_width, image_height); + draw_bar(pixelbuf, 0, image_width, 300 + (symbol->border_width * 2), symbol->border_width * 2, image_width, image_height); + } + + if((symbol->output_options & BARCODE_BOX) != 0) { + /* side bars */ + draw_bar(pixelbuf, 0, symbol->border_width * 2, 0, image_height, image_width, image_height); + draw_bar(pixelbuf, 300 + ((symbol->border_width + symbol->whitespace_width + symbol->whitespace_width) * 2), symbol->border_width * 2, 0, image_height, image_width, image_height); + } + + error_number=png_to_file(symbol, image_height, image_width, pixelbuf, rotate_angle, data_type); + free(pixelbuf); + return error_number; +} + +/* Convert UTF-8 to Latin1 Codepage for the interpretation line */ +void to_latin1(unsigned char source[], unsigned char preprocessed[]) +{ + int j, i, input_length; + + input_length = ustrlen(source); + + j = 0; + i = 0; + while (i < input_length) { + switch (source[i]) { + case 0xC2: /* UTF-8 C2xxh */ - /* Character range: C280h (latin: 80h) to C2BFh (latin: BFh) */ + /* Character range: C280h (latin: 80h) to C2BFh (latin: BFh) */ i++; - preprocessed[j] = source[i]; - j++; - break; + preprocessed[j] = source[i]; + j++; + break; case 0xC3: - /* UTF-8 C3xx */ - /* Character range: C380h (latin: C0h) to C3BFh (latin: FFh) */ + /* UTF-8 C3xx */ + /* Character range: C380h (latin: C0h) to C3BFh (latin: FFh) */ i++; - preprocessed[j] = source[i] + 64; - j++; - break; - default: - /* Process ASCII (< 80h), all other unicode points are ignored */ - if(source[i] < 128) { - preprocessed[j] = source[i]; - j++; - } - break; - } - i++; - } - preprocessed[j] = '\0'; - - return; -} - -int png_plot(struct zint_symbol *symbol, int rotate_angle, int data_type) -{ - int textdone, main_width, comp_offset, large_bar_count; - char textpart[10], addon[6]; - float addon_text_posn, preset_height, large_bar_height; - int i, r, textoffset, yoffset, xoffset, latch, image_width, image_height; - char *pixelbuf; - int addon_latch = 0, smalltext = 0; - int this_row, block_width, plot_height, plot_yposn, textpos; - float row_height, row_posn; - int error_number; - int default_text_posn; - int next_yposn; -#ifndef _MSC_VER - unsigned char local_text[ustrlen(symbol->text) + 1]; -#else - unsigned char* local_text = (unsigned char*)_alloca(ustrlen(symbol->text) + 1); -#endif - - if(symbol->show_hrt != 0) { - to_latin1(symbol->text, local_text); - } else { - local_text[0] = '\0'; - } - - textdone = 0; - main_width = symbol->width; - strcpy(addon, ""); - comp_offset = 0; - addon_text_posn = 0.0; - row_height = 0; - if(symbol->output_options & SMALL_TEXT) { - smalltext = 1; - } - - if (symbol->height == 0) { - symbol->height = 50; - } - - large_bar_count = 0; - preset_height = 0.0; - for(i = 0; i < symbol->rows; i++) { - preset_height += symbol->row_height[i]; - if(symbol->row_height[i] == 0) { - large_bar_count++; - } - } - - if (large_bar_count == 0) { - symbol->height = preset_height; - large_bar_height = 10; - } else { - large_bar_height = (symbol->height - preset_height) / large_bar_count; - } - - while(!(module_is_set(symbol, symbol->rows - 1, comp_offset))) { - comp_offset++; - } - - /* Certain symbols need whitespace otherwise characters get chopped off the sides */ - if ((((symbol->symbology == BARCODE_EANX) && (symbol->rows == 1)) || (symbol->symbology == BARCODE_EANX_CC)) - || (symbol->symbology == BARCODE_ISBNX)) { - switch(ustrlen(local_text)) { - case 13: /* EAN 13 */ - case 16: - case 19: - if(symbol->whitespace_width == 0) { - symbol->whitespace_width = 10; - } - main_width = 96 + comp_offset; - break; - default: - main_width = 68 + comp_offset; - } - } - - if (((symbol->symbology == BARCODE_UPCA) && (symbol->rows == 1)) || (symbol->symbology == BARCODE_UPCA_CC)) { - if(symbol->whitespace_width == 0) { - symbol->whitespace_width = 10; - main_width = 96 + comp_offset; - } - } - - if (((symbol->symbology == BARCODE_UPCE) && (symbol->rows == 1)) || (symbol->symbology == BARCODE_UPCE_CC)) { - if(symbol->whitespace_width == 0) { - symbol->whitespace_width = 10; - main_width = 51 + comp_offset; - } - } - - latch = 0; - r = 0; - /* Isolate add-on text */ - if(is_extendable(symbol->symbology)) { - for(i = 0; i < ustrlen(local_text); i++) { - if (latch == 1) { - addon[r] = local_text[i]; - r++; - } - if (symbol->text[i] == '+') { - latch = 1; - } - } - } - addon[r] = '\0'; - - if(ustrlen(local_text) != 0) { - textoffset = 9; - } else { - textoffset = 0; - } - xoffset = symbol->border_width + symbol->whitespace_width; - yoffset = symbol->border_width; - image_width = 2 * (symbol->width + xoffset + xoffset); - image_height = 2 * (symbol->height + textoffset + yoffset + yoffset); - - if (!(pixelbuf = (char *) malloc(image_width * image_height))) { - printf("Insufficient memory for pixel buffer"); - return ERROR_ENCODING_PROBLEM; - } else { - for(i = 0; i < (image_width * image_height); i++) { - *(pixelbuf + i) = '0'; - } - } - - if(((symbol->output_options & BARCODE_BOX) != 0) || ((symbol->output_options & BARCODE_BIND) != 0)) { - default_text_posn = image_height - 17; - } else { - default_text_posn = image_height - 17 - symbol->border_width - symbol->border_width; - } - - row_posn = textoffset + yoffset; - next_yposn = textoffset + yoffset; - row_height = 0; - - /* Plot the body of the symbol to the pixel buffer */ - for(r = 0; r < symbol->rows; r++) { - this_row = symbol->rows - r - 1; /* invert r otherwise plots upside down */ - row_posn += row_height; - plot_yposn = next_yposn; - if(symbol->row_height[this_row] == 0) { - row_height = large_bar_height; - } else { - row_height = symbol->row_height[this_row]; - } - next_yposn = (int)(row_posn + row_height); - plot_height = next_yposn - plot_yposn; - - i = 0; - if(module_is_set(symbol, this_row, 0)) { - latch = 1; - } else { - latch = 0; - } - - do { - block_width = 0; - do { - block_width++; - } while (module_is_set(symbol, this_row, i + block_width) == module_is_set(symbol, this_row, i)); - if((addon_latch == 0) && (r == 0) && (i > main_width)) { - plot_height = (int)(row_height - 5.0); - plot_yposn = (int)(row_posn - 5.0); - addon_text_posn = row_posn + row_height - 8.0; - addon_latch = 1; - } - if(latch == 1) { - /* a bar */ - draw_bar(pixelbuf, (i + xoffset) * 2, block_width * 2, plot_yposn * 2, plot_height * 2, image_width, image_height); - latch = 0; - } else { - /* a space */ - latch = 1; - } - i += block_width; - - } while (i < symbol->width); - } - - xoffset += comp_offset; - - if ((((symbol->symbology == BARCODE_EANX) && (symbol->rows == 1)) || (symbol->symbology == BARCODE_EANX_CC)) || (symbol->symbology == BARCODE_ISBNX)) { - /* guard bar extensions and text formatting for EAN8 and EAN13 */ - switch(ustrlen(local_text)) { - case 8: /* EAN-8 */ - case 11: - case 14: - draw_bar(pixelbuf, (0 + xoffset) * 2, 1 * 2, (4 + (int)yoffset) * 2, 5 * 2, image_width, image_height); - draw_bar(pixelbuf, (2 + xoffset) * 2, 1 * 2, (4 + (int)yoffset) * 2, 5 * 2, image_width, image_height); - draw_bar(pixelbuf, (32 + xoffset) * 2, 1 * 2, (4 + (int)yoffset) * 2, 5 * 2, image_width, image_height); - draw_bar(pixelbuf, (34 + xoffset) * 2, 1 * 2, (4 + (int)yoffset) * 2, 5 * 2, image_width, image_height); - draw_bar(pixelbuf, (64 + xoffset) * 2, 1 * 2, (4 + (int)yoffset) * 2, 5 * 2, image_width, image_height); - draw_bar(pixelbuf, (66 + xoffset) * 2, 1 * 2, (4 + (int)yoffset) * 2, 5 * 2, image_width, image_height); - for(i = 0; i < 4; i++) { - textpart[i] = symbol->text[i]; - } - textpart[4] = '\0'; - textpos = 2 * (17 + xoffset); - - draw_string(pixelbuf, textpart, textpos, default_text_posn, smalltext, image_width, image_height); - for(i = 0; i < 4; i++) { - textpart[i] = symbol->text[i + 4]; - } - textpart[4] = '\0'; - textpos = 2 * (50 + xoffset); - draw_string(pixelbuf, textpart, textpos, default_text_posn, smalltext, image_width, image_height); - textdone = 1; - switch(strlen(addon)) { - case 2: - textpos = 2 * (xoffset + 86); - draw_string(pixelbuf, addon, textpos, image_height - (addon_text_posn * 2) - 13, smalltext, image_width, image_height); - break; - case 5: - textpos = 2 * (xoffset + 100); - draw_string(pixelbuf, addon, textpos, image_height - (addon_text_posn * 2) - 13, smalltext, image_width, image_height); - break; - } - - break; - case 13: /* EAN 13 */ - case 16: - case 19: - draw_bar(pixelbuf, (0 + xoffset) * 2, 1 * 2, (4 + (int)yoffset) * 2, 5 * 2, image_width, image_height); - draw_bar(pixelbuf, (2 + xoffset) * 2, 1 * 2, (4 + (int)yoffset) * 2, 5 * 2, image_width, image_height); - draw_bar(pixelbuf, (46 + xoffset) * 2, 1 * 2, (4 + (int)yoffset) * 2, 5 * 2, image_width, image_height); - draw_bar(pixelbuf, (48 + xoffset) * 2, 1 * 2, (4 + (int)yoffset) * 2, 5 * 2, image_width, image_height); - draw_bar(pixelbuf, (92 + xoffset) * 2, 1 * 2, (4 + (int)yoffset) * 2, 5 * 2, image_width, image_height); - draw_bar(pixelbuf, (94 + xoffset) * 2, 1 * 2, (4 + (int)yoffset) * 2, 5 * 2, image_width, image_height); - - textpart[0] = symbol->text[0]; - textpart[1] = '\0'; - textpos = 2 * (-7 + xoffset); - draw_string(pixelbuf, textpart, textpos, default_text_posn, smalltext, image_width, image_height); - for(i = 0; i < 6; i++) { - textpart[i] = symbol->text[i + 1]; - } - textpart[6] = '\0'; - textpos = 2 * (24 + xoffset); - draw_string(pixelbuf, textpart, textpos, default_text_posn, smalltext, image_width, image_height); - for(i = 0; i < 6; i++) { - textpart[i] = symbol->text[i + 7]; - } - textpart[6] = '\0'; - textpos = 2 * (71 + xoffset); - draw_string(pixelbuf, textpart, textpos, default_text_posn, smalltext, image_width, image_height); - textdone = 1; - switch(strlen(addon)) { - case 2: - textpos = 2 * (xoffset + 114); - draw_string(pixelbuf, addon, textpos, image_height - (addon_text_posn * 2) - 13, smalltext, image_width, image_height); - break; - case 5: - textpos = 2 * (xoffset + 128); - draw_string(pixelbuf, addon, textpos, image_height - (addon_text_posn * 2) - 13, smalltext, image_width, image_height); - break; - } - break; - - } - } - - if (((symbol->symbology == BARCODE_UPCA) && (symbol->rows == 1)) || (symbol->symbology == BARCODE_UPCA_CC)) { - /* guard bar extensions and text formatting for UPCA */ - latch = 1; - - i = 0 + comp_offset; - do { - block_width = 0; - do { - block_width++; - } while (module_is_set(symbol, symbol->rows - 1, i + block_width) == module_is_set(symbol, symbol->rows - 1, i)); - if(latch == 1) { - /* a bar */ - draw_bar(pixelbuf, (i + xoffset - comp_offset) * 2, block_width * 2, (4 + (int)yoffset) * 2, 5 * 2, image_width, image_height); - latch = 0; - } else { - /* a space */ - latch = 1; - } - i += block_width; - } while (i < 11 + comp_offset); - draw_bar(pixelbuf, (46 + xoffset) * 2, 1 * 2, (4 + (int)yoffset) * 2, 5 * 2, image_width, image_height); - draw_bar(pixelbuf, (48 + xoffset) * 2, 1 * 2, (4 + (int)yoffset) * 2, 5 * 2, image_width, image_height); - latch = 1; - i = 85 + comp_offset; - do { - block_width = 0; - do { - block_width++; - } while (module_is_set(symbol, symbol->rows - 1, i + block_width) == module_is_set(symbol, symbol->rows - 1, i)); - if(latch == 1) { - /* a bar */ - draw_bar(pixelbuf, (i + xoffset - comp_offset) * 2, block_width * 2, (4 + (int)yoffset) * 2, 5 * 2, image_width, image_height); - latch = 0; - } else { - /* a space */ - latch = 1; - } - i += block_width; - } while (i < 96 + comp_offset); - textpart[0] = symbol->text[0]; - textpart[1] = '\0'; - textpos = 2 * (-5 + xoffset); - draw_string(pixelbuf, textpart, textpos, default_text_posn, smalltext, image_width, image_height); - for(i = 0; i < 5; i++) { - textpart[i] = symbol->text[i + 1]; - } - textpart[5] = '\0'; - textpos = 2 * (27 + xoffset); - draw_string(pixelbuf, textpart, textpos, default_text_posn, smalltext, image_width, image_height); - for(i = 0; i < 5; i++) { - textpart[i] = symbol->text[i + 6]; - } - textpart[6] = '\0'; - textpos = 2 * (68 + xoffset); - draw_string(pixelbuf, textpart, textpos, default_text_posn, smalltext, image_width, image_height); - textpart[0] = symbol->text[11]; - textpart[1] = '\0'; - textpos = 2 * (100 + xoffset); - draw_string(pixelbuf, textpart, textpos, default_text_posn, smalltext, image_width, image_height); - textdone = 1; - switch(strlen(addon)) { - case 2: - textpos = 2 * (xoffset + 116); - draw_string(pixelbuf, addon, textpos, image_height - (addon_text_posn * 2) - 13, smalltext, image_width, image_height); - break; - case 5: - textpos = 2 * (xoffset + 130); - draw_string(pixelbuf, addon, textpos, image_height - (addon_text_posn * 2) - 13, smalltext, image_width, image_height); - break; - } - - } - - if (((symbol->symbology == BARCODE_UPCE) && (symbol->rows == 1)) || (symbol->symbology == BARCODE_UPCE_CC)) { - /* guard bar extensions and text formatting for UPCE */ - draw_bar(pixelbuf, (0 + xoffset) * 2, 1 * 2, (4 + (int)yoffset) * 2, 5 * 2, image_width, image_height); - draw_bar(pixelbuf, (2 + xoffset) * 2, 1 * 2, (4 + (int)yoffset) * 2, 5 * 2, image_width, image_height); - draw_bar(pixelbuf, (46 + xoffset) * 2, 1 * 2, (4 + (int)yoffset) * 2, 5 * 2, image_width, image_height); - draw_bar(pixelbuf, (48 + xoffset) * 2, 1 * 2, (4 + (int)yoffset) * 2, 5 * 2, image_width, image_height); - draw_bar(pixelbuf, (50 + xoffset) * 2, 1 * 2, (4 + (int)yoffset) * 2, 5 * 2, image_width, image_height); - - textpart[0] = symbol->text[0]; - textpart[1] = '\0'; - textpos = 2 * (-5 + xoffset); - draw_string(pixelbuf, textpart, textpos, default_text_posn, smalltext, image_width, image_height); - for(i = 0; i < 6; i++) { - textpart[i] = symbol->text[i + 1]; - } - textpart[6] = '\0'; - textpos = 2 * (24 + xoffset); - draw_string(pixelbuf, textpart, textpos, default_text_posn, smalltext, image_width, image_height); - textpart[0] = symbol->text[7]; - textpart[1] = '\0'; - textpos = 2 * (55 + xoffset); - draw_string(pixelbuf, textpart, textpos, default_text_posn, smalltext, image_width, image_height); - textdone = 1; - switch(strlen(addon)) { - case 2: - textpos = 2 * (xoffset + 70); - draw_string(pixelbuf, addon, textpos, image_height - (addon_text_posn * 2) - 13, smalltext, image_width, image_height); - break; - case 5: - textpos = 2 * (xoffset + 84); - draw_string(pixelbuf, addon, textpos, image_height - (addon_text_posn * 2) - 13, smalltext, image_width, image_height); - break; - } - - } - - xoffset -= comp_offset; - - /* Put boundary bars or box around symbol */ - if(((symbol->output_options & BARCODE_BOX) != 0) || ((symbol->output_options & BARCODE_BIND) != 0)) { - /* boundary bars */ - draw_bar(pixelbuf, 0, (symbol->width + xoffset + xoffset) * 2, textoffset * 2, symbol->border_width * 2, image_width, image_height); - draw_bar(pixelbuf, 0, (symbol->width + xoffset + xoffset) * 2, (textoffset + symbol->height + symbol->border_width) * 2, symbol->border_width * 2, image_width, image_height); - if((symbol->output_options & BARCODE_BIND) != 0) { - if((symbol->rows > 1) && (is_stackable(symbol->symbology) == 1)) { - /* row binding */ - for(r = 1; r < symbol->rows; r++) { - draw_bar(pixelbuf, xoffset * 2, symbol->width * 2, ((r * row_height) + textoffset + yoffset - 1) * 2, 2 * 2, image_width, image_height); - } - } - } - } - - if((symbol->output_options & BARCODE_BOX) != 0) { - /* side bars */ - draw_bar(pixelbuf, 0, symbol->border_width * 2, textoffset * 2, (symbol->height + (2 * symbol->border_width)) * 2, image_width, image_height); - draw_bar(pixelbuf, (symbol->width + xoffset + xoffset - symbol->border_width) * 2, symbol->border_width * 2, textoffset * 2, (symbol->height + (2 * symbol->border_width)) * 2, image_width, image_height); - } - - /* Put the human readable text at the bottom */ - if((textdone == 0) && (ustrlen(local_text) != 0)) { - textpos = (image_width / 2); - draw_string(pixelbuf, (char*)local_text, textpos, default_text_posn, smalltext, image_width, image_height); - } - - error_number=png_to_file(symbol, image_height, image_width, pixelbuf, rotate_angle, data_type); - free(pixelbuf); - return error_number; -} - -#ifndef NO_PNG -int png_handle(struct zint_symbol *symbol, int rotate_angle) -{ - int error; - - if(symbol->symbology == BARCODE_MAXICODE) { - error = maxi_png_plot(symbol, rotate_angle, PNG_DATA); - } else { - - error = png_plot(symbol, rotate_angle, PNG_DATA); - } - - return error; -} -#endif /* NO_PNG */ - -int bmp_handle(struct zint_symbol *symbol, int rotate_angle) -{ - int error; - - if(symbol->symbology == BARCODE_MAXICODE) { - error = maxi_png_plot(symbol, rotate_angle, BMP_DATA); - } else { - error = png_plot(symbol, rotate_angle, BMP_DATA); - } - - return error; -} - + preprocessed[j] = source[i] + 64; + j++; + break; + default: + /* Process ASCII (< 80h), all other unicode points are ignored */ + if(source[i] < 128) { + preprocessed[j] = source[i]; + j++; + } + break; + } + i++; + } + preprocessed[j] = '\0'; + + return; +} + +int png_plot(struct zint_symbol *symbol, int rotate_angle, int data_type) +{ + int textdone, main_width, comp_offset, large_bar_count; + char textpart[10], addon[6]; + float addon_text_posn, preset_height, large_bar_height; + int i, r, textoffset, yoffset, xoffset, latch, image_width, image_height; + char *pixelbuf; + int addon_latch = 0, smalltext = 0; + int this_row, block_width, plot_height, plot_yposn, textpos; + float row_height, row_posn; + int error_number; + int default_text_posn; + int next_yposn; +#ifndef _MSC_VER + unsigned char local_text[ustrlen(symbol->text) + 1]; +#else + unsigned char* local_text = (unsigned char*)_alloca(ustrlen(symbol->text) + 1); +#endif + + if(symbol->show_hrt != 0) { + to_latin1(symbol->text, local_text); + } else { + local_text[0] = '\0'; + } + + textdone = 0; + main_width = symbol->width; + strcpy(addon, ""); + comp_offset = 0; + addon_text_posn = 0.0; + row_height = 0; + if(symbol->output_options & SMALL_TEXT) { + smalltext = 1; + } + + if (symbol->height == 0) { + symbol->height = 50; + } + + large_bar_count = 0; + preset_height = 0.0; + for(i = 0; i < symbol->rows; i++) { + preset_height += symbol->row_height[i]; + if(symbol->row_height[i] == 0) { + large_bar_count++; + } + } + + if (large_bar_count == 0) { + symbol->height = preset_height; + large_bar_height = 10; + } else { + large_bar_height = (symbol->height - preset_height) / large_bar_count; + } + + while(!(module_is_set(symbol, symbol->rows - 1, comp_offset))) { + comp_offset++; + } + + /* Certain symbols need whitespace otherwise characters get chopped off the sides */ + if ((((symbol->symbology == BARCODE_EANX) && (symbol->rows == 1)) || (symbol->symbology == BARCODE_EANX_CC)) + || (symbol->symbology == BARCODE_ISBNX)) { + switch(ustrlen(local_text)) { + case 13: /* EAN 13 */ + case 16: + case 19: + if(symbol->whitespace_width == 0) { + symbol->whitespace_width = 10; + } + main_width = 96 + comp_offset; + break; + default: + main_width = 68 + comp_offset; + } + } + + if (((symbol->symbology == BARCODE_UPCA) && (symbol->rows == 1)) || (symbol->symbology == BARCODE_UPCA_CC)) { + if(symbol->whitespace_width == 0) { + symbol->whitespace_width = 10; + main_width = 96 + comp_offset; + } + } + + if (((symbol->symbology == BARCODE_UPCE) && (symbol->rows == 1)) || (symbol->symbology == BARCODE_UPCE_CC)) { + if(symbol->whitespace_width == 0) { + symbol->whitespace_width = 10; + main_width = 51 + comp_offset; + } + } + + latch = 0; + r = 0; + /* Isolate add-on text */ + if(is_extendable(symbol->symbology)) { + for(i = 0; i < ustrlen(local_text); i++) { + if (latch == 1) { + addon[r] = local_text[i]; + r++; + } + if (symbol->text[i] == '+') { + latch = 1; + } + } + } + addon[r] = '\0'; + + if(ustrlen(local_text) != 0) { + textoffset = 9; + } else { + textoffset = 0; + } + xoffset = symbol->border_width + symbol->whitespace_width; + yoffset = symbol->border_width; + image_width = 2 * (symbol->width + xoffset + xoffset); + image_height = 2 * (symbol->height + textoffset + yoffset + yoffset); + + if (!(pixelbuf = (char *) malloc(image_width * image_height))) { + printf("Insufficient memory for pixel buffer"); + return ZINT_ERROR_ENCODING_PROBLEM; + } else { + for(i = 0; i < (image_width * image_height); i++) { + *(pixelbuf + i) = '0'; + } + } + + if(((symbol->output_options & BARCODE_BOX) != 0) || ((symbol->output_options & BARCODE_BIND) != 0)) { + default_text_posn = image_height - 17; + } else { + default_text_posn = image_height - 17 - symbol->border_width - symbol->border_width; + } + + row_posn = textoffset + yoffset; + next_yposn = textoffset + yoffset; + row_height = 0; + + /* Plot the body of the symbol to the pixel buffer */ + for(r = 0; r < symbol->rows; r++) { + this_row = symbol->rows - r - 1; /* invert r otherwise plots upside down */ + row_posn += row_height; + plot_yposn = next_yposn; + if(symbol->row_height[this_row] == 0) { + row_height = large_bar_height; + } else { + row_height = symbol->row_height[this_row]; + } + next_yposn = (int)(row_posn + row_height); + plot_height = next_yposn - plot_yposn; + + i = 0; + if(module_is_set(symbol, this_row, 0)) { + latch = 1; + } else { + latch = 0; + } + + do { + block_width = 0; + do { + block_width++; + } while (module_is_set(symbol, this_row, i + block_width) == module_is_set(symbol, this_row, i)); + if((addon_latch == 0) && (r == 0) && (i > main_width)) { + plot_height = (int)(row_height - 5.0); + plot_yposn = (int)(row_posn - 5.0); + addon_text_posn = row_posn + row_height - 8.0; + addon_latch = 1; + } + if(latch == 1) { + /* a bar */ + draw_bar(pixelbuf, (i + xoffset) * 2, block_width * 2, plot_yposn * 2, plot_height * 2, image_width, image_height); + latch = 0; + } else { + /* a space */ + latch = 1; + } + i += block_width; + + } while (i < symbol->width); + } + + xoffset += comp_offset; + + if ((((symbol->symbology == BARCODE_EANX) && (symbol->rows == 1)) || (symbol->symbology == BARCODE_EANX_CC)) || (symbol->symbology == BARCODE_ISBNX)) { + /* guard bar extensions and text formatting for EAN8 and EAN13 */ + switch(ustrlen(local_text)) { + case 8: /* EAN-8 */ + case 11: + case 14: + draw_bar(pixelbuf, (0 + xoffset) * 2, 1 * 2, (4 + (int)yoffset) * 2, 5 * 2, image_width, image_height); + draw_bar(pixelbuf, (2 + xoffset) * 2, 1 * 2, (4 + (int)yoffset) * 2, 5 * 2, image_width, image_height); + draw_bar(pixelbuf, (32 + xoffset) * 2, 1 * 2, (4 + (int)yoffset) * 2, 5 * 2, image_width, image_height); + draw_bar(pixelbuf, (34 + xoffset) * 2, 1 * 2, (4 + (int)yoffset) * 2, 5 * 2, image_width, image_height); + draw_bar(pixelbuf, (64 + xoffset) * 2, 1 * 2, (4 + (int)yoffset) * 2, 5 * 2, image_width, image_height); + draw_bar(pixelbuf, (66 + xoffset) * 2, 1 * 2, (4 + (int)yoffset) * 2, 5 * 2, image_width, image_height); + for(i = 0; i < 4; i++) { + textpart[i] = symbol->text[i]; + } + textpart[4] = '\0'; + textpos = 2 * (17 + xoffset); + + draw_string(pixelbuf, textpart, textpos, default_text_posn, smalltext, image_width, image_height); + for(i = 0; i < 4; i++) { + textpart[i] = symbol->text[i + 4]; + } + textpart[4] = '\0'; + textpos = 2 * (50 + xoffset); + draw_string(pixelbuf, textpart, textpos, default_text_posn, smalltext, image_width, image_height); + textdone = 1; + switch(strlen(addon)) { + case 2: + textpos = 2 * (xoffset + 86); + draw_string(pixelbuf, addon, textpos, image_height - (addon_text_posn * 2) - 13, smalltext, image_width, image_height); + break; + case 5: + textpos = 2 * (xoffset + 100); + draw_string(pixelbuf, addon, textpos, image_height - (addon_text_posn * 2) - 13, smalltext, image_width, image_height); + break; + } + + break; + case 13: /* EAN 13 */ + case 16: + case 19: + draw_bar(pixelbuf, (0 + xoffset) * 2, 1 * 2, (4 + (int)yoffset) * 2, 5 * 2, image_width, image_height); + draw_bar(pixelbuf, (2 + xoffset) * 2, 1 * 2, (4 + (int)yoffset) * 2, 5 * 2, image_width, image_height); + draw_bar(pixelbuf, (46 + xoffset) * 2, 1 * 2, (4 + (int)yoffset) * 2, 5 * 2, image_width, image_height); + draw_bar(pixelbuf, (48 + xoffset) * 2, 1 * 2, (4 + (int)yoffset) * 2, 5 * 2, image_width, image_height); + draw_bar(pixelbuf, (92 + xoffset) * 2, 1 * 2, (4 + (int)yoffset) * 2, 5 * 2, image_width, image_height); + draw_bar(pixelbuf, (94 + xoffset) * 2, 1 * 2, (4 + (int)yoffset) * 2, 5 * 2, image_width, image_height); + + textpart[0] = symbol->text[0]; + textpart[1] = '\0'; + textpos = 2 * (-7 + xoffset); + draw_string(pixelbuf, textpart, textpos, default_text_posn, smalltext, image_width, image_height); + for(i = 0; i < 6; i++) { + textpart[i] = symbol->text[i + 1]; + } + textpart[6] = '\0'; + textpos = 2 * (24 + xoffset); + draw_string(pixelbuf, textpart, textpos, default_text_posn, smalltext, image_width, image_height); + for(i = 0; i < 6; i++) { + textpart[i] = symbol->text[i + 7]; + } + textpart[6] = '\0'; + textpos = 2 * (71 + xoffset); + draw_string(pixelbuf, textpart, textpos, default_text_posn, smalltext, image_width, image_height); + textdone = 1; + switch(strlen(addon)) { + case 2: + textpos = 2 * (xoffset + 114); + draw_string(pixelbuf, addon, textpos, image_height - (addon_text_posn * 2) - 13, smalltext, image_width, image_height); + break; + case 5: + textpos = 2 * (xoffset + 128); + draw_string(pixelbuf, addon, textpos, image_height - (addon_text_posn * 2) - 13, smalltext, image_width, image_height); + break; + } + break; + + } + } + + if (((symbol->symbology == BARCODE_UPCA) && (symbol->rows == 1)) || (symbol->symbology == BARCODE_UPCA_CC)) { + /* guard bar extensions and text formatting for UPCA */ + latch = 1; + + i = 0 + comp_offset; + do { + block_width = 0; + do { + block_width++; + } while (module_is_set(symbol, symbol->rows - 1, i + block_width) == module_is_set(symbol, symbol->rows - 1, i)); + if(latch == 1) { + /* a bar */ + draw_bar(pixelbuf, (i + xoffset - comp_offset) * 2, block_width * 2, (4 + (int)yoffset) * 2, 5 * 2, image_width, image_height); + latch = 0; + } else { + /* a space */ + latch = 1; + } + i += block_width; + } while (i < 11 + comp_offset); + draw_bar(pixelbuf, (46 + xoffset) * 2, 1 * 2, (4 + (int)yoffset) * 2, 5 * 2, image_width, image_height); + draw_bar(pixelbuf, (48 + xoffset) * 2, 1 * 2, (4 + (int)yoffset) * 2, 5 * 2, image_width, image_height); + latch = 1; + i = 85 + comp_offset; + do { + block_width = 0; + do { + block_width++; + } while (module_is_set(symbol, symbol->rows - 1, i + block_width) == module_is_set(symbol, symbol->rows - 1, i)); + if(latch == 1) { + /* a bar */ + draw_bar(pixelbuf, (i + xoffset - comp_offset) * 2, block_width * 2, (4 + (int)yoffset) * 2, 5 * 2, image_width, image_height); + latch = 0; + } else { + /* a space */ + latch = 1; + } + i += block_width; + } while (i < 96 + comp_offset); + textpart[0] = symbol->text[0]; + textpart[1] = '\0'; + textpos = 2 * (-5 + xoffset); + draw_string(pixelbuf, textpart, textpos, default_text_posn, smalltext, image_width, image_height); + for(i = 0; i < 5; i++) { + textpart[i] = symbol->text[i + 1]; + } + textpart[5] = '\0'; + textpos = 2 * (27 + xoffset); + draw_string(pixelbuf, textpart, textpos, default_text_posn, smalltext, image_width, image_height); + for(i = 0; i < 5; i++) { + textpart[i] = symbol->text[i + 6]; + } + textpart[6] = '\0'; + textpos = 2 * (68 + xoffset); + draw_string(pixelbuf, textpart, textpos, default_text_posn, smalltext, image_width, image_height); + textpart[0] = symbol->text[11]; + textpart[1] = '\0'; + textpos = 2 * (100 + xoffset); + draw_string(pixelbuf, textpart, textpos, default_text_posn, smalltext, image_width, image_height); + textdone = 1; + switch(strlen(addon)) { + case 2: + textpos = 2 * (xoffset + 116); + draw_string(pixelbuf, addon, textpos, image_height - (addon_text_posn * 2) - 13, smalltext, image_width, image_height); + break; + case 5: + textpos = 2 * (xoffset + 130); + draw_string(pixelbuf, addon, textpos, image_height - (addon_text_posn * 2) - 13, smalltext, image_width, image_height); + break; + } + + } + + if (((symbol->symbology == BARCODE_UPCE) && (symbol->rows == 1)) || (symbol->symbology == BARCODE_UPCE_CC)) { + /* guard bar extensions and text formatting for UPCE */ + draw_bar(pixelbuf, (0 + xoffset) * 2, 1 * 2, (4 + (int)yoffset) * 2, 5 * 2, image_width, image_height); + draw_bar(pixelbuf, (2 + xoffset) * 2, 1 * 2, (4 + (int)yoffset) * 2, 5 * 2, image_width, image_height); + draw_bar(pixelbuf, (46 + xoffset) * 2, 1 * 2, (4 + (int)yoffset) * 2, 5 * 2, image_width, image_height); + draw_bar(pixelbuf, (48 + xoffset) * 2, 1 * 2, (4 + (int)yoffset) * 2, 5 * 2, image_width, image_height); + draw_bar(pixelbuf, (50 + xoffset) * 2, 1 * 2, (4 + (int)yoffset) * 2, 5 * 2, image_width, image_height); + + textpart[0] = symbol->text[0]; + textpart[1] = '\0'; + textpos = 2 * (-5 + xoffset); + draw_string(pixelbuf, textpart, textpos, default_text_posn, smalltext, image_width, image_height); + for(i = 0; i < 6; i++) { + textpart[i] = symbol->text[i + 1]; + } + textpart[6] = '\0'; + textpos = 2 * (24 + xoffset); + draw_string(pixelbuf, textpart, textpos, default_text_posn, smalltext, image_width, image_height); + textpart[0] = symbol->text[7]; + textpart[1] = '\0'; + textpos = 2 * (55 + xoffset); + draw_string(pixelbuf, textpart, textpos, default_text_posn, smalltext, image_width, image_height); + textdone = 1; + switch(strlen(addon)) { + case 2: + textpos = 2 * (xoffset + 70); + draw_string(pixelbuf, addon, textpos, image_height - (addon_text_posn * 2) - 13, smalltext, image_width, image_height); + break; + case 5: + textpos = 2 * (xoffset + 84); + draw_string(pixelbuf, addon, textpos, image_height - (addon_text_posn * 2) - 13, smalltext, image_width, image_height); + break; + } + + } + + xoffset -= comp_offset; + + /* Put boundary bars or box around symbol */ + if(((symbol->output_options & BARCODE_BOX) != 0) || ((symbol->output_options & BARCODE_BIND) != 0)) { + /* boundary bars */ + draw_bar(pixelbuf, 0, (symbol->width + xoffset + xoffset) * 2, textoffset * 2, symbol->border_width * 2, image_width, image_height); + draw_bar(pixelbuf, 0, (symbol->width + xoffset + xoffset) * 2, (textoffset + symbol->height + symbol->border_width) * 2, symbol->border_width * 2, image_width, image_height); + if((symbol->output_options & BARCODE_BIND) != 0) { + if((symbol->rows > 1) && (is_stackable(symbol->symbology) == 1)) { + /* row binding */ + for(r = 1; r < symbol->rows; r++) { + draw_bar(pixelbuf, xoffset * 2, symbol->width * 2, ((r * row_height) + textoffset + yoffset - 1) * 2, 2 * 2, image_width, image_height); + } + } + } + } + + if((symbol->output_options & BARCODE_BOX) != 0) { + /* side bars */ + draw_bar(pixelbuf, 0, symbol->border_width * 2, textoffset * 2, (symbol->height + (2 * symbol->border_width)) * 2, image_width, image_height); + draw_bar(pixelbuf, (symbol->width + xoffset + xoffset - symbol->border_width) * 2, symbol->border_width * 2, textoffset * 2, (symbol->height + (2 * symbol->border_width)) * 2, image_width, image_height); + } + + /* Put the human readable text at the bottom */ + if((textdone == 0) && (ustrlen(local_text) != 0)) { + textpos = (image_width / 2); + draw_string(pixelbuf, (char*)local_text, textpos, default_text_posn, smalltext, image_width, image_height); + } + + error_number=png_to_file(symbol, image_height, image_width, pixelbuf, rotate_angle, data_type); + free(pixelbuf); + return error_number; +} + +#ifndef NO_PNG +int png_handle(struct zint_symbol *symbol, int rotate_angle) +{ + int error; + + if(symbol->symbology == BARCODE_MAXICODE) { + error = maxi_png_plot(symbol, rotate_angle, PNG_DATA); + } else { + + error = png_plot(symbol, rotate_angle, PNG_DATA); + } + + return error; +} +#endif /* NO_PNG */ + +int bmp_handle(struct zint_symbol *symbol, int rotate_angle) +{ + int error; + + if(symbol->symbology == BARCODE_MAXICODE) { + error = maxi_png_plot(symbol, rotate_angle, BMP_DATA); + } else { + error = png_plot(symbol, rotate_angle, BMP_DATA); + } + + return error; +} + diff --git a/backend/postal.c b/backend/postal.c index ea75f2ed..8ffd3cd1 100644 --- a/backend/postal.c +++ b/backend/postal.c @@ -79,10 +79,10 @@ int postnet(struct zint_symbol *symbol, unsigned char source[], char dest[], int if(length > 38) { strcpy(symbol->errtxt, "Input too long"); - return ERROR_TOO_LONG; + return ZINT_ERROR_TOO_LONG; } error_number = is_sane(NEON, source, length); - if(error_number == ERROR_INVALID_DATA) { + if(error_number == ZINT_ERROR_INVALID_DATA) { strcpy(symbol->errtxt, "Invalid characters in data"); return error_number; } @@ -150,10 +150,10 @@ int planet(struct zint_symbol *symbol, unsigned char source[], char dest[], int if(length > 38) { strcpy(symbol->errtxt, "Input too long"); - return ERROR_TOO_LONG; + return ZINT_ERROR_TOO_LONG; } error_number = is_sane(NEON, source, length); - if(error_number == ERROR_INVALID_DATA) { + if(error_number == ZINT_ERROR_INVALID_DATA) { strcpy(symbol->errtxt, "Invalid characters in data"); return error_number; } @@ -219,10 +219,10 @@ int korea_post(struct zint_symbol *symbol, unsigned char source[], int length) error_number = 0; if(length > 6) { strcpy(symbol->errtxt, "Input too long"); - return ERROR_TOO_LONG; + return ZINT_ERROR_TOO_LONG; } error_number = is_sane(NEON, source, length); - if(error_number == ERROR_INVALID_DATA) { + if(error_number == ZINT_ERROR_INVALID_DATA) { strcpy(symbol->errtxt, "Invalid characters in data"); return error_number; } @@ -257,7 +257,7 @@ int fim(struct zint_symbol *symbol, unsigned char source[], int length) if(length > 1) { strcpy(symbol->errtxt, "Input too long"); - return ERROR_TOO_LONG; + return ZINT_ERROR_TOO_LONG; } switch((char)source[0]) { @@ -279,7 +279,7 @@ int fim(struct zint_symbol *symbol, unsigned char source[], int length) break; default: strcpy(symbol->errtxt, "Invalid characters in data"); - return ERROR_INVALID_DATA; + return ZINT_ERROR_INVALID_DATA; break; } @@ -334,11 +334,11 @@ int royal_plot(struct zint_symbol *symbol, unsigned char source[], int length) if(length > 120) { strcpy(symbol->errtxt, "Input too long"); - return ERROR_TOO_LONG; + return ZINT_ERROR_TOO_LONG; } to_upper(source); error_number = is_sane(KRSET, source, length); - if(error_number == ERROR_INVALID_DATA) { + if(error_number == ZINT_ERROR_INVALID_DATA) { strcpy(symbol->errtxt, "Invalid characters in data"); return error_number; } @@ -384,11 +384,11 @@ int kix_code(struct zint_symbol *symbol, unsigned char source[], int length) if(length > 18) { strcpy(symbol->errtxt, "Input too long"); - return ERROR_TOO_LONG; + return ZINT_ERROR_TOO_LONG; } to_upper(source); error_number = is_sane(KRSET, source, length); - if(error_number == ERROR_INVALID_DATA) { + if(error_number == ZINT_ERROR_INVALID_DATA) { strcpy(symbol->errtxt, "Invalid characters in data"); return error_number; } @@ -441,12 +441,12 @@ int daft_code(struct zint_symbol *symbol, unsigned char source[], int length) error_number = 0; if(length > 50) { strcpy(symbol->errtxt, "Input too long"); - return ERROR_TOO_LONG; + return ZINT_ERROR_TOO_LONG; } to_upper((unsigned char*)source); error_number = is_sane(DAFTSET, (unsigned char*)source, length); - if(error_number == ERROR_INVALID_DATA) { + if(error_number == ZINT_ERROR_INVALID_DATA) { strcpy(symbol->errtxt, "Invalid characters in data"); return error_number; } @@ -493,10 +493,10 @@ int flattermarken(struct zint_symbol *symbol, unsigned char source[], int length if(length > 90) { strcpy(symbol->errtxt, "Input too long"); - return ERROR_TOO_LONG; + return ZINT_ERROR_TOO_LONG; } error_number = is_sane(NEON, source, length); - if(error_number == ERROR_INVALID_DATA) { + if(error_number == ZINT_ERROR_INVALID_DATA) { strcpy(symbol->errtxt, "Invalid characters in data"); return error_number; } @@ -533,7 +533,7 @@ int japan_post(struct zint_symbol *symbol, unsigned char source[], int length) to_upper((unsigned char*)local_source); error_number = is_sane(SHKASUTSET, (unsigned char*)local_source, length); - if(error_number == ERROR_INVALID_DATA) { + if(error_number == ZINT_ERROR_INVALID_DATA) { strcpy(symbol->errtxt, "Invalid characters in data"); return error_number; } diff --git a/backend/ps.c b/backend/ps.c index 34ced930..605d46cb 100644 --- a/backend/ps.c +++ b/backend/ps.c @@ -73,7 +73,7 @@ int ps_plot(struct zint_symbol *symbol) } if(feps == NULL) { strcpy(symbol->errtxt, "Could not open output file"); - return ERROR_FILE_ACCESS; + return ZINT_ERROR_FILE_ACCESS; } /* sort out colour options */ @@ -82,21 +82,21 @@ int ps_plot(struct zint_symbol *symbol) if(strlen(symbol->fgcolour) != 6) { strcpy(symbol->errtxt, "Malformed foreground colour target"); - return ERROR_INVALID_OPTION; + return ZINT_ERROR_INVALID_OPTION; } if(strlen(symbol->bgcolour) != 6) { strcpy(symbol->errtxt, "Malformed background colour target"); - return ERROR_INVALID_OPTION; + return ZINT_ERROR_INVALID_OPTION; } error_number = is_sane(SSET, (unsigned char*)symbol->fgcolour, strlen(symbol->fgcolour)); - if (error_number == ERROR_INVALID_DATA) { + if (error_number == ZINT_ERROR_INVALID_DATA) { strcpy(symbol->errtxt, "Malformed foreground colour target"); - return ERROR_INVALID_OPTION; + return ZINT_ERROR_INVALID_OPTION; } error_number = is_sane(SSET, (unsigned char*)symbol->bgcolour, strlen(symbol->bgcolour)); - if (error_number == ERROR_INVALID_DATA) { + if (error_number == ZINT_ERROR_INVALID_DATA) { strcpy(symbol->errtxt, "Malformed background colour target"); - return ERROR_INVALID_OPTION; + return ZINT_ERROR_INVALID_OPTION; } locale = setlocale(LC_ALL, "C"); diff --git a/backend/qr.c b/backend/qr.c index 1f06ac6f..acebec5b 100644 --- a/backend/qr.c +++ b/backend/qr.c @@ -1,2462 +1,2464 @@ -/* qr.c Handles QR Code */ - -/* - libzint - the open source barcode library - Copyright (C) 2009 Robin Stuart - - Redistribution and use in source and binary forms, with or without - modification, are permitted provided that the following conditions - are met: - - 1. Redistributions of source code must retain the above copyright - notice, this list of conditions and the following disclaimer. - 2. Redistributions in binary form must reproduce the above copyright - notice, this list of conditions and the following disclaimer in the - documentation and/or other materials provided with the distribution. - 3. Neither the name of the project nor the names of its contributors - may be used to endorse or promote products derived from this software - without specific prior written permission. - - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND - ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE - FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - SUCH DAMAGE. -*/ - -#include -#ifdef _MSC_VER -#include -#endif -#include "common.h" -#include -#include "sjis.h" -#include "qr.h" -#include "reedsol.h" -#include /* abs */ - -int in_alpha(int glyph) { - /* Returns true if input glyph is in the Alphanumeric set */ - int retval = 0; - char cglyph = (char) glyph; - - if((cglyph >= '0') && (cglyph <= '9')) { - retval = 1; - } - if((cglyph >= 'A') && (cglyph <= 'Z')) { - retval = 1; - } - switch (cglyph) { - case ' ': - case '$': - case '%': - case '*': - case '+': - case '-': - case '.': - case '/': - case ':': - retval = 1; - break; - } - - return retval; -} - -void define_mode(char mode[], int jisdata[], int length, int gs1) -{ - /* Values placed into mode[] are: K = Kanji, B = Binary, A = Alphanumeric, N = Numeric */ - int i, mlen, j; - - for(i = 0; i < length; i++) { - if(jisdata[i] > 0xff) { - mode[i] = 'K'; - } else { - mode[i] = 'B'; - if(in_alpha(jisdata[i])) { mode[i] = 'A'; } - if(gs1 && (jisdata[i] == '[')) { mode[i] = 'A'; } - if((jisdata[i] >= '0') && (jisdata[i] <= '9')) { mode[i] = 'N'; } - } - } - - /* If less than 6 numeric digits together then don't use numeric mode */ - for(i = 0; i < length; i++) { - if(mode[i] == 'N') { - if(((i != 0) && (mode[i - 1] != 'N')) || (i == 0)) { - mlen = 0; - while (((mlen + i) < length) && (mode[mlen + i] == 'N')) { - mlen++; - }; - if(mlen < 6) { - for(j = 0; j < mlen; j++) { - mode[i + j] = 'A'; - } - } - } - } - } - - /* If less than 4 alphanumeric characters together then don't use alphanumeric mode */ - for(i = 0; i < length; i++) { - if(mode[i] == 'A') { - if(((i != 0) && (mode[i - 1] != 'A')) || (i == 0)) { - mlen = 0; - while (((mlen + i) < length) && (mode[mlen + i] == 'A')) { - mlen++; - }; - if(mlen < 6) { - for(j = 0; j < mlen; j++) { - mode[i + j] = 'B'; - } - } - } - } - } -} - -int estimate_binary_length(char mode[], int length, int gs1) -{ - /* Make an estimate (worst case scenario) of how long the binary string will be */ - int i, count = 0; - char current = 0; - int a_count = 0; - int n_count = 0; - - if(gs1) { count += 4; } - - for(i = 0; i < length; i++) { - if(mode[i] != current) { - switch(mode[i]) { - case 'K': count += 12 + 4; current = 'K'; break; - case 'B': count += 16 + 4; current = 'B'; break; - case 'A': count += 13 + 4; current = 'A'; a_count = 0; break; - case 'N': count += 14 + 4; current = 'N'; n_count = 0; break; - } - } - - switch(mode[i]) { - case 'K': count += 13; break; - case 'B': count += 8; break; - case 'A': - a_count++; - if((a_count & 1) == 0) { - count += 5; // 11 in total - a_count = 0; - } - else - count += 6; - break; - case 'N': - n_count++; - if((n_count % 3) == 0) { - count += 3; // 10 in total - n_count = 0; - } - else if ((n_count & 1) == 0) - count += 3; // 7 in total - else - count += 4; - break; - } - } - - return count; -} - -static void qr_bscan(char *binary, int data, int h) -{ - for (; h; h>>=1) { - concat(binary, data & h ? "1" : "0"); - } -} - -void qr_binary(int datastream[], int version, int target_binlen, char mode[], int jisdata[], int length, int gs1, int est_binlen) -{ - /* Convert input data to a binary stream and add padding */ - int position = 0, debug = 0; - int short_data_block_length, i, scheme = 1; - char data_block, padbits; - int current_binlen, current_bytes; - int toggle, percent; - -#ifndef _MSC_VER - char binary[est_binlen + 12]; -#else - char* binary = (char *)_alloca(est_binlen + 12); -#endif - strcpy(binary, ""); - - if(gs1) { - concat(binary, "0101"); /* FNC1 */ - } - - if(version <= 9) { - scheme = 1; - } else if((version >= 10) && (version <= 26)) { - scheme = 2; - } else if(version >= 27) { - scheme = 3; - } - - if(debug) { - for(i = 0; i < length; i++) { - printf("%c", mode[i]); - } - printf("\n"); - } - - percent = 0; - - do { - data_block = mode[position]; - short_data_block_length = 0; - do { - short_data_block_length++; - } while (((short_data_block_length + position) < length) && (mode[position + short_data_block_length] == data_block)); - - switch(data_block) { - case 'K': - /* Kanji mode */ - /* Mode indicator */ - concat(binary, "1000"); - - /* Character count indicator */ - qr_bscan(binary, short_data_block_length, 0x20 << (scheme*2)); /* scheme = 1..3 */ - - if(debug) { printf("Kanji block (length %d)\n\t", short_data_block_length); } - - /* Character representation */ - for(i = 0; i < short_data_block_length; i++) { - int jis = jisdata[position + i]; - int msb, lsb, prod; - - if(jis > 0x9fff) { jis -= 0xc140; } - msb = (jis & 0xff00) >> 4; - lsb = (jis & 0xff); - prod = (msb * 0xc0) + lsb; - - qr_bscan(binary, prod, 0x1000); - - if(debug) { printf("0x%4X ", prod); } - } - - if(debug) { printf("\n"); } - - break; - case 'B': - /* Byte mode */ - /* Mode indicator */ - concat(binary, "0100"); - - /* Character count indicator */ - qr_bscan(binary, short_data_block_length, scheme > 1 ? 0x8000 : 0x80); /* scheme = 1..3 */ - - if(debug) { printf("Byte block (length %d)\n\t", short_data_block_length); } - - /* Character representation */ - for(i = 0; i < short_data_block_length; i++) { - int byte = jisdata[position + i]; - - if(gs1 && (byte == '[')) { - byte = 0x1d; /* FNC1 */ - } - - qr_bscan(binary, byte, 0x80); - - if(debug) { printf("0x%2X(%d) ", byte, byte); } - } - - if(debug) { printf("\n"); } - - break; - case 'A': - /* Alphanumeric mode */ - /* Mode indicator */ - concat(binary, "0010"); - - /* Character count indicator */ - qr_bscan(binary, short_data_block_length, 0x40 << (2 * scheme)); /* scheme = 1..3 */ - - if(debug) { printf("Alpha block (length %d)\n\t", short_data_block_length); } - - /* Character representation */ - i = 0; - while ( i < short_data_block_length ) { - int count; - int first = 0, second = 0, prod; - - if(percent == 0) { - if(gs1 && (jisdata[position + i] == '%')) { - first = posn(RHODIUM, '%'); - second = posn(RHODIUM, '%'); - count = 2; - prod = (first * 45) + second; - i++; - } else { - if(gs1 && (jisdata[position + i] == '[')) { - first = posn(RHODIUM, '%'); /* FNC1 */ - } else { - first = posn(RHODIUM, (char) jisdata[position + i]); - } - count = 1; - i++; - prod = first; - - if(i < short_data_block_length && mode[position + i] == 'A') { - if(gs1 && (jisdata[position + i] == '%')) { - second = posn(RHODIUM, '%'); - count = 2; - prod = (first * 45) + second; - percent = 1; - } else { - if(gs1 && (jisdata[position + i] == '[')) { - second = posn(RHODIUM, '%'); /* FNC1 */ - } else { - second = posn(RHODIUM, (char) jisdata[position + i]); - } - count = 2; - i++; - prod = (first * 45) + second; - } - } - } - } else { - first = posn(RHODIUM, '%'); - count = 1; - i++; - prod = first; - percent = 0; - - if(i < short_data_block_length && mode[position + i] == 'A') { - if(gs1 && (jisdata[position + i] == '%')) { - second = posn(RHODIUM, '%'); - count = 2; - prod = (first * 45) + second; - percent = 1; - } else { - if(gs1 && (jisdata[position + i] == '[')) { - second = posn(RHODIUM, '%'); /* FNC1 */ - } else { - second = posn(RHODIUM, (char) jisdata[position + i]); - } - count = 2; - i++; - prod = (first * 45) + second; - } - } - } - - qr_bscan(binary, prod, count == 2 ? 0x400 : 0x20); /* count = 1..2 */ - - if(debug) { printf("0x%4X ", prod); } - }; - - if(debug) { printf("\n"); } - - break; - case 'N': - /* Numeric mode */ - /* Mode indicator */ - concat(binary, "0001"); - - /* Character count indicator */ - qr_bscan(binary, short_data_block_length, 0x80 << (2 * scheme)); /* scheme = 1..3 */ - - if(debug) { printf("Number block (length %d)\n\t", short_data_block_length); } - - /* Character representation */ - i = 0; - while ( i < short_data_block_length ) { - int count; - int first = 0, second = 0, third = 0, prod; - - first = posn(NEON, (char) jisdata[position + i]); - count = 1; - prod = first; - - if(i + 1 < short_data_block_length && mode[position + i + 1] == 'N') { - second = posn(NEON, (char) jisdata[position + i + 1]); - count = 2; - prod = (prod * 10) + second; - - if(i + 2 < short_data_block_length && mode[position + i + 2] == 'N') { - third = posn(NEON, (char) jisdata[position + i + 2]); - count = 3; - prod = (prod * 10) + third; - } - } - - qr_bscan(binary, prod, 1 << (3 * count)); /* count = 1..3 */ - - if(debug) { printf("0x%4X (%d)", prod, prod); } - - i += count; - }; - - if(debug) { printf("\n"); } - - break; - } - - position += short_data_block_length; - } while (position < length) ; - - /* Terminator */ - concat(binary, "0000"); - - current_binlen = strlen(binary); - padbits = 8 - (current_binlen % 8); - if(padbits == 8) { padbits = 0; } - current_bytes = (current_binlen + padbits) / 8; - - /* Padding bits */ - for(i = 0; i < padbits; i++) { - concat(binary, "0"); - } - - /* Put data into 8-bit codewords */ - for(i = 0; i < current_bytes; i++) { - datastream[i] = 0x00; - if(binary[i * 8] == '1') { datastream[i] += 0x80; } - if(binary[i * 8 + 1] == '1') { datastream[i] += 0x40; } - if(binary[i * 8 + 2] == '1') { datastream[i] += 0x20; } - if(binary[i * 8 + 3] == '1') { datastream[i] += 0x10; } - if(binary[i * 8 + 4] == '1') { datastream[i] += 0x08; } - if(binary[i * 8 + 5] == '1') { datastream[i] += 0x04; } - if(binary[i * 8 + 6] == '1') { datastream[i] += 0x02; } - if(binary[i * 8 + 7] == '1') { datastream[i] += 0x01; } - } - - /* Add pad codewords */ - toggle = 0; - for(i = current_bytes; i < target_binlen; i++) { - if(toggle == 0) { - datastream[i] = 0xec; - toggle = 1; - } else { - datastream[i] = 0x11; - toggle = 0; - } - } - - if(debug) { - printf("Resulting codewords:\n\t"); - for(i = 0; i < target_binlen; i++) { - printf("0x%2X ", datastream[i]); - } - printf("\n"); - } -} - -void add_ecc(int fullstream[], int datastream[], int version, int data_cw, int blocks) -{ - /* Split data into blocks, add error correction and then interleave the blocks and error correction data */ - int ecc_cw = qr_total_codewords[version - 1] - 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; - - -#ifndef _MSC_VER - unsigned char data_block[short_data_block_length + 2]; - unsigned char ecc_block[ecc_block_length + 2]; - int interleaved_data[data_cw + 2]; - int interleaved_ecc[ecc_cw + 2]; -#else - unsigned char* data_block = (unsigned char *)_alloca(short_data_block_length + 2); - unsigned char* ecc_block = (unsigned char *)_alloca(ecc_block_length + 2); - int* interleaved_data = (int *)_alloca((data_cw + 2) * sizeof(int)); - int* interleaved_ecc = (int *)_alloca((ecc_cw + 2) * sizeof(int)); -#endif - - posn = 0; - - for(i = 0; i < blocks; i++) { - if(i < qty_short_blocks) { length_this_block = short_data_block_length; } else { length_this_block = short_data_block_length + 1; } - - for(j = 0; j < ecc_block_length; j++) { - ecc_block[j] = 0; - } - - for(j = 0; j < length_this_block; j++) { - data_block[j] = (unsigned char) datastream[posn + j]; - } - - rs_init_gf(0x11d); - rs_init_code(ecc_block_length, 0); - rs_encode(length_this_block, data_block, ecc_block); - rs_free(); - - if(debug) { - printf("Block %d: ", i + 1); - for(j = 0; j < length_this_block; j++) { - printf("%2X ", data_block[j]); - } - if(i < qty_short_blocks) { - printf(" "); - } - printf(" // "); - for(j = 0; j < ecc_block_length; j++) { - printf("%2X ", ecc_block[ecc_block_length - j - 1]); - } - printf("\n"); - } - - for(j = 0; j < short_data_block_length; j++) { - interleaved_data[(j * blocks) + i] = (int) data_block[j]; - } - - if(i >= qty_short_blocks){ - interleaved_data[(short_data_block_length * blocks) + (i - qty_short_blocks)] = (int) data_block[short_data_block_length]; - } - - for(j = 0; j < ecc_block_length; j++) { - interleaved_ecc[(j * blocks) + i] = (int) ecc_block[ecc_block_length - j - 1]; - } - - posn += length_this_block; - } - - for(j = 0; j < data_cw; j++) { - fullstream[j] = interleaved_data[j]; - } - for(j = 0; j < ecc_cw; j++) { - fullstream[j + data_cw] = interleaved_ecc[j]; - } - - if(debug) { - printf("\nData Stream: \n"); - for(j = 0; j < (data_cw + ecc_cw); j++) { - printf("%2X ", fullstream[j]); - } - printf("\n"); - } -} - -void place_finder(unsigned char grid[], int size, int x, int y) -{ - int xp, yp; - - int finder[] = { - 1, 1, 1, 1, 1, 1, 1, - 1, 0, 0, 0, 0, 0, 1, - 1, 0, 1, 1, 1, 0, 1, - 1, 0, 1, 1, 1, 0, 1, - 1, 0, 1, 1, 1, 0, 1, - 1, 0, 0, 0, 0, 0, 1, - 1, 1, 1, 1, 1, 1, 1 - }; - - for(xp = 0; xp < 7; xp++) { - for(yp = 0; yp < 7; yp++) { - if (finder[xp + (7 * yp)] == 1) { - grid[((yp + y) * size) + (xp + x)] = 0x11; - } else { - grid[((yp + y) * size) + (xp + x)] = 0x10; - } - } - } -} - -void place_align(unsigned char grid[], int size, int x, int y) -{ - int xp, yp; - - int alignment[] = { - 1, 1, 1, 1, 1, - 1, 0, 0, 0, 1, - 1, 0, 1, 0, 1, - 1, 0, 0, 0, 1, - 1, 1, 1, 1, 1 - }; - - x -= 2; - y -= 2; /* Input values represent centre of pattern */ - - for(xp = 0; xp < 5; xp++) { - for(yp = 0; yp < 5; yp++) { - if (alignment[xp + (5 * yp)] == 1) { - grid[((yp + y) * size) + (xp + x)] = 0x11; - } else { - grid[((yp + y) * size) + (xp + x)] = 0x10; - } - } - } -} - -void setup_grid(unsigned char* grid, int size, int version) -{ - int i, toggle = 1; - int loopsize, x, y, xcoord, ycoord; - - /* Add timing patterns */ - for(i = 0; i < size; i++) { - if(toggle == 1) { - grid[(6 * size) + i] = 0x21; - grid[(i * size) + 6] = 0x21; - toggle = 0; - } else { - grid[(6 * size) + i] = 0x20; - grid[(i * size) + 6] = 0x20; - toggle = 1; - } - } - - /* Add finder patterns */ - place_finder(grid, size, 0, 0); - place_finder(grid, size, 0, size - 7); - place_finder(grid, size, size - 7, 0); - - /* Add separators */ - for(i = 0; i < 7; i++) { - grid[(7 * size) + i] = 0x10; - grid[(i * size) + 7] = 0x10; - grid[(7 * size) + (size - 1 - i)] = 0x10; - grid[(i * size) + (size - 8)] = 0x10; - grid[((size - 8) * size) + i] = 0x10; - grid[((size - 1 - i) * size) + 7] = 0x10; - } - grid[(7 * size) + 7] = 0x10; - grid[(7 * size) + (size - 8)] = 0x10; - grid[((size - 8) * size) + 7] = 0x10; - - /* Add alignment patterns */ - if(version != 1) { - /* Version 1 does not have alignment patterns */ - - loopsize = qr_align_loopsize[version - 1]; - for(x = 0; x < loopsize; x++) { - for(y = 0; y < loopsize; y++) { - xcoord = qr_table_e1[((version - 2) * 7) + x]; - ycoord = qr_table_e1[((version - 2) * 7) + y]; - - if(!(grid[(ycoord * size) + xcoord] & 0x10)) { - place_align(grid, size, xcoord, ycoord); - } - } - } - } - - /* Reserve space for format information */ - for(i = 0; i < 8; i++) { - grid[(8 * size) + i] += 0x20; - grid[(i * size) + 8] += 0x20; - grid[(8 * size) + (size - 1 - i)] = 0x20; - grid[((size - 1 - i) * size) + 8] = 0x20; - } - grid[(8 * size) + 8] += 20; - grid[((size - 1 - 7) * size) + 8] = 0x21; /* Dark Module from Figure 25 */ - - /* Reserve space for version information */ - if(version >= 7) { - for(i = 0; i < 6; i++) { - grid[((size - 9) * size) + i] = 0x20; - grid[((size - 10) * size) + i] = 0x20; - grid[((size - 11) * size) + i] = 0x20; - grid[(i * size) + (size - 9)] = 0x20; - grid[(i * size) + (size - 10)] = 0x20; - grid[(i * size) + (size - 11)] = 0x20; - } - } -} - -int cwbit(int* datastream, int i) { - int word = i / 8; - int bit = i % 8; - int resultant = 0; - - switch(bit) { - case 0: if(datastream[word] & 0x80) { resultant = 1; } else { resultant = 0; } break; - case 1: if(datastream[word] & 0x40) { resultant = 1; } else { resultant = 0; } break; - case 2: if(datastream[word] & 0x20) { resultant = 1; } else { resultant = 0; } break; - case 3: if(datastream[word] & 0x10) { resultant = 1; } else { resultant = 0; } break; - case 4: if(datastream[word] & 0x08) { resultant = 1; } else { resultant = 0; } break; - case 5: if(datastream[word] & 0x04) { resultant = 1; } else { resultant = 0; } break; - case 6: if(datastream[word] & 0x02) { resultant = 1; } else { resultant = 0; } break; - case 7: if(datastream[word] & 0x01) { resultant = 1; } else { resultant = 0; } break; - } - - return resultant; -} - -void populate_grid(unsigned char* grid, int size, int* datastream, int cw) -{ - int direction = 1; /* up */ - int row = 0; /* right hand side */ - - int i, n, x, y; - - n = cw * 8; - y = size - 1; - i = 0; - do { - x = (size - 2) - (row * 2); - if(x < 6) - x--; /* skip over vertical timing pattern */ - - if(!(grid[(y * size) + (x + 1)] & 0xf0)) { - if (cwbit(datastream, i)) { - grid[(y * size) + (x + 1)] = 0x01; - } else { - grid[(y * size) + (x + 1)] = 0x00; - } - i++; - } - - if(i < n) { - if(!(grid[(y * size) + x] & 0xf0)) { - if (cwbit(datastream, i)) { - grid[(y * size) + x] = 0x01; - } else { - grid[(y * size) + x] = 0x00; - } - i++; - } - } - - if(direction) { y--; } else { y++; } - if(y == -1) { - /* reached the top */ - row++; - y = 0; - direction = 0; - } - if(y == size) { - /* reached the bottom */ - row++; - y = size - 1; - direction = 1; - } - } while (i < n); -} - -#ifdef ZINTLOG -int append_log(char log) -{ - FILE *file; - +/* qr.c Handles QR Code */ + +/* + libzint - the open source barcode library + Copyright (C) 2009 Robin Stuart + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + 3. Neither the name of the project nor the names of its contributors + may be used to endorse or promote products derived from this software + without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + SUCH DAMAGE. +*/ + +#include +#ifdef _MSC_VER +#include +#endif +#include "common.h" +#include +#include "sjis.h" +#include "qr.h" +#include "reedsol.h" +#include /* abs */ + +int in_alpha(int glyph) { + /* Returns true if input glyph is in the Alphanumeric set */ + int retval = 0; + char cglyph = (char) glyph; + + if((cglyph >= '0') && (cglyph <= '9')) { + retval = 1; + } + if((cglyph >= 'A') && (cglyph <= 'Z')) { + retval = 1; + } + switch (cglyph) { + case ' ': + case '$': + case '%': + case '*': + case '+': + case '-': + case '.': + case '/': + case ':': + retval = 1; + break; + } + + return retval; +} + +void define_mode(char mode[], int jisdata[], int length, int gs1) +{ + /* Values placed into mode[] are: K = Kanji, B = Binary, A = Alphanumeric, N = Numeric */ + int i, mlen, j; + + for(i = 0; i < length; i++) { + if(jisdata[i] > 0xff) { + mode[i] = 'K'; + } else { + mode[i] = 'B'; + if(in_alpha(jisdata[i])) { mode[i] = 'A'; } + if(gs1 && (jisdata[i] == '[')) { mode[i] = 'A'; } + if((jisdata[i] >= '0') && (jisdata[i] <= '9')) { mode[i] = 'N'; } + } + } + + /* If less than 6 numeric digits together then don't use numeric mode */ + for(i = 0; i < length; i++) { + if(mode[i] == 'N') { + if(((i != 0) && (mode[i - 1] != 'N')) || (i == 0)) { + mlen = 0; + while (((mlen + i) < length) && (mode[mlen + i] == 'N')) { + mlen++; + }; + if(mlen < 6) { + for(j = 0; j < mlen; j++) { + mode[i + j] = 'A'; + } + } + } + } + } + + /* If less than 4 alphanumeric characters together then don't use alphanumeric mode */ + for(i = 0; i < length; i++) { + if(mode[i] == 'A') { + if(((i != 0) && (mode[i - 1] != 'A')) || (i == 0)) { + mlen = 0; + while (((mlen + i) < length) && (mode[mlen + i] == 'A')) { + mlen++; + }; + if(mlen < 6) { + for(j = 0; j < mlen; j++) { + mode[i + j] = 'B'; + } + } + } + } + } +} + +int estimate_binary_length(char mode[], int length, int gs1) +{ + /* Make an estimate (worst case scenario) of how long the binary string will be */ + int i, count = 0; + char current = 0; + int a_count = 0; + int n_count = 0; + + if(gs1) { count += 4; } + + for(i = 0; i < length; i++) { + if(mode[i] != current) { + switch(mode[i]) { + case 'K': count += 12 + 4; current = 'K'; break; + case 'B': count += 16 + 4; current = 'B'; break; + case 'A': count += 13 + 4; current = 'A'; a_count = 0; break; + case 'N': count += 14 + 4; current = 'N'; n_count = 0; break; + } + } + + switch(mode[i]) { + case 'K': count += 13; break; + case 'B': count += 8; break; + case 'A': + a_count++; + if((a_count & 1) == 0) { + count += 5; // 11 in total + a_count = 0; + } + else + count += 6; + break; + case 'N': + n_count++; + if((n_count % 3) == 0) { + count += 3; // 10 in total + n_count = 0; + } + else if ((n_count & 1) == 0) + count += 3; // 7 in total + else + count += 4; + break; + } + } + + return count; +} + +static void qr_bscan(char *binary, int data, int h) +{ + for (; h; h>>=1) { + concat(binary, data & h ? "1" : "0"); + } +} + +void qr_binary(int datastream[], int version, int target_binlen, char mode[], int jisdata[], int length, int gs1, int est_binlen) +{ + /* Convert input data to a binary stream and add padding */ + int position = 0, debug = 0; + int short_data_block_length, i, scheme = 1; + char data_block, padbits; + int current_binlen, current_bytes; + int toggle, percent; + +#ifndef _MSC_VER + char binary[est_binlen + 12]; +#else + char* binary = (char *)_alloca(est_binlen + 12); +#endif + strcpy(binary, ""); + + if(gs1) { + concat(binary, "0101"); /* FNC1 */ + } + + if(version <= 9) { + scheme = 1; + } else if((version >= 10) && (version <= 26)) { + scheme = 2; + } else if(version >= 27) { + scheme = 3; + } + + if(debug) { + for(i = 0; i < length; i++) { + printf("%c", mode[i]); + } + printf("\n"); + } + + percent = 0; + + do { + data_block = mode[position]; + short_data_block_length = 0; + do { + short_data_block_length++; + } while (((short_data_block_length + position) < length) && (mode[position + short_data_block_length] == data_block)); + + switch(data_block) { + case 'K': + /* Kanji mode */ + /* Mode indicator */ + concat(binary, "1000"); + + /* Character count indicator */ + qr_bscan(binary, short_data_block_length, 0x20 << (scheme*2)); /* scheme = 1..3 */ + + if(debug) { printf("Kanji block (length %d)\n\t", short_data_block_length); } + + /* Character representation */ + for(i = 0; i < short_data_block_length; i++) { + int jis = jisdata[position + i]; + int msb, lsb, prod; + + if(jis > 0x9fff) { jis -= 0xc140; } + msb = (jis & 0xff00) >> 4; + lsb = (jis & 0xff); + prod = (msb * 0xc0) + lsb; + + qr_bscan(binary, prod, 0x1000); + + if(debug) { printf("0x%4X ", prod); } + } + + if(debug) { printf("\n"); } + + break; + case 'B': + /* Byte mode */ + /* Mode indicator */ + concat(binary, "0100"); + + /* Character count indicator */ + qr_bscan(binary, short_data_block_length, scheme > 1 ? 0x8000 : 0x80); /* scheme = 1..3 */ + + if(debug) { printf("Byte block (length %d)\n\t", short_data_block_length); } + + /* Character representation */ + for(i = 0; i < short_data_block_length; i++) { + int byte = jisdata[position + i]; + + if(gs1 && (byte == '[')) { + byte = 0x1d; /* FNC1 */ + } + + qr_bscan(binary, byte, 0x80); + + if(debug) { printf("0x%2X(%d) ", byte, byte); } + } + + if(debug) { printf("\n"); } + + break; + case 'A': + /* Alphanumeric mode */ + /* Mode indicator */ + concat(binary, "0010"); + + /* Character count indicator */ + qr_bscan(binary, short_data_block_length, 0x40 << (2 * scheme)); /* scheme = 1..3 */ + + if(debug) { printf("Alpha block (length %d)\n\t", short_data_block_length); } + + /* Character representation */ + i = 0; + while ( i < short_data_block_length ) { + int count; + int first = 0, second = 0, prod; + + if(percent == 0) { + if(gs1 && (jisdata[position + i] == '%')) { + first = posn(RHODIUM, '%'); + second = posn(RHODIUM, '%'); + count = 2; + prod = (first * 45) + second; + i++; + } else { + if(gs1 && (jisdata[position + i] == '[')) { + first = posn(RHODIUM, '%'); /* FNC1 */ + } else { + first = posn(RHODIUM, (char) jisdata[position + i]); + } + count = 1; + i++; + prod = first; + + if(i < short_data_block_length && mode[position + i] == 'A') { + if(gs1 && (jisdata[position + i] == '%')) { + second = posn(RHODIUM, '%'); + count = 2; + prod = (first * 45) + second; + percent = 1; + } else { + if(gs1 && (jisdata[position + i] == '[')) { + second = posn(RHODIUM, '%'); /* FNC1 */ + } else { + second = posn(RHODIUM, (char) jisdata[position + i]); + } + count = 2; + i++; + prod = (first * 45) + second; + } + } + } + } else { + first = posn(RHODIUM, '%'); + count = 1; + i++; + prod = first; + percent = 0; + + if(i < short_data_block_length && mode[position + i] == 'A') { + if(gs1 && (jisdata[position + i] == '%')) { + second = posn(RHODIUM, '%'); + count = 2; + prod = (first * 45) + second; + percent = 1; + } else { + if(gs1 && (jisdata[position + i] == '[')) { + second = posn(RHODIUM, '%'); /* FNC1 */ + } else { + second = posn(RHODIUM, (char) jisdata[position + i]); + } + count = 2; + i++; + prod = (first * 45) + second; + } + } + } + + qr_bscan(binary, prod, count == 2 ? 0x400 : 0x20); /* count = 1..2 */ + + if(debug) { printf("0x%4X ", prod); } + }; + + if(debug) { printf("\n"); } + + break; + case 'N': + /* Numeric mode */ + /* Mode indicator */ + concat(binary, "0001"); + + /* Character count indicator */ + qr_bscan(binary, short_data_block_length, 0x80 << (2 * scheme)); /* scheme = 1..3 */ + + if(debug) { printf("Number block (length %d)\n\t", short_data_block_length); } + + /* Character representation */ + i = 0; + while ( i < short_data_block_length ) { + int count; + int first = 0, second = 0, third = 0, prod; + + first = posn(NEON, (char) jisdata[position + i]); + count = 1; + prod = first; + + if(i + 1 < short_data_block_length && mode[position + i + 1] == 'N') { + second = posn(NEON, (char) jisdata[position + i + 1]); + count = 2; + prod = (prod * 10) + second; + + if(i + 2 < short_data_block_length && mode[position + i + 2] == 'N') { + third = posn(NEON, (char) jisdata[position + i + 2]); + count = 3; + prod = (prod * 10) + third; + } + } + + qr_bscan(binary, prod, 1 << (3 * count)); /* count = 1..3 */ + + if(debug) { printf("0x%4X (%d)", prod, prod); } + + i += count; + }; + + if(debug) { printf("\n"); } + + break; + } + + position += short_data_block_length; + } while (position < length) ; + + printf("Actual binary: %d\n", (int) strlen(binary)); + + /* Terminator */ + concat(binary, "0000"); + + current_binlen = strlen(binary); + padbits = 8 - (current_binlen % 8); + if(padbits == 8) { padbits = 0; } + current_bytes = (current_binlen + padbits) / 8; + + /* Padding bits */ + for(i = 0; i < padbits; i++) { + concat(binary, "0"); + } + + /* Put data into 8-bit codewords */ + for(i = 0; i < current_bytes; i++) { + datastream[i] = 0x00; + if(binary[i * 8] == '1') { datastream[i] += 0x80; } + if(binary[i * 8 + 1] == '1') { datastream[i] += 0x40; } + if(binary[i * 8 + 2] == '1') { datastream[i] += 0x20; } + if(binary[i * 8 + 3] == '1') { datastream[i] += 0x10; } + if(binary[i * 8 + 4] == '1') { datastream[i] += 0x08; } + if(binary[i * 8 + 5] == '1') { datastream[i] += 0x04; } + if(binary[i * 8 + 6] == '1') { datastream[i] += 0x02; } + if(binary[i * 8 + 7] == '1') { datastream[i] += 0x01; } + } + + /* Add pad codewords */ + toggle = 0; + for(i = current_bytes; i < target_binlen; i++) { + if(toggle == 0) { + datastream[i] = 0xec; + toggle = 1; + } else { + datastream[i] = 0x11; + toggle = 0; + } + } + + if(debug) { + printf("Resulting codewords:\n\t"); + for(i = 0; i < target_binlen; i++) { + printf("0x%2X ", datastream[i]); + } + printf("\n"); + } +} + +void add_ecc(int fullstream[], int datastream[], int version, int data_cw, int blocks) +{ + /* Split data into blocks, add error correction and then interleave the blocks and error correction data */ + int ecc_cw = qr_total_codewords[version - 1] - 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; + + +#ifndef _MSC_VER + unsigned char data_block[short_data_block_length + 2]; + unsigned char ecc_block[ecc_block_length + 2]; + int interleaved_data[data_cw + 2]; + int interleaved_ecc[ecc_cw + 2]; +#else + unsigned char* data_block = (unsigned char *)_alloca(short_data_block_length + 2); + unsigned char* ecc_block = (unsigned char *)_alloca(ecc_block_length + 2); + int* interleaved_data = (int *)_alloca((data_cw + 2) * sizeof(int)); + int* interleaved_ecc = (int *)_alloca((ecc_cw + 2) * sizeof(int)); +#endif + + posn = 0; + + for(i = 0; i < blocks; i++) { + if(i < qty_short_blocks) { length_this_block = short_data_block_length; } else { length_this_block = short_data_block_length + 1; } + + for(j = 0; j < ecc_block_length; j++) { + ecc_block[j] = 0; + } + + for(j = 0; j < length_this_block; j++) { + data_block[j] = (unsigned char) datastream[posn + j]; + } + + rs_init_gf(0x11d); + rs_init_code(ecc_block_length, 0); + rs_encode(length_this_block, data_block, ecc_block); + rs_free(); + + if(debug) { + printf("Block %d: ", i + 1); + for(j = 0; j < length_this_block; j++) { + printf("%2X ", data_block[j]); + } + if(i < qty_short_blocks) { + printf(" "); + } + printf(" // "); + for(j = 0; j < ecc_block_length; j++) { + printf("%2X ", ecc_block[ecc_block_length - j - 1]); + } + printf("\n"); + } + + for(j = 0; j < short_data_block_length; j++) { + interleaved_data[(j * blocks) + i] = (int) data_block[j]; + } + + if(i >= qty_short_blocks){ + interleaved_data[(short_data_block_length * blocks) + (i - qty_short_blocks)] = (int) data_block[short_data_block_length]; + } + + for(j = 0; j < ecc_block_length; j++) { + interleaved_ecc[(j * blocks) + i] = (int) ecc_block[ecc_block_length - j - 1]; + } + + posn += length_this_block; + } + + for(j = 0; j < data_cw; j++) { + fullstream[j] = interleaved_data[j]; + } + for(j = 0; j < ecc_cw; j++) { + fullstream[j + data_cw] = interleaved_ecc[j]; + } + + if(debug) { + printf("\nData Stream: \n"); + for(j = 0; j < (data_cw + ecc_cw); j++) { + printf("%2X ", fullstream[j]); + } + printf("\n"); + } +} + +void place_finder(unsigned char grid[], int size, int x, int y) +{ + int xp, yp; + + int finder[] = { + 1, 1, 1, 1, 1, 1, 1, + 1, 0, 0, 0, 0, 0, 1, + 1, 0, 1, 1, 1, 0, 1, + 1, 0, 1, 1, 1, 0, 1, + 1, 0, 1, 1, 1, 0, 1, + 1, 0, 0, 0, 0, 0, 1, + 1, 1, 1, 1, 1, 1, 1 + }; + + for(xp = 0; xp < 7; xp++) { + for(yp = 0; yp < 7; yp++) { + if (finder[xp + (7 * yp)] == 1) { + grid[((yp + y) * size) + (xp + x)] = 0x11; + } else { + grid[((yp + y) * size) + (xp + x)] = 0x10; + } + } + } +} + +void place_align(unsigned char grid[], int size, int x, int y) +{ + int xp, yp; + + int alignment[] = { + 1, 1, 1, 1, 1, + 1, 0, 0, 0, 1, + 1, 0, 1, 0, 1, + 1, 0, 0, 0, 1, + 1, 1, 1, 1, 1 + }; + + x -= 2; + y -= 2; /* Input values represent centre of pattern */ + + for(xp = 0; xp < 5; xp++) { + for(yp = 0; yp < 5; yp++) { + if (alignment[xp + (5 * yp)] == 1) { + grid[((yp + y) * size) + (xp + x)] = 0x11; + } else { + grid[((yp + y) * size) + (xp + x)] = 0x10; + } + } + } +} + +void setup_grid(unsigned char* grid, int size, int version) +{ + int i, toggle = 1; + int loopsize, x, y, xcoord, ycoord; + + /* Add timing patterns */ + for(i = 0; i < size; i++) { + if(toggle == 1) { + grid[(6 * size) + i] = 0x21; + grid[(i * size) + 6] = 0x21; + toggle = 0; + } else { + grid[(6 * size) + i] = 0x20; + grid[(i * size) + 6] = 0x20; + toggle = 1; + } + } + + /* Add finder patterns */ + place_finder(grid, size, 0, 0); + place_finder(grid, size, 0, size - 7); + place_finder(grid, size, size - 7, 0); + + /* Add separators */ + for(i = 0; i < 7; i++) { + grid[(7 * size) + i] = 0x10; + grid[(i * size) + 7] = 0x10; + grid[(7 * size) + (size - 1 - i)] = 0x10; + grid[(i * size) + (size - 8)] = 0x10; + grid[((size - 8) * size) + i] = 0x10; + grid[((size - 1 - i) * size) + 7] = 0x10; + } + grid[(7 * size) + 7] = 0x10; + grid[(7 * size) + (size - 8)] = 0x10; + grid[((size - 8) * size) + 7] = 0x10; + + /* Add alignment patterns */ + if(version != 1) { + /* Version 1 does not have alignment patterns */ + + loopsize = qr_align_loopsize[version - 1]; + for(x = 0; x < loopsize; x++) { + for(y = 0; y < loopsize; y++) { + xcoord = qr_table_e1[((version - 2) * 7) + x]; + ycoord = qr_table_e1[((version - 2) * 7) + y]; + + if(!(grid[(ycoord * size) + xcoord] & 0x10)) { + place_align(grid, size, xcoord, ycoord); + } + } + } + } + + /* Reserve space for format information */ + for(i = 0; i < 8; i++) { + grid[(8 * size) + i] += 0x20; + grid[(i * size) + 8] += 0x20; + grid[(8 * size) + (size - 1 - i)] = 0x20; + grid[((size - 1 - i) * size) + 8] = 0x20; + } + grid[(8 * size) + 8] += 20; + grid[((size - 1 - 7) * size) + 8] = 0x21; /* Dark Module from Figure 25 */ + + /* Reserve space for version information */ + if(version >= 7) { + for(i = 0; i < 6; i++) { + grid[((size - 9) * size) + i] = 0x20; + grid[((size - 10) * size) + i] = 0x20; + grid[((size - 11) * size) + i] = 0x20; + grid[(i * size) + (size - 9)] = 0x20; + grid[(i * size) + (size - 10)] = 0x20; + grid[(i * size) + (size - 11)] = 0x20; + } + } +} + +int cwbit(int* datastream, int i) { + int word = i / 8; + int bit = i % 8; + int resultant = 0; + + switch(bit) { + case 0: if(datastream[word] & 0x80) { resultant = 1; } else { resultant = 0; } break; + case 1: if(datastream[word] & 0x40) { resultant = 1; } else { resultant = 0; } break; + case 2: if(datastream[word] & 0x20) { resultant = 1; } else { resultant = 0; } break; + case 3: if(datastream[word] & 0x10) { resultant = 1; } else { resultant = 0; } break; + case 4: if(datastream[word] & 0x08) { resultant = 1; } else { resultant = 0; } break; + case 5: if(datastream[word] & 0x04) { resultant = 1; } else { resultant = 0; } break; + case 6: if(datastream[word] & 0x02) { resultant = 1; } else { resultant = 0; } break; + case 7: if(datastream[word] & 0x01) { resultant = 1; } else { resultant = 0; } break; + } + + return resultant; +} + +void populate_grid(unsigned char* grid, int size, int* datastream, int cw) +{ + int direction = 1; /* up */ + int row = 0; /* right hand side */ + + int i, n, x, y; + + n = cw * 8; + y = size - 1; + i = 0; + do { + x = (size - 2) - (row * 2); + if(x < 6) + x--; /* skip over vertical timing pattern */ + + if(!(grid[(y * size) + (x + 1)] & 0xf0)) { + if (cwbit(datastream, i)) { + grid[(y * size) + (x + 1)] = 0x01; + } else { + grid[(y * size) + (x + 1)] = 0x00; + } + i++; + } + + if(i < n) { + if(!(grid[(y * size) + x] & 0xf0)) { + if (cwbit(datastream, i)) { + grid[(y * size) + x] = 0x01; + } else { + grid[(y * size) + x] = 0x00; + } + i++; + } + } + + if(direction) { y--; } else { y++; } + if(y == -1) { + /* reached the top */ + row++; + y = 0; + direction = 0; + } + if(y == size) { + /* reached the bottom */ + row++; + y = size - 1; + direction = 1; + } + } while (i < n); +} + +#ifdef ZINTLOG +int append_log(char log) +{ + FILE *file; + file = fopen("zintlog.txt", "a+"); fprintf(file, "%c", log); - fclose(file); - return 0; -} - -int write_log(char log[]) -{ - FILE *file; - + fclose(file); + return 0; +} + +int write_log(char log[]) +{ + FILE *file; + file = fopen("zintlog.txt", "a+"); fprintf(file, log); /*writes*/ fprintf(file, "\r\n"); /*writes*/ - fclose(file); - return 0; -} -#endif - -int evaluate(unsigned char *grid, int size, int pattern) -{ - int x, y, block; - int result = 0; - char state; - int p; - int dark_mods; - int percentage, k, k2; + fclose(file); + return 0; +} +#endif + +int evaluate(unsigned char *grid, int size, int pattern) +{ + int x, y, block; + int result = 0; + char state; + int p; + int dark_mods; + int percentage, k, k2; int m; int smallest; #ifdef ZINTLOG int result_b = 0; char str[15]; #endif - -#ifndef _MSC_VER - char local[size * size]; -#else - char* local = (char *)_alloca((size * size) * sizeof(char)); -#endif - - -#ifdef ZINTLOG - write_log(""); - sprintf(str, "%d", pattern); - write_log(str); -#endif - - // all eight bitmask variants have been encoded in the 8 bits of the bytes that make up the grid array. select them for evaluation according to the desired pattern. - for(x = 0; x < size; x++) { - for(y = 0; y < size; y++) { - switch(pattern) { - case 0: if (grid[(y * size) + x] & 0x01) { local[(y * size) + x] = '1'; } else { local[(y * size) + x] = '0'; } break; - case 1: if (grid[(y * size) + x] & 0x02) { local[(y * size) + x] = '1'; } else { local[(y * size) + x] = '0'; } break; - case 2: if (grid[(y * size) + x] & 0x04) { local[(y * size) + x] = '1'; } else { local[(y * size) + x] = '0'; } break; - case 3: if (grid[(y * size) + x] & 0x08) { local[(y * size) + x] = '1'; } else { local[(y * size) + x] = '0'; } break; - case 4: if (grid[(y * size) + x] & 0x10) { local[(y * size) + x] = '1'; } else { local[(y * size) + x] = '0'; } break; - case 5: if (grid[(y * size) + x] & 0x20) { local[(y * size) + x] = '1'; } else { local[(y * size) + x] = '0'; } break; - case 6: if (grid[(y * size) + x] & 0x40) { local[(y * size) + x] = '1'; } else { local[(y * size) + x] = '0'; } break; - case 7: if (grid[(y * size) + x] & 0x80) { local[(y * size) + x] = '1'; } else { local[(y * size) + x] = '0'; } break; - } - } - } - -#ifdef ZINTLOG - //bitmask output - for(y = 0; y < size; y++) { - strcpy (str, ""); - for(x = 0; x < size; x++) { - state =local[(y * size) + x]; - append_log(state); - } - write_log(""); - } - write_log(""); -#endif - - /* Test 1: Adjacent modules in row/column in same colour */ - /* Vertical */ - for(x = 0; x < size; x++) { - state = local[x]; - block = 0; - for(y = 0; y < size; y++) { - if(local[(y * size) + x] == state) { - block++; - if(block ==5) - result += 3; - - if(block>5) - result +=1; - } else { - block=0; - } - } - } - - /* Horizontal */ - for(y = 0; y < size; y++) { - state = local[y * size]; - block = 0; - for(x = 0; x < size; x++) { - if(local[(y * size) + x] == state) { - block++; - if(block ==5) - result += 3; - - if(block>5) - result +=1; - } else { - block=0; - } - } - } - + +#ifndef _MSC_VER + char local[size * size]; +#else + char* local = (char *)_alloca((size * size) * sizeof(char)); +#endif + + +#ifdef ZINTLOG + write_log(""); + sprintf(str, "%d", pattern); + write_log(str); +#endif + + // all eight bitmask variants have been encoded in the 8 bits of the bytes that make up the grid array. select them for evaluation according to the desired pattern. + for(x = 0; x < size; x++) { + for(y = 0; y < size; y++) { + switch(pattern) { + case 0: if (grid[(y * size) + x] & 0x01) { local[(y * size) + x] = '1'; } else { local[(y * size) + x] = '0'; } break; + case 1: if (grid[(y * size) + x] & 0x02) { local[(y * size) + x] = '1'; } else { local[(y * size) + x] = '0'; } break; + case 2: if (grid[(y * size) + x] & 0x04) { local[(y * size) + x] = '1'; } else { local[(y * size) + x] = '0'; } break; + case 3: if (grid[(y * size) + x] & 0x08) { local[(y * size) + x] = '1'; } else { local[(y * size) + x] = '0'; } break; + case 4: if (grid[(y * size) + x] & 0x10) { local[(y * size) + x] = '1'; } else { local[(y * size) + x] = '0'; } break; + case 5: if (grid[(y * size) + x] & 0x20) { local[(y * size) + x] = '1'; } else { local[(y * size) + x] = '0'; } break; + case 6: if (grid[(y * size) + x] & 0x40) { local[(y * size) + x] = '1'; } else { local[(y * size) + x] = '0'; } break; + case 7: if (grid[(y * size) + x] & 0x80) { local[(y * size) + x] = '1'; } else { local[(y * size) + x] = '0'; } break; + } + } + } + +#ifdef ZINTLOG + //bitmask output + for(y = 0; y < size; y++) { + strcpy (str, ""); + for(x = 0; x < size; x++) { + state =local[(y * size) + x]; + append_log(state); + } + write_log(""); + } + write_log(""); +#endif + + /* Test 1: Adjacent modules in row/column in same colour */ + /* Vertical */ + for(x = 0; x < size; x++) { + state = local[x]; + block = 0; + for(y = 0; y < size; y++) { + if(local[(y * size) + x] == state) { + block++; + if(block ==5) + result += 3; + + if(block>5) + result +=1; + } else { + block=0; + } + } + } + + /* Horizontal */ + for(y = 0; y < size; y++) { + state = local[y * size]; + block = 0; + for(x = 0; x < size; x++) { + if(local[(y * size) + x] == state) { + block++; + if(block ==5) + result += 3; + + if(block>5) + result +=1; + } else { + block=0; + } + } + } + #ifdef ZINTLOG /* output Test 1 */ - sprintf(str, "%d", result); - result_b=result; - write_log(str); -#endif - - /* Test 2 fd02131114 */ - for(x = 0; x < size-1; x++) { - for(y = 0; y < (size - 7) -1; y++) { - // y + 1??? - if((local[((y + 1) * size) + x] == '1') && - (local[((y + 1) * size) + x+1] == '1') && - (local[(((y + 1)+1) * size) + x] == '1') && - (local[(((y + 1)+1) * size) + x+1] == '1') - ) { result += 3; } - - if((local[((y + 1) * size) + x] == '0') && - (local[((y + 1) * size) + x+1] == '0') && - (local[(((y + 1)+1) * size) + x] == '0') && - (local[(((y + 1)+1) * size) + x+1] == '0') - ) { result += 3; } - } - } - -#ifdef ZINTLOG - /* output Test 2 */ - sprintf(str, "%d", result-result_b); - result_b=result; - write_log(str); -#endif - - /* Test 3: fd02131114 */ - /*pattern 10111010000 */ - /* Vertical */ - for(x = 0; x < size; x++) { - for(y = 0; y < (size - 11); y++) { - p = 0; - if(local[(y * size) + x] == '1') { p += 1; } - if(local[((y + 1) * size) + x] == '0') { p += 1; } - if(local[((y + 2) * size) + x] == '1') { p += 1; } - if(local[((y + 3) * size) + x] == '1') { p += 1; } - if(local[((y + 4) * size) + x] == '1') { p += 1; } - if(local[((y + 5) * size) + x] == '0') { p += 1; } - if(local[((y + 6) * size) + x] == '1') { p += 1; } - if(local[((y + 7) * size) + x] == '0') { p += 1; } - if(local[((y + 8) * size) + x] == '0') { p += 1; } - if(local[((y + 9) * size) + x] == '0') { p += 1; } - if(local[((y + 10) * size) + x] == '0') { p += 1; } - if(p == 11) { - result += 40; - } - } - } - - /* Horizontal */ - for(y = 0; y < size; y++) { - for(x = 0; x < (size - 11); x++) { - p = 0; - if(local[(y * size) + x] == '1') { p += 1; } - if(local[(y * size) + x + 1] == '0') { p += 1; } - if(local[(y * size) + x + 2] == '1') { p += 1; } - if(local[(y * size) + x + 3] == '1') { p += 1; } - if(local[(y * size) + x + 4] == '1') { p += 1; } - if(local[(y * size) + x + 5] == '0') { p += 1; } - if(local[(y * size) + x + 6] == '1') { p += 1; } - if(local[(y * size) + x + 7] == '0') { p += 1; } - if(local[(y * size) + x + 8] == '0') { p += 1; } - if(local[(y * size) + x + 9] == '0') { p += 1; } - if(local[(y * size) + x + 10] == '0') { p += 1; } - if(p == 11) { - result += 40; - } - } - } - - /*pattern 00001011101 */ - /* Vertical */ - for(x = 0; x < size; x++) { - for(y = 0; y < (size - 11); y++) { - p = 0; - if(local[(y * size) + x] == '0') { p += 1; } - if(local[((y + 1) * size) + x] == '0') { p += 1; } - if(local[((y + 2) * size) + x] == '0') { p += 1; } - if(local[((y + 3) * size) + x] == '0') { p += 1; } - if(local[((y + 4) * size) + x] == '1') { p += 1; } - if(local[((y + 5) * size) + x] == '0') { p += 1; } - if(local[((y + 6) * size) + x] == '1') { p += 1; } - if(local[((y + 7) * size) + x] == '1') { p += 1; } - if(local[((y + 8) * size) + x] == '1') { p += 1; } - if(local[((y + 9) * size) + x] == '0') { p += 1; } - if(local[((y + 10) * size) + x] == '1') { p += 1; } - if(p == 11) { - result += 40; - } - } - } - - /* Horizontal */ - for(y = 0; y < size; y++) { - for(x = 0; x < (size - 11); x++) { - p = 0; - if(local[(y * size) + x] == '0') { p += 1; } - if(local[(y * size) + x + 1] == '0') { p += 1; } - if(local[(y * size) + x + 2] == '0') { p += 1; } - if(local[(y * size) + x + 3] == '0') { p += 1; } - if(local[(y * size) + x + 4] == '1') { p += 1; } - if(local[(y * size) + x + 5] == '0') { p += 1; } - if(local[(y * size) + x + 6] == '1') { p += 1; } - if(local[(y * size) + x + 7] == '1') { p += 1; } - if(local[(y * size) + x + 8] == '1') { p += 1; } - if(local[(y * size) + x + 9] == '0') { p += 1; } - if(local[(y * size) + x + 10] == '1') { p += 1; } - if(p == 11) { - result += 40; - } - } - } - -#ifdef ZINTLOG - /* output Test 3 */ - sprintf(str, "%d", result-result_b); - result_b=result; - write_log(str); -#endif - - /* Test 4: Proportion of dark modules in entire symbol */ - dark_mods = 0; - for(x = 0; x < size; x++) { - for(y = 0; y < size; y++) { - if(local[(y * size) + x] == '1') { - dark_mods++; - } - } - } - percentage = 100 * (dark_mods / (size * size)); - m=0; - for(x = 0; x < 100; x+=5) { - if(x> i) & 0x01 ? (0x01 >> pattern) : 0x00; - } - - for(i = 0; i < 8; i++) { - eval[(8 * size) + (size - i - 1)] = (seq >> i) & 0x01 ? (0x01 >> pattern) : 0x00; - } - - for(i = 0; i < 6; i++) { - eval[(8 * size) + (5 - i)] = (seq >> (i + 9)) & 0x01 ? (0x01 >> pattern) : 0x00; - } - - for(i = 0; i < 7; i++) { - eval[(((size - 7) + i) * size) + 8] = (seq >> (i + 8)) & 0x01 ? (0x01 >> pattern) : 0x00; - } - - eval[(7 * size) + 8] = (seq >> 6) & 0x01 ? (0x01 >> pattern) : 0x00; - eval[(8 * size) + 8] = (seq >> 7) & 0x01 ? (0x01 >> pattern) : 0x00; - eval[(8 * size) + 7] = (seq >> 8) & 0x01 ? (0x01 >> pattern) : 0x00; -} - -int apply_bitmask(unsigned char *grid, int size, int ecc_level) -{ - int x, y; - unsigned char p; - int pattern, penalty[8]; - int best_val, best_pattern; - int bit; - -#ifndef _MSC_VER - unsigned char mask[size * size]; - unsigned char eval[size * size]; -#else - unsigned char* mask = (unsigned char *)_alloca((size * size) * sizeof(unsigned char)); - unsigned char* eval = (unsigned char *)_alloca((size * size) * sizeof(unsigned char)); -#endif - - /* Perform data masking */ - for(x = 0; x < size; x++) { - for(y = 0; y < size; y++) { - mask[(y * size) + x] = 0x00; - - // all eight bitmask variants are encoded in the 8 bits of the bytes that make up the mask array. - if (!(grid[(y * size) + x] & 0xf0)) { // exclude areas not to be masked. - if(((y + x) & 1) == 0) { mask[(y * size) + x] += 0x01; } - if((y & 1) == 0) { mask[(y * size) + x] += 0x02; } - if((x % 3) == 0) { mask[(y * size) + x] += 0x04; } - if(((y + x) % 3) == 0) { mask[(y * size) + x] += 0x08; } - if((((y / 2) + (x / 3)) & 1) == 0) { mask[(y * size) + x] += 0x10; } - if((((y * x) & 1) + ((y * x) % 3)) == 0) { mask[(y * size) + x] += 0x20; } - if(((((y * x) & 1) + ((y * x) % 3)) & 1) == 0) { mask[(y * size) + x] += 0x40; } - if(((((y + x) & 1) + ((y * x) % 3)) & 1) == 0) { mask[(y * size) + x] += 0x80; } - } - } - } - - // apply data masks to grid, result in eval - for(x = 0; x < size; x++) { - for(y = 0; y < size; y++) { - if(grid[(y * size) + x] & 0x01) - { p = 0xff; } - else { p = 0x00; } - - eval[(y * size) + x] = mask[(y * size) + x] ^ p; - } - } - - - /* Evaluate result */ - for(pattern = 0; pattern < 8; pattern++) { - - add_format_info_eval(eval, size, ecc_level, pattern); - - penalty[pattern] = evaluate(eval, size, pattern); - } - - best_pattern = 0; - best_val = penalty[0]; - for(pattern = 1; pattern < 8; pattern++) { - if(penalty[pattern] < best_val) { - best_pattern = pattern; - best_val = penalty[pattern]; - } - } - -#ifdef ZINTLOG - char str[15]; - sprintf(str, "%d", best_val); - write_log("choosed pattern:"); - write_log(str); -#endif - - /* Apply mask */ - for(x = 0; x < size; x++) { - for(y = 0; y < size; y++) { - bit = 0; - switch(best_pattern) { - case 0: if(mask[(y * size) + x] & 0x01) { bit = 1; } break; - case 1: if(mask[(y * size) + x] & 0x02) { bit = 1; } break; - case 2: if(mask[(y * size) + x] & 0x04) { bit = 1; } break; - case 3: if(mask[(y * size) + x] & 0x08) { bit = 1; } break; - case 4: if(mask[(y * size) + x] & 0x10) { bit = 1; } break; - case 5: if(mask[(y * size) + x] & 0x20) { bit = 1; } break; - case 6: if(mask[(y * size) + x] & 0x40) { bit = 1; } break; - case 7: if(mask[(y * size) + x] & 0x80) { bit = 1; } break; - } - if(bit == 1) { - if(grid[(y * size) + x] & 0x01) { - grid[(y * size) + x] = 0x00; - } else { - grid[(y * size) + x] = 0x01; - } - } - } - } - - return best_pattern; -} - -void add_format_info(unsigned char *grid, int size, int ecc_level, int pattern) -{ - /* Add format information to grid */ - - int format = pattern; - unsigned int seq; - int i; - - switch(ecc_level) { - case LEVEL_L: format += 0x08; break; - case LEVEL_Q: format += 0x18; break; - case LEVEL_H: format += 0x10; break; - } - - seq = qr_annex_c[format]; - - for(i = 0; i < 6; i++) { - grid[(i * size) + 8] += (seq >> i) & 0x01; - } - - for(i = 0; i < 8; i++) { - grid[(8 * size) + (size - i - 1)] += (seq >> i) & 0x01; - } - - for(i = 0; i < 6; i++) { - grid[(8 * size) + (5 - i)] += (seq >> (i + 9)) & 0x01; - } - - for(i = 0; i < 7; i++) { - grid[(((size - 7) + i) * size) + 8] += (seq >> (i + 8)) & 0x01; - } - - grid[(7 * size) + 8] += (seq >> 6) & 0x01; - grid[(8 * size) + 8] += (seq >> 7) & 0x01; - grid[(8 * size) + 7] += (seq >> 8) & 0x01; -} - -void add_version_info(unsigned char *grid, int size, int version) -{ - /* Add version information */ - int i; - - long int version_data = qr_annex_d[version - 7]; - for(i = 0; i < 6; i++) { - grid[((size - 11) * size) + i] += (version_data >> (i * 3)) & 0x41; - grid[((size - 10) * size) + i] += (version_data >> ((i * 3) + 1)) & 0x41; - grid[((size - 9) * size) + i] += (version_data >> ((i * 3) + 2)) & 0x41; - grid[(i * size) + (size - 11)] += (version_data >> (i * 3)) & 0x41; - grid[(i * size) + (size - 10)] += (version_data >> ((i * 3) + 1)) & 0x41; - grid[(i * size) + (size - 9)] += (version_data >> ((i * 3) + 2)) & 0x41; - } -} - -int qr_code(struct zint_symbol *symbol, unsigned char source[], int length) -{ - int error_number, i, j, glyph, est_binlen; - int ecc_level, autosize, version, max_cw, target_binlen, blocks, size; - int bitmask, gs1; - -#ifndef _MSC_VER - int utfdata[length + 1]; - int jisdata[length + 1]; - char mode[length + 1]; -#else + sprintf(str, "%d", result); + result_b=result; + write_log(str); +#endif + + /* Test 2 fd02131114 */ + for(x = 0; x < size-1; x++) { + for(y = 0; y < (size - 7) -1; y++) { + // y + 1??? + if((local[((y + 1) * size) + x] == '1') && + (local[((y + 1) * size) + x+1] == '1') && + (local[(((y + 1)+1) * size) + x] == '1') && + (local[(((y + 1)+1) * size) + x+1] == '1') + ) { result += 3; } + + if((local[((y + 1) * size) + x] == '0') && + (local[((y + 1) * size) + x+1] == '0') && + (local[(((y + 1)+1) * size) + x] == '0') && + (local[(((y + 1)+1) * size) + x+1] == '0') + ) { result += 3; } + } + } + +#ifdef ZINTLOG + /* output Test 2 */ + sprintf(str, "%d", result-result_b); + result_b=result; + write_log(str); +#endif + + /* Test 3: fd02131114 */ + /*pattern 10111010000 */ + /* Vertical */ + for(x = 0; x < size; x++) { + for(y = 0; y < (size - 11); y++) { + p = 0; + if(local[(y * size) + x] == '1') { p += 1; } + if(local[((y + 1) * size) + x] == '0') { p += 1; } + if(local[((y + 2) * size) + x] == '1') { p += 1; } + if(local[((y + 3) * size) + x] == '1') { p += 1; } + if(local[((y + 4) * size) + x] == '1') { p += 1; } + if(local[((y + 5) * size) + x] == '0') { p += 1; } + if(local[((y + 6) * size) + x] == '1') { p += 1; } + if(local[((y + 7) * size) + x] == '0') { p += 1; } + if(local[((y + 8) * size) + x] == '0') { p += 1; } + if(local[((y + 9) * size) + x] == '0') { p += 1; } + if(local[((y + 10) * size) + x] == '0') { p += 1; } + if(p == 11) { + result += 40; + } + } + } + + /* Horizontal */ + for(y = 0; y < size; y++) { + for(x = 0; x < (size - 11); x++) { + p = 0; + if(local[(y * size) + x] == '1') { p += 1; } + if(local[(y * size) + x + 1] == '0') { p += 1; } + if(local[(y * size) + x + 2] == '1') { p += 1; } + if(local[(y * size) + x + 3] == '1') { p += 1; } + if(local[(y * size) + x + 4] == '1') { p += 1; } + if(local[(y * size) + x + 5] == '0') { p += 1; } + if(local[(y * size) + x + 6] == '1') { p += 1; } + if(local[(y * size) + x + 7] == '0') { p += 1; } + if(local[(y * size) + x + 8] == '0') { p += 1; } + if(local[(y * size) + x + 9] == '0') { p += 1; } + if(local[(y * size) + x + 10] == '0') { p += 1; } + if(p == 11) { + result += 40; + } + } + } + + /*pattern 00001011101 */ + /* Vertical */ + for(x = 0; x < size; x++) { + for(y = 0; y < (size - 11); y++) { + p = 0; + if(local[(y * size) + x] == '0') { p += 1; } + if(local[((y + 1) * size) + x] == '0') { p += 1; } + if(local[((y + 2) * size) + x] == '0') { p += 1; } + if(local[((y + 3) * size) + x] == '0') { p += 1; } + if(local[((y + 4) * size) + x] == '1') { p += 1; } + if(local[((y + 5) * size) + x] == '0') { p += 1; } + if(local[((y + 6) * size) + x] == '1') { p += 1; } + if(local[((y + 7) * size) + x] == '1') { p += 1; } + if(local[((y + 8) * size) + x] == '1') { p += 1; } + if(local[((y + 9) * size) + x] == '0') { p += 1; } + if(local[((y + 10) * size) + x] == '1') { p += 1; } + if(p == 11) { + result += 40; + } + } + } + + /* Horizontal */ + for(y = 0; y < size; y++) { + for(x = 0; x < (size - 11); x++) { + p = 0; + if(local[(y * size) + x] == '0') { p += 1; } + if(local[(y * size) + x + 1] == '0') { p += 1; } + if(local[(y * size) + x + 2] == '0') { p += 1; } + if(local[(y * size) + x + 3] == '0') { p += 1; } + if(local[(y * size) + x + 4] == '1') { p += 1; } + if(local[(y * size) + x + 5] == '0') { p += 1; } + if(local[(y * size) + x + 6] == '1') { p += 1; } + if(local[(y * size) + x + 7] == '1') { p += 1; } + if(local[(y * size) + x + 8] == '1') { p += 1; } + if(local[(y * size) + x + 9] == '0') { p += 1; } + if(local[(y * size) + x + 10] == '1') { p += 1; } + if(p == 11) { + result += 40; + } + } + } + +#ifdef ZINTLOG + /* output Test 3 */ + sprintf(str, "%d", result-result_b); + result_b=result; + write_log(str); +#endif + + /* Test 4: Proportion of dark modules in entire symbol */ + dark_mods = 0; + for(x = 0; x < size; x++) { + for(y = 0; y < size; y++) { + if(local[(y * size) + x] == '1') { + dark_mods++; + } + } + } + percentage = 100 * (dark_mods / (size * size)); + m=0; + for(x = 0; x < 100; x+=5) { + if(x> i) & 0x01 ? (0x01 >> pattern) : 0x00; + } + + for(i = 0; i < 8; i++) { + eval[(8 * size) + (size - i - 1)] = (seq >> i) & 0x01 ? (0x01 >> pattern) : 0x00; + } + + for(i = 0; i < 6; i++) { + eval[(8 * size) + (5 - i)] = (seq >> (i + 9)) & 0x01 ? (0x01 >> pattern) : 0x00; + } + + for(i = 0; i < 7; i++) { + eval[(((size - 7) + i) * size) + 8] = (seq >> (i + 8)) & 0x01 ? (0x01 >> pattern) : 0x00; + } + + eval[(7 * size) + 8] = (seq >> 6) & 0x01 ? (0x01 >> pattern) : 0x00; + eval[(8 * size) + 8] = (seq >> 7) & 0x01 ? (0x01 >> pattern) : 0x00; + eval[(8 * size) + 7] = (seq >> 8) & 0x01 ? (0x01 >> pattern) : 0x00; +} + +int apply_bitmask(unsigned char *grid, int size, int ecc_level) +{ + int x, y; + unsigned char p; + int pattern, penalty[8]; + int best_val, best_pattern; + int bit; + +#ifndef _MSC_VER + unsigned char mask[size * size]; + unsigned char eval[size * size]; +#else + unsigned char* mask = (unsigned char *)_alloca((size * size) * sizeof(unsigned char)); + unsigned char* eval = (unsigned char *)_alloca((size * size) * sizeof(unsigned char)); +#endif + + /* Perform data masking */ + for(x = 0; x < size; x++) { + for(y = 0; y < size; y++) { + mask[(y * size) + x] = 0x00; + + // all eight bitmask variants are encoded in the 8 bits of the bytes that make up the mask array. + if (!(grid[(y * size) + x] & 0xf0)) { // exclude areas not to be masked. + if(((y + x) & 1) == 0) { mask[(y * size) + x] += 0x01; } + if((y & 1) == 0) { mask[(y * size) + x] += 0x02; } + if((x % 3) == 0) { mask[(y * size) + x] += 0x04; } + if(((y + x) % 3) == 0) { mask[(y * size) + x] += 0x08; } + if((((y / 2) + (x / 3)) & 1) == 0) { mask[(y * size) + x] += 0x10; } + if((((y * x) & 1) + ((y * x) % 3)) == 0) { mask[(y * size) + x] += 0x20; } + if(((((y * x) & 1) + ((y * x) % 3)) & 1) == 0) { mask[(y * size) + x] += 0x40; } + if(((((y + x) & 1) + ((y * x) % 3)) & 1) == 0) { mask[(y * size) + x] += 0x80; } + } + } + } + + // apply data masks to grid, result in eval + for(x = 0; x < size; x++) { + for(y = 0; y < size; y++) { + if(grid[(y * size) + x] & 0x01) + { p = 0xff; } + else { p = 0x00; } + + eval[(y * size) + x] = mask[(y * size) + x] ^ p; + } + } + + + /* Evaluate result */ + for(pattern = 0; pattern < 8; pattern++) { + + add_format_info_eval(eval, size, ecc_level, pattern); + + penalty[pattern] = evaluate(eval, size, pattern); + } + + best_pattern = 0; + best_val = penalty[0]; + for(pattern = 1; pattern < 8; pattern++) { + if(penalty[pattern] < best_val) { + best_pattern = pattern; + best_val = penalty[pattern]; + } + } + +#ifdef ZINTLOG + char str[15]; + sprintf(str, "%d", best_val); + write_log("choosed pattern:"); + write_log(str); +#endif + + /* Apply mask */ + for(x = 0; x < size; x++) { + for(y = 0; y < size; y++) { + bit = 0; + switch(best_pattern) { + case 0: if(mask[(y * size) + x] & 0x01) { bit = 1; } break; + case 1: if(mask[(y * size) + x] & 0x02) { bit = 1; } break; + case 2: if(mask[(y * size) + x] & 0x04) { bit = 1; } break; + case 3: if(mask[(y * size) + x] & 0x08) { bit = 1; } break; + case 4: if(mask[(y * size) + x] & 0x10) { bit = 1; } break; + case 5: if(mask[(y * size) + x] & 0x20) { bit = 1; } break; + case 6: if(mask[(y * size) + x] & 0x40) { bit = 1; } break; + case 7: if(mask[(y * size) + x] & 0x80) { bit = 1; } break; + } + if(bit == 1) { + if(grid[(y * size) + x] & 0x01) { + grid[(y * size) + x] = 0x00; + } else { + grid[(y * size) + x] = 0x01; + } + } + } + } + + return best_pattern; +} + +void add_format_info(unsigned char *grid, int size, int ecc_level, int pattern) +{ + /* Add format information to grid */ + + int format = pattern; + unsigned int seq; + int i; + + switch(ecc_level) { + case LEVEL_L: format += 0x08; break; + case LEVEL_Q: format += 0x18; break; + case LEVEL_H: format += 0x10; break; + } + + seq = qr_annex_c[format]; + + for(i = 0; i < 6; i++) { + grid[(i * size) + 8] += (seq >> i) & 0x01; + } + + for(i = 0; i < 8; i++) { + grid[(8 * size) + (size - i - 1)] += (seq >> i) & 0x01; + } + + for(i = 0; i < 6; i++) { + grid[(8 * size) + (5 - i)] += (seq >> (i + 9)) & 0x01; + } + + for(i = 0; i < 7; i++) { + grid[(((size - 7) + i) * size) + 8] += (seq >> (i + 8)) & 0x01; + } + + grid[(7 * size) + 8] += (seq >> 6) & 0x01; + grid[(8 * size) + 8] += (seq >> 7) & 0x01; + grid[(8 * size) + 7] += (seq >> 8) & 0x01; +} + +void add_version_info(unsigned char *grid, int size, int version) +{ + /* Add version information */ + int i; + + long int version_data = qr_annex_d[version - 7]; + for(i = 0; i < 6; i++) { + grid[((size - 11) * size) + i] += (version_data >> (i * 3)) & 0x41; + grid[((size - 10) * size) + i] += (version_data >> ((i * 3) + 1)) & 0x41; + grid[((size - 9) * size) + i] += (version_data >> ((i * 3) + 2)) & 0x41; + grid[(i * size) + (size - 11)] += (version_data >> (i * 3)) & 0x41; + grid[(i * size) + (size - 10)] += (version_data >> ((i * 3) + 1)) & 0x41; + grid[(i * size) + (size - 9)] += (version_data >> ((i * 3) + 2)) & 0x41; + } +} + +int qr_code(struct zint_symbol *symbol, unsigned char source[], int length) +{ + int error_number, i, j, glyph, est_binlen; + int ecc_level, autosize, version, max_cw, target_binlen, blocks, size; + int bitmask, gs1; + +#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); - - switch(symbol->input_mode) { - case DATA_MODE: - for(i = 0; i < length; i++) { - jisdata[i] = (int)source[i]; - } - break; - default: - /* Convert Unicode input to Shift-JIS */ - 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 { - j = 0; - glyph = 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, "Invalid character in input data"); - return ERROR_INVALID_DATA; - } - jisdata[i] = glyph; - } - } - break; - } - - define_mode(mode, jisdata, length, gs1); - est_binlen = estimate_binary_length(mode, length, gs1); - - ecc_level = LEVEL_L; - max_cw = 2956; - if((symbol->option_1 >= 1) && (symbol->option_1 <= 4)) { - switch (symbol->option_1) { - case 1: ecc_level = LEVEL_L; max_cw = 2956; break; - case 2: ecc_level = LEVEL_M; max_cw = 2334; break; - case 3: ecc_level = LEVEL_Q; max_cw = 1666; break; - case 4: ecc_level = LEVEL_H; max_cw = 1276; break; - } - } - - if(est_binlen > (8 * max_cw)) { - strcpy(symbol->errtxt, "Input too long for selected error correction level"); - return ERROR_TOO_LONG; - } - - autosize = 40; - for(i = 39; i >= 0; i--) { - switch(ecc_level) { - case LEVEL_L: - if ((8 * qr_data_codewords_L[i]) >= est_binlen) { - autosize = i + 1; - } - break; - case LEVEL_M: - if ((8 * qr_data_codewords_M[i]) >= est_binlen) { - autosize = i + 1; - } - break; - case LEVEL_Q: - if ((8 * qr_data_codewords_Q[i]) >= est_binlen) { - autosize = i + 1; - } - break; - case LEVEL_H: - if ((8 * qr_data_codewords_H[i]) >= est_binlen) { - autosize = i + 1; - } - break; - } - } - - if((symbol->option_2 >= 1) && (symbol->option_2 <= 40)) { - if (symbol->option_2 > autosize) { - version = symbol->option_2; - } else { - version = autosize; - } - } else { - version = autosize; - } - - /* Ensure maxium error correction capacity */ - if(est_binlen <= qr_data_codewords_M[version - 1]) { ecc_level = LEVEL_M; } - if(est_binlen <= qr_data_codewords_Q[version - 1]) { ecc_level = LEVEL_Q; } - if(est_binlen <= qr_data_codewords_H[version - 1]) { ecc_level = LEVEL_H; } - - target_binlen = 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]; blocks = qr_blocks_M[version - 1]; break; - case LEVEL_Q: target_binlen = qr_data_codewords_Q[version - 1]; blocks = qr_blocks_Q[version - 1]; break; - case LEVEL_H: target_binlen = qr_data_codewords_H[version - 1]; blocks = qr_blocks_H[version - 1]; break; - } - -#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, gs1, est_binlen); - 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]); - - if(version >= 7) { - add_version_info(grid, size, version); - } - - 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; -} - -/* NOTE: From this point forward concerns Micro QR Code only */ - -int micro_qr_intermediate(char binary[], int jisdata[], char mode[], int length, int *kanji_used, int *alphanum_used, int *byte_used) -{ - /* Convert input data to an "intermediate stage" where data is binary encoded but - control information is not */ - int position = 0, debug = 0; - int short_data_block_length, i; - char data_block; - char buffer[2]; - - strcpy(binary, ""); - - if(debug) { - for(i = 0; i < length; i++) { - printf("%c", mode[i]); - } - printf("\n"); - } - - do { - if(strlen(binary) > 128) { - return ERROR_TOO_LONG; - } - - data_block = mode[position]; - short_data_block_length = 0; - do { - short_data_block_length++; - } while (((short_data_block_length + position) < length) && (mode[position + short_data_block_length] == data_block)); - - switch(data_block) { - case 'K': - /* Kanji mode */ - /* Mode indicator */ - concat(binary, "K"); - *kanji_used = 1; - - /* Character count indicator */ - buffer[0] = short_data_block_length; - buffer[1] = '\0'; - concat(binary, buffer); - - if(debug) { printf("Kanji block (length %d)\n\t", short_data_block_length); } - - /* Character representation */ - for(i = 0; i < short_data_block_length; i++) { - int jis = jisdata[position + i]; - int msb, lsb, prod; - - if(jis > 0x9fff) { jis -= 0xc140; } - msb = (jis & 0xff00) >> 4; - lsb = (jis & 0xff); - prod = (msb * 0xc0) + lsb; - - qr_bscan(binary, prod, 0x1000); - - if(debug) { printf("0x%4X ", prod); } - - if(strlen(binary) > 128) { - return ERROR_TOO_LONG; - } - } - - if(debug) { printf("\n"); } - - break; - case 'B': - /* Byte mode */ - /* Mode indicator */ - concat(binary, "B"); - *byte_used = 1; - - /* Character count indicator */ - buffer[0] = short_data_block_length; - buffer[1] = '\0'; - concat(binary, buffer); - - if(debug) { printf("Byte block (length %d)\n\t", short_data_block_length); } - - /* Character representation */ - for(i = 0; i < short_data_block_length; i++) { - int byte = jisdata[position + i]; - - qr_bscan(binary, byte, 0x80); - - if(debug) { printf("0x%4X ", byte); } - - if(strlen(binary) > 128) { - return ERROR_TOO_LONG; - } - } - - if(debug) { printf("\n"); } - - break; - case 'A': - /* Alphanumeric mode */ - /* Mode indicator */ - concat(binary, "A"); - *alphanum_used = 1; - - /* Character count indicator */ - buffer[0] = short_data_block_length; - buffer[1] = '\0'; - concat(binary, buffer); - - if(debug) { printf("Alpha block (length %d)\n\t", short_data_block_length); } - - /* Character representation */ - i = 0; - while ( i < short_data_block_length ) { - int count; - int first = 0, second = 0, prod; - - first = posn(RHODIUM, (char) jisdata[position + i]); - count = 1; - prod = first; - - if(i + 1 < short_data_block_length && mode[position + i + 1] == 'A') { - second = posn(RHODIUM, (char) jisdata[position + i + 1]); - count = 2; - prod = (first * 45) + second; - } - - qr_bscan(binary, prod, 1 << (5 * count)); /* count = 1..2 */ - - if(debug) { printf("0x%4X ", prod); } - - if(strlen(binary) > 128) { - return ERROR_TOO_LONG; - } - - i += 2; - }; - - if(debug) { printf("\n"); } - - break; - case 'N': - /* Numeric mode */ - /* Mode indicator */ - concat(binary, "N"); - - /* Character count indicator */ - buffer[0] = short_data_block_length; - buffer[1] = '\0'; - concat(binary, buffer); - - if(debug) { printf("Number block (length %d)\n\t", short_data_block_length); } - - /* Character representation */ - i = 0; - while ( i < short_data_block_length ) { - int count; - int first = 0, second = 0, third = 0, prod; - - first = posn(NEON, (char) jisdata[position + i]); - count = 1; - prod = first; - - if(i + 1 < short_data_block_length && mode[position + i + 1] == 'N') { - second = posn(NEON, (char) jisdata[position + i + 1]); - count = 2; - prod = (prod * 10) + second; - } - - if(i + 2 < short_data_block_length && mode[position + i + 2] == 'N') { - third = posn(NEON, (char) jisdata[position + i + 2]); - count = 3; - prod = (prod * 10) + third; - } - - qr_bscan(binary, prod, 1 << (3 * count)); /* count = 1..3 */ - - if(debug) { printf("0x%4X (%d)", prod, prod); } - - if(strlen(binary) > 128) { - return ERROR_TOO_LONG; - } - - i += 3; - }; - - if(debug) { printf("\n"); } - - break; - } - - position += short_data_block_length; - } while (position < length - 1) ; - - return 0; -} - -void get_bitlength(int count[], char stream[]) { - int length, i; - - length = strlen(stream); - - for(i = 0; i < 4; i++) { - count[i] = 0; - } - - i = 0; - do { - if((stream[i] == '0') || (stream[i] == '1')) { - count[0]++; - count[1]++; - count[2]++; - count[3]++; - i++; - } else { - switch(stream[i]) { - case 'K': - count[2] += 5; - count[3] += 7; - i += 2; - break; - case 'B': - count[2] += 6; - count[3] += 8; - i += 2; - break; - case 'A': - count[1] += 4; - count[2] += 6; - count[3] += 8; - i += 2; - break; - case 'N': - count[0] += 3; - count[1] += 5; - count[2] += 7; - count[3] += 9; - i += 2; - break; - } - } - } while (i < length); -} - -void microqr_expand_binary(char binary_stream[], char full_stream[], int version) -{ - int i, length; - - length = strlen(binary_stream); - - i = 0; - do { - switch(binary_stream[i]) { - case '1': concat(full_stream, "1"); i++; break; - case '0': concat(full_stream, "0"); i++; break; - case 'N': - /* Numeric Mode */ - /* Mode indicator */ - switch(version) { - case 1: concat(full_stream, "0"); break; - case 2: concat(full_stream, "00"); break; - case 3: concat(full_stream, "000"); break; - } - - /* Character count indicator */ - qr_bscan(full_stream, binary_stream[i + 1], 4 << version); /* version = 0..3 */ - - i += 2; - break; - case 'A': - /* Alphanumeric Mode */ - /* Mode indicator */ - switch(version) { - case 1: concat(full_stream, "1"); break; - case 2: concat(full_stream, "01"); break; - case 3: concat(full_stream, "001"); break; - } - - /* Character count indicator */ - qr_bscan(full_stream, binary_stream[i + 1], 2 << version); /* version = 1..3 */ - - i += 2; - break; - case 'B': - /* Byte Mode */ - /* Mode indicator */ - switch(version) { - case 2: concat(full_stream, "10"); break; - case 3: concat(full_stream, "010"); break; - } - - /* Character count indicator */ - qr_bscan(full_stream, binary_stream[i + 1], 2 << version); /* version = 2..3 */ - - i += 2; - break; - case 'K': - /* Kanji Mode */ - /* Mode indicator */ - switch(version) { - case 2: concat(full_stream, "11"); break; - case 3: concat(full_stream, "011"); break; - } - - /* Character count indicator */ - qr_bscan(full_stream, binary_stream[i + 1], 1 << version); /* version = 2..3 */ - - i += 2; - break; - } - - } while (i < length); -} - -void micro_qr_m1(char binary_data[]) -{ - int i, latch; - int bits_total, bits_left, remainder; - int data_codewords, ecc_codewords; - unsigned char data_blocks[4], ecc_blocks[3]; - - bits_total = 20; - latch = 0; - - /* Add terminator */ - bits_left = bits_total - strlen(binary_data); - if(bits_left <= 3) { - for(i = 0; i < bits_left; i++) { - concat(binary_data, "0"); - } - latch = 1; - } else { - concat(binary_data, "000"); - } - - if(latch == 0) { - /* Manage last (4-bit) block */ - bits_left = bits_total - strlen(binary_data); - if(bits_left <= 4) { - for(i = 0; i < bits_left; i++) { - concat(binary_data, "0"); - } - latch = 1; - } - } - - if(latch == 0) { - /* Complete current byte */ - remainder = 8 - (strlen(binary_data) % 8); - if(remainder == 8) { remainder = 0; } - for(i = 0; i < remainder; i++) { - concat(binary_data, "0"); - } - - /* Add padding */ - bits_left = bits_total - strlen(binary_data); - if(bits_left > 4) { - remainder = (bits_left - 4) / 8; - for(i = 0; i < remainder; i++) { - concat(binary_data, i & 1 ? "00010001" : "11101100"); - } - } - concat(binary_data, "0000"); - } - - data_codewords = 3; - ecc_codewords = 2; - - /* Copy data into codewords */ - for(i = 0; i < (data_codewords - 1); i++) { - data_blocks[i] = 0; - if(binary_data[i * 8] == '1') { data_blocks[i] += 0x80; } - if(binary_data[(i * 8) + 1] == '1') { data_blocks[i] += 0x40; } - if(binary_data[(i * 8) + 2] == '1') { data_blocks[i] += 0x20; } - if(binary_data[(i * 8) + 3] == '1') { data_blocks[i] += 0x10; } - if(binary_data[(i * 8) + 4] == '1') { data_blocks[i] += 0x08; } - if(binary_data[(i * 8) + 5] == '1') { data_blocks[i] += 0x04; } - if(binary_data[(i * 8) + 6] == '1') { data_blocks[i] += 0x02; } - if(binary_data[(i * 8) + 7] == '1') { data_blocks[i] += 0x01; } - } - data_blocks[2] = 0; - if(binary_data[16] == '1') { data_blocks[2] += 0x08; } - if(binary_data[17] == '1') { data_blocks[2] += 0x04; } - if(binary_data[18] == '1') { data_blocks[2] += 0x02; } - if(binary_data[19] == '1') { data_blocks[2] += 0x01; } - - /* Calculate Reed-Solomon error codewords */ - rs_init_gf(0x11d); - rs_init_code(ecc_codewords, 0); - rs_encode(data_codewords,data_blocks,ecc_blocks); - rs_free(); - - /* Add Reed-Solomon codewords to binary data */ - for(i = 0; i < ecc_codewords; i++) { - qr_bscan(binary_data, ecc_blocks[ecc_codewords - i - 1], 0x80); - } -} - -void micro_qr_m2(char binary_data[], int ecc_mode) -{ - int i, latch; - int bits_total, bits_left, remainder; - int data_codewords, ecc_codewords; - unsigned char data_blocks[6], ecc_blocks[7]; - - latch = 0; - - if(ecc_mode == LEVEL_L) { bits_total = 40; } - if(ecc_mode == LEVEL_M) { bits_total = 32; } - - /* Add terminator */ - bits_left = bits_total - strlen(binary_data); - if(bits_left <= 5) { - for(i = 0; i < bits_left; i++) { - concat(binary_data, "0"); - } - latch = 1; - } else { - concat(binary_data, "00000"); - } - - if(latch == 0) { - /* Complete current byte */ - remainder = 8 - (strlen(binary_data) % 8); - if(remainder == 8) { remainder = 0; } - for(i = 0; i < remainder; i++) { - concat(binary_data, "0"); - } - - /* Add padding */ - bits_left = bits_total - strlen(binary_data); - remainder = bits_left / 8; - for(i = 0; i < remainder; i++) { - concat(binary_data, i & 1 ? "00010001" : "11101100"); - } - } - - if(ecc_mode == LEVEL_L) { data_codewords = 5; ecc_codewords = 5; } - if(ecc_mode == LEVEL_M) { data_codewords = 4; ecc_codewords = 6; } - - /* Copy data into codewords */ - for(i = 0; i < data_codewords; i++) { - data_blocks[i] = 0; - if(binary_data[i * 8] == '1') { data_blocks[i] += 0x80; } - if(binary_data[(i * 8) + 1] == '1') { data_blocks[i] += 0x40; } - if(binary_data[(i * 8) + 2] == '1') { data_blocks[i] += 0x20; } - if(binary_data[(i * 8) + 3] == '1') { data_blocks[i] += 0x10; } - if(binary_data[(i * 8) + 4] == '1') { data_blocks[i] += 0x08; } - if(binary_data[(i * 8) + 5] == '1') { data_blocks[i] += 0x04; } - if(binary_data[(i * 8) + 6] == '1') { data_blocks[i] += 0x02; } - if(binary_data[(i * 8) + 7] == '1') { data_blocks[i] += 0x01; } - } - - /* Calculate Reed-Solomon error codewords */ - rs_init_gf(0x11d); - rs_init_code(ecc_codewords, 0); - rs_encode(data_codewords,data_blocks,ecc_blocks); - rs_free(); - - /* Add Reed-Solomon codewords to binary data */ - for(i = 0; i < ecc_codewords; i++) { - qr_bscan(binary_data, ecc_blocks[ecc_codewords - i - 1], 0x80); - } - - return; -} - -void micro_qr_m3(char binary_data[], int ecc_mode) -{ - int i, latch; - int bits_total, bits_left, remainder; - int data_codewords, ecc_codewords; - unsigned char data_blocks[12], ecc_blocks[9]; - - latch = 0; - - if(ecc_mode == LEVEL_L) { bits_total = 84; } - if(ecc_mode == LEVEL_M) { bits_total = 68; } - - /* Add terminator */ - bits_left = bits_total - strlen(binary_data); - if(bits_left <= 7) { - for(i = 0; i < bits_left; i++) { - concat(binary_data, "0"); - } - latch = 1; - } else { - concat(binary_data, "0000000"); - } - - if(latch == 0) { - /* Manage last (4-bit) block */ - bits_left = bits_total - strlen(binary_data); - if(bits_left <= 4) { - for(i = 0; i < bits_left; i++) { - concat(binary_data, "0"); - } - latch = 1; - } - } - - if(latch == 0) { - /* Complete current byte */ - remainder = 8 - (strlen(binary_data) % 8); - if(remainder == 8) { remainder = 0; } - for(i = 0; i < remainder; i++) { - concat(binary_data, "0"); - } - - /* Add padding */ - bits_left = bits_total - strlen(binary_data); - if(bits_left > 4) { - remainder = (bits_left - 4) / 8; - for(i = 0; i < remainder; i++) { - concat(binary_data, i & 1 ? "00010001" : "11101100"); - } - } - concat(binary_data, "0000"); - } - - if(ecc_mode == LEVEL_L) { data_codewords = 11; ecc_codewords = 6; } - if(ecc_mode == LEVEL_M) { data_codewords = 9; ecc_codewords = 8; } - - /* Copy data into codewords */ - for(i = 0; i < (data_codewords - 1); i++) { - data_blocks[i] = 0; - if(binary_data[i * 8] == '1') { data_blocks[i] += 0x80; } - if(binary_data[(i * 8) + 1] == '1') { data_blocks[i] += 0x40; } - if(binary_data[(i * 8) + 2] == '1') { data_blocks[i] += 0x20; } - if(binary_data[(i * 8) + 3] == '1') { data_blocks[i] += 0x10; } - if(binary_data[(i * 8) + 4] == '1') { data_blocks[i] += 0x08; } - if(binary_data[(i * 8) + 5] == '1') { data_blocks[i] += 0x04; } - if(binary_data[(i * 8) + 6] == '1') { data_blocks[i] += 0x02; } - if(binary_data[(i * 8) + 7] == '1') { data_blocks[i] += 0x01; } - } - - if(ecc_mode == LEVEL_L) { - data_blocks[10] = 0; - if(binary_data[80] == '1') { data_blocks[10] += 0x08; } - if(binary_data[81] == '1') { data_blocks[10] += 0x04; } - if(binary_data[82] == '1') { data_blocks[10] += 0x02; } - if(binary_data[83] == '1') { data_blocks[10] += 0x01; } - } - - if(ecc_mode == LEVEL_M) { - data_blocks[8] = 0; - if(binary_data[64] == '1') { data_blocks[8] += 0x08; } - if(binary_data[65] == '1') { data_blocks[8] += 0x04; } - if(binary_data[66] == '1') { data_blocks[8] += 0x02; } - if(binary_data[67] == '1') { data_blocks[8] += 0x01; } - } - - /* Calculate Reed-Solomon error codewords */ - rs_init_gf(0x11d); - rs_init_code(ecc_codewords, 0); - rs_encode(data_codewords,data_blocks,ecc_blocks); - rs_free(); - - /* Add Reed-Solomon codewords to binary data */ - for(i = 0; i < ecc_codewords; i++) { - qr_bscan(binary_data, ecc_blocks[ecc_codewords - i - 1], 0x80); - } - - return; -} - -void micro_qr_m4(char binary_data[], int ecc_mode) -{ - int i, latch; - int bits_total, bits_left, remainder; - int data_codewords, ecc_codewords; - unsigned char data_blocks[17], ecc_blocks[15]; - - latch = 0; - - if(ecc_mode == LEVEL_L) { bits_total = 128; } - if(ecc_mode == LEVEL_M) { bits_total = 112; } - if(ecc_mode == LEVEL_Q) { bits_total = 80; } - - /* Add terminator */ - bits_left = bits_total - strlen(binary_data); - if(bits_left <= 9) { - for(i = 0; i < bits_left; i++) { - concat(binary_data, "0"); - } - latch = 1; - } else { - concat(binary_data, "000000000"); - } - - if(latch == 0) { - /* Complete current byte */ - remainder = 8 - (strlen(binary_data) % 8); - if(remainder == 8) { remainder = 0; } - for(i = 0; i < remainder; i++) { - concat(binary_data, "0"); - } - - /* Add padding */ - bits_left = bits_total - strlen(binary_data); - remainder = bits_left / 8; - for(i = 0; i < remainder; i++) { - concat(binary_data, i & 1 ? "00010001" : "11101100"); - } - } - - if(ecc_mode == LEVEL_L) { data_codewords = 16; ecc_codewords = 8; } - if(ecc_mode == LEVEL_M) { data_codewords = 14; ecc_codewords = 10; } - if(ecc_mode == LEVEL_Q) { data_codewords = 10; ecc_codewords = 14; } - - /* Copy data into codewords */ - for(i = 0; i < data_codewords; i++) { - data_blocks[i] = 0; - if(binary_data[i * 8] == '1') { data_blocks[i] += 0x80; } - if(binary_data[(i * 8) + 1] == '1') { data_blocks[i] += 0x40; } - if(binary_data[(i * 8) + 2] == '1') { data_blocks[i] += 0x20; } - if(binary_data[(i * 8) + 3] == '1') { data_blocks[i] += 0x10; } - if(binary_data[(i * 8) + 4] == '1') { data_blocks[i] += 0x08; } - if(binary_data[(i * 8) + 5] == '1') { data_blocks[i] += 0x04; } - if(binary_data[(i * 8) + 6] == '1') { data_blocks[i] += 0x02; } - if(binary_data[(i * 8) + 7] == '1') { data_blocks[i] += 0x01; } - } - - /* Calculate Reed-Solomon error codewords */ - rs_init_gf(0x11d); - rs_init_code(ecc_codewords, 0); - rs_encode(data_codewords,data_blocks,ecc_blocks); - rs_free(); - - /* Add Reed-Solomon codewords to binary data */ - for(i = 0; i < ecc_codewords; i++) { - qr_bscan(binary_data, ecc_blocks[ecc_codewords - i - 1], 0x80); - } -} - -void micro_setup_grid(unsigned char* grid, int size) -{ - int i, toggle = 1; - - /* Add timing patterns */ - for(i = 0; i < size; i++) { - if(toggle == 1) { - grid[i] = 0x21; - grid[(i * size)] = 0x21; - toggle = 0; - } else { - grid[i] = 0x20; - grid[(i * size)] = 0x20; - toggle = 1; - } - } - - /* Add finder patterns */ - place_finder(grid, size, 0, 0); - - /* Add separators */ - for(i = 0; i < 7; i++) { - grid[(7 * size) + i] = 0x10; - grid[(i * size) + 7] = 0x10; - } - grid[(7 * size) + 7] = 0x10; - - - /* Reserve space for format information */ - for(i = 0; i < 8; i++) { - grid[(8 * size) + i] += 0x20; - grid[(i * size) + 8] += 0x20; - } - grid[(8 * size) + 8] += 20; -} - -void micro_populate_grid(unsigned char* grid, int size, char full_stream[]) -{ - int direction = 1; /* up */ - int row = 0; /* right hand side */ - - int i, n, x, y; - - n = strlen(full_stream); - y = size - 1; - i = 0; - do { - x = (size - 2) - (row * 2); - - if(!(grid[(y * size) + (x + 1)] & 0xf0)) { - if (full_stream[i] == '1') { - grid[(y * size) + (x + 1)] = 0x01; - } else { - grid[(y * size) + (x + 1)] = 0x00; - } - i++; - } - - if(i < n) { - if(!(grid[(y * size) + x] & 0xf0)) { - if (full_stream[i] == '1') { - grid[(y * size) + x] = 0x01; - } else { - grid[(y * size) + x] = 0x00; - } - i++; - } - } - - if(direction) { y--; } else { y++; } - if(y == 0) { - /* reached the top */ - row++; - y = 1; - direction = 0; - } - if(y == size) { - /* reached the bottom */ - row++; - y = size - 1; - direction = 1; - } - } while (i < n); -} - -int micro_evaluate(unsigned char *grid, int size, int pattern) -{ - int sum1, sum2, i, filter = 0, retval; - - switch(pattern) { - case 0: filter = 0x01; break; - case 1: filter = 0x02; break; - case 2: filter = 0x04; break; - case 3: filter = 0x08; break; - } - - sum1 = 0; - sum2 = 0; - for(i = 1; i < size; i++) { - if(grid[(i * size) + size - 1] & filter) { sum1++; } - if(grid[((size - 1) * size) + i] & filter) { sum2++; } - } - - if(sum1 <= sum2) { retval = (sum1 * 16) + sum2; } else { retval = (sum2 * 16) + sum1; } - - return retval; -} - -int micro_apply_bitmask(unsigned char *grid, int size) -{ - int x, y; - unsigned char p; - int pattern, value[8]; - int best_val, best_pattern; - int bit; - -#ifndef _MSC_VER - unsigned char mask[size * size]; - unsigned char eval[size * size]; -#else - unsigned char* mask = (unsigned char *)_alloca((size * size) * sizeof(unsigned char)); - unsigned char* eval = (unsigned char *)_alloca((size * size) * sizeof(unsigned char)); -#endif - - /* Perform data masking */ - for(x = 0; x < size; x++) { - for(y = 0; y < size; y++) { - mask[(y * size) + x] = 0x00; - - if (!(grid[(y * size) + x] & 0xf0)) { - if((y & 1) == 0) { - mask[(y * size) + x] += 0x01; - } - - if((((y / 2) + (x / 3)) & 1) == 0) { - mask[(y * size) + x] += 0x02; - } - - if(((((y * x) & 1) + ((y * x) % 3)) & 1) == 0) { - mask[(y * size) + x] += 0x04; - } - - if(((((y + x) & 1) + ((y * x) % 3)) & 1) == 0) { - mask[(y * size) + x] += 0x08; - } - } - } - } - - for(x = 0; x < size; x++) { - for(y = 0; y < size; y++) { - if(grid[(y * size) + x] & 0x01) { p = 0xff; } else { p = 0x00; } - - eval[(y * size) + x] = mask[(y * size) + x] ^ p; - } - } - - - /* Evaluate result */ - for(pattern = 0; pattern < 8; pattern++) { - value[pattern] = micro_evaluate(eval, size, pattern); - } - - best_pattern = 0; - best_val = value[0]; - for(pattern = 1; pattern < 4; pattern++) { - if(value[pattern] > best_val) { - best_pattern = pattern; - best_val = value[pattern]; - } - } - - /* Apply mask */ - for(x = 0; x < size; x++) { - for(y = 0; y < size; y++) { - bit = 0; - switch(best_pattern) { - case 0: if(mask[(y * size) + x] & 0x01) { bit = 1; } break; - case 1: if(mask[(y * size) + x] & 0x02) { bit = 1; } break; - case 2: if(mask[(y * size) + x] & 0x04) { bit = 1; } break; - case 3: if(mask[(y * size) + x] & 0x08) { bit = 1; } break; - } - if(bit == 1) { - if(grid[(y * size) + x] & 0x01) { - grid[(y * size) + x] = 0x00; - } else { - grid[(y * size) + x] = 0x01; - } - } - } - } - - return best_pattern; -} - -int microqr(struct zint_symbol *symbol, unsigned char source[], int length) -{ - int i, j, glyph, size; - char binary_stream[200]; - char full_stream[200]; - int utfdata[40]; - int jisdata[40]; - char mode[40]; - int error_number, kanji_used = 0, alphanum_used = 0, byte_used = 0; - int version_valid[4]; - int binary_count[4]; - int ecc_level, autoversion, version; - int n_count, a_count, bitmask, format, format_full; + 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); + + switch(symbol->input_mode) { + case DATA_MODE: + for(i = 0; i < length; i++) { + jisdata[i] = (int)source[i]; + } + break; + default: + /* Convert Unicode input to Shift-JIS */ + 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 { + j = 0; + glyph = 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, "Invalid character in input data"); + return ZINT_ERROR_INVALID_DATA; + } + jisdata[i] = glyph; + } + } + break; + } + + define_mode(mode, jisdata, length, gs1); + est_binlen = estimate_binary_length(mode, length, gs1); + + ecc_level = LEVEL_L; + max_cw = 2956; + if((symbol->option_1 >= 1) && (symbol->option_1 <= 4)) { + switch (symbol->option_1) { + case 1: ecc_level = LEVEL_L; max_cw = 2956; break; + case 2: ecc_level = LEVEL_M; max_cw = 2334; break; + case 3: ecc_level = LEVEL_Q; max_cw = 1666; break; + case 4: ecc_level = LEVEL_H; max_cw = 1276; break; + } + } + + if(est_binlen > (8 * max_cw)) { + strcpy(symbol->errtxt, "Input too long for selected error correction level"); + return ZINT_ERROR_TOO_LONG; + } + + autosize = 40; + for(i = 39; i >= 0; i--) { + switch(ecc_level) { + case LEVEL_L: + if ((8 * qr_data_codewords_L[i]) >= est_binlen) { + autosize = i + 1; + } + break; + case LEVEL_M: + if ((8 * qr_data_codewords_M[i]) >= est_binlen) { + autosize = i + 1; + } + break; + case LEVEL_Q: + if ((8 * qr_data_codewords_Q[i]) >= est_binlen) { + autosize = i + 1; + } + break; + case LEVEL_H: + if ((8 * qr_data_codewords_H[i]) >= est_binlen) { + autosize = i + 1; + } + break; + } + } + + if((symbol->option_2 >= 1) && (symbol->option_2 <= 40)) { + if (symbol->option_2 > autosize) { + version = symbol->option_2; + } else { + version = autosize; + } + } else { + version = autosize; + } + + /* Ensure maxium error correction capacity */ + if(est_binlen <= qr_data_codewords_M[version - 1]) { ecc_level = LEVEL_M; } + if(est_binlen <= qr_data_codewords_Q[version - 1]) { ecc_level = LEVEL_Q; } + if(est_binlen <= qr_data_codewords_H[version - 1]) { ecc_level = LEVEL_H; } + + target_binlen = 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]; blocks = qr_blocks_M[version - 1]; break; + case LEVEL_Q: target_binlen = qr_data_codewords_Q[version - 1]; blocks = qr_blocks_Q[version - 1]; break; + case LEVEL_H: target_binlen = qr_data_codewords_H[version - 1]; blocks = qr_blocks_H[version - 1]; break; + } + +#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, gs1, est_binlen); + 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]); + + if(version >= 7) { + add_version_info(grid, size, version); + } + + 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; +} + +/* NOTE: From this point forward concerns Micro QR Code only */ + +int micro_qr_intermediate(char binary[], int jisdata[], char mode[], int length, int *kanji_used, int *alphanum_used, int *byte_used) +{ + /* Convert input data to an "intermediate stage" where data is binary encoded but + control information is not */ + int position = 0, debug = 0; + int short_data_block_length, i; + char data_block; + char buffer[2]; + + strcpy(binary, ""); + + if(debug) { + for(i = 0; i < length; i++) { + printf("%c", mode[i]); + } + printf("\n"); + } + + do { + if(strlen(binary) > 128) { + return ZINT_ERROR_TOO_LONG; + } + + data_block = mode[position]; + short_data_block_length = 0; + do { + short_data_block_length++; + } while (((short_data_block_length + position) < length) && (mode[position + short_data_block_length] == data_block)); + + switch(data_block) { + case 'K': + /* Kanji mode */ + /* Mode indicator */ + concat(binary, "K"); + *kanji_used = 1; + + /* Character count indicator */ + buffer[0] = short_data_block_length; + buffer[1] = '\0'; + concat(binary, buffer); + + if(debug) { printf("Kanji block (length %d)\n\t", short_data_block_length); } + + /* Character representation */ + for(i = 0; i < short_data_block_length; i++) { + int jis = jisdata[position + i]; + int msb, lsb, prod; + + if(jis > 0x9fff) { jis -= 0xc140; } + msb = (jis & 0xff00) >> 4; + lsb = (jis & 0xff); + prod = (msb * 0xc0) + lsb; + + qr_bscan(binary, prod, 0x1000); + + if(debug) { printf("0x%4X ", prod); } + + if(strlen(binary) > 128) { + return ZINT_ERROR_TOO_LONG; + } + } + + if(debug) { printf("\n"); } + + break; + case 'B': + /* Byte mode */ + /* Mode indicator */ + concat(binary, "B"); + *byte_used = 1; + + /* Character count indicator */ + buffer[0] = short_data_block_length; + buffer[1] = '\0'; + concat(binary, buffer); + + if(debug) { printf("Byte block (length %d)\n\t", short_data_block_length); } + + /* Character representation */ + for(i = 0; i < short_data_block_length; i++) { + int byte = jisdata[position + i]; + + qr_bscan(binary, byte, 0x80); + + if(debug) { printf("0x%4X ", byte); } + + if(strlen(binary) > 128) { + return ZINT_ERROR_TOO_LONG; + } + } + + if(debug) { printf("\n"); } + + break; + case 'A': + /* Alphanumeric mode */ + /* Mode indicator */ + concat(binary, "A"); + *alphanum_used = 1; + + /* Character count indicator */ + buffer[0] = short_data_block_length; + buffer[1] = '\0'; + concat(binary, buffer); + + if(debug) { printf("Alpha block (length %d)\n\t", short_data_block_length); } + + /* Character representation */ + i = 0; + while ( i < short_data_block_length ) { + int count; + int first = 0, second = 0, prod; + + first = posn(RHODIUM, (char) jisdata[position + i]); + count = 1; + prod = first; + + if(i + 1 < short_data_block_length && mode[position + i + 1] == 'A') { + second = posn(RHODIUM, (char) jisdata[position + i + 1]); + count = 2; + prod = (first * 45) + second; + } + + qr_bscan(binary, prod, 1 << (5 * count)); /* count = 1..2 */ + + if(debug) { printf("0x%4X ", prod); } + + if(strlen(binary) > 128) { + return ZINT_ERROR_TOO_LONG; + } + + i += 2; + }; + + if(debug) { printf("\n"); } + + break; + case 'N': + /* Numeric mode */ + /* Mode indicator */ + concat(binary, "N"); + + /* Character count indicator */ + buffer[0] = short_data_block_length; + buffer[1] = '\0'; + concat(binary, buffer); + + if(debug) { printf("Number block (length %d)\n\t", short_data_block_length); } + + /* Character representation */ + i = 0; + while ( i < short_data_block_length ) { + int count; + int first = 0, second = 0, third = 0, prod; + + first = posn(NEON, (char) jisdata[position + i]); + count = 1; + prod = first; + + if(i + 1 < short_data_block_length && mode[position + i + 1] == 'N') { + second = posn(NEON, (char) jisdata[position + i + 1]); + count = 2; + prod = (prod * 10) + second; + } + + if(i + 2 < short_data_block_length && mode[position + i + 2] == 'N') { + third = posn(NEON, (char) jisdata[position + i + 2]); + count = 3; + prod = (prod * 10) + third; + } + + qr_bscan(binary, prod, 1 << (3 * count)); /* count = 1..3 */ + + if(debug) { printf("0x%4X (%d)", prod, prod); } + + if(strlen(binary) > 128) { + return ZINT_ERROR_TOO_LONG; + } + + i += 3; + }; + + if(debug) { printf("\n"); } + + break; + } + + position += short_data_block_length; + } while (position < length - 1) ; + + return 0; +} + +void get_bitlength(int count[], char stream[]) { + int length, i; + + length = strlen(stream); + + for(i = 0; i < 4; i++) { + count[i] = 0; + } + + i = 0; + do { + if((stream[i] == '0') || (stream[i] == '1')) { + count[0]++; + count[1]++; + count[2]++; + count[3]++; + i++; + } else { + switch(stream[i]) { + case 'K': + count[2] += 5; + count[3] += 7; + i += 2; + break; + case 'B': + count[2] += 6; + count[3] += 8; + i += 2; + break; + case 'A': + count[1] += 4; + count[2] += 6; + count[3] += 8; + i += 2; + break; + case 'N': + count[0] += 3; + count[1] += 5; + count[2] += 7; + count[3] += 9; + i += 2; + break; + } + } + } while (i < length); +} + +void microqr_expand_binary(char binary_stream[], char full_stream[], int version) +{ + int i, length; + + length = strlen(binary_stream); + + i = 0; + do { + switch(binary_stream[i]) { + case '1': concat(full_stream, "1"); i++; break; + case '0': concat(full_stream, "0"); i++; break; + case 'N': + /* Numeric Mode */ + /* Mode indicator */ + switch(version) { + case 1: concat(full_stream, "0"); break; + case 2: concat(full_stream, "00"); break; + case 3: concat(full_stream, "000"); break; + } + + /* Character count indicator */ + qr_bscan(full_stream, binary_stream[i + 1], 4 << version); /* version = 0..3 */ + + i += 2; + break; + case 'A': + /* Alphanumeric Mode */ + /* Mode indicator */ + switch(version) { + case 1: concat(full_stream, "1"); break; + case 2: concat(full_stream, "01"); break; + case 3: concat(full_stream, "001"); break; + } + + /* Character count indicator */ + qr_bscan(full_stream, binary_stream[i + 1], 2 << version); /* version = 1..3 */ + + i += 2; + break; + case 'B': + /* Byte Mode */ + /* Mode indicator */ + switch(version) { + case 2: concat(full_stream, "10"); break; + case 3: concat(full_stream, "010"); break; + } + + /* Character count indicator */ + qr_bscan(full_stream, binary_stream[i + 1], 2 << version); /* version = 2..3 */ + + i += 2; + break; + case 'K': + /* Kanji Mode */ + /* Mode indicator */ + switch(version) { + case 2: concat(full_stream, "11"); break; + case 3: concat(full_stream, "011"); break; + } + + /* Character count indicator */ + qr_bscan(full_stream, binary_stream[i + 1], 1 << version); /* version = 2..3 */ + + i += 2; + break; + } + + } while (i < length); +} + +void micro_qr_m1(char binary_data[]) +{ + int i, latch; + int bits_total, bits_left, remainder; + int data_codewords, ecc_codewords; + unsigned char data_blocks[4], ecc_blocks[3]; + + bits_total = 20; + latch = 0; + + /* Add terminator */ + bits_left = bits_total - strlen(binary_data); + if(bits_left <= 3) { + for(i = 0; i < bits_left; i++) { + concat(binary_data, "0"); + } + latch = 1; + } else { + concat(binary_data, "000"); + } + + if(latch == 0) { + /* Manage last (4-bit) block */ + bits_left = bits_total - strlen(binary_data); + if(bits_left <= 4) { + for(i = 0; i < bits_left; i++) { + concat(binary_data, "0"); + } + latch = 1; + } + } + + if(latch == 0) { + /* Complete current byte */ + remainder = 8 - (strlen(binary_data) % 8); + if(remainder == 8) { remainder = 0; } + for(i = 0; i < remainder; i++) { + concat(binary_data, "0"); + } + + /* Add padding */ + bits_left = bits_total - strlen(binary_data); + if(bits_left > 4) { + remainder = (bits_left - 4) / 8; + for(i = 0; i < remainder; i++) { + concat(binary_data, i & 1 ? "00010001" : "11101100"); + } + } + concat(binary_data, "0000"); + } + + data_codewords = 3; + ecc_codewords = 2; + + /* Copy data into codewords */ + for(i = 0; i < (data_codewords - 1); i++) { + data_blocks[i] = 0; + if(binary_data[i * 8] == '1') { data_blocks[i] += 0x80; } + if(binary_data[(i * 8) + 1] == '1') { data_blocks[i] += 0x40; } + if(binary_data[(i * 8) + 2] == '1') { data_blocks[i] += 0x20; } + if(binary_data[(i * 8) + 3] == '1') { data_blocks[i] += 0x10; } + if(binary_data[(i * 8) + 4] == '1') { data_blocks[i] += 0x08; } + if(binary_data[(i * 8) + 5] == '1') { data_blocks[i] += 0x04; } + if(binary_data[(i * 8) + 6] == '1') { data_blocks[i] += 0x02; } + if(binary_data[(i * 8) + 7] == '1') { data_blocks[i] += 0x01; } + } + data_blocks[2] = 0; + if(binary_data[16] == '1') { data_blocks[2] += 0x08; } + if(binary_data[17] == '1') { data_blocks[2] += 0x04; } + if(binary_data[18] == '1') { data_blocks[2] += 0x02; } + if(binary_data[19] == '1') { data_blocks[2] += 0x01; } + + /* Calculate Reed-Solomon error codewords */ + rs_init_gf(0x11d); + rs_init_code(ecc_codewords, 0); + rs_encode(data_codewords,data_blocks,ecc_blocks); + rs_free(); + + /* Add Reed-Solomon codewords to binary data */ + for(i = 0; i < ecc_codewords; i++) { + qr_bscan(binary_data, ecc_blocks[ecc_codewords - i - 1], 0x80); + } +} + +void micro_qr_m2(char binary_data[], int ecc_mode) +{ + int i, latch; + int bits_total, bits_left, remainder; + int data_codewords, ecc_codewords; + unsigned char data_blocks[6], ecc_blocks[7]; + + latch = 0; + + if(ecc_mode == LEVEL_L) { bits_total = 40; } + if(ecc_mode == LEVEL_M) { bits_total = 32; } + + /* Add terminator */ + bits_left = bits_total - strlen(binary_data); + if(bits_left <= 5) { + for(i = 0; i < bits_left; i++) { + concat(binary_data, "0"); + } + latch = 1; + } else { + concat(binary_data, "00000"); + } + + if(latch == 0) { + /* Complete current byte */ + remainder = 8 - (strlen(binary_data) % 8); + if(remainder == 8) { remainder = 0; } + for(i = 0; i < remainder; i++) { + concat(binary_data, "0"); + } + + /* Add padding */ + bits_left = bits_total - strlen(binary_data); + remainder = bits_left / 8; + for(i = 0; i < remainder; i++) { + concat(binary_data, i & 1 ? "00010001" : "11101100"); + } + } + + if(ecc_mode == LEVEL_L) { data_codewords = 5; ecc_codewords = 5; } + if(ecc_mode == LEVEL_M) { data_codewords = 4; ecc_codewords = 6; } + + /* Copy data into codewords */ + for(i = 0; i < data_codewords; i++) { + data_blocks[i] = 0; + if(binary_data[i * 8] == '1') { data_blocks[i] += 0x80; } + if(binary_data[(i * 8) + 1] == '1') { data_blocks[i] += 0x40; } + if(binary_data[(i * 8) + 2] == '1') { data_blocks[i] += 0x20; } + if(binary_data[(i * 8) + 3] == '1') { data_blocks[i] += 0x10; } + if(binary_data[(i * 8) + 4] == '1') { data_blocks[i] += 0x08; } + if(binary_data[(i * 8) + 5] == '1') { data_blocks[i] += 0x04; } + if(binary_data[(i * 8) + 6] == '1') { data_blocks[i] += 0x02; } + if(binary_data[(i * 8) + 7] == '1') { data_blocks[i] += 0x01; } + } + + /* Calculate Reed-Solomon error codewords */ + rs_init_gf(0x11d); + rs_init_code(ecc_codewords, 0); + rs_encode(data_codewords,data_blocks,ecc_blocks); + rs_free(); + + /* Add Reed-Solomon codewords to binary data */ + for(i = 0; i < ecc_codewords; i++) { + qr_bscan(binary_data, ecc_blocks[ecc_codewords - i - 1], 0x80); + } + + return; +} + +void micro_qr_m3(char binary_data[], int ecc_mode) +{ + int i, latch; + int bits_total, bits_left, remainder; + int data_codewords, ecc_codewords; + unsigned char data_blocks[12], ecc_blocks[9]; + + latch = 0; + + if(ecc_mode == LEVEL_L) { bits_total = 84; } + if(ecc_mode == LEVEL_M) { bits_total = 68; } + + /* Add terminator */ + bits_left = bits_total - strlen(binary_data); + if(bits_left <= 7) { + for(i = 0; i < bits_left; i++) { + concat(binary_data, "0"); + } + latch = 1; + } else { + concat(binary_data, "0000000"); + } + + if(latch == 0) { + /* Manage last (4-bit) block */ + bits_left = bits_total - strlen(binary_data); + if(bits_left <= 4) { + for(i = 0; i < bits_left; i++) { + concat(binary_data, "0"); + } + latch = 1; + } + } + + if(latch == 0) { + /* Complete current byte */ + remainder = 8 - (strlen(binary_data) % 8); + if(remainder == 8) { remainder = 0; } + for(i = 0; i < remainder; i++) { + concat(binary_data, "0"); + } + + /* Add padding */ + bits_left = bits_total - strlen(binary_data); + if(bits_left > 4) { + remainder = (bits_left - 4) / 8; + for(i = 0; i < remainder; i++) { + concat(binary_data, i & 1 ? "00010001" : "11101100"); + } + } + concat(binary_data, "0000"); + } + + if(ecc_mode == LEVEL_L) { data_codewords = 11; ecc_codewords = 6; } + if(ecc_mode == LEVEL_M) { data_codewords = 9; ecc_codewords = 8; } + + /* Copy data into codewords */ + for(i = 0; i < (data_codewords - 1); i++) { + data_blocks[i] = 0; + if(binary_data[i * 8] == '1') { data_blocks[i] += 0x80; } + if(binary_data[(i * 8) + 1] == '1') { data_blocks[i] += 0x40; } + if(binary_data[(i * 8) + 2] == '1') { data_blocks[i] += 0x20; } + if(binary_data[(i * 8) + 3] == '1') { data_blocks[i] += 0x10; } + if(binary_data[(i * 8) + 4] == '1') { data_blocks[i] += 0x08; } + if(binary_data[(i * 8) + 5] == '1') { data_blocks[i] += 0x04; } + if(binary_data[(i * 8) + 6] == '1') { data_blocks[i] += 0x02; } + if(binary_data[(i * 8) + 7] == '1') { data_blocks[i] += 0x01; } + } + + if(ecc_mode == LEVEL_L) { + data_blocks[10] = 0; + if(binary_data[80] == '1') { data_blocks[10] += 0x08; } + if(binary_data[81] == '1') { data_blocks[10] += 0x04; } + if(binary_data[82] == '1') { data_blocks[10] += 0x02; } + if(binary_data[83] == '1') { data_blocks[10] += 0x01; } + } + + if(ecc_mode == LEVEL_M) { + data_blocks[8] = 0; + if(binary_data[64] == '1') { data_blocks[8] += 0x08; } + if(binary_data[65] == '1') { data_blocks[8] += 0x04; } + if(binary_data[66] == '1') { data_blocks[8] += 0x02; } + if(binary_data[67] == '1') { data_blocks[8] += 0x01; } + } + + /* Calculate Reed-Solomon error codewords */ + rs_init_gf(0x11d); + rs_init_code(ecc_codewords, 0); + rs_encode(data_codewords,data_blocks,ecc_blocks); + rs_free(); + + /* Add Reed-Solomon codewords to binary data */ + for(i = 0; i < ecc_codewords; i++) { + qr_bscan(binary_data, ecc_blocks[ecc_codewords - i - 1], 0x80); + } + + return; +} + +void micro_qr_m4(char binary_data[], int ecc_mode) +{ + int i, latch; + int bits_total, bits_left, remainder; + int data_codewords, ecc_codewords; + unsigned char data_blocks[17], ecc_blocks[15]; + + latch = 0; + + if(ecc_mode == LEVEL_L) { bits_total = 128; } + if(ecc_mode == LEVEL_M) { bits_total = 112; } + if(ecc_mode == LEVEL_Q) { bits_total = 80; } + + /* Add terminator */ + bits_left = bits_total - strlen(binary_data); + if(bits_left <= 9) { + for(i = 0; i < bits_left; i++) { + concat(binary_data, "0"); + } + latch = 1; + } else { + concat(binary_data, "000000000"); + } + + if(latch == 0) { + /* Complete current byte */ + remainder = 8 - (strlen(binary_data) % 8); + if(remainder == 8) { remainder = 0; } + for(i = 0; i < remainder; i++) { + concat(binary_data, "0"); + } + + /* Add padding */ + bits_left = bits_total - strlen(binary_data); + remainder = bits_left / 8; + for(i = 0; i < remainder; i++) { + concat(binary_data, i & 1 ? "00010001" : "11101100"); + } + } + + if(ecc_mode == LEVEL_L) { data_codewords = 16; ecc_codewords = 8; } + if(ecc_mode == LEVEL_M) { data_codewords = 14; ecc_codewords = 10; } + if(ecc_mode == LEVEL_Q) { data_codewords = 10; ecc_codewords = 14; } + + /* Copy data into codewords */ + for(i = 0; i < data_codewords; i++) { + data_blocks[i] = 0; + if(binary_data[i * 8] == '1') { data_blocks[i] += 0x80; } + if(binary_data[(i * 8) + 1] == '1') { data_blocks[i] += 0x40; } + if(binary_data[(i * 8) + 2] == '1') { data_blocks[i] += 0x20; } + if(binary_data[(i * 8) + 3] == '1') { data_blocks[i] += 0x10; } + if(binary_data[(i * 8) + 4] == '1') { data_blocks[i] += 0x08; } + if(binary_data[(i * 8) + 5] == '1') { data_blocks[i] += 0x04; } + if(binary_data[(i * 8) + 6] == '1') { data_blocks[i] += 0x02; } + if(binary_data[(i * 8) + 7] == '1') { data_blocks[i] += 0x01; } + } + + /* Calculate Reed-Solomon error codewords */ + rs_init_gf(0x11d); + rs_init_code(ecc_codewords, 0); + rs_encode(data_codewords,data_blocks,ecc_blocks); + rs_free(); + + /* Add Reed-Solomon codewords to binary data */ + for(i = 0; i < ecc_codewords; i++) { + qr_bscan(binary_data, ecc_blocks[ecc_codewords - i - 1], 0x80); + } +} + +void micro_setup_grid(unsigned char* grid, int size) +{ + int i, toggle = 1; + + /* Add timing patterns */ + for(i = 0; i < size; i++) { + if(toggle == 1) { + grid[i] = 0x21; + grid[(i * size)] = 0x21; + toggle = 0; + } else { + grid[i] = 0x20; + grid[(i * size)] = 0x20; + toggle = 1; + } + } + + /* Add finder patterns */ + place_finder(grid, size, 0, 0); + + /* Add separators */ + for(i = 0; i < 7; i++) { + grid[(7 * size) + i] = 0x10; + grid[(i * size) + 7] = 0x10; + } + grid[(7 * size) + 7] = 0x10; + + + /* Reserve space for format information */ + for(i = 0; i < 8; i++) { + grid[(8 * size) + i] += 0x20; + grid[(i * size) + 8] += 0x20; + } + grid[(8 * size) + 8] += 20; +} + +void micro_populate_grid(unsigned char* grid, int size, char full_stream[]) +{ + int direction = 1; /* up */ + int row = 0; /* right hand side */ + + int i, n, x, y; + + n = strlen(full_stream); + y = size - 1; + i = 0; + do { + x = (size - 2) - (row * 2); + + if(!(grid[(y * size) + (x + 1)] & 0xf0)) { + if (full_stream[i] == '1') { + grid[(y * size) + (x + 1)] = 0x01; + } else { + grid[(y * size) + (x + 1)] = 0x00; + } + i++; + } + + if(i < n) { + if(!(grid[(y * size) + x] & 0xf0)) { + if (full_stream[i] == '1') { + grid[(y * size) + x] = 0x01; + } else { + grid[(y * size) + x] = 0x00; + } + i++; + } + } + + if(direction) { y--; } else { y++; } + if(y == 0) { + /* reached the top */ + row++; + y = 1; + direction = 0; + } + if(y == size) { + /* reached the bottom */ + row++; + y = size - 1; + direction = 1; + } + } while (i < n); +} + +int micro_evaluate(unsigned char *grid, int size, int pattern) +{ + int sum1, sum2, i, filter = 0, retval; + + switch(pattern) { + case 0: filter = 0x01; break; + case 1: filter = 0x02; break; + case 2: filter = 0x04; break; + case 3: filter = 0x08; break; + } + + sum1 = 0; + sum2 = 0; + for(i = 1; i < size; i++) { + if(grid[(i * size) + size - 1] & filter) { sum1++; } + if(grid[((size - 1) * size) + i] & filter) { sum2++; } + } + + if(sum1 <= sum2) { retval = (sum1 * 16) + sum2; } else { retval = (sum2 * 16) + sum1; } + + return retval; +} + +int micro_apply_bitmask(unsigned char *grid, int size) +{ + int x, y; + unsigned char p; + int pattern, value[8]; + int best_val, best_pattern; + int bit; + +#ifndef _MSC_VER + unsigned char mask[size * size]; + unsigned char eval[size * size]; +#else + unsigned char* mask = (unsigned char *)_alloca((size * size) * sizeof(unsigned char)); + unsigned char* eval = (unsigned char *)_alloca((size * size) * sizeof(unsigned char)); +#endif + + /* Perform data masking */ + for(x = 0; x < size; x++) { + for(y = 0; y < size; y++) { + mask[(y * size) + x] = 0x00; + + if (!(grid[(y * size) + x] & 0xf0)) { + if((y & 1) == 0) { + mask[(y * size) + x] += 0x01; + } + + if((((y / 2) + (x / 3)) & 1) == 0) { + mask[(y * size) + x] += 0x02; + } + + if(((((y * x) & 1) + ((y * x) % 3)) & 1) == 0) { + mask[(y * size) + x] += 0x04; + } + + if(((((y + x) & 1) + ((y * x) % 3)) & 1) == 0) { + mask[(y * size) + x] += 0x08; + } + } + } + } + + for(x = 0; x < size; x++) { + for(y = 0; y < size; y++) { + if(grid[(y * size) + x] & 0x01) { p = 0xff; } else { p = 0x00; } + + eval[(y * size) + x] = mask[(y * size) + x] ^ p; + } + } + + + /* Evaluate result */ + for(pattern = 0; pattern < 8; pattern++) { + value[pattern] = micro_evaluate(eval, size, pattern); + } + + best_pattern = 0; + best_val = value[0]; + for(pattern = 1; pattern < 4; pattern++) { + if(value[pattern] > best_val) { + best_pattern = pattern; + best_val = value[pattern]; + } + } + + /* Apply mask */ + for(x = 0; x < size; x++) { + for(y = 0; y < size; y++) { + bit = 0; + switch(best_pattern) { + case 0: if(mask[(y * size) + x] & 0x01) { bit = 1; } break; + case 1: if(mask[(y * size) + x] & 0x02) { bit = 1; } break; + case 2: if(mask[(y * size) + x] & 0x04) { bit = 1; } break; + case 3: if(mask[(y * size) + x] & 0x08) { bit = 1; } break; + } + if(bit == 1) { + if(grid[(y * size) + x] & 0x01) { + grid[(y * size) + x] = 0x00; + } else { + grid[(y * size) + x] = 0x01; + } + } + } + } + + return best_pattern; +} + +int microqr(struct zint_symbol *symbol, unsigned char source[], int length) +{ + int i, j, glyph, size; + char binary_stream[200]; + char full_stream[200]; + int utfdata[40]; + int jisdata[40]; + char mode[40]; + int error_number, kanji_used = 0, alphanum_used = 0, byte_used = 0; + int version_valid[4]; + int binary_count[4]; + int ecc_level, autoversion, version; + int n_count, a_count, bitmask, format, format_full; #ifdef _MSC_VER unsigned char* grid; #endif - - if(length > 35) { - strcpy(symbol->errtxt, "Input data too long"); - return ERROR_TOO_LONG; - } - - for(i = 0; i < 4; i++) { - version_valid[i] = 1; - } - - switch(symbol->input_mode) { - case DATA_MODE: - for(i = 0; i < length; i++) { - jisdata[i] = (int)source[i]; - } - break; - default: - /* Convert Unicode input to Shift-JIS */ - 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 { - j = 0; - glyph = 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, "Invalid character in input data"); - return ERROR_INVALID_DATA; - } - jisdata[i] = glyph; - } - } - break; - } - - define_mode(mode, jisdata, length, 0); - - n_count = 0; - a_count = 0; - for(i = 0; i < length; i++) { - if((jisdata[i] >= '0') && (jisdata[i] <= '9')) { n_count++; } - if(in_alpha(jisdata[i])) { a_count++; } - } - - if(a_count == length) { - /* All data can be encoded in Alphanumeric mode */ - for(i = 0; i < length; i++) { - mode[i] = 'A'; - } - } - - if(n_count == length) { - /* All data can be encoded in Numeric mode */ - for(i = 0; i < length; i++) { - mode[i] = 'N'; - } - } - - error_number = micro_qr_intermediate(binary_stream, jisdata, mode, length, &kanji_used, &alphanum_used, &byte_used); - if(error_number != 0) { - strcpy(symbol->errtxt, "Input data too long"); - return error_number; - } - - get_bitlength(binary_count, binary_stream); - - /* Eliminate possivle versions depending on type of content */ - if(byte_used) { - version_valid[0] = 0; - version_valid[1] = 0; - } - - if(alphanum_used) { - version_valid[0] = 0; - } - - if(kanji_used) { - version_valid[0] = 0; - version_valid[1] = 0; - } - - /* Eliminate possible versions depending on length of binary data */ - if(binary_count[0] > 20) { version_valid[0] = 0; } - if(binary_count[1] > 40) { version_valid[1] = 0; } - if(binary_count[2] > 84) { version_valid[2] = 0; } - if(binary_count[3] > 128) { - strcpy(symbol->errtxt, "Input data too long"); - return ERROR_TOO_LONG; - } - - /* Eliminate possible versions depending on error correction level specified */ - ecc_level = LEVEL_L; - if((symbol->option_1 >= 1) && (symbol->option_2 <= 4)) { - ecc_level = symbol->option_1; - } - - if(ecc_level == LEVEL_H) { - strcpy(symbol->errtxt, "Error correction level H not available"); - return ERROR_INVALID_OPTION; - } - - if(ecc_level == LEVEL_Q) { - version_valid[0] = 0; - version_valid[1] = 0; - version_valid[2] = 0; - if(binary_count[3] > 80) { - strcpy(symbol->errtxt, "Input data too long"); - return ERROR_TOO_LONG; - } - } - - if(ecc_level == LEVEL_M) { - version_valid[0] = 0; - if(binary_count[1] > 32) { version_valid[1] = 0; } - if(binary_count[2] > 68) { version_valid[2] = 0; } - if(binary_count[3] > 112) { - strcpy(symbol->errtxt, "Input data too long"); - return ERROR_TOO_LONG; - } - } - - autoversion = 3; - if(version_valid[2]) { autoversion = 2; } - if(version_valid[1]) { autoversion = 1; } - if(version_valid[0]) { autoversion = 0; } - - version = autoversion; - /* Get version from user */ - if((symbol->option_2 >= 1) && (symbol->option_2 <= 4)) { - if(symbol->option_2 >= autoversion) { - version = symbol->option_2; - } - } - - /* If there is enough unused space then increase the error correction level */ - if(version == 3) { - if(binary_count[3] <= 112) { ecc_level = LEVEL_M; } - if(binary_count[3] <= 80) { ecc_level = LEVEL_Q; } - } - - if(version == 2) { - if(binary_count[2] <= 68) { ecc_level = LEVEL_M; } - } - - if(version == 1) { - if(binary_count[1] <= 32) { ecc_level = LEVEL_M; } - } - - strcpy(full_stream, ""); - microqr_expand_binary(binary_stream, full_stream, version); - - switch(version) { - case 0: micro_qr_m1(full_stream); break; - case 1: micro_qr_m2(full_stream, ecc_level); break; - case 2: micro_qr_m3(full_stream, ecc_level); break; - case 3: micro_qr_m4(full_stream, ecc_level); break; - } - - size = micro_qr_sizes[version]; -#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; - } - } - - micro_setup_grid(grid, size); - micro_populate_grid(grid, size, full_stream); - bitmask = micro_apply_bitmask(grid, size); - - /* Add format data */ - format = 0; - switch(version) { - case 1: switch(ecc_level) { - case 1: format = 1; break; - case 2: format = 2; break; - } - break; - case 2: switch(ecc_level) { - case 1: format = 3; break; - case 2: format = 4; break; - } - break; - case 3: switch(ecc_level) { - case 1: format = 5; break; - case 2: format = 6; break; - case 3: format = 7; break; - } - break; - } - - format_full = qr_annex_c1[(format << 2) + bitmask]; - - if(format_full & 0x4000) { grid[(8 * size) + 1] += 0x01; } - if(format_full & 0x2000) { grid[(8 * size) + 2] += 0x01; } - if(format_full & 0x1000) { grid[(8 * size) + 3] += 0x01; } - if(format_full & 0x800) { grid[(8 * size) + 4] += 0x01; } - if(format_full & 0x400) { grid[(8 * size) + 5] += 0x01; } - if(format_full & 0x200) { grid[(8 * size) + 6] += 0x01; } - if(format_full & 0x100) { grid[(8 * size) + 7] += 0x01; } - if(format_full & 0x80) { grid[(8 * size) + 8] += 0x01; } - if(format_full & 0x40) { grid[(7 * size) + 8] += 0x01; } - if(format_full & 0x20) { grid[(6 * size) + 8] += 0x01; } - if(format_full & 0x10) { grid[(5 * size) + 8] += 0x01; } - if(format_full & 0x08) { grid[(4 * size) + 8] += 0x01; } - if(format_full & 0x04) { grid[(3 * size) + 8] += 0x01; } - if(format_full & 0x02) { grid[(2 * size) + 8] += 0x01; } - if(format_full & 0x01) { grid[(1 * size) + 8] += 0x01; } - - 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; -} + + if(length > 35) { + strcpy(symbol->errtxt, "Input data too long"); + return ZINT_ERROR_TOO_LONG; + } + + for(i = 0; i < 4; i++) { + version_valid[i] = 1; + } + + switch(symbol->input_mode) { + case DATA_MODE: + for(i = 0; i < length; i++) { + jisdata[i] = (int)source[i]; + } + break; + default: + /* Convert Unicode input to Shift-JIS */ + 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 { + j = 0; + glyph = 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, "Invalid character in input data"); + return ZINT_ERROR_INVALID_DATA; + } + jisdata[i] = glyph; + } + } + break; + } + + define_mode(mode, jisdata, length, 0); + + n_count = 0; + a_count = 0; + for(i = 0; i < length; i++) { + if((jisdata[i] >= '0') && (jisdata[i] <= '9')) { n_count++; } + if(in_alpha(jisdata[i])) { a_count++; } + } + + if(a_count == length) { + /* All data can be encoded in Alphanumeric mode */ + for(i = 0; i < length; i++) { + mode[i] = 'A'; + } + } + + if(n_count == length) { + /* All data can be encoded in Numeric mode */ + for(i = 0; i < length; i++) { + mode[i] = 'N'; + } + } + + error_number = micro_qr_intermediate(binary_stream, jisdata, mode, length, &kanji_used, &alphanum_used, &byte_used); + if(error_number != 0) { + strcpy(symbol->errtxt, "Input data too long"); + return error_number; + } + + get_bitlength(binary_count, binary_stream); + + /* Eliminate possivle versions depending on type of content */ + if(byte_used) { + version_valid[0] = 0; + version_valid[1] = 0; + } + + if(alphanum_used) { + version_valid[0] = 0; + } + + if(kanji_used) { + version_valid[0] = 0; + version_valid[1] = 0; + } + + /* Eliminate possible versions depending on length of binary data */ + if(binary_count[0] > 20) { version_valid[0] = 0; } + if(binary_count[1] > 40) { version_valid[1] = 0; } + if(binary_count[2] > 84) { version_valid[2] = 0; } + if(binary_count[3] > 128) { + strcpy(symbol->errtxt, "Input data too long"); + return ZINT_ERROR_TOO_LONG; + } + + /* Eliminate possible versions depending on error correction level specified */ + ecc_level = LEVEL_L; + if((symbol->option_1 >= 1) && (symbol->option_2 <= 4)) { + ecc_level = symbol->option_1; + } + + if(ecc_level == LEVEL_H) { + strcpy(symbol->errtxt, "Error correction level H not available"); + return ZINT_ERROR_INVALID_OPTION; + } + + if(ecc_level == LEVEL_Q) { + version_valid[0] = 0; + version_valid[1] = 0; + version_valid[2] = 0; + if(binary_count[3] > 80) { + strcpy(symbol->errtxt, "Input data too long"); + return ZINT_ERROR_TOO_LONG; + } + } + + if(ecc_level == LEVEL_M) { + version_valid[0] = 0; + if(binary_count[1] > 32) { version_valid[1] = 0; } + if(binary_count[2] > 68) { version_valid[2] = 0; } + if(binary_count[3] > 112) { + strcpy(symbol->errtxt, "Input data too long"); + return ZINT_ERROR_TOO_LONG; + } + } + + autoversion = 3; + if(version_valid[2]) { autoversion = 2; } + if(version_valid[1]) { autoversion = 1; } + if(version_valid[0]) { autoversion = 0; } + + version = autoversion; + /* Get version from user */ + if((symbol->option_2 >= 1) && (symbol->option_2 <= 4)) { + if(symbol->option_2 >= autoversion) { + version = symbol->option_2; + } + } + + /* If there is enough unused space then increase the error correction level */ + if(version == 3) { + if(binary_count[3] <= 112) { ecc_level = LEVEL_M; } + if(binary_count[3] <= 80) { ecc_level = LEVEL_Q; } + } + + if(version == 2) { + if(binary_count[2] <= 68) { ecc_level = LEVEL_M; } + } + + if(version == 1) { + if(binary_count[1] <= 32) { ecc_level = LEVEL_M; } + } + + strcpy(full_stream, ""); + microqr_expand_binary(binary_stream, full_stream, version); + + switch(version) { + case 0: micro_qr_m1(full_stream); break; + case 1: micro_qr_m2(full_stream, ecc_level); break; + case 2: micro_qr_m3(full_stream, ecc_level); break; + case 3: micro_qr_m4(full_stream, ecc_level); break; + } + + size = micro_qr_sizes[version]; +#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; + } + } + + micro_setup_grid(grid, size); + micro_populate_grid(grid, size, full_stream); + bitmask = micro_apply_bitmask(grid, size); + + /* Add format data */ + format = 0; + switch(version) { + case 1: switch(ecc_level) { + case 1: format = 1; break; + case 2: format = 2; break; + } + break; + case 2: switch(ecc_level) { + case 1: format = 3; break; + case 2: format = 4; break; + } + break; + case 3: switch(ecc_level) { + case 1: format = 5; break; + case 2: format = 6; break; + case 3: format = 7; break; + } + break; + } + + format_full = qr_annex_c1[(format << 2) + bitmask]; + + if(format_full & 0x4000) { grid[(8 * size) + 1] += 0x01; } + if(format_full & 0x2000) { grid[(8 * size) + 2] += 0x01; } + if(format_full & 0x1000) { grid[(8 * size) + 3] += 0x01; } + if(format_full & 0x800) { grid[(8 * size) + 4] += 0x01; } + if(format_full & 0x400) { grid[(8 * size) + 5] += 0x01; } + if(format_full & 0x200) { grid[(8 * size) + 6] += 0x01; } + if(format_full & 0x100) { grid[(8 * size) + 7] += 0x01; } + if(format_full & 0x80) { grid[(8 * size) + 8] += 0x01; } + if(format_full & 0x40) { grid[(7 * size) + 8] += 0x01; } + if(format_full & 0x20) { grid[(6 * size) + 8] += 0x01; } + if(format_full & 0x10) { grid[(5 * size) + 8] += 0x01; } + if(format_full & 0x08) { grid[(4 * size) + 8] += 0x01; } + if(format_full & 0x04) { grid[(3 * size) + 8] += 0x01; } + if(format_full & 0x02) { grid[(2 * size) + 8] += 0x01; } + if(format_full & 0x01) { grid[(1 * size) + 8] += 0x01; } + + 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; +} diff --git a/backend/rss.c b/backend/rss.c index 540d6f5b..cb51c0d6 100644 --- a/backend/rss.c +++ b/backend/rss.c @@ -168,10 +168,10 @@ int rss14(struct zint_symbol *symbol, unsigned char source[], int src_len) if(src_len > 13) { strcpy(symbol->errtxt, "Input too long"); - return ERROR_TOO_LONG; + return ZINT_ERROR_TOO_LONG; } error_number = is_sane(NEON, source, src_len); - if(error_number == ERROR_INVALID_DATA) { + if(error_number == ZINT_ERROR_INVALID_DATA) { strcpy(symbol->errtxt, "Invalid characters in data"); return error_number; } @@ -674,17 +674,17 @@ int rsslimited(struct zint_symbol *symbol, unsigned char source[], int src_len) if(src_len > 13) { strcpy(symbol->errtxt, "Input too long"); - return ERROR_TOO_LONG; + return ZINT_ERROR_TOO_LONG; } error_number = is_sane(NEON, source, src_len); - if(error_number == ERROR_INVALID_DATA) { + if(error_number == ZINT_ERROR_INVALID_DATA) { strcpy(symbol->errtxt, "Invalid characters in data"); return error_number; } if(src_len == 13) { if((source[0] != '0') && (source[0] != '1')) { strcpy(symbol->errtxt, "Input out of range"); - return ERROR_INVALID_DATA; + return ZINT_ERROR_INVALID_DATA; } } @@ -1254,7 +1254,7 @@ int rss_binary_string(struct zint_symbol *symbol, char source[], char binary_str if((source[i] != '[') && (source[i] != ']')) { /* Something is wrong */ strcpy(symbol->errtxt, "Invalid characters in input data"); - return ERROR_INVALID_DATA; + return ZINT_ERROR_INVALID_DATA; } } } @@ -1580,7 +1580,7 @@ int rss_binary_string(struct zint_symbol *symbol, char source[], char binary_str if(latch == 1) { /* Invalid characters in input data */ strcpy(symbol->errtxt, "Invalid characters in input data"); - return ERROR_INVALID_DATA; + return ZINT_ERROR_INVALID_DATA; } for(i = 0; i < strlen(general_field); i++) { @@ -1821,7 +1821,7 @@ int rss_binary_string(struct zint_symbol *symbol, char source[], char binary_str if(strlen(binary_string) > 252) { strcpy(symbol->errtxt, "Input too long"); - return ERROR_TOO_LONG; + return ZINT_ERROR_TOO_LONG; } /* Now add padding to binary string (7.2.5.5.4) */ diff --git a/backend/svg.c b/backend/svg.c index 9c58c352..e7d203f2 100644 --- a/backend/svg.c +++ b/backend/svg.c @@ -67,7 +67,7 @@ int svg_plot(struct zint_symbol *symbol) } if(fsvg == NULL) { strcpy(symbol->errtxt, "Could not open output file"); - return ERROR_FILE_ACCESS; + return ZINT_ERROR_FILE_ACCESS; } /* sort out colour options */ @@ -76,21 +76,21 @@ int svg_plot(struct zint_symbol *symbol) if(strlen(symbol->fgcolour) != 6) { strcpy(symbol->errtxt, "Malformed foreground colour target"); - return ERROR_INVALID_OPTION; + return ZINT_ERROR_INVALID_OPTION; } if(strlen(symbol->bgcolour) != 6) { strcpy(symbol->errtxt, "Malformed background colour target"); - return ERROR_INVALID_OPTION; + return ZINT_ERROR_INVALID_OPTION; } error_number = is_sane(SSET, (unsigned char*)symbol->fgcolour, strlen(symbol->fgcolour)); - if (error_number == ERROR_INVALID_DATA) { + if (error_number == ZINT_ERROR_INVALID_DATA) { strcpy(symbol->errtxt, "Malformed foreground colour target"); - return ERROR_INVALID_OPTION; + return ZINT_ERROR_INVALID_OPTION; } error_number = is_sane(SSET, (unsigned char*)symbol->bgcolour, strlen(symbol->bgcolour)); - if (error_number == ERROR_INVALID_DATA) { + if (error_number == ZINT_ERROR_INVALID_DATA) { strcpy(symbol->errtxt, "Malformed background colour target"); - return ERROR_INVALID_OPTION; + return ZINT_ERROR_INVALID_OPTION; } locale = setlocale(LC_ALL, "C"); diff --git a/backend/telepen.c b/backend/telepen.c index c0853e24..68ab4494 100644 --- a/backend/telepen.c +++ b/backend/telepen.c @@ -68,7 +68,7 @@ int telepen(struct zint_symbol *symbol, unsigned char source[], int src_len) if(src_len > 30) { strcpy(symbol->errtxt, "Input too long"); - return ERROR_TOO_LONG; + return ZINT_ERROR_TOO_LONG; } /* Start character */ strcpy(dest, TeleTable['_']); @@ -77,7 +77,7 @@ int telepen(struct zint_symbol *symbol, unsigned char source[], int src_len) if(source[i] > 126) { /* Cannot encode extended ASCII */ strcpy(symbol->errtxt, "Invalid characters in input data"); - return ERROR_INVALID_DATA; + return ZINT_ERROR_INVALID_DATA; } concat(dest, TeleTable[source[i]]); count += source[i]; @@ -114,12 +114,12 @@ int telepen_num(struct zint_symbol *symbol, unsigned char source[], int src_len) if(temp_length > 60) { strcpy(symbol->errtxt, "Input too long"); - return ERROR_TOO_LONG; + return ZINT_ERROR_TOO_LONG; } ustrcpy(temp, source); to_upper(temp); error_number = is_sane(NEON, temp, temp_length); - if(error_number == ERROR_INVALID_DATA) { + if(error_number == ZINT_ERROR_INVALID_DATA) { strcpy(symbol->errtxt, "Invalid characters in data"); return error_number; } @@ -140,7 +140,7 @@ int telepen_num(struct zint_symbol *symbol, unsigned char source[], int src_len) { if(temp[i] == 'X') { strcpy(symbol->errtxt, "Invalid position of X in Telepen data"); - return ERROR_INVALID_DATA; + return ZINT_ERROR_INVALID_DATA; } if(temp[i + 1] == 'X') { diff --git a/backend/upcean.c b/backend/upcean.c index 154cb1a6..c71f5c17 100644 --- a/backend/upcean.c +++ b/backend/upcean.c @@ -421,7 +421,7 @@ int isbn(struct zint_symbol *symbol, unsigned char source[], const unsigned int to_upper(source); error_number = is_sane("0123456789X", source, src_len); - if(error_number == ERROR_INVALID_DATA) { + if(error_number == ZINT_ERROR_INVALID_DATA) { strcpy(symbol->errtxt, "Invalid characters in input"); return error_number; } @@ -430,7 +430,7 @@ int isbn(struct zint_symbol *symbol, unsigned char source[], const unsigned int if(((src_len < 9) || (src_len > 13)) || ((src_len > 10) && (src_len < 13))) { strcpy(symbol->errtxt, "Input wrong length"); - return ERROR_TOO_LONG; + return ZINT_ERROR_TOO_LONG; } if(src_len == 13) /* Using 13 character ISBN */ @@ -439,14 +439,14 @@ int isbn(struct zint_symbol *symbol, unsigned char source[], const unsigned int ((source[2] == '8') || (source[2] == '9')))) { strcpy(symbol->errtxt, "Invalid ISBN"); - return ERROR_INVALID_DATA; + return ZINT_ERROR_INVALID_DATA; } check_digit = isbn13_check(source); if (source[src_len - 1] != check_digit) { strcpy(symbol->errtxt, "Incorrect ISBN check"); - return ERROR_INVALID_CHECK; + return ZINT_ERROR_INVALID_CHECK; } source[12] = '\0'; @@ -459,7 +459,7 @@ int isbn(struct zint_symbol *symbol, unsigned char source[], const unsigned int if(check_digit != source[src_len - 1]) { strcpy(symbol->errtxt, "Incorrect ISBN check"); - return ERROR_INVALID_CHECK; + return ZINT_ERROR_INVALID_CHECK; } for(i = 13; i > 0; i--) { @@ -487,7 +487,7 @@ int isbn(struct zint_symbol *symbol, unsigned char source[], const unsigned int if(check_digit != source[ustrlen(source) - 1]) { strcpy(symbol->errtxt, "Incorrect SBN check"); - return ERROR_INVALID_CHECK; + return ZINT_ERROR_INVALID_CHECK; } /* Convert to EAN-13 number */ @@ -603,18 +603,18 @@ int eanx(struct zint_symbol *symbol, unsigned char source[], int src_len) if(src_len > 19) { strcpy(symbol->errtxt, "Input too long"); - return ERROR_TOO_LONG; + return ZINT_ERROR_TOO_LONG; } if(symbol->symbology != BARCODE_ISBNX) { /* ISBN has it's own checking routine */ error_number = is_sane("0123456789+", source, src_len); - if(error_number == ERROR_INVALID_DATA) { + if(error_number == ZINT_ERROR_INVALID_DATA) { strcpy(symbol->errtxt, "Invalid characters in data"); return error_number; } } else { error_number = is_sane("0123456789Xx", source, src_len); - if(error_number == ERROR_INVALID_DATA) { + if(error_number == ZINT_ERROR_INVALID_DATA) { strcpy(symbol->errtxt, "Invalid characters in input"); return error_number; } @@ -668,7 +668,7 @@ int eanx(struct zint_symbol *symbol, unsigned char source[], int src_len) case 5: add_on(first_part, (char*)dest, 0); ustrcpy(symbol->text, first_part); break; case 7: ean8(symbol, first_part, (char*)dest); break; case 12: ean13(symbol, first_part, (char*)dest); break; - default: strcpy(symbol->errtxt, "Invalid length input"); return ERROR_TOO_LONG; + default: strcpy(symbol->errtxt, "Invalid length input"); return ZINT_ERROR_TOO_LONG; } break; case BARCODE_EANX_CC: @@ -696,7 +696,7 @@ int eanx(struct zint_symbol *symbol, unsigned char source[], int src_len) symbol->row_height[symbol->rows + 2] = 2; symbol->rows += 3; ean13(symbol, first_part, (char*)dest); break; - default: strcpy(symbol->errtxt, "Invalid length EAN input"); return ERROR_TOO_LONG; + default: strcpy(symbol->errtxt, "Invalid length EAN input"); return ZINT_ERROR_TOO_LONG; } break; case BARCODE_UPCA: @@ -704,7 +704,7 @@ int eanx(struct zint_symbol *symbol, unsigned char source[], int src_len) upca(symbol, first_part, (char*)dest); } else { strcpy(symbol->errtxt, "Input wrong length"); - return ERROR_TOO_LONG; + return ZINT_ERROR_TOO_LONG; } break; case BARCODE_UPCA_CC: @@ -722,7 +722,7 @@ int eanx(struct zint_symbol *symbol, unsigned char source[], int src_len) upca(symbol, first_part, (char*)dest); } else { strcpy(symbol->errtxt, "UPCA input wrong length"); - return ERROR_TOO_LONG; + return ZINT_ERROR_TOO_LONG; } break; case BARCODE_UPCE: @@ -730,7 +730,7 @@ int eanx(struct zint_symbol *symbol, unsigned char source[], int src_len) upce(symbol, first_part, (char*)dest); } else { strcpy(symbol->errtxt, "Input wrong length"); - return ERROR_TOO_LONG; + return ZINT_ERROR_TOO_LONG; } break; case BARCODE_UPCE_CC: @@ -748,7 +748,7 @@ int eanx(struct zint_symbol *symbol, unsigned char source[], int src_len) upce(symbol, first_part, (char*)dest); } else { strcpy(symbol->errtxt, "UPCE input wrong length"); - return ERROR_TOO_LONG; + return ZINT_ERROR_TOO_LONG; } break; case BARCODE_ISBNX: @@ -773,7 +773,7 @@ int eanx(struct zint_symbol *symbol, unsigned char source[], int src_len) break; default: strcpy(symbol->errtxt, "Invalid length input"); - return ERROR_TOO_LONG; + return ZINT_ERROR_TOO_LONG; } expand(symbol, (char*)dest); diff --git a/backend/zint.h b/backend/zint.h index 59449f6d..6aec3659 100644 --- a/backend/zint.h +++ b/backend/zint.h @@ -203,14 +203,14 @@ struct zint_symbol { #define DM_SQUARE 100 -#define WARN_INVALID_OPTION 2 -#define ERROR_TOO_LONG 5 -#define ERROR_INVALID_DATA 6 -#define ERROR_INVALID_CHECK 7 -#define ERROR_INVALID_OPTION 8 -#define ERROR_ENCODING_PROBLEM 9 -#define ERROR_FILE_ACCESS 10 -#define ERROR_MEMORY 11 +#define ZINT_WARN_INVALID_OPTION 2 +#define ZINT_ERROR_TOO_LONG 5 +#define ZINT_ERROR_INVALID_DATA 6 +#define ZINT_ERROR_INVALID_CHECK 7 +#define ZINT_ERROR_INVALID_OPTION 8 +#define ZINT_ERROR_ENCODING_PROBLEM 9 +#define ZINT_ERROR_FILE_ACCESS 10 +#define ZINT_ERROR_MEMORY 11 #if defined(__WIN32__) || defined(_WIN32) || defined(WIN32) || defined(_MSC_VER) # if defined (DLL_EXPORT) || defined(PIC) || defined(_USRDLL) diff --git a/backend_qt4/qzint.cpp b/backend_qt4/qzint.cpp index da7793e5..ee0209ac 100644 --- a/backend_qt4/qzint.cpp +++ b/backend_qt4/qzint.cpp @@ -79,7 +79,7 @@ void QZint::encode() QByteArray pstr=m_primaryMessage.left(99).toAscii(); strcpy(m_zintSymbol->primary,pstr.data()); int error = ZBarcode_Encode(m_zintSymbol, (unsigned char*)bstr.data(), bstr.length()); - if (error > WARN_INVALID_OPTION) + if (error > ZINT_WARN_INVALID_OPTION) m_lastError=m_zintSymbol->errtxt; if (m_zintSymbol->symbology == BARCODE_MAXICODE) @@ -282,10 +282,10 @@ bool QZint::save_to_file(QString filename) strcpy(m_zintSymbol->fgcolour,fgcol.data()); strcpy(m_zintSymbol->bgcolour,bgcol.data()); int error = ZBarcode_Encode(m_zintSymbol, (unsigned char*)bstr.data(), bstr.length()); - if (error > WARN_INVALID_OPTION) + if (error > ZINT_WARN_INVALID_OPTION) m_lastError=m_zintSymbol->errtxt; error = ZBarcode_Print(m_zintSymbol, 0); - if (error > WARN_INVALID_OPTION) + if (error > ZINT_WARN_INVALID_OPTION) m_lastError=m_zintSymbol->errtxt; if(error == 0) { return true; } else { return false; } } diff --git a/frontend/main.c b/frontend/main.c index 9c26c97d..a247a6e1 100644 --- a/frontend/main.c +++ b/frontend/main.c @@ -117,7 +117,7 @@ int validator(char test_string[], char source[]) for(j = 0; j < strlen(test_string); j++) { if (source[i] == test_string[j]) { latch = 1; } } if (!(latch)) { - return ERROR_INVALID_DATA; } + return ZINT_ERROR_INVALID_DATA; } } return 0; @@ -207,7 +207,7 @@ int batch_process(struct zint_symbol *symbol, char *filename) strcpy(format_string, symbol->outfile); } else { strcpy(symbol->errtxt, "Format string too long"); - return ERROR_INVALID_DATA; + return ZINT_ERROR_INVALID_DATA; } } memset(adjusted, 0, sizeof(char) * 2); @@ -218,7 +218,7 @@ int batch_process(struct zint_symbol *symbol, char *filename) file = fopen(filename, "rb"); if (!file) { strcpy(symbol->errtxt, "Unable to read input file"); - return ERROR_INVALID_DATA; + return ZINT_ERROR_INVALID_DATA; } } @@ -452,7 +452,7 @@ int main(int argc, char **argv) } if(!strcmp(long_options[option_index].name, "border")) { error_number = validator(NESET, optarg); - if(error_number == ERROR_INVALID_DATA) { + if(error_number == ZINT_ERROR_INVALID_DATA) { fprintf(stderr, "Invalid border width\n"); exit(1); } @@ -465,7 +465,7 @@ int main(int argc, char **argv) } if(!strcmp(long_options[option_index].name, "height")) { error_number = validator(NESET, optarg); - if(error_number == ERROR_INVALID_DATA) { + if(error_number == ZINT_ERROR_INVALID_DATA) { fprintf(stderr, "Invalid symbol height\n"); exit(1); } @@ -520,7 +520,7 @@ int main(int argc, char **argv) if(!strcmp(long_options[option_index].name, "rotate")) { /* Only certain inputs allowed */ error_number = validator(NESET, optarg); - if(error_number == ERROR_INVALID_DATA) { + if(error_number == ZINT_ERROR_INVALID_DATA) { fprintf(stderr, "Invalid rotation parameter\n"); exit(1); } @@ -547,7 +547,7 @@ int main(int argc, char **argv) case 'b': error_number = validator(NESET, optarg); - if(error_number == ERROR_INVALID_DATA) { + if(error_number == ZINT_ERROR_INVALID_DATA) { fprintf(stderr, "Invalid barcode type\n"); exit(1); } @@ -556,7 +556,7 @@ int main(int argc, char **argv) case 'w': error_number = validator(NESET, optarg); - if(error_number == ERROR_INVALID_DATA) { + if(error_number == ZINT_ERROR_INVALID_DATA) { fprintf(stderr, "Invalid whitespace value\n"); exit(1); } diff --git a/frontend_qt4/sequencewindow.cpp b/frontend_qt4/sequencewindow.cpp index f5076b84..45d700f6 100644 --- a/frontend_qt4/sequencewindow.cpp +++ b/frontend_qt4/sequencewindow.cpp @@ -187,10 +187,8 @@ void SequenceWindow::import() void SequenceWindow::generate_sequence() { - int returnval; - ExportWindow dlg; dlg.barcode = barcode; dlg.output_data = txtPreview->toPlainText(); - returnval = dlg.exec(); + dlg.exec(); }