Utah Raster Toolkit  9999-git
URT Development version (post-3.1b)
painttorle.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  * paint2rle.c - Convert MacPaint images to RLE.
20  *
21  * Author: John W. Peterson
22  * Computer Science Dept.
23  * University of Utah
24  * Date: Wed Oct 15 1986
25  * Copyright (c) 1986, John W. Peterson
26  *
27  * Usage is:
28  * paint2rle [-c [r] [g] [b] [alpha]] [-r] [-o outfile.rle] [infile.paint]
29  *
30  * -r Inverts the pixels in the macpaint file
31  * r,g,b,a Allows the value of the resulting RLE file to be specified.
32  *
33  * The rleflip is needed because MacPaint images have their origin at the
34  * top left instead of the bottom.
35  */
36 
37 #include <stdio.h>
38 #include "rle.h"
39 
40 /* Default color values */
41 int redval = 255, grnval = 255, bluval = 255, alfval = 255;
42 unsigned char in_line[72];
44 int xlat[256];
45 int invert_flag = 0, oflag = 0;
46 
47 void init(), read_scan(), write_scan();
48 
49 void
51 int argc;
52 char *argv[];
53 {
54  char *in_fname = NULL,
55  *out_fname = NULL;
56  int cflag = 0;
57  FILE *infile;
58  int i;
59  rle_hdr hdr;
60 
61  if ( scanargs( argc, argv,
62  "% c%-red%dgreen%dblue%dalpha%d r%- o%-outfile!s infile.paint%s\n(\
63 \tConvert MacPaint file to RLE.\n\
64 \t-c\tSpecify \"white\" color, optionally include alpha value.\n\
65 \t-r\tReverse image (foreground colored, background black).)",
67  &oflag, &out_fname, &in_fname ) == 0)
68  exit(-1);
69 
70  hdr = *rle_hdr_init( (rle_hdr *)NULL );
71  rle_names( &hdr, cmd_name( argv ), in_fname, 0 );
72  infile = rle_open_f( hdr.cmd, in_fname, "r" );
73  hdr.rle_file = rle_open_f(hdr.cmd, out_fname, "w");
74 
75  hdr.xmax = 575;
76  hdr.ymax = 719;
77  hdr.alpha = 1;
78  hdr.ncolors = 3;
79  RLE_SET_BIT( hdr, RLE_ALPHA ); /* So it gets generated */
80 
81  if (rle_row_alloc( &hdr, &outrows ))
82  {
83  fprintf(stderr, "%s: No heap space\n", hdr.cmd);
84  exit(-2);
85  }
86 
87  rle_addhist( argv, (rle_hdr *)NULL, &hdr );
88  rle_put_setup( &hdr );
89  /* initialize bit-twiddling tables */
90  init();
91 
92  /* read and discard 512 byte MacPaint header */
93  for (i=0; i<512; i++)
94  getc(infile);
95 
96  /* Read and process each of the 720 MacPaint scan lines */
97  for (i=0; i < 720; i++)
98  {
99  read_scan( infile );
100  write_scan( &hdr );
101  }
102 
103  rle_puteof( &hdr );
104  exit(0);
105 }
106 
107 /*
108  * Read a line from the MacPaint file, uncompressing the data into in_line
109  */
110 void
111 read_scan( infile )
112 FILE *infile;
113 {
114  int in_pos, count, data_byte;
115 
116  in_pos = 0;
117  while (in_pos < 72)
118  {
119  count = getc(infile);
120  if (count > 127) count -= 256;
121 
122  if (count >= 0) { /* run of raw bytes */
123  count++; /* # of bytes to read */
124  while (count--)
125  in_line[in_pos++] = getc(infile);
126  }
127  else { /* run of repeated byte */
128  count = -count+1; /* repetition factor */
129  data_byte = getc(infile); /* byte to repeat */
130 
131  while (count--)
132  in_line[in_pos++] = data_byte;
133  }
134  }
135 }
136 
137 /*
138  * Write out a scanline
139  */
140 void
141 write_scan( hdr )
142 rle_hdr *hdr;
143 {
144  register int i, j;
145  register int bit;
146  int outval, outpos;
147 
148  /* Convert the array of bits to an array of pixels */
149 
150  for (i = 0; i < 72; i++ )
151  {
152  for (bit = 7; bit >= 0; bit--)
153  {
154  outval = xlat[in_line[i] & 0xff] & 0xff;
155  outval = (outval >> bit) & 1; /* Convert to boolean */
156  if (invert_flag) outval = 1-outval;
157  outpos = i*8 + (7-bit);
158  if (outval)
159  {
160  outrows[RLE_ALPHA][outpos] = alfval;
161  outrows[RLE_RED][outpos] = redval;
162  outrows[RLE_GREEN][outpos] = grnval;
163  outrows[RLE_BLUE][outpos] = bluval;
164  }
165  else
166  {
167  for( j = RLE_ALPHA; j <= RLE_BLUE; j++ )
168  outrows[j][outpos] = 0;
169  }
170  }
171  }
172  rle_putrow( outrows, 576, hdr );
173 }
174 
175 /* Set up some tables for converting bits to bytes */
176 void
177 init()
178 {
179  int bits[8], i, j;
180 
181  /* initialize translation table */
182  j = 1;
183  for( i=0; i<8; i++ )
184  {
185  bits[i] = j;
186  j *= (1 << 1);
187  }
188 
189  for( i=0; i<256; i++ )
190  {
191  if( i & 1 ) xlat[i] = bits[0];
192  else xlat[i] = 0;
193 
194  if( i & 2 ) xlat[i] += bits[1];
195  if( i & 4 ) xlat[i] += bits[2];
196  if( i & 8 ) xlat[i] += bits[3];
197  if( i & 16 ) xlat[i] += bits[4];
198  if( i & 32 ) xlat[i] += bits[5];
199  if( i & 64 ) xlat[i] += bits[6];
200  if( i & 128 ) xlat[i] += bits[7];
201  }
202 }
FILE * rle_open_f(char *prog_name, char *file_name, char *mode)
Definition: rle_open_f.c:216
#define RLE_SET_BIT(glob, bit)
Definition: rle.h:122
int grnval
Definition: painttorle.c:41
void rle_names(rle_hdr *the_hdr, const char *pgmname, const char *fname, int img_num)
Definition: rle_hdr.c:48
unsigned char in_line[72]
Definition: painttorle.c:42
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 bluval
Definition: painttorle.c:41
#define RLE_GREEN
Definition: rle.h:63
#define RLE_BLUE
Definition: rle.h:64
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
#define RLE_RED
Definition: rle.h:62
void rle_putrow(rows, int rowlen, rle_hdr *the_hdr)
Definition: rle_putrow.c:96
int xmax
Definition: rle.h:100
int xlat[256]
Definition: painttorle.c:44
void rle_addhist(argv, rle_hdr *in_hdr, rle_hdr *out_hdr)
Definition: rle_addhist.c:54
int alfval
Definition: painttorle.c:41
int ymax
Definition: rle.h:100
int invert_flag
Definition: painttorle.c:45
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
#define RLE_ALPHA
Definition: rle.h:65
rle_hdr * rle_hdr_init(rle_hdr *the_hdr)
Definition: rle_hdr.c:267
int oflag
Definition: painttorle.c:45
FILE * rle_file
Definition: rle.h:114
unsigned char * outrows[4]
Definition: targatorle.c:100
int ncolors
Definition: rle.h:100
int redval
Definition: painttorle.c:41