-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGenerator.cs
308 lines (267 loc) · 11.6 KB
/
Generator.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AsciiArtGenerator
{
public class Generator
{
private Font _chosenFont;
private bool _fontIsDirty;
private List<CharacterDarknessMap> _characterDarknessMaps;
private int _textColumns;
private int _textRows;
private int _horizResolution;
private int _vertResolution;
private bool _imageReady;
private bool _charactersReady;
private Bitmap _inputBitmap;
private List<char> _characterPool;
public Generator()
{
_characterPool = generateCharacterPool();
_horizResolution = 4;
_vertResolution = 4;
_textColumns = 80;
_textRows = 80;
}
public Generator(List<char> characterPool, int horizResolution, int vertResolution, int textColumns, int textRows)
{
_characterPool = characterPool;
_horizResolution = horizResolution;
_vertResolution = vertResolution;
_textColumns = textColumns;
_textRows = textRows;
}
public bool CharactersReady
{
get { return _charactersReady; }
}
public bool IsReady{
get { return _imageReady && _charactersReady; }
}
public bool ImageIsReady
{
get { return _imageReady; }
}
public void ChooseFont(Font chosenFont)
{
_chosenFont = new Font(chosenFont.FontFamily, 72);
_fontIsDirty = true;
generateCharacterDarknessMaps();
}
public void SetImage(Bitmap incomingBitmap)
{
//TODO some form of error checking
_inputBitmap = incomingBitmap;
_imageReady = true;
}
private List<char> generateCharacterPool()
{
var ret = new List<char>();
//TODO Generate default character list intelligently
// - this is just standard ASCII. We can do more.
for (int i = 32; i < 127; i++)
{
ret.Add((char)i);
}
return ret;
}
private void generateCharacterDarknessMaps()
{
//TODO make this multithreaded
var ret = new List<CharacterDarknessMap>();
Debug.WriteLine("Generating Character Darkness Maps");
//TODO figure out a better way to get the dimensions of this font
Bitmap a = new Bitmap(1000, 1000);
Graphics charG = Graphics.FromImage(a);
Debug.WriteLine("Creating character buffer image...");
Bitmap charBuffer = new Bitmap((int)charG.MeasureString("M", _chosenFont).Width, (int)charG.MeasureString("M", _chosenFont).Height);
Debug.WriteLine("Buffer image is " + charBuffer.Width + "x" + charBuffer.Height);
Debug.WriteLine("Creating character quadrant buffer image...");
Bitmap charQuadBuffer = new Bitmap(charBuffer.Width / _horizResolution, charBuffer.Height / _vertResolution);
Graphics quadG = Graphics.FromImage(charQuadBuffer);
Debug.WriteLine("Character Quadrang buffer image is " + charQuadBuffer.Width + "x" + charQuadBuffer.Height);
charG = Graphics.FromImage(charBuffer);
charG.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
charG.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
charG.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
foreach (char iChar in _characterPool)
{
Debug.WriteLine("Analyzing '" + iChar + "'");
Debug.WriteLine("Painting '" + iChar + "' to buffer image...");
charG.FillRectangle(Brushes.White, 0, 0, charBuffer.Width, charBuffer.Height);
charG.DrawString(iChar.ToString(), _chosenFont, Brushes.Black, 0, 0);
charG.Flush();
CharacterDarknessMap dm = new CharacterDarknessMap { Character = iChar };
byte[] darknessMap = new byte[_vertResolution * _horizResolution];
int d = 0;
for (int y = 0; y < _vertResolution; y++)
{
for (int x = 0; x < _horizResolution; x++)
{
Debug.WriteLine("Analyzing character quadrant " + x + "," + y);
var destRect = new Rectangle(0, 0, charQuadBuffer.Width, charQuadBuffer.Height);
var srcRect = new Rectangle((charBuffer.Width * x) / _horizResolution, (charBuffer.Height * y) / _vertResolution, charQuadBuffer.Width, charQuadBuffer.Height);
quadG.DrawImage(charBuffer, destRect, srcRect, GraphicsUnit.Pixel);
quadG.Flush();
byte quadrantDarkness = charQuadBuffer.GetDarkness();
Debug.WriteLine("Darkness: " + quadrantDarkness);
darknessMap[d] = quadrantDarkness;
d++;
}
}
dm.Darknesses = darknessMap;
ret.Add(dm);
}
Debug.WriteLine("Normalizing Character Darkness Maps");
byte highestOriginal = 0;
foreach (var map in ret)
{
highestOriginal = Math.Max(map.Darknesses.Max(), highestOriginal);
}
Debug.WriteLine("Highest darkness was " + highestOriginal);
decimal stretchFactor = ((decimal)255 / (decimal)highestOriginal);
Debug.WriteLine("Stretching each value by a factor of " + stretchFactor);
foreach (var map in ret)
{
Debug.Write("'" + map.Character + "' -");
for (int i = 0; i < map.Darknesses.Length; i++)
{
map.Darknesses[i] = (byte)(map.Darknesses[i] * stretchFactor);
Debug.Write(" " + map.Darknesses[i].ToString("D3"));
}
Debug.WriteLine("");
}
_characterDarknessMaps = ret;
_charactersReady = true;
}
public String Generate()
{
if (!IsReady)
{
throw new Exception("ASCII Art Generator is not ready!");
//TODO Seriously? That error message sucks.
}
string ret = "";
int sectionWidth = _inputBitmap.Width / _textColumns;
int sectionHeight = _inputBitmap.Height / _textRows;
Bitmap section = new Bitmap(sectionWidth, sectionHeight);
Rectangle sectionFullRect = new Rectangle(0, 0, sectionWidth, sectionHeight);
Graphics g = Graphics.FromImage(section);
for (int y = 0; y < _textRows; y++)
{
for (int x = 0; x < _textColumns; x++)
{
Debug.WriteLine("Analyzing input image section " + x + "," + y);
Rectangle sourceRect = new Rectangle(x * sectionWidth, y * sectionHeight, sectionWidth, sectionHeight);
g.DrawImage(_inputBitmap, sectionFullRect, sourceRect, GraphicsUnit.Pixel);
g.Flush();
ret += getBestCharForSection(section);
}
ret += "\r\n";
}
g.Dispose();
return ret;
}
private char getBestCharForSection(Bitmap section)
{
char ret = new char();
Bitmap quadrant = new Bitmap(section.Width / _horizResolution, section.Height / _vertResolution);
Rectangle fullQuadrant = new Rectangle(0, 0, quadrant.Width, quadrant.Height);
Graphics g = Graphics.FromImage(quadrant);
byte[] sectionDarknessMap = new byte[_horizResolution * _vertResolution];
int d = 0;
Debug.Write("Darkness map:");
for (int y = 0; y < _vertResolution; y++)
{
for (int x = 0; x < _horizResolution; x++)
{
Rectangle srcRect = new Rectangle((section.Width * x) / _horizResolution, (section.Height * y) / _vertResolution, quadrant.Width, quadrant.Height);
g.FillRectangle(Brushes.White, fullQuadrant);
g.DrawImage(section, fullQuadrant, srcRect, GraphicsUnit.Pixel);
g.Flush();
sectionDarknessMap[d] = quadrant.GetDarkness();
Debug.Write(" " + sectionDarknessMap[d]);
d++;
}
}
Debug.WriteLine("");
Debug.Write("Closest character - '");
char leastWrong = ' ';
int leastWrongness = int.MaxValue;
foreach (var charDarknessMap in _characterDarknessMaps)
{
var thisWrongness = mapWrongness(sectionDarknessMap, charDarknessMap.Darknesses);
if (thisWrongness < leastWrongness)
{
leastWrong = charDarknessMap.Character;
leastWrongness = thisWrongness;
}
}
ret = leastWrong;
Debug.WriteLine(ret + "'");
return ret;
}
private int mapWrongness(byte[] map1, byte[] map2)
{
if (map1.Length != map2.Length)
throw new ArgumentException("Byte arrays are not the same length!");
int wrongness = 0;
for (int i = 0; i < map1.Length; i++)
{
wrongness += Math.Abs((int)map1[i] - (int)map2[i]);
}
return wrongness;
}
}
public class CharacterDarknessMap
{
public Char Character;
public byte[] Darknesses;
}
public static class Helpers
{
public static Bitmap ConvertToGrayscale(this Bitmap original)
{
// Credit - http://social.msdn.microsoft.com/Forums/sqlserver/en-US/a9f0cda8-9d1d-4236-a737-2e8061180af8/converting-jpg-images?forum=csharpgeneral
Bitmap newBitmap = new Bitmap(original.Width, original.Height);
Graphics g = Graphics.FromImage(newBitmap);
ColorMatrix bwColorMatrix = new ColorMatrix(
new float[][]
{
new float[] {.3f, .3f, .3f, 0, 0},
new float[] {.59f, .59f, .59f, 0, 0},
new float[] {.11f, .11f, .11f, 0, 0},
new float[] {0,0,0,1,0},
new float[] {0,0,0,0,1},
});
ImageAttributes attrs = new ImageAttributes();
attrs.SetColorMatrix(bwColorMatrix);
g.DrawImage(original, new Rectangle(0, 0, original.Width, original.Height), 0, 0, original.Width, original.Height, GraphicsUnit.Pixel, attrs);
g.Dispose();
return newBitmap;
}
public static byte GetDarkness(this Bitmap bitmap)
{
//TODO this is inefficient
int cumulativeBrightness = 0;
int totalPixels = 0;
for (int y = 0; y < bitmap.Height; y++)
{
for (int x = 0; x < bitmap.Width; x++)
{
// TODO this assumes grayscale, and therefore only bothers to check red value
byte redVal = bitmap.GetPixel(x, y).R;
cumulativeBrightness += redVal;
totalPixels++;
}
}
return (byte)(255 - ((cumulativeBrightness / totalPixels)));
}
}
}