Don't need to block multiple thread access. Refactor and update a bit for an upcoming...
[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 using System;\r
17 using ReaderException = com.google.zxing.ReaderException;\r
18 namespace com.google.zxing.common\r
19 {\r
20         \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         /// </author>\r
34         /// <author>www.Redivivus.in (suraj.supekar@redivivus.in) - Ported from ZXING Java Source \r
35         /// </author>\r
36         public abstract class GridSampler\r
37         {\r
38                 /// <returns> the current implementation of {@link GridSampler}\r
39                 /// </returns>\r
40                 public static GridSampler Instance\r
41                 {\r
42                         get\r
43                         {\r
44                                 return gridSampler;\r
45                         }\r
46                         \r
47                 }\r
48                 \r
49                 private static GridSampler gridSampler = new DefaultGridSampler();\r
50                 \r
51                 /// <summary> Sets the implementation of {@link GridSampler} used by the library. One global\r
52                 /// instance is stored, which may sound problematic. But, the implementation provided\r
53                 /// ought to be appropriate for the entire platform, and all uses of this library\r
54                 /// in the whole lifetime of the JVM. For instance, an Android activity can swap in\r
55                 /// an implementation that takes advantage of native platform libraries.\r
56                 /// \r
57                 /// </summary>\r
58                 /// <param name="newGridSampler">The platform-specific object to install.\r
59                 /// </param>\r
60                 public static void  setGridSampler(GridSampler newGridSampler)\r
61                 {\r
62                         if (newGridSampler == null)\r
63                         {\r
64                                 throw new System.ArgumentException();\r
65                         }\r
66                         gridSampler = newGridSampler;\r
67                 }\r
68                 \r
69                 /// <summary> <p>Samples an image for a square matrix of bits of the given dimension. This is used to extract\r
70                 /// the black/white modules of a 2D barcode like a QR Code found in an image. Because this barcode\r
71                 /// may be rotated or perspective-distorted, the caller supplies four points in the source image\r
72                 /// that define known points in the barcode, so that the image may be sampled appropriately.</p>\r
73                 /// \r
74                 /// <p>The last eight "from" parameters are four X/Y coordinate pairs of locations of points in\r
75                 /// the image that define some significant points in the image to be sample. For example,\r
76                 /// these may be the location of finder pattern in a QR Code.</p>\r
77                 /// \r
78                 /// <p>The first eight "to" parameters are four X/Y coordinate pairs measured in the destination\r
79                 /// {@link BitMatrix}, from the top left, where the known points in the image given by the "from"\r
80                 /// parameters map to.</p>\r
81                 /// \r
82                 /// <p>These 16 parameters define the transformation needed to sample the image.</p>\r
83                 /// \r
84                 /// </summary>\r
85                 /// <param name="image">image to sample\r
86                 /// </param>\r
87                 /// <param name="dimension">width/height of {@link BitMatrix} to sample from image\r
88                 /// </param>\r
89                 /// <returns> {@link BitMatrix} representing a grid of points sampled from the image within a region\r
90                 /// defined by the "from" parameters\r
91                 /// </returns>\r
92                 /// <throws>  ReaderException if image can't be sampled, for example, if the transformation defined </throws>\r
93                 /// <summary>   by the given points is invalid or results in sampling outside the image boundaries\r
94                 /// </summary>\r
95                 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
96                 \r
97                 public virtual BitMatrix sampleGrid(BitMatrix image, int dimension, PerspectiveTransform transform)\r
98                 {\r
99                         throw new System.NotSupportedException();\r
100                 }\r
101                 \r
102                 \r
103                 /// <summary> <p>Checks a set of points that have been transformed to sample points on an image against\r
104                 /// the image's dimensions to see if the point are even within the image.</p>\r
105                 /// \r
106                 /// <p>This method will actually "nudge" the endpoints back onto the image if they are found to be\r
107                 /// barely (less than 1 pixel) off the image. This accounts for imperfect detection of finder\r
108                 /// patterns in an image where the QR Code runs all the way to the image border.</p>\r
109                 /// \r
110                 /// <p>For efficiency, the method will check points from either end of the line until one is found\r
111                 /// to be within the image. Because the set of points are assumed to be linear, this is valid.</p>\r
112                 /// \r
113                 /// </summary>\r
114                 /// <param name="image">image into which the points should map\r
115                 /// </param>\r
116                 /// <param name="points">actual points in x1,y1,...,xn,yn form\r
117                 /// </param>\r
118                 /// <throws>  ReaderException if an endpoint is lies outside the image boundaries </throws>\r
119                 protected internal static void  checkAndNudgePoints(BitMatrix image, float[] points)\r
120                 {\r
121                         int width = image.Width;\r
122                         int height = image.Height;\r
123                         // Check and nudge points from start until we see some that are OK:\r
124                         bool nudged = true;\r
125                         for (int offset = 0; offset < points.Length && nudged; offset += 2)\r
126                         {\r
127                                 //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
128                                 int x = (int) points[offset];\r
129                                 //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
130                                 int y = (int) points[offset + 1];\r
131                                 if (x < - 1 || x > width || y < - 1 || y > height)\r
132                                 {\r
133                                         throw ReaderException.Instance;\r
134                                 }\r
135                                 nudged = false;\r
136                                 if (x == - 1)\r
137                                 {\r
138                                         points[offset] = 0.0f;\r
139                                         nudged = true;\r
140                                 }\r
141                                 else if (x == width)\r
142                                 {\r
143                                         points[offset] = width - 1;\r
144                                         nudged = true;\r
145                                 }\r
146                                 if (y == - 1)\r
147                                 {\r
148                                         points[offset + 1] = 0.0f;\r
149                                         nudged = true;\r
150                                 }\r
151                                 else if (y == height)\r
152                                 {\r
153                                         points[offset + 1] = height - 1;\r
154                                         nudged = true;\r
155                                 }\r
156                         }\r
157                         // Check and nudge points from end:\r
158                         nudged = true;\r
159                         for (int offset = points.Length - 2; offset >= 0 && nudged; offset -= 2)\r
160                         {\r
161                                 //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
162                                 int x = (int) points[offset];\r
163                                 //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
164                                 int y = (int) points[offset + 1];\r
165                                 if (x < - 1 || x > width || y < - 1 || y > height)\r
166                                 {\r
167                                         throw ReaderException.Instance;\r
168                                 }\r
169                                 nudged = false;\r
170                                 if (x == - 1)\r
171                                 {\r
172                                         points[offset] = 0.0f;\r
173                                         nudged = true;\r
174                                 }\r
175                                 else if (x == width)\r
176                                 {\r
177                                         points[offset] = width - 1;\r
178                                         nudged = true;\r
179                                 }\r
180                                 if (y == - 1)\r
181                                 {\r
182                                         points[offset + 1] = 0.0f;\r
183                                         nudged = true;\r
184                                 }\r
185                                 else if (y == height)\r
186                                 {\r
187                                         points[offset + 1] = height - 1;\r
188                                         nudged = true;\r
189                                 }\r
190                         }\r
191                 }\r
192         }\r
193 }