Skip to content

Commit

Permalink
feat: add comment tag encoding (#87)
Browse files Browse the repository at this point in the history
  • Loading branch information
brainbugfix committed Apr 7, 2021
1 parent 417e8e2 commit 13e1ffa
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 2 deletions.
2 changes: 1 addition & 1 deletion index.d.ts
Expand Up @@ -10,7 +10,7 @@ type UintArrRet = RawImageData<Uint8Array>;
type ImageData = BufferRet | UintArrRet;
type BufferLike = Buffer | Uint8Array | ArrayLike<number> | Iterable<number> | ArrayBuffer;

export declare function encode(imgData: RawImageData<BufferLike>, quality?: number): BufferRet;
export declare function encode(imgData: RawImageData<BufferLike> & {comments?: string[]}, quality?: number): BufferRet

export declare function decode(
jpegData: BufferLike,
Expand Down
17 changes: 16 additions & 1 deletion lib/encoder.js
Expand Up @@ -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()
{
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -782,7 +797,7 @@ function encode(imgData, qu) {
return {
data: data,
width: imgData.width,
height: imgData.height
height: imgData.height,
};
}

Expand Down
Binary file added test/fixtures/redbox_comment.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
26 changes: 26 additions & 0 deletions test/index.js
Expand Up @@ -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});
Expand Down

0 comments on commit 13e1ffa

Please sign in to comment.