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