From 9f6b126f9bbea66c65080f30bd2fef5c2aed1adc Mon Sep 17 00:00:00 2001 From: giraffedata Date: Mon, 27 Nov 2006 07:35:43 +0000 Subject: pamx: fix bug: incorrect display of one-plane input image git-svn-id: http://svn.code.sf.net/p/netpbm/code/trunk@148 9d0c8265-081b-0410-96cb-a4ca84ce46f8 --- other/pamx/pamx.c | 50 ++++++++++++++++++++++++++------------------------ 1 file changed, 26 insertions(+), 24 deletions(-) (limited to 'other/pamx') diff --git a/other/pamx/pamx.c b/other/pamx/pamx.c index 8a64fec4..c6503d5e 100644 --- a/other/pamx/pamx.c +++ b/other/pamx/pamx.c @@ -175,35 +175,32 @@ errorHandler(Display * const disp, -static void -fillRow1(struct pam * const pamP, - tuple * const tuplerow, - unsigned char ** const pP) { - - unsigned int col; - - for (col = 0; col < pamP->width; ++col) { - unsigned int plane; - for (plane = 0; plane < pamP->depth; ++plane) - *(*pP)++ = - pnm_scalesample(tuplerow[col][0], pamP->maxval, 255); - } -} - - +enum usableDepth {DEPTH_1, DEPTH_3}; static void -fillRow3(struct pam * const pamP, - tuple * const tuplerow, - unsigned char ** const pP) { +fillRow(struct pam * const pamP, + tuple * const tuplerow, + enum usableDepth const depth, + unsigned char ** const pP) { +/*---------------------------------------------------------------------------- + Add one row to the 24-bit truecolor image data at *pP, and advance + *pP just past that row. + Use either the first plane or the first 3 planes of tuplerow[] + for its contents, according to 'depth'. +-----------------------------------------------------------------------------*/ unsigned int col; for (col = 0; col < pamP->width; ++col) { + /* Truecolor image data has 3 bytes per pixel, one each for + red, green, and blue. + */ unsigned int plane; - for (plane = 0; plane < pamP->depth; ++plane) + for (plane = 0; plane < 3; ++plane) { + unsigned int const tuplePlane = depth == DEPTH_3 ? plane : 0; *(*pP)++ = - pnm_scalesample(tuplerow[col][plane], pamP->maxval, 255); + pnm_scalesample(tuplerow[col][tuplePlane], pamP->maxval, 255); + } } } @@ -218,7 +215,7 @@ loadPamImage(FILE * const ifP, unsigned int row; tuple * tuplerow; unsigned char * p; - enum {DEPTH_1, DEPTH_3} depth; + enum usableDepth depth; pnm_readpaminit(ifP, &pam, PAM_STRUCT_SIZE(tuple_type)); @@ -238,12 +235,17 @@ loadPamImage(FILE * const ifP, for (row = 0; row < pam.height; ++row) { pnm_readpamrow(&pam, tuplerow); + /* This semantically wasteful code allows a dumb compiler + optimizer to recognize that the depth is constant and + therefore not generate code that checks the depth every + time it processes a sample. + */ switch (depth) { case DEPTH_3: - fillRow3(&pam, tuplerow, &p); + fillRow(&pam, tuplerow, DEPTH_3, &p); break; case DEPTH_1: - fillRow1(&pam, tuplerow, &p); + fillRow(&pam, tuplerow, DEPTH_1, &p); break; } } -- cgit 1.4.1