vipsthumbnail uses new vips_info()

This commit is contained in:
John Cupitt 2013-08-07 09:57:18 +01:00
parent 6855770362
commit aa935133d0
4 changed files with 451 additions and 58 deletions

View File

@ -0,0 +1,407 @@
/* find histograms
*
* Copyright: 1990, 1991, N. Dessipris.
*
* Author: Nicos Dessipris.
* Written on: 09/07/1990
* Modified on : 11/03/1991
* 19/7/93 JC
* - test for Coding type added
* 26/10/94 JC
* - rewritten for ANSI
* - now does USHORT too
* - 5 x faster!
* 2/6/95 JC
* - rewritten for partials
* 3/3/01 JC
* - tiny speed ups
* 21/1/07
* - number bands from zero
* 24/3/10
* - gtkdoc
* - small cleanups
* 25/1/12
* - cast @in to u8/u16.
*/
/*
This file is part of VIPS.
VIPS is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
/*
These files are distributed with VIPS - http://www.vips.ecs.soton.ac.uk
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif /*HAVE_CONFIG_H*/
#include <vips/intl.h>
#include <vips/vips.h>
/* Accumulate a histogram in one of these.
*/
typedef struct {
int bands; /* Number of bands in output */
int which; /* If one band in out, which band of input */
int size; /* Length of bins */
int mx; /* Maximum value we have seen */
unsigned int **bins; /* All the bins! */
} Histogram;
typedef struct _VipsHistFind {
VipsStatistic parent_instance;
VipsImage *in;
int band;
/* Main image histogram. Subhists accumulate to this.
*/
Histogram *hist;
} VipsHistFind;
typedef VipsStatisticClass VipsHistFindClass;
G_DEFINE_TYPE( VipsHistFind, vips_hist_find, VIPS_TYPE_STATISTIC );
/* Build a Histogram.
*/
static Histogram *
histogram_new( VipsImage *out, int bands, int which, int size )
{
Histogram *hist;
int i;
if( !(hist = VIPS_NEW( out, Histogram )) ||
!(hist->bins = VIPS_ARRAY( out, bands, unsigned int * )) )
return( NULL );
for( i = 0; i < bands; i++ ) {
if( !(hist->bins[i] = VIPS_ARRAY( out, size, unsigned int )) )
return( NULL );
memset( hist->bins[i], 0, size * sizeof( unsigned int ) );
}
hist->bands = bands;
hist->which = which;
hist->size = size;
hist->mx = 0;
return( hist );
}
/* Build a sub-hist, based on the main hist.
*/
static void *
vips_hist_find_start( VipsImage *out, void *a, void *b )
{
VipsHistFind *hist_find = (VipsHistFind *) a;
Histogram *hist = hist_find->hist;
return( (void *)
histogram_new( out, hist->bands, hist->which, hist->size ) );
}
/* Join a sub-hist onto the main hist.
*/
static int
vips_hist_find_stop( void *seq, void *a, void *b )
{
Histogram *sub_hist = (Histogram *) seq;
VipsHistFind *hist_find = (VipsHistFind *) a;
Histogram *hist = hist_find->hist;
int i, j;
g_assert( sub_hist->bands == hist->bands &&
sub_hist->size == hist->size );
/* Add on sub-data.
*/
hist->mx = VIPS_MAX( hist->mx, sub_hist->mx );
for( i = 0; i < hist->bands; i++ )
for( j = 0; j < hist->size; j++ )
hist->bins[i][j] += sub_hist->bins[i][j];
/* Blank out sub-hist to make sure we can't add it again.
*/
sub_hist->mx = 0;
for( i = 0; i < sub_hist->bands; i++ )
sub_hist->bins[i] = NULL;
return( 0 );
}
/* Hist of all bands of uchar.
*/
static int
vips_hist_find_uchar_scan( VipsStatistic *statistic,
void *seq, int x, int y, void *in, int n )
{
VipsHistFind *hist_find = (VipsHistFind *) statistic;
Histogram *hist = (Histogram *) seq;
int nb = hist_find->in->Bands;
VipsPel *p;
int x, z, i;
p = (VipsPel *) in;
for( i = 0, x = 0; x < n; x++ )
for( z = 0; z < nb; z++, i++ )
hist->bins[z][p[i]] += 1;
/* Note the maximum.
*/
hist->mx = 255;
return( 0 );
}
/* Histogram of a selected band of a uchar image.
*/
static int
vips_hist_find_uchar_extract_scan( VipsStatistic *statistic,
void *seq, int x, int y, void *in, int n )
{
VipsHistFind *hist_find = (VipsHistFind *) statistic;
Histogram *hist = (Histogram *) seq;
int nb = hist_find->in->Bands;
int max = n * nb;
unsigned int *bins = hist->bins[0];
VipsPel *p;
int x;
p = (VipsPel *) in;
for( x = hist->which; x < max; x += nb )
bins[p[x]] += 1;
/* Note the maximum.
*/
hist->mx = 255;
return( 0 );
}
/* Histogram of all bands of a ushort image.
*/
static int
vips_hist_find_ushort_hist( VipsRegion *reg,
void *seq, void *a, void *b, gboolean *stop )
{
Histogram *hist = (Histogram *) seq;
VipsRect *r = &reg->valid;
VipsImage *im = reg->im;
int le = r->left;
int to = r->top;
int bo = VIPS_RECT_BOTTOM(r);
int mx = hist->mx;
int nb = im->Bands;
int x, y, z;
/* Accumulate!
*/
for( y = to; y < bo; y++ ) {
unsigned short *p = (unsigned short *)
VIPS_REGION_ADDR( reg, le, y );
int i;
for( i = 0, x = 0; x < r->width; x++ )
for( z = 0; z < nb; z++, i++ ) {
int v = p[i];
/* Adjust maximum.
*/
if( v > mx )
mx = v;
hist->bins[z][v] += 1;
}
}
/* Note the maximum.
*/
hist->mx = mx;
return( 0 );
}
/* Histogram of one band of a ushort image.
*/
static int
vips_hist_find_ushort_hist_extract( VipsRegion *reg,
void *seq, void *a, void *b, gboolean *stop )
{
Histogram *hist = (Histogram *) seq;
VipsRect *r = &reg->valid;
VipsImage *im = reg->im;
int le = r->left;
int to = r->top;
int bo = VIPS_RECT_BOTTOM(r);
int mx = hist->mx;
unsigned int *bins = hist->bins[0];
int nb = im->Bands;
int max = nb * r->width;
int x, y;
/* Accumulate!
*/
for( y = to; y < bo; y++ ) {
unsigned short *p = (unsigned short *)
VIPS_REGION_ADDR( reg, le, y ) + hist->which;
for( x = hist->which; x < max; x += nb ) {
int v = p[x];
/* Adjust maximum.
*/
if( v > mx )
mx = v;
bins[v] += 1;
}
}
/* Note the maximum.
*/
hist->mx = mx;
return( 0 );
}
/* Save a bit of typing.
*/
#define UC VIPS_FORMAT_UCHAR
#define US VIPS_FORMAT_USHORT
#define UI VIPS_FORMAT_UINT
/* Type mapping: go to uchar or ushort.
*/
static int bandfmt_histgr[10] = {
/* UC C US S UI I F X D DX */
UC, UC, US, US, US, US, US, US, US, US
};
/**
* im_histgr:
* @in: input image
* @out: output image
* @bandno: band to equalise
*
* Find the histogram of @in. Find the histogram for band @bandno (producing a
* one-band histogram), or for all bands (producing an n-band histogram) if
* @bandno is -1.
*
* @in is cast to u8 or u16. @out is always u32.
*
* See also: im_hist_indexed(), im_histeq().
*
* Returns: 0 on success, -1 on error
*/
int
im_histgr( VipsImage *in, VipsImage *out, int bandno )
{
VipsImage *t;
int size; /* Length of hist */
int bands; /* Number of bands in output */
Histogram *mhist;
VipsGenerateFn scanfn;
int i, j;
unsigned int *obuffer, *q;
/* Check images. PIO from in, WIO to out.
*/
if( im_check_uncoded( "im_histgr", in ) ||
im_check_bandno( "im_histgr", in, bandno ) ||
im_pincheck( in ) ||
im_outcheck( out ) )
return( -1 );
/* Cast in to u8/u16.
*/
if( !(t = im_open_local( out, "im_histgr", "p" )) ||
im_clip2fmt( in, t, bandfmt_histgr[in->BandFmt] ) )
return( -1 );
in = t;
/* Find the range of pixel values we must handle.
*/
size = in->BandFmt == VIPS_FORMAT_UCHAR ? 256 : 65536;
/* How many output bands?
*/
if( bandno == -1 )
bands = in->Bands;
else
bands = 1;
/* Build main hist we accumulate data in.
*/
if( !(mhist = build_hist( out, bands, bandno, size )) )
return( -1 );
/* Select scan function.
*/
if( in->BandFmt == VIPS_FORMAT_UCHAR && bandno == -1 )
scanfn = vips_hist_find_uchar_hist;
else if( in->BandFmt == VIPS_FORMAT_UCHAR )
scanfn = vips_hist_find_uchar_hist_extract;
else if( in->BandFmt == VIPS_FORMAT_USHORT && bandno == -1 )
scanfn = vips_hist_find_ushort_hist;
else
scanfn = vips_hist_find_ushort_hist_extract;
/* Accumulate data.
*/
if( vips_sink( in,
vips_hist_find_start, scanfn, vips_hist_find_stop,
mhist, NULL ) )
return( -1 );
/* Make the output image.
*/
if( im_cp_desc( out, in ) )
return( -1 );
im_initdesc( out,
mhist->mx + 1, 1, bands, VIPS_FORMAT_UINT,
VIPS_CODING_NONE, VIPS_TYPE_HISTOGRAM, 1.0, 1.0, 0, 0 );
if( im_setupout( out ) )
return( -1 );
/* Interleave for output.
*/
if( !(obuffer = VIPS_ARRAY( out,
VIPS_IMAGE_N_ELEMENTS( out ), unsigned int )) )
return( -1 );
for( q = obuffer, j = 0; j < out->Xsize; j++ )
for( i = 0; i < out->Bands; i++ )
*q++ = mhist->bins[i][j];
/* Write interleaved buffer into hist.
*/
if( im_writeline( 0, out, (VipsPel *) obuffer ) )
return( -1 );
return( 0 );
}

