From 53e0bc66984576da844e4bc4586a5afa9ad92c8c Mon Sep 17 00:00:00 2001 From: John Cupitt Date: Thu, 25 Jul 2019 11:31:21 +0100 Subject: [PATCH] pdfload / pdfiumload use minimise to close early see https://github.com/libvips/libvips/issues/1370#issuecomment-514949292 --- libvips/foreign/pdfiumload.c | 21 ++++++++++++++------- libvips/foreign/pdfload.c | 22 ++++++++++++++-------- 2 files changed, 28 insertions(+), 15 deletions(-) diff --git a/libvips/foreign/pdfiumload.c b/libvips/foreign/pdfiumload.c index 4deb2ba0..694bdb0e 100644 --- a/libvips/foreign/pdfiumload.c +++ b/libvips/foreign/pdfiumload.c @@ -380,6 +380,15 @@ vips_foreign_load_pdf_header( VipsForeignLoad *load ) return( 0 ); } +static void +vips_foreign_load_pdf_minimise( VipsObject *object, VipsForeignLoadPdf *pdf ) +{ + /* In seq mode, we can shut down the input at the end of computation. + */ + if( VIPS_FOREIGN_LOAD( pdf )->access == VIPS_ACCESS_SEQUENTIAL ) + vips_foreign_load_pdf_close( pdf ); +} + static int vips_foreign_load_pdf_generate( VipsRegion *or, void *seq, void *a, void *b, gboolean *stop ) @@ -436,13 +445,6 @@ vips_foreign_load_pdf_generate( VipsRegion *or, i += 1; } - /* In seq mode, we can shut down the input when we render the last - * row. - */ - if( top >= or->im->Ysize && - load->access == VIPS_ACCESS_SEQUENTIAL ) - vips_foreign_load_pdf_close( pdf ); - /* PDFium writes BRGA, we must swap. * * FIXME ... this is a bit slow. @@ -476,6 +478,11 @@ vips_foreign_load_pdf_load( VipsForeignLoad *load ) */ t[0] = vips_image_new(); + /* Close input immediately at end of read. + */ + g_signal_connect( t[0], "minimise", + G_CALLBACK( vips_foreign_load_pdf_minimise ), pdf ); + vips_foreign_load_pdf_set_image( pdf, t[0] ); if( vips_image_generate( t[0], NULL, vips_foreign_load_pdf_generate, NULL, pdf, NULL ) ) diff --git a/libvips/foreign/pdfload.c b/libvips/foreign/pdfload.c index 1a6e5b74..bf2dffde 100644 --- a/libvips/foreign/pdfload.c +++ b/libvips/foreign/pdfload.c @@ -334,12 +334,20 @@ vips_foreign_load_pdf_header( VipsForeignLoad *load ) return( 0 ); } +static void +vips_foreign_load_pdf_minimise( VipsObject *object, VipsForeignLoadPdf *pdf ) +{ + /* In seq mode, we can shut down the input at the end of computation. + */ + if( VIPS_FOREIGN_LOAD( pdf )->access == VIPS_ACCESS_SEQUENTIAL ) + vips_foreign_load_pdf_close( pdf ); +} + static int vips_foreign_load_pdf_generate( VipsRegion *or, void *seq, void *a, void *b, gboolean *stop ) { VipsForeignLoadPdf *pdf = (VipsForeignLoadPdf *) a; - VipsForeignLoad *load = (VipsForeignLoad *) pdf; VipsRect *r = &or->valid; int top; @@ -398,13 +406,6 @@ vips_foreign_load_pdf_generate( VipsRegion *or, i += 1; } - /* In seq mode, we can shut down the input when we render the last - * row. - */ - if( top >= or->im->Ysize && - load->access == VIPS_ACCESS_SEQUENTIAL ) - vips_foreign_load_pdf_close( pdf ); - /* Cairo makes pre-multipled BRGA, we must byteswap and unpremultiply. */ for( y = 0; y < r->height; y++ ) @@ -430,6 +431,11 @@ vips_foreign_load_pdf_load( VipsForeignLoad *load ) */ t[0] = vips_image_new(); + /* Close input immediately at end of read. + */ + g_signal_connect( t[0], "minimise", + G_CALLBACK( vips_foreign_load_pdf_minimise ), pdf ); + vips_foreign_load_pdf_set_image( pdf, t[0] ); if( vips_image_generate( t[0], NULL, vips_foreign_load_pdf_generate, NULL, pdf, NULL ) )