voxel / voxel-stitch

stitch block textures into a tile map for rect-mip-map
4 stars 1 forks source link

Performance: 25% of time (~7 seconds) spent in getPixels splitPath in createGLTexture #7

Closed deathcap closed 9 years ago

deathcap commented 9 years ago
StitchPlugin.prototype.createGLTexture = function(gl, cb) {
  var atlas = this.atlas;
  var showLevels = this.debug;
  var self = this;

  getPixels(this.atlas.canvas.toDataURL(), function(err, array) {

is too slow, bottlenecked on the regex to parse the data URL to determine the filename "extension" (there isn't one) to infer the file type (not needed):

screen shot 2015-02-05 at 9 44 22 pm

get-pixels could be optimized to not examine the extension if not needed, after all it does accept an optional type parameter (not used here), but I'm not sure why I'm using this roundabout method to get at the canvas pixel data to begin with. get-pixels just writes the data to a canvas! then reads it:

function defaultImage(url, cb) {
  var img = new Image()
  img.onload = function() {
    var canvas = document.createElement('canvas')
    canvas.width = img.width
    canvas.height = img.height
    var context = canvas.getContext('2d')
    context.drawImage(img, 0, 0)
    var pixels = context.getImageData(0, 0, img.width, img.height)
    cb(null, ndarray(new Uint8Array(pixels.data), [img.width, img.height, 4], [4, 4*img.width, 1], 0))
  }
  img.onerror = function(err) {
    cb(err)
  }
  img.src = url
}
deathcap commented 9 years ago

Trying this:

-  getPixels(this.atlas.canvas.toDataURL(), function(err, array) {
-    if (err) return cb(err);
+  var context = atlas.canvas.getContext('2d');
+  var s = this.atlasSize;
+  console.log(atlas.canvas.toDataURL());
+  var pixels = context.getImageData(0, 0, s, s);
+  var array = ndarray(new Uint8Array(pixels.data), [s, s, 4], [4, 4*s, 1], 0);
+
+  getPixels(atlas.canvas.toDataURL(), function(err, array2) {
+    console.log(array)
+    console.log(array2)

but all the textures are wrong:

screen shot 2015-02-05 at 10 19 15 pm

for some reason the ndarray strides differ:

View3duint8 {data: Uint8Array[16777216], _shape0: 2048, _shape1: 2048, _shape2: 4, _stride0: 4…} stitch.js:219 View3duint8 {data: Uint8Array[16777216], _shape0: 2048, _shape1: 2048, _shape2: 4, _stride0: 8192…}

update: found it, get-pixels 2.0.0 to 3.0.0 changed from rows/cols to width/height: https://github.com/scijs/get-pixels/commit/352b7115663fcd0de2edd071ab0e9ef059ecc772 - voxel-stitch was using get-pixels 1.0.1 (≈1.1.3)

deathcap commented 9 years ago

screen shot 2015-02-05 at 10 43 24 pm