2013-09-22 23:02:43 +04:00
|
|
|
(function () {
|
|
|
|
var ns = $.namespace('pskl.utils');
|
2013-11-01 14:17:50 +04:00
|
|
|
var colorCache = {};
|
2013-09-22 23:02:43 +04:00
|
|
|
ns.FrameUtils = {
|
|
|
|
merge : function (frames) {
|
2013-09-26 09:47:11 +04:00
|
|
|
var merged = null;
|
|
|
|
if (frames.length) {
|
|
|
|
merged = frames[0].clone();
|
|
|
|
var w = merged.getWidth(), h = merged.getHeight();
|
|
|
|
for (var i = 1 ; i < frames.length ; i++) {
|
|
|
|
pskl.utils.FrameUtils.mergeFrames_(merged, frames[i]);
|
|
|
|
}
|
2013-09-22 23:02:43 +04:00
|
|
|
}
|
|
|
|
return merged;
|
|
|
|
},
|
|
|
|
|
|
|
|
mergeFrames_ : function (frameA, frameB) {
|
|
|
|
frameB.forEachPixel(function (p, col, row) {
|
|
|
|
if (p != Constants.TRANSPARENT_COLOR) {
|
|
|
|
frameA.setPixel(col, row, p);
|
|
|
|
}
|
|
|
|
});
|
2013-10-24 01:34:09 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2013-11-01 14:17:50 +04:00
|
|
|
* Alpha compositing using porter duff algorithm :
|
|
|
|
* http://en.wikipedia.org/wiki/Alpha_compositing
|
|
|
|
* http://keithp.com/~keithp/porterduff/p253-porter.pdf
|
|
|
|
* @param {String} strColor1 color over
|
|
|
|
* @param {String} strColor2 color under
|
|
|
|
* @return {String} the composite color
|
|
|
|
*/
|
|
|
|
mergePixels : function (strColor1, strColor2, globalOpacity1) {
|
|
|
|
var col1 = pskl.utils.FrameUtils.toRgba(strColor1);
|
|
|
|
var col2 = pskl.utils.FrameUtils.toRgba(strColor2);
|
|
|
|
if (typeof globalOpacity1 == 'number') {
|
|
|
|
col1 = JSON.parse(JSON.stringify(col1));
|
|
|
|
col1.a = globalOpacity1 * col1.a;
|
|
|
|
}
|
|
|
|
var a = col1.a + col2.a * (1 - col1.a);
|
|
|
|
|
|
|
|
var r = ((col1.r * col1.a + col2.r * col2.a * (1 - col1.a)) / a)|0;
|
|
|
|
var g = ((col1.g * col1.a + col2.g * col2.a * (1 - col1.a)) / a)|0;
|
|
|
|
var b = ((col1.b * col1.a + col2.b * col2.a * (1 - col1.a)) / a)|0;
|
|
|
|
|
|
|
|
return 'rgba('+r+','+g+','+b+','+a+')';
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert a color defined as a string (hex, rgba, rgb, 'TRANSPARENT') to an Object with r,g,b,a properties.
|
|
|
|
* r, g and b are integers between 0 and 255, a is a float between 0 and 1
|
|
|
|
* @param {String} c color as a string
|
|
|
|
* @return {Object} {r:Number,g:Number,b:Number,a:Number}
|
|
|
|
*/
|
|
|
|
toRgba : function (c) {
|
|
|
|
if (colorCache[c]) {
|
|
|
|
return colorCache[c];
|
|
|
|
}
|
|
|
|
var color, matches;
|
|
|
|
if (c === 'TRANSPARENT') {
|
|
|
|
color = {
|
|
|
|
r : 0,
|
|
|
|
g : 0,
|
|
|
|
b : 0,
|
|
|
|
a : 0
|
|
|
|
};
|
|
|
|
} else if (c.indexOf('rgba(') != -1) {
|
|
|
|
matches = /rgba\((\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*,\s*(1|0\.\d+)\s*\)/.exec(c);
|
|
|
|
color = {
|
|
|
|
r : parseInt(matches[1],10),
|
|
|
|
g : parseInt(matches[2],10),
|
|
|
|
b : parseInt(matches[3],10),
|
|
|
|
a : parseFloat(matches[4])
|
|
|
|
};
|
|
|
|
} else if (c.indexOf('rgb(') != -1) {
|
|
|
|
matches = /rgb\((\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*\)/.exec(c);
|
|
|
|
color = {
|
|
|
|
r : parseInt(matches[1],10),
|
|
|
|
g : parseInt(matches[2],10),
|
|
|
|
b : parseInt(matches[3],10),
|
|
|
|
a : 1
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
matches = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(c);
|
|
|
|
color = {
|
|
|
|
r : parseInt(matches[1], 16),
|
|
|
|
g : parseInt(matches[2], 16),
|
|
|
|
b : parseInt(matches[3], 16),
|
|
|
|
a : 1
|
|
|
|
};
|
|
|
|
}
|
|
|
|
colorCache[c] = color;
|
|
|
|
return color;
|
2013-11-02 02:37:09 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
/*
|
2013-10-24 01:34:09 +04:00
|
|
|
* Create a pskl.model.Frame from an Image object.
|
|
|
|
* Transparent pixels will either be converted to completely opaque or completely transparent pixels.
|
|
|
|
* @param {Image} image source image
|
|
|
|
* @return {pskl.model.Frame} corresponding frame
|
|
|
|
*/
|
|
|
|
createFromImage : function (image) {
|
|
|
|
var w = image.width,
|
|
|
|
h = image.height;
|
|
|
|
var canvas = pskl.CanvasUtils.createCanvas(w, h);
|
|
|
|
var context = canvas.getContext('2d');
|
|
|
|
|
|
|
|
context.drawImage(image, 0,0,w,h,0,0,w,h);
|
|
|
|
var imgData = context.getImageData(0,0,w,h).data;
|
2013-11-14 02:51:27 +04:00
|
|
|
return pskl.utils.FrameUtils.createFromImageData(imgData, w, h);
|
fix : reduce piskel model size
- Initial implementation : working but ...
- MODEL_VERSION has been bumped to 2
- The loading process is now theoretically asynchronous (loading images to
read the content of the layers), but for now, the asynchronous behaviour
is hidden behind a nasty hack, which is somehow similar to lazy loading.
When loading the piskel, a Piskel is created synchronously, with fake
empty frames, and as the images will get loaded, the fake frames will be
replaced by the actual frames.
I really don't like this, and the asynchronous nature of the loading
should be clearly expressed
- There is no backward compatible deserializer for the previous version of
the model (1)
- The Serializer utils is just badly designed. Serialization and
deserialization should be splitted into two different classes
- Saving & loading are still done in app.js and should be moved to
services
BUT : the size of the piskels is now pretty small. A piskel which was
using 890kB previously is now using only 10kB. Although it should be
noted, that after gzip there is no significant difference between this
version and the existing one. The only gains we can really expect with
this are : less disk space used on appengine, ability to reuse the
layers' pngs directly on piskel-website (but to be honest I can't see any
valid use case for this)
2013-11-08 03:44:24 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
createFromImageData : function (imageData, width, height) {
|
2013-10-24 01:34:09 +04:00
|
|
|
// Draw the zoomed-up pixels to a different canvas context
|
2013-11-15 02:03:29 +04:00
|
|
|
var grid = [];
|
fix : reduce piskel model size
- Initial implementation : working but ...
- MODEL_VERSION has been bumped to 2
- The loading process is now theoretically asynchronous (loading images to
read the content of the layers), but for now, the asynchronous behaviour
is hidden behind a nasty hack, which is somehow similar to lazy loading.
When loading the piskel, a Piskel is created synchronously, with fake
empty frames, and as the images will get loaded, the fake frames will be
replaced by the actual frames.
I really don't like this, and the asynchronous nature of the loading
should be clearly expressed
- There is no backward compatible deserializer for the previous version of
the model (1)
- The Serializer utils is just badly designed. Serialization and
deserialization should be splitted into two different classes
- Saving & loading are still done in app.js and should be moved to
services
BUT : the size of the piskels is now pretty small. A piskel which was
using 890kB previously is now using only 10kB. Although it should be
noted, that after gzip there is no significant difference between this
version and the existing one. The only gains we can really expect with
this are : less disk space used on appengine, ability to reuse the
layers' pngs directly on piskel-website (but to be honest I can't see any
valid use case for this)
2013-11-08 03:44:24 +04:00
|
|
|
for (var x = 0 ; x < width ; x++){
|
2013-11-15 02:03:29 +04:00
|
|
|
grid[x] = [];
|
fix : reduce piskel model size
- Initial implementation : working but ...
- MODEL_VERSION has been bumped to 2
- The loading process is now theoretically asynchronous (loading images to
read the content of the layers), but for now, the asynchronous behaviour
is hidden behind a nasty hack, which is somehow similar to lazy loading.
When loading the piskel, a Piskel is created synchronously, with fake
empty frames, and as the images will get loaded, the fake frames will be
replaced by the actual frames.
I really don't like this, and the asynchronous nature of the loading
should be clearly expressed
- There is no backward compatible deserializer for the previous version of
the model (1)
- The Serializer utils is just badly designed. Serialization and
deserialization should be splitted into two different classes
- Saving & loading are still done in app.js and should be moved to
services
BUT : the size of the piskels is now pretty small. A piskel which was
using 890kB previously is now using only 10kB. Although it should be
noted, that after gzip there is no significant difference between this
version and the existing one. The only gains we can really expect with
this are : less disk space used on appengine, ability to reuse the
layers' pngs directly on piskel-website (but to be honest I can't see any
valid use case for this)
2013-11-08 03:44:24 +04:00
|
|
|
for (var y = 0 ; y < height ; y++){
|
2013-10-24 01:34:09 +04:00
|
|
|
// Find the starting index in the one-dimensional image data
|
fix : reduce piskel model size
- Initial implementation : working but ...
- MODEL_VERSION has been bumped to 2
- The loading process is now theoretically asynchronous (loading images to
read the content of the layers), but for now, the asynchronous behaviour
is hidden behind a nasty hack, which is somehow similar to lazy loading.
When loading the piskel, a Piskel is created synchronously, with fake
empty frames, and as the images will get loaded, the fake frames will be
replaced by the actual frames.
I really don't like this, and the asynchronous nature of the loading
should be clearly expressed
- There is no backward compatible deserializer for the previous version of
the model (1)
- The Serializer utils is just badly designed. Serialization and
deserialization should be splitted into two different classes
- Saving & loading are still done in app.js and should be moved to
services
BUT : the size of the piskels is now pretty small. A piskel which was
using 890kB previously is now using only 10kB. Although it should be
noted, that after gzip there is no significant difference between this
version and the existing one. The only gains we can really expect with
this are : less disk space used on appengine, ability to reuse the
layers' pngs directly on piskel-website (but to be honest I can't see any
valid use case for this)
2013-11-08 03:44:24 +04:00
|
|
|
var i = (y * width + x)*4;
|
|
|
|
var r = imageData[i ];
|
|
|
|
var g = imageData[i+1];
|
|
|
|
var b = imageData[i+2];
|
|
|
|
var a = imageData[i+3];
|
2013-10-24 01:34:09 +04:00
|
|
|
if (a < 125) {
|
2013-11-15 02:03:29 +04:00
|
|
|
grid[x][y] = Constants.TRANSPARENT_COLOR;
|
2013-10-24 01:34:09 +04:00
|
|
|
} else {
|
2013-11-15 02:03:29 +04:00
|
|
|
grid[x][y] = pskl.utils.FrameUtils.rgbToHex(r,g,b);
|
2013-10-24 01:34:09 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-11-15 02:03:29 +04:00
|
|
|
return pskl.model.Frame.fromPixelGrid(grid);
|
2013-10-24 01:34:09 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert a rgb(Number, Number, Number) color to hexadecimal representation
|
|
|
|
* @param {Number} r red value, between 0 and 255
|
|
|
|
* @param {Number} g green value, between 0 and 255
|
|
|
|
* @param {Number} b blue value, between 0 and 255
|
|
|
|
* @return {String} hex representation of the color '#ABCDEF'
|
|
|
|
*/
|
|
|
|
rgbToHex : function (r, g, b) {
|
|
|
|
return "#" + this.componentToHex(r) + this.componentToHex(g) + this.componentToHex(b);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert a color component (as a Number between 0 and 255) to its string hexa representation
|
|
|
|
* @param {Number} c component value, between 0 and 255
|
|
|
|
* @return {String} eg. '0A'
|
|
|
|
*/
|
|
|
|
componentToHex : function (c) {
|
|
|
|
var hex = c.toString(16);
|
|
|
|
return hex.length == 1 ? "0" + hex : hex;
|
2013-09-22 23:02:43 +04:00
|
|
|
}
|
|
|
|
};
|
2013-10-24 01:34:09 +04:00
|
|
|
})();
|