Merge remote-tracking branch 'origin/master'

This commit is contained in:
John Cupitt 2013-11-16 18:40:24 +00:00
commit 8b2dc6f53f
15 changed files with 753 additions and 422 deletions

View File

@ -1,7 +1,8 @@
19/10/13 started 7.37.0
- redone im_rotate_*mask45(), im_gauss_*mask*(), im_log_*mask(), im_dilate(),
im_erode(), im_rank_image(), im_compass(), im_linedet(), im_gradient(),
im_convsep(), im_convsep_f(), im_fastcor(), im_spcor() as classes
im_convsep(), im_convsep_f(), im_fastcor(), im_spcor(), im_sharpen()
as classes
- im_gradcor() deprecated
- vips_init() now does some ABI compat checking, though this change requires
an ABI break
@ -21,6 +22,7 @@
- add --linear mode to vipsthumbnail
- support XYZ as a PCS for vips_icc_import() and vips_icc_export()
- add --strip option to jpegsave
- added vips_gaussblur() convenience function
15/11/13 started 7.36.4
- improve compat with im_init_world()

37
TODO
View File

@ -1,28 +1,26 @@
- large alpha PNGs seem to need huge amounts of ram to process
- we're spending a lot of time on threading
john@bambam ~/pics $ vipsthumbnail wtc.png
memory: high-water mark 31.37 MB
john@bambam ~/pics $ vipsthumbnail wtc.png --linear
memory: high-water mark 47.83 MB
$ time vips sharpen x.v x2.v
real 0m6.925s
user 0m18.240s
sys 0m0.996s
$ time vips sharpen x.v x2.v --vips-concurrency=1
real 0m12.831s
user 0m10.016s
sys 0m0.480s
john@bambam ~/pics $ vipsthumbnail wtc_alpha.png
memory: high-water mark 41.85 MB
john@bambam ~/pics $ vipsthumbnail wtc_alpha.png --linear
memory: high-water mark 101.59 MB
john@bambam ~/pics $
almost 50% of CPU! ouch
it seems to be the vips_colourspace() cast from sRGB to XYZ that's doing it?
how odd
- vips_gaussblur() should switch to float prec if given a float image?
maybe the thing that detaches and reattaches the alpha?
same for vips_conv()?
- still don't get progress feedback in nip2 for a long png save, eg. make a
4-band wtc, save as png
maybe precision is a dumb thing
- do morph quickly as simple wrappers over the vips7 operations
- support --strip for other writers
- vipstumbnail is broken for the gamma trick png on eric brasseur's page
- vipsthumbnail could shrink-on-load openslide and pyr tiff as well?
- look again at gcc auto-vectorisation, what would we need to do to use this?
@ -35,11 +33,6 @@ john@bambam ~/pics $
note on memuse page
- do conv and morph quickly as simple wrappers over the vips7 operations
- add vips_gaussian_blur() with approx / int / float precision, maybe
vips_resize() as well?
- do much fancier profiling with timing on all locks saved in memory and
dumped on exit

View File

@ -481,12 +481,11 @@ vips_colour_code_build( VipsObject *object )
VipsColour *colour = VIPS_COLOUR( object );
VipsColourCode *code = VIPS_COLOUR_CODE( object );
VipsColourCodeClass *class = VIPS_COLOUR_CODE_GET_CLASS( object );
VipsImage **t = (VipsImage **) vips_object_local_array( object, 6 );
VipsImage **t;
VipsImage *in;
VipsImage *extra;
t = (VipsImage **) vips_object_local_array( object, 5 );
in = code->in;
extra = NULL;
@ -559,15 +558,19 @@ vips_colour_code_build( VipsObject *object )
return( -1 );
/* Reattach higher bands, if necessary.
*
* Our processing on the first three bands may have changed the image
* format. For example, converting LAB to LABS will make a short
* image. We need to force the extra bands to match this new type.
*/
if( extra ) {
VipsImage *x;
if( vips_bandjoin2( colour->out, extra, &x, NULL ) )
if( vips_cast( extra, &t[5], colour->out->BandFmt, NULL ) ||
vips_bandjoin2( colour->out, t[5], &x, NULL ) )
return( -1 );
VIPS_UNREF( colour->out );
colour->out = x;
}

View File

@ -11,10 +11,11 @@ libconvolution_la_SOURCES = \
morph.c \
fastcor.c \
spcor.c \
sharpen.c \
gaussblur.c \
im_aconv.c \
im_aconvsep.c \
im_conv.c \
im_conv_f.c \
im_sharpen.c
im_conv_f.c
AM_CPPFLAGS = -I${top_srcdir}/libvips/include @VIPS_CFLAGS@ @VIPS_INCLUDES@

View File

@ -152,6 +152,8 @@ vips_convolution_operation_init( void )
extern int vips_convsep_get_type( void );
extern int vips_fastcor_get_type( void );
extern int vips_spcor_get_type( void );
extern int vips_sharpen_get_type( void );
extern int vips_gaussblur_get_type( void );
vips_conv_get_type();
vips_morph_get_type();
@ -159,4 +161,6 @@ vips_convolution_operation_init( void )
vips_convsep_get_type();
vips_fastcor_get_type();
vips_spcor_get_type();
vips_sharpen_get_type();
vips_gaussblur_get_type();
}

