Utah Raster Toolkit  9999-git
URT Development version (post-3.1b)
wasatchrle.c
Go to the documentation of this file.
1 /*
2  * This software is copyrighted as noted below. It may be freely copied,
3  * modified, and redistributed, provided that the copyright notice is
4  * preserved on all copies.
5  *
6  * There is no warranty or other guarantee of fitness for this software,
7  * it is provided solely "as is". Bug reports or fixes may be sent
8  * to the author, who may or may not act on them as he desires.
9  *
10  * You may not include this software in a program or other software product
11  * without supplying the source, or without informing the end-user that the
12  * source is available for no extra charge.
13  *
14  * If you modify this software, you should include a notice giving the
15  * name of the person performing the modification, the date of modification,
16  * and the reason for such modification.
17  */
18 /*
19  * wasatchrle.c - Convert Wasatch Paintbox files into RLE
20  *
21  * Author: John W. Peterson
22  * Computer Science Dept.
23  * University of Utah
24  * Date: Tue Dec 15 1987
25  * Copyright (c) 1987, University of Utah
26  *
27  * Thanks to Mike Ware of Wasatch for providing the format information.
28  */
29 
30 #include <stdio.h>
31 #include <ctype.h>
32 #include <errno.h>
33 #include "rle.h"
34 
35 
36 /* "short" in our world is 16 bits. Beware of swyte-bopping. */
37 
38 struct was_head {
39  short x1, y1; /* First corner of viewport */
40  short x2, y2; /* Second corner of viewport */
41  short top, bottom; /* "Reserved" */
42  short unused[19];
43  char reserved[14];
44 } was_head;
45 
46 short rlemap[3][256]; /* RLE version of the color map */
47 rle_pixel rgb[3]; /* For reading in the color map */
48 
49 struct was_op { /* A wasatch "opcode" */
51 } was_op;
52 
53 void
55 int argc;
56 char **argv;
57 {
58  FILE *open_with_ext();
59  int oflag = 0;
60  char *out_name = NULL;
61  rle_hdr hdr;
62  char *was_basename;
63  FILE *lut_file, *rlc_file; /* Wasatch input files */
64  rle_pixel **row;
65  rle_pixel *pxlptr;
66  int i, xpos;
67 
68  if (scanargs( argc, argv, "% o%-outfile!s basename!s",
69  &oflag, &out_name, &was_basename ) == 0)
70  exit(-1);
71 
72  /* Initialize header. */
73  hdr = *rle_hdr_init( (rle_hdr *)NULL );
74  rle_names( &hdr, cmd_name( argv ), out_name, 0 );
75 
76  lut_file = open_with_ext( was_basename, ".lut" );
77  rlc_file = open_with_ext( was_basename, ".rlc" );
78 
79  hdr.rle_file = rle_open_f( hdr.cmd, out_name, "w" );
80 
81  fread( &was_head, sizeof( was_head ), 1, rlc_file );
82 
83  /*
84  * The spec provided by Wasatch is not clear on the meaning or any
85  * intended order of the "viewport corners". Also, look out for
86  * BYTE SWAPPING.
87  */
88  hdr.xmin = was_head.x1;
89  hdr.ymin = was_head.y1;
90  hdr.xmax = was_head.x2;
91  hdr.ymax = was_head.y2;
92 
93  hdr.ncolors = 1;
94  hdr.alpha = 0; /* What a shame... */
95  for (i = 1; i < hdr.ncolors; i++)
96  RLE_CLR_BIT( hdr, i );
97  hdr.ncmap = 3;
98  hdr.cmaplen = 8; /* == 256 entries */
99  hdr.cmap = (rle_map *) rlemap;
100 
101  /* Grab the map. */
102 
103  for (i = 0; i < 256; i++)
104  {
105  fread( rgb, sizeof( rgb ), 1, lut_file );
106  rlemap[RLE_RED][i] = ( (int)rgb[RLE_RED] ) << 8;
107  rlemap[RLE_GREEN][i] = ( (int)rgb[RLE_GREEN] ) << 8;
108  rlemap[RLE_BLUE][i] = ( (int)rgb[RLE_BLUE] ) << 8;
109  }
110 
111  rle_addhist( argv, (rle_hdr *)NULL, &hdr );
112  rle_put_setup( &hdr );
113  hdr.xmax -= hdr.xmin;
114  hdr.xmin = 0;
115 
116  if (rle_row_alloc( &hdr, &row ))
117  {
118  fprintf( stderr, "wasacthrle: malloc failed\n" );
119  exit(-2);
120  }
121 
122  xpos = 0;
123  pxlptr = row[0];
124  was_op.count = 0; /* Haven't read anything yet. */
125 
126  do {
127  while (was_op.count--)
128  {
129  if (xpos > hdr.xmax) /* Flush scanline */
130  {
131  rle_putrow( row, (hdr.xmax + 1), &hdr );
132  xpos = 0;
133  pxlptr = row[0];
134  }
135  *pxlptr++ = was_op.data;
136  xpos++;
137  }
138  fread( &was_op, sizeof( was_op ), 1, rlc_file );
139  } while( was_op.count );
140 
141  if (xpos)
142  rle_putrow( row, (hdr.xmax + 1), &hdr );
143  rle_puteof( &hdr );
144  exit(0);
145 }
146 
147 /*****************************************************************
148  * TAG( open_with_ext )
149  *
150  * Open a file "basename" with extension (suffix) "ext". Tries "ext"
151  * with both lower and upper case (should be called with "ext" in
152  * lower case). If both opens fail, it exits the program.
153  */
154 
155 FILE *
156 open_with_ext( basename, ext )
157 char * basename;
158 char * ext;
159 {
160  char file_name[255];
161  char ext_name[10];
162  char * cptr;
163  FILE * f;
164 
165  strcpy( ext_name, ext );
166  strcpy( file_name, basename );
167  strcat( file_name, ext_name );
168 
169  if (! (f = fopen( file_name, "r" )))
170  {
171  if (errno == ENOENT) /* Not found, try upper case */
172  {
173  strcpy( file_name, basename );
174  cptr = ext_name;
175  for (; *cptr; cptr++ )
176  *cptr = toupper( *cptr );
177  strcat( file_name, ext_name );
178  f = fopen( file_name, "r" );
179  }
180  }
181 
182  if (! f) /* fopen failed */
183  {
184  perror( file_name );
185  exit( -1 );
186  }
187  return( f );
188 }
FILE * rle_open_f(char *prog_name, char *file_name, char *mode)
Definition: rle_open_f.c:216
rle_pixel count
Definition: wasatchrle.c:50
int xmin
Definition: rle.h:100
short y2
Definition: wasatchrle.c:40
short unused[19]
Definition: wasatchrle.c:42
rle_pixel rgb[3]
Definition: wasatchrle.c:47
void rle_names(rle_hdr *the_hdr, const char *pgmname, const char *fname, int img_num)
Definition: rle_hdr.c:48
char * cmd_name(char **argv)
Definition: cmd_name.c:31
short top
Definition: wasatchrle.c:41
void main(int argc, char **argv)
Definition: aliastorle.c:121
rle_map * cmap
Definition: rle.h:112
int rle_row_alloc(rle_hdr *the_hdr, rle_pixel ***scanp)
Definition: rle_row_alc.c:56
#define RLE_GREEN
Definition: rle.h:63
int ymin
Definition: rle.h:100
#define RLE_BLUE
Definition: rle.h:64
int scanargs(int argc, char **argv, const char *format,...)
Definition: scanargs.c:94
const char * cmd
Definition: rle.h:133
void rle_puteof(rle_hdr *the_hdr)
Definition: rle_putrow.c:474
#define RLE_RED
Definition: rle.h:62
void rle_putrow(rows, int rowlen, rle_hdr *the_hdr)
Definition: rle_putrow.c:96
int xmax
Definition: rle.h:100
short rlemap[3][256]
Definition: wasatchrle.c:46
void rle_addhist(argv, rle_hdr *in_hdr, rle_hdr *out_hdr)
Definition: rle_addhist.c:54
short bottom
Definition: wasatchrle.c:41
#define RLE_CLR_BIT(glob, bit)
Definition: rle.h:124
int ncmap
Definition: rle.h:100
int ymax
Definition: rle.h:100
unsigned char rle_pixel
Definition: rle.h:56
void rle_put_setup(rle_hdr *the_hdr)
Definition: rle_putrow.c:453
int cmaplen
Definition: rle.h:100
rle_pixel data
Definition: wasatchrle.c:50
int alpha
Definition: rle.h:100
char reserved[14]
Definition: wasatchrle.c:43
short x1
Definition: wasatchrle.c:39
short y1
Definition: wasatchrle.c:39
unsigned short rle_map
Definition: rle.h:57
rle_hdr * rle_hdr_init(rle_hdr *the_hdr)
Definition: rle_hdr.c:267
FILE * rle_file
Definition: rle.h:114
int ncolors
Definition: rle.h:100
short x2
Definition: wasatchrle.c:40