New C# port from Suraj Supekar
[zxing.git] / csharp / common / reedsolomon / GF256.cs
diff --git a/csharp/common/reedsolomon/GF256.cs b/csharp/common/reedsolomon/GF256.cs
new file mode 100755 (executable)
index 0000000..d006633
--- /dev/null
@@ -0,0 +1,174 @@
+/*\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
+* You may obtain a copy of the License at\r
+*\r
+*      http://www.apache.org/licenses/LICENSE-2.0\r
+*\r
+* Unless required by applicable law or agreed to in writing, software\r
+* distributed under the License is distributed on an "AS IS" BASIS,\r
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\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.reedsolomon\r
+{\r
+       \r
+       /// <summary> <p>This class contains utility methods for performing mathematical operations over\r
+       /// the Galois Field GF(256). Operations use a given primitive polynomial in calculations.</p>\r
+       /// \r
+       /// <p>Throughout this package, elements of GF(256) are represented as an <code>int</code>\r
+       /// for convenience and speed (but at the cost of memory).\r
+       /// Only the bottom 8 bits are really used.</p>\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 sealed class GF256\r
+       {\r
+               internal GF256Poly Zero\r
+               {\r
+                       get\r
+                       {\r
+                               return zero;\r
+                       }\r
+                       \r
+               }\r
+               internal GF256Poly One\r
+               {\r
+                       get\r
+                       {\r
+                               return one;\r
+                       }\r
+                       \r
+               }\r
+               \r
+               //UPGRADE_NOTE: Final was removed from the declaration of 'QR_CODE_FIELD '. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'"\r
+               public static readonly GF256 QR_CODE_FIELD = new GF256(0x011D); // x^8 + x^4 + x^3 + x^2 + 1\r
+               //UPGRADE_NOTE: Final was removed from the declaration of 'DATA_MATRIX_FIELD '. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'"\r
+               public static readonly GF256 DATA_MATRIX_FIELD = new GF256(0x012D); // x^8 + x^5 + x^3 + x^2 + 1\r
+               \r
+               //UPGRADE_NOTE: Final was removed from the declaration of 'expTable '. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'"\r
+               private int[] expTable;\r
+               //UPGRADE_NOTE: Final was removed from the declaration of 'logTable '. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'"\r
+               private int[] logTable;\r
+               //UPGRADE_NOTE: Final was removed from the declaration of 'zero '. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'"\r
+               private GF256Poly zero;\r
+               //UPGRADE_NOTE: Final was removed from the declaration of 'one '. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'"\r
+               private GF256Poly one;\r
+               \r
+               /// <summary> Create a representation of GF(256) using the given primitive polynomial.\r
+               /// \r
+               /// </summary>\r
+               /// <param name="primitive">irreducible polynomial whose coefficients are represented by\r
+               /// the bits of an int, where the least-significant bit represents the constant\r
+               /// coefficient\r
+               /// </param>\r
+               private GF256(int primitive)\r
+               {\r
+                       expTable = new int[256];\r
+                       logTable = new int[256];\r
+                       int x = 1;\r
+                       for (int i = 0; i < 256; i++)\r
+                       {\r
+                               expTable[i] = x;\r
+                               x <<= 1; // x = x * 2; we're assuming the generator alpha is 2\r
+                               if (x >= 0x100)\r
+                               {\r
+                                       x ^= primitive;\r
+                               }\r
+                       }\r
+                       for (int i = 0; i < 255; i++)\r
+                       {\r
+                               logTable[expTable[i]] = i;\r
+                       }\r
+                       // logTable[0] == 0 but this should never be used\r
+                       zero = new GF256Poly(this, new int[]{0});\r
+                       one = new GF256Poly(this, new int[]{1});\r
+               }\r
+               \r
+               /// <returns> the monomial representing coefficient * x^degree\r
+               /// </returns>\r
+               internal GF256Poly buildMonomial(int degree, int coefficient)\r
+               {\r
+                       if (degree < 0)\r
+                       {\r
+                               throw new System.ArgumentException();\r
+                       }\r
+                       if (coefficient == 0)\r
+                       {\r
+                               return zero;\r
+                       }\r
+                       int[] coefficients = new int[degree + 1];\r
+                       coefficients[0] = coefficient;\r
+                       return new GF256Poly(this, coefficients);\r
+               }\r
+               \r
+               /// <summary> Implements both addition and subtraction -- they are the same in GF(256).\r
+               /// \r
+               /// </summary>\r
+               /// <returns> sum/difference of a and b\r
+               /// </returns>\r
+               internal static int addOrSubtract(int a, int b)\r
+               {\r
+                       return a ^ b;\r
+               }\r
+               \r
+               /// <returns> 2 to the power of a in GF(256)\r
+               /// </returns>\r
+               internal int exp(int a)\r
+               {\r
+                       return expTable[a];\r
+               }\r
+               \r
+               /// <returns> base 2 log of a in GF(256)\r
+               /// </returns>\r
+               internal int log(int a)\r
+               {\r
+                       if (a == 0)\r
+                       {\r
+                               throw new System.ArgumentException();\r
+                       }\r
+                       return logTable[a];\r
+               }\r
+               \r
+               /// <returns> multiplicative inverse of a\r
+               /// </returns>\r
+               internal int inverse(int a)\r
+               {\r
+                       if (a == 0)\r
+                       {\r
+                               throw new System.ArithmeticException();\r
+                       }\r
+                       return expTable[255 - logTable[a]];\r
+               }\r
+               \r
+               /// <param name="a">\r
+               /// </param>\r
+               /// <param name="b">\r
+               /// </param>\r
+               /// <returns> product of a and b in GF(256)\r
+               /// </returns>\r
+               internal int multiply(int a, int b)\r
+               {\r
+                       if (a == 0 || b == 0)\r
+                       {\r
+                               return 0;\r
+                       }\r
+                       if (a == 1)\r
+                       {\r
+                               return b;\r
+                       }\r
+                       if (b == 1)\r
+                       {\r
+                               return a;\r
+                       }\r
+                       return expTable[(logTable[a] + logTable[b]) % 255];\r
+               }\r
+       }\r
+}
\ No newline at end of file