Skip to content

Commit 67c96af

Browse files
authored
feat(decoder): add comment support (jpeg-js#71)
1 parent b31a974 commit 67c96af

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

lib/decoder.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -623,6 +623,7 @@ var JpegImage = (function jpegImage() {
623623
var quantizationTables = [], frames = [];
624624
var huffmanTablesAC = [], huffmanTablesDC = [];
625625
var fileMarker = readUint16();
626+
this.comments = [];
626627
if (fileMarker != 0xFFD8) { // SOI (Start of Image)
627628
throw new Error("SOI not found");
628629
}
@@ -651,6 +652,11 @@ var JpegImage = (function jpegImage() {
651652
case 0xFFFE: // COM (Comment)
652653
var appData = readDataBlock();
653654

655+
if (fileMarker === 0xFFFE) {
656+
var comment = String.fromCharCode.apply(null, appData);
657+
this.comments.push(comment);
658+
}
659+
654660
if (fileMarker === 0xFFE0) {
655661
if (appData[0] === 0x4A && appData[1] === 0x46 && appData[2] === 0x49 &&
656662
appData[3] === 0x46 && appData[4] === 0) { // 'JFIF\x00'
@@ -1096,6 +1102,9 @@ function decode(jpegData, userOpts = {}) {
10961102
new Uint8Array(bytesNeeded) :
10971103
new Buffer(bytesNeeded)
10981104
};
1105+
if(decoder.comments.length > 0) {
1106+
image["comments"] = decoder.comments;
1107+
}
10991108
} catch (err){
11001109
if (err instanceof RangeError){
11011110
throw new Error("Could not allocate enough memory for the image. " +

test/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ it('should be able to decode a grayscale JPEG', function () {
4949
var rawImageData = jpeg.decode(jpegData);
5050
expect(rawImageData.width).toEqual(580);
5151
expect(rawImageData.height).toEqual(599);
52+
expect(rawImageData.comments).toEqual(['File source: http://commons.wikimedia.org/wiki/File:Apsara-mit-Sitar.jpg']);
5253
var expected = fixture('apsara.rgba');
5354
expect(rawImageData.data).toEqual(expected);
5455
});

0 commit comments

Comments
 (0)