Skip to content

Instantly share code, notes, and snippets.

@adityathebe
Last active March 26, 2018 04:43
Show Gist options
  • Save adityathebe/a870631d6c38629b63bb88e77a689ba3 to your computer and use it in GitHub Desktop.
Save adityathebe/a870631d6c38629b63bb88e77a689ba3 to your computer and use it in GitHub Desktop.
A function to map rgba value into better accessible pixels
/*
* 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