98 #include "allheaders.h" 100 #define OUTROOT "fmorphgen" 101 #define TEMPLATE1 "morphtemplate1.txt" 102 #define TEMPLATE2 "morphtemplate2.txt" 104 #define PROTOARGS "(l_uint32 *, l_int32, l_int32, l_int32, l_uint32 *, l_int32);" 112 static char wpldecls[][53] = {
114 "l_int32 wpls2, wpls3;",
115 "l_int32 wpls2, wpls3, wpls4;",
117 "l_int32 wpls5, wpls6;",
118 "l_int32 wpls5, wpls6, wpls7;",
119 "l_int32 wpls5, wpls6, wpls7, wpls8;",
121 "l_int32 wpls9, wpls10;",
122 "l_int32 wpls9, wpls10, wpls11;",
123 "l_int32 wpls9, wpls10, wpls11, wpls12;",
125 "l_int32 wpls13, wpls14;",
126 "l_int32 wpls13, wpls14, wpls15;",
127 "l_int32 wpls13, wpls14, wpls15, wpls16;",
129 "l_int32 wpls17, wpls18;",
130 "l_int32 wpls17, wpls18, wpls19;",
131 "l_int32 wpls17, wpls18, wpls19, wpls20;",
133 "l_int32 wpls21, wpls22;",
134 "l_int32 wpls21, wpls22, wpls23;",
135 "l_int32 wpls21, wpls22, wpls23, wpls24;",
137 "l_int32 wpls25, wpls26;",
138 "l_int32 wpls25, wpls26, wpls27;",
139 "l_int32 wpls25, wpls26, wpls27, wpls28;",
141 "l_int32 wpls29, wpls30;",
142 "l_int32 wpls29, wpls30, wpls31;"};
144 static char wplgendecls[][30] = {
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;"};
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"};
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"};
245 const char *filename)
249 PROCNAME(
"fmorphautogen");
252 return ERROR_INT(
"sela not defined", procName, 1);
254 ret2 = fmorphautogen2(sela, fileindex, filename);
256 return ERROR_INT(
"code generation problem", procName, 1);
285 const char *filename)
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;
294 l_int32 i, nsels, nbytes, actstart, end, newstart;
298 PROCNAME(
"fmorphautogen1");
301 return ERROR_INT(
"sela not defined", procName, 1);
305 return ERROR_INT(
"no sels in sela", procName, 1);
308 if ((filestr = (
char *)
l_binaryRead(TEMPLATE1, &size)) == NULL)
309 return ERROR_INT(
"filestr not made", procName, 1);
313 return ERROR_INT(
"sa2 not made", procName, 1);
319 sprintf(bigbuf,
"PIX *pixMorphDwa_%d(PIX *pixd, PIX *pixs, " 320 "l_int32 operation, char *selname);", fileindex);
322 sprintf(bigbuf,
"PIX *pixFMorphopGen_%d(PIX *pixd, PIX *pixs, " 323 "l_int32 operation, char *selname);", fileindex);
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);
330 sprintf(bigbuf,
" * PIX *pixMorphDwa_%d()", fileindex);
332 sprintf(bigbuf,
" * PIX *pixFMorphopGen_%d()", fileindex);
334 sprintf(bigbuf,
" * pixMorphDwa_%d()", fileindex);
336 sprintf(bigbuf,
" * pixFMorphopGen_%d()", fileindex);
338 sprintf(bigbuf,
"pixMorphDwa_%d(PIX *pixd,", fileindex);
340 sprintf(bigbuf,
"pixFMorphopGen_%d(PIX *pixd,", fileindex);
342 sprintf(bigbuf,
" PROCNAME(\"pixMorphDwa_%d\");", fileindex);
344 sprintf(bigbuf,
" PROCNAME(\"pixFMorphopGen_%d\");", fileindex);
347 " pixt2 = pixFMorphopGen_%d(NULL, pixt1, operation, selname);",
351 " fmorphopgen_low_%d(datad, w, h, wpld, datat, wpls, index);",
355 " fmorphopgen_low_%d(datad, w, h, wpld, datas, wpls, index);",
359 " fmorphopgen_low_%d(datat, w, h, wpls, datas, wpls, index+1);",
363 " fmorphopgen_low_%d(datat, w, h, wpls, datas, wpls, index);",
367 " fmorphopgen_low_%d(datad, w, h, wpld, datat, wpls, index+1);",
392 sprintf(bigbuf,
"\nstatic l_int32 NUM_SELS_GENERATED = %d;", nsels);
394 sprintf(bigbuf,
"static char SEL_NAMES[][80] = {");
396 for (i = 0; i < nsels - 1; i++) {
397 sprintf(bigbuf,
" \"%s\",",
401 sprintf(bigbuf,
" \"%s\"};",
456 nbytes = strlen(filestr);
458 snprintf(bigbuf,
L_BUF_SIZE,
"%s.%d.c", filename, fileindex);
460 sprintf(bigbuf,
"%s.%d.c", OUTROOT, fileindex);
490 fmorphautogen2(
SELA *sela,
492 const char *filename)
494 char *filestr, *linestr, *fname;
495 char *str_doc1, *str_doc2, *str_doc3, *str_doc4, *str_def1;
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;
502 SARRAY *sa1, *sa2, *sa3, *sa4, *sa5, *sa6;
505 PROCNAME(
"fmorphautogen2");
508 return ERROR_INT(
"sela not defined", procName, 1);
512 return ERROR_INT(
"no sels in sela", procName, 1);
515 if ((filestr = (
char *)
l_binaryRead(TEMPLATE2, &size)) == NULL)
516 return ERROR_INT(
"filestr not made", procName, 1);
520 return ERROR_INT(
"sa1 not made", procName, 1);
525 return ERROR_INT(
"sa2 not made", procName, 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", procName, 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", procName, 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;
682 PROCNAME(
"sarrayMakeWplsCode");
685 return (
SARRAY *)ERROR_PTR(
"sel not defined", procName, NULL);
687 for (i = 0; i < 32; i++)
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);
696 ymax = L_MAX(ymax, dely);
701 L_WARNING(
"ymax > 31; truncating to 31\n", procName);
707 for (i = 0; i < ymax; i++) {
708 if (vshift[i] == 0) {
717 if (allvshifts == TRUE) {
735 for (i = 2; i <= ymax; i++) {
744 for (i = 2; i <= ymax; i++) {
761 char logicalor[] =
"|";
762 char logicaland[] =
"&";
764 l_int32 i, j, optype, count, nfound, delx, dely;
767 PROCNAME(
"sarrayMakeInnerLoopDWACode");
770 return (
SARRAY *)ERROR_PTR(
"sel not defined", procName, NULL);
772 if (index % 2 == 0) {
773 optype = L_MORPH_DILATE;
776 optype = L_MORPH_ERODE;
781 for (i = 0; i < sel->
sy; i++) {
782 for (j = 0; j < sel->
sx; j++) {
783 if (sel->
data[i][j] == 1)
790 L_WARNING(
"no hits in Sel %d\n", procName, index);
795 for (i = 0; i < sel->
sy; i++) {
796 for (j = 0; j < sel->
sx; j++) {
797 if (sel->
data[i][j] == 1) {
799 if (optype == L_MORPH_DILATE) {
807 L_WARNING(
"barrel shift string not made\n", procName);
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);
817 sprintf(bigbuf,
" %s;",
string);
838 PROCNAME(
"makeBarrelshiftString");
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);
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))",
856 else if ((delx > 0) && (dely == 0))
857 sprintf(bigbuf,
"((*(sptr) << %d) | (*(sptr + 1) >> %d))",
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);
869 sprintf(bigbuf,
"((*(sptr %s) << %d) | (*(sptr %s + 1) >> %d))",
870 wplstrp[absy - 1], absx, wplstrp[absy - 1], 32 - absx);
char * sarrayToString(SARRAY *sa, l_int32 addnlflag)
sarrayToString()
char * stringNew(const char *src)
stringNew()
l_int32 selaGetCount(SELA *sela)
selaGetCount()
SARRAY * sarrayCreate(l_int32 n)
sarrayCreate()
SARRAY * selaGetSelnames(SELA *sela)
selaGetSelnames()
l_ok sarrayAppendRange(SARRAY *sa1, SARRAY *sa2, l_int32 start, l_int32 end)
sarrayAppendRange()
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()
l_ok sarrayAddString(SARRAY *sa, const char *string, l_int32 copyflag)
sarrayAddString()
char * sarrayGetString(SARRAY *sa, l_int32 index, l_int32 copyflag)
sarrayGetString()
SARRAY * sarrayCreateLinesFromString(const char *string, l_int32 blankflag)
sarrayCreateLinesFromString()
static SARRAY * sarrayMakeWplsCode(SEL *sel)
sarrayMakeWplsCode()
l_ok fmorphautogen1(SELA *sela, l_int32 fileindex, const char *filename)
fmorphautogen1()
l_ok sarrayJoin(SARRAY *sa1, SARRAY *sa2)
sarrayJoin()
l_ok fmorphautogen(SELA *sela, l_int32 fileindex, const char *filename)
fmorphautogen()
static SARRAY * sarrayMakeInnerLoopDWACode(SEL *sel, l_int32 index)
sarrayMakeInnerLoopDWACode()
SEL * selaGetSel(SELA *sela, l_int32 i)
selaGetSel()
l_int32 sarrayParseRange(SARRAY *sa, l_int32 start, l_int32 *pactualstart, l_int32 *pend, l_int32 *pnewstart, const char *substr, l_int32 loc)
sarrayParseRange()
static const l_int32 L_BUF_SIZE
static char * makeBarrelshiftString(l_int32 delx, l_int32 dely)
makeBarrelshiftString()
void sarrayDestroy(SARRAY **psa)
sarrayDestroy()