comparison mupdf-source/thirdparty/leptonica/src/bilateral.h @ 2:b50eed0cc0ef upstream

ADD: MuPDF v1.26.7: the MuPDF source as downloaded by a default build of PyMuPDF 1.26.4. The directory name has changed: no version number in the expanded directory now.
author Franz Glasner <fzglas.hg@dom66.de>
date Mon, 15 Sep 2025 11:43:07 +0200
parents
children
comparison
equal deleted inserted replaced
1:1d09e1dec1d9 2:b50eed0cc0ef
1 /*====================================================================*
2 - Copyright (C) 2001 Leptonica. All rights reserved.
3 -
4 - Redistribution and use in source and binary forms, with or without
5 - modification, are permitted provided that the following conditions
6 - are met:
7 - 1. Redistributions of source code must retain the above copyright
8 - notice, this list of conditions and the following disclaimer.
9 - 2. Redistributions in binary form must reproduce the above
10 - copyright notice, this list of conditions and the following
11 - disclaimer in the documentation and/or other materials
12 - provided with the distribution.
13 -
14 - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
15 - ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
16 - LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
17 - A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ANY
18 - CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 - EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 - PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21 - PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22 - OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
23 - NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24 - SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 *====================================================================*/
26
27 #ifndef LEPTONICA_BILATERAL_H
28 #define LEPTONICA_BILATERAL_H
29
30 /*!
31 * \file bilateral.h
32 *
33 * <pre>
34 * Contains the following struct
35 * struct L_Bilateral
36 *
37 *
38 * For a tutorial introduction to bilateral filters, which apply a
39 * gaussian blur to smooth parts of the image while preserving edges, see
40 * http://people.csail.mit.edu/sparis/bf_course/slides/03_definition_bf.pdf
41 *
42 * We give an implementation of a bilateral filtering algorithm given in:
43 * "Real-Time O(1) Bilateral Filtering," by Yang, Tan and Ahuja, CVPR 2009
44 * which is at:
45 * http://vision.ai.uiuc.edu/~qyang6/publications/cvpr-09-qingxiong-yang.pdf
46 * This is based on an earlier algorithm by Sylvain Paris and Frédo Durand:
47 * http://people.csail.mit.edu/sparis/publi/2006/eccv/
48 * Paris_06_Fast_Approximation.pdf
49 *
50 * The kernel of the filter is a product of a spatial gaussian and a
51 * monotonically decreasing function of the difference in intensity
52 * between the source pixel and the neighboring pixel. The intensity
53 * part of the filter gives higher influence for pixels with intensities
54 * that are near to the source pixel, and the spatial part of the
55 * filter gives higher weight to pixels that are near the source pixel.
56 * This combination smooths in relatively uniform regions, while
57 * maintaining edges.
58 *
59 * The advantage of the appoach of Yang et al is that it is separable,
60 * so the computation time is linear in the gaussian filter size.
61 * Furthermore, it is possible to do much of the computation as a reduced
62 * scale, which gives a good approximation to the full resolution version
63 * but greatly speeds it up.
64 *
65 * The bilateral filtered value at x is:
66 *
67 * sum[y in N(x)]: spatial(|y - x|) * range(|I(x) - I(y)|) * I(y)
68 * I'(x) = --------------------------------------------------------------
69 * sum[y in N(x)]: spatial(|y - x|) * range(|I(x) - I(y)|)
70 *
71 * where I() is the input image, I'() is the filtered image, N(x) is the
72 * set of pixels around x in the filter support, and spatial() and range()
73 * are gaussian functions:
74 * spatial(x) = exp(-x^2 / (2 * s_s^2))
75 * range(x) = exp(-x^2 / (2 * s_r^2))
76 * and s_s and s_r and the standard deviations of the two gaussians.
77 *
78 * Yang et al use a separable approximation to this, by defining a set
79 * of related but separable functions J(k,x), that we call Principal
80 * Bilateral Components (PBC):
81 *
82 * sum[y in N(x)]: spatial(|y - x|) * range(|k - I(y)|) * I(y)
83 * J(k,x) = -----------------------------------------------------------
84 * sum[y in N(x)]: spatial(|y - x|) * range(|k - I(y)|)
85 *
86 * which are computed quickly for a set of n values k[p], p = 0 ... n-1.
87 * Then each output pixel is found using a linear interpolation:
88 *
89 * I'(x) = (1 - q) * J(k[p],x) + q * J(k[p+1],x)
90 *
91 * where J(k[p],x) and J(k[p+1],x) are PBC for which
92 * k[p] <= I(x) and k[p+1] >= I(x), and
93 * q = (I(x) - k[p]) / (k[p+1] - k[p]).
94 *
95 * We can also subsample I(x), create subsampled versions of J(k,x),
96 * which are then interpolated between for I'(x).
97 *
98 * We generate 'pixsc', by optionally downscaling the input image
99 * (using area mapping by the factor 'reduction'), and then adding
100 * a mirrored border to avoid boundary cases. This is then used
101 * to compute 'ncomps' PBCs.
102 *
103 * The 'spatial_stdev' is also downscaled by 'reduction'. The size
104 * of the 'spatial' array is 4 * (reduced 'spatial_stdev') + 1.
105 * The size of the 'range' array is 256.
106 * </pre>
107 */
108
109
110 /*------------------------------------------------------------------------*
111 * Bilateral filter *
112 *------------------------------------------------------------------------*/
113
114 /*! Bilateral filter */
115 struct L_Bilateral
116 {
117 struct Pix *pixs; /*!< clone of source pix */
118 struct Pix *pixsc; /*!< downscaled pix with mirrored border */
119 l_int32 reduction; /*!< 1, 2 or 4x for intermediates */
120 l_float32 spatial_stdev; /*!< stdev of spatial gaussian */
121 l_float32 range_stdev; /*!< stdev of range gaussian */
122 l_float32 *spatial; /*!< 1D gaussian spatial kernel */
123 l_float32 *range; /*!< one-sided gaussian range kernel */
124 l_int32 minval; /*!< min value in 8 bpp pix */
125 l_int32 maxval; /*!< max value in 8 bpp pix */
126 l_int32 ncomps; /*!< number of intermediate results */
127 l_int32 *nc; /*!< set of k values (size ncomps) */
128 l_int32 *kindex; /*!< mapping from intensity to lower k */
129 l_float32 *kfract; /*!< mapping from intensity to fract k */
130 struct Pixa *pixac; /*!< intermediate result images (PBC) */
131 l_uint32 ***lineset; /*!< lineptrs for pixac */
132 };
133 typedef struct L_Bilateral L_BILATERAL;
134
135
136 #endif /* LEPTONICA_BILATERAL_H */