Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

- added possibility to write comment tags #87

Merged
merged 5 commits into from Apr 7, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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);
patrickhulce marked this conversation as resolved.
Show resolved Hide resolved
expect(jpeg.decode(jpegImageData.data).comments).toEqual(['First comment', 'second comment']);
patrickhulce marked this conversation as resolved.
Show resolved Hide resolved
});

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