Last active
August 31, 2020 13:32
-
-
Save stengoes/e9753663bf086bc9b21a97439692c8be to your computer and use it in GitHub Desktop.
Pads an image with a specified amount of zeros on the top, left, bottom and right.
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
function zeropad(image, top, bottom, left, right) { | |
// Pads an image with a specified amount of zeros on the top, left, bottom and right. | |
// Validate inputs | |
const image_size = image.height * image.width * image.depth; | |
if (image.data.length != image_size) { | |
console.log("Error: image dimensions do not match rgba buffer size!"); | |
return null; | |
} | |
if (image.data.constructor !== Uint8ClampedArray && image.data.constructor !== Float32Array) { | |
console.log("Error: image data should be of type Uint8ClampedArray or Float32Array!"); | |
return null; | |
} | |
if(top < 0 || bottom < 0 || left < 0 || right < 0) | |
{ | |
console.log("Error: the padding amounts (top,bottom,left and right) should be non-negative integers!"); | |
return null; | |
} | |
// Compute output dimensions | |
const output_height = top + image.height + bottom; | |
const output_width = left + image.width + right; | |
const output_depth = image.depth; | |
// Init output image | |
const output = { | |
height: output_height, | |
width: output_width, | |
depth: output_depth, | |
data: new image.data.constructor(output_height*output_width*output_depth) // Initialized with zeros | |
} | |
// Copy input image into output | |
let output_idx = top*output.width*output.depth + left*output_depth; | |
let image_idx = 0; | |
for(let y = 0; y < image.height; y++, image_idx += image.width*image.depth, output_idx += output.width*output.depth) | |
output.data.set(image.data.slice(image_idx, image_idx+image.width*image.depth), output_idx) | |
return output; | |
} | |
function zeropad768x768(image) | |
{ | |
// Compute padding amounts | |
const output_height = 768; | |
const output_width = 768; | |
const top = Math.floor((output_height - image.height) / 2); | |
const bottom = output_height - image.height - top; | |
const left = Math.floor((output_width - image.width) / 2); | |
const right = output_width - image.width - left; | |
return zeropad(image, top, bottom, left, right); | |
} | |
// Utility function (only for testing) | |
function show_image(image) | |
{ | |
str = ""; | |
let idx = 0; | |
for(let y = 0; y < image.height; y++) | |
{ | |
for(let x = 0; x < image.width; x++) | |
{ | |
str += "[ "; | |
for(let z = 0; z < image.depth; z++) | |
str += image.data[idx++] + " "; | |
str += "] "; | |
} | |
str += "\n"; | |
} | |
console.log(str); | |
} | |
// Test | |
test_image = { | |
"height": 3, | |
"width": 4, | |
"depth": 4, | |
"data": (new Float32Array(3*4*4)).fill(1) | |
} | |
show_image(test_image); | |
/* should output: | |
[ 1 1 1 1 ] [ 1 1 1 1 ] [ 1 1 1 1 ] [ 1 1 1 1 ] | |
[ 1 1 1 1 ] [ 1 1 1 1 ] [ 1 1 1 1 ] [ 1 1 1 1 ] | |
[ 1 1 1 1 ] [ 1 1 1 1 ] [ 1 1 1 1 ] [ 1 1 1 1 ] | |
*/ | |
output = zeropad(test_image, 1, 2, 3, 4); | |
show_image(output); | |
/* should output: | |
[ 0 0 0 0 ] [ 0 0 0 0 ] [ 0 0 0 0 ] [ 0 0 0 0 ] [ 0 0 0 0 ] [ 0 0 0 0 ] [ 0 0 0 0 ] [ 0 0 0 0 ] [ 0 0 0 0 ] [ 0 0 0 0 ] [ 0 0 0 0 ] | |
[ 0 0 0 0 ] [ 0 0 0 0 ] [ 0 0 0 0 ] [ 1 1 1 1 ] [ 1 1 1 1 ] [ 1 1 1 1 ] [ 1 1 1 1 ] [ 0 0 0 0 ] [ 0 0 0 0 ] [ 0 0 0 0 ] [ 0 0 0 0 ] | |
[ 0 0 0 0 ] [ 0 0 0 0 ] [ 0 0 0 0 ] [ 1 1 1 1 ] [ 1 1 1 1 ] [ 1 1 1 1 ] [ 1 1 1 1 ] [ 0 0 0 0 ] [ 0 0 0 0 ] [ 0 0 0 0 ] [ 0 0 0 0 ] | |
[ 0 0 0 0 ] [ 0 0 0 0 ] [ 0 0 0 0 ] [ 1 1 1 1 ] [ 1 1 1 1 ] [ 1 1 1 1 ] [ 1 1 1 1 ] [ 0 0 0 0 ] [ 0 0 0 0 ] [ 0 0 0 0 ] [ 0 0 0 0 ] | |
[ 0 0 0 0 ] [ 0 0 0 0 ] [ 0 0 0 0 ] [ 0 0 0 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