-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimageProcessing.js
More file actions
242 lines (237 loc) · 9.09 KB
/
imageProcessing.js
File metadata and controls
242 lines (237 loc) · 9.09 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
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
function imageProcessing() {
"use strict";
var RgbColor = function (r, g, b) {
this.red = r;
this.green = g;
this.blue = b;
this.prototype.equals = function (other) {
try {
return other !== 'undefined' &&
other !== null &&
Object.keys(this).length ===
Object.keys(other).length &&
this.red === other.red &&
this.green === other.green &&
this.blue === other.blue;
} catch (err) {
return false;
}
};
this.hashCode = function () {
return this.red * 37 + this.green * 13 + this.blue;
};
this.toArray = function () {
return [this.red, this.green, this.blue];
};
},
/**
*
*/
instantiateArray = function (elementsCount) {
var i = 0, resultArray = [];
for (i = 0; i < elementsCount; i += 1) {
resultArray[i] = 0;
}
return resultArray;
},
/**
* Used in calculating RGB to HSL
* return the min of three integers.
*/
findMin = function (r, g, b) {
if (r < g) {
if (r < b) { return r; }
} else {
if (g < b) { return g; }
}
return b;
},
/**
* Used in calculating RGB to HSV
* return the max of three integers.
*/
findMax = function (r, g, b) {
if (r > g) {
if (r > b) { return r; }
} else {
if (g > b) { return g; }
}
return b;
},
publ = {
RgbColor: RgbColor,
/**
* returns the decimal equivalent of the hexadecimal number of the
* color.
*/
calculateColorNumber: function (red, green, blue) {
return (red * 256 * 256 + green * 256 + blue);
},
/**
* converts HSL to RGB
* @param hslObject object with three properties:
* hue - integer from 0 to 360;
* saturation - integer from 0 to 100;
* lightness - integer from 0 to 100.
* @returns - an object with properties
* R - red - integer from 0 to 255,
* G - green - integer from 0 to 255,
* B - blue - integer from 0 to 255
*/
hsl2rgb: function (hslObject) {
var H = hslObject.H / 360,
S = hslObject.S / 100,
L = hslObject.L / 100,
red,
green,
blue,
temp_1,
temp_2;
function hue_2_RGB(v1, v2, vH) {
if (vH < 0) { vH += 1; }
if (vH > 1) { vH -= 1; }
if ((6 * vH) < 1) { return v1 + (v2 - v1) * 6 * vH; }
if ((2 * vH) < 1) { return v2; }
if ((3 * vH) < 2) {
return v1 + (v2 - v1) * ((2 / 3) - vH) * 6;
}
return v1;
}
if (S === 0) { // HSL from 0 to 1
red = L * 255;
green = L * 255;
blue = L * 255;
} else {
if (L < 0.5) {
temp_2 = L * (1 + S);
} else {
temp_2 = (L + S) - (S * L);
}
temp_1 = 2 * L - temp_2;
red = Math.round(
255 * hue_2_RGB(temp_1, temp_2, H + (1 / 3))
);
green = Math.round(255 * hue_2_RGB(temp_1, temp_2, H));
blue = Math.round(255 * hue_2_RGB(temp_1, temp_2, H - (1 / 3)));
}
return { R: red, G: green, B: blue};
},
// rgb2hsl: function (rgbObject) {
// var min = findMin(rgbObject), max = findMax(rgbObject);
// var hue = 0, saturation = 0, luminace = (min + max) / 2;
// if (min != max) {
// if (L/255 < 0.5) {
//
// }
// }
// return {H: hue, S: saturation, L: luminace};
// },
/**
* Takes the canvas image data and extracts the color info,
* leaving the opacity away.
* @param canvas - the HTML5 canvas element.
*/
extractColorMatrix: function (canvas) {
var imagedata, i = 0, cmIndex = 0, pixels, colorMatrix,
context = canvas.getContext('2d');
imagedata = context.getImageData(0, 0, canvas.width, canvas.height);
colorMatrix = [];
pixels = imagedata.data;
for (i = 0; i < pixels.length; i += 4) {
colorMatrix[cmIndex] = new this.RgbColor(pixels[i],
pixels[i + 1], pixels[i + 2]
);
cmIndex += 1;
}
return colorMatrix;
},
/**
* Calculates the length of a color section by the number of bits
* @param bits - number of bits for all 3 color ingredients.
* @returns integer with the section length or FALSE if the number of
* bits cannot be divided to 3
*/
colorSectionLength: function (bits) {
var bitsPerColor = 0, sectionLength = 0;
if (bits % 3 !== 0 && bits / 3 > 8) {
return false;
}
bitsPerColor = Math.pow(2, bits / 3);
sectionLength = 256 / bitsPerColor;
return Math.round(sectionLength);
},
/**
*
* Maps the color ingredients values of the real 24-bit color to
* central values of a color cluster.
* @param bits - pow(2, bits) gives the number of
* possible values of the new color.
* @param original - the original color; a 3 item array.
* @returns color code near to original
* but equal for a certain amount of colors.
*
* TODO: refactor using RgbColor objects.
*/
requantify: function (bits, original) {
var sectionLength = this.colorSectionLength(bits),
previousColorSection = 0,
requantifiedColor = new RgbColor(),
i = 0,
requantifiedIngredient = 0,
ingrSection = 0,
sectionPosition = Math.round(sectionLength / 2);
for (i = 0; i < 3; i += 1) {
previousColorSection = Math.floor(original[i] / sectionLength);
ingrSection = previousColorSection * sectionLength;
requantifiedIngredient = ingrSection + sectionPosition;
requantifiedColor.push(requantifiedIngredient);
}
return requantifiedColor;
},
/**
*
*/
requantifyImage: function (bits, canvas) {
var matrix = this.extractColorMatrix(canvas),
i,
matrixLength = matrix.length,
originalColor,
requantifiedMatrix = [];
for (i = 0; i < matrixLength; i += 3) {
originalColor = [matrix[i], matrix[i + 1], matrix[i + 2]];
requantifiedMatrix.push(this.requantify(bits, originalColor));
}
return requantifiedMatrix;
},
/**
* Counts the number of occurances for every color in a canvas image.
* @param canvas - the canvas from where the image is extracted.
* @param colorCount - the number of colors in the palette for which
* the histogram will be calculated.
* @return a jsStructures.Map object containing all colors as keys
* and their occurances as values.
*/
calculateHistogram: function (canvas, colorCount) {
var i = 0,
colorResolution = Math.round(
Math.log(colorCount) / Math.log(2)
),
sectionBorders = this.colorSectionLength(colorResolution),
calculatedColor = 0,
requantified = this.requantifyImage(colorResolution, canvas),
reqLength = requantified.length,
histogram = new Hashtable(),
colorHits = 0;
for (i = 0; i < reqLength; i += 1) {
if (histogram.containsKey(requantified[i])) {
colorHits = histogram.get(requantified[i]) + 1;
histogram.put(requantified[i], colorHits);
} else {
histogram.put(requantified[i], 1);
}
}
return histogram;
}
};
return publ;
}