@@ -230,62 +230,46 @@ typedef struct {
230
230
static void
231
231
source_init_source ( j_decompress_ptr cinfo )
232
232
{
233
- Source * src = (Source * ) cinfo -> src ;
234
-
235
- /* Start off empty ... libjpeg will call fill_input_buffer to get the
236
- * first bytes.
233
+ /* No work necessary here.
237
234
*/
238
- src -> pub .next_input_byte = src -> buf ;
239
- src -> pub .bytes_in_buffer = 0 ;
240
- }
241
-
242
- static void
243
- source_init_source_mappable ( j_decompress_ptr cinfo )
244
- {
245
235
}
246
236
247
237
/* Fill the input buffer --- called whenever buffer is emptied.
248
238
*/
249
239
static boolean
250
240
source_fill_input_buffer ( j_decompress_ptr cinfo )
251
241
{
252
- static const JOCTET eoi_buffer [4 ] = {
253
- (JOCTET ) 0xFF , (JOCTET ) JPEG_EOI , 0 , 0
254
- };
255
-
256
242
Source * src = (Source * ) cinfo -> src ;
257
243
258
- gint64 bytes_read ;
244
+ gint64 n_bytes ;
259
245
260
- if ( (bytes_read = vips_source_read ( src -> source ,
261
- src -> buf , SOURCE_BUFFER_SIZE )) > 0 ) {
262
- src -> pub .next_input_byte = src -> buf ;
263
- src -> pub .bytes_in_buffer = bytes_read ;
264
- }
265
- else {
246
+ if ( (n_bytes = vips_source_read ( src -> source ,
247
+ src -> buf , SOURCE_BUFFER_SIZE )) <= 0 ) {
266
248
if ( src -> jpeg -> fail_on >= VIPS_FAIL_ON_TRUNCATED ) {
267
- /* Knock the output out of cache.
268
- */
269
- vips_foreign_load_invalidate ( src -> jpeg -> out );
249
+ /* Knock the output out of cache.
250
+ */
251
+ vips_foreign_load_invalidate ( src -> jpeg -> out );
270
252
ERREXIT ( cinfo , JERR_INPUT_EOF );
271
- }
253
+ }
272
254
else
273
255
WARNMS ( cinfo , JWRN_JPEG_EOF );
274
256
275
- src -> pub .next_input_byte = eoi_buffer ;
276
- src -> pub .bytes_in_buffer = 2 ;
257
+ /* Insert a fake EOI marker.
258
+ */
259
+ src -> buf [0 ] = (JOCTET ) 0xFF ;
260
+ src -> buf [1 ] = (JOCTET ) JPEG_EOI ;
261
+ n_bytes = 2 ;
277
262
}
278
263
264
+ src -> pub .next_input_byte = src -> buf ;
265
+ src -> pub .bytes_in_buffer = n_bytes ;
266
+
279
267
return ( TRUE );
280
268
}
281
269
282
270
static boolean
283
271
source_fill_input_buffer_mappable ( j_decompress_ptr cinfo )
284
272
{
285
- static const JOCTET eoi_buffer [4 ] = {
286
- (JOCTET ) 0xFF , (JOCTET ) JPEG_EOI , 0 , 0
287
- };
288
-
289
273
Source * src = (Source * ) cinfo -> src ;
290
274
291
275
if ( src -> jpeg -> fail_on >= VIPS_FAIL_ON_TRUNCATED ) {
@@ -297,7 +281,12 @@ source_fill_input_buffer_mappable( j_decompress_ptr cinfo )
297
281
else
298
282
WARNMS ( cinfo , JWRN_JPEG_EOF );
299
283
300
- src -> pub .next_input_byte = eoi_buffer ;
284
+ /* Insert a fake EOI marker.
285
+ */
286
+ src -> buf [0 ] = (JOCTET ) 0xFF ;
287
+ src -> buf [1 ] = (JOCTET ) JPEG_EOI ;
288
+
289
+ src -> pub .next_input_byte = src -> buf ;
301
290
src -> pub .bytes_in_buffer = 2 ;
302
291
303
292
return ( TRUE );
@@ -309,9 +298,9 @@ skip_input_data( j_decompress_ptr cinfo, long num_bytes )
309
298
Source * src = (Source * ) cinfo -> src ;
310
299
311
300
if ( num_bytes > 0 ) {
312
- while ( num_bytes > (long ) src -> pub .bytes_in_buffer ) {
301
+ while ( num_bytes > (long ) src -> pub .bytes_in_buffer ) {
313
302
num_bytes -= (long ) src -> pub .bytes_in_buffer ;
314
- (void ) (* src -> pub .fill_input_buffer ) ( cinfo );
303
+ (void ) (* src -> pub .fill_input_buffer )( cinfo );
315
304
316
305
/* note we assume that fill_input_buffer will never
317
306
* return FALSE, so suspension need not be handled.
@@ -331,7 +320,8 @@ skip_input_data_mappable( j_decompress_ptr cinfo, long num_bytes )
331
320
if ( num_bytes > (long ) src -> pub .bytes_in_buffer ) {
332
321
src -> pub .next_input_byte += src -> pub .bytes_in_buffer ;
333
322
src -> pub .bytes_in_buffer = 0 ;
334
- } else {
323
+ }
324
+ else {
335
325
src -> pub .next_input_byte += (size_t ) num_bytes ;
336
326
src -> pub .bytes_in_buffer -= (size_t ) num_bytes ;
337
327
}
@@ -357,26 +347,34 @@ readjpeg_open_input( ReadJpeg *jpeg )
357
347
src = (Source * ) cinfo -> src ;
358
348
src -> jpeg = jpeg ;
359
349
src -> source = jpeg -> source ;
350
+ src -> pub .init_source = source_init_source ;
351
+
352
+ /* Use default method.
353
+ */
360
354
src -> pub .resync_to_restart = jpeg_resync_to_restart ;
361
355
362
356
if ( vips_source_is_mappable ( jpeg -> source ) ) {
363
357
size_t src_len ;
364
358
const unsigned char * src_data = vips_source_map (
365
359
jpeg -> source , & src_len );
366
360
367
- src -> pub .init_source = source_init_source_mappable ;
368
361
src -> pub .fill_input_buffer =
369
362
source_fill_input_buffer_mappable ;
370
363
src -> pub .skip_input_data = skip_input_data_mappable ;
371
364
src -> pub .bytes_in_buffer = src_len ;
372
365
src -> pub .next_input_byte = src_data ;
373
366
}
374
367
else {
375
- src -> pub .init_source = source_init_source ;
376
368
src -> pub .fill_input_buffer = source_fill_input_buffer ;
377
369
src -> pub .skip_input_data = skip_input_data ;
370
+
371
+ /* Forces fill_input_buffer on first read.
372
+ */
378
373
src -> pub .bytes_in_buffer = 0 ;
379
- src -> pub .next_input_byte = src -> buf ;
374
+
375
+ /* Until buffer loaded.
376
+ */
377
+ src -> pub .next_input_byte = NULL ;
380
378
}
381
379
}
382
380
0 commit comments