-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathBitmapConverter.cs
More file actions
48 lines (43 loc) · 1.46 KB
/
BitmapConverter.cs
File metadata and controls
48 lines (43 loc) · 1.46 KB
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
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AdvancedOCR
{
static class BitmapConverter
{
public static double[] ToDoubles(this Bitmap bitmap)
{
int width = bitmap.Width;
int height= bitmap.Height;
int length = width * height;
double[] result = new double[width * height];
for (int i = 0; i < result.Length; i++)
{
result[i] = 1.0 - (bitmap.GetPixel(i % width, i / width).GetBrightness() * 2.0);
}
return result;
}
public static Bitmap ToBitmap(this double[] doubles, int width)
{
if (width <= 0) throw new ArgumentException();
int length = doubles.Length;
int height = (length + width - 1) / width;
Bitmap result = new Bitmap(width, height, PixelFormat.Format32bppPArgb);
for (int i = 0; i < length; i++)
{
result.SetPixel(i % width, i / width, ToPixel(doubles[i]));
}
return result;
}
private static Color ToPixel(double value)
{
double boundedValue = Math.Min(Math.Max(value + 2, 0), 4);
byte pixelState = (byte)(boundedValue * 255.0 / 4.0);
return Color.FromArgb(255, pixelState, pixelState, pixelState);
}
}
}