View File

@ -0,0 +1,180 @@
/* Gaussian blur.
*
* 15/11/13
* - from vips_sharpen()
*/
/*
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
*/
/*
#define DEBUG
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif /*HAVE_CONFIG_H*/
#include <vips/intl.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <vips/vips.h>
typedef struct _VipsGaussblur {
VipsOperation parent_instance;
VipsImage *in;
VipsImage *out;
int radius;
VipsPrecision precision;
} VipsGaussblur;
typedef VipsOperationClass VipsGaussblurClass;
G_DEFINE_TYPE( VipsGaussblur, vips_gaussblur, VIPS_TYPE_OPERATION );
static int
vips_gaussblur_build( VipsObject *object )
{
VipsGaussblur *gaussblur = (VipsGaussblur *) object;
VipsImage **t = (VipsImage **) vips_object_local_array( object, 2 );
if( VIPS_OBJECT_CLASS( vips_gaussblur_parent_class )->build( object ) )
return( -1 );
/* Stop at 20% of max ... bit mean, but means mask radius is roughly
* right.
*/
if( vips_gaussmat( &t[0], gaussblur->radius / 2, 0.2,
"separable", TRUE,
"integer", gaussblur->precision != VIPS_PRECISION_FLOAT,
NULL ) )
return( -1 );
#ifdef DEBUG
printf( "gaussblur: blurring with:\n" );
vips_matrixprint( t[0], NULL );
#endif /*DEBUG*/
if( vips_convsep( gaussblur->in, &t[1], t[0],
"precision", gaussblur->precision,
NULL ) )
return( -1 );
g_object_set( object, "out", vips_image_new(), NULL );
if( vips_image_write( t[1], gaussblur->out ) )
return( -1 );
return( 0 );
}
static void
vips_gaussblur_class_init( VipsGaussblurClass *class )
{
GObjectClass *gobject_class = G_OBJECT_CLASS( class );
VipsObjectClass *object_class = (VipsObjectClass *) class;
gobject_class->set_property = vips_object_set_property;
gobject_class->get_property = vips_object_get_property;
object_class->nickname = "gaussblur";
object_class->description = _( "Unsharp masking for print" );
object_class->build = vips_gaussblur_build;
VIPS_ARG_IMAGE( class, "in", 1,
_( "Input" ),
_( "Input image" ),
VIPS_ARGUMENT_REQUIRED_INPUT,
G_STRUCT_OFFSET( VipsGaussblur, in ) );
VIPS_ARG_IMAGE( class, "out", 2,
_( "Output" ),
_( "Output image" ),
VIPS_ARGUMENT_REQUIRED_OUTPUT,
G_STRUCT_OFFSET( VipsGaussblur, out ) );
VIPS_ARG_INT( class, "radius", 3,
_( "radius" ),
_( "Mask radius" ),
VIPS_ARGUMENT_REQUIRED_INPUT,
G_STRUCT_OFFSET( VipsGaussblur, radius ),
1, 1000000, 3 );
VIPS_ARG_ENUM( class, "precision", 4,
_( "Precision" ),
_( "Convolve with this precision" ),
VIPS_ARGUMENT_OPTIONAL_INPUT,
G_STRUCT_OFFSET( VipsGaussblur, precision ),
VIPS_TYPE_PRECISION, VIPS_PRECISION_INTEGER );
}
static void
vips_gaussblur_init( VipsGaussblur *gaussblur )
{
gaussblur->radius = 3;
gaussblur->precision = VIPS_PRECISION_INTEGER;
}
/**
* vips_gaussblur:
* @in: input image
* @out: output image
* @radius: how large a mask to use
* @...: %NULL-terminated list of optional named arguments
*
* Optional arguments:
*
* @precision: #VipsPrecision for blur
*
* This operator runs vips_gaussmat() and vips_convsep() for you on an image.
*
* @radius is not used directly. Instead the standard deviation of
* vips_gaussmat() is set to @radius / 2 and the minimum amplitude set to 20%.
* This gives a mask radius of approximately @radius pixels.
*
* See also: vips_gaussmat(), vips_conv().
*
* Returns: 0 on success, -1 on error.
*/
int
vips_gaussblur( VipsImage *in, VipsImage **out, int radius, ... )
{
va_list ap;
int result;
va_start( ap, radius );
result = vips_call_split( "gaussblur", ap, in, out, radius );
va_end( ap );
return( result );
}

View File

