'use strict' var ndarray = require('ndarray') var path = require('path') var PNG = require('pngjs').PNG var jpeg = require('jpeg-js') var pack = require('ndarray-pack') var GifReader = require('omggif').GifReader var Bitmap = require('node-bitmap') var fs = require('fs') var request = require('request') var mime = require('mime-types') var parseDataURI = require('parse-data-uri') function handlePNG(data, cb) { var png = new PNG(); png.parse(data, function(err, img_data) { if(err) { cb(err) return } cb(null, ndarray(new Uint8Array(img_data.data), [img_data.width|0, img_data.height|0, 4], [4, 4*img_data.width|0, 1], 0)) }) } function handleJPEG(data, cb) { var jpegData try { jpegData = jpeg.decode(data) } catch(e) { cb(e) return } if(!jpegData) { cb(new Error("Error decoding jpeg")) return } var nshape = [ jpegData.height, jpegData.width, 4 ] var result = ndarray(jpegData.data, nshape) cb(null, result.transpose(1,0)) } function handleGIF(data, cb) { var reader try { reader = new GifReader(data) } catch(err) { cb(err) return } if(reader.numFrames() > 0) { var nshape = [reader.numFrames(), reader.height, reader.width, 4] try { var ndata = new Uint8Array(nshape[0] * nshape[1] * nshape[2] * nshape[3]) } catch(err) { cb(err) return } var result = ndarray(ndata, nshape) try { for(var i=0; i