148 lines
No EOL
3.5 KiB
JavaScript
Executable file
148 lines
No EOL
3.5 KiB
JavaScript
Executable file
'use strict';
|
|
|
|
/*global window:true*/
|
|
/*eslint space-infix-ops:0*/
|
|
|
|
// Feature detect
|
|
var WORKER = (typeof window !== 'undefined') && ('Worker' in window);
|
|
if (WORKER) {
|
|
// IE don't allow to create webworkers from string. We should check it.
|
|
// https://connect.microsoft.com/IE/feedback/details/801810/web-workers-from-blob-urls-in-ie-10-and-11
|
|
try {
|
|
var wkr = require('webworkify')(function () {});
|
|
wkr.terminate();
|
|
} catch (__) {
|
|
WORKER = false;
|
|
}
|
|
}
|
|
|
|
var resize = require('./lib/resize');
|
|
var resizeWorker = require('./lib/resize_worker');
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
// Helpers
|
|
function _class(obj) { return Object.prototype.toString.call(obj); }
|
|
function isFunction(obj) { return _class(obj) === '[object Function]'; }
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
// API methods
|
|
|
|
|
|
// RGBA buffer async resize
|
|
//
|
|
function resizeBuffer(options, callback) {
|
|
var wr;
|
|
|
|
var _opts = {
|
|
src: options.src,
|
|
dest: null,
|
|
width: options.width|0,
|
|
height: options.height|0,
|
|
toWidth: options.toWidth|0,
|
|
toHeight: options.toHeight|0,
|
|
quality: options.quality,
|
|
alpha: options.alpha,
|
|
unsharpAmount: options.unsharpAmount,
|
|
unsharpThreshold: options.unsharpThreshold
|
|
};
|
|
|
|
if (WORKER && exports.WW) {
|
|
wr = require('webworkify')(resizeWorker);
|
|
|
|
wr.onmessage = function(ev) {
|
|
var i, l,
|
|
dest = options.dest,
|
|
output = ev.data.output;
|
|
|
|
// If we got output buffer by reference, we should copy data,
|
|
// because WW returns independent instance
|
|
if (dest) {
|
|
// IE ImageData can return old-style CanvasPixelArray
|
|
// without .set() method. Copy manually for such case.
|
|
if (dest.set) {
|
|
dest.set(output);
|
|
} else {
|
|
for (i = 0, l = output.length; i < l; i++) {
|
|
dest[i] = output[i];
|
|
}
|
|
}
|
|
}
|
|
callback(ev.data.err, output);
|
|
wr.terminate();
|
|
};
|
|
|
|
if (options.transferable) {
|
|
wr.postMessage(_opts, [ options.src.buffer ]);
|
|
} else {
|
|
wr.postMessage(_opts);
|
|
}
|
|
// Expose worker when available, to allow early termination.
|
|
return wr;
|
|
}
|
|
|
|
// Fallback to sync call, if WebWorkers not available
|
|
_opts.dest = options.dest;
|
|
resize(_opts, callback);
|
|
return undefined;
|
|
}
|
|
|
|
|
|
// Canvas async resize
|
|
//
|
|
function resizeCanvas(from, to, options, callback) {
|
|
var w = from.width,
|
|
h = from.height,
|
|
w2 = to.width,
|
|
h2 = to.height;
|
|
|
|
if (isFunction(options)) {
|
|
callback = options;
|
|
options = {};
|
|
}
|
|
|
|
if (!isNaN(options)) {
|
|
options = { quality: options, alpha: false };
|
|
}
|
|
|
|
var ctxTo = to.getContext('2d');
|
|
var imageDataTo = ctxTo.getImageData(0, 0, w2, h2);
|
|
|
|
var _opts = {
|
|
src: from.getContext('2d').getImageData(0, 0, w, h).data,
|
|
dest: imageDataTo.data,
|
|
width: from.width,
|
|
height: from.height,
|
|
toWidth: to.width,
|
|
toHeight: to.height,
|
|
quality: options.quality,
|
|
alpha: options.alpha,
|
|
unsharpAmount: options.unsharpAmount,
|
|
unsharpThreshold: options.unsharpThreshold,
|
|
transferable: true
|
|
};
|
|
|
|
return resizeBuffer(_opts, function (err/*, output*/) {
|
|
if (err) {
|
|
callback(err);
|
|
return;
|
|
}
|
|
|
|
ctxTo.putImageData(imageDataTo, 0, 0);
|
|
callback();
|
|
});
|
|
}
|
|
|
|
|
|
exports.resizeBuffer = resizeBuffer;
|
|
exports.resizeCanvas = resizeCanvas;
|
|
exports.WW = WORKER;
|
|
|
|
|
|
|
|
//////////////////
|
|
// WEBPACK FOOTER
|
|
// ./~/pica/index.js
|
|
// module id = 1204
|
|
// module chunks = 4
|