@ -1,371 +0,0 @@
/* Cored sharpen of LABQ image.
*
* Usage:
*
* int im_sharpen( IMAGE *in, IMAGE *out,
* int mask_size,
* int x1, int x2,
* double m1, double m2 )
*
* Returns 0 on success and -1 on error
*
* Copyright: 1995 A. Abbood
* Author: A. Abbood
* Written on: 30/01/1995
* 15/5/95 JC
* - updated for latest 7.3 mods
* - m3 parameter removed
* - bug fixes and speed-ups
* 4/7/95 JC
* - x3 parameter added
* - xs are now double
* 6/7/95 JC
* - xs are now ys
* - better LUT generation
* 12/3/01 JC
* - uses seperable convolution for umask
* - tiny clean ups
* 23/7/01 JC
* - fix for band extract index changed
* 21/4/04
* - switched to gaussian mask and radius
* 20/11/04
* - uses extract_bands() to remove and reattach ab for slight speedup
* - accepts LabS as well as LabQ for slight speedup
* - small code tidies
* - ~15% speed up in total
* 29/11/06
* - convolve first to help region sharing
* 3/2/10
* - gtkdoc
* - cleanups
*/
/*
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
*/
/*
#define DEBUG
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif /*HAVE_CONFIG_H*/
#include <vips/intl.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <vips/vips.h>
/* A lut --- we need indexes in the range [-x3,x2], so add x3 to indexes
* before starting to index table.
*/
typedef struct {
int *lut; /* Start of lut */
int x1, x2, x3; /* Parameters scaled up to int */
} SharpenLut;
/* Make a lut.
*/
static SharpenLut *
build_lut( IMAGE *out, int x1, int x2, int x3, double m1, double m2 )
{
SharpenLut *slut;
int i;
if( !(slut = IM_NEW( out, SharpenLut )) )
return( NULL );
if( !(slut->lut = IM_ARRAY( out, x2 + x3 + 1, int )) )
return( NULL );
slut->x1 = x1;
slut->x2 = x2;
slut->x3 = x3;
for( i = 0; i < x1; i++ ) {
slut->lut[x3 + i] = i * m1;
slut->lut[x3 - i] = -i * m1;
}
for( i = x1; i <= x2; i++ )
slut->lut[x3 + i] = x1 * m1 + (i - x1) * m2;
for( i = x1; i <= x3; i++ )
slut->lut[x3 - i] = -(x1 * m1 + (i - x1) * m2);
return( slut );
}
/* Take the difference of in1 and in2 and LUT it.
*/
static void
buf_difflut( short **in, short *out, int n, SharpenLut *slut )
{
int range = slut->x2 + slut->x3;
int *lut = slut->lut;
int x3 = slut->x3;
short *p1 = in[1];
short *p2 = in[0];
int i;
for( i = 0; i < n; i++ ) {
int v1 = p1[i];
int v2 = p2[i];
/* v2 is the area average. If this is zero, then we pass the
* original image through unaltered.
*/
if( v2 == 0 )
out[i] = v1;
else {
/* Find difference. Offset by x3 to get the expected
* range of values.
*/
int s1 = x3 + (v1 - v2);
int s2;
/* Clip to LUT range.
*/
if( s1 < 0 )
s1 = 0;
else if( s1 > range )
s1 = range;
/* Transform!
*/
s2 = v1 + lut[s1];
/* Clip to LabS range.
*/
if( s2 < 0 )
s2 = 0;
else if( s2 > 32767 )
s2 = 32767;
/* And write.
*/
out[i] = s2;
}
}
}
/* Make a 1 line gaussian of a specified radius.
*/
static INTMASK *
sharpen_mask_new( int radius )
{
INTMASK *base;
INTMASK *line;
int total;
int i;
/* Stop at 20% of max ... bit mean, but means mask radius is roughly
* right.
*/
if( !(base = im_gauss_imask( "big1", radius / 2, 0.2 )) )
return( NULL );
if( !(line = im_create_imask( "sharpen-line", base->xsize, 1 )) ) {
im_free_imask( base );
return( NULL );
}
total = 0;
for( i = 0; i < base->xsize; i++ ) {
line->coeff[i] =
base->coeff[base->xsize * (base->ysize / 2) + i];
total += line->coeff[i];
}
line->scale = total;
im_free_imask( base );
#ifdef DEBUG
printf( "sharpen_mask_new: created mask:\n" );
im_print_imask( line );
#endif /*DEBUG*/
return( line );
}
/**
* im_sharpen:
* @in: input image
* @out: output image
* @mask_size: how large a mask to use
* @x1: flat/jaggy threshold
* @y2: maximum amount of brightening
* @y3: maximum amount of darkening
* @m1: slope for flat areas
* @m2: slope for jaggy areas
*
* Selectively sharpen the L channel of a LAB image. Works for %IM_CODING_LABQ
* and LABS images.
*
* The operation performs a gaussian blur of size @mask_size and subtracts
* from @in to
* generate a high-frequency signal. This signal is passed through a lookup
* table formed from the five parameters and added back to @in.
*
* The lookup table is formed like this:
*
* |[
^
y2 |- - - - - -----------
| /
| / slope m2
| .../
-x1 | ... |
-------------------...---------------------->
| ... | x1
|... slope m1
/ |
/ m2 |
/ |
/ |
/ |
/ |
______/ _ _ _ _ _ _ | -y3
|
* ]|
*
* For printing, we recommend the following settings:
*
* |[
mask_size == 7
x1 == 1.5
y2 == 20 (don't brighten by more than 20 L*)
y3 == 50 (can darken by up to 50 L*)
m1 == 1 (some sharpening in flat areas)
m2 == 2 (more sharpening in jaggy areas)
* ]|
*
* If you want more or less sharpening, we suggest you just change the m1
* and m2 parameters.
*
* The @mask_size parameter changes the width of the fringe and can be
* adjusted according to the output printing resolution. As an approximate
* guideline, use 3 for 4 pixels/mm (CRT display resolution), 5 for 8
* pixels/mm, 7 for 12 pixels/mm and 9 for 16 pixels/mm (300 dpi == 12
* pixels/mm). These figures refer to the image raster, not the half-tone
* resolution.
*
* See also: im_conv().
*
* Returns: 0 on success, -1 on error.
*/
int
im_sharpen( IMAGE *in, IMAGE *out,
int mask_size,
double x1, double y2, double y3,
double m1, double m2 )
{
IMAGE *arry[3];
IMAGE *t[4];
INTMASK *mask;
SharpenLut *slut;
/* Turn y parameters into xs.
*/
double x2 = (y2 - x1 * (m1 - m2)) / m2;
double x3 = (y3 - x1 * (m1 - m2)) / m2;
if( in->Coding == IM_CODING_LABQ ) {
IMAGE *tc[2];
if( im_open_local_array( out, tc, 2, "im_sharpen:1", "p" ) ||
im_LabQ2LabS( in, tc[0] ) ||
im_sharpen( tc[0], tc[1],
mask_size, x1, y2, y3, m1, m2 ) ||
im_LabS2LabQ( tc[1], out ) )
return( -1 );
return( 0 );
}
/* Check IMAGE parameters
*/
if( im_piocheck( in, out ) ||
im_check_uncoded( "im_sharpen", in ) ||
im_check_bands( "im_gradcor", in, 3 ) ||
im_check_format( "im_gradcor", in, IM_BANDFMT_SHORT ) )
return( -1 );
/* Check number range.
*/
if( x1 < 0 || x1 > 99 ||
x2 < 0 || x2 > 99 ||
x1 > x2 ||
x3 < 0 || x3 > 99 ||
x1 > x3 ) {
im_error( "im_sharpen", "%s", _( "parameters out of range" ) );
return( -1 );
}
/* Set up data structures we need. First, the convolution mask we will
* use.
*/
if( !(mask = im_local_imask( out, sharpen_mask_new( mask_size ) )) )
return( -1 );
/* Make the lut we will use. We need to scale up x1, x2, x3 to the
* LabS range.
*/
if( !(slut = build_lut( out,
x1 * 327.67, x2 * 327.67, x3 * 327.67, m1, m2 )) )
return( -1 );
/* Open a set of local image descriptors.
*/
if( im_open_local_array( out, t, 4, "im_sharpen:2", "p" ) )
return( -1 );
/* Extract L and ab, convolve L.
*/
if( im_extract_band( in, t[0], 0 ) ||
im_extract_bands( in, t[1], 1, 2 ) ||
im_convsep( t[0], t[2], mask ) )
return( -1 );
/* Find difference of L channel and convolved L channel, and pass
* through LUT.
*/
if( im_cp_desc( t[3], t[2] ) )
return( -1 );
arry[0] = t[2]; arry[1] = t[0]; arry[2] = NULL;
if( im_wrapmany( arry, t[3],
(im_wrapmany_fn) buf_difflut, slut, NULL ) )
return( -1 );
/* Reattach ab.
*/
if( im_bandjoin( t[3], t[1], out ) )
return( -1 );
return( 0 );
}

