aboutsummaryrefslogtreecommitdiff
path: root/lpg
diff options
context:
space:
mode:
authorLeo Howell <leo@lwh.jp>2009-09-28 22:09:47 +0900
committerLeo Howell <leo@lwh.jp>2009-09-28 22:09:47 +0900
commit36f38e01ddd23f332374093fa213ca08429729b1 (patch)
treea0a8fd4b198346501353eb33a6463f934141249d /lpg
parent388507a61df32c0bddf7e61c5da013c3f29d32d4 (diff)
downloadpdf-simple-sign-36f38e01ddd23f332374093fa213ca08429729b1.tar.gz
pdf-simple-sign-36f38e01ddd23f332374093fa213ca08429729b1.tar.xz
pdf-simple-sign-36f38e01ddd23f332374093fa213ca08429729b1.zip
QR masks
Diffstat (limited to 'lpg')
-rw-r--r--lpg/libqr/Makefile1
-rw-r--r--lpg/libqr/qr-bitmap.c19
-rw-r--r--lpg/libqr/qr-bitmap.h2
-rw-r--r--lpg/libqr/qr-mask.c46
-rw-r--r--lpg/libqr/qr-mask.h10
5 files changed, 78 insertions, 0 deletions
diff --git a/lpg/libqr/Makefile b/lpg/libqr/Makefile
index 49f89d4..5eba18b 100644
--- a/lpg/libqr/Makefile
+++ b/lpg/libqr/Makefile
@@ -9,6 +9,7 @@ OBJECTS := code-common.o \
qr-bitmap-pbm.o \
qr-bitmap-render.o \
qr-bitstream.o \
+ qr-mask.o \
rs-encode.o
CFLAGS := -std=c89 -pedantic -I. -Wall
diff --git a/lpg/libqr/qr-bitmap.c b/lpg/libqr/qr-bitmap.c
index e42b550..1a5ca52 100644
--- a/lpg/libqr/qr-bitmap.c
+++ b/lpg/libqr/qr-bitmap.c
@@ -48,6 +48,25 @@ void qr_bitmap_destroy(struct qr_bitmap * bmp)
}
}
+struct qr_bitmap * qr_bitmap_clone(const struct qr_bitmap * src)
+{
+ struct qr_bitmap * bmp;
+ size_t size;
+
+ bmp = qr_bitmap_create(src->width, src->height, !!src->mask);
+ if (!bmp)
+ return 0;
+
+ assert(bmp->stride == src->stride);
+
+ size = bmp->width * bmp->stride;
+ memcpy(bmp->bits, src->bits, size);
+ if (bmp->mask)
+ memcpy(bmp->mask, src->mask, size);
+
+ return bmp;
+}
+
void qr_bitmap_merge(struct qr_bitmap * dest, const struct qr_bitmap * src)
{
unsigned char * d, * s, * m;
diff --git a/lpg/libqr/qr-bitmap.h b/lpg/libqr/qr-bitmap.h
index d60049a..936b23d 100644
--- a/lpg/libqr/qr-bitmap.h
+++ b/lpg/libqr/qr-bitmap.h
@@ -11,6 +11,8 @@ struct qr_bitmap {
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,
diff --git a/lpg/libqr/qr-mask.c b/lpg/libqr/qr-mask.c
new file mode 100644
index 0000000..2a63c0b
--- /dev/null
+++ b/lpg/libqr/qr-mask.c
@@ -0,0 +1,46 @@
+#include <limits.h>
+#include <stdlib.h>
+#include "qr-bitmap.h"
+#include "qr-mask.h"
+
+struct qr_bitmap * qr_mask_apply(const struct qr_bitmap * orig,
+ unsigned int mask)
+{
+ struct qr_bitmap * bmp;
+ int i, j;
+
+ if (mask & ~0x7)
+ return 0;
+
+ bmp = qr_bitmap_clone(orig);
+ if (!bmp)
+ return 0;
+
+ /* Slow version for now; we can optimize later */
+
+ for (i = 0; i < bmp->height; ++i) {
+ unsigned char * p = bmp->bits + i * bmp->stride;
+
+ for (j = 0; j < bmp->width; ++j) {
+ int bit = j % CHAR_BIT;
+ size_t off = j / CHAR_BIT;
+ int t;
+
+ switch (mask) {
+ case 0: t = (i + j) % 2; break;
+ case 1: t = i % 2; break;
+ case 2: t = j % 3; break;
+ case 3: t = (i + j) % 3; break;
+ case 4: t = (i/2 + j/3) % 2; break;
+ case 5: t = ((i*j) % 2) + ((i*j) % 3); break;
+ case 6: t = (((i*j) % 2) + ((i*j) % 3)) % 2; break;
+ case 7: t = (((i*j) % 3) + ((i+j) % 2)) % 2; break;
+ }
+
+ p[off] ^= (t == 0) << bit;
+ }
+ }
+
+ return bmp;
+}
+
diff --git a/lpg/libqr/qr-mask.h b/lpg/libqr/qr-mask.h
new file mode 100644
index 0000000..b3f3523
--- /dev/null
+++ b/lpg/libqr/qr-mask.h
@@ -0,0 +1,10 @@
+#ifndef QR_MASK_H
+#define QR_MASK_H
+
+#include "qr-bitmap.h"
+
+struct qr_bitmap * qr_mask_apply(const struct qr_bitmap * orig,
+ unsigned int mask);
+
+#endif
+