File tree Expand file tree Collapse file tree 4 files changed +43
-2
lines changed
Expand file tree Collapse file tree 4 files changed +43
-2
lines changed Original file line number Diff line number Diff line change @@ -10,7 +10,7 @@ type UintArrRet = RawImageData<Uint8Array>;
1010type ImageData = BufferRet | UintArrRet ;
1111type BufferLike = Buffer | Uint8Array | ArrayLike < number > | Iterable < number > | ArrayBuffer ;
1212
13- export declare function encode ( imgData : RawImageData < BufferLike > , quality ?: number ) : BufferRet ;
13+ export declare function encode ( imgData : RawImageData < BufferLike > & { comments ?: string [ ] } , quality ?: number ) : BufferRet
1414
1515export declare function decode (
1616 jpegData : BufferLike ,
Original file line number Diff line number Diff line change @@ -535,6 +535,20 @@ function JPEGEncoder(quality) {
535535 writeByte ( std_ac_chrominance_values [ p ] ) ;
536536 }
537537 }
538+
539+ function writeCOM ( comments )
540+ {
541+ if ( typeof comments === "undefined" || comments . constructor !== Array ) return ;
542+ comments . forEach ( e => {
543+ if ( typeof e !== "string" ) return ;
544+ writeWord ( 0xFFFE ) ; // marker
545+ var l = e . length ;
546+ writeWord ( l + 2 ) ; // length itself as well
547+ var i ;
548+ for ( i = 0 ; i < l ; i ++ )
549+ writeByte ( e . charCodeAt ( i ) ) ;
550+ } ) ;
551+ }
538552
539553 function writeSOS ( )
540554 {
@@ -625,6 +639,7 @@ function JPEGEncoder(quality) {
625639 // Add JPEG headers
626640 writeWord ( 0xFFD8 ) ; // SOI
627641 writeAPP0 ( ) ;
642+ writeCOM ( image . comments ) ;
628643 writeAPP1 ( image . exifBuffer ) ;
629644 writeDQT ( ) ;
630645 writeSOF0 ( image . width , image . height ) ;
@@ -782,7 +797,7 @@ function encode(imgData, qu) {
782797 return {
783798 data : data ,
784799 width : imgData . width ,
785- height : imgData . height
800+ height : imgData . height ,
786801 } ;
787802}
788803
Original file line number Diff line number Diff line change @@ -179,6 +179,32 @@ 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+ expect ( jpeg . decode ( jpegImageData . data ) . comments ) . toEqual ( [ 'First comment' , 'Second comment' ] ) ;
206+ } ) ;
207+
182208it ( 'should be able to decode a JPEG into a typed array' , function ( ) {
183209 var jpegData = fixture ( 'grumpycat.jpg' ) ;
184210 var rawImageData = jpeg . decode ( jpegData , { useTArray : true } ) ;
You can’t perform that action at this time.
0 commit comments