--- ray/src/px/ciq.c 1989/04/07 13:30:09 1.3 +++ ray/src/px/ciq.c 2004/03/28 20:33:13 2.5 @@ -1,9 +1,6 @@ -/* Copyright 1988 Regents of the University of California */ - #ifndef lint -static char SCCSid[] = "$SunId$ LBL"; +static const char RCSid[] = "$Id: ciq.c,v 2.5 2004/03/28 20:33:13 schorsch Exp $"; #endif - /* CIQ - main program for color image quantization options for Floyd-Steinberg dithering by minimization of accumulated error @@ -13,6 +10,9 @@ Paul Heckbert 16 April 82, cleaned up 8 June 86 Greg Ward 1 March 88, modified for arbitrary picture sizes */ +#include + +#include "standard.h" #include "ciq.h" #define table(m,r,g,b) hist[m[0][r]|m[1][g]|m[2][b]] /* histogram/pvtable */ @@ -22,14 +22,24 @@ int hist[len]; /* list of frequencies or pixelvalues colormap color; /* quantization colormap */ int n; /* number of colors in it */ +#define ERRMAX 20 /* maximum allowed propagated error, + if >=255 then no clamping */ + +static void sample(colormap ocm); +static void convertmap(colormap in, colormap out); +static void draw_nodith(colormap ocm); +static void draw_dith(colormap ocm); + + /*----------------------------------------------------------------------*/ -ciq(dith,nw,synth,cm) -int dith; /* is dithering desired? 0=no, 1=yes */ -int nw; /* number of colors wanted in output image */ -int synth; /* synthesize colormap? 0=no, 1=yes */ -colormap cm; /* quantization colormap */ - /* read if synth=0; always written */ +void +ciq( + int dith, /* is dithering desired? 0=no, 1=yes */ + int nw, /* number of colors wanted in output image */ + int synth, /* synthesize colormap? 0=no, 1=yes */ + colormap cm /* quantization colormap */ +) /* read if synth=0; always written */ { int na,i; colormap ocm; @@ -40,7 +50,7 @@ colormap cm; /* quantization colormap */ if (synth) n = makecm(nw,&na); /* analyze histogram and synthesize colormap */ else { - bcopy(cm,color,sizeof color); + memcpy((void *)color,(void *)cm,sizeof color); n = nw; na = 0; for (i=0; ir,lp->g,lp->b)++; } - free((char *)line); + free((void *)line); } -convertmap(in,out) /* convert to shifted color map */ -register colormap in,out; +static void +convertmap( /* convert to shifted color map */ + register colormap in, + register colormap out +) { register int j; @@ -96,8 +112,10 @@ register colormap in,out; } } -draw_nodith(ocm) /* quantize without dithering */ -colormap ocm; /* colormap for orig */ +static void +draw_nodith( /* quantize without dithering */ + colormap ocm /* colormap for orig */ +) { register int x,y; register rgbpixel *lp; @@ -115,12 +133,14 @@ colormap ocm; /* colormap for orig */ oline[x] = table(map,lp->r,lp->g,lp->b); picwriteline(y,oline); } - free((char *)iline); - free((char *)oline); + free((void *)iline); + free((void *)oline); } -draw_dith(ocm) /* quantize with dithering */ -colormap ocm; /* colormap for orig */ +static void +draw_dith( /* quantize with dithering */ + colormap ocm /* colormap for orig */ +) { register int *p,*q,rr,gg,bb,v,x,y,*P,*Q,*R; int *buf; @@ -139,9 +159,16 @@ colormap ocm; /* colormap for orig */ Q[0] = Q[1] = Q[2] = 0; picreadline3(y,iline); for (p=P, q=Q, x=0; xERRMAX) rr = ERRMAX; + gg = p[1]; + if (gg<-ERRMAX) gg = -ERRMAX; else if (gg>ERRMAX) gg = ERRMAX; + bb = p[2]; + if (bb<-ERRMAX) bb = -ERRMAX; else if (bb>ERRMAX) bb = ERRMAX; + /* now rr,gg,bb is propagated color */ + rr += ocm[0][iline[x].r]; + gg += ocm[1][iline[x].g]; /* ideal */ + bb += ocm[2][iline[x].b]; if (rr<0) rr = 0; else if (rr>255) rr = 255; if (gg<0) gg = 0; else if (gg>255) gg = 255; if (bb<0) bb = 0; else if (bb>255) bb = 255; @@ -158,7 +185,7 @@ colormap ocm; /* colormap for orig */ } picwriteline(y,oline); } - free((char *)iline); - free((char *)oline); - free((char *)buf); + free((void *)iline); + free((void *)oline); + free((void *)buf); }