comparison mupdf-source/thirdparty/leptonica/src/fhmtgen.1.c @ 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 /*!
28 * Top-level fast hit-miss transform with auto-generated sels
29 *
30 * PIX *pixHMTDwa_1()
31 * PIX *pixFHMTGen_1()
32 */
33
34 #include <string.h>
35 #include "allheaders.h"
36
37 PIX *pixHMTDwa_1(PIX *pixd, PIX *pixs, const char *selname);
38 PIX *pixFHMTGen_1(PIX *pixd, PIX *pixs, const char *selname);
39 l_int32 fhmtgen_low_1(l_uint32 *datad, l_int32 w,
40 l_int32 h, l_int32 wpld,
41 l_uint32 *datas, l_int32 wpls,
42 l_int32 index);
43
44 static l_int32 NUM_SELS_GENERATED = 10;
45 static char SEL_NAMES[][80] = {
46 "sel_3hm",
47 "sel_3de",
48 "sel_3ue",
49 "sel_3re",
50 "sel_3le",
51 "sel_sl1",
52 "sel_ulc",
53 "sel_urc",
54 "sel_llc",
55 "sel_lrc"};
56
57 /*!
58 * \brief pixHMTDwa_1()
59 *
60 * \param[in] pixd usual 3 choices: null, == pixs, != pixs
61 * \param[in] pixs 1 bpp
62 * \param[in] sel name
63 * \return pixd
64 *
65 * <pre>
66 * Notes:
67 * (1) This simply adds a 32 pixel border, calls the appropriate
68 * pixFHMTGen_*(), and removes the border.
69 * See notes below for that function.
70 * </pre>
71 */
72 PIX *
73 pixHMTDwa_1(PIX *pixd,
74 PIX *pixs,
75 const char *selname)
76 {
77 PIX *pixt1, *pixt2, *pixt3;
78
79 if (!pixs)
80 return (PIX *)ERROR_PTR("pixs not defined", __func__, pixd);
81 if (pixGetDepth(pixs) != 1)
82 return (PIX *)ERROR_PTR("pixs must be 1 bpp", __func__, pixd);
83
84 pixt1 = pixAddBorder(pixs, 32, 0);
85 pixt2 = pixFHMTGen_1(NULL, pixt1, selname);
86 pixt3 = pixRemoveBorder(pixt2, 32);
87 pixDestroy(&pixt1);
88 pixDestroy(&pixt2);
89
90 if (!pixd)
91 return pixt3;
92
93 pixCopy(pixd, pixt3);
94 pixDestroy(&pixt3);
95 return pixd;
96 }
97
98
99 /*!
100 * \brief pixFHMTGen_1()
101 *
102 * \param[in] pixd usual 3 choices: null, == pixs, != pixs
103 * \param[in] pixs 1 bpp
104 * \param[in] sel name
105 * \return pixd
106 *
107 * <pre>
108 * Notes:
109 * (1) This is a dwa implementation of the hit-miss transform
110 * on pixs by the sel.
111 * (2) The sel must be limited in size to not more than 31 pixels
112 * about the origin. It must have at least one hit, and it
113 * can have any number of misses.
114 * (3) This handles all required setting of the border pixels
115 * before erosion and dilation.
116 * </pre>
117 */
118 PIX *
119 pixFHMTGen_1(PIX *pixd,
120 PIX *pixs,
121 const char *selname)
122 {
123 l_int32 i, index, found, w, h, wpls, wpld;
124 l_uint32 *datad, *datas, *datat;
125 PIX *pixt;
126
127 if (!pixs)
128 return (PIX *)ERROR_PTR("pixs not defined", __func__, pixd);
129 if (pixGetDepth(pixs) != 1)
130 return (PIX *)ERROR_PTR("pixs must be 1 bpp", __func__, pixd);
131
132 found = FALSE;
133 for (i = 0; i < NUM_SELS_GENERATED; i++) {
134 if (strcmp(selname, SEL_NAMES[i]) == 0) {
135 found = TRUE;
136 index = i;
137 break;
138 }
139 }
140 if (found == FALSE)
141 return (PIX *)ERROR_PTR("sel index not found", __func__, pixd);
142
143 if (!pixd) {
144 if ((pixd = pixCreateTemplate(pixs)) == NULL)
145 return (PIX *)ERROR_PTR("pixd not made", __func__, NULL);
146 }
147 else /* for in-place or pre-allocated */
148 pixResizeImageData(pixd, pixs);
149 wpls = pixGetWpl(pixs);
150 wpld = pixGetWpl(pixd);
151
152 /* The images must be surrounded with 32 additional border
153 * pixels, that we'll read from. We fabricate a "proper"
154 * image as the subimage within the border, having the
155 * following parameters: */
156 w = pixGetWidth(pixs) - 64;
157 h = pixGetHeight(pixs) - 64;
158 datas = pixGetData(pixs) + 32 * wpls + 1;
159 datad = pixGetData(pixd) + 32 * wpld + 1;
160
161 if (pixd == pixs) { /* need temp image if in-place */
162 if ((pixt = pixCopy(NULL, pixs)) == NULL)
163 return (PIX *)ERROR_PTR("pixt not made", __func__, pixd);
164 datat = pixGetData(pixt) + 32 * wpls + 1;
165 fhmtgen_low_1(datad, w, h, wpld, datat, wpls, index);
166 pixDestroy(&pixt);
167 }
168 else { /* not in-place */
169 fhmtgen_low_1(datad, w, h, wpld, datas, wpls, index);
170 }
171
172 return pixd;
173 }