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