Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit db1eec4

Browse files
authored
jpeg2vips: simplify (libvips#3542)
Non-functional change.
1 parent e08190f commit db1eec4

File tree

1 file changed

+37
-39
lines changed

1 file changed

+37
-39
lines changed

libvips/foreign/jpeg2vips.c

Lines changed: 37 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -230,62 +230,46 @@ typedef struct {
230230
static void
231231
source_init_source( j_decompress_ptr cinfo )
232232
{
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.
237234
*/
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-
{
245235
}
246236

247237
/* Fill the input buffer --- called whenever buffer is emptied.
248238
*/
249239
static boolean
250240
source_fill_input_buffer( j_decompress_ptr cinfo )
251241
{
252-
static const JOCTET eoi_buffer[4] = {
253-
(JOCTET) 0xFF, (JOCTET) JPEG_EOI, 0, 0
254-
};
255-
256242
Source *src = (Source *) cinfo->src;
257243

258-
gint64 bytes_read;
244+
gint64 n_bytes;
259245

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 ) {
266248
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 );
270252
ERREXIT( cinfo, JERR_INPUT_EOF );
271-
}
253+
}
272254
else
273255
WARNMS( cinfo, JWRN_JPEG_EOF );
274256

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;
277262
}
278263

264+
src->pub.next_input_byte = src->buf;
265+
src->pub.bytes_in_buffer = n_bytes;
266+
279267
return( TRUE );
280268
}
281269

282270
static boolean
283271
source_fill_input_buffer_mappable( j_decompress_ptr cinfo )
284272
{
285-
static const JOCTET eoi_buffer[4] = {
286-
(JOCTET) 0xFF, (JOCTET) JPEG_EOI, 0, 0
287-
};
288-
289273
Source *src = (Source *) cinfo->src;
290274

291275
if( src->jpeg->fail_on >= VIPS_FAIL_ON_TRUNCATED ) {
@@ -297,7 +281,12 @@ source_fill_input_buffer_mappable( j_decompress_ptr cinfo )
297281
else
298282
WARNMS( cinfo, JWRN_JPEG_EOF );
299283

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;
301290
src->pub.bytes_in_buffer = 2;
302291

303292
return( TRUE );
@@ -309,9 +298,9 @@ skip_input_data( j_decompress_ptr cinfo, long num_bytes )
309298
Source *src = (Source *) cinfo->src;
310299

311300
if( num_bytes > 0 ) {
312-
while (num_bytes > (long) src->pub.bytes_in_buffer) {
301+
while( num_bytes > (long) src->pub.bytes_in_buffer ) {
313302
num_bytes -= (long) src->pub.bytes_in_buffer;
314-
(void) (*src->pub.fill_input_buffer) (cinfo);
303+
(void) (*src->pub.fill_input_buffer)( cinfo );
315304

316305
/* note we assume that fill_input_buffer will never
317306
* return FALSE, so suspension need not be handled.
@@ -331,7 +320,8 @@ skip_input_data_mappable( j_decompress_ptr cinfo, long num_bytes )
331320
if( num_bytes > (long) src->pub.bytes_in_buffer ) {
332321
src->pub.next_input_byte += src->pub.bytes_in_buffer;
333322
src->pub.bytes_in_buffer = 0;
334-
} else {
323+
}
324+
else {
335325
src->pub.next_input_byte += (size_t) num_bytes;
336326
src->pub.bytes_in_buffer -= (size_t) num_bytes;
337327
}
@@ -357,26 +347,34 @@ readjpeg_open_input( ReadJpeg *jpeg )
357347
src = (Source *) cinfo->src;
358348
src->jpeg = jpeg;
359349
src->source = jpeg->source;
350+
src->pub.init_source = source_init_source;
351+
352+
/* Use default method.
353+
*/
360354
src->pub.resync_to_restart = jpeg_resync_to_restart;
361355

362356
if( vips_source_is_mappable( jpeg->source ) ) {
363357
size_t src_len;
364358
const unsigned char *src_data = vips_source_map(
365359
jpeg->source, &src_len );
366360

367-
src->pub.init_source = source_init_source_mappable;
368361
src->pub.fill_input_buffer =
369362
source_fill_input_buffer_mappable;
370363
src->pub.skip_input_data = skip_input_data_mappable;
371364
src->pub.bytes_in_buffer = src_len;
372365
src->pub.next_input_byte = src_data;
373366
}
374367
else {
375-
src->pub.init_source = source_init_source;
376368
src->pub.fill_input_buffer = source_fill_input_buffer;
377369
src->pub.skip_input_data = skip_input_data;
370+
371+
/* Forces fill_input_buffer on first read.
372+
*/
378373
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;
380378
}
381379
}
382380

0 commit comments

Comments
 (0)