Leptonica  1.77.0
Image processing and image analysis suite
fmorphauto.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 
27 
97 #include <string.h>
98 #include "allheaders.h"
99 
100 #define OUTROOT "fmorphgen"
101 #define TEMPLATE1 "morphtemplate1.txt"
102 #define TEMPLATE2 "morphtemplate2.txt"
103 
104 #define PROTOARGS "(l_uint32 *, l_int32, l_int32, l_int32, l_uint32 *, l_int32);"
105 
106 static const l_int32 L_BUF_SIZE = 512;
107 
108 static char * makeBarrelshiftString(l_int32 delx, l_int32 dely);
109 static SARRAY * sarrayMakeInnerLoopDWACode(SEL *sel, l_int32 index);
110 static SARRAY * sarrayMakeWplsCode(SEL *sel);
111 
112 static char wpldecls[][53] = {
113  "l_int32 wpls2;",
114  "l_int32 wpls2, wpls3;",
115  "l_int32 wpls2, wpls3, wpls4;",
116  "l_int32 wpls5;",
117  "l_int32 wpls5, wpls6;",
118  "l_int32 wpls5, wpls6, wpls7;",
119  "l_int32 wpls5, wpls6, wpls7, wpls8;",
120  "l_int32 wpls9;",
121  "l_int32 wpls9, wpls10;",
122  "l_int32 wpls9, wpls10, wpls11;",
123  "l_int32 wpls9, wpls10, wpls11, wpls12;",
124  "l_int32 wpls13;",
125  "l_int32 wpls13, wpls14;",
126  "l_int32 wpls13, wpls14, wpls15;",
127  "l_int32 wpls13, wpls14, wpls15, wpls16;",
128  "l_int32 wpls17;",
129  "l_int32 wpls17, wpls18;",
130  "l_int32 wpls17, wpls18, wpls19;",
131  "l_int32 wpls17, wpls18, wpls19, wpls20;",
132  "l_int32 wpls21;",
133  "l_int32 wpls21, wpls22;",
134  "l_int32 wpls21, wpls22, wpls23;",
135  "l_int32 wpls21, wpls22, wpls23, wpls24;",
136  "l_int32 wpls25;",
137  "l_int32 wpls25, wpls26;",
138  "l_int32 wpls25, wpls26, wpls27;",
139  "l_int32 wpls25, wpls26, wpls27, wpls28;",
140  "l_int32 wpls29;",
141  "l_int32 wpls29, wpls30;",
142  "l_int32 wpls29, wpls30, wpls31;"};
143 
144 static char wplgendecls[][30] = {
145  "l_int32 wpls2;",
146  "l_int32 wpls3;",
147  "l_int32 wpls4;",
148  "l_int32 wpls5;",
149  "l_int32 wpls6;",
150  "l_int32 wpls7;",
151  "l_int32 wpls8;",
152  "l_int32 wpls9;",
153  "l_int32 wpls10;",
154  "l_int32 wpls11;",
155  "l_int32 wpls12;",
156  "l_int32 wpls13;",
157  "l_int32 wpls14;",
158  "l_int32 wpls15;",
159  "l_int32 wpls16;",
160  "l_int32 wpls17;",
161  "l_int32 wpls18;",
162  "l_int32 wpls19;",
163  "l_int32 wpls20;",
164  "l_int32 wpls21;",
165  "l_int32 wpls22;",
166  "l_int32 wpls23;",
167  "l_int32 wpls24;",
168  "l_int32 wpls25;",
169  "l_int32 wpls26;",
170  "l_int32 wpls27;",
171  "l_int32 wpls28;",
172  "l_int32 wpls29;",
173  "l_int32 wpls30;",
174  "l_int32 wpls31;"};
175 
176 static char wpldefs[][25] = {
177  " wpls2 = 2 * wpls;",
178  " wpls3 = 3 * wpls;",
179  " wpls4 = 4 * wpls;",
180  " wpls5 = 5 * wpls;",
181  " wpls6 = 6 * wpls;",
182  " wpls7 = 7 * wpls;",
183  " wpls8 = 8 * wpls;",
184  " wpls9 = 9 * wpls;",
185  " wpls10 = 10 * wpls;",
186  " wpls11 = 11 * wpls;",
187  " wpls12 = 12 * wpls;",
188  " wpls13 = 13 * wpls;",
189  " wpls14 = 14 * wpls;",
190  " wpls15 = 15 * wpls;",
191  " wpls16 = 16 * wpls;",
192  " wpls17 = 17 * wpls;",
193  " wpls18 = 18 * wpls;",
194  " wpls19 = 19 * wpls;",
195  " wpls20 = 20 * wpls;",
196  " wpls21 = 21 * wpls;",
197  " wpls22 = 22 * wpls;",
198  " wpls23 = 23 * wpls;",
199  " wpls24 = 24 * wpls;",
200  " wpls25 = 25 * wpls;",
201  " wpls26 = 26 * wpls;",
202  " wpls27 = 27 * wpls;",
203  " wpls28 = 28 * wpls;",
204  " wpls29 = 29 * wpls;",
205  " wpls30 = 30 * wpls;",
206  " wpls31 = 31 * wpls;"};
207 
208 static char wplstrp[][10] = {"+ wpls", "+ wpls2", "+ wpls3", "+ wpls4",
209  "+ wpls5", "+ wpls6", "+ wpls7", "+ wpls8",
210  "+ wpls9", "+ wpls10", "+ wpls11", "+ wpls12",
211  "+ wpls13", "+ wpls14", "+ wpls15", "+ wpls16",
212  "+ wpls17", "+ wpls18", "+ wpls19", "+ wpls20",
213  "+ wpls21", "+ wpls22", "+ wpls23", "+ wpls24",
214  "+ wpls25", "+ wpls26", "+ wpls27", "+ wpls28",
215  "+ wpls29", "+ wpls30", "+ wpls31"};
216 
217 static char wplstrm[][10] = {"- wpls", "- wpls2", "- wpls3", "- wpls4",
218  "- wpls5", "- wpls6", "- wpls7", "- wpls8",
219  "- wpls9", "- wpls10", "- wpls11", "- wpls12",
220  "- wpls13", "- wpls14", "- wpls15", "- wpls16",
221  "- wpls17", "- wpls18", "- wpls19", "- wpls20",
222  "- wpls21", "- wpls22", "- wpls23", "- wpls24",
223  "- wpls25", "- wpls26", "- wpls27", "- wpls28",
224  "- wpls29", "- wpls30", "- wpls31"};
225 
226 
242 l_ok
244  l_int32 fileindex,
245  const char *filename)
246 {
247 l_int32 ret1, ret2;
248 
249  PROCNAME("fmorphautogen");
250 
251  if (!sela)
252  return ERROR_INT("sela not defined", procName, 1);
253  ret1 = fmorphautogen1(sela, fileindex, filename);
254  ret2 = fmorphautogen2(sela, fileindex, filename);
255  if (ret1 || ret2)
256  return ERROR_INT("code generation problem", procName, 1);
257  return 0;
258 }
259 
260 
282 l_ok
284  l_int32 fileindex,
285  const char *filename)
286 {
287 char *filestr;
288 char *str_proto1, *str_proto2, *str_proto3;
289 char *str_doc1, *str_doc2, *str_doc3, *str_doc4;
290 char *str_def1, *str_def2, *str_proc1, *str_proc2;
291 char *str_dwa1, *str_low_dt, *str_low_ds, *str_low_ts;
292 char *str_low_tsp1, *str_low_dtp1;
293 char bigbuf[L_BUF_SIZE];
294 l_int32 i, nsels, nbytes, actstart, end, newstart;
295 size_t size;
296 SARRAY *sa1, *sa2, *sa3;
297 
298  PROCNAME("fmorphautogen1");
299 
300  if (!sela)
301  return ERROR_INT("sela not defined", procName, 1);
302  if (fileindex < 0)
303  fileindex = 0;
304  if ((nsels = selaGetCount(sela)) == 0)
305  return ERROR_INT("no sels in sela", procName, 1);
306 
307  /* Make array of textlines from morphtemplate1.txt */
308  if ((filestr = (char *)l_binaryRead(TEMPLATE1, &size)) == NULL)
309  return ERROR_INT("filestr not made", procName, 1);
310  sa2 = sarrayCreateLinesFromString(filestr, 1);
311  LEPT_FREE(filestr);
312  if (!sa2)
313  return ERROR_INT("sa2 not made", procName, 1);
314 
315  /* Make array of sel names */
316  sa1 = selaGetSelnames(sela);
317 
318  /* Make strings containing function call names */
319  sprintf(bigbuf, "PIX *pixMorphDwa_%d(PIX *pixd, PIX *pixs, "
320  "l_int32 operation, char *selname);", fileindex);
321  str_proto1 = stringNew(bigbuf);
322  sprintf(bigbuf, "PIX *pixFMorphopGen_%d(PIX *pixd, PIX *pixs, "
323  "l_int32 operation, char *selname);", fileindex);
324  str_proto2 = stringNew(bigbuf);
325  sprintf(bigbuf, "l_int32 fmorphopgen_low_%d(l_uint32 *datad, l_int32 w,\n"
326  " l_int32 h, l_int32 wpld,\n"
327  " l_uint32 *datas, l_int32 wpls,\n"
328  " l_int32 index);", fileindex);
329  str_proto3 = stringNew(bigbuf);
330  sprintf(bigbuf, " * PIX *pixMorphDwa_%d()", fileindex);
331  str_doc1 = stringNew(bigbuf);
332  sprintf(bigbuf, " * PIX *pixFMorphopGen_%d()", fileindex);
333  str_doc2 = stringNew(bigbuf);
334  sprintf(bigbuf, " * pixMorphDwa_%d()", fileindex);
335  str_doc3 = stringNew(bigbuf);
336  sprintf(bigbuf, " * pixFMorphopGen_%d()", fileindex);
337  str_doc4 = stringNew(bigbuf);
338  sprintf(bigbuf, "pixMorphDwa_%d(PIX *pixd,", fileindex);
339  str_def1 = stringNew(bigbuf);
340  sprintf(bigbuf, "pixFMorphopGen_%d(PIX *pixd,", fileindex);
341  str_def2 = stringNew(bigbuf);
342  sprintf(bigbuf, " PROCNAME(\"pixMorphDwa_%d\");", fileindex);
343  str_proc1 = stringNew(bigbuf);
344  sprintf(bigbuf, " PROCNAME(\"pixFMorphopGen_%d\");", fileindex);
345  str_proc2 = stringNew(bigbuf);
346  sprintf(bigbuf,
347  " pixt2 = pixFMorphopGen_%d(NULL, pixt1, operation, selname);",
348  fileindex);
349  str_dwa1 = stringNew(bigbuf);
350  sprintf(bigbuf,
351  " fmorphopgen_low_%d(datad, w, h, wpld, datat, wpls, index);",
352  fileindex);
353  str_low_dt = stringNew(bigbuf);
354  sprintf(bigbuf,
355  " fmorphopgen_low_%d(datad, w, h, wpld, datas, wpls, index);",
356  fileindex);
357  str_low_ds = stringNew(bigbuf);
358  sprintf(bigbuf,
359  " fmorphopgen_low_%d(datat, w, h, wpls, datas, wpls, index+1);",
360  fileindex);
361  str_low_tsp1 = stringNew(bigbuf);
362  sprintf(bigbuf,
363  " fmorphopgen_low_%d(datat, w, h, wpls, datas, wpls, index);",
364  fileindex);
365  str_low_ts = stringNew(bigbuf);
366  sprintf(bigbuf,
367  " fmorphopgen_low_%d(datad, w, h, wpld, datat, wpls, index+1);",
368  fileindex);
369  str_low_dtp1 = stringNew(bigbuf);
370 
371  /* Make the output sa */
372  sa3 = sarrayCreate(0);
373 
374  /* Copyright notice and info header */
375  sarrayParseRange(sa2, 0, &actstart, &end, &newstart, "--", 0);
376  sarrayAppendRange(sa3, sa2, actstart, end);
377 
378  /* Insert function names as documentation */
379  sarrayAddString(sa3, str_doc1, L_INSERT);
380  sarrayAddString(sa3, str_doc2, L_INSERT);
381 
382  /* Add '#include's */
383  sarrayParseRange(sa2, newstart, &actstart, &end, &newstart, "--", 0);
384  sarrayAppendRange(sa3, sa2, actstart, end);
385 
386  /* Insert function prototypes */
387  sarrayAddString(sa3, str_proto1, L_INSERT);
388  sarrayAddString(sa3, str_proto2, L_INSERT);
389  sarrayAddString(sa3, str_proto3, L_INSERT);
390 
391  /* Add static globals */
392  sprintf(bigbuf, "\nstatic l_int32 NUM_SELS_GENERATED = %d;", nsels);
393  sarrayAddString(sa3, bigbuf, L_COPY);
394  sprintf(bigbuf, "static char SEL_NAMES[][80] = {");
395  sarrayAddString(sa3, bigbuf, L_COPY);
396  for (i = 0; i < nsels - 1; i++) {
397  sprintf(bigbuf, " \"%s\",",
398  sarrayGetString(sa1, i, L_NOCOPY));
399  sarrayAddString(sa3, bigbuf, L_COPY);
400  }
401  sprintf(bigbuf, " \"%s\"};",
402  sarrayGetString(sa1, i, L_NOCOPY));
403  sarrayAddString(sa3, bigbuf, L_COPY);
404 
405  /* Start pixMorphDwa_*() function description */
406  sarrayParseRange(sa2, newstart, &actstart, &end, &newstart, "--", 0);
407  sarrayAppendRange(sa3, sa2, actstart, end);
408  sarrayAddString(sa3, str_doc3, L_INSERT);
409  sarrayParseRange(sa2, newstart, &actstart, &end, &newstart, "--", 0);
410  sarrayAppendRange(sa3, sa2, actstart, end);
411 
412  /* Finish pixMorphDwa_*() function definition */
413  sarrayAddString(sa3, str_def1, L_INSERT);
414  sarrayParseRange(sa2, newstart, &actstart, &end, &newstart, "--", 0);
415  sarrayAppendRange(sa3, sa2, actstart, end);
416  sarrayAddString(sa3, str_proc1, L_INSERT);
417  sarrayParseRange(sa2, newstart, &actstart, &end, &newstart, "--", 0);
418  sarrayAppendRange(sa3, sa2, actstart, end);
419  sarrayAddString(sa3, str_dwa1, L_INSERT);
420  sarrayParseRange(sa2, newstart, &actstart, &end, &newstart, "--", 0);
421  sarrayAppendRange(sa3, sa2, actstart, end);
422 
423  /* Start pixFMorphopGen_*() function description */
424  sarrayAddString(sa3, str_doc4, L_INSERT);
425  sarrayParseRange(sa2, newstart, &actstart, &end, &newstart, "--", 0);
426  sarrayAppendRange(sa3, sa2, actstart, end);
427 
428  /* Finish pixFMorphopGen_*() function definition */
429  sarrayAddString(sa3, str_def2, L_INSERT);
430  sarrayParseRange(sa2, newstart, &actstart, &end, &newstart, "--", 0);
431  sarrayAppendRange(sa3, sa2, actstart, end);
432  sarrayAddString(sa3, str_proc2, L_INSERT);
433  sarrayParseRange(sa2, newstart, &actstart, &end, &newstart, "--", 0);
434  sarrayAppendRange(sa3, sa2, actstart, end);
435  sarrayAddString(sa3, str_low_dt, L_COPY);
436  sarrayParseRange(sa2, newstart, &actstart, &end, &newstart, "--", 0);
437  sarrayAppendRange(sa3, sa2, actstart, end);
438  sarrayAddString(sa3, str_low_ds, L_INSERT);
439  sarrayParseRange(sa2, newstart, &actstart, &end, &newstart, "--", 0);
440  sarrayAppendRange(sa3, sa2, actstart, end);
441  sarrayAddString(sa3, str_low_tsp1, L_INSERT);
442  sarrayParseRange(sa2, newstart, &actstart, &end, &newstart, "--", 0);
443  sarrayAppendRange(sa3, sa2, actstart, end);
444  sarrayAddString(sa3, str_low_dt, L_INSERT);
445  sarrayParseRange(sa2, newstart, &actstart, &end, &newstart, "--", 0);
446  sarrayAppendRange(sa3, sa2, actstart, end);
447  sarrayAddString(sa3, str_low_ts, L_INSERT);
448  sarrayParseRange(sa2, newstart, &actstart, &end, &newstart, "--", 0);
449  sarrayAppendRange(sa3, sa2, actstart, end);
450  sarrayAddString(sa3, str_low_dtp1, L_INSERT);
451  sarrayParseRange(sa2, newstart, &actstart, &end, &newstart, "--", 0);
452  sarrayAppendRange(sa3, sa2, actstart, end);
453 
454  /* Output to file */
455  filestr = sarrayToString(sa3, 1);
456  nbytes = strlen(filestr);
457  if (filename)
458  snprintf(bigbuf, L_BUF_SIZE, "%s.%d.c", filename, fileindex);
459  else
460  sprintf(bigbuf, "%s.%d.c", OUTROOT, fileindex);
461  l_binaryWrite(bigbuf, "w", filestr, nbytes);
462  sarrayDestroy(&sa1);
463  sarrayDestroy(&sa2);
464  sarrayDestroy(&sa3);
465  LEPT_FREE(filestr);
466  return 0;
467 }
468 
469 
470 /*
471  * fmorphautogen2()
472  *
473  * Input: sela
474  * fileindex
475  * filename (<optional>; can be null)
476  * Return: 0 if OK; 1 on error
477  *
478  * Notes:
479  * (1) This function uses morphtemplate2.txt to create a
480  * low-level file that contains the low-level functions for
481  * implementing dilation and erosion for every sel
482  * in the input sela.
483  * (2) The fileindex parameter is inserted into the output
484  * filename, as described below.
485  * (3) If filename == NULL, the output file is fmorphgenlow.[n].c,
486  * where [n] is equal to the 'fileindex' parameter.
487  * (4) If filename != NULL, the output file is [filename]low.[n].c.
488  */
489 l_int32
490 fmorphautogen2(SELA *sela,
491  l_int32 fileindex,
492  const char *filename)
493 {
494 char *filestr, *linestr, *fname;
495 char *str_doc1, *str_doc2, *str_doc3, *str_doc4, *str_def1;
496 char bigbuf[L_BUF_SIZE];
497 char breakstring[] = " break;";
498 char staticstring[] = "static void";
499 l_int32 i, nsels, nbytes, actstart, end, newstart;
500 l_int32 argstart, argend, loopstart, loopend, finalstart, finalend;
501 size_t size;
502 SARRAY *sa1, *sa2, *sa3, *sa4, *sa5, *sa6;
503 SEL *sel;
504 
505  PROCNAME("fmorphautogen2");
506 
507  if (!sela)
508  return ERROR_INT("sela not defined", procName, 1);
509  if (fileindex < 0)
510  fileindex = 0;
511  if ((nsels = selaGetCount(sela)) == 0)
512  return ERROR_INT("no sels in sela", procName, 1);
513 
514  /* Make the array of textlines from morphtemplate2.txt */
515  if ((filestr = (char *)l_binaryRead(TEMPLATE2, &size)) == NULL)
516  return ERROR_INT("filestr not made", procName, 1);
517  sa1 = sarrayCreateLinesFromString(filestr, 1);
518  LEPT_FREE(filestr);
519  if (!sa1)
520  return ERROR_INT("sa1 not made", procName, 1);
521 
522  /* Make the array of static function names */
523  if ((sa2 = sarrayCreate(2 * nsels)) == NULL) {
524  sarrayDestroy(&sa1);
525  return ERROR_INT("sa2 not made", procName, 1);
526  }
527  for (i = 0; i < nsels; i++) {
528  sprintf(bigbuf, "fdilate_%d_%d", fileindex, i);
529  sarrayAddString(sa2, bigbuf, L_COPY);
530  sprintf(bigbuf, "ferode_%d_%d", fileindex, i);
531  sarrayAddString(sa2, bigbuf, L_COPY);
532  }
533 
534  /* Make the static prototype strings */
535  sa3 = sarrayCreate(2 * nsels); /* should be ok */
536  for (i = 0; i < 2 * nsels; i++) {
537  fname = sarrayGetString(sa2, i, L_NOCOPY);
538  sprintf(bigbuf, "static void %s%s", fname, PROTOARGS);
539  sarrayAddString(sa3, bigbuf, L_COPY);
540  }
541 
542  /* Make strings containing function names */
543  sprintf(bigbuf, " * l_int32 fmorphopgen_low_%d()",
544  fileindex);
545  str_doc1 = stringNew(bigbuf);
546  sprintf(bigbuf, " * void fdilate_%d_*()", fileindex);
547  str_doc2 = stringNew(bigbuf);
548  sprintf(bigbuf, " * void ferode_%d_*()", fileindex);
549  str_doc3 = stringNew(bigbuf);
550 
551  /* Output to this sa */
552  sa4 = sarrayCreate(0);
553 
554  /* Copyright notice and info header */
555  sarrayParseRange(sa1, 0, &actstart, &end, &newstart, "--", 0);
556  sarrayAppendRange(sa4, sa1, actstart, end);
557 
558  /* Insert function names as documentation */
559  sarrayAddString(sa4, str_doc1, L_INSERT);
560  sarrayParseRange(sa1, newstart, &actstart, &end, &newstart, "--", 0);
561  sarrayAppendRange(sa4, sa1, actstart, end);
562  sarrayAddString(sa4, str_doc2, L_INSERT);
563  sarrayAddString(sa4, str_doc3, L_INSERT);
564  sarrayParseRange(sa1, newstart, &actstart, &end, &newstart, "--", 0);
565  sarrayAppendRange(sa4, sa1, actstart, end);
566 
567  /* Insert static protos */
568  for (i = 0; i < 2 * nsels; i++) {
569  if ((linestr = sarrayGetString(sa3, i, L_COPY)) == NULL) {
570  sarrayDestroy(&sa1);
571  sarrayDestroy(&sa2);
572  sarrayDestroy(&sa3);
573  sarrayDestroy(&sa4);
574  return ERROR_INT("linestr not retrieved", procName, 1);
575  }
576  sarrayAddString(sa4, linestr, L_INSERT);
577  }
578 
579  /* More strings with function names */
580  sprintf(bigbuf, " * fmorphopgen_low_%d()", fileindex);
581  str_doc4 = stringNew(bigbuf);
582  sprintf(bigbuf, "fmorphopgen_low_%d(l_uint32 *datad,", fileindex);
583  str_def1 = stringNew(bigbuf);
584 
585  /* Insert function header */
586  sarrayParseRange(sa1, newstart, &actstart, &end, &newstart, "--", 0);
587  sarrayAppendRange(sa4, sa1, actstart, end);
588  sarrayAddString(sa4, str_doc4, L_INSERT);
589  sarrayParseRange(sa1, newstart, &actstart, &end, &newstart, "--", 0);
590  sarrayAppendRange(sa4, sa1, actstart, end);
591  sarrayAddString(sa4, str_def1, L_INSERT);
592  sarrayParseRange(sa1, newstart, &actstart, &end, &newstart, "--", 0);
593  sarrayAppendRange(sa4, sa1, actstart, end);
594 
595  /* Generate and insert the dispatcher code */
596  for (i = 0; i < 2 * nsels; i++) {
597  sprintf(bigbuf, " case %d:", i);
598  sarrayAddString(sa4, bigbuf, L_COPY);
599  sprintf(bigbuf, " %s(datad, w, h, wpld, datas, wpls);",
600  sarrayGetString(sa2, i, L_NOCOPY));
601  sarrayAddString(sa4, bigbuf, L_COPY);
602  sarrayAddString(sa4, breakstring, L_COPY);
603  }
604 
605  /* Finish the dispatcher and introduce the low-level code */
606  sarrayParseRange(sa1, newstart, &actstart, &end, &newstart, "--", 0);
607  sarrayAppendRange(sa4, sa1, actstart, end);
608 
609  /* Get the range for the args common to all functions */
610  sarrayParseRange(sa1, newstart, &argstart, &argend, &newstart, "--", 0);
611 
612  /* Get the range for the loop code common to all functions */
613  sarrayParseRange(sa1, newstart, &loopstart, &loopend, &newstart, "--", 0);
614 
615  /* Get the range for the ending code common to all functions */
616  sarrayParseRange(sa1, newstart, &finalstart, &finalend, &newstart, "--", 0);
617 
618  /* Do all the static functions */
619  for (i = 0; i < 2 * nsels; i++) {
620  /* Generate the function header and add the common args */
621  sarrayAddString(sa4, staticstring, L_COPY);
622  fname = sarrayGetString(sa2, i, L_NOCOPY);
623  sprintf(bigbuf, "%s(l_uint32 *datad,", fname);
624  sarrayAddString(sa4, bigbuf, L_COPY);
625  sarrayAppendRange(sa4, sa1, argstart, argend);
626 
627  /* Declare and define wplsN args, as necessary */
628  if ((sel = selaGetSel(sela, i/2)) == NULL) {
629  sarrayDestroy(&sa1);
630  sarrayDestroy(&sa2);
631  sarrayDestroy(&sa3);
632  sarrayDestroy(&sa4);
633  return ERROR_INT("sel not returned", procName, 1);
634  }
635  sa5 = sarrayMakeWplsCode(sel);
636  sarrayJoin(sa4, sa5);
637  sarrayDestroy(&sa5);
638 
639  /* Add the function loop code */
640  sarrayAppendRange(sa4, sa1, loopstart, loopend);
641 
642  /* Insert barrel-op code for *dptr */
643  sa6 = sarrayMakeInnerLoopDWACode(sel, i);
644  sarrayJoin(sa4, sa6);
645  sarrayDestroy(&sa6);
646 
647  /* Finish the function code */
648  sarrayAppendRange(sa4, sa1, finalstart, finalend);
649  }
650 
651  /* Output to file */
652  filestr = sarrayToString(sa4, 1);
653  nbytes = strlen(filestr);
654  if (filename)
655  snprintf(bigbuf, L_BUF_SIZE, "%slow.%d.c", filename, fileindex);
656  else
657  sprintf(bigbuf, "%slow.%d.c", OUTROOT, fileindex);
658  l_binaryWrite(bigbuf, "w", filestr, nbytes);
659  sarrayDestroy(&sa1);
660  sarrayDestroy(&sa2);
661  sarrayDestroy(&sa3);
662  sarrayDestroy(&sa4);
663  LEPT_FREE(filestr);
664  return 0;
665 }
666 
667 
668 /*--------------------------------------------------------------------------*
669  * Helper code for sel *
670  *--------------------------------------------------------------------------*/
674 static SARRAY *
676 {
677 char emptystring[] = "";
678 l_int32 i, j, ymax, dely, allvshifts;
679 l_int32 vshift[32];
680 SARRAY *sa;
681 
682  PROCNAME("sarrayMakeWplsCode");
683 
684  if (!sel)
685  return (SARRAY *)ERROR_PTR("sel not defined", procName, NULL);
686 
687  for (i = 0; i < 32; i++)
688  vshift[i] = 0;
689  ymax = 0;
690  for (i = 0; i < sel->sy; i++) {
691  for (j = 0; j < sel->sx; j++) {
692  if (sel->data[i][j] == 1) {
693  dely = L_ABS(i - sel->cy);
694  if (dely < 32)
695  vshift[dely] = 1;
696  ymax = L_MAX(ymax, dely);
697  }
698  }
699  }
700  if (ymax > 31) {
701  L_WARNING("ymax > 31; truncating to 31\n", procName);
702  ymax = 31;
703  }
704 
705  /* Test if this is a vertical brick */
706  allvshifts = TRUE;
707  for (i = 0; i < ymax; i++) {
708  if (vshift[i] == 0) {
709  allvshifts = FALSE;
710  break;
711  }
712  }
713 
714  sa = sarrayCreate(0);
715 
716  /* Add declarations */
717  if (allvshifts == TRUE) { /* packs them as well as possible */
718  if (ymax > 4)
719  sarrayAddString(sa, wpldecls[2], L_COPY);
720  if (ymax > 8)
721  sarrayAddString(sa, wpldecls[6], L_COPY);
722  if (ymax > 12)
723  sarrayAddString(sa, wpldecls[10], L_COPY);
724  if (ymax > 16)
725  sarrayAddString(sa, wpldecls[14], L_COPY);
726  if (ymax > 20)
727  sarrayAddString(sa, wpldecls[18], L_COPY);
728  if (ymax > 24)
729  sarrayAddString(sa, wpldecls[22], L_COPY);
730  if (ymax > 28)
731  sarrayAddString(sa, wpldecls[26], L_COPY);
732  if (ymax > 1)
733  sarrayAddString(sa, wpldecls[ymax - 2], L_COPY);
734  } else { /* puts them one/line */
735  for (i = 2; i <= ymax; i++) {
736  if (vshift[i])
737  sarrayAddString(sa, wplgendecls[i - 2], L_COPY);
738  }
739  }
740 
741  sarrayAddString(sa, emptystring, L_COPY);
742 
743  /* Add definitions */
744  for (i = 2; i <= ymax; i++) {
745  if (vshift[i])
746  sarrayAddString(sa, wpldefs[i - 2], L_COPY);
747  }
748 
749  return sa;
750 }
751 
752 
756 static SARRAY *
758  l_int32 index)
759 {
760 char *tstr, *string;
761 char logicalor[] = "|";
762 char logicaland[] = "&";
763 char bigbuf[L_BUF_SIZE];
764 l_int32 i, j, optype, count, nfound, delx, dely;
765 SARRAY *sa;
766 
767  PROCNAME("sarrayMakeInnerLoopDWACode");
768 
769  if (!sel)
770  return (SARRAY *)ERROR_PTR("sel not defined", procName, NULL);
771 
772  if (index % 2 == 0) {
773  optype = L_MORPH_DILATE;
774  tstr = logicalor;
775  } else {
776  optype = L_MORPH_ERODE;
777  tstr = logicaland;
778  }
779 
780  count = 0;
781  for (i = 0; i < sel->sy; i++) {
782  for (j = 0; j < sel->sx; j++) {
783  if (sel->data[i][j] == 1)
784  count++;
785  }
786  }
787 
788  sa = sarrayCreate(0);
789  if (count == 0) {
790  L_WARNING("no hits in Sel %d\n", procName, index);
791  return sa; /* no code inside! */
792  }
793 
794  nfound = 0;
795  for (i = 0; i < sel->sy; i++) {
796  for (j = 0; j < sel->sx; j++) {
797  if (sel->data[i][j] == 1) {
798  nfound++;
799  if (optype == L_MORPH_DILATE) {
800  dely = sel->cy - i;
801  delx = sel->cx - j;
802  } else { /* optype == L_MORPH_ERODE */
803  dely = i - sel->cy;
804  delx = j - sel->cx;
805  }
806  if ((string = makeBarrelshiftString(delx, dely)) == NULL) {
807  L_WARNING("barrel shift string not made\n", procName);
808  continue;
809  }
810  if (count == 1) /* just one item */
811  sprintf(bigbuf, " *dptr = %s;", string);
812  else if (nfound == 1)
813  sprintf(bigbuf, " *dptr = %s %s", string, tstr);
814  else if (nfound < count)
815  sprintf(bigbuf, " %s %s", string, tstr);
816  else /* nfound == count */
817  sprintf(bigbuf, " %s;", string);
818  sarrayAddString(sa, bigbuf, L_COPY);
819  LEPT_FREE(string);
820  }
821  }
822  }
823 
824  return sa;
825 }
826 
827 
831 static char *
832 makeBarrelshiftString(l_int32 delx, /* j - cx */
833  l_int32 dely) /* i - cy */
834 {
835 l_int32 absx, absy;
836 char bigbuf[L_BUF_SIZE];
837 
838  PROCNAME("makeBarrelshiftString");
839 
840  if (delx < -31 || delx > 31)
841  return (char *)ERROR_PTR("delx out of bounds", procName, NULL);
842  if (dely < -31 || dely > 31)
843  return (char *)ERROR_PTR("dely out of bounds", procName, NULL);
844  absx = L_ABS(delx);
845  absy = L_ABS(dely);
846 
847  if ((delx == 0) && (dely == 0))
848  sprintf(bigbuf, "(*sptr)");
849  else if ((delx == 0) && (dely < 0))
850  sprintf(bigbuf, "(*(sptr %s))", wplstrm[absy - 1]);
851  else if ((delx == 0) && (dely > 0))
852  sprintf(bigbuf, "(*(sptr %s))", wplstrp[absy - 1]);
853  else if ((delx < 0) && (dely == 0))
854  sprintf(bigbuf, "((*(sptr) >> %d) | (*(sptr - 1) << %d))",
855  absx, 32 - absx);
856  else if ((delx > 0) && (dely == 0))
857  sprintf(bigbuf, "((*(sptr) << %d) | (*(sptr + 1) >> %d))",
858  absx, 32 - absx);
859  else if ((delx < 0) && (dely < 0))
860  sprintf(bigbuf, "((*(sptr %s) >> %d) | (*(sptr %s - 1) << %d))",
861  wplstrm[absy - 1], absx, wplstrm[absy - 1], 32 - absx);
862  else if ((delx > 0) && (dely < 0))
863  sprintf(bigbuf, "((*(sptr %s) << %d) | (*(sptr %s + 1) >> %d))",
864  wplstrm[absy - 1], absx, wplstrm[absy - 1], 32 - absx);
865  else if ((delx < 0) && (dely > 0))
866  sprintf(bigbuf, "((*(sptr %s) >> %d) | (*(sptr %s - 1) << %d))",
867  wplstrp[absy - 1], absx, wplstrp[absy - 1], 32 - absx);
868  else /* ((delx > 0) && (dely > 0)) */
869  sprintf(bigbuf, "((*(sptr %s) << %d) | (*(sptr %s + 1) >> %d))",
870  wplstrp[absy - 1], absx, wplstrp[absy - 1], 32 - absx);
871 
872  return stringNew(bigbuf);
873 }
char * sarrayToString(SARRAY *sa, l_int32 addnlflag)
sarrayToString()
Definition: sarray1.c:763
Definition: pix.h:717
Definition: pix.h:716
char * stringNew(const char *src)
stringNew()
Definition: utils2.c:215
l_int32 selaGetCount(SELA *sela)
selaGetCount()
Definition: sel1.c:639
SARRAY * sarrayCreate(l_int32 n)
sarrayCreate()
Definition: sarray1.c:163
l_int32 sy
Definition: morph.h:64
l_int32 ** data
Definition: morph.h:68
SARRAY * selaGetSelnames(SELA *sela)
selaGetSelnames()
Definition: sel1.c:1148
Definition: array.h:116
l_ok sarrayAppendRange(SARRAY *sa1, SARRAY *sa2, l_int32 start, l_int32 end)
sarrayAppendRange()
Definition: sarray1.c:920
l_ok l_binaryWrite(const char *filename, const char *operation, const void *data, size_t nbytes)
l_binaryWrite()
Definition: utils2.c:1429
l_uint8 * l_binaryRead(const char *filename, size_t *pnbytes)
l_binaryRead()
Definition: utils2.c:1212
l_ok sarrayAddString(SARRAY *sa, const char *string, l_int32 copyflag)
sarrayAddString()
Definition: sarray1.c:446
l_int32 sx
Definition: morph.h:65
char * sarrayGetString(SARRAY *sa, l_int32 index, l_int32 copyflag)
sarrayGetString()
Definition: sarray1.c:681
l_int32 cx
Definition: morph.h:67
l_int32 cy
Definition: morph.h:66
SARRAY * sarrayCreateLinesFromString(const char *string, l_int32 blankflag)
sarrayCreateLinesFromString()
Definition: sarray1.c:276
static SARRAY * sarrayMakeWplsCode(SEL *sel)
sarrayMakeWplsCode()
Definition: fmorphauto.c:675
l_ok fmorphautogen1(SELA *sela, l_int32 fileindex, const char *filename)
fmorphautogen1()
Definition: fmorphauto.c:283
l_ok sarrayJoin(SARRAY *sa1, SARRAY *sa2)
sarrayJoin()
Definition: sarray1.c:880
l_ok fmorphautogen(SELA *sela, l_int32 fileindex, const char *filename)
fmorphautogen()
Definition: fmorphauto.c:243
Definition: pix.h:718
static SARRAY * sarrayMakeInnerLoopDWACode(SEL *sel, l_int32 index)
sarrayMakeInnerLoopDWACode()
Definition: fmorphauto.c:757
SEL * selaGetSel(SELA *sela, l_int32 i)
selaGetSel()
Definition: sel1.c:664
Definition: morph.h:74
l_int32 sarrayParseRange(SARRAY *sa, l_int32 start, l_int32 *pactualstart, l_int32 *pend, l_int32 *pnewstart, const char *substr, l_int32 loc)
sarrayParseRange()
Definition: sarray1.c:1260
static const l_int32 L_BUF_SIZE
Definition: classapp.c:55
static char * makeBarrelshiftString(l_int32 delx, l_int32 dely)
makeBarrelshiftString()
Definition: fmorphauto.c:832
void sarrayDestroy(SARRAY **psa)
sarrayDestroy()
Definition: sarray1.c:355