198 #include <config_auto.h>
202 #include "allheaders.h"
237 #ifdef LEPTONICA_INTERCEPT_ALLOC
247 pixdata_malloc(
size_t size)
254 return LEPT_MALLOC(size);
259 pixdata_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",
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);
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);
500 l_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 pixd = (
PIX *)LEPT_CALLOC(1,
sizeof(
PIX));
543 pixSetWidth(pixd, width);
544 pixSetHeight(pixd, height);
545 pixSetDepth(pixd, depth);
546 pixSetWpl(pixd, wpl);
547 if (depth == 24 || depth == 32)
585 return (
PIX *)ERROR_PTR(
"pixs not defined", __func__, NULL);
613 L_WARNING(
"ptr address is null!\n", __func__);
617 if ((pix = *ppix) == NULL)
695 return (
PIX *)ERROR_PTR(
"pixs not defined", __func__, pixd);
700 bytes = 4 * pixGetWpl(pixs) * pixGetHeight(pixs);
705 return (
PIX *)ERROR_PTR(
"pixd not made", __func__, NULL);
706 memcpy(pixd->
data, pixs->
data, bytes);
717 return (
PIX *)ERROR_PTR(
"reallocation of data failed", __func__, NULL);
722 pixCopyResolution(pixd, pixs);
723 pixCopyInputFormat(pixd, pixs);
724 pixCopyText(pixd, pixs);
727 memcpy(pixd->
data, pixs->
data, bytes);
753 l_int32 w, h, d, wpl, bytes;
757 return ERROR_INT(
"pixs not defined", __func__, 1);
759 return ERROR_INT(
"pixd not defined", __func__, 1);
766 wpl = pixGetWpl(pixs);
768 if ((data = (l_uint32 *)pixdata_malloc(bytes)) == NULL)
769 return ERROR_INT(
"pixdata_malloc fail for data", __func__, 1);
772 pixSetWidth(pixd, w);
773 pixSetHeight(pixd, h);
774 pixSetDepth(pixd, d);
775 pixSetWpl(pixd, wpl);
777 pixCopyResolution(pixd, pixs);
803 return ERROR_INT(
"pixs not defined", __func__, 1);
805 return ERROR_INT(
"pixd not defined", __func__, 1);
808 if (pixGetDepth(pixs) != pixGetDepth(pixd))
809 return ERROR_INT(
"depths of pixs and pixd differ", __func__, 1);
812 if ((cmaps = pixs->
colormap) == NULL)
816 return ERROR_INT(
"cmap not valid", __func__, 1);
819 return ERROR_INT(
"cmapd not made", __func__, 1);
888 return ERROR_INT(
"&pixs not defined", __func__, 1);
889 if ((pixs = *ppixs) == NULL)
890 return ERROR_INT(
"pixs not defined", __func__, 1);
892 return ERROR_INT(
"pixd not defined", __func__, 1);
894 return ERROR_INT(
"pixd == pixs", __func__, 1);
901 pixd->
colormap = pixGetColormap(pixs);
909 nbytes = 4 * pixGetWpl(pixs) * pixGetHeight(pixs);
913 pixCopyText(pixd, pixs);
917 pixCopyResolution(pixd, pixs);
920 pixCopyInputFormat(pixd, pixs);
972 return ERROR_INT(
"&pixd not defined", __func__, 1);
974 return ERROR_INT(
"&pixs not defined", __func__, 1);
976 return ERROR_INT(
"pixs not defined", __func__, 1);
978 return ERROR_INT(
"&pixd == &pixs", __func__, 1);
991 pixGetWidth(
const PIX *pix)
994 return ERROR_INT(
"pix not defined", __func__, 0);
1001 pixSetWidth(
PIX *pix,
1005 return ERROR_INT(
"pix not defined", __func__, 1);
1008 return ERROR_INT(
"width must be >= 0", __func__, 1);
1017 pixGetHeight(
const PIX *pix)
1020 return ERROR_INT(
"pix not defined", __func__, 0);
1027 pixSetHeight(
PIX *pix,
1031 return ERROR_INT(
"pix not defined", __func__, 1);
1034 return ERROR_INT(
"h must be >= 0", __func__, 1);
1043 pixGetDepth(
const PIX *pix)
1046 return ERROR_INT(
"pix not defined", __func__, 0);
1053 pixSetDepth(
PIX *pix,
1057 return ERROR_INT(
"pix not defined", __func__, 1);
1059 return ERROR_INT(
"d must be >= 1", __func__, 1);
1083 return ERROR_INT(
"pix not defined", __func__, 1);
1084 if (pw) *pw = pix->
w;
1085 if (ph) *ph = pix->
h;
1086 if (pd) *pd = pix->
d;
1105 return ERROR_INT(
"pix not defined", __func__, 1);
1106 if (w > 0) pixSetWidth(pix, w);
1107 if (h > 0) pixSetHeight(pix, h);
1108 if (d > 0) pixSetDepth(pix, d);
1125 return ERROR_INT(
"pixd not defined", __func__, 1);
1127 return ERROR_INT(
"pixs not defined", __func__, 1);
1131 pixSetWidth(pixd, pixGetWidth(pixs));
1132 pixSetHeight(pixd, pixGetHeight(pixs));
1133 pixSetDepth(pixd, pixGetDepth(pixs));
1134 pixSetWpl(pixd, pixGetWpl(pixs));
1140 pixGetSpp(
const PIX *pix)
1143 return ERROR_INT(
"pix not defined", __func__, 0);
1170 return ERROR_INT(
"pix not defined", __func__, 1);
1172 return ERROR_INT(
"spp must be >= 1", __func__, 1);
1191 return ERROR_INT(
"pixd not defined", __func__, 1);
1193 return ERROR_INT(
"pixs not defined", __func__, 1);
1197 pixSetSpp(pixd, pixGetSpp(pixs));
1203 pixGetWpl(
const PIX *pix)
1206 return ERROR_INT(
"pix not defined", __func__, 0);
1216 return ERROR_INT(
"pix not defined", __func__, 1);
1224 pixGetXRes(
const PIX *pix)
1227 return ERROR_INT(
"pix not defined", __func__, 0);
1233 pixSetXRes(
PIX *pix,
1237 return ERROR_INT(
"pix not defined", __func__, 1);
1245 pixGetYRes(
const PIX *pix)
1248 return ERROR_INT(
"pix not defined", __func__, 0);
1254 pixSetYRes(
PIX *pix,
1258 return ERROR_INT(
"pix not defined", __func__, 1);
1277 if (pxres) *pxres = 0;
1278 if (pyres) *pyres = 0;
1279 if (!pxres && !pyres)
1280 return ERROR_INT(
"no output requested", __func__, 1);
1282 return ERROR_INT(
"pix not defined", __func__, 1);
1283 if (pxres) *pxres = pix->
xres;
1284 if (pyres) *pyres = pix->
yres;
1302 return ERROR_INT(
"pix not defined", __func__, 1);
1303 if (xres > 0) pix->
xres = xres;
1304 if (yres > 0) pix->
yres = yres;
1310 pixCopyResolution(
PIX *pixd,
1314 return ERROR_INT(
"pixs not defined", __func__, 1);
1316 return ERROR_INT(
"pixd not defined", __func__, 1);
1320 pixSetXRes(pixd, pixGetXRes(pixs));
1321 pixSetYRes(pixd, pixGetYRes(pixs));
1327 pixScaleResolution(
PIX *pix,
1331 l_float64 xres, yres;
1332 l_float64 maxres = 100000000.0;
1335 return ERROR_INT(
"pix not defined", __func__, 1);
1336 if (xscale <= 0 || yscale <= 0)
1337 return ERROR_INT(
"invalid scaling ratio", __func__, 1);
1339 xres = (l_float64)xscale * (l_float32)(pix->
xres) + 0.5;
1340 yres = (l_float64)yscale * (l_float32)(pix->
yres) + 0.5;
1341 pix->
xres = (l_uint32)L_MIN(xres, maxres);
1342 pix->
yres = (l_uint32)L_MIN(yres, maxres);
1348 pixGetInputFormat(
const PIX *pix)
1351 return ERROR_INT(
"pix not defined", __func__, 0);
1357 pixSetInputFormat(
PIX *pix,
1361 return ERROR_INT(
"pix not defined", __func__, 1);
1368 pixCopyInputFormat(
PIX *pixd,
1372 return ERROR_INT(
"pixs not defined", __func__, 1);
1374 return ERROR_INT(
"pixd not defined", __func__, 1);
1378 pixSetInputFormat(pixd, pixGetInputFormat(pixs));
1384 pixSetSpecial(
PIX *pix,
1388 return ERROR_INT(
"pix not defined", __func__, 1);
1411 return (
char *)ERROR_PTR(
"pix not defined", __func__, NULL);
1431 const char *textstring)
1434 return ERROR_INT(
"pix not defined", __func__, 1);
1457 const char *textstring)
1462 return ERROR_INT(
"pix not defined", __func__, 1);
1466 LEPT_FREE(newstring);
1472 pixCopyText(
PIX *pixd,
1476 return ERROR_INT(
"pixs not defined", __func__, 1);
1478 return ERROR_INT(
"pixd not defined", __func__, 1);
1511 return (l_uint8 *)ERROR_PTR(
"pix not defined", __func__, NULL);
1513 return decodeAscii85WithComp(str, strlen(str), psize);
1535 const l_uint8 *data,
1541 return ERROR_INT(
"pix not defined", __func__, 1);
1543 LEPT_FREE(pix->
text);
1544 pix->
text = encodeAscii85WithComp(data, size, &encodesize);
1550 pixGetColormap(
PIX *pix)
1553 return (
PIXCMAP *)ERROR_PTR(
"pix not defined", __func__, NULL);
1588 return ERROR_INT(
"pix not defined", __func__, 1);
1589 if (!colormap)
return 0;
1597 return ERROR_INT(
"colormap is not valid", __func__, 1);
1614 return ERROR_INT(
"pix not defined", __func__, 1);
1616 if ((cmap = pix->
colormap) != NULL) {
1645 return (l_uint32 *)ERROR_PTR(
"pix not defined", __func__, NULL);
1667 return ERROR_INT(
"pix not defined", __func__, 1);
1693 return ERROR_INT(
"pix not defined", __func__, 1);
1720 return ERROR_INT(
"pix not defined", __func__, 1);
1750 l_uint32 *data, *datas;
1753 return (l_uint32 *)ERROR_PTR(
"pixs not defined", __func__, NULL);
1759 bytes = 4 * pixGetWpl(pixs) * pixGetHeight(pixs);
1761 if ((data = (l_uint32 *)pixdata_malloc(bytes)) == NULL)
1762 return (l_uint32 *)ERROR_PTR(
"data not made", __func__, NULL);
1763 memcpy(data, datas, bytes);
1851 if (psize) *psize = 0;
1853 return (
void **)ERROR_PTR(
"pix not defined", __func__, NULL);
1855 h = pixGetHeight(pix);
1856 if (psize) *psize = h;
1857 if ((lines = (
void **)LEPT_CALLOC(h,
sizeof(
void *))) == NULL)
1858 return (
void **)ERROR_PTR(
"lines not made", __func__, NULL);
1859 wpl = pixGetWpl(pix);
1861 for (i = 0; i < h; i++)
1862 lines[i] = (
void *)(data + i * wpl);
1882 return ERROR_INT(
"pix1 and pix2 not both defined", __func__, 0);
1887 if ((pixGetWidth(pix1) != pixGetWidth(pix2)) ||
1888 (pixGetHeight(pix1) != pixGetHeight(pix2)) ||
1889 (pixGetDepth(pix1) != pixGetDepth(pix2)))
1910 return ERROR_INT(
"&ratio not defined", __func__, 1);
1913 return ERROR_INT(
"pixs not defined", __func__, 1);
1915 if (w == 0 || h == 0) {
1916 L_ERROR(
"invalid size: w = %d, h = %d\n", __func__, w, h);
1920 *pratio = L_MAX((l_float32)h / (l_float32)w, (l_float32)w / (l_float32)h);
1928 extern const char *ImageFileFormatExtensions[];
1947 return ERROR_INT(
"fp not defined", __func__, 1);
1949 return ERROR_INT(
"pix not defined", __func__, 1);
1952 fprintf(fp,
" Pix Info for %s:\n", text);
1953 fprintf(fp,
" width = %d, height = %d, depth = %d, spp = %d\n",
1954 pixGetWidth(pix), pixGetHeight(pix), pixGetDepth(pix),
1956 fprintf(fp,
" wpl = %d, data = %p, refcount = %d\n",
1958 fprintf(fp,
" xres = %d, yres = %d\n", pixGetXRes(pix), pixGetYRes(pix));
1959 if ((cmap = pix->
colormap) != NULL)
1962 fprintf(fp,
" no colormap\n");
1963 informat = pixGetInputFormat(pix);
1964 fprintf(fp,
" input format: %d (%s)\n", informat,
1965 ImageFileFormatExtensions[informat]);
1966 if (pix->
text != NULL)
1967 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_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()
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()