Utah Raster Toolkit  9999-git
URT Development version (post-3.1b)
rlehdr.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  * rlehdr.c - Print header from an RLE file.
20  *
21  * Author: Spencer W. Thomas
22  * Computer Science Dept.
23  * University of Utah
24  * Date: Thu Jan 22 1987
25  * Copyright (c) 1987, University of Utah
26  */
27 #ifndef lint
28 char rcsid[] = "$Header: /l/spencer/src/urt/tools/RCS/rlehdr.c,v 3.0.1.4 1992/04/30 14:12:09 spencer Exp $";
29 #endif
30 /*
31 rlehdr() Tag the file.
32 */
33 
34 #include <stdio.h>
35 #include <stdint.h>
36 #include "rle.h"
37 #include "../patchlevel.h"
38 
39 /* Utah Raster Toolkit major version number. */
40 #define URT_VERSION 3.0
41 
43 
44 /*****************************************************************
45  * TAG( main )
46  *
47  * Read and print in human readable form the header of an RLE file.
48  *
49  * Usage:
50  * rlehdr [-b] [-c comment-name] [-d] [-m] [-v] [files ...]
51  * Inputs:
52  * -b: Brief. Fit it on one line.
53  * -c comment-names,...:
54  The first line of the first comment matching
55  * one of these names will be printed. Sets -b.
56  * -m: Dump color map contents.
57  * -d: Dump entire file contents.
58  * -v: Print URT version number (and ignores files.)
59  * files: RLE file(s) to interpret. If none, reads
60  * from standard input.
61  * Outputs:
62  * Prints contents of file header.
63  * Assumptions:
64  * [None]
65  * Algorithm:
66  * [None]
67  */
68 
69 void
71 int argc;
72 char **argv;
73 {
74  CONST_DECL char ** fname = NULL;
75  CONST_DECL char *stdname = "-";
76  char **comment_names = NULL;
77  int brief = 0,
78  cflag = 0,
79  hflag = 0,
80  num_hdr = 1,
81  mflag = 0,
82  suppress = 0,
83  dbg_flag = 0,
84  nfname = 0,
85  version = 0;
86  int rle_err = 0, rle_cnt;
87  rle_hdr in_hdr;
88 
89  in_hdr = *rle_hdr_init( NULL );
90 
91  if ( scanargs( argc, argv,
92  "% b%- c%-comment-name!,s h%-n%d Mm%- s%- d%- v%- infile%*s (\n\
93 \t-b\tBrief mode, one line per image.\n\
94 \t-c\tWith -b, print 1st line of 1st comment matching a name.\n\
95 \t-h [n]\tPrint only first (n) header(s) in each file.\n\
96 \t-m\tPrint contents of colormap.\n\
97 \t-M\tPrint colormap suitable for rleldmap -t.\n\
98 \t-s\tSuppress printing header at all (useful with -M).\n\
99 \t-d\tPrint image data (LOTS of output)\n\
100 \t-v\tPrint URT version number. Ignores other arguments.)",
101  &brief, &cflag, &cflag, &comment_names,
102  &hflag, &num_hdr, &mflag, &suppress, &dbg_flag, &version,
103  &nfname, &fname ) == 0 )
104  exit( 1 );
105 
106  /* If -c was specified, set -b. */
107  if ( cflag )
108  brief = 1;
109 
110  if ( nfname == 0 )
111  {
112  nfname = 1;
113  fname = &stdname;
114  }
115 
116  if ( version )
117  {
118  printf( "Utah Raster Toolkit version %3.1f", URT_VERSION );
119 #if PATCHLEVEL > 0
120  printf( "(patch %d)", PATCHLEVEL );
121 #endif
122  putchar( '\n' );
123  exit( 0 );
124  }
125 
126  for ( ; nfname > 0; fname++, nfname-- )
127  {
128  in_hdr.rle_file = rle_open_f(cmd_name( argv ), *fname, "r");
129  rle_names( &in_hdr, cmd_name( argv ), *fname, 0 );
130 
131  for ( rle_cnt = 0;
132  !(hflag && rle_cnt >= num_hdr) &&
133  (rle_err = rle_get_setup( &in_hdr )) == RLE_SUCCESS;
134  rle_cnt++ )
135  {
136  /* Separate headers with a newline. */
137  if ( !brief && rle_cnt > 0 )
138  putchar( '\n' );
139 
140  if ( !suppress )
141  if ( brief )
142  print_brief_hdr( &in_hdr, cflag, comment_names );
143  else
144  print_hdr( &in_hdr );
145  if ( mflag )
146  print_map( &in_hdr, mflag );
147  if ( dbg_flag )
148  print_codes( &in_hdr );
149  else if ( !hflag || rle_cnt + 1 < num_hdr )
150  while ( rle_getskip( &in_hdr ) != 32768 )
151  ;
152  }
153 
154  /* Separate headers with a newline. */
155  if ( !brief && nfname > 1 )
156  putchar( '\n' );
157 
158  fclose( in_hdr.rle_file );
159 
160  /* Check for an error. EOF or EMPTY is ok if at least one image
161  * has been read. Otherwise, print an error message.
162  */
163  if ( rle_cnt == 0 || (rle_err != RLE_EOF && rle_err != RLE_EMPTY) )
164  rle_get_error( rle_err, cmd_name( argv ), *fname );
165  }
166 
167  exit( 0 ); /* success */
168 }
169 
170 
171 /*****************************************************************
172  * TAG( print_hdr )
173  *
174  * Print the RLE header information given.
175  *
176  * Inputs:
177  * the_hdr: Header information.
178  * Outputs:
179  * Prints info on stdout.
180  * Assumptions:
181  * [None]
182  * Algorithm:
183  * [None]
184  */
185 void
187 rle_hdr *the_hdr;
188 {
189  register int i;
190 
191  if ( the_hdr->img_num > 1 )
192  printf( "RLE header information for %s image %d:\n",
193  the_hdr->file_name, the_hdr->img_num );
194  else
195  printf( "RLE header information for %s:\n", the_hdr->file_name );
196  printf( "Originally positioned at (%d, %d), size (%d, %d)\n",
197  the_hdr->xmin, the_hdr->ymin,
198  the_hdr->xmax - the_hdr->xmin + 1,
199  the_hdr->ymax - the_hdr->ymin + 1 );
200  printf( "Saved %d color channels%s\n", the_hdr->ncolors,
201  the_hdr->alpha ? " plus Alpha channel" : "" );
202  if ( the_hdr->background == 0 )
203  printf( "No background information was saved\n" );
204  else
205  {
206  if ( the_hdr->background == 1 )
207  printf( "Saved in overlay mode with original background color" );
208  else
209  printf( "Screen will be cleared to background color" );
210  for ( i = 0; i < the_hdr->ncolors; i++ )
211  printf( " %d", the_hdr->bg_color[i] );
212  putchar( '\n' );
213  }
214 
215  if ( the_hdr->ncmap > 0 )
216  printf( "%d channels of color map %d entries long were saved.\n",
217  the_hdr->ncmap, 1 << the_hdr->cmaplen );
218 
219  if ( the_hdr->comments != NULL && *the_hdr->comments != NULL )
220  {
221  printf( "Comments:\n" );
222  for ( i = 0; the_hdr->comments[i] != NULL; i++ )
223  printf( "%s\n", the_hdr->comments[i] );
224  }
225 
226  fflush( stdout );
227 }
228 
229 /*****************************************************************
230  * TAG( print_brief_hdr )
231  *
232  * Print the RLE header information on one line.
233  *
234  * Inputs:
235  * the_hdr: Header information.
236  * ncomment: Number of comment names to test.
237  * comment_names: Print the first line of the first matching
238  * comment found.
239  * Outputs:
240  * Prints info on stdout.
241  * Assumptions:
242  * [None]
243  * Algorithm:
244  * [None]
245  */
246 void
248 rle_hdr *the_hdr;
249 int ncomment;
250 char **comment_names;
251 {
252  register int i;
253 
254  if ( the_hdr->img_num > 1 )
255  printf( "%s(%d):",
256  the_hdr->file_name, the_hdr->img_num );
257  else
258  printf( "%s:", the_hdr->file_name );
259  printf( "\t[%d,%d]+[%d,%d]",
260  the_hdr->xmin, the_hdr->ymin,
261  the_hdr->xmax - the_hdr->xmin + 1,
262  the_hdr->ymax - the_hdr->ymin + 1 );
263  printf( "x%d%s", the_hdr->ncolors,
264  the_hdr->alpha ? "+A" : "" );
265  if ( the_hdr->background == 0 )
266  ;
267  else
268  {
269  if ( the_hdr->background == 1 )
270  printf( ", OV=" );
271  else
272  printf( ", BG=" );
273  for ( i = 0; i < the_hdr->ncolors; i++ )
274  printf( "%d%s", the_hdr->bg_color[i],
275  i == the_hdr->ncolors - 1 ? " " : "," );
276  }
277 
278  if ( the_hdr->ncmap > 0 )
279  printf( ", map %dx%d",
280  the_hdr->ncmap, 1 << the_hdr->cmaplen );
281 
282  if ( the_hdr->comments != NULL && *the_hdr->comments != NULL )
283  {
284  char *the_comment, *cp;
285 
286  if ( ncomment > 0 )
287  {
288  for ( ; ncomment > 0; ncomment--, comment_names++ )
289  if (the_comment = rle_getcom( *comment_names, the_hdr ))
290  {
291  if ( (cp = index( the_comment, '\n' )) )
292  printf( ", %s=%.*s", *comment_names,
293  cp - the_comment - 1, the_comment );
294  else
295  printf( ", %s=%s", *comment_names, the_comment );
296  break;
297  }
298  }
299  if ( ncomment == 0 )
300  printf( ", (C)" );
301  }
302 
303  putchar( '\n' );
304 
305  fflush( stdout );
306 }
307 
308 /*****************************************************************
309  * TAG( print_map )
310  *
311  * Print the color map from a the_hdr structure.
312  * Inputs:
313  * the_hdr: Sv_hdr structure containing color map.
314  * mflag: If 1, print "human readable". If 2, print in
315  * form suitable for rleldmap -t.
316  * Outputs:
317  * Prints color map (if present) on stdout.
318  * Assumptions:
319  * color_map_length comment is relevant.
320  * Algorithm:
321  * [None]
322  */
323 void
325 rle_hdr *the_hdr;
326 int mflag;
327 {
328  register int i, j;
329  int c, maplen, ncmap, nmap;
330  rle_map * cmap;
331  char *len_com;
332 
333  if ( the_hdr->ncmap == 0 )
334  return;
335 
336  maplen = (1 << the_hdr->cmaplen);
337  ncmap = the_hdr->ncmap;
338  cmap = the_hdr->cmap;
339 
340  if ( (len_com = rle_getcom( "color_map_length", the_hdr )) != NULL &&
341  atoi( len_com ) > 0 )
342  nmap = atoi( len_com );
343  else
344  nmap = maplen;
345 
346  if ( mflag == 1 )
347  {
348  printf( "Color map contents, values are 16-bit(8-bit):\n" );
349  for ( i = 0; i < nmap; i++ )
350  {
351  printf( "%3d:\t", i );
352  for ( j = 0, c = 0; j < ncmap; j++, c += maplen )
353  printf( "%5d(%3d)%c", cmap[i+c], cmap[i+c] >> 8,
354  j == ncmap - 1 ? '\n' : '\t' );
355 
356  }
357  }
358  else
359  for ( i = 0; i < nmap; i++ )
360  {
361  for ( j = 0, c = 0; j < ncmap; j++, c += maplen )
362  printf( "%3d%c", cmap[i+c] >> 8,
363  j == ncmap - 1 ? '\n' : '\t' );
364 
365  }
366 }
367 
368 
369 /*****************************************************************
370  * TAG( print_codes )
371  *
372  * Print the RLE opcodes in an RLE file.
373  * Inputs:
374  * the_hdr: Header for RLE file (already open).
375  * Outputs:
376  * Prints file contents on stderr.
377  * Assumptions:
378  *
379  * Algorithm:
380  * [None]
381  */
382 void
384 rle_hdr *the_hdr;
385 {
386  rle_pixel ** scans;
387 
388  if ( rle_row_alloc( the_hdr, &scans ) < 0 )
389  RLE_CHECK_ALLOC( "rlehdr", 0, 0 );
390 
391  rle_debug(1);
392  while ( !feof( the_hdr->rle_file ) &&
393  !the_hdr->priv.get.is_eof )
394  rle_getrow( the_hdr, scans );
395 
396  rle_row_free( the_hdr, scans );
397 }
FILE * rle_open_f(char *prog_name, char *file_name, char *mode)
Definition: rle_open_f.c:216
char rcsid[]
Definition: rleswap.c:28
#define URT_VERSION
Definition: rlehdr.c:40
int xmin
Definition: rle.h:100
const char ** comments
Definition: rle.h:113
void rle_names(rle_hdr *the_hdr, const char *pgmname, const char *fname, int img_num)
Definition: rle_hdr.c:48
void print_brief_hdr(rle_hdr *the_hdr, int ncomment, char **comment_names)
Definition: rlehdr.c:247
void rle_debug(int on_off)
Definition: rle_getrow.c:293
void rle_row_free(rle_hdr *the_hdr, rle_pixel **scanp)
Definition: rle_row_alc.c:114
void print_hdr(rle_hdr *the_hdr)
Definition: rlehdr.c:186
#define RLE_EMPTY
Definition: rle.h:73
char * cmd_name(char **argv)
Definition: cmd_name.c:31
void main(int argc, char **argv)
Definition: aliastorle.c:121
int rle_get_setup(rle_hdr *the_hdr)
Definition: rle_getrow.c:74
rle_map * cmap
Definition: rle.h:112
int rle_row_alloc(rle_hdr *the_hdr, rle_pixel ***scanp)
Definition: rle_row_alc.c:56
int rle_getrow(rle_hdr *the_hdr, scanline)
Definition: rle_getrow.c:333
int * bg_color
Definition: rle.h:100
#define RLE_SUCCESS
Definition: rle.h:70
int ymin
Definition: rle.h:100
int rle_get_error(int code, const char *pgmname, const char *fname)
Definition: rle_error.c:76
int scanargs(int argc, char **argv, const char *format,...)
Definition: scanargs.c:94
int xmax
Definition: rle.h:100
int img_num
Definition: rle.h:135
#define index
Definition: rle_config.h:96
void print_codes(rle_hdr *the_hdr)
Definition: rlehdr.c:383
#define RLE_EOF
Definition: rle.h:74
#define CONST_DECL
Definition: rle_config.h:42
unsigned int rle_getskip(rle_hdr *the_hdr)
Definition: rle_getskip.c:57
char * rle_getcom(char *name, rle_hdr *the_hdr) const
Definition: rle_getcom.c:81
int background
Definition: rle.h:100
int ncmap
Definition: rle.h:100
int ymax
Definition: rle.h:100
unsigned char rle_pixel
Definition: rle.h:56
int cmaplen
Definition: rle.h:100
int alpha
Definition: rle.h:100
void print_map(int outalpha, int noutput, int *outchan, char *where)
Definition: rleswap.c:574
unsigned short rle_map
Definition: rle.h:57
const char * file_name
Definition: rle.h:134
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
#define RLE_CHECK_ALLOC(pgm, ptr, name)
Definition: rle.h:86