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,
113 return ERROR_INT(
"&pixd not defined", __func__, 1);
115 if (pdewa) *pdewa = NULL;
117 return ERROR_INT(
"pixs not defined", __func__, 1);
120 check_columns, &pixb, &dewa);
123 return ERROR_INT(
"pixb not made", __func__, 1);
170 l_int32 check_columns,
176 if (ppixb) *ppixb = NULL;
177 if (pdewa) *pdewa = NULL;
178 if (!ppixb || !pdewa)
179 return ERROR_INT(
"&pixb and &dewa not both defined", __func__, 1);
181 return ERROR_INT(
"pixs not defined", __func__, 1);
184 if (pixGetDepth(pixs) > 1) {
186 return ERROR_INT(
"pix1 not made", __func__, 1);
193 return ERROR_INT(
"pix2 not made", __func__, 1);
232 const char *debugfile;
233 l_int32 vsuccess, ret;
237 return ERROR_INT(
"&pixd not defined", __func__, 1);
240 return ERROR_INT(
"pixs not defined", __func__, 1);
242 return ERROR_INT(
"pixb not defined", __func__, 1);
244 return ERROR_INT(
"dewa not defined", __func__, 1);
252 debugfile = (debug) ?
"/tmp/lept/dewarp/singlepage_model.pdf" : NULL;
256 L_ERROR(
"failure to build model for vertical disparity\n", __func__);
262 debugfile = (debug) ?
"/tmp/lept/dewarp/singlepage_apply.pdf" : NULL;
265 L_ERROR(
"invalid model; failure to apply disparity\n", __func__);
294 NUMA *namodels, *napages;
297 return ERROR_INT(
"dewa not defined", __func__, 1);
305 for (i = 0; i <= dewa->
maxpage; i++) {
344 l_int32 i, n, maxcurv, diffcurv, diffedge;
348 return ERROR_INT(
"dewa not defined", __func__, 1);
351 for (i = 0; i < n; i++) {
357 L_INFO(
"page %d: has only a ref model\n", __func__, i);
359 L_INFO(
"page %d: no model successfully built\n",
361 }
else if (!notests) {
365 L_INFO(
"page %d: useboth, but no horiz disparity\n",
368 L_INFO(
"page %d: max curvature %d > max_linecurv\n",
369 __func__, i, diffcurv);
370 if (diffcurv < dewa->min_diff_linecurv)
371 L_INFO(
"page %d: diff curv %d < min_diff_linecurv\n",
372 __func__, i, diffcurv);
374 L_INFO(
"page %d: abs diff curv %d > max_diff_linecurv\n",
375 __func__, i, diffcurv);
378 L_INFO(
"page %d: abs left slope %d > max_edgeslope\n",
381 L_INFO(
"page %d: abs right slope %d > max_edgeslope\n",
385 L_INFO(
"page %d: left curvature %d > max_edgecurv\n",
388 L_INFO(
"page %d: right curvature %d > max_edgecurv\n",
391 L_INFO(
"page %d: abs diff left-right curv %d > "
392 "max_diff_edgecurv\n", __func__, i, diffedge);
448 l_int32 i, j, n, val, min, distdown, distup;
453 return ERROR_INT(
"dewa not defined", __func__, 1);
455 L_INFO(
"maxdist < 2; no ref models can be used\n", __func__);
461 for (i = 0; i < n; i++) {
473 for (i = 0; i < n; i++) {
475 if (val == 1)
continue;
476 if ((dew = dewa->
dewarp[i]) != NULL) {
480 if (dewa->
maxdist < 2)
continue;
482 distdown = distup = dewa->
maxdist + 1;
483 for (j = i - 2; j >= 0 && distdown > dewa->
maxdist; j -= 2) {
485 if (val == 1) distdown = i - j;
488 for (j = i + 2; j < n && distup > dewa->
maxdist; j += 2) {
490 if (val == 1) distup = j - i;
492 min = L_MIN(distdown, distup);
493 if (min > dewa->
maxdist)
continue;
494 if (distdown <= distup)
509 for (i = 0; i < n; i++) {
514 for (i = 0; i < n; i++) {
516 if (val == 1)
continue;
517 if (dewa->
maxdist < 2)
continue;
518 distdown = distup = 100000;
519 for (j = i - 2; j >= 0; j -= 2) {
526 for (j = i + 2; j < n; j += 2) {
533 min = L_MIN(distdown, distup);
534 if (min > dewa->
maxdist)
continue;
539 L_ERROR(
"dew is null for page %d!\n", __func__, i);
546 if (distdown <= distup)
579 return ERROR_INT(
"dewa not defined", __func__, 1);
581 for (i = 0; i <= dewa->
maxpage; i++) {
618 return ERROR_INT(
"dewa not defined", __func__, 1);
625 for (i = 0; i <= dewa->
maxpage; i++) {
628 L_ERROR(
"dew in both cache and main array!: page %d\n",
658 l_int32 i, n, pageno, nnone, nvsuccess, nvvalid, nhsuccess, nhvalid, nref;
662 return ERROR_INT(
"dewa not defined", __func__, 1);
664 return ERROR_INT(
"dewa not defined", __func__, 1);
666 fprintf(fp,
"\nDewarpaInfo: %p\n", dewa);
667 fprintf(fp,
"nalloc = %d, maxpage = %d\n", dewa->
nalloc, dewa->
maxpage);
668 fprintf(fp,
"sampling = %d, redfactor = %d, minlines = %d\n",
670 fprintf(fp,
"maxdist = %d, useboth = %d\n",
674 &nhsuccess, &nhvalid, &nref);
676 lept_stderr(
"Total number of pages with a dew = %d\n", n);
677 lept_stderr(
"Number of pages without any models = %d\n", nnone);
678 lept_stderr(
"Number of pages with a vert model = %d\n", nvsuccess);
679 lept_stderr(
"Number of pages with a valid vert model = %d\n", nvvalid);
680 lept_stderr(
"Number of pages with both models = %d\n", nhsuccess);
681 lept_stderr(
"Number of pages with both models valid = %d\n", nhvalid);
682 lept_stderr(
"Number of pages with a ref model = %d\n", nref);
684 for (i = 0; i < n; i++) {
693 dew->
w, dew->
h, dew->
nx, dew->
ny);
696 " (min,max,abs-diff) line curvature = (%d,%d,%d)\n",
700 " left edge slope = %d, right edge slope = %d\n"
701 " (left,right,abs-diff) edge curvature = (%d,%d,%d)\n",
753 l_int32 i, n, pageno, nnone, nvsuccess, nvvalid, nhsuccess, nhvalid, nref;
757 return ERROR_INT(
"dewa not defined", __func__, 1);
761 nnone = nref = nvsuccess = nvvalid = nhsuccess = nhvalid = 0;
762 for (i = 0; i < n; i++) {
782 if (pnnone) *pnnone = nnone;
783 if (pnref) *pnref = nref;
784 if (pnvsuccess) *pnvsuccess = nvsuccess;
785 if (pnvvalid) *pnvvalid = nvvalid;
786 if (pnhsuccess) *pnhsuccess = nhsuccess;
787 if (pnhvalid) *pnhvalid = nhvalid;
815 l_int32 maxcurv, diffcurv, diffedge;
818 return ERROR_INT(
"dewa and dew not both defined", __func__, 1);
830 if (dew->
hasref == 1)
return 0;
837 if (maxcurv <= dewa->max_linecurv &&
839 diffcurv <= dewa->max_diff_linecurv) {
842 L_INFO(
"invalid vert model for page %d:\n", __func__, dew->
pageno);
843 #if DEBUG_INVALID_MODELS
844 lept_stderr(
" max line curv = %d, max allowed = %d\n",
846 lept_stderr(
" diff line curv = %d, max allowed = %d\n",
858 diffedge <= dewa->max_diff_edgecurv) {
861 L_INFO(
"invalid horiz model for page %d:\n", __func__, dew->
pageno);
862 #if DEBUG_INVALID_MODELS
863 lept_stderr(
" left edge slope = %d, max allowed = %d\n",
865 lept_stderr(
" right edge slope = %d, max allowed = %d\n",
867 lept_stderr(
" left edge curv = %d, max allowed = %d\n",
869 lept_stderr(
" right edge curv = %d, max allowed = %d\n",
871 lept_stderr(
" diff edge curv = %d, max allowed = %d\n",
906 PIX *pixv, *pixvs, *pixh, *pixhs, *pixt, *pixd;
910 return ERROR_INT(
"dew not defined", __func__, 1);
911 if (first < 0 || first > dewa->
maxpage)
912 return ERROR_INT(
"first out of bounds", __func__, 1);
915 return ERROR_INT(
"last < first", __func__, 1);
920 L_ERROR(
"bmf not made; page info not displayed", __func__);
923 for (i = first; i <= last; i++) {
924 if (i && ((i % 10) == 0))
928 if (dew->
hasref == 1)
continue;
933 L_ERROR(
"sampvdispar not made for page %d!\n", __func__, i);
955 snprintf(buf,
sizeof(buf),
"Page %d", i);
958 snprintf(buf,
sizeof(buf),
"/tmp/lept/dewarp1/arrays_%04d.png", i);
959 pixWriteDebug(buf, pixd, IFF_PNG);
969 0,
"Disparity arrays",
"/tmp/lept/disparity_arrays.pdf");
970 lept_stderr(
"Output written to: /tmp/lept/disparity_arrays.pdf\n");
1001 return ERROR_INT(
"dew not defined", __func__, 1);
1003 return ERROR_INT(
"subdirs not defined", __func__, 1);
1005 lept_stderr(
"pageno = %d, hasref = %d, refpage = %d\n",
1007 lept_stderr(
"sampling = %d, redfactor = %d, minlines = %d\n",
1013 lept_stderr(
"sampv = %d, samph = %d\n", svd, shd);
1018 lept_stderr(
"(min,max,abs-diff) line curvature = (%d,%d,%d)\n",
1022 lept_stderr(
"(left edge slope = %d, right edge slope = %d\n",
1024 lept_stderr(
"(left,right,abs-diff) edge curvature = "
1036 outdir =
pathJoin(
"/tmp", subdirs);
1039 snprintf(fname,
sizeof(fname),
"%s/pixv_%d.png", outdir, index);
1040 pixWriteDebug(fname, pixv, IFF_PNG);
1045 snprintf(fname,
sizeof(fname),
"%s/pixh_%d.png", outdir, index);
1046 pixWriteDebug(fname, pixh, IFF_PNG);
1084 l_int32 i, modelpage;
1088 PIX *pixs, *pixc, *pixd, *pixt1, *pixt2;
1092 return ERROR_INT(
"dewa not defined", __func__, 1);
1094 return ERROR_INT(
"sa not defined", __func__, 1);
1096 return ERROR_INT(
"pdfout not defined", __func__, 1);
1097 if (firstpage > lastpage)
1098 return ERROR_INT(
"invalid first/last page numbers", __func__, 1);
1104 lept_stderr(
"Dewarping and generating s/by/s view\n");
1105 for (i = firstpage; i <= lastpage; i++) {
1119 GrayInValue, 0, 0, &pixd, NULL);
1129 snprintf(bufstr,
sizeof(bufstr),
"Page %d; using %d\n",
1133 snprintf(bufstr,
sizeof(bufstr),
"Page %d; no dewarp\n", i);
1136 snprintf(bufstr,
sizeof(bufstr),
"/tmp/lept/dewarp_pdfout/%05d", i);
1137 pixWriteDebug(bufstr, pixt2, IFF_JFIF_JPEG);
1147 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()