Morgan's cosmetic improvement to a translation, and equivalent for other translations...
[zxing.git] / csharp / common / GridSampler.cs
index 1964708..bec46b7 100755 (executable)
@@ -1,4 +1,4 @@
-/*\r
+/*\r
 * Copyright 2007 ZXing authors\r
 *\r
 * Licensed under the Apache License, Version 2.0 (the "License");\r
 * See the License for the specific language governing permissions and\r
 * limitations under the License.\r
 */\r
+using System;\r
+using ReaderException = com.google.zxing.ReaderException;\r
 namespace com.google.zxing.common\r
 {\r
-    using System;\r
-    using MonochromeBitmapSource = com.google.zxing.MonochromeBitmapSource;\r
-    using ReaderException = com.google.zxing.ReaderException;\r
-    /// <summary> Implementations of this class can, given locations of finder patterns for a QR code in an\r
-    /// image, sample the right points in the image to reconstruct the QR code, accounting for\r
-    /// perspective distortion. It is abstracted since it is relatively expensive and should be allowed\r
-    /// to take advantage of platform-specific optimized implementations, like Sun's Java Advanced\r
-    /// Imaging library, but which may not be available in other environments such as J2ME, and vice\r
-    /// versa.\r
-    /// *\r
-    /// The implementation used can be controlled by calling {@link #setGridSampler(GridSampler)}\r
-    /// with an instance of a class which implements this interface.\r
-    /// *\r
-    /// </summary>\r
-    /// <author>  Sean Owen\r
-    /// \r
-    /// </author>\r
-    public abstract class GridSampler\r
-    {\r
-        public static GridSampler Instance\r
-        {\r
-            get\r
-            {\r
-                // No real point in trying to make this thread-safe;\r
-                // doesn't matter if a second instance is created\r
-                if (gridSampler == null)\r
-                {\r
-                    gridSampler = new DefaultGridSampler();\r
-                }\r
-                return gridSampler;\r
-            }\r
-\r
-        }\r
-\r
-        private static GridSampler gridSampler = null;\r
-        /// <summary> <p>Samples an image for a square matrix of bits of the given dimension. This is used to extract the\r
-        /// black/white modules of a 2D barcode like a QR Code found in an image. Because this barcode may be\r
-        /// rotated or perspective-distorted, the caller supplies four points in the source image that define\r
-        /// known points in the barcode, so that the image may be sampled appropriately.</p>\r
-        /// *\r
-        /// <p>The last eight "from" parameters are four X/Y coordinate pairs of locations of points in\r
-        /// the image that define some significant points in the image to be sample. For example,\r
-        /// these may be the location of finder pattern in a QR Code.</p>\r
-        /// *\r
-        /// <p>The first eight "to" parameters are four X/Y coordinate pairs measured in the destination\r
-        /// {@link BitMatrix}, from the top left, where the known points in the image given by the "from" parameters\r
-        /// map to.</p>\r
-        /// *\r
-        /// <p>These 16 parameters define the transformation needed to sample the image.</p>\r
-        /// *\r
-        /// </summary>\r
-        /// <param name="image">image to sample\r
-        /// </param>\r
-        /// <param name="dimension">width/height of {@link BitMatrix} to sample from iamge\r
-        /// </param>\r
-        /// <returns> {@link BitMatrix} representing a grid of points sampled from the image within a region\r
-        /// defined by the "from" parameters\r
-        /// @throws ReaderException if image can't be sampled, for example, if the transformation defined by\r
-        /// the given points is invalid or results in sampling outside the image boundaries\r
-        /// \r
-        /// </returns>\r
-        public abstract BitMatrix sampleGrid(MonochromeBitmapSource image, int dimension, float p1ToX, float p1ToY, float p2ToX, float p2ToY, float p3ToX, float p3ToY, float p4ToX, float p4ToY, float p1FromX, float p1FromY, float p2FromX, float p2FromY, float p3FromX, float p3FromY, float p4FromX, float p4FromY);\r
-\r
-        /// <summary> <p>Checks a set of points that have been transformed to sample points on an image against\r
-        /// the image's dimensions to see if the point are even within the image.</p>\r
-        /// *\r
-        /// <p>This method will actually "nudge" the endpoints back onto the image if they are found to be barely\r
-        /// (less than 1 pixel) off the image. This accounts for imperfect detection of finder patterns in an image\r
-        /// where the QR Code runs all the way to the image border.</p>\r
-        /// *\r
-        /// <p>For efficiency, the method will check points from either end of the line until one is found\r
-        /// to be within the image. Because the set of points are assumed to be linear, this is valid.</p>\r
-        /// *\r
-        /// </summary>\r
-        /// <param name="image">image into which the points should map\r
-        /// </param>\r
-        /// <param name="points">actual points in x1,y1,...,xn,yn form\r
-        /// @throws ReaderException if an endpoint is lies outside the image boundaries\r
-        /// \r
-        /// </param>\r
-        protected internal static void checkAndNudgePoints(MonochromeBitmapSource image, float[] points)\r
-        {\r
-            int width = image.getWidth();\r
-            int height = image.getHeight();\r
-            // Check and nudge points from start until we see some that are OK:\r
-            bool nudged = true;\r
-            for (int offset = 0; offset < points.Length && nudged; offset += 2)\r
-            {\r
-                int x = (int)points[offset];\r
-                int y = (int)points[offset + 1];\r
-                if (x < -1 || x > width || y < -1 || y > height)\r
-                {\r
-                    throw new ReaderException("");\r
-                }\r
-                nudged = false;\r
-                if (x == -1)\r
-                {\r
-                    points[offset] = 0.0f;\r
-                    nudged = true;\r
-                }\r
-                else if (x == width)\r
-                {\r
-                    points[offset] = width - 1;\r
-                    nudged = true;\r
-                }\r
-                if (y == -1)\r
-                {\r
-                    points[offset + 1] = 0.0f;\r
-                    nudged = true;\r
-                }\r
-                else if (y == height)\r
-                {\r
-                    points[offset + 1] = height - 1;\r
-                    nudged = true;\r
-                }\r
-            }\r
-            // Check and nudge points from end:\r
-            nudged = true;\r
-            for (int offset = points.Length - 2; offset >= 0 && nudged; offset -= 2)\r
-            {\r
-                int x = (int)points[offset];\r
-                int y = (int)points[offset + 1];\r
-                if (x < -1 || x > width || y < -1 || y > height)\r
-                {\r
-                    throw new ReaderException("");\r
-                }\r
-                nudged = false;\r
-                if (x == -1)\r
-                {\r
-                    points[offset] = 0.0f;\r
-                    nudged = true;\r
-                }\r
-                else if (x == width)\r
-                {\r
-                    points[offset] = width - 1;\r
-                    nudged = true;\r
-                }\r
-                if (y == -1)\r
-                {\r
-                    points[offset + 1] = 0.0f;\r
-                    nudged = true;\r
-                }\r
-                else if (y == height)\r
-                {\r
-                    points[offset + 1] = height - 1;\r
-                    nudged = true;\r
-                }\r
-            }\r
-        }\r
-    }\r
+       \r
+       /// <summary> Implementations of this class can, given locations of finder patterns for a QR code in an\r
+       /// image, sample the right points in the image to reconstruct the QR code, accounting for\r
+       /// perspective distortion. It is abstracted since it is relatively expensive and should be allowed\r
+       /// to take advantage of platform-specific optimized implementations, like Sun's Java Advanced\r
+       /// Imaging library, but which may not be available in other environments such as J2ME, and vice\r
+       /// versa.\r
+       /// \r
+       /// The implementation used can be controlled by calling {@link #setGridSampler(GridSampler)}\r
+       /// with an instance of a class which implements this interface.\r
+       /// \r
+       /// </summary>\r
+       /// <author>  Sean Owen\r
+       /// </author>\r
+       /// <author>www.Redivivus.in (suraj.supekar@redivivus.in) - Ported from ZXING Java Source \r
+       /// </author>\r
+       public abstract class GridSampler\r
+       {\r
+               /// <returns> the current implementation of {@link GridSampler}\r
+               /// </returns>\r
+               public static GridSampler Instance\r
+               {\r
+                       get\r
+                       {\r
+                               return gridSampler;\r
+                       }\r
+                       \r
+               }\r
+               \r
+               private static GridSampler gridSampler = new DefaultGridSampler();\r
+               \r
+               /// <summary> Sets the implementation of {@link GridSampler} used by the library. One global\r
+               /// instance is stored, which may sound problematic. But, the implementation provided\r
+               /// ought to be appropriate for the entire platform, and all uses of this library\r
+               /// in the whole lifetime of the JVM. For instance, an Android activity can swap in\r
+               /// an implementation that takes advantage of native platform libraries.\r
+               /// \r
+               /// </summary>\r
+               /// <param name="newGridSampler">The platform-specific object to install.\r
+               /// </param>\r
+               public static void  setGridSampler(GridSampler newGridSampler)\r
+               {\r
+                       if (newGridSampler == null)\r
+                       {\r
+                               throw new System.ArgumentException();\r
+                       }\r
+                       gridSampler = newGridSampler;\r
+               }\r
+               \r
+               /// <summary> <p>Samples an image for a square matrix of bits of the given dimension. This is used to extract\r
+               /// the black/white modules of a 2D barcode like a QR Code found in an image. Because this barcode\r
+               /// may be rotated or perspective-distorted, the caller supplies four points in the source image\r
+               /// that define known points in the barcode, so that the image may be sampled appropriately.</p>\r
+               /// \r
+               /// <p>The last eight "from" parameters are four X/Y coordinate pairs of locations of points in\r
+               /// the image that define some significant points in the image to be sample. For example,\r
+               /// these may be the location of finder pattern in a QR Code.</p>\r
+               /// \r
+               /// <p>The first eight "to" parameters are four X/Y coordinate pairs measured in the destination\r
+               /// {@link BitMatrix}, from the top left, where the known points in the image given by the "from"\r
+               /// parameters map to.</p>\r
+               /// \r
+               /// <p>These 16 parameters define the transformation needed to sample the image.</p>\r
+               /// \r
+               /// </summary>\r
+               /// <param name="image">image to sample\r
+               /// </param>\r
+               /// <param name="dimension">width/height of {@link BitMatrix} to sample from image\r
+               /// </param>\r
+               /// <returns> {@link BitMatrix} representing a grid of points sampled from the image within a region\r
+               /// defined by the "from" parameters\r
+               /// </returns>\r
+               /// <throws>  ReaderException if image can't be sampled, for example, if the transformation defined </throws>\r
+               /// <summary>   by the given points is invalid or results in sampling outside the image boundaries\r
+               /// </summary>\r
+               public abstract BitMatrix sampleGrid(BitMatrix image, int dimension, float p1ToX, float p1ToY, float p2ToX, float p2ToY, float p3ToX, float p3ToY, float p4ToX, float p4ToY, float p1FromX, float p1FromY, float p2FromX, float p2FromY, float p3FromX, float p3FromY, float p4FromX, float p4FromY);\r
+               \r
+               public virtual BitMatrix sampleGrid(BitMatrix image, int dimension, PerspectiveTransform transform)\r
+               {\r
+                       throw new System.NotSupportedException();\r
+               }\r
+               \r
+               \r
+               /// <summary> <p>Checks a set of points that have been transformed to sample points on an image against\r
+               /// the image's dimensions to see if the point are even within the image.</p>\r
+               /// \r
+               /// <p>This method will actually "nudge" the endpoints back onto the image if they are found to be\r
+               /// barely (less than 1 pixel) off the image. This accounts for imperfect detection of finder\r
+               /// patterns in an image where the QR Code runs all the way to the image border.</p>\r
+               /// \r
+               /// <p>For efficiency, the method will check points from either end of the line until one is found\r
+               /// to be within the image. Because the set of points are assumed to be linear, this is valid.</p>\r
+               /// \r
+               /// </summary>\r
+               /// <param name="image">image into which the points should map\r
+               /// </param>\r
+               /// <param name="points">actual points in x1,y1,...,xn,yn form\r
+               /// </param>\r
+               /// <throws>  ReaderException if an endpoint is lies outside the image boundaries </throws>\r
+               protected internal static void  checkAndNudgePoints(BitMatrix image, float[] points)\r
+               {\r
+                       int width = image.Width;\r
+                       int height = image.Height;\r
+                       // Check and nudge points from start until we see some that are OK:\r
+                       bool nudged = true;\r
+                       for (int offset = 0; offset < points.Length && nudged; offset += 2)\r
+                       {\r
+                               //UPGRADE_WARNING: Data types in Visual C# might be different.  Verify the accuracy of narrowing conversions. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1042'"\r
+                               int x = (int) points[offset];\r
+                               //UPGRADE_WARNING: Data types in Visual C# might be different.  Verify the accuracy of narrowing conversions. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1042'"\r
+                               int y = (int) points[offset + 1];\r
+                               if (x < - 1 || x > width || y < - 1 || y > height)\r
+                               {\r
+                                       throw ReaderException.Instance;\r
+                               }\r
+                               nudged = false;\r
+                               if (x == - 1)\r
+                               {\r
+                                       points[offset] = 0.0f;\r
+                                       nudged = true;\r
+                               }\r
+                               else if (x == width)\r
+                               {\r
+                                       points[offset] = width - 1;\r
+                                       nudged = true;\r
+                               }\r
+                               if (y == - 1)\r
+                               {\r
+                                       points[offset + 1] = 0.0f;\r
+                                       nudged = true;\r
+                               }\r
+                               else if (y == height)\r
+                               {\r
+                                       points[offset + 1] = height - 1;\r
+                                       nudged = true;\r
+                               }\r
+                       }\r
+                       // Check and nudge points from end:\r
+                       nudged = true;\r
+                       for (int offset = points.Length - 2; offset >= 0 && nudged; offset -= 2)\r
+                       {\r
+                               //UPGRADE_WARNING: Data types in Visual C# might be different.  Verify the accuracy of narrowing conversions. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1042'"\r
+                               int x = (int) points[offset];\r
+                               //UPGRADE_WARNING: Data types in Visual C# might be different.  Verify the accuracy of narrowing conversions. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1042'"\r
+                               int y = (int) points[offset + 1];\r
+                               if (x < - 1 || x > width || y < - 1 || y > height)\r
+                               {\r
+                                       throw ReaderException.Instance;\r
+                               }\r
+                               nudged = false;\r
+                               if (x == - 1)\r
+                               {\r
+                                       points[offset] = 0.0f;\r
+                                       nudged = true;\r
+                               }\r
+                               else if (x == width)\r
+                               {\r
+                                       points[offset] = width - 1;\r
+                                       nudged = true;\r
+                               }\r
+                               if (y == - 1)\r
+                               {\r
+                                       points[offset + 1] = 0.0f;\r
+                                       nudged = true;\r
+                               }\r
+                               else if (y == height)\r
+                               {\r
+                                       points[offset + 1] = height - 1;\r
+                                       nudged = true;\r
+                               }\r
+                       }\r
+               }\r
+       }\r
 }
\ No newline at end of file