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)
255 PROCNAME(
"fmorphautogen");
258 return ERROR_INT(
"sela not defined", procName, 1);
260 ret2 = fmorphautogen2(sela, fileindex, filename);
262 return ERROR_INT(
"code generation problem", procName, 1);
291 const char *filename)
294 char *str_proto1, *str_proto2, *str_proto3;
295 char *str_doc1, *str_doc2, *str_doc3, *str_doc4;
296 char *str_def1, *str_def2, *str_proc1, *str_proc2;
297 char *str_dwa1, *str_low_dt, *str_low_ds, *str_low_ts;
298 char *str_low_tsp1, *str_low_dtp1;
300 l_int32 i, nsels, nbytes, actstart, end, newstart;
304 PROCNAME(
"fmorphautogen1");
307 return ERROR_INT(
"sela not defined", procName, 1);
311 return ERROR_INT(
"no sels in sela", procName, 1);
314 if ((filestr = (
char *)
l_binaryRead(TEMPLATE1, &size)) == NULL)
315 return ERROR_INT(
"filestr not made", procName, 1);
319 return ERROR_INT(
"sa2 not made", procName, 1);
325 sprintf(bigbuf,
"PIX *pixMorphDwa_%d(PIX *pixd, PIX *pixs, "
326 "l_int32 operation, char *selname);", fileindex);
328 sprintf(bigbuf,
"PIX *pixFMorphopGen_%d(PIX *pixd, PIX *pixs, "
329 "l_int32 operation, char *selname);", fileindex);
331 sprintf(bigbuf,
"l_int32 fmorphopgen_low_%d(l_uint32 *datad, l_int32 w,\n"
332 " l_int32 h, l_int32 wpld,\n"
333 " l_uint32 *datas, l_int32 wpls,\n"
334 " l_int32 index);", fileindex);
336 sprintf(bigbuf,
" * PIX *pixMorphDwa_%d()", fileindex);
338 sprintf(bigbuf,
" * PIX *pixFMorphopGen_%d()", fileindex);
340 sprintf(bigbuf,
" * \\brief pixMorphDwa_%d()", fileindex);
342 sprintf(bigbuf,
" * \\brief pixFMorphopGen_%d()", fileindex);
344 sprintf(bigbuf,
"pixMorphDwa_%d(PIX *pixd,", fileindex);
346 sprintf(bigbuf,
"pixFMorphopGen_%d(PIX *pixd,", fileindex);
348 sprintf(bigbuf,
" PROCNAME(\"pixMorphDwa_%d\");", fileindex);
350 sprintf(bigbuf,
" PROCNAME(\"pixFMorphopGen_%d\");", fileindex);
353 " pixt2 = pixFMorphopGen_%d(NULL, pixt1, operation, selname);",
357 " fmorphopgen_low_%d(datad, w, h, wpld, datat, wpls, index);",
361 " fmorphopgen_low_%d(datad, w, h, wpld, datas, wpls, index);",
365 " fmorphopgen_low_%d(datat, w, h, wpls, datas, wpls, index+1);",
369 " fmorphopgen_low_%d(datat, w, h, wpls, datas, wpls, index);",
373 " fmorphopgen_low_%d(datad, w, h, wpld, datat, wpls, index+1);",
398 sprintf(bigbuf,
"\nstatic l_int32 NUM_SELS_GENERATED = %d;", nsels);
400 sprintf(bigbuf,
"static char SEL_NAMES[][80] = {");
402 for (i = 0; i < nsels - 1; i++) {
403 sprintf(bigbuf,
" \"%s\",",
407 sprintf(bigbuf,
" \"%s\"};",
462 nbytes = strlen(filestr);
464 snprintf(bigbuf,
L_BUF_SIZE,
"%s.%d.c", filename, fileindex);
466 sprintf(bigbuf,
"%s.%d.c", OUTROOT, fileindex);
496 fmorphautogen2(
SELA *sela,
498 const char *filename)
500 char *filestr, *linestr, *fname;
501 char *str_doc1, *str_doc2, *str_doc3, *str_doc4, *str_def1;
503 char breakstring[] =
" break;";
504 char staticstring[] =
"static void";
505 l_int32 i, nsels, nbytes, actstart, end, newstart;
506 l_int32 argstart, argend, loopstart, loopend, finalstart, finalend;
508 SARRAY *sa1, *sa2, *sa3, *sa4, *sa5, *sa6;
511 PROCNAME(
"fmorphautogen2");
514 return ERROR_INT(
"sela not defined", procName, 1);
518 return ERROR_INT(
"no sels in sela", procName, 1);
521 if ((filestr = (
char *)
l_binaryRead(TEMPLATE2, &size)) == NULL)
522 return ERROR_INT(
"filestr not made", procName, 1);
526 return ERROR_INT(
"sa1 not made", procName, 1);
531 return ERROR_INT(
"sa2 not made", procName, 1);
533 for (i = 0; i < nsels; i++) {
534 sprintf(bigbuf,
"fdilate_%d_%d", fileindex, i);
536 sprintf(bigbuf,
"ferode_%d_%d", fileindex, i);
542 for (i = 0; i < 2 * nsels; i++) {
544 sprintf(bigbuf,
"static void %s%s", fname, PROTOARGS);
549 sprintf(bigbuf,
" * l_int32 fmorphopgen_low_%d()",
552 sprintf(bigbuf,
" * void fdilate_%d_*()", fileindex);
554 sprintf(bigbuf,
" * void ferode_%d_*()", fileindex);
574 for (i = 0; i < 2 * nsels; i++) {
580 return ERROR_INT(
"linestr not retrieved", procName, 1);
586 sprintf(bigbuf,
" * fmorphopgen_low_%d()", fileindex);
588 sprintf(bigbuf,
"fmorphopgen_low_%d(l_uint32 *datad,", fileindex);
602 for (i = 0; i < 2 * nsels; i++) {
603 sprintf(bigbuf,
" case %d:", i);
605 sprintf(bigbuf,
" %s(datad, w, h, wpld, datas, wpls);",
622 sarrayParseRange(sa1, newstart, &finalstart, &finalend, &newstart,
"--", 0);
625 for (i = 0; i < 2 * nsels; i++) {
629 sprintf(bigbuf,
"%s(l_uint32 *datad,", fname);
639 return ERROR_INT(
"sel not returned", procName, 1);
659 nbytes = strlen(filestr);
661 snprintf(bigbuf,
L_BUF_SIZE,
"%slow.%d.c", filename, fileindex);
663 sprintf(bigbuf,
"%slow.%d.c", OUTROOT, fileindex);
683 char emptystring[] =
"";
684 l_int32 i, j, ymax, dely, allvshifts;
688 PROCNAME(
"sarrayMakeWplsCode");
691 return (
SARRAY *)ERROR_PTR(
"sel not defined", procName, NULL);
693 for (i = 0; i < 32; i++)
696 for (i = 0; i < sel->
sy; i++) {
697 for (j = 0; j < sel->
sx; j++) {
698 if (sel->
data[i][j] == 1) {
699 dely = L_ABS(i - sel->
cy);
702 ymax = L_MAX(ymax, dely);
707 L_WARNING(
"ymax > 31; truncating to 31\n", procName);
713 for (i = 0; i < ymax; i++) {
714 if (vshift[i] == 0) {
723 if (allvshifts == TRUE) {
741 for (i = 2; i <= ymax; i++) {
750 for (i = 2; i <= ymax; i++) {
767 char logicalor[] =
"|";
768 char logicaland[] =
"&";
770 l_int32 i, j, optype, count, nfound, delx, dely;
773 PROCNAME(
"sarrayMakeInnerLoopDWACode");
776 return (
SARRAY *)ERROR_PTR(
"sel not defined", procName, NULL);
778 if (index % 2 == 0) {
779 optype = L_MORPH_DILATE;
782 optype = L_MORPH_ERODE;
787 for (i = 0; i < sel->
sy; i++) {
788 for (j = 0; j < sel->
sx; j++) {
789 if (sel->
data[i][j] == 1)
796 L_WARNING(
"no hits in Sel %d\n", procName, index);
801 for (i = 0; i < sel->
sy; i++) {
802 for (j = 0; j < sel->
sx; j++) {
803 if (sel->
data[i][j] == 1) {
805 if (optype == L_MORPH_DILATE) {
813 L_WARNING(
"barrel shift string not made\n", procName);
817 sprintf(bigbuf,
" *dptr = %s;",
string);
818 else if (nfound == 1)
819 sprintf(bigbuf,
" *dptr = %s %s",
string, tstr);
820 else if (nfound < count)
821 sprintf(bigbuf,
" %s %s",
string, tstr);
823 sprintf(bigbuf,
" %s;",
string);
844 PROCNAME(
"makeBarrelshiftString");
846 if (delx < -31 || delx > 31)
847 return (
char *)ERROR_PTR(
"delx out of bounds", procName, NULL);
848 if (dely < -31 || dely > 31)
849 return (
char *)ERROR_PTR(
"dely out of bounds", procName, NULL);
853 if ((delx == 0) && (dely == 0))
854 sprintf(bigbuf,
"(*sptr)");
855 else if ((delx == 0) && (dely < 0))
856 sprintf(bigbuf,
"(*(sptr %s))", wplstrm[absy - 1]);
857 else if ((delx == 0) && (dely > 0))
858 sprintf(bigbuf,
"(*(sptr %s))", wplstrp[absy - 1]);
859 else if ((delx < 0) && (dely == 0))
860 sprintf(bigbuf,
"((*(sptr) >> %d) | (*(sptr - 1) << %d))",
862 else if ((delx > 0) && (dely == 0))
863 sprintf(bigbuf,
"((*(sptr) << %d) | (*(sptr + 1) >> %d))",
865 else if ((delx < 0) && (dely < 0))
866 sprintf(bigbuf,
"((*(sptr %s) >> %d) | (*(sptr %s - 1) << %d))",
867 wplstrm[absy - 1], absx, wplstrm[absy - 1], 32 - absx);
868 else if ((delx > 0) && (dely < 0))
869 sprintf(bigbuf,
"((*(sptr %s) << %d) | (*(sptr %s + 1) >> %d))",
870 wplstrm[absy - 1], absx, wplstrm[absy - 1], 32 - absx);
871 else if ((delx < 0) && (dely > 0))
872 sprintf(bigbuf,
"((*(sptr %s) >> %d) | (*(sptr %s - 1) << %d))",
873 wplstrp[absy - 1], absx, wplstrp[absy - 1], 32 - absx);
875 sprintf(bigbuf,
"((*(sptr %s) << %d) | (*(sptr %s + 1) >> %d))",
876 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()