aboutsummaryrefslogtreecommitdiff
path: root/lpg/libqr/code-layout.c
diff options
context:
space:
mode:
authorLeo Howell <leo@lwh.jp>2009-09-26 16:08:27 +0900
committerLeo Howell <leo@lwh.jp>2009-09-26 16:08:27 +0900
commit28849b589dcd933b2527d9f231ce6668ed7d19fa (patch)
treefe3bd370668940c18e37a0a10176c09e5a54be3c /lpg/libqr/code-layout.c
parent64a96442629b720b2e2b1cc3997ee3dbc6a71b90 (diff)
downloadpdf-simple-sign-28849b589dcd933b2527d9f231ce6668ed7d19fa.tar.gz
pdf-simple-sign-28849b589dcd933b2527d9f231ce6668ed7d19fa.tar.xz
pdf-simple-sign-28849b589dcd933b2527d9f231ce6668ed7d19fa.zip
(partially) layout code
Diffstat (limited to 'lpg/libqr/code-layout.c')
-rw-r--r--lpg/libqr/code-layout.c125
1 files changed, 125 insertions, 0 deletions
diff --git a/lpg/libqr/code-layout.c b/lpg/libqr/code-layout.c
new file mode 100644
index 0000000..228758a
--- /dev/null
+++ b/lpg/libqr/code-layout.c
@@ -0,0 +1,125 @@
+#include <limits.h>
+#include <stdlib.h>
+#include "code-common.h"
+#include "code-layout.h"
+
+struct qr_iterator {
+ struct qr_code * code;
+ int dim;
+ int column;
+ int row;
+ int up;
+ int mask;
+ unsigned char * p;
+};
+
+static int is_data_bit(const struct qr_iterator * i)
+{
+ if (i->row == 6 || i->column == 6) /* timing */
+ return 0;
+
+ if (i->column < 9 && i->row < 9) /* top-left */
+ return 0;
+
+ if (i->column >= i->dim - 8 && i->row < 9) /* top-right */
+ return 0;
+
+ if (i->column < 9 && i->row >= i->dim - 8) /* bottom-left */
+ return 0;
+
+ /* XXX: format data */
+ /* XXX: alignment pattern */
+
+ return 1;
+}
+
+static void set_pointer(struct qr_iterator * i)
+{
+ i->mask = 1 << (i->column % CHAR_BIT);
+ i->p = i->code->modules
+ + i->code->line_stride * i->row
+ + i->column / CHAR_BIT;
+}
+
+static void advance(struct qr_iterator * i)
+{
+ do {
+ /* This XOR is to account for the vertical strip of
+ * timing bits in column 6 which displaces everything.
+ */
+ if ((i->column < 6) ^ !(i->column % 2)) {
+ /* Right-hand part or at left edge */
+ i->column -= 1;
+ } else {
+ /* Left-hand part */
+ i->column += 1;
+
+ if (( i->up && i->row == 0) ||
+ (!i->up && i->row == i->dim - 1)) {
+ /* Hit the top / bottom */
+ i->column -= 2;
+ i->up = !i->up;
+ } else {
+ i->row += i->up ? -1 : 1;
+ }
+ }
+
+ if (i->column < 0)
+ continue; /* don't go off left edge */
+
+ /* Check for one-past-end */
+ if (i->column == 0 && i->row >= i->dim - 8)
+ break;
+
+ } while (!is_data_bit(i));
+
+ set_pointer(i);
+}
+
+struct qr_iterator * qr_layout_begin(struct qr_code * code)
+{
+ struct qr_iterator * i;
+
+ i = malloc(sizeof(*i));
+ if (i) {
+ i->dim = code_side_length(code->format);
+ i->code = code;
+ i->column = i->dim - 1;
+ i->row = i->dim - 1;
+ i->up = 1;
+ set_pointer(i);
+ }
+
+ return i;
+}
+
+void qr_layout_end(struct qr_iterator * i)
+{
+ free(i);
+}
+
+unsigned int qr_layout_read(struct qr_iterator * i)
+{
+ unsigned int x = 0;
+ int b;
+
+ for (b = 0; b < 8; ++b) {
+ x |= (*i->p & i->mask) ? 1 : 0;
+ advance(i);
+ x <<= 1;
+ }
+
+ return x;
+}
+
+void qr_layout_write(struct qr_iterator * i, unsigned int x)
+{
+ int b;
+
+ for (b = 0; b < 8; ++b) {
+ *i->p |= (x & 0x80) ? i->mask : 0;
+ advance(i);
+ x <<= 1;
+ }
+}
+