blob: 54947d1b680e3a691ba87356baabdf62b184c56d (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
|
#include <assert.h>
#include <limits.h>
#include <stdlib.h>
#include <string.h>
#include <qr/bitmap.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;
};
void qr_layout_init_mask(struct qr_code * code)
{
int x, y;
int dim = qr_code_width(code);
struct qr_bitmap * bmp = code->modules;
assert(bmp->mask);
memset(bmp->mask, 0, bmp->height * bmp->stride);
/* slow and stupid, but I'm sleepy */
for (y = 0; y < bmp->height; ++y) {
unsigned char * row = bmp->mask + y * bmp->stride;
for (x = 0; x < bmp->width; ++x) {
unsigned char bit = 1 << (x % CHAR_BIT);
int off = x / CHAR_BIT;
if (x == 6 || y == 6) /* timing */
continue;
if (x < 9 && y < 9) /* top-left */
continue;
if (x >= dim - 8 && y < 9) /* top-right */
continue;
if (x < 9 && y >= dim - 8) /* bottom-left */
continue;
/* version info */
if (code->version >= 7) {
if (y < 6 && x >= dim - 11)
continue;
if (x < 6 && y >= dim - 11)
continue;
}
/* XXX: alignment pattern */
row[off] |= bit;
}
}
}
static int is_data_bit(const struct qr_iterator * i)
{
unsigned char bit = 1 << (i->column % CHAR_BIT);
int off = (i->row * i->code->modules->stride) + (i->column / CHAR_BIT);
return i->code->modules->mask[off] & bit;
}
static void set_pointer(struct qr_iterator * i)
{
i->mask = 1 << (i->column % CHAR_BIT);
i->p = i->code->modules->bits
+ i->code->modules->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 = qr_code_width(code);
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;
}
}
|