View File

@ -0,0 +1,455 @@
/* Cored sharpen of LABQ image.
*
* Usage:
*
* int im_sharpen( IMAGE *in, IMAGE *out,
* int mask_size,
* int x1, int x2,
* double m1, double m2 )
*
* Returns 0 on success and -1 on error
*
* Copyright: 1995 A. Abbood
* Author: A. Abbood
* Written on: 30/01/1995
* 15/5/95 JC
* - updated for latest 7.3 mods
* - m3 parameter removed
* - bug fixes and speed-ups
* 4/7/95 JC
* - x3 parameter added
* - xs are now double
* 6/7/95 JC
* - xs are now ys
* - better LUT generation
* 12/3/01 JC
* - uses seperable convolution for umask
* - tiny clean ups
* 23/7/01 JC
* - fix for band extract index changed
* 21/4/04
* - switched to gaussian mask and radius
* 20/11/04
* - uses extract_bands() to remove and reattach ab for slight speedup
* - accepts LabS as well as LabQ for slight speedup
* - small code tidies
* - ~15% speed up in total
* 29/11/06
* - convolve first to help region sharing
* 3/2/10
* - gtkdoc
* - cleanups
* 13/11/13
* - redo as a class
* - does any type, any number of bands
*/
/*
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
*/
/*
#define DEBUG
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif /*HAVE_CONFIG_H*/
#include <vips/intl.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <vips/vips.h>
typedef struct _VipsSharpen {
VipsOperation parent_instance;
VipsImage *in;
VipsImage *out;
int radius;
double x1;
double y2;
double y3;
double m1;
double m2;
double x2;
double x3;
/* Parameters scaled up to int.
*
* We need indexes in the range [-x3,x2], so add x3 to
* indexes before starting to index table.
*/
int ix1;
int ix2;
int ix3;
/* The lut we build.
*/
int *lut;
} VipsSharpen;
typedef VipsOperationClass VipsSharpenClass;
G_DEFINE_TYPE( VipsSharpen, vips_sharpen, VIPS_TYPE_OPERATION );
static int
vips_sharpen_generate( VipsRegion *or,
void *vseq, void *a, void *b, gboolean *stop )
{
VipsRegion **in = (VipsRegion **) vseq;
VipsSharpen *sharpen = (VipsSharpen *) b;
VipsRect *r = &or->valid;
int ix3 = sharpen->ix3;
int range = sharpen->ix2 + sharpen->ix3;
int *lut = sharpen->lut;
int x, y;
if( vips_region_prepare( in[0], r ) ||
vips_region_prepare( in[1], r ) )
return( -1 );
for( y = 0; y < r->height; y++ ) {
short *p1 = (short *)
VIPS_REGION_ADDR( in[0], r->left, r->top + y );
short *p2 = (short *)
VIPS_REGION_ADDR( in[1], r->left, r->top + y );
short *q = (short *)
VIPS_REGION_ADDR( or, r->left, r->top + y );
for( x = 0; x < r->width; x++ ) {
int v1 = p1[x];
int v2 = p2[x];
/* v2 is the area average. If this is zero, then we
* pass the original image through unaltered.
*/
if( v2 == 0 )
q[x] = v1;
else {
/* Find difference. Offset by x3 to get the
* expected range of values.
*/
int s1 = ix3 + (v1 - v2);
int s2;
if( s1 < 0 )
s1 = 0;
else if( s1 > range )
s1 = range;
s2 = v1 + lut[s1];
if( s2 < 0 )
s2 = 0;
else if( s2 > 32767 )
s2 = 32767;
q[x] = s2;
}
}
}
return( 0 );
}
static int
vips_sharpen_build( VipsObject *object )
{
VipsObjectClass *class = VIPS_OBJECT_GET_CLASS( object );
VipsSharpen *sharpen = (VipsSharpen *) object;
VipsImage **t = (VipsImage **) vips_object_local_array( object, 7 );
VipsImage **args = (VipsImage **) vips_object_local_array( object, 2 );
VipsImage *in;
int ix1, ix2, ix3;
int i;
if( VIPS_OBJECT_CLASS( vips_sharpen_parent_class )->build( object ) )
return( -1 );
/* Turn y parameters into xs.
*/
sharpen->x2 = (sharpen->y2 -
sharpen->x1 * (sharpen->m1 - sharpen->m2)) / sharpen->m2;
sharpen->x3 = (sharpen->y3 -
sharpen->x1 * (sharpen->m1 - sharpen->m2)) / sharpen->m2;
in = sharpen->in;
if( vips_colourspace( in, &t[0], VIPS_INTERPRETATION_LABS, NULL ) )
return( -1 );
in = t[0];
if( vips_check_uncoded( class->nickname, in ) ||
vips_check_bands_atleast( class->nickname, in, 3 ) ||
vips_check_format( class->nickname, in, VIPS_FORMAT_SHORT ) )
return( -1 );
if( sharpen->x1 < 0 || sharpen->x1 > 99 ||
sharpen->x2 < 0 || sharpen->x2 > 99 ||
sharpen->x1 > sharpen->x2 ||
sharpen->x3 < 0 || sharpen->x3 > 99 ||
sharpen->x1 > sharpen->x3 ) {
vips_error( class->nickname,
"%s", _( "parameters out of range" ) );
return( -1 );
}
/* Stop at 20% of max ... bit mean, but means mask radius is roughly
* right. We always sharpen a short, so no point using a float mask.
*/
if( vips_gaussmat( &t[1], sharpen->radius / 2, 0.2,
"separable", TRUE,
"integer", TRUE,
NULL ) )
return( -1 );
#ifdef DEBUG
printf( "sharpen: blurring with:\n" );
vips_matrixprint( t[1], NULL );
#endif /*DEBUG*/
/* Build the int lut.
*/
sharpen->ix1 = ix1 = sharpen->x1 * 327.67;
sharpen->ix2 = ix2 = sharpen->x2 * 327.67;
sharpen->ix3 = ix3 = sharpen->x3 * 327.67;
if( !(sharpen->lut = VIPS_ARRAY( sharpen->out, ix2 + ix3 + 1, int )) )
return( -1 );
for( i = 0; i < ix1; i++ ) {
sharpen->lut[ix3 + i] = i * sharpen->m1;
sharpen->lut[ix3 - i] = -i * sharpen->m1;
}
for( i = ix1; i <= ix2; i++ )
sharpen->lut[ix3 + i] =
ix1 * sharpen->m1 + (i - ix1) * sharpen->m2;
for( i = ix1; i <= ix3; i++ )
sharpen->lut[ix3 - i] =
-(ix1 * sharpen->m1 + (i - ix1) * sharpen->m2);
/* Extract L and the rest, convolve L.
*/
if( vips_extract_band( in, &args[0], 0, NULL ) ||
vips_extract_band( in, &t[3], 1, "n", in->Bands - 1, NULL ) ||
vips_convsep( args[0], &args[1], t[1], NULL ) )
return( -1 );
/* Set demand hints. FATSTRIP is good for us, as THINSTRIP will cause
* too many recalculations on overlaps.
*/
t[5] = vips_image_new();
if( vips_image_pipeline_array( t[5],
VIPS_DEMAND_STYLE_FATSTRIP, args ) )
return( -1 );
if( vips_image_generate( t[5],
vips_start_many, vips_sharpen_generate, vips_stop_many,
args, sharpen ) )
return( -1 );
g_object_set( object, "out", vips_image_new(), NULL );
/* Reattach the rest.
*/
if( vips_bandjoin2( t[5], t[3], &t[6], NULL ) ||
vips_image_write( t[6], sharpen->out ) )
return( -1 );
return( 0 );
}
static void
vips_sharpen_class_init( VipsSharpenClass *class )
{
GObjectClass *gobject_class = G_OBJECT_CLASS( class );
VipsObjectClass *object_class = (VipsObjectClass *) class;
gobject_class->set_property = vips_object_set_property;
gobject_class->get_property = vips_object_get_property;
object_class->nickname = "sharpen";
object_class->description = _( "Unsharp masking for print" );
object_class->build = vips_sharpen_build;
VIPS_ARG_IMAGE( class, "in", 1,
_( "Input" ),
_( "Input image" ),
VIPS_ARGUMENT_REQUIRED_INPUT,
G_STRUCT_OFFSET( VipsSharpen, in ) );
VIPS_ARG_IMAGE( class, "out", 2,
_( "Output" ),
_( "Output image" ),
VIPS_ARGUMENT_REQUIRED_OUTPUT,
G_STRUCT_OFFSET( VipsSharpen, out ) );
VIPS_ARG_INT( class, "radius", 3,
_( "Radius" ),
_( "Mask radius" ),
VIPS_ARGUMENT_OPTIONAL_INPUT,
G_STRUCT_OFFSET( VipsSharpen, radius ),
1, 1000000, 3 );
VIPS_ARG_DOUBLE( class, "x1", 5,
_( "x1" ),
_( "Flat/jaggy threshold" ),
VIPS_ARGUMENT_OPTIONAL_INPUT,
G_STRUCT_OFFSET( VipsSharpen, x1 ),
1, 1000000, 1.5 );
VIPS_ARG_DOUBLE( class, "y2", 6,
_( "y2" ),
_( "Maximum brightening" ),
VIPS_ARGUMENT_OPTIONAL_INPUT,
G_STRUCT_OFFSET( VipsSharpen, y2 ),
1, 1000000, 20 );
VIPS_ARG_DOUBLE( class, "y3", 7,
_( "y3" ),
_( "Maximum darkening" ),
VIPS_ARGUMENT_OPTIONAL_INPUT,
G_STRUCT_OFFSET( VipsSharpen, y3 ),
1, 1000000, 50 );
VIPS_ARG_DOUBLE( class, "m1", 8,
_( "m1" ),
_( "Slope for flat areas" ),
VIPS_ARGUMENT_OPTIONAL_INPUT,
G_STRUCT_OFFSET( VipsSharpen, m1 ),
1, 1000000, 1 );
VIPS_ARG_DOUBLE( class, "m2", 9,
_( "m2" ),
_( "Slope for jaggy areas" ),
VIPS_ARGUMENT_OPTIONAL_INPUT,
G_STRUCT_OFFSET( VipsSharpen, m2 ),
1, 1000000, 2 );
}
static void
vips_sharpen_init( VipsSharpen *sharpen )
{
sharpen->radius = 3;
sharpen->x1 = 1.5;
sharpen->y2 = 20;
sharpen->y3 = 50;
sharpen->m1 = 1;
sharpen->m2 = 2;
}
/**
* vips_sharpen:
* @in: input image
* @out: output image
* @...: %NULL-terminated list of optional named arguments
*
* Optional arguments:
*
* @radius: how large a mask to use
* @x1: flat/jaggy threshold
* @y2: maximum amount of brightening
* @y3: maximum amount of darkening
* @m1: slope for flat areas
* @m2: slope for jaggy areas
*
* Selectively sharpen the L channel of a LAB image. The input image is
* transformed to #VIPS_INTERPRETATION_LABS.
*
* The operation performs a gaussian blur of radius @radius and subtracts
* from @in to generate a high-frequency signal. This signal is passed
* through a lookup table formed from the five parameters and added back to
* @in.
*
* The lookup table is formed like this:
*
* |[
^
y2 |- - - - - -----------
| /
| / slope m2
| .../
-x1 | ... |
-------------------...---------------------->
| ... | x1
|... slope m1
/ |
/ m2 |
/ |
/ |
/ |
/ |
______/ _ _ _ _ _ _ | -y3
|
* ]|
*
* For printing, we recommend the following settings (the defaults):
*
* |[
radius == 3
x1 == 1.5
y2 == 20 (don't brighten by more than 20 L*)
y3 == 50 (can darken by up to 50 L*)
m1 == 1 (some sharpening in flat areas)
m2 == 2 (more sharpening in jaggy areas)
* ]|
*
* If you want more or less sharpening, we suggest you just change the m1
* and m2 parameters.
*
* The @radius parameter changes the width of the fringe and can be
* adjusted according to the output printing resolution. As an approximate
* guideline, use 1 for 4 pixels/mm (CRT display resolution), 2 for 8
* pixels/mm, 3 for 12 pixels/mm and 4 for 16 pixels/mm (300 dpi == 12
* pixels/mm). These figures refer to the image raster, not the half-tone
* resolution.
*
* See also: im_conv().
*
* Returns: 0 on success, -1 on error.
*/
int
vips_sharpen( VipsImage *in, VipsImage **out, ... )
{
va_list ap;
int result;
va_start( ap, out );
result = vips_call_split( "sharpen", ap, in, out );
va_end( ap );
return( result );
}

