Skip to content

Commit

Permalink
Adds support for the pHYs header (pngjs#383)
Browse files Browse the repository at this point in the history
  • Loading branch information
Aapo Kuuselo committed Mar 19, 2024
1 parent c565210 commit d48ed28
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ module.exports = {
TYPE_PLTE: 0x504c5445,
TYPE_tRNS: 0x74524e53, // eslint-disable-line camelcase
TYPE_gAMA: 0x67414d41, // eslint-disable-line camelcase
TYPE_pHYs: 0x70485973, // eslint-disable-line camelcase

// color-type bits
COLORTYPE_GRAYSCALE: 0,
Expand Down
9 changes: 9 additions & 0 deletions lib/parser-async.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ let ParserAsync = (module.exports = function (options) {
error: this._handleError.bind(this),
metadata: this._handleMetaData.bind(this),
gamma: this.emit.bind(this, "gamma"),
physical: this._handlePhysical.bind(this),
palette: this._handlePalette.bind(this),
transColor: this._handleTransColor.bind(this),
finished: this._finished.bind(this),
Expand Down Expand Up @@ -113,6 +114,14 @@ ParserAsync.prototype._handleMetaData = function (metaData) {
this._filter = new FilterAsync(this._bitmapInfo);
};

ParserAsync.prototype._handlePhysical = function (x, y, unit) {
this._metaData.physical = {
x: x,
y: y,
unit: unit,
};
};

ParserAsync.prototype._handleTransColor = function (transColor) {
this._bitmapInfo.transColor = transColor;
};
Expand Down
13 changes: 13 additions & 0 deletions lib/parser-sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,15 @@ module.exports = function (buffer, options) {
gamma = _gamma_;
}

let physical;
function handlePhysical(x, y, unit) {
physical = {
x: x,
y: y,
unit: unit,
};
}

let inflateDataList = [];
function handleInflateData(inflatedData) {
inflateDataList.push(inflatedData);
Expand All @@ -58,6 +67,7 @@ module.exports = function (buffer, options) {
error: handleError,
metadata: handleMetaData,
gamma: handleGamma,
physical: handlePhysical,
palette: handlePalette,
transColor: handleTransColor,
inflateData: handleInflateData,
Expand Down Expand Up @@ -107,6 +117,9 @@ module.exports = function (buffer, options) {

metaData.data = normalisedBitmapData;
metaData.gamma = gamma || 0;
if (physical) {
metaData.physical = physical;
}

return metaData;
};
16 changes: 16 additions & 0 deletions lib/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@ let Parser = (module.exports = function (options, dependencies) {
this._chunks[constants.TYPE_PLTE] = this._handlePLTE.bind(this);
this._chunks[constants.TYPE_tRNS] = this._handleTRNS.bind(this);
this._chunks[constants.TYPE_gAMA] = this._handleGAMA.bind(this);
this._chunks[constants.TYPE_pHYs] = this._handlePHYS.bind(this);

this.read = dependencies.read;
this.error = dependencies.error;
this.metadata = dependencies.metadata;
this.gamma = dependencies.gamma;
this.physical = dependencies.physical;
this.transColor = dependencies.transColor;
this.palette = dependencies.palette;
this.parsed = dependencies.parsed;
Expand Down Expand Up @@ -253,6 +255,20 @@ Parser.prototype._parseGAMA = function (data) {
this._handleChunkEnd();
};

Parser.prototype._handlePHYS = function (length) {
this.read(length, this._parsePHYS.bind(this));
};
Parser.prototype._parsePHYS = function (data) {
this._crc.write(data);
this.physical(
data.readUIntBE(0, 4),
data.readUIntBE(4, 4),
data.readUIntBE(8, 1)
);

this._handleChunkEnd();
};

Parser.prototype._handleIDAT = function (length) {
if (!this._emittedHeadersFinished) {
this._emittedHeadersFinished = true;
Expand Down
9 changes: 9 additions & 0 deletions test/png-parse-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,15 @@ test("should set alpha=true in metadata for images with tRNS chunk", function (t
});
});

test("should set the pHYs header in metadata for images with a pHYs chunk", function (t) {
fs.createReadStream(path.join(__dirname, "in", "cdfn2c08.png"))
.pipe(new PNG())
.on("metadata", function (metadata) {
t.ok(metadata.physical, "Image should have a pHys header");
t.end();
});
});

test("Should parse with low highWaterMark", function (t) {
fs.createReadStream(path.join(__dirname, "in", "tbbn0g04.png"), {
highWaterMark: 2,
Expand Down

0 comments on commit d48ed28

Please sign in to comment.