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

Go to the source code of this file.

Macros

#define FASTRUNS   /* Faster run finding */
 
#define FALSE   0
 
#define TRUE   1
 
#define PBRUN   the_hdr->priv.put.brun
 

Functions

static int findruns ()
 
void rle_putrow (rows, int rowlen, rle_hdr *the_hdr)
 
void rle_skiprow (rle_hdr *the_hdr, int nrow)
 
void rle_put_init (rle_hdr *the_hdr)
 
void rle_put_setup (rle_hdr *the_hdr)
 
void DefaultBlockHook (rle_hdr *the_hdr)
 
void rle_puteof (rle_hdr *the_hdr)
 
static int findruns (rle_pixel *row, int rowlen, int color, int nrun, short *brun)
 
void rgb_to_bw (rle_pixel *red_row, rle_pixel *green_row, rle_pixel *blue_row, rle_pixel *bw_row, int rowlen)
 

Macro Definition Documentation

#define FALSE   0

Definition at line 44 of file rle_putrow.c.

#define FASTRUNS   /* Faster run finding */

Definition at line 39 of file rle_putrow.c.

#define PBRUN   the_hdr->priv.put.brun

Definition at line 48 of file rle_putrow.c.

#define TRUE   1

Definition at line 45 of file rle_putrow.c.

Function Documentation

void DefaultBlockHook ( rle_hdr the_hdr)

Definition at line 463 of file rle_putrow.c.

465 {
466  /* Do nothing */
467 }
static int findruns ( )
static
static int findruns ( rle_pixel row,
int  rowlen,
int  color,
int  nrun,
short *  brun 
)
static

Definition at line 548 of file rle_putrow.c.

552 {
553  int i = 0, lower, upper;
554  register int s, j;
555 
556 #ifdef DEBUG
557  fprintf( stderr, "findruns( " );
558  for ( s = 0; s < rowlen; s++ )
559  fprintf( stderr, "%2x.%s", row[s], (s % 20 == 19) ? "\n\t" : "" );
560  if ( s % 20 != 0 )
561  fprintf( stderr, "\n\t" );
562  fprintf( stderr, "%d, %d, %d, \n\t", rowlen, color, nrun );
563  for ( j = 0; j < nrun; j++ )
564  fprintf( stderr, "(%3d,%3d) %s", brun[j][0], brun[j][1],
565  (j % 6 == 5) ? "\n\t" : "" );
566  fprintf( stderr, ")\n" );
567 #endif
568 
569  while ( i <= nrun )
570  {
571  /* Assert: 0 <= i <= rowlen
572  * brun[i] is the run following the "blank" space being
573  * searched. If i == rowlen, search after brun[i-1].
574  */
575 
576  /* get lower and upper bounds of search */
577 
578  if ( i == 0 )
579  lower = 0;
580  else
581  lower = brun[i-1][1] + 1;
582 
583  if ( i == nrun )
584  upper = rowlen - 1;
585  else
586  upper = brun[i][0] - 1;
587 
588 #ifdef DEBUG
589  fprintf( stderr, "Searching before run %d from %d to %d\n",
590  i, lower, upper );
591 #endif
592  /* Search for beginning of run != color */
593 #if defined(LOCC)&defined(vax)
594  s = upper - skpc( (char *)row + lower, upper - lower + 1, color ) + 1;
595 #else
596  for ( s = lower; s <= upper; s++ )
597  if ( row[s] != color )
598  break;
599 #endif
600 
601  if ( s <= upper ) /* found a new run? */
602  {
603  if ( s > lower + 1 || i == 0 ) /* disjoint from preceding run? */
604  {
605 #ifdef DEBUG
606  fprintf( stderr, "Found new run starting at %d\n", s );
607 #endif
608  /* Shift following runs up */
609  for ( j = nrun; j > i; j-- )
610  {
611  brun[j][0] = brun[j-1][0];
612  brun[j][1] = brun[j-1][1];
613  }
614  brun[i][0] = s;
615  nrun++;
616  }
617  else
618  {
619  i--; /* just add to preceding run */
620 #ifdef DEBUG
621  fprintf( stderr, "Adding to previous run\n" );
622 #endif
623  }
624 
625 #if defined(LOCC)&defined(vax)
626  s = upper - locc( (char *)row + s, upper - s + 1, color ) + 1;
627 #else
628  for ( ; s <= upper; s++ )
629  if ( row[s] == color )
630  break;
631 #endif
632  brun[i][1] = s - 1;
633 
634 #ifdef DEBUG
635  fprintf( stderr, "Ends at %d", s - 1 );
636 #endif
637  if ( s >= upper && i < nrun - 1 ) /* merge with following run */
638  {
639  brun[i][1] = brun[i+1][1];
640  /* move following runs back down */
641  for ( j = i + 2; j < nrun; j++ )
642  {
643  brun[j-1][0] = brun[j][0];
644  brun[j-1][1] = brun[j][1];
645  }
646  nrun--;
647 #ifdef DEBUG
648  fprintf( stderr, ", add to next run" );
649 #endif
650  }
651 #ifdef DEBUG
652  putc( '\n', stderr );
653 #endif
654  }
655 
656  /* Search in next space */
657  i++;
658  }
659 
660  return nrun;
661 }
_urt_stack * s
Definition: rleClock.c:919
int i
Definition: rletorla.c:82
int row
Definition: rle.c:27
void rgb_to_bw ( rle_pixel red_row,
rle_pixel green_row,
rle_pixel blue_row,
rle_pixel bw_row,
int  rowlen 
)

