29 static char rcs_id[] =
"$Header: /l/spencer/src/urt/cnv/RCS/aliastorle.c,v 3.0.1.1 1992/04/29 20:01:12 spencer Exp $";
39 #define byte unsigned char
40 #define Fread(p, s, n, f) if ( fread(p, s, n, f) != n ) error(3
)
41 #define Fwrite(p, s, n, f) if ( fwrite(p, s, n, f) != n ) error(4
)
42 #define VPRINTF if (verbose) fprintf
69 static char *Malloc(
long int);
70 static char *read_file(FILE *,
int *);
71 static long filelen(FILE *);
72 static int read_alias(FILE *, bitmap_hdr *);
73 static void write_rle(FILE *, bitmap_hdr *);
74 static void read_alias_header(FILE *, alias_hdr *);
75 static void create_alias_cmap(bitmap_hdr *);
78 static int read_line_alias24(FILE *,
byte *,
byte *,
byte *,
int);
79 static int read_line_alias(FILE *,
byte *,
int);
80 static void error(
int );
82 static char *Malloc();
83 static char *read_file();
84 static long filelen();
85 static int read_alias();
86 static void write_rle(), read_alias_header();
87 static void create_alias_cmap(), uncode_alias24(), uncode_alias();
88 static int read_line_alias24(), read_line_alias();
126 char *outname = NULL;
136 if ( !
scanargs( argc
, argv
, "% v%- o%-outfile!s infile%s",
148 read_alias(infile, &bitmap);
156 write_rle(outfile, &bitmap);
163 read_alias(handle, image)
172 byte *r, *g = 0, *b = 0;
174 byte *buffer = 0, *ptr = 0;
187 can_read_all = (handle != stdin);
188 VPRINTF(stderr,
"Reading Alias from %s\n",
189 (can_read_all ?
"file" :
"stdin"));
196 ptr= buffer= (
byte *) read_file(handle, &filesize);
197 VPRINTF(stderr,
"File size: %d\n", filesize);
199 bcopy(ptr, &header,
sizeof(alias_hdr));
200 ptr +=
sizeof(alias_hdr);
206 VPRINTF(stderr,
"Reading Alias header\n");
207 read_alias_header(handle, &header);
218 allplanes = ( image
->depth > 8 );
228 r= image
->r = (
byte *) Malloc(total_size);
229 g= image
->g = (
byte *) Malloc(total_size);
230 b= image
->b = (
byte *) Malloc(total_size);
236 r= image
->r = (
byte *) Malloc(total_size);
240 VPRINTF(stderr,
"Creating cmap\n");
241 create_alias_cmap(image);
247 VPRINTF(stderr,
"Uncompressing Alias file\n");
252 end = r + total_size;
254 uncode_alias24(ptr, r, g, b, end);
255 else uncode_alias(ptr, r, end);
267 for (i= 0; i< image
->ysize; i++) {
274 error= read_line_alias24(handle, r, b, g, xsize);
277 error= read_line_alias(handle, r, xsize);
281 fprintf(stderr,
"Error while reading line %d\n", i);
299 read_alias_header(handle, header)
304 Fread(header,
sizeof(alias_hdr), 1, handle);
315 read_line_alias24(handle, r, g, b, size)
321 register int count = 0;
326 while (count < size) {
330 if ( !fread(buffer, 4, 1, handle) )
335 for (i= 0; i< buffer[0]; i++) {
349 read_line_alias(handle, r, size)
355 register int count = 0;
360 while (count < size) {
364 if ( !fread(buffer, 2, 1, handle) )
369 for (i= 0; i< buffer[0]; i++) {
381 create_alias_cmap(image)
399 bitmapcmap_to_rlecmap(bitmap, rle)
417 for (i= 0; i< bitmap
->colors; i++) {
418 *rch++ = (*ptr++ << 8);
419 *gch++ = (*ptr++ << 8);
420 *bch++ = (*ptr++ << 8);
425 uncode_alias24(ptr, rbuf, gbuf, bbuf, end)
427 byte *rbuf, *gbuf, *bbuf;
433 while ( rbuf < end ) {
434 if ( (i= *ptr++) > 1 ) {
452 uncode_alias(ptr, rbuf, end)
460 while ( rbuf < end ) {
461 if ( (i= *ptr++) > 1 ) {
473 write_rle(handle, image)
482 VPRINTF(stderr,
"Writing RLE file\n");
499 bitmapcmap_to_rlecmap(image, &
the_hdr);
512 VPRINTF(stderr,
"Compressing RLE lines\n");
524 row[0] = image
->r + offset_last;
525 row[1] = image
->g + offset_last;
526 row[2] = image
->b + offset_last;
529 for (i= 0; i< image
->ysize; i++) {
534 for ( j = 0; j < 3; j++ )
545 r = image
->r + offset_last;
546 for (i= 0; i< image
->ysize; i++) {
566 case 1: fprintf(stderr,
"Cannot open file\n");
568 case 2: fprintf(stderr,
"Out of memory\n");
570 case 3: fprintf(stderr,
"Error while reading input file\n");
572 case 4: fprintf(stderr,
"Error while writing output file\n");
574 case 5: fprintf(stderr,
"Input file is not an Alias pix\n");
576 case 6: fprintf(stderr,
"File corrupt ( uncompress too bytes )\n");
578 case 99: fprintf(stderr,
"Not ready\n");
580 default: fprintf(stderr,
"Unknow error code (%d)\n", code);
590 static char *Malloc(size)
595 if ((ptr = (
char *) malloc(size)) == NULL)
607 static char *read_file(handle, bytes)
614 *bytes= (
int) filelen(handle);
617 buffer= (
char *) Malloc(*bytes);
620 Fread(buffer, (
int) (*bytes), 1, handle);
624 int n = 0, bufsize = 0;
626 buffer = Malloc( BUFSIZ );
627 while ( (n = fread( buffer + bufsize, 1, BUFSIZ, handle )) == BUFSIZ )
630 buffer = realloc( buffer, bufsize + BUFSIZ );
643 static long filelen(handle)
650 current_pos= ftell(handle);
657 fseek(handle, current_pos, 0);
FILE * rle_open_f(char *prog_name, char *file_name, char *mode)
void rle_names(rle_hdr *the_hdr, const char *pgmname, const char *fname, int img_num)
char * cmd_name(char **argv)
void rle_close_f(FILE *fd)
void main(int argc, char **argv)
int scanargs(int argc, char **argv, const char *format,...)
void rle_puteof(rle_hdr *the_hdr)
void rle_putrow(rows, int rowlen, rle_hdr *the_hdr)
#define Fread(p, s, n, f)
void rle_put_setup(rle_hdr *the_hdr)
rle_hdr * rle_hdr_init(rle_hdr *the_hdr)
#define RLE_CHECK_ALLOC(pgm, ptr, name)