-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlowres.js
More file actions
103 lines (80 loc) · 3.08 KB
/
lowres.js
File metadata and controls
103 lines (80 loc) · 3.08 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
/**
* Low Resolution Algorithm
*
* Averages out the pixel colors in an nxn matrix
*
* @author Alok Swamy
*/
const Jimp = require('jimp');
/*
* size of window of pixels to apply average
*/
const COMPARISON_MATRIX_SIZE = 10;
/*
* Main code starts here
*/
async function main() {
if(!process.env.INPUT_FILE_PATH) {
console.error('Set environment variable INPUT_FILE_PATH before running the code');
process.exit(-1);
}
const file = Jimp.read(process.env.INPUT_FILE_PATH);
const image = (await file);
const imageMatrix = Array.from(Array(image.bitmap.height), () => new Array(image.bitmap.width));
// Initialize all rbg values into a 2D Array
for(let y = 0; y < image.bitmap.height; y++) {
for(let x = 0; x < image.bitmap.width; x++) {
const { r, g, b } = Jimp.intToRGBA(image.getPixelColor(x, y));
imageMatrix[y][x] = { r, g, b };
}
}
for(let y = 0; y < image.bitmap.height; y+=COMPARISON_MATRIX_SIZE) {
for(let x = 0; x < image.bitmap.width; x+=COMPARISON_MATRIX_SIZE) {
let imageMatrixSum = { r: 0, g: 0, b: 0 };
let cellCount = 0;
// Go through every cell in the chosen window (COMPARISON_MATRIX_SIZE x COMPARISON_MATRIX_SIZE) and average the RGB values
for(let comparisonMatrixY = 0; comparisonMatrixY < COMPARISON_MATRIX_SIZE; comparisonMatrixY++) {
for(let comparisonMatrixX = 0; comparisonMatrixX < COMPARISON_MATRIX_SIZE; comparisonMatrixX++) {
let indexX = comparisonMatrixX + x;
let indexY = comparisonMatrixY + y;
if(indexX >= imageMatrix[0].length || indexY >= imageMatrix.length) {
continue;
}
cellCount++;
let { r, g, b } = imageMatrix[indexY][indexX];
imageMatrixSum = {
r: imageMatrixSum.r + r,
g: imageMatrixSum.g + g,
b: imageMatrixSum.b + b,
};
}
}
// Fill the cells in the matrix with the average RGB value
for(let comparisonMatrixY = 0; comparisonMatrixY < COMPARISON_MATRIX_SIZE; comparisonMatrixY++) {
for(let comparisonMatrixX = 0; comparisonMatrixX < COMPARISON_MATRIX_SIZE; comparisonMatrixX++) {
let indexX = comparisonMatrixX + x;
let indexY = comparisonMatrixY + y;
if(indexX >= imageMatrix[0].length || indexY >= imageMatrix.length) {
continue;
}
imageMatrix[indexY][indexX] = {
r: Math.round(imageMatrixSum.r / cellCount),
g: Math.round(imageMatrixSum.g / cellCount),
b: Math.round(imageMatrixSum.b / cellCount),
}
}
}
}
}
exportAsImage(image, imageMatrix);
}
async function exportAsImage(jimpImage, pixelMatrix) {
// Set the values from 2D array to image object
for(let y = 0; y < jimpImage.bitmap.height; y++) {
for(let x = 0; x < jimpImage.bitmap.width; x++) {
jimpImage.setPixelColor(Jimp.rgbaToInt(pixelMatrix[y][x].r, pixelMatrix[y][x].g, pixelMatrix[y][x].b, 255), x, y);
}
}
await jimpImage.write(`output-${Date.now()}.${jimpImage.getExtension()}`);
}
main();