Utah Raster Toolkit  9999-git
URT Development version (post-3.1b)
cubitorle.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  * cubitorle.c - Convert cubicomp image to an RLE file.
20  *
21  * Author: Rod Bogart
22  * Computer Science Dept.
23  * University of Utah
24  * Date: Thu Nov 6 1986
25  * Copyright (c) 1986 Rod Bogart
26  *
27  */
28 
29 #include <stdio.h>
30 #include "rle.h"
31 
32 void read_cubi_hdr(), read_cubi_row(), read_cubi_chan(), bit_read();
33 
34 void
36 int argc;
37 char *argv[];
38 {
39  FILE *cubifiles[4];
40  int i, j, oflag=0;
41  rle_pixel ** rows;
42  int xlen;
43  int cubi_xlen, cubi_ylen;
44  char *infname = NULL, *outfname = NULL;
45  char filename[256];
46  rle_hdr hdr;
47 
48  if ( scanargs( argc, argv,
49  "% o%-outfile!s inprefix!s\n(\
50 \tConvert Cubicomp image to URT image. Input image is in 3 files,\n\
51 \tinprefix.r, inprefix.g, inprefix.b.)",
52  &oflag, &outfname, &infname ) == 0 )
53  exit( 1 );
54  hdr = *rle_hdr_init( (rle_hdr *)NULL );
55  rle_names( &hdr, cmd_name( argv ), outfname, 0 );
56  hdr.rle_file = rle_open_f(hdr.cmd, outfname, "w");
57 
58  for ( i = 0; i < 3; i++ )
59  {
60  sprintf( filename, "%s.%c8", infname, "rgb"[i] );
61  cubifiles[i] = rle_open_f( hdr.cmd, filename, "r" );
62  }
63 
64  read_cubi_hdr(cubifiles, &cubi_xlen, &cubi_ylen);
65 
69  rle_dflt_hdr.xmax = cubi_xlen - 1;
71  rle_dflt_hdr.ymax = cubi_ylen - 1;
73 
74  rle_addhist( argv, (rle_hdr *)NULL, &rle_dflt_hdr );
76 
77  if (rle_row_alloc( &rle_dflt_hdr, &rows ))
78  {
79  fprintf( stderr, "%s: malloc failed\n", hdr.cmd );
80  exit( -2 );
81  }
82 
83  for (j=rle_dflt_hdr.ymin; j <= rle_dflt_hdr.ymax; j++)
84  {
85  read_cubi_row( cubifiles, rows );
86  rle_putrow( rows, xlen, &rle_dflt_hdr);
87  }
88 
89 }
90 
91 void
92 read_cubi_hdr(cubifiles, xlen, ylen)
93 FILE *cubifiles[];
94 short *xlen, *ylen;
95 {
96  char junk[128];
97  short xmin, ymin, xmax, ymax;
98 
99  fread(junk, sizeof(char), 12, cubifiles[0]);
100  fread(xlen, sizeof(short), 1, cubifiles[0]);
101  fread(ylen, sizeof(short), 1, cubifiles[0]);
102  fread(&xmin, sizeof(short), 1, cubifiles[0]);
103  fread(&ymin, sizeof(short), 1, cubifiles[0]);
104  fread(&xmax, sizeof(short), 1, cubifiles[0]);
105  fread(&ymax, sizeof(short), 1, cubifiles[0]);
106  fread(junk, sizeof(char), 104, cubifiles[0]);
107 
108  fread(junk, sizeof(char), 128, cubifiles[1]);
109  fread(junk, sizeof(char), 128, cubifiles[2]);
110 }
111 
112 void
113 read_cubi_row(cubifiles, rows)
114 FILE *cubifiles[];
115 rle_pixel ** rows;
116 {
117  read_cubi_chan(cubifiles[0],rows,0);
118  read_cubi_chan(cubifiles[1],rows,1);
119  read_cubi_chan(cubifiles[2],rows,2);
120 }
121 
122 void
123 read_cubi_chan(infile, rows, chan)
124 FILE * infile;
125 rle_pixel **rows;
126 int chan;
127 {
128  static char headchar[3];
129  static int scanfull[3] = {-1, -1, -1};
130  int xpos = 0, bit;
131 
132  while (xpos < 512)
133  {
134  if (scanfull[chan] == -1)
135  headchar[chan] = fgetc(infile);
136 
137  for (bit = 0; bit < 8; bit++)
138  if (scanfull[chan] <= bit)
139  {
140  bit_read(infile, headchar[chan], bit, rows, chan, &xpos);
141  if (xpos >= 512)
142  {
143  scanfull[chan] = bit + 1;
144  break;
145  }
146  }
147  if (bit >= 7) scanfull[chan] = -1;
148  }
149 }
150 
151 void
152 bit_read(infile, headchar, bit, rows, chan, xpos)
153 FILE * infile;
154 char headchar;
155 int bit, chan, *xpos;
156 rle_pixel **rows;
157 {
158  unsigned char runlength, rundata, bytedata;
159  int i;
160 
161  if (headchar & (1 << bit))
162  {
163  /* bit set, run data */
164  rundata = fgetc(infile);
165  runlength = fgetc(infile);
166  for (i=(*xpos); i < runlength+(*xpos); i++)
167  rows[chan][i] = rundata;
168  *xpos += (int) runlength;
169  }
170  else
171  {
172  /* bit not set, byte data */
173  bytedata = fgetc(infile);
174  rows[chan][*xpos] = bytedata;
175  (*xpos)++;
176  }
177 }
FILE * rle_open_f(char *prog_name, char *file_name, char *mode)
Definition: rle_open_f.c:216
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 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
void rle_putrow(rows, int rowlen, rle_hdr *the_hdr)
Definition: rle_putrow.c:96
int xmax
Definition: rle.h:100
void rle_addhist(argv, rle_hdr *in_hdr, rle_hdr *out_hdr)
Definition: rle_addhist.c:54
int ymax
Definition: rle.h:100
unsigned char rle_pixel
Definition: rle.h:56
void rle_put_setup(rle_hdr *the_hdr)
Definition: rle_putrow.c:453
int alpha
Definition: rle.h:100
rle_hdr * rle_hdr_init(rle_hdr *the_hdr)
Definition: rle_hdr.c:267
rle_hdr rle_dflt_hdr
Definition: rle_global.c:66
FILE * rle_file
Definition: rle.h:114
int ncolors
Definition: rle.h:100