From:	SMTP%"tihor@acf4.NYU.EDU" 21-SEP-1990 19:54:12.41
To:	tihor@acf4.NYU.EDU
CC:	
Subj:	Re: Patch to PBMPLUS

Received: from acf4.NYU.EDU by ACF7.NYU.EDU with SMTP; 
          Fri, 21 Sep 1990 19:54:04 EDT
Received: by acf4.NYU.EDU (5.61/1.34)
	id AA06156; Fri, 21 Sep 90 19:52:09 -0400
Date: Fri, 21 Sep 90 19:52:09 -0400
From: tihor@acf4.NYU.EDU (Stephen Tihor)
Message-Id: <9009212352.AA06156@acf4.NYU.EDU>
To: tihor@acf4.NYU.EDU
Subject: Re: Patch to PBMPLUS
Newsgroups: vmsnet.sources,alt.graphics.pixutils,comp.os.vms
In-Reply-To: article <180.26f49ef9@mccall.com> of 17 Sep 90 06:01 EDT

/* acf4:vmsnet.sources / tp@mccall.com /  6:01 am  Sep 17, 1990 */
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
/* ---------- */

