From 13e1ffa04670d7c7a363d54eab92e31f01243cd7 Mon Sep 17 00:00:00 2001 From: Markus <53560412+brainbugfix@users.noreply.github.com> Date: Thu, 8 Apr 2021 00:26:09 +0200 Subject: [PATCH] feat: add comment tag encoding (#87) --- index.d.ts | 2 +- lib/encoder.js | 17 ++++++++++++++++- test/fixtures/redbox_comment.jpg | Bin 0 -> 2259 bytes test/index.js | 26 ++++++++++++++++++++++++++ 4 files changed, 43 insertions(+), 2 deletions(-) create mode 100644 test/fixtures/redbox_comment.jpg diff --git a/index.d.ts b/index.d.ts index 696cc7a..adbdd4b 100644 --- a/index.d.ts +++ b/index.d.ts @@ -10,7 +10,7 @@ type UintArrRet = RawImageData; type ImageData = BufferRet | UintArrRet; type BufferLike = Buffer | Uint8Array | ArrayLike | Iterable | ArrayBuffer; -export declare function encode(imgData: RawImageData, quality?: number): BufferRet; +export declare function encode(imgData: RawImageData & {comments?: string[]}, quality?: number): BufferRet export declare function decode( jpegData: BufferLike, diff --git a/lib/encoder.js b/lib/encoder.js index bb17d3b..fdbc184 100644 --- a/lib/encoder.js +++ b/lib/encoder.js @@ -535,6 +535,20 @@ function JPEGEncoder(quality) { writeByte(std_ac_chrominance_values[p]); } } + + function writeCOM(comments) + { + if (typeof comments === "undefined" || comments.constructor !== Array) return; + comments.forEach(e => { + if (typeof e !== "string") return; + writeWord(0xFFFE); // marker + var l = e.length; + writeWord(l + 2); // length itself as well + var i; + for (i = 0; i < l; i++) + writeByte(e.charCodeAt(i)); + }); + } function writeSOS() { @@ -625,6 +639,7 @@ function JPEGEncoder(quality) { // Add JPEG headers writeWord(0xFFD8); // SOI writeAPP0(); + writeCOM(image.comments); writeAPP1(image.exifBuffer); writeDQT(); writeSOF0(image.width,image.height); @@ -782,7 +797,7 @@ function encode(imgData, qu) { return { data: data, width: imgData.width, - height: imgData.height + height: imgData.height, }; } diff --git a/test/fixtures/redbox_comment.jpg b/test/fixtures/redbox_comment.jpg new file mode 100644 index 0000000000000000000000000000000000000000..a0c4235df179f608851127b5637ff590bda64e0e GIT binary patch literal 2259 zcmex=X>4Kc;1T2J;1cd?ZV`|h7#^RL zo{?_nTUc3;QW2Ayp3EpHBqSj!p)Vz+pX_AeluR=Ce}F-dgJBD!12dx_1Ct;lvmoRD zBaDlH-e6?}0|qEyWMXDvWn<^yovIz$! zvMUve7&T5@$f4}C@t|nX#SbdRNkvVZTw>x9l2WQ_>Kd9_CZ=ZQ7M51dF0O9w9-dyo zA)#U65s^{JDXD4c8JStdC8cHM6_r)ZEv;?s9i3g1CQq3GGAU*RJ2VdF$b$$4{OPfBE|D`;VW$ z7#Wx$-T{&j4Gc)#w^HS&v10T&hG;Y hm>RekG#Eyy(I6O21)~{Zv@94c4#}z!{xkf)2>`3X8TSAH literal 0 HcmV?d00001 diff --git a/test/index.js b/test/index.js index 6944449..fd1339a 100644 --- a/test/index.js +++ b/test/index.js @@ -179,6 +179,32 @@ it('should be able to create a JPEG from an array', function () { expect(jpegImageData.data).toEqual(expected); }); +it('should be able to create a JPEG from an array with comment', function () { + var width = 320, + height = 180; + var comments = ["First comment", "Second comment"]; + var frameData = new Buffer(width * height * 4); + var i = 0; + while (i < frameData.length) { + frameData[i++] = 0xff; // red + frameData[i++] = 0x00; // green + frameData[i++] = 0x00; // blue + frameData[i++] = 0xff; // alpha - ignored in JPEGs + } + var rawImageData = { + data: frameData, + width: width, + height: height, + comments: comments, + }; + var jpegImageData = jpeg.encode(rawImageData, 50); + expect(jpegImageData.width).toEqual(width); + expect(jpegImageData.height).toEqual(height); + var expected = fixture('redbox_comment.jpg'); + expect(jpegImageData.data).toEqual(expected); + expect(jpeg.decode(jpegImageData.data).comments).toEqual(['First comment', 'Second comment']); +}); + it('should be able to decode a JPEG into a typed array', function () { var jpegData = fixture('grumpycat.jpg'); var rawImageData = jpeg.decode(jpegData, {useTArray: true});