94#include <config_auto.h>
98#include "allheaders.h"
102static const l_uint32 MaxArraySize = 100000000;
103static const l_uint32 MaxPtrArraySize = 10000000;
124 if (n <= 0 || n > (l_int32)MaxArraySize)
127 pta = (
PTA *)LEPT_CALLOC(1,
sizeof(
PTA));
131 pta->x = (l_float32 *)LEPT_CALLOC(n,
sizeof(l_float32));
132 pta->
y = (l_float32 *)LEPT_CALLOC(n,
sizeof(l_float32));
133 if (!pta->x || !pta->
y) {
135 return (
PTA *)ERROR_PTR(
"x and y arrays not both made", __func__, NULL);
154l_float32 startx, delx, xval, yval;
158 return (
PTA *)ERROR_PTR(
"nay not defined", __func__, NULL);
159 n = numaGetCount(nay);
160 if (nax && numaGetCount(nax) != n)
161 return (
PTA *)ERROR_PTR(
"nax and nay sizes differ", __func__, NULL);
164 numaGetParameters(nay, &startx, &delx);
165 for (i = 0; i < n; i++) {
167 numaGetFValue(nax, i, &xval);
169 xval = startx + i * delx;
170 numaGetFValue(nay, i, &yval);
196 L_WARNING(
"ptr address is NULL!\n", __func__);
200 if ((pta = *ppta) == NULL)
226 return (
PTA *)ERROR_PTR(
"pta not defined", __func__, NULL);
229 return (
PTA *)ERROR_PTR(
"npta not made", __func__, NULL);
231 for (i = 0; i < pta->
n; i++) {
257 return (
PTA *)ERROR_PTR(
"ptas not defined", __func__, NULL);
262 return (
PTA *)ERROR_PTR(
"istart out of bounds", __func__, NULL);
263 if (iend <= 0 || iend >= n)
266 return (
PTA *)ERROR_PTR(
"istart > iend; no pts", __func__, NULL);
268 if ((ptad =
ptaCreate(iend - istart + 1)) == NULL)
269 return (
PTA *)ERROR_PTR(
"ptad not made", __func__, NULL);
270 for (i = istart; i <= iend; i++) {
289 return (
PTA *)ERROR_PTR(
"pta not defined", __func__, NULL);
311 return ERROR_INT(
"ptad not defined", __func__, 1);
335 return ERROR_INT(
"pta not defined", __func__, 1);
340 return ERROR_INT(
"extension failed", __func__, 1);
365size_t oldsize, newsize;
368 return ERROR_INT(
"pta not defined", __func__, 1);
369 if (pta->
nalloc > (l_int32)MaxArraySize)
370 return ERROR_INT(
"pta at maximum size; can't extend", __func__, 1);
371 oldsize = 4 * pta->
nalloc;
372 if (pta->
nalloc > MaxArraySize / 2) {
373 newsize = 4 * MaxArraySize;
374 pta->
nalloc = MaxArraySize;
376 newsize = 2 * oldsize;
379 if ((pta->x = (l_float32 *)reallocNew((
void **)&pta->x,
380 oldsize, newsize)) == NULL)
381 return ERROR_INT(
"new x array not returned", __func__, 1);
382 if ((pta->
y = (l_float32 *)reallocNew((
void **)&pta->
y,
383 oldsize, newsize)) == NULL)
384 return ERROR_INT(
"new y array not returned", __func__, 1);
410 return ERROR_INT(
"pta not defined", __func__, 1);
412 if (index < 0 || index > n) {
413 L_ERROR(
"index %d not in [0,...,%d]\n", __func__, index, n);
419 return ERROR_INT(
"extension failed", __func__, 1);
422 for (i = n; i > index; i--) {
423 pta->x[i] = pta->x[i - 1];
424 pta->
y[i] = pta->
y[i - 1];
453 return ERROR_INT(
"pta not defined", __func__, 1);
455 if (index < 0 || index >= n) {
456 L_ERROR(
"index %d not in [0,...,%d]\n", __func__, index, n - 1);
461 for (i = index + 1; i < n; i++) {
462 pta->x[i - 1] = pta->x[i];
463 pta->
y[i - 1] = pta->
y[i];
483 return ERROR_INT(
"pta not defined", __func__, 0);
507 return ERROR_INT(
"pta not defined", __func__, 1);
508 if (index < 0 || index >= pta->
n)
509 return ERROR_INT(
"invalid index", __func__, 1);
511 if (px) *px = pta->x[index];
512 if (py) *py = pta->
y[index];
535 return ERROR_INT(
"pta not defined", __func__, 1);
536 if (index < 0 || index >= pta->
n)
537 return ERROR_INT(
"invalid index", __func__, 1);
539 if (px) *px = (l_int32)(pta->x[index] + 0.5);
540 if (py) *py = (l_int32)(pta->
y[index] + 0.5);
560 return ERROR_INT(
"pta not defined", __func__, 1);
561 if (index < 0 || index >= pta->
n)
562 return ERROR_INT(
"invalid index", __func__, 1);
592 return ERROR_INT(
"no output requested", __func__, 1);
593 if (pnax) *pnax = NULL;
594 if (pnay) *pnay = NULL;
596 return ERROR_INT(
"pta not defined", __func__, 1);
598 return ERROR_INT(
"pta is empty", __func__, 1);
601 if ((nax = numaCreate(n)) == NULL)
602 return ERROR_INT(
"nax not made", __func__, 1);
604 for (i = 0; i < n; i++)
605 nax->
array[i] = pta->x[i];
609 if ((nay = numaCreate(n)) == NULL)
610 return ERROR_INT(
"nay not made", __func__, 1);
612 for (i = 0; i < n; i++)
613 nay->
array[i] = pta->
y[i];
636 return (
PTA *)ERROR_PTR(
"filename not defined", __func__, NULL);
638 if ((fp = fopenReadStream(filename)) == NULL)
639 return (
PTA *)ERROR_PTR_1(
"stream not opened",
640 filename, __func__, NULL);
644 return (
PTA *)ERROR_PTR_1(
"pta not read", filename, __func__, NULL);
665l_int32 i, n, ix, iy, type, version;
670 return (
PTA *)ERROR_PTR(
"stream not defined", __func__, NULL);
672 if (fscanf(fp,
"\n Pta Version %d\n", &version) != 1)
673 return (
PTA *)ERROR_PTR(
"not a pta file", __func__, NULL);
675 return (
PTA *)ERROR_PTR(
"invalid pta version", __func__, NULL);
676 if (fscanf(fp,
" Number of pts = %d; format = %127s\n", &n, typestr) != 2)
677 return (
PTA *)ERROR_PTR(
"not a pta file", __func__, NULL);
679 return (
PTA *)ERROR_PTR(
"num pts <= 0", __func__, NULL);
680 if (n > (l_int32)MaxArraySize)
681 return (
PTA *)ERROR_PTR(
"too many pts", __func__, NULL);
682 if (n == 0) L_INFO(
"the pta is empty\n", __func__);
684 if (!strcmp(typestr,
"float"))
689 return (
PTA *)ERROR_PTR(
"pta not made", __func__, NULL);
690 for (i = 0; i < n; i++) {
692 if (fscanf(fp,
" (%f, %f)\n", &x, &y) != 2) {
694 return (
PTA *)ERROR_PTR(
"error reading floats", __func__, NULL);
698 if (fscanf(fp,
" (%d, %d)\n", &ix, &iy) != 2) {
700 return (
PTA *)ERROR_PTR(
"error reading ints", __func__, NULL);
725 return (
PTA *)ERROR_PTR(
"data not defined", __func__, NULL);
726 if ((fp = fopenReadFromMemory(data, size)) == NULL)
727 return (
PTA *)ERROR_PTR(
"stream not opened", __func__, NULL);
731 if (!pta) L_ERROR(
"pta not read\n", __func__);
759 return ptaWrite(filename, pta, type);
761 L_INFO(
"write to named temp file %s is disabled\n", __func__, filename);
784 return ERROR_INT(
"filename not defined", __func__, 1);
786 return ERROR_INT(
"pta not defined", __func__, 1);
788 if ((fp = fopenWriteStream(filename,
"w")) == NULL)
789 return ERROR_INT_1(
"stream not opened", filename, __func__, 1);
793 return ERROR_INT_1(
"pta not written to stream", filename, __func__, 1);
815 return ERROR_INT(
"stream not defined", __func__, 1);
817 return ERROR_INT(
"pta not defined", __func__, 1);
822 fprintf(fp,
" Number of pts = %d; format = float\n", n);
824 fprintf(fp,
" Number of pts = %d; format = integer\n", n);
825 for (i = 0; i < n; i++) {
828 fprintf(fp,
" (%f, %f)\n", x, y);
831 fprintf(fp,
" (%d, %d)\n", ix, iy);
862 if (pdata) *pdata = NULL;
863 if (psize) *psize = 0;
865 return ERROR_INT(
"&data not defined", __func__, 1);
867 return ERROR_INT(
"&size not defined", __func__, 1);
869 return ERROR_INT(
"pta not defined", __func__, 1);
872 if ((fp = open_memstream((
char **)pdata, psize)) == NULL)
873 return ERROR_INT(
"stream not opened", __func__, 1);
877 if (*psize > 0) *psize = *psize - 1;
879 L_INFO(
"no fmemopen API --> work-around: write to temp file\n", __func__);
881 if ((fp = fopenWriteWinTempfile()) == NULL)
882 return ERROR_INT(
"tmpfile stream not opened", __func__, 1);
884 if ((fp = tmpfile()) == NULL)
885 return ERROR_INT(
"tmpfile stream not opened", __func__, 1);
889 *pdata = l_binaryReadStream(fp, psize);
910 if (n <= 0 || n > (l_int32)MaxPtrArraySize)
913 ptaa = (
PTAA *)LEPT_CALLOC(1,
sizeof(
PTAA));
916 if ((ptaa->
pta = (
PTA **)LEPT_CALLOC(n,
sizeof(
PTA *))) == NULL) {
918 return (
PTAA *)ERROR_PTR(
"pta ptrs not made", __func__, NULL);
937 L_WARNING(
"ptr address is NULL!\n", __func__);
941 if ((ptaa = *pptaa) == NULL)
944 for (i = 0; i < ptaa->
n; i++)
946 LEPT_FREE(ptaa->
pta);
972 return ERROR_INT(
"ptaa not defined", __func__, 1);
974 return ERROR_INT(
"pta not defined", __func__, 1);
978 }
else if (copyflag ==
L_COPY) {
979 if ((ptac =
ptaCopy(pta)) == NULL)
980 return ERROR_INT(
"ptac not made", __func__, 1);
981 }
else if (copyflag ==
L_CLONE) {
983 return ERROR_INT(
"pta clone not made", __func__, 1);
985 return ERROR_INT(
"invalid copyflag", __func__, 1);
993 return ERROR_INT(
"extension failed", __func__, 1);
1019size_t oldsize, newsize;
1022 return ERROR_INT(
"ptaa not defined", __func__, 1);
1024 newsize = 2 * oldsize;
1025 if (newsize > 8 * MaxPtrArraySize)
1026 return ERROR_INT(
"newsize > 80 MB; too large", __func__, 1);
1028 if ((ptaa->
pta = (
PTA **)reallocNew((
void **)&ptaa->
pta,
1029 oldsize, newsize)) == NULL)
1030 return ERROR_INT(
"new ptr array not returned", __func__, 1);
1050 return ERROR_INT(
"ptaa not defined", __func__, 0);
1070 return (
PTA *)ERROR_PTR(
"ptaa not defined", __func__, NULL);
1071 if (index < 0 || index >= ptaa->
n)
1072 return (
PTA *)ERROR_PTR(
"index not valid", __func__, NULL);
1074 if (accessflag ==
L_COPY)
1076 else if (accessflag ==
L_CLONE)
1079 return (
PTA *)ERROR_PTR(
"invalid accessflag", __func__, NULL);
1105 return ERROR_INT(
"ptaa not defined", __func__, 1);
1106 if (ipta < 0 || ipta >= ptaa->
n)
1107 return ERROR_INT(
"index ipta not valid", __func__, 1);
1110 if (jpt < 0 || jpt >= pta->
n) {
1112 return ERROR_INT(
"index jpt not valid", __func__, 1);
1139 return ERROR_INT(
"ptaa not defined", __func__, 1);
1141 return ERROR_INT(
"pta not defined", __func__, 1);
1145 for (i = 0; i < n; i++) {
1176 return ERROR_INT(
"ptaa not defined", __func__, 1);
1178 return ERROR_INT(
"pta not defined", __func__, 1);
1180 if (index < 0 || index >= n)
1181 return ERROR_INT(
"index not valid", __func__, 1);
1184 ptaa->
pta[index] = pta;
1206 return ERROR_INT(
"ptaa not defined", __func__, 1);
1207 if (ipta < 0 || ipta >= ptaa->
n)
1208 return ERROR_INT(
"index ipta not valid", __func__, 1);
1237 return ERROR_INT(
"ptaa not defined", __func__, 1);
1240 for (i = n - 1; i >= 0; i--) {
1275 return (
PTAA *)ERROR_PTR(
"filename not defined", __func__, NULL);
1277 if ((fp = fopenReadStream(filename)) == NULL)
1278 return (
PTAA *)ERROR_PTR_1(
"stream not opened",
1279 filename, __func__, NULL);
1283 return (
PTAA *)ERROR_PTR_1(
"ptaa not read", filename, __func__, NULL);
1302l_int32 i, n, version;
1307 return (
PTAA *)ERROR_PTR(
"stream not defined", __func__, NULL);
1309 if (fscanf(fp,
"\nPtaa Version %d\n", &version) != 1)
1310 return (
PTAA *)ERROR_PTR(
"not a ptaa file", __func__, NULL);
1312 return (
PTAA *)ERROR_PTR(
"invalid ptaa version", __func__, NULL);
1313 if (fscanf(fp,
"Number of Pta = %d\n", &n) != 1)
1314 return (
PTAA *)ERROR_PTR(
"not a ptaa file", __func__, NULL);
1316 return (
PTAA *)ERROR_PTR(
"num pta ptrs <= 0", __func__, NULL);
1317 if (n > (l_int32)MaxPtrArraySize)
1318 return (
PTAA *)ERROR_PTR(
"too many pta ptrs", __func__, NULL);
1319 if (n == 0) L_INFO(
"the ptaa is empty\n", __func__);
1322 return (
PTAA *)ERROR_PTR(
"ptaa not made", __func__, NULL);
1323 for (i = 0; i < n; i++) {
1326 return (
PTAA *)ERROR_PTR(
"error reading pta", __func__, NULL);
1350 return (
PTAA *)ERROR_PTR(
"data not defined", __func__, NULL);
1351 if ((fp = fopenReadFromMemory(data, size)) == NULL)
1352 return (
PTAA *)ERROR_PTR(
"stream not opened", __func__, NULL);
1356 if (!ptaa) L_ERROR(
"ptaa not read\n", __func__);
1386 L_INFO(
"write to named temp file %s is disabled\n", __func__, filename);
1409 return ERROR_INT(
"filename not defined", __func__, 1);
1411 return ERROR_INT(
"ptaa not defined", __func__, 1);
1413 if ((fp = fopenWriteStream(filename,
"w")) == NULL)
1414 return ERROR_INT_1(
"stream not opened", filename, __func__, 1);
1418 return ERROR_INT_1(
"ptaa not written to stream", filename, __func__, 1);
1440 return ERROR_INT(
"stream not defined", __func__, 1);
1442 return ERROR_INT(
"ptaa not defined", __func__, 1);
1446 fprintf(fp,
"Number of Pta = %d\n", n);
1447 for (i = 0; i < n; i++) {
1480 if (pdata) *pdata = NULL;
1481 if (psize) *psize = 0;
1483 return ERROR_INT(
"&data not defined", __func__, 1);
1485 return ERROR_INT(
"&size not defined", __func__, 1);
1487 return ERROR_INT(
"ptaa not defined", __func__, 1);
1490 if ((fp = open_memstream((
char **)pdata, psize)) == NULL)
1491 return ERROR_INT(
"stream not opened", __func__, 1);
1495 if (*psize > 0) *psize = *psize - 1;
1497 L_INFO(
"no fmemopen API --> work-around: write to temp file\n", __func__);
1499 if ((fp = fopenWriteWinTempfile()) == NULL)
1500 return ERROR_INT(
"tmpfile stream not opened", __func__, 1);
1502 if ((fp = tmpfile()) == NULL)
1503 return ERROR_INT(
"tmpfile stream not opened", __func__, 1);
1507 *pdata = l_binaryReadStream(fp, psize);
#define PTA_VERSION_NUMBER
l_ok ptaWriteStream(FILE *fp, PTA *pta, l_int32 type)
ptaWriteStream()
static l_int32 ptaaExtendArray(PTAA *ptaa)
ptaaExtendArray()
PTA * ptaCreateFromNuma(NUMA *nax, NUMA *nay)
ptaCreateFromNuma()
l_ok ptaWriteMem(l_uint8 **pdata, size_t *psize, PTA *pta, l_int32 type)
ptaWriteMem()
PTA * ptaRead(const char *filename)
ptaRead()
l_ok ptaaAddPt(PTAA *ptaa, l_int32 ipta, l_float32 x, l_float32 y)
ptaaAddPt()
l_ok ptaSetPt(PTA *pta, l_int32 index, l_float32 x, l_float32 y)
ptaSetPt()
PTA * ptaCopy(PTA *pta)
ptaCopy()
l_ok ptaEmpty(PTA *pta)
ptaEmpty()
l_ok ptaGetIPt(PTA *pta, l_int32 index, l_int32 *px, l_int32 *py)
ptaGetIPt()
l_ok ptaRemovePt(PTA *pta, l_int32 index)
ptaRemovePt()
PTA * ptaCreate(l_int32 n)
ptaCreate()
PTA * ptaClone(PTA *pta)
ptaClone()
l_ok ptaWriteDebug(const char *filename, PTA *pta, l_int32 type)
ptaWriteDebug()
l_ok ptaaWriteMem(l_uint8 **pdata, size_t *psize, PTAA *ptaa, l_int32 type)
ptaaWriteMem()
PTA * ptaReadStream(FILE *fp)
ptaReadStream()
l_ok ptaaAddPta(PTAA *ptaa, PTA *pta, l_int32 copyflag)
ptaaAddPta()
l_int32 ptaaGetCount(PTAA *ptaa)
ptaaGetCount()
l_ok ptaaWrite(const char *filename, PTAA *ptaa, l_int32 type)
ptaaWrite()
PTA * ptaReadMem(const l_uint8 *data, size_t size)
ptaReadMem()
l_ok ptaaInitFull(PTAA *ptaa, PTA *pta)
ptaaInitFull()
PTA * ptaaGetPta(PTAA *ptaa, l_int32 index, l_int32 accessflag)
ptaaGetPta()
PTAA * ptaaReadMem(const l_uint8 *data, size_t size)
ptaaReadMem()
PTA * ptaCopyRange(PTA *ptas, l_int32 istart, l_int32 iend)
ptaCopyRange()
l_ok ptaaGetPt(PTAA *ptaa, l_int32 ipta, l_int32 jpt, l_float32 *px, l_float32 *py)
ptaaGetPt()
l_ok ptaaWriteStream(FILE *fp, PTAA *ptaa, l_int32 type)
ptaaWriteStream()
l_ok ptaAddPt(PTA *pta, l_float32 x, l_float32 y)
ptaAddPt()
l_ok ptaaWriteDebug(const char *filename, PTAA *ptaa, l_int32 type)
ptaaWriteDebug()
l_ok ptaGetArrays(PTA *pta, NUMA **pnax, NUMA **pnay)
ptaGetArrays()
l_ok ptaGetPt(PTA *pta, l_int32 index, l_float32 *px, l_float32 *py)
ptaGetPt()
l_ok ptaInsertPt(PTA *pta, l_int32 index, l_int32 x, l_int32 y)
ptaInsertPt()
l_ok ptaWrite(const char *filename, PTA *pta, l_int32 type)
ptaWrite()
static const l_int32 InitialArraySize
l_int32 ptaGetCount(PTA *pta)
ptaGetCount()
PTAA * ptaaRead(const char *filename)
ptaaRead()
PTAA * ptaaCreate(l_int32 n)
ptaaCreate()
void ptaaDestroy(PTAA **pptaa)
ptaaDestroy()
PTAA * ptaaReadStream(FILE *fp)
ptaaReadStream()
void ptaDestroy(PTA **ppta)
ptaDestroy()
l_ok ptaaReplacePta(PTAA *ptaa, l_int32 index, PTA *pta)
ptaaReplacePta()
static l_int32 ptaExtendArrays(PTA *pta)
ptaExtendArrays()
l_ok ptaaTruncate(PTAA *ptaa)
ptaaTruncate()