/src/libvips/libvips/convolution/sharpen.c
Line | Count | Source |
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 separable 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 <glib/gi18n-lib.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 | 2 | G_DEFINE_TYPE(VipsSharpen, vips_sharpen, VIPS_TYPE_OPERATION); |
114 | 2 | |
115 | 2 | static int |
116 | 2 | vips_sharpen_generate(VipsRegion *out_region, |
117 | 2 | void *vseq, void *a, void *b, gboolean *stop) |
118 | 2 | { |
119 | 0 | VipsRegion **in = (VipsRegion **) vseq; |
120 | 0 | VipsSharpen *sharpen = (VipsSharpen *) b; |
121 | 0 | VipsRect *r = &out_region->valid; |
122 | 0 | int *lut = sharpen->lut; |
123 | |
|
124 | 0 | int x, y; |
125 | |
|
126 | 0 | if (vips_reorder_prepare_many(out_region->im, in, r)) |
127 | 0 | return -1; |
128 | | |
129 | 0 | VIPS_GATE_START("vips_sharpen_generate: work"); |
130 | |
|
131 | 0 | for (y = 0; y < r->height; y++) { |
132 | 0 | short *p1 = (short *restrict) |
133 | 0 | VIPS_REGION_ADDR(in[0], r->left, r->top + y); |
134 | 0 | short *p2 = (short *restrict) |
135 | 0 | VIPS_REGION_ADDR(in[1], r->left, r->top + y); |
136 | 0 | short *q = (short *restrict) |
137 | 0 | VIPS_REGION_ADDR(out_region, r->left, r->top + y); |
138 | |
|
139 | 0 | for (x = 0; x < r->width; x++) { |
140 | 0 | int v1 = p1[x]; |
141 | 0 | 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 | 0 | int diff = ((v1 & 0x7fff) - (v2 & 0x7fff)); |
148 | |
|
149 | 0 | int out; |
150 | |
|
151 | 0 | g_assert(diff + 32768 >= 0); |
152 | 0 | g_assert(diff + 32768 < 65536); |
153 | | |
154 | 0 | out = v1 + lut[diff + 32768]; |
155 | |
|
156 | 0 | if (out < 0) |
157 | 0 | out = 0; |
158 | 0 | if (out > 32767) |
159 | 0 | out = 32767; |
160 | |
|
161 | 0 | q[x] = out; |
162 | 0 | } |
163 | 0 | } |
164 | | |
165 | 0 | VIPS_GATE_STOP("vips_sharpen_generate: work"); |
166 | |
|
167 | 0 | return 0; |
168 | 0 | } |
169 | | |
170 | | static int |
171 | | vips_sharpen_build(VipsObject *object) |
172 | 0 | { |
173 | 0 | VipsObjectClass *class = VIPS_OBJECT_GET_CLASS(object); |
174 | 0 | VipsSharpen *sharpen = (VipsSharpen *) object; |
175 | 0 | VipsImage **t = (VipsImage **) vips_object_local_array(object, 8); |
176 | 0 | VipsImage **args = (VipsImage **) vips_object_local_array(object, 2); |
177 | |
|
178 | 0 | VipsImage *in; |
179 | 0 | VipsInterpretation old_interpretation; |
180 | 0 | int i; |
181 | |
|
182 | 0 | VIPS_GATE_START("vips_sharpen_build: build"); |
183 | |
|
184 | 0 | if (VIPS_OBJECT_CLASS(vips_sharpen_parent_class)->build(object)) |
185 | 0 | 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 | 0 | if (!vips_object_argument_isset(object, "sigma") && |
191 | 0 | vips_object_argument_isset(object, "radius")) |
192 | 0 | sharpen->sigma = 1 + sharpen->radius / 2; // FIXME: Invalidates operation cache |
193 | |
|
194 | 0 | in = sharpen->in; |
195 | |
|
196 | 0 | old_interpretation = in->Type; |
197 | 0 | if (vips_colourspace(in, &t[0], VIPS_INTERPRETATION_LABS, NULL)) |
198 | 0 | return -1; |
199 | 0 | in = t[0]; |
200 | |
|
201 | 0 | if (vips_check_uncoded(class->nickname, in) || |
202 | 0 | vips_check_bands_atleast(class->nickname, in, 3)) |
203 | 0 | return -1; |
204 | | |
205 | | /* Stop at 10% of max ... a bit mean. We always sharpen a short, |
206 | | * so there's no point using a float mask. |
207 | | */ |
208 | 0 | if (vips_gaussmat(&t[1], sharpen->sigma, 0.1, |
209 | 0 | "separable", TRUE, |
210 | 0 | "precision", VIPS_PRECISION_INTEGER, |
211 | 0 | NULL)) |
212 | 0 | return -1; |
213 | | |
214 | | #ifdef DEBUG |
215 | | printf("sharpen: blurring with:\n"); |
216 | | vips_matrixprint(t[1], NULL); |
217 | | #endif /*DEBUG*/ |
218 | | |
219 | | /* Make sure we're short (need this for the LUT) and not eg. float LABS. |
220 | | */ |
221 | 0 | if (vips_cast_short(in, &t[2], NULL)) |
222 | 0 | return -1; |
223 | 0 | in = t[2]; |
224 | | |
225 | | /* Index with the signed difference between two 0 - 32767 images. |
226 | | */ |
227 | 0 | if (!(sharpen->lut = VIPS_ARRAY(object, 65536, int))) |
228 | 0 | return -1; |
229 | | |
230 | 0 | for (i = 0; i < 65536; i++) { |
231 | | /* Rescale to +/- 100. |
232 | | */ |
233 | 0 | double v = (i - 32767) / 327.67; |
234 | 0 | double y; |
235 | |
|
236 | 0 | if (v < -sharpen->x1) |
237 | | /* Left of -x1. |
238 | | */ |
239 | 0 | y = (v + sharpen->x1) * sharpen->m2 + |
240 | 0 | -sharpen->x1 * sharpen->m1; |
241 | 0 | else if (v < sharpen->x1) |
242 | | /* Centre section. |
243 | | */ |
244 | 0 | y = v * sharpen->m1; |
245 | 0 | else |
246 | | /* Right of x1. |
247 | | */ |
248 | 0 | y = (v - sharpen->x1) * sharpen->m2 + |
249 | 0 | sharpen->x1 * sharpen->m1; |
250 | |
|
251 | 0 | if (y < -sharpen->y3) |
252 | 0 | y = -sharpen->y3; |
253 | 0 | if (y > sharpen->y2) |
254 | 0 | y = sharpen->y2; |
255 | |
|
256 | 0 | sharpen->lut[i] = rint(y * 327.67); |
257 | 0 | } |
258 | |
|
259 | | #ifdef DEBUG |
260 | | { |
261 | | VipsImage *mat; |
262 | | |
263 | | mat = vips_image_new_matrix(65536, 1); |
264 | | for (i = 0; i < 65536; i++) |
265 | | *VIPS_MATRIX(mat, i, 0) = sharpen->lut[i]; |
266 | | vips_image_write_to_file(mat, "x.v", NULL); |
267 | | printf("lut written to x.v\n"); |
268 | | g_object_unref(mat); |
269 | | } |
270 | | #endif /*DEBUG*/ |
271 | | |
272 | | /* Extract L and the rest, convolve L. |
273 | | */ |
274 | 0 | if (vips_extract_band(in, &args[0], 0, NULL) || |
275 | 0 | vips_extract_band(in, &t[3], 1, "n", in->Bands - 1, NULL) || |
276 | 0 | vips_convsep(args[0], &args[1], t[1], |
277 | 0 | "precision", VIPS_PRECISION_INTEGER, |
278 | 0 | NULL)) |
279 | 0 | return -1; |
280 | | |
281 | 0 | t[5] = vips_image_new(); |
282 | 0 | if (vips_image_pipeline_array(t[5], |
283 | 0 | VIPS_DEMAND_STYLE_FATSTRIP, args)) |
284 | 0 | return -1; |
285 | | |
286 | 0 | if (vips_image_generate(t[5], |
287 | 0 | vips_start_many, vips_sharpen_generate, vips_stop_many, |
288 | 0 | args, sharpen)) |
289 | 0 | return -1; |
290 | | |
291 | 0 | g_object_set(object, "out", vips_image_new(), NULL); |
292 | | |
293 | | /* Reattach the rest. |
294 | | */ |
295 | 0 | if (vips_bandjoin2(t[5], t[3], &t[6], NULL) || |
296 | 0 | vips_colourspace(t[6], &t[7], old_interpretation, NULL) || |
297 | 0 | vips_image_write(t[7], sharpen->out)) |
298 | 0 | return -1; |
299 | | |
300 | 0 | VIPS_GATE_STOP("vips_sharpen_build: build"); |
301 | |
|
302 | 0 | return 0; |
303 | 0 | } |
304 | | |
305 | | static void |
306 | | vips_sharpen_class_init(VipsSharpenClass *class) |
307 | 1 | { |
308 | 1 | GObjectClass *gobject_class = G_OBJECT_CLASS(class); |
309 | 1 | VipsObjectClass *object_class = (VipsObjectClass *) class; |
310 | 1 | VipsOperationClass *operation_class = VIPS_OPERATION_CLASS(class); |
311 | | |
312 | 1 | gobject_class->set_property = vips_object_set_property; |
313 | 1 | gobject_class->get_property = vips_object_get_property; |
314 | | |
315 | 1 | object_class->nickname = "sharpen"; |
316 | 1 | object_class->description = _("unsharp masking for print"); |
317 | 1 | object_class->build = vips_sharpen_build; |
318 | | |
319 | 1 | operation_class->flags = VIPS_OPERATION_SEQUENTIAL; |
320 | | |
321 | 1 | VIPS_ARG_IMAGE(class, "in", 1, |
322 | 1 | _("Input"), |
323 | 1 | _("Input image"), |
324 | 1 | VIPS_ARGUMENT_REQUIRED_INPUT, |
325 | 1 | G_STRUCT_OFFSET(VipsSharpen, in)); |
326 | | |
327 | 1 | VIPS_ARG_IMAGE(class, "out", 2, |
328 | 1 | _("Output"), |
329 | 1 | _("Output image"), |
330 | 1 | VIPS_ARGUMENT_REQUIRED_OUTPUT, |
331 | 1 | G_STRUCT_OFFSET(VipsSharpen, out)); |
332 | | |
333 | 1 | VIPS_ARG_DOUBLE(class, "sigma", 3, |
334 | 1 | _("Sigma"), |
335 | 1 | _("Sigma of Gaussian"), |
336 | 1 | VIPS_ARGUMENT_OPTIONAL_INPUT, |
337 | 1 | G_STRUCT_OFFSET(VipsSharpen, sigma), |
338 | 1 | 0.000001, 10.0, 0.5); |
339 | | |
340 | 1 | VIPS_ARG_DOUBLE(class, "x1", 5, |
341 | 1 | _("x1"), |
342 | 1 | _("Flat/jaggy threshold"), |
343 | 1 | VIPS_ARGUMENT_OPTIONAL_INPUT, |
344 | 1 | G_STRUCT_OFFSET(VipsSharpen, x1), |
345 | 1 | 0, 1000000, 2.0); |
346 | | |
347 | 1 | VIPS_ARG_DOUBLE(class, "y2", 6, |
348 | 1 | _("y2"), |
349 | 1 | _("Maximum brightening"), |
350 | 1 | VIPS_ARGUMENT_OPTIONAL_INPUT, |
351 | 1 | G_STRUCT_OFFSET(VipsSharpen, y2), |
352 | 1 | 0, 1000000, 10); |
353 | | |
354 | 1 | VIPS_ARG_DOUBLE(class, "y3", 7, |
355 | 1 | _("y3"), |
356 | 1 | _("Maximum darkening"), |
357 | 1 | VIPS_ARGUMENT_OPTIONAL_INPUT, |
358 | 1 | G_STRUCT_OFFSET(VipsSharpen, y3), |
359 | 1 | 0, 1000000, 20); |
360 | | |
361 | 1 | VIPS_ARG_DOUBLE(class, "m1", 8, |
362 | 1 | _("m1"), |
363 | 1 | _("Slope for flat areas"), |
364 | 1 | VIPS_ARGUMENT_OPTIONAL_INPUT, |
365 | 1 | G_STRUCT_OFFSET(VipsSharpen, m1), |
366 | 1 | 0, 1000000, 0.0); |
367 | | |
368 | 1 | VIPS_ARG_DOUBLE(class, "m2", 9, |
369 | 1 | _("m2"), |
370 | 1 | _("Slope for jaggy areas"), |
371 | 1 | VIPS_ARGUMENT_OPTIONAL_INPUT, |
372 | 1 | G_STRUCT_OFFSET(VipsSharpen, m2), |
373 | 1 | 0, 1000000, 3.0); |
374 | | |
375 | | /* We used to have a radius control. |
376 | | */ |
377 | 1 | VIPS_ARG_INT(class, "radius", 3, |
378 | 1 | _("Radius"), |
379 | 1 | _("Radius of Gaussian"), |
380 | 1 | VIPS_ARGUMENT_OPTIONAL_INPUT | VIPS_ARGUMENT_DEPRECATED, |
381 | 1 | G_STRUCT_OFFSET(VipsSharpen, radius), |
382 | 1 | 1, 100, 1); |
383 | 1 | } |
384 | | |
385 | | static void |
386 | | vips_sharpen_init(VipsSharpen *sharpen) |
387 | 0 | { |
388 | 0 | sharpen->sigma = 0.5; |
389 | 0 | sharpen->x1 = 2.0; |
390 | 0 | sharpen->y2 = 10.0; |
391 | 0 | sharpen->y3 = 20.0; |
392 | 0 | sharpen->m1 = 0.0; |
393 | 0 | sharpen->m2 = 3.0; |
394 | 0 | } |
395 | | |
396 | | /** |
397 | | * vips_sharpen: (method) |
398 | | * @in: input image |
399 | | * @out: (out): output image |
400 | | * @...: `NULL`-terminated list of optional named arguments |
401 | | * |
402 | | * Selectively sharpen the L channel of a LAB image. The input image is |
403 | | * transformed to [enum@Vips.Interpretation.LABS]. |
404 | | * |
405 | | * The operation performs a gaussian blur and subtracts from @in to generate a |
406 | | * high-frequency signal. This signal is passed through a lookup table formed |
407 | | * from the five parameters and added back to @in. |
408 | | * |
409 | | * The lookup table is formed like this: |
410 | | * |
411 | | * ``` |
412 | | * ^ |
413 | | * y2 |- - - - - ----------- |
414 | | * | / |
415 | | * | / slope m2 |
416 | | * | .../ |
417 | | * -x1 | ... | |
418 | | * -------------------...----------------------> |
419 | | * | ... | x1 |
420 | | * |... slope m1 |
421 | | * / | |
422 | | * / m2 | |
423 | | * / | |
424 | | * / | |
425 | | * / | |
426 | | * / | |
427 | | * ______/ _ _ _ _ _ _ | -y3 |
428 | | * | |
429 | | * ``` |
430 | | * |
431 | | * For screen output, we suggest the following settings (the defaults): |
432 | | * |
433 | | * ``` |
434 | | * sigma == 0.5 |
435 | | * x1 == 2 |
436 | | * y2 == 10 (don't brighten by more than 10 L*) |
437 | | * y3 == 20 (can darken by up to 20 L*) |
438 | | * m1 == 0 (no sharpening in flat areas) |
439 | | * m2 == 3 (some sharpening in jaggy areas) |
440 | | * ``` |
441 | | * |
442 | | * If you want more or less sharpening, we suggest you just change the |
443 | | * m2 parameter. |
444 | | * |
445 | | * The @sigma parameter changes the width of the fringe and can be |
446 | | * adjusted according to the output printing resolution. As an approximate |
447 | | * guideline, use 0.5 for 4 pixels/mm (display resolution), |
448 | | * 1.0 for 12 pixels/mm and 1.5 for 16 pixels/mm (300 dpi == 12 |
449 | | * pixels/mm). These figures refer to the image raster, not the half-tone |
450 | | * resolution. |
451 | | * |
452 | | * ::: tip "Optional arguments" |
453 | | * * @sigma: `gdouble`, sigma of gaussian |
454 | | * * @x1: `gdouble`, flat/jaggy threshold |
455 | | * * @y2: `gdouble`, maximum amount of brightening |
456 | | * * @y3: `gdouble`, maximum amount of darkening |
457 | | * * @m1: `gdouble`, slope for flat areas |
458 | | * * @m2: `gdouble`, slope for jaggy areas |
459 | | * |
460 | | * ::: seealso |
461 | | * [method@Image.conv]. |
462 | | * |
463 | | * Returns: 0 on success, -1 on error. |
464 | | */ |
465 | | int |
466 | | vips_sharpen(VipsImage *in, VipsImage **out, ...) |
467 | 0 | { |
468 | 0 | va_list ap; |
469 | 0 | int result; |
470 | |
|
471 | 0 | va_start(ap, out); |
472 | 0 | result = vips_call_split("sharpen", ap, in, out); |
473 | 0 | va_end(ap); |
474 | |
|
475 | 0 | return result; |
476 | 0 | } |