30 char rcsid[] =
"$Header: /tmp_mnt/n/itn/hendrix/u/spencer/RCS/rlequant.c,v 3.0.1.8 1992/04/30 14:13:13 spencer Exp spencer $";
40 #define Quantize(x) (x >> shift)
43 #define CHECK_MALLOC(type, ptr, size, desc)
44 if ( ((ptr) = (type *)malloc( size )) == NULL
)
46 fprintf( stderr
, "%s: Can't allocate memory for %s.\n",
65 #define PROCESS_HIST 3
144 char *infname = NULL,
161 int rle_cnt, rle_err, width, height, shift;
164 FILE *outfile = stdout;
166 rle_hdr in_hdr, out_hdr;
171 rle_pixel *alpha = NULL, *red, *green, *blue;
172 rle_pixel *img_red, *img_green, *img_blue;
184 "% b%-bits!d c%- d%- f%- i%-cubeside!d m%- n%-colors!d r%-rlemap!s o%-outfile!s infile%s",
185 &bflag
, &bits
, &cflag
, &dflag
, &fflag
,
187 &mflag
, &nflag
, &colors_in
,
189 &oflag
, &outfname
, &infname
) == 0 )
193 if ( cubeside <= 1 || cubeside > 6 )
196 "%s: Cubeside (%d given) must be be >= 2 and <= 6.\n",
198 fprintf( stderr,
"%s: Cubeside set to 0.",
MY_NAME );
202 cubesize = cubeside * cubeside * cubeside;
213 if ( (map_comment =
rle_getcom( "color_map_length", &map_hdr
)) )
214 mapsize = atoi( map_comment );
224 if ( bits <= 0 || bits > 8 )
226 fprintf( stderr,
"%s: The bits argument must be >0 and <= 8.\n",
234 colors_in = 256 - mapsize - cubesize;
240 if ( colors_in + mapsize + cubesize <= 0 ||
241 colors_in + mapsize + cubesize > 256 )
244 "%s: colors + cubeside^3 + rlemap size (%d + %d + %d = %d ) \n\
245 \tmust be >0 and <= 256.\n",
246 MY_NAME, colors_in, cubesize, mapsize,
247 colors_in + cubesize + mapsize );
252 if ( mapsize > 0 || cubesize > 0 )
265 if ( mflag && (!cflag) && ftell( in_hdr
.rle_file ) < 0 )
268 "%s: Piped input with -m, colors will be chosen from first image only.\n",
291 if ( colors_in == 0 )
293 else if ( mflag > 0 )
325 "%s: Input image %d has only %d color%s, faking the other %d.\n",
342 width = in_hdr
.xmax + 1;
346 entries = width * height;
350 &red
, &green
, &blue
, &alpha
,
351 &img_red
, &img_green
, &img_blue
);
354 if ( !(cflag && state ==
OUTPUT) )
356 red
, green
, blue
, alpha
,
357 img_red
, img_green
, img_blue
, rows
);
362 colormap
, colors_in
, bits
,
363 rgbmap
, fflag
, state
);
374 for ( i = 0; i < mapsize; i++, colors++ )
376 colormap[0][colors] = inputmap[0][i];
377 colormap[1][colors] = inputmap[1][i];
378 colormap[2][colors] = inputmap[2][i];
386 if ( mapsize + cubesize > 0 )
388 unsigned long *dist_buf;
392 (1L << (3*bits)) *
sizeof(
long),
412 red
, green
, blue
, alpha
,
413 img_red
, img_green
, img_blue
, rgbmap
, colormap
,
418 if (mflag < 0 && state ==
NORMAL)
424 img_red
, img_green
, img_blue
, rows
, outrows
);
440 colormap
, colors_in
, bits
,
449 for ( i = 0; i < mapsize; i++, colors++ )
451 colormap[colors][0] = inputmap[i][0];
452 colormap[colors][1] = inputmap[i][1];
453 colormap[colors][2] = inputmap[i][2];
490 rle_hdr *in_hdr, *out_hdr;
526 unsigned long entries;
529 rle_pixel **red, **green, **blue, **alpha;
530 rle_pixel **img_red, **img_green, **img_blue;
538 fprintf( stderr,
"%s: Unable to allocate scanline memory.\n",
569 *img_green = *img_red;
575 *img_blue = *img_green;
583 int width, shift, dflag;
585 rle_pixel *img_red, *img_green, *img_blue;
589 register rle_pixel *irp = NULL, *igp = NULL, *ibp = NULL;
610 for ( x = 0; x < width; x++ )
638 int colors, cubeside;
640 register int r, g, b, i;
644 for ( r = 0; r < cubeside; r++ )
645 for ( g = 0; g < cubeside; g++ )
646 for ( b = 0; b < cubeside; b++ )
648 i = colors + (r * cubeside + g) * cubeside + b;
649 colormap[0][i] = (r * 255) / (cubeside - 1);
650 colormap[1][i] = (g * 255) / (cubeside - 1);
651 colormap[2][i] = (b * 255) / (cubeside - 1);
671 for ( y = 0; y < 3; y++ )
673 for ( x = 0; x < colors; x++ )
674 hdr
->cmap[y * 256 + x] = colormap[y][x] << 8;
675 for ( ; x < 256; x++ )
676 hdr
->cmap[y * 256 + x] = 0;
680 sprintf( buf,
"color_map_length=%d", colors );
692 int width, height, bits, dflag;
694 rle_pixel *img_red, *img_green, *img_blue;
699 int shift = 8 - bits;
715 for ( y = 0; y < height; y++ )
723 for ( x = 0; x < width; x++, rp++, gp++, bp++ )
724 outrows[1][x] = rgbmap[(((*rp<<bits)|*gp)<<bits)|*bp];
730 register short *thisptr, *nextptr ;
731 int lastline, lastpixel ;
732 short *thisline, *nextline, *tmpptr;
739 CHECK_MALLOC(
short, thisline, width * 3 *
sizeof(
short),
741 CHECK_MALLOC(
short, nextline, width * 3 *
sizeof(
short),
744 for (x=0; x < width; x++)
746 *nextptr++ = *irp++ ;
747 *nextptr++ = *igp++ ;
748 *nextptr++ = *ibp++ ;
750 for (y=0; y < height; y++)
754 thisline = nextline ;
756 lastline = (y == height - 1) ;
760 for (x=0; x < width; x++)
762 *nextptr++ = *irp++ ;
763 *nextptr++ = *igp++ ;
764 *nextptr++ = *ibp++ ;
771 for(x=0; x < width ; x++)
773 int rval, gval, bval, color ;
776 lastpixel = (x == width - 1) ;
785 if( rval < 0 ) rval = 0 ;
786 else if( rval > 255 ) rval = 255 ;
787 if( gval < 0 ) gval = 0 ;
788 else if( gval > 255 ) gval = 255 ;
789 if( bval < 0 ) bval = 0 ;
790 else if( bval > 255 ) bval = 255 ;
796 color = rgbmap[(((r2<<bits)|g2)<<bits)|b2];
797 outrows[1][x] = color;
799 rval -= colormap[0][color];
800 gval -= colormap[1][color];
801 bval -= colormap[2][color];
805 thisptr[0] += rval * 7 / 16 ;
806 thisptr[1] += gval * 7 / 16 ;
807 thisptr[2] += bval * 7 / 16 ;
813 nextptr[-3] += rval * 3 / 16 ;
814 nextptr[-2] += gval * 3 / 16 ;
815 nextptr[-1] += bval * 3 / 16 ;
817 nextptr[0] += rval * 5 / 16 ;
818 nextptr[1] += gval * 5 / 16 ;
819 nextptr[2] += bval * 5 / 16 ;
822 nextptr[3] += rval / 16 ;
823 nextptr[4] += gval / 16 ;
824 nextptr[5] += bval / 16 ;
850 rle_pixel *img_red, *img_green, *img_blue;
FILE * rle_open_f(char *prog_name, char *file_name, char *mode)
static void add_cube(colormap, int colors, int cubeside)
rle_hdr * rle_hdr_cp(rle_hdr *from_hdr, rle_hdr *to_hdr)
static void write_output(rle_hdr *hdr, int width, int height, int bits, int dflag, rle_pixel *red, rle_pixel *green, rle_pixel *blue, rle_pixel *alpha, rle_pixel *img_red, rle_pixel *img_green, rle_pixel *img_blue, rle_pixel *rgbmap, colormap, outrows)
void rle_names(rle_hdr *the_hdr, const char *pgmname, const char *fname, int img_num)
void rle_row_free(rle_hdr *the_hdr, rle_pixel **scanp)
char * cmd_name(char **argv)
static const char * MY_NAME
void main(int argc, char **argv)
static void setup_output(rle_hdr *hdr, int colors, colormap)
int rle_get_setup(rle_hdr *the_hdr)
int rle_row_alloc(rle_hdr *the_hdr, rle_pixel ***scanp)
int rle_getrow(rle_hdr *the_hdr, scanline)
int rle_get_error(int code, const char *pgmname, const char *fname)
int scanargs(int argc, char **argv, const char *format,...)
rle_pixel ** buildmap(rle_hdr *the_hdr, int minmap, double orig_gamma, double new_gamma)
void rle_puteof(rle_hdr *the_hdr)
void rle_putrow(rows, int rowlen, rle_hdr *the_hdr)
int colorquant(unsigned char *red, unsigned char *green, unsigned char *blue, unsigned long pixels, colormap, int colors, int bits, unsigned char *rgbmap, int flags, int accum_hist)
static void copy_hdr(char **argv, rle_hdr *in_hdr, rle_hdr *out_hdr, FILE *outfile, int cflag)
void rle_addhist(argv, rle_hdr *in_hdr, rle_hdr *out_hdr)
#define CHECK_MALLOC(type, ptr, size, desc)
void inv_cmap(int colors, colormap, int bits, unsigned long *dist_buf,*rgbmap)
void rle_get_setup_ok(rle_hdr *the_hdr, const char *prog_name, const char *file_name)
char * rle_getcom(char *name, rle_hdr *the_hdr) const
static void read_input(rle_hdr *hdr, int width, int shift, int dflag, rle_pixel *red, rle_pixel *green, rle_pixel *blue, rle_pixel *alpha, rle_pixel *img_red, rle_pixel *img_green, rle_pixel *img_blue, rle_pixel **rows)
void rle_put_setup(rle_hdr *the_hdr)
const char * rle_putcom(char *value, rle_hdr *the_hdr) const
static void mem_alloc(rle_hdr *hdr, unsigned long entries, int dflag, rle_pixel ***rows, rle_pixel **red, rle_pixel **green, rle_pixel **blue, rle_pixel **alpha, rle_pixel **img_red, rle_pixel **img_green, rle_pixel **img_blue)
static void free_mem(rle_hdr *hdr, int dflag, rle_pixel *red, rle_pixel *green, rle_pixel *blue, rle_pixel *alpha, rle_pixel *img_red, rle_pixel *img_green, rle_pixel *img_blue, rle_pixel **rows, outrows)
rle_hdr * rle_hdr_init(rle_hdr *the_hdr)