Leptonica  1.77.0
Image processing and image analysis suite
fhmtauto.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 
93 #include <string.h>
94 #include "allheaders.h"
95 
96 #define OUTROOT "fhmtgen"
97 #define TEMPLATE1 "hmttemplate1.txt"
98 #define TEMPLATE2 "hmttemplate2.txt"
99 
100 #define PROTOARGS "(l_uint32 *, l_int32, l_int32, l_int32, l_uint32 *, l_int32);"
101 
102 static const l_int32 L_BUF_SIZE = 512;
103 
104 static char * makeBarrelshiftString(l_int32 delx, l_int32 dely, l_int32 type);
105 static SARRAY * sarrayMakeInnerLoopDWACode(SEL *sel, l_int32 nhits, l_int32 nmisses);
106 static SARRAY * sarrayMakeWplsCode(SEL *sel);
107 
108 static char wpldecls[][60] = {
109  "l_int32 wpls2;",
110  "l_int32 wpls2, wpls3;",
111  "l_int32 wpls2, wpls3, wpls4;",
112  "l_int32 wpls5;",
113  "l_int32 wpls5, wpls6;",
114  "l_int32 wpls5, wpls6, wpls7;",
115  "l_int32 wpls5, wpls6, wpls7, wpls8;",
116  "l_int32 wpls9;",
117  "l_int32 wpls9, wpls10;",
118  "l_int32 wpls9, wpls10, wpls11;",
119  "l_int32 wpls9, wpls10, wpls11, wpls12;",
120  "l_int32 wpls13;",
121  "l_int32 wpls13, wpls14;",
122  "l_int32 wpls13, wpls14, wpls15;",
123  "l_int32 wpls13, wpls14, wpls15, wpls16;",
124  "l_int32 wpls17;",
125  "l_int32 wpls17, wpls18;",
126  "l_int32 wpls17, wpls18, wpls19;",
127  "l_int32 wpls17, wpls18, wpls19, wpls20;",
128  "l_int32 wpls21;",
129  "l_int32 wpls21, wpls22;",
130  "l_int32 wpls21, wpls22, wpls23;",
131  "l_int32 wpls21, wpls22, wpls23, wpls24;",
132  "l_int32 wpls25;",
133  "l_int32 wpls25, wpls26;",
134  "l_int32 wpls25, wpls26, wpls27;",
135  "l_int32 wpls25, wpls26, wpls27, wpls28;",
136  "l_int32 wpls29;",
137  "l_int32 wpls29, wpls30;",
138  "l_int32 wpls29, wpls30, wpls31;"};
139 
140 static char wpldefs[][24] = {
141  " wpls2 = 2 * wpls;",
142  " wpls3 = 3 * wpls;",
143  " wpls4 = 4 * wpls;",
144  " wpls5 = 5 * wpls;",
145  " wpls6 = 6 * wpls;",
146  " wpls7 = 7 * wpls;",
147  " wpls8 = 8 * wpls;",
148  " wpls9 = 9 * wpls;",
149  " wpls10 = 10 * wpls;",
150  " wpls11 = 11 * wpls;",
151  " wpls12 = 12 * wpls;",
152  " wpls13 = 13 * wpls;",
153  " wpls14 = 14 * wpls;",
154  " wpls15 = 15 * wpls;",
155  " wpls16 = 16 * wpls;",
156  " wpls17 = 17 * wpls;",
157  " wpls18 = 18 * wpls;",
158  " wpls19 = 19 * wpls;",
159  " wpls20 = 20 * wpls;",
160  " wpls21 = 21 * wpls;",
161  " wpls22 = 22 * wpls;",
162  " wpls23 = 23 * wpls;",
163  " wpls24 = 24 * wpls;",
164  " wpls25 = 25 * wpls;",
165  " wpls26 = 26 * wpls;",
166  " wpls27 = 27 * wpls;",
167  " wpls28 = 28 * wpls;",
168  " wpls29 = 29 * wpls;",
169  " wpls30 = 30 * wpls;",
170  " wpls31 = 31 * wpls;"};
171 
172 static char wplstrp[][10] = {"+ wpls", "+ wpls2", "+ wpls3", "+ wpls4",
173  "+ wpls5", "+ wpls6", "+ wpls7", "+ wpls8",
174  "+ wpls9", "+ wpls10", "+ wpls11", "+ wpls12",
175  "+ wpls13", "+ wpls14", "+ wpls15", "+ wpls16",
176  "+ wpls17", "+ wpls18", "+ wpls19", "+ wpls20",
177  "+ wpls21", "+ wpls22", "+ wpls23", "+ wpls24",
178  "+ wpls25", "+ wpls26", "+ wpls27", "+ wpls28",
179  "+ wpls29", "+ wpls30", "+ wpls31"};
180 
181 static char wplstrm[][10] = {"- wpls", "- wpls2", "- wpls3", "- wpls4",
182  "- wpls5", "- wpls6", "- wpls7", "- wpls8",
183  "- wpls9", "- wpls10", "- wpls11", "- wpls12",
184  "- wpls13", "- wpls14", "- wpls15", "- wpls16",
185  "- wpls17", "- wpls18", "- wpls19", "- wpls20",
186  "- wpls21", "- wpls22", "- wpls23", "- wpls24",
187  "- wpls25", "- wpls26", "- wpls27", "- wpls28",
188  "- wpls29", "- wpls30", "- wpls31"};
189 
190 
206 l_ok
208  l_int32 fileindex,
209  const char *filename)
210 {
211 l_int32 ret1, ret2;
212 
213  PROCNAME("fhmtautogen");
214 
215  if (!sela)
216  return ERROR_INT("sela not defined", procName, 1);
217  ret1 = fhmtautogen1(sela, fileindex, filename);
218  ret2 = fhmtautogen2(sela, fileindex, filename);
219  if (ret1 || ret2)
220  return ERROR_INT("code generation problem", procName, 1);
221  return 0;
222 }
223 
224 
248 l_ok
250  l_int32 fileindex,
251  const char *filename)
252 {
253 char *filestr;
254 char *str_proto1, *str_proto2, *str_proto3;
255 char *str_doc1, *str_doc2, *str_doc3, *str_doc4;
256 char *str_def1, *str_def2, *str_proc1, *str_proc2;
257 char *str_dwa1, *str_low_dt, *str_low_ds;
258 char bigbuf[L_BUF_SIZE];
259 l_int32 i, nsels, nbytes, actstart, end, newstart;
260 size_t size;
261 SARRAY *sa1, *sa2, *sa3;
262 
263  PROCNAME("fhmtautogen1");
264 
265  if (!sela)
266  return ERROR_INT("sela not defined", procName, 1);
267  if (fileindex < 0)
268  fileindex = 0;
269  if ((nsels = selaGetCount(sela)) == 0)
270  return ERROR_INT("no sels in sela", procName, 1);
271 
272  /* Make array of textlines from from hmttemplate1.txt */
273  if ((filestr = (char *)l_binaryRead(TEMPLATE1, &size)) == NULL)
274  return ERROR_INT("filestr not made", procName, 1);
275  sa2 = sarrayCreateLinesFromString(filestr, 1);
276  LEPT_FREE(filestr);
277  if (!sa2)
278  return ERROR_INT("sa2 not made", procName, 1);
279 
280  /* Make array of sel names */
281  sa1 = selaGetSelnames(sela);
282 
283  /* Make strings containing function call names */
284  sprintf(bigbuf, "PIX *pixHMTDwa_%d(PIX *pixd, PIX *pixs, "
285  "const char *selname);", fileindex);
286  str_proto1 = stringNew(bigbuf);
287  sprintf(bigbuf, "PIX *pixFHMTGen_%d(PIX *pixd, PIX *pixs, "
288  "const char *selname);", fileindex);
289  str_proto2 = stringNew(bigbuf);
290  sprintf(bigbuf, "l_int32 fhmtgen_low_%d(l_uint32 *datad, l_int32 w,\n"
291  " l_int32 h, l_int32 wpld,\n"
292  " l_uint32 *datas, l_int32 wpls,\n"
293  " l_int32 index);", fileindex);
294  str_proto3 = stringNew(bigbuf);
295  sprintf(bigbuf, " * PIX *pixHMTDwa_%d()", fileindex);
296  str_doc1 = stringNew(bigbuf);
297  sprintf(bigbuf, " * PIX *pixFHMTGen_%d()", fileindex);
298  str_doc2 = stringNew(bigbuf);
299  sprintf(bigbuf, " * pixHMTDwa_%d()", fileindex);
300  str_doc3 = stringNew(bigbuf);
301  sprintf(bigbuf, " * pixFHMTGen_%d()", fileindex);
302  str_doc4 = stringNew(bigbuf);
303  sprintf(bigbuf, "pixHMTDwa_%d(PIX *pixd,", fileindex);
304  str_def1 = stringNew(bigbuf);
305  sprintf(bigbuf, "pixFHMTGen_%d(PIX *pixd,", fileindex);
306  str_def2 = stringNew(bigbuf);
307  sprintf(bigbuf, " PROCNAME(\"pixHMTDwa_%d\");", fileindex);
308  str_proc1 = stringNew(bigbuf);
309  sprintf(bigbuf, " PROCNAME(\"pixFHMTGen_%d\");", fileindex);
310  str_proc2 = stringNew(bigbuf);
311  sprintf(bigbuf, " pixt2 = pixFHMTGen_%d(NULL, pixt1, selname);",
312  fileindex);
313  str_dwa1 = stringNew(bigbuf);
314  sprintf(bigbuf,
315  " fhmtgen_low_%d(datad, w, h, wpld, datat, wpls, index);",
316  fileindex);
317  str_low_dt = stringNew(bigbuf);
318  sprintf(bigbuf,
319  " fhmtgen_low_%d(datad, w, h, wpld, datas, wpls, index);",
320  fileindex);
321  str_low_ds = stringNew(bigbuf);
322 
323  /* Make the output sa */
324  sa3 = sarrayCreate(0);
325 
326  /* Copyright notice and info header */
327  sarrayParseRange(sa2, 0, &actstart, &end, &newstart, "--", 0);
328  sarrayAppendRange(sa3, sa2, actstart, end);
329 
330  /* Insert function names as documentation */
331  sarrayAddString(sa3, str_doc1, L_INSERT);
332  sarrayAddString(sa3, str_doc2, L_INSERT);
333 
334  /* Add '#include's */
335  sarrayParseRange(sa2, newstart, &actstart, &end, &newstart, "--", 0);
336  sarrayAppendRange(sa3, sa2, actstart, end);
337 
338  /* Insert function prototypes */
339  sarrayAddString(sa3, str_proto1, L_INSERT);
340  sarrayAddString(sa3, str_proto2, L_INSERT);
341  sarrayAddString(sa3, str_proto3, L_INSERT);
342 
343  /* Add static globals */
344  sprintf(bigbuf, "\nstatic l_int32 NUM_SELS_GENERATED = %d;", nsels);
345  sarrayAddString(sa3, bigbuf, L_COPY);
346  sprintf(bigbuf, "static char SEL_NAMES[][80] = {");
347  sarrayAddString(sa3, bigbuf, L_COPY);
348  for (i = 0; i < nsels - 1; i++) {
349  sprintf(bigbuf, " \"%s\",",
350  sarrayGetString(sa1, i, L_NOCOPY));
351  sarrayAddString(sa3, bigbuf, L_COPY);
352  }
353  sprintf(bigbuf, " \"%s\"};",
354  sarrayGetString(sa1, i, L_NOCOPY));
355  sarrayAddString(sa3, bigbuf, L_COPY);
356 
357  /* Start pixHMTDwa_*() function description */
358  sarrayParseRange(sa2, newstart, &actstart, &end, &newstart, "--", 0);
359  sarrayAppendRange(sa3, sa2, actstart, end);
360  sarrayAddString(sa3, str_doc3, L_INSERT);
361  sarrayParseRange(sa2, newstart, &actstart, &end, &newstart, "--", 0);
362  sarrayAppendRange(sa3, sa2, actstart, end);
363 
364  /* Finish pixHMTDwa_*() function definition */
365  sarrayAddString(sa3, str_def1, L_INSERT);
366  sarrayParseRange(sa2, newstart, &actstart, &end, &newstart, "--", 0);
367  sarrayAppendRange(sa3, sa2, actstart, end);
368  sarrayAddString(sa3, str_proc1, L_INSERT);
369  sarrayParseRange(sa2, newstart, &actstart, &end, &newstart, "--", 0);
370  sarrayAppendRange(sa3, sa2, actstart, end);
371  sarrayAddString(sa3, str_dwa1, L_INSERT);
372  sarrayParseRange(sa2, newstart, &actstart, &end, &newstart, "--", 0);
373  sarrayAppendRange(sa3, sa2, actstart, end);
374 
375  /* Start pixFHMTGen_*() function description */
376  sarrayAddString(sa3, str_doc4, L_INSERT);
377  sarrayParseRange(sa2, newstart, &actstart, &end, &newstart, "--", 0);
378  sarrayAppendRange(sa3, sa2, actstart, end);
379 
380  /* Finish pixFHMTGen_*() function description */
381  sarrayAddString(sa3, str_def2, L_INSERT);
382  sarrayParseRange(sa2, newstart, &actstart, &end, &newstart, "--", 0);
383  sarrayAppendRange(sa3, sa2, actstart, end);
384  sarrayAddString(sa3, str_proc2, L_INSERT);
385  sarrayParseRange(sa2, newstart, &actstart, &end, &newstart, "--", 0);
386  sarrayAppendRange(sa3, sa2, actstart, end);
387  sarrayAddString(sa3, str_low_dt, L_INSERT);
388  sarrayParseRange(sa2, newstart, &actstart, &end, &newstart, "--", 0);
389  sarrayAppendRange(sa3, sa2, actstart, end);
390  sarrayAddString(sa3, str_low_ds, L_INSERT);
391  sarrayParseRange(sa2, newstart, &actstart, &end, &newstart, "--", 0);
392  sarrayAppendRange(sa3, sa2, actstart, end);
393 
394  filestr = sarrayToString(sa3, 1);
395  nbytes = strlen(filestr);
396  if (filename)
397  snprintf(bigbuf, L_BUF_SIZE, "%s.%d.c", filename, fileindex);
398  else
399  sprintf(bigbuf, "%s.%d.c", OUTROOT, fileindex);
400  l_binaryWrite(bigbuf, "w", filestr, nbytes);
401  sarrayDestroy(&sa1);
402  sarrayDestroy(&sa2);
403  sarrayDestroy(&sa3);
404  LEPT_FREE(filestr);
405  return 0;
406 }
407 
408 
430 l_ok
432  l_int32 fileindex,
433  const char *filename)
434 {
435 char *filestr, *fname, *linestr;
436 char *str_doc1, *str_doc2, *str_doc3, *str_def1;
437 char bigbuf[L_BUF_SIZE];
438 char breakstring[] = " break;";
439 char staticstring[] = "static void";
440 l_int32 i, k, l, nsels, nbytes, nhits, nmisses;
441 l_int32 actstart, end, newstart;
442 l_int32 argstart, argend, loopstart, loopend, finalstart, finalend;
443 size_t size;
444 SARRAY *sa1, *sa2, *sa3, *sa4, *sa5, *sa6;
445 SEL *sel;
446 
447  PROCNAME("fhmtautogen2");
448 
449  if (!sela)
450  return ERROR_INT("sela not defined", procName, 1);
451  if (fileindex < 0)
452  fileindex = 0;
453  if ((nsels = selaGetCount(sela)) == 0)
454  return ERROR_INT("no sels in sela", procName, 1);
455 
456  /* Make the array of textlines from hmttemplate2.txt */
457  if ((filestr = (char *)l_binaryRead(TEMPLATE2, &size)) == NULL)
458  return ERROR_INT("filestr not made", procName, 1);
459  sa1 = sarrayCreateLinesFromString(filestr, 1);
460  LEPT_FREE(filestr);
461  if (!sa1)
462  return ERROR_INT("sa1 not made", procName, 1);
463 
464  /* Make the array of static function names */
465  if ((sa2 = sarrayCreate(nsels)) == NULL) {
466  sarrayDestroy(&sa1);
467  return ERROR_INT("sa2 not made", procName, 1);
468  }
469  for (i = 0; i < nsels; i++) {
470  sprintf(bigbuf, "fhmt_%d_%d", fileindex, i);
471  sarrayAddString(sa2, bigbuf, L_COPY);
472  }
473 
474  /* Make the static prototype strings */
475  sa3 = sarrayCreate(2 * nsels); /* should be ok */
476  for (i = 0; i < nsels; i++) {
477  fname = sarrayGetString(sa2, i, L_NOCOPY);
478  sprintf(bigbuf, "static void %s%s", fname, PROTOARGS);
479  sarrayAddString(sa3, bigbuf, L_COPY);
480  }
481 
482  /* Make strings containing function names */
483  sprintf(bigbuf, " * l_int32 fhmtgen_low_%d()",
484  fileindex);
485  str_doc1 = stringNew(bigbuf);
486  sprintf(bigbuf, " * void fhmt_%d_*()", fileindex);
487  str_doc2 = stringNew(bigbuf);
488 
489  /* Output to this sa */
490  sa4 = sarrayCreate(0);
491 
492  /* Copyright notice and info header */
493  sarrayParseRange(sa1, 0, &actstart, &end, &newstart, "--", 0);
494  sarrayAppendRange(sa4, sa1, actstart, end);
495 
496  /* Insert function names as documentation */
497  sarrayAddString(sa4, str_doc1, L_INSERT);
498  sarrayParseRange(sa1, newstart, &actstart, &end, &newstart, "--", 0);
499  sarrayAppendRange(sa4, sa1, actstart, end);
500  sarrayAddString(sa4, str_doc2, L_INSERT);
501  sarrayParseRange(sa1, newstart, &actstart, &end, &newstart, "--", 0);
502  sarrayAppendRange(sa4, sa1, actstart, end);
503 
504  /* Insert static protos */
505  for (i = 0; i < nsels; i++) {
506  if ((linestr = sarrayGetString(sa3, i, L_COPY)) == NULL) {
507  sarrayDestroy(&sa1);
508  sarrayDestroy(&sa2);
509  sarrayDestroy(&sa3);
510  sarrayDestroy(&sa4);
511  return ERROR_INT("linestr not retrieved", procName, 1);
512  }
513  sarrayAddString(sa4, linestr, L_INSERT);
514  }
515 
516  /* Make more strings containing function names */
517  sprintf(bigbuf, " * fhmtgen_low_%d()", fileindex);
518  str_doc3 = stringNew(bigbuf);
519  sprintf(bigbuf, "fhmtgen_low_%d(l_uint32 *datad,", fileindex);
520  str_def1 = stringNew(bigbuf);
521 
522  /* Insert function header */
523  sarrayParseRange(sa1, newstart, &actstart, &end, &newstart, "--", 0);
524  sarrayAppendRange(sa4, sa1, actstart, end);
525  sarrayAddString(sa4, str_doc3, L_INSERT);
526  sarrayParseRange(sa1, newstart, &actstart, &end, &newstart, "--", 0);
527  sarrayAppendRange(sa4, sa1, actstart, end);
528  sarrayAddString(sa4, str_def1, L_INSERT);
529  sarrayParseRange(sa1, newstart, &actstart, &end, &newstart, "--", 0);
530  sarrayAppendRange(sa4, sa1, actstart, end);
531 
532  /* Generate and insert the dispatcher code */
533  for (i = 0; i < nsels; i++) {
534  sprintf(bigbuf, " case %d:", i);
535  sarrayAddString(sa4, bigbuf, L_COPY);
536  sprintf(bigbuf, " %s(datad, w, h, wpld, datas, wpls);",
537  sarrayGetString(sa2, i, L_NOCOPY));
538  sarrayAddString(sa4, bigbuf, L_COPY);
539  sarrayAddString(sa4, breakstring, L_COPY);
540  }
541 
542  /* Finish the dispatcher and introduce the low-level code */
543  sarrayParseRange(sa1, newstart, &actstart, &end, &newstart, "--", 0);
544  sarrayAppendRange(sa4, sa1, actstart, end);
545 
546  /* Get the range for the args common to all functions */
547  sarrayParseRange(sa1, newstart, &argstart, &argend, &newstart, "--", 0);
548 
549  /* Get the range for the loop code common to all functions */
550  sarrayParseRange(sa1, newstart, &loopstart, &loopend, &newstart, "--", 0);
551 
552  /* Get the range for the ending code common to all functions */
553  sarrayParseRange(sa1, newstart, &finalstart, &finalend, &newstart, "--", 0);
554 
555  /* Do all the static functions */
556  for (i = 0; i < nsels; i++) {
557  /* Generate the function header and add the common args */
558  sarrayAddString(sa4, staticstring, L_COPY);
559  fname = sarrayGetString(sa2, i, L_NOCOPY);
560  sprintf(bigbuf, "%s(l_uint32 *datad,", fname);
561  sarrayAddString(sa4, bigbuf, L_COPY);
562  sarrayAppendRange(sa4, sa1, argstart, argend);
563 
564  /* Declare and define wplsN args, as necessary */
565  if ((sel = selaGetSel(sela, i)) == NULL) {
566  sarrayDestroy(&sa1);
567  sarrayDestroy(&sa2);
568  sarrayDestroy(&sa3);
569  sarrayDestroy(&sa4);
570  return ERROR_INT("sel not returned", procName, 1);
571  }
572  sa5 = sarrayMakeWplsCode(sel);
573  sarrayJoin(sa4, sa5);
574  sarrayDestroy(&sa5);
575 
576  /* Make sure sel has at least one hit */
577  nhits = 0;
578  nmisses = 0;
579  for (k = 0; k < sel->sy; k++) {
580  for (l = 0; l < sel->sx; l++) {
581  if (sel->data[k][l] == 1)
582  nhits++;
583  else if (sel->data[k][l] == 2)
584  nmisses++;
585  }
586  }
587  if (nhits == 0) {
588  linestr = stringNew(" fprintf(stderr, \"Error in HMT: no hits in sel!\\n\");\n}\n\n");
589  sarrayAddString(sa4, linestr, L_INSERT);
590  continue;
591  }
592 
593  /* Add the function loop code */
594  sarrayAppendRange(sa4, sa1, loopstart, loopend);
595 
596  /* Insert barrel-op code for *dptr */
597  if ((sa6 = sarrayMakeInnerLoopDWACode(sel, nhits, nmisses)) == NULL) {
598  sarrayDestroy(&sa1);
599  sarrayDestroy(&sa2);
600  sarrayDestroy(&sa3);
601  sarrayDestroy(&sa4);
602  return ERROR_INT("sa6 not made", procName, 1);
603  }
604  sarrayJoin(sa4, sa6);
605  sarrayDestroy(&sa6);
606 
607  /* Finish the function code */
608  sarrayAppendRange(sa4, sa1, finalstart, finalend);
609  }
610 
611  /* Output to file */
612  filestr = sarrayToString(sa4, 1);
613  nbytes = strlen(filestr);
614  if (filename)
615  snprintf(bigbuf, L_BUF_SIZE, "%slow.%d.c", filename, fileindex);
616  else
617  sprintf(bigbuf, "%slow.%d.c", OUTROOT, fileindex);
618  l_binaryWrite(bigbuf, "w", filestr, nbytes);
619  sarrayDestroy(&sa1);
620  sarrayDestroy(&sa2);
621  sarrayDestroy(&sa3);
622  sarrayDestroy(&sa4);
623  LEPT_FREE(filestr);
624  return 0;
625 }
626 
627 
628 
629 /*--------------------------------------------------------------------------*
630  * Helper code for sel *
631  *--------------------------------------------------------------------------*/
635 static SARRAY *
637 {
638 char emptystring[] = "";
639 l_int32 i, j, ymax, dely;
640 SARRAY *sa;
641 
642  PROCNAME("sarrayMakeWplsCode");
643 
644  if (!sel)
645  return (SARRAY *)ERROR_PTR("sel not defined", procName, NULL);
646 
647  ymax = 0;
648  for (i = 0; i < sel->sy; i++) {
649  for (j = 0; j < sel->sx; j++) {
650  if (sel->data[i][j] == 1 || sel->data[i][j] == 2) {
651  dely = L_ABS(i - sel->cy);
652  ymax = L_MAX(ymax, dely);
653  }
654  }
655  }
656  if (ymax > 31) {
657  L_WARNING("ymax > 31; truncating to 31\n", procName);
658  ymax = 31;
659  }
660 
661  sa = sarrayCreate(0);
662 
663  /* Declarations */
664  if (ymax > 4)
665  sarrayAddString(sa, wpldecls[2], L_COPY);
666  if (ymax > 8)
667  sarrayAddString(sa, wpldecls[6], L_COPY);
668  if (ymax > 12)
669  sarrayAddString(sa, wpldecls[10], L_COPY);
670  if (ymax > 16)
671  sarrayAddString(sa, wpldecls[14], L_COPY);
672  if (ymax > 20)
673  sarrayAddString(sa, wpldecls[18], L_COPY);
674  if (ymax > 24)
675  sarrayAddString(sa, wpldecls[22], L_COPY);
676  if (ymax > 28)
677  sarrayAddString(sa, wpldecls[26], L_COPY);
678  if (ymax > 1)
679  sarrayAddString(sa, wpldecls[ymax - 2], L_COPY);
680 
681  sarrayAddString(sa, emptystring, L_COPY);
682 
683  /* Definitions */
684  for (i = 2; i <= ymax; i++)
685  sarrayAddString(sa, wpldefs[i - 2], L_COPY);
686 
687  return sa;
688 }
689 
690 
694 static SARRAY *
696  l_int32 nhits,
697  l_int32 nmisses)
698 {
699 char *string;
700 char land[] = "&";
701 char bigbuf[L_BUF_SIZE];
702 l_int32 i, j, ntot, nfound, type, delx, dely;
703 SARRAY *sa;
704 
705  PROCNAME("sarrayMakeInnerLoopDWACode");
706 
707  if (!sel)
708  return (SARRAY *)ERROR_PTR("sel not defined", procName, NULL);
709 
710  sa = sarrayCreate(0);
711  ntot = nhits + nmisses;
712  nfound = 0;
713  for (i = 0; i < sel->sy; i++) {
714  for (j = 0; j < sel->sx; j++) {
715  type = sel->data[i][j];
716  if (type == SEL_HIT || type == SEL_MISS) {
717  nfound++;
718  dely = i - sel->cy;
719  delx = j - sel->cx;
720  if ((string = makeBarrelshiftString(delx, dely, type))
721  == NULL) {
722  L_WARNING("barrel shift string not made\n", procName);
723  continue;
724  }
725  if (ntot == 1) /* just one item */
726  sprintf(bigbuf, " *dptr = %s;", string);
727  else if (nfound == 1)
728  sprintf(bigbuf, " *dptr = %s %s", string, land);
729  else if (nfound < ntot)
730  sprintf(bigbuf, " %s %s", string, land);
731  else /* nfound == ntot */
732  sprintf(bigbuf, " %s;", string);
733  sarrayAddString(sa, bigbuf, L_COPY);
734  LEPT_FREE(string);
735  }
736  }
737  }
738 
739  return sa;
740 }
741 
742 
746 static char *
747 makeBarrelshiftString(l_int32 delx, /* j - cx */
748  l_int32 dely, /* i - cy */
749  l_int32 type) /* SEL_HIT or SEL_MISS */
750 {
751 l_int32 absx, absy;
752 char bigbuf[L_BUF_SIZE];
753 
754  PROCNAME("makeBarrelshiftString");
755 
756  if (delx < -31 || delx > 31)
757  return (char *)ERROR_PTR("delx out of bounds", procName, NULL);
758  if (dely < -31 || dely > 31)
759  return (char *)ERROR_PTR("dely out of bounds", procName, NULL);
760  absx = L_ABS(delx);
761  absy = L_ABS(dely);
762 
763  if (type == SEL_HIT) {
764  if ((delx == 0) && (dely == 0))
765  sprintf(bigbuf, "(*sptr)");
766  else if ((delx == 0) && (dely < 0))
767  sprintf(bigbuf, "(*(sptr %s))", wplstrm[absy - 1]);
768  else if ((delx == 0) && (dely > 0))
769  sprintf(bigbuf, "(*(sptr %s))", wplstrp[absy - 1]);
770  else if ((delx < 0) && (dely == 0))
771  sprintf(bigbuf, "((*(sptr) >> %d) | (*(sptr - 1) << %d))",
772  absx, 32 - absx);
773  else if ((delx > 0) && (dely == 0))
774  sprintf(bigbuf, "((*(sptr) << %d) | (*(sptr + 1) >> %d))",
775  absx, 32 - absx);
776  else if ((delx < 0) && (dely < 0))
777  sprintf(bigbuf, "((*(sptr %s) >> %d) | (*(sptr %s - 1) << %d))",
778  wplstrm[absy - 1], absx, wplstrm[absy - 1], 32 - absx);
779  else if ((delx > 0) && (dely < 0))
780  sprintf(bigbuf, "((*(sptr %s) << %d) | (*(sptr %s + 1) >> %d))",
781  wplstrm[absy - 1], absx, wplstrm[absy - 1], 32 - absx);
782  else if ((delx < 0) && (dely > 0))
783  sprintf(bigbuf, "((*(sptr %s) >> %d) | (*(sptr %s - 1) << %d))",
784  wplstrp[absy - 1], absx, wplstrp[absy - 1], 32 - absx);
785  else /* ((delx > 0) && (dely > 0)) */
786  sprintf(bigbuf, "((*(sptr %s) << %d) | (*(sptr %s + 1) >> %d))",
787  wplstrp[absy - 1], absx, wplstrp[absy - 1], 32 - absx);
788  } else { /* type == SEL_MISS */
789  if ((delx == 0) && (dely == 0))
790  sprintf(bigbuf, "(~*sptr)");
791  else if ((delx == 0) && (dely < 0))
792  sprintf(bigbuf, "(~*(sptr %s))", wplstrm[absy - 1]);
793  else if ((delx == 0) && (dely > 0))
794  sprintf(bigbuf, "(~*(sptr %s))", wplstrp[absy - 1]);
795  else if ((delx < 0) && (dely == 0))
796  sprintf(bigbuf, "((~*(sptr) >> %d) | (~*(sptr - 1) << %d))",
797  absx, 32 - absx);
798  else if ((delx > 0) && (dely == 0))
799  sprintf(bigbuf, "((~*(sptr) << %d) | (~*(sptr + 1) >> %d))",
800  absx, 32 - absx);
801  else if ((delx < 0) && (dely < 0))
802  sprintf(bigbuf, "((~*(sptr %s) >> %d) | (~*(sptr %s - 1) << %d))",
803  wplstrm[absy - 1], absx, wplstrm[absy - 1], 32 - absx);
804  else if ((delx > 0) && (dely < 0))
805  sprintf(bigbuf, "((~*(sptr %s) << %d) | (~*(sptr %s + 1) >> %d))",
806  wplstrm[absy - 1], absx, wplstrm[absy - 1], 32 - absx);
807  else if ((delx < 0) && (dely > 0))
808  sprintf(bigbuf, "((~*(sptr %s) >> %d) | (~*(sptr %s - 1) << %d))",
809  wplstrp[absy - 1], absx, wplstrp[absy - 1], 32 - absx);
810  else /* ((delx > 0) && (dely > 0)) */
811  sprintf(bigbuf, "((~*(sptr %s) << %d) | (~*(sptr %s + 1) >> %d))",
812  wplstrp[absy - 1], absx, wplstrp[absy - 1], 32 - absx);
813  }
814 
815  return stringNew(bigbuf);
816 }
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
static SARRAY * sarrayMakeInnerLoopDWACode(SEL *sel, l_int32 nhits, l_int32 nmisses)
sarrayMakeInnerLoopDWACode()
Definition: fhmtauto.c:695
l_ok fhmtautogen2(SELA *sela, l_int32 fileindex, const char *filename)
fhmtautogen2()
Definition: fhmtauto.c:431
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
l_ok sarrayJoin(SARRAY *sa1, SARRAY *sa2)
sarrayJoin()
Definition: sarray1.c:880
Definition: pix.h:718
static SARRAY * sarrayMakeWplsCode(SEL *sel)
sarrayMakeWplsCode()
Definition: fhmtauto.c:636
l_ok fhmtautogen(SELA *sela, l_int32 fileindex, const char *filename)
fhmtautogen()
Definition: fhmtauto.c:207
static char * makeBarrelshiftString(l_int32 delx, l_int32 dely, l_int32 type)
makeBarrelshiftString()
Definition: fhmtauto.c:747
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
l_ok fhmtautogen1(SELA *sela, l_int32 fileindex, const char *filename)
fhmtautogen1()
Definition: fhmtauto.c:249
static const l_int32 L_BUF_SIZE
Definition: classapp.c:55
void sarrayDestroy(SARRAY **psa)
sarrayDestroy()
Definition: sarray1.c:355