| 1 | 
greg | 
1.2 | 
/* Copyright 1988 Regents of the University of California */ | 
| 2 | 
greg | 
1.1 | 
 | 
| 3 | 
  | 
  | 
#ifndef lint | 
| 4 | 
  | 
  | 
static char SCCSid[] = "$SunId$ LBL"; | 
| 5 | 
  | 
  | 
#endif | 
| 6 | 
greg | 
1.2 | 
 | 
| 7 | 
  | 
  | 
/* | 
| 8 | 
greg | 
1.1 | 
 *  biq.c - simple greyscale quantization. | 
| 9 | 
  | 
  | 
 * | 
| 10 | 
  | 
  | 
 *      9/19/88 | 
| 11 | 
  | 
  | 
 */ | 
| 12 | 
  | 
  | 
 | 
| 13 | 
greg | 
1.5 | 
#include "standard.h" | 
| 14 | 
greg | 
1.1 | 
#include "ciq.h" | 
| 15 | 
  | 
  | 
 | 
| 16 | 
  | 
  | 
 | 
| 17 | 
  | 
  | 
biq(dith,nw,synth,cm) | 
| 18 | 
  | 
  | 
int dith;               /* is dithering desired? 0=no, 1=yes */ | 
| 19 | 
  | 
  | 
int nw;                 /* number of colors wanted in output image */ | 
| 20 | 
  | 
  | 
int synth;              /* synthesize colormap? 0=no, 1=yes */ | 
| 21 | 
  | 
  | 
colormap cm;            /* quantization colormap */ | 
| 22 | 
  | 
  | 
                        /* read if synth=0; always written */ | 
| 23 | 
  | 
  | 
{ | 
| 24 | 
  | 
  | 
    colormap ocm; | 
| 25 | 
  | 
  | 
 | 
| 26 | 
  | 
  | 
    picreadcm(ocm);     /* read original picture's colormap (usually identity)*/ | 
| 27 | 
  | 
  | 
 | 
| 28 | 
  | 
  | 
    for (n = 0; n < nw; n++)                    /* also sets n to nw */ | 
| 29 | 
  | 
  | 
        color[0][n] = color[1][n] = color[2][n] = (n*256L+128)/nw; | 
| 30 | 
  | 
  | 
 | 
| 31 | 
  | 
  | 
    picwritecm(color); | 
| 32 | 
  | 
  | 
 | 
| 33 | 
  | 
  | 
    draw_grey(ocm); | 
| 34 | 
  | 
  | 
 | 
| 35 | 
greg | 
1.4 | 
    bcopy((char *)color,(char *)cm,sizeof color); | 
| 36 | 
greg | 
1.1 | 
} | 
| 37 | 
  | 
  | 
 | 
| 38 | 
  | 
  | 
/*----------------------------------------------------------------------*/ | 
| 39 | 
  | 
  | 
 | 
| 40 | 
  | 
  | 
draw_grey(ocm) | 
| 41 | 
  | 
  | 
colormap ocm; | 
| 42 | 
  | 
  | 
{ | 
| 43 | 
  | 
  | 
    register rgbpixel *linin; | 
| 44 | 
  | 
  | 
    register pixel *linout; | 
| 45 | 
greg | 
1.3 | 
    rgbpixel intmp; | 
| 46 | 
  | 
  | 
    int outtmp; | 
| 47 | 
greg | 
1.1 | 
    int y; | 
| 48 | 
  | 
  | 
    register int x; | 
| 49 | 
  | 
  | 
 | 
| 50 | 
  | 
  | 
    linin = line3alloc(xmax); | 
| 51 | 
  | 
  | 
    linout = linealloc(xmax); | 
| 52 | 
  | 
  | 
 | 
| 53 | 
  | 
  | 
    for (y = 0; y < ymax; y++) { | 
| 54 | 
  | 
  | 
        picreadline3(y, linin); | 
| 55 | 
  | 
  | 
        for (x = 0; x < xmax; x++) { | 
| 56 | 
greg | 
1.3 | 
                intmp.r = ocm[0][linin[x].r]; | 
| 57 | 
  | 
  | 
                intmp.g = ocm[1][linin[x].g]; | 
| 58 | 
  | 
  | 
                intmp.b = ocm[2][linin[x].b]; | 
| 59 | 
  | 
  | 
                outtmp = rgb_bright(&intmp); | 
| 60 | 
  | 
  | 
                linout[x] = (outtmp*n+n/2)/256; | 
| 61 | 
greg | 
1.1 | 
        } | 
| 62 | 
  | 
  | 
        picwriteline(y, linout); | 
| 63 | 
  | 
  | 
    } | 
| 64 | 
  | 
  | 
    free((char *)linin); | 
| 65 | 
  | 
  | 
    free((char *)linout); | 
| 66 | 
  | 
  | 
} |