13
13
* limitations under the License.
14
14
*/
15
15
16
- import { shadow , warn } from "../shared/util.js" ;
17
16
import { DecodeStream } from "./decode_stream.js" ;
18
17
import { Dict } from "./primitives.js" ;
19
18
import { JpegImage } from "./jpg.js" ;
19
+ import { shadow } from "../shared/util.js" ;
20
20
21
21
/**
22
22
* For JPEG's we use a library to decode these images and the stream behaves
@@ -32,18 +32,6 @@ class JpegStream extends DecodeStream {
32
32
this . params = params ;
33
33
}
34
34
35
- static get canUseImageDecoder ( ) {
36
- return shadow (
37
- this ,
38
- "canUseImageDecoder" ,
39
- // eslint-disable-next-line no-undef
40
- typeof ImageDecoder === "undefined"
41
- ? Promise . resolve ( false )
42
- : // eslint-disable-next-line no-undef
43
- ImageDecoder . isTypeSupported ( "image/jpeg" )
44
- ) ;
45
- }
46
-
47
35
get bytes ( ) {
48
36
// If `this.maybeLength` is null, we'll get the entire stream.
49
37
return shadow ( this , "bytes" , this . stream . getBytes ( this . maybeLength ) ) ;
@@ -58,7 +46,22 @@ class JpegStream extends DecodeStream {
58
46
this . decodeImage ( ) ;
59
47
}
60
48
61
- get jpegOptions ( ) {
49
+ decodeImage ( bytes ) {
50
+ if ( this . eof ) {
51
+ return this . buffer ;
52
+ }
53
+ bytes ||= this . bytes ;
54
+
55
+ // Some images may contain 'junk' before the SOI (start-of-image) marker.
56
+ // Note: this seems to mainly affect inline images.
57
+ for ( let i = 0 , ii = bytes . length - 1 ; i < ii ; i ++ ) {
58
+ if ( bytes [ i ] === 0xff && bytes [ i + 1 ] === 0xd8 ) {
59
+ if ( i > 0 ) {
60
+ bytes = bytes . subarray ( i ) ;
61
+ }
62
+ break ;
63
+ }
64
+ }
62
65
const jpegOptions = {
63
66
decodeTransform : undefined ,
64
67
colorTransform : undefined ,
@@ -90,34 +93,8 @@ class JpegStream extends DecodeStream {
90
93
jpegOptions . colorTransform = colorTransform ;
91
94
}
92
95
}
93
- return shadow ( this , "jpegOptions" , jpegOptions ) ;
94
- }
95
-
96
- #skipUselessBytes( data ) {
97
- // Some images may contain 'junk' before the SOI (start-of-image) marker.
98
- // Note: this seems to mainly affect inline images.
99
- for ( let i = 0 , ii = data . length - 1 ; i < ii ; i ++ ) {
100
- if ( data [ i ] === 0xff && data [ i + 1 ] === 0xd8 ) {
101
- if ( i > 0 ) {
102
- data = data . subarray ( i ) ;
103
- }
104
- break ;
105
- }
106
- }
107
- return data ;
108
- }
109
-
110
- decodeImage ( bytes ) {
111
- if ( this . eof ) {
112
- return this . buffer ;
113
- }
114
- bytes = this . #skipUselessBytes( bytes || this . bytes ) ;
115
96
116
- // TODO: if an image has a mask we need to combine the data.
117
- // So ideally get a VideoFrame from getTransferableImage and then use
118
- // copyTo.
119
-
120
- const jpegImage = new JpegImage ( this . jpegOptions ) ;
97
+ const jpegImage = new JpegImage ( jpegOptions ) ;
121
98
jpegImage . parse ( bytes ) ;
122
99
const data = jpegImage . getData ( {
123
100
width : this . drawWidth ,
@@ -136,48 +113,6 @@ class JpegStream extends DecodeStream {
136
113
get canAsyncDecodeImageFromBuffer ( ) {
137
114
return this . stream . isAsync ;
138
115
}
139
-
140
- async getTransferableImage ( ) {
141
- if ( ! ( await JpegStream . canUseImageDecoder ) ) {
142
- return null ;
143
- }
144
- const jpegOptions = this . jpegOptions ;
145
- if ( jpegOptions . decodeTransform ) {
146
- // TODO: We could decode the image thanks to ImageDecoder and then
147
- // get the pixels with copyTo and apply the decodeTransform.
148
- return null ;
149
- }
150
- let decoder ;
151
- try {
152
- // TODO: If the stream is Flate & DCT we could try to just pipe the
153
- // the DecompressionStream into the ImageDecoder: it'll avoid the
154
- // intermediate ArrayBuffer.
155
- const bytes =
156
- ( this . canAsyncDecodeImageFromBuffer &&
157
- ( await this . stream . asyncGetBytes ( ) ) ) ||
158
- this . bytes ;
159
- if ( ! bytes ) {
160
- return null ;
161
- }
162
- const data = this . #skipUselessBytes( bytes ) ;
163
- if ( ! JpegImage . canUseImageDecoder ( data , jpegOptions . colorTransform ) ) {
164
- return null ;
165
- }
166
- // eslint-disable-next-line no-undef
167
- decoder = new ImageDecoder ( {
168
- data,
169
- type : "image/jpeg" ,
170
- preferAnimation : false ,
171
- } ) ;
172
-
173
- return ( await decoder . decode ( ) ) . image ;
174
- } catch ( reason ) {
175
- warn ( `getTransferableImage - failed: "${ reason } ".` ) ;
176
- return null ;
177
- } finally {
178
- decoder ?. close ( ) ;
179
- }
180
- }
181
116
}
182
117
183
118
export { JpegStream } ;
0 commit comments