Committed C# port from Mohamad
[zxing.git] / csharp / common / DefaultGridSampler.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     /// <author>  Sean Owen\r
22     /// \r
23     /// </author>\r
24     public sealed class DefaultGridSampler : GridSampler\r
25     {\r
26 \r
27         public override 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
28         {\r
29             PerspectiveTransform transform = PerspectiveTransform.quadrilateralToQuadrilateral(p1ToX, p1ToY, p2ToX, p2ToY, p3ToX, p3ToY, p4ToX, p4ToY, p1FromX, p1FromY, p2FromX, p2FromY, p3FromX, p3FromY, p4FromX, p4FromY);\r
30 \r
31             BitMatrix bits = new BitMatrix(dimension);\r
32             float[] points = new float[dimension << 1];\r
33             for (int i = 0; i < dimension; i++)\r
34             {\r
35                 int max = points.Length;\r
36                 float iValue = (float)i + 0.5f;\r
37                 for (int j = 0; j < max; j += 2)\r
38                 {\r
39                     points[j] = (float)(j >> 1) + 0.5f;\r
40                     points[j + 1] = iValue;\r
41                 }\r
42                 transform.transformPoints(points);\r
43                 // Quick check to see if points transformed to something inside the image;\r
44                 // sufficent to check the endpoints\r
45                 checkAndNudgePoints(image, points);\r
46                 try\r
47                 {\r
48                     for (int j = 0; j < max; j += 2)\r
49                     {\r
50                         //UPGRADE_WARNING: Narrowing conversions may produce unexpected results in C#. 'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="jlca1042"'\r
51                         if (image.isBlack((int)points[j], (int)points[j + 1]))\r
52                         {\r
53                             // Black(-ish) pixel\r
54                             bits.set(i, j >> 1);\r
55                         }\r
56                     }\r
57                 }\r
58                 catch (System.IndexOutOfRangeException aioobe)\r
59                 {\r
60                     // This feels wrong, but, sometimes if the finder patterns are misidentified, the resulting\r
61                     // transform gets "twisted" such that it maps a straight line of points to a set of points\r
62                     // whose endpoints are in bounds, but others are not. There is probably some mathematical\r
63                     // way to detect this about the transformation that I don't know yet.\r
64                     // This results in an ugly runtime exception despite our clever checks above -- can't have that.\r
65                     // We could check each point's coordinates but that feels duplicative. We settle for\r
66                     // catching and wrapping ArrayIndexOutOfBoundsException.\r
67                     throw new ReaderException(aioobe.Message);\r
68                 }\r
69             }\r
70             return bits;\r
71         }\r
72     }\r
73 }