1- import exif from 'jpeg-exif' ;
2-
3- const MARKERS = [
4- 0xffc0 , 0xffc1 , 0xffc2 , 0xffc3 , 0xffc5 , 0xffc6 , 0xffc7 , 0xffc8 , 0xffc9 ,
5- 0xffca , 0xffcb , 0xffcc , 0xffcd , 0xffce , 0xffcf ,
6- ] ;
1+ import _JPEG from 'jay-peg' ;
72
83const COLOR_SPACE_MAP = {
94 1 : 'DeviceGray' ,
@@ -13,40 +8,30 @@ const COLOR_SPACE_MAP = {
138
149class JPEG {
1510 constructor ( data , label ) {
16- let marker ;
1711 this . data = data ;
1812 this . label = label ;
13+ this . orientation = 1 ;
14+
1915 if ( this . data . readUInt16BE ( 0 ) !== 0xffd8 ) {
2016 throw 'SOI not found in JPEG' ;
2117 }
2218
23- // Parse the EXIF orientation
24- this . orientation = exif . fromBuffer ( this . data ) . Orientation || 1 ;
19+ const markers = _JPEG . decode ( this . data ) ;
2520
26- let pos = 2 ;
27- while ( pos < this . data . length ) {
28- marker = this . data . readUInt16BE ( pos ) ;
29- pos += 2 ;
30- if ( MARKERS . includes ( marker ) ) {
31- break ;
21+ for ( let i = 0 ; i < markers . length ; i += 1 ) {
22+ const marker = markers [ i ] ;
23+
24+ if ( marker . name === 'EXIF' && marker . entries . orientation ) {
25+ this . orientation = marker . entries . orientation ;
3226 }
33- pos += this . data . readUInt16BE ( pos ) ;
34- }
3527
36- if ( ! MARKERS . includes ( marker ) ) {
37- throw 'Invalid JPEG.' ;
28+ if ( marker . name === 'SOF' ) {
29+ this . bits ||= marker . precision ;
30+ this . width ||= marker . width ;
31+ this . height ||= marker . height ;
32+ this . colorSpace ||= COLOR_SPACE_MAP [ marker . numberOfComponents ] ;
33+ }
3834 }
39- pos += 2 ;
40-
41- this . bits = this . data [ pos ++ ] ;
42- this . height = this . data . readUInt16BE ( pos ) ;
43- pos += 2 ;
44-
45- this . width = this . data . readUInt16BE ( pos ) ;
46- pos += 2 ;
47-
48- const channels = this . data [ pos ++ ] ;
49- this . colorSpace = COLOR_SPACE_MAP [ channels ] ;
5035
5136 this . obj = null ;
5237 }
0 commit comments