Definition at line 680 of file rle_putrow.c.

686 {
687  register int x, bw;
688 
689  for (x=0; x<rowlen; x++)
690  {
691  /* 68000 won't store float > 127 into byte? */
692  /* HP compiler blows it */
693  bw = 0.5 + .30*red_row[x] + .59*green_row[x] + .11*blue_row[x];
694  bw_row[x] = bw;
695  }
696 }
static int bw
Definition: getami.c:106
static int x
Definition: getami.c:691
void rle_put_init ( rle_hdr the_hdr)

Definition at line 415 of file rle_putrow.c.

417 {
418  the_hdr->dispatch = RUN_DISPATCH;
419 
420  if ( the_hdr->is_init != RLE_INIT_MAGIC )
421  {
422  the_hdr->cmd = "Urt";
423  the_hdr->file_name = "some file";
424  }
425  the_hdr->priv.put.nblank = 0; /* Reinit static vars */
426  /* Would like to be able to free previously allocated storage,
427  * but can't count on a non-NULL value being a valid pointer.
428  */
429  PBRUN = NULL;
430  the_hdr->priv.put.fileptr = 0;
431 
432  /* Only save alpha if alpha AND alpha channel bit are set. */
433  if ( the_hdr->alpha )
434  the_hdr->alpha = (RLE_BIT( *the_hdr, -1 ) != 0);
435  else
436  RLE_CLR_BIT( *the_hdr, -1 );
437 }
struct rle_hdr::@0::@2 put
long int is_init
Definition: rle.h:131
union rle_hdr::@0 priv
const char * cmd
Definition: rle.h:133
enum rle_dispatch dispatch
Definition: rle.h:99
#define RLE_INIT_MAGIC
Definition: rle.h:79
#define RLE_CLR_BIT(glob, bit)
Definition: rle.h:124
int alpha
Definition: rle.h:100
const char * file_name
Definition: rle.h:134
#define PBRUN
Definition: rle_putrow.c:48
#define RLE_BIT(glob, bit)
Definition: rle.h:126
void rle_put_setup ( rle_hdr the_hdr)

Definition at line 453 of file rle_putrow.c.

Referenced by find_most_used(), and write_rle_header().

455 {
456  rle_put_init( the_hdr );
457  the_hdr->img_num++; /* Count output images. */
458  Setup();
459 }
int img_num
Definition: rle.h:135
void rle_put_init(rle_hdr *the_hdr)
Definition: rle_putrow.c:415
#define Setup()
Definition: rle_put.h:75