View File

@ -2329,6 +2329,32 @@ im_fastcor( IMAGE *in, IMAGE *ref, IMAGE *out )
return( 0 );
}
int
im_sharpen( IMAGE *in, IMAGE *out,
int mask_size,
double x1, double y2, double y3,
double m1, double m2 )
{
VipsImage *x;
if( vips_call( "sharpen", in, &x,
"radius", mask_size / 2,
"x1", x1,
"y2", y2,
"y3", y3,
"m1", m1,
"m2", m2,
NULL ) )
return( -1 );
if( im_copy( x, out ) ) {
g_object_unref( x );
return( -1 );
}
g_object_unref( x );
return( 0 );
}
static int
vips__round( VipsImage *in, VipsImage *out, VipsOperationRound round )
{

View File

@ -1389,7 +1389,7 @@ vips_foreign_save_class_init( VipsForeignSaveClass *class )
/* I think all savers are sequential. Hopefully.
*/
operation_class->flags |= VIPS_OPERATION_SEQUENTIAL;
operation_class->flags |= VIPS_OPERATION_SEQUENTIAL_UNBUFFERED;
/* Must not cache savers.
*/
@ -2002,6 +2002,7 @@ vips_jpegload( const char *filename, VipsImage **out, ... )
* @optimize_coding: compute optimal Huffman coding tables
* @interlace: write an interlaced (progressive) jpeg
* @strip: remove all metadata from image
* @no-subsample: disable chroma subsampling
*
* Write a VIPS image to a file as JPEG.
*
@ -2036,6 +2037,9 @@ vips_jpegload( const char *filename, VipsImage **out, ... )
* If @strip is set, no EXIF data, IPCT data, ICC profile or XMP metadata is
* written into the output file.
*
* If @no-subsample is set, chrominance subsampling is disabled. This will
* improve quality at the cost of larger file size. Useful for high Q factors.
*
* See also: vips_jpegsave_buffer(), vips_image_write_file().
*
* Returns: 0 on success, -1 on error.
@ -2067,6 +2071,7 @@ vips_jpegsave( VipsImage *in, const char *filename, ... )
* @optimize_coding: compute optimal Huffman coding tables
* @interlace: write an interlaced (progressive) jpeg
* @strip: remove all metadata from image
* @no-subsample: disable chroma subsampling
*
* As vips_jpegsave(), but save to a memory buffer.
*
@ -2117,6 +2122,7 @@ vips_jpegsave_buffer( VipsImage *in, void **buf, size_t *len, ... )
* @profile: attach this ICC profile
* @optimize_coding: compute optimal Huffman coding tables
* @strip: remove all metadata from image
* @no-subsample: disable chroma subsampling
*
* As vips_jpegsave(), but save as a mime jpeg on stdout.
*

View File

@ -87,6 +87,10 @@ typedef struct _VipsForeignSaveJpeg {
*/
gboolean interlace;
/* Disable chroma subsampling.
*/
gboolean no_subsample;
} VipsForeignSaveJpeg;
typedef VipsForeignSaveClass VipsForeignSaveJpegClass;
@ -127,26 +131,33 @@ vips_foreign_save_jpeg_class_init( VipsForeignSaveJpegClass *class )
1, 100, 75 );
VIPS_ARG_STRING( class, "profile", 11,
_( "profile" ),
_( "Profile" ),
_( "ICC profile to embed" ),
VIPS_ARGUMENT_OPTIONAL_INPUT,
G_STRUCT_OFFSET( VipsForeignSaveJpeg, profile ),
NULL );
VIPS_ARG_BOOL( class, "optimize_coding", 12,
_( "optimize_coding" ),
_( "Optimize_coding" ),
_( "Compute optimal Huffman coding tables" ),
VIPS_ARGUMENT_OPTIONAL_INPUT,
G_STRUCT_OFFSET( VipsForeignSaveJpeg, optimize_coding ),
FALSE );
VIPS_ARG_BOOL( class, "interlace", 13,
_( "interlace" ),
_( "Interlace" ),
_( "Generate an interlaced (progressive) jpeg" ),
VIPS_ARGUMENT_OPTIONAL_INPUT,
G_STRUCT_OFFSET( VipsForeignSaveJpeg, interlace ),
FALSE );
VIPS_ARG_BOOL( class, "no_subsample", 14,
_( "No subsample" ),
_( "Disable chroma subsample" ),
VIPS_ARGUMENT_OPTIONAL_INPUT,
G_STRUCT_OFFSET( VipsForeignSaveJpeg, no_subsample ),
FALSE );
}
static void
@ -182,7 +193,7 @@ vips_foreign_save_jpeg_file_build( VipsObject *object )
if( vips__jpeg_write_file( save->ready, file->filename,
jpeg->Q, jpeg->profile, jpeg->optimize_coding,
jpeg->interlace, save->strip ) )
jpeg->interlace, save->strip, jpeg->no_subsample ) )
return( -1 );
return( 0 );
@ -248,7 +259,7 @@ vips_foreign_save_jpeg_buffer_build( VipsObject *object )
if( vips__jpeg_write_buffer( save->ready,
&obuf, &olen, jpeg->Q, jpeg->profile, jpeg->optimize_coding,
jpeg->interlace, save->strip ) )
jpeg->interlace, save->strip, jpeg->no_subsample ) )
return( -1 );
area = vips_area_new_blob( (VipsCallbackFn) vips_free, obuf, olen );
@ -310,7 +321,7 @@ vips_foreign_save_jpeg_mime_build( VipsObject *object )
if( vips__jpeg_write_buffer( save->ready,
&obuf, &olen, jpeg->Q, jpeg->profile, jpeg->optimize_coding,
jpeg->interlace, save->strip ) )
jpeg->interlace, save->strip, jpeg->no_subsample ) )
return( -1 );
printf( "Content-length: %zd\r\n", olen );

