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

Go to the source code of this file.

Macros

#define RANDOM_MAX   ((double)(int)(((unsigned int)~0)>>1))
 
#define MALLOC_ERR   RLE_CHECK_ALLOC( cmd_name( argv ), 0, 0 )
 

Functions

void main (int argc, char **argv)
 

Macro Definition Documentation

#define MALLOC_ERR   RLE_CHECK_ALLOC( cmd_name( argv ), 0, 0 )

Definition at line 29 of file rlenoise.c.

#define RANDOM_MAX   ((double)(int)(((unsigned int)~0)>>1))

Definition at line 27 of file rlenoise.c.

Function Documentation

void main ( int  argc,
char **  argv 
)

Definition at line 32 of file rlenoise.c.

35 {
36  int x, y, i;
37  int oflag = 0, noise_amount = 4;
38  char * in_fname = NULL, * out_fname = NULL;
39  FILE *outfile = stdout;
41  rle_pixel **inrows, **outrows;
42  double rand_mult, newpix;
43  int rle_err, rle_cnt;
44 
45  in_hdr = *rle_hdr_init( NULL );
46  out_hdr = *rle_hdr_init( NULL );
47 
48 #ifdef USE_RANDOM
49  /* Use the BSD random() function */
50  {
51  long seed;
52  (void)time( &seed );
53  srandom( *(int *)&seed );
54  }
55 #else
56  /* use the standard Unix rand function if nothing better */
57  {
58  long seed;
59  (void)time( &seed );
60  srand( *(int *)&seed );
61  }
62 #define random rand
63 #endif
64 
65  if ( scanargs( argc, argv, "% n%-amount!d o%-outfile!s infile%s",
66  &noise_amount, &noise_amount, &oflag, &out_fname, &in_fname) == 0 )
67  exit( 1 );
68 
69  rand_mult = noise_amount / RANDOM_MAX;
70  noise_amount /= 2;
71 
72  in_hdr.rle_file = rle_open_f(cmd_name( argv ), in_fname, "r");
73  rle_names( &in_hdr, cmd_name( argv ), in_fname, 0 );
74  rle_names( &out_hdr, in_hdr.cmd, out_fname, 0 );
75 
76  for ( rle_cnt = 0;
77  (rle_err = rle_get_setup( &in_hdr )) == RLE_SUCCESS;
78  rle_cnt++ )
79  {
80  /* Always write to stdout, well almost always */
81  (void)rle_hdr_cp( &in_hdr, &out_hdr );
82  if ( rle_cnt == 0 )
83  outfile = rle_open_f( cmd_name( argv ), out_fname, "w" );
84  out_hdr.rle_file = outfile;
85 
86  rle_addhist( argv, &in_hdr, &out_hdr );
87 
88  /* Init output file */
89  rle_put_setup( &out_hdr );
90 
91  /* Get some memory */
92  if ( rle_row_alloc( &in_hdr, &inrows ) < 0 )
93  MALLOC_ERR;
94  /* Set up the output pointers. This is a pain. */
95  if ( (outrows = ((rle_pixel **) malloc(sizeof(rle_pixel *)
96  * out_hdr.ncolors + 1 ) + 1))
97  == NULL )
98  MALLOC_ERR;
99  for ( i = -out_hdr.alpha; i < out_hdr.ncolors; i++ )
100  if ( inrows[i] )
101  outrows[i] = inrows[i] + out_hdr.xmin;
102 
103  for ( y = in_hdr.ymin; y <= in_hdr.ymax; y++ )
104  {
105  rle_getrow( &in_hdr, inrows );
106  for ( i = 0; i < in_hdr.ncolors; i++ )
107  if ( inrows[i] != NULL )
108  for ( x = in_hdr.xmin; x <= in_hdr.xmax; x++ )
109  {
110  newpix = inrows[i][x] + random() * rand_mult -
111  noise_amount + 0.5;
112  if ( newpix < 0 )
113  newpix = 0;
114  if ( newpix > 255 )
115  newpix = 255;
116  inrows[i][x] = newpix;
117  }
118  rle_putrow( outrows, out_hdr.xmax - out_hdr.xmin + 1,
119  &out_hdr );
120  }
121 
122  rle_puteof( &out_hdr );
123 
124  /* Free memory. */
125  rle_row_free( &in_hdr, inrows );
126  free( outrows - 1 );
127  }
128 
129  /* Check for an error. EOF or EMPTY is ok if at least one image
130  * has been read. Otherwise, print an error message.
131  */
132  if ( rle_cnt == 0 || (rle_err != RLE_EOF && rle_err != RLE_EMPTY) )
133  rle_get_error( rle_err, cmd_name( argv ), in_fname );
134 
135  exit( 0 );
136 }
void rle_row_free(rle_hdr *the_hdr, rle_pixel **scanp)
Definition: rle_row_alc.c:114
int xmin
Definition: rle.h:100
int scanargs(int argc, char **argv, const char *format,...)
Definition: scanargs.c:94
static rle_hdr in_hdr
Definition: rletogif.c:37
FILE * outfile
Definition: giftorle.c:61
int rle_row_alloc(rle_hdr *the_hdr, rle_pixel ***scanp)
Definition: rle_row_alc.c:56
#define RLE_EMPTY
Definition: rle.h:73
void rle_putrow(rle_pixel *rows[], int rowlen, rle_hdr *the_hdr)
void rle_addhist(char *argv[], rle_hdr *in_hdr, rle_hdr *out_hdr)
#define RLE_SUCCESS
Definition: rle.h:70
int ymin
Definition: rle.h:100
void rle_puteof(rle_hdr *the_hdr)
Definition: rle_putrow.c:474
static int y
Definition: getami.c:691
void rle_names(rle_hdr *the_hdr, const char *pgmname, const char *fname, int img_num)
const char * cmd
Definition: rle.h:133
int rle_get_setup(rle_hdr *the_hdr)
Definition: rle_getrow.c:74
Definition: rle.h:96
int xmax
Definition: rle.h:100
static int x
Definition: getami.c:691
#define RANDOM_MAX
Definition: rlenoise.c:27
int rle_get_error(int code, const char *pgmname, const char *fname)
#define RLE_EOF
Definition: rle.h:74
int ymax
Definition: rle.h:100
unsigned char rle_pixel
Definition: rle.h:56
rle_hdr * rle_hdr_cp(rle_hdr *from_hdr, rle_hdr *to_hdr)
Definition: rle_hdr.c:119
void * malloc()
int i
Definition: rletorla.c:82
int alpha
Definition: rle.h:100
FILE * rle_open_f(const char *prog_name, const char *f_name, const char *mode)
void rle_put_setup(rle_hdr *the_hdr)
Definition: rle_putrow.c:453
#define MALLOC_ERR
Definition: rlenoise.c:29
char * cmd_name(char **argv)
Definition: cmd_name.c:31
int oflag
Definition: painttorle.c:45
rle_pixel ** outrows
Definition: painttorle.c:43
int rle_getrow(rle_hdr *the_hdr, rle_pixel *scanline[])
FILE * rle_file
Definition: rle.h:114
rle_hdr out_hdr
Definition: dvirle2.c:89
int ncolors
Definition: rle.h:100
rle_hdr * rle_hdr_init(rle_hdr *the_hdr)
Definition: rle_hdr.c:267