From 6a154ac9f2d4fa74991f4ee62f914484e03852eb Mon Sep 17 00:00:00 2001 From: John Cupitt Date: Tue, 30 Aug 2022 12:31:47 +0100 Subject: [PATCH] add ".pnm" save see https://github.com/libvips/libvips/issues/3016 --- ChangeLog | 1 + libvips/foreign/pforeign.h | 1 + libvips/foreign/ppmload.c | 5 ++++- libvips/foreign/ppmsave.c | 40 +++++++++++++++++++++++++++++++++- libvips/include/vips/foreign.h | 6 +++++ 5 files changed, 51 insertions(+), 2 deletions(-) diff --git a/ChangeLog b/ChangeLog index a286b952..92b10840 100644 --- a/ChangeLog +++ b/ChangeLog @@ -10,6 +10,7 @@ master - require libjxl 0.7+ - add "interlace" option to GIF save [dloebl] - magick load sets "magick-format" metadata [aksdb] +- add ".pnm", save as image format [ewelot] 24/7/22 started 8.13.1 - fix im7 feature detection in meson diff --git a/libvips/foreign/pforeign.h b/libvips/foreign/pforeign.h index e8cec087..162c5bb5 100644 --- a/libvips/foreign/pforeign.h +++ b/libvips/foreign/pforeign.h @@ -131,6 +131,7 @@ extern const char *vips__save_pbm_suffs[]; extern const char *vips__save_pgm_suffs[]; extern const char *vips__save_ppm_suffs[]; extern const char *vips__save_pfm_suffs[]; +extern const char *vips__save_pnm_suffs[]; int vips__ppm_save_target( VipsImage *in, VipsTarget *target, gboolean ascii, gboolean squash ); diff --git a/libvips/foreign/ppmload.c b/libvips/foreign/ppmload.c index 7114cc15..5907cd1d 100644 --- a/libvips/foreign/ppmload.c +++ b/libvips/foreign/ppmload.c @@ -141,11 +141,14 @@ static char *magic_names[] = { /* Shared with ppmsave. */ -const char *vips__ppm_suffs[] = { ".pbm", ".pgm", ".ppm", ".pfm", NULL }; +const char *vips__ppm_suffs[] = { + ".pbm", ".pgm", ".ppm", ".pfm", ".pnm", NULL +}; const char *vips__save_pbm_suffs[] = { ".pbm", NULL }; const char *vips__save_pgm_suffs[] = { ".pgm", NULL }; const char *vips__save_ppm_suffs[] = { ".ppm", NULL }; const char *vips__save_pfm_suffs[] = { ".pfm", NULL }; +const char *vips__save_pnm_suffs[] = { ".pnm", NULL }; static gboolean vips_foreign_load_ppm_is_a_source( VipsSource *source ) diff --git a/libvips/foreign/ppmsave.c b/libvips/foreign/ppmsave.c index 37230307..3812abde 100644 --- a/libvips/foreign/ppmsave.c +++ b/libvips/foreign/ppmsave.c @@ -14,6 +14,8 @@ * - don't add date with @strip [ewelot] * 28/10/21 * - add @format, default type by filename + * 30/8/22 + * - add ".pnm", save as image format [ewelot] */ /* @@ -239,6 +241,7 @@ vips_foreign_save_ppm_build( VipsObject *object ) * pgm ... 1 band many bit * ppm ... 3 band many bit * pfm ... 1 or 3 bands, 32 bit + * pnm ... pick from input */ switch( ppm->format ) { case VIPS_FOREIGN_PPM_FORMAT_PBM: @@ -265,8 +268,9 @@ vips_foreign_save_ppm_build( VipsObject *object ) target_format = VIPS_FORMAT_FLOAT; break; + case VIPS_FOREIGN_PPM_FORMAT_PNM: default: - /* Harmless. + /* Just use the input format and interpretation. */ break; } @@ -527,6 +531,8 @@ vips_foreign_save_ppm_file_build( VipsObject *object ) ppm->format = VIPS_FOREIGN_PPM_FORMAT_PGM; else if( vips_iscasepostfix( file->filename, ".pfm" ) ) ppm->format = VIPS_FOREIGN_PPM_FORMAT_PFM; + else if( vips_iscasepostfix( file->filename, ".pnm" ) ) + ppm->format = VIPS_FOREIGN_PPM_FORMAT_PNM; return( VIPS_OBJECT_CLASS( vips_foreign_save_ppm_file_parent_class )-> build( object ) ); @@ -716,6 +722,38 @@ vips_foreign_save_pfm_target_init( VipsForeignSavePfmTarget *target ) ppm->format = VIPS_FOREIGN_PPM_FORMAT_PFM; } +typedef VipsForeignSavePpmTarget VipsForeignSavePnmTarget; +typedef VipsForeignSavePpmTargetClass VipsForeignSavePnmTargetClass; + +G_DEFINE_TYPE( VipsForeignSavePnmTarget, vips_foreign_save_pnm_target, + vips_foreign_save_ppm_target_get_type() ); + +static void +vips_foreign_save_pnm_target_class_init( + VipsForeignSavePfmTargetClass *class ) +{ + VipsObjectClass *object_class = (VipsObjectClass *) class; + VipsForeignClass *foreign_class = (VipsForeignClass *) class; + VipsOperationClass *operation_class = (VipsOperationClass *) class; + + object_class->nickname = "pnmsave_target"; + object_class->description = _( "save image in pnm format" ); + + foreign_class->suffs = vips__save_pnm_suffs; + + /* Hide from UI. + */ + operation_class->flags |= VIPS_OPERATION_DEPRECATED; +} + +static void +vips_foreign_save_pnm_target_init( VipsForeignSavePfmTarget *target ) +{ + VipsForeignSavePpm *ppm = (VipsForeignSavePpm *) target; + + ppm->format = VIPS_FOREIGN_PPM_FORMAT_PNM; +} + #endif /*HAVE_PPM*/ /** diff --git a/libvips/include/vips/foreign.h b/libvips/include/vips/foreign.h index dba1bd13..5c16076d 100644 --- a/libvips/include/vips/foreign.h +++ b/libvips/include/vips/foreign.h @@ -715,6 +715,7 @@ int vips_pngsave_buffer( VipsImage *in, void **buf, size_t *len, ... ) * @VIPS_FOREIGN_PPM_FORMAT_PGM: portable greymap * @VIPS_FOREIGN_PPM_FORMAT_PPM: portable pixmap * @VIPS_FOREIGN_PPM_FORMAT_PFM: portable float map + * @VIPS_FOREIGN_PPM_FORMAT_PNM: portable anymap * * The netpbm file format to save as. * @@ -725,12 +726,17 @@ int vips_pngsave_buffer( VipsImage *in, void **buf, size_t *len, ... ) * #VIPS_FOREIGN_PPM_FORMAT_PPM images are 8, 16, or 32-bits, three bands. * * #VIPS_FOREIGN_PPM_FORMAT_PFM images are 32-bit float pixels. + * + * #VIPS_FOREIGN_PPM_FORMAT_PNM images are anymap images -- the image format + * is used to pick the saver. + * */ typedef enum { VIPS_FOREIGN_PPM_FORMAT_PBM, VIPS_FOREIGN_PPM_FORMAT_PGM, VIPS_FOREIGN_PPM_FORMAT_PPM, VIPS_FOREIGN_PPM_FORMAT_PFM, + VIPS_FOREIGN_PPM_FORMAT_PNM, VIPS_FOREIGN_PPM_FORMAT_LAST } VipsForeignPpmFormat;