Skip to content

Commit

Permalink
feat(decoder): add comment support (#71)
Browse files Browse the repository at this point in the history
  • Loading branch information
sparticvs committed Jun 27, 2020
1 parent a2f7080 commit d2323c1
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
9 changes: 9 additions & 0 deletions lib/decoder.js
Expand Up @@ -623,6 +623,7 @@ var JpegImage = (function jpegImage() {
var quantizationTables = [], frames = [];
var huffmanTablesAC = [], huffmanTablesDC = [];
var fileMarker = readUint16();
this.comments = [];
if (fileMarker != 0xFFD8) { // SOI (Start of Image)
throw new Error("SOI not found");
}
Expand Down Expand Up @@ -651,6 +652,11 @@ var JpegImage = (function jpegImage() {
case 0xFFFE: // COM (Comment)
var appData = readDataBlock();

if (fileMarker === 0xFFFE) {
var comment = String.fromCharCode.apply(null, appData);
this.comments.push(comment);
}

if (fileMarker === 0xFFE0) {
if (appData[0] === 0x4A && appData[1] === 0x46 && appData[2] === 0x49 &&
appData[3] === 0x46 && appData[4] === 0) { // 'JFIF\x00'
Expand Down Expand Up @@ -1096,6 +1102,9 @@ function decode(jpegData, userOpts = {}) {
new Uint8Array(bytesNeeded) :
new Buffer(bytesNeeded)
};
if(decoder.comments.length > 0) {
image["comments"] = decoder.comments;
}
} catch (err){
if (err instanceof RangeError){
throw new Error("Could not allocate enough memory for the image. " +
Expand Down
1 change: 1 addition & 0 deletions test/index.js
Expand Up @@ -49,6 +49,7 @@ it('should be able to decode a grayscale JPEG', function () {
var rawImageData = jpeg.decode(jpegData);
expect(rawImageData.width).toEqual(580);
expect(rawImageData.height).toEqual(599);
expect(rawImageData.comments).toEqual(['File source: http://commons.wikimedia.org/wiki/File:Apsara-mit-Sitar.jpg']);
var expected = fixture('apsara.rgba');
expect(rawImageData.data).toEqual(expected);
});
Expand Down

0 comments on commit d2323c1

Please sign in to comment.