413 #include <config_auto.h>
417 #include "allheaders.h"
422 static const l_int32 InitialPtrArraySize = 20;
423 static const l_int32 MaxPtrArraySize = 10000;
424 static const l_int32 DefaultArraySampling = 30;
425 static const l_int32 MinArraySampling = 8;
426 static const l_int32 DefaultMinLines = 15;
427 static const l_int32 MinMinLines = 4;
428 static const l_int32 DefaultMaxRefDist = 16;
429 static const l_int32 DefaultUseBoth = TRUE;
430 static const l_int32 DefaultCheckColumns = TRUE;
433 static const l_int32 DefaultMaxLineCurv = 150;
434 static const l_int32 DefaultMinDiffLineCurv = 0;
435 static const l_int32 DefaultMaxDiffLineCurv = 170;
436 static const l_int32 DefaultMaxEdgeCurv = 50;
437 static const l_int32 DefaultMaxDiffEdgeCurv = 40;
438 static const l_int32 DefaultMaxEdgeSlope = 80;
465 return (
L_DEWARP *)ERROR_PTR(
"pixs not defined", __func__, NULL);
466 if (pixGetDepth(pixs) != 1)
467 return (
L_DEWARP *)ERROR_PTR(
"pixs not 1 bpp", __func__, NULL);
472 dew->
w = pixGetWidth(pixs);
473 dew->
h = pixGetHeight(pixs);
521 L_WARNING(
"ptr address is null!\n", __func__);
524 if ((dew = *pdew) == NULL)
585 nptrs = InitialPtrArraySize;
586 if (nptrs > MaxPtrArraySize)
587 return (
L_DEWARPA *)ERROR_PTR(
"too many pages", __func__, NULL);
588 if (redfactor != 1 && redfactor != 2)
589 return (
L_DEWARPA *)ERROR_PTR(
"redfactor not in {1,2}",
592 sampling = DefaultArraySampling;
593 }
else if (sampling < MinArraySampling) {
594 L_WARNING(
"sampling too small; setting to %d\n", __func__,
596 sampling = MinArraySampling;
599 minlines = DefaultMinLines;
600 }
else if (minlines < MinMinLines) {
601 L_WARNING(
"minlines too small; setting to %d\n", __func__,
603 minlines = DefaultMinLines;
606 maxdist = DefaultMaxRefDist;
613 return (
L_DEWARPA *)ERROR_PTR(
"dewarp ptrs not made", __func__, NULL);
627 dewa->
useboth = DefaultUseBoth;
669 l_int32 i, nptrs, pageno;
675 return (
L_DEWARPA *)ERROR_PTR(
"pixac not defined", __func__, NULL);
679 sampling, 1, minlines, maxdist)) == NULL)
680 return (
L_DEWARPA *)ERROR_PTR(
"dewa not made", __func__, NULL);
683 for (i = 0; i < nptrs; i++) {
686 if (pixt && (pixGetWidth(pixt) > 1)) {
690 ERROR_INT(
"unable to make dew!", __func__, 1);
701 L_ERROR(
"unable to build model for page %d\n", __func__, i);
729 L_WARNING(
"ptr address is null!\n", __func__);
732 if ((dewa = *pdewa) == NULL)
735 for (i = 0; i < dewa->
nalloc; i++) {
736 if ((dew = dewa->
dewarp[i]) != NULL)
765 return ERROR_INT(
"dewa or dew not defined", __func__, 1);
766 if (pageno < 0 || pageno > dewa->
maxpage)
767 return ERROR_INT(
"page out of bounds", __func__, 1);
768 if ((dew = dewa->
dewarp[pageno]) == NULL)
769 return ERROR_INT(
"dew not defined", __func__, 1);
772 dewa->
dewarp[pageno] = NULL;
803 l_int32 pageno, n, newsize;
807 return ERROR_INT(
"dewa not defined", __func__, 1);
809 return ERROR_INT(
"dew not defined", __func__, 1);
813 if (pageno > MaxPtrArraySize)
814 return ERROR_INT(
"too many pages", __func__, 1);
823 newsize = 2 * pageno;
824 else if (pageno >= n)
828 return ERROR_INT(
"extension failed", __func__, 1);
833 dewa->
dewarp[pageno] = dew;
869 return ERROR_INT(
"dewa not defined", __func__, 1);
871 if (size > dewa->
nalloc) {
874 size *
sizeof(
L_DEWARP *))) == NULL)
875 return ERROR_INT(
"new ptr array not returned", __func__, 1);
879 size *
sizeof(
L_DEWARP *))) == NULL)
880 return ERROR_INT(
"new ptr cache array not returned", __func__, 1);
899 return (
L_DEWARP *)ERROR_PTR(
"dewa not defined", __func__, NULL);
900 if (index < 0 || index > dewa->
maxpage) {
901 L_ERROR(
"index = %d is invalid; max index = %d\n",
902 __func__, index, dewa->
maxpage);
906 return dewa->
dewarp[index];
963 l_int32 max_linecurv,
964 l_int32 min_diff_linecurv,
965 l_int32 max_diff_linecurv,
966 l_int32 max_edgecurv,
967 l_int32 max_diff_edgecurv,
968 l_int32 max_edgeslope)
971 return ERROR_INT(
"dewa not defined", __func__, 1);
973 if (max_linecurv == -1)
978 if (min_diff_linecurv == -1)
983 if (max_diff_linecurv == -1)
988 if (max_edgecurv == -1)
993 if (max_diff_edgecurv == -1)
998 if (max_edgeslope == -1)
1028 return ERROR_INT(
"dewa not defined", __func__, 1);
1062 l_int32 check_columns)
1065 return ERROR_INT(
"dewa not defined", __func__, 1);
1089 return ERROR_INT(
"dewa not defined", __func__, 1);
1113 return (
L_DEWARP *)ERROR_PTR(
"filename not defined", __func__, NULL);
1115 return (
L_DEWARP *)ERROR_PTR(
"stream not opened", __func__, NULL);
1119 return (
L_DEWARP *)ERROR_PTR(
"dew not read", __func__, NULL);
1147 l_int32 version, sampling, redfactor, minlines, pageno, hasref, refpage;
1148 l_int32 w, h, nx, ny, vdispar, hdispar, nlines;
1149 l_int32 mincurv, maxcurv, leftslope, rightslope, leftcurv, rightcurv;
1151 FPIX *fpixv, *fpixh;
1154 return (
L_DEWARP *)ERROR_PTR(
"stream not defined", __func__, NULL);
1156 if (fscanf(fp,
"\nDewarp Version %d\n", &version) != 1)
1157 return (
L_DEWARP *)ERROR_PTR(
"not a dewarp file", __func__, NULL);
1159 return (
L_DEWARP *)ERROR_PTR(
"invalid dewarp version", __func__, NULL);
1160 if (fscanf(fp,
"pageno = %d\n", &pageno) != 1)
1161 return (
L_DEWARP *)ERROR_PTR(
"read fail for pageno", __func__, NULL);
1162 if (fscanf(fp,
"hasref = %d, refpage = %d\n", &hasref, &refpage) != 2)
1163 return (
L_DEWARP *)ERROR_PTR(
"read fail for hasref, refpage",
1165 if (fscanf(fp,
"sampling = %d, redfactor = %d\n", &sampling, &redfactor)
1167 return (
L_DEWARP *)ERROR_PTR(
"read fail for sampling/redfactor",
1169 if (fscanf(fp,
"nlines = %d, minlines = %d\n", &nlines, &minlines) != 2)
1170 return (
L_DEWARP *)ERROR_PTR(
"read fail for nlines/minlines",
1172 if (fscanf(fp,
"w = %d, h = %d\n", &w, &h) != 2)
1173 return (
L_DEWARP *)ERROR_PTR(
"read fail for w, h", __func__, NULL);
1174 if (fscanf(fp,
"nx = %d, ny = %d\n", &nx, &ny) != 2)
1175 return (
L_DEWARP *)ERROR_PTR(
"read fail for nx, ny", __func__, NULL);
1176 if (fscanf(fp,
"vert_dispar = %d, horiz_dispar = %d\n", &vdispar, &hdispar)
1178 return (
L_DEWARP *)ERROR_PTR(
"read fail for flags", __func__, NULL);
1180 if (fscanf(fp,
"min line curvature = %d, max line curvature = %d\n",
1181 &mincurv, &maxcurv) != 2)
1182 return (
L_DEWARP *)ERROR_PTR(
"read fail for mincurv & maxcurv",
1186 if (fscanf(fp,
"left edge slope = %d, right edge slope = %d\n",
1187 &leftslope, &rightslope) != 2)
1188 return (
L_DEWARP *)ERROR_PTR(
"read fail for leftslope & rightslope",
1190 if (fscanf(fp,
"left edge curvature = %d, right edge curvature = %d\n",
1191 &leftcurv, &rightcurv) != 2)
1192 return (
L_DEWARP *)ERROR_PTR(
"read fail for leftcurv & rightcurv",
1197 return (
L_DEWARP *)ERROR_PTR(
"read fail for vdispar",
1202 return (
L_DEWARP *)ERROR_PTR(
"read fail for hdispar",
1255 return (
L_DEWARP *)ERROR_PTR(
"data not defined", __func__, NULL);
1257 return (
L_DEWARP *)ERROR_PTR(
"stream not opened", __func__, NULL);
1261 if (!dew) L_ERROR(
"dew not read\n", __func__);
1281 return ERROR_INT(
"filename not defined", __func__, 1);
1283 return ERROR_INT(
"dew not defined", __func__, 1);
1286 return ERROR_INT(
"stream not opened", __func__, 1);
1290 return ERROR_INT(
"dew not written to stream", __func__, 1);
1313 l_int32 vdispar, hdispar;
1316 return ERROR_INT(
"stream not defined", __func__, 1);
1318 return ERROR_INT(
"dew not defined", __func__, 1);
1321 fprintf(fp,
"pageno = %d\n", dew->
pageno);
1322 fprintf(fp,
"hasref = %d, refpage = %d\n", dew->
hasref, dew->
refpage);
1323 fprintf(fp,
"sampling = %d, redfactor = %d\n",
1325 fprintf(fp,
"nlines = %d, minlines = %d\n", dew->
nlines, dew->
minlines);
1326 fprintf(fp,
"w = %d, h = %d\n", dew->
w, dew->
h);
1327 fprintf(fp,
"nx = %d, ny = %d\n", dew->
nx, dew->
ny);
1330 fprintf(fp,
"vert_dispar = %d, horiz_dispar = %d\n", vdispar, hdispar);
1332 fprintf(fp,
"min line curvature = %d, max line curvature = %d\n",
1335 fprintf(fp,
"left edge slope = %d, right edge slope = %d\n",
1337 fprintf(fp,
"left edge curvature = %d, right edge curvature = %d\n",
1345 L_WARNING(
"no disparity arrays!\n", __func__);
1371 if (pdata) *pdata = NULL;
1372 if (psize) *psize = 0;
1374 return ERROR_INT(
"&data not defined", __func__, 1);
1376 return ERROR_INT(
"&size not defined", __func__, 1);
1378 return ERROR_INT(
"dew not defined", __func__, 1);
1381 if ((fp = open_memstream((
char **)pdata, psize)) == NULL)
1382 return ERROR_INT(
"stream not opened", __func__, 1);
1386 *psize = *psize - 1;
1388 L_INFO(
"work-around: writing to a temp file\n", __func__);
1391 return ERROR_INT(
"tmpfile stream not opened", __func__, 1);
1393 if ((fp = tmpfile()) == NULL)
1394 return ERROR_INT(
"tmpfile stream not opened", __func__, 1);
1421 return (
L_DEWARPA *)ERROR_PTR(
"filename not defined", __func__, NULL);
1423 return (
L_DEWARPA *)ERROR_PTR(
"stream not opened", __func__, NULL);
1427 return (
L_DEWARPA *)ERROR_PTR(
"dewa not read", __func__, NULL);
1452 l_int32 i, version, ndewarp, maxpage;
1453 l_int32 sampling, redfactor, minlines, maxdist, useboth;
1454 l_int32 max_linecurv, min_diff_linecurv, max_diff_linecurv;
1455 l_int32 max_edgeslope, max_edgecurv, max_diff_edgecurv;
1461 return (
L_DEWARPA *)ERROR_PTR(
"stream not defined", __func__, NULL);
1463 if (fscanf(fp,
"\nDewarpa Version %d\n", &version) != 1)
1464 return (
L_DEWARPA *)ERROR_PTR(
"not a dewarpa file", __func__, NULL);
1466 return (
L_DEWARPA *)ERROR_PTR(
"invalid dewarp version", __func__, NULL);
1468 if (fscanf(fp,
"ndewarp = %d, maxpage = %d\n", &ndewarp, &maxpage) != 2)
1469 return (
L_DEWARPA *)ERROR_PTR(
"read fail for maxpage+", __func__, NULL);
1471 return (
L_DEWARPA *)ERROR_PTR(
"pages not >= 1", __func__, NULL);
1472 if (ndewarp > MaxPtrArraySize)
1473 return (
L_DEWARPA *)ERROR_PTR(
"too many pages", __func__, NULL);
1475 "sampling = %d, redfactor = %d, minlines = %d, maxdist = %d\n",
1476 &sampling, &redfactor, &minlines, &maxdist) != 4)
1477 return (
L_DEWARPA *)ERROR_PTR(
"read fail for 4 params", __func__, NULL);
1479 "max_linecurv = %d, min_diff_linecurv = %d, max_diff_linecurv = %d\n",
1480 &max_linecurv, &min_diff_linecurv, &max_diff_linecurv) != 3)
1481 return (
L_DEWARPA *)ERROR_PTR(
"read fail for linecurv", __func__, NULL);
1483 "max_edgeslope = %d, max_edgecurv = %d, max_diff_edgecurv = %d\n",
1484 &max_edgeslope, &max_edgecurv, &max_diff_edgecurv) != 3)
1485 return (
L_DEWARPA *)ERROR_PTR(
"read fail for edgecurv", __func__, NULL);
1486 if (fscanf(fp,
"fullmodel = %d\n", &useboth) != 1)
1487 return (
L_DEWARPA *)ERROR_PTR(
"read fail for useboth", __func__, NULL);
1489 dewa =
dewarpaCreate(maxpage + 1, sampling, redfactor, minlines, maxdist);
1500 for (i = 0; i < ndewarp; i++) {
1502 L_ERROR(
"read fail for dew[%d]\n", __func__, i);
1531 return (
L_DEWARPA *)ERROR_PTR(
"data not defined", __func__, NULL);
1533 return (
L_DEWARPA *)ERROR_PTR(
"stream not opened", __func__, NULL);
1537 if (!dewa) L_ERROR(
"dewa not read\n", __func__);
1557 return ERROR_INT(
"filename not defined", __func__, 1);
1559 return ERROR_INT(
"dewa not defined", __func__, 1);
1562 return ERROR_INT(
"stream not opened", __func__, 1);
1566 return ERROR_INT(
"dewa not written to stream", __func__, 1);
1582 l_int32 ndewarp, i, pageno;
1585 return ERROR_INT(
"stream not defined", __func__, 1);
1587 return ERROR_INT(
"dewa not defined", __func__, 1);
1596 return ERROR_INT(
"dewa->namodels not made", __func__, 1);
1600 fprintf(fp,
"ndewarp = %d, maxpage = %d\n", ndewarp, dewa->
maxpage);
1601 fprintf(fp,
"sampling = %d, redfactor = %d, minlines = %d, maxdist = %d\n",
1604 "max_linecurv = %d, min_diff_linecurv = %d, max_diff_linecurv = %d\n",
1607 "max_edgeslope = %d, max_edgecurv = %d, max_diff_edgecurv = %d\n",
1609 fprintf(fp,
"fullmodel = %d\n", dewa->
useboth);
1610 for (i = 0; i < ndewarp; i++) {
1640 if (pdata) *pdata = NULL;
1641 if (psize) *psize = 0;
1643 return ERROR_INT(
"&data not defined", __func__, 1);
1645 return ERROR_INT(
"&size not defined", __func__, 1);
1647 return ERROR_INT(
"dewa not defined", __func__, 1);
1650 if ((fp = open_memstream((
char **)pdata, psize)) == NULL)
1651 return ERROR_INT(
"stream not opened", __func__, 1);
1655 *psize = *psize - 1;
1657 L_INFO(
"work-around: writing to a temp file\n", __func__);
1660 return ERROR_INT(
"tmpfile stream not opened", __func__, 1);
1662 if ((fp = tmpfile()) == NULL)
1663 return ERROR_INT(
"tmpfile stream not opened", __func__, 1);
L_DEWARPA * dewarpaRead(const char *filename)
dewarpaRead()
L_DEWARPA * dewarpaReadStream(FILE *fp)
dewarpaReadStream()
l_ok dewarpaUseBothArrays(L_DEWARPA *dewa, l_int32 useboth)
dewarpaUseBothArrays()
l_ok dewarpaSetMaxDistance(L_DEWARPA *dewa, l_int32 maxdist)
dewarpaSetMaxDistance()
L_DEWARP * dewarpCreateRef(l_int32 pageno, l_int32 refpage)
dewarpCreateRef()
void dewarpaDestroy(L_DEWARPA **pdewa)
dewarpaDestroy()
static l_int32 dewarpaExtendArraysToSize(L_DEWARPA *dewa, l_int32 size)
dewarpaExtendArraysToSize()
l_ok dewarpWriteStream(FILE *fp, L_DEWARP *dew)
dewarpWriteStream()
l_ok dewarpaDestroyDewarp(L_DEWARPA *dewa, l_int32 pageno)
dewarpaDestroyDewarp()
l_ok dewarpaInsertDewarp(L_DEWARPA *dewa, L_DEWARP *dew)
dewarpaInsertDewarp()
void dewarpDestroy(L_DEWARP **pdew)
dewarpDestroy()
l_ok dewarpWrite(const char *filename, L_DEWARP *dew)
dewarpWrite()
L_DEWARP * dewarpReadStream(FILE *fp)
dewarpReadStream()
L_DEWARPA * dewarpaCreateFromPixacomp(PIXAC *pixac, l_int32 useboth, l_int32 sampling, l_int32 minlines, l_int32 maxdist)
dewarpaCreateFromPixacomp()
l_ok dewarpaSetCheckColumns(L_DEWARPA *dewa, l_int32 check_columns)
dewarpaSetCheckColumns()
L_DEWARPA * dewarpaReadMem(const l_uint8 *data, size_t size)
dewarpaReadMem()
l_ok dewarpaWriteStream(FILE *fp, L_DEWARPA *dewa)
dewarpaWriteStream()
l_ok dewarpWriteMem(l_uint8 **pdata, size_t *psize, L_DEWARP *dew)
dewarpWriteMem()
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 dewarpaWrite(const char *filename, L_DEWARPA *dewa)
dewarpaWrite()
L_DEWARP * dewarpReadMem(const l_uint8 *data, size_t size)
dewarpReadMem()
l_ok dewarpaWriteMem(l_uint8 **pdata, size_t *psize, L_DEWARPA *dewa)
dewarpaWriteMem()
l_ok dewarpaSetCurvatures(L_DEWARPA *dewa, l_int32 max_linecurv, l_int32 min_diff_linecurv, l_int32 max_diff_linecurv, l_int32 max_edgecurv, l_int32 max_diff_edgecurv, l_int32 max_edgeslope)
dewarpaSetCurvatures()
L_DEWARP * dewarpRead(const char *filename)
dewarpRead()
l_ok dewarpBuildPageModel(L_DEWARP *dew, const char *debugfile)
dewarpBuildPageModel()
l_ok dewarpMinimize(L_DEWARP *dew)
dewarpMinimize()
l_ok dewarpaListPages(L_DEWARPA *dewa)
dewarpaListPages()
l_ok dewarpaInsertRefModels(L_DEWARPA *dewa, l_int32 notests, l_int32 debug)
dewarpaInsertRefModels()
#define DEWARP_VERSION_NUMBER
l_ok fpixWriteStream(FILE *fp, FPIX *fpix)
fpixWriteStream()
void fpixDestroy(FPIX **pfpix)
fpixDestroy()
FPIX * fpixReadStream(FILE *fp)
fpixReadStream()
l_ok numaAddNumber(NUMA *na, l_float32 val)
numaAddNumber()
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()
void pixDestroy(PIX **ppix)
pixDestroy()
PIX * pixClone(PIX *pixs)
pixClone()
l_int32 pixacompGetOffset(PIXAC *pixac)
pixacompGetOffset()
l_int32 pixacompGetCount(PIXAC *pixac)
pixacompGetCount()
PIX * pixacompGetPix(PIXAC *pixac, l_int32 index)
pixacompGetPix()
struct FPix * fullvdispar
struct FPix * fullydispar
struct FPix * fullhdispar
struct FPix * sampydispar
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
l_uint8 * l_binaryReadStream(FILE *fp, size_t *pnbytes)
l_binaryReadStream()
FILE * fopenReadFromMemory(const l_uint8 *data, size_t size)
fopenReadFromMemory()
FILE * fopenWriteStream(const char *filename, const char *modestring)
fopenWriteStream()
void * reallocNew(void **pindata, size_t oldsize, size_t newsize)
reallocNew()
FILE * fopenWriteWinTempfile(void)
fopenWriteWinTempfile()
FILE * fopenReadStream(const char *filename)
fopenReadStream()