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

Go to the source code of this file.

Macros

#define incr(bp, fudge)
 

Functions

 WriteVICARHeader (FILE *fd, int width, int height, int BandsPerPixel)
 
static void WriteVICARScanLine (FILE *fd, unsigned char *VICARScanLine, int VICARScanLineLength)
 
static unsigned char * read_image (rle_hdr *the_hdr)
 
static void write_image (rle_hdr *the_hdr, FILE *outFD, unsigned char *VICARImage)
 
int main (int argc, argv)
 

Variables

int VERBOSE = 0
 
static int LBLSIZE
 

Macro Definition Documentation

#define incr (   bp,
  fudge 
)
Value:
bp += strlen( bp ); \
if ( bp - buffer + fudge > LBLSIZE ) \
{ \
bp = buffer = realloc( buffer, LBLSIZE += width ); \
bp += strlen( bp ); \
}
int width
Definition: pgmtorle.c:51
static int LBLSIZE
Definition: rletovcr.c:58
unsigned char * buffer
Definition: getsun.c:87

Function Documentation

int main ( int  argc,
argv   
)

Definition at line 253 of file rletovcr.c.

256 {
257  char *infname = NULL, outfname = NULL;
258  int oflag = 0;
259  unsigned char *VICARImage;
261  FILE *outFD;
262 
263  if ( scanargs( argc, argv, "% v%- o%-outfile!s infile%s\n(\
264 \tConvert URT image to VICAR format (as currently understood).)",
265  &VERBOSE, &oflag, &outfname, &infname ) == 0 )
266  exit( 1 );
267 
268  the_hdr = *rle_hdr_init( (rle_hdr *)NULL );
269  rle_names( &the_hdr, cmd_name( argv ), infname );
270 
271  the_hdr.rle_file = rle_open_f( the_hdr.cmd, infname, "r" );
272 
273  VICARImage = read_image( &the_hdr );
274 
275  outFD = rle_open_f( the_hdr.cmd, outfname, "w" );
276 
277  write_image( &the_hdr, outFD, VICARImage );
278 
279  exit(0);
280 }
rle_hdr the_hdr
Definition: aliastorle.c:100
int scanargs(int argc, char **argv, const char *format,...)
Definition: scanargs.c:94
static unsigned char * read_image(rle_hdr *the_hdr)
Definition: rletovcr.c:173
string infname
Definition: getbob.c:68
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
int VERBOSE
Definition: rletovcr.c:53
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
static void write_image(rle_hdr *the_hdr, FILE *outFD, unsigned char *VICARImage)
Definition: rletovcr.c:221
int oflag
Definition: painttorle.c:45
FILE * rle_file
Definition: rle.h:114
rle_hdr * rle_hdr_init(rle_hdr *the_hdr)
Definition: rle_hdr.c:267
static unsigned char* read_image ( rle_hdr the_hdr)
static

Definition at line 173 of file rletovcr.c.

175 {
176  int y,width,height;
177  unsigned char *VICARImage;
178  rle_pixel **rows;
179 
180  /* Read the RLE file header. */
181  rle_get_setup_ok( the_hdr, NULL, NULL );
182  /* Don't read alpha channel. */
183  RLE_CLR_BIT( *the_hdr, RLE_ALPHA );
184 
185  /* Sanity check. */
186  if ( the_hdr->ncolors > 1 || the_hdr->cmap )
187  {
188  fprintf( stderr,
189  "%s: Only black & white images can be converted to VICAR.\n",
190  the_hdr->cmd );
191  if ( the_hdr->ncolors > 1 )
192  fprintf( stderr, "\t%s has %d colors.\n",
193  the_hdr->file_name, the_hdr->ncolors );
194  else
195  fprintf( stderr, "\t%s has a color map.\n",
196  the_hdr->file_name );
197  exit( 1 );
198  }
199 
200  /* Shift image over to save space. */
201  the_hdr->xmax -= the_hdr->xmin;
202  the_hdr->xmin = 0;
203  width = the_hdr->xmax + 1;
204  height = the_hdr->ymax - the_hdr->ymin + 1;
205 
206  VICARImage = (unsigned char *) malloc(width * height);
207  RLE_CHECK_ALLOC( the_hdr->cmd, VICARImage, "image" );
208  rows = (rle_pixel **)malloc( the_hdr->ncolors * sizeof(rle_pixel *));
209 
210  for ( y = the_hdr->ymin; y <= the_hdr->ymax; y++ )
211  {
212  rows[0] = VICARImage + width * (the_hdr->ymax - y);
213  rle_getrow( the_hdr, rows );
214  }
215  free( rows );
216 
217  return VICARImage;
218 }
int xmin
Definition: rle.h:100
int width
Definition: pgmtorle.c:51
rle_map * cmap
Definition: rle.h:112
int height
Definition: pgmtorle.c:51
int ymin
Definition: rle.h:100
static int y
Definition: getami.c:691
const char * cmd
Definition: rle.h:133
int xmax
Definition: rle.h:100
rle_pixel ** rows
Definition: rletopaint.c:57
#define RLE_CLR_BIT(glob, bit)
Definition: rle.h:124
int ymax
Definition: rle.h:100
unsigned char rle_pixel
Definition: rle.h:56
void * malloc()
#define RLE_ALPHA
Definition: rle.h:65
const char * file_name
Definition: rle.h:134
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[])
int ncolors
Definition: rle.h:100
#define RLE_CHECK_ALLOC(pgm, ptr, name)
Definition: rle.h:86
static void write_image ( rle_hdr the_hdr,
FILE *  outFD,
unsigned char *  VICARImage 
)
static

