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

Go to the source code of this file.

Macros

#define NO_DECLARE_MALLOC
 
#define VPRINTF   if (verbose || header) fprintf
 

Typedefs

typedef unsigned char U_CHAR
 

Functions

void read_pgm_header ()
 
void write_rle_header ()
 
void write_rle_data ()
 
int main (int argc, char **argv)
 

Variables

static char rcsid [] = "$Header: /l/spencer/src/urt/cnv/RCS/pgmtorle.c,v 3.0.1.5 1992/04/30 13:57:29 spencer Exp $"
 
FILE * fp
 
int format
 
int width
 
int height
 
int verbose = 0
 
int header = 0
 
int do_alpha = 0
 
gray maxval
 
rle_hdr hdr
 

Macro Definition Documentation

#define NO_DECLARE_MALLOC

Definition at line 40 of file pgmtorle.c.

#define VPRINTF   if (verbose || header) fprintf

Definition at line 43 of file pgmtorle.c.

Typedef Documentation

typedef unsigned char U_CHAR

Definition at line 45 of file pgmtorle.c.

Function Documentation

int main ( int  argc,
char **  argv 
)

Definition at line 138 of file pgmtorle.c.

141 {
142  int oflag = 0;
143  char *periodP, *pgmname = 0, *outname = 0;
144  static char filename[BUFSIZ];
145 /*
146  * Get those options.
147  */
148  if (!scanargs(argc,argv,
149  "% v%- h%- a%- o%-outfile!s infile.pgm%s\n(\
150 \tConvert a PGM file to URT RLE format.\n\
151 \t-a\tFake an alpha channel. Alpha=0 when input=0, 255 otherwise.\n\
152 \t-h\tPrint header of PGM file.\n\
153 \t-v\tVerbose mode.\n\
154 \tInput file name is forced to end in .pgm.)",
155  &verbose,
156  &header,
157  &do_alpha,
158  &oflag, &outname,
159  &pgmname))
160  exit(-1);
161  hdr = *rle_hdr_init( (rle_hdr *)NULL );
162  rle_names( &hdr, cmd_name( argv ), outname, 0 );
163 
164 /*
165  * Open the file.
166  */
167  if (pgmname == NULL) {
168  strcpy(filename, "stdin");
169  fp = stdin;
170  }
171  else {
172  periodP = strrchr(pgmname, '.');
173  strcpy(filename, pgmname);
174  if (periodP) {
175  if (strcmp(periodP, ".pgm")) /* does not end in pgm */
176  strcat(filename, ".pgm");
177  }
178  else /* no ext -- add one */
179  strcat(filename, ".pgm");
180  if (!(fp = fopen(filename, "r"))) {
181  fprintf(stderr, "%s: Cannot open %s for reading.\n",
182  hdr.cmd, filename);
183  exit(-1);
184  }
185  }
186 /*
187  * Read the PGM file header.
188  */
189  read_pgm_header();
190  if (header)
191  exit(0);
192 /*
193  * Write the rle file header.
194  */
195  hdr.rle_file = rle_open_f( cmd_name( argv ), outname, "w" );
196  rle_addhist(argv, (rle_hdr *)NULL, &hdr);
198 /*
199  * Write the rle file data.
200  */
201  write_rle_data();
202  fclose(fp);
203 
204  return 0;
205 }
int scanargs(int argc, char **argv, const char *format,...)
Definition: scanargs.c:94
void rle_addhist(char *argv[], rle_hdr *in_hdr, rle_hdr *out_hdr)
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 do_alpha
Definition: pgmtorle.c:52
int verbose
Definition: pgmtorle.c:52
int header
Definition: pgmtorle.c:52
void write_rle_data()
Definition: pgmtorle.c:89
void write_rle_header()
Definition: pgmtorle.c:71
FILE * fp
Definition: pgmtorle.c:49
FILE * rle_open_f(const char *prog_name, const char *f_name, const char *mode)
void read_pgm_header()
Definition: pgmtorle.c:59
char * cmd_name(char **argv)
Definition: cmd_name.c:31
int oflag
Definition: painttorle.c:45
rle_hdr hdr
Definition: pgmtorle.c:54
FILE * rle_file
Definition: rle.h:114
rle_hdr * rle_hdr_init(rle_hdr *the_hdr)
Definition: rle_hdr.c:267
void read_pgm_header ( )

Definition at line 59 of file pgmtorle.c.

References do_alpha, height, and width.

60 {
61  pgm_readpgminit(fp, &width, &height, &maxval, &format);
62  VPRINTF(stderr, "Image type: 8 bit grayscale\n");
63  VPRINTF(stderr, "Full image: %dx%d\n", width, height);
64  VPRINTF(stderr, "Maxval: %d\n", maxval);
65  if (do_alpha)
66  VPRINTF(stderr, "Computing alpha channel...\n");
67 }
int width
Definition: pgmtorle.c:51
int height
Definition: pgmtorle.c:51
#define VPRINTF
Definition: pgmtorle.c:43
int do_alpha
Definition: pgmtorle.c:52
FILE * fp
Definition: pgmtorle.c:49
int format
Definition: pgmtorle.c:50
gray maxval
Definition: pgmtorle.c:53
void write_rle_data ( )

Definition at line 89 of file pgmtorle.c.

References rle_hdr::cmd, hdr, height, rle_puteof(), rle_putrow(), rle_row_alloc(), rle_row_free(), and width.

