-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
executable file
·101 lines (91 loc) · 4.07 KB
/
Program.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
using System;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Processing;
using System.Globalization;
using System.Text;
using System.IO;
namespace ImageBrailleArtGen
{
public class Program
{
public static void Main(string[] args)
{
if(args.Length != 5)
{
System.Console.WriteLine("Usage: image-braille-art-gen <width> <height> <color threshold> <input file> <output file>");
return;
}
int w = int.Parse(args[0], CultureInfo.InvariantCulture);
int h = int.Parse(args[1], CultureInfo.InvariantCulture);
int th = int.Parse(args[2], CultureInfo.InvariantCulture);
using(Image<Rgba32> img = Image.Load<Rgba32>(args[3]))
{
// Resize and Dither the input image
// Dithering greatly increases the final "readability" of the image
img.Mutate(x => x.Resize(w, h).BinaryDither(KnownDitherings.FloydSteinberg));
// Save the generated output text
File.WriteAllText(args[4], GetBraille(img, th));
}
}
/// <summary>
/// Converts a 6 bit value to the corresponding braille character
/// In the unicode standard the braille characters are neatly organized
/// </summary>
private static char Bin2Braille(int x)
{
const int offset = 10240;
return (char)(offset + x);
}
/// <summary>
/// A simple thresholding function
/// True: Pixel is White
/// False: Pixel is Black
/// </summary>
private static bool Threshold(Rgba32 value, int th)
{
return ((int)value.R + value.G + value.B) / 3 >= th;
}
public static string GetBraille(Image<Rgba32> i, int threshold)
{
StringBuilder sb = new StringBuilder();
const int bW = 2, bH = 3; // Each braille character can display a 2x3 grid of black and white pixels
// calculate how many 2x3 blocks are there in their respective image-axis
int nX = (int)MathF.Ceiling(i.Width / (float)bW),
nY = (int)MathF.Ceiling(i.Height / (float)bH);
// run through the entire image
for(int y = 0; y < nY; y++)
{
for(int x = 0; x < nX; x++)
{
/*
* Since the Braille characters are neatly organized in their code point values,
* we can take advantage of that and greatly shorten the code.
* A Dictionary lookup is not required.
*/
int bitfield = 0;
// Scan the image in 2x3 blocks
for(int jx = 0; jx < bW; jx++)
{
for(int jy = 0; jy < bH; jy++)
{
int xidx = x * bW + jx, yidx = y * bH + jy;
if(xidx > 0 && xidx < i.Width && yidx > 0 && yidx < i.Height)
{
Rgba32 pixel = i[xidx, yidx];
if(!Threshold(pixel, threshold))
// Set the bit if the pixel is dark
// jx * bH + jy converts the coordinate inside the 2x3 grid into a linear one
bitfield |= (1 << (jx * bH + jy));
}
}
}
// After having traversed the block, we can finally add the corresponding braille character
sb.Append(Bin2Braille(bitfield));
}
sb.AppendLine(); // After each horizontal image pixel line, we also need a newline in the final text output
}
return sb.ToString();
}
}
}