Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit dcdecf9

Browse files
committed
- updated the typescript - added a test for writing the comments
1 parent f43b191 commit dcdecf9

File tree

4 files changed

+29
-2
lines changed

4 files changed

+29
-2
lines changed

index.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ export interface RawImageData<T> {
22
width: number;
33
height: number;
44
data: T;
5+
comments?: string[];
56
}
67

78
type BufferRet = RawImageData<Buffer>;
@@ -10,7 +11,7 @@ type UintArrRet = RawImageData<Uint8Array>;
1011
type ImageData = BufferRet | UintArrRet;
1112
type BufferLike = Buffer | Uint8Array | ArrayLike<number> | Iterable<number> | ArrayBuffer;
1213

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

1516
export declare function decode(
1617
jpegData: BufferLike,

lib/encoder.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -797,7 +797,8 @@ function encode(imgData, qu) {
797797
return {
798798
data: data,
799799
width: imgData.width,
800-
height: imgData.height
800+
height: imgData.height,
801+
comments: imgData.comments
801802
};
802803
}
803804

test/fixtures/redbox_comment.jpg

2.21 KB
Loading

test/index.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,31 @@ it('should be able to create a JPEG from an array', function () {
179179
expect(jpegImageData.data).toEqual(expected);
180180
});
181181

182+
it('should be able to create a JPEG from an array with comment', function () {
183+
var width = 320,
184+
height = 180;
185+
var comments = ["First comment", "Second comment"];
186+
var frameData = new Buffer(width * height * 4);
187+
var i = 0;
188+
while (i < frameData.length) {
189+
frameData[i++] = 0xff; // red
190+
frameData[i++] = 0x00; // green
191+
frameData[i++] = 0x00; // blue
192+
frameData[i++] = 0xff; // alpha - ignored in JPEGs
193+
}
194+
var rawImageData = {
195+
data: frameData,
196+
width: width,
197+
height: height,
198+
comments: comments,
199+
};
200+
var jpegImageData = jpeg.encode(rawImageData, 50);
201+
expect(jpegImageData.width).toEqual(width);
202+
expect(jpegImageData.height).toEqual(height);
203+
var expected = fixture('redbox_comment.jpg');
204+
expect(jpegImageData.data).toEqual(expected);
205+
});
206+
182207
it('should be able to decode a JPEG into a typed array', function () {
183208
var jpegData = fixture('grumpycat.jpg');
184209
var rawImageData = jpeg.decode(jpegData, {useTArray: true});

0 commit comments

Comments
 (0)