413#include <config_auto.h>
417#include "allheaders.h"
422static const l_int32 InitialPtrArraySize = 20;
423static const l_int32 MaxPtrArraySize = 10000;
424static const l_int32 DefaultArraySampling = 30;
425static const l_int32 MinArraySampling = 8;
426static const l_int32 DefaultMinLines = 15;
427static const l_int32 MinMinLines = 4;
428static const l_int32 DefaultMaxRefDist = 16;
429static const l_int32 DefaultUseBoth = TRUE;
430static const l_int32 DefaultCheckColumns = TRUE;
433static const l_int32 DefaultMaxLineCurv = 150;
434static const l_int32 DefaultMinDiffLineCurv = 0;
435static const l_int32 DefaultMaxDiffLineCurv = 170;
436static const l_int32 DefaultMaxEdgeCurv = 50;
437static const l_int32 DefaultMaxDiffEdgeCurv = 40;
438static 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);
470 dew->
pixs = pixClone(pixs);
472 dew->
w = pixGetWidth(pixs);
473 dew->
h = pixGetHeight(pixs);
521 L_WARNING(
"ptr address is null!\n", __func__);
524 if ((dew = *pdew) == NULL)
527 pixDestroy(&dew->
pixs);
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;
669l_int32 i, nptrs, pageno;
675 return (
L_DEWARPA *)ERROR_PTR(
"pixac not defined", __func__, NULL);
677 nptrs = pixacompGetCount(pixac);
679 sampling, 1, minlines, maxdist)) == NULL)
680 return (
L_DEWARPA *)ERROR_PTR(
"dewa not made", __func__, NULL);
683 for (i = 0; i < nptrs; i++) {
684 pageno = pixacompGetOffset(pixac) + i;
685 pixt = pixacompGetPix(pixac, pageno);
686 if (pixt && (pixGetWidth(pixt) > 1)) {
690 ERROR_INT(
"unable to make dew!", __func__, 1);
698 dewarpBuildPageModel(dew, NULL);
701 L_ERROR(
"unable to build model for page %d\n", __func__, i);
709 dewarpaInsertRefModels(dewa, 0, 0);
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;
803l_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);
1114 if ((fp = fopenReadStream(filename)) == NULL)
1115 return (
L_DEWARP *)ERROR_PTR_1(
"stream not opened",
1116 filename, __func__, NULL);
1120 return (
L_DEWARP *)ERROR_PTR_1(
"dew not read",
1121 filename, __func__, NULL);
1149l_int32 version, sampling, redfactor, minlines, pageno, hasref, refpage;
1150l_int32 w, h, nx, ny, vdispar, hdispar, nlines;
1151l_int32 mincurv, maxcurv, leftslope, rightslope, leftcurv, rightcurv;
1153FPIX *fpixv = NULL, *fpixh = NULL;
1156 return (
L_DEWARP *)ERROR_PTR(
"stream not defined", __func__, NULL);
1158 if (fscanf(fp,
"\nDewarp Version %d\n", &version) != 1)
1159 return (
L_DEWARP *)ERROR_PTR(
"not a dewarp file", __func__, NULL);
1161 return (
L_DEWARP *)ERROR_PTR(
"invalid dewarp version", __func__, NULL);
1162 if (fscanf(fp,
"pageno = %d\n", &pageno) != 1)
1163 return (
L_DEWARP *)ERROR_PTR(
"read fail for pageno", __func__, NULL);
1164 if (fscanf(fp,
"hasref = %d, refpage = %d\n", &hasref, &refpage) != 2)
1165 return (
L_DEWARP *)ERROR_PTR(
"read fail for hasref, refpage",
1167 if (fscanf(fp,
"sampling = %d, redfactor = %d\n", &sampling, &redfactor)
1169 return (
L_DEWARP *)ERROR_PTR(
"read fail for sampling/redfactor",
1171 if (fscanf(fp,
"nlines = %d, minlines = %d\n", &nlines, &minlines) != 2)
1172 return (
L_DEWARP *)ERROR_PTR(
"read fail for nlines/minlines",
1174 if (fscanf(fp,
"w = %d, h = %d\n", &w, &h) != 2)
1175 return (
L_DEWARP *)ERROR_PTR(
"read fail for w, h", __func__, NULL);
1176 if (fscanf(fp,
"nx = %d, ny = %d\n", &nx, &ny) != 2)
1177 return (
L_DEWARP *)ERROR_PTR(
"read fail for nx, ny", __func__, NULL);
1178 if (fscanf(fp,
"vert_dispar = %d, horiz_dispar = %d\n", &vdispar, &hdispar)
1180 return (
L_DEWARP *)ERROR_PTR(
"read fail for flags", __func__, NULL);
1182 if (fscanf(fp,
"min line curvature = %d, max line curvature = %d\n",
1183 &mincurv, &maxcurv) != 2)
1184 return (
L_DEWARP *)ERROR_PTR(
"read fail for mincurv & maxcurv",
1188 if (fscanf(fp,
"left edge slope = %d, right edge slope = %d\n",
1189 &leftslope, &rightslope) != 2)
1190 return (
L_DEWARP *)ERROR_PTR(
"read fail for leftslope & rightslope",
1192 if (fscanf(fp,
"left edge curvature = %d, right edge curvature = %d\n",
1193 &leftcurv, &rightcurv) != 2)
1194 return (
L_DEWARP *)ERROR_PTR(
"read fail for leftcurv & rightcurv",
1198 if ((fpixv = fpixReadStream(fp)) == NULL)
1199 return (
L_DEWARP *)ERROR_PTR(
"read fail for vdispar",
1203 if ((fpixh = fpixReadStream(fp)) == NULL)
1204 return (
L_DEWARP *)ERROR_PTR(
"read fail for hdispar",
1257 return (
L_DEWARP *)ERROR_PTR(
"data not defined", __func__, NULL);
1258 if ((fp = fopenReadFromMemory(data, size)) == NULL)
1259 return (
L_DEWARP *)ERROR_PTR(
"stream not opened", __func__, NULL);
1263 if (!dew) L_ERROR(
"dew not read\n", __func__);
1283 return ERROR_INT(
"filename not defined", __func__, 1);
1285 return ERROR_INT(
"dew not defined", __func__, 1);
1287 if ((fp = fopenWriteStream(filename,
"wb")) == NULL)
1288 return ERROR_INT_1(
"stream not opened", filename, __func__, 1);
1292 return ERROR_INT_1(
"dew not written to stream", filename, __func__, 1);
1315l_int32 vdispar, hdispar;
1318 return ERROR_INT(
"stream not defined", __func__, 1);
1320 return ERROR_INT(
"dew not defined", __func__, 1);
1323 fprintf(fp,
"pageno = %d\n", dew->
pageno);
1324 fprintf(fp,
"hasref = %d, refpage = %d\n", dew->
hasref, dew->
refpage);
1325 fprintf(fp,
"sampling = %d, redfactor = %d\n",
1327 fprintf(fp,
"nlines = %d, minlines = %d\n", dew->
nlines, dew->
minlines);
1328 fprintf(fp,
"w = %d, h = %d\n", dew->
w, dew->
h);
1329 fprintf(fp,
"nx = %d, ny = %d\n", dew->
nx, dew->
ny);
1332 fprintf(fp,
"vert_dispar = %d, horiz_dispar = %d\n", vdispar, hdispar);
1334 fprintf(fp,
"min line curvature = %d, max line curvature = %d\n",
1337 fprintf(fp,
"left edge slope = %d, right edge slope = %d\n",
1339 fprintf(fp,
"left edge curvature = %d, right edge curvature = %d\n",
1342 if (vdispar) fpixWriteStream(fp, dew->
sampvdispar);
1343 if (hdispar) fpixWriteStream(fp, dew->
samphdispar);
1347 L_WARNING(
"no disparity arrays!\n", __func__);
1373 if (pdata) *pdata = NULL;
1374 if (psize) *psize = 0;
1376 return ERROR_INT(
"&data not defined", __func__, 1);
1378 return ERROR_INT(
"&size not defined", __func__, 1);
1380 return ERROR_INT(
"dew not defined", __func__, 1);
1383 if ((fp = open_memstream((
char **)pdata, psize)) == NULL)
1384 return ERROR_INT(
"stream not opened", __func__, 1);
1388 if (*psize > 0) *psize = *psize - 1;
1390 L_INFO(
"no fmemopen API --> work-around: write to temp file\n", __func__);
1392 if ((fp = fopenWriteWinTempfile()) == NULL)
1393 return ERROR_INT(
"tmpfile stream not opened", __func__, 1);
1395 if ((fp = tmpfile()) == NULL)
1396 return ERROR_INT(
"tmpfile stream not opened", __func__, 1);
1400 *pdata = l_binaryReadStream(fp, psize);
1423 return (
L_DEWARPA *)ERROR_PTR(
"filename not defined", __func__, NULL);
1424 if ((fp = fopenReadStream(filename)) == NULL)
1425 return (
L_DEWARPA *)ERROR_PTR_1(
"stream not opened",
1426 filename, __func__, NULL);
1430 return (
L_DEWARPA *)ERROR_PTR_1(
"dewa not read",
1431 filename, __func__, NULL);
1456l_int32 i, version, ndewarp, maxpage;
1457l_int32 sampling, redfactor, minlines, maxdist, useboth;
1458l_int32 max_linecurv, min_diff_linecurv, max_diff_linecurv;
1459l_int32 max_edgeslope, max_edgecurv, max_diff_edgecurv;
1465 return (
L_DEWARPA *)ERROR_PTR(
"stream not defined", __func__, NULL);
1467 if (fscanf(fp,
"\nDewarpa Version %d\n", &version) != 1)
1468 return (
L_DEWARPA *)ERROR_PTR(
"not a dewarpa file", __func__, NULL);
1470 return (
L_DEWARPA *)ERROR_PTR(
"invalid dewarp version", __func__, NULL);
1472 if (fscanf(fp,
"ndewarp = %d, maxpage = %d\n", &ndewarp, &maxpage) != 2)
1473 return (
L_DEWARPA *)ERROR_PTR(
"read fail for maxpage+", __func__, NULL);
1475 return (
L_DEWARPA *)ERROR_PTR(
"pages not >= 1", __func__, NULL);
1476 if (ndewarp > MaxPtrArraySize)
1477 return (
L_DEWARPA *)ERROR_PTR(
"too many pages", __func__, NULL);
1479 "sampling = %d, redfactor = %d, minlines = %d, maxdist = %d\n",
1480 &sampling, &redfactor, &minlines, &maxdist) != 4)
1481 return (
L_DEWARPA *)ERROR_PTR(
"read fail for 4 params", __func__, NULL);
1483 "max_linecurv = %d, min_diff_linecurv = %d, max_diff_linecurv = %d\n",
1484 &max_linecurv, &min_diff_linecurv, &max_diff_linecurv) != 3)
1485 return (
L_DEWARPA *)ERROR_PTR(
"read fail for linecurv", __func__, NULL);
1487 "max_edgeslope = %d, max_edgecurv = %d, max_diff_edgecurv = %d\n",
1488 &max_edgeslope, &max_edgecurv, &max_diff_edgecurv) != 3)
1489 return (
L_DEWARPA *)ERROR_PTR(
"read fail for edgecurv", __func__, NULL);
1490 if (fscanf(fp,
"fullmodel = %d\n", &useboth) != 1)
1491 return (
L_DEWARPA *)ERROR_PTR(
"read fail for useboth", __func__, NULL);
1493 dewa =
dewarpaCreate(maxpage + 1, sampling, redfactor, minlines, maxdist);
1502 namodels = numaCreate(ndewarp);
1504 for (i = 0; i < ndewarp; i++) {
1506 L_ERROR(
"read fail for dew[%d]\n", __func__, i);
1511 numaAddNumber(namodels, dew->
pageno);
1515 dewarpaInsertRefModels(dewa, 0, 0);
1535 return (
L_DEWARPA *)ERROR_PTR(
"data not defined", __func__, NULL);
1536 if ((fp = fopenReadFromMemory(data, size)) == NULL)
1537 return (
L_DEWARPA *)ERROR_PTR(
"stream not opened", __func__, NULL);
1541 if (!dewa) L_ERROR(
"dewa not read\n", __func__);
1561 return ERROR_INT(
"filename not defined", __func__, 1);
1563 return ERROR_INT(
"dewa not defined", __func__, 1);
1565 if ((fp = fopenWriteStream(filename,
"wb")) == NULL)
1566 return ERROR_INT_1(
"stream not opened", filename, __func__, 1);
1570 return ERROR_INT_1(
"dewa not written to stream", filename, __func__, 1);
1586l_int32 ndewarp, i, pageno;
1589 return ERROR_INT(
"stream not defined", __func__, 1);
1591 return ERROR_INT(
"dewa not defined", __func__, 1);
1598 dewarpaListPages(dewa);
1600 return ERROR_INT(
"dewa->namodels not made", __func__, 1);
1601 ndewarp = numaGetCount(dewa->
namodels);
1604 fprintf(fp,
"ndewarp = %d, maxpage = %d\n", ndewarp, dewa->
maxpage);
1605 fprintf(fp,
"sampling = %d, redfactor = %d, minlines = %d, maxdist = %d\n",
1608 "max_linecurv = %d, min_diff_linecurv = %d, max_diff_linecurv = %d\n",
1611 "max_edgeslope = %d, max_edgecurv = %d, max_diff_edgecurv = %d\n",
1613 fprintf(fp,
"fullmodel = %d\n", dewa->
useboth);
1614 for (i = 0; i < ndewarp; i++) {
1615 numaGetIValue(dewa->
namodels, i, &pageno);
1644 if (pdata) *pdata = NULL;
1645 if (psize) *psize = 0;
1647 return ERROR_INT(
"&data not defined", __func__, 1);
1649 return ERROR_INT(
"&size not defined", __func__, 1);
1651 return ERROR_INT(
"dewa not defined", __func__, 1);
1654 if ((fp = open_memstream((
char **)pdata, psize)) == NULL)
1655 return ERROR_INT(
"stream not opened", __func__, 1);
1659 if (*psize > 0) *psize = *psize - 1;
1661 L_INFO(
"no fmemopen API --> work-around: write to temp file\n", __func__);
1663 if ((fp = fopenWriteWinTempfile()) == NULL)
1664 return ERROR_INT(
"tmpfile stream not opened", __func__, 1);
1666 if ((fp = tmpfile()) == NULL)
1667 return ERROR_INT(
"tmpfile stream not opened", __func__, 1);
1671 *pdata = l_binaryReadStream(fp, psize);
L_DEWARP * dewarpaGetDewarp(L_DEWARPA *dewa, l_int32 index)
dewarpaGetDewarp()
l_ok dewarpaUseBothArrays(L_DEWARPA *dewa, l_int32 useboth)
dewarpaUseBothArrays()
L_DEWARP * dewarpRead(const char *filename)
dewarpRead()
l_ok dewarpaSetMaxDistance(L_DEWARPA *dewa, l_int32 maxdist)
dewarpaSetMaxDistance()
L_DEWARP * dewarpReadStream(FILE *fp)
dewarpReadStream()
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_DEWARPA * dewarpaReadMem(const l_uint8 *data, size_t size)
dewarpaReadMem()
l_ok dewarpaInsertDewarp(L_DEWARPA *dewa, L_DEWARP *dew)
dewarpaInsertDewarp()
L_DEWARP * dewarpCreate(PIX *pixs, l_int32 pageno)
dewarpCreate()
L_DEWARPA * dewarpaCreateFromPixacomp(PIXAC *pixac, l_int32 useboth, l_int32 sampling, l_int32 minlines, l_int32 maxdist)
dewarpaCreateFromPixacomp()
void dewarpDestroy(L_DEWARP **pdew)
dewarpDestroy()
l_ok dewarpWrite(const char *filename, L_DEWARP *dew)
dewarpWrite()
l_ok dewarpaSetCheckColumns(L_DEWARPA *dewa, l_int32 check_columns)
dewarpaSetCheckColumns()
l_ok dewarpaWriteStream(FILE *fp, L_DEWARPA *dewa)
dewarpaWriteStream()
l_ok dewarpWriteMem(l_uint8 **pdata, size_t *psize, L_DEWARP *dew)
dewarpWriteMem()
L_DEWARPA * dewarpaRead(const char *filename)
dewarpaRead()
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_DEWARPA * dewarpaCreate(l_int32 nptrs, l_int32 sampling, l_int32 redfactor, l_int32 minlines, l_int32 maxdist)
dewarpaCreate()
L_DEWARPA * dewarpaReadStream(FILE *fp)
dewarpaReadStream()
#define DEWARP_VERSION_NUMBER
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