From 36f38e01ddd23f332374093fa213ca08429729b1 Mon Sep 17 00:00:00 2001 From: Leo Howell Date: Mon, 28 Sep 2009 22:09:47 +0900 Subject: QR masks --- lpg/libqr/Makefile | 1 + lpg/libqr/qr-bitmap.c | 19 +++++++++++++++++++ lpg/libqr/qr-bitmap.h | 2 ++ lpg/libqr/qr-mask.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ lpg/libqr/qr-mask.h | 10 ++++++++++ 5 files changed, 78 insertions(+) create mode 100644 lpg/libqr/qr-mask.c create mode 100644 lpg/libqr/qr-mask.h 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 +#include +#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 + -- cgit v1.2.3-70-g09d2