forked from khrome/ascii-art
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage.js
308 lines (301 loc) · 11.7 KB
/
image.js
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
(function (root, factory) {
var imgLoadBrowser = function(url, cb){
var img = new root.Image();
img.onload = function(){
cb(undefined, img)
}
img.src = url;
};
var renderersBrowser = function(url, cb){
return [ 'average.js' ];
};
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define(['./renderers/average'], function(ave){
return factory(imgLoadBrowser, renderersBrowser, function(){
return root.document.createElement('canvas');
}, root.Image, function(){
return ave;
});
});
} else if (typeof module === 'object' && module.exports) {
var Canvas = require('canvas');
var Image = Canvas.Image;
var fs = require('fs');
module.exports = factory(function(url, cb){
fs.readFile(url, function(err, data){
if (err) return cb(err);
var image = new Image();
image.src = data;
cb(undefined, image)
; });
}, function(){
var res = fs.readdirSync(__dirname+'/renderers/average');
return res;
}, Canvas, Image, function(){ return require('./renderers/average')});
} else {
// Browser globals (root is window)
root.AsciiArtImage = factory(imgLoadBrowser, renderersBrowser, root.Canvas, root.Image);
}
}(this, function (readImage, getRenderers, Canvas, Image, getAverage) {
var color_profiles = {
"darwin" : {
"black" : [0, 0, 0],
"red" : [194, 54, 33],
"green" : [37, 188, 36],
"yellow" : [173, 173, 39],
"blue" : [73, 46, 225],
"magenta": [211, 56, 211],
"cyan": [51, 187, 200],
"white": [203, 204, 205],
"bright_black": [129, 131, 131],
"bright_red": [252,57,31],
"bright_green": [49, 231, 34],
"bright_yellow": [234, 236, 35],
"bright_blue": [88, 51, 255],
"bright_magenta": [249, 53, 248],
"bright_cyan": [20, 240, 240],
"bright_white": [233, 235, 235]
}
};
var AsciiArt = {};
var parentArt;
AsciiArt.Image = function(options){
if(typeof options == 'string'){
if(options.indexOf('://') !== -1){
options = {
uri : options
}
}else{
options = {
filepath : options
}
}
}
var ob = this;
if(!options.alphabet) options.alphabet = 'ultra-wide';
options.alphabet = parentArt.valueScales[options.alphabet];
this.options = options;
if(!this.options.renderer) this.options.renderer = 'average';
var jobs = [];
this.ready = function(callback){
jobs.push(callback);
};
if(this.uri){
throw new Error('uris not yet implemented!')
return;
}
if(this.options.filepath){
//todo: handle in UMD wrapper.. pass in assetloader?
readImage(this.options.filepath, function(err, image){
if (err) throw err;
ob.image = image;
ob.aspectRatio = ob.image.height/ob.image.width;
if(
(!ob.options.width) &&
(!ob.options.height)
){
ob.options.width = 80;
}
if(ob.options.width){
if(!ob.options.height){
ob.options.height = ob.options.width * ob.aspectRatio;
}
}else{
if(ob.options.height){
ob.options.width = ob.options.height / ob.aspectRatio;
}
}
ob.canvas = new Canvas(ob.image.width, ob.image.height);
ob.context = ob.canvas.getContext('2d');
ob.context.drawImage(
ob.image, 0, 0, ob.image.width, ob.image.height
);
ob.ready = function(cb){ if(cb) cb() };
jobs.forEach(function(job){
if(job) job();
});
jobs = [];
});
}else throw new Error('no filepath provided!');
};
AsciiArt.Image.Canvas = Canvas;
AsciiArt.Image.Image = Image;
AsciiArt.Image.prototype.write = function(location, callback){
if(typeof location === 'function' && !callback){
callback = location;
location = undefined;
}
var ob = this;
this.ready(function(){
if(location && location.indexOf('://') !== -1){
throw new Error("uris not yet implemented!")
}else{
AsciiArt.Image.renderers[ob.options.renderer].render(
ob,
{
imageFromCanvas : function(canvas){
var newImage = new Image();
if(canvas.toBuffer) newImage.src = canvas.toBuffer();
else newImage.src = canvas.toDataURL();
return newImage;
},
canvas : function(width, height){
var canvas = new Canvas(width, height);
return canvas;
}
},
function(err, text){
if(err) return callback(err);
if(location) require('fs').writeFile(location, text, function(err){
return callback(err, text);
});
else callback(err, text);
}
);
}
});
}
AsciiArt.Image.Color = {};
/*AsciiArt.Image.Color.distance = function(r1, g1, b1, r2, g2, b2){
return (Math.abs(r1-r2)+Math.abs(g1-g2)+Math.abs(b1-b2))/3;
}//*/
AsciiArt.Image.Color.distance = function(r1, g1, b1, r2, g2, b2){
return (Math.abs(r1-r2)+Math.abs(g1-g2)+Math.abs(b1-b2)+
//add the value change in as well
0
//Math.abs(Math.max(r1, g1, b1)-Math.max(r2, g2, b2))/2
)/3 + Math.abs(Math.max(r1, g1, b1)-Math.max(r2, g2, b2))/2;
}
AsciiArt.Image.Colors = function(colorList){
this.colors = colorList;
};
AsciiArt.Image.Color.channels = function(value){
//todo: handle, like, any other format
//todo: cache?
return [
parseInt("0x"+value.substring(0,2)),
parseInt("0x"+value.substring(2,4)),
parseInt("0x"+value.substring(4,6))
];
}
AsciiArt.Image.Colors.prototype.average = function(callback){
var total = ob.colors.map(function(color){
return AsciiArt.Image.Color.channels(color);
}).reduce(function(a, b){
return [a[0]+b[0], a[1]+b[1], a[2]+b[2]];
});
var result = [
Math.floor(total[0]/this.colors.length),
Math.floor(total[1]/this.colors.length),
Math.floor(total[2]/this.colors.length),
];
this.colors = result[0].toString(16)+
result[1].toString(16)+
result[2].toString(16);
if(callback) callback();
}
AsciiArt.Image.Colors.prototype.reduceTo = function(count, callback){
var done = function(){ if(callback) callback() };
if(count === 1) this.average(done);
else this.shrink({count: this.colors.length - count}, done);
}
AsciiArt.Image.Colors.prototype.shrink = function(options, callback){
if(options && options.count){
var cache = {};
for(var lcv=0; lcv < options.count || 1; lcv++) this.shrink({
weights : options.weights,
cache : cache
});
if(callback) callback();
return;
}
if(!options.cache) options.cache = {};
//todo: lots of caching
var occurances = options.occurances || {};
var results = this.colors.map(function(thisColor){
var theseChannels = AsciiArt.Image.Color.channels(thisColor);
var minimum = ob.colors.map(function(thatColor){
if(options.cache[thisColor+thatColor]) return options.cache[thisColor+thatColor];
var thoseChannels = AsciiArt.Image.Color.channels(thatColor);
var distance = (options.distance || AsciiArt.Image.Color.distance)(
theseChannels.concat(thoseChannels)
);
var result = {
distance : distance,
color : thatColor
}
options.cache[thisColor+thatColor] = result;
return result;
}).reduce(function(a, b){
if(a.distance < b.distance) return a;
else return b;
});
return {
color : thisColor,
other : minimum.color,
distance : minimum.distance,
occurances : occurances[thisColor]
}
});
var minimumDistance;
results.forEach(function(result){
if( (!minimumDistance) || result.minimumDistance < minimumDistance){
minimumDistance = result.minimumDistance;
}
});
var result = results.filter(function(result){
result.distance == minimumDistance;
}).reduce(function(a, b){
return a.occurances > b.occurances?b:a;
});
var position = this.colors.indexOf(result.color);
if(position === -1) throw new Error('could not find color');
this.colors.splice(position, 1);
}
var closest = function(color, colors, names, options){
var distances = colors.map(function(candidate){
return (options.distance || AsciiArt.Image.Color.distance)(
color[0], color[1], color[2],
candidate[0], candidate[1], candidate[2]
);
});
var position;
var distance;
distances.forEach(function(thisDistance, pos){
if( (!distance) || distance > thisDistance ){
distance = thisDistance;
position = pos;
}
});
return names?names[position]:colors[position];
};
AsciiArt.Image.getTerminalColor = function(r, g, b, options){
var names = Object.keys(AsciiArt.Image.colorProfiles.darwin);
var colors = names.map(function(name){
return AsciiArt.Image.colorProfiles.darwin[name];
});
return closest([r, g, b], colors, names, options);
}
AsciiArt.Image.renderers = {};
AsciiArt.Image.renderers['average'] = getAverage();
//todo: make dynamic load async
/*var dir = getRenderers();
dir.forEach(function(file){
var name = file.substring(0, file.indexOf('.'));
AsciiArt.Image.renderers[name] = require(__dirname+'/renderers/'+file);
});*/
AsciiArt.Image.setInstance = function(art){
Object.keys(AsciiArt.Image.renderers).forEach(function(name){
AsciiArt.Image.renderers[name].setInstance(art);
});
parentArt = art;
}
AsciiArt.Image.colorProfiles = color_profiles;
//todo: AsciiArt.Image.renderers.foregroundBackground
// sample down to two colors by subsample grid, sample posistions
// compare two-color layout to a full ASCII character map for a maximally
// perfect two color-per character layout
AsciiArt.Image.terminalAspectRatioDistortion = 0.7;
return AsciiArt;
}));