From 5db2c64d90528f2552821e24199ec1e4da8a8b93 Mon Sep 17 00:00:00 2001 From: John Cupitt Date: Mon, 8 Feb 2016 16:57:37 +0000 Subject: [PATCH] add @scale option and set xres/yres --- TODO | 2 -- libvips/foreign/foreign.c | 4 +++- libvips/foreign/popplerload.c | 38 ++++++++++++++++++++++++++++++----- 3 files changed, 36 insertions(+), 8 deletions(-) diff --git a/TODO b/TODO index aa2c2fea..bda32361 100644 --- a/TODO +++ b/TODO @@ -4,8 +4,6 @@ - add file type sniffing .. "%PDF" should be enough -- metadata: xres/yres - - trim page edges? we often have black now - new vips_reduce: diff --git a/libvips/foreign/foreign.c b/libvips/foreign/foreign.c index b533a290..d2c75845 100644 --- a/libvips/foreign/foreign.c +++ b/libvips/foreign/foreign.c @@ -2857,12 +2857,14 @@ vips_matload( const char *filename, VipsImage **out, ... ) * * @page: %gint, load this page, numbered from zero * @dpi: %gdouble, render at this DPI + * @scale: %gdouble, scale render by this factor * * Render a PDF file into a VIPS image. * * Use @page to select a page to render, numbering from zero. * - * Use @dpi to set the rendering resolution. The default is 72. + * Use @dpi to set the rendering resolution. The default is 72. Alternatively + * you can scale the rendering by @scale. * * This function only reads the image header and does not render any pixel * data. Rendering only occurs when pixels are accessed. diff --git a/libvips/foreign/popplerload.c b/libvips/foreign/popplerload.c index 1c64dc56..debd2c0d 100644 --- a/libvips/foreign/popplerload.c +++ b/libvips/foreign/popplerload.c @@ -97,6 +97,21 @@ vips_foreign_load_poppler_dispose( GObject *gobject ) dispose( gobject ); } +static int +vips_foreign_load_poppler_build( VipsObject *object ) +{ + VipsForeignLoadPoppler *poppler = (VipsForeignLoadPoppler *) object; + + if( !vips_object_argument_isset( object, "scale" ) ) + poppler->scale = poppler->dpi / 72.0; + + if( VIPS_OBJECT_CLASS( vips_foreign_load_poppler_parent_class )-> + build( object ) ) + return( -1 ); + + return( 0 ); +} + static VipsForeignFlags vips_foreign_load_poppler_get_flags_filename( const char *filename ) { @@ -136,17 +151,21 @@ vips_foreign_load_poppler_parse( VipsForeignLoadPoppler *poppler, PopplerRectangle crop_box; double width; double height; + double res; int i; - char *str; poppler_page_get_size( poppler->page, &width, &height ); poppler_page_get_crop_box( poppler->page, &crop_box ); + /* We need pixels/mm for vips. + */ + res = poppler->dpi / 25.4; + vips_image_init_fields( out, width * poppler->scale, height * poppler->scale, 4, VIPS_FORMAT_UCHAR, - VIPS_CODING_NONE, VIPS_INTERPRETATION_sRGB, 1.0, 1.0 ); + VIPS_CODING_NONE, VIPS_INTERPRETATION_sRGB, res, res ); VIPS_SETSTR( out->filename, poppler->filename ); @@ -163,6 +182,8 @@ vips_foreign_load_poppler_parse( VipsForeignLoadPoppler *poppler, VipsForeignLoadPopperMetadata *metadata = &vips_foreign_load_poppler_metadata[i]; + char *str; + if( (str = metadata->poppler_fetch( poppler->doc )) ) { vips_image_set_string( out, metadata->field, str ); g_free( str ); @@ -179,8 +200,6 @@ vips_foreign_load_poppler_header( VipsForeignLoad *load ) char *path; GError *error = NULL; - poppler->scale = poppler->dpi / 72.0; - /* We need an absolute path for a URI. */ path = vips_realpath( poppler->filename ); @@ -309,6 +328,7 @@ vips_foreign_load_poppler_class_init( VipsForeignLoadPopplerClass *class ) object_class->nickname = "popplerload"; object_class->description = _( "load PDF with poppler" ); + object_class->build = vips_foreign_load_poppler_build; foreign_class->suffs = vips_foreign_poppler_suffs; @@ -332,19 +352,27 @@ vips_foreign_load_poppler_class_init( VipsForeignLoadPopplerClass *class ) G_STRUCT_OFFSET( VipsForeignLoadPoppler, page_no ), 0, 100000, 0 ); - VIPS_ARG_DOUBLE( class, "dpi", 10, + VIPS_ARG_DOUBLE( class, "dpi", 11, _( "DPI" ), _( "Render at this DPI" ), VIPS_ARGUMENT_OPTIONAL_INPUT, G_STRUCT_OFFSET( VipsForeignLoadPoppler, dpi ), 0.001, 100000.0, 72.0 ); + VIPS_ARG_DOUBLE( class, "scale", 12, + _( "Scale" ), + _( "Scale output by this factor" ), + VIPS_ARGUMENT_OPTIONAL_INPUT, + G_STRUCT_OFFSET( VipsForeignLoadPoppler, scale ), + 0.001, 100000.0, 1.0 ); + } static void vips_foreign_load_poppler_init( VipsForeignLoadPoppler *poppler ) { poppler->dpi = 72.0; + poppler->scale = 1.0; } #endif /*HAVE_POPPLER*/