120 #include <config_auto.h>
124 #include "allheaders.h"
128 static const size_t MaxPtrArraySize = 100000;
160 return (
FPIX *)ERROR_PTR(
"width must be > 0", __func__, NULL);
162 return (
FPIX *)ERROR_PTR(
"height must be > 0", __func__, NULL);
165 npix64 = (l_uint64)width * (l_uint64)height;
166 if (npix64 >= (1LL << 29)) {
167 L_ERROR(
"requested w = %d, h = %d\n", __func__, width, height);
168 return (
FPIX *)ERROR_PTR(
"requested bytes >= 2^31", __func__, NULL);
171 fpixd = (
FPIX *)LEPT_CALLOC(1,
sizeof(
FPIX));
176 data = (l_float32 *)LEPT_CALLOC((
size_t)width * height,
sizeof(l_float32));
179 return (
FPIX *)ERROR_PTR(
"calloc fail for data", __func__, NULL);
206 return (
FPIX *)ERROR_PTR(
"fpixs not defined", __func__, NULL);
210 return (
FPIX *)ERROR_PTR(
"fpixd not made", __func__, NULL);
231 return (
FPIX *)ERROR_PTR(
"fpix not defined", __func__, NULL);
248 l_float32 *datas, *datad;
252 return (
FPIX *)ERROR_PTR(
"fpixs not defined", __func__, NULL);
259 return (
FPIX *)ERROR_PTR(
"fpixd not made", __func__, NULL);
262 memcpy(datad, datas, bytes);
286 L_WARNING(
"ptr address is null!\n", __func__);
290 if ((fpix = *pfpix) == NULL)
319 return ERROR_INT(
"no return val requested", __func__, 1);
323 return ERROR_INT(
"fpix not defined", __func__, 1);
324 if (pw) *pw = fpix->
w;
325 if (ph) *ph = fpix->
h;
343 return ERROR_INT(
"fpix not defined", __func__, 1);
360 return ERROR_INT(
"fpix not defined", __func__, 0);
377 return ERROR_INT(
"fpix not defined", __func__, 1);
397 return ERROR_INT(
"fpix not defined", __func__, 1);
398 if (pxres) *pxres = fpix->
xres;
399 if (pyres) *pyres = fpix->
yres;
417 return ERROR_INT(
"fpix not defined", __func__, 1);
436 if (!fpixs || !fpixd)
437 return ERROR_INT(
"fpixs and fpixd not both defined", __func__, 1);
455 return (l_float32 *)ERROR_PTR(
"fpix not defined", __func__, NULL);
472 return ERROR_INT(
"fpix not defined", __func__, 1);
500 return ERROR_INT(
"pval not defined", __func__, 1);
503 return ERROR_INT(
"fpix not defined", __func__, 1);
506 if (x < 0 || x >= w || y < 0 || y >= h)
509 *pval = *(fpix->
data + y * w + x);
535 return ERROR_INT(
"fpix not defined", __func__, 1);
538 if (x < 0 || x >= w || y < 0 || y >= h)
541 *(fpix->
data + y * w + x) = val;
560 if (n <= 0 || n > MaxPtrArraySize)
563 fpixa = (
FPIXA *)LEPT_CALLOC(1,
sizeof(
FPIXA));
596 return (
FPIXA *)ERROR_PTR(
"fpixa not defined", __func__, NULL);
604 return (
FPIXA *)ERROR_PTR(
"invalid copyflag", __func__, NULL);
607 return (
FPIXA *)ERROR_PTR(
"fpixac not made", __func__, NULL);
608 for (i = 0; i < fpixa->
n; i++) {
638 if (pfpixa == NULL) {
639 L_WARNING(
"ptr address is NULL!\n", __func__);
643 if ((fpixa = *pfpixa) == NULL)
648 for (i = 0; i < fpixa->
n; i++)
650 LEPT_FREE(fpixa->
fpix);
677 return ERROR_INT(
"fpixa not defined", __func__, 1);
679 return ERROR_INT(
"fpix not defined", __func__, 1);
683 else if (copyflag ==
L_COPY)
688 return ERROR_INT(
"invalid copyflag", __func__, 1);
690 return ERROR_INT(
"fpixc not made", __func__, 1);
697 return ERROR_INT(
"extension failed", __func__, 1);
700 fpixa->
fpix[n] = fpixc;
722 return ERROR_INT(
"fpixa not defined", __func__, 1);
745 size_t oldsize, newsize;
748 return ERROR_INT(
"fpixa not defined", __func__, 1);
749 if (fpixa->
nalloc > MaxPtrArraySize)
750 return ERROR_INT(
"fpixa has too many ptrs", __func__, 1);
751 if (size > MaxPtrArraySize)
752 return ERROR_INT(
"size > 100K ptrs; too large", __func__, 1);
753 if (size <= fpixa->nalloc) {
754 L_INFO(
"size too small; no extension\n", __func__);
759 newsize = size *
sizeof(
FPIX *);
761 oldsize, newsize)) == NULL)
762 return ERROR_INT(
"new ptr array not returned", __func__, 1);
781 return ERROR_INT(
"fpixa not defined", __func__, 0);
801 return (
FPIX *)ERROR_PTR(
"fpixa not defined", __func__, NULL);
802 if (index < 0 || index >= fpixa->
n)
803 return (
FPIX *)ERROR_PTR(
"index not valid", __func__, NULL);
807 else if (accesstype ==
L_CLONE)
810 return (
FPIX *)ERROR_PTR(
"invalid accesstype", __func__, NULL);
831 return ERROR_INT(
"no return val requested", __func__, 1);
835 return ERROR_INT(
"fpixa not defined", __func__, 1);
836 if (index < 0 || index >= fpixa->
n)
837 return ERROR_INT(
"index not valid", __func__, 1);
840 return ERROR_INT(
"fpix not found!", __func__, 1);
863 return (l_float32 *)ERROR_PTR(
"fpixa not defined", __func__, NULL);
865 if (index < 0 || index >= n)
866 return (l_float32 *)ERROR_PTR(
"invalid index", __func__, NULL);
895 return ERROR_INT(
"pval not defined", __func__, 1);
898 return ERROR_INT(
"fpixa not defined", __func__, 1);
900 if (index < 0 || index >= n)
901 return ERROR_INT(
"invalid index into fpixa", __func__, 1);
930 return ERROR_INT(
"fpixa not defined", __func__, 1);
932 if (index < 0 || index >= n)
933 return ERROR_INT(
"invalid index into fpixa", __func__, 1);
967 return (
DPIX *)ERROR_PTR(
"width must be > 0", __func__, NULL);
969 return (
DPIX *)ERROR_PTR(
"height must be > 0", __func__, NULL);
972 npix64 = (l_uint64)width * (l_uint64)height;
973 if (npix64 >= (1LL << 28)) {
974 L_ERROR(
"requested w = %d, h = %d\n", __func__, width, height);
975 return (
DPIX *)ERROR_PTR(
"requested bytes >= 2^31", __func__, NULL);
978 dpix = (
DPIX *)LEPT_CALLOC(1,
sizeof(
DPIX));
983 data = (l_float64 *)LEPT_CALLOC((
size_t)width * height,
sizeof(l_float64));
986 return (
DPIX *)ERROR_PTR(
"calloc fail for data", __func__, NULL);
1013 return (
DPIX *)ERROR_PTR(
"dpixs not defined", __func__, NULL);
1037 return (
DPIX *)ERROR_PTR(
"dpix not defined", __func__, NULL);
1052 l_int32 w, h, bytes;
1053 l_float64 *datas, *datad;
1057 return (
DPIX *)ERROR_PTR(
"dpixs not defined", __func__, NULL);
1064 return (
DPIX *)ERROR_PTR(
"dpixd not made", __func__, NULL);
1067 memcpy(datad, datas, bytes);
1091 L_WARNING(
"ptr address is null!\n", __func__);
1095 if ((dpix = *pdpix) == NULL)
1124 return ERROR_INT(
"no return val requested", __func__, 1);
1128 return ERROR_INT(
"dpix not defined", __func__, 1);
1129 if (pw) *pw = dpix->
w;
1130 if (ph) *ph = dpix->
h;
1148 return ERROR_INT(
"dpix not defined", __func__, 1);
1165 return ERROR_INT(
"dpix not defined", __func__, 0);
1182 return ERROR_INT(
"dpix not defined", __func__, 1);
1202 return ERROR_INT(
"dpix not defined", __func__, 1);
1203 if (pxres) *pxres = dpix->
xres;
1204 if (pyres) *pyres = dpix->
yres;
1222 return ERROR_INT(
"dpix not defined", __func__, 1);
1241 if (!dpixs || !dpixd)
1242 return ERROR_INT(
"dpixs and dpixd not both defined", __func__, 1);
1260 return (l_float64 *)ERROR_PTR(
"dpix not defined", __func__, NULL);
1277 return ERROR_INT(
"dpix not defined", __func__, 1);
1305 return ERROR_INT(
"pval not defined", __func__, 1);
1308 return ERROR_INT(
"dpix not defined", __func__, 1);
1311 if (x < 0 || x >= w || y < 0 || y >= h)
1314 *pval = *(dpix->
data + y * w + x);
1340 return ERROR_INT(
"dpix not defined", __func__, 1);
1343 if (x < 0 || x >= w || y < 0 || y >= h)
1346 *(dpix->
data + y * w + x) = val;
1367 return (
FPIX *)ERROR_PTR(
"filename not defined", __func__, NULL);
1370 return (
FPIX *)ERROR_PTR(
"stream not opened", __func__, NULL);
1374 return (
FPIX *)ERROR_PTR(
"fpix not read", __func__, NULL);
1389 l_int32 w, h, nbytes, xres, yres, version;
1394 return (
FPIX *)ERROR_PTR(
"stream not defined", __func__, NULL);
1396 if (fscanf(fp,
"\nFPix Version %d\n", &version) != 1)
1397 return (
FPIX *)ERROR_PTR(
"not a fpix file", __func__, NULL);
1399 return (
FPIX *)ERROR_PTR(
"invalid fpix version", __func__, NULL);
1400 if (fscanf(fp,
"w = %d, h = %d, nbytes = %d\n", &w, &h, &nbytes) != 3)
1401 return (
FPIX *)ERROR_PTR(
"read fail for data size", __func__, NULL);
1408 if (fgets(buf,
sizeof(buf), fp) == NULL)
1409 return (
FPIX *)ERROR_PTR(
"fgets read fail", __func__, NULL);
1410 if (sscanf(buf,
"xres = %d, yres = %d\n", &xres, &yres) != 2)
1411 return (
FPIX *)ERROR_PTR(
"read fail for xres, yres", __func__, NULL);
1414 return (
FPIX *)ERROR_PTR(
"fpix not made", __func__, NULL);
1417 if (fread(data, 1, nbytes, fp) != nbytes) {
1419 return (
FPIX *)ERROR_PTR(
"read error for nbytes", __func__, NULL);
1444 return (
FPIX *)ERROR_PTR(
"data not defined", __func__, NULL);
1446 return (
FPIX *)ERROR_PTR(
"stream not opened", __func__, NULL);
1450 if (!fpix) L_ERROR(
"fpix not read\n", __func__);
1470 return ERROR_INT(
"filename not defined", __func__, 1);
1472 return ERROR_INT(
"fpix not defined", __func__, 1);
1475 return ERROR_INT(
"stream not opened", __func__, 1);
1479 return ERROR_INT(
"fpix not written to stream", __func__, 1);
1495 l_int32 w, h, xres, yres;
1501 return ERROR_INT(
"stream not defined", __func__, 1);
1503 return ERROR_INT(
"fpix not defined", __func__, 1);
1510 nbytes =
sizeof(l_float32) * w * h;
1513 fprintf(fp,
"w = %d, h = %d, nbytes = %u\n", w, h, nbytes);
1514 fprintf(fp,
"xres = %d, yres = %d\n", xres, yres);
1515 fwrite(data, 1, nbytes, fp);
1544 if (pdata) *pdata = NULL;
1545 if (psize) *psize = 0;
1547 return ERROR_INT(
"&data not defined", __func__, 1);
1549 return ERROR_INT(
"&size not defined", __func__, 1);
1551 return ERROR_INT(
"fpix not defined", __func__, 1);
1554 if ((fp = open_memstream((
char **)pdata, psize)) == NULL)
1555 return ERROR_INT(
"stream not opened", __func__, 1);
1559 *psize = *psize - 1;
1561 L_INFO(
"work-around: writing to a temp file\n", __func__);
1564 return ERROR_INT(
"tmpfile stream not opened", __func__, 1);
1566 if ((fp = tmpfile()) == NULL)
1567 return ERROR_INT(
"tmpfile stream not opened", __func__, 1);
1602 return (
FPIX *)ERROR_PTR(
"fpixs not defined", __func__, fpixd);
1603 if (fpixd && (fpixs != fpixd))
1604 return (
FPIX *)ERROR_PTR(
"fpixd != fpixs", __func__, fpixd);
1617 for (i = 0; i < h; i++) {
1618 for (j = 0; j < w; j++, data++) {
1620 *data = (word >> 24) |
1621 ((word >> 8) & 0x0000ff00) |
1622 ((word << 8) & 0x00ff0000) |
1655 return (
DPIX *)ERROR_PTR(
"filename not defined", __func__, NULL);
1658 return (
DPIX *)ERROR_PTR(
"stream not opened", __func__, NULL);
1662 return (
DPIX *)ERROR_PTR(
"dpix not read", __func__, NULL);
1677 l_int32 w, h, nbytes, version, xres, yres;
1682 return (
DPIX *)ERROR_PTR(
"stream not defined", __func__, NULL);
1684 if (fscanf(fp,
"\nDPix Version %d\n", &version) != 1)
1685 return (
DPIX *)ERROR_PTR(
"not a dpix file", __func__, NULL);
1687 return (
DPIX *)ERROR_PTR(
"invalid dpix version", __func__, NULL);
1688 if (fscanf(fp,
"w = %d, h = %d, nbytes = %d\n", &w, &h, &nbytes) != 3)
1689 return (
DPIX *)ERROR_PTR(
"read fail for data size", __func__, NULL);
1696 if (fgets(buf,
sizeof(buf), fp) == NULL)
1697 return (
DPIX *)ERROR_PTR(
"fgets read fail", __func__, NULL);
1698 if (sscanf(buf,
"xres = %d, yres = %d\n", &xres, &yres) != 2)
1699 return (
DPIX *)ERROR_PTR(
"read fail for xres, yres", __func__, NULL);
1702 return (
DPIX *)ERROR_PTR(
"dpix not made", __func__, NULL);
1705 if (fread(data, 1, nbytes, fp) != nbytes) {
1707 return (
DPIX *)ERROR_PTR(
"read error for nbytes", __func__, NULL);
1732 return (
DPIX *)ERROR_PTR(
"data not defined", __func__, NULL);
1734 return (
DPIX *)ERROR_PTR(
"stream not opened", __func__, NULL);
1738 if (!dpix) L_ERROR(
"dpix not read\n", __func__);
1758 return ERROR_INT(
"filename not defined", __func__, 1);
1760 return ERROR_INT(
"dpix not defined", __func__, 1);
1763 return ERROR_INT(
"stream not opened", __func__, 1);
1767 return ERROR_INT(
"dpix not written to stream", __func__, 1);
1783 l_int32 w, h, xres, yres;
1789 return ERROR_INT(
"stream not defined", __func__, 1);
1791 return ERROR_INT(
"dpix not defined", __func__, 1);
1799 nbytes =
sizeof(l_float64) * w * h;
1801 fprintf(fp,
"w = %d, h = %d, nbytes = %u\n", w, h, nbytes);
1802 fprintf(fp,
"xres = %d, yres = %d\n", xres, yres);
1803 fwrite(data, 1, nbytes, fp);
1832 if (pdata) *pdata = NULL;
1833 if (psize) *psize = 0;
1835 return ERROR_INT(
"&data not defined", __func__, 1);
1837 return ERROR_INT(
"&size not defined", __func__, 1);
1839 return ERROR_INT(
"dpix not defined", __func__, 1);
1842 if ((fp = open_memstream((
char **)pdata, psize)) == NULL)
1843 return ERROR_INT(
"stream not opened", __func__, 1);
1847 *psize = *psize - 1;
1849 L_INFO(
"work-around: writing to a temp file\n", __func__);
1852 return ERROR_INT(
"tmpfile stream not opened", __func__, 1);
1854 if ((fp = tmpfile()) == NULL)
1855 return ERROR_INT(
"tmpfile stream not opened", __func__, 1);
1890 return (
DPIX *)ERROR_PTR(
"dpixs not defined", __func__, dpixd);
1891 if (dpixd && (dpixs != dpixd))
1892 return (
DPIX *)ERROR_PTR(
"dpixd != dpixs", __func__, dpixd);
1905 for (i = 0; i < h; i++) {
1906 for (j = 0; j < 2 * w; j++, data++) {
1908 *data = (word >> 24) |
1909 ((word >> 8) & 0x0000ff00) |
1910 ((word << 8) & 0x00ff0000) |
1948 l_int32 i, j, w, h, count;
1952 return ERROR_INT(
"stream not defined", __func__, 1);
1954 return ERROR_INT(
"fpix not defined", __func__, 1);
1956 return ERROR_INT(
"sampling factor < 1f", __func__, 1);
1959 fprintf(fp,
"\nFPix: w = %d, h = %d\n", w, h);
1960 for (i = 0; i < h; i += factor) {
1961 for (count = 0, j = 0; j < w; j += factor, count++) {
1963 fprintf(fp,
"val[%d, %d] = %f ", i, j, val);
1964 if ((count + 1) % 3 == 0) fprintf(fp,
"\n");
1966 if (count % 3) fprintf(fp,
"\n");
DPIX * dpixCopy(DPIX *dpixs)
dpixCopy()
l_ok fpixCopyResolution(FPIX *fpixd, FPIX *fpixs)
fpixCopyResolution()
FPIX * fpixCreateTemplate(FPIX *fpixs)
fpixCreateTemplate()
l_ok dpixSetWpl(DPIX *dpix, l_int32 wpl)
dpixSetWpl()
l_ok dpixWrite(const char *filename, DPIX *dpix)
dpixWrite()
l_ok fpixaAddFPix(FPIXA *fpixa, FPIX *fpix, l_int32 copyflag)
fpixaAddFPix()
FPIXA * fpixaCopy(FPIXA *fpixa, l_int32 copyflag)
fpixaCopy()
l_ok dpixGetDimensions(DPIX *dpix, l_int32 *pw, l_int32 *ph)
dpixGetDimensions()
void dpixDestroy(DPIX **pdpix)
dpixDestroy()
void fpixaDestroy(FPIXA **pfpixa)
fpixaDestroy()
DPIX * dpixEndianByteSwap(DPIX *dpixd, DPIX *dpixs)
dpixEndianByteSwap()
DPIX * dpixReadMem(const l_uint8 *data, size_t size)
dpixReadMem()
l_ok dpixSetPixel(DPIX *dpix, l_int32 x, l_int32 y, l_float64 val)
dpixSetPixel()
FPIX * fpixReadMem(const l_uint8 *data, size_t size)
fpixReadMem()
l_int32 dpixGetWpl(DPIX *dpix)
dpixGetWpl()
DPIX * dpixCreate(l_int32 width, l_int32 height)
dpixCreate()
l_ok fpixWriteStream(FILE *fp, FPIX *fpix)
fpixWriteStream()
l_ok fpixaGetPixel(FPIXA *fpixa, l_int32 index, l_int32 x, l_int32 y, l_float32 *pval)
fpixaGetPixel()
l_ok fpixWriteMem(l_uint8 **pdata, size_t *psize, FPIX *fpix)
fpixWriteMem()
l_ok dpixWriteMem(l_uint8 **pdata, size_t *psize, DPIX *dpix)
dpixWriteMem()
l_ok fpixaSetPixel(FPIXA *fpixa, l_int32 index, l_int32 x, l_int32 y, l_float32 val)
fpixaSetPixel()
l_ok dpixSetData(DPIX *dpix, l_float64 *data)
dpixSetData()
l_ok dpixWriteStream(FILE *fp, DPIX *dpix)
dpixWriteStream()
DPIX * dpixReadStream(FILE *fp)
dpixReadStream()
static l_int32 fpixaExtendArray(FPIXA *fpixa)
fpixaExtendArray()
DPIX * dpixClone(DPIX *dpix)
dpixClone()
DPIX * dpixRead(const char *filename)
dpixRead()
l_float64 * dpixGetData(DPIX *dpix)
dpixGetData()
static l_int32 fpixaExtendArrayToSize(FPIXA *fpixa, l_int32 size)
fpixaExtendArrayToSize()
l_ok fpixGetDimensions(FPIX *fpix, l_int32 *pw, l_int32 *ph)
fpixGetDimensions()
l_ok fpixSetDimensions(FPIX *fpix, l_int32 w, l_int32 h)
fpixSetDimensions()
l_ok fpixWrite(const char *filename, FPIX *fpix)
fpixWrite()
l_ok dpixGetPixel(DPIX *dpix, l_int32 x, l_int32 y, l_float64 *pval)
dpixGetPixel()
static const size_t InitialPtrArraySize
l_ok fpixSetWpl(FPIX *fpix, l_int32 wpl)
fpixSetWpl()
l_float32 * fpixaGetData(FPIXA *fpixa, l_int32 index)
fpixaGetData()
l_ok dpixSetResolution(DPIX *dpix, l_int32 xres, l_int32 yres)
dpixSetResolution()
FPIX * fpixRead(const char *filename)
fpixRead()
FPIX * fpixEndianByteSwap(FPIX *fpixd, FPIX *fpixs)
fpixEndianByteSwap()
l_ok dpixCopyResolution(DPIX *dpixd, DPIX *dpixs)
dpixCopyResolution()
FPIX * fpixaGetFPix(FPIXA *fpixa, l_int32 index, l_int32 accesstype)
fpixaGetFPix()
l_int32 fpixGetWpl(FPIX *fpix)
fpixGetWpl()
FPIXA * fpixaCreate(l_int32 n)
fpixaCreate()
l_ok fpixGetResolution(FPIX *fpix, l_int32 *pxres, l_int32 *pyres)
fpixGetResolution()
DPIX * dpixCreateTemplate(DPIX *dpixs)
dpixCreateTemplate()
l_ok fpixSetData(FPIX *fpix, l_float32 *data)
fpixSetData()
l_ok fpixSetPixel(FPIX *fpix, l_int32 x, l_int32 y, l_float32 val)
fpixSetPixel()
void fpixDestroy(FPIX **pfpix)
fpixDestroy()
l_ok fpixSetResolution(FPIX *fpix, l_int32 xres, l_int32 yres)
fpixSetResolution()
l_int32 fpixaGetCount(FPIXA *fpixa)
fpixaGetCount()
FPIX * fpixCreate(l_int32 width, l_int32 height)
fpixCreate()
FPIX * fpixReadStream(FILE *fp)
fpixReadStream()
l_ok dpixGetResolution(DPIX *dpix, l_int32 *pxres, l_int32 *pyres)
dpixGetResolution()
l_ok fpixGetPixel(FPIX *fpix, l_int32 x, l_int32 y, l_float32 *pval)
fpixGetPixel()
l_ok dpixSetDimensions(DPIX *dpix, l_int32 w, l_int32 h)
dpixSetDimensions()
FPIX * fpixClone(FPIX *fpix)
fpixClone()
l_ok fpixaGetFPixDimensions(FPIXA *fpixa, l_int32 index, l_int32 *pw, l_int32 *ph)
fpixaGetFPixDimensions()
l_float32 * fpixGetData(FPIX *fpix)
fpixGetData()
l_ok fpixPrintStream(FILE *fp, FPIX *fpix, l_int32 factor)
fpixPrintStream()
FPIX * fpixCopy(FPIX *fpixs)
fpixCopy()
#define FPIX_VERSION_NUMBER
#define DPIX_VERSION_NUMBER
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()