198#include <config_auto.h>
202#include "allheaders.h"
237#ifdef LEPTONICA_INTERCEPT_ALLOC
247pixdata_malloc(
size_t size)
254 return LEPT_MALLOC(size);
259pixdata_free(
void *ptr)
322 return (
PIX *)ERROR_PTR(
"pixd not made", __func__, NULL);
323 memset(pixd->
data, 0, 4LL * pixd->
wpl * pixd->
h);
353 return (
PIX *)ERROR_PTR(
"pixd not made", __func__, NULL);
354 wpl = pixGetWpl(pixd);
355 if ((data = (l_uint32 *)pixdata_malloc(4LL * wpl * height)) == NULL) {
357 return (
PIX *)ERROR_PTR(
"pixdata_malloc fail for data",
361 pixSetPadBits(pixd, 0);
385 return (
PIX *)ERROR_PTR(
"pixs not defined", __func__, NULL);
388 return (
PIX *)ERROR_PTR(
"pixd not made", __func__, NULL);
389 memset(pixd->
data, 0, 4LL * pixd->
wpl * pixd->
h);
418 return (
PIX *)ERROR_PTR(
"pixs not defined", __func__, NULL);
422 return (
PIX *)ERROR_PTR(
"pixd not made", __func__, NULL);
424 pixCopyResolution(pixd, pixs);
426 pixCopyText(pixd, pixs);
427 pixCopyInputFormat(pixd, pixs);
428 pixSetPadBits(pixd, 0);
459 if (depth != 2 && depth != 4 && depth != 8)
460 return (
PIX *)ERROR_PTR(
"depth not 2, 4 or 8 bpp", __func__, NULL);
462 if ((pix =
pixCreate(width, height, depth)) == NULL)
463 return (
PIX *)ERROR_PTR(
"pix not made", __func__, NULL);
464 cmap = pixcmapCreate(depth);
467 pixcmapAddColor(cmap, 0, 0, 0);
469 pixcmapAddColor(cmap, 255, 255, 255);
500l_uint64 wpl64, bignum;
503 if ((depth != 1) && (depth != 2) && (depth != 4) && (depth != 8)
504 && (depth != 16) && (depth != 24) && (depth != 32))
505 return (
PIX *)ERROR_PTR(
"depth must be {1, 2, 4, 8, 16, 24, 32}",
508 return (
PIX *)ERROR_PTR(
"width must be > 0", __func__, NULL);
510 return (
PIX *)ERROR_PTR(
"height must be > 0", __func__, NULL);
513 wpl64 = ((l_uint64)width * (l_uint64)depth + 31) / 32;
514 if (wpl64 > ((1LL << 24) - 1)) {
515 L_ERROR(
"requested w = %d, h = %d, d = %d\n",
516 __func__, width, height, depth);
517 return (
PIX *)ERROR_PTR(
"wpl >= 2^24", __func__, NULL);
519 wpl = (l_int32)wpl64;
520 bignum = 4LL * wpl * height;
521 if (bignum > ((1LL << 31) - 1)) {
522 L_ERROR(
"requested w = %d, h = %d, d = %d\n",
523 __func__, width, height, depth);
524 return (
PIX *)ERROR_PTR(
"requested bytes >= 2^31", __func__, NULL);
527#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
528 if (bignum > (1LL << 26)) {
529 L_ERROR(
"fuzzer requested > 64 MB; refused\n", __func__);
533 L_ERROR(
"fuzzer requested width > 20K; refused\n", __func__);
536 if (height > 20000) {
537 L_ERROR(
"fuzzer requested height > 20K; refused\n", __func__);
542 if ((pixd = (
PIX *)LEPT_CALLOC(1,
sizeof(
PIX))) == NULL)
543 return (
PIX *)ERROR_PTR(
"pixd not made", __func__, NULL);
544 pixSetWidth(pixd, width);
545 pixSetHeight(pixd, height);
546 pixSetDepth(pixd, depth);
547 pixSetWpl(pixd, wpl);
548 if (depth == 24 || depth == 32)
586 return (
PIX *)ERROR_PTR(
"pixs not defined", __func__, NULL);
614 L_WARNING(
"ptr address is null!\n", __func__);
618 if ((pix = *ppix) == NULL)
696 return (
PIX *)ERROR_PTR(
"pixs not defined", __func__, pixd);
701 bytes = 4 * pixGetWpl(pixs) * pixGetHeight(pixs);
706 return (
PIX *)ERROR_PTR(
"pixd not made", __func__, NULL);
707 memcpy(pixd->
data, pixs->
data, bytes);
718 return (
PIX *)ERROR_PTR(
"reallocation of data failed", __func__, NULL);
723 pixCopyResolution(pixd, pixs);
724 pixCopyInputFormat(pixd, pixs);
725 pixCopyText(pixd, pixs);
728 memcpy(pixd->
data, pixs->
data, bytes);
754l_int32 w, h, d, wpl, bytes;
758 return ERROR_INT(
"pixs not defined", __func__, 1);
760 return ERROR_INT(
"pixd not defined", __func__, 1);
767 wpl = pixGetWpl(pixs);
769 if ((data = (l_uint32 *)pixdata_malloc(bytes)) == NULL)
770 return ERROR_INT(
"pixdata_malloc fail for data", __func__, 1);
773 pixSetWidth(pixd, w);
774 pixSetHeight(pixd, h);
775 pixSetDepth(pixd, d);
776 pixSetWpl(pixd, wpl);
778 pixCopyResolution(pixd, pixs);
804 return ERROR_INT(
"pixs not defined", __func__, 1);
806 return ERROR_INT(
"pixd not defined", __func__, 1);
809 if (pixGetDepth(pixs) != pixGetDepth(pixd))
810 return ERROR_INT(
"depths of pixs and pixd differ", __func__, 1);
813 if ((cmaps = pixs->
colormap) == NULL)
815 pixcmapIsValid(cmaps, NULL, &valid);
817 return ERROR_INT(
"cmap not valid", __func__, 1);
819 if ((cmapd = pixcmapCopy(cmaps)) == NULL)
820 return ERROR_INT(
"cmapd not made", __func__, 1);
889 return ERROR_INT(
"&pixs not defined", __func__, 1);
890 if ((pixs = *ppixs) == NULL)
891 return ERROR_INT(
"pixs not defined", __func__, 1);
893 return ERROR_INT(
"pixd not defined", __func__, 1);
895 return ERROR_INT(
"pixd == pixs", __func__, 1);
902 pixd->
colormap = pixGetColormap(pixs);
910 nbytes = 4 * pixGetWpl(pixs) * pixGetHeight(pixs);
914 pixCopyText(pixd, pixs);
918 pixCopyResolution(pixd, pixs);
921 pixCopyInputFormat(pixd, pixs);
973 return ERROR_INT(
"&pixd not defined", __func__, 1);
975 return ERROR_INT(
"&pixs not defined", __func__, 1);
977 return ERROR_INT(
"pixs not defined", __func__, 1);
979 return ERROR_INT(
"&pixd == &pixs", __func__, 1);
992pixGetWidth(
const PIX *pix)
995 return ERROR_INT(
"pix not defined", __func__, 0);
1002pixSetWidth(
PIX *pix,
1006 return ERROR_INT(
"pix not defined", __func__, 1);
1009 return ERROR_INT(
"width must be >= 0", __func__, 1);
1018pixGetHeight(
const PIX *pix)
1021 return ERROR_INT(
"pix not defined", __func__, 0);
1028pixSetHeight(
PIX *pix,
1032 return ERROR_INT(
"pix not defined", __func__, 1);
1035 return ERROR_INT(
"h must be >= 0", __func__, 1);
1044pixGetDepth(
const PIX *pix)
1047 return ERROR_INT(
"pix not defined", __func__, 0);
1054pixSetDepth(
PIX *pix,
1058 return ERROR_INT(
"pix not defined", __func__, 1);
1060 return ERROR_INT(
"d must be >= 1", __func__, 1);
1084 return ERROR_INT(
"pix not defined", __func__, 1);
1085 if (pw) *pw = pix->
w;
1086 if (ph) *ph = pix->
h;
1087 if (pd) *pd = pix->
d;
1106 return ERROR_INT(
"pix not defined", __func__, 1);
1107 if (w > 0) pixSetWidth(pix, w);
1108 if (h > 0) pixSetHeight(pix, h);
1109 if (d > 0) pixSetDepth(pix, d);
1126 return ERROR_INT(
"pixd not defined", __func__, 1);
1128 return ERROR_INT(
"pixs not defined", __func__, 1);
1132 pixSetWidth(pixd, pixGetWidth(pixs));
1133 pixSetHeight(pixd, pixGetHeight(pixs));
1134 pixSetDepth(pixd, pixGetDepth(pixs));
1135 pixSetWpl(pixd, pixGetWpl(pixs));
1141pixGetSpp(
const PIX *pix)
1144 return ERROR_INT(
"pix not defined", __func__, 0);
1171 return ERROR_INT(
"pix not defined", __func__, 1);
1173 return ERROR_INT(
"spp must be >= 1", __func__, 1);
1192 return ERROR_INT(
"pixd not defined", __func__, 1);
1194 return ERROR_INT(
"pixs not defined", __func__, 1);
1198 pixSetSpp(pixd, pixGetSpp(pixs));
1204pixGetWpl(
const PIX *pix)
1207 return ERROR_INT(
"pix not defined", __func__, 0);
1217 return ERROR_INT(
"pix not defined", __func__, 1);
1225pixGetXRes(
const PIX *pix)
1228 return ERROR_INT(
"pix not defined", __func__, 0);
1238 return ERROR_INT(
"pix not defined", __func__, 1);
1246pixGetYRes(
const PIX *pix)
1249 return ERROR_INT(
"pix not defined", __func__, 0);
1259 return ERROR_INT(
"pix not defined", __func__, 1);
1278 if (pxres) *pxres = 0;
1279 if (pyres) *pyres = 0;
1280 if (!pxres && !pyres)
1281 return ERROR_INT(
"no output requested", __func__, 1);
1283 return ERROR_INT(
"pix not defined", __func__, 1);
1284 if (pxres) *pxres = pix->
xres;
1285 if (pyres) *pyres = pix->
yres;
1303 return ERROR_INT(
"pix not defined", __func__, 1);
1304 if (xres > 0) pix->
xres = xres;
1305 if (yres > 0) pix->
yres = yres;
1311pixCopyResolution(
PIX *pixd,
1315 return ERROR_INT(
"pixs not defined", __func__, 1);
1317 return ERROR_INT(
"pixd not defined", __func__, 1);
1321 pixSetXRes(pixd, pixGetXRes(pixs));
1322 pixSetYRes(pixd, pixGetYRes(pixs));
1328pixScaleResolution(
PIX *pix,
1332l_float64 xres, yres;
1333l_float64 maxres = 100000000.0;
1336 return ERROR_INT(
"pix not defined", __func__, 1);
1337 if (xscale <= 0 || yscale <= 0)
1338 return ERROR_INT(
"invalid scaling ratio", __func__, 1);
1340 xres = (l_float64)xscale * (l_float32)(pix->
xres) + 0.5;
1341 yres = (l_float64)yscale * (l_float32)(pix->
yres) + 0.5;
1342 pix->
xres = (l_uint32)L_MIN(xres, maxres);
1343 pix->
yres = (l_uint32)L_MIN(yres, maxres);
1349pixGetInputFormat(
const PIX *pix)
1352 return ERROR_INT(
"pix not defined", __func__, 0);
1358pixSetInputFormat(
PIX *pix,
1362 return ERROR_INT(
"pix not defined", __func__, 1);
1369pixCopyInputFormat(
PIX *pixd,
1373 return ERROR_INT(
"pixs not defined", __func__, 1);
1375 return ERROR_INT(
"pixd not defined", __func__, 1);
1379 pixSetInputFormat(pixd, pixGetInputFormat(pixs));
1385pixSetSpecial(
PIX *pix,
1389 return ERROR_INT(
"pix not defined", __func__, 1);
1412 return (
char *)ERROR_PTR(
"pix not defined", __func__, NULL);
1432 const char *textstring)
1435 return ERROR_INT(
"pix not defined", __func__, 1);
1437 stringReplace(&pix->
text, textstring);
1458 const char *textstring)
1463 return ERROR_INT(
"pix not defined", __func__, 1);
1465 newstring = stringJoin(
pixGetText(pix), textstring);
1466 stringReplace(&pix->
text, newstring);
1467 LEPT_FREE(newstring);
1473pixCopyText(
PIX *pixd,
1477 return ERROR_INT(
"pixs not defined", __func__, 1);
1479 return ERROR_INT(
"pixd not defined", __func__, 1);
1512 return (l_uint8 *)ERROR_PTR(
"pix not defined", __func__, NULL);
1514 return decodeAscii85WithComp(str, strlen(str), psize);
1536 const l_uint8 *data,
1542 return ERROR_INT(
"pix not defined", __func__, 1);
1544 LEPT_FREE(pix->
text);
1545 pix->
text = encodeAscii85WithComp(data, size, &encodesize);
1551pixGetColormap(
PIX *pix)
1554 return (
PIXCMAP *)ERROR_PTR(
"pix not defined", __func__, NULL);
1589 return ERROR_INT(
"pix not defined", __func__, 1);
1590 if (!colormap)
return 0;
1596 pixcmapIsValid(colormap, NULL, &valid);
1598 return ERROR_INT(
"colormap is not valid", __func__, 1);
1615 return ERROR_INT(
"pix not defined", __func__, 1);
1617 if ((cmap = pix->
colormap) != NULL) {
1618 pixcmapDestroy(&cmap);
1646 return (l_uint32 *)ERROR_PTR(
"pix not defined", __func__, NULL);
1668 return ERROR_INT(
"pix not defined", __func__, 1);
1694 return ERROR_INT(
"pix not defined", __func__, 1);
1721 return ERROR_INT(
"pix not defined", __func__, 1);
1751l_uint32 *data, *datas;
1754 return (l_uint32 *)ERROR_PTR(
"pixs not defined", __func__, NULL);
1760 bytes = 4 * pixGetWpl(pixs) * pixGetHeight(pixs);
1762 if ((data = (l_uint32 *)pixdata_malloc(bytes)) == NULL)
1763 return (l_uint32 *)ERROR_PTR(
"data not made", __func__, NULL);
1764 memcpy(data, datas, bytes);
1852 if (psize) *psize = 0;
1854 return (
void **)ERROR_PTR(
"pix not defined", __func__, NULL);
1856 h = pixGetHeight(pix);
1857 if (psize) *psize = h;
1858 if ((lines = (
void **)LEPT_CALLOC(h,
sizeof(
void *))) == NULL)
1859 return (
void **)ERROR_PTR(
"lines not made", __func__, NULL);
1860 wpl = pixGetWpl(pix);
1862 for (i = 0; i < h; i++)
1863 lines[i] = (
void *)(data + i * wpl);
1883 return ERROR_INT(
"pix1 and pix2 not both defined", __func__, 0);
1888 if ((pixGetWidth(pix1) != pixGetWidth(pix2)) ||
1889 (pixGetHeight(pix1) != pixGetHeight(pix2)) ||
1890 (pixGetDepth(pix1) != pixGetDepth(pix2)))
1911 return ERROR_INT(
"&ratio not defined", __func__, 1);
1914 return ERROR_INT(
"pixs not defined", __func__, 1);
1916 if (w == 0 || h == 0) {
1917 L_ERROR(
"invalid size: w = %d, h = %d\n", __func__, w, h);
1921 *pratio = L_MAX((l_float32)h / (l_float32)w, (l_float32)w / (l_float32)h);
1929extern const char *ImageFileFormatExtensions[];
1948 return ERROR_INT(
"fp not defined", __func__, 1);
1950 return ERROR_INT(
"pix not defined", __func__, 1);
1953 fprintf(fp,
" Pix Info for %s:\n", text);
1954 fprintf(fp,
" width = %d, height = %d, depth = %d, spp = %d\n",
1955 pixGetWidth(pix), pixGetHeight(pix), pixGetDepth(pix),
1957 fprintf(fp,
" wpl = %d, data = %p, refcount = %d\n",
1959 fprintf(fp,
" xres = %d, yres = %d\n", pixGetXRes(pix), pixGetYRes(pix));
1960 if ((cmap = pix->
colormap) != NULL)
1961 pixcmapWriteStream(fp, cmap);
1963 fprintf(fp,
" no colormap\n");
1964 informat = pixGetInputFormat(pix);
1965 fprintf(fp,
" input format: %d (%s)\n", informat,
1966 ImageFileFormatExtensions[informat]);
1967 if (pix->
text != NULL)
1968 fprintf(fp,
" text: %s\n", pix->
text);
l_uint8 * pixGetTextCompNew(PIX *pix, size_t *psize)
pixGetTextCompNew()
void ** pixGetLinePtrs(PIX *pix, l_int32 *psize)
pixGetLinePtrs()
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
l_ok pixCopyDimensions(PIX *pixd, const PIX *pixs)
pixCopyDimensions()
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 * pixClone(PIX *pixs)
pixClone()
l_ok pixSwapAndDestroy(PIX **ppixd, PIX **ppixs)
pixSwapAndDestroy()
l_ok pixTransferAllData(PIX *pixd, PIX **ppixs, l_int32 copytext, l_int32 copyformat)
pixTransferAllData()
PIX * pixCreateWithCmap(l_int32 width, l_int32 height, l_int32 depth, l_int32 initcolor)
pixCreateWithCmap()
static void pixFree(PIX *pix)
pixFree()
l_int32 pixSetData(PIX *pix, l_uint32 *data)
pixSetData()
PIX * pixCreateTemplate(const PIX *pixs)
pixCreateTemplate()
PIX * pixCreate(l_int32 width, l_int32 height, l_int32 depth)
pixCreate()
l_int32 pixFreeAndSetData(PIX *pix, l_uint32 *data)
pixFreeAndSetData()
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()
l_ok pixCopyColormap(PIX *pixd, const PIX *pixs)
pixCopyColormap()
PIX * pixCopy(PIX *pixd, const PIX *pixs)
pixCopy()
char * pixGetText(PIX *pix)
pixGetText()
PIX * pixCreateHeader(l_int32 width, l_int32 height, l_int32 depth)
pixCreateHeader()
PIX * pixCreateNoInit(l_int32 width, l_int32 height, l_int32 depth)
pixCreateNoInit()
l_uint32 * pixGetData(PIX *pix)
pixGetData()
PIX * pixCreateTemplateNoInit(const PIX *pixs)
pixCreateTemplateNoInit()
void *(* alloc_fn)(size_t)
void(* dealloc_fn)(void *)
struct PixColormap * colormap