blockman
Blockman is a block-based array manipulation tool that works similar to Array.map
and Array.reduce
, but for several elements at once. Written for image processing purposes.
$ npm install blockman$ npm test
API
map = (data, f)
Like Array.map()
, but iterates over blocks of data
. The block size is inferred from the number of arguments supplied to the mapping function. The mapping function should always return an array.
var bm = data = 1 2 3 4 5 6; bm;// [3, 4, 5, 6, 7, 8] bm;// [2, 1, 4, 3, 6, 5] bm;// [3, 2, 1, 6, 5, 4]
expand = (data, f)
Expand blocks of data
. The block size is inferred from the number of arguments supplied to the expanding function. The expanding function should always return an array.
bm;// [-1, 0, 1] bm;// [1, 2, 3, 3, 4, 7, 5, 6, 11] bm;// [1, 2, 3, 6, 4, 5, 6, 120]
reduce = (data, f)
Reduce blocks of data
. The block size is inferred from the number of arguments supplied to the reduction function. The reduction function should always return a Number
.
bm;// [3, 7, 11] bm;// [5, 26]
Usage
You can for example use it to easily reduce an RGBA image into a double precision grayscale bitmap array (each pixel a value from 0
to 1
) and then expand it back into an RBGA array:
var fs = PNG = PNG bm = ; // 1. Read image into a node Buffervar image = PNGsync; // 2. Create a Float64Array view of the image data buffervar f64 = imagedata; // Reduce every 4 elements (R, G, B and alpha respectively).// Resulting array will be 1/4 the size of the original.var f64Gray = bm; // 4. Expand the image back to its original sizef64Gray = bm; // 5. Replace image dataimagedata = f64Gray; // 6. Write new image to filefs;console;console;