94 #include <config_auto.h>
98 #include "allheaders.h"
102 static const l_uint32 MaxArraySize = 100000000;
103 static const l_uint32 MaxPtrArraySize = 10000000;
124 if (n <= 0 || n > 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);
154 l_float32 startx, delx, xval, yval;
158 return (
PTA *)ERROR_PTR(
"nay not defined", __func__, NULL);
161 return (
PTA *)ERROR_PTR(
"nax and nay sizes differ", __func__, NULL);
165 for (i = 0; i < n; i++) {
169 xval = startx + i * delx;
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);
365 size_t oldsize, newsize;
368 return ERROR_INT(
"pta not defined", __func__, 1);
369 if (pta->
nalloc > 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);
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);
602 return ERROR_INT(
"nax not made", __func__, 1);
604 for (i = 0; i < n; i++)
605 nax->
array[i] = pta->x[i];
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);
639 return (
PTA *)ERROR_PTR(
"stream not opened", __func__, NULL);
643 return (
PTA *)ERROR_PTR(
"pta not read", __func__, NULL);
664 l_int32 i, n, ix, iy, type, version;
669 return (
PTA *)ERROR_PTR(
"stream not defined", __func__, NULL);
671 if (fscanf(fp,
"\n Pta Version %d\n", &version) != 1)
672 return (
PTA *)ERROR_PTR(
"not a pta file", __func__, NULL);
674 return (
PTA *)ERROR_PTR(
"invalid pta version", __func__, NULL);
675 if (fscanf(fp,
" Number of pts = %d; format = %127s\n", &n, typestr) != 2)
676 return (
PTA *)ERROR_PTR(
"not a pta file", __func__, NULL);
678 return (
PTA *)ERROR_PTR(
"num pts <= 0", __func__, NULL);
679 if (n > MaxArraySize)
680 return (
PTA *)ERROR_PTR(
"too many pts", __func__, NULL);
681 if (n == 0) L_INFO(
"the pta is empty\n", __func__);
683 if (!strcmp(typestr,
"float"))
688 return (
PTA *)ERROR_PTR(
"pta not made", __func__, NULL);
689 for (i = 0; i < n; i++) {
691 if (fscanf(fp,
" (%f, %f)\n", &x, &y) != 2) {
693 return (
PTA *)ERROR_PTR(
"error reading floats", __func__, NULL);
697 if (fscanf(fp,
" (%d, %d)\n", &ix, &iy) != 2) {
699 return (
PTA *)ERROR_PTR(
"error reading ints", __func__, NULL);
724 return (
PTA *)ERROR_PTR(
"data not defined", __func__, NULL);
726 return (
PTA *)ERROR_PTR(
"stream not opened", __func__, NULL);
730 if (!pta) L_ERROR(
"pta not read\n", __func__);
758 return ptaWrite(filename, pta, type);
760 L_INFO(
"write to named temp file %s is disabled\n", __func__, filename);
783 return ERROR_INT(
"filename not defined", __func__, 1);
785 return ERROR_INT(
"pta not defined", __func__, 1);
788 return ERROR_INT(
"stream not opened", __func__, 1);
792 return ERROR_INT(
"pta not written to stream", __func__, 1);
810 l_int32 i, n, ix, iy;
814 return ERROR_INT(
"stream not defined", __func__, 1);
816 return ERROR_INT(
"pta not defined", __func__, 1);
821 fprintf(fp,
" Number of pts = %d; format = float\n", n);
823 fprintf(fp,
" Number of pts = %d; format = integer\n", n);
824 for (i = 0; i < n; i++) {
827 fprintf(fp,
" (%f, %f)\n", x, y);
830 fprintf(fp,
" (%d, %d)\n", ix, iy);
861 if (pdata) *pdata = NULL;
862 if (psize) *psize = 0;
864 return ERROR_INT(
"&data not defined", __func__, 1);
866 return ERROR_INT(
"&size not defined", __func__, 1);
868 return ERROR_INT(
"pta not defined", __func__, 1);
871 if ((fp = open_memstream((
char **)pdata, psize)) == NULL)
872 return ERROR_INT(
"stream not opened", __func__, 1);
878 L_INFO(
"work-around: writing to a temp file\n", __func__);
881 return ERROR_INT(
"tmpfile stream not opened", __func__, 1);
883 if ((fp = tmpfile()) == NULL)
884 return ERROR_INT(
"tmpfile stream not opened", __func__, 1);
909 if (n <= 0 || n > MaxPtrArraySize)
912 ptaa = (
PTAA *)LEPT_CALLOC(1,
sizeof(
PTAA));
915 if ((ptaa->
pta = (
PTA **)LEPT_CALLOC(n,
sizeof(
PTA *))) == NULL) {
917 return (
PTAA *)ERROR_PTR(
"pta ptrs not made", __func__, NULL);
936 L_WARNING(
"ptr address is NULL!\n", __func__);
940 if ((ptaa = *pptaa) == NULL)
943 for (i = 0; i < ptaa->
n; i++)
945 LEPT_FREE(ptaa->
pta);
971 return ERROR_INT(
"ptaa not defined", __func__, 1);
973 return ERROR_INT(
"pta not defined", __func__, 1);
977 }
else if (copyflag ==
L_COPY) {
978 if ((ptac =
ptaCopy(pta)) == NULL)
979 return ERROR_INT(
"ptac not made", __func__, 1);
980 }
else if (copyflag ==
L_CLONE) {
982 return ERROR_INT(
"pta clone not made", __func__, 1);
984 return ERROR_INT(
"invalid copyflag", __func__, 1);
992 return ERROR_INT(
"extension failed", __func__, 1);
1018 size_t oldsize, newsize;
1021 return ERROR_INT(
"ptaa not defined", __func__, 1);
1023 newsize = 2 * oldsize;
1024 if (newsize > 8 * MaxPtrArraySize)
1025 return ERROR_INT(
"newsize > 80 MB; too large", __func__, 1);
1028 oldsize, newsize)) == NULL)
1029 return ERROR_INT(
"new ptr array not returned", __func__, 1);
1049 return ERROR_INT(
"ptaa not defined", __func__, 0);
1069 return (
PTA *)ERROR_PTR(
"ptaa not defined", __func__, NULL);
1070 if (index < 0 || index >= ptaa->
n)
1071 return (
PTA *)ERROR_PTR(
"index not valid", __func__, NULL);
1073 if (accessflag ==
L_COPY)
1075 else if (accessflag ==
L_CLONE)
1078 return (
PTA *)ERROR_PTR(
"invalid accessflag", __func__, NULL);
1104 return ERROR_INT(
"ptaa not defined", __func__, 1);
1105 if (ipta < 0 || ipta >= ptaa->
n)
1106 return ERROR_INT(
"index ipta not valid", __func__, 1);
1109 if (jpt < 0 || jpt >= pta->
n) {
1111 return ERROR_INT(
"index jpt not valid", __func__, 1);
1138 return ERROR_INT(
"ptaa not defined", __func__, 1);
1140 return ERROR_INT(
"pta not defined", __func__, 1);
1144 for (i = 0; i < n; i++) {
1175 return ERROR_INT(
"ptaa not defined", __func__, 1);
1177 return ERROR_INT(
"pta not defined", __func__, 1);
1179 if (index < 0 || index >= n)
1180 return ERROR_INT(
"index not valid", __func__, 1);
1183 ptaa->
pta[index] = pta;
1205 return ERROR_INT(
"ptaa not defined", __func__, 1);
1206 if (ipta < 0 || ipta >= ptaa->
n)
1207 return ERROR_INT(
"index ipta not valid", __func__, 1);
1236 return ERROR_INT(
"ptaa not defined", __func__, 1);
1239 for (i = n - 1; i >= 0; i--) {
1274 return (
PTAA *)ERROR_PTR(
"filename not defined", __func__, NULL);
1277 return (
PTAA *)ERROR_PTR(
"stream not opened", __func__, NULL);
1281 return (
PTAA *)ERROR_PTR(
"ptaa not read", __func__, NULL);
1300 l_int32 i, n, version;
1305 return (
PTAA *)ERROR_PTR(
"stream not defined", __func__, NULL);
1307 if (fscanf(fp,
"\nPtaa Version %d\n", &version) != 1)
1308 return (
PTAA *)ERROR_PTR(
"not a ptaa file", __func__, NULL);
1310 return (
PTAA *)ERROR_PTR(
"invalid ptaa version", __func__, NULL);
1311 if (fscanf(fp,
"Number of Pta = %d\n", &n) != 1)
1312 return (
PTAA *)ERROR_PTR(
"not a ptaa file", __func__, NULL);
1314 return (
PTAA *)ERROR_PTR(
"num pta ptrs <= 0", __func__, NULL);
1315 if (n > MaxPtrArraySize)
1316 return (
PTAA *)ERROR_PTR(
"too many pta ptrs", __func__, NULL);
1317 if (n == 0) L_INFO(
"the ptaa is empty\n", __func__);
1320 return (
PTAA *)ERROR_PTR(
"ptaa not made", __func__, NULL);
1321 for (i = 0; i < n; i++) {
1324 return (
PTAA *)ERROR_PTR(
"error reading pta", __func__, NULL);
1348 return (
PTAA *)ERROR_PTR(
"data not defined", __func__, NULL);
1350 return (
PTAA *)ERROR_PTR(
"stream not opened", __func__, NULL);
1354 if (!ptaa) L_ERROR(
"ptaa not read\n", __func__);
1384 L_INFO(
"write to named temp file %s is disabled\n", __func__, filename);
1407 return ERROR_INT(
"filename not defined", __func__, 1);
1409 return ERROR_INT(
"ptaa not defined", __func__, 1);
1412 return ERROR_INT(
"stream not opened", __func__, 1);
1416 return ERROR_INT(
"ptaa not written to stream", __func__, 1);
1438 return ERROR_INT(
"stream not defined", __func__, 1);
1440 return ERROR_INT(
"ptaa not defined", __func__, 1);
1444 fprintf(fp,
"Number of Pta = %d\n", n);
1445 for (i = 0; i < n; i++) {
1478 if (pdata) *pdata = NULL;
1479 if (psize) *psize = 0;
1481 return ERROR_INT(
"&data not defined", __func__, 1);
1483 return ERROR_INT(
"&size not defined", __func__, 1);
1485 return ERROR_INT(
"ptaa not defined", __func__, 1);
1488 if ((fp = open_memstream((
char **)pdata, psize)) == NULL)
1489 return ERROR_INT(
"stream not opened", __func__, 1);
1493 *psize = *psize - 1;
1495 L_INFO(
"work-around: writing to a temp file\n", __func__);
1498 return ERROR_INT(
"tmpfile stream not opened", __func__, 1);
1500 if ((fp = tmpfile()) == NULL)
1501 return ERROR_INT(
"tmpfile stream not opened", __func__, 1);
l_ok numaGetFValue(NUMA *na, l_int32 index, l_float32 *pval)
numaGetFValue()
NUMA * numaCreate(l_int32 n)
numaCreate()
l_int32 numaGetCount(NUMA *na)
numaGetCount()
l_ok numaGetParameters(NUMA *na, l_float32 *pstartx, l_float32 *pdelx)
numaGetParameters()
#define PTA_VERSION_NUMBER
l_ok ptaWriteStream(FILE *fp, PTA *pta, l_int32 type)
ptaWriteStream()
PTA * ptaCreateFromNuma(NUMA *nax, NUMA *nay)
ptaCreateFromNuma()
PTA * ptaReadStream(FILE *fp)
ptaReadStream()
static l_int32 ptaaExtendArray(PTAA *ptaa)
ptaaExtendArray()
l_ok ptaWriteMem(l_uint8 **pdata, size_t *psize, PTA *pta, l_int32 type)
ptaWriteMem()
PTAA * ptaaReadMem(const l_uint8 *data, size_t size)
ptaaReadMem()
PTAA * ptaaCreate(l_int32 n)
ptaaCreate()
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 * ptaaGetPta(PTAA *ptaa, l_int32 index, l_int32 accessflag)
ptaaGetPta()
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()
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()
l_ok ptaaAddPta(PTAA *ptaa, PTA *pta, l_int32 copyflag)
ptaaAddPta()
PTA * ptaClone(PTA *pta)
ptaClone()
l_int32 ptaaGetCount(PTAA *ptaa)
ptaaGetCount()
l_ok ptaaWrite(const char *filename, PTAA *ptaa, l_int32 type)
ptaaWrite()
l_ok ptaaInitFull(PTAA *ptaa, PTA *pta)
ptaaInitFull()
PTA * ptaReadMem(const l_uint8 *data, size_t size)
ptaReadMem()
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
PTA * ptaRead(const char *filename)
ptaRead()
l_int32 ptaGetCount(PTA *pta)
ptaGetCount()
void ptaaDestroy(PTAA **pptaa)
ptaaDestroy()
PTAA * ptaaReadStream(FILE *fp)
ptaaReadStream()
PTAA * ptaaRead(const char *filename)
ptaaRead()
PTA * ptaCreate(l_int32 n)
ptaCreate()
void ptaDestroy(PTA **ppta)
ptaDestroy()
l_ok ptaaReplacePta(PTAA *ptaa, l_int32 index, PTA *pta)
ptaaReplacePta()
PTA * ptaCopyRange(PTA *ptas, l_int32 istart, l_int32 iend)
ptaCopyRange()
static l_int32 ptaExtendArrays(PTA *pta)
ptaExtendArrays()
l_ok ptaaTruncate(PTAA *ptaa)
ptaaTruncate()
PTA * ptaCopy(PTA *pta)
ptaCopy()
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()