Leptonica  1.83.1
Image processing and image analysis suite
fhmtgen.1.c
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 
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 
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 
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 }
l_uint32 * pixGetData(PIX *pix)
pixGetData()
Definition: pix1.c:1642
l_ok pixResizeImageData(PIX *pixd, const PIX *pixs)
pixResizeImageData()
Definition: pix1.c:750
void pixDestroy(PIX **ppix)
pixDestroy()
Definition: pix1.c:608
PIX * pixCopy(PIX *pixd, const PIX *pixs)
pixCopy()
Definition: pix1.c:689
PIX * pixCreateTemplate(const PIX *pixs)
pixCreateTemplate()
Definition: pix1.c:380
PIX * pixAddBorder(PIX *pixs, l_int32 npix, l_uint32 val)
pixAddBorder()
Definition: pix2.c:1773
PIX * pixRemoveBorder(PIX *pixs, l_int32 npix)
pixRemoveBorder()
Definition: pix2.c:1977