100 #include <config_auto.h>
104 #include "allheaders.h"
106 #define OUTROOT "fmorphgen"
107 #define TEMPLATE1 "morphtemplate1.txt"
108 #define TEMPLATE2 "morphtemplate2.txt"
110 #define PROTOARGS "(l_uint32 *, l_int32, l_int32, l_int32, l_uint32 *, l_int32);"
112 #define L_BUF_SIZE 512
118 static char wpldecls[][53] = {
120 "l_int32 wpls2, wpls3;",
121 "l_int32 wpls2, wpls3, wpls4;",
123 "l_int32 wpls5, wpls6;",
124 "l_int32 wpls5, wpls6, wpls7;",
125 "l_int32 wpls5, wpls6, wpls7, wpls8;",
127 "l_int32 wpls9, wpls10;",
128 "l_int32 wpls9, wpls10, wpls11;",
129 "l_int32 wpls9, wpls10, wpls11, wpls12;",
131 "l_int32 wpls13, wpls14;",
132 "l_int32 wpls13, wpls14, wpls15;",
133 "l_int32 wpls13, wpls14, wpls15, wpls16;",
135 "l_int32 wpls17, wpls18;",
136 "l_int32 wpls17, wpls18, wpls19;",
137 "l_int32 wpls17, wpls18, wpls19, wpls20;",
139 "l_int32 wpls21, wpls22;",
140 "l_int32 wpls21, wpls22, wpls23;",
141 "l_int32 wpls21, wpls22, wpls23, wpls24;",
143 "l_int32 wpls25, wpls26;",
144 "l_int32 wpls25, wpls26, wpls27;",
145 "l_int32 wpls25, wpls26, wpls27, wpls28;",
147 "l_int32 wpls29, wpls30;",
148 "l_int32 wpls29, wpls30, wpls31;"};
150 static char wplgendecls[][30] = {
182 static char wpldefs[][25] = {
183 " wpls2 = 2 * wpls;",
184 " wpls3 = 3 * wpls;",
185 " wpls4 = 4 * wpls;",
186 " wpls5 = 5 * wpls;",
187 " wpls6 = 6 * wpls;",
188 " wpls7 = 7 * wpls;",
189 " wpls8 = 8 * wpls;",
190 " wpls9 = 9 * wpls;",
191 " wpls10 = 10 * wpls;",
192 " wpls11 = 11 * wpls;",
193 " wpls12 = 12 * wpls;",
194 " wpls13 = 13 * wpls;",
195 " wpls14 = 14 * wpls;",
196 " wpls15 = 15 * wpls;",
197 " wpls16 = 16 * wpls;",
198 " wpls17 = 17 * wpls;",
199 " wpls18 = 18 * wpls;",
200 " wpls19 = 19 * wpls;",
201 " wpls20 = 20 * wpls;",
202 " wpls21 = 21 * wpls;",
203 " wpls22 = 22 * wpls;",
204 " wpls23 = 23 * wpls;",
205 " wpls24 = 24 * wpls;",
206 " wpls25 = 25 * wpls;",
207 " wpls26 = 26 * wpls;",
208 " wpls27 = 27 * wpls;",
209 " wpls28 = 28 * wpls;",
210 " wpls29 = 29 * wpls;",
211 " wpls30 = 30 * wpls;",
212 " wpls31 = 31 * wpls;"};
214 static char wplstrp[][10] = {
"+ wpls",
"+ wpls2",
"+ wpls3",
"+ wpls4",
215 "+ wpls5",
"+ wpls6",
"+ wpls7",
"+ wpls8",
216 "+ wpls9",
"+ wpls10",
"+ wpls11",
"+ wpls12",
217 "+ wpls13",
"+ wpls14",
"+ wpls15",
"+ wpls16",
218 "+ wpls17",
"+ wpls18",
"+ wpls19",
"+ wpls20",
219 "+ wpls21",
"+ wpls22",
"+ wpls23",
"+ wpls24",
220 "+ wpls25",
"+ wpls26",
"+ wpls27",
"+ wpls28",
221 "+ wpls29",
"+ wpls30",
"+ wpls31"};
223 static char wplstrm[][10] = {
"- wpls",
"- wpls2",
"- wpls3",
"- wpls4",
224 "- wpls5",
"- wpls6",
"- wpls7",
"- wpls8",
225 "- wpls9",
"- wpls10",
"- wpls11",
"- wpls12",
226 "- wpls13",
"- wpls14",
"- wpls15",
"- wpls16",
227 "- wpls17",
"- wpls18",
"- wpls19",
"- wpls20",
228 "- wpls21",
"- wpls22",
"- wpls23",
"- wpls24",
229 "- wpls25",
"- wpls26",
"- wpls27",
"- wpls28",
230 "- wpls29",
"- wpls30",
"- wpls31"};
251 const char *filename)
256 return ERROR_INT(
"sela not defined", __func__, 1);
258 ret2 = fmorphautogen2(sela, fileindex, filename);
260 return ERROR_INT(
"code generation problem", __func__, 1);
289 const char *filename)
292 char *str_proto1, *str_proto2, *str_proto3;
293 char *str_doc1, *str_doc2, *str_doc3, *str_doc4;
294 char *str_def1, *str_def2, *str_proc1, *str_proc2;
295 char *str_dwa1, *str_low_dt, *str_low_ds, *str_low_ts;
296 char *str_low_tsp1, *str_low_dtp1;
298 l_int32 i, nsels, nbytes, actstart, end, newstart;
303 return ERROR_INT(
"sela not defined", __func__, 1);
307 return ERROR_INT(
"no sels in sela", __func__, 1);
310 if ((filestr = (
char *)
l_binaryRead(TEMPLATE1, &size)) == NULL)
311 return ERROR_INT(
"filestr not made", __func__, 1);
315 return ERROR_INT(
"sa2 not made", __func__, 1);
321 sprintf(bigbuf,
"PIX *pixMorphDwa_%d(PIX *pixd, PIX *pixs, "
322 "l_int32 operation, char *selname);", fileindex);
324 sprintf(bigbuf,
"PIX *pixFMorphopGen_%d(PIX *pixd, PIX *pixs, "
325 "l_int32 operation, char *selname);", fileindex);
327 sprintf(bigbuf,
"l_int32 fmorphopgen_low_%d(l_uint32 *datad, l_int32 w,\n"
328 " l_int32 h, l_int32 wpld,\n"
329 " l_uint32 *datas, l_int32 wpls,\n"
330 " l_int32 index);", fileindex);
332 sprintf(bigbuf,
" * PIX *pixMorphDwa_%d()", fileindex);
334 sprintf(bigbuf,
" * PIX *pixFMorphopGen_%d()", fileindex);
336 sprintf(bigbuf,
" * \\brief pixMorphDwa_%d()", fileindex);
338 sprintf(bigbuf,
" * \\brief pixFMorphopGen_%d()", fileindex);
340 sprintf(bigbuf,
"pixMorphDwa_%d(PIX *pixd,", fileindex);
342 sprintf(bigbuf,
"pixFMorphopGen_%d(PIX *pixd,", fileindex);
344 sprintf(bigbuf,
" PROCNAME(\"pixMorphDwa_%d\");", fileindex);
346 sprintf(bigbuf,
" PROCNAME(\"pixFMorphopGen_%d\");", fileindex);
349 " pixt2 = pixFMorphopGen_%d(NULL, pixt1, operation, selname);",
353 " fmorphopgen_low_%d(datad, w, h, wpld, datat, wpls, index);",
357 " fmorphopgen_low_%d(datad, w, h, wpld, datas, wpls, index);",
361 " fmorphopgen_low_%d(datat, w, h, wpls, datas, wpls, index+1);",
365 " fmorphopgen_low_%d(datat, w, h, wpls, datas, wpls, index);",
369 " fmorphopgen_low_%d(datad, w, h, wpld, datat, wpls, index+1);",
394 sprintf(bigbuf,
"\nstatic l_int32 NUM_SELS_GENERATED = %d;", nsels);
396 sprintf(bigbuf,
"static char SEL_NAMES[][80] = {");
398 for (i = 0; i < nsels - 1; i++) {
399 sprintf(bigbuf,
" \"%s\",",
403 sprintf(bigbuf,
" \"%s\"};",
458 nbytes = strlen(filestr);
460 snprintf(bigbuf,
L_BUF_SIZE,
"%s.%d.c", filename, fileindex);
462 sprintf(bigbuf,
"%s.%d.c", OUTROOT, fileindex);
492 fmorphautogen2(
SELA *sela,
494 const char *filename)
496 char *filestr, *linestr, *fname;
497 char *str_doc1, *str_doc2, *str_doc3, *str_doc4, *str_def1;
499 char breakstring[] =
" break;";
500 char staticstring[] =
"static void";
501 l_int32 i, nsels, nbytes, actstart, end, newstart;
502 l_int32 argstart, argend, loopstart, loopend, finalstart, finalend;
504 SARRAY *sa1, *sa2, *sa3, *sa4, *sa5, *sa6;
508 return ERROR_INT(
"sela not defined", __func__, 1);
512 return ERROR_INT(
"no sels in sela", __func__, 1);
515 if ((filestr = (
char *)
l_binaryRead(TEMPLATE2, &size)) == NULL)
516 return ERROR_INT(
"filestr not made", __func__, 1);
520 return ERROR_INT(
"sa1 not made", __func__, 1);
525 return ERROR_INT(
"sa2 not made", __func__, 1);
527 for (i = 0; i < nsels; i++) {
528 sprintf(bigbuf,
"fdilate_%d_%d", fileindex, i);
530 sprintf(bigbuf,
"ferode_%d_%d", fileindex, i);
536 for (i = 0; i < 2 * nsels; i++) {
538 sprintf(bigbuf,
"static void %s%s", fname, PROTOARGS);
543 sprintf(bigbuf,
" * l_int32 fmorphopgen_low_%d()",
546 sprintf(bigbuf,
" * void fdilate_%d_*()", fileindex);
548 sprintf(bigbuf,
" * void ferode_%d_*()", fileindex);
568 for (i = 0; i < 2 * nsels; i++) {
574 return ERROR_INT(
"linestr not retrieved", __func__, 1);
580 sprintf(bigbuf,
" * fmorphopgen_low_%d()", fileindex);
582 sprintf(bigbuf,
"fmorphopgen_low_%d(l_uint32 *datad,", fileindex);
596 for (i = 0; i < 2 * nsels; i++) {
597 sprintf(bigbuf,
" case %d:", i);
599 sprintf(bigbuf,
" %s(datad, w, h, wpld, datas, wpls);",
616 sarrayParseRange(sa1, newstart, &finalstart, &finalend, &newstart,
"--", 0);
619 for (i = 0; i < 2 * nsels; i++) {
623 sprintf(bigbuf,
"%s(l_uint32 *datad,", fname);
633 return ERROR_INT(
"sel not returned", __func__, 1);
653 nbytes = strlen(filestr);
655 snprintf(bigbuf,
L_BUF_SIZE,
"%slow.%d.c", filename, fileindex);
657 sprintf(bigbuf,
"%slow.%d.c", OUTROOT, fileindex);
677 char emptystring[] =
"";
678 l_int32 i, j, ymax, dely, allvshifts;
683 return (
SARRAY *)ERROR_PTR(
"sel not defined", __func__, NULL);
685 for (i = 0; i < 32; i++)
688 for (i = 0; i < sel->
sy; i++) {
689 for (j = 0; j < sel->
sx; j++) {
690 if (sel->
data[i][j] == 1) {
691 dely = L_ABS(i - sel->
cy);
694 ymax = L_MAX(ymax, dely);
699 L_WARNING(
"ymax > 31; truncating to 31\n", __func__);
705 for (i = 0; i < ymax; i++) {
706 if (vshift[i] == 0) {
715 if (allvshifts == TRUE) {
733 for (i = 2; i <= ymax; i++) {
742 for (i = 2; i <= ymax; i++) {
759 char logicalor[] =
"|";
760 char logicaland[] =
"&";
762 l_int32 i, j, optype, count, nfound, delx, dely;
766 return (
SARRAY *)ERROR_PTR(
"sel not defined", __func__, NULL);
768 if (index % 2 == 0) {
769 optype = L_MORPH_DILATE;
772 optype = L_MORPH_ERODE;
777 for (i = 0; i < sel->
sy; i++) {
778 for (j = 0; j < sel->
sx; j++) {
779 if (sel->
data[i][j] == 1)
786 L_WARNING(
"no hits in Sel %d\n", __func__, index);
791 for (i = 0; i < sel->
sy; i++) {
792 for (j = 0; j < sel->
sx; j++) {
793 if (sel->
data[i][j] == 1) {
795 if (optype == L_MORPH_DILATE) {
803 L_WARNING(
"barrel shift string not made\n", __func__);
807 sprintf(bigbuf,
" *dptr = %s;",
string);
808 else if (nfound == 1)
809 sprintf(bigbuf,
" *dptr = %s %s",
string, tstr);
810 else if (nfound < count)
811 sprintf(bigbuf,
" %s %s",
string, tstr);
813 sprintf(bigbuf,
" %s;",
string);
834 if (delx < -31 || delx > 31)
835 return (
char *)ERROR_PTR(
"delx out of bounds", __func__, NULL);
836 if (dely < -31 || dely > 31)
837 return (
char *)ERROR_PTR(
"dely out of bounds", __func__, NULL);
841 if ((delx == 0) && (dely == 0))
842 sprintf(bigbuf,
"(*sptr)");
843 else if ((delx == 0) && (dely < 0))
844 sprintf(bigbuf,
"(*(sptr %s))", wplstrm[absy - 1]);
845 else if ((delx == 0) && (dely > 0))
846 sprintf(bigbuf,
"(*(sptr %s))", wplstrp[absy - 1]);
847 else if ((delx < 0) && (dely == 0))
848 sprintf(bigbuf,
"((*(sptr) >> %d) | (*(sptr - 1) << %d))",
850 else if ((delx > 0) && (dely == 0))
851 sprintf(bigbuf,
"((*(sptr) << %d) | (*(sptr + 1) >> %d))",
853 else if ((delx < 0) && (dely < 0))
854 sprintf(bigbuf,
"((*(sptr %s) >> %d) | (*(sptr %s - 1) << %d))",
855 wplstrm[absy - 1], absx, wplstrm[absy - 1], 32 - absx);
856 else if ((delx > 0) && (dely < 0))
857 sprintf(bigbuf,
"((*(sptr %s) << %d) | (*(sptr %s + 1) >> %d))",
858 wplstrm[absy - 1], absx, wplstrm[absy - 1], 32 - absx);
859 else if ((delx < 0) && (dely > 0))
860 sprintf(bigbuf,
"((*(sptr %s) >> %d) | (*(sptr %s - 1) << %d))",
861 wplstrp[absy - 1], absx, wplstrp[absy - 1], 32 - absx);
863 sprintf(bigbuf,
"((*(sptr %s) << %d) | (*(sptr %s + 1) >> %d))",
864 wplstrp[absy - 1], absx, wplstrp[absy - 1], 32 - absx);
static SARRAY * sarrayMakeInnerLoopDWACode(SEL *sel, l_int32 index)
sarrayMakeInnerLoopDWACode()
l_ok fmorphautogen1(SELA *sela, l_int32 fileindex, const char *filename)
fmorphautogen1()
static SARRAY * sarrayMakeWplsCode(SEL *sel)
sarrayMakeWplsCode()
l_ok fmorphautogen(SELA *sela, l_int32 fileindex, const char *filename)
fmorphautogen()
static char * makeBarrelshiftString(l_int32 delx, l_int32 dely)
makeBarrelshiftString()
SARRAY * sarrayCreate(l_int32 n)
sarrayCreate()
char * sarrayGetString(SARRAY *sa, l_int32 index, l_int32 copyflag)
sarrayGetString()
l_ok sarrayJoin(SARRAY *sa1, SARRAY *sa2)
sarrayJoin()
void sarrayDestroy(SARRAY **psa)
sarrayDestroy()
SARRAY * sarrayCreateLinesFromString(const char *string, l_int32 blankflag)
sarrayCreateLinesFromString()
l_ok sarrayAddString(SARRAY *sa, const char *string, l_int32 copyflag)
sarrayAddString()
l_int32 sarrayParseRange(SARRAY *sa, l_int32 start, l_int32 *pactualstart, l_int32 *pend, l_int32 *pnewstart, const char *substr, l_int32 loc)
sarrayParseRange()
char * sarrayToString(SARRAY *sa, l_int32 addnlflag)
sarrayToString()
l_ok sarrayAppendRange(SARRAY *sa1, SARRAY *sa2, l_int32 start, l_int32 end)
sarrayAppendRange()
SARRAY * selaGetSelnames(SELA *sela)
selaGetSelnames()
SEL * selaGetSel(SELA *sela, l_int32 i)
selaGetSel()
l_int32 selaGetCount(SELA *sela)
selaGetCount()
char * stringNew(const char *src)
stringNew()
l_ok l_binaryWrite(const char *filename, const char *operation, const void *data, size_t nbytes)
l_binaryWrite()
l_uint8 * l_binaryRead(const char *filename, size_t *pnbytes)
l_binaryRead()