Utah Raster Toolkit  9999-git
URT Development version (post-3.1b)
rle_getskip.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  * rle_getskip.c - Skip scanlines on input.
20  *
21  * Author: Spencer W. Thomas
22  * EECS Dept.
23  * University of Michigan
24  * Date: Wed Jun 27 1990
25  * Copyright (c) 1990, University of Michigan
26  */
27 
28 #include "rle.h"
29 #include "rle_code.h"
30 
31 /* Read a two-byte "short" that started in VAX (LITTLE_ENDIAN) order */
32 #define VAXSHORT( var, fp )
33  { var = fgetc(fp)&0xFF; var |= (fgetc(fp)) << 8; }
34 
35 /* Instruction format -- first byte is opcode, second is datum. */
36 
37 #define OPCODE(inst) (inst[0] & ~LONG)
38 #define LONGP(inst) (inst[0] & LONG)
39 #define DATUM(inst) (inst[1] & 0xff) /* Make sure it's unsigned. */
40 
41 /*****************************************************************
42  * TAG( rle_getskip )
43  *
44  * Skip the next scanline with data on it.
45  * Most useful for skipping to end-of-image.
46  * Inputs:
47  * the_hdr: Describes input image.
48  * Outputs:
49  * Returns the number of the next scanline. At EOF returns 32768.
50  * Assumptions:
51  * rle_get_setup has been called.
52  * Algorithm:
53  * Read input to the beginning of the next scanline, or to EOF or
54  * end of image.
55  */
56 unsigned int
58 rle_hdr *the_hdr;
59 {
60  unsigned char inst[2];
61  register FILE *infile = the_hdr->rle_file;
62  int nc;
63 
64  /* Add in vertical skip from last scanline */
65  if ( the_hdr->priv.get.vert_skip > 0)
66  the_hdr->priv.get.scan_y += the_hdr->priv.get.vert_skip;
67  the_hdr->priv.get.vert_skip = 0;
68 
69  if ( the_hdr->priv.get.is_eof )
70  return 32768; /* too big for 16 bits, signal EOF */
71 
72  /* Otherwise, read and interpret instructions until a skipLines
73  * instruction is encountered.
74  */
75  for (;;)
76  {
77  inst[0] = getc( infile );
78  inst[1] = getc( infile );
79  if ( feof(infile) )
80  {
81  the_hdr->priv.get.is_eof = 1;
82  break; /* <--- one of the exits */
83  }
84 
85  switch( OPCODE(inst) )
86  {
87  case RSkipLinesOp:
88  if ( LONGP(inst) )
89  {
90  VAXSHORT( the_hdr->priv.get.vert_skip, infile );
91  }
92  else
93  the_hdr->priv.get.vert_skip = DATUM(inst);
94  break; /* need to break for() here, too */
95 
96  case RSetColorOp:
97  /* No-op here. */
98  break;
99 
100  case RSkipPixelsOp:
101  if ( LONGP(inst) )
102  {
103  (void)getc( infile );
104  (void)getc( infile );
105  }
106  break;
107 
108  case RByteDataOp:
109  if ( LONGP(inst) )
110  {
111  VAXSHORT( nc, infile );
112  }
113  else
114  nc = DATUM(inst);
115  nc++;
116  if ( the_hdr->priv.get.is_seek )
117  fseek( infile, ((nc + 1) / 2) * 2, 1 );
118  else
119  {
120  register int ii;
121  for ( ii = ((nc + 1) / 2) * 2; ii > 0; ii-- )
122  (void) getc( infile ); /* discard it */
123  }
124 
125  break;
126 
127  case RRunDataOp:
128  if ( LONGP(inst) )
129  {
130  (void)getc( infile );
131  (void)getc( infile );
132  }
133  (void)getc( infile );
134  (void)getc( infile );
135  break;
136 
137  case REOFOp:
138  the_hdr->priv.get.is_eof = 1;
139  break;
140 
141  default:
142  fprintf( stderr,
143  "%s: rle_getskip: Unrecognized opcode: %d, reading %s\n",
144  the_hdr->cmd, OPCODE(inst), the_hdr->file_name );
145  exit(1);
146  }
147  if ( OPCODE(inst) == REOFOp )
148  break; /* <--- the other loop exit */
149  if ( OPCODE(inst) == RSkipLinesOp )
150  break;
151  }
152 
153  /* Return the number of the NEXT scanline. */
154  the_hdr->priv.get.scan_y +=
155  the_hdr->priv.get.vert_skip;
156  the_hdr->priv.get.vert_skip = 0;
157 
158  if ( the_hdr->priv.get.is_eof )
159  return 32768; /* too big for 16 bits, signal EOF */
160  else
161  return the_hdr->priv.get.scan_y;
162 }
#define RSetColorOp
Definition: rle_code.h:38
#define RRunDataOp
Definition: rle_code.h:41
#define RSkipLinesOp
Definition: rle_code.h:37
#define LONGP(inst)
Definition: XtndRunget.c:85
#define REOFOp
Definition: rle_code.h:42
const char * cmd
Definition: rle.h:133
#define RSkipPixelsOp
Definition: rle_code.h:39
unsigned int rle_getskip(rle_hdr *the_hdr)
Definition: rle_getskip.c:57
#define LONG
Definition: rle_code.h:36
#define VAXSHORT(var, fp)
Definition: rle_cp.c:33
#define DATUM(inst)
Definition: XtndRunget.c:86
const char * file_name
Definition: rle.h:134
#define OPCODE(inst)
Definition: XtndRunget.c:84
FILE * rle_file
Definition: rle.h:114
#define RByteDataOp
Definition: rle_code.h:40