libvips/tools/vipsthumbnail.c

440 lines
10 KiB
C
Raw Normal View History

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
* 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
* 8/2/12
* - use :seq mode for png images
* - shrink to a scanline cache to ensure we request pixels sequentially
* from the input
2010-01-11 16:08:13 +01:00
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif /*HAVE_CONFIG_H*/
#include <vips/intl.h>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
2010-05-25 18:46:03 +02:00
#include <locale.h>
2010-01-11 16:08:13 +01:00
#include <vips/vips.h>
static int thumbnail_size = 128;
2010-02-06 11:40:41 +01:00
static char *output_format = "tn_%s.jpg";
2010-02-05 15:34:38 +01:00
static char *interpolator = "bilinear";;
static gboolean nosharpen = FALSE;
static char *export_profile = NULL;
static char *import_profile = NULL;
2010-02-06 11:40:41 +01:00
static gboolean nodelete_profile = FALSE;
2010-02-05 15:34:38 +01:00
static gboolean verbose = FALSE;
2010-01-11 16:08:13 +01:00
static GOptionEntry options[] = {
{ "size", 's', 0, G_OPTION_ARG_INT, &thumbnail_size,
2010-05-26 13:26:09 +02:00
N_( "set thumbnail size to SIZE" ),
N_( "SIZE" ) },
2010-02-06 11:40:41 +01:00
{ "output", 'o', 0, G_OPTION_ARG_STRING, &output_format,
2010-05-26 13:26:09 +02:00
N_( "set output to FORMAT" ),
N_( "FORMAT" ) },
2010-02-05 15:34:38 +01:00
{ "interpolator", 'p', 0, G_OPTION_ARG_STRING, &interpolator,
2010-05-26 13:26:09 +02:00
N_( "resample with INTERPOLATOR" ),
N_( "INTERPOLATOR" ) },
2010-02-05 15:34:38 +01:00
{ "nosharpen", 'n', 0, G_OPTION_ARG_NONE, &nosharpen,
N_( "don't sharpen thumbnail" ), NULL },
{ "eprofile", 'e', 0, G_OPTION_ARG_STRING, &export_profile,
2010-05-26 13:26:09 +02:00
N_( "export with PROFILE" ),
N_( "PROFILE" ) },
{ "iprofile", 'i', 0, G_OPTION_ARG_STRING, &import_profile,
2010-05-26 13:26:09 +02:00
N_( "import untagged images with PROFILE" ),
N_( "PROFILE" ) },
2010-02-06 11:40:41 +01:00
{ "nodelete", 'l', 0, G_OPTION_ARG_NONE, &nodelete_profile,
N_( "don't delete profile from exported image" ), NULL },
2010-01-11 16:08:13 +01:00
{ "verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose,
N_( "verbose output" ), NULL },
{ NULL }
};
/* Calculate the shrink factors.
*
* We shrink in two stages: first, a shrink with a block average. This can
* only accurately shrink by integer factors. We then do a second shrink with
2010-02-05 15:34:38 +01:00
* a supplied interpolator to get the exact size we want.
2010-01-11 16:08:13 +01:00
*/
static int
calculate_shrink( int width, int height, double *residual )
{
/* We shrink to make the largest dimension equal to size.
*/
int dimension = IM_MAX( width, height );
double factor = dimension / (double) thumbnail_size;
2010-01-13 18:35:05 +01:00
/* If the shrink factor is <=1.0, we need to zoom rather than shrink.
* Just set the factor to 1 in this case.
*/
2010-01-14 14:39:46 +01:00
double factor2 = factor < 1.0 ? 1.0 : factor;
2010-01-13 18:35:05 +01:00
2010-01-11 16:08:13 +01:00
/* Int component of shrink.
*/
2010-01-13 18:35:05 +01:00
int shrink = floor( factor2 );
2010-01-11 16:08:13 +01:00
/* Size after int shrink.
*/
int isize = floor( dimension / shrink );
/* Therefore residual scale factor is.
*/
if( residual )
*residual = thumbnail_size / (double) isize;
return( shrink );
}
2010-02-05 15:34:38 +01:00
/* Some interpolators look a little soft, so we have an optional sharpening
* stage.
2010-01-11 16:08:13 +01:00
*/
static INTMASK *
sharpen_filter( void )
{
static INTMASK *mask = NULL;
if( !mask ) {
mask = im_create_imaskv( "sharpen.con", 3, 3,
-1, -1, -1,
2010-05-13 22:29:28 +02:00
-1, 16, -1,
2010-01-11 16:08:13 +01:00
-1, -1, -1 );
2010-05-13 22:29:28 +02:00
mask->scale = 8;
2010-01-11 16:08:13 +01:00
}
return( mask );
}
static int
2010-02-05 15:34:38 +01:00
shrink_factor( IMAGE *in, IMAGE *out,
int shrink, double residual, VipsInterpolate *interp )
2010-01-11 16:08:13 +01:00
{
2010-01-14 14:39:46 +01:00
IMAGE *t[9];
2010-01-13 19:03:27 +01:00
IMAGE *x;
2010-01-11 16:08:13 +01:00
2010-01-13 19:03:27 +01:00
if( im_open_local_array( out, t, 9, "thumbnail", "p" ) )
return( -1 );
x = in;
/* Unpack the two coded formats we support to float for processing.
*/
if( x->Coding == IM_CODING_LABQ ) {
if( verbose )
printf( "unpacking LAB to RGB\n" );
if( im_LabQ2disp( x, t[1], im_col_displays( 7 ) ) )
return( -1 );
x = t[1];
}
else if( x->Coding == IM_CODING_RAD ) {
if( verbose )
printf( "unpacking Rad to float\n" );
if( im_rad2float( x, t[1] ) )
return( -1 );
x = t[1];
}
/* Shrink!
*
* We want to make sure we read the image sequentially
* so that :seq mode works. However, the convolution we may be doing
* later will force us into SMALLTILE mode and that will break
* sequentiallity.
*
* So ... read into a cache where tiles are scanlines, and make sure
* we keep enough scanlines to be able to serve a line of tiles.
2010-01-13 19:03:27 +01:00
*/
if( im_shrink( x, t[2], shrink, shrink ) ||
im_tile_cache( t[2], t[3],
t[2]->Xsize, 1,
VIPS__TILE_HEIGHT * 2 ) )
2010-01-11 16:08:13 +01:00
return( -1 );
2010-01-14 14:39:46 +01:00
x = t[3];
if( im_affinei_all( t[3], t[4],
interp, residual, 0, 0, residual, 0, 0 ) )
return( -1 );
x = t[4];
2010-01-14 14:39:46 +01:00
/* If we are upsampling, don't sharpen, since nearest looks dumb
* sharpened.
*/
if( shrink > 1 && residual <= 1.0 && !nosharpen ) {
2010-02-05 15:34:38 +01:00
if( verbose )
printf( "sharpening thumbnail\n" );
if( im_conv( x, t[5], sharpen_filter() ) )
2010-01-14 14:39:46 +01:00
return( -1 );
x = t[5];
2010-01-14 14:39:46 +01:00
}
2010-01-11 16:08:13 +01:00
/* Colour management: we can transform the image if we have an output
* profile and an input profile. The input profile can be in the
* image, or if there is no profile there, supplied by the user.
2010-01-13 19:03:27 +01:00
*/
if( export_profile &&
(im_header_get_typeof( x, IM_META_ICC_NAME ) ||
import_profile) ) {
if( im_header_get_typeof( x, IM_META_ICC_NAME ) ) {
2010-02-05 15:34:38 +01:00
if( verbose )
printf( "importing with embedded profile\n" );
if( im_icc_import_embedded( x, t[6],
2010-01-11 16:08:13 +01:00
IM_INTENT_RELATIVE_COLORIMETRIC ) )
return( -1 );
}
else {
2010-02-05 15:34:38 +01:00
if( verbose )
printf( "importing with profile %s\n",
import_profile );
if( im_icc_import( x, t[6],
import_profile,
IM_INTENT_RELATIVE_COLORIMETRIC ) )
return( -1 );
}
2010-02-05 15:34:38 +01:00
if( verbose )
printf( "exporting with profile %s\n", export_profile );
if( im_icc_export_depth( t[6], t[7],
8, export_profile,
IM_INTENT_RELATIVE_COLORIMETRIC ) )
2010-01-11 16:08:13 +01:00
return( -1 );
x = t[7];
2010-01-11 16:08:13 +01:00
}
2010-02-06 11:40:41 +01:00
if( !nodelete_profile ) {
2010-02-05 15:34:38 +01:00
if( verbose )
2010-01-25 15:23:30 +01:00
printf( "deleting profile from output image\n" );
2010-02-05 15:34:38 +01:00
2010-05-26 17:32:13 +02:00
/* Only try to remove if it exists to avoid extra error
* messages.
*/
if( im_meta_get_typeof( x, IM_META_ICC_NAME ) &&
!im_meta_remove( x, IM_META_ICC_NAME ) )
2010-02-05 15:34:38 +01:00
return( -1 );
2010-01-25 15:23:30 +01:00
}
2010-01-13 19:03:27 +01:00
if( im_copy( x, out ) )
2010-01-11 16:08:13 +01:00
return( -1 );
return( 0 );
}
2010-02-05 15:34:38 +01:00
static int
thumbnail3( IMAGE *in, IMAGE *out )
{
int shrink;
double residual;
VipsInterpolate *interp;
int result;
shrink = calculate_shrink( in->Xsize, in->Ysize, &residual );
/* For images smaller than the thumbnail, we upscale with nearest
* neighbor. Otherwise we makes thumbnails that look fuzzy and awful.
*/
if( !(interp = VIPS_INTERPOLATE( vips_object_new_from_string(
2011-05-30 12:41:02 +02:00
g_type_class_ref( VIPS_TYPE_INTERPOLATE ),
2010-02-05 15:34:38 +01:00
residual > 1.0 ? "nearest" : interpolator ) )) )
return( -1 );
if( verbose ) {
printf( "integer shrink by %d\n", shrink );
printf( "residual scale by %g\n", residual );
printf( "%s interpolation\n",
VIPS_OBJECT_GET_CLASS( interp )->nickname );
2010-02-05 15:34:38 +01:00
}
result = shrink_factor( in, out, shrink, residual, interp );
g_object_unref( interp );
return( result );
}
2010-01-11 16:08:13 +01:00
/* Given (eg.) "/poop/somefile.png", make the thumbnail name,
* "/poop/tn_somefile.jpg".
*/
static char *
make_thumbnail_name( const char *filename )
{
char *dir;
char *file;
char *p;
char buf[FILENAME_MAX];
char *result;
dir = g_path_get_dirname( filename );
file = g_path_get_basename( filename );
if( (p = strrchr( file, '.' )) )
*p = '\0';
2010-02-06 11:40:41 +01:00
im_snprintf( buf, FILENAME_MAX, output_format, file );
2010-01-11 16:08:13 +01:00
result = g_build_filename( dir, buf, NULL );
if( verbose )
printf( "thumbnailing %s as %s\n", filename, buf );
g_free( dir );
g_free( file );
return( result );
}
static int
thumbnail2( const char *filename )
{
IMAGE *in;
IMAGE *out;
char *tn_filename;
int result;
2010-07-30 14:30:45 +02:00
if( !(in = im_open( filename, "rd" )) )
2010-01-11 16:08:13 +01:00
return( -1 );
tn_filename = make_thumbnail_name( filename );
if( !(out = im_open( tn_filename, "w" )) ) {
im_close( in );
g_free( tn_filename );
return( -1 );
}
2010-02-05 15:34:38 +01:00
result = thumbnail3( in, out );
2010-01-11 16:08:13 +01:00
g_free( tn_filename );
im_close( out );
im_close( in );
return( result );
}
/* JPEGs get special treatment. libjpeg supports fast shrink-on-read,
* so if we have a JPEG, we can ask VIPS to load a lower resolution
* version.
*/
static int
thumbnail( const char *filename )
{
VipsFormatClass *format;
char buf[FILENAME_MAX];
2010-01-11 16:08:13 +01:00
2010-01-13 18:35:05 +01:00
if( verbose )
printf( "thumbnailing %s\n", filename );
2010-01-11 16:08:13 +01:00
if( !(format = vips_format_for_file( filename )) )
return( -1 );
2010-01-13 18:35:05 +01:00
if( verbose )
printf( "detected format as %s\n",
VIPS_OBJECT_CLASS( format )->nickname );
2010-01-11 16:08:13 +01:00
if( strcmp( VIPS_OBJECT_CLASS( format )->nickname, "jpeg" ) == 0 ) {
IMAGE *im;
int shrink;
/* This will just read in the header and is quick.
*/
if( !(im = im_open( filename, "r" )) )
return( -1 );
shrink = calculate_shrink( im->Xsize, im->Ysize, NULL );
im_close( im );
if( shrink > 8 )
shrink = 8;
else if( shrink > 4 )
shrink = 4;
else if( shrink > 2 )
shrink = 2;
else
shrink = 1;
im_snprintf( buf, FILENAME_MAX, "%s:%d", filename, shrink );
if( verbose )
printf( "using fast jpeg shrink, factor %d\n", shrink );
return( thumbnail2( buf ) );
}
else if( strcmp( VIPS_OBJECT_CLASS( format )->nickname, "png" ) == 0 ) {
char buf[FILENAME_MAX];
if( verbose )
printf( "enabling sequential mode for png load\n" );
im_snprintf( buf, FILENAME_MAX, "%s:seq", filename );
2010-01-11 16:08:13 +01:00
return( thumbnail2( buf ) );
}
else
return( thumbnail2( filename ) );
}
int
main( int argc, char **argv )
{
GOptionContext *context;
GError *error = NULL;
int i;
2010-05-25 18:46:03 +02:00
if( im_init_world( argv[0] ) )
error_exit( "unable to start VIPS" );
textdomain( GETTEXT_PACKAGE );
setlocale( LC_ALL, "" );
2010-01-11 16:08:13 +01:00
context = g_option_context_new( _( "- thumbnail generator" ) );
g_option_context_add_main_entries( context, options, GETTEXT_PACKAGE );
g_option_context_add_group( context, im_get_option_group() );
if( !g_option_context_parse( context, &argc, &argv, &error ) ) {
if( error ) {
fprintf( stderr, "%s\n", error->message );
g_error_free( error );
}
error_exit( "try \"%s --help\"", g_get_prgname() );
}
g_option_context_free( context );
for( i = 1; i < argc; i++ )
if( thumbnail( argv[i] ) ) {
fprintf( stderr, "%s: unable to thumbnail %s\n",
argv[0], argv[i] );
fprintf( stderr, "%s", im_error_buffer() );
im_error_clear();
}
vips_shutdown();
2010-01-11 16:08:13 +01:00
return( 0 );
}