From:	CRDGW2::CRDGW2::MRGATE::"SMTP::CRVAX.SRI.COM::RELAY-INFO-VAX" 18-SEP-1990 05:21:54.16
To:	MRGATE::"ARISIA::EVERHART"
CC:	
Subj:	Patch to PBMPLUS

Received:  by crdgw1.ge.com (5.57/GE 1.73)
	 id AA11090; Mon, 17 Sep 90 21:44:01 EDT
Received: From UCBVAX.BERKELEY.EDU by CRVAX.SRI.COM with TCP; Mon, 17 SEP 90 12:28:14 PDT
Received: by ucbvax.Berkeley.EDU (5.63/1.42)
	id AA08870; Mon, 17 Sep 90 12:08:45 -0700
Received: from USENET by ucbvax.Berkeley.EDU with netnews
	for info-vax@kl.sri.com (info-vax@kl.sri.com)
	(contact usenet@ucbvax.Berkeley.EDU if you have questions)
Date: 17 Sep 90 10:01:28 GMT
From: maverick.ksu.ksu.edu!deimos.cis.ksu.edu!mccall!tp@rutgers.edu
Organization: The McCall Pattern Co., Manhattan, KS, USA
Subject: Patch to PBMPLUS
Message-Id: <180.26f49ef9@mccall.com>
Sender: info-vax-request@kl.sri.com
To: info-vax@kl.sri.com

Submitted-by: tp@mccall.com (Terry Poot)
Archive-name: pbmplus/patch3-part1

This is one of those "I don't know how it worked before!" patches, so it
may be useful to non-VMS people. I _know_ it is needed on VMS.

This patch fixes a divide by zero bug in ppmquant. When using the
average-pixels method, it divides by the sum of the pixel values. If that
sum is 0, it aborts (at least on VMS). I have modified it to skip the
division and set the r, g, and b values to zero in this case. It works for
me. If I read the code right, this should happen whenever there is a black
region large enough to completely enclose the sampling box used, and the
division would be 0/0. The fix simply sets it to black, which is the
correct answer anyway.

*** ppmquant.c_orig
--- ppmquant.c
**************
*** 531,542
  	    b += PPM_GETB( chv[indx + i].color ) * chv[indx + i].value;
  	    sum += chv[indx + i].value;
  	    }
! 	r = r / sum;
! 	if ( r > maxval ) r = maxval;	/* avoid math errors */
! 	g = g / sum;
! 	if ( g > maxval ) g = maxval;
! 	b = b / sum;
! 	if ( b > maxval ) b = maxval;
  	PPM_ASSIGN( colormap[bi].color, r, g, b );
  #endif /*REP_AVERAGE_PIXELS*/
  	}
--- 531,549 -----
  	    b += PPM_GETB( chv[indx + i].color ) * chv[indx + i].value;
  	    sum += chv[indx + i].value;
  	    }
! 	if(sum)
! 	    {
! 	    r = r / sum;
! 	    if ( r > maxval ) r = maxval;	/* avoid math errors */
! 	    g = g / sum;
! 	    if ( g > maxval ) g = maxval;
! 	    b = b / sum;
! 	    if ( b > maxval ) b = maxval;
! 	    }
! 	else
! 	    {
! 	    r = g = b = 0;
! 	    }
  	PPM_ASSIGN( colormap[bi].color, r, g, b );
  #endif /*REP_AVERAGE_PIXELS*/
  	}


-- 
Terry Poot <tp@mccall.com>                The McCall Pattern Company
(uucp: ...!rutgers!ksuvax1!mccall!tp)     615 McCall Road
(800)255-2762, in KS (913)776-4041        Manhattan, KS 66502, USA

