Utah Raster Toolkit  9999-git
URT Development version (post-3.1b)
Macros | Functions | Variables
rletopaint.c File Reference
#include <stdio.h>
#include "rle.h"
#include <math.h>
Include dependency graph for rletopaint.c:

Go to the source code of this file.

Macros

#define MIN(x, y)   ( (x) < (y) ? (x) : (y) )
 
#define SAME   1
 
#define NOTSAME   0
 

Functions

int compress_line ()
 
void write_paint_line ()
 
void bytes_to_bits ()
 
void main (int argc, argv)
 
void write_paint_line (int num, FILE *fp)
 
void bytes_to_bits (int y)
 

Variables

static char rcsid [] = "$Header: /tmp_mnt/n/itn/hendrix/u/spencer/RCS/rletopaint.c,v 3.0.1.3 1992/04/30 14:00:05 spencer Exp spencer $"
 
rle_hdr hdr
 
rle_pixel ** rows
 
int gammamap [256]
 
unsigned char rawbits [73]
 
unsigned char squishedbits [74]
 
int inverse_flag = 0
 
int dmgray [8][8]
 

Macro Definition Documentation

#define MIN (   x,
  y 
)    ( (x) < (y) ? (x) : (y) )

Definition at line 53 of file rletopaint.c.

#define NOTSAME   0
#define SAME   1

Function Documentation

void bytes_to_bits ( )
void bytes_to_bits ( int  y)

Definition at line 171 of file rletopaint.c.

173 {
174  register unsigned char *r, *g, *b;
175  register int i, col, row;
176  int pixel;
177  int n = MIN(hdr.xmax, 576);
178  int B = 0, W = 1;
179 
180  if (inverse_flag)
181  {
182  B = 1; W = 0; /* Swap meaning of Black and White */
183  }
184  for (i = 0; i < 72; i++)
185  rawbits[i] = 0;
186 
187  r = g = b = rows[0];
188  if ( hdr.ncolors > 1 )
189  g = rows[1];
190  if ( hdr.ncolors > 2 )
191  b = rows[2];
192  for ( row = y % 8, col = 0, i = 0;
193  i < n;
194  i++, r++, g++, b++, col = ((col + 1) & 7) )
195  {
196  /* Conver to BW (uses YIQ/percentage xformation) */
197  if ( hdr.ncolors > 2 )
198  pixel = (35*gammamap[*r] + 55*gammamap[*g] + 10*gammamap[*b]) / 100;
199  else if ( hdr.ncolors == 2 )
200  pixel = (gammamap[*r] + gammamap[*g]) / 2;
201  else
202  pixel = gammamap[*r];
203  if (pixel < 0) pixel += 256;
204 
205  /* Use dither to convert to zero/one */
206  pixel = ((pixel > dmgray[col][row]) ? W : B);
207 
208  /* Compress scanline bytes to unencoded macpaint bits */
209  rawbits[(i / 8)] |= pixel << (7-(i % 8));
210  }
211 }
int inverse_flag
Definition: rletopaint.c:62
#define MIN(x, y)
Definition: rletopaint.c:53
static unsigned char g
Definition: getami.c:692
int gammamap[256]
Definition: rletopaint.c:59
unsigned char rawbits[73]
Definition: rletopaint.c:60
static unsigned char r
Definition: getami.c:692
static int y
Definition: getami.c:691
static unsigned char b
Definition: getami.c:692
static unsigned int B[32]
Definition: getami.c:98
int dmgray[8][8]
Definition: rletopaint.c:65
int xmax
Definition: rle.h:100
rle_hdr hdr
Definition: rletopaint.c:56
rle_pixel ** rows
Definition: rletopaint.c:57
int i
Definition: rletorla.c:82
int row
Definition: rle.c:27
int ncolors
Definition: rle.h:100
int compress_line ( )

Definition at line 224 of file rletopaint.c.

References rawbits, and squishedbits.

