2 * Copyright 2007 Google Inc.
\r
4 * Licensed under the Apache License, Version 2.0 (the "License");
\r
5 * you may not use this file except in compliance with the License.
\r
6 * You may obtain a copy of the License at
\r
8 * http://www.apache.org/licenses/LICENSE-2.0
\r
10 * Unless required by applicable law or agreed to in writing, software
\r
11 * distributed under the License is distributed on an "AS IS" BASIS,
\r
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
13 * See the License for the specific language governing permissions and
\r
14 * limitations under the License.
\r
17 package com.google.zxing.common;
\r
20 * <p>Represnts a square matrix of bits. In function arguments below, i is the row position
\r
21 * and j the column position of a bit. The top left bit corresponds to i = 0 and j = 0.</p>
\r
23 * <p>Internally the bits are represented in a compact 1-D array of 32-bit ints. The
\r
24 * ordering of bits is column-major; that is the bits in this array correspond to
\r
25 * j=0 and i=0..dimension-1 first, then j=1 and i=0..dimension-1, etc.</p>
\r
27 * <p>Within each int, less-signficant bits correspond to lower values of i and higher rows.
\r
28 * That is, the top-left bit is the least significant bit of the first int.</p>
\r
30 * <p>This class is a convenient wrapper around this representation, but also exposes the internal
\r
31 * array for efficient access and manipulation.</p>
\r
33 * @author srowen@google.com (Sean Owen)
\r
35 public final class BitMatrix {
\r
37 private final int dimension;
\r
38 private final int[] bits;
\r
40 public BitMatrix(int dimension) {
\r
41 if (dimension < 1) {
\r
42 throw new IllegalArgumentException("dimension must be at least 1");
\r
44 this.dimension = dimension;
\r
45 int numBits = dimension * dimension;
\r
46 int arraySize = numBits >> 5; // one int per 32 bits
\r
47 if ((numBits & 0x1F) != 0) { // plus one more if there are leftovers
\r
50 bits = new int[arraySize];
\r
54 * @param i row offset
\r
55 * @param j column offset
\r
56 * @return value of given bit in matrix
\r
58 public boolean get(int i, int j) {
\r
59 int offset = i + dimension * j;
\r
60 return ((bits[offset >> 5] >>> (offset & 0x1F)) & 0x01) != 0;
\r
64 * <p>Sets the given bit to true.</p>
\r
66 * @param i row offset
\r
67 * @param j column offset
\r
69 public void set(int i, int j) {
\r
70 int offset = i + dimension * j;
\r
71 bits[offset >> 5] |= 1 << (offset & 0x1F);
\r
75 * <p>Sets a square region of the bit matrix to true.</p>
\r
77 * @param topI row offset of region's top-left corner (inclusive)
\r
78 * @param leftJ column offset of region's top-left corner (inclusive)
\r
79 * @param height height of region
\r
80 * @param width width of region
\r
82 public void setRegion(int topI, int leftJ, int height, int width) {
\r
83 if (topI < 0 || leftJ < 0) {
\r
84 throw new IllegalArgumentException("topI and leftJ must be nonnegative");
\r
86 if (height < 1 || width < 1) {
\r
87 throw new IllegalArgumentException("height and width must be at least 1");
\r
89 int maxJ = leftJ + width;
\r
90 int maxI = topI + height;
\r
91 if (maxI > dimension || maxJ > dimension) {
\r
92 throw new IllegalArgumentException(
\r
93 "topI + height and leftJ + width must be <= matrix dimension");
\r
95 for (int j = leftJ; j < maxJ; j++) {
\r
96 int jOffset = dimension * j;
\r
97 for (int i = topI; i < maxI; i++) {
\r
98 int offset = i + jOffset;
\r
99 bits[offset >> 5] |= 1 << (offset & 0x1F);
\r
105 * @return row/column dimension of this matrix
\r
107 public int getDimension() {
\r
112 * @return array of ints holding internal representation of this matrix's bits
\r
114 public int[] getBits() {
\r
118 public String toString() {
\r
119 StringBuffer result = new StringBuffer(dimension * (dimension + 1));
\r
120 for (int i = 0; i < dimension; i++) {
\r
121 for (int j = 0; j < dimension; j++) {
\r
122 result.append(get(i, j) ? 'X' : ' ');
\r
124 result.append('\n');
\r
126 return result.toString();
\r