Slight code reduction - use loops when converting to or from binary

This commit is contained in:
Robin Stuart 2016-09-17 18:22:26 +01:00
parent 0dd072437e
commit b13efe334a
4 changed files with 97 additions and 487 deletions

View File

@ -53,6 +53,7 @@ static void mapshorten(int *charmap, int *typemap, const int start, const int le
/**
* Insert a character into the middle of a string at position posn
*/
/*
static void insert(char binary_string[], const size_t posn, const char newbit) {
size_t i, end;
@ -62,6 +63,7 @@ static void insert(char binary_string[], const size_t posn, const char newbit) {
}
binary_string[posn] = newbit;
}
*/
/**
* Encode input data into a binary string
@ -1383,60 +1385,27 @@ int aztec(struct zint_symbol *symbol, unsigned char source[], int length) {
descriptor[2] = '0';
}
}
if ((data_blocks - 1) & 0x10) {
descriptor[3] = '1';
} else {
descriptor[3] = '0';
}
if ((data_blocks - 1) & 0x08) {
descriptor[4] = '1';
} else {
descriptor[4] = '0';
}
if ((data_blocks - 1) & 0x04) {
descriptor[5] = '1';
} else {
descriptor[5] = '0';
}
if ((data_blocks - 1) & 0x02) {
descriptor[6] = '1';
} else {
descriptor[6] = '0';
}
if ((data_blocks - 1) & 0x01) {
descriptor[7] = '1';
} else {
descriptor[7] = '0';
for (i = 3; i < 8; i++) {
if ((data_blocks - 1) & (0x10 >> (i - 3))) {
descriptor[i] = '1';
} else {
descriptor[i] = '0';
}
}
descriptor[8] = '\0';
if (debug) printf("Mode Message = %s\n", descriptor);
} else {
/* The first 5 bits represent the number of layers minus 1 */
if ((layers - 1) & 0x10) {
descriptor[0] = '1';
} else {
descriptor[0] = '0';
}
if ((layers - 1) & 0x08) {
descriptor[1] = '1';
} else {
descriptor[1] = '0';
}
if ((layers - 1) & 0x04) {
descriptor[2] = '1';
} else {
descriptor[2] = '0';
}
if ((layers - 1) & 0x02) {
descriptor[3] = '1';
} else {
descriptor[3] = '0';
}
if ((layers - 1) & 0x01) {
descriptor[4] = '1';
} else {
descriptor[4] = '0';
for (i = 0; i < 5; i++) {
if ((layers - 1) & (0x10 >> i)) {
descriptor[i] = '1';
} else {
descriptor[i] = '0';
}
}
/* The next 11 bits represent the number of data blocks minus 1 */
if (reader) {
descriptor[5] = '1';
@ -1447,55 +1416,12 @@ int aztec(struct zint_symbol *symbol, unsigned char source[], int length) {
descriptor[5] = '0';
}
}
if ((data_blocks - 1) & 0x200) {
descriptor[6] = '1';
} else {
descriptor[6] = '0';
}
if ((data_blocks - 1) & 0x100) {
descriptor[7] = '1';
} else {
descriptor[7] = '0';
}
if ((data_blocks - 1) & 0x80) {
descriptor[8] = '1';
} else {
descriptor[8] = '0';
}
if ((data_blocks - 1) & 0x40) {
descriptor[9] = '1';
} else {
descriptor[9] = '0';
}
if ((data_blocks - 1) & 0x20) {
descriptor[10] = '1';
} else {
descriptor[10] = '0';
}
if ((data_blocks - 1) & 0x10) {
descriptor[11] = '1';
} else {
descriptor[11] = '0';
}
if ((data_blocks - 1) & 0x08) {
descriptor[12] = '1';
} else {
descriptor[12] = '0';
}
if ((data_blocks - 1) & 0x04) {
descriptor[13] = '1';
} else {
descriptor[13] = '0';
}
if ((data_blocks - 1) & 0x02) {
descriptor[14] = '1';
} else {
descriptor[14] = '0';
}
if ((data_blocks - 1) & 0x01) {
descriptor[15] = '1';
} else {
descriptor[15] = '0';
for (i = 6; i < 16; i++) {
if ((data_blocks - 1) & (0x200 >> (i - 6))) {
descriptor[i] = '1';
} else {
descriptor[i] = '0';
}
}
descriptor[16] = '\0';
if (debug) printf("Mode Message = %s\n", descriptor);

View File

@ -887,26 +887,14 @@ int c1_encode(struct zint_symbol *symbol, unsigned char source[], unsigned int t
if (decimal_count >= 1) {
int sub_value = ctoi(source[sp]) + 1;
if (sub_value & 0x08) {
strcat(decimal_binary, "1");
} else {
strcat(decimal_binary, "0");
}
if (sub_value & 0x04) {
strcat(decimal_binary, "1");
} else {
strcat(decimal_binary, "0");
}
if (sub_value & 0x02) {
strcat(decimal_binary, "1");
} else {
strcat(decimal_binary, "0");
}
if (sub_value & 0x01) {
strcat(decimal_binary, "1");
} else {
strcat(decimal_binary, "0");
for (i = 0x08; i > 0; i = i >> 1) {
if (sub_value & i) {
strcat(decimal_binary, "1");
} else {
strcat(decimal_binary, "0");
}
}
sp++;
} else {
strcat(decimal_binary, "1111");
@ -920,87 +908,33 @@ int c1_encode(struct zint_symbol *symbol, unsigned char source[], unsigned int t
/* Binary buffer is full - transfer to target */
if (target_count >= 1) {
sub_target = 0;
if (decimal_binary[0] == '1') {
sub_target += 128;
}
if (decimal_binary[1] == '1') {
sub_target += 64;
}
if (decimal_binary[2] == '1') {
sub_target += 32;
}
if (decimal_binary[3] == '1') {
sub_target += 16;
}
if (decimal_binary[4] == '1') {
sub_target += 8;
}
if (decimal_binary[5] == '1') {
sub_target += 4;
}
if (decimal_binary[6] == '1') {
sub_target += 2;
}
if (decimal_binary[7] == '1') {
sub_target += 1;
for (i = 0; i < 8; i++) {
if (decimal_binary[i] == '1') {
sub_target += 128 >> i;
}
}
target[tp] = sub_target;
tp++;
}
if (target_count >= 2) {
sub_target = 0;
if (decimal_binary[8] == '1') {
sub_target += 128;
}
if (decimal_binary[9] == '1') {
sub_target += 64;
}
if (decimal_binary[10] == '1') {
sub_target += 32;
}
if (decimal_binary[11] == '1') {
sub_target += 16;
}
if (decimal_binary[12] == '1') {
sub_target += 8;
}
if (decimal_binary[13] == '1') {
sub_target += 4;
}
if (decimal_binary[14] == '1') {
sub_target += 2;
}
if (decimal_binary[15] == '1') {
sub_target += 1;
for (i = 0; i < 8; i++) {
if (decimal_binary[i + 8] == '1') {
sub_target += 128 >> i;
}
}
target[tp] = sub_target;
tp++;
}
if (target_count == 3) {
sub_target = 0;
if (decimal_binary[16] == '1') {
sub_target += 128;
}
if (decimal_binary[17] == '1') {
sub_target += 64;
}
if (decimal_binary[18] == '1') {
sub_target += 32;
}
if (decimal_binary[19] == '1') {
sub_target += 16;
}
if (decimal_binary[20] == '1') {
sub_target += 8;
}
if (decimal_binary[21] == '1') {
sub_target += 4;
}
if (decimal_binary[22] == '1') {
sub_target += 2;
}
if (decimal_binary[23] == '1') {
sub_target += 1;
for (i = 0; i < 8; i++) {
if (decimal_binary[i + 16] == '1') {
sub_target += 128 >> i;
}
}
target[tp] = sub_target;
tp++;
@ -1183,87 +1117,33 @@ int c1_encode(struct zint_symbol *symbol, unsigned char source[], unsigned int t
/* Binary buffer is full - transfer to target */
if (target_count >= 1) {
sub_target = 0;
if (decimal_binary[0] == '1') {
sub_target += 128;
}
if (decimal_binary[1] == '1') {
sub_target += 64;
}
if (decimal_binary[2] == '1') {
sub_target += 32;
}
if (decimal_binary[3] == '1') {
sub_target += 16;
}
if (decimal_binary[4] == '1') {
sub_target += 8;
}
if (decimal_binary[5] == '1') {
sub_target += 4;
}
if (decimal_binary[6] == '1') {
sub_target += 2;
}
if (decimal_binary[7] == '1') {
sub_target += 1;
for (i = 0; i < 8; i++) {
if (decimal_binary[i] == '1') {
sub_target += 128 >> i;
}
}
target[tp] = sub_target;
tp++;
}
if (target_count >= 2) {
sub_target = 0;
if (decimal_binary[8] == '1') {
sub_target += 128;
}
if (decimal_binary[9] == '1') {
sub_target += 64;
}
if (decimal_binary[10] == '1') {
sub_target += 32;
}
if (decimal_binary[11] == '1') {
sub_target += 16;
}
if (decimal_binary[12] == '1') {
sub_target += 8;
}
if (decimal_binary[13] == '1') {
sub_target += 4;
}
if (decimal_binary[14] == '1') {
sub_target += 2;
}
if (decimal_binary[15] == '1') {
sub_target += 1;
for (i = 0; i < 8; i++) {
if (decimal_binary[i + 8] == '1') {
sub_target += 128 >> i;
}
}
target[tp] = sub_target;
tp++;
}
if (target_count == 3) {
sub_target = 0;
if (decimal_binary[16] == '1') {
sub_target += 128;
}
if (decimal_binary[17] == '1') {
sub_target += 64;
}
if (decimal_binary[18] == '1') {
sub_target += 32;
}
if (decimal_binary[19] == '1') {
sub_target += 16;
}
if (decimal_binary[20] == '1') {
sub_target += 8;
}
if (decimal_binary[21] == '1') {
sub_target += 4;
}
if (decimal_binary[22] == '1') {
sub_target += 2;
}
if (decimal_binary[23] == '1') {
sub_target += 1;
for (i = 0; i < 8; i++) {
if (decimal_binary[i + 16] == '1') {
sub_target += 128 >> i;
}
}
target[tp] = sub_target;
tp++;

View File

@ -256,66 +256,6 @@ extern unsigned short USPS_MSB_Math_CRC11GenerateFrameCheckSequence(unsigned cha
return FrameCheckSequence;
}
void breakup(short int fcs_bit[], unsigned short usps_crc) {
int i;
for (i = 0; i < 13; i++) {
fcs_bit[i] = 0;
}
if (usps_crc >= 4096) {
fcs_bit[12] = 1;
usps_crc -= 4096;
}
if (usps_crc >= 2048) {
fcs_bit[11] = 1;
usps_crc -= 2048;
}
if (usps_crc >= 1024) {
fcs_bit[10] = 1;
usps_crc -= 1024;
}
if (usps_crc >= 512) {
fcs_bit[9] = 1;
usps_crc -= 512;
}
if (usps_crc >= 256) {
fcs_bit[8] = 1;
usps_crc -= 256;
}
if (usps_crc >= 128) {
fcs_bit[7] = 1;
usps_crc -= 128;
}
if (usps_crc >= 64) {
fcs_bit[6] = 1;
usps_crc -= 64;
}
if (usps_crc >= 32) {
fcs_bit[5] = 1;
usps_crc -= 32;
}
if (usps_crc >= 16) {
fcs_bit[4] = 1;
usps_crc -= 16;
}
if (usps_crc >= 8) {
fcs_bit[3] = 1;
usps_crc -= 8;
}
if (usps_crc >= 4) {
fcs_bit[2] = 1;
usps_crc -= 4;
}
if (usps_crc >= 2) {
fcs_bit[1] = 1;
usps_crc -= 2;
}
if (usps_crc == 1) {
fcs_bit[0] = 1;
}
}
int imail(struct zint_symbol *symbol, unsigned char source[], int length) {
char data_pattern[200];
int error_number;
@ -326,7 +266,7 @@ int imail(struct zint_symbol *symbol, unsigned char source[], int length) {
unsigned short usps_crc;
int codeword[10];
unsigned short characters[10];
short int bit_pattern[13], bar_map[130];
short int bar_map[130];
error_number = 0;
@ -626,20 +566,20 @@ int imail(struct zint_symbol *symbol, unsigned char source[], int length) {
}
}
breakup(bit_pattern, usps_crc);
for (i = 0; i < 10; i++) {
if (bit_pattern[i] == 1) {
if (usps_crc & (1 << i)) {
characters[i] = 0x1FFF - characters[i];
}
}
/* *** Step 6 - Conversion from Characters to the Intelligent Mail Barcode *** */
for (i = 0; i < 10; i++) {
breakup(bit_pattern, characters[i]);
for (j = 0; j < 13; j++) {
bar_map[AppxD_IV[(13 * i) + j] - 1] = bit_pattern[j];
if (characters[i] & (1 << j)) {
bar_map[AppxD_IV[(13 * i) + j] - 1] = 1;
} else {
bar_map[AppxD_IV[(13 * i) + j] - 1] = 0;
}
}
}

View File

@ -1097,7 +1097,6 @@ int apply_bitmask(unsigned char *grid, int size, int ecc_level) {
unsigned char p;
int pattern, penalty[8];
int best_val, best_pattern;
int bit;
#ifndef _MSC_VER
unsigned char mask[size * size];
@ -1183,42 +1182,7 @@ int apply_bitmask(unsigned char *grid, int size, int ecc_level) {
/* 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 (mask[(y * size) + x] & (0x01 << best_pattern)) {
if (grid[(y * size) + x] & 0x01) {
grid[(y * size) + x] = 0x00;
} else {
@ -2098,7 +2062,7 @@ void microqr_expand_binary(char binary_stream[], char full_stream[], int version
}
void micro_qr_m1(char binary_data[]) {
int i, latch;
int i, j, latch;
int bits_total, bits_left, remainder;
int data_codewords, ecc_codewords;
unsigned char data_blocks[4], ecc_blocks[3];
@ -2155,43 +2119,17 @@ void micro_qr_m1(char binary_data[]) {
/* 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;
for (j = 0; j < 8; j++) {
if (binary_data[(i * 8) + j] == '1') {
data_blocks[i] += 0x80 >> j;
}
}
}
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;
for (j = 0; j < 4; j++) {
if (binary_data[16 + j] == '1') {
data_blocks[2] += 0x08 >> j;
}
}
/* Calculate Reed-Solomon error codewords */
@ -2207,7 +2145,7 @@ void micro_qr_m1(char binary_data[]) {
}
void micro_qr_m2(char binary_data[], int ecc_mode) {
int i, latch;
int i, j, latch;
int bits_total, bits_left, remainder;
int data_codewords, ecc_codewords;
unsigned char data_blocks[6], ecc_blocks[7];
@ -2262,29 +2200,11 @@ void micro_qr_m2(char binary_data[], int ecc_mode) {
/* 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;
for (j = 0; j < 8; j++) {
if (binary_data[(i * 8) + j] == '1') {
data_blocks[i] += 0x80 >> j;
}
}
}
@ -2303,7 +2223,7 @@ void micro_qr_m2(char binary_data[], int ecc_mode) {
}
void micro_qr_m3(char binary_data[], int ecc_mode) {
int i, latch;
int i, j, latch;
int bits_total, bits_left, remainder;
int data_codewords, ecc_codewords;
unsigned char data_blocks[12], ecc_blocks[9];
@ -2372,29 +2292,11 @@ void micro_qr_m3(char binary_data[], int ecc_mode) {
/* 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;
for (j = 0; j < 8; j++) {
if (binary_data[(i * 8) + j] == '1') {
data_blocks[i] += 0x80 >> j;
}
}
}
@ -2445,7 +2347,7 @@ void micro_qr_m3(char binary_data[], int ecc_mode) {
}
void micro_qr_m4(char binary_data[], int ecc_mode) {
int i, latch;
int i, j, latch;
int bits_total, bits_left, remainder;
int data_codewords, ecc_codewords;
unsigned char data_blocks[17], ecc_blocks[15];
@ -2507,29 +2409,11 @@ void micro_qr_m4(char binary_data[], int ecc_mode) {
/* 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;
for (j = 0; j < 8; j++) {
if (binary_data[(i * 8) + j] == '1') {
data_blocks[i] += 0x80 >> j;
}
}
}
@ -2671,7 +2555,6 @@ int micro_apply_bitmask(unsigned char *grid, int size) {
unsigned char p;
int pattern, value[8];
int best_val, best_pattern;
int bit;
#ifndef _MSC_VER
unsigned char mask[size * size];
@ -2736,26 +2619,7 @@ int micro_apply_bitmask(unsigned char *grid, int size) {
/* 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 (mask[(y * size) + x] & (0x01 << best_pattern)) {
if (grid[(y * size) + x] & 0x01) {
grid[(y * size) + x] = 0x00;
} else {