From c247c5a51d758388496c5e66b48b77a179ddb93c Mon Sep 17 00:00:00 2001 From: Markus Date: Thu, 25 Mar 2021 20:01:28 +0100 Subject: [PATCH 1/5] - added possibility to write comment tags --- lib/encoder.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/lib/encoder.js b/lib/encoder.js index bb17d3b..c3de3de 100644 --- a/lib/encoder.js +++ b/lib/encoder.js @@ -535,6 +535,19 @@ function JPEGEncoder(quality) { writeByte(std_ac_chrominance_values[p]); } } + + function writeCOM(comments) + { + if (typeof comments === "undefined") return; + comments.forEach(e => { + 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 +638,7 @@ function JPEGEncoder(quality) { // Add JPEG headers writeWord(0xFFD8); // SOI writeAPP0(); + writeCOM(image.comments); writeAPP1(image.exifBuffer); writeDQT(); writeSOF0(image.width,image.height); From f43b1912c57b9c33cbc7e7f6c0dc7ec691e5ee1b Mon Sep 17 00:00:00 2001 From: Markus Date: Fri, 26 Mar 2021 19:54:37 +0100 Subject: [PATCH 2/5] - added checks for comments being an array and the content is string --- lib/encoder.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/encoder.js b/lib/encoder.js index c3de3de..28fc71c 100644 --- a/lib/encoder.js +++ b/lib/encoder.js @@ -538,8 +538,9 @@ function JPEGEncoder(quality) { function writeCOM(comments) { - if (typeof comments === "undefined") return; + 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 From dcdecf92f042422e95f18f680e78cf71364eae0f Mon Sep 17 00:00:00 2001 From: Markus Date: Sat, 3 Apr 2021 22:01:14 +0200 Subject: [PATCH 3/5] - updated the typescript - added a test for writing the comments --- index.d.ts | 3 ++- lib/encoder.js | 3 ++- test/fixtures/redbox_comment.jpg | Bin 0 -> 2259 bytes test/index.js | 25 +++++++++++++++++++++++++ 4 files changed, 29 insertions(+), 2 deletions(-) create mode 100644 test/fixtures/redbox_comment.jpg diff --git a/index.d.ts b/index.d.ts index 696cc7a..10cf189 100644 --- a/index.d.ts +++ b/index.d.ts @@ -2,6 +2,7 @@ export interface RawImageData { width: number; height: number; data: T; + comments?: string[]; } type BufferRet = RawImageData; @@ -10,7 +11,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, quality?: number): BufferRet & {comments?: string[]}; export declare function decode( jpegData: BufferLike, diff --git a/lib/encoder.js b/lib/encoder.js index 28fc71c..b9df544 100644 --- a/lib/encoder.js +++ b/lib/encoder.js @@ -797,7 +797,8 @@ function encode(imgData, qu) { return { data: data, width: imgData.width, - height: imgData.height + height: imgData.height, + comments: imgData.comments }; } 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..5324ed7 100644 --- a/test/index.js +++ b/test/index.js @@ -179,6 +179,31 @@ 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); +}); + 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}); From 1f513309b39e9030955823c217ab0c6fbec292bb Mon Sep 17 00:00:00 2001 From: Patrick Hulce Date: Wed, 7 Apr 2021 17:22:49 -0500 Subject: [PATCH 4/5] Apply suggestions from code review --- index.d.ts | 3 +-- lib/encoder.js | 1 - test/index.js | 1 + 3 files changed, 2 insertions(+), 3 deletions(-) diff --git a/index.d.ts b/index.d.ts index 10cf189..adbdd4b 100644 --- a/index.d.ts +++ b/index.d.ts @@ -2,7 +2,6 @@ export interface RawImageData { width: number; height: number; data: T; - comments?: string[]; } type BufferRet = RawImageData; @@ -11,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 & {comments?: string[]}; +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 b9df544..fdbc184 100644 --- a/lib/encoder.js +++ b/lib/encoder.js @@ -798,7 +798,6 @@ function encode(imgData, qu) { data: data, width: imgData.width, height: imgData.height, - comments: imgData.comments }; } diff --git a/test/index.js b/test/index.js index 5324ed7..0eb17e8 100644 --- a/test/index.js +++ b/test/index.js @@ -202,6 +202,7 @@ it('should be able to create a JPEG from an array with comment', function () { 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 () { From 3024321d54acc056b3a8465b3dad20973268c34f Mon Sep 17 00:00:00 2001 From: Patrick Hulce Date: Wed, 7 Apr 2021 17:24:48 -0500 Subject: [PATCH 5/5] Update test/index.js --- test/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/index.js b/test/index.js index 0eb17e8..fd1339a 100644 --- a/test/index.js +++ b/test/index.js @@ -202,7 +202,7 @@ it('should be able to create a JPEG from an array with comment', function () { 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']); + expect(jpeg.decode(jpegImageData.data).comments).toEqual(['First comment', 'Second comment']); }); it('should be able to decode a JPEG into a typed array', function () {