Korean translation from Chang Hyun Park
[zxing.git] / csharp / common / BitMatrix.cs
index 3d9a993..6612ff4 100755 (executable)
@@ -1,5 +1,5 @@
-/*\r
-* Copyright 2008 ZXing authors\r
+/*\r
+* Copyright 2007 ZXing authors\r
 *\r
 * Licensed under the Apache License, Version 2.0 (the "License");\r
 * you may not use this file except in compliance with the License.\r
 * See the License for the specific language governing permissions and\r
 * limitations under the License.\r
 */\r
+using System;\r
 namespace com.google.zxing.common\r
 {\r
-    using System;\r
-    using System.Text;\r
-\r
-    /// <summary> A class which wraps a 2D array of bytes. The default usage is signed. If you want to use it as a\r
-    /// unsigned container, it's up to you to do byteValue & 0xff at each location.\r
-    /// *\r
-    /// JAVAPORT: I'm not happy about the argument ordering throughout the file, as I always like to have\r
-    /// the horizontal component first, but this is for compatibility with the C++ code. The original\r
-    /// code was a 2D array of ints, but since it only ever gets assigned -1, 0, and 1, I'm going to use\r
-    /// less memory and go with bytes.\r
-    /// *\r
-    /// </summary>\r
-    /// <author>  dswitkin@google.com (Daniel Switkin)\r
-    /// \r
-    /// </author>\r
-    public sealed class BitMatrix\r
-    {\r
-          private int dimension;\r
-          private int[] bits;\r
-\r
-          public BitMatrix(int dimension) {\r
-            if (dimension < 1) {\r
-              throw new Exception("dimension must be at least 1");\r
-            }\r
-            this.dimension = dimension;\r
-            int numBits = dimension * dimension;\r
-            int arraySize = numBits >> 5; // one int per 32 bits\r
-            if ((numBits & 0x1F) != 0) { // plus one more if there are leftovers\r
-              arraySize++;\r
-            }\r
-            bits = new int[arraySize];\r
-          }\r
-\r
-          /**\r
-           * @param i row offset\r
-           * @param j column offset\r
-           * @return value of given bit in matrix\r
-           */\r
-          public bool get(int i, int j) {\r
-            int offset = i + dimension * j;\r
-            //return ((bits[offset >> 5] >>> (offset & 0x1F)) & 0x01) != 0;\r
-                       return ((SupportClass.URShift(bits[offset >> 5], (offset & 0x1F))) & 0x01) != 0;\r
-          }\r
-\r
-          /**\r
-           * <p>Sets the given bit to true.</p>\r
-           *\r
-           * @param i row offset\r
-           * @param j column offset\r
-           */\r
-          public void set(int i, int j) {\r
-            int offset = i + dimension * j;\r
-            bits[offset >> 5] |= 1 << (offset & 0x1F);\r
-          }\r
-\r
-          /**\r
-           * <p>Sets a square region of the bit matrix to true.</p>\r
-           *\r
-           * @param topI row offset of region's top-left corner (inclusive)\r
-           * @param leftJ column offset of region's top-left corner (inclusive)\r
-           * @param height height of region\r
-           * @param width width of region\r
-           */\r
-          public void setRegion(int topI, int leftJ, int height, int width) {\r
-            if (topI < 0 || leftJ < 0) {\r
-              throw new Exception("topI and leftJ must be nonnegative");\r
-            }\r
-            if (height < 1 || width < 1) {\r
-                throw new Exception("height and width must be at least 1");\r
-            }\r
-            int maxJ = leftJ + width;\r
-            int maxI = topI + height;\r
-            if (maxI > dimension || maxJ > dimension) {\r
-                throw new Exception(\r
-                  "topI + height and leftJ + width must be <= matrix dimension");\r
-            }\r
-            for (int j = leftJ; j < maxJ; j++) {\r
-              int jOffset = dimension * j;\r
-              for (int i = topI; i < maxI; i++) {\r
-                int offset = i + jOffset;\r
-                bits[offset >> 5] |= 1 << (offset & 0x1F);\r
-              }\r
-            }\r
-          }\r
-\r
-          /**\r
-           * @return row/column dimension of this matrix\r
-           */\r
-          public int getDimension() {\r
-            return dimension;\r
-          }\r
-\r
-          /**\r
-           * @return array of ints holding internal representation of this matrix's bits\r
-           */\r
-          public int[] getBits() {\r
-            return bits;\r
-          }\r
-\r
-          public String toString() {\r
-              StringBuilder result = new StringBuilder(dimension * (dimension + 1));\r
-            for (int i = 0; i < dimension; i++) {\r
-              for (int j = 0; j < dimension; j++) {\r
-                result.Append(get(i, j) ? "X " : "  ");\r
-              }\r
-              result.Append('\n');\r
-            }\r
-            return result.ToString();\r
-          }\r
-   \r
-    }\r
+       \r
+       /// <summary> <p>Represents a 2D matrix of bits. In function arguments below, and throughout the common\r
+       /// module, x is the column position, and y is the row position. The ordering is always x, y.\r
+       /// The origin is at the top-left.</p>\r
+       /// \r
+       /// <p>Internally the bits are represented in a 1-D array of 32-bit ints. However, each row begins\r
+       /// with a new int. This is done intentionally so that we can copy out a row into a BitArray very\r
+       /// efficiently.</p>\r
+       /// \r
+       /// <p>The ordering of bits is row-major. Within each int, the least significant bits are used first,\r
+       /// meaning they represent lower x values. This is compatible with BitArray's implementation.</p>\r
+       /// \r
+       /// </summary>\r
+       /// <author>  Sean Owen\r
+       /// </author>\r
+       /// <author>  dswitkin@google.com (Daniel Switkin)\r
+       /// </author>\r
+       /// <author>www.Redivivus.in (suraj.supekar@redivivus.in) - Ported from ZXING Java Source \r
+       /// </author>\r
+       public sealed class BitMatrix\r
+       {\r
+               /// <returns> The width of the matrix\r
+               /// </returns>\r
+               public int Width\r
+               {\r
+                       get\r
+                       {\r
+                               return width;\r
+                       }\r
+                       \r
+               }\r
+               /// <returns> The height of the matrix\r
+               /// </returns>\r
+               public int Height\r
+               {\r
+                       get\r
+                       {\r
+                               return height;\r
+                       }\r
+                       \r
+               }\r
+               /// <summary> This method is for compatibility with older code. It's only logical to call if the matrix\r
+               /// is square, so I'm throwing if that's not the case.\r
+               /// \r
+               /// </summary>\r
+               /// <returns> row/column dimension of this matrix\r
+               /// </returns>\r
+               public int Dimension\r
+               {\r
+                       get\r
+                       {\r
+                               if (width != height)\r
+                               {\r
+                                       throw new System.SystemException("Can't call getDimension() on a non-square matrix");\r
+                               }\r
+                               return width;\r
+                       }\r
+                       \r
+               }\r
+               \r
+               // TODO: Just like BitArray, these need to be public so ProGuard can inline them.\r
+               //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
+               public int width;\r
+               //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
+               public int height;\r
+               //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
+               public int rowSize;\r
+               //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
+               public int[] bits;\r
+               \r
+               // A helper to construct a square matrix.\r
+               public BitMatrix(int dimension):this(dimension, dimension)\r
+               {\r
+               }\r
+               \r
+               public BitMatrix(int width, int height)\r
+               {\r
+                       if (width < 1 || height < 1)\r
+                       {\r
+                               throw new System.ArgumentException("Both dimensions must be greater than 0");\r
+                       }\r
+                       this.width = width;\r
+                       this.height = height;\r
+                       int rowSize = width >> 5;\r
+                       if ((width & 0x1f) != 0)\r
+                       {\r
+                               rowSize++;\r
+                       }\r
+                       this.rowSize = rowSize;\r
+                       bits = new int[rowSize * height];\r
+               }\r
+               \r
+               /// <summary> <p>Gets the requested bit, where true means black.</p>\r
+               /// \r
+               /// </summary>\r
+               /// <param name="x">The horizontal component (i.e. which column)\r
+               /// </param>\r
+               /// <param name="y">The vertical component (i.e. which row)\r
+               /// </param>\r
+               /// <returns> value of given bit in matrix\r
+               /// </returns>\r
+               public bool get_Renamed(int x, int y)\r
+               {\r
+                       int offset = y * rowSize + (x >> 5);\r
+                       return ((SupportClass.URShift(bits[offset], (x & 0x1f))) & 1) != 0;\r
+               }\r
+               \r
+               /// <summary> <p>Sets the given bit to true.</p>\r
+               /// \r
+               /// </summary>\r
+               /// <param name="x">The horizontal component (i.e. which column)\r
+               /// </param>\r
+               /// <param name="y">The vertical component (i.e. which row)\r
+               /// </param>\r
+               public void  set_Renamed(int x, int y)\r
+               {\r
+                       int offset = y * rowSize + (x >> 5);\r
+                       bits[offset] |= 1 << (x & 0x1f);\r
+               }\r
+               \r
+               /// <summary> <p>Flips the given bit.</p>\r
+               /// \r
+               /// </summary>\r
+               /// <param name="x">The horizontal component (i.e. which column)\r
+               /// </param>\r
+               /// <param name="y">The vertical component (i.e. which row)\r
+               /// </param>\r
+               public void  flip(int x, int y)\r
+               {\r
+                       int offset = y * rowSize + (x >> 5);\r
+                       bits[offset] ^= 1 << (x & 0x1f);\r
+               }\r
+               \r
+               /// <summary> Clears all bits (sets to false).</summary>\r
+               public void  clear()\r
+               {\r
+                       int max = bits.Length;\r
+                       for (int i = 0; i < max; i++)\r
+                       {\r
+                               bits[i] = 0;\r
+                       }\r
+               }\r
+               \r
+               /// <summary> <p>Sets a square region of the bit matrix to true.</p>\r
+               /// \r
+               /// </summary>\r
+               /// <param name="left">The horizontal position to begin at (inclusive)\r
+               /// </param>\r
+               /// <param name="top">The vertical position to begin at (inclusive)\r
+               /// </param>\r
+               /// <param name="width">The width of the region\r
+               /// </param>\r
+               /// <param name="height">The height of the region\r
+               /// </param>\r
+               public void  setRegion(int left, int top, int width, int height)\r
+               {\r
+                       if (top < 0 || left < 0)\r
+                       {\r
+                               throw new System.ArgumentException("Left and top must be nonnegative");\r
+                       }\r
+                       if (height < 1 || width < 1)\r
+                       {\r
+                               throw new System.ArgumentException("Height and width must be at least 1");\r
+                       }\r
+                       int right = left + width;\r
+                       int bottom = top + height;\r
+                       if (bottom > this.height || right > this.width)\r
+                       {\r
+                               throw new System.ArgumentException("The region must fit inside the matrix");\r
+                       }\r
+                       for (int y = top; y < bottom; y++)\r
+                       {\r
+                               int offset = y * rowSize;\r
+                               for (int x = left; x < right; x++)\r
+                               {\r
+                                       bits[offset + (x >> 5)] |= 1 << (x & 0x1f);\r
+                               }\r
+                       }\r
+               }\r
+               \r
+               /// <summary> A fast method to retrieve one row of data from the matrix as a BitArray.\r
+               /// \r
+               /// </summary>\r
+               /// <param name="y">The row to retrieve\r
+               /// </param>\r
+               /// <param name="row">An optional caller-allocated BitArray, will be allocated if null or too small\r
+               /// </param>\r
+               /// <returns> The resulting BitArray - this reference should always be used even when passing\r
+               /// your own row\r
+               /// </returns>\r
+               public BitArray getRow(int y, BitArray row)\r
+               {\r
+                       if (row == null || row.Size < width)\r
+                       {\r
+                               row = new BitArray(width);\r
+                       }\r
+                       int offset = y * rowSize;\r
+                       for (int x = 0; x < rowSize; x++)\r
+                       {\r
+                               row.setBulk(x << 5, bits[offset + x]);\r
+                       }\r
+                       return row;\r
+               }\r
+               \r
+               public override System.String ToString()\r
+               {\r
+                       System.Text.StringBuilder result = new System.Text.StringBuilder(height * (width + 1));\r
+                       for (int y = 0; y < height; y++)\r
+                       {\r
+                               for (int x = 0; x < width; x++)\r
+                               {\r
+                                       result.Append(get_Renamed(x, y)?"X ":"  ");\r
+                               }\r
+                               result.Append('\n');\r
+                       }\r
+                       return result.ToString();\r
+               }\r
+       }\r
 }
\ No newline at end of file