Definition at line 221 of file rletovcr.c.

225 {
226  int width, height, y;
227 
228  if (VERBOSE) fprintf(stderr,"%s: Writing VICARHeader\n", the_hdr->cmd);
229 
230  width = the_hdr->xmax - the_hdr->xmin + 1;
231  height = the_hdr->ymax - the_hdr->ymin + 1;
232  WriteVICARHeader(outFD, width, height, the_hdr->ncolors);
233 
234  if (VERBOSE) fprintf(stderr,"%s: Writing VICAR image", the_hdr->cmd);
235 
236  for ( y = 0; y < height; y++ )
237  {
238  WriteVICARScanLine(outFD, VICARImage + y * width, width);
239  if (VERBOSE) fprintf(stderr,".");
240  }
241 
242  if (VERBOSE) fprintf(stderr,"\n");
243 
244  if (ferror(outFD))
245  fprintf(stderr,"%s: Error writing image\n", the_hdr->cmd);
246 
247  if (VERBOSE)
248  fprintf(stderr,"%s: finished writing the image\n", the_hdr->cmd);
249 
250  fflush(outFD);
251 }
int xmin
Definition: rle.h:100
int width
Definition: pgmtorle.c:51
int height
Definition: pgmtorle.c:51
int ymin
Definition: rle.h:100
static int y
Definition: getami.c:691
const char * cmd
Definition: rle.h:133
int xmax
Definition: rle.h:100
WriteVICARHeader(FILE *fd, int width, int height, int BandsPerPixel)
Definition: rletovcr.c:86
int VERBOSE
Definition: rletovcr.c:53
static void WriteVICARScanLine(FILE *fd, unsigned char *VICARScanLine, int VICARScanLineLength)
Definition: rletovcr.c:164
int ymax
Definition: rle.h:100
int ncolors
Definition: rle.h:100
WriteVICARHeader ( FILE *  fd,
int  width,
int  height,
int  BandsPerPixel 
)

Definition at line 86 of file rletovcr.c.

89 {
90  int pad;
91  char *buffer, *bp;
92 
93  /*
94  LBLSIZE must be an integer multiple of width.
95  It also needs to be large enough for everything below to fit.
96  We use 1024 as a reasonable minimum size, and pick the first integer
97  multiple of 'width' to be the LBLSIZE.
98 
99  Look - I don't really understand VICAR format. We're just hacking...
100 
101  */
102  LBLSIZE = width; while(LBLSIZE < 1024) LBLSIZE += width;
103  /* Allocate a buffer. */
104  buffer = (char *)malloc( LBLSIZE );
105  bp = buffer;
106 
107 #define incr(bp,fudge) \
108  bp += strlen( bp ); \
109  if ( bp - buffer + fudge > LBLSIZE ) \
110  { \
111  bp = buffer = realloc( buffer, LBLSIZE += width ); \
112  bp += strlen( bp ); \
113  }
114 
115  sprintf(bp,"LBLSIZE=%-d ",LBLSIZE); /* see above */
116  incr( bp, 20 );
117  sprintf(bp," FORMAT='BYTE'");
118  incr( bp, 20 );
119  sprintf(bp," TYPE='IMAGE'");
120  incr( bp, 20 );
121  sprintf(bp," BUFSIZ=%-d",20*LBLSIZE);
122  incr( bp, 20 );
123  sprintf(bp," DIM=3");
124  incr( bp, 20 );
125  sprintf(bp," EOL=0");
126  incr( bp, 20 );
127  sprintf(bp," RECSIZE=%-d",LBLSIZE);
128  incr( bp, 20 );
129  sprintf(bp," ORG='BSQ'");
130  incr( bp, 20 );
131  sprintf(bp," NL=%-d",height);
132  incr( bp, 20 );
133  sprintf(bp," NS=%-d",width);
134  incr( bp, 20 );
135  sprintf(bp," NB=1");
136  incr( bp, 20 );
137  sprintf(bp," N1=%-d",height);
138  incr( bp, 20 );
139  sprintf(bp," N2=%-d",width);
140  incr( bp, 20 );
141  sprintf(bp," N3=1");
142  incr( bp, 20 );
143  sprintf(bp," N4=0");
144  incr( bp, 20 );
145  sprintf(bp," NBB=0");
146  incr( bp, 20 );
147  sprintf(bp," NLB=0");
148  incr( bp, 100 );
149  sprintf(bp," COMMENT='created by rletovcr'");
150  bp += strlen( bp );
151 
152 #undef incr
153 
154  /* Rewrite LBLSIZE in case it changed (unlikely). */
155  sprintf( buffer, "LBLSIZE=%-d", LBLSIZE );
156 
157  while ( bp < buffer + LBLSIZE )
158  *bp++ = 0;
159 
160  fwrite( buffer, 1, LBLSIZE, fd );
161  free( buffer );
162 }
#define incr(bp, fudge)
int width
Definition: pgmtorle.c:51
int height
Definition: pgmtorle.c:51
static int LBLSIZE
Definition: rletovcr.c:58
FILE * fd
Definition: getfb.h:18
void * malloc()
unsigned char * buffer
Definition: getsun.c:87
static void WriteVICARScanLine ( FILE *  fd,
unsigned char *  VICARScanLine,
int  VICARScanLineLength 
)
static

Definition at line 164 of file rletovcr.c.

168 {
169  (void)fwrite(VICARScanLine, 1, VICARScanLineLength, fd);
170 }
FILE * fd
Definition: getfb.h:18

Variable Documentation

int LBLSIZE
static

Definition at line 58 of file rletovcr.c.

int VERBOSE = 0

Definition at line 53 of file rletovcr.c.