@@ -744,109 +744,55 @@ function findNextFileMarker(data, currentPos, startPos = currentPos) {
744
744
} ;
745
745
}
746
746
747
- function prepareComponents ( frame ) {
748
- const mcusPerLine = Math . ceil ( frame . samplesPerLine / 8 / frame . maxH ) ;
749
- const mcusPerColumn = Math . ceil ( frame . scanLines / 8 / frame . maxV ) ;
750
- for ( const component of frame . components ) {
751
- const blocksPerLine = Math . ceil (
752
- ( Math . ceil ( frame . samplesPerLine / 8 ) * component . h ) / frame . maxH
753
- ) ;
754
- const blocksPerColumn = Math . ceil (
755
- ( Math . ceil ( frame . scanLines / 8 ) * component . v ) / frame . maxV
756
- ) ;
757
- const blocksPerLineForMcu = mcusPerLine * component . h ;
758
- const blocksPerColumnForMcu = mcusPerColumn * component . v ;
759
-
760
- const blocksBufferSize =
761
- 64 * blocksPerColumnForMcu * ( blocksPerLineForMcu + 1 ) ;
762
- component . blockData = new Int16Array ( blocksBufferSize ) ;
763
- component . blocksPerLine = blocksPerLine ;
764
- component . blocksPerColumn = blocksPerColumn ;
765
- }
766
- frame . mcusPerLine = mcusPerLine ;
767
- frame . mcusPerColumn = mcusPerColumn ;
768
- }
769
-
770
- function readDataBlock ( data , offset ) {
771
- const length = readUint16 ( data , offset ) ;
772
- offset += 2 ;
773
- let endOffset = offset + length - 2 ;
774
-
775
- const fileMarker = findNextFileMarker ( data , endOffset , offset ) ;
776
- if ( fileMarker ?. invalid ) {
777
- warn (
778
- "readDataBlock - incorrect length, current marker is: " +
779
- fileMarker . invalid
780
- ) ;
781
- endOffset = fileMarker . offset ;
782
- }
783
-
784
- const array = data . subarray ( offset , endOffset ) ;
785
- offset += array . length ;
786
- return { appData : array , newOffset : offset } ;
787
- }
788
-
789
- function skipData ( data , offset ) {
790
- const length = readUint16 ( data , offset ) ;
791
- offset += 2 ;
792
- const endOffset = offset + length - 2 ;
793
-
794
- const fileMarker = findNextFileMarker ( data , endOffset , offset ) ;
795
- if ( fileMarker ?. invalid ) {
796
- return fileMarker . offset ;
797
- }
798
- return endOffset ;
799
- }
800
-
801
747
class JpegImage {
802
748
constructor ( { decodeTransform = null , colorTransform = - 1 } = { } ) {
803
749
this . _decodeTransform = decodeTransform ;
804
750
this . _colorTransform = colorTransform ;
805
751
}
806
752
807
- static canUseImageDecoder ( data , colorTransform = - 1 ) {
808
- let offset = 0 ;
809
- let numComponents = null ;
810
- let fileMarker = readUint16 ( data , offset ) ;
811
- offset += 2 ;
812
- if ( fileMarker !== /* SOI (Start of Image) = */ 0xffd8 ) {
813
- throw new JpegError ( "SOI not found" ) ;
814
- }
815
- fileMarker = readUint16 ( data , offset ) ;
816
- offset += 2 ;
753
+ parse ( data , { dnlScanLines = null } = { } ) {
754
+ function readDataBlock ( ) {
755
+ const length = readUint16 ( data , offset ) ;
756
+ offset += 2 ;
757
+ let endOffset = offset + length - 2 ;
817
758
818
- markerLoop: while ( fileMarker !== /* EOI (End of Image) = */ 0xffd9 ) {
819
- switch ( fileMarker ) {
820
- case 0xffc0 : // SOF0 (Start of Frame, Baseline DCT)
821
- case 0xffc1 : // SOF1 (Start of Frame, Extended DCT)
822
- case 0xffc2 : // SOF2 (Start of Frame, Progressive DCT)
823
- // Skip marker length.
824
- // Skip precision.
825
- // Skip scanLines.
826
- // Skip samplesPerLine.
827
- numComponents = data [ offset + ( 2 + 1 + 2 + 2 ) ] ;
828
- break markerLoop;
829
- case 0xffff : // Fill bytes
830
- if ( data [ offset ] !== 0xff ) {
831
- // Avoid skipping a valid marker.
832
- offset -- ;
833
- }
834
- break ;
759
+ const fileMarker = findNextFileMarker ( data , endOffset , offset ) ;
760
+ if ( fileMarker ?. invalid ) {
761
+ warn (
762
+ "readDataBlock - incorrect length, current marker is: " +
763
+ fileMarker . invalid
764
+ ) ;
765
+ endOffset = fileMarker . offset ;
835
766
}
836
- offset = skipData ( data , offset ) ;
837
- fileMarker = readUint16 ( data , offset ) ;
838
- offset += 2 ;
839
- }
840
- if ( numComponents === 4 ) {
841
- return false ;
767
+
768
+ const array = data . subarray ( offset , endOffset ) ;
769
+ offset += array . length ;
770
+ return array ;
842
771
}
843
- if ( numComponents === 3 && colorTransform === 0 ) {
844
- return false ;
772
+
773
+ function prepareComponents ( frame ) {
774
+ const mcusPerLine = Math . ceil ( frame . samplesPerLine / 8 / frame . maxH ) ;
775
+ const mcusPerColumn = Math . ceil ( frame . scanLines / 8 / frame . maxV ) ;
776
+ for ( const component of frame . components ) {
777
+ const blocksPerLine = Math . ceil (
778
+ ( Math . ceil ( frame . samplesPerLine / 8 ) * component . h ) / frame . maxH
779
+ ) ;
780
+ const blocksPerColumn = Math . ceil (
781
+ ( Math . ceil ( frame . scanLines / 8 ) * component . v ) / frame . maxV
782
+ ) ;
783
+ const blocksPerLineForMcu = mcusPerLine * component . h ;
784
+ const blocksPerColumnForMcu = mcusPerColumn * component . v ;
785
+
786
+ const blocksBufferSize =
787
+ 64 * blocksPerColumnForMcu * ( blocksPerLineForMcu + 1 ) ;
788
+ component . blockData = new Int16Array ( blocksBufferSize ) ;
789
+ component . blocksPerLine = blocksPerLine ;
790
+ component . blocksPerColumn = blocksPerColumn ;
791
+ }
792
+ frame . mcusPerLine = mcusPerLine ;
793
+ frame . mcusPerColumn = mcusPerColumn ;
845
794
}
846
- return true ;
847
- }
848
795
849
- parse ( data , { dnlScanLines = null } = { } ) {
850
796
let offset = 0 ;
851
797
let jfif = null ;
852
798
let adobe = null ;
@@ -884,8 +830,7 @@ class JpegImage {
884
830
case 0xffee : // APP14
885
831
case 0xffef : // APP15
886
832
case 0xfffe : // COM (Comment)
887
- const { appData, newOffset } = readDataBlock ( data , offset ) ;
888
- offset = newOffset ;
833
+ const appData = readDataBlock ( ) ;
889
834
890
835
if ( fileMarker === 0xffe0 ) {
891
836
// 'JFIF\x00'
0 commit comments