diff options
Diffstat (limited to 'lib/libpam.c')
-rw-r--r-- | lib/libpam.c | 23 |
1 files changed, 18 insertions, 5 deletions
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 */ } |