View File

@ -355,7 +355,7 @@ vips_error_clear( void )
*
* Sends a formatted informational message to stderr if the --vips-info flag
* has been given to the program or the environment variable IM_INFO has been
* set.
* defined.
*
* Informational messages are used to report details about the operation of
* functions.
@ -367,7 +367,7 @@ vips_vinfo( const char *domain, const char *fmt, va_list ap )
{
if( vips__info ) {
g_mutex_lock( vips__global_lock );
(void) fprintf( stderr, _( "%s: " ), _( "vips info" ) );
(void) fprintf( stderr, _( "%s: " ), _( "info" ) );
if( domain )
(void) fprintf( stderr, _( "%s: " ), domain );
(void) vfprintf( stderr, fmt, ap );
@ -382,10 +382,11 @@ vips_vinfo( const char *domain, const char *fmt, va_list ap )
* @fmt: printf()-style format string for the message
* @Varargs: arguments to the format string
*
* Sends a formatted diagnostic message to stderr. If you define the
* environment variable IM_DIAGNOSTICS, these message are surpressed.
* Sends a formatted informational message to stderr if the --vips-info flag
* has been given to the program or the environment variable IM_INFO has been
* defined.
*
* Diagnostic messages are used to report details about the operation of
* Informational messages are used to report details about the operation of
* functions.
*
* See also: vips_vdiag(), vips_warn().

View File

@ -37,9 +37,9 @@
*/
/*
*/
#define VIPS_DEBUG
#define DEBUG
*/
#ifdef HAVE_CONFIG_H
#include <config.h>

View File

@ -60,7 +60,6 @@ static char *export_profile = NULL;
static char *import_profile = NULL;
static char *convolution_mask = "mild";
static gboolean delete_profile = FALSE;
static gboolean verbose = FALSE;
/* Deprecated and unused.
*/
@ -95,9 +94,9 @@ static GOptionEntry options[] = {
{ "delete", 'd', 0,
G_OPTION_ARG_NONE, &delete_profile,
N_( "delete profile from exported image" ), NULL },
{ "verbose", 'v', 0,
G_OPTION_ARG_NONE, &verbose,
N_( "verbose output" ), NULL },
{ "verbose", 'v', G_OPTION_FLAG_HIDDEN,
G_OPTION_ARG_NONE, NULL,
N_( "(deprecated, does nothing)" ), NULL },
{ "nodelete", 'l', G_OPTION_FLAG_HIDDEN,
G_OPTION_ARG_NONE, &nodelete_profile,
N_( "(deprecated, does nothing)" ), NULL },
@ -176,15 +175,13 @@ thumbnail_get_thumbnail( VipsImage *im )
if( !vips_image_get_typeof( im, THUMBNAIL ) ||
vips_image_get_blob( im, THUMBNAIL, &ptr, &size ) ||
vips_jpegload_buffer( ptr, size, &thumb, NULL ) ) {
if( verbose )
printf( "no jpeg thumbnail\n" );
vips_info( "vipsthumbnail", "no jpeg thumbnail" );
return( NULL );
}
calculate_shrink( thumb->Xsize, thumb->Ysize, &residual );
if( residual > 1.0 ) {
if( verbose )
printf( "jpeg thumbnail too small\n" );
vips_info( "vipsthumbnail", "jpeg thumbnail too small" );
g_object_unref( thumb );
return( NULL );
}
@ -192,21 +189,19 @@ thumbnail_get_thumbnail( VipsImage *im )
/* Reload with the correct downshrink.
*/
jpegshrink = thumbnail_find_jpegshrink( thumb );
if( verbose )
printf( "loading jpeg thumbnail with factor %d pre-shrink\n",
jpegshrink );
vips_info( "vipsthumbnail",
"loading jpeg thumbnail with factor %d pre-shrink",
jpegshrink );
g_object_unref( thumb );
if( vips_jpegload_buffer( ptr, size, &thumb,
"shrink", jpegshrink,
NULL ) ) {
if( verbose )
printf( "jpeg thumbnail reload failed\n" );
vips_info( "vipsthumbnail", "jpeg thumbnail reload failed" );
return( NULL );
}
if( verbose )
printf( "using %dx%d jpeg thumbnail\n",
thumb->Xsize, thumb->Ysize );
vips_info( "vipsthumbnail", "using %dx%d jpeg thumbnail",
thumb->Xsize, thumb->Ysize );
return( thumb );
}
@ -224,14 +219,12 @@ thumbnail_open( VipsObject *thumbnail, const char *filename )
const char *loader;
VipsImage *im;
if( verbose )
printf( "thumbnailing %s\n", filename );
vips_info( "vipsthumbnail", "thumbnailing %s", filename );
if( !(loader = vips_foreign_find_load( filename )) )
return( NULL );
if( verbose )
printf( "selected loader is \"%s\"\n", loader );
vips_info( "vipsthumbnail", "selected loader is %s", loader );
if( strcmp( loader, "VipsForeignLoadJpegFile" ) == 0 ) {
VipsImage *thumb;
@ -256,16 +249,16 @@ thumbnail_open( VipsObject *thumbnail, const char *filename )
else {
int jpegshrink;
if( verbose )
printf( "processing main jpeg image\n" );
vips_info( "vipsthumbnail",
"processing main jpeg image" );
jpegshrink = thumbnail_find_jpegshrink( im );
g_object_unref( im );
if( verbose )
printf( "loading jpeg with factor %d "
"pre-shrink\n", jpegshrink );
vips_info( "vipsthumbnail",
"loading jpeg with factor %d pre-shrink",
jpegshrink );
if( vips_foreign_load( filename, &im,
"sequential", TRUE,
@ -303,8 +296,7 @@ thumbnail_shrink( VipsObject *thumbnail, VipsImage *in,
/* Unpack the two coded formats we support.
*/
if( in->Coding == VIPS_CODING_LABQ ) {
if( verbose )
printf( "unpacking LAB to RGB\n" );
vips_info( "vipsthumbnail", "unpacking LAB to RGB" );
if( vips_colourspace( in, &t[0],
VIPS_INTERPRETATION_sRGB, NULL ) )
@ -313,8 +305,7 @@ thumbnail_shrink( VipsObject *thumbnail, VipsImage *in,
in = t[0];
}
else if( in->Coding == IM_CODING_RAD ) {
if( verbose )
printf( "unpacking Rad to float\n" );
vips_info( "vipsthumbnail", "unpacking Rad to float" );
/* rad is scrgb.
*/
@ -328,8 +319,7 @@ thumbnail_shrink( VipsObject *thumbnail, VipsImage *in,
shrink = calculate_shrink( in->Xsize, in->Ysize, &residual );
if( verbose )
printf( "integer shrink by %d\n", shrink );
vips_info( "vipsthumbnail", "integer shrink by %d", shrink );
if( vips_shrink( in, &t[3], shrink, shrink, NULL ) )
return( NULL );
@ -369,11 +359,9 @@ thumbnail_shrink( VipsObject *thumbnail, VipsImage *in,
return( NULL );
in = t[5];
if( verbose ) {
printf( "residual scale by %g\n", residual );
printf( "%s interpolation\n",
VIPS_OBJECT_GET_CLASS( interp )->nickname );
}
vips_info( "vipsthumbnail", "residual scale by %g", residual );
vips_info( "vipsthumbnail", "%s interpolation",
VIPS_OBJECT_GET_CLASS( interp )->nickname );
/* If we are upsampling, don't sharpen, since nearest looks dumb
* sharpened.
@ -381,9 +369,7 @@ thumbnail_shrink( VipsObject *thumbnail, VipsImage *in,
if( shrink >= 1 &&
residual <= 1.0 &&
sharpen ) {
if( verbose )
printf( "sharpening thumbnail\n" );
vips_info( "vipsthumbnail", "sharpening thumbnail" );
t[6] = vips_image_new();
if( im_conv( in, t[6], sharpen ) )
return( NULL );
@ -397,15 +383,15 @@ thumbnail_shrink( VipsObject *thumbnail, VipsImage *in,
if( export_profile &&
(vips_image_get_typeof( in, VIPS_META_ICC_NAME ) ||
import_profile) ) {
if( verbose ) {
if( vips_image_get_typeof( in, VIPS_META_ICC_NAME ) )
printf( "importing with embedded profile\n" );
else
printf( "importing with profile %s\n",
import_profile );
if( vips_image_get_typeof( in, VIPS_META_ICC_NAME ) )
vips_info( "vipsthumbnail",
"importing with embedded profile" );
else
vips_info( "vipsthumbnail",
"importing with profile %s", import_profile );
printf( "exporting with profile %s\n", export_profile );
}
vips_info( "vipsthumbnail",
"exporting with profile %s", export_profile );
if( vips_icc_transform( in, &t[7], export_profile,
"input_profile", import_profile,
@ -418,9 +404,8 @@ thumbnail_shrink( VipsObject *thumbnail, VipsImage *in,
if( delete_profile &&
vips_image_get_typeof( in, VIPS_META_ICC_NAME ) ) {
if( verbose )
printf( "deleting profile from output image\n" );
vips_info( "vipsthumbnail",
"deleting profile from output image" );
if( vips_image_remove( in, VIPS_META_ICC_NAME ) )
return( NULL );
}
@ -507,8 +492,8 @@ thumbnail_write( VipsImage *im, const char *filename )
g_free( dir );
}
if( verbose )
printf( "thumbnailing %s as %s\n", filename, output_name );
vips_info( "vipsthumbnail",
"thumbnailing %s as %s", filename, output_name );
g_free( file );