Utah Raster Toolkit  9999-git
URT Development version (post-3.1b)
read98721.c
Go to the documentation of this file.
1 /*
2  * read98721 - read an RLE image from a display device
3  *
4  * Author: Filippo Tampieri
5  * Program of Computer Graphics
6  * Cornell University
7  * Date: Tue June 9 1987
8  *
9  * [-m] - store maps
10  * [-O] - store overlay (doesn't seem to do anything)
11  * [-d display] - display device ("/dev/...")
12  * [-x driver] - driver name ("hp98721")
13  * [-p xstart ystart] - lower left pixel start
14  * [-s xsize ysize] - size of image to store
15  * [-b rbak gbak bbak] - background color
16  * [-o fname] - output filename
17  * [comments ...]
18  *
19  */
20 
21 #include "starbase.c.h"
22 #include <stdio.h>
23 #include "rle.h"
24 
25 #define TRUE 1
26 #define FALSE 0
27 #define MAX(i,j) ((i) > (j) ? (i) : (j))
28 
29 #define NCHANS 3
30 #define NCMAPS 3
31 #define CMAPLEN 8
32 #define COLMAPLEN 256
33 #define MAX_X_SIZE 1280
34 
35 /* if cmap_flag is TRUE the color maps are saved */
37 
40 
41 /* default display device and device driver */
42 char *display_name = "/dev/crt";
43 char *driver_name = "hp98721";
44 
46 
47 /* (xstart,ystart) are the coordinates of the lower left corner of the block to be saved */
48 #define D_XSTART 0
49 #define D_YSTART 0
52 
54 
55 /* xsize and ysize are the size of the block to be saved */
56 #define D_XSIZE 512
57 #define D_YSIZE 480
58 int xsize = D_XSIZE;
59 int ysize = D_YSIZE;
60 
62 
63 /* if backgnd_flag is TRUE the background color given in backgnd[] is not explicitly saved */
65 
67 
69 
70 /* if fname is NULL the image is written to the standard output */
71 char *fname = NULL;
72 
73 int ncomments = 0;
74 char **comments = NULL;
75 
76 /* starbase file descriptor for device from which the picture is read */
78 
80 int argc;
81 char *argv[];
82 {
83  int i,y;
84  int dev_xmax,dev_ymax;
85  rle_hdr the_hdr;
86  rle_map cmap[COLMAPLEN * NCMAPS];
87  unsigned char scanline[NCHANS][MAX_X_SIZE],*rows[NCHANS]; /* Scanline storage & RLE row pointers */
88 
89  if (scanargs(argc, argv,
90  "% m%- O%- d%-display!s x%-driver!s p%-xstart!dystart!d s%-xsize!dysize!d \
91  b%-rbak!dgbak!dbbak!d o%-fname!s comments%*s",
92  &cmap_flag,
98  &backgnd_flag,&(backgnd[0]),&(backgnd[1]),&(backgnd[2]),
100  &ncomments,&comments) == 0)
101  exit(1);
102 
103  the_hdr = *rle_hdr_init( (rle_hdr *)NULL );
104  rle_names( &the_hdr, cmd_name( argv ), fname, 0 );
105 
106  if(overlay_flag && backgnd_flag) {
107  fprintf(stderr,"read98721: incompatible options -O and -b\n");
108  exit(1);
109  }
110 
111  /* initialize the display device */
112  setup_graphics_device(&dev_xmax,&dev_ymax);
113 
114  clip(dev_xmax,dev_ymax,&xstart,&ystart,&xsize,&ysize);
115  if((xsize <= 0)||(ysize <= 0)) {
116  fprintf(stderr,"%s: null image size\n", the_hdr.cmd);
117  exit(1);
118  }
119 
120  /* open output file */
121  the_hdr.rle_file = rle_open_f(the_hdr.cmd, fname, "w");
122  /* initialize image header */
123  the_hdr.ncolors = NCHANS;
124  the_hdr.bg_color = backgnd_flag ? backgnd : (int *)0;
125  the_hdr.alpha = FALSE;
126  the_hdr.background = backgnd_flag ? 2 : (overlay_flag ? 1 : 0);
127  the_hdr.xmin = xstart;
128  the_hdr.xmax = xstart + xsize -1;
129  the_hdr.ymin = ystart;
130  the_hdr.ymax = ystart + ysize -1;
131  if(cmap_flag) {
132  read_color_maps(picture_fd,cmap);
133  the_hdr.ncmap = NCMAPS;
134  the_hdr.cmaplen = CMAPLEN;
135  the_hdr.cmap = cmap;
136  } else {
137  the_hdr.ncmap = 0;
138  the_hdr.cmaplen = 0;
139  the_hdr.cmap = (rle_map *)0;
140  }
141  the_hdr.bits[0] = 0x07;
142  for(i=0; i<ncomments; i++)
143  rle_putcom(comments[i],&the_hdr);
144 
145  rle_addhist( argv, (rle_hdr *)NULL, &the_hdr );
146  rle_put_setup(&the_hdr);
147 
148  /* set up rows to point to our copy of the scanline */
149  for (i= 0; i<NCHANS; i++)
150  rows[i] = scanline[i];
151 
152  /* save image */
153  for(y=the_hdr.ymin; y<=the_hdr.ymax; y++) {
154  read_scanline(y,rows);
155  rle_putrow(rows,xsize,&the_hdr);
156  }
157 
158  rle_puteof(&the_hdr);
159 
160  exit(0);
161 }
162 
163 /* initialize the device to 24 bit/pixel */
165 int *dev_xmax,*dev_ymax;
166 {
167  float p_lim[2][3],res[3],p1[3],p2[3];
168  int map_size;
169 
170  picture_fd = gopen(display_name,OUTDEV,driver_name,0);
171  if(picture_fd < 0) {
172  fprintf(stderr,"read98721: can't open device.\n");
173  exit(1);
174  }
175  shade_mode(picture_fd,CMAP_FULL,FALSE);
176 
177  /* find out info about this device. */
178  inquire_sizes(picture_fd,p_lim,res,p1,p2,&map_size);
179 
180  /* set the screen limits in pixels so we can use starbase in a device
181  * independent way.
182  *
183  * it is assumed that the p_lim values returned by inquire_sizes are the
184  * lower left screen coordinates in (float) pixels and the upper right
185  * screen coordinates in (float) pixels. It is also assumed that the
186  * range of values in x and y coordinates are given from 0 to some
187  * positive maximum value.
188  */
189  *dev_xmax = MAX(p_lim[0][0],p_lim[1][0]) + 1.0;
190  *dev_ymax = MAX(p_lim[0][1],p_lim[1][1]) + 1.0;
191 
192  /* set to use the whole display surface. */
193  mapping_mode(picture_fd,DISTORT);
194 
195  /* set to address the pixels in a device independent way. */
196  vdc_extent(picture_fd,0.0,0.0,0.0,(float)(*dev_xmax)-1.0,
197  (float)(*dev_ymax)-1.0,0.0);
198  clip_rectangle(picture_fd,0.0,(float)(*dev_xmax)-1.0,
199  0.0,(float)(*dev_ymax)-1.0);
200 }
201 
202 /* clip specified region size against maximum device window size */
204 int dev_xmax,dev_ymax,*xstart,*ystart,*xsize,*ysize;
205 {
206  if(*xstart < 0) {
207  *xsize += *xstart;
208  *xstart = 0;
209  }
210  if(*ystart < 0) {
211  *ysize += *ystart;
212  *ystart = 0;
213  }
214  if(*xstart + *xsize > dev_xmax)
215  *xsize = dev_xmax - *xstart;
216  if(*ystart + *ysize > dev_ymax)
217  *ysize = dev_ymax - *ystart;
218 }
219 
220 /* read scanline y from the screen */
222 rle_hdr *the_hdr;
223 int y;
224 unsigned char *rows[];
225 {
226  int i,j;
227 
228  for(i=0,j=NCHANS-1; i<NCHANS; i++,j--) {
229  bank_switch(picture_fd,j,0);
230  block_read(picture_fd,(float)(the_hdr->xmin),(float)y,
231  the_hdr->xmax - the_hdr->xmin + 1,1,rows[i],FALSE);
232  }
233 }
234 
235 /* read the color maps */
237 int pic_fd;
238 rle_map *cmap;
239 {
240  int i,cmaplen;
241  rle_map *rmap,*gmap,*bmap;
242  float colmap[COLMAPLEN][NCMAPS];
243 
244  inquire_color_table(pic_fd,0,COLMAPLEN,colmap);
245  rmap = cmap;
246  gmap = cmap + COLMAPLEN;
247  bmap = cmap + COLMAPLEN * 2;
248  for(i=0; i<COLMAPLEN; i++) {
249  rmap[i] = colmap[i][2];
250  gmap[i] = colmap[i][1];
251  bmap[i] = colmap[i][0];
252  }
253 }
FILE * rle_open_f(char *prog_name, char *file_name, char *mode)
Definition: rle_open_f.c:216
int pos_flag
Definition: read98721.c:45
int cmap_flag
Definition: read98721.c:36
int xmin
Definition: rle.h:100
#define D_YSTART
Definition: read98721.c:49
#define MAX_X_SIZE
Definition: read98721.c:33
void rle_names(rle_hdr *the_hdr, const char *pgmname, const char *fname, int img_num)
Definition: rle_hdr.c:48
int ncomments
Definition: read98721.c:73
char * cmd_name(char **argv)
Definition: cmd_name.c:31
#define D_XSIZE
Definition: read98721.c:56
#define D_YSIZE
Definition: read98721.c:57
void main(int argc, char **argv)
Definition: aliastorle.c:121
#define FALSE
Definition: giftorle.c:39
rle_map * cmap
Definition: rle.h:112
int * bg_color
Definition: rle.h:100
int xsize
Definition: read98721.c:58
int ymin
Definition: rle.h:100
int ystart
Definition: read98721.c:51
#define D_XSTART
Definition: read98721.c:48
int xstart
Definition: read98721.c:50
#define NCMAPS
Definition: read98721.c:30
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
int backgnd_flag
Definition: read98721.c:64
void rle_putrow(rows, int rowlen, rle_hdr *the_hdr)
Definition: rle_putrow.c:96
int xmax
Definition: rle.h:100
int size_flag
Definition: read98721.c:53
char * driver_name
Definition: read98721.c:43
int display_flag
Definition: read98721.c:38
int backgnd[3]
Definition: read98721.c:66
void rle_addhist(argv, rle_hdr *in_hdr, rle_hdr *out_hdr)
Definition: rle_addhist.c:54
char bits[256/8]
Definition: rle.h:128
int fname_flag
Definition: read98721.c:68
#define CMAPLEN
Definition: read98721.c:31
#define MAX(i, j)
Definition: get4d.c:23
clip(int dev_xmax, int dev_ymax, int *xstart, int *ystart, int *xsize, int *ysize)
Definition: read98721.c:203
int background
Definition: rle.h:100
read_color_maps(int pic_fd, rle_map *cmap)
Definition: read98721.c:236
char * display_name
Definition: read98721.c:42
int ncmap
Definition: rle.h:100
int ymax
Definition: rle.h:100
#define NCHANS
Definition: read98721.c:29
int ysize
Definition: read98721.c:59
void rle_put_setup(rle_hdr *the_hdr)
Definition: rle_putrow.c:453
const char * rle_putcom(char *value, rle_hdr *the_hdr) const
Definition: rle_putcom.c:82
char ** comments
Definition: read98721.c:74
int cmaplen
Definition: rle.h:100
int alpha
Definition: rle.h:100
read_scanline(rle_hdr *the_hdr, int y, rows)
Definition: read98721.c:221
int picture_fd
Definition: read98721.c:77
#define COLMAPLEN
Definition: read98721.c:32
unsigned short rle_map
Definition: rle.h:57
int driver_flag
Definition: read98721.c:39
rle_hdr * rle_hdr_init(rle_hdr *the_hdr)
Definition: rle_hdr.c:267
int overlay_flag
Definition: read98721.c:61
FILE * rle_file
Definition: rle.h:114
int ncolors
Definition: rle.h:100
char * fname
Definition: read98721.c:71
setup_graphics_device(int *dev_xmax, int *dev_ymax)
Definition: read98721.c:164