2010-01-11 16:08:13 +01:00
|
|
|
/* VIPS thumbnailer
|
|
|
|
*
|
2010-01-14 15:02:46 +01:00
|
|
|
* 11/1/09
|
2010-01-13 18:35:05 +01:00
|
|
|
*
|
|
|
|
* 13/1/09
|
2010-01-13 19:03:27 +01:00
|
|
|
* - decode labq and rad images
|
2010-01-14 15:02:46 +01:00
|
|
|
* - colour management
|
|
|
|
* - better handling of tiny images
|
2010-01-25 17:28:34 +01:00
|
|
|
* 25/1/10
|
|
|
|
* - added "--delete"
|
2010-02-06 11:40:41 +01:00
|
|
|
* 6/2/10
|
|
|
|
* - added "--interpolator"
|
|
|
|
* - added "--nosharpen"
|
|
|
|
* - better 'open' logic, test lazy flag now
|
2010-05-13 22:29:28 +02:00
|
|
|
* 13/5/10
|
|
|
|
* - oops hehe residual sharpen test was reversed
|
|
|
|
* - and the mask coefficients were messed up
|
2010-05-26 17:32:13 +02:00
|
|
|
* 26/5/10
|
|
|
|
* - delete failed if there was a profile
|
2010-07-04 22:05:20 +02:00
|
|
|
* 4/7/10
|
|
|
|
* - oops sharpening was turning off for integer shrinks, thanks Nicolas
|
2010-07-30 14:30:45 +02:00
|
|
|
* 30/7/10
|
|
|
|
* - use new "rd" mode rather than our own open via disc
|
2012-02-08 14:33:19 +01:00
|
|
|
* 8/2/12
|
|
|
|
* - use :seq mode for png images
|
2012-02-08 15:05:58 +01:00
|
|
|
* - shrink to a scanline cache to ensure we request pixels sequentially
|
|
|
|
* from the input
|
2012-06-13 10:40:33 +02:00
|
|
|
* 13/6/12
|
|
|
|
* - update the sequential stuff to the general method
|
2012-06-21 14:32:47 +02:00
|
|
|
* 21/6/12
|
|
|
|
* - remove "--nodelete" option, have a --delete option instead, off by
|
|
|
|
* default
|
|
|
|
* - much more gentle extra sharpening
|
2012-11-13 21:09:30 +01:00
|
|
|
* 13/11/12
|
|
|
|
* - allow absolute paths in -o (thanks fuho)
|
2013-05-03 14:56:38 +02:00
|
|
|
* 3/5/13
|
|
|
|
* - add optional sharpening mask from file
|
2013-07-10 12:05:45 +02:00
|
|
|
* 10/7/13
|
|
|
|
* - rewrite for vips8
|
|
|
|
* - handle embedded jpeg thumbnails
|
2013-11-12 10:53:41 +01:00
|
|
|
* 12/11/13
|
|
|
|
* - add --linear option
|
2013-12-18 15:23:39 +01:00
|
|
|
* 18/12/13
|
|
|
|
* - add --crop option
|
2014-03-05 12:41:16 +01:00
|
|
|
* 5/3/14
|
|
|
|
* - copy main image metadata to embedded thumbnails, thanks ottob
|
2014-03-06 23:41:56 +01:00
|
|
|
* 6/3/14
|
|
|
|
* - add --rotate flag
|
2014-03-07 17:57:20 +01:00
|
|
|
* 7/3/14
|
|
|
|
* - remove the embedded thumbnail reader, embedded thumbnails are too
|
|
|
|
* unlike the main image wrt. rotation / colour / etc.
|
2014-06-30 13:07:00 +02:00
|
|
|
* 30/6/14
|
|
|
|
* - fix interlaced thumbnail output, thanks lovell
|
2014-08-04 10:55:18 +02:00
|
|
|
* 3/8/14
|
2014-08-11 11:26:12 +02:00
|
|
|
* - box shrink less, use interpolator more, if window_size is large
|
2014-08-04 10:55:18 +02:00
|
|
|
* enough
|
2014-08-11 11:26:12 +02:00
|
|
|
* - default to bicubic if available
|
|
|
|
* - add an anti-alias filter between shrink and affine
|
2014-08-11 17:48:51 +02:00
|
|
|
* - support CMYK
|
|
|
|
* - use SEQ_UNBUF for a memory saving
|
2014-09-12 11:51:49 +02:00
|
|
|
* 12/9/14
|
|
|
|
* - try with embedded profile first, if that fails retry with fallback
|
|
|
|
* profile
|
2015-01-13 18:31:09 +01:00
|
|
|
* 13/1/15
|
|
|
|
* - exit with an error code if one or more conversions failed
|
2015-01-20 10:10:36 +01:00
|
|
|
* 20/1/15
|
|
|
|
* - rename -o as -f, keep -o as a hidden flag
|
2015-05-09 13:54:50 +02:00
|
|
|
* 9/5/15
|
|
|
|
* - use vips_resize() instead of our own code
|
2015-05-09 17:33:38 +02:00
|
|
|
* - premultiply alpha
|
2015-07-30 12:32:53 +02:00
|
|
|
* 30/7/15
|
|
|
|
* - warn if you autorot and there's no exif support
|
2016-02-09 10:26:54 +01:00
|
|
|
* 9/2/16
|
|
|
|
* - add PDF --size support
|
2016-02-09 12:01:12 +01:00
|
|
|
* - add SVG --size support
|
2016-02-28 15:36:21 +01:00
|
|
|
* 28/2/16
|
|
|
|
* - add webp --shrink support
|
2016-03-01 12:19:30 +01:00
|
|
|
* 29/2/16
|
2016-03-10 20:53:05 +01:00
|
|
|
* - deprecate sharpen and interpolate
|
2016-05-16 14:18:59 +02:00
|
|
|
* 6/5/16
|
|
|
|
* - restore BandFmt after unpremultiply
|
2016-05-23 13:18:39 +02:00
|
|
|
* 23/5/16
|
|
|
|
* - no need to guess max-alpha now premultiply does this for us
|
2016-08-01 09:43:53 +02:00
|
|
|
* 1/8/16
|
|
|
|
* - use scRGB as the working space in linear mode
|
2016-08-15 16:45:26 +02:00
|
|
|
* 15/8/16
|
|
|
|
* - can now remove 0.1 rounding adjustment
|
2017-01-06 14:43:43 +01:00
|
|
|
* 2/11/16
|
|
|
|
* - use vips_thumbnail(), most code moved there
|
|
|
|
* 6/1/17
|
|
|
|
* - fancy geometry strings
|
|
|
|
* - support VipSize restrictions
|
2017-05-04 15:54:49 +02:00
|
|
|
* 4/5/17
|
|
|
|
* - add ! geo modifier
|
2017-08-30 17:34:46 +02:00
|
|
|
* 30/8/17
|
|
|
|
* - add --intent
|
2017-10-23 23:27:23 +02:00
|
|
|
* 23/10/17
|
|
|
|
* - --size Nx didn't work, argh ... thanks jrochkind
|
2020-02-03 15:49:21 +01:00
|
|
|
* 3/2/20
|
|
|
|
* - add --no-rotate
|
|
|
|
* - add --import-profile / --export-profile names
|
|
|
|
* - back to -o for output
|
2020-02-29 16:50:01 +01:00
|
|
|
* 29/2/20
|
|
|
|
* - deprecate --delete
|
2020-10-02 17:46:03 +02:00
|
|
|
* 2/10/20
|
|
|
|
* - support "stdin" as a magic input filename for thumbnail_source
|
|
|
|
* - support ".suffix" as a magic ouput format for stdout write
|
2010-01-11 16:08:13 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include <config.h>
|
|
|
|
#endif /*HAVE_CONFIG_H*/
|
2022-02-27 16:27:28 +01:00
|
|
|
#include <glib/gi18n.h>
|
2010-01-11 16:08:13 +01:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <math.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
2010-05-25 18:46:03 +02:00
|
|
|
#include <locale.h>
|
2017-03-15 13:08:52 +01:00
|
|
|
#include <ctype.h>
|
2010-01-11 16:08:13 +01:00
|
|
|
|
|
|
|
#include <vips/vips.h>
|
2014-09-27 12:37:28 +02:00
|
|
|
#include <vips/internal.h>
|
2010-01-11 16:08:13 +01:00
|
|
|
|
2014-08-11 11:26:12 +02:00
|
|
|
/* Default settings. We change the default to bicubic in main() if
|
2014-08-04 10:55:18 +02:00
|
|
|
* this vips has been compiled with bicubic support.
|
|
|
|
*/
|
|
|
|
|
2013-10-23 10:37:45 +02:00
|
|
|
static char *thumbnail_size = "128";
|
|
|
|
static int thumbnail_width = 128;
|
|
|
|
static int thumbnail_height = 128;
|
2017-01-06 14:43:43 +01:00
|
|
|
static VipsSize size_restriction = VIPS_SIZE_BOTH;
|
2010-02-06 11:40:41 +01:00
|
|
|
static char *output_format = "tn_%s.jpg";
|
2010-01-14 15:00:12 +01:00
|
|
|
static char *export_profile = NULL;
|
|
|
|
static char *import_profile = NULL;
|
2013-11-12 10:53:41 +01:00
|
|
|
static gboolean linear_processing = FALSE;
|
2013-12-18 15:23:39 +01:00
|
|
|
static gboolean crop_image = FALSE;
|
2020-02-03 15:49:21 +01:00
|
|
|
static gboolean no_rotate_image = FALSE;
|
2017-03-08 15:31:00 +01:00
|
|
|
static char *smartcrop_image = NULL;
|
2017-08-30 17:34:46 +02:00
|
|
|
static char *thumbnail_intent = NULL;
|
2021-06-15 11:33:42 +02:00
|
|
|
static gboolean version = FALSE;
|
2010-01-11 16:08:13 +01:00
|
|
|
|
2013-05-03 14:56:38 +02:00
|
|
|
/* Deprecated and unused.
|
|
|
|
*/
|
2020-02-29 16:50:01 +01:00
|
|
|
static gboolean delete_profile = FALSE;
|
2013-05-03 14:56:38 +02:00
|
|
|
static gboolean nosharpen = FALSE;
|
|
|
|
static gboolean nodelete_profile = FALSE;
|
2013-11-09 14:10:11 +01:00
|
|
|
static gboolean verbose = FALSE;
|
2016-03-10 20:53:05 +01:00
|
|
|
static char *convolution_mask = NULL;
|
|
|
|
static char *interpolator = NULL;
|
2020-02-03 15:49:21 +01:00
|
|
|
static gboolean rotate_image = FALSE;
|
2013-05-03 14:56:38 +02:00
|
|
|
|
2010-01-11 16:08:13 +01:00
|
|
|
static GOptionEntry options[] = {
|
2012-07-20 14:34:05 +02:00
|
|
|
{ "size", 's', 0,
|
2013-10-23 10:37:45 +02:00
|
|
|
G_OPTION_ARG_STRING, &thumbnail_size,
|
|
|
|
N_( "shrink to SIZE or to WIDTHxHEIGHT" ),
|
2010-05-26 13:26:09 +02:00
|
|
|
N_( "SIZE" ) },
|
2020-02-03 15:49:21 +01:00
|
|
|
{ "output", 'o', 0,
|
2012-07-20 14:34:05 +02:00
|
|
|
G_OPTION_ARG_STRING, &output_format,
|
2020-02-03 15:49:21 +01:00
|
|
|
N_( "output to FORMAT" ),
|
2015-01-20 10:10:36 +01:00
|
|
|
N_( "FORMAT" ) },
|
2020-02-03 15:49:21 +01:00
|
|
|
{ "export-profile", 'e', 0,
|
2017-03-08 15:31:00 +01:00
|
|
|
G_OPTION_ARG_FILENAME, &export_profile,
|
2010-05-26 13:26:09 +02:00
|
|
|
N_( "export with PROFILE" ),
|
|
|
|
N_( "PROFILE" ) },
|
2020-02-03 15:49:21 +01:00
|
|
|
{ "import-profile", 'i', 0,
|
2017-03-08 15:31:00 +01:00
|
|
|
G_OPTION_ARG_FILENAME, &import_profile,
|
2010-05-26 13:26:09 +02:00
|
|
|
N_( "import untagged images with PROFILE" ),
|
|
|
|
N_( "PROFILE" ) },
|
2013-11-12 10:53:41 +01:00
|
|
|
{ "linear", 'a', 0,
|
|
|
|
G_OPTION_ARG_NONE, &linear_processing,
|
|
|
|
N_( "process in linear space" ), NULL },
|
2017-03-31 14:26:25 +02:00
|
|
|
{ "smartcrop", 'm', 0,
|
2017-03-08 15:31:00 +01:00
|
|
|
G_OPTION_ARG_STRING, &smartcrop_image,
|
2017-03-31 14:26:25 +02:00
|
|
|
N_( "shrink and crop to fill SIZE using STRATEGY" ),
|
|
|
|
N_( "STRATEGY" ) },
|
2017-08-30 17:34:46 +02:00
|
|
|
{ "intent", 'n', 0,
|
|
|
|
G_OPTION_ARG_STRING, &thumbnail_intent,
|
|
|
|
N_( "ICC transform with INTENT" ),
|
|
|
|
N_( "INTENT" ) },
|
2020-02-29 16:50:01 +01:00
|
|
|
{ "delete", 'd', G_OPTION_FLAG_HIDDEN,
|
2012-07-20 14:34:05 +02:00
|
|
|
G_OPTION_ARG_NONE, &delete_profile,
|
2020-02-29 16:50:01 +01:00
|
|
|
N_( "(deprecated, does nothing)" ), NULL },
|
2020-02-03 15:49:21 +01:00
|
|
|
{ "no-rotate", 0, 0,
|
|
|
|
G_OPTION_ARG_NONE, &no_rotate_image,
|
|
|
|
N_( "don't auto-rotate" ), NULL },
|
2021-06-15 11:33:42 +02:00
|
|
|
{ "version", 'v', 0, G_OPTION_ARG_NONE, &version,
|
|
|
|
N_( "print version" ), NULL },
|
2016-03-10 20:53:05 +01:00
|
|
|
|
2020-02-03 15:49:21 +01:00
|
|
|
{ "format", 'f', G_OPTION_FLAG_HIDDEN,
|
|
|
|
G_OPTION_ARG_STRING, &output_format,
|
|
|
|
N_( "set output format string to FORMAT" ),
|
|
|
|
N_( "FORMAT" ) },
|
|
|
|
{ "eprofile", 0, G_OPTION_FLAG_HIDDEN,
|
|
|
|
G_OPTION_ARG_FILENAME, &export_profile,
|
|
|
|
N_( "export with PROFILE" ),
|
|
|
|
N_( "PROFILE" ) },
|
|
|
|
{ "iprofile", 0, G_OPTION_FLAG_HIDDEN,
|
|
|
|
G_OPTION_ARG_FILENAME, &import_profile,
|
|
|
|
N_( "import untagged images with PROFILE" ),
|
|
|
|
N_( "PROFILE" ) },
|
|
|
|
{ "rotate", 't', G_OPTION_FLAG_HIDDEN,
|
|
|
|
G_OPTION_ARG_NONE, &rotate_image,
|
|
|
|
N_( "(deprecated, does nothing)" ), NULL },
|
2017-03-08 15:31:00 +01:00
|
|
|
{ "crop", 'c', G_OPTION_FLAG_HIDDEN,
|
|
|
|
G_OPTION_ARG_NONE, &crop_image,
|
|
|
|
N_( "(deprecated, crop exactly to SIZE)" ), NULL },
|
2013-08-07 10:57:18 +02:00
|
|
|
{ "verbose", 'v', G_OPTION_FLAG_HIDDEN,
|
2013-11-09 14:10:11 +01:00
|
|
|
G_OPTION_ARG_NONE, &verbose,
|
2013-08-07 10:57:18 +02:00
|
|
|
N_( "(deprecated, does nothing)" ), NULL },
|
2013-05-03 14:56:38 +02:00
|
|
|
{ "nodelete", 'l', G_OPTION_FLAG_HIDDEN,
|
|
|
|
G_OPTION_ARG_NONE, &nodelete_profile,
|
|
|
|
N_( "(deprecated, does nothing)" ), NULL },
|
|
|
|
{ "nosharpen", 'n', G_OPTION_FLAG_HIDDEN,
|
|
|
|
G_OPTION_ARG_NONE, &nosharpen,
|
|
|
|
N_( "(deprecated, does nothing)" ), NULL },
|
2016-03-10 20:53:05 +01:00
|
|
|
{ "interpolator", 'p', G_OPTION_FLAG_HIDDEN,
|
|
|
|
G_OPTION_ARG_STRING, &interpolator,
|
|
|
|
N_( "(deprecated, does nothing)" ), NULL },
|
|
|
|
{ "sharpen", 'r', G_OPTION_FLAG_HIDDEN,
|
|
|
|
G_OPTION_ARG_STRING, &convolution_mask,
|
|
|
|
N_( "(deprecated, does nothing)" ), NULL },
|
2010-01-11 16:08:13 +01:00
|
|
|
{ NULL }
|
|
|
|
};
|
|
|
|
|
2013-07-10 12:05:45 +02:00
|
|
|
/* Given (eg.) "/poop/somefile.png", write @im to the thumbnail name,
|
|
|
|
* (eg.) "/poop/tn_somefile.jpg".
|
2020-10-02 17:46:03 +02:00
|
|
|
*
|
|
|
|
* If
|
2010-01-11 16:08:13 +01:00
|
|
|
*/
|
2013-07-10 12:05:45 +02:00
|
|
|
static int
|
2020-10-02 17:46:03 +02:00
|
|
|
thumbnail_write_file( VipsObject *process, VipsImage *im, const char *filename )
|
2010-01-11 16:08:13 +01:00
|
|
|
{
|
|
|
|
char *file;
|
|
|
|
char *p;
|
|
|
|
char buf[FILENAME_MAX];
|
2013-07-10 12:05:45 +02:00
|
|
|
char *output_name;
|
2010-01-11 16:08:13 +01:00
|
|
|
|
|
|
|
file = g_path_get_basename( filename );
|
|
|
|
|
2012-11-13 21:09:30 +01:00
|
|
|
/* Remove the suffix from the file portion.
|
|
|
|
*/
|
2010-01-11 16:08:13 +01:00
|
|
|
if( (p = strrchr( file, '.' )) )
|
|
|
|
*p = '\0';
|
|
|
|
|
2014-09-27 12:37:28 +02:00
|
|
|
/* Don't use vips_snprintf(), we only want to optionally substitute a
|
|
|
|
* single %s.
|
|
|
|
*/
|
|
|
|
vips_strncpy( buf, output_format, FILENAME_MAX );
|
|
|
|
vips__substitute( buf, FILENAME_MAX, file );
|
|
|
|
|
2012-11-13 21:09:30 +01:00
|
|
|
/* output_format can be an absolute path, in which case we discard the
|
|
|
|
* path from the incoming file.
|
|
|
|
*/
|
|
|
|
if( g_path_is_absolute( output_format ) )
|
2013-07-10 12:05:45 +02:00
|
|
|
output_name = g_strdup( buf );
|
2012-11-13 21:09:30 +01:00
|
|
|
else {
|
|
|
|
char *dir;
|
|
|
|
|
|
|
|
dir = g_path_get_dirname( filename );
|
2013-07-10 12:05:45 +02:00
|
|
|
output_name = g_build_filename( dir, buf, NULL );
|
2012-11-13 21:09:30 +01:00
|
|
|
g_free( dir );
|
|
|
|
}
|
2010-01-11 16:08:13 +01:00
|
|
|
|
2017-01-03 16:52:27 +01:00
|
|
|
g_info( "thumbnailing %s as %s", filename, output_name );
|
2010-01-11 16:08:13 +01:00
|
|
|
|
|
|
|
g_free( file );
|
|
|
|
|
2014-08-03 18:59:25 +02:00
|
|
|
if( vips_image_write_to_file( im, output_name, NULL ) ) {
|
2013-07-10 12:05:45 +02:00
|
|
|
g_free( output_name );
|
2010-01-11 16:08:13 +01:00
|
|
|
return( -1 );
|
|
|
|
}
|
2013-07-10 12:05:45 +02:00
|
|
|
g_free( output_name );
|
2010-01-11 16:08:13 +01:00
|
|
|
|
2013-07-10 12:05:45 +02:00
|
|
|
return( 0 );
|
2010-01-11 16:08:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2020-10-02 17:46:03 +02:00
|
|
|
thumbnail_process( VipsObject *process, const char *name )
|
2010-01-11 16:08:13 +01:00
|
|
|
{
|
2017-03-08 15:31:00 +01:00
|
|
|
VipsInteresting interesting;
|
2017-01-06 14:43:43 +01:00
|
|
|
VipsImage *image;
|
2017-08-30 17:34:46 +02:00
|
|
|
VipsIntent intent;
|
2020-10-02 17:46:03 +02:00
|
|
|
char filename[VIPS_PATH_MAX];
|
|
|
|
char option_string[VIPS_PATH_MAX];
|
2013-11-13 10:00:47 +01:00
|
|
|
|
2017-03-08 15:31:00 +01:00
|
|
|
interesting = VIPS_INTERESTING_NONE;
|
|
|
|
if( crop_image )
|
|
|
|
interesting = VIPS_INTERESTING_CENTRE;
|
2017-03-25 17:10:30 +01:00
|
|
|
if( smartcrop_image ) {
|
|
|
|
int n;
|
2017-08-30 17:34:46 +02:00
|
|
|
|
2017-03-25 17:10:30 +01:00
|
|
|
if( (n = vips_enum_from_nick( "vipsthumbnail",
|
2017-03-08 15:31:00 +01:00
|
|
|
VIPS_TYPE_INTERESTING, smartcrop_image )) < 0 )
|
2017-03-25 17:10:30 +01:00
|
|
|
return( -1 );
|
|
|
|
interesting = n;
|
|
|
|
}
|
2020-10-02 17:46:03 +02:00
|
|
|
|
2017-08-30 17:34:46 +02:00
|
|
|
intent = VIPS_INTENT_RELATIVE;
|
|
|
|
if( thumbnail_intent ) {
|
|
|
|
int n;
|
|
|
|
|
|
|
|
if( (n = vips_enum_from_nick( "vipsthumbnail",
|
|
|
|
VIPS_TYPE_INTENT, thumbnail_intent )) < 0 )
|
|
|
|
return( -1 );
|
|
|
|
intent = n;
|
|
|
|
}
|
2017-03-08 15:31:00 +01:00
|
|
|
|
2020-10-02 17:46:03 +02:00
|
|
|
vips__filename_split8( name, filename, option_string );
|
|
|
|
if( strcmp( filename, "stdin" ) == 0 ) {
|
|
|
|
VipsSource *source;
|
2010-01-11 16:08:13 +01:00
|
|
|
|
2020-10-02 17:46:03 +02:00
|
|
|
if( !(source =
|
|
|
|
vips_source_new_from_descriptor( 0 )) )
|
|
|
|
return( -1 );
|
|
|
|
|
|
|
|
if( vips_thumbnail_source( source, &image, thumbnail_width,
|
|
|
|
"option-string", option_string,
|
|
|
|
"height", thumbnail_height,
|
|
|
|
"size", size_restriction,
|
|
|
|
"no-rotate", no_rotate_image,
|
|
|
|
"crop", interesting,
|
|
|
|
"linear", linear_processing,
|
|
|
|
"import-profile", import_profile,
|
|
|
|
"export-profile", export_profile,
|
|
|
|
"intent", intent,
|
|
|
|
NULL ) ) {
|
|
|
|
VIPS_UNREF( source );
|
|
|
|
return( -1 );
|
|
|
|
}
|
|
|
|
VIPS_UNREF( source );
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if( vips_thumbnail( name, &image, thumbnail_width,
|
|
|
|
"height", thumbnail_height,
|
|
|
|
"size", size_restriction,
|
|
|
|
"no-rotate", no_rotate_image,
|
|
|
|
"crop", interesting,
|
|
|
|
"linear", linear_processing,
|
|
|
|
"import-profile", import_profile,
|
|
|
|
"export-profile", export_profile,
|
|
|
|
"intent", intent,
|
|
|
|
NULL ) )
|
|
|
|
return( -1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If the output format is something like ".jpg", we write to stdout
|
|
|
|
* instead.
|
|
|
|
*
|
|
|
|
* (but allow "./%s.jpg" as a output format)
|
|
|
|
*/
|
|
|
|
if( vips_isprefix( ".", output_format ) &&
|
|
|
|
!vips_isprefix( "./", output_format ) ) {
|
|
|
|
VipsTarget *target;
|
|
|
|
|
|
|
|
if( !(target = vips_target_new_to_descriptor( 1 )) )
|
|
|
|
return( -1 );
|
|
|
|
|
|
|
|
if( vips_image_write_to_target( image,
|
|
|
|
output_format, target, NULL ) ) {
|
|
|
|
VIPS_UNREF( image );
|
|
|
|
VIPS_UNREF( target );
|
|
|
|
return( -1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
VIPS_UNREF( target );
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if( thumbnail_write_file( process, image, name ) ) {
|
|
|
|
VIPS_UNREF( image );
|
|
|
|
return( -1 );
|
|
|
|
}
|
2016-11-02 15:24:18 +01:00
|
|
|
}
|
|
|
|
|
2020-10-02 17:46:03 +02:00
|
|
|
VIPS_UNREF( image );
|
2016-11-02 15:24:18 +01:00
|
|
|
|
2013-07-10 12:05:45 +02:00
|
|
|
return( 0 );
|
2010-01-11 16:08:13 +01:00
|
|
|
}
|
|
|
|
|
2017-01-05 16:02:29 +01:00
|
|
|
/* Parse a geometry string and set thumbnail_width and thumbnail_height.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
thumbnail_parse_geometry( const char *geometry )
|
|
|
|
{
|
2017-03-15 13:08:52 +01:00
|
|
|
/* Geometry strings have a regex like:
|
|
|
|
*
|
|
|
|
* ^(\\d+)? (x)? (\\d+)? ([<>])?$
|
|
|
|
*
|
|
|
|
* Sadly GRegex is 2.14 and later, and we need to work with 2.6.
|
|
|
|
*/
|
|
|
|
|
|
|
|
const char *p;
|
2017-10-23 23:27:23 +02:00
|
|
|
gboolean had_x;
|
2017-03-15 13:08:52 +01:00
|
|
|
|
|
|
|
/* w or h missing means replace with a huuuge value to prevent
|
|
|
|
* reduction or enlargement in that axis.
|
|
|
|
*/
|
|
|
|
thumbnail_width = VIPS_MAX_COORD;
|
|
|
|
thumbnail_height = VIPS_MAX_COORD;
|
|
|
|
|
|
|
|
p = geometry;
|
|
|
|
|
|
|
|
/* Get the width.
|
|
|
|
*/
|
|
|
|
while( isspace( *p ) )
|
|
|
|
p++;
|
|
|
|
if( isdigit ( *p ) ) {
|
2017-10-23 23:27:23 +02:00
|
|
|
thumbnail_width = atoi( p );
|
2017-03-15 13:08:52 +01:00
|
|
|
|
|
|
|
while( isdigit( *p ) )
|
|
|
|
p++;
|
2017-01-06 14:43:43 +01:00
|
|
|
}
|
2017-01-05 16:02:29 +01:00
|
|
|
|
2017-03-15 13:08:52 +01:00
|
|
|
/* Get the optional 'x'.
|
|
|
|
*/
|
|
|
|
while( isspace( *p ) )
|
|
|
|
p++;
|
2017-10-23 23:27:23 +02:00
|
|
|
had_x = FALSE;
|
|
|
|
if( *p == 'x' ) {
|
2017-03-15 13:08:52 +01:00
|
|
|
p += 1;
|
2017-10-23 23:27:23 +02:00
|
|
|
had_x = TRUE;
|
|
|
|
}
|
2017-03-15 13:08:52 +01:00
|
|
|
while( isspace( *p ) )
|
|
|
|
p++;
|
|
|
|
|
|
|
|
/* Get the height.
|
|
|
|
*/
|
|
|
|
if( isdigit( *p ) ) {
|
|
|
|
thumbnail_height = atoi( p );
|
2017-01-05 16:02:29 +01:00
|
|
|
|
2017-03-15 13:08:52 +01:00
|
|
|
while( isdigit( *p ) )
|
|
|
|
p++;
|
|
|
|
}
|
2017-01-05 16:02:29 +01:00
|
|
|
|
2017-05-04 15:54:49 +02:00
|
|
|
/* Get the final <>!
|
2017-03-15 13:08:52 +01:00
|
|
|
*/
|
|
|
|
while( isspace( *p ) )
|
|
|
|
p++;
|
|
|
|
if( *p == '<' )
|
|
|
|
size_restriction = VIPS_SIZE_UP;
|
|
|
|
else if( *p == '>' )
|
|
|
|
size_restriction = VIPS_SIZE_DOWN;
|
2017-05-04 15:54:49 +02:00
|
|
|
else if( *p == '!' )
|
|
|
|
size_restriction = VIPS_SIZE_FORCE;
|
2017-03-15 13:08:52 +01:00
|
|
|
else if( *p != '\0' ||
|
|
|
|
(thumbnail_width == VIPS_MAX_COORD &&
|
|
|
|
thumbnail_height == VIPS_MAX_COORD) ) {
|
|
|
|
vips_error( "thumbnail", "%s", _( "bad geometry spec" ) );
|
|
|
|
return( -1 );
|
|
|
|
}
|
|
|
|
|
2017-10-23 23:27:23 +02:00
|
|
|
/* If there was no 'x' we have just width. vipsthumbnail history means
|
|
|
|
* this is a square bounding box.
|
|
|
|
*/
|
|
|
|
if( !had_x )
|
|
|
|
thumbnail_height = thumbnail_width;
|
|
|
|
|
2017-05-04 15:54:49 +02:00
|
|
|
/* If force is set and one of width or height isn't set, copy from the
|
|
|
|
* one that is.
|
2017-03-15 13:08:52 +01:00
|
|
|
*/
|
2017-05-04 15:54:49 +02:00
|
|
|
if( size_restriction == VIPS_SIZE_FORCE ) {
|
|
|
|
if( thumbnail_width == VIPS_MAX_COORD )
|
|
|
|
thumbnail_width = thumbnail_height;
|
|
|
|
if( thumbnail_height == VIPS_MAX_COORD )
|
|
|
|
thumbnail_height = thumbnail_width;
|
2017-01-05 16:02:29 +01:00
|
|
|
}
|
|
|
|
|
2017-05-04 15:54:49 +02:00
|
|
|
/* If --crop is set or force is set, both width and height must be
|
|
|
|
* specified, since we'll need a complete bounding box to fill.
|
|
|
|
*/
|
|
|
|
if( crop_image ||
|
|
|
|
smartcrop_image ||
|
|
|
|
size_restriction == VIPS_SIZE_FORCE )
|
|
|
|
if( thumbnail_width == VIPS_MAX_COORD ||
|
|
|
|
thumbnail_height == VIPS_MAX_COORD ) {
|
|
|
|
vips_error( "thumbnail",
|
|
|
|
"both width and height must be given if "
|
|
|
|
"crop is enabled" );
|
|
|
|
return( -1 );
|
|
|
|
}
|
|
|
|
|
2017-01-06 14:43:43 +01:00
|
|
|
return( 0 );
|
2017-01-05 16:02:29 +01:00
|
|
|
}
|
|
|
|
|
2010-01-11 16:08:13 +01:00
|
|
|
int
|
|
|
|
main( int argc, char **argv )
|
|
|
|
{
|
|
|
|
GOptionContext *context;
|
2014-11-18 12:29:32 +01:00
|
|
|
GOptionGroup *main_group;
|
2010-01-11 16:08:13 +01:00
|
|
|
GError *error = NULL;
|
|
|
|
int i;
|
2015-01-13 18:31:09 +01:00
|
|
|
int result;
|
2010-01-11 16:08:13 +01:00
|
|
|
|
2014-11-18 12:29:32 +01:00
|
|
|
if( VIPS_INIT( argv[0] ) )
|
2013-07-10 12:05:45 +02:00
|
|
|
vips_error_exit( "unable to start VIPS" );
|
2022-02-27 16:27:28 +01:00
|
|
|
|
|
|
|
#ifdef ENABLE_NLS
|
2010-05-25 18:46:03 +02:00
|
|
|
textdomain( GETTEXT_PACKAGE );
|
2022-02-27 16:27:28 +01:00
|
|
|
#endif /* ENABLE_NLS */
|
2010-05-25 18:46:03 +02:00
|
|
|
setlocale( LC_ALL, "" );
|
2010-01-11 16:08:13 +01:00
|
|
|
|
2021-06-22 16:28:21 +02:00
|
|
|
{
|
|
|
|
char *basename;
|
|
|
|
|
|
|
|
basename = g_path_get_basename( argv[0] );
|
|
|
|
g_set_prgname( basename );
|
|
|
|
g_free( basename );
|
|
|
|
}
|
2017-01-10 15:12:24 +01:00
|
|
|
|
2016-08-16 17:22:38 +02:00
|
|
|
/* On Windows, argv is ascii-only .. use this to get a utf-8 version of
|
|
|
|
* the args.
|
|
|
|
*/
|
2021-03-06 13:47:48 +01:00
|
|
|
#ifdef G_OS_WIN32
|
2016-08-16 17:22:38 +02:00
|
|
|
argv = g_win32_get_command_line();
|
2021-03-06 13:47:48 +01:00
|
|
|
#endif /*G_OS_WIN32*/
|
2016-08-16 17:22:38 +02:00
|
|
|
|
2010-01-11 16:08:13 +01:00
|
|
|
context = g_option_context_new( _( "- thumbnail generator" ) );
|
|
|
|
|
2014-11-18 12:29:32 +01:00
|
|
|
main_group = g_option_group_new( NULL, NULL, NULL, NULL, NULL );
|
|
|
|
g_option_group_add_entries( main_group, options );
|
|
|
|
vips_add_option_entries( main_group );
|
|
|
|
g_option_group_set_translation_domain( main_group, GETTEXT_PACKAGE );
|
|
|
|
g_option_context_set_main_group( context, main_group );
|
2010-01-11 16:08:13 +01:00
|
|
|
|
2021-03-06 13:47:48 +01:00
|
|
|
#ifdef G_OS_WIN32
|
2016-08-17 13:14:43 +02:00
|
|
|
if( !g_option_context_parse_strv( context, &argv, &error ) )
|
2021-03-06 13:47:48 +01:00
|
|
|
#else /*!G_OS_WIN32*/
|
2016-08-17 13:14:43 +02:00
|
|
|
if( !g_option_context_parse( context, &argc, &argv, &error ) )
|
2021-03-06 13:47:48 +01:00
|
|
|
#endif /*G_OS_WIN32*/
|
2016-08-17 13:14:43 +02:00
|
|
|
{
|
2010-01-11 16:08:13 +01:00
|
|
|
if( error ) {
|
|
|
|
fprintf( stderr, "%s\n", error->message );
|
|
|
|
g_error_free( error );
|
|
|
|
}
|
|
|
|
|
2013-07-10 12:05:45 +02:00
|
|
|
vips_error_exit( "try \"%s --help\"", g_get_prgname() );
|
2010-01-11 16:08:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
g_option_context_free( context );
|
|
|
|
|
2021-06-15 11:33:42 +02:00
|
|
|
if( version )
|
|
|
|
printf( "vips-%s\n", vips_version_string() );
|
|
|
|
|
2017-01-06 15:01:55 +01:00
|
|
|
if( thumbnail_size &&
|
|
|
|
thumbnail_parse_geometry( thumbnail_size ) )
|
2017-01-06 14:43:43 +01:00
|
|
|
vips_error_exit( NULL );
|
2017-01-05 16:02:29 +01:00
|
|
|
|
2015-07-30 12:32:53 +02:00
|
|
|
#ifndef HAVE_EXIF
|
2017-01-06 14:43:43 +01:00
|
|
|
if( rotate_image )
|
2017-01-03 16:52:27 +01:00
|
|
|
g_warning( "%s",
|
2015-07-30 12:32:53 +02:00
|
|
|
_( "auto-rotate disabled: "
|
|
|
|
"libvips built without exif support" ) );
|
|
|
|
#endif /*!HAVE_EXIF*/
|
|
|
|
|
2015-01-13 18:31:09 +01:00
|
|
|
result = 0;
|
|
|
|
|
2016-08-17 13:14:43 +02:00
|
|
|
for( i = 1; argv[i]; i++ ) {
|
2013-12-18 15:23:39 +01:00
|
|
|
/* Hang resources for processing this thumbnail off @process.
|
2013-07-10 12:05:45 +02:00
|
|
|
*/
|
2013-11-13 10:00:47 +01:00
|
|
|
VipsObject *process = VIPS_OBJECT( vips_image_new() );
|
2013-07-10 12:05:45 +02:00
|
|
|
|
2013-11-13 10:00:47 +01:00
|
|
|
if( thumbnail_process( process, argv[i] ) ) {
|
2010-01-11 16:08:13 +01:00
|
|
|
fprintf( stderr, "%s: unable to thumbnail %s\n",
|
|
|
|
argv[0], argv[i] );
|
2013-07-10 12:05:45 +02:00
|
|
|
fprintf( stderr, "%s", vips_error_buffer() );
|
|
|
|
vips_error_clear();
|
2015-01-13 18:31:09 +01:00
|
|
|
|
|
|
|
/* We had a conversion failure: return an error code
|
|
|
|
* when we finally exit.
|
|
|
|
*/
|
|
|
|
result = -1;
|
2010-01-11 16:08:13 +01:00
|
|
|
}
|
|
|
|
|
2013-11-13 10:00:47 +01:00
|
|
|
g_object_unref( process );
|
2013-07-10 12:05:45 +02:00
|
|
|
}
|
|
|
|
|
2016-08-16 17:22:38 +02:00
|
|
|
/* We don't free this on error exit, sadly.
|
|
|
|
*/
|
2021-03-06 13:47:48 +01:00
|
|
|
#ifdef G_OS_WIN32
|
2016-08-16 17:22:38 +02:00
|
|
|
g_strfreev( argv );
|
2021-03-06 13:47:48 +01:00
|
|
|
#endif /*G_OS_WIN32*/
|
2016-08-16 17:22:38 +02:00
|
|
|
|
2011-09-24 11:17:32 +02:00
|
|
|
vips_shutdown();
|
|
|
|
|
2015-01-13 18:31:09 +01:00
|
|
|
return( result );
|
2010-01-11 16:08:13 +01:00
|
|
|
}
|