Skip to content

Commit

Permalink
Merge 70949fb into c8c702f
Browse files Browse the repository at this point in the history
  • Loading branch information
selaux committed Aug 24, 2017
2 parents c8c702f + 70949fb commit f7ad9c7
Show file tree
Hide file tree
Showing 68 changed files with 1,371 additions and 1,179 deletions.
5 changes: 3 additions & 2 deletions .eslintrc
@@ -1,15 +1,16 @@
{
"extends": "eslint:recommended",
"env": {
"node": true
"node": true,
"es6": true
},
"rules": {
"quotes": [2, "single"],
"no-warning-comments": [2, { "terms": ["todo", "fixme", "wtf"], "location": "anywhere" }],
"no-process-exit": 2,
"eqeqeq": 2,
"no-delete-var": 2,
"one-var": 2,
"one-var": [ 2, { "var": "always", "let": "never", "const": "never" } ],
"no-plusplus": 2,
"semi": [2, "always"],
"no-mixed-requires": 0,
Expand Down
7 changes: 4 additions & 3 deletions .travis.yml
Expand Up @@ -4,14 +4,12 @@ node_js:
- "node"
- "6"
- "6.0"
- "iojs"
- "5"
- "5.0"
- "4"
- "4.0"
- "0.12"
- "0.10"
addons:
firefox: "latest-esr"
apt:
sources:
- ubuntu-toolchain-r-test
Expand All @@ -25,6 +23,9 @@ addons:
- build-essential
- gcc-4.8
- g++-4.8
before_script:
- export DISPLAY=:99.0
- sh -e /etc/init.d/xvfb start
before_install:
- "export CXX=\"g++-4.8\" CC=\"gcc-4.8\""
after_success:
Expand Down
61 changes: 61 additions & 0 deletions karma.conf.js
@@ -0,0 +1,61 @@
'use strict';

var path = require('path');

module.exports = function(config) {
var grep = config.grep,
cfg = {
frameworks: ['mocha'],
preprocessors: {},
webpack: {
devtool: 'inline-source-map',
module: {
loaders: [
{
test: /\/stylesheet\/.*\.js$/,
exclude: /(node_modules|bower_components)/,
loader: 'babel-loader',
query: {
plugins: ['static-fs']
}
},
{
test: /\/test\/functional\/browser\.js$/,
exclude: /(node_modules|bower_components)/,
loader: 'babel-loader',
query: {
plugins: ['static-fs']
}
},
{
test: /jimp\/browser\/lib\/jimp\.js$/,
loader: 'exports-loader',
options: {
Jimp: true
}
}
]
},
resolve: {
alias: {
proxyquire: path.resolve(__dirname, './lib/browser/function.js'),
canvas: path.resolve(__dirname, './lib/browser/canvasFacade.js'),
mkdirp: path.resolve(__dirname, './lib/browser/function.js'),
gm: path.resolve(__dirname, './lib/browser/function.js'),
jimp: path.resolve(__dirname, './node_modules/jimp/browser/lib/jimp.js')
}
}
},
plugins: [
'karma-webpack',
'karma-mocha',
'karma-sourcemap-loader',
'karma-firefox-launcher'
]
};

cfg.files = [grep];
cfg.preprocessors[grep] = ['webpack', 'sourcemap'];

config.set(cfg);
};
3 changes: 3 additions & 0 deletions lib/browser/canvasFacade.js
@@ -0,0 +1,3 @@
'use strict';

module.exports = function () {};
14 changes: 14 additions & 0 deletions lib/browser/function.js
@@ -0,0 +1,14 @@
'use strict';

var message = 'You are doing something that is not supported in the browser';

module.exports = function rejectWithNotInBrowser() {
var callback = Array.prototype.slice.call(arguments).find(function (f) {
return typeof f === 'function';
});
if (callback) {
return callback(new Error(message));
} else {
throw new Error(message);
}
};
3 changes: 3 additions & 0 deletions lib/browser/nothing.js
@@ -0,0 +1,3 @@
'use strict';

module.exports = {};
52 changes: 52 additions & 0 deletions lib/compositor/browserCanvas.js
@@ -0,0 +1,52 @@
'use strict';

var Promise = require('bluebird'),
_ = require('underscore');

module.exports = function createCanvasCompositor(window) {
function readImage(rawImg) {
const image = new window.Image();

return new Promise(function (resolve, reject) {
image.onload = function () {
resolve({
path: rawImg.path,
width: image.width,
height: image.height,
data: image
});
};
image.onerror = function (e) {
reject(e);
};
image.src = window.URL.createObjectURL(new window.Blob([rawImg.data]));
});
}

function renderSprite(layout) {
const canvasEl = window.document.createElement('canvas');

canvasEl.width = layout.width;
canvasEl.height = layout.height;

const ctx = canvasEl.getContext('2d');

// render images to canvas
_(layout.images).each(function (image) {
ctx.drawImage(image.data, image.x, image.y, image.width, image.height);
});

return Promise.fromCallback(function (callback) {
canvasEl.toBlob((blob) => {
const reader = new window.FileReader();
reader.onloadend = () => callback(null, new Uint8Array(reader.result));
reader.readAsArrayBuffer(blob);
});
});
}

return {
readImage: readImage,
render: renderSprite
};
};
98 changes: 49 additions & 49 deletions lib/compositor/canvas.js
@@ -1,53 +1,53 @@
'use strict';

var Promise = require('bluebird'),
_ = require('underscore'),
Canvas = require('canvas'),
Image = Canvas.Image;

function readImage(rawImg) {
var image = new Image();

image.src = rawImg.data;

return Promise.resolve({
path: rawImg.path,
width: image.width,
height: image.height,
data: image
});
}

function filterToParam(filter, canvasInstance) {
var filterMap = {
none: canvasInstance.PNG_FILTER_NONE,
sub: canvasInstance.PNG_FILTER_SUB,
up: canvasInstance.PNG_FILTER_UP,
average: canvasInstance.PNG_FILTER_AVG,
paeth: canvasInstance.PNG_FILTER_PAETH,
all: canvasInstance.PNG_ALL_FILTERS
};

return filterMap[filter];
}

function renderSprite(layout, spritePath, options) {
var canvas = new Canvas(layout.width, layout.height),
ctx = canvas.getContext('2d');

// render images to canvas
_(layout.images).each(function (image) {
ctx.drawImage(image.data, image.x, image.y, image.width, image.height);
});

// store canvas to file, we need to use this new promise because of the weird signature of toBuffer
return Promise.fromCallback(function (promiseCallback) {
return canvas.toBuffer(promiseCallback, options.compressionLevel, filterToParam(options.filter, canvas));
});
}

module.exports = {
readImage: readImage,
render: renderSprite,
filterToParam: filterToParam
_ = require('underscore');

module.exports = function createCanvasCompositor(Canvas) {
function readImage(rawImg) {
var image = new Canvas.Image();

image.src = rawImg.data;

return Promise.resolve({
path: rawImg.path,
width: image.width,
height: image.height,
data: image
});
}

function filterToParam(filter, canvasInstance) {
var filterMap = {
none: canvasInstance.PNG_FILTER_NONE,
sub: canvasInstance.PNG_FILTER_SUB,
up: canvasInstance.PNG_FILTER_UP,
average: canvasInstance.PNG_FILTER_AVG,
paeth: canvasInstance.PNG_FILTER_PAETH,
all: canvasInstance.PNG_ALL_FILTERS
};

return filterMap[filter];
}

function renderSprite(layout, spritePath, options) {
var canvas = new Canvas(layout.width, layout.height),
ctx = canvas.getContext('2d');

// render images to canvas
_(layout.images).each(function (image) {
ctx.drawImage(image.data, image.x, image.y, image.width, image.height);
});

// store canvas to file, we need to use this new promise because of the weird signature of toBuffer
return Promise.fromCallback(function (promiseCallback) {
return canvas.toBuffer(promiseCallback, options.compressionLevel, filterToParam(options.filter, canvas));
});
}

return {
readImage: readImage,
render: renderSprite,
filterToParam: filterToParam
};
};
68 changes: 34 additions & 34 deletions lib/compositor/gm.js
Expand Up @@ -3,8 +3,6 @@
var Promise = require('bluebird'),
_ = require('underscore'),
path = require('path'),
gm = require('gm'),

filterMap = {
none: 0,
sub: 1,
Expand All @@ -14,40 +12,42 @@ var Promise = require('bluebird'),
all: 9
};

function readImage(rawImage) {
var gmObject = gm(rawImage.data),
getSize = Promise.promisify(gmObject.size, { context: gmObject });

return getSize().then(function (size) {
return {
path: rawImage.path,
width: size.width,
height: size.height,
data: rawImage.data
};
});
}

function filterToQuality(filter) {
return filterMap[filter];
}

function renderSprite(layout, filePath, options) {
return Promise.fromCallback(function (callback) {
var img = gm(layout.width, layout.height, '#FFFFFFFF');

_(layout.images).each(function (image) {
img.in('-geometry', image.width + 'x' + image.height).in('-page', '+' + image.x + '+' + image.y).in(path.resolve(image.path));
module.exports = function createGmCompositor(gm) {
function readImage(rawImage) {
var gmObject = gm(rawImage.data),
getSize = Promise.promisify(gmObject.size, { context: gmObject });

return getSize().then(function (size) {
return {
path: rawImage.path,
width: size.width,
height: size.height,
data: rawImage.data
};
});
}

function filterToQuality(filter) {
return filterMap[filter];
}

function renderSprite(layout, filePath, options) {
return Promise.fromCallback(function (callback) {
var img = gm(layout.width, layout.height, '#FFFFFFFF');

img.mosaic().quality(options.compressionLevel * 10 + filterToQuality(options.filter));
_(layout.images).each(function (image) {
img.in('-geometry', image.width + 'x' + image.height).in('-page', '+' + image.x + '+' + image.y).in(path.resolve(image.path));
});

img.toBuffer('PNG', callback);
});
}
img.mosaic().quality(options.compressionLevel * 10 + filterToQuality(options.filter));

module.exports = {
readImage: readImage,
render: renderSprite,
filterToQuality: filterToQuality
img.toBuffer('PNG', callback);
});
}

return {
readImage: readImage,
render: renderSprite,
filterToQuality: filterToQuality
};
};
6 changes: 3 additions & 3 deletions lib/compositor/index.js
@@ -1,16 +1,16 @@
'use strict';

module.exports = {
jimp: require('./jimp')
jimp: require('./jimp')(require('jimp'))
};

try {
module.exports.canvas = require('./canvas');
module.exports.canvas = require('./canvas')(require('canvas'));
} catch (e) {
/*eslint no-empty: 0*/
}
try {
module.exports.gm = require('./gm');
module.exports.gm = require('./gm')(require('gm'));
} catch (e) {
/*eslint no-empty: 0*/
}

0 comments on commit f7ad9c7

Please sign in to comment.