|
| 1 | +/* VIPS thumbnailer |
| 2 | + * |
| 3 | + * J. Cupitt, 11/1/09 |
| 4 | + */ |
| 5 | + |
| 6 | +#ifdef HAVE_CONFIG_H |
| 7 | +#include <config.h> |
| 8 | +#endif /*HAVE_CONFIG_H*/ |
| 9 | +#include <vips/intl.h> |
| 10 | + |
| 11 | +#include <stdio.h> |
| 12 | +#include <math.h> |
| 13 | +#include <string.h> |
| 14 | +#include <stdlib.h> |
| 15 | + |
| 16 | +#include <vips/vips.h> |
| 17 | + |
| 18 | +static gboolean verbose = FALSE; |
| 19 | +static int use_disc_threshold = 1024 * 1024; |
| 20 | +static int thumbnail_size = 128; |
| 21 | +static char *thumbnail_format = "tn_%s.jpg"; |
| 22 | +static char *colour_profile = NULL; |
| 23 | + |
| 24 | +static GOptionEntry options[] = { |
| 25 | + { "size", 's', 0, G_OPTION_ARG_INT, &thumbnail_size, |
| 26 | + N_( "set thumbnail size to N" ), "N" }, |
| 27 | + { "format", 'f', 0, G_OPTION_ARG_STRING, &thumbnail_format, |
| 28 | + N_( "set thumbnail format to S" ), "S" }, |
| 29 | + { "disc", 'd', 0, G_OPTION_ARG_INT, &use_disc_threshold, |
| 30 | + N_( "set disc use threshold to N" ), "N" }, |
| 31 | + { "profile", 'p', 0, G_OPTION_ARG_STRING, &colour_profile, |
| 32 | + N_( "export with profile P" ), "P" }, |
| 33 | + { "verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose, |
| 34 | + N_( "verbose output" ), NULL }, |
| 35 | + { NULL } |
| 36 | +}; |
| 37 | + |
| 38 | +/* Open an image, using a disc temporary for 'large' images. |
| 39 | + */ |
| 40 | +static IMAGE * |
| 41 | +open_image( const char *filename ) |
| 42 | +{ |
| 43 | + IMAGE *im; |
| 44 | + IMAGE *disc; |
| 45 | + size_t size; |
| 46 | + VipsFormatClass *format; |
| 47 | + |
| 48 | + if( !(im = im_open( filename, "r" )) ) |
| 49 | + return( NULL ); |
| 50 | + |
| 51 | + /* Estimate decompressed image size. |
| 52 | + */ |
| 53 | + size = IM_IMAGE_SIZEOF_LINE( im ) * im->Ysize; |
| 54 | + |
| 55 | + /* If it's less than a megabyte, we can just use 'im'. This will |
| 56 | + * decompress to memory. |
| 57 | + */ |
| 58 | + if( size < use_disc_threshold ) |
| 59 | + return( im ); |
| 60 | + |
| 61 | + /* Nope, too big, we need to decompress to disc and return the disc |
| 62 | + * file. |
| 63 | + */ |
| 64 | + im_close( im ); |
| 65 | + |
| 66 | + /* This makes a disc temp which be unlinked automatically when the |
| 67 | + * image is closed. The temp is made in "/tmp", or "$TMPDIR/", if the |
| 68 | + * environment variable is set. |
| 69 | + */ |
| 70 | + if( !(disc = im__open_temp( "%s.v" )) ) |
| 71 | + return( NULL ); |
| 72 | + |
| 73 | + if( verbose ) |
| 74 | + printf( "large file, decompressing to disc temp %s\n", |
| 75 | + disc->filename ); |
| 76 | + |
| 77 | + /* Find a decompress class and use it to load the image. |
| 78 | + */ |
| 79 | + if( !(format = vips_format_for_file( filename )) || |
| 80 | + format->load( filename, disc ) ) { |
| 81 | + im_close( disc ); |
| 82 | + return( NULL ); |
| 83 | + } |
| 84 | + |
| 85 | + return( disc ); |
| 86 | +} |
| 87 | + |
| 88 | +/* Calculate the shrink factors. |
| 89 | + * |
| 90 | + * We shrink in two stages: first, a shrink with a block average. This can |
| 91 | + * only accurately shrink by integer factors. We then do a second shrink with |
| 92 | + * bilinear interpolation to get the exact size we want. |
| 93 | + */ |
| 94 | +static int |
| 95 | +calculate_shrink( int width, int height, double *residual ) |
| 96 | +{ |
| 97 | + /* We shrink to make the largest dimension equal to size. |
| 98 | + */ |
| 99 | + int dimension = IM_MAX( width, height ); |
| 100 | + |
| 101 | + double factor = dimension / (double) thumbnail_size; |
| 102 | + |
| 103 | + /* Int component of shrink. |
| 104 | + */ |
| 105 | + int shrink = floor( factor ); |
| 106 | + |
| 107 | + /* Size after int shrink. |
| 108 | + */ |
| 109 | + int isize = floor( dimension / shrink ); |
| 110 | + |
| 111 | + /* Therefore residual scale factor is. |
| 112 | + */ |
| 113 | + if( residual ) |
| 114 | + *residual = thumbnail_size / (double) isize; |
| 115 | + |
| 116 | + return( shrink ); |
| 117 | +} |
| 118 | + |
| 119 | +/* We use bilinear interpolation for the final shrink. VIPS has higher-order |
| 120 | + * interpolators, but they are only built if a C++ compiler is available. |
| 121 | + * Bilinear can look a little 'soft', so after shrinking, we need to sharpen a |
| 122 | + * little. |
| 123 | + * |
| 124 | + * This is a simple sharpen filter. |
| 125 | + */ |
| 126 | +static INTMASK * |
| 127 | +sharpen_filter( void ) |
| 128 | +{ |
| 129 | + static INTMASK *mask = NULL; |
| 130 | + |
| 131 | + if( !mask ) { |
| 132 | + mask = im_create_imaskv( "sharpen.con", 3, 3, |
| 133 | + -1, -1, -1, |
| 134 | + -1, 16, -1, |
| 135 | + -1, -1, -1 ); |
| 136 | + mask->scale = 8; |
| 137 | + } |
| 138 | + |
| 139 | + return( mask ); |
| 140 | +} |
| 141 | + |
| 142 | +static int |
| 143 | +shrink_factor( IMAGE *in, IMAGE *out ) |
| 144 | +{ |
| 145 | + IMAGE *t[5]; |
| 146 | + int shrink; |
| 147 | + double residual; |
| 148 | + |
| 149 | + shrink = calculate_shrink( in->Xsize, in->Ysize, &residual ); |
| 150 | + |
| 151 | + if( verbose ) { |
| 152 | + printf( "integer shrink by %d\n", shrink ); |
| 153 | + printf( "residual scale by %g\n", residual ); |
| 154 | + } |
| 155 | + |
| 156 | + if( im_open_local_array( out, t, 5, "thumbnail", "p" ) || |
| 157 | + im_shrink( in, t[0], shrink, shrink ) || |
| 158 | + im_affinei_all( t[0], t[1], |
| 159 | + vips_interpolate_bilinear_static(), |
| 160 | + residual, 0, 0, residual, 0, 0 ) || |
| 161 | + im_conv( t[1], t[2], sharpen_filter() ) ) |
| 162 | + return( -1 ); |
| 163 | + |
| 164 | + if( colour_profile && im_header_get_typeof( t[2], IM_META_ICC_NAME ) ) { |
| 165 | + if( im_icc_import_embedded( t[2], t[3], |
| 166 | + IM_INTENT_RELATIVE_COLORIMETRIC ) || |
| 167 | + im_icc_export_depth( t[3], t[4], |
| 168 | + 8, colour_profile, |
| 169 | + IM_INTENT_RELATIVE_COLORIMETRIC ) ) |
| 170 | + return( -1 ); |
| 171 | + |
| 172 | + t[2] = t[4]; |
| 173 | + } |
| 174 | + |
| 175 | + if( im_copy( t[4], out ) ) |
| 176 | + return( -1 ); |
| 177 | + |
| 178 | + return( 0 ); |
| 179 | +} |
| 180 | + |
| 181 | +/* Given (eg.) "/poop/somefile.png", make the thumbnail name, |
| 182 | + * "/poop/tn_somefile.jpg". |
| 183 | + */ |
| 184 | +static char * |
| 185 | +make_thumbnail_name( const char *filename ) |
| 186 | +{ |
| 187 | + char *dir; |
| 188 | + char *file; |
| 189 | + char *p; |
| 190 | + char buf[FILENAME_MAX]; |
| 191 | + char *result; |
| 192 | + |
| 193 | + dir = g_path_get_dirname( filename ); |
| 194 | + file = g_path_get_basename( filename ); |
| 195 | + |
| 196 | + if( (p = strrchr( file, '.' )) ) |
| 197 | + *p = '\0'; |
| 198 | + |
| 199 | + im_snprintf( buf, FILENAME_MAX, thumbnail_format, file ); |
| 200 | + result = g_build_filename( dir, buf, NULL ); |
| 201 | + |
| 202 | + if( verbose ) |
| 203 | + printf( "thumbnailing %s as %s\n", filename, buf ); |
| 204 | + |
| 205 | + g_free( dir ); |
| 206 | + g_free( file ); |
| 207 | + |
| 208 | + return( result ); |
| 209 | +} |
| 210 | + |
| 211 | +static int |
| 212 | +thumbnail2( const char *filename ) |
| 213 | +{ |
| 214 | + IMAGE *in; |
| 215 | + IMAGE *out; |
| 216 | + char *tn_filename; |
| 217 | + int result; |
| 218 | + |
| 219 | + if( !(in = open_image( filename )) ) |
| 220 | + return( -1 ); |
| 221 | + |
| 222 | + tn_filename = make_thumbnail_name( filename ); |
| 223 | + if( !(out = im_open( tn_filename, "w" )) ) { |
| 224 | + im_close( in ); |
| 225 | + g_free( tn_filename ); |
| 226 | + return( -1 ); |
| 227 | + } |
| 228 | + |
| 229 | + result = shrink_factor( in, out ); |
| 230 | + |
| 231 | + g_free( tn_filename ); |
| 232 | + im_close( out ); |
| 233 | + im_close( in ); |
| 234 | + |
| 235 | + return( result ); |
| 236 | +} |
| 237 | + |
| 238 | +/* JPEGs get special treatment. libjpeg supports fast shrink-on-read, |
| 239 | + * so if we have a JPEG, we can ask VIPS to load a lower resolution |
| 240 | + * version. |
| 241 | + */ |
| 242 | +static int |
| 243 | +thumbnail( const char *filename ) |
| 244 | +{ |
| 245 | + VipsFormatClass *format; |
| 246 | + |
| 247 | + if( !(format = vips_format_for_file( filename )) ) |
| 248 | + return( -1 ); |
| 249 | + |
| 250 | + if( strcmp( VIPS_OBJECT_CLASS( format )->nickname, "jpeg" ) == 0 ) { |
| 251 | + IMAGE *im; |
| 252 | + int shrink; |
| 253 | + char buf[FILENAME_MAX]; |
| 254 | + |
| 255 | + /* This will just read in the header and is quick. |
| 256 | + */ |
| 257 | + if( !(im = im_open( filename, "r" )) ) |
| 258 | + return( -1 ); |
| 259 | + shrink = calculate_shrink( im->Xsize, im->Ysize, NULL ); |
| 260 | + im_close( im ); |
| 261 | + |
| 262 | + if( shrink > 8 ) |
| 263 | + shrink = 8; |
| 264 | + else if( shrink > 4 ) |
| 265 | + shrink = 4; |
| 266 | + else if( shrink > 2 ) |
| 267 | + shrink = 2; |
| 268 | + else |
| 269 | + shrink = 1; |
| 270 | + |
| 271 | + im_snprintf( buf, FILENAME_MAX, "%s:%d", filename, shrink ); |
| 272 | + |
| 273 | + if( verbose ) |
| 274 | + printf( "using fast jpeg shrink, factor %d\n", shrink ); |
| 275 | + |
| 276 | + return( thumbnail2( buf ) ); |
| 277 | + } |
| 278 | + else |
| 279 | + return( thumbnail2( filename ) ); |
| 280 | +} |
| 281 | + |
| 282 | +int |
| 283 | +main( int argc, char **argv ) |
| 284 | +{ |
| 285 | + GOptionContext *context; |
| 286 | + GError *error = NULL; |
| 287 | + int i; |
| 288 | + |
| 289 | + im_init_world( argv[0] ); |
| 290 | + |
| 291 | + context = g_option_context_new( _( "- thumbnail generator" ) ); |
| 292 | + |
| 293 | + g_option_context_add_main_entries( context, options, GETTEXT_PACKAGE ); |
| 294 | + g_option_context_add_group( context, im_get_option_group() ); |
| 295 | + |
| 296 | + if( !g_option_context_parse( context, &argc, &argv, &error ) ) { |
| 297 | + if( error ) { |
| 298 | + fprintf( stderr, "%s\n", error->message ); |
| 299 | + g_error_free( error ); |
| 300 | + } |
| 301 | + |
| 302 | + error_exit( "try \"%s --help\"", g_get_prgname() ); |
| 303 | + } |
| 304 | + |
| 305 | + g_option_context_free( context ); |
| 306 | + |
| 307 | + for( i = 1; i < argc; i++ ) |
| 308 | + if( thumbnail( argv[i] ) ) { |
| 309 | + fprintf( stderr, "%s: unable to thumbnail %s\n", |
| 310 | + argv[0], argv[i] ); |
| 311 | + fprintf( stderr, "%s", im_error_buffer() ); |
| 312 | + im_error_clear(); |
| 313 | + } |
| 314 | + |
| 315 | + return( 0 ); |
| 316 | +} |
0 commit comments