Skip to content
This repository has been archived by the owner on May 1, 2020. It is now read-only.

Commit

Permalink
Load structure from image when available
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelerz committed Jun 3, 2015
1 parent 3741b32 commit 88fd7ad
Showing 1 changed file with 88 additions and 1 deletion.
89 changes: 88 additions & 1 deletion lib/configuration/image.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
// Copyright 2015 Yahoo! Inc.
// Copyrights licensed under the Mit License. See the accompanying LICENSE file for terms.

var fs = require('fs');

var Base = require('./base');
var PNGImage = require('pngjs-image');

var Rect = require('./atoms/rect');
var Structure = require('./structure/structure');

/**
* @class Image
* @extends Base
* @module Configuration
*
* @property {PNGImage} _image
* @property {Structure} _structure
* @property {Rect} _crop
*
* @property {boolean} _alreadyCropped
Expand All @@ -24,12 +28,14 @@ var Image = Base.extend(
*
* @param {object} options
* @param {string|PNGImage} options.image Image
* @param {object|Structure} options.structure Structure of the image as a DOM
* @param {object|Rect} [options.crop] Cropping of the image
* @constructor
*/
function (options) {
this.__super();

this.setStructure(options.structure);
this.setImage(options.image);

this.setCropRect(options.crop);
Expand All @@ -55,7 +61,10 @@ var Image = Base.extend(
setImage: function (image) {

if (typeof image == 'string') {
image = PNGImage.readImageSync(image);
image = this._readImage(image);

} else if (typeof image == 'buffer') {
image = this._loadImage(image);
}

if (image instanceof PNGImage) {
Expand All @@ -67,6 +76,84 @@ var Image = Base.extend(
}
},

/**
* Reads the image from the FS
*
* @param {string} path
* @return {PNGImage}
* @private
*/
_readImage: function (path) {
return this._loadImage(fs.readFileSync(path));
},

/**
* Loads the image from a buffer
*
* @param {Buffer} blob
* @return {PNGImage}
* @private
*/
_loadImage: function (blob) {

var decoder,
data,
headerChunk,
structureChunk,
width, height,
image,
Decoder = PNGImage.Decoder;

decoder = new Decoder();
data = decoder.decode(blob, { strict: false });

headerChunk = decoder.getHeaderChunk();
width = headerChunk.getWidth();
height = headerChunk.getHeight();

// Load structure when embedded
if (decoder.hasChunksOfType('stRT')) {
structureChunk = decoder.getFirstChunk('stRT');

if ((structureChunk.getDataType() == 'BLDF') &&
(structureChunk.getMajor() == 1) &&
(structureChunk.getMinor() == 1) &&
structureChunk.getContent())
{
this.setStructure(structureChunk.getContent());
}
}

image = new PNG({
width: width,
height: height
});
data.copy(image.data, 0, 0, data.length);

return new PNGImage(image);
},


/**
* Gets the structure of the image as a DOM
*
* @method getStructure
* @return {Structure}
*/
getStructure: function () {
return this._structure;
},

/**
* Sets the structure of the image as a DOM
*
* @method setStructure
* @return {object|Structure}
*/
setStructure: function (value) {
this._structure = this._parseObject(value, Structure, 'structure');
},


/**
* Is image cropped?
Expand Down

0 comments on commit 88fd7ad

Please sign in to comment.