2 * Copyright 2007 ZXing authors
\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 namespace com.google.zxing.common
\r
20 /// <summary> <p>Represents a 2D matrix of bits. In function arguments below, and throughout the common
\r
21 /// module, x is the column position, and y is the row position. The ordering is always x, y.
\r
22 /// The origin is at the top-left.</p>
\r
24 /// <p>Internally the bits are represented in a 1-D array of 32-bit ints. However, each row begins
\r
25 /// with a new int. This is done intentionally so that we can copy out a row into a BitArray very
\r
26 /// efficiently.</p>
\r
28 /// <p>The ordering of bits is row-major. Within each int, the least significant bits are used first,
\r
29 /// meaning they represent lower x values. This is compatible with BitArray's implementation.</p>
\r
32 /// <author> Sean Owen
\r
34 /// <author> dswitkin@google.com (Daniel Switkin)
\r
36 /// <author>www.Redivivus.in (suraj.supekar@redivivus.in) - Ported from ZXING Java Source
\r
38 public sealed class BitMatrix
\r
40 /// <returns> The width of the matrix
\r
50 /// <returns> The height of the matrix
\r
60 /// <summary> This method is for compatibility with older code. It's only logical to call if the matrix
\r
61 /// is square, so I'm throwing if that's not the case.
\r
64 /// <returns> row/column dimension of this matrix
\r
66 public int Dimension
\r
70 if (width != height)
\r
72 throw new System.SystemException("Can't call getDimension() on a non-square matrix");
\r
79 // TODO: Just like BitArray, these need to be public so ProGuard can inline them.
\r
80 //UPGRADE_NOTE: Final was removed from the declaration of 'width '. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'"
\r
82 //UPGRADE_NOTE: Final was removed from the declaration of 'height '. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'"
\r
84 //UPGRADE_NOTE: Final was removed from the declaration of 'rowSize '. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'"
\r
86 //UPGRADE_NOTE: Final was removed from the declaration of 'bits '. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'"
\r
89 // A helper to construct a square matrix.
\r
90 public BitMatrix(int dimension):this(dimension, dimension)
\r
94 public BitMatrix(int width, int height)
\r
96 if (width < 1 || height < 1)
\r
98 throw new System.ArgumentException("Both dimensions must be greater than 0");
\r
100 this.width = width;
\r
101 this.height = height;
\r
102 int rowSize = width >> 5;
\r
103 if ((width & 0x1f) != 0)
\r
107 this.rowSize = rowSize;
\r
108 bits = new int[rowSize * height];
\r
111 /// <summary> <p>Gets the requested bit, where true means black.</p>
\r
114 /// <param name="x">The horizontal component (i.e. which column)
\r
116 /// <param name="y">The vertical component (i.e. which row)
\r
118 /// <returns> value of given bit in matrix
\r
120 public bool get_Renamed(int x, int y)
\r
122 int offset = y * rowSize + (x >> 5);
\r
123 return ((SupportClass.URShift(bits[offset], (x & 0x1f))) & 1) != 0;
\r
126 /// <summary> <p>Sets the given bit to true.</p>
\r
129 /// <param name="x">The horizontal component (i.e. which column)
\r
131 /// <param name="y">The vertical component (i.e. which row)
\r
133 public void set_Renamed(int x, int y)
\r
135 int offset = y * rowSize + (x >> 5);
\r
136 bits[offset] |= 1 << (x & 0x1f);
\r
139 /// <summary> <p>Flips the given bit.</p>
\r
142 /// <param name="x">The horizontal component (i.e. which column)
\r
144 /// <param name="y">The vertical component (i.e. which row)
\r
146 public void flip(int x, int y)
\r
148 int offset = y * rowSize + (x >> 5);
\r
149 bits[offset] ^= 1 << (x & 0x1f);
\r
152 /// <summary> Clears all bits (sets to false).</summary>
\r
153 public void clear()
\r
155 int max = bits.Length;
\r
156 for (int i = 0; i < max; i++)
\r
162 /// <summary> <p>Sets a square region of the bit matrix to true.</p>
\r
165 /// <param name="left">The horizontal position to begin at (inclusive)
\r
167 /// <param name="top">The vertical position to begin at (inclusive)
\r
169 /// <param name="width">The width of the region
\r
171 /// <param name="height">The height of the region
\r
173 public void setRegion(int left, int top, int width, int height)
\r
175 if (top < 0 || left < 0)
\r
177 throw new System.ArgumentException("Left and top must be nonnegative");
\r
179 if (height < 1 || width < 1)
\r
181 throw new System.ArgumentException("Height and width must be at least 1");
\r
183 int right = left + width;
\r
184 int bottom = top + height;
\r
185 if (bottom > this.height || right > this.width)
\r
187 throw new System.ArgumentException("The region must fit inside the matrix");
\r
189 for (int y = top; y < bottom; y++)
\r
191 int offset = y * rowSize;
\r
192 for (int x = left; x < right; x++)
\r
194 bits[offset + (x >> 5)] |= 1 << (x & 0x1f);
\r
199 /// <summary> A fast method to retrieve one row of data from the matrix as a BitArray.
\r
202 /// <param name="y">The row to retrieve
\r
204 /// <param name="row">An optional caller-allocated BitArray, will be allocated if null or too small
\r
206 /// <returns> The resulting BitArray - this reference should always be used even when passing
\r
209 public BitArray getRow(int y, BitArray row)
\r
211 if (row == null || row.Size < width)
\r
213 row = new BitArray(width);
\r
215 int offset = y * rowSize;
\r
216 for (int x = 0; x < rowSize; x++)
\r
218 row.setBulk(x << 5, bits[offset + x]);
\r
223 public override System.String ToString()
\r
225 System.Text.StringBuilder result = new System.Text.StringBuilder(height * (width + 1));
\r
226 for (int y = 0; y < height; y++)
\r
228 for (int x = 0; x < width; x++)
\r
230 result.Append(get_Renamed(x, y)?"X ":" ");
\r
232 result.Append('\n');
\r
234 return result.ToString();
\r