58 #include <config_auto.h>
62 #include "allheaders.h"
68 #define DEBUG_INVALID_MODELS 0
72 static const l_int32 GrayInValue = 200;
104 l_int32 check_columns,
112 PROCNAME(
"dewarpSinglePage");
115 return ERROR_INT(
"&pixd not defined", procName, 1);
117 if (pdewa) *pdewa = NULL;
119 return ERROR_INT(
"pixs not defined", procName, 1);
122 check_columns, &pixb, &dewa);
125 return ERROR_INT(
"pixb not made", procName, 1);
172 l_int32 check_columns,
178 PROCNAME(
"dewarpSinglePageInit");
180 if (ppixb) *ppixb = NULL;
181 if (pdewa) *pdewa = NULL;
182 if (!ppixb || !pdewa)
183 return ERROR_INT(
"&pixb and &dewa not both defined", procName, 1);
185 return ERROR_INT(
"pixs not defined", procName, 1);
188 if (pixGetDepth(pixs) > 1) {
190 return ERROR_INT(
"pix1 not made", procName, 1);
197 return ERROR_INT(
"pix2 not made", procName, 1);
236 const char *debugfile;
237 l_int32 vsuccess, ret;
240 PROCNAME(
"dewarpSinglePageRun");
243 return ERROR_INT(
"&pixd not defined", procName, 1);
246 return ERROR_INT(
"pixs not defined", procName, 1);
248 return ERROR_INT(
"pixb not defined", procName, 1);
250 return ERROR_INT(
"dewa not defined", procName, 1);
258 debugfile = (debug) ?
"/tmp/lept/dewarp/singlepage_model.pdf" : NULL;
262 L_ERROR(
"failure to build model for vertical disparity\n", procName);
268 debugfile = (debug) ?
"/tmp/lept/dewarp/singlepage_apply.pdf" : NULL;
271 L_ERROR(
"invalid model; failure to apply disparity\n", procName);
300 NUMA *namodels, *napages;
302 PROCNAME(
"dewarpaListPages");
305 return ERROR_INT(
"dewa not defined", procName, 1);
313 for (i = 0; i <= dewa->
maxpage; i++) {
352 l_int32 i, n, maxcurv, diffcurv, diffedge;
355 PROCNAME(
"dewarpaSetValidModels");
358 return ERROR_INT(
"dewa not defined", procName, 1);
361 for (i = 0; i < n; i++) {
367 L_INFO(
"page %d: has only a ref model\n", procName, i);
369 L_INFO(
"page %d: no model successfully built\n",
371 }
else if (!notests) {
375 L_INFO(
"page %d: useboth, but no horiz disparity\n",
378 L_INFO(
"page %d: max curvature %d > max_linecurv\n",
379 procName, i, diffcurv);
380 if (diffcurv < dewa->min_diff_linecurv)
381 L_INFO(
"page %d: diff curv %d < min_diff_linecurv\n",
382 procName, i, diffcurv);
384 L_INFO(
"page %d: abs diff curv %d > max_diff_linecurv\n",
385 procName, i, diffcurv);
388 L_INFO(
"page %d: abs left slope %d > max_edgeslope\n",
391 L_INFO(
"page %d: abs right slope %d > max_edgeslope\n",
395 L_INFO(
"page %d: left curvature %d > max_edgecurv\n",
398 L_INFO(
"page %d: right curvature %d > max_edgecurv\n",
401 L_INFO(
"page %d: abs diff left-right curv %d > "
402 "max_diff_edgecurv\n", procName, i, diffedge);
458 l_int32 i, j, n, val, min, distdown, distup;
462 PROCNAME(
"dewarpaInsertRefModels");
465 return ERROR_INT(
"dewa not defined", procName, 1);
467 L_INFO(
"maxdist < 2; no ref models can be used\n", procName);
473 for (i = 0; i < n; i++) {
485 for (i = 0; i < n; i++) {
487 if (val == 1)
continue;
488 if ((dew = dewa->
dewarp[i]) != NULL) {
492 if (dewa->
maxdist < 2)
continue;
494 distdown = distup = dewa->
maxdist + 1;
495 for (j = i - 2; j >= 0 && distdown > dewa->
maxdist; j -= 2) {
497 if (val == 1) distdown = i - j;
500 for (j = i + 2; j < n && distup > dewa->
maxdist; j += 2) {
502 if (val == 1) distup = j - i;
504 min = L_MIN(distdown, distup);
505 if (min > dewa->
maxdist)
continue;
506 if (distdown <= distup)
521 for (i = 0; i < n; i++) {
526 for (i = 0; i < n; i++) {
528 if (val == 1)
continue;
529 if (dewa->
maxdist < 2)
continue;
530 distdown = distup = 100000;
531 for (j = i - 2; j >= 0; j -= 2) {
538 for (j = i + 2; j < n; j += 2) {
545 min = L_MIN(distdown, distup);
546 if (min > dewa->
maxdist)
continue;
551 L_ERROR(
"dew is null for page %d!\n", procName, i);
558 if (distdown <= distup)
590 PROCNAME(
"dewarpaStripRefModels");
593 return ERROR_INT(
"dewa not defined", procName, 1);
595 for (i = 0; i <= dewa->
maxpage; i++) {
631 PROCNAME(
"dewarpaRestoreModels");
634 return ERROR_INT(
"dewa not defined", procName, 1);
641 for (i = 0; i <= dewa->
maxpage; i++) {
644 L_ERROR(
"dew in both cache and main array!: page %d\n",
674 l_int32 i, n, pageno, nnone, nvsuccess, nvvalid, nhsuccess, nhvalid, nref;
677 PROCNAME(
"dewarpaInfo");
680 return ERROR_INT(
"dewa not defined", procName, 1);
682 return ERROR_INT(
"dewa not defined", procName, 1);
684 fprintf(fp,
"\nDewarpaInfo: %p\n", dewa);
685 fprintf(fp,
"nalloc = %d, maxpage = %d\n", dewa->
nalloc, dewa->
maxpage);
686 fprintf(fp,
"sampling = %d, redfactor = %d, minlines = %d\n",
688 fprintf(fp,
"maxdist = %d, useboth = %d\n",
692 &nhsuccess, &nhvalid, &nref);
694 lept_stderr(
"Total number of pages with a dew = %d\n", n);
695 lept_stderr(
"Number of pages without any models = %d\n", nnone);
696 lept_stderr(
"Number of pages with a vert model = %d\n", nvsuccess);
697 lept_stderr(
"Number of pages with a valid vert model = %d\n", nvvalid);
698 lept_stderr(
"Number of pages with both models = %d\n", nhsuccess);
699 lept_stderr(
"Number of pages with both models valid = %d\n", nhvalid);
700 lept_stderr(
"Number of pages with a ref model = %d\n", nref);
702 for (i = 0; i < n; i++) {
711 dew->
w, dew->
h, dew->
nx, dew->
ny);
714 " (min,max,abs-diff) line curvature = (%d,%d,%d)\n",
718 " left edge slope = %d, right edge slope = %d\n"
719 " (left,right,abs-diff) edge curvature = (%d,%d,%d)\n",
771 l_int32 i, n, pageno, nnone, nvsuccess, nvvalid, nhsuccess, nhvalid, nref;
774 PROCNAME(
"dewarpaModelStats");
777 return ERROR_INT(
"dewa not defined", procName, 1);
781 nnone = nref = nvsuccess = nvvalid = nhsuccess = nhvalid = 0;
782 for (i = 0; i < n; i++) {
802 if (pnnone) *pnnone = nnone;
803 if (pnref) *pnref = nref;
804 if (pnvsuccess) *pnvsuccess = nvsuccess;
805 if (pnvvalid) *pnvvalid = nvvalid;
806 if (pnhsuccess) *pnhsuccess = nhsuccess;
807 if (pnhvalid) *pnhvalid = nhvalid;
835 l_int32 maxcurv, diffcurv, diffedge;
837 PROCNAME(
"dewarpaTestForValidModel");
840 return ERROR_INT(
"dewa and dew not both defined", procName, 1);
852 if (dew->
hasref == 1)
return 0;
859 if (maxcurv <= dewa->max_linecurv &&
861 diffcurv <= dewa->max_diff_linecurv) {
864 L_INFO(
"invalid vert model for page %d:\n", procName, dew->
pageno);
865 #if DEBUG_INVALID_MODELS
866 lept_stderr(
" max line curv = %d, max allowed = %d\n",
868 lept_stderr(
" diff line curv = %d, max allowed = %d\n",
880 diffedge <= dewa->max_diff_edgecurv) {
883 L_INFO(
"invalid horiz model for page %d:\n", procName, dew->
pageno);
884 #if DEBUG_INVALID_MODELS
885 lept_stderr(
" left edge slope = %d, max allowed = %d\n",
887 lept_stderr(
" right edge slope = %d, max allowed = %d\n",
889 lept_stderr(
" left edge curv = %d, max allowed = %d\n",
891 lept_stderr(
" right edge curv = %d, max allowed = %d\n",
893 lept_stderr(
" diff edge curv = %d, max allowed = %d\n",
928 PIX *pixv, *pixvs, *pixh, *pixhs, *pixt, *pixd;
931 PROCNAME(
"dewarpaShowArrays");
934 return ERROR_INT(
"dew not defined", procName, 1);
935 if (first < 0 || first > dewa->
maxpage)
936 return ERROR_INT(
"first out of bounds", procName, 1);
939 return ERROR_INT(
"last < first", procName, 1);
944 L_ERROR(
"bmf not made; page info not displayed", procName);
947 for (i = first; i <= last; i++) {
948 if (i && ((i % 10) == 0))
952 if (dew->
hasref == 1)
continue;
957 L_ERROR(
"sampvdispar not made for page %d!\n", procName, i);
979 snprintf(buf,
sizeof(buf),
"Page %d", i);
982 snprintf(buf,
sizeof(buf),
"/tmp/lept/dewarp1/arrays_%04d.png", i);
983 pixWriteDebug(buf, pixd, IFF_PNG);
993 0,
"Disparity arrays",
"/tmp/lept/disparity_arrays.pdf");
994 lept_stderr(
"Output written to: /tmp/lept/disparity_arrays.pdf\n");
1016 const char *subdirs,
1024 PROCNAME(
"dewarpDebug");
1027 return ERROR_INT(
"dew not defined", procName, 1);
1029 return ERROR_INT(
"subdirs not defined", procName, 1);
1031 lept_stderr(
"pageno = %d, hasref = %d, refpage = %d\n",
1033 lept_stderr(
"sampling = %d, redfactor = %d, minlines = %d\n",
1039 lept_stderr(
"sampv = %d, samph = %d\n", svd, shd);
1044 lept_stderr(
"(min,max,abs-diff) line curvature = (%d,%d,%d)\n",
1048 lept_stderr(
"(left edge slope = %d, right edge slope = %d\n",
1050 lept_stderr(
"(left,right,abs-diff) edge curvature = "
1062 outdir =
pathJoin(
"/tmp", subdirs);
1065 snprintf(fname,
sizeof(fname),
"%s/pixv_%d.png", outdir, index);
1066 pixWriteDebug(fname, pixv, IFF_PNG);
1071 snprintf(fname,
sizeof(fname),
"%s/pixh_%d.png", outdir, index);
1072 pixWriteDebug(fname, pixh, IFF_PNG);
1110 l_int32 i, modelpage;
1114 PIX *pixs, *pixc, *pixd, *pixt1, *pixt2;
1117 PROCNAME(
"dewarpShowResults");
1120 return ERROR_INT(
"dewa not defined", procName, 1);
1122 return ERROR_INT(
"sa not defined", procName, 1);
1124 return ERROR_INT(
"pdfout not defined", procName, 1);
1125 if (firstpage > lastpage)
1126 return ERROR_INT(
"invalid first/last page numbers", procName, 1);
1132 lept_stderr(
"Dewarping and generating s/by/s view\n");
1133 for (i = firstpage; i <= lastpage; i++) {
1147 GrayInValue, 0, 0, &pixd, NULL);
1157 snprintf(bufstr,
sizeof(bufstr),
"Page %d; using %d\n",
1161 snprintf(bufstr,
sizeof(bufstr),
"Page %d; no dewarp\n", i);
1164 snprintf(bufstr,
sizeof(bufstr),
"/tmp/lept/dewarp_pdfout/%05d", i);
1165 pixWriteDebug(bufstr, pixt2, IFF_JFIF_JPEG);
1175 0,
"Dewarp sequence", pdfout);
void bmfDestroy(L_BMF **pbmf)
bmfDestroy()
L_BMF * bmfCreate(const char *dir, l_int32 fontsize)
bmfCreate()
void boxDestroy(BOX **pbox)
boxDestroy()
BOX * boxaGetBox(BOXA *boxa, l_int32 index, l_int32 accessflag)
boxaGetBox()
l_ok dewarpaUseBothArrays(L_DEWARPA *dewa, l_int32 useboth)
dewarpaUseBothArrays()
L_DEWARP * dewarpCreateRef(l_int32 pageno, l_int32 refpage)
dewarpCreateRef()
void dewarpaDestroy(L_DEWARPA **pdewa)
dewarpaDestroy()
l_ok dewarpaInsertDewarp(L_DEWARPA *dewa, L_DEWARP *dew)
dewarpaInsertDewarp()
void dewarpDestroy(L_DEWARP **pdew)
dewarpDestroy()
l_ok dewarpaSetCheckColumns(L_DEWARPA *dewa, l_int32 check_columns)
dewarpaSetCheckColumns()
L_DEWARP * dewarpaGetDewarp(L_DEWARPA *dewa, l_int32 index)
dewarpaGetDewarp()
L_DEWARPA * dewarpaCreate(l_int32 nptrs, l_int32 sampling, l_int32 redfactor, l_int32 minlines, l_int32 maxdist)
dewarpaCreate()
L_DEWARP * dewarpCreate(PIX *pixs, l_int32 pageno)
dewarpCreate()
l_ok dewarpBuildPageModel(L_DEWARP *dew, const char *debugfile)
dewarpBuildPageModel()
l_ok dewarpaModelStatus(L_DEWARPA *dewa, l_int32 pageno, l_int32 *pvsuccess, l_int32 *phsuccess)
dewarpaModelStatus()
l_ok dewarpaApplyDisparity(L_DEWARPA *dewa, l_int32 pageno, PIX *pixs, l_int32 grayin, l_int32 x, l_int32 y, PIX **ppixd, const char *debugfile)
dewarpaApplyDisparity()
l_ok dewarpMinimize(L_DEWARP *dew)
dewarpMinimize()
l_ok dewarpPopulateFullRes(L_DEWARP *dew, PIX *pix, l_int32 x, l_int32 y)
dewarpPopulateFullRes()
l_ok dewarpaStripRefModels(L_DEWARPA *dewa)
dewarpaStripRefModels()
l_ok dewarpaInfo(FILE *fp, L_DEWARPA *dewa)
dewarpaInfo()
l_ok dewarpSinglePageRun(PIX *pixs, PIX *pixb, L_DEWARPA *dewa, PIX **ppixd, l_int32 debug)
dewarpSinglePageRun()
l_ok dewarpaShowArrays(L_DEWARPA *dewa, l_float32 scalefact, l_int32 first, l_int32 last)
dewarpaShowArrays()
l_ok dewarpaListPages(L_DEWARPA *dewa)
dewarpaListPages()
l_ok dewarpaSetValidModels(L_DEWARPA *dewa, l_int32 notests, l_int32 debug)
dewarpaSetValidModels()
l_ok dewarpDebug(L_DEWARP *dew, const char *subdirs, l_int32 index)
dewarpDebug()
l_ok dewarpaModelStats(L_DEWARPA *dewa, l_int32 *pnnone, l_int32 *pnvsuccess, l_int32 *pnvvalid, l_int32 *pnhsuccess, l_int32 *pnhvalid, l_int32 *pnref)
dewarpaModelStats()
l_ok dewarpSinglePage(PIX *pixs, l_int32 thresh, l_int32 adaptive, l_int32 useboth, l_int32 check_columns, PIX **ppixd, L_DEWARPA **pdewa, l_int32 debug)
dewarpSinglePage()
l_ok dewarpaInsertRefModels(L_DEWARPA *dewa, l_int32 notests, l_int32 debug)
dewarpaInsertRefModels()
l_ok dewarpaRestoreModels(L_DEWARPA *dewa)
dewarpaRestoreModels()
l_ok dewarpSinglePageInit(PIX *pixs, l_int32 thresh, l_int32 adaptive, l_int32 useboth, l_int32 check_columns, PIX **ppixb, L_DEWARPA **pdewa)
dewarpSinglePageInit()
l_ok dewarpShowResults(L_DEWARPA *dewa, SARRAY *sa, BOXA *boxa, l_int32 firstpage, l_int32 lastpage, const char *pdfout)
dewarpShowResults()
static l_int32 dewarpaTestForValidModel(L_DEWARPA *dewa, L_DEWARP *dew, l_int32 notests)
dewarpaTestForValidModel()
PIX * fpixRenderContours(FPIX *fpixs, l_float32 incr, l_float32 proxim)
fpixRenderContours()
PIX * pixAdaptThresholdToBinary(PIX *pixs, PIX *pixm, l_float32 gamma)
pixAdaptThresholdToBinary()
PIX * pixThresholdToBinary(PIX *pixs, l_int32 thresh)
pixThresholdToBinary()
l_ok numaAddNumber(NUMA *na, l_float32 val)
numaAddNumber()
l_ok numaReplaceNumber(NUMA *na, l_int32 index, l_float32 val)
numaReplaceNumber()
NUMA * numaCreate(l_int32 n)
numaCreate()
void numaDestroy(NUMA **pna)
numaDestroy()
l_int32 numaGetCount(NUMA *na)
numaGetCount()
l_ok numaGetIValue(NUMA *na, l_int32 index, l_int32 *pival)
numaGetIValue()
NUMA * numaMakeConstant(l_float32 val, l_int32 size)
numaMakeConstant()
l_ok convertFilesToPdf(const char *dirname, const char *substr, l_int32 res, l_float32 scalefactor, l_int32 type, l_int32 quality, const char *title, const char *fileout)
convertFilesToPdf()
void pixDestroy(PIX **ppix)
pixDestroy()
PIX * pixCopy(PIX *pixd, const PIX *pixs)
pixCopy()
PIX * pixClone(PIX *pixs)
pixClone()
PIX * pixClipRectangle(PIX *pixs, BOX *box, BOX **pboxc)
pixClipRectangle()
l_ok pixaAddPix(PIXA *pixa, PIX *pix, l_int32 copyflag)
pixaAddPix()
void pixaDestroy(PIXA **ppixa)
pixaDestroy()
PIXA * pixaCreate(l_int32 n)
pixaCreate()
PIX * pixaDisplayTiledInRows(PIXA *pixa, l_int32 outdepth, l_int32 maxwidth, l_float32 scalefactor, l_int32 background, l_int32 spacing, l_int32 border)
pixaDisplayTiledInRows()
PIX * pixaDisplayTiledAndScaled(PIXA *pixa, l_int32 outdepth, l_int32 tilewidth, l_int32 ncols, l_int32 background, l_int32 spacing, l_int32 border)
pixaDisplayTiledAndScaled()
PIX * pixConvertTo8(PIX *pixs, l_int32 cmapflag)
pixConvertTo8()
PIX * pixReadIndexed(SARRAY *sa, l_int32 index)
pixReadIndexed()
PIX * pixScaleBySampling(PIX *pixs, l_float32 scalex, l_float32 scaley)
pixScaleBySampling()
struct FPix * fullvdispar
struct FPix * fullhdispar
struct FPix * sampvdispar
struct FPix * samphdispar
l_int32 min_diff_linecurv
struct L_Dewarp ** dewarp
l_int32 max_diff_linecurv
struct L_Dewarp ** dewarpcache
l_int32 max_diff_edgecurv
PIX * pixAddSingleTextblock(PIX *pixs, L_BMF *bmf, const char *textstr, l_uint32 val, l_int32 location, l_int32 *poverflow)
pixAddSingleTextblock()
void lept_stderr(const char *fmt,...)
lept_stderr()
l_int32 lept_rmdir(const char *subdir)
lept_rmdir()
char * pathJoin(const char *dir, const char *fname)
pathJoin()
l_int32 lept_mkdir(const char *subdir)
lept_mkdir()