Utah Raster Toolkit  9999-git
URT Development version (post-3.1b)
pgmtorle.c
Go to the documentation of this file.
1 /*
2  * This software is copyrighted as noted below. It may be freely copied,
3  * modified, and redistributed, provided that the copyright notice is
4  * preserved on all copies.
5  *
6  * There is no warranty or other guarantee of fitness for this software,
7  * it is provided solely "as is". Bug reports or fixes may be sent
8  * to the author, who may or may not act on them as he desires.
9  *
10  * You may not include this software in a program or other software product
11  * without supplying the source, or without informing the end-user that the
12  * source is available for no extra charge.
13  *
14  * If you modify this software, you should include a notice giving the
15  * name of the person performing the modification, the date of modification,
16  * and the reason for such modification.
17  */
18 /*
19  * pgmtorle - A program which will convert pbmplus/pgm images
20  * to Utah's "rle" image format.
21  *
22  * Author: Wesley C. Barris
23  * AHPCRC
24  * Minnesota Supercomputer Center, Inc.
25  * Date: Fri July 20 1990
26  * Copyright (c) 1990 Wesley C. Barris
27  */
28 #ifndef lint
29 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 $";
30 #endif
31 #if 0
32 pgmtorle() /* Tag. */
33 #endif
34 /*-----------------------------------------------------
35  * System includes.
36  */
37 #include <stdio.h>
38 #include <pgm.h>
39 #undef abs /* Screws up stdlib.h. */
40 #define NO_DECLARE_MALLOC
41 #include "rle.h"
42 
43 #define VPRINTF if (verbose || header) fprintf
44 
45 typedef unsigned char U_CHAR;
46 /*
47  * Global variables.
48  */
49 FILE *fp;
50 int format;
52 int verbose = 0, header = 0, do_alpha = 0;
54 rle_hdr hdr;
55 
56 /*-----------------------------------------------------------------------------
57  * Read the Wavefront image file header.
58  */
59 void read_pgm_header()
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 }
68 /*-----------------------------------------------------------------------------
69  * Write the rle image file header.
70  */
71 void write_rle_header()
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 }
86 /*-----------------------------------------------------------------------------
87  * Write the rle data portion of the file.
88  */
89 void write_rle_data()
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");
130  /*
131  * Free up some stuff.
132  */
133 }
134 /*-----------------------------------------------------------------------------
135  * Convert a pgm image file into an rle image file.
136  */
137 int
139 int argc;
140 char **argv;
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);
197  write_rle_header();
198 /*
199  * Write the rle file data.
200  */
201  write_rle_data();
202  fclose(fp);
203 
204  return 0;
205 }
FILE * rle_open_f(char *prog_name, char *file_name, char *mode)
Definition: rle_open_f.c:216
rle_hdr hdr
Definition: getx10.c:84
#define RLE_SET_BIT(glob, bit)
Definition: rle.h:122
int xmin
Definition: rle.h:100
void rle_names(rle_hdr *the_hdr, const char *pgmname, const char *fname, int img_num)
Definition: rle_hdr.c:48
unsigned char U_CHAR
Definition: pgmtorle.c:45
gpr_ $bmf_group_header_array_t header
Definition: getap.c:113
pixval maxval
Definition: rletoppm.c:51
void rle_row_free(rle_hdr *the_hdr, rle_pixel **scanp)
Definition: rle_row_alc.c:114
int height
Definition: rletoppm.c:62
char * cmd_name(char **argv)
Definition: cmd_name.c:31
void main(int argc, char **argv)
Definition: aliastorle.c:121
int do_alpha
Definition: ppmtorle.c:52
int rle_row_alloc(rle_hdr *the_hdr, rle_pixel ***scanp)
Definition: rle_row_alc.c:56
int ymin
Definition: rle.h:100
int verbose
Definition: rletorla.c:81
int scanargs(int argc, char **argv, const char *format,...)
Definition: scanargs.c:94
int format
Definition: ppmtorle.c:50
const char * cmd
Definition: rle.h:133
void rle_puteof(rle_hdr *the_hdr)
Definition: rle_putrow.c:474
#define RLE_RED
Definition: rle.h:62
void rle_putrow(rows, int rowlen, rle_hdr *the_hdr)
Definition: rle_putrow.c:96
int xmax
Definition: rle.h:100
FILE * fp
Definition: ppmtorle.c:48
void rle_addhist(argv, rle_hdr *in_hdr, rle_hdr *out_hdr)
Definition: rle_addhist.c:54
int background
Definition: rle.h:100
#define VPRINTF
Definition: aliastorle.c:42
int width
Definition: rletoppm.c:62
int ymax
Definition: rle.h:100
unsigned char rle_pixel
Definition: rle.h:56
void rle_put_setup(rle_hdr *the_hdr)
Definition: rle_putrow.c:453
int alpha
Definition: rle.h:100
static char rcsid[]
Definition: pgmtorle.c:29
#define RLE_ALPHA
Definition: rle.h:65
rle_hdr * rle_hdr_init(rle_hdr *the_hdr)
Definition: rle_hdr.c:267
FILE * rle_file
Definition: rle.h:114
int ncolors
Definition: rle.h:100
#define RLE_CHECK_ALLOC(pgm, ptr, name)
Definition: rle.h:86