Last active
March 26, 2018 04:43
-
-
Save adityathebe/a870631d6c38629b63bb88e77a689ba3 to your computer and use it in GitHub Desktop.
A function to map rgba value into better accessible pixels
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* A function to map rgba value into better accessible pixels | |
* */ | |
// ========= Actual Pixels ( 2x2 image ) ========== | |
/* | |
let x = [ | |
[1, 2], | |
[3, 4] | |
] | |
*/ | |
// =============== We Get a OneDimensional Array =============== | |
// let z = ['1r', '1g', '1b', '1a', '2r', '2g', '2b', '2a', '3r', '3g', '3b', '3a', '4r', '4g', '4b', '4a'] | |
// ================ We want =================== | |
/* | |
let y = [ | |
[ ['1r', '1g', '1b', '1a'], ['2r', '2g', '2b', '2a'] ], | |
[ ['3r', '3g', '3b', '3a'], ['4r', '4g', '4b', '4a'] ] | |
] | |
*/ | |
function pixelMaper(img_data, height, width) { | |
let pixels = []; | |
let width_step = width * 4; | |
for (let i = 0; i < img_data.length; i += width_step) { | |
let row = img_data.slice(i, i + width_step); | |
let newRow = []; | |
for (let j = 0; j < row.length; j += 4) { | |
let pixel = row.slice(j, j + 4); | |
newRow.push(pixel); | |
} | |
pixels.push(newRow); | |
} | |
return pixels; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment