first commit
This commit is contained in:
@@ -0,0 +1,351 @@
|
||||
/* common.c - Contains functions needed for a number of barcodes */
|
||||
|
||||
/*
|
||||
libzint - the open source barcode library
|
||||
Copyright (C) 2008 Robin Stuart <robin@zint.org.uk>
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License along
|
||||
with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*/
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "common.h"
|
||||
|
||||
int ustrlen(unsigned char data[]) {
|
||||
/* Local replacement for strlen() with unsigned char strings */
|
||||
int i;
|
||||
for (i=0;data[i];i++);
|
||||
|
||||
return i;
|
||||
}
|
||||
|
||||
void ustrcpy(unsigned char target[], unsigned char source[]) {
|
||||
/* Local replacement for strcpy() with unsigned char strings */
|
||||
int i, len;
|
||||
|
||||
len = ustrlen(source);
|
||||
for(i = 0; i < len; i++) {
|
||||
target[i] = source[i];
|
||||
}
|
||||
target[i] = '\0';
|
||||
}
|
||||
|
||||
void concat(char dest[], char source[])
|
||||
{ /* Concatinates dest[] with the contents of source[], copying /0 as well */
|
||||
unsigned int i, j, n;
|
||||
|
||||
j = strlen(dest);
|
||||
n = strlen(source);
|
||||
for(i = 0; i <= n; i++) {
|
||||
dest[i + j] = source[i]; }
|
||||
}
|
||||
|
||||
void uconcat(unsigned char dest[], unsigned char source[])
|
||||
{ /* Concatinates dest[] with the contents of source[], copying /0 as well */
|
||||
unsigned int i, j;
|
||||
|
||||
j = ustrlen(dest);
|
||||
for(i = 0; i <= ustrlen(source); i++) {
|
||||
dest[i + j] = source[i]; }
|
||||
}
|
||||
|
||||
|
||||
int ctoi(char source)
|
||||
{ /* Converts a character 0-9 to its equivalent integer value */
|
||||
if((source >= '0') && (source <= '9'))
|
||||
return (source - '0');
|
||||
return(source - 'A' + 10);
|
||||
}
|
||||
|
||||
char itoc(int source)
|
||||
{ /* Converts an integer value to its hexadecimal character */
|
||||
if ((source >= 0) && (source <= 9)) {
|
||||
return ('0' + source); }
|
||||
else {
|
||||
return ('A' + (source - 10)); }
|
||||
}
|
||||
|
||||
void to_upper(unsigned char source[])
|
||||
{ /* Converts lower case characters to upper case in a string source[] */
|
||||
unsigned int i, src_len = ustrlen(source);
|
||||
|
||||
for (i = 0; i < src_len; i++) {
|
||||
if ((source[i] >= 'a') && (source[i] <= 'z')) {
|
||||
source [i] = (source[i] - 'a') + 'A'; }
|
||||
}
|
||||
}
|
||||
|
||||
int is_sane(char test_string[], unsigned char source[], int length)
|
||||
{ /* Verifies that a string only uses valid characters */
|
||||
unsigned int i, j, latch;
|
||||
unsigned int lt = strlen(test_string);
|
||||
|
||||
for(i = 0; i < length; i++) {
|
||||
latch = FALSE;
|
||||
for(j = 0; j < lt; j++) {
|
||||
if (source[i] == test_string[j]) {
|
||||
latch = TRUE;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!(latch)) {
|
||||
return ERROR_INVALID_DATA;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int posn(char set_string[], char data)
|
||||
{ /* Returns the position of data in set_string */
|
||||
unsigned int i, n = strlen(set_string);
|
||||
|
||||
for(i = 0; i < n; i++) {
|
||||
if (data == set_string[i]) { return i; } }
|
||||
return 0;
|
||||
}
|
||||
|
||||
void lookup(char set_string[], char *table[], char data, char dest[])
|
||||
{ /* Replaces huge switch statements for looking up in tables */
|
||||
unsigned int i, n = strlen(set_string);
|
||||
|
||||
for(i = 0; i < n; i++) {
|
||||
if (data == set_string[i]) { concat(dest, table[i]); } }
|
||||
}
|
||||
|
||||
int module_is_set(struct zint_symbol *symbol, int y_coord, int x_coord)
|
||||
{
|
||||
return (symbol->encoded_data[y_coord][x_coord / 7] & (1 << (x_coord % 7))) ? 1 : 0;
|
||||
}
|
||||
|
||||
void set_module(struct zint_symbol *symbol, int y_coord, int x_coord)
|
||||
{
|
||||
if(module_is_set(symbol, y_coord, x_coord)) { return; }
|
||||
symbol->encoded_data[y_coord][x_coord / 7] += 1 << (x_coord % 7);
|
||||
}
|
||||
|
||||
void unset_module(struct zint_symbol *symbol, int y_coord, int x_coord)
|
||||
{
|
||||
if(!(module_is_set(symbol, y_coord, x_coord))) { return; }
|
||||
symbol->encoded_data[y_coord][x_coord / 7] -= 1 << (x_coord % 7);
|
||||
}
|
||||
|
||||
void expand(struct zint_symbol *symbol, char data[])
|
||||
{ /* Expands from a width pattern to a bit pattern */
|
||||
|
||||
unsigned int reader, n = strlen(data);
|
||||
int writer, i;
|
||||
char latch;
|
||||
|
||||
writer = 0;
|
||||
latch = '1';
|
||||
|
||||
for(reader = 0; reader < n; reader++) {
|
||||
for(i = 0; i < ctoi(data[reader]); i++) {
|
||||
if(latch == '1') { set_module(symbol, symbol->rows, writer); }
|
||||
writer++;
|
||||
}
|
||||
if(latch == '1') {
|
||||
latch = '0';
|
||||
} else {
|
||||
latch = '1';
|
||||
}
|
||||
}
|
||||
|
||||
if(symbol->symbology != BARCODE_PHARMA) {
|
||||
if(writer > symbol->width) {
|
||||
symbol->width = writer;
|
||||
}
|
||||
} else {
|
||||
/* Pharmacode One ends with a space - adjust for this */
|
||||
if(writer > symbol->width + 2) {
|
||||
symbol->width = writer - 2;
|
||||
}
|
||||
}
|
||||
symbol->rows = symbol->rows + 1;
|
||||
}
|
||||
|
||||
int is_stackable(int symbology) {
|
||||
/* Indicates which symbologies can have row binding */
|
||||
if(symbology < BARCODE_PDF417) { return 1; }
|
||||
if(symbology == BARCODE_CODE128B) { return 1; }
|
||||
if(symbology == BARCODE_ISBNX) { return 1; }
|
||||
if(symbology == BARCODE_EAN14) { return 1; }
|
||||
if(symbology == BARCODE_NVE18) { return 1; }
|
||||
if(symbology == BARCODE_KOREAPOST) { return 1; }
|
||||
if(symbology == BARCODE_PLESSEY) { return 1; }
|
||||
if(symbology == BARCODE_TELEPEN_NUM) { return 1; }
|
||||
if(symbology == BARCODE_ITF14) { return 1; }
|
||||
if(symbology == BARCODE_CODE32) { return 1; }
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int is_extendable(int symbology) {
|
||||
/* Indicates which symbols can have addon */
|
||||
if(symbology == BARCODE_EANX) { return 1; }
|
||||
if(symbology == BARCODE_UPCA) { return 1; }
|
||||
if(symbology == BARCODE_UPCE) { return 1; }
|
||||
if(symbology == BARCODE_ISBNX) { return 1; }
|
||||
if(symbology == BARCODE_UPCA_CC) { return 1; }
|
||||
if(symbology == BARCODE_UPCE_CC) { return 1; }
|
||||
if(symbology == BARCODE_EANX_CC) { return 1; }
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int roundup(float input)
|
||||
{
|
||||
float remainder;
|
||||
int integer_part;
|
||||
|
||||
integer_part = (int)input;
|
||||
remainder = input - integer_part;
|
||||
|
||||
if(remainder > 0.1) {
|
||||
integer_part++;
|
||||
}
|
||||
|
||||
return integer_part;
|
||||
}
|
||||
|
||||
int istwodigits(unsigned char source[], int position)
|
||||
{
|
||||
if((source[position] >= '0') && (source[position] <= '9')) {
|
||||
if((source[position + 1] >= '0') && (source[position + 1] <= '9')) {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
float froundup(float input)
|
||||
{
|
||||
float fraction, output = 0.0;
|
||||
|
||||
fraction = input - (int)input;
|
||||
if(fraction > 0.01) { output = (input - fraction) + 1.0; } else { output = input; }
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
int latin1_process(struct zint_symbol *symbol, unsigned char source[], unsigned char preprocessed[], int *length)
|
||||
{
|
||||
int j, i, next;
|
||||
|
||||
/* Convert Unicode to Latin-1 for those symbologies which only support Latin-1 */
|
||||
j = 0;
|
||||
i = 0;
|
||||
do {
|
||||
next = -1;
|
||||
if(source[i] < 128) {
|
||||
preprocessed[j] = source[i];
|
||||
j++;
|
||||
next = i + 1;
|
||||
} else {
|
||||
if(source[i] == 0xC2) {
|
||||
preprocessed[j] = source[i + 1];
|
||||
j++;
|
||||
next = i + 2;
|
||||
}
|
||||
if(source[i] == 0xC3) {
|
||||
preprocessed[j] = source[i + 1] + 64;
|
||||
j++;
|
||||
next = i + 2;
|
||||
}
|
||||
}
|
||||
if(next == -1) {
|
||||
strcpy(symbol->errtxt, "error: Invalid character in input string (only Latin-1 characters supported)");
|
||||
return ERROR_INVALID_DATA;
|
||||
}
|
||||
i = next;
|
||||
} while(i < *length);
|
||||
preprocessed[j] = '\0';
|
||||
*length = j;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int utf8toutf16(struct zint_symbol *symbol, unsigned char source[], int vals[], int *length)
|
||||
{
|
||||
int bpos, jpos, error_number, done;
|
||||
int next;
|
||||
|
||||
bpos = 0;
|
||||
jpos = 0;
|
||||
error_number = 0;
|
||||
next = 0;
|
||||
|
||||
do {
|
||||
done = 0;
|
||||
|
||||
if(source[bpos] <= 0x7f) {
|
||||
/* 1 byte mode (7-bit ASCII) */
|
||||
vals[jpos] = source[bpos];
|
||||
next = bpos + 1;
|
||||
jpos++;
|
||||
done = 1;
|
||||
}
|
||||
|
||||
if(done == 0) {
|
||||
if((source[bpos] >= 0x80) && (source[bpos] <= 0xbf)) {
|
||||
strcpy(symbol->errtxt, "Corrupt Unicode data");
|
||||
return ERROR_INVALID_DATA;
|
||||
}
|
||||
}
|
||||
|
||||
if(done == 0) {
|
||||
if((source[bpos] >= 0xc0) && (source[bpos] <= 0xc1)) {
|
||||
strcpy(symbol->errtxt, "Overlong encoding not supported");
|
||||
return ERROR_INVALID_DATA;
|
||||
}
|
||||
}
|
||||
|
||||
if(done == 0) {
|
||||
if((source[bpos] >= 0xc2) && (source[bpos] <= 0xdf)) {
|
||||
/* 2 byte mode */
|
||||
vals[jpos] = ((source[bpos] & 0x1f) << 6) + (source[bpos + 1] & 0x3f);
|
||||
next = bpos + 2;
|
||||
jpos++;
|
||||
done = 1;
|
||||
}
|
||||
}
|
||||
|
||||
if(done == 0) {
|
||||
if((source[bpos] >= 0xe0) && (source[bpos] <= 0xef)) {
|
||||
/* 3 byte mode */
|
||||
vals[jpos] = ((source[bpos] & 0x0f) << 12) + ((source[bpos + 1] & 0x3f) << 6) + (source[bpos + 2] & 0x3f);
|
||||
next = bpos + 3;
|
||||
jpos ++;
|
||||
done = 1;
|
||||
}
|
||||
}
|
||||
|
||||
if(done == 0) {
|
||||
if(source[bpos] >= 0xf0) {
|
||||
strcpy(symbol->errtxt, "Unicode sequences of more than 3 bytes not supported");
|
||||
return ERROR_INVALID_DATA;
|
||||
}
|
||||
}
|
||||
|
||||
bpos = next;
|
||||
|
||||
} while(bpos < *length);
|
||||
*length = jpos;
|
||||
|
||||
return error_number;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
/* Used in some logic */
|
||||
#ifndef __COMMON_H
|
||||
#define __COMMON_H
|
||||
|
||||
#ifndef FALSE
|
||||
#define FALSE 0
|
||||
#endif
|
||||
|
||||
#ifndef TRUE
|
||||
#define TRUE 1
|
||||
#endif
|
||||
|
||||
/* The most commonly used set */
|
||||
#define NEON "0123456789"
|
||||
|
||||
#include "zint.h"
|
||||
|
||||
#define U08 unsigned char
|
||||
#define U16 unsigned short
|
||||
#define U32 unsigned int
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif /* __cplusplus */
|
||||
|
||||
extern int ustrlen(unsigned char source[]);
|
||||
extern void ustrcpy(unsigned char target[], unsigned char source[]);
|
||||
extern void concat(char dest[], char source[]);
|
||||
extern void uconcat(unsigned char dest[], unsigned char source[]);
|
||||
extern int ctoi(char source);
|
||||
extern char itoc(int source);
|
||||
extern void to_upper(unsigned char source[]);
|
||||
extern int is_sane(char test_string[], unsigned char source[], int length);
|
||||
extern void lookup(char set_string[], char *table[], char data, char dest[]);
|
||||
extern int posn(char set_string[], char data);
|
||||
extern void expand(struct zint_symbol *symbol, char data[]);
|
||||
extern int is_stackable(int symbology);
|
||||
extern int is_extendable(int symbology);
|
||||
extern int roundup(float input);
|
||||
extern int module_is_set(struct zint_symbol *symbol, int y_coord, int x_coord);
|
||||
extern void set_module(struct zint_symbol *symbol, int y_coord, int x_coord);
|
||||
extern void unset_module(struct zint_symbol *symbol, int y_coord, int x_coord);
|
||||
extern int istwodigits(unsigned char source[], int position);
|
||||
extern float froundup(float input);
|
||||
extern int parunmodd(unsigned char llyth);
|
||||
extern int latin1_process(struct zint_symbol *symbol, unsigned char source[], unsigned char preprocessed[], int *length);
|
||||
extern int utf8toutf16(struct zint_symbol *symbol, unsigned char source[], int vals[], int *length);
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif /* __cplusplus */
|
||||
|
||||
#endif /* __COMMON_H */
|
||||
@@ -0,0 +1,943 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <ctype.h>
|
||||
#include <string.h>
|
||||
#include "reedsol.h"
|
||||
#include "common.h"
|
||||
#include "dm200.h"
|
||||
#include <QDebug>
|
||||
|
||||
static void *safemalloc(int n)
|
||||
{
|
||||
void *p = malloc(n);
|
||||
return p;
|
||||
}
|
||||
|
||||
// Annex M placement alorithm low level
|
||||
static void ecc200placementbit(int *array, int NR, int NC, int r, int c, int p, char b)
|
||||
{
|
||||
if (r < 0) {
|
||||
r += NR;
|
||||
c += 4 - ((NR + 4) % 8);
|
||||
}
|
||||
if (c < 0) {
|
||||
c += NC;
|
||||
r += 4 - ((NC + 4) % 8);
|
||||
}
|
||||
array[r * NC + c] = (p << 3) + b;
|
||||
}
|
||||
|
||||
static void ecc200placementblock(int *array, int NR, int NC, int r,
|
||||
int c, int p)
|
||||
{
|
||||
ecc200placementbit(array, NR, NC, r - 2, c - 2, p, 7);
|
||||
ecc200placementbit(array, NR, NC, r - 2, c - 1, p, 6);
|
||||
ecc200placementbit(array, NR, NC, r - 1, c - 2, p, 5);
|
||||
ecc200placementbit(array, NR, NC, r - 1, c - 1, p, 4);
|
||||
ecc200placementbit(array, NR, NC, r - 1, c - 0, p, 3);
|
||||
ecc200placementbit(array, NR, NC, r - 0, c - 2, p, 2);
|
||||
ecc200placementbit(array, NR, NC, r - 0, c - 1, p, 1);
|
||||
ecc200placementbit(array, NR, NC, r - 0, c - 0, p, 0);
|
||||
}
|
||||
|
||||
static void ecc200placementcornerA(int *array, int NR, int NC, int p)
|
||||
{
|
||||
ecc200placementbit(array, NR, NC, NR - 1, 0, p, 7);
|
||||
ecc200placementbit(array, NR, NC, NR - 1, 1, p, 6);
|
||||
ecc200placementbit(array, NR, NC, NR - 1, 2, p, 5);
|
||||
ecc200placementbit(array, NR, NC, 0, NC - 2, p, 4);
|
||||
ecc200placementbit(array, NR, NC, 0, NC - 1, p, 3);
|
||||
ecc200placementbit(array, NR, NC, 1, NC - 1, p, 2);
|
||||
ecc200placementbit(array, NR, NC, 2, NC - 1, p, 1);
|
||||
ecc200placementbit(array, NR, NC, 3, NC - 1, p, 0);
|
||||
}
|
||||
|
||||
static void ecc200placementcornerB(int *array, int NR, int NC, int p)
|
||||
{
|
||||
ecc200placementbit(array, NR, NC, NR - 3, 0, p, 7);
|
||||
ecc200placementbit(array, NR, NC, NR - 2, 0, p, 6);
|
||||
ecc200placementbit(array, NR, NC, NR - 1, 0, p, 5);
|
||||
ecc200placementbit(array, NR, NC, 0, NC - 4, p, 4);
|
||||
ecc200placementbit(array, NR, NC, 0, NC - 3, p, 3);
|
||||
ecc200placementbit(array, NR, NC, 0, NC - 2, p, 2);
|
||||
ecc200placementbit(array, NR, NC, 0, NC - 1, p, 1);
|
||||
ecc200placementbit(array, NR, NC, 1, NC - 1, p, 0);
|
||||
}
|
||||
|
||||
static void ecc200placementcornerC(int *array, int NR, int NC, int p)
|
||||
{
|
||||
ecc200placementbit(array, NR, NC, NR - 3, 0, p, 7);
|
||||
ecc200placementbit(array, NR, NC, NR - 2, 0, p, 6);
|
||||
ecc200placementbit(array, NR, NC, NR - 1, 0, p, 5);
|
||||
ecc200placementbit(array, NR, NC, 0, NC - 2, p, 4);
|
||||
ecc200placementbit(array, NR, NC, 0, NC - 1, p, 3);
|
||||
ecc200placementbit(array, NR, NC, 1, NC - 1, p, 2);
|
||||
ecc200placementbit(array, NR, NC, 2, NC - 1, p, 1);
|
||||
ecc200placementbit(array, NR, NC, 3, NC - 1, p, 0);
|
||||
}
|
||||
|
||||
static void ecc200placementcornerD(int *array, int NR, int NC, int p)
|
||||
{
|
||||
ecc200placementbit(array, NR, NC, NR - 1, 0, p, 7);
|
||||
ecc200placementbit(array, NR, NC, NR - 1, NC - 1, p, 6);
|
||||
ecc200placementbit(array, NR, NC, 0, NC - 3, p, 5);
|
||||
ecc200placementbit(array, NR, NC, 0, NC - 2, p, 4);
|
||||
ecc200placementbit(array, NR, NC, 0, NC - 1, p, 3);
|
||||
ecc200placementbit(array, NR, NC, 1, NC - 3, p, 2);
|
||||
ecc200placementbit(array, NR, NC, 1, NC - 2, p, 1);
|
||||
ecc200placementbit(array, NR, NC, 1, NC - 1, p, 0);
|
||||
}
|
||||
|
||||
// Annex M placement alorithm main function
|
||||
static void ecc200placement(int *array, int NR, int NC)
|
||||
{
|
||||
int r, c, p;
|
||||
// invalidate
|
||||
for (r = 0; r < NR; r++)
|
||||
for (c = 0; c < NC; c++)
|
||||
array[r * NC + c] = 0;
|
||||
// start
|
||||
p = 1;
|
||||
r = 4;
|
||||
c = 0;
|
||||
do {
|
||||
// check corner
|
||||
if (r == NR && !c)
|
||||
ecc200placementcornerA(array, NR, NC, p++);
|
||||
if (r == NR - 2 && !c && NC % 4)
|
||||
ecc200placementcornerB(array, NR, NC, p++);
|
||||
if (r == NR - 2 && !c && (NC % 8) == 4)
|
||||
ecc200placementcornerC(array, NR, NC, p++);
|
||||
if (r == NR + 4 && c == 2 && !(NC % 8))
|
||||
ecc200placementcornerD(array, NR, NC, p++);
|
||||
// up/right
|
||||
do {
|
||||
if (r < NR && c >= 0 && !array[r * NC + c])
|
||||
ecc200placementblock(array, NR, NC, r, c, p++);
|
||||
r -= 2;
|
||||
c += 2;
|
||||
}
|
||||
while (r >= 0 && c < NC);
|
||||
r++;
|
||||
c += 3;
|
||||
// down/left
|
||||
do {
|
||||
if (r >= 0 && c < NC && !array[r * NC + c])
|
||||
ecc200placementblock(array, NR, NC, r, c, p++);
|
||||
r += 2;
|
||||
c -= 2;
|
||||
}
|
||||
while (r < NR && c >= 0);
|
||||
r += 3;
|
||||
c++;
|
||||
}
|
||||
while (r < NR || c < NC);
|
||||
// unfilled corner
|
||||
if (!array[NR * NC - 1])
|
||||
array[NR * NC - 1] = array[NR * NC - NC - 2] = 1;
|
||||
}
|
||||
|
||||
// calculate and append ecc code, and if necessary interleave
|
||||
static void ecc200(unsigned char *binary, int bytes, int datablock, int rsblock)
|
||||
{
|
||||
int blocks, b;
|
||||
|
||||
blocks = (bytes + 2) / datablock;
|
||||
|
||||
rs_init_gf(0x12d);
|
||||
rs_init_code(rsblock, 1);
|
||||
for (b = 0; b < blocks; b++) {
|
||||
unsigned char buf[256], ecc[256];
|
||||
int n, p = 0;
|
||||
for (n = b; n < bytes; n += blocks)
|
||||
buf[p++] = binary[n];
|
||||
rs_encode(p, buf, ecc);
|
||||
p = rsblock - 1; // comes back reversed
|
||||
for (n = b; n < rsblock * blocks; n += blocks)
|
||||
binary[bytes + n] = ecc[p--];
|
||||
}
|
||||
rs_free();
|
||||
}
|
||||
|
||||
float dmroundup(float input)
|
||||
{
|
||||
float fraction, output = 0.0;
|
||||
|
||||
fraction = input - (int)input;
|
||||
if(fraction > 0.01) { output = (input - fraction) + 1.0; } else { output = input; }
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
/*int istwodigits(unsigned char source[], int position)
|
||||
{
|
||||
if((source[position] >= '0') && (source[position] <= '9')) {
|
||||
if((source[position + 1] >= '0') && (source[position + 1] <= '9')) {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
} */
|
||||
|
||||
int isx12(unsigned char source)
|
||||
{
|
||||
if(source == 13) { return 1; }
|
||||
if(source == 42) { return 1; }
|
||||
if(source == 62) { return 1; }
|
||||
if(source == 32) { return 1; }
|
||||
if((source >= '0') && (source <= '9')) { return 1; }
|
||||
if((source >= 'A') && (source <= 'Z')) { return 1; }
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void dminsert(char binary_string[], int posn, char newbit)
|
||||
{ /* Insert a character into the middle of a string at position posn */
|
||||
int i, end;
|
||||
|
||||
end = strlen(binary_string);
|
||||
for(i = end; i > posn; i--) {
|
||||
binary_string[i] = binary_string[i - 1];
|
||||
}
|
||||
binary_string[posn] = newbit;
|
||||
}
|
||||
|
||||
void insert_value(unsigned char binary_stream[], int posn, int streamlen, char newbit)
|
||||
{
|
||||
int i;
|
||||
|
||||
for(i = streamlen; i > posn; i--) {
|
||||
binary_stream[i] = binary_stream[i - 1];
|
||||
}
|
||||
binary_stream[posn] = newbit;
|
||||
}
|
||||
|
||||
int look_ahead_test(unsigned char source[], int sourcelen, int position, int current_mode, int gs1)
|
||||
{
|
||||
/* A custom version of the 'look ahead test' from Annex P */
|
||||
/* This version is deliberately very reluctant to end a data stream with EDIFACT encoding */
|
||||
|
||||
float ascii_count, c40_count, text_count, x12_count, edf_count, b256_count, best_count;
|
||||
int sp, done, best_scheme;
|
||||
char reduced_char;
|
||||
|
||||
/* step (j) */
|
||||
if(current_mode == DM_ASCII) {
|
||||
ascii_count = 0.0;
|
||||
c40_count = 1.0;
|
||||
text_count = 1.0;
|
||||
x12_count = 1.0;
|
||||
edf_count = 1.0;
|
||||
b256_count = 1.25;
|
||||
} else {
|
||||
ascii_count = 1.0;
|
||||
c40_count = 2.0;
|
||||
text_count = 2.0;
|
||||
x12_count = 2.0;
|
||||
edf_count = 2.0;
|
||||
b256_count = 2.25;
|
||||
}
|
||||
|
||||
switch(current_mode) {
|
||||
case DM_C40: c40_count = 0.0; break;
|
||||
case DM_TEXT: text_count = 0.0; break;
|
||||
case DM_X12: x12_count = 0.0; break;
|
||||
case DM_EDIFACT: edf_count = 0.0; break;
|
||||
case DM_BASE256: b256_count = 0.0; break;
|
||||
}
|
||||
|
||||
for(sp = position; (sp < sourcelen) && (sp <= (position + 8)); sp++) {
|
||||
|
||||
if(source[sp] <= 127) { reduced_char = source[sp]; } else { reduced_char = source[sp] - 127; }
|
||||
|
||||
if((source[sp] >= '0') && (source[sp] <= '9')) { ascii_count += 0.5; } else { ascii_count += 1.0; }
|
||||
if(source[sp] > 127) { ascii_count += 1.0; }
|
||||
|
||||
done = 0;
|
||||
if(reduced_char == ' ') { c40_count += (2.0 / 3.0); done = 1; }
|
||||
if((reduced_char >= '0') && (reduced_char <= '9')) { c40_count += (2.0 / 3.0); done = 1; }
|
||||
if((reduced_char >= 'A') && (reduced_char <= 'Z')) { c40_count += (2.0 / 3.0); done = 1; }
|
||||
if(source[sp] > 127) { c40_count += (4.0 / 3.0); }
|
||||
if(done == 0) { c40_count += (4.0 / 3.0); }
|
||||
|
||||
done = 0;
|
||||
if(reduced_char == ' ') { text_count += (2.0 / 3.0); done = 1; }
|
||||
if((reduced_char >= '0') && (reduced_char <= '9')) { text_count += (2.0 / 3.0); done = 1; }
|
||||
if((reduced_char >= 'a') && (reduced_char <= 'z')) { text_count += (2.0 / 3.0); done = 1; }
|
||||
if(source[sp] > 127) { text_count += (4.0 / 3.0); }
|
||||
if(done == 0) { text_count += (4.0 / 3.0); }
|
||||
|
||||
if(isx12(source[sp])) { x12_count += (2.0 / 3.0); } else { x12_count += 4.0; }
|
||||
|
||||
/* step (p) */
|
||||
done = 0;
|
||||
if((source[sp] >= ' ') && (source[sp] <= '^')) { edf_count += (3.0 / 4.0); } else { edf_count += 6.0; }
|
||||
if(gs1 && (source[sp] == '[')) { edf_count += 6.0; }
|
||||
if(sp >= (sourcelen - 5)) { edf_count += 6.0; } /* MMmmm fudge! */
|
||||
|
||||
/* step (q) */
|
||||
if(gs1 && (source[sp] == '[')) { b256_count += 4.0; } else { b256_count += 1.0; }
|
||||
|
||||
/*printf("lat a%.2f c%.2f t%.2f x%.2f e%.2f b%.2f\n", ascii_count, c40_count, text_count, x12_count, edf_count, b256_count);*/
|
||||
|
||||
}
|
||||
|
||||
/* Round up all the counts to round numbers */
|
||||
|
||||
best_count = ascii_count;
|
||||
best_scheme = DM_ASCII;
|
||||
|
||||
if(b256_count <= best_count) {
|
||||
best_count = b256_count;
|
||||
best_scheme = DM_BASE256;
|
||||
}
|
||||
|
||||
if(edf_count <= best_count) {
|
||||
best_count = edf_count;
|
||||
best_scheme = DM_EDIFACT;
|
||||
}
|
||||
|
||||
if(text_count <= best_count) {
|
||||
best_count = text_count;
|
||||
best_scheme = DM_TEXT;
|
||||
}
|
||||
|
||||
if(x12_count <= best_count) {
|
||||
best_count = x12_count;
|
||||
best_scheme = DM_X12;
|
||||
}
|
||||
|
||||
if(c40_count <= best_count) {
|
||||
best_count = c40_count;
|
||||
best_scheme = DM_C40;
|
||||
}
|
||||
|
||||
return best_scheme;
|
||||
}
|
||||
|
||||
int dm200encode(unsigned char source[], unsigned char target[], int *last_mode)
|
||||
{
|
||||
/* Encodes data using ASCII, C40, Text, X12, EDIFACT or Base 256 modes as appropriate */
|
||||
/* Supports encoding FNC1 in supporting systems */
|
||||
|
||||
int sp, tp, i, gs1;
|
||||
int current_mode, next_mode;
|
||||
int c40_buffer[6], c40_p;
|
||||
int text_buffer[6], text_p;
|
||||
int x12_buffer[6], x12_p;
|
||||
int edifact_buffer[8], edifact_p;
|
||||
int inputlen;
|
||||
char *binary;
|
||||
|
||||
|
||||
inputlen = ustrlen(source);
|
||||
|
||||
binary = (char *)malloc(2 * inputlen);
|
||||
// binary = (char *)QRcode_malloc(2 * inputlen);
|
||||
memset(binary, 0x00, 2*inputlen);
|
||||
|
||||
sp = 0;
|
||||
tp = 0;
|
||||
memset(c40_buffer, 0, 6);
|
||||
c40_p = 0;
|
||||
memset(text_buffer, 0, 6);
|
||||
text_p = 0;
|
||||
memset(x12_buffer, 0, 6);
|
||||
x12_p = 0;
|
||||
memset(edifact_buffer, 0, 8);
|
||||
edifact_p = 0;
|
||||
strcpy(binary, "");
|
||||
|
||||
/* step (a) */
|
||||
current_mode = DM_ASCII;
|
||||
next_mode = DM_ASCII;
|
||||
|
||||
gs1 = 0;
|
||||
|
||||
if(gs1) { target[tp] = 232; tp++; concat(binary, " "); } /* FNC1 */
|
||||
|
||||
while (sp < inputlen) {
|
||||
|
||||
current_mode = next_mode;
|
||||
|
||||
/* step (b) - ASCII encodation */
|
||||
if(current_mode == DM_ASCII) {
|
||||
next_mode = DM_ASCII;
|
||||
|
||||
if(istwodigits(source, sp) && ((sp + 1) != inputlen)) {
|
||||
target[tp] = (10 * ctoi(source[sp])) + ctoi(source[sp + 1]) + 130;
|
||||
tp++; concat(binary, " ");
|
||||
sp += 2;
|
||||
} else {
|
||||
next_mode = look_ahead_test(source, inputlen, sp, current_mode, gs1);
|
||||
|
||||
if(next_mode != DM_ASCII) {
|
||||
switch(next_mode) {
|
||||
case DM_C40: target[tp] = 230; tp++; concat(binary, " "); break;
|
||||
case DM_TEXT: target[tp] = 239; tp++; concat(binary, " "); break;
|
||||
case DM_X12: target[tp] = 238; tp++; concat(binary, " "); break;
|
||||
case DM_EDIFACT: target[tp] = 240; tp++; concat(binary, " "); break;
|
||||
case DM_BASE256: target[tp] = 231; tp++; concat(binary, " "); break;
|
||||
}
|
||||
} else {
|
||||
if(source[sp] > 127) {
|
||||
target[tp] = 235;
|
||||
tp++;
|
||||
target[tp] = (source[sp] - 128) + 1;
|
||||
tp++; concat(binary, " ");
|
||||
} else {
|
||||
if(gs1 && (source[sp] == '[')) {
|
||||
target[tp] = 232; /* FNC1 */
|
||||
} else {
|
||||
target[tp] = source[sp] + 1;
|
||||
}
|
||||
tp++;
|
||||
concat(binary, " ");
|
||||
}
|
||||
sp++;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/* step (c) C40 encodation */
|
||||
if(current_mode == DM_C40) {
|
||||
int shift_set, value;
|
||||
|
||||
next_mode = DM_C40;
|
||||
if(c40_p == 0) {
|
||||
next_mode = look_ahead_test(source, inputlen, sp, current_mode, gs1);
|
||||
}
|
||||
|
||||
if(next_mode != DM_C40) {
|
||||
target[tp] = 254; tp++; concat(binary, " ");/* Unlatch */
|
||||
next_mode = DM_ASCII;
|
||||
} else {
|
||||
if(source[sp] > 127) {
|
||||
c40_buffer[c40_p] = 1; c40_p++;
|
||||
c40_buffer[c40_p] = 30; c40_p++; /* Upper Shift */
|
||||
shift_set = c40_shift[source[sp] - 128];
|
||||
value = c40_value[source[sp] - 128];
|
||||
} else {
|
||||
shift_set = c40_shift[source[sp]];
|
||||
value = c40_value[source[sp]];
|
||||
}
|
||||
|
||||
if(gs1 && (source[sp] == '[')) {
|
||||
shift_set = 2;
|
||||
value = 27; /* FNC1 */
|
||||
}
|
||||
|
||||
if(shift_set != 0) {
|
||||
c40_buffer[c40_p] = shift_set - 1; c40_p++;
|
||||
}
|
||||
c40_buffer[c40_p] = value; c40_p++;
|
||||
|
||||
if(c40_p >= 3) {
|
||||
int iv;
|
||||
|
||||
iv = (1600 * c40_buffer[0]) + (40 * c40_buffer[1]) + (c40_buffer[2]) + 1;
|
||||
target[tp] = iv / 256; tp++;
|
||||
target[tp] = iv % 256; tp++;
|
||||
concat(binary, " ");
|
||||
|
||||
c40_buffer[0] = c40_buffer[3];
|
||||
c40_buffer[1] = c40_buffer[4];
|
||||
c40_buffer[2] = c40_buffer[5];
|
||||
c40_buffer[3] = 0;
|
||||
c40_buffer[4] = 0;
|
||||
c40_buffer[5] = 0;
|
||||
c40_p -= 3;
|
||||
}
|
||||
sp++;
|
||||
}
|
||||
}
|
||||
|
||||
/* step (d) Text encodation */
|
||||
if(current_mode == DM_TEXT) {
|
||||
int shift_set, value;
|
||||
|
||||
next_mode = DM_TEXT;
|
||||
if(text_p == 0) {
|
||||
next_mode = look_ahead_test(source, inputlen, sp, current_mode, gs1);
|
||||
}
|
||||
|
||||
if(next_mode != DM_TEXT) {
|
||||
target[tp] = 254; tp++; concat(binary, " ");/* Unlatch */
|
||||
next_mode = DM_ASCII;
|
||||
|
||||
} else {
|
||||
if(source[sp] > 127) {
|
||||
text_buffer[text_p] = 1; text_p++;
|
||||
text_buffer[text_p] = 30; text_p++; /* Upper Shift */
|
||||
shift_set = text_shift[source[sp] - 128];
|
||||
value = text_value[source[sp] - 128];
|
||||
} else {
|
||||
shift_set = text_shift[source[sp]];
|
||||
value = text_value[source[sp]];
|
||||
}
|
||||
|
||||
if(gs1 && (source[sp] == '[')) {
|
||||
shift_set = 2;
|
||||
value = 27; /* FNC1 */
|
||||
}
|
||||
|
||||
if(shift_set != 0) {
|
||||
text_buffer[text_p] = shift_set - 1; text_p++;
|
||||
}
|
||||
text_buffer[text_p] = value; text_p++;
|
||||
|
||||
if(text_p >= 3) {
|
||||
int iv;
|
||||
|
||||
iv = (1600 * text_buffer[0]) + (40 * text_buffer[1]) + (text_buffer[2]) + 1;
|
||||
target[tp] = iv / 256; tp++;
|
||||
target[tp] = iv % 256; tp++;
|
||||
concat(binary, " ");
|
||||
|
||||
text_buffer[0] = text_buffer[3];
|
||||
text_buffer[1] = text_buffer[4];
|
||||
text_buffer[2] = text_buffer[5];
|
||||
text_buffer[3] = 0;
|
||||
text_buffer[4] = 0;
|
||||
text_buffer[5] = 0;
|
||||
text_p -= 3;
|
||||
}
|
||||
sp++;
|
||||
}
|
||||
}
|
||||
|
||||
/* step (e) X12 encodation */
|
||||
if(current_mode == DM_X12) {
|
||||
int value = 0;
|
||||
|
||||
next_mode = DM_X12;
|
||||
if(text_p == 0) {
|
||||
next_mode = look_ahead_test(source, inputlen, sp, current_mode, gs1);
|
||||
}
|
||||
|
||||
if(next_mode != DM_X12) {
|
||||
target[tp] = 254; tp++; concat(binary, " ");/* Unlatch */
|
||||
} else {
|
||||
if(source[sp] == 13) { value = 0; }
|
||||
if(source[sp] == '*') { value = 1; }
|
||||
if(source[sp] == '>') { value = 2; }
|
||||
if(source[sp] == ' ') { value = 3; }
|
||||
if((source[sp] >= '0') && (source[sp] <= '9')) { value = (source[sp] - '0') + 4; }
|
||||
if((source[sp] >= 'A') && (source[sp] <= 'Z')) { value = (source[sp] - 'A') + 14; }
|
||||
|
||||
x12_buffer[x12_p] = value; x12_p++;
|
||||
|
||||
if(x12_p >= 3) {
|
||||
int iv;
|
||||
|
||||
iv = (1600 * x12_buffer[0]) + (40 * x12_buffer[1]) + (x12_buffer[2]) + 1;
|
||||
target[tp] = iv / 256; tp++;
|
||||
target[tp] = iv % 256; tp++;
|
||||
concat(binary, " ");
|
||||
|
||||
x12_buffer[0] = x12_buffer[3];
|
||||
x12_buffer[1] = x12_buffer[4];
|
||||
x12_buffer[2] = x12_buffer[5];
|
||||
x12_buffer[3] = 0;
|
||||
x12_buffer[4] = 0;
|
||||
x12_buffer[5] = 0;
|
||||
x12_p -= 3;
|
||||
}
|
||||
sp++;
|
||||
}
|
||||
}
|
||||
|
||||
/* step (f) EDIFACT encodation */
|
||||
if(current_mode == DM_EDIFACT) {
|
||||
int value = 0;
|
||||
|
||||
next_mode = DM_EDIFACT;
|
||||
if(edifact_p == 3) {
|
||||
next_mode = look_ahead_test(source, inputlen, sp, current_mode, gs1);
|
||||
}
|
||||
|
||||
if(next_mode != DM_EDIFACT) {
|
||||
edifact_buffer[edifact_p] = 31; edifact_p++;
|
||||
next_mode = DM_ASCII;
|
||||
} else {
|
||||
if((source[sp] >= '@') && (source[sp] <= '^')) { value = source[sp] - '@'; }
|
||||
if((source[sp] >= ' ') && (source[sp] <= '?')) { value = source[sp]; }
|
||||
|
||||
edifact_buffer[edifact_p] = value; edifact_p++;
|
||||
sp++;
|
||||
}
|
||||
|
||||
if(edifact_p >= 4) {
|
||||
target[tp] = (edifact_buffer[0] << 2) + ((edifact_buffer[1] & 0x30) >> 4); tp++;
|
||||
target[tp] = ((edifact_buffer[1] & 0x0f) << 4) + ((edifact_buffer[2] & 0x3c) >> 2); tp++;
|
||||
target[tp] = ((edifact_buffer[2] & 0x03) << 6) + edifact_buffer[3]; tp++;
|
||||
concat(binary, " ");
|
||||
|
||||
edifact_buffer[0] = edifact_buffer[4];
|
||||
edifact_buffer[1] = edifact_buffer[5];
|
||||
edifact_buffer[2] = edifact_buffer[6];
|
||||
edifact_buffer[3] = edifact_buffer[7];
|
||||
edifact_buffer[4] = 0;
|
||||
edifact_buffer[5] = 0;
|
||||
edifact_buffer[6] = 0;
|
||||
edifact_buffer[7] = 0;
|
||||
edifact_p -= 4;
|
||||
}
|
||||
}
|
||||
|
||||
/* step (g) Base 256 encodation */
|
||||
if(current_mode == DM_BASE256) {
|
||||
next_mode = look_ahead_test(source, inputlen, sp, current_mode, gs1);
|
||||
|
||||
if(next_mode == DM_BASE256) {
|
||||
target[tp] = source[sp];
|
||||
tp++;
|
||||
sp++;
|
||||
concat(binary, "b");
|
||||
} else {
|
||||
next_mode = DM_ASCII;
|
||||
}
|
||||
}
|
||||
|
||||
if(tp > 1558) {
|
||||
free(binary);
|
||||
return 0;
|
||||
}
|
||||
|
||||
} /* while */
|
||||
|
||||
/* Empty buffers */
|
||||
if(c40_p == 2) {
|
||||
target[tp] = 254; tp++; /* unlatch */
|
||||
target[tp] = source[inputlen - 2] + 1; tp++;
|
||||
target[tp] = source[inputlen - 1] + 1; tp++;
|
||||
concat(binary, " ");
|
||||
current_mode = DM_ASCII;
|
||||
}
|
||||
if(c40_p == 1) {
|
||||
target[tp] = 254; tp++; /* unlatch */
|
||||
target[tp] = source[inputlen - 1] + 1; tp++;
|
||||
concat(binary, " ");
|
||||
current_mode = DM_ASCII;
|
||||
}
|
||||
|
||||
if(text_p == 2) {
|
||||
target[tp] = 254; tp++; /* unlatch */
|
||||
target[tp] = source[inputlen - 2] + 1; tp++;
|
||||
target[tp] = source[inputlen - 1] + 1; tp++;
|
||||
concat(binary, " ");
|
||||
current_mode = DM_ASCII;
|
||||
}
|
||||
if(text_p == 1) {
|
||||
target[tp] = 254; tp++; /* unlatch */
|
||||
target[tp] = source[inputlen - 1] + 1; tp++;
|
||||
concat(binary, " ");
|
||||
current_mode = DM_ASCII;
|
||||
}
|
||||
|
||||
if(x12_p == 2) {
|
||||
target[tp] = 254; tp++; /* unlatch */
|
||||
target[tp] = source[inputlen - 2] + 1; tp++;
|
||||
target[tp] = source[inputlen - 1] + 1; tp++;
|
||||
concat(binary, " ");
|
||||
current_mode = DM_ASCII;
|
||||
}
|
||||
if(x12_p == 1) {
|
||||
target[tp] = 254; tp++; /* unlatch */
|
||||
target[tp] = source[inputlen - 1] + 1; tp++;
|
||||
concat(binary, " ");
|
||||
current_mode = DM_ASCII;
|
||||
}
|
||||
|
||||
/* Add length and randomising algorithm to b256 */
|
||||
i = 0;
|
||||
while (i < tp) {
|
||||
if(binary[i] == 'b') {
|
||||
if((i == 0) || ((i != 0) && (binary[i - 1] != 'b'))) {
|
||||
/* start of binary data */
|
||||
int binary_count; /* length of b256 data */
|
||||
|
||||
for(binary_count = 0; binary[binary_count + i] == 'b'; binary_count++);
|
||||
|
||||
if(binary_count <= 249) {
|
||||
dminsert(binary, i, 'b');
|
||||
insert_value(target, i, tp, binary_count); tp++;
|
||||
} else {
|
||||
dminsert(binary, i, 'b');
|
||||
dminsert(binary, i + 1, 'b');
|
||||
insert_value(target, i, tp, (binary_count / 250) + 249); tp++;
|
||||
insert_value(target, i + 1, tp, binary_count % 250); tp++;
|
||||
}
|
||||
}
|
||||
}
|
||||
i++;
|
||||
}
|
||||
|
||||
for(i = 0; i < tp; i++) {
|
||||
if(binary[i] == 'b') {
|
||||
int prn, temp;
|
||||
|
||||
prn = ((149 * (i + 1)) % 255) + 1;
|
||||
temp = target[i] + prn;
|
||||
if (temp <= 255) { target[i] = temp; } else { target[i] = temp - 256; }
|
||||
}
|
||||
}
|
||||
|
||||
// QString s,s1;
|
||||
// s="";
|
||||
// for(i = 0; i < tp; i++){
|
||||
// s1.sprintf("%02X ", target[i]);
|
||||
// s+=s1;
|
||||
// }
|
||||
//qDebug()<<"bin:"<<tp;
|
||||
|
||||
*(last_mode) = current_mode;
|
||||
free(binary);
|
||||
return tp;
|
||||
}
|
||||
|
||||
void add_tail(unsigned char target[], int tp, int tail_length, int last_mode)
|
||||
{
|
||||
/* adds unlatch and pad bits */
|
||||
int i, prn, temp;
|
||||
|
||||
switch(last_mode) {
|
||||
case DM_C40:
|
||||
case DM_TEXT:
|
||||
case DM_X12:
|
||||
target[tp] = 254; tp++; /* Unlatch */
|
||||
tail_length--;
|
||||
}
|
||||
|
||||
for(i = tail_length; i > 0; i--) {
|
||||
if(i == tail_length) {
|
||||
target[tp] = 129; tp++; /* Pad */
|
||||
} else {
|
||||
prn = ((149 * (tp + 1)) % 253) + 1;
|
||||
temp = 129 + prn;
|
||||
if(temp <= 254) {
|
||||
target[tp] = temp; tp++;
|
||||
} else {
|
||||
target[tp] = temp - 254; tp++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
unsigned char *iec16022ecc200(short *Wptr, short *Hptr, unsigned char *barcode, short* type)
|
||||
|
||||
{
|
||||
int inputlen, i;
|
||||
int binlen;
|
||||
int symbolsize, optionsize, calcsize;
|
||||
int taillength, error_number = 0;
|
||||
int H, W, FH, FW, datablock, bytes, rsblock;
|
||||
int last_mode;
|
||||
unsigned char *grid = 0;
|
||||
unsigned char *source;
|
||||
|
||||
// unsigned char binary[100];
|
||||
unsigned char *binary;
|
||||
binary = (unsigned char *)malloc(2000+10);
|
||||
// binary = (unsigned char *)QRcode_malloc(2000+10);
|
||||
|
||||
#ifdef WIN32
|
||||
printf("New Lib Routine !!! \n");
|
||||
#endif
|
||||
|
||||
source = barcode;
|
||||
|
||||
inputlen = ustrlen(source);
|
||||
|
||||
binlen = dm200encode(barcode, binary, &last_mode);
|
||||
|
||||
if(binlen == 0) {
|
||||
free(binary);
|
||||
return 0;
|
||||
}
|
||||
|
||||
calcsize = 29;
|
||||
for(i = 29; i > -1; i--) {
|
||||
if(matrixbytes[i] > binlen) {
|
||||
calcsize = i;
|
||||
}
|
||||
}
|
||||
short k = *type;
|
||||
if(k != 0)
|
||||
{
|
||||
switch(k)
|
||||
{
|
||||
case 1:
|
||||
{
|
||||
if(calcsize > 2)
|
||||
{
|
||||
*type = -1;
|
||||
}
|
||||
else
|
||||
{
|
||||
calcsize = 2;
|
||||
}
|
||||
}break;
|
||||
case 2:
|
||||
{
|
||||
if(calcsize > 4)
|
||||
{
|
||||
*type = -1;
|
||||
}
|
||||
else
|
||||
{
|
||||
calcsize = 4;
|
||||
}
|
||||
}break;
|
||||
case 3:
|
||||
{
|
||||
if(calcsize > 6)
|
||||
{
|
||||
*type = -1;
|
||||
}
|
||||
else
|
||||
{
|
||||
calcsize = 6;
|
||||
}
|
||||
}break;
|
||||
case 4:
|
||||
{
|
||||
if(calcsize > 9)
|
||||
{
|
||||
*type = -1;
|
||||
}
|
||||
else
|
||||
{
|
||||
calcsize = 9;
|
||||
}
|
||||
}break;
|
||||
case 5:
|
||||
{
|
||||
if(calcsize > 11)
|
||||
{
|
||||
*type = -1;
|
||||
}
|
||||
else
|
||||
{
|
||||
calcsize = 11;
|
||||
}
|
||||
}break;
|
||||
case 6:
|
||||
{
|
||||
if(calcsize > 14)
|
||||
{
|
||||
*type = -1;
|
||||
}
|
||||
else
|
||||
{
|
||||
calcsize = 14;
|
||||
}
|
||||
}break;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
switch(calcsize) {
|
||||
case 2:
|
||||
case 4:
|
||||
case 6:
|
||||
case 9:
|
||||
case 11:
|
||||
case 14:
|
||||
calcsize++;
|
||||
}
|
||||
}
|
||||
|
||||
//optionsize = -1;
|
||||
optionsize = 10;
|
||||
|
||||
symbolsize = optionsize;
|
||||
|
||||
if(calcsize <= optionsize) {
|
||||
symbolsize = optionsize;
|
||||
} else
|
||||
{
|
||||
symbolsize = calcsize;
|
||||
}
|
||||
|
||||
H = matrixH[symbolsize];
|
||||
W = matrixW[symbolsize];
|
||||
FH = matrixFH[symbolsize];
|
||||
FW = matrixFW[symbolsize];
|
||||
bytes = matrixbytes[symbolsize];
|
||||
datablock = matrixdatablock[symbolsize];
|
||||
rsblock = matrixrsblock[symbolsize];
|
||||
|
||||
taillength = bytes - binlen;
|
||||
|
||||
|
||||
if(taillength != 0) {
|
||||
add_tail(binary, binlen, taillength, last_mode);
|
||||
}
|
||||
|
||||
// ecc code
|
||||
ecc200(binary, bytes, datablock, rsblock);
|
||||
{ // placement
|
||||
int x, y, NC, NR, *places;
|
||||
NC = W - 2 * (W / FW);
|
||||
NR = H - 2 * (H / FH);
|
||||
places = (int*)safemalloc(NC * NR * sizeof(int));
|
||||
// places = (int*)QRcode_malloc(NC * NR * sizeof(int));
|
||||
ecc200placement(places, NR, NC);
|
||||
grid = (unsigned char*)safemalloc(W * H);
|
||||
// grid = (unsigned char*)QRcode_malloc(W * H);
|
||||
memset(grid, 0, W * H);
|
||||
for (y = 0; y < H; y += FH) {
|
||||
for (x = 0; x < W; x++)
|
||||
grid[y * W + x] = 1;
|
||||
for (x = 0; x < W; x += 2)
|
||||
grid[(y + FH - 1) * W + x] = 1;
|
||||
}
|
||||
for (x = 0; x < W; x += FW) {
|
||||
for (y = 0; y < H; y++)
|
||||
grid[y * W + x] = 1;
|
||||
for (y = 0; y < H; y += 2)
|
||||
grid[y * W + x + FW - 1] = 1;
|
||||
}
|
||||
for (y = 0; y < NR; y++) {
|
||||
for (x = 0; x < NC; x++) {
|
||||
int v = places[(NR - y - 1) * NC + x];
|
||||
//fprintf (stderr, "%4d", v);
|
||||
if (v == 1 || (v > 7 && (binary[(v >> 3) - 1] & (1 << (v & 7)))))
|
||||
grid[(1 + y + 2 * (y / (FH - 2))) * W + 1 + x + 2 * (x / (FW - 2))] = 1;
|
||||
}
|
||||
//fprintf (stderr, "\n");
|
||||
}
|
||||
|
||||
for(y = H - 1; y >= 0; y--) {
|
||||
int x;
|
||||
for(x = 0; x < W; x++) {
|
||||
if(grid[W * y + x])
|
||||
{
|
||||
#ifdef WIN32
|
||||
printf("X");
|
||||
#endif
|
||||
}
|
||||
#ifdef WIN32
|
||||
else
|
||||
printf(" ");
|
||||
#endif
|
||||
|
||||
}
|
||||
#ifdef WIN32
|
||||
printf("\n");
|
||||
#endif
|
||||
}
|
||||
|
||||
free(places);
|
||||
}
|
||||
*Wptr = (short)W;
|
||||
*Hptr = (short)H;
|
||||
free(binary);
|
||||
return grid;
|
||||
}
|
||||
|
||||
|
||||
|
||||
+162
@@ -0,0 +1,162 @@
|
||||
/* dm200.h - Handles Data Matrix ECC 200 */
|
||||
|
||||
/*
|
||||
libzint - the open source barcode library
|
||||
Copyright (C) 2009 Robin Stuart <robin@zint.org.uk>
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License along
|
||||
with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*/
|
||||
|
||||
#include "common.h"
|
||||
|
||||
#ifndef __IEC16022ECC200_H
|
||||
#define __IEC16022ECC200_H
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif /* __cplusplus */
|
||||
//void MatrixEnCode(unsigned char *intext);
|
||||
//extern int data_matrix_200(struct zint_symbol *symbol, unsigned char source[]);
|
||||
extern unsigned char *iec16022ecc200(short *Wptr, short *Hptr, unsigned char *barcode, short* type);
|
||||
void Draw_Matrix(unsigned char *buffer);
|
||||
//char DataMatrixdDrawImg(U08 *OutBuff, U16 cRows, U16 cColumns, U08 MSize);
|
||||
//char DataMatrixdDrawImg(U08 *OutBuff, U16 cRows, U16 cColumns, U08 MSize,QImage *pImg);
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif /* __cplusplus */
|
||||
|
||||
#define MAXBARCODE 3116
|
||||
|
||||
#define DM_ASCII 1
|
||||
#define DM_C40 2
|
||||
#define DM_TEXT 3
|
||||
#define DM_X12 4
|
||||
#define DM_EDIFACT 5
|
||||
#define DM_BASE256 6
|
||||
|
||||
|
||||
static int c40_shift[] = {
|
||||
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
|
||||
0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
2, 2, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3 };
|
||||
|
||||
static int c40_value[] = {
|
||||
0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,
|
||||
3,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,4,5,6,7,8,9,10,11,12,13,
|
||||
15,16,17,18,19,20,21,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,
|
||||
22,23,24,25,26,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31 };
|
||||
|
||||
static int text_shift[] = {
|
||||
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
|
||||
0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
|
||||
2, 2, 2, 2, 2, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 3, 3, 3 };
|
||||
|
||||
static int text_value[] = {
|
||||
0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,
|
||||
3,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,4,5,6,7,8,9,10,11,12,13,
|
||||
15,16,17,18,19,20,21,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,
|
||||
22,23,24,25,26,0,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,27,28,29,30,31 };
|
||||
|
||||
static int intsymbol[] = {
|
||||
0,1,3,5,7,8,10,12,13,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,2,4,6,9,11,14 };
|
||||
|
||||
static int matrixH[] = {
|
||||
10, 12, 8, 14, 8, 16, 12, 18, 20, 12, 22, 16, 24, 26, 16, 32, 36, 40, 44, 48,
|
||||
52, 64, 72, 80, 88, 96, 104, 120, 132, 144 };
|
||||
|
||||
static int matrixW[] = {
|
||||
10, 12, 18, 14, 32, 16, 26, 18, 20, 36, 22, 36, 24, 26, 48, 32, 36, 40, 44,
|
||||
48, 52, 64, 72, 80, 88, 96, 104, 120, 132, 144 };
|
||||
|
||||
static int matrixFH[] = {
|
||||
10, 12, 8, 14, 8, 16, 12, 18, 20, 12, 22, 16, 24, 26, 16, 16, 18, 20, 22, 24,
|
||||
26, 16, 18, 20, 22, 24, 26, 20, 22, 24 };
|
||||
|
||||
static int matrixFW[] = {
|
||||
10, 12, 18, 14, 16, 16, 26, 18, 20, 18, 22, 18, 24, 26, 24, 16, 18, 20, 22,
|
||||
24, 26, 16, 18, 20, 22, 24, 26, 20, 22, 24 };
|
||||
|
||||
static int matrixbytes[] = {
|
||||
3, 5, 5, 8, 10, 12, 16, 18, 22, 22, 30, 32, 36, 44, 49, 62, 86, 114, 144,
|
||||
174, 204, 280, 368, 456, 576, 696, 816, 1050, 1304, 1558 };
|
||||
|
||||
static int matrixdatablock[] = {
|
||||
3, 5, 5, 8, 10, 12, 16, 18, 22, 22, 30, 32, 36, 44, 49, 62, 86, 114, 144,
|
||||
174, 102, 140, 92, 114, 144, 174, 136, 175, 163, 156 };
|
||||
|
||||
static int matrixrsblock[] = {
|
||||
5, 7, 7, 10, 11, 12, 14, 14, 18, 18, 20, 24, 24, 28, 28, 36, 42, 48, 56, 68,
|
||||
42, 56, 36, 48, 56, 68, 56, 68, 62, 62 };
|
||||
/*
|
||||
|
||||
static int c40_shift[] = {
|
||||
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
|
||||
0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
2, 2, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3 };
|
||||
|
||||
static int c40_value[] = {
|
||||
0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,
|
||||
3,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,4,5,6,7,8,9,10,11,12,13,
|
||||
15,16,17,18,19,20,21,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,
|
||||
22,23,24,25,26,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31 };
|
||||
|
||||
static int text_shift[] = {
|
||||
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
|
||||
0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
|
||||
2, 2, 2, 2, 2, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 3, 3, 3 };
|
||||
|
||||
static int text_value[] = {
|
||||
0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,
|
||||
3,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,4,5,6,7,8,9,10,11,12,13,
|
||||
15,16,17,18,19,20,21,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,
|
||||
22,23,24,25,26,0,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,27,28,29,30,31 };
|
||||
|
||||
static int intsymbol[] = {
|
||||
0,1,3,5,7,8,10,12,13,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,2,4,6,9,11,14 };
|
||||
|
||||
static int matrixH[] = {
|
||||
10, 12, 8, 14, 8, 16, 12, 18, 20, 12, 22, 16, 24, 26, 16, 32, 36, 40, 44, 48,
|
||||
52, 64, 72, 80, 88, 96, 104, 120, 132, 144 };
|
||||
|
||||
static int matrixW[] = {
|
||||
10, 12, 18, 14, 32, 16, 26, 18, 20, 36, 22, 36, 24, 26, 48, 32, 36, 40, 44,
|
||||
48, 52, 64, 72, 80, 88, 96, 104, 120, 132, 144 };
|
||||
|
||||
static int matrixFH[] = {
|
||||
10, 12, 8, 14, 8, 16, 12, 18, 20, 12, 22, 16, 24, 26, 16, 16, 18, 20, 22, 24,
|
||||
26, 16, 18, 20, 22, 24, 26, 20, 22, 24 };
|
||||
|
||||
static int matrixFW[] = {
|
||||
10, 12, 18, 14, 16, 16, 26, 18, 20, 18, 22, 18, 24, 26, 24, 16, 18, 20, 22,
|
||||
24, 26, 16, 18, 20, 22, 24, 26, 20, 22, 24 };
|
||||
|
||||
static int matrixbytes[] = {
|
||||
3, 5, 5, 8, 10, 12, 16, 18, 22, 22, 30, 32, 36, 44, 49, 62, 86, 114, 144,
|
||||
174, 204, 280, 368, 456, 576, 696, 816, 1050, 1304, 1558 };
|
||||
|
||||
static int matrixdatablock[] = {
|
||||
3, 5, 5, 8, 10, 12, 16, 18, 22, 22, 30, 32, 36, 44, 49, 62, 86, 114, 144,
|
||||
174, 102, 140, 92, 114, 144, 174, 136, 175, 163, 156 };
|
||||
|
||||
static int matrixrsblock[] = {
|
||||
5, 7, 7, 10, 11, 12, 14, 14, 18, 18, 20, 24, 24, 28, 28, 36, 42, 48, 56, 68,
|
||||
42, 56, 36, 48, 56, 68, 56, 68, 62, 62 };
|
||||
|
||||
*/
|
||||
#endif /* __IEC16022ECC200_H */
|
||||
@@ -0,0 +1,68 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
//
|
||||
// U08 *OutBuff, U16 cRows, U16 cColumns, U08 MSize
|
||||
#include "dm200.h"
|
||||
|
||||
#include "QImage"
|
||||
#include "QDebug"
|
||||
|
||||
#define MAX_WIDTH_BYTE 48 // 384/8
|
||||
|
||||
char DataMatrixdDraw(U08 *OutBuff, U16 cRows, U16 cColumns, U08 MSize);
|
||||
//char DataMatrixdDrawImg(U08 *OutBuff, U16 cRows, U16 cColumns, U08 MSize,QImage *pImg);
|
||||
|
||||
void Draw_Matrix(unsigned char *buffer)
|
||||
{
|
||||
U16 sColumns, sRows;
|
||||
U08 *Grid;
|
||||
sColumns = sRows = 0;
|
||||
Grid = 0;
|
||||
sColumns = sRows = 0;
|
||||
short sizeType = 0;
|
||||
Grid = iec16022ecc200((short*)&sRows, (short*)&sColumns, buffer, &sizeType);
|
||||
DataMatrixdDraw(Grid,sRows,sColumns,2);
|
||||
}
|
||||
|
||||
char DataMatrixdDraw(U08 *OutBuff, U16 cRows, U16 cColumns, U08 MSize)
|
||||
{
|
||||
int Size=MSize;
|
||||
int w = cColumns*Size;
|
||||
int h = cRows*Size;
|
||||
//qDebug()<<w<<h<<Size;
|
||||
QImage QRImage(w,h,QImage::Format_ARGB32 );
|
||||
QImage *img;
|
||||
img = &QRImage;
|
||||
img->fill(0x00ffffff);
|
||||
/* data */
|
||||
int margin=0;
|
||||
U08 *p = OutBuff;
|
||||
int ypos=0;//margin * Size;
|
||||
int xpos=0;
|
||||
int x,y,xx,yy,bit;
|
||||
for(y=0; y<cRows; y++) {
|
||||
bit = 7;
|
||||
bit = 7 - (margin * Size % 8);
|
||||
xpos=margin * Size;
|
||||
for(x=0; x<cColumns; x++) {
|
||||
for(xx=0; xx<Size; xx++) {
|
||||
if((*p & 1) << bit){
|
||||
for(yy=0; yy<Size; yy++) {
|
||||
img->setPixel(xpos,h-1-(ypos+yy),0xff000000);
|
||||
}
|
||||
}
|
||||
xpos++;
|
||||
}
|
||||
p++;
|
||||
bit--;
|
||||
if(bit < 0) {
|
||||
//q++;
|
||||
bit = 7;
|
||||
}
|
||||
}
|
||||
ypos+=Size;
|
||||
}
|
||||
img->save("Myqr.bmp");
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,173 @@
|
||||
/**
|
||||
*
|
||||
* This is a simple Reed-Solomon encoder
|
||||
* (C) Cliff Hones 2004
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*
|
||||
*/
|
||||
|
||||
// It is not written with high efficiency in mind, so is probably
|
||||
// not suitable for real-time encoding. The aim was to keep it
|
||||
// simple, general and clear.
|
||||
//
|
||||
// <Some notes on the theory and implementation need to be added here>
|
||||
|
||||
// Usage:
|
||||
// First call rs_init_gf(poly) to set up the Galois Field parameters.
|
||||
// Then call rs_init_code(size, index) to set the encoding size
|
||||
// Then call rs_encode(datasize, data, out) to encode the data.
|
||||
//
|
||||
// These can be called repeatedly as required - but note that
|
||||
// rs_init_code must be called following any rs_init_gf call.
|
||||
//
|
||||
// If the parameters are fixed, some of the statics below can be
|
||||
// replaced with constants in the obvious way, and additionally
|
||||
// malloc/free can be avoided by using static arrays of a suitable
|
||||
// size.
|
||||
|
||||
#include <stdio.h> // only needed for debug (main)
|
||||
#include <stdlib.h> // only needed for malloc/free
|
||||
#include "reedsol.h"
|
||||
|
||||
//#pragma section data begin
|
||||
//#pragma section _sdram
|
||||
int gfpoly;
|
||||
int symsize; // in bits
|
||||
int logmod; // 2**symsize - 1
|
||||
int rlen;
|
||||
|
||||
int *log = NULL, *alog = NULL, *rspoly = NULL;
|
||||
|
||||
void rs_init_gf(int poly)
|
||||
{
|
||||
int m, b, p, v;
|
||||
|
||||
// Return storage from previous setup
|
||||
if (log) {
|
||||
free(log);
|
||||
if (alog)free(alog);
|
||||
if (rspoly)free(rspoly);
|
||||
rspoly = NULL;
|
||||
log =NULL;
|
||||
alog = NULL;
|
||||
}
|
||||
|
||||
// Find the top bit, and hence the symbol size
|
||||
for (b = 1, m = 0; b <= poly; b <<= 1)
|
||||
m++;
|
||||
b >>= 1;
|
||||
m--;
|
||||
gfpoly = poly;
|
||||
symsize = m;
|
||||
|
||||
// Calculate the log/alog tables
|
||||
logmod = (1 << m) - 1;
|
||||
log = (int *)malloc(sizeof(int) * (logmod + 1));
|
||||
alog = (int *)malloc(sizeof(int) * logmod);
|
||||
|
||||
// log = (int *)QRcode_malloc(sizeof(int) * (logmod + 1));
|
||||
// alog = (int *)QRcode_malloc(sizeof(int) * logmod);
|
||||
|
||||
for (p = 1, v = 0; v < logmod; v++) {
|
||||
alog[v] = p;
|
||||
log[p] = v;
|
||||
p <<= 1;
|
||||
if (p & b)
|
||||
p ^= poly;
|
||||
}
|
||||
}
|
||||
|
||||
// rs_init_code(nsym, index) initialises the Reed-Solomon encoder
|
||||
// nsym is the number of symbols to be generated (to be appended
|
||||
// to the input data). index is usually 1 - it is the index of
|
||||
// the constant in the first term (i) of the RS generator polynomial:
|
||||
// (x + 2**i)*(x + 2**(i+1))*... [nsym terms]
|
||||
// For ECC200, index is 1.
|
||||
|
||||
void rs_init_code(int nsym, int index)
|
||||
{
|
||||
int i, k;
|
||||
|
||||
if (rspoly)
|
||||
free(rspoly);
|
||||
rspoly = (int *)malloc(sizeof(int) * (nsym + 1));
|
||||
//rspoly = (int *)QRcode_malloc(sizeof(int) * (nsym + 1));
|
||||
|
||||
rlen = nsym;
|
||||
|
||||
rspoly[0] = 1;
|
||||
for (i = 1; i <= nsym; i++) {
|
||||
rspoly[i] = 1;
|
||||
for (k = i - 1; k > 0; k--) {
|
||||
if (rspoly[k])
|
||||
rspoly[k] = alog[(log[rspoly[k]] + index) % logmod];
|
||||
rspoly[k] ^= rspoly[k - 1];
|
||||
}
|
||||
rspoly[0] = alog[(log[rspoly[0]] + index) % logmod];
|
||||
index++;
|
||||
}
|
||||
}
|
||||
|
||||
void rs_encode(int len, unsigned char *data, unsigned char *res)
|
||||
{
|
||||
int i, k, m;
|
||||
for (i = 0; i < rlen; i++)
|
||||
res[i] = 0;
|
||||
for (i = 0; i < len; i++) {
|
||||
m = res[rlen - 1] ^ data[i];
|
||||
for (k = rlen - 1; k > 0; k--) {
|
||||
if (m && rspoly[k])
|
||||
res[k] = res[k - 1] ^ alog[(log[m] + log[rspoly[k]]) % logmod];
|
||||
else
|
||||
res[k] = res[k - 1];
|
||||
}
|
||||
if (m && rspoly[0])
|
||||
res[0] = alog[(log[m] + log[rspoly[0]]) % logmod];
|
||||
else
|
||||
res[0] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
void rs_encode_long(int len, unsigned int *data, unsigned int *res)
|
||||
{ /* The same as above but for larger bitlengths - Aztec code compatible */
|
||||
int i, k, m;
|
||||
for (i = 0; i < rlen; i++)
|
||||
res[i] = 0;
|
||||
for (i = 0; i < len; i++) {
|
||||
m = res[rlen - 1] ^ data[i];
|
||||
for (k = rlen - 1; k > 0; k--) {
|
||||
if (m && rspoly[k])
|
||||
res[k] = res[k - 1] ^ alog[(log[m] + log[rspoly[k]]) % logmod];
|
||||
else
|
||||
res[k] = res[k - 1];
|
||||
}
|
||||
|
||||
if (m && rspoly[0])
|
||||
res[0] = alog[(log[m] + log[rspoly[0]]) % logmod];
|
||||
else
|
||||
res[0] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
void rs_free(void)
|
||||
{ /* Free memory */
|
||||
free(log);
|
||||
free(alog);
|
||||
free(rspoly);
|
||||
log =NULL;
|
||||
alog = NULL;
|
||||
rspoly = NULL;
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
/**
|
||||
*
|
||||
* This is a simple Reed-Solomon encoder
|
||||
* (C) Cliff Hones 2004
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef __REEDSOL_H
|
||||
#define __REEDSOL_H
|
||||
|
||||
/*#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif */ /* __cplusplus */
|
||||
|
||||
extern void rs_init_gf(int poly);
|
||||
extern void rs_init_code(int nsym, int index);
|
||||
extern void rs_encode(int len, unsigned char *data, unsigned char *res);
|
||||
extern void rs_encode_long(int len, unsigned int *data, unsigned int *res);
|
||||
extern void rs_free(void);
|
||||
|
||||
/*#ifdef __cplusplus
|
||||
}
|
||||
#endif */ /* __cplusplus */
|
||||
|
||||
#endif /* __REEDSOL_H */
|
||||
+209
@@ -0,0 +1,209 @@
|
||||
/* zint.h - definitions for libzint
|
||||
|
||||
libzint - the open source barcode library
|
||||
Copyright (C) 2009 Robin Stuart <robin@zint.org.uk>
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License along
|
||||
with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*/
|
||||
|
||||
#ifndef ZINT_H
|
||||
#define ZINT_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif /* __cplusplus */
|
||||
|
||||
//#pragma section data begin
|
||||
struct zint_symbol {
|
||||
int symbology;
|
||||
int height;
|
||||
int whitespace_width;
|
||||
int border_width;
|
||||
int output_options;
|
||||
char fgcolour[10];
|
||||
char bgcolour[10];
|
||||
char outfile[256];
|
||||
float scale;
|
||||
int option_1;
|
||||
int option_2;
|
||||
int option_3;
|
||||
int show_hrt;
|
||||
int input_mode;
|
||||
unsigned char text[128];
|
||||
int rows;
|
||||
int width;
|
||||
char primary[128];
|
||||
unsigned char encoded_data[178][143];
|
||||
int row_height[178]; /* Largest symbol is 177x177 QR Code */
|
||||
char errtxt[100];
|
||||
char *bitmap;
|
||||
int bitmap_width;
|
||||
int bitmap_height;
|
||||
};
|
||||
|
||||
//#pragma section data end
|
||||
|
||||
/* Tbarcode 7 codes */
|
||||
#define BARCODE_CODE11 1
|
||||
#define BARCODE_C25MATRIX 2
|
||||
#define BARCODE_C25INTER 3
|
||||
#define BARCODE_C25IATA 4
|
||||
#define BARCODE_C25LOGIC 6
|
||||
#define BARCODE_C25IND 7
|
||||
//#define BARCODE_CODE39 8
|
||||
#define BARCODE_EXCODE39 9
|
||||
#define BARCODE_EANX 13
|
||||
//#define BARCODE_EAN128 16
|
||||
//#define BARCODE_CODABAR 18
|
||||
//#define BARCODE_CODE128 20
|
||||
#define BARCODE_DPLEIT 21
|
||||
#define BARCODE_DPIDENT 22
|
||||
#define BARCODE_CODE16K 23
|
||||
#define BARCODE_CODE49 24
|
||||
//#define BARCODE_CODE93 25
|
||||
#define BARCODE_FLAT 28
|
||||
#define BARCODE_RSS14 29
|
||||
#define BARCODE_RSS_LTD 30
|
||||
#define BARCODE_RSS_EXP 31
|
||||
#define BARCODE_TELEPEN 32
|
||||
#define BARCODE_UPCA 34
|
||||
#define BARCODE_UPCE 37
|
||||
//#define BARCODE_POSTNET 40
|
||||
#define BARCODE_MSI_PLESSEY 47
|
||||
#define BARCODE_FIM 49
|
||||
#define BARCODE_LOGMARS 50
|
||||
#define BARCODE_PHARMA 51
|
||||
#define BARCODE_PZN 52
|
||||
#define BARCODE_PHARMA_TWO 53
|
||||
#define BARCODE_PDF417 55
|
||||
#define BARCODE_PDF417TRUNC 56
|
||||
#define BARCODE_MAXICODE 57
|
||||
#define BARCODE_QRCODE 58
|
||||
#define BARCODE_CODE128B 60
|
||||
#define BARCODE_AUSPOST 63
|
||||
#define BARCODE_AUSREPLY 66
|
||||
#define BARCODE_AUSROUTE 67
|
||||
#define BARCODE_AUSREDIRECT 68
|
||||
#define BARCODE_ISBNX 69
|
||||
#define BARCODE_RM4SCC 70
|
||||
#define BARCODE_DATAMATRIX 71
|
||||
#define BARCODE_EAN14 72
|
||||
#define BARCODE_CODABLOCKF 74
|
||||
#define BARCODE_NVE18 75
|
||||
#define BARCODE_JAPANPOST 76
|
||||
#define BARCODE_KOREAPOST 77
|
||||
#define BARCODE_RSS14STACK 79
|
||||
#define BARCODE_RSS14STACK_OMNI 80
|
||||
#define BARCODE_RSS_EXPSTACK 81
|
||||
//#define BARCODE_PLANET 82
|
||||
#define BARCODE_MICROPDF417 84
|
||||
#define BARCODE_ONECODE 85
|
||||
#define BARCODE_PLESSEY 86
|
||||
|
||||
/* Tbarcode 8 codes */
|
||||
#define BARCODE_TELEPEN_NUM 87
|
||||
#define BARCODE_ITF14 89
|
||||
#define BARCODE_KIX 90
|
||||
#define BARCODE_AZTEC 92
|
||||
#define BARCODE_DAFT 93
|
||||
#define BARCODE_MICROQR 97
|
||||
|
||||
/* Tbarcode 9 codes */
|
||||
#define BARCODE_HIBC_128 98
|
||||
#define BARCODE_HIBC_39 99
|
||||
#define BARCODE_HIBC_DM 102
|
||||
#define BARCODE_HIBC_QR 104
|
||||
#define BARCODE_HIBC_PDF 106
|
||||
#define BARCODE_HIBC_MICPDF 108
|
||||
#define BARCODE_HIBC_BLOCKF 110
|
||||
#define BARCODE_HIBC_AZTEC 112
|
||||
|
||||
/* Zint specific */
|
||||
#define BARCODE_AZRUNE 128
|
||||
#define BARCODE_CODE32 129
|
||||
#define BARCODE_EANX_CC 130
|
||||
#define BARCODE_EAN128_CC 131
|
||||
#define BARCODE_RSS14_CC 132
|
||||
#define BARCODE_RSS_LTD_CC 133
|
||||
#define BARCODE_RSS_EXP_CC 134
|
||||
#define BARCODE_UPCA_CC 135
|
||||
#define BARCODE_UPCE_CC 136
|
||||
#define BARCODE_RSS14STACK_CC 137
|
||||
#define BARCODE_RSS14_OMNI_CC 138
|
||||
#define BARCODE_RSS_EXPSTACK_CC 139
|
||||
#define BARCODE_CHANNEL 140
|
||||
#define BARCODE_CODEONE 141
|
||||
#define BARCODE_GRIDMATRIX 142
|
||||
|
||||
#define BARCODE_NO_ASCII 1
|
||||
#define BARCODE_BIND 2
|
||||
#define BARCODE_BOX 4
|
||||
#define BARCODE_STDOUT 8
|
||||
#define READER_INIT 16
|
||||
|
||||
#define DATA_MODE 0
|
||||
#define UNICODE_MODE 1
|
||||
#define GS1_MODE 2
|
||||
#define KANJI_MODE 3
|
||||
#define SJIS_MODE 4
|
||||
|
||||
#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
|
||||
|
||||
#if defined(__WIN32__) || defined(_WIN32) || defined(WIN32) || defined(_MSC_VER)
|
||||
# if defined (DLL_EXPORT) || defined(PIC) || defined(_USRDLL)
|
||||
# define ZINT_EXTERN __declspec(dllexport)
|
||||
# elif defined(ZINT_DLL)
|
||||
# define ZINT_EXTERN __declspec(dllimport)
|
||||
# else
|
||||
# define ZINT_EXTERN extern
|
||||
# endif
|
||||
#else
|
||||
# define ZINT_EXTERN extern
|
||||
#endif
|
||||
|
||||
ZINT_EXTERN struct zint_symbol *ZBarcode_Create(void);
|
||||
ZINT_EXTERN void ZBarcode_Clear(struct zint_symbol *symbol);
|
||||
ZINT_EXTERN void ZBarcode_Delete(struct zint_symbol *symbol);
|
||||
|
||||
ZINT_EXTERN int ZBarcode_Encode(struct zint_symbol *symbol, unsigned char *input, int length);
|
||||
ZINT_EXTERN int ZBarcode_Encode_File(struct zint_symbol *symbol, char *filename);
|
||||
ZINT_EXTERN int ZBarcode_Print(struct zint_symbol *symbol, int rotate_angle);
|
||||
ZINT_EXTERN int ZBarcode_Encode_and_Print(struct zint_symbol *symbol, unsigned char *input, int length, int rotate_angle);
|
||||
ZINT_EXTERN int ZBarcode_Encode_File_and_Print(struct zint_symbol *symbol, char *filename, int rotate_angle);
|
||||
|
||||
ZINT_EXTERN int ZBarcode_Buffer(struct zint_symbol *symbol, int rotate_angle);
|
||||
ZINT_EXTERN int ZBarcode_Encode_and_Buffer(struct zint_symbol *symbol, unsigned char *input, int length, int rotate_angle);
|
||||
ZINT_EXTERN int ZBarcode_Encode_File_and_Buffer(struct zint_symbol *symbol, char *filename, int rotate_angle);
|
||||
|
||||
ZINT_EXTERN int ZBarcode_ValidID(int symbol_id);
|
||||
|
||||
/* Depreciated */
|
||||
ZINT_EXTERN int ZBarcode_Print_Rotated(struct zint_symbol *symbol, int rotate_angle);
|
||||
ZINT_EXTERN int ZBarcode_Encode_and_Print_Rotated(struct zint_symbol *symbol, unsigned char *input, int rotate_angle);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif /* __cplusplus */
|
||||
|
||||
#endif /* ZINT_H */
|
||||
Reference in New Issue
Block a user