225 {
226  int i,j,k = 0,cntpsn,count = 0;
227  int flag;
228  unsigned char pixel;
229 #define SAME 1
230 #define NOTSAME 0
231 
232  i = 0;
233  j = 2;
234  if( rawbits[0] == rawbits[1] )
235  {
236  flag = SAME;
237  cntpsn = 0;
238  squishedbits[1] = rawbits[0];
239  }
240  else
241  {
242  flag = NOTSAME;
243  cntpsn = 0;
244  squishedbits[1] = rawbits[0];
245  }
246 
247  while( i < 72 )
248  {
249  switch( flag )
250  {
251 
252  /* Same case see how far the run goes then update stuff */
253 
254  case SAME:
255  count = 0;
256  for( k=i; k<72; k++ )
257  {
258  if( squishedbits[j-1] != rawbits[k] )
259  break;
260  else
261  count++;
262  }
263 
264  /* If count is zero then just skip all this stuff and try again
265  * with the flag the other way
266  */
267  if( count != 0 )
268  {
269 
270  /* Ok update the count and save the byte in the output line
271  * NOTE: Count is just 2's compliment of the value
272  */
273  pixel = -1 * (count-1);
274  squishedbits[cntpsn] = 0xff & pixel;
275 
276  /* Set the flag for the other & advance j to next frame */
277 
278  flag = NOTSAME;
279  }
280  else
281  flag = NOTSAME;
282  break;
283 
284  /* Not the same, look for a run of something if found quit */
285 
286  case NOTSAME:
287  count = 0;
288  for( k=i+1; k<72; k++ )
289  {
290  if( squishedbits[j-1] == rawbits[k] )
291  break;
292  else
293  {
294  count++;
295  squishedbits[j++] = rawbits[k];
296  }
297  }
298  /* If count is zero then skip all the updating stuff and just try
299  * the other method
300  */
301  if( count != 0 )
302  {
303  if ( k == 72 )
304  squishedbits[cntpsn] = count;
305  else
306  {
307  squishedbits[cntpsn] = count - 1;
308 
309  /* Set the flag for the other and back up the psn
310  * to get the start of the run.
311  */
312 
313  k = k - 1;
314  j--;
315  flag = SAME;
316  }
317  }
318  else
319  flag = SAME;
320  break;
321  }
322 
323 
324  /* End of loop update the positions of the count save lcn and
325  * next character to look at
326  *
327  * Only do update on non zero counts
328  */
329 
330  if( count != 0 )
331  {
332  /* Sometimes 'compression' doesn't. Check for this. */
333  if ( j > k + 1 )
334  {
335  squishedbits[0] = k - 1;
336  for ( j = 0; j < k; j++ )
337  squishedbits[j + 1] = rawbits[j];
338  j++;
339  }
340  cntpsn = j;
341  squishedbits[++j] = rawbits[k];
342  j++;
343  i = k;
344  }
345  }
346 
347  return( j-2 );
348 }
unsigned char rawbits[73]
Definition: rletopaint.c:60
#define NOTSAME
#define SAME
unsigned char squishedbits[74]
Definition: rletopaint.c:61
int i
Definition: rletorla.c:82
void main ( int  argc,
argv   
)

Definition at line 78 of file rletopaint.c.

