32 #define MAX(i,j) ( (i) > (j) ? (i) : (j) )
33 #define MIN(i,j) ( (i) < (j) ? (i) : (j) )
39 #define TLISTLINKS(type) type * next, * prev
44 #define P(node) ((node)->prev)
45 #define N(node) ((node)->next)
50 #define TRACE(t_var,ini) for((t_var)=(ini);(t_var)!=NULL
;(t_var)=N(t_var))
60 #define ADD(new,first) ( P(new) = NULL
,N(new) = (first),
61 ( ((first)!=NULL
) ? (P(first)=(new), 0
) :0
),
64 #define INSERT(new,after) ( P(new) = (after),N(new) = N(after),
65 ( (N(after)!=NULL
) ? (P(N(after))=(new), 0
) :0
),
72 #define BLUEMASK(x) ((x) & 0x1f
)
73 #define GREENMASK(x) (((x)>>5
) & 0x1f
)
74 #define REDMASK(x) (((x)>>10
) & 0x1f
)
76 #define TO_5_BITS(x) (((x)>>3
) & 0x1f
)
77 #define FROM_5_BITS(x) (((x)<<3
) & 0xff
)
79 #define FAST_PACK(r,g,b) (((r)&0xf8
)<<7
| ((g)&0xf8
)<<2
| (TO_5_BITS(b)))
81 #define DISTANCE(x,y) (((x).r-(y).r) * ((x).r-(y).r) +
82 ((x).g-(y).g) * ((x).g-(y).g) +
83 ((x).b-(y).b) * ((x).b-(y).b))
241 int tmp, dither = 0, oflag = 0;
244 char * infname = NULL, *out_fname = NULL;
245 FILE *outfile = stdout;
246 char cmap_comment[80];
247 int rle_err, rle_cnt;
250 if ( !
scanargs( argc
, argv
, "% n%-colors!d d%- o%-outfile!s infile!s",
262 for ( rle_cnt = 0; ; rle_cnt++ )
267 fprintf( stderr,
"%s: Sorry, can't accept piped input.\n",
280 "%s: input file must be three channel (RGB) image.\n",
315 register int tmp =
FAST_PACK( *rptr++, *gptr++, *bptr++ );
334 sprintf( cmap_comment,
"color_map_length=%d",
cb_list_size );
378 register int oldh, newh, i, total;
386 for ( oldh = newh = 0; oldh < 32768; oldh++ )
390 if ( oldh != newh )
hist[oldh] = NULL;
439 N(box) =
P(box) = NULL;
490 for ( t = 0, i = 0; i < box
->hsize; i++ )
531 float red, grn, blu, nsamp;
535 red = grn = blu = nsamp = 0;
583 register int newdist =
DISTANCE( ref_col, *newcol );
585 if ( newdist < dist )
656 register rle_pixel *rptr, *gptr, *bptr, *optr;
668 tmp =
FAST_PACK( *rptr++, *gptr++, *bptr++ );
669 if (
hist[tmp] == NULL)
670 fprintf(stderr,
"bogus hash: %d\n", tmp);
689 register rle_pixel *rptr, *gptr, *bptr, *optr;
691 short *thisline, *nextline, *tmpptr ;
692 register short *thisptr, *nextptr ;
694 int lastline, lastpixel ;
696 thisline = (
short *) malloc( (
in_hdr.xmax + 1) * 3 *
sizeof(
short));
698 nextline = (
short *) malloc( (
in_hdr.xmax + 1) * 3 *
sizeof(
short));
710 *nextptr++ = *bptr++ ;
711 *nextptr++ = *gptr++ ;
712 *nextptr++ = *rptr++ ;
719 thisline = nextline ;
731 *nextptr++ = *bptr++ ;
732 *nextptr++ = *gptr++ ;
733 *nextptr++ = *rptr++ ;
755 if( red < 0 ) red = 0 ;
756 else if( red > 255 ) red = 255 ;
757 if( green < 0 ) green = 0 ;
758 else if( green > 255 ) green = 255 ;
759 if( blue < 0 ) blue = 0 ;
760 else if( blue > 255 ) blue = 255 ;
767 if (
hist[tmp] == NULL)
794 register newdist =
DISTANCE( ref_col, *newcol );
796 if ( newdist < dist )
805 fprintf( stderr,
"Color box list is empty.\n" );
816 thisptr[0] += blue * 7 / 16 ;
817 thisptr[1] += green * 7 / 16 ;
818 thisptr[2] += red * 7 / 16 ;
824 nextptr[-3] += blue * 3 / 16 ;
825 nextptr[-2] += green * 3 / 16 ;
826 nextptr[-1] += red * 3 / 16 ;
828 nextptr[0] += blue * 5 / 16 ;
829 nextptr[1] += green * 5 / 16 ;
830 nextptr[2] += red * 5 / 16 ;
833 nextptr[3] += blue / 16 ;
834 nextptr[4] += green / 16 ;
835 nextptr[5] += red / 16 ;
917 for ( i=0; i<box
->hsize; i++, hp++ )
946 int start_bit, num_bits;
950 mask = ~(~0 << num_bits) << start_bit;
966 register c1 = -1, c2 = -1;
973 return c1>c2 ? 1 : ( c2>c1 ? -1 : 0 );
991 else if (
P(list) == NULL &&
N(list) == NULL )
1002 for ( lp = list;
N(lp); lp =
N(lp) )
1010 if (
P(lp) == NULL )
1016 for (ll = lp;
N(ll) != NULL; ll =
N(ll)) ;
1040 for( tmp_cb =
cb_list; tmp_cb; tmp_cb = next_cb )
1042 next_cb =
N(tmp_cb);
1048 for ( i = 0; i < 32768; i++ )
FILE * rle_open_f(char *prog_name, char *file_name, char *mode)
rle_hdr * rle_hdr_cp(rle_hdr *from_hdr, rle_hdr *to_hdr)
void quantize_rle(rle_pixel **scan)
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)
void quantize_dither_rle(rle_pixel **scan)
void main(int argc, char **argv)
int rle_get_setup(rle_hdr *the_hdr)
color_box_t * insert_elt(color_box_t *list, color_box_t *elt)
int rle_row_alloc(rle_hdr *the_hdr, rle_pixel ***scanp)
struct _histogram histogram_t
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,...)
color_box_t * split_box(color_box_t *box)
void rle_puteof(rle_hdr *the_hdr)
void rle_putrow(rows, int rowlen, rle_hdr *the_hdr)
histogram_t * hist[32768]
#define TRACE(t_var, ini)
void rle_addhist(argv, rle_hdr *in_hdr, rle_hdr *out_hdr)
#define INSERT(new, after)
void rle_get_setup_ok(rle_hdr *the_hdr, const char *prog_name, const char *file_name)
#define RLE_CLR_BIT(glob, bit)
void rle_put_setup(rle_hdr *the_hdr)
void bound_rgb(color_box_t *box)
const char * rle_putcom(char *value, rle_hdr *the_hdr) const
struct _color_box color_box_t
#define FAST_PACK(r, g, b)
int cmp_radices(histogram_t **h1, histogram_t **h2)
void set_size_axis(color_box_t *cb)
rle_hdr * rle_hdr_init(rle_hdr *the_hdr)
int resort_compare(histogram_t **c1, histogram_t **c2)
void radix_sort(color_box_t *bbox, int start_bit, int num_bits)
#define RLE_CHECK_ALLOC(pgm, ptr, name)