fix log to reflect reality
[zxing.git] / csharp / qrcode / QRCodeWriter.cs
index 8807537..1fb1947 100755 (executable)
@@ -1,5 +1,5 @@
-/*\r
-* Copyright 2007 ZXing authors\r
+/*\r
+* Copyright 2008 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
 * See the License for the specific language governing permissions and\r
 * limitations under the License.\r
 */\r
+using System;\r
+using BarcodeFormat = com.google.zxing.BarcodeFormat;\r
+using EncodeHintType = com.google.zxing.EncodeHintType;\r
+using Writer = com.google.zxing.Writer;\r
+using WriterException = com.google.zxing.WriterException;\r
+using ByteMatrix = com.google.zxing.common.ByteMatrix;\r
+using Encoder = com.google.zxing.qrcode.encoder.Encoder;\r
+using QRCode = com.google.zxing.qrcode.encoder.QRCode;\r
+using ErrorCorrectionLevel = com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;\r
 namespace com.google.zxing.qrcode\r
 {\r
-    using System;\r
-    using System.Collections;\r
-    using com.google.zxing.common;\r
-    using com.google.zxing.qrcode.decoder;\r
-    using com.google.zxing.qrcode.detector;\r
-    using com.google.zxing.qrcode.encoder;\r
-\r
-    public sealed class QRCodeWriter : Writer\r
-    { \r
-          private static  int QUIET_ZONE_SIZE = 4;\r
-          public ByteMatrix encode(String contents, BarcodeFormat format, int width, int height)\r
-          {\r
-              try{\r
-                return encode(contents, format, width, height, null);\r
-              }catch(Exception e){\r
-                throw new WriterException(e.Message);\r
-              }            \r
-          }\r
-\r
-          public ByteMatrix encode(String contents, BarcodeFormat format, int width, int height,Hashtable hints)  {\r
-\r
-            if (contents == null || contents.Length == 0) {\r
-              throw new ArgumentException("Found empty contents");\r
-            }\r
-\r
-            if (format != BarcodeFormat.QR_CODE) {\r
-              throw new ArgumentException("Can only encode QR_CODE, but got " + format);\r
-            }\r
-\r
-            if (width < 0 || height < 0) {\r
-              throw new ArgumentException("Requested dimensions are too small: " + width + 'x' +\r
-                  height);\r
-            }\r
-\r
-            ErrorCorrectionLevel errorCorrectionLevel = ErrorCorrectionLevel.L;\r
-            if (hints != null) {\r
-              ErrorCorrectionLevel requestedECLevel = (ErrorCorrectionLevel) hints[EncodeHintType.ERROR_CORRECTION];\r
-              if (requestedECLevel != null) {\r
-                errorCorrectionLevel = requestedECLevel;\r
-              }\r
-            }\r
-\r
-            QRCode code = new QRCode();\r
-            Encoder.encode(contents, errorCorrectionLevel, code);\r
-            return renderResult(code, width, height);\r
-          }\r
-\r
-          // Note that the input matrix uses 0 == white, 1 == black, while the output matrix uses\r
-          // 0 == black, 255 == white (i.e. an 8 bit greyscale bitmap).\r
-          private static ByteMatrix renderResult(QRCode code, int width, int height) {\r
-            ByteMatrix input = code.getMatrix();\r
-            int inputWidth = input.width();\r
-            int inputHeight = input.height();\r
-            int qrWidth = inputWidth + (QUIET_ZONE_SIZE << 1);\r
-            int qrHeight = inputHeight + (QUIET_ZONE_SIZE << 1);\r
-            int outputWidth = Math.Max(width, qrWidth);\r
-            int outputHeight = Math.Max(height, qrHeight);\r
-\r
-            int multiple = Math.Min(outputWidth / qrWidth, outputHeight / qrHeight);\r
-            // Padding includes both the quiet zone and the extra white pixels to accomodate the requested\r
-            // dimensions. For example, if input is 25x25 the QR will be 33x33 including the quiet zone.\r
-            // If the requested size is 200x160, the multiple will be 4, for a QR of 132x132. These will\r
-            // handle all the padding from 100x100 (the actual QR) up to 200x160.\r
-            int leftPadding = (outputWidth - (inputWidth * multiple)) / 2;\r
-            int topPadding = (outputHeight - (inputHeight * multiple)) / 2;\r
-\r
-            ByteMatrix output = new ByteMatrix(outputHeight, outputWidth);\r
-            sbyte[][] outputArray = output.getArray();\r
-\r
-            // We could be tricky and use the first row in each set of multiple as the temporary storage,\r
-            // instead of allocating this separate array.\r
-            sbyte[] row = new sbyte[outputWidth];\r
-\r
-            // 1. Write the white lines at the top\r
-            for (int y = 0; y < topPadding; y++) {\r
-              setRowColor(outputArray[y], unchecked((sbyte)255));\r
-            }\r
-\r
-            // 2. Expand the QR image to the multiple\r
-            sbyte[][] inputArray = input.getArray();\r
-            for (int y = 0; y < inputHeight; y++) {\r
-              // a. Write the white pixels at the left of each row\r
-              for (int x = 0; x < leftPadding; x++) {\r
-                row[x] = unchecked((sbyte) 255);\r
-              }\r
-\r
-              // b. Write the contents of this row of the barcode\r
-              int offset = leftPadding;\r
-              for (int x = 0; x < inputWidth; x++) {\r
-                sbyte value = (inputArray[y][x] == 1) ? (sbyte) 0 : unchecked((sbyte) 255);\r
-                for (int z = 0; z < multiple; z++) {\r
-                  row[offset + z] = value;\r
-                }\r
-                offset += multiple;\r
-              }\r
-\r
-              // c. Write the white pixels at the right of each row\r
-              offset = leftPadding + (inputWidth * multiple);\r
-              for (int x = offset; x < outputWidth; x++) {\r
-                row[x] = unchecked((sbyte) 255);\r
-              }\r
-\r
-              // d. Write the completed row multiple times\r
-              offset = topPadding + (y * multiple);\r
-              for (int z = 0; z < multiple; z++) {\r
-                System.Array.Copy(row, 0, outputArray[offset + z], 0, outputWidth);\r
-              }\r
-            }\r
-\r
-            // 3. Write the white lines at the bottom\r
-            int offset2 = topPadding + (inputHeight * multiple);\r
-            for (int y = offset2; y < outputHeight; y++) {\r
-              setRowColor(outputArray[y], unchecked((sbyte) 255));\r
-            }\r
-\r
-            // Added per beyonddeath\r
-            for (int x = 0; x < outputHeight; x++) {\r
-              for (int y = 0; y < outputWidth; y++) {\r
-                output.set(y, x, outputArray[x][y]);\r
-              }\r
-            }\r
-\r
-            return output;\r
-          }\r
-\r
-          private static void setRowColor(sbyte[] row, sbyte value) {\r
-            for (int x = 0; x < row.Length; x++) {\r
-              row[x] = value;\r
-            }\r
-          }\r
-    \r
-    }\r
-}\r
+       \r
+       /// <summary> This object renders a QR Code as a ByteMatrix 2D array of greyscale values.\r
+       /// \r
+       /// </summary>\r
+       /// <author>  dswitkin@google.com (Daniel Switkin)\r
+       /// </author>\r
+       /// <author>www.Redivivus.in (suraj.supekar@redivivus.in) - Ported from ZXING Java Source \r
+       /// </author>\r
+       public sealed class QRCodeWriter : Writer\r
+       {\r
+               \r
+               private const int QUIET_ZONE_SIZE = 4;\r
+               \r
+               public ByteMatrix encode(System.String contents, BarcodeFormat format, int width, int height)\r
+               {\r
+                       \r
+                       return encode(contents, format, width, height, null);\r
+               }\r
+               \r
+               public ByteMatrix encode(System.String contents, BarcodeFormat format, int width, int height, System.Collections.Hashtable hints)\r
+               {\r
+                       \r
+                       if (contents == null || contents.Length == 0)\r
+                       {\r
+                               throw new System.ArgumentException("Found empty contents");\r
+                       }\r
+                       \r
+                       if (format != BarcodeFormat.QR_CODE)\r
+                       {\r
+                               throw new System.ArgumentException("Can only encode QR_CODE, but got " + format);\r
+                       }\r
+                       \r
+                       if (width < 0 || height < 0)\r
+                       {\r
+                               throw new System.ArgumentException("Requested dimensions are too small: " + width + 'x' + height);\r
+                       }\r
+                       \r
+                       ErrorCorrectionLevel errorCorrectionLevel = ErrorCorrectionLevel.L;\r
+                       if (hints != null)\r
+                       {\r
+                               ErrorCorrectionLevel requestedECLevel = (ErrorCorrectionLevel) hints[EncodeHintType.ERROR_CORRECTION];\r
+                               if (requestedECLevel != null)\r
+                               {\r
+                                       errorCorrectionLevel = requestedECLevel;\r
+                               }\r
+                       }\r
+                       \r
+                       QRCode code = new QRCode();\r
+                       Encoder.encode(contents, errorCorrectionLevel, hints, code);\r
+                       return renderResult(code, width, height);\r
+               }\r
+               \r
+               // Note that the input matrix uses 0 == white, 1 == black, while the output matrix uses\r
+               // 0 == black, 255 == white (i.e. an 8 bit greyscale bitmap).\r
+               private static ByteMatrix renderResult(QRCode code, int width, int height)\r
+               {\r
+                       ByteMatrix input = code.Matrix;\r
+                       int inputWidth = input.Width;\r
+                       int inputHeight = input.Height;\r
+                       int qrWidth = inputWidth + (QUIET_ZONE_SIZE << 1);\r
+                       int qrHeight = inputHeight + (QUIET_ZONE_SIZE << 1);\r
+                       int outputWidth = System.Math.Max(width, qrWidth);\r
+                       int outputHeight = System.Math.Max(height, qrHeight);\r
+                       \r
+                       int multiple = System.Math.Min(outputWidth / qrWidth, outputHeight / qrHeight);\r
+                       // Padding includes both the quiet zone and the extra white pixels to accommodate the requested\r
+                       // dimensions. For example, if input is 25x25 the QR will be 33x33 including the quiet zone.\r
+                       // If the requested size is 200x160, the multiple will be 4, for a QR of 132x132. These will\r
+                       // handle all the padding from 100x100 (the actual QR) up to 200x160.\r
+                       int leftPadding = (outputWidth - (inputWidth * multiple)) / 2;\r
+                       int topPadding = (outputHeight - (inputHeight * multiple)) / 2;\r
+                       \r
+                       ByteMatrix output = new ByteMatrix(outputWidth, outputHeight);\r
+                       sbyte[][] outputArray = output.Array;\r
+                       \r
+                       // We could be tricky and use the first row in each set of multiple as the temporary storage,\r
+                       // instead of allocating this separate array.\r
+                       sbyte[] row = new sbyte[outputWidth];\r
+                       \r
+                       // 1. Write the white lines at the top\r
+                       for (int y = 0; y < topPadding; y++)\r
+                       {\r
+                               setRowColor(outputArray[y], (sbyte) SupportClass.Identity(255));\r
+                       }\r
+                       \r
+                       // 2. Expand the QR image to the multiple\r
+                       sbyte[][] inputArray = input.Array;\r
+                       for (int y = 0; y < inputHeight; y++)\r
+                       {\r
+                               // a. Write the white pixels at the left of each row\r
+                               for (int x = 0; x < leftPadding; x++)\r
+                               {\r
+                                       row[x] = (sbyte) SupportClass.Identity(255);\r
+                               }\r
+                               \r
+                               // b. Write the contents of this row of the barcode\r
+                               int offset = leftPadding;\r
+                               for (int x = 0; x < inputWidth; x++)\r
+                               {\r
+                    // Redivivus.in Java to c# Porting update - Type cased sbyte\r
+                    // 30/01/2010 \r
+                                       // sbyte value_Renamed = (inputArray[y][x] == 1)?0:(sbyte) SupportClass.Identity(255);\r
+                    sbyte value_Renamed = (sbyte)((inputArray[y][x] == 1) ? 0 : SupportClass.Identity(255));\r
+                                       for (int z = 0; z < multiple; z++)\r
+                                       {\r
+                                               row[offset + z] = value_Renamed;\r
+                                       }\r
+                                       offset += multiple;\r
+                               }\r
+                               \r
+                               // c. Write the white pixels at the right of each row\r
+                               offset = leftPadding + (inputWidth * multiple);\r
+                               for (int x = offset; x < outputWidth; x++)\r
+                               {\r
+                                       row[x] = (sbyte) SupportClass.Identity(255);\r
+                               }\r
+                               \r
+                               // d. Write the completed row multiple times\r
+                               offset = topPadding + (y * multiple);\r
+                               for (int z = 0; z < multiple; z++)\r
+                               {\r
+                                       Array.Copy(row, 0, outputArray[offset + z], 0, outputWidth);\r
+                               }\r
+                       }\r
+                       \r
+                       // 3. Write the white lines at the bottom\r
+                       int offset2 = topPadding + (inputHeight * multiple);\r
+                       for (int y = offset2; y < outputHeight; y++)\r
+                       {\r
+                               setRowColor(outputArray[y], (sbyte) SupportClass.Identity(255));\r
+                       }\r
+                       \r
+                       return output;\r
+               }\r
+               \r
+               private static void  setRowColor(sbyte[] row, sbyte value_Renamed)\r
+               {\r
+                       for (int x = 0; x < row.Length; x++)\r
+                       {\r
+                               row[x] = value_Renamed;\r
+                       }\r
+               }\r
+       }\r
+}
\ No newline at end of file