'use strict' module.exports = createSelectBuffer var createFBO = require('gl-fbo') var pool = require('typedarray-pool') var ndarray = require('ndarray') var cwise = require('cwise') var nextPow2 = require('bit-twiddle').nextPow2 var selectRange = cwise({ args: [ 'array', {'offset': [0,0,1], 'array':0}, {'offset': [0,0,2], 'array':0}, {'offset': [0,0,3], 'array':0}, 'scalar', 'scalar', 'index'], pre: function() { this.closestD2 = 1e8 this.closestX = -1 this.closestY = -1 }, body: function(r, g, b, a, x, y, idx) { if(r < 255 || g < 255 || b < 255 || a < 255) { var dx = x - idx[0] var dy = y - idx[1] var d2 = dx*dx + dy*dy if(d2 < this.closestD2) { this.closestD2 = d2 this.closestX = idx[0] this.closestY = idx[1] } } }, post: function() { return [this.closestX, this.closestY, this.closestD2] } }) function SelectResult(x, y, id, value, distance) { this.coord = [x, y] this.id = id this.value = value this.distance = distance } function SelectBuffer(gl, fbo, buffer) { this.gl = gl this.fbo = fbo this.buffer = buffer this._readTimeout = null var self = this this._readCallback = function() { if(!self.gl) { return } fbo.bind() gl.readPixels(0,0,fbo.shape[0],fbo.shape[1],gl.RGBA,gl.UNSIGNED_BYTE,self.buffer) self._readTimeout = null } } var proto = SelectBuffer.prototype Object.defineProperty(proto, 'shape', { get: function() { if(!this.gl) { return [0,0] } return this.fbo.shape.slice() }, set: function(v) { if(!this.gl) { return } this.fbo.shape = v var c = this.fbo.shape[0] var r = this.fbo.shape[1] if(r*c*4 > this.buffer.length) { pool.free(this.buffer) var buffer = this.buffer = pool.mallocUint8(nextPow2(r*c*4)) for(var i=0; i