View File

@ -60,6 +60,8 @@
* - add optimize_coding parameter
* 12/11/13
* - add "strip" option to remove all metadata
* 13/11/13
* - add a "no_subsample" option to disable chroma subsample
*/
/*
@ -843,7 +845,8 @@ write_jpeg_block( REGION *region, Rect *area, void *a )
*/
static int
write_vips( Write *write, int qfac, const char *profile,
gboolean optimize_coding, gboolean progressive, gboolean strip )
gboolean optimize_coding, gboolean progressive, gboolean strip,
gboolean no_subsample )
{
VipsImage *in;
J_COLOR_SPACE space;
@ -905,6 +908,17 @@ write_vips( Write *write, int qfac, const char *profile,
if( progressive )
jpeg_simple_progression( &write->cinfo );
/* Turn off chroma subsampling.
*/
if( no_subsample ) {
int i;
for( i = 0; i < in->Bands; i++ ) {
write->cinfo.comp_info[i].h_samp_factor = 1;
write->cinfo.comp_info[i].v_samp_factor = 1;
}
}
/* Build compress tables.
*/
jpeg_start_compress( &write->cinfo, TRUE );
@ -952,7 +966,8 @@ write_vips( Write *write, int qfac, const char *profile,
int
vips__jpeg_write_file( VipsImage *in,
const char *filename, int Q, const char *profile,
gboolean optimize_coding, gboolean progressive, gboolean strip )
gboolean optimize_coding, gboolean progressive, gboolean strip,
gboolean no_subsample )
{
Write *write;
@ -983,7 +998,8 @@ vips__jpeg_write_file( VipsImage *in,
/* Convert!
*/
if( write_vips( write,
Q, profile, optimize_coding, progressive, strip ) ) {
Q, profile, optimize_coding, progressive, strip,
no_subsample ) ) {
write_destroy( write );
return( -1 );
}
@ -1231,7 +1247,7 @@ int
vips__jpeg_write_buffer( VipsImage *in,
void **obuf, size_t *olen, int Q, const char *profile,
gboolean optimize_coding, gboolean progressive,
gboolean strip )
gboolean strip, gboolean no_subsample )
{
Write *write;
@ -1261,7 +1277,8 @@ vips__jpeg_write_buffer( VipsImage *in,
/* Convert!
*/
if( write_vips( write,
Q, profile, optimize_coding, progressive, strip ) ) {
Q, profile, optimize_coding, progressive, strip,
no_subsample ) ) {
write_destroy( write );
return( -1 );

View File

@ -39,10 +39,12 @@ extern const char *vips__jpeg_suffs[];
int vips__jpeg_write_file( VipsImage *in,
const char *filename, int Q, const char *profile,
gboolean optimize_coding, gboolean progressive, gboolean strip );
gboolean optimize_coding, gboolean progressive, gboolean strip,
gboolean no_subsample );
int vips__jpeg_write_buffer( VipsImage *in,
void **obuf, size_t *olen, int Q, const char *profile,
gboolean optimize_coding, gboolean progressive, gboolean strip );
gboolean optimize_coding, gboolean progressive, gboolean strip,
gboolean no_subsample );
int vips__isjpeg( const char *filename );
int vips__jpeg_read_file( const char *name, VipsImage *out,

View File

@ -78,16 +78,13 @@ int vips_morph( VipsImage *in, VipsImage **out, VipsImage *mask,
VipsOperationMorphology morph, ... )
__attribute__((sentinel));
int vips_sharpen( VipsImage *in, VipsImage **out, ... )
__attribute__((sentinel));
int vips_gaussblur( VipsImage *in, VipsImage **out, int radius, ... )
__attribute__((sentinel));
void vips_convolution_operation_init( void );
int im_sharpen( VipsImage *in, VipsImage *out,
int mask_size,
double x1, double y2, double y3,
double m1, double m2 );
#ifdef __cplusplus
}
#endif /*__cplusplus*/

View File

@ -932,6 +932,11 @@ int im_fastcor( VipsImage *in, VipsImage *ref, VipsImage *out );
int im_spcor( VipsImage *in, VipsImage *ref, VipsImage *out );
int im_gradcor( VipsImage *in, VipsImage *ref, VipsImage *out );
int im_sharpen( VipsImage *in, VipsImage *out,
int mask_size,
double x1, double y2, double y3,
double m1, double m2 );
#ifdef __cplusplus
}
#endif /*__cplusplus*/