Committed C# port from Mohamad
[zxing.git] / csharp / common / GridSampler.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 namespace com.google.zxing.common\r
17 {\r
18     using System;\r
19     using MonochromeBitmapSource = com.google.zxing.MonochromeBitmapSource;\r
20     using ReaderException = com.google.zxing.ReaderException;\r
21     /// <summary> Implementations of this class can, given locations of finder patterns for a QR code in an\r
22     /// image, sample the right points in the image to reconstruct the QR code, accounting for\r
23     /// perspective distortion. It is abstracted since it is relatively expensive and should be allowed\r
24     /// to take advantage of platform-specific optimized implementations, like Sun's Java Advanced\r
25     /// Imaging library, but which may not be available in other environments such as J2ME, and vice\r
26     /// versa.\r
27     /// *\r
28     /// The implementation used can be controlled by calling {@link #setGridSampler(GridSampler)}\r
29     /// with an instance of a class which implements this interface.\r
30     /// *\r
31     /// </summary>\r
32     /// <author>  Sean Owen\r
33     /// \r
34     /// </author>\r
35     public abstract class GridSampler\r
36     {\r
37         public static GridSampler Instance\r
38         {\r
39             get\r
40             {\r
41                 // No real point in trying to make this thread-safe;\r
42                 // doesn't matter if a second instance is created\r
43                 if (gridSampler == null)\r
44                 {\r
45                     gridSampler = new DefaultGridSampler();\r
46                 }\r
47                 return gridSampler;\r
48             }\r
49 \r
50         }\r
51 \r
52         private static GridSampler gridSampler = null;\r
53         /// <summary> <p>Samples an image for a square matrix of bits of the given dimension. This is used to extract the\r
54         /// black/white modules of a 2D barcode like a QR Code found in an image. Because this barcode may be\r
55         /// rotated or perspective-distorted, the caller supplies four points in the source image that define\r
56         /// known points in the barcode, so that the image may be sampled appropriately.</p>\r
57         /// *\r
58         /// <p>The last eight "from" parameters are four X/Y coordinate pairs of locations of points in\r
59         /// the image that define some significant points in the image to be sample. For example,\r
60         /// these may be the location of finder pattern in a QR Code.</p>\r
61         /// *\r
62         /// <p>The first eight "to" parameters are four X/Y coordinate pairs measured in the destination\r
63         /// {@link BitMatrix}, from the top left, where the known points in the image given by the "from" parameters\r
64         /// map to.</p>\r
65         /// *\r
66         /// <p>These 16 parameters define the transformation needed to sample the image.</p>\r
67         /// *\r
68         /// </summary>\r
69         /// <param name="image">image to sample\r
70         /// </param>\r
71         /// <param name="dimension">width/height of {@link BitMatrix} to sample from iamge\r
72         /// </param>\r
73         /// <returns> {@link BitMatrix} representing a grid of points sampled from the image within a region\r
74         /// defined by the "from" parameters\r
75         /// @throws ReaderException if image can't be sampled, for example, if the transformation defined by\r
76         /// the given points is invalid or results in sampling outside the image boundaries\r
77         /// \r
78         /// </returns>\r
79         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
80 \r
81         /// <summary> <p>Checks a set of points that have been transformed to sample points on an image against\r
82         /// the image's dimensions to see if the point are even within the image.</p>\r
83         /// *\r
84         /// <p>This method will actually "nudge" the endpoints back onto the image if they are found to be barely\r
85         /// (less than 1 pixel) off the image. This accounts for imperfect detection of finder patterns in an image\r
86         /// where the QR Code runs all the way to the image border.</p>\r
87         /// *\r
88         /// <p>For efficiency, the method will check points from either end of the line until one is found\r
89         /// to be within the image. Because the set of points are assumed to be linear, this is valid.</p>\r
90         /// *\r
91         /// </summary>\r
92         /// <param name="image">image into which the points should map\r
93         /// </param>\r
94         /// <param name="points">actual points in x1,y1,...,xn,yn form\r
95         /// @throws ReaderException if an endpoint is lies outside the image boundaries\r
96         /// \r
97         /// </param>\r
98         protected internal static void checkAndNudgePoints(MonochromeBitmapSource image, float[] points)\r
99         {\r
100             int width = image.getWidth();\r
101             int height = image.getHeight();\r
102             // Check and nudge points from start until we see some that are OK:\r
103             bool nudged = true;\r
104             for (int offset = 0; offset < points.Length && nudged; offset += 2)\r
105             {\r
106                 int x = (int)points[offset];\r
107                 int y = (int)points[offset + 1];\r
108                 if (x < -1 || x > width || y < -1 || y > height)\r
109                 {\r
110                     throw new ReaderException("");\r
111                 }\r
112                 nudged = false;\r
113                 if (x == -1)\r
114                 {\r
115                     points[offset] = 0.0f;\r
116                     nudged = true;\r
117                 }\r
118                 else if (x == width)\r
119                 {\r
120                     points[offset] = width - 1;\r
121                     nudged = true;\r
122                 }\r
123                 if (y == -1)\r
124                 {\r
125                     points[offset + 1] = 0.0f;\r
126                     nudged = true;\r
127                 }\r
128                 else if (y == height)\r
129                 {\r
130                     points[offset + 1] = height - 1;\r
131                     nudged = true;\r
132                 }\r
133             }\r
134             // Check and nudge points from end:\r
135             nudged = true;\r
136             for (int offset = points.Length - 2; offset >= 0 && nudged; offset -= 2)\r
137             {\r
138                 int x = (int)points[offset];\r
139                 int y = (int)points[offset + 1];\r
140                 if (x < -1 || x > width || y < -1 || y > height)\r
141                 {\r
142                     throw new ReaderException("");\r
143                 }\r
144                 nudged = false;\r
145                 if (x == -1)\r
146                 {\r
147                     points[offset] = 0.0f;\r
148                     nudged = true;\r
149                 }\r
150                 else if (x == width)\r
151                 {\r
152                     points[offset] = width - 1;\r
153                     nudged = true;\r
154                 }\r
155                 if (y == -1)\r
156                 {\r
157                     points[offset + 1] = 0.0f;\r
158                     nudged = true;\r
159                 }\r
160                 else if (y == height)\r
161                 {\r
162                     points[offset + 1] = height - 1;\r
163                     nudged = true;\r
164                 }\r
165             }\r
166         }\r
167     }\r
168 }