This package is a dependency of ePoint HotSpot. It facilitates the generation of 1D barcodes that are printed on tickets to facilitate use with barcode-reader equipped workstations. It can be used for other purposes as well.
Signed-off-by: Daniel A. Nagy <[email protected]> Index: utils/code128b/src/pngenc.c =================================================================== --- utils/code128b/src/pngenc.c (revision 0) +++ utils/code128b/src/pngenc.c (revision 0) @@ -0,0 +1,277 @@ +/** + * barcode - barcode png encoder + * + * Copyright (C) 2009 ePoint Systems Ltd. + * + * Losely based on Kentaro Fukuchi's libqrencode + */ + +#include <stdio.h> +#include <string.h> +#include <stdlib.h> +#include <png.h> +#include <getopt.h> + +#include "code128font.c" + +#define VERSION "0.1" + +static const struct option options[] = { + {"help" , no_argument , NULL, 'h'}, + {"rotate" , no_argument , NULL, 'r'}, + {"output" , required_argument, NULL, 'o'}, + {"size" , required_argument, NULL, 's'}, + {"margin" , required_argument, NULL, 'm'}, + {"height" , required_argument, NULL, 't'}, + {"version" , no_argument , NULL, 'v'}, + {NULL, 0, NULL, 0} +}; + +static char *optstring = "hro:s:m:t:v"; + +static void usage(int help) +{ + fprintf(stderr, +"barcode version %s\n" +"Copyright (C) 2009 ePoint Systems Ltd.\n", VERSION); + if(help) { + fprintf(stderr, +"Usage: barcode [OPTION]... [STRING]\n" +"Save barcode as a PNG image.\n\n" +" -h, --help display the help message.\n\n" +" -o FILENAME, --output=FILENAME\n" +" write PNG image to FILENAME. If '-' is specified, the result\n" +" will be output to standard output.\n" +" -s NUMBER, --size=NUMBER\n" +" specify the size of module (unit bar). (default=2)\n\n" +" -r, --rotate rotate the barcode to vertical position. (by 90 degrees)\n\n" +" -m NUMBER, --margin=NUMBER\n" +" specify the width of margin. (default=10)\n\n" +" -t NUMBER, --height=NUMBER\n" +" specify the height of the barcode (default=20)\n\n" +" -v, --version\n" +" display the version number and copyrights of the qrencode.\n\n" +" [STRING] input data. If it is not specified, data will be taken from\n" +" standard input.\n" + ); + } +} + +#define MAX_DATA_SIZE (1024) +static char buffer[MAX_DATA_SIZE + 1]; + +static int size = 2, margin = 10, height = 20, vertical = 0; + +static char *readStdin(void) +{ + int ret; + + ret = fread(buffer, 1, MAX_DATA_SIZE, stdin); + if(ret == 0) { + fprintf(stderr, "No input data.\n"); + exit(1); + } + if(!feof(stdin)) { + fprintf(stderr, "Input data is too large.\n"); + exit(1); + } + + buffer[ret] = '\0'; + + return buffer; +} + +static int writePNG(char* barcode, const char *outfile) +{ + FILE *fp; + png_structp png_ptr; + png_infop info_ptr; + unsigned char *p, *q; + int c, m, s; + int x, y, xx, yy, bit; + int width = strlen(barcode); + int realwidth = ((width + 3) * 11 + 2 + margin * 2) * size; + unsigned char row[((vertical ? height : realwidth) + 7) >> 3]; + + if(outfile[0] == '-' && outfile[1] == '\0') { + fp = stdout; + } else { + fp = fopen(outfile, "wb"); + if(fp == NULL) { + fprintf(stderr, "Failed to create file: %s\n", outfile); + perror(NULL); + exit(1); + } + } + + png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); + if(png_ptr == NULL) { + fclose(fp); + fprintf(stderr, "Failed to initialize PNG writer.\n"); + exit(1); + } + + info_ptr = png_create_info_struct(png_ptr); + if(info_ptr == NULL) { + fclose(fp); + fprintf(stderr, "Failed to initialize PNG write.\n"); + exit(1); + } + + if(setjmp(png_jmpbuf(png_ptr))) { + png_destroy_write_struct(&png_ptr, &info_ptr); + fclose(fp); + fprintf(stderr, "Failed to write PNG image.\n"); + exit(1); + } + + png_init_io(png_ptr, fp); + png_set_IHDR(png_ptr, info_ptr, + vertical ? height : realwidth, + vertical ? realwidth : height, + 1, + PNG_COLOR_TYPE_GRAY, + PNG_INTERLACE_NONE, + PNG_COMPRESSION_TYPE_DEFAULT, + PNG_FILTER_TYPE_DEFAULT); + png_write_info(png_ptr, info_ptr); + + /* data */ + p = barcode; + s = START_B; + c = code128font[s]; + m = 0x400; + bit = 7; + memset(row, 0xff, ((vertical ? height : realwidth) + 7) >> 3); + if(vertical) { + x = margin * size; + while(x-- > 0) png_write_row(png_ptr, row); + } else { + q = row; + q += (margin * size) >> 3; + bit = 7 - (margin * size % 8); + } + for(x = -1; x <= width;) { + if(vertical) { + memset(row, (c & m) ? 0 : 0xFF, (height + 7) >> 3); + xx = size; + while(xx-- > 0) png_write_row(png_ptr, row); + } else { + for(xx = 0; xx < size; xx++) { + *q ^= ((c & m) ? 1 : 0) << bit; + bit--; + if(bit < 0) { + q++; + bit = 7; + } + } + } + m >>= 1; + if(!m) { + x++; + if(*p) { + if((*p < ' ') || (*p & 0x80)) { + c = code128font[0]; + } else { + c = code128font[*p - ' ']; + s += (*p - ' ') * (x + 1); + s %= 103; + } + m = 0x400; + } else { + c = code128font[s] << 13 | + code128font[STOP] << 2 | 3; + m = 0x800000; + } + p++; + } + } + if(vertical) { + memset(row, 0xFF, (height + 7) >> 3); + x = margin * size; + while(x-- > 0) png_write_row(png_ptr, row); + } else { + for(y = 0; y < height; y++) { + png_write_row(png_ptr, row); + } + } + + png_write_end(png_ptr, info_ptr); + png_destroy_write_struct(&png_ptr, &info_ptr); + + fclose(fp); + + return 0; +} + +int main(int argc, char **argv) +{ + int opt, lindex = -1; + char *outfile = NULL; + char *intext = NULL; + + while((opt = getopt_long(argc, argv, optstring, options, &lindex)) != -1) { + switch(opt) { + case 'h': + usage(1); + exit(0); + break; + case 'o': + outfile = optarg; + break; + case 'r': + vertical = -1; + break; + case 's': + size = atoi(optarg); + if(size <= 0) { + fprintf(stderr, "Invalid size: %d\n", size); + exit(1); + } + break; + case 'm': + margin = atoi(optarg); + if(margin < 0) { + fprintf(stderr, "Invalid margin: %d\n", margin); + exit(1); + } + break; + case 't': + height = atoi(optarg); + if(height < 1) { + fprintf(stderr, "Invalid height: %d\n", height); + exit(1); + } + break; + case 'v': + usage(0); + exit(0); + break; + default: + fprintf(stderr, "Try `qrencode --help' for more information.\n"); + exit(1); + break; + } + } + + if(argc == 1) { + usage(1); + exit(0); + } + + if(outfile == NULL) { + fprintf(stderr, "No output filename is given.\n"); + exit(1); + } + + if(optind < argc) { + intext = argv[optind]; + } + if(intext == NULL) { + intext = readStdin(); + } + + writePNG(intext, outfile); + + return 0; +} Index: utils/code128b/src/code128font.c =================================================================== --- utils/code128b/src/code128font.c (revision 0) +++ utils/code128b/src/code128font.c (revision 0) @@ -0,0 +1,41 @@ +static const int code128font[] = { + 1740, 1644, 1638, + 1176, 1164, 1100, + 1224, 1220, 1124, + 1608, 1604, 1572, + 1436, 1244, 1230, + 1484, 1260, 1254, + 1650, 1628, 1614, + 1764, 1652, 1902, + 1868, 1836, 1830, + 1892, 1844, 1842, + 1752, 1734, 1590, + 1304, 1112, 1094, + 1416, 1128, 1122, + 1672, 1576, 1570, + 1464, 1422, 1134, + 1496, 1478, 1142, + 1910, 1678, 1582, + 1768, 1762, 1774, + 1880, 1862, 1814, + 1896, 1890, 1818, + 1914, 1602, 1930, + 1328, 1292, 1200, + 1158, 1068, 1062, + 1424, 1412, 1232, + 1218, 1076, 1074, + 1554, 1616, 1978, + 1556, 1146, 1340, + 1212, 1182, 1508, + 1268, 1266, 1956, + 1940, 1938, 1758, + 1782, 1974, 1400, + 1310, 1118, 1512, + 1506, 1960, 1954, + 1502, 1518, 1886, + 1966, 1668, 1680, + 1692, 1594 +}; + +#define START_B (104) +#define STOP (106) Index: utils/code128b/src/Makefile =================================================================== --- utils/code128b/src/Makefile (revision 0) +++ utils/code128b/src/Makefile (revision 0) @@ -0,0 +1,7 @@ +all: code128b + +code128b: pngenc.c code128font.c + $(CC) $(CFLAGS) $(EXTRA_CFLAGS) -o $@ -L $(STAGING_DIR)/usr/lib pngenc.c -lpng -lz + +clean: + rm -f *.o *~ a.out code128b Index: utils/code128b/Makefile =================================================================== --- utils/code128b/Makefile (revision 0) +++ utils/code128b/Makefile (revision 0) @@ -0,0 +1,32 @@ +include $(TOPDIR)/rules.mk + +PKG_NAME:=code128b +PKG_VERSION:=0.2 +PKG_RELEASE:=1 + +include $(INCLUDE_DIR)/package.mk + +define Package/code128b + SECTION:=utils + CATEGORY:=Utilities + TITLE:=Barcode to png encoder for CODE128B + MAINTAINER:=ePoint Systems Ltd <[email protected]> + DEPENDS:=+libpng +endef + +define Package/code128b/description + Barcode to png encoder for CODE128B +endef + +define Build/Prepare + mkdir -p $(PKG_BUILD_DIR) + make -C ./src clean + $(CP) ./src/* $(PKG_BUILD_DIR) +endef + +define Package/code128b/install + mkdir -p $(1)/usr/bin + $(INSTALL_BIN) $(PKG_BUILD_DIR)/code128b $(1)/usr/bin/ +endef + +$(eval $(call BuildPackage,code128b))
signature.asc
Description: OpenPGP digital signature
_______________________________________________ openwrt-devel mailing list [email protected] https://lists.openwrt.org/mailman/listinfo/openwrt-devel
