From 40a120115a9201d590d90d7a62f31dbcb006bdb0 Mon Sep 17 00:00:00 2001 From: giraffedata Date: Sat, 12 May 2018 20:00:11 +0000 Subject: Add pnm_colorspec_*, pnm_parsecolor2, pnm_allocpamtuplen, pnm_freepamtuplen. Make normalized tuple functions respect pam.allocation_depth git-svn-id: http://svn.code.sf.net/p/netpbm/code/trunk@3250 9d0c8265-081b-0410-96cb-a4ca84ce46f8 --- lib/libpamn.c | 42 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 40 insertions(+), 2 deletions(-) (limited to 'lib/libpamn.c') diff --git a/lib/libpamn.c b/lib/libpamn.c index ce844c7f..65c8979f 100644 --- a/lib/libpamn.c +++ b/lib/libpamn.c @@ -23,6 +23,43 @@ +static unsigned int +allocationDepth(const struct pam * const pamP) { + + unsigned int retval; + + if (pamP->len >= PAM_STRUCT_SIZE(allocation_depth)) { + if (pamP->allocation_depth == 0) + retval = pamP->depth; + else { + if (pamP->depth > pamP->allocation_depth) + pm_error("'allocationDepth' (%u) is smaller than 'depth' (%u)", + pamP->allocation_depth, pamP->depth); + retval = pamP->allocation_depth; + } + } else + retval = pamP->depth; + return retval; +} + + + +tuplen +pnm_allocpamtuplen(const struct pam * const pamP) { + + tuplen retval; + + retval = malloc(allocationDepth(pamP) * sizeof(retval[0])); + + if (retval == NULL) + pm_error("Out of memory allocating %u-plane normalized tuple", + allocationDepth(pamP)); + + return retval; +} + + + static void allocpamrown(const struct pam * const pamP, tuplen ** const tuplerownP, @@ -32,7 +69,7 @@ allocpamrown(const struct pam * const pamP, overflow will not occur in our calculations. NOTE: pnm_readpaminit() ensures this assumption is valid. -----------------------------------------------------------------------------*/ - int const bytes_per_tuple = pamP->depth * sizeof(samplen); + int const bytes_per_tuple = allocationDepth(pamP) * sizeof(samplen); tuplen * tuplerown; const char * error; @@ -47,7 +84,8 @@ allocpamrown(const struct pam * const pamP, pm_asprintf(&error, "Out of memory allocating space for a tuple row of" "%u tuples by %u samples per tuple " "by %u bytes per sample.", - pamP->width, pamP->depth, (unsigned)sizeof(samplen)); + pamP->width, allocationDepth(pamP), + (unsigned)sizeof(samplen)); else { /* Now we initialize the pointers to the individual tuples to make this a regulation C two dimensional array. -- cgit 1.4.1