Leptonica  1.77.0
Image processing and image analysis suite
dnafunc1.c
Go to the documentation of this file.
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 
57 #include "allheaders.h"
58 
59 /*----------------------------------------------------------------------*
60  * Rearrangements *
61  *----------------------------------------------------------------------*/
78 l_ok
80  L_DNA *das,
81  l_int32 istart,
82  l_int32 iend)
83 {
84 l_int32 n, i;
85 l_float64 val;
86 
87  PROCNAME("l_dnaJoin");
88 
89  if (!dad)
90  return ERROR_INT("dad not defined", procName, 1);
91  if (!das)
92  return 0;
93 
94  if (istart < 0)
95  istart = 0;
96  n = l_dnaGetCount(das);
97  if (iend < 0 || iend >= n)
98  iend = n - 1;
99  if (istart > iend)
100  return ERROR_INT("istart > iend; nothing to add", procName, 1);
101 
102  for (i = istart; i <= iend; i++) {
103  l_dnaGetDValue(das, i, &val);
104  l_dnaAddNumber(dad, val);
105  }
106 
107  return 0;
108 }
109 
110 
124 L_DNA *
126 {
127 l_int32 i, nalloc;
128 L_DNA *da, *dad;
129 L_DNA **array;
130 
131  PROCNAME("l_dnaaFlattenToDna");
132 
133  if (!daa)
134  return (L_DNA *)ERROR_PTR("daa not defined", procName, NULL);
135 
136  nalloc = daa->nalloc;
137  array = daa->dna;
138  dad = l_dnaCreate(0);
139  for (i = 0; i < nalloc; i++) {
140  da = array[i];
141  if (!da) continue;
142  l_dnaJoin(dad, da, 0, -1);
143  }
144 
145  return dad;
146 }
147 
148 
149 /*----------------------------------------------------------------------*
150  * Conversion between numa and dna *
151  *----------------------------------------------------------------------*/
158 NUMA *
160 {
161 l_int32 i, n;
162 l_float64 val;
163 NUMA *na;
164 
165  PROCNAME("l_dnaConvertToNuma");
166 
167  if (!da)
168  return (NUMA *)ERROR_PTR("da not defined", procName, NULL);
169 
170  n = l_dnaGetCount(da);
171  na = numaCreate(n);
172  for (i = 0; i < n; i++) {
173  l_dnaGetDValue(da, i, &val);
174  numaAddNumber(na, val);
175  }
176  return na;
177 }
178 
179 
186 L_DNA *
188 {
189 l_int32 i, n;
190 l_float32 val;
191 L_DNA *da;
192 
193  PROCNAME("numaConvertToDna");
194 
195  if (!na)
196  return (L_DNA *)ERROR_PTR("na not defined", procName, NULL);
197 
198  n = numaGetCount(na);
199  da = l_dnaCreate(n);
200  for (i = 0; i < n; i++) {
201  numaGetFValue(na, i, &val);
202  l_dnaAddNumber(da, val);
203  }
204  return da;
205 }
206 
207 
208 /*----------------------------------------------------------------------*
209  * Set operations using aset (rbtree) *
210  *----------------------------------------------------------------------*/
225 L_DNA *
227  L_DNA *da2)
228 {
229 L_DNA *da3, *dad;
230 
231  PROCNAME("l_dnaUnionByAset");
232 
233  if (!da1)
234  return (L_DNA *)ERROR_PTR("da1 not defined", procName, NULL);
235  if (!da2)
236  return (L_DNA *)ERROR_PTR("da2 not defined", procName, NULL);
237 
238  /* Join */
239  da3 = l_dnaCopy(da1);
240  l_dnaJoin(da3, da2, 0, -1);
241 
242  /* Eliminate duplicates */
243  dad = l_dnaRemoveDupsByAset(da3);
244  l_dnaDestroy(&da3);
245  return dad;
246 }
247 
248 
255 L_DNA *
257 {
258 l_int32 i, n;
259 l_float64 val;
260 L_DNA *dad;
261 L_ASET *set;
262 RB_TYPE key;
263 
264  PROCNAME("l_dnaRemoveDupsByAset");
265 
266  if (!das)
267  return (L_DNA *)ERROR_PTR("das not defined", procName, NULL);
268 
269  set = l_asetCreate(L_FLOAT_TYPE);
270  dad = l_dnaCreate(0);
271  n = l_dnaGetCount(das);
272  for (i = 0; i < n; i++) {
273  l_dnaGetDValue(das, i, &val);
274  key.ftype = val;
275  if (!l_asetFind(set, key)) {
276  l_dnaAddNumber(dad, val);
277  l_asetInsert(set, key);
278  }
279  }
280 
281  l_asetDestroy(&set);
282  return dad;
283 }
284 
285 
300 L_DNA *
302  L_DNA *da2)
303 {
304 l_int32 n1, n2, i, n;
305 l_float64 val;
306 L_ASET *set1, *set2;
307 RB_TYPE key;
308 L_DNA *da_small, *da_big, *dad;
309 
310  PROCNAME("l_dnaIntersectionByAset");
311 
312  if (!da1)
313  return (L_DNA *)ERROR_PTR("da1 not defined", procName, NULL);
314  if (!da2)
315  return (L_DNA *)ERROR_PTR("da2 not defined", procName, NULL);
316 
317  /* Put the elements of the largest array into a set */
318  n1 = l_dnaGetCount(da1);
319  n2 = l_dnaGetCount(da2);
320  da_small = (n1 < n2) ? da1 : da2; /* do not destroy da_small */
321  da_big = (n1 < n2) ? da2 : da1; /* do not destroy da_big */
322  set1 = l_asetCreateFromDna(da_big);
323 
324  /* Build up the intersection of floats */
325  dad = l_dnaCreate(0);
326  n = l_dnaGetCount(da_small);
327  set2 = l_asetCreate(L_FLOAT_TYPE);
328  for (i = 0; i < n; i++) {
329  l_dnaGetDValue(da_small, i, &val);
330  key.ftype = val;
331  if (l_asetFind(set1, key) && !l_asetFind(set2, key)) {
332  l_dnaAddNumber(dad, val);
333  l_asetInsert(set2, key);
334  }
335  }
336 
337  l_asetDestroy(&set1);
338  l_asetDestroy(&set2);
339  return dad;
340 }
341 
342 
349 L_ASET *
351 {
352 l_int32 i, n;
353 l_float64 val;
354 L_ASET *set;
355 RB_TYPE key;
356 
357  PROCNAME("l_asetCreateFromDna");
358 
359  if (!da)
360  return (L_ASET *)ERROR_PTR("da not defined", procName, NULL);
361 
362  set = l_asetCreate(L_FLOAT_TYPE);
363  n = l_dnaGetCount(da);
364  for (i = 0; i < n; i++) {
365  l_dnaGetDValue(da, i, &val);
366  key.ftype = val;
367  l_asetInsert(set, key);
368  }
369 
370  return set;
371 }
372 
373 
374 /*----------------------------------------------------------------------*
375  * Miscellaneous operations *
376  *----------------------------------------------------------------------*/
384 L_DNA *
386 {
387 l_int32 i, n, prev, cur;
388 L_DNA *dad;
389 
390  PROCNAME("l_dnaDiffAdjValues");
391 
392  if (!das)
393  return (L_DNA *)ERROR_PTR("das not defined", procName, NULL);
394  n = l_dnaGetCount(das);
395  dad = l_dnaCreate(n - 1);
396  prev = 0;
397  for (i = 1; i < n; i++) {
398  l_dnaGetIValue(das, i, &cur);
399  l_dnaAddNumber(dad, cur - prev);
400  prev = cur;
401  }
402  return dad;
403 }
404 
l_ok l_dnaJoin(L_DNA *dad, L_DNA *das, l_int32 istart, l_int32 iend)
l_dnaJoin()
Definition: dnafunc1.c:79
l_ok numaGetFValue(NUMA *na, l_int32 index, l_float32 *pval)
numaGetFValue()
Definition: numabasic.c:692
l_int32 l_dnaGetCount(L_DNA *da)
l_dnaGetCount()
Definition: dnabasic.c:597
L_DNA * l_dnaUnionByAset(L_DNA *da1, L_DNA *da2)
l_dnaUnionByAset()
Definition: dnafunc1.c:226
l_ok numaAddNumber(NUMA *na, l_float32 val)
numaAddNumber()
Definition: numabasic.c:473
void l_dnaDestroy(L_DNA **pda)
l_dnaDestroy()
Definition: dnabasic.c:321
l_ok l_dnaGetIValue(L_DNA *da, l_int32 index, l_int32 *pival)
l_dnaGetIValue()
Definition: dnabasic.c:693
Definition: array.h:83
NUMA * numaCreate(l_int32 n)
numaCreate()
Definition: numabasic.c:187
Definition: array.h:95
l_ok l_dnaAddNumber(L_DNA *da, l_float64 val)
l_dnaAddNumber()
Definition: dnabasic.c:439
Definition: array.h:59
l_int32 numaGetCount(NUMA *na)
numaGetCount()
Definition: numabasic.c:631
L_DNA * l_dnaaFlattenToDna(L_DNAA *daa)
l_dnaaFlattenToDna()
Definition: dnafunc1.c:125
struct L_Dna ** dna
Definition: array.h:99
l_int32 nalloc
Definition: array.h:97
L_DNA * numaConvertToDna(NUMA *na)
numaConvertToDna
Definition: dnafunc1.c:187
L_DNA * l_dnaIntersectionByAset(L_DNA *da1, L_DNA *da2)
l_dnaIntersectionByAset()
Definition: dnafunc1.c:301
L_DNA * l_dnaDiffAdjValues(L_DNA *das)
l_dnaDiffAdjValues()
Definition: dnafunc1.c:385
l_ok l_dnaGetDValue(L_DNA *da, l_int32 index, l_float64 *pval)
l_dnaGetDValue()
Definition: dnabasic.c:658
L_DNA * l_dnaRemoveDupsByAset(L_DNA *das)
l_dnaRemoveDupsByAset()
Definition: dnafunc1.c:256
NUMA * l_dnaConvertToNuma(L_DNA *da)
l_dnaConvertToNuma()
Definition: dnafunc1.c:159
L_DNA * l_dnaCreate(l_int32 n)
l_dnaCreate()
Definition: dnabasic.c:169
L_DNA * l_dnaCopy(L_DNA *da)
l_dnaCopy()
Definition: dnabasic.c:360
L_ASET * l_asetCreateFromDna(L_DNA *da)
l_asetCreateFromDna()
Definition: dnafunc1.c:350
Definition: rbtree.h:61