diff options
-rw-r--r-- | doc/HISTORY | 2 | ||||
-rw-r--r-- | lib/libpam.c | 23 |
2 files changed, 19 insertions, 6 deletions
diff --git a/doc/HISTORY b/doc/HISTORY index 493b04a2..29c39ed1 100644 --- a/doc/HISTORY +++ b/doc/HISTORY @@ -25,7 +25,7 @@ not yet BJH Release 10.51.00 libnetpbm, various PBM programs: Use SSE insted of MMX. Thanks Prophet of the Way <afu@wta.att.ne.jp>. - libnetpbm: remove bogus assertion. + pnm_bytespersample(): fix bogus assertion on 64 bit system. pnmtops: fix bug: 12 bits per sample output when 8 would do. Introduced in 10.40. 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 */ } |