5 * Created by Christian Brunschen on 19/05/2008.
6 * Copyright 2008 ZXing authors All rights reserved.
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
22 #include "../../common/IllegalArgumentException.h"
28 vector<DataMask*> DataMask::DATA_MASKS;
29 static int N_DATA_MASKS = DataMask::buildDataMasks();
31 DataMask &DataMask::forReference(int reference) {
32 if (reference < 0 || reference > 7) {
33 throw new IllegalArgumentException("reference must be between 0 and 7");
35 return *DATA_MASKS[reference];
39 class DataMask000 : public DataMask {
41 static unsigned int BITMASK;
44 virtual void unmaskBitMatrix(valarray<unsigned int> &bits,
46 size_t max = bits.size();
47 for (size_t i = 0; i < max; i++) {
52 unsigned int DataMask000::BITMASK = 0x55555555U;
54 class DataMask001 : public DataMask {
57 virtual void unmaskBitMatrix(valarray<unsigned int> &bits,
59 unsigned int bitMask = 0;
62 for (int j = 0; j < dimension; j++) {
63 for (int i = 0; i < dimension; i++) {
64 if ((i & 0x01) == 0) {
65 bitMask |= 1 << count;
68 bits[offset++] ^= bitMask;
74 bits[offset] ^= bitMask;
79 class DataMask010 : public DataMask {
82 virtual void unmaskBitMatrix(valarray<unsigned int> &bits,
87 for (int j = 0; j < dimension; j++) {
88 bool columnMasked = j % 3 == 0;
89 for (int i = 0; i < dimension; i++) {
91 bitMask |= 1 << count;
94 bits[offset++] ^= bitMask;
100 bits[offset] ^= bitMask;
105 class DataMask011 : public DataMask {
108 virtual void unmaskBitMatrix(valarray<unsigned int> &bits,
110 unsigned int bitMask = 0;
113 for (int j = 0; j < dimension; j++) {
114 for (int i = 0; i < dimension; i++) {
115 if ((i + j) % 3 == 0) {
116 bitMask |= 1 << count;
119 bits[offset++] ^= bitMask;
125 bits[offset] ^= bitMask;
130 class DataMask100 : public DataMask {
133 virtual void unmaskBitMatrix(valarray<unsigned int> &bits,
135 unsigned int bitMask = 0;
138 for (int j = 0; j < dimension; j++) {
139 int jComponentParity = (j / 3) & 0x01;
140 for (int i = 0; i < dimension; i++) {
141 if (((i >> 1) & 0x01) == jComponentParity) {
142 bitMask |= 1 << count;
145 bits[offset++] ^= bitMask;
151 bits[offset] ^= bitMask;
156 class DataMask101 : public DataMask {
159 virtual void unmaskBitMatrix(valarray<unsigned int> &bits,
161 unsigned int bitMask = 0;
164 for (int j = 0; j < dimension; j++) {
165 for (int i = 0; i < dimension; i++) {
167 if (((product & 0x01) == 0) && product % 3 == 0) {
168 bitMask |= 1 << count;
171 bits[offset++] ^= bitMask;
177 bits[offset] ^= bitMask;
182 class DataMask110 : public DataMask {
185 virtual void unmaskBitMatrix(valarray<unsigned int> &bits,
187 unsigned int bitMask = 0;
190 for (int j = 0; j < dimension; j++) {
191 for (int i = 0; i < dimension; i++) {
193 if ((((product & 0x01) + product % 3) & 0x01) == 0) {
194 bitMask |= 1 << count;
197 bits[offset++] ^= bitMask;
203 bits[offset] ^= bitMask;
208 class DataMask111 : public DataMask {
211 virtual void unmaskBitMatrix(valarray<unsigned int> &bits,
213 unsigned int bitMask = 0;
216 for (int j = 0; j < dimension; j++) {
217 for (int i = 0; i < dimension; i++) {
218 if (((((i + j) & 0x01) + (i * j) % 3) & 0x01) == 0) {
219 bitMask |= 1 << count;
222 bits[offset++] ^= bitMask;
228 bits[offset] ^= bitMask;
232 int DataMask::buildDataMasks() {
233 DATA_MASKS.push_back(new DataMask000());
234 DATA_MASKS.push_back(new DataMask001());
235 DATA_MASKS.push_back(new DataMask010());
236 DATA_MASKS.push_back(new DataMask011());
237 DATA_MASKS.push_back(new DataMask100());
238 DATA_MASKS.push_back(new DataMask101());
239 DATA_MASKS.push_back(new DataMask110());
240 DATA_MASKS.push_back(new DataMask111());
241 return DATA_MASKS.size();