120#include <config_auto.h>
124#include "allheaders.h"
128static 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);
248l_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);
745size_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 *);
760 if ((fpixa->
fpix = (
FPIX **)reallocNew((
void **)&fpixa->
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);
1053l_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);
1369 if ((fp = fopenReadStream(filename)) == NULL)
1370 return (
FPIX *)ERROR_PTR_1(
"stream not opened",
1371 filename, __func__, NULL);
1375 return (
FPIX *)ERROR_PTR_1(
"fpix not read", filename, __func__, NULL);
1390l_int32 w, h, nbytes, xres, yres, version;
1395 return (
FPIX *)ERROR_PTR(
"stream not defined", __func__, NULL);
1397 if (fscanf(fp,
"\nFPix Version %d\n", &version) != 1)
1398 return (
FPIX *)ERROR_PTR(
"not a fpix file", __func__, NULL);
1400 return (
FPIX *)ERROR_PTR(
"invalid fpix version", __func__, NULL);
1401 if (fscanf(fp,
"w = %d, h = %d, nbytes = %d\n", &w, &h, &nbytes) != 3)
1402 return (
FPIX *)ERROR_PTR(
"read fail for data size", __func__, NULL);
1409 if (fgets(buf,
sizeof(buf), fp) == NULL)
1410 return (
FPIX *)ERROR_PTR(
"fgets read fail", __func__, NULL);
1411 if (sscanf(buf,
"xres = %d, yres = %d\n", &xres, &yres) != 2)
1412 return (
FPIX *)ERROR_PTR(
"read fail for xres, yres", __func__, NULL);
1415 return (
FPIX *)ERROR_PTR(
"fpix not made", __func__, NULL);
1418 if (fread(data, 1, nbytes, fp) != nbytes) {
1420 return (
FPIX *)ERROR_PTR(
"read error for nbytes", __func__, NULL);
1445 return (
FPIX *)ERROR_PTR(
"data not defined", __func__, NULL);
1446 if ((fp = fopenReadFromMemory(data, size)) == NULL)
1447 return (
FPIX *)ERROR_PTR(
"stream not opened", __func__, NULL);
1451 if (!fpix) L_ERROR(
"fpix not read\n", __func__);
1471 return ERROR_INT(
"filename not defined", __func__, 1);
1473 return ERROR_INT(
"fpix not defined", __func__, 1);
1475 if ((fp = fopenWriteStream(filename,
"wb")) == NULL)
1476 return ERROR_INT_1(
"stream not opened", filename, __func__, 1);
1480 return ERROR_INT_1(
"fpix not written to stream", filename, __func__, 1);
1496l_int32 w, h, xres, yres;
1502 return ERROR_INT(
"stream not defined", __func__, 1);
1504 return ERROR_INT(
"fpix not defined", __func__, 1);
1511 nbytes =
sizeof(l_float32) * w * h;
1514 fprintf(fp,
"w = %d, h = %d, nbytes = %u\n", w, h, nbytes);
1515 fprintf(fp,
"xres = %d, yres = %d\n", xres, yres);
1516 fwrite(data, 1, nbytes, fp);
1545 if (pdata) *pdata = NULL;
1546 if (psize) *psize = 0;
1548 return ERROR_INT(
"&data not defined", __func__, 1);
1550 return ERROR_INT(
"&size not defined", __func__, 1);
1552 return ERROR_INT(
"fpix not defined", __func__, 1);
1555 if ((fp = open_memstream((
char **)pdata, psize)) == NULL)
1556 return ERROR_INT(
"stream not opened", __func__, 1);
1560 if (*psize > 0) *psize = *psize - 1;
1562 L_INFO(
"no fmemopen API --> work-around: write to temp file\n", __func__);
1564 if ((fp = fopenWriteWinTempfile()) == NULL)
1565 return ERROR_INT(
"tmpfile stream not opened", __func__, 1);
1567 if ((fp = tmpfile()) == NULL)
1568 return ERROR_INT(
"tmpfile stream not opened", __func__, 1);
1572 *pdata = l_binaryReadStream(fp, psize);
1603 return (
FPIX *)ERROR_PTR(
"fpixs not defined", __func__, fpixd);
1604 if (fpixd && (fpixs != fpixd))
1605 return (
FPIX *)ERROR_PTR(
"fpixd != fpixs", __func__, fpixd);
1618 for (i = 0; i < h; i++) {
1619 for (j = 0; j < w; j++, data++) {
1621 *data = (word >> 24) |
1622 ((word >> 8) & 0x0000ff00) |
1623 ((word << 8) & 0x00ff0000) |
1656 return (
DPIX *)ERROR_PTR(
"filename not defined", __func__, NULL);
1658 if ((fp = fopenReadStream(filename)) == NULL)
1659 return (
DPIX *)ERROR_PTR_1(
"stream not opened",
1660 filename, __func__, NULL);
1664 return (
DPIX *)ERROR_PTR_1(
"dpix not read", filename, __func__, NULL);
1679l_int32 w, h, nbytes, version, xres, yres;
1684 return (
DPIX *)ERROR_PTR(
"stream not defined", __func__, NULL);
1686 if (fscanf(fp,
"\nDPix Version %d\n", &version) != 1)
1687 return (
DPIX *)ERROR_PTR(
"not a dpix file", __func__, NULL);
1689 return (
DPIX *)ERROR_PTR(
"invalid dpix version", __func__, NULL);
1690 if (fscanf(fp,
"w = %d, h = %d, nbytes = %d\n", &w, &h, &nbytes) != 3)
1691 return (
DPIX *)ERROR_PTR(
"read fail for data size", __func__, NULL);
1698 if (fgets(buf,
sizeof(buf), fp) == NULL)
1699 return (
DPIX *)ERROR_PTR(
"fgets read fail", __func__, NULL);
1700 if (sscanf(buf,
"xres = %d, yres = %d\n", &xres, &yres) != 2)
1701 return (
DPIX *)ERROR_PTR(
"read fail for xres, yres", __func__, NULL);
1704 return (
DPIX *)ERROR_PTR(
"dpix not made", __func__, NULL);
1707 if (fread(data, 1, nbytes, fp) != nbytes) {
1709 return (
DPIX *)ERROR_PTR(
"read error for nbytes", __func__, NULL);
1734 return (
DPIX *)ERROR_PTR(
"data not defined", __func__, NULL);
1735 if ((fp = fopenReadFromMemory(data, size)) == NULL)
1736 return (
DPIX *)ERROR_PTR(
"stream not opened", __func__, NULL);
1740 if (!dpix) L_ERROR(
"dpix not read\n", __func__);
1760 return ERROR_INT(
"filename not defined", __func__, 1);
1762 return ERROR_INT(
"dpix not defined", __func__, 1);
1764 if ((fp = fopenWriteStream(filename,
"wb")) == NULL)
1765 return ERROR_INT_1(
"stream not opened", filename, __func__, 1);
1769 return ERROR_INT_1(
"dpix not written to stream", filename, __func__, 1);
1785l_int32 w, h, xres, yres;
1791 return ERROR_INT(
"stream not defined", __func__, 1);
1793 return ERROR_INT(
"dpix not defined", __func__, 1);
1801 nbytes =
sizeof(l_float64) * w * h;
1803 fprintf(fp,
"w = %d, h = %d, nbytes = %u\n", w, h, nbytes);
1804 fprintf(fp,
"xres = %d, yres = %d\n", xres, yres);
1805 fwrite(data, 1, nbytes, fp);
1834 if (pdata) *pdata = NULL;
1835 if (psize) *psize = 0;
1837 return ERROR_INT(
"&data not defined", __func__, 1);
1839 return ERROR_INT(
"&size not defined", __func__, 1);
1841 return ERROR_INT(
"dpix not defined", __func__, 1);
1844 if ((fp = open_memstream((
char **)pdata, psize)) == NULL)
1845 return ERROR_INT(
"stream not opened", __func__, 1);
1849 *psize = *psize - 1;
1851 L_INFO(
"no fmemopen API --> work-around: write to temp file\n", __func__);
1853 if ((fp = fopenWriteWinTempfile()) == NULL)
1854 return ERROR_INT(
"tmpfile stream not opened", __func__, 1);
1856 if ((fp = tmpfile()) == NULL)
1857 return ERROR_INT(
"tmpfile stream not opened", __func__, 1);
1861 *pdata = l_binaryReadStream(fp, psize);
1892 return (
DPIX *)ERROR_PTR(
"dpixs not defined", __func__, dpixd);
1893 if (dpixd && (dpixs != dpixd))
1894 return (
DPIX *)ERROR_PTR(
"dpixd != dpixs", __func__, dpixd);
1907 for (i = 0; i < h; i++) {
1908 for (j = 0; j < 2 * w; j++, data++) {
1910 *data = (word >> 24) |
1911 ((word >> 8) & 0x0000ff00) |
1912 ((word << 8) & 0x00ff0000) |
1950l_int32 i, j, w, h, count;
1954 return ERROR_INT(
"stream not defined", __func__, 1);
1956 return ERROR_INT(
"fpix not defined", __func__, 1);
1958 return ERROR_INT(
"sampling factor < 1f", __func__, 1);
1961 fprintf(fp,
"\nFPix: w = %d, h = %d\n", w, h);
1962 for (i = 0; i < h; i += factor) {
1963 for (count = 0, j = 0; j < w; j += factor, count++) {
1965 fprintf(fp,
"val[%d, %d] = %f ", i, j, val);
1966 if ((count + 1) % 3 == 0) fprintf(fp,
"\n");
1968 if (count % 3) fprintf(fp,
"\n");
DPIX * dpixClone(DPIX *dpix)
dpixClone()
FPIX * fpixReadStream(FILE *fp)
fpixReadStream()
l_float32 * fpixaGetData(FPIXA *fpixa, l_int32 index)
fpixaGetData()
l_ok fpixCopyResolution(FPIX *fpixd, FPIX *fpixs)
fpixCopyResolution()
l_ok dpixSetWpl(DPIX *dpix, l_int32 wpl)
dpixSetWpl()
l_float64 * dpixGetData(DPIX *dpix)
dpixGetData()
l_ok dpixWrite(const char *filename, DPIX *dpix)
dpixWrite()
FPIX * fpixCreate(l_int32 width, l_int32 height)
fpixCreate()
l_ok fpixaAddFPix(FPIXA *fpixa, FPIX *fpix, l_int32 copyflag)
fpixaAddFPix()
l_ok dpixGetDimensions(DPIX *dpix, l_int32 *pw, l_int32 *ph)
dpixGetDimensions()
void dpixDestroy(DPIX **pdpix)
dpixDestroy()
void fpixaDestroy(FPIXA **pfpixa)
fpixaDestroy()
FPIXA * fpixaCopy(FPIXA *fpixa, l_int32 copyflag)
fpixaCopy()
l_ok dpixSetPixel(DPIX *dpix, l_int32 x, l_int32 y, l_float64 val)
dpixSetPixel()
l_float32 * fpixGetData(FPIX *fpix)
fpixGetData()
l_int32 dpixGetWpl(DPIX *dpix)
dpixGetWpl()
FPIX * fpixReadMem(const l_uint8 *data, size_t size)
fpixReadMem()
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()
FPIX * fpixaGetFPix(FPIXA *fpixa, l_int32 index, l_int32 accesstype)
fpixaGetFPix()
l_ok dpixWriteStream(FILE *fp, DPIX *dpix)
dpixWriteStream()
static l_int32 fpixaExtendArray(FPIXA *fpixa)
fpixaExtendArray()
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()
FPIX * fpixEndianByteSwap(FPIX *fpixd, FPIX *fpixs)
fpixEndianByteSwap()
DPIX * dpixReadMem(const l_uint8 *data, size_t size)
dpixReadMem()
l_ok fpixWrite(const char *filename, FPIX *fpix)
fpixWrite()
DPIX * dpixReadStream(FILE *fp)
dpixReadStream()
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_ok dpixSetResolution(DPIX *dpix, l_int32 xres, l_int32 yres)
dpixSetResolution()
DPIX * dpixRead(const char *filename)
dpixRead()
FPIX * fpixCopy(FPIX *fpixs)
fpixCopy()
FPIXA * fpixaCreate(l_int32 n)
fpixaCreate()
l_ok dpixCopyResolution(DPIX *dpixd, DPIX *dpixs)
dpixCopyResolution()
l_int32 fpixGetWpl(FPIX *fpix)
fpixGetWpl()
DPIX * dpixCreateTemplate(DPIX *dpixs)
dpixCreateTemplate()
DPIX * dpixCreate(l_int32 width, l_int32 height)
dpixCreate()
l_ok fpixGetResolution(FPIX *fpix, l_int32 *pxres, l_int32 *pyres)
fpixGetResolution()
DPIX * dpixEndianByteSwap(DPIX *dpixd, DPIX *dpixs)
dpixEndianByteSwap()
l_ok fpixSetData(FPIX *fpix, l_float32 *data)
fpixSetData()
l_ok fpixSetPixel(FPIX *fpix, l_int32 x, l_int32 y, l_float32 val)
fpixSetPixel()
FPIX * fpixClone(FPIX *fpix)
fpixClone()
void fpixDestroy(FPIX **pfpix)
fpixDestroy()
DPIX * dpixCopy(DPIX *dpixs)
dpixCopy()
l_ok fpixSetResolution(FPIX *fpix, l_int32 xres, l_int32 yres)
fpixSetResolution()
l_int32 fpixaGetCount(FPIXA *fpixa)
fpixaGetCount()
FPIX * fpixRead(const char *filename)
fpixRead()
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()
l_ok fpixaGetFPixDimensions(FPIXA *fpixa, l_int32 index, l_int32 *pw, l_int32 *ph)
fpixaGetFPixDimensions()
FPIX * fpixCreateTemplate(FPIX *fpixs)
fpixCreateTemplate()
l_ok fpixPrintStream(FILE *fp, FPIX *fpix, l_int32 factor)
fpixPrintStream()
#define FPIX_VERSION_NUMBER
#define DPIX_VERSION_NUMBER