aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLeo Howell <leo@lwh.jp>2009-10-08 16:41:56 +0900
committerLeo Howell <leo@lwh.jp>2009-10-08 16:41:56 +0900
commitce330147acca10cd66c370847bdc3e824a83a0c6 (patch)
treebb9dd881daacfe1dff656371df6d2292bcbd7658
parent6359bfddcc5868093418784fa2e972581ebd82b6 (diff)
downloadpdf-simple-sign-ce330147acca10cd66c370847bdc3e824a83a0c6.tar.gz
pdf-simple-sign-ce330147acca10cd66c370847bdc3e824a83a0c6.tar.xz
pdf-simple-sign-ce330147acca10cd66c370847bdc3e824a83a0c6.zip
add qrgen sample app
-rw-r--r--lpg/libqr/.gitignore2
-rw-r--r--lpg/libqr/Makefile20
-rw-r--r--lpg/libqr/code-common.c3
-rw-r--r--lpg/libqr/code-common.h2
-rw-r--r--lpg/libqr/code-layout.c2
-rw-r--r--lpg/libqr/qr-bitmap-pbm.c47
-rw-r--r--lpg/libqr/qr-bitmap-render.c2
-rw-r--r--lpg/libqr/qr-bitmap.c2
-rw-r--r--lpg/libqr/qr-mask.c2
-rw-r--r--lpg/libqr/qr-mask.h2
-rw-r--r--lpg/libqr/qr/bitmap.h (renamed from lpg/libqr/qr-bitmap.h)4
-rw-r--r--lpg/libqr/qrgen.c210
-rw-r--r--lpg/libqr/test.c53
13 files changed, 228 insertions, 123 deletions
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 <stdlib.h>
#include <qr/code.h>
-
+#include <qr/bitmap.h>
#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 <qr/code.h>
+#include <qr/bitmap.h>
#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 <limits.h>
#include <stdlib.h>
#include <string.h>
+#include <qr/bitmap.h>
#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 <limits.h>
-#include <stdio.h>
-#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 <limits.h>
#include <assert.h>
-#include "qr-bitmap.h"
+#include <qr/bitmap.h>
/* 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 <limits.h>
#include <stdlib.h>
#include <string.h>
-#include "qr-bitmap.h"
+#include <qr/bitmap.h>
struct qr_bitmap * qr_bitmap_create(int width, int height, int masked)
{
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 <limits.h>
#include <stdlib.h>
-#include "qr-bitmap.h"
+#include <qr/bitmap.h>
#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 <qr/bitmap.h>
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
index 936b23d..83e82ab 100644
--- a/lpg/libqr/qr-bitmap.h
+++ b/lpg/libqr/qr/bitmap.h
@@ -23,9 +23,5 @@ void qr_bitmap_render(const struct qr_bitmap * bmp,
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/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 <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <assert.h>
+#include <limits.h>
+#include <getopt.h>
+#include <qr/bitmap.h>
+#include <qr/code.h>
+#include <qr/data.h>
+
+#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 <stdio.h>
-#include <string.h>
-#include <assert.h>
-#include <qr/code.h>
-#include <qr/data.h>
-
-#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;
-}
-