From 1f2aae5b55e42ed8fa247785ee896f750042a12d Mon Sep 17 00:00:00 2001 From: giraffedata Date: Wed, 19 May 2010 03:11:23 +0000 Subject: fix assertion on 64 bit git-svn-id: http://svn.code.sf.net/p/netpbm/code/trunk@1215 9d0c8265-081b-0410-96cb-a4ca84ce46f8 --- lib/libpam.c | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) (limited to 'lib/libpam.c') diff --git a/lib/libpam.c b/lib/libpam.c index 6e158dcd..3c5e5192 100644 --- a/lib/libpam.c +++ b/lib/libpam.c @@ -755,13 +755,26 @@ pnm_readpaminitrestaspnm(FILE * const fileP, unsigned int pnm_bytespersample(sample const maxval) { +/*---------------------------------------------------------------------------- + Return the number of bytes per sample in the PAM raster of a PAM image + with maxval 'maxval'. It's defined to be the minimum number of bytes + needed for that maxval, i.e. 1 for maxval < 256, 2 otherwise. +-----------------------------------------------------------------------------*/ + + /* The PAM format requires maxval to be greater than zero and less than + 1<<16, but since that is a largely arbitrary restriction, we don't want + to rely on it. + */ - assert(sizeof(maxval) * 8 <= 32); + unsigned int i; + sample a; - if (maxval >> 8 == 0) return 1; - else if (maxval >> 16 == 0) return 2; - else if (maxval >> 24 == 0) return 3; - else return 4; + for (i = 0, a = maxval; i < sizeof(maxval)/8; ++i) { + if (a == 0) + return i; + a >>= 8; + } + return 0; /* silence compiler warning */ } -- cgit 1.4.1