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

Go to the source code of this file.

Macros

#define NO_DECLARE_MALLOC   /* ppm.h does it */
 
#define VPRINTF   if (verbose || header) fprintf
 

Typedefs

typedef unsigned char U_CHAR
 

Functions

void read_ppm_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/ppmtorle.c,v 3.0.1.4 1992/03/04 19:29:43 spencer Exp $"
 
FILE * fp
 
rle_hdr hdr
 
int format
 
int width
 
int height
 
int verbose = 0
 
int header = 0
 
int do_alpha = 0
 
gray maxval
 

Macro Definition Documentation

#define NO_DECLARE_MALLOC   /* ppm.h does it */

Definition at line 37 of file ppmtorle.c.

#define VPRINTF   if (verbose || header) fprintf

Definition at line 42 of file ppmtorle.c.

Typedef Documentation

typedef unsigned char U_CHAR

Definition at line 44 of file ppmtorle.c.

Function Documentation

int main ( int  argc,
char **  argv 
)

Definition at line 140 of file ppmtorle.c.

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

Definition at line 57 of file ppmtorle.c.

References do_alpha, height, and width.

58 {
59  ppm_readppminit(fp, &width, &height, &maxval, &format);
60  VPRINTF(stderr, "Image type: 24 bit true color\n");
61  VPRINTF(stderr, "Full image: %dx%d\n", width, height);
62  VPRINTF(stderr, "Maxval: %d\n", maxval);
63  if (do_alpha)
64  VPRINTF(stderr, "Computing alpha channel...\n");
65 }
#define VPRINTF
Definition: ppmtorle.c:42
int do_alpha
Definition: ppmtorle.c:52
gray maxval
Definition: ppmtorle.c:53
int format
Definition: ppmtorle.c:50
FILE * fp
Definition: ppmtorle.c:48
int height
Definition: ppmtorle.c:51
int width
Definition: ppmtorle.c:51
void write_rle_data ( )

Definition at line 89 of file ppmtorle.c.

References rle_hdr::cmd, do_alpha, 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 pixel *pixelrow, *pP;
94  rle_pixel ***scanlines, **scanline;
95  /*
96  * Allocate some memory.
97  */
98  pixelrow = ppm_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 ppm files image window, read data and flip vertically.
108  */
109  for (scan = 0; scan < height; scan++) {
110  scanline = scanlines[height - scan - 1];
111  ppm_readppmrow(fp, pixelrow, width, maxval, format);
112  for (x = 0, pP = pixelrow; x < width; x++, pP++) {
113  scanline[RLE_RED][x] = PPM_GETR(*pP);
114  scanline[RLE_GREEN][x] = PPM_GETG(*pP);
115  scanline[RLE_BLUE][x] = PPM_GETB(*pP);
116  if (do_alpha) {
117  scanline[RLE_ALPHA][x] = (scanline[RLE_RED][x] ||
118  scanline[RLE_GREEN][x] ||
119  scanline[RLE_BLUE][x] ? 255 : 0);
120  }
121  }
122  }
123  /*
124  * Write out data in URT order (bottom to top).
125  */
126  for ( scan = 0; scan < height; scan++ )
127  {
128  rle_putrow(scanlines[scan], width, &hdr);
129  rle_row_free( &hdr, scanlines[scan] );
130  }
131  free( scanlines );
132 
133  VPRINTF(stderr, "Done -- write eof to RLE data.\n");
134  rle_puteof(&hdr);
135 }
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
#define VPRINTF
Definition: ppmtorle.c:42
int do_alpha
Definition: ppmtorle.c:52
void rle_putrow(rle_pixel *rows[], int rowlen, rle_hdr *the_hdr)
#define RLE_GREEN
Definition: rle.h:63
gray maxval
Definition: ppmtorle.c:53
#define RLE_BLUE
Definition: rle.h:64
void rle_puteof(rle_hdr *the_hdr)
Definition: rle_putrow.c:474
unsigned char ** scan
Definition: rle.c:26
int format
Definition: ppmtorle.c:50
const char * cmd
Definition: rle.h:133
#define RLE_RED
Definition: rle.h:62
static int x
Definition: getami.c:691
FILE * fp
Definition: ppmtorle.c:48
int height
Definition: ppmtorle.c:51
rle_hdr hdr
Definition: ppmtorle.c:49
unsigned char rle_pixel
Definition: rle.h:56
void * malloc()
#define RLE_ALPHA
Definition: rle.h:65
int width
Definition: ppmtorle.c:51
unsigned char scanline[4][1280]
Definition: get_orion.c:37
#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 69 of file ppmtorle.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.

70 {
71  hdr.xmin = 0;
72  hdr.xmax = width-1;
73  hdr.ymin = 0;
74  hdr.ymax = height-1;
75  hdr.ncolors = 3;
76  hdr.background = 0;
77  if (do_alpha) {
78  hdr.alpha = 1;
80  }
85 }
#define RLE_SET_BIT(glob, bit)
Definition: rle.h:122
int xmin
Definition: rle.h:100
int do_alpha
Definition: ppmtorle.c:52
#define RLE_GREEN
Definition: rle.h:63
int ymin
Definition: rle.h:100
#define RLE_BLUE
Definition: rle.h:64
#define RLE_RED
Definition: rle.h:62
int xmax
Definition: rle.h:100
int height
Definition: ppmtorle.c:51
int background
Definition: rle.h:100
rle_hdr hdr
Definition: ppmtorle.c:49
int ymax
Definition: rle.h:100
int alpha
Definition: rle.h:100
#define RLE_ALPHA
Definition: rle.h:65
int width
Definition: ppmtorle.c:51
void rle_put_setup(rle_hdr *the_hdr)
Definition: rle_putrow.c:453
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 ppmtorle.c.

Referenced by read_pgm_header(), read_ppm_header(), write_rle_data(), and write_rle_header().

int format

Definition at line 50 of file ppmtorle.c.

FILE* fp

Definition at line 48 of file ppmtorle.c.

rle_hdr hdr

Definition at line 49 of file ppmtorle.c.

int header = 0

Definition at line 52 of file ppmtorle.c.

int height

Definition at line 51 of file ppmtorle.c.

gray maxval

Definition at line 53 of file ppmtorle.c.

char rcsid[] = "$Header: /l/spencer/src/urt/cnv/RCS/ppmtorle.c,v 3.0.1.4 1992/03/04 19:29:43 spencer Exp $"
static

Definition at line 29 of file ppmtorle.c.

int verbose = 0

Definition at line 52 of file ppmtorle.c.

int width

Definition at line 51 of file ppmtorle.c.