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 int
main(argc,argv)35 main(argc, argv)
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 
66     rle_dflt_hdr.alpha = 0;
67     rle_dflt_hdr.ncolors = 3;
68     rle_dflt_hdr.xmin = 0;
69     rle_dflt_hdr.xmax = cubi_xlen - 1;
70     rle_dflt_hdr.ymin = 0;
71     rle_dflt_hdr.ymax = cubi_ylen - 1;
72     xlen = rle_dflt_hdr.xmax - rle_dflt_hdr.xmin + 1;
73 
74     rle_addhist( argv, (rle_hdr *)NULL, &rle_dflt_hdr );
75     rle_put_setup( &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
read_cubi_hdr(cubifiles,xlen,ylen)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
read_cubi_row(cubifiles,rows)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
read_cubi_chan(infile,rows,chan)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
bit_read(infile,headchar,bit,rows,chan,xpos)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 }
178