Here is the caller graph for this function:

void rle_puteof ( rle_hdr the_hdr)

Definition at line 474 of file rle_putrow.c.

Referenced by FormFeed(), rasterDone(), and write_rle_data().

476 {
477  /* Don't puteof twice. */
478  if ( the_hdr->dispatch == NO_DISPATCH )
479  return;
480  PutEof();
481  fflush( the_hdr->rle_file );
482  /* Free storage allocated by rle_put_init. */
483  if ( PBRUN != NULL )
484  {
485  free( PBRUN );
486  PBRUN = NULL;
487  }
488  /* Signal that puteof has been called. */
489  the_hdr->dispatch = NO_DISPATCH;
490 }
#define PutEof()
Definition: rle_put.h:83
enum rle_dispatch dispatch
Definition: rle.h:99
#define PBRUN
Definition: rle_putrow.c:48
FILE * rle_file
Definition: rle.h:114

Here is the caller graph for this function:

void rle_putrow ( rows  ,
int  rowlen,
rle_hdr the_hdr 
)

Definition at line 96 of file rle_putrow.c.

Referenced by find_most_used(), and write_rle_data().

100 {
101  register int i, j;
102  int nrun;
103  register rle_pixel *row;
104  int mask;
105  char bits[256];
106  short state, /* State of run-finding state machine. */
107  dstart, /* Starting point for current data segment. */
108  dend, /* Ending point of current data segment. */
109  rstart = 0, /* Starting point of current run. */
110  runval = 0; /* Data value for current run. */
111 
112  if (rows == NULL)
113  {
114  the_hdr->priv.put.nblank += rowlen;
115  return;
116  }
117  /*
118  * If not done already, allocate space to remember runs of
119  * non-background color. A run of bg color must be at least 2
120  * bytes long to count, so there can be at most rowlen/3 of them.
121  */
122  if ( PBRUN == NULL )
123  {
124  PBRUN = (short (*)[2])malloc(
125  (unsigned)((rowlen/3 + 1) * 2 * sizeof(short)) );
126  if ( PBRUN == NULL )
127  {
128  fprintf( stderr, "%s: Malloc failed in rle_putrow, writing %s\n",
129  the_hdr->cmd, the_hdr->file_name );
130  exit(1);
131  }
132  }
133  /* Unpack bitmask in the_hdr struct */
134  for ( i=0; i < the_hdr->ncolors; i++ )
135  bits[i] = RLE_BIT( *the_hdr, i );
136  bits[255] = RLE_BIT( *the_hdr, -1 );
137 
138  /*
139  * If saving only non-background pixels, find runs of them. Note
140  * that the alpha channel is considered to be background iff it is
141  * zero.
142  */
143 #ifdef FASTRUNS
144  if ( the_hdr->background )
145  {
146  /*
147  * Find runs in each color individually, merging them as we go.
148  */
149  nrun = 0; /* start out with no runs */
150  /* Alpha channel first */
151  if ( the_hdr->alpha )
152  nrun = findruns( rows[-1], rowlen, 0, nrun, PBRUN );
153  /* Now the color channels */
154  for ( i = 0; i < the_hdr->ncolors; i++ )
155  if ( bits[i] )
156  nrun = findruns( rows[i], rowlen, the_hdr->bg_color[i],
157  nrun, PBRUN );
158  }
159  else
160  {
161  PBRUN[0][0] = 0;
162  PBRUN[0][1] = rowlen-1;
163  nrun = 1;
164  }
165 #else /* FASTRUNS */
166  if (the_hdr->background) /* find non-background runs */
167  {
168  j = 0;
169  for (i=0; i<rowlen; i++)
170  if (!same_color( i, rows, the_hdr->bg_color,
171  the_hdr->ncolors, bits ) ||
172  (the_hdr->alpha && rows[-1][i] != 0))
173  {
174  if (j > 0 && i - PBRUN[j-1][1] <= 2)
175  j--;
176  else
177  PBRUN[j][0] = i; /* start of run */
178  for ( i++;
179  i < rowlen &&
180  ( !same_color( i, rows, the_hdr->bg_color,
181  the_hdr->ncolors, bits ) ||
182  (the_hdr->alpha && rows[-1][i] != 0) );
183  i++)
184  ; /* find the end of this run */
185  PBRUN[j][1] = i-1; /* last in run */
186  j++;
187  }
188  nrun = j;
189  }
190  else
191  {
192  PBRUN[0][0] = 0;
193  PBRUN[0][1] = rowlen-1;
194  nrun = 1;
195  }
196 #endif /* FASTRUNS */
197  /* One final pass merges runs with fewer than 4 intervening pixels
198  * if the second run is longer than 255 pixels. This is because
199  * the startup cost for such a run is 4 bytes.
200  */
201  if ( nrun > 1 )
202  {
203  for ( i = nrun - 1; i > 0; i-- )
204  {
205  if ( PBRUN[i][1] - PBRUN[i][0] > 255 &&
206  PBRUN[i-1][1] + 4 > PBRUN[i][0] )
207  {
208  PBRUN[i-1][1] = PBRUN[i][1];
209  for ( j = i; j < nrun - 1; j++ )
210  {
211  PBRUN[j][0] = PBRUN[j+1][0];
212  PBRUN[j][1] = PBRUN[j+1][1];
213  }
214  nrun--;
215  }
216  }
217  }
218 
219  if (nrun > 0)
220  {
221  if (the_hdr->priv.put.nblank > 0)
222  {
223  SkipBlankLines(the_hdr->priv.put.nblank);
224  the_hdr->priv.put.nblank = 0;
225  }
226  for ( mask = (the_hdr->alpha ? -1 : 0);
227  mask < the_hdr->ncolors;
228  mask++) /* do all colors */
229  {
230  if ( ! bits[mask & 0xff] )
231  {
232  continue;
233  }
234  row = rows[mask];
235  SetColor(mask);
236  if (PBRUN[0][0] > 0)
237  {
238  SkipPixels(PBRUN[0][0], FALSE, FALSE);
239  }
240  for (j=0; j<nrun; j++)
241  {
242  state = DATA;
243  dstart = PBRUN[j][0];
244  dend = PBRUN[j][1];
245  for (i=dstart; i<=dend; i++)
246  {
247  switch(state)
248  {
249  case DATA:
250  if (i > dstart && runval == row[i])
251  {
252  /* 2 in a row may be a run. */
253  /* If odd data length, start with RUN1 */
254  if ( ((i - dstart) % 2) == 0)
255  state = RUN1;
256  else
257  state = RUN2;
258  }
259  else
260  {
261  runval = row[i]; /* maybe a run starts here? */
262  rstart = i;
263  }
264  break;
265 
266  case RUN4:
267  if (runval == row[i])
268  {
269  /* If the following data might be longer
270  * than 255 pixels then look for 8 in a
271  * row, otherwise, 6 in a row is
272  * sufficient. Fake this by skipping to
273  * state RUN5.
274  */
275  if ( dend - i > 255 )
276  state = RUN5; /* Need some more. */
277  else
278  state = RUN7; /* Next one makes a run. */
279 
280  }
281  else
282  {
283  state = DATA; /* Nope, back to data */
284  runval = row[i]; /* but maybe a new run here? */
285  rstart = i;
286  }
287  break;
288 
289  case RUN1:
290  case RUN2:
291  case RUN3:
292  case RUN5:
293  case RUN6:
294  if (runval == row[i])
295  {
296  /* Move to the next state. */
297  state++;
298  }
299  else
300  {
301  state = DATA; /* Nope, back to data */
302  runval = row[i]; /* but maybe a new run here? */
303  rstart = i;
304  }
305  break;
306 
307 
308  case RUN7:
309  if (runval == row[i]) /* enough in a row for a run */
310  {
311  state = INRUN;
312  putdata(row + dstart, rstart - dstart);
313 #ifdef FASTRUNS
314 #ifdef LOCC
315  /* Shortcut to find end of run! */
316  i = dend - skpc( (char *)row + i, dend + 1 - i,
317  runval );
318 #else
319  while ( row[++i] == runval && i <= dend)
320  ; /* not quite so good, but not bad */
321  i--;
322 #endif /* LOCC */
323 #endif /* FASTRUNS */
324  }
325  else
326  {
327  state = DATA; /* not a run, */
328  runval = row[i]; /* but may this starts one */
329  rstart = i;
330  }
331  break;
332 
333  case INRUN:
334  if (runval != row[i]) /* if run out */
335  {
336  state = DATA;
337  putrun(runval, i - rstart, FALSE);
338  runval = row[i]; /* who knows, might be more */
339  rstart = i;
340  dstart = i; /* starting a new 'data' run */
341  }
342  break;
343  }
344  }
345  if (state == INRUN)
346  putrun(runval, i - rstart, TRUE); /* last bit */
347  else
348  putdata(row + dstart, i - dstart);
349 
350  if (j < nrun-1)
351  SkipPixels(
352  PBRUN[j+1][0] - dend - 1,
353  FALSE, state == INRUN);
354  else
355  {
356  if (rowlen - dend > 0)
357  SkipPixels(
358  rowlen - dend - 1,
359  TRUE, state == INRUN);
360  }
361  }
362 
363  if ( mask != the_hdr->ncolors - 1 )
365  }
366  }
367 
368  /* Increment to next scanline */
369  the_hdr->priv.put.nblank++;
370 
371  /* flush every scanline */
372  fflush( the_hdr->rle_file );
373 }
static unsigned mask
Definition: mcut.c:162
#define DATA
Definition: rle_put.h:88
#define SkipPixels(n, l, r)
Definition: rle_put.h:78
#define RUN6
Definition: rle_put.h:94
struct rle_hdr::@0::@2 put
#define RUN1
Definition: rle_put.h:89
#define putrun(val, len, f)
Definition: rle_put.h:81
int * bg_color
Definition: rle.h:100
#define SetColor(c)
Definition: rle_put.h:77
#define FALSE
Definition: rle_putrow.c:44
#define RUN2
Definition: rle_put.h:90
union rle_hdr::@0 priv
const char * cmd
Definition: rle.h:133
#define RUN4
Definition: rle_put.h:92
#define SkipBlankLines(n)
Definition: rle_put.h:76
static int findruns()
rle_pixel ** rows
Definition: rletopaint.c:57
#define NewScanLine(flag)
Definition: rle_put.h:79
#define TRUE
Definition: rle_putrow.c:45
int background
Definition: rle.h:100
unsigned char rle_pixel
Definition: rle.h:56
#define putdata(buf, len)
Definition: rle_put.h:80
#define RUN7
Definition: rle_put.h:95
void * malloc()
int i
Definition: rletorla.c:82
int alpha
Definition: rle.h:100
#define RUN3
Definition: rle_put.h:91
const char * file_name
Definition: rle.h:134
int row
Definition: rle.c:27
#define PBRUN
Definition: rle_putrow.c:48
#define RUN5
Definition: rle_put.h:93
#define INRUN
Definition: rle_put.h:96
FILE * rle_file
Definition: rle.h:114
int ncolors
Definition: rle.h:100
#define RLE_BIT(glob, bit)
Definition: rle.h:126

Here is the caller graph for this function:

void rle_skiprow ( rle_hdr the_hdr,
int  nrow 
)

Definition at line 393 of file rle_putrow.c.

Referenced by write_rle_data().

396 {
397  the_hdr->priv.put.nblank += nrow;
398 }
struct rle_hdr::@0::@2 put
int nrow
Definition: getsun.c:82
union rle_hdr::@0 priv

Here is the caller graph for this function: