Rest of cosmetic changes -- simpler, black theme with easier-to-touch buttons and...
[zxing.git] / csharp / common / BitMatrix.cs
1 /*\r
2 * Copyright 2007 ZXing authors\r
3 *\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
7 *\r
8 *      http://www.apache.org/licenses/LICENSE-2.0\r
9 *\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
15 */\r
16 using System;\r
17 namespace com.google.zxing.common\r
18 {\r
19         \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
23         /// \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
27         /// \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
30         /// \r
31         /// </summary>\r
32         /// <author>  Sean Owen\r
33         /// </author>\r
34         /// <author>  dswitkin@google.com (Daniel Switkin)\r
35         /// </author>\r
36         /// <author>www.Redivivus.in (suraj.supekar@redivivus.in) - Ported from ZXING Java Source \r
37         /// </author>\r
38         public sealed class BitMatrix\r
39         {\r
40                 /// <returns> The width of the matrix\r
41                 /// </returns>\r
42                 public int Width\r
43                 {\r
44                         get\r
45                         {\r
46                                 return width;\r
47                         }\r
48                         \r
49                 }\r
50                 /// <returns> The height of the matrix\r
51                 /// </returns>\r
52                 public int Height\r
53                 {\r
54                         get\r
55                         {\r
56                                 return height;\r
57                         }\r
58                         \r
59                 }\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
62                 /// \r
63                 /// </summary>\r
64                 /// <returns> row/column dimension of this matrix\r
65                 /// </returns>\r
66                 public int Dimension\r
67                 {\r
68                         get\r
69                         {\r
70                                 if (width != height)\r
71                                 {\r
72                                         throw new System.SystemException("Can't call getDimension() on a non-square matrix");\r
73                                 }\r
74                                 return width;\r
75                         }\r
76                         \r
77                 }\r
78                 \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
81                 public int width;\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
83                 public int height;\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
85                 public int rowSize;\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
87                 public int[] bits;\r
88                 \r
89                 // A helper to construct a square matrix.\r
90                 public BitMatrix(int dimension):this(dimension, dimension)\r
91                 {\r
92                 }\r
93                 \r
94                 public BitMatrix(int width, int height)\r
95                 {\r
96                         if (width < 1 || height < 1)\r
97                         {\r
98                                 throw new System.ArgumentException("Both dimensions must be greater than 0");\r
99                         }\r
100                         this.width = width;\r
101                         this.height = height;\r
102                         int rowSize = width >> 5;\r
103                         if ((width & 0x1f) != 0)\r
104                         {\r
105                                 rowSize++;\r
106                         }\r
107                         this.rowSize = rowSize;\r
108                         bits = new int[rowSize * height];\r
109                 }\r
110                 \r
111                 /// <summary> <p>Gets the requested bit, where true means black.</p>\r
112                 /// \r
113                 /// </summary>\r
114                 /// <param name="x">The horizontal component (i.e. which column)\r
115                 /// </param>\r
116                 /// <param name="y">The vertical component (i.e. which row)\r
117                 /// </param>\r
118                 /// <returns> value of given bit in matrix\r
119                 /// </returns>\r
120                 public bool get_Renamed(int x, int y)\r
121                 {\r
122                         int offset = y * rowSize + (x >> 5);\r
123                         return ((SupportClass.URShift(bits[offset], (x & 0x1f))) & 1) != 0;\r
124                 }\r
125                 \r
126                 /// <summary> <p>Sets the given bit to true.</p>\r
127                 /// \r
128                 /// </summary>\r
129                 /// <param name="x">The horizontal component (i.e. which column)\r
130                 /// </param>\r
131                 /// <param name="y">The vertical component (i.e. which row)\r
132                 /// </param>\r
133                 public void  set_Renamed(int x, int y)\r
134                 {\r
135                         int offset = y * rowSize + (x >> 5);\r
136                         bits[offset] |= 1 << (x & 0x1f);\r
137                 }\r
138                 \r
139                 /// <summary> <p>Flips the given bit.</p>\r
140                 /// \r
141                 /// </summary>\r
142                 /// <param name="x">The horizontal component (i.e. which column)\r
143                 /// </param>\r
144                 /// <param name="y">The vertical component (i.e. which row)\r
145                 /// </param>\r
146                 public void  flip(int x, int y)\r
147                 {\r
148                         int offset = y * rowSize + (x >> 5);\r
149                         bits[offset] ^= 1 << (x & 0x1f);\r
150                 }\r
151                 \r
152                 /// <summary> Clears all bits (sets to false).</summary>\r
153                 public void  clear()\r
154                 {\r
155                         int max = bits.Length;\r
156                         for (int i = 0; i < max; i++)\r
157                         {\r
158                                 bits[i] = 0;\r
159                         }\r
160                 }\r
161                 \r
162                 /// <summary> <p>Sets a square region of the bit matrix to true.</p>\r
163                 /// \r
164                 /// </summary>\r
165                 /// <param name="left">The horizontal position to begin at (inclusive)\r
166                 /// </param>\r
167                 /// <param name="top">The vertical position to begin at (inclusive)\r
168                 /// </param>\r
169                 /// <param name="width">The width of the region\r
170                 /// </param>\r
171                 /// <param name="height">The height of the region\r
172                 /// </param>\r
173                 public void  setRegion(int left, int top, int width, int height)\r
174                 {\r
175                         if (top < 0 || left < 0)\r
176                         {\r
177                                 throw new System.ArgumentException("Left and top must be nonnegative");\r
178                         }\r
179                         if (height < 1 || width < 1)\r
180                         {\r
181                                 throw new System.ArgumentException("Height and width must be at least 1");\r
182                         }\r
183                         int right = left + width;\r
184                         int bottom = top + height;\r
185                         if (bottom > this.height || right > this.width)\r
186                         {\r
187                                 throw new System.ArgumentException("The region must fit inside the matrix");\r
188                         }\r
189                         for (int y = top; y < bottom; y++)\r
190                         {\r
191                                 int offset = y * rowSize;\r
192                                 for (int x = left; x < right; x++)\r
193                                 {\r
194                                         bits[offset + (x >> 5)] |= 1 << (x & 0x1f);\r
195                                 }\r
196                         }\r
197                 }\r
198                 \r
199                 /// <summary> A fast method to retrieve one row of data from the matrix as a BitArray.\r
200                 /// \r
201                 /// </summary>\r
202                 /// <param name="y">The row to retrieve\r
203                 /// </param>\r
204                 /// <param name="row">An optional caller-allocated BitArray, will be allocated if null or too small\r
205                 /// </param>\r
206                 /// <returns> The resulting BitArray - this reference should always be used even when passing\r
207                 /// your own row\r
208                 /// </returns>\r
209                 public BitArray getRow(int y, BitArray row)\r
210                 {\r
211                         if (row == null || row.Size < width)\r
212                         {\r
213                                 row = new BitArray(width);\r
214                         }\r
215                         int offset = y * rowSize;\r
216                         for (int x = 0; x < rowSize; x++)\r
217                         {\r
218                                 row.setBulk(x << 5, bits[offset + x]);\r
219                         }\r
220                         return row;\r
221                 }\r
222                 \r
223                 public override System.String ToString()\r
224                 {\r
225                         System.Text.StringBuilder result = new System.Text.StringBuilder(height * (width + 1));\r
226                         for (int y = 0; y < height; y++)\r
227                         {\r
228                                 for (int x = 0; x < width; x++)\r
229                                 {\r
230                                         result.Append(get_Renamed(x, y)?"X ":"  ");\r
231                                 }\r
232                                 result.Append('\n');\r
233                         }\r
234                         return result.ToString();\r
235                 }\r
236         }\r
237 }