From ef01767c732629b7eff7d1601e76a1bac58f5bfd Mon Sep 17 00:00:00 2001 From: Leo Howell Date: Sat, 5 Sep 2009 09:33:22 +0900 Subject: The story so far --- lpg/libqr/.gitignore | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 lpg/libqr/.gitignore (limited to 'lpg/libqr/.gitignore') diff --git a/lpg/libqr/.gitignore b/lpg/libqr/.gitignore new file mode 100644 index 0000000..dbfb6fb --- /dev/null +++ b/lpg/libqr/.gitignore @@ -0,0 +1,5 @@ +*~ +*.o +*.a +*.so +test -- cgit v1.2.3-70-g09d2 From ce330147acca10cd66c370847bdc3e824a83a0c6 Mon Sep 17 00:00:00 2001 From: Leo Howell Date: Thu, 8 Oct 2009 16:41:56 +0900 Subject: add qrgen sample app --- lpg/libqr/.gitignore | 2 +- lpg/libqr/Makefile | 20 ++--- lpg/libqr/code-common.c | 3 +- lpg/libqr/code-common.h | 2 +- lpg/libqr/code-layout.c | 2 +- lpg/libqr/qr-bitmap-pbm.c | 47 ---------- lpg/libqr/qr-bitmap-render.c | 2 +- lpg/libqr/qr-bitmap.c | 2 +- lpg/libqr/qr-bitmap.h | 31 ------- lpg/libqr/qr-mask.c | 2 +- lpg/libqr/qr-mask.h | 2 +- lpg/libqr/qr/bitmap.h | 27 ++++++ lpg/libqr/qrgen.c | 210 +++++++++++++++++++++++++++++++++++++++++++ lpg/libqr/test.c | 53 ----------- 14 files changed, 255 insertions(+), 150 deletions(-) delete mode 100644 lpg/libqr/qr-bitmap-pbm.c delete mode 100644 lpg/libqr/qr-bitmap.h create mode 100644 lpg/libqr/qr/bitmap.h create mode 100644 lpg/libqr/qrgen.c delete mode 100644 lpg/libqr/test.c (limited to 'lpg/libqr/.gitignore') diff --git a/lpg/libqr/.gitignore b/lpg/libqr/.gitignore index dbfb6fb..41c2dbc 100644 --- a/lpg/libqr/.gitignore +++ b/lpg/libqr/.gitignore @@ -2,4 +2,4 @@ *.o *.a *.so -test +qrgen diff --git a/lpg/libqr/Makefile b/lpg/libqr/Makefile index 5eba18b..19e8791 100644 --- a/lpg/libqr/Makefile +++ b/lpg/libqr/Makefile @@ -1,12 +1,11 @@ OBJECTS := code-common.o \ - code-create.o \ - code-layout.o \ - code-parse.o \ - data-common.o \ - data-create.o \ - data-parse.o \ + code-create.o \ + code-layout.o \ + code-parse.o \ + data-common.o \ + data-create.o \ + data-parse.o \ qr-bitmap.o \ - qr-bitmap-pbm.o \ qr-bitmap-render.o \ qr-bitstream.o \ qr-mask.o \ @@ -16,15 +15,16 @@ CFLAGS := -std=c89 -pedantic -I. -Wall CFLAGS += -g #CFLAGS += -O3 -DNDEBUG -all : libqr test +all : libqr qrgen $(OBJECTS) : $(wildcard *.h qr/*.h) libqr : libqr.a($(OBJECTS)) -test : libqr.a test.c +qrgen : libqr qrgen.c + $(CC) $(CFLAGS) -o qrgen qrgen.c libqr.a .PHONY : clean clean: - $(RM) qr/*~ *~ *.o *.a *.so test + $(RM) qr/*~ *~ *.o *.a *.so qrgen diff --git a/lpg/libqr/code-common.c b/lpg/libqr/code-common.c index dc2a8f0..150dfc5 100644 --- a/lpg/libqr/code-common.c +++ b/lpg/libqr/code-common.c @@ -1,8 +1,7 @@ #include #include - +#include #include "code-common.h" -#include "qr-bitmap.h" void qr_code_destroy(struct qr_code * code) { diff --git a/lpg/libqr/code-common.h b/lpg/libqr/code-common.h index 11538d8..0459708 100644 --- a/lpg/libqr/code-common.h +++ b/lpg/libqr/code-common.h @@ -2,8 +2,8 @@ #define CODE_COMMON_H #include +#include #include "qr-bitstream.h" -#include "qr-bitmap.h" struct qr_code { int version; diff --git a/lpg/libqr/code-layout.c b/lpg/libqr/code-layout.c index ed01112..54947d1 100644 --- a/lpg/libqr/code-layout.c +++ b/lpg/libqr/code-layout.c @@ -2,9 +2,9 @@ #include #include #include +#include #include "code-common.h" #include "code-layout.h" -#include "qr-bitmap.h" struct qr_iterator { struct qr_code * code; diff --git a/lpg/libqr/qr-bitmap-pbm.c b/lpg/libqr/qr-bitmap-pbm.c deleted file mode 100644 index 3bcddb2..0000000 --- a/lpg/libqr/qr-bitmap-pbm.c +++ /dev/null @@ -1,47 +0,0 @@ -#include -#include -#include "qr-bitmap.h" - -int qr_bitmap_write_pbm(const char * path, - const char * comment, - const struct qr_bitmap * bmp) -{ - FILE * out; - size_t count, x, y; - - out = fopen(path, "w"); - if (!out) - return -1; - - count = 0; - - count += fputs("P1\n", out); - - if (comment) - count += fprintf(out, "# %s\n", comment); - - count += fprintf(out, "%u %u\n", - (unsigned)bmp->width, - (unsigned)bmp->height); - - for (y = 0; y < bmp->height; ++y) { - unsigned char * row = bmp->bits + y * bmp->stride; - - for (x = 0; x < bmp->width; ++x) { - int bit = row[x / CHAR_BIT] & (1 << x % CHAR_BIT); - - if (x > 0) - fputc(' ', out); - fputc(bit ? '1' : '0', out); - } - count += fputc('\n', out); - } - - if (ferror(out)) - count = -1; - - fclose(out); - - return count; -} - diff --git a/lpg/libqr/qr-bitmap-render.c b/lpg/libqr/qr-bitmap-render.c index 4f6c921..197e947 100644 --- a/lpg/libqr/qr-bitmap-render.c +++ b/lpg/libqr/qr-bitmap-render.c @@ -2,7 +2,7 @@ #include #include -#include "qr-bitmap.h" +#include /* CHAR_BIT | mod_bits (multi-byte) */ static void render_line_1(unsigned char * out, diff --git a/lpg/libqr/qr-bitmap.c b/lpg/libqr/qr-bitmap.c index 2bd0c50..7d2d900 100644 --- a/lpg/libqr/qr-bitmap.c +++ b/lpg/libqr/qr-bitmap.c @@ -2,7 +2,7 @@ #include #include #include -#include "qr-bitmap.h" +#include struct qr_bitmap * qr_bitmap_create(int width, int height, int masked) { diff --git a/lpg/libqr/qr-bitmap.h b/lpg/libqr/qr-bitmap.h deleted file mode 100644 index 936b23d..0000000 --- a/lpg/libqr/qr-bitmap.h +++ /dev/null @@ -1,31 +0,0 @@ -#ifndef QR_BITMAP_H -#define QR_BITMAP_H - -struct qr_bitmap { - unsigned char * bits; - unsigned char * mask; - size_t stride; - size_t width, height; -}; - -struct qr_bitmap * qr_bitmap_create(int width, int height, int masked); -void qr_bitmap_destroy(struct qr_bitmap *); - -struct qr_bitmap * qr_bitmap_clone(const struct qr_bitmap *); - -void qr_bitmap_merge(struct qr_bitmap * dest, const struct qr_bitmap * src); - -void qr_bitmap_render(const struct qr_bitmap * bmp, - void * buffer, - size_t mod_bits, - size_t line_stride, - size_t line_repeat, - unsigned long mark, - unsigned long space); - -int qr_bitmap_write_pbm(const char * path, - const char * comment, - const struct qr_bitmap * bmp); - -#endif - diff --git a/lpg/libqr/qr-mask.c b/lpg/libqr/qr-mask.c index 2a63c0b..7ff78a4 100644 --- a/lpg/libqr/qr-mask.c +++ b/lpg/libqr/qr-mask.c @@ -1,6 +1,6 @@ #include #include -#include "qr-bitmap.h" +#include #include "qr-mask.h" struct qr_bitmap * qr_mask_apply(const struct qr_bitmap * orig, diff --git a/lpg/libqr/qr-mask.h b/lpg/libqr/qr-mask.h index b3f3523..eb254af 100644 --- a/lpg/libqr/qr-mask.h +++ b/lpg/libqr/qr-mask.h @@ -1,7 +1,7 @@ #ifndef QR_MASK_H #define QR_MASK_H -#include "qr-bitmap.h" +#include struct qr_bitmap * qr_mask_apply(const struct qr_bitmap * orig, unsigned int mask); diff --git a/lpg/libqr/qr/bitmap.h b/lpg/libqr/qr/bitmap.h new file mode 100644 index 0000000..83e82ab --- /dev/null +++ b/lpg/libqr/qr/bitmap.h @@ -0,0 +1,27 @@ +#ifndef QR_BITMAP_H +#define QR_BITMAP_H + +struct qr_bitmap { + unsigned char * bits; + unsigned char * mask; + size_t stride; + size_t width, height; +}; + +struct qr_bitmap * qr_bitmap_create(int width, int height, int masked); +void qr_bitmap_destroy(struct qr_bitmap *); + +struct qr_bitmap * qr_bitmap_clone(const struct qr_bitmap *); + +void qr_bitmap_merge(struct qr_bitmap * dest, const struct qr_bitmap * src); + +void qr_bitmap_render(const struct qr_bitmap * bmp, + void * buffer, + size_t mod_bits, + size_t line_stride, + size_t line_repeat, + unsigned long mark, + unsigned long space); + +#endif + diff --git a/lpg/libqr/qrgen.c b/lpg/libqr/qrgen.c new file mode 100644 index 0000000..9899373 --- /dev/null +++ b/lpg/libqr/qrgen.c @@ -0,0 +1,210 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "qr-bitstream.h" +#include "code-common.h" + +struct config { + int version; + enum qr_ec_level ec; + enum qr_data_type dtype; + int ansi; + const char * input; +}; + +struct qr_code * create(int version, + enum qr_ec_level ec, + enum qr_data_type dtype, + const char * input) +{ + struct qr_data * data; + struct qr_code * code; + size_t len; + + len = strlen(input); + + data = qr_create_data(version, dtype, input, len); + + if (!data) { + /* BUG: this could also indicate OOM or + * some other error. + */ + fprintf(stderr, "Invalid data\n"); + exit(1); + } + + code = qr_code_create(ec, data); + + if (!code) { + perror("Failed to create code"); + qr_free_data(data); + exit(2); + } + + return code; +} + +void output_pbm(const struct qr_bitmap * bmp, const char * comment) +{ + unsigned char * row; + int x, y; + + puts("P1"); + + if (comment) + printf("# %s\n", comment); + + printf("%u %u\n", + (unsigned)bmp->width + 8, + (unsigned)bmp->height + 8); + + row = bmp->bits; + + for (y = -4; y < (int)bmp->height + 4; ++y) { + + if (y < 0 || y >= (int)bmp->height) { + for (x = 0; x < (int)bmp->width + 8; ++x) + printf("0 "); + putchar('\n'); + continue; + } + + printf("0 0 0 0 "); + + for (x = 0; x < (int)bmp->width; ++x) { + + int mask = 1 << x % CHAR_BIT; + int byte = row[x / CHAR_BIT]; + + printf("%c ", (byte & mask) ? '1' : '0'); + } + + puts("0 0 0 0"); + row += bmp->stride; + } +} + +void output_ansi(const struct qr_bitmap * bmp) +{ + const char * out[2] = { + " ", + "\033[7m \033[0m", + }; + + unsigned char * line; + int x, y; + + line = bmp->bits; + + for (y = 0; y < bmp->height; ++y) { + + for (x = 0; x < bmp->width; ++x) { + + int mask = 1 << (x % CHAR_BIT); + int byte = line[x / CHAR_BIT]; + + printf("%s", out[!!(byte & mask)]); + } + + putchar('\n'); + + line += bmp->stride; + } +} + +void show_help() { + fprintf(stderr, "(help message)\n"); +} + +void set_default_config(struct config * conf) +{ + conf->version = 1; + conf->ec = QR_EC_LEVEL_M; + conf->dtype = QR_DATA_NUMERIC; + conf->ansi = 1; + conf->input = "01234567"; +} + +void parse_options(int argc, char ** argv, struct config * conf) +{ + int c; + + for (;;) { + c = getopt(argc, argv, ":?vetap"); + + if (c == -1) /* no more options */ + break; + + switch (c) { + case '?': /* help */ + show_help(); + exit(0); + break; + case 'v': /* version */ + if (!optarg) { + fprintf(stderr, "No version\n"); + exit(1); + } + conf->version = atoi(optarg); + if (conf->version < 1 || conf->version > 40) { + fprintf(stderr, + "Version must be between 1 and 40\n"); + exit(1); + } + break; + case 'e': /* ec */ + fprintf(stderr, "XXX: ignored \"ec\"\n"); break; + case 't': /* type */ + fprintf(stderr, "XXX: ignored \"type\"\n"); break; + case 'a': /* ansi */ + conf->ansi = 1; break; + case 'p': /* pnm */ + conf->ansi = 0; break; + case ':': default: + fprintf(stderr, "Unknown option: \"%s\"\n", + argv[optind]); + exit(1); + break; + } + } + + + if (optind < argc) + conf->input = argv[optind++]; + + if (!conf->input) { + fprintf(stderr, "No data (try -? for help)\n"); + exit(1); + } +} + +int main(int argc, char ** argv) { + + struct config conf; + struct qr_code * code; + + set_default_config(&conf); + parse_options(argc, argv, &conf); + + code = create(conf.version, + conf.ec, + conf.dtype, + conf.input); + + if (conf.ansi) + output_ansi(code->modules); + else + output_pbm(code->modules, "qrgen v0.1"); + + qr_code_destroy(code); + + return 0; +} + diff --git a/lpg/libqr/test.c b/lpg/libqr/test.c deleted file mode 100644 index 4e288af..0000000 --- a/lpg/libqr/test.c +++ /dev/null @@ -1,53 +0,0 @@ -#include -#include -#include -#include -#include - -#include "qr-bitstream.h" -#include "code-common.h" - -int main() { - - struct qr_code * code; - struct qr_data * data; - enum qr_data_type type; - char *str; - size_t len; - - type = QR_DATA_NUMERIC; - str = "01234567"; - len = strlen(str); - - data = qr_create_data(1, type, str, len); - assert(data); - assert(qr_get_data_type(data) == type); - assert(qr_get_data_length(data) == len); - - type = qr_parse_data(data, &str, &len); - printf("[%d] %d\n", type, (int)len); - printf("\"%s\"\n", str); - - code = qr_code_create(QR_EC_LEVEL_M, data); - assert(code); - - printf("Code width %d\n", qr_code_width(code)); - - { - /* Hack: render the code using ANSI terminal art */ - char buf[80*25]; - int x, y; - - qr_bitmap_render(code->modules, buf, 8, 80, 0, 1, 0); - for (y=0;y<21;++y) { - printf("\t|"); - for (x=0;x<21;++x) { - printf("%s ", buf[y*80+x]?"\033[7m":"\033[0m"); - } - printf("\033[0m|\n"); - } - } - - return 0; -} - -- cgit v1.2.3-70-g09d2 From fed32fca55210ce26e103a7f941a71694945311c Mon Sep 17 00:00:00 2001 From: Leo Uino Date: Fri, 15 Jul 2011 15:26:27 +0900 Subject: Update gitignore --- lpg/libqr/.gitignore | 1 + 1 file changed, 1 insertion(+) (limited to 'lpg/libqr/.gitignore') diff --git a/lpg/libqr/.gitignore b/lpg/libqr/.gitignore index 41c2dbc..33bcf18 100644 --- a/lpg/libqr/.gitignore +++ b/lpg/libqr/.gitignore @@ -3,3 +3,4 @@ *.a *.so qrgen +qrparse -- cgit v1.2.3-70-g09d2