Utah Raster Toolkit  9999-git
URT Development version (post-3.1b)
rletogray.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 notices are
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  * rletogray.c - Splits an RLE file into one gray file per channel.
20  *
21  * Author: Michael J. Banks
22  * Computer Science Dept.
23  * University of Utah
24  * Date: Wed Jun 22 1988
25  * Copyright (c) 1988, University of Utah
26  */
27 static char rcsid[] = "$Header: /l/spencer/src/urt/cnv/RCS/rletogray.c,v 3.0.1.2 1992/04/30 13:59:29 spencer Exp $";
28 /*
29 rletogray() Tag the file.
30 */
31 
32 #include <stdio.h>
33 #include "rle.h"
34 
35 typedef FILE *FILPTR;
36 
37 /*
38  * usage : rletogray [-o outprefix] [infile]
39  *
40  * -o outprefix Specifies ouput file name prefix.
41  * infile File to split. If none, uses stdin.
42  */
43 
44 void
46 int argc;
47 char *argv[];
48 {
49  char *inpnam = NULL; /* Input file name. */
50  static char defpref[] = "out"; /* So it's not CONST. */
51  char *prefix = NULL; /* Output file name prefix. */
52  rle_hdr hdr;
53  int aflag = 0; /* Alpha channel flag. */
54  int oflag = 0; /* Output file name prefix flag. */
55  register char * cp, * slashp;
56  FILPTR *outfil; /* Output file pointers. */
57  char outnam[BUFSIZ]; /* Output file name. */
58  int files; /* Number of output files. */
59  int scans, rasts; /* Number of scan lines. */
60  rle_pixel **inprow; /* Input buffer. */
61  int i, row;
62 
63  if (! scanargs( argc,argv,
64  "% o%-outprefix!s infile%s", &oflag, &prefix, &inpnam ))
65  exit( -1 );
66 
67  /* Set up header and open input. */
68  /* Initialize header. */
69  hdr = *rle_hdr_init( (rle_hdr *)NULL );
70  rle_names( &hdr, cmd_name( argv ), inpnam, 0 );
71 
72  hdr.rle_file = rle_open_f(hdr.cmd, inpnam, "r");
73 
74  /* Read header information. */
75 
76  rle_get_setup_ok( &hdr, NULL, NULL );
77  if ( hdr.alpha )
78  aflag = 1;
79  scans = hdr.ymax - hdr.ymin + 1;
80  rasts = hdr.xmax - hdr.xmin + 1;
81  files = aflag + hdr.ncolors;
82 
83  /* Figure out what we want to call the output files. */
84 
85  if ( !inpnam && !oflag )
86  prefix = defpref;
87  else if ( inpnam && !oflag )
88  {
89  /* Strip ".rle" suffix from input file name */
90  /* Avoid strrchr, rindex problem */
91  for ( cp = inpnam; *cp; cp++ )
92  ; /* find end of name */
93  /* Look for last slash */
94  for ( slashp = cp - 1; *slashp != '/' && slashp > inpnam; )
95  slashp--;
96  if ( *slashp == '/' )
97  slashp++;
98  /* Look for last dot */
99  while ( *--cp != '.' && cp > inpnam )
100  ; /* find last . */
101  if ( strcmp( cp, ".rle" ) != 0 )
102  cp = inpnam + strlen( inpnam );
103  /* Make null full string buffer */
104  prefix = (char *)calloc( cp - slashp + 1, 1 );
105  /* Copy everything but suffix */
106  strncpy( prefix, inpnam, cp - slashp );
107  }
108 
109  /*
110  * Get enough file pointers for all output files that are necessary,
111  * and try to open them.
112  */
113 
114  outfil = (FILPTR *)malloc( sizeof( FILPTR ) * files );
115  for ( i = -aflag; i<files-aflag; i++ )
116  {
117  switch( i )
118  {
119  case -1:
120  sprintf( outnam, "%s.alpha", prefix );
121  break;
122 
123  case 0:
124  sprintf( outnam, "%s.red", prefix );
125  break;
126 
127  case 1:
128  sprintf( outnam, "%s.green", prefix );
129  break;
130 
131  case 2:
132  sprintf( outnam, "%s.blue", prefix );
133  break;
134 
135  default:
136  sprintf( outnam, "%s.%03d", prefix, i );
137  break;
138  }
139 
140 
141  if ( (outfil[i+aflag] = fopen( outnam, "w" )) == NULL )
142  {
143  fprintf( stderr, "%s: Can't create %s: ", hdr.cmd, outnam );
144  perror( "" );
145  exit( -1 );
146  }
147  }
148 
149  /* Allocate input buffer. */
150 
151  if (rle_row_alloc( &hdr, &inprow ))
152  {
153  fprintf(stderr, "%s: Out of memory.\n", hdr.cmd);
154  exit(-2);
155  }
156 
157  /* Read .rle file, splitting into gray files. */
158 
159  for (row=0; (row<scans); row++)
160  {
161  rle_getrow( &hdr, inprow );
162  for ( i = -aflag; i<files-aflag; i++ )
163  fwrite( inprow[i], 1, rasts, outfil[i+aflag] );
164  }
165 }
FILE * rle_open_f(char *prog_name, char *file_name, char *mode)
Definition: rle_open_f.c:216
FILE * FILPTR
Definition: rletogray.c:35
int xmin
Definition: rle.h:100
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
void main(int argc, char **argv)
Definition: aliastorle.c:121
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 ymin
Definition: rle.h:100
int scanargs(int argc, char **argv, const char *format,...)
Definition: scanargs.c:94
const char * cmd
Definition: rle.h:133
int xmax
Definition: rle.h:100
void rle_get_setup_ok(rle_hdr *the_hdr, const char *prog_name, const char *file_name)
Definition: rle_getrow.c:254
int ymax
Definition: rle.h:100
unsigned char rle_pixel
Definition: rle.h:56
int alpha
Definition: rle.h:100
static char rcsid[]
Definition: rletogray.c:27
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