fix log to reflect reality
[zxing.git] / csharp / oned / UPCEANWriter.cs
1 /*\r
2 * Copyright 2009 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 BarcodeFormat = com.google.zxing.BarcodeFormat;\r
18 using Writer = com.google.zxing.Writer;\r
19 using WriterException = com.google.zxing.WriterException;\r
20 using ByteMatrix = com.google.zxing.common.ByteMatrix;\r
21 namespace com.google.zxing.oned\r
22 {\r
23         \r
24         /// <summary> <p>Encapsulates functionality and implementation that is common to UPC and EAN families\r
25         /// of one-dimensional barcodes.</p>\r
26         /// \r
27         /// </summary>\r
28         /// <author>  aripollak@gmail.com (Ari Pollak)\r
29         /// </author>\r
30         /// <author>www.Redivivus.in (suraj.supekar@redivivus.in) - Ported from ZXING Java Source \r
31         /// </author>\r
32         public abstract class UPCEANWriter : Writer\r
33         {\r
34                 \r
35                 public virtual ByteMatrix encode(System.String contents, BarcodeFormat format, int width, int height)\r
36                 {\r
37                         return encode(contents, format, width, height, null);\r
38                 }\r
39                 \r
40                 public virtual ByteMatrix encode(System.String contents, BarcodeFormat format, int width, int height, System.Collections.Hashtable hints)\r
41                 {\r
42                         if (contents == null || contents.Length == 0)\r
43                         {\r
44                                 throw new System.ArgumentException("Found empty contents");\r
45                         }\r
46                         \r
47                         if (width < 0 || height < 0)\r
48                         {\r
49                                 throw new System.ArgumentException("Requested dimensions are too small: " + width + 'x' + height);\r
50                         }\r
51                         \r
52                         sbyte[] code = encode(contents);\r
53                         return renderResult(code, width, height);\r
54                 }\r
55                 \r
56                 /// <returns> a byte array of horizontal pixels (0 = white, 1 = black) \r
57                 /// </returns>\r
58                 private static ByteMatrix renderResult(sbyte[] code, int width, int height)\r
59                 {\r
60                         int inputWidth = code.Length;\r
61                         // Add quiet zone on both sides\r
62                         int fullWidth = inputWidth + (UPCEANReader.START_END_PATTERN.Length << 1);\r
63                         int outputWidth = System.Math.Max(width, fullWidth);\r
64                         int outputHeight = System.Math.Max(1, height);\r
65                         \r
66                         int multiple = outputWidth / fullWidth;\r
67                         int leftPadding = (outputWidth - (inputWidth * multiple)) / 2;\r
68                         \r
69                         ByteMatrix output = new ByteMatrix(outputWidth, outputHeight);\r
70                         sbyte[][] outputArray = output.Array;\r
71                         \r
72                         sbyte[] row = new sbyte[outputWidth];\r
73                         \r
74                         // a. Write the white pixels at the left of each row\r
75                         for (int x = 0; x < leftPadding; x++)\r
76                         {\r
77                                 row[x] = (sbyte) SupportClass.Identity(255);\r
78                         }\r
79                         \r
80                         // b. Write the contents of this row of the barcode\r
81                         int offset = leftPadding;\r
82                         for (int x = 0; x < inputWidth; x++)\r
83                         {\r
84                 // Redivivus.in Java to c# Porting update\r
85                 // 30/01/2010 \r
86                 // type cased 0 with sbyte\r
87                 sbyte value_Renamed = (code[x] == 1) ? (sbyte)0 : (sbyte)SupportClass.Identity(255);\r
88                                 for (int z = 0; z < multiple; z++)\r
89                                 {\r
90                                         row[offset + z] = value_Renamed;\r
91                                 }\r
92                                 offset += multiple;\r
93                         }\r
94                         \r
95                         // c. Write the white pixels at the right of each row\r
96                         offset = leftPadding + (inputWidth * multiple);\r
97                         for (int x = offset; x < outputWidth; x++)\r
98                         {\r
99                                 row[x] = (sbyte) SupportClass.Identity(255);\r
100                         }\r
101                         \r
102                         // d. Write the completed row multiple times\r
103                         for (int z = 0; z < outputHeight; z++)\r
104                         {\r
105                                 Array.Copy(row, 0, outputArray[z], 0, outputWidth);\r
106                         }\r
107                         \r
108                         return output;\r
109                 }\r
110                 \r
111                 \r
112                 /// <summary> Appends the given pattern to the target array starting at pos.\r
113                 /// \r
114                 /// </summary>\r
115                 /// <param name="startColor">starting color - 0 for white, 1 for black\r
116                 /// </param>\r
117                 /// <returns> the number of elements added to target.\r
118                 /// </returns>\r
119                 protected internal static int appendPattern(sbyte[] target, int pos, int[] pattern, int startColor)\r
120                 {\r
121                         if (startColor != 0 && startColor != 1)\r
122                         {\r
123                                 throw new System.ArgumentException("startColor must be either 0 or 1, but got: " + startColor);\r
124                         }\r
125                         \r
126                         sbyte color = (sbyte) startColor;\r
127                         int numAdded = 0;\r
128                         for (int i = 0; i < pattern.Length; i++)\r
129                         {\r
130                                 for (int j = 0; j < pattern[i]; j++)\r
131                                 {\r
132                                         target[pos] = color;\r
133                                         pos += 1;\r
134                                         numAdded += 1;\r
135                                 }\r
136                                 color ^= 1; // flip color after each segment\r
137                         }\r
138                         return numAdded;\r
139                 }\r
140                 \r
141                 /// <returns> a byte array of horizontal pixels (0 = white, 1 = black) \r
142                 /// </returns>\r
143                 public abstract sbyte[] encode(System.String contents);\r
144         }\r
145 }