90 {
91  register int x;
92  register int scan;
93  register gray *grayrow, *gP;
94  rle_pixel ***scanlines, **scanline;
95  /*
96  * Allocate some memory.
97  */
98  grayrow = pgm_allocrow(width);
99  scanlines = (rle_pixel ***)malloc( height * sizeof(rle_pixel **) );
100  RLE_CHECK_ALLOC( hdr.cmd, scanlines, "scanline pointers" );
101 
102  for ( scan = 0; scan < height; scan++ )
103  RLE_CHECK_ALLOC( hdr.cmd, (rle_row_alloc(&hdr, &scanlines[scan]) >= 0),
104  "pixel memory" );
105 
106  /*
107  * Loop through the pgm files image window, write blank lines outside
108  * active window.
109  */
110  for (scan = 0; scan < height; scan++) {
111  pgm_readpgmrow(fp, grayrow, width, maxval, format);
112  scanline = scanlines[height - scan - 1];
113  for (x = 0, gP = grayrow; x < width; x++, gP++) {
114  scanline[RLE_RED][x] = *gP;
115  if (do_alpha)
116  scanline[RLE_ALPHA][x] = (*gP ? 255 : 0);
117  }
118  }
119  /*
120  * Write out data in URT order (bottom to top).
121  */
122  for ( scan = 0; scan < height; scan++ )
123  {
124  rle_putrow(scanlines[scan], width, &hdr);
125  rle_row_free( &hdr, scanlines[scan] );
126  }
127  free( scanlines );
128  VPRINTF(stderr, "Done -- write eof to RLE data.\n");
129  rle_puteof(&hdr);
130  /*
131  * Free up some stuff.
132  */
133 }
void rle_row_free(rle_hdr *the_hdr, rle_pixel **scanp)
Definition: rle_row_alc.c:114
int rle_row_alloc(rle_hdr *the_hdr, rle_pixel ***scanp)
Definition: rle_row_alc.c:56
int width
Definition: pgmtorle.c:51
void rle_putrow(rle_pixel *rows[], int rowlen, rle_hdr *the_hdr)
int height
Definition: pgmtorle.c:51
void rle_puteof(rle_hdr *the_hdr)
Definition: rle_putrow.c:474
unsigned char ** scan
Definition: rle.c:26
#define VPRINTF
Definition: pgmtorle.c:43
const char * cmd
Definition: rle.h:133
int do_alpha
Definition: pgmtorle.c:52
#define RLE_RED
Definition: rle.h:62
static int x
Definition: getami.c:691
unsigned char rle_pixel
Definition: rle.h:56
FILE * fp
Definition: pgmtorle.c:49
int format
Definition: pgmtorle.c:50
void * malloc()
#define RLE_ALPHA
Definition: rle.h:65
unsigned char scanline[4][1280]
Definition: get_orion.c:37
gray maxval
Definition: pgmtorle.c:53
rle_hdr hdr
Definition: pgmtorle.c:54
#define RLE_CHECK_ALLOC(pgm, ptr, name)
Definition: rle.h:86

Here is the call graph for this function:

void write_rle_header ( )

Definition at line 71 of file pgmtorle.c.

References rle_hdr::alpha, rle_hdr::background, do_alpha, hdr, height, rle_hdr::ncolors, rle_put_setup(), width, rle_hdr::xmax, rle_hdr::xmin, rle_hdr::ymax, and rle_hdr::ymin.

72 {
73  hdr.xmin = 0;
74  hdr.xmax = width-1;
75  hdr.ymin = 0;
76  hdr.ymax = height-1;
77  hdr.ncolors = 1;
78  hdr.background = 0;
80  if (do_alpha) {
81  hdr.alpha = 1;
83  }
85 }
#define RLE_SET_BIT(glob, bit)
Definition: rle.h:122
int xmin
Definition: rle.h:100
int width
Definition: pgmtorle.c:51
int height
Definition: pgmtorle.c:51
int ymin
Definition: rle.h:100
int do_alpha
Definition: pgmtorle.c:52
#define RLE_RED
Definition: rle.h:62
int xmax
Definition: rle.h:100
int background
Definition: rle.h:100
int ymax
Definition: rle.h:100
int alpha
Definition: rle.h:100
#define RLE_ALPHA
Definition: rle.h:65
void rle_put_setup(rle_hdr *the_hdr)
Definition: rle_putrow.c:453
rle_hdr hdr
Definition: pgmtorle.c:54
int ncolors
Definition: rle.h:100

Here is the call graph for this function:

Variable Documentation

int do_alpha = 0

Definition at line 52 of file pgmtorle.c.

int format

Definition at line 50 of file pgmtorle.c.

FILE* fp

Definition at line 49 of file pgmtorle.c.

rle_hdr hdr

Definition at line 54 of file pgmtorle.c.

int header = 0

Definition at line 52 of file pgmtorle.c.

int height

Definition at line 51 of file pgmtorle.c.

gray maxval

Definition at line 53 of file pgmtorle.c.

char rcsid[] = "$Header: /l/spencer/src/urt/cnv/RCS/pgmtorle.c,v 3.0.1.5 1992/04/30 13:57:29 spencer Exp $"
static

Definition at line 29 of file pgmtorle.c.

int verbose = 0

Definition at line 52 of file pgmtorle.c.

int width

Definition at line 51 of file pgmtorle.c.