Utah Raster Toolkit  9999-git
URT Development version (post-3.1b)
rletops.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  * rletops.c - Convert RLE to postscript file.
20  *
21  * Author: Rod Bogart & John W. Peterson
22  * Computer Science Dept.
23  * University of Utah
24  * Date: Tue Nov 4 1986
25  * Copyright (c) 1986 Rod Bogart
26  *
27  * Modified by: Gregg Townsend
28  * Department of Computer Science
29  * University of Arizona
30  * Date: June 22, 1990
31  * Changes: 50% speedup using putc(), add -C option, translate to page ctr
32  * Fix comments to conform to EPS Version 2.0 (Al Clark)
33  *
34  * Based on "tobw.c" by Spencer Thomas, and
35  * "rps" by Marc Majka (UBC Vision lab)
36  *
37  * EPSF code from Mike Zyda (Naval Postgraduate School)
38  *
39  * Options:
40  * -C - Generate color PostScript (instead of monochrome)
41  * -h X - Image height in inches (default 3", width is found via aspect & size)
42  * -s - Scribe mode: don't generate "showpage", and default center is 3.25
43  * -c X - Center image about X inches on the page (or margin if -s is on).
44  * -a - Aspect ratio of image (default 1.0)
45  *
46  * If an input file isn't specified, it reads from stdin. An output file
47  * can be specified with -o (otherwise it writes to stdout).
48  */
49 #ifndef lint
50 static char rcs_ident[] = "$Id: rletops.c,v 3.0.1.2 1992/01/20 18:04:10 spencer Exp $";
51 #endif
52 
53 #include <stdio.h>
54 #include <math.h>
55 #include "rle.h"
56 
57 void prologue(), puthexpix(), epilogue();
58 
59 static int gencps = 0; /* generate color PostScript? */
60 
61 void
63 int argc;
64 char **argv;
65 {
66  char *infname = NULL,
67  *outfname = NULL;
68  FILE *outfile;
69  rle_hdr hdr;
70  int sflag = 0,
71  oflag = 0,
72  cflag = 0;
73  int add_extra_white_line = 0;
74  float heightinch = 3.0,
75  center = 3.25,
76  aspect = 1.0;
77  int y, nrow, nscan, i, pix, dummy;
78  float x1, y1, x2, y2, widthinch;
79  unsigned char ** scan;
80  unsigned char * buffer;
81 
82  if ( scanargs( argc, argv,
83  "% C%- s%- h%-height!f c%-center!f a%-aspect!f o%-outfile!s infile%s\n(\
84 \tConvert a URT RLE image to PostScript.\n\
85 \t-C\tOutput color PostScript.\n\
86 \t-s\t\"Scribe mode\", no showpage, center about 3.25\" at bottom.\n\
87 \t-h\tScale output to given height in inches.\n\
88 \t-c\tCenter at given distance from left (default 3.25\").\n\
89 \t-a\tPixel aspect ratio (default 1.0).)",
90  &gencps, &sflag, &dummy, &heightinch, &cflag, &center, &dummy,
91  &aspect, &oflag, &outfname, &infname ) == 0 )
92  exit( 1 );
93 
94  /* Open RLE file and get header information */
95  /* Initialize header. */
96  hdr = *rle_hdr_init( (rle_hdr *)NULL );
97  rle_names( &hdr, cmd_name( argv ), infname, 0 );
98 
99  hdr.rle_file = rle_open_f(hdr.cmd, infname, "r");
100  rle_get_setup_ok( &hdr, hdr.cmd, infname );
101 
102  if ( ( hdr.ncolors != 3 ) && ( hdr.ncolors != 1))
103  {
104  fprintf( stderr, "%s is not RGB or b&w image",
105  infname ? infname : "stdin" );
106  exit( 0 );
107  }
108 
109  if ( hdr.ncolors == 1 )
110  gencps = 0; /* can't have color out if no color in! */
111 
112  outfile = rle_open_f(hdr.cmd, outfname, "w");
113 
114  /*
115  * Spencer trick: save space by sliding the input image over to the
116  * left margin.
117  */
118  hdr.xmax -= hdr.xmin;
119  hdr.xmin = 0;
120  nrow = hdr.xmax + 1;
121  nscan = (hdr.ymax - hdr.ymin + 1);
122 
123  /* The laserwriters throw out files with an odd number of scanlines! */
124  if (nscan % 2)
125  {
126  nscan++;
127  add_extra_white_line = 1;
128  }
129 
130  /* Allocate scanline memory */
131 
132  buffer = (unsigned char *)malloc( nrow );
133  rle_row_alloc( &hdr, &scan );
134 
135  /* Calculate image size and placement */
136 
137  widthinch = (float) nrow * heightinch * aspect / (float) nscan;
138  if (sflag)
139  {
140  x1 = center - widthinch / 2.0;
141  y1 = 0.0;
142  }
143  else
144  {
145  if (cflag)
146  x1 = center - widthinch / 2.0;
147  else
148  /* center on whole page */
149  x1 = 4.25 - widthinch / 2.0;
150  /* place top edge one inch from top of page */
151  y1 = 11.0 - 1.0 - heightinch;
152  }
153  x2 = x1 + widthinch;
154  y2 = y1 + heightinch;
155  prologue(outfile,sflag,nscan,nrow,x1,y1,x2,y2);
156 
157  while ( (y = rle_getrow( &hdr, scan )) <=
158  hdr.ymax )
159  {
160  if (gencps) {
161  /* generate a color line */
162  for(pix = 0; pix < nrow; pix ++) {
163  puthexpix(outfile,scan[0][pix]);
164  puthexpix(outfile,scan[1][pix]);
165  puthexpix(outfile,scan[2][pix]);
166  }
167  } else {
168  /* generate a monochrome line */
169  if (hdr.ncolors == 1)
170  buffer = scan[0];
171  else
172  rgb_to_bw( scan[0], scan[1], scan[2], buffer, nrow );
173  for(pix = 0; pix < nrow; pix ++)
174  puthexpix(outfile,buffer[pix]);
175  }
176  }
177  if (add_extra_white_line)
178  for(i = 0; i < (gencps ? 3 : 1); i++)
179  for(pix = 0; pix < nrow; pix ++)
180  puthexpix(outfile,255);
181 
182  epilogue(outfile, sflag);
183 
184  exit( 0 );
185 }
186 
187 void
188 prologue(outfile,scribe_mode,nr,nc,x1,y1,x2,y2)
189 FILE *outfile;
190 int scribe_mode;
191 int nr,nc;
192 float x1,y1,x2,y2;
193 {
194  fprintf(outfile,"%%!\n");
195  fprintf(outfile, "%%%%BoundingBox: %d %d %d %d\n",
196  (int)(x1 * 72), (int)(y1 * 72),
197  (int)(x2 * 72), (int)(y2 * 72));
198  fprintf(outfile, "%%%%EndComments\n");
199  fprintf(outfile,"gsave\n");
200  if (! scribe_mode)
201  fprintf(outfile,"initgraphics\n");
202  fprintf(outfile,"72 72 scale\n");
203  fprintf(outfile,"/imline %d string def\n",nc*2*(gencps?3:1));
204  fprintf(outfile,"/drawimage {\n");
205  fprintf(outfile," %d %d 8\n",nc,nr);
206  fprintf(outfile," [%d 0 0 %d 0 %d]\n",nc,-1*nr,nr);
207  fprintf(outfile," { currentfile imline readhexstring pop } ");
208  if (gencps)
209  fprintf(outfile,"false 3 colorimage\n");
210  else
211  fprintf(outfile,"image\n");
212  fprintf(outfile,"} def\n");
213  fprintf(outfile,"%f %f translate\n",x1,y2);
214  fprintf(outfile,"%f %f scale\n",x2-x1,y1-y2);
215  fprintf(outfile,"drawimage\n");
216 }
217 
218 void
219 epilogue(outfile, scribemode)
220 FILE *outfile;
221 int scribemode;
222 {
223  fprintf(outfile,"\n");
224  if (!scribemode)
225  fprintf(outfile,"showpage\n");
226  fprintf(outfile,"grestore\n");
227 }
228 
229 void
230 puthexpix(outfile,p)
231 FILE *outfile;
232 unsigned char p;
233 {
234  static npixo = 0;
235  static char tohex[] = "0123456789ABCDEF";
236 
237  putc(tohex[(p>>4)&0xF],outfile);
238  putc(tohex[p&0xF],outfile);
239  npixo += 1;
240  if (npixo >= 32) {
241  putc('\n',outfile);
242  npixo = 0;
243  }
244 }
FILE * rle_open_f(char *prog_name, char *file_name, char *mode)
Definition: rle_open_f.c:216
int xmin
Definition: rle.h:100
static int gencps
Definition: rletops.c:59
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
void rgb_to_bw(rle_pixel *red_row, rle_pixel *green_row, rle_pixel *blue_row, rle_pixel *bw_row, int rowlen)
Definition: rle_putrow.c:680
static char rcs_ident[]
Definition: rletops.c:50
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
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