Korean translation from Chang Hyun Park
[zxing.git] / csharp / RGBLuminanceSource.cs
1 using com.google.zxing;\r
2 using com.google.zxing.common;\r
3 using System.Drawing.Imaging;\r
4 using System.Drawing;\r
5 using System;\r
6 \r
7 public class RGBLuminanceSource : LuminanceSource\r
8 {\r
9 \r
10     private sbyte[] luminances;\r
11     private bool isRotated = false;\r
12     private bool __isRegionSelect = false;\r
13     private Rectangle __Region;\r
14 \r
15     override public int Height\r
16     {\r
17         get\r
18         {\r
19             if (!isRotated)\r
20                 return __height;\r
21             else\r
22                 return __width;\r
23         }\r
24 \r
25     }\r
26     override public int Width\r
27     {\r
28         get\r
29         {\r
30             if (!isRotated)\r
31                 return __width;\r
32             else\r
33                 return __height;\r
34         }\r
35 \r
36     }\r
37     private int __height;\r
38     private int __width;\r
39 \r
40     public RGBLuminanceSource(byte[] d, int W, int H)\r
41         : base(W, H)\r
42     {\r
43         __width = W;\r
44         __height = H;\r
45         int width = W;\r
46         int height = H;\r
47         // In order to measure pure decoding speed, we convert the entire image to a greyscale array\r
48         // up front, which is the same as the Y channel of the YUVLuminanceSource in the real app.\r
49         luminances = new sbyte[width * height];\r
50         for (int y = 0; y < height; y++)\r
51         {\r
52             int offset = y * width;\r
53             for (int x = 0; x < width; x++)\r
54             {\r
55                 int r = d[offset * 3 + x * 3];\r
56                 int g = d[offset * 3 + x * 3 + 1];\r
57                 int b = d[offset * 3 + x * 3 + 2];\r
58                 if (r == g && g == b)\r
59                 {\r
60                     // Image is already greyscale, so pick any channel.\r
61                     luminances[offset + x] = (sbyte)r;\r
62                 }\r
63                 else\r
64                 {\r
65                     // Calculate luminance cheaply, favoring green.\r
66                     luminances[offset + x] = (sbyte)((r + g + g + b) >> 2);\r
67                 }\r
68             }\r
69         }\r
70     }\r
71     public RGBLuminanceSource(byte[] d, int W, int H,bool Is8Bit)\r
72         : base(W, H)\r
73     {\r
74         __width = W;\r
75         __height = H;\r
76         luminances = new sbyte[W * H];\r
77         Buffer.BlockCopy(d,0, luminances,0, W * H);\r
78     }\r
79     \r
80     public RGBLuminanceSource(byte[] d, int W, int H, bool Is8Bit,Rectangle Region)\r
81         : base(W, H)\r
82     {\r
83         __width = Region.Width;\r
84         __height = Region.Height;\r
85         __Region = Region;\r
86         __isRegionSelect = true;\r
87         //luminances = Red.Imaging.Filters.CropArea(d, W, H, Region);\r
88     }\r
89 \r
90 \r
91     public RGBLuminanceSource(Bitmap d, int W, int H)\r
92         : base(W, H)\r
93     {\r
94         int width = __width = W;\r
95         int height = __height = H;\r
96         // In order to measure pure decoding speed, we convert the entire image to a greyscale array\r
97         // up front, which is the same as the Y channel of the YUVLuminanceSource in the real app.\r
98         luminances = new sbyte[width * height];\r
99         //if (format == PixelFormat.Format8bppIndexed)\r
100         {\r
101             Color c;\r
102             for (int y = 0; y < height; y++)\r
103             {\r
104                 int offset = y * width;\r
105                 for (int x = 0; x < width; x++)\r
106                 {\r
107                     c = d.GetPixel(x, y);\r
108                     luminances[offset + x] = (sbyte)(((int)c.R) << 16 | ((int)c.G) << 8 | ((int)c.B));\r
109                 }\r
110             }\r
111         }\r
112     }\r
113     override public sbyte[] getRow(int y, sbyte[] row)\r
114     {\r
115         if (isRotated == false)\r
116         {\r
117             int width = Width;\r
118             if (row == null || row.Length < width)\r
119             {\r
120                 row = new sbyte[width];\r
121             }\r
122             for (int i = 0; i < width; i++)\r
123                 row[i] = luminances[y * width + i];\r
124             //System.arraycopy(luminances, y * width, row, 0, width);\r
125             return row;\r
126         }\r
127         else\r
128         {\r
129             int width = __width;\r
130             int height = __height;\r
131             if (row == null || row.Length < height)\r
132             {\r
133                 row = new sbyte[height];\r
134             }\r
135             for (int i = 0; i < height; i++)\r
136                 row[i] = luminances[i * width + y];\r
137             //System.arraycopy(luminances, y * width, row, 0, width);\r
138             return row;\r
139         }\r
140     }\r
141     public override sbyte[] Matrix\r
142     {\r
143         get { return luminances; }\r
144     }\r
145 \r
146     public override LuminanceSource crop(int left, int top, int width, int height)\r
147     {\r
148         return base.crop(left, top, width, height);\r
149     }\r
150     public override LuminanceSource rotateCounterClockwise()\r
151     {\r
152         isRotated = true;\r
153         return this;\r
154     }\r
155     public override bool RotateSupported\r
156     {\r
157         get\r
158         {\r
159             return true;\r
160         }\r
161 \r
162     }\r
163 }\r