comparison mupdf-source/thirdparty/leptonica/src/hmttemplate1.txt @ 3:2c135c81b16c

MERGE: upstream PyMuPDF 1.26.4 with MuPDF 1.26.7
author Franz Glasner <fzglas.hg@dom66.de>
date Mon, 15 Sep 2025 11:44:09 +0200
parents b50eed0cc0ef
children
comparison
equal deleted inserted replaced
0:6015a75abc2d 3:2c135c81b16c
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_*()
31 --- * PIX *pixFHMTGen_*()
32 */
33
34 #include <string.h>
35 #include "allheaders.h"
36
37 --- This file is: hmttemplate1.txt
38 ---
39 --- We need to include these prototypes:
40 --- PIX *pixHMTDwa_*(PIX *pixd, PIX *pixs, l_int32 operation);
41 --- PIX *pixFHMTGen_*(PIX *pixd, PIX *pixs, l_int32 operation);
42 --- l_int32 fhmtgen_low_*(l_uint32 *datad, l_int32 w, l_int32 h,
43 --- l_int32 wpld, l_uint32 *datas,
44 --- l_int32 wpls, l_int32 index);
45 ---
46 --- We need to input two static globals here:
47 --- static l_int32 NUM_SELS_GENERATED = <some number>;
48 --- static char SEL_NAMES[][80] = {"<string1>", "<string2>", ...};
49
50 /*!
51 --- * \brief pixHMTDwa_*()
52 *
53 * \param[in] pixd usual 3 choices: null, == pixs, != pixs
54 * \param[in] pixs 1 bpp
55 * \param[in] sel name
56 * \return pixd
57 *
58 * <pre>
59 * Notes:
60 * (1) This simply adds a 32 pixel border, calls the appropriate
61 * pixFHMTGen_*(), and removes the border.
62 * See notes below for that function.
63 * </pre>
64 */
65 PIX *
66 --- pixHMTDwa_*(PIX *pixd,
67 PIX *pixs,
68 const char *selname)
69 {
70 PIX *pixt1, *pixt2, *pixt3;
71
72 --- PROCNAME("pixHMTDwa_*");
73
74 if (!pixs)
75 return (PIX *)ERROR_PTR("pixs not defined", __func__, pixd);
76 if (pixGetDepth(pixs) != 1)
77 return (PIX *)ERROR_PTR("pixs must be 1 bpp", __func__, pixd);
78
79 pixt1 = pixAddBorder(pixs, 32, 0);
80 --- pixt2 = pixFHMTGen_*(NULL, pixt1, selname);
81 pixt3 = pixRemoveBorder(pixt2, 32);
82 pixDestroy(&pixt1);
83 pixDestroy(&pixt2);
84
85 if (!pixd)
86 return pixt3;
87
88 pixCopy(pixd, pixt3);
89 pixDestroy(&pixt3);
90 return pixd;
91 }
92
93
94 /*!
95 --- * \brief pixFHMTGen_*()
96 *
97 * \param[in] pixd usual 3 choices: null, == pixs, != pixs
98 * \param[in] pixs 1 bpp
99 * \param[in] sel name
100 * \return pixd
101 *
102 * <pre>
103 * Notes:
104 * (1) This is a dwa implementation of the hit-miss transform
105 * on pixs by the sel.
106 * (2) The sel must be limited in size to not more than 31 pixels
107 * about the origin. It must have at least one hit, and it
108 * can have any number of misses.
109 * (3) This handles all required setting of the border pixels
110 * before erosion and dilation.
111 * </pre>
112 */
113 PIX *
114 --- pixFHMTGen_*(PIX *pixd,
115 PIX *pixs,
116 const char *selname)
117 {
118 l_int32 i, index, found, w, h, wpls, wpld;
119 l_uint32 *datad, *datas, *datat;
120 PIX *pixt;
121
122 --- PROCNAME("pixFHMTGen_*");
123
124 if (!pixs)
125 return (PIX *)ERROR_PTR("pixs not defined", __func__, pixd);
126 if (pixGetDepth(pixs) != 1)
127 return (PIX *)ERROR_PTR("pixs must be 1 bpp", __func__, pixd);
128
129 found = FALSE;
130 for (i = 0; i < NUM_SELS_GENERATED; i++) {
131 if (strcmp(selname, SEL_NAMES[i]) == 0) {
132 found = TRUE;
133 index = i;
134 break;
135 }
136 }
137 if (found == FALSE)
138 return (PIX *)ERROR_PTR("sel index not found", __func__, pixd);
139
140 if (!pixd) {
141 if ((pixd = pixCreateTemplate(pixs)) == NULL)
142 return (PIX *)ERROR_PTR("pixd not made", __func__, NULL);
143 }
144 else /* for in-place or pre-allocated */
145 pixResizeImageData(pixd, pixs);
146 wpls = pixGetWpl(pixs);
147 wpld = pixGetWpl(pixd);
148
149 /* The images must be surrounded with 32 additional border
150 * pixels, that we'll read from. We fabricate a "proper"
151 * image as the subimage within the border, having the
152 * following parameters: */
153 w = pixGetWidth(pixs) - 64;
154 h = pixGetHeight(pixs) - 64;
155 datas = pixGetData(pixs) + 32 * wpls + 1;
156 datad = pixGetData(pixd) + 32 * wpld + 1;
157
158 if (pixd == pixs) { /* need temp image if in-place */
159 if ((pixt = pixCopy(NULL, pixs)) == NULL)
160 return (PIX *)ERROR_PTR("pixt not made", __func__, pixd);
161 datat = pixGetData(pixt) + 32 * wpls + 1;
162 --- fhmtgen_low_*(datad, w, h, wpld, datat, wpls, index);
163 pixDestroy(&pixt);
164 }
165 else { /* not in-place */
166 --- fhmtgen_low_*(datad, w, h, wpld, datas, wpls, index);
167 }
168
169 return pixd;
170 }