81 {
82  int i;
83  int databytes;
84  float gam = 2.0; /* Default gamma */
85  int gamma_flag = 0, oflag = 0;
86  int linear_flag = 1;
87  char * fname = NULL, * out_fname = NULL;
88  FILE * out_fp;
89 
90  if ( scanargs( argc, argv, "% r%- l%- g%-gamma%f o%-outfile!s infile%s",
91  &inverse_flag, &linear_flag,
92  &gamma_flag, &gam, &oflag, &out_fname, &fname ) == 0)
93  exit(-1);
94 
95  if (gamma_flag) linear_flag = 0;
96  /* Initialize header. */
97  hdr = *rle_hdr_init( (rle_hdr *)NULL );
98  rle_names( &hdr, cmd_name( argv ), fname, 0 );
99 
100  hdr.rle_file = rle_open_f(hdr.cmd, fname, "r");
101  rle_get_setup_ok( &hdr, NULL, NULL );
102 
103  out_fp = rle_open_f(hdr.cmd, out_fname, "w");
104 
105  /* set up rows to point to our copy of the scanline */
106  if (rle_row_alloc( &hdr, &rows ))
107  {
108  fprintf(stderr,"%s: malloc failed\n", hdr.cmd);
109  exit(-2);
110  }
111 
112  /* Compute a gamma correction map and tables */
113  for ( i = 0; i < 256; i++ )
114  {
115  if (linear_flag)
116  gammamap[i] = i;
117  else
118  gammamap[i] = (int)(0.5 + 255 * pow( i / 255.0, 1.0/gam ));
119  }
120 
121  /* Output MacPaint header */
122  for (i = 0; i < 512; i++)
123  putc('\0', out_fp); /* Null tells MacPaint to ignore header */
124 
125  for (i = 0; i < 72; i++) /* Kludge - pad bottom with blank lines */
126  rawbits[i] = 0;
127 
128  /* If the picture doesn't start at the bottom, output blank lines
129  * to fill the space */
130 
131  databytes = compress_line(); /* Blank compressed data */
132  for (i = 0; i < hdr.ymin; i++)
133  write_paint_line(databytes, out_fp);
134 
135  for (i = hdr.ymin; i <= MIN( hdr.ymax, 719 ); i++)
136  {
137  rle_getrow( &hdr, rows );
138  bytes_to_bits( i );
139  write_paint_line(compress_line(), out_fp);
140  }
141 
142  /* Finish up unused picture space */
143  if (hdr.ymax < 719)
144  {
145  for (i = 0; i < 72; i++) /* Kludge - pad bottom with blank lines */
146  rawbits[i] = 0;
147 
148  databytes = compress_line(); /* Blank compressed data */
149  for (i = hdr.ymax+1; i < 720; i++)
150  write_paint_line(databytes, out_fp);
151  }
152 }
int inverse_flag
Definition: rletopaint.c:62
#define MIN(x, y)
Definition: rletopaint.c:53
int scanargs(int argc, char **argv, const char *format,...)
Definition: scanargs.c:94
int gammamap[256]
Definition: rletopaint.c:59
unsigned char rawbits[73]
Definition: rletopaint.c:60
int rle_row_alloc(rle_hdr *the_hdr, rle_pixel ***scanp)
Definition: rle_row_alc.c:56
void bytes_to_bits()
float gam
Definition: get_orion.c:55
boolean linear_flag
Definition: get_orion.c:58
char * fname[3]
Definition: show3.c:31
int ymin
Definition: rle.h:100
void rle_names(rle_hdr *the_hdr, const char *pgmname, const char *fname, int img_num)
const char * cmd
Definition: rle.h:133
Definition: rle.h:96
rle_hdr hdr
Definition: rletopaint.c:56
rle_pixel ** rows
Definition: rletopaint.c:57
int
Definition: getami.c:848
int ymax
Definition: rle.h:100
int i
Definition: rletorla.c:82
FILE * rle_open_f(const char *prog_name, const char *f_name, const char *mode)
char * cmd_name(char **argv)
Definition: cmd_name.c:31
void write_paint_line()
int compress_line()
Definition: rletopaint.c:224
boolean gamma_flag
Definition: get_orion.c:59
int oflag
Definition: painttorle.c:45
void rle_get_setup_ok(rle_hdr *the_hdr, const char *prog_name, const char *file_name)
int rle_getrow(rle_hdr *the_hdr, rle_pixel *scanline[])
FILE * rle_file
Definition: rle.h:114
rle_hdr * rle_hdr_init(rle_hdr *the_hdr)
Definition: rle_hdr.c:267
void write_paint_line ( )
void write_paint_line ( int  num,
FILE *  fp 
)

Definition at line 157 of file rletopaint.c.

160 {
161  int j;
162  for (j = 0; j < num; j++)
163  putc(squishedbits[j], fp);
164 }
unsigned char squishedbits[74]
Definition: rletopaint.c:61
FILE * fp
Definition: pgmtorle.c:49

Variable Documentation

int dmgray[8][8]
Initial value:
= { 0 ,128 , 32 ,160 , 8 ,136 , 40 ,168 ,
192 , 64 ,224 , 96 ,200 , 72 ,232 ,104 ,
48 ,176 , 16 ,144 , 56 ,184 , 24 ,152 ,
240 ,112 ,208 , 80 ,248 ,120 ,216 , 88 ,
12 ,140 , 44 ,172 , 4 ,132 , 36 ,164 ,
204 , 76 ,236 ,108 ,196 , 68 ,228 ,100 ,
60 ,188 , 28 ,156 , 52 ,220 , 20 ,148 ,
252 ,124 ,220 , 92 ,244 ,116 ,212 ,84 }

Definition at line 65 of file rletopaint.c.

int gammamap[256]

Definition at line 59 of file rletopaint.c.

rle_hdr hdr

Definition at line 56 of file rletopaint.c.

int inverse_flag = 0

Definition at line 62 of file rletopaint.c.

unsigned char rawbits[73]

Definition at line 60 of file rletopaint.c.

Referenced by compress_line().

char rcsid[] = "$Header: /tmp_mnt/n/itn/hendrix/u/spencer/RCS/rletopaint.c,v 3.0.1.3 1992/04/30 14:00:05 spencer Exp spencer $"
static

Definition at line 43 of file rletopaint.c.

rle_pixel** rows

Definition at line 57 of file rletopaint.c.

unsigned char squishedbits[74]

Definition at line 61 of file rletopaint.c.

Referenced by compress_line().