1 /* Cored sharpen of LABQ image.
2 *
3 * Returns 0 on success and -1 on error
4 *
5 * Copyright: 1995 A. Abbood
6 * Author: A. Abbood
7 * Written on: 30/01/1995
8 * 15/5/95 JC
9 * - updated for latest 7.3 mods
10 * - m3 parameter removed
11 * - bug fixes and speed-ups
12 * 4/7/95 JC
13 * - x3 parameter added
14 * - xs are now double
15 * 6/7/95 JC
16 * - xs are now ys
17 * - better LUT generation
18 * 12/3/01 JC
19 * - uses seperable convolution for umask
20 * - tiny clean ups
21 * 23/7/01 JC
22 * - fix for band extract index changed
23 * 21/4/04
24 * - switched to gaussian mask and radius
25 * 20/11/04
26 * - uses extract_bands() to remove and reattach ab for slight speedup
27 * - accepts LabS as well as LabQ for slight speedup
28 * - small code tidies
29 * - ~15% speed up in total
30 * 29/11/06
31 * - convolve first to help region sharing
32 * 3/2/10
33 * - gtkdoc
34 * - cleanups
35 * 13/11/13
36 * - redo as a class
37 * - does any type, any number of bands
38 * 24/2/16
39 * - swap "radius" for "sigma", allows finer control
40 * - allow a much greater range of parameters
41 * - move to defaults suitable for screen output
42 * 28/8/19
43 * - fix sigma 0.5 case (thanks 2h4dl)
44 */
45
46 /*
47
48 This file is part of VIPS.
49
50 VIPS is free software; you can redistribute it and/or modify
51 it under the terms of the GNU Lesser General Public License as published by
52 the Free Software Foundation; either version 2 of the License, or
53 (at your option) any later version.
54
55 This program is distributed in the hope that it will be useful,
56 but WITHOUT ANY WARRANTY; without even the implied warranty of
57 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
58 GNU Lesser General Public License for more details.
59
60 You should have received a copy of the GNU Lesser General Public License
61 along with this program; if not, write to the Free Software
62 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
63 02110-1301 USA
64
65 */
66
67 /*
68
69 These files are distributed with VIPS - http://www.vips.ecs.soton.ac.uk
70
71 */
72
73 /*
74 #define DEBUG
75 */
76
77 #ifdef HAVE_CONFIG_H
78 #include <config.h>
79 #endif /*HAVE_CONFIG_H*/
80 #include <vips/intl.h>
81
82 #include <stdio.h>
83 #include <stdlib.h>
84 #include <math.h>
85
86 #include <vips/vips.h>
87
88 typedef struct _VipsSharpen {
89 VipsOperation parent_instance;
90
91 VipsImage *in;
92 VipsImage *out;
93
94 double sigma;
95 double x1;
96 double y2;
97 double y3;
98 double m1;
99 double m2;
100
101 /* The lut we build.
102 */
103 int *lut;
104
105 /* We used to have a radius control.
106 */
107 int radius;
108
109 } VipsSharpen;
110
111 typedef VipsOperationClass VipsSharpenClass;
112
113 G_DEFINE_TYPE( VipsSharpen, vips_sharpen, VIPS_TYPE_OPERATION );
114
115 static int
vips_sharpen_generate(VipsRegion * or,void * vseq,void * a,void * b,gboolean * stop)116 vips_sharpen_generate( VipsRegion *or,
117 void *vseq, void *a, void *b, gboolean *stop )
118 {
119 VipsRegion **in = (VipsRegion **) vseq;
120 VipsSharpen *sharpen = (VipsSharpen *) b;
121 VipsRect *r = &or->valid;
122 int *lut = sharpen->lut;
123
124 int x, y;
125
126 if( vips_reorder_prepare_many( or->im, in, r ) )
127 return( -1 );
128
129 VIPS_GATE_START( "vips_sharpen_generate: work" );
130
131 for( y = 0; y < r->height; y++ ) {
132 short *p1 = (short * restrict)
133 VIPS_REGION_ADDR( in[0], r->left, r->top + y );
134 short *p2 = (short * restrict)
135 VIPS_REGION_ADDR( in[1], r->left, r->top + y );
136 short *q = (short * restrict)
137 VIPS_REGION_ADDR( or, r->left, r->top + y );
138
139 for( x = 0; x < r->width; x++ ) {
140 int v1 = p1[x];
141 int v2 = p2[x];
142
143 /* Our LUT is -32768 - 32767. For the v1, v2
144 * difference to be in this range, both must be 0 -
145 * 32767.
146 */
147 int diff = ((v1 & 0x7fff) - (v2 & 0x7fff));
148
149 int out;
150
151 g_assert( diff + 32768 >= 0 );
152 g_assert( diff + 32768 < 65536 );
153
154 out = v1 + lut[diff + 32768];
155
156 if( out < 0 )
157 out = 0;
158 if( out > 32767 )
159 out = 32767;
160
161 q[x] = out;
162 }
163 }
164
165 VIPS_GATE_STOP( "vips_sharpen_generate: work" );
166
167 return( 0 );
168 }
169
170 static int
vips_sharpen_build(VipsObject * object)171 vips_sharpen_build( VipsObject *object )
172 {
173 VipsObjectClass *class = VIPS_OBJECT_GET_CLASS( object );
174 VipsSharpen *sharpen = (VipsSharpen *) object;
175 VipsImage **t = (VipsImage **) vips_object_local_array( object, 8 );
176 VipsImage **args = (VipsImage **) vips_object_local_array( object, 2 );
177
178 VipsImage *in;
179 VipsInterpretation old_interpretation;
180 int i;
181
182 VIPS_GATE_START( "vips_sharpen_build: build" );
183
184 if( VIPS_OBJECT_CLASS( vips_sharpen_parent_class )->build( object ) )
185 return( -1 );
186
187 /* We used to have a radius control. If that's set but sigma isn't,
188 * use it to set a reasonable value for sigma.
189 */
190 if( !vips_object_argument_isset( object, "sigma" ) &&
191 vips_object_argument_isset( object, "radius" ) )
192 sharpen->sigma = 1 + sharpen->radius / 2;
193
194 in = sharpen->in;
195
196 old_interpretation = in->Type;
197 if( vips_colourspace( in, &t[0], VIPS_INTERPRETATION_LABS, NULL ) )
198 return( -1 );
199 in = t[0];
200
201 if( vips_check_uncoded( class->nickname, in ) ||
202 vips_check_bands_atleast( class->nickname, in, 3 ) ||
203 vips_check_format( class->nickname, in, VIPS_FORMAT_SHORT ) )
204 return( -1 );
205
206 /* Stop at 10% of max ... a bit mean. We always sharpen a short,
207 * so there's no point using a float mask.
208 */
209 if( vips_gaussmat( &t[1], sharpen->sigma, 0.1,
210 "separable", TRUE,
211 "precision", VIPS_PRECISION_INTEGER,
212 NULL ) )
213 return( -1 );
214
215 #ifdef DEBUG
216 printf( "sharpen: blurring with:\n" );
217 vips_matrixprint( t[1], NULL );
218 #endif /*DEBUG*/
219
220 /* Index with the signed difference between two 0 - 32767 images.
221 */
222 if( !(sharpen->lut = VIPS_ARRAY( object, 65536, int )) )
223 return( -1 );
224
225 for( i = 0; i < 65536; i++ ) {
226 /* Rescale to +/- 100.
227 */
228 double v = (i - 32767) / 327.67;
229 double y;
230
231 if( v < -sharpen->x1 )
232 /* Left of -x1.
233 */
234 y = (v + sharpen->x1) * sharpen->m2 +
235 -sharpen->x1 * sharpen->m1;
236 else if( v < sharpen->x1 )
237 /* Centre section.
238 */
239 y = v * sharpen->m1;
240 else
241 /* Right of x1.
242 */
243 y = (v - sharpen->x1) * sharpen->m2 +
244 sharpen->x1 * sharpen->m1;
245
246 if( y < -sharpen->y3 )
247 y = -sharpen->y3;
248 if( y > sharpen->y2 )
249 y = sharpen->y2;
250
251 sharpen->lut[i] = VIPS_RINT( y * 327.67 );
252 }
253
254 #ifdef DEBUG
255 {
256 VipsImage *mat;
257
258 mat = vips_image_new_matrix( 65536, 1 );
259 for( i = 0; i < 65536; i++ )
260 *VIPS_MATRIX( mat, i, 0 ) = sharpen->lut[i];
261 vips_image_write_to_file( mat, "x.v", NULL );
262 printf( "lut written to x.v\n" );
263 g_object_unref( mat );
264 }
265 #endif /*DEBUG*/
266
267 /* Extract L and the rest, convolve L.
268 */
269 if( vips_extract_band( in, &args[0], 0, NULL ) ||
270 vips_extract_band( in, &t[3], 1, "n", in->Bands - 1, NULL ) ||
271 vips_convsep( args[0], &args[1], t[1],
272 "precision", VIPS_PRECISION_INTEGER,
273 NULL ) )
274 return( -1 );
275
276 t[5] = vips_image_new();
277 if( vips_image_pipeline_array( t[5],
278 VIPS_DEMAND_STYLE_FATSTRIP, args ) )
279 return( -1 );
280
281 if( vips_image_generate( t[5],
282 vips_start_many, vips_sharpen_generate, vips_stop_many,
283 args, sharpen ) )
284 return( -1 );
285
286 g_object_set( object, "out", vips_image_new(), NULL );
287
288 /* Reattach the rest.
289 */
290 if( vips_bandjoin2( t[5], t[3], &t[6], NULL ) ||
291 vips_colourspace( t[6], &t[7], old_interpretation, NULL ) ||
292 vips_image_write( t[7], sharpen->out ) )
293 return( -1 );
294
295 VIPS_GATE_STOP( "vips_sharpen_build: build" );
296
297 return( 0 );
298 }
299
300 static void
vips_sharpen_class_init(VipsSharpenClass * class)301 vips_sharpen_class_init( VipsSharpenClass *class )
302 {
303 GObjectClass *gobject_class = G_OBJECT_CLASS( class );
304 VipsObjectClass *object_class = (VipsObjectClass *) class;
305 VipsOperationClass *operation_class = VIPS_OPERATION_CLASS( class );
306
307 gobject_class->set_property = vips_object_set_property;
308 gobject_class->get_property = vips_object_get_property;
309
310 object_class->nickname = "sharpen";
311 object_class->description = _( "unsharp masking for print" );
312 object_class->build = vips_sharpen_build;
313
314 operation_class->flags = VIPS_OPERATION_SEQUENTIAL;
315
316 VIPS_ARG_IMAGE( class, "in", 1,
317 _( "Input" ),
318 _( "Input image" ),
319 VIPS_ARGUMENT_REQUIRED_INPUT,
320 G_STRUCT_OFFSET( VipsSharpen, in ) );
321
322 VIPS_ARG_IMAGE( class, "out", 2,
323 _( "Output" ),
324 _( "Output image" ),
325 VIPS_ARGUMENT_REQUIRED_OUTPUT,
326 G_STRUCT_OFFSET( VipsSharpen, out ) );
327
328 VIPS_ARG_DOUBLE( class, "sigma", 3,
329 _( "Sigma" ),
330 _( "Sigma of Gaussian" ),
331 VIPS_ARGUMENT_OPTIONAL_INPUT,
332 G_STRUCT_OFFSET( VipsSharpen, sigma ),
333 0.000001, 10000.0, 0.5 );
334
335 VIPS_ARG_DOUBLE( class, "x1", 5,
336 _( "x1" ),
337 _( "Flat/jaggy threshold" ),
338 VIPS_ARGUMENT_OPTIONAL_INPUT,
339 G_STRUCT_OFFSET( VipsSharpen, x1 ),
340 0, 1000000, 2.0 );
341
342 VIPS_ARG_DOUBLE( class, "y2", 6,
343 _( "y2" ),
344 _( "Maximum brightening" ),
345 VIPS_ARGUMENT_OPTIONAL_INPUT,
346 G_STRUCT_OFFSET( VipsSharpen, y2 ),
347 0, 1000000, 10 );
348
349 VIPS_ARG_DOUBLE( class, "y3", 7,
350 _( "y3" ),
351 _( "Maximum darkening" ),
352 VIPS_ARGUMENT_OPTIONAL_INPUT,
353 G_STRUCT_OFFSET( VipsSharpen, y3 ),
354 0, 1000000, 20 );
355
356 VIPS_ARG_DOUBLE( class, "m1", 8,
357 _( "m1" ),
358 _( "Slope for flat areas" ),
359 VIPS_ARGUMENT_OPTIONAL_INPUT,
360 G_STRUCT_OFFSET( VipsSharpen, m1 ),
361 0, 1000000, 0.0 );
362
363 VIPS_ARG_DOUBLE( class, "m2", 9,
364 _( "m2" ),
365 _( "Slope for jaggy areas" ),
366 VIPS_ARGUMENT_OPTIONAL_INPUT,
367 G_STRUCT_OFFSET( VipsSharpen, m2 ),
368 0, 1000000, 3.0 );
369
370 /* We used to have a radius control.
371 */
372 VIPS_ARG_INT( class, "radius", 3,
373 _( "Radius" ),
374 _( "radius of Gaussian" ),
375 VIPS_ARGUMENT_OPTIONAL_INPUT | VIPS_ARGUMENT_DEPRECATED,
376 G_STRUCT_OFFSET( VipsSharpen, radius ),
377 1, 100, 1 );
378
379 }
380
381 static void
vips_sharpen_init(VipsSharpen * sharpen)382 vips_sharpen_init( VipsSharpen *sharpen )
383 {
384 sharpen->sigma = 0.5;
385 sharpen->x1 = 2.0;
386 sharpen->y2 = 10.0;
387 sharpen->y3 = 20.0;
388 sharpen->m1 = 0.0;
389 sharpen->m2 = 3.0;
390 }
391
392 /**
393 * vips_sharpen: (method)
394 * @in: input image
395 * @out: (out): output image
396 * @...: %NULL-terminated list of optional named arguments
397 *
398 * Optional arguments:
399 *
400 * * @sigma: sigma of gaussian
401 * * @x1: flat/jaggy threshold
402 * * @y2: maximum amount of brightening
403 * * @y3: maximum amount of darkening
404 * * @m1: slope for flat areas
405 * * @m2: slope for jaggy areas
406 *
407 * Selectively sharpen the L channel of a LAB image. The input image is
408 * transformed to #VIPS_INTERPRETATION_LABS.
409 *
410 * The operation performs a gaussian blur and subtracts from @in to generate a
411 * high-frequency signal. This signal is passed through a lookup table formed
412 * from the five parameters and added back to @in.
413 *
414 * The lookup table is formed like this:
415 *
416 * |[
417 * . ^
418 * . y2 |- - - - - -----------
419 * . | /
420 * . | / slope m2
421 * . | .../
422 * . -x1 | ... |
423 * . -------------------...---------------------->
424 * . | ... | x1
425 * . |... slope m1
426 * . / |
427 * . / m2 |
428 * . / |
429 * . / |
430 * . / |
431 * . / |
432 * . ______/ _ _ _ _ _ _ | -y3
433 * . |
434 * ]|
435 *
436 * For screen output, we suggest the following settings (the defaults):
437 *
438 * |[
439 * sigma == 0.5
440 * x1 == 2
441 * y2 == 10 (don't brighten by more than 10 L*)
442 * y3 == 20 (can darken by up to 20 L*)
443 * m1 == 0 (no sharpening in flat areas)
444 * m2 == 3 (some sharpening in jaggy areas)
445 * ]|
446 *
447 * If you want more or less sharpening, we suggest you just change the
448 * m2 parameter.
449 *
450 * The @sigma parameter changes the width of the fringe and can be
451 * adjusted according to the output printing resolution. As an approximate
452 * guideline, use 0.5 for 4 pixels/mm (display resolution),
453 * 1.0 for 12 pixels/mm and 1.5 for 16 pixels/mm (300 dpi == 12
454 * pixels/mm). These figures refer to the image raster, not the half-tone
455 * resolution.
456 *
457 * See also: vips_conv().
458 *
459 * Returns: 0 on success, -1 on error.
460 */
461 int
vips_sharpen(VipsImage * in,VipsImage ** out,...)462 vips_sharpen( VipsImage *in, VipsImage **out, ... )
463 {
464 va_list ap;
465 int result;
466
467 va_start( ap, out );
468 result = vips_call_split( "sharpen", ap, in, out );
469 va_end( ap );
470
471 return( result );
472 }
473