Skip to content
This repository has been archived by the owner on Jul 15, 2019. It is now read-only.

Commit

Permalink
Completed implementation of basic dynamic encoder
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelerz committed May 8, 2015
1 parent 0f27a52 commit 9e8ae46
Show file tree
Hide file tree
Showing 8 changed files with 115 additions and 176 deletions.
25 changes: 15 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,17 +173,23 @@ PNGImage.loadImage = function (blob, fn) {
* @return {PNGImage}
*/
PNGImage.loadImageSync = function (blob) {
var decoder = new Decoder(blob, false);
var result = decoder.decode();
var headerChunk = decoder.getHeaderChunk();
var width = headerChunk.getWidth();
var height = headerChunk.getHeight();
var decoder,
data,
headerChunk,
width, height;

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

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

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

Expand Down Expand Up @@ -398,10 +404,9 @@ PNGImage.prototype = {
fs.writeFileSync(filename, this.toBlobSync());
},

toBlobSync: function () {
var encoder = new Encoder(this.getWidth(), this.getHeight(), this.getBlob(), 0);

return encoder.encode();
toBlobSync: function (options) {
var encoder = new Encoder();
return encoder.encode(this.getBlob(), this.getWidth(), this.getHeight(), options);
},

/**
Expand Down
34 changes: 21 additions & 13 deletions lib/png/chunk.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,6 @@ Chunk.prototype.getType = function () {
return null;
};

/**
* Gets the chunk-type as id
*
* Note:
* This is the numeric version of `getType`.
*
* @method getTypeId
* @return {int}
*/
Chunk.prototype.getTypeId = function () {
return 0;
};

/**
* Gets the sequence
*
Expand Down Expand Up @@ -394,6 +381,27 @@ Chunk.getChunkType = function (type) {
return this._registry[type];
};

/**
* Gets a list of registered chunk types
*
* @static
* @method getChunkTypes
* @return {string[]}
*/
Chunk.getChunkTypes = function () {

var result = [],
i;

for (i in this._registry) {
if (this._registry.hasOwnProperty(i)) {
result.push(i);
}
}

return result;
};

/**
* Applies the chunk-module on an object
*
Expand Down
3 changes: 2 additions & 1 deletion lib/png/chunks/IHDR.js
Original file line number Diff line number Diff line change
Expand Up @@ -690,7 +690,7 @@ module.exports = {
stream.writeUInt8(this.getBitDepth());
stream.writeUInt8(this.getColorType());
stream.writeUInt8(this.getCompressionMethod());
stream.writeUInt8(this.getFilterMethod);
stream.writeUInt8(this.getFilterMethod());
stream.writeUInt8(this.getInterlaceMethod());
},

Expand All @@ -714,6 +714,7 @@ module.exports = {
lastChunkLength, i;

for (i = 0; i < chunkQuantity; i++) {

chunk = new Chunk('IDAT', this._chunks);

lastChunkLength = Math.min(stream.length, chunkLength);
Expand Down
22 changes: 10 additions & 12 deletions lib/png/decoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,9 @@ var path = require('path');
* @module PNG
* @submodule PNGCore
* @extends chunkUtils
* @param {Buffer} data Data to decode
* @param {int} [offset=0] Offset within data
* @param {int} [length=data.length-offset] Length of data
* @constructor
*/
var Decoder = function (data, offset, length) {
this._stream = new BufferedStream(data, offset, length);

var Decoder = function () {
this._chunks = {};
this._chunkData = {};

Expand All @@ -48,7 +43,8 @@ Decoder.prototype.getChunkData = function () {
*
* @method decode
* @param {Buffer} data Image data
* @param {object} options Decoding options
* @param {object} [options] Decoding options
* @param {boolean} [options.strict=false] Strict decoding
*/
Decoder.prototype.decode = function (data, options) {

Expand All @@ -72,13 +68,13 @@ Decoder.prototype.decode = function (data, options) {
}

// Load all chunks until end is reached
// Phase 1
// Phase 1 - Forward
do {
chunk = this._parseChunk(stream, strict);
} while (chunk.getType() !== 'IEND');

// Run through all chunks before scaling
// Phase 2
// Phase 2 - Forward
this.applyWithSortedChunks(function (chunk) {

try {
Expand All @@ -95,7 +91,7 @@ Decoder.prototype.decode = function (data, options) {
}, true);

// Run through all chunks after scaling
// Phase 3
// Phase 3 - Forward
this.applyWithSortedChunks(function (chunk) {

try {
Expand All @@ -112,7 +108,7 @@ Decoder.prototype.decode = function (data, options) {
}, true);

// Run through all chunks-types (not chunks) to gather chunk-data
// Phase 4
// Phase 4 - Forward
this.applyWithSortedChunks(function (chunk) {
Chunk.decodeTypeData(chunk.getType(), this._chunkData, this._chunks, strict);
}.bind(this), false);
Expand All @@ -134,6 +130,7 @@ Decoder.prototype._parseChunk = function (stream, strict) {
var chunkLength,
chunkType,
chunkCrc,
calculatedCrc,
crc,
chunk,
chunkOffset;
Expand All @@ -157,7 +154,8 @@ Decoder.prototype._parseChunk = function (stream, strict) {

// Load crc and compare with calculated one
chunkCrc = stream.readInt32BE();
if (strict && (chunkCrc !== crc.getValue())) {
calculatedCrc = crc.getValue();
if (strict && (chunkCrc !== calculatedCrc)) {
throw new Error('CRC error.');
}

Expand Down
Loading

0 comments on commit 9e8ae46

Please sign in to comment.