199 #include <config_auto.h>
203 #include "allheaders.h"
237 #ifdef LEPTONICA_INTERCEPT_ALLOC
247 pixdata_malloc(
size_t size)
254 return LEPT_MALLOC(size);
259 pixdata_free(
void *ptr)
321 PROCNAME(
"pixCreate");
324 return (
PIX *)ERROR_PTR(
"pixd not made", procName, NULL);
325 memset(pixd->
data, 0, 4LL * pixd->
wpl * pixd->
h);
354 PROCNAME(
"pixCreateNoInit");
356 return (
PIX *)ERROR_PTR(
"pixd not made", procName, NULL);
357 wpl = pixGetWpl(pixd);
358 if ((data = (l_uint32 *)pixdata_malloc(4LL * wpl * height)) == NULL) {
360 return (
PIX *)ERROR_PTR(
"pixdata_malloc fail for data",
387 PROCNAME(
"pixCreateTemplate");
390 return (
PIX *)ERROR_PTR(
"pixs not defined", procName, NULL);
393 return (
PIX *)ERROR_PTR(
"pixd not made", procName, NULL);
394 memset(pixd->
data, 0, 4LL * pixd->
wpl * pixd->
h);
422 PROCNAME(
"pixCreateTemplateNoInit");
425 return (
PIX *)ERROR_PTR(
"pixs not defined", procName, NULL);
429 return (
PIX *)ERROR_PTR(
"pixd not made", procName, NULL);
431 pixCopyResolution(pixd, pixs);
433 pixCopyText(pixd, pixs);
434 pixCopyInputFormat(pixd, pixs);
466 PROCNAME(
"pixCreateWithCmap");
468 if (depth != 2 && depth != 4 && depth != 8)
469 return (
PIX *)ERROR_PTR(
"depth not 2, 4 or 8 bpp", procName, NULL);
471 if ((pix =
pixCreate(width, height, depth)) == NULL)
472 return (
PIX *)ERROR_PTR(
"pix not made", procName, NULL);
509 l_uint64 wpl64, bignum;
512 PROCNAME(
"pixCreateHeader");
514 if ((depth != 1) && (depth != 2) && (depth != 4) && (depth != 8)
515 && (depth != 16) && (depth != 24) && (depth != 32))
516 return (
PIX *)ERROR_PTR(
"depth must be {1, 2, 4, 8, 16, 24, 32}",
519 return (
PIX *)ERROR_PTR(
"width must be > 0", procName, NULL);
521 return (
PIX *)ERROR_PTR(
"height must be > 0", procName, NULL);
524 wpl64 = ((l_uint64)width * (l_uint64)depth + 31) / 32;
525 if (wpl64 > ((1LL << 24) - 1)) {
526 L_ERROR(
"requested w = %d, h = %d, d = %d\n",
527 procName, width, height, depth);
528 return (
PIX *)ERROR_PTR(
"wpl >= 2^24", procName, NULL);
530 wpl = (l_int32)wpl64;
531 bignum = 4LL * wpl * height;
532 if (bignum > ((1LL << 31) - 1)) {
533 L_ERROR(
"requested w = %d, h = %d, d = %d\n",
534 procName, width, height, depth);
535 return (
PIX *)ERROR_PTR(
"requested bytes >= 2^31", procName, NULL);
538 #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
539 if (bignum > (1LL << 26)) {
540 L_ERROR(
"fuzzer requested > 64 MB; refused\n", procName);
544 L_ERROR(
"fuzzer requested width > 20K; refused\n", procName);
547 if (height > 20000) {
548 L_ERROR(
"fuzzer requested height > 20K; refused\n", procName);
553 pixd = (
PIX *)LEPT_CALLOC(1,
sizeof(
PIX));
554 pixSetWidth(pixd, width);
555 pixSetHeight(pixd, height);
556 pixSetDepth(pixd, depth);
557 pixSetWpl(pixd, wpl);
558 if (depth == 24 || depth == 32)
595 PROCNAME(
"pixClone");
598 return (
PIX *)ERROR_PTR(
"pixs not defined", procName, NULL);
599 pixChangeRefcount(pixs, 1);
625 PROCNAME(
"pixDestroy");
628 L_WARNING(
"ptr address is null!\n", procName);
632 if ((pix = *ppix) == NULL)
658 pixChangeRefcount(pix, -1);
659 if (pixGetRefcount(pix) <= 0) {
713 return (
PIX *)ERROR_PTR(
"pixs not defined", procName, pixd);
718 bytes = 4 * pixGetWpl(pixs) * pixGetHeight(pixs);
723 return (
PIX *)ERROR_PTR(
"pixd not made", procName, NULL);
724 memcpy(pixd->
data, pixs->
data, bytes);
735 return (
PIX *)ERROR_PTR(
"reallocation of data failed", procName, NULL);
740 pixCopyResolution(pixd, pixs);
741 pixCopyInputFormat(pixd, pixs);
742 pixCopyText(pixd, pixs);
745 memcpy(pixd->
data, pixs->
data, bytes);
771 l_int32 w, h, d, wpl, bytes;
774 PROCNAME(
"pixResizeImageData");
777 return ERROR_INT(
"pixs not defined", procName, 1);
779 return ERROR_INT(
"pixd not defined", procName, 1);
786 wpl = pixGetWpl(pixs);
788 if ((data = (l_uint32 *)pixdata_malloc(bytes)) == NULL)
789 return ERROR_INT(
"pixdata_malloc fail for data", procName, 1);
792 pixSetWidth(pixd, w);
793 pixSetHeight(pixd, h);
794 pixSetDepth(pixd, d);
795 pixSetWpl(pixd, wpl);
798 pixCopyResolution(pixd, pixs);
823 PROCNAME(
"pixCopyColormap");
826 return ERROR_INT(
"pixs not defined", procName, 1);
828 return ERROR_INT(
"pixd not defined", procName, 1);
831 if (pixGetDepth(pixs) != pixGetDepth(pixd))
832 return ERROR_INT(
"depths of pixs and pixd differ", procName, 1);
835 if ((cmaps = pixs->
colormap) == NULL)
839 return ERROR_INT(
"cmap not valid", procName, 1);
842 return ERROR_INT(
"cmapd not made", procName, 1);
910 PROCNAME(
"pixTransferAllData");
913 return ERROR_INT(
"&pixs not defined", procName, 1);
914 if ((pixs = *ppixs) == NULL)
915 return ERROR_INT(
"pixs not defined", procName, 1);
917 return ERROR_INT(
"pixd not defined", procName, 1);
919 return ERROR_INT(
"pixd == pixs", procName, 1);
921 if (pixGetRefcount(pixs) == 1) {
926 pixd->
colormap = pixGetColormap(pixs);
934 nbytes = 4 * pixGetWpl(pixs) * pixGetHeight(pixs);
938 pixCopyText(pixd, pixs);
942 pixCopyResolution(pixd, pixs);
945 pixCopyInputFormat(pixd, pixs);
996 PROCNAME(
"pixSwapAndDestroy");
999 return ERROR_INT(
"&pixd not defined", procName, 1);
1001 return ERROR_INT(
"&pixs not defined", procName, 1);
1003 return ERROR_INT(
"pixs not defined", procName, 1);
1005 return ERROR_INT(
"&pixd == &pixs", procName, 1);
1018 pixGetWidth(
const PIX *pix)
1020 PROCNAME(
"pixGetWidth");
1023 return ERROR_INT(
"pix not defined", procName, 0);
1030 pixSetWidth(
PIX *pix,
1033 PROCNAME(
"pixSetWidth");
1036 return ERROR_INT(
"pix not defined", procName, 1);
1039 return ERROR_INT(
"width must be >= 0", procName, 1);
1048 pixGetHeight(
const PIX *pix)
1050 PROCNAME(
"pixGetHeight");
1053 return ERROR_INT(
"pix not defined", procName, 0);
1060 pixSetHeight(
PIX *pix,
1063 PROCNAME(
"pixSetHeight");
1066 return ERROR_INT(
"pix not defined", procName, 1);
1069 return ERROR_INT(
"h must be >= 0", procName, 1);
1078 pixGetDepth(
const PIX *pix)
1080 PROCNAME(
"pixGetDepth");
1083 return ERROR_INT(
"pix not defined", procName, 0);
1090 pixSetDepth(
PIX *pix,
1093 PROCNAME(
"pixSetDepth");
1096 return ERROR_INT(
"pix not defined", procName, 1);
1098 return ERROR_INT(
"d must be >= 1", procName, 1);
1118 PROCNAME(
"pixGetDimensions");
1124 return ERROR_INT(
"pix not defined", procName, 1);
1125 if (pw) *pw = pix->
w;
1126 if (ph) *ph = pix->
h;
1127 if (pd) *pd = pix->
d;
1145 PROCNAME(
"pixSetDimensions");
1148 return ERROR_INT(
"pix not defined", procName, 1);
1149 if (w > 0) pixSetWidth(pix, w);
1150 if (h > 0) pixSetHeight(pix, h);
1151 if (d > 0) pixSetDepth(pix, d);
1167 PROCNAME(
"pixCopyDimensions");
1170 return ERROR_INT(
"pixd not defined", procName, 1);
1172 return ERROR_INT(
"pixs not defined", procName, 1);
1176 pixSetWidth(pixd, pixGetWidth(pixs));
1177 pixSetHeight(pixd, pixGetHeight(pixs));
1178 pixSetDepth(pixd, pixGetDepth(pixs));
1179 pixSetWpl(pixd, pixGetWpl(pixs));
1185 pixGetSpp(
const PIX *pix)
1187 PROCNAME(
"pixGetSpp");
1190 return ERROR_INT(
"pix not defined", procName, 0);
1216 PROCNAME(
"pixSetSpp");
1219 return ERROR_INT(
"pix not defined", procName, 1);
1221 return ERROR_INT(
"spp must be >= 1", procName, 1);
1239 PROCNAME(
"pixCopySpp");
1242 return ERROR_INT(
"pixd not defined", procName, 1);
1244 return ERROR_INT(
"pixs not defined", procName, 1);
1248 pixSetSpp(pixd, pixGetSpp(pixs));
1254 pixGetWpl(
const PIX *pix)
1256 PROCNAME(
"pixGetWpl");
1259 return ERROR_INT(
"pix not defined", procName, 0);
1268 PROCNAME(
"pixSetWpl");
1271 return ERROR_INT(
"pix not defined", procName, 1);
1279 pixGetRefcount(
const PIX *pix)
1281 PROCNAME(
"pixGetRefcount");
1284 return ERROR_INT(
"pix not defined", procName, 0);
1290 pixChangeRefcount(
PIX *pix,
1293 PROCNAME(
"pixChangeRefcount");
1296 return ERROR_INT(
"pix not defined", procName, 1);
1304 pixGetXRes(
const PIX *pix)
1306 PROCNAME(
"pixGetXRes");
1309 return ERROR_INT(
"pix not defined", procName, 0);
1315 pixSetXRes(
PIX *pix,
1318 PROCNAME(
"pixSetXRes");
1321 return ERROR_INT(
"pix not defined", procName, 1);
1329 pixGetYRes(
const PIX *pix)
1331 PROCNAME(
"pixGetYRes");
1334 return ERROR_INT(
"pix not defined", procName, 0);
1340 pixSetYRes(
PIX *pix,
1343 PROCNAME(
"pixSetYRes");
1346 return ERROR_INT(
"pix not defined", procName, 1);
1365 PROCNAME(
"pixGetResolution");
1367 if (pxres) *pxres = 0;
1368 if (pyres) *pyres = 0;
1369 if (!pxres && !pyres)
1370 return ERROR_INT(
"no output requested", procName, 1);
1372 return ERROR_INT(
"pix not defined", procName, 1);
1373 if (pxres) *pxres = pix->
xres;
1374 if (pyres) *pyres = pix->
yres;
1391 PROCNAME(
"pixSetResolution");
1394 return ERROR_INT(
"pix not defined", procName, 1);
1395 if (xres > 0) pix->
xres = xres;
1396 if (yres > 0) pix->
yres = yres;
1402 pixCopyResolution(
PIX *pixd,
1405 PROCNAME(
"pixCopyResolution");
1408 return ERROR_INT(
"pixs not defined", procName, 1);
1410 return ERROR_INT(
"pixd not defined", procName, 1);
1414 pixSetXRes(pixd, pixGetXRes(pixs));
1415 pixSetYRes(pixd, pixGetYRes(pixs));
1421 pixScaleResolution(
PIX *pix,
1425 l_float64 xres, yres;
1426 l_float64 maxres = 100000000.0;
1428 PROCNAME(
"pixScaleResolution");
1431 return ERROR_INT(
"pix not defined", procName, 1);
1432 if (xscale <= 0 || yscale <= 0)
1433 return ERROR_INT(
"invalid scaling ratio", procName, 1);
1435 xres = (l_float64)xscale * (l_float32)(pix->
xres) + 0.5;
1436 yres = (l_float64)yscale * (l_float32)(pix->
yres) + 0.5;
1437 pix->
xres = (l_uint32)L_MIN(xres, maxres);
1438 pix->
yres = (l_uint32)L_MIN(yres, maxres);
1444 pixGetInputFormat(
const PIX *pix)
1446 PROCNAME(
"pixGetInputFormat");
1449 return ERROR_INT(
"pix not defined", procName, 0);
1455 pixSetInputFormat(
PIX *pix,
1458 PROCNAME(
"pixSetInputFormat");
1461 return ERROR_INT(
"pix not defined", procName, 1);
1468 pixCopyInputFormat(
PIX *pixd,
1471 PROCNAME(
"pixCopyInputFormat");
1474 return ERROR_INT(
"pixs not defined", procName, 1);
1476 return ERROR_INT(
"pixd not defined", procName, 1);
1480 pixSetInputFormat(pixd, pixGetInputFormat(pixs));
1486 pixSetSpecial(
PIX *pix,
1489 PROCNAME(
"pixSetSpecial");
1492 return ERROR_INT(
"pix not defined", procName, 1);
1514 PROCNAME(
"pixGetText");
1517 return (
char *)ERROR_PTR(
"pix not defined", procName, NULL);
1537 const char *textstring)
1539 PROCNAME(
"pixSetText");
1542 return ERROR_INT(
"pix not defined", procName, 1);
1565 const char *textstring)
1569 PROCNAME(
"pixAddText");
1572 return ERROR_INT(
"pix not defined", procName, 1);
1576 LEPT_FREE(newstring);
1582 pixCopyText(
PIX *pixd,
1585 PROCNAME(
"pixCopyText");
1588 return ERROR_INT(
"pixs not defined", procName, 1);
1590 return ERROR_INT(
"pixd not defined", procName, 1);
1622 PROCNAME(
"pixGetTextCompNew");
1625 return (l_uint8 *)ERROR_PTR(
"pix not defined", procName, NULL);
1627 return decodeAscii85WithComp(str, strlen(str), psize);
1649 const l_uint8 *data,
1654 PROCNAME(
"pixSetTextCompNew");
1657 return ERROR_INT(
"pix not defined", procName, 1);
1665 pixGetColormap(
PIX *pix)
1667 PROCNAME(
"pixGetColormap");
1670 return (
PIXCMAP *)ERROR_PTR(
"pix not defined", procName, NULL);
1704 PROCNAME(
"pixSetColormap");
1707 return ERROR_INT(
"pix not defined", procName, 1);
1708 if (!colormap)
return 0;
1716 return ERROR_INT(
"colormap is not valid", procName, 1);
1732 PROCNAME(
"pixDestroyColormap");
1735 return ERROR_INT(
"pix not defined", procName, 1);
1737 if ((cmap = pix->
colormap) != NULL) {
1765 PROCNAME(
"pixGetData");
1768 return (l_uint32 *)ERROR_PTR(
"pix not defined", procName, NULL);
1790 PROCNAME(
"pixSetData");
1793 return ERROR_INT(
"pix not defined", procName, 1);
1819 l_int32 count, bytes;
1820 l_uint32 *data, *datas;
1822 PROCNAME(
"pixExtractData");
1825 return (l_uint32 *)ERROR_PTR(
"pixs not defined", procName, NULL);
1827 count = pixGetRefcount(pixs);
1832 bytes = 4 * pixGetWpl(pixs) * pixGetHeight(pixs);
1834 if ((data = (l_uint32 *)pixdata_malloc(bytes)) == NULL)
1835 return (l_uint32 *)ERROR_PTR(
"data not made", procName, NULL);
1836 memcpy(data, datas, bytes);
1862 PROCNAME(
"pixFreeData");
1865 return ERROR_INT(
"pix not defined", procName, 1);
1956 PROCNAME(
"pixGetLinePtrs");
1958 if (psize) *psize = 0;
1960 return (
void **)ERROR_PTR(
"pix not defined", procName, NULL);
1962 h = pixGetHeight(pix);
1963 if (psize) *psize = h;
1964 if ((lines = (
void **)LEPT_CALLOC(h,
sizeof(
void *))) == NULL)
1965 return (
void **)ERROR_PTR(
"lines not made", procName, NULL);
1966 wpl = pixGetWpl(pix);
1968 for (i = 0; i < h; i++)
1969 lines[i] = (
void *)(data + i * wpl);
1988 PROCNAME(
"pixSizesEqual");
1991 return ERROR_INT(
"pix1 and pix2 not both defined", procName, 0);
1996 if ((pixGetWidth(pix1) != pixGetWidth(pix2)) ||
1997 (pixGetHeight(pix1) != pixGetHeight(pix2)) ||
1998 (pixGetDepth(pix1) != pixGetDepth(pix2)))
2018 PROCNAME(
"pixMaxAspectRatio");
2021 return ERROR_INT(
"&ratio not defined", procName, 1);
2024 return ERROR_INT(
"pixs not defined", procName, 1);
2026 if (w == 0 || h == 0) {
2027 L_ERROR(
"invalid size: w = %d, h = %d\n", procName, w, h);
2031 *pratio = L_MAX((l_float32)h / (l_float32)w, (l_float32)w / (l_float32)h);
2039 extern const char *ImageFileFormatExtensions[];
2057 PROCNAME(
"pixPrintStreamInfo");
2060 return ERROR_INT(
"fp not defined", procName, 1);
2062 return ERROR_INT(
"pix not defined", procName, 1);
2065 fprintf(fp,
" Pix Info for %s:\n", text);
2066 fprintf(fp,
" width = %d, height = %d, depth = %d, spp = %d\n",
2067 pixGetWidth(pix), pixGetHeight(pix), pixGetDepth(pix),
2069 fprintf(fp,
" wpl = %d, data = %p, refcount = %d\n",
2070 pixGetWpl(pix), pix->
data, pixGetRefcount(pix));
2071 fprintf(fp,
" xres = %d, yres = %d\n", pixGetXRes(pix), pixGetYRes(pix));
2072 if ((cmap = pix->
colormap) != NULL)
2075 fprintf(fp,
" no colormap\n");
2076 informat = pixGetInputFormat(pix);
2077 fprintf(fp,
" input format: %d (%s)\n", informat,
2078 ImageFileFormatExtensions[informat]);
2079 if (pix->
text != NULL)
2080 fprintf(fp,
" text: %s\n", pix->
text);
void pixcmapDestroy(PIXCMAP **pcmap)
pixcmapDestroy()
l_ok pixcmapIsValid(const PIXCMAP *cmap, PIX *pix, l_int32 *pvalid)
pixcmapIsValid()
l_ok pixcmapWriteStream(FILE *fp, const PIXCMAP *cmap)
pixcmapWriteStream()
PIXCMAP * pixcmapCreate(l_int32 depth)
pixcmapCreate()
PIXCMAP * pixcmapCopy(const PIXCMAP *cmaps)
pixcmapCopy()
l_ok pixcmapAddColor(PIXCMAP *cmap, l_int32 rval, l_int32 gval, l_int32 bval)
pixcmapAddColor()
l_uint32 * pixGetData(PIX *pix)
pixGetData()
l_ok pixSetColormap(PIX *pix, PIXCMAP *colormap)
pixSetColormap()
l_ok pixResizeImageData(PIX *pixd, const PIX *pixs)
pixResizeImageData()
l_ok pixSetResolution(PIX *pix, l_int32 xres, l_int32 yres)
pixSetResolution()
l_ok pixDestroyColormap(PIX *pix)
pixDestroyColormap()
void setPixMemoryManager(alloc_fn allocator, dealloc_fn deallocator)
setPixMemoryManager()
void pixDestroy(PIX **ppix)
pixDestroy()
l_uint32 * pixExtractData(PIX *pixs)
pixExtractData()
l_int32 pixFreeData(PIX *pix)
pixFreeData()
l_ok pixGetDimensions(const PIX *pix, l_int32 *pw, l_int32 *ph, l_int32 *pd)
pixGetDimensions()
l_int32 pixSizesEqual(const PIX *pix1, const PIX *pix2)
pixSizesEqual()
l_ok pixSetDimensions(PIX *pix, l_int32 w, l_int32 h, l_int32 d)
pixSetDimensions()
static struct PixMemoryManager pix_mem_manager
PIX * pixCreateHeader(l_int32 width, l_int32 height, l_int32 depth)
pixCreateHeader()
l_ok pixCopyDimensions(PIX *pixd, const PIX *pixs)
pixCopyDimensions()
char * pixGetText(PIX *pix)
pixGetText()
l_ok pixSetText(PIX *pix, const char *textstring)
pixSetText()
l_ok pixCopySpp(PIX *pixd, const PIX *pixs)
pixCopySpp()
l_ok pixGetResolution(const PIX *pix, l_int32 *pxres, l_int32 *pyres)
pixGetResolution()
l_ok pixSetTextCompNew(PIX *pix, const l_uint8 *data, size_t size)
pixSetTextCompNew()
PIX * pixCopy(PIX *pixd, const PIX *pixs)
pixCopy()
l_ok pixSwapAndDestroy(PIX **ppixd, PIX **ppixs)
pixSwapAndDestroy()
l_ok pixTransferAllData(PIX *pixd, PIX **ppixs, l_int32 copytext, l_int32 copyformat)
pixTransferAllData()
static void pixFree(PIX *pix)
pixFree()
l_int32 pixSetData(PIX *pix, l_uint32 *data)
pixSetData()
l_uint8 * pixGetTextCompNew(PIX *pix, size_t *psize)
pixGetTextCompNew()
PIX * pixCreateTemplateNoInit(const PIX *pixs)
pixCreateTemplateNoInit()
PIX * pixCreateTemplate(const PIX *pixs)
pixCreateTemplate()
l_ok pixAddText(PIX *pix, const char *textstring)
pixAddText()
l_ok pixPrintStreamInfo(FILE *fp, const PIX *pix, const char *text)
pixPrintStreamInfo()
l_ok pixMaxAspectRatio(PIX *pixs, l_float32 *pratio)
pixMaxAspectRatio()
PIX * pixCreateNoInit(l_int32 width, l_int32 height, l_int32 depth)
pixCreateNoInit()
l_ok pixCopyColormap(PIX *pixd, const PIX *pixs)
pixCopyColormap()
PIX * pixCreateWithCmap(l_int32 width, l_int32 height, l_int32 depth, l_int32 initcolor)
pixCreateWithCmap()
PIX * pixCreate(l_int32 width, l_int32 height, l_int32 depth)
pixCreate()
PIX * pixClone(PIX *pixs)
pixClone()
void ** pixGetLinePtrs(PIX *pix, l_int32 *psize)
pixGetLinePtrs()
l_ok pixSetPadBits(PIX *pix, l_int32 val)
pixSetPadBits()
void(* dealloc_fn)(void *)
void *(* alloc_fn)(size_t)
struct PixColormap * colormap
l_ok stringReplace(char **pdest, const char *src)
stringReplace()
char * stringJoin(const char *src1, const char *src2)
stringJoin()