Utah Raster Toolkit  9999-git
URT Development version (post-3.1b)
ppmtorle.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  * ppmtorle - A program which will convert pbmplus/ppm 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) Wesley C. Barris
27  */
28 #ifndef lint
29 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 $";
30 #endif
31 #if 0
32 ppmtorle() /* Tag. */
33 #endif
34 /*-----------------------------------------------------
35  * System includes.
36  */
37 #define NO_DECLARE_MALLOC /* ppm.h does it */
38 #include "rle.h"
39 #include <stdio.h>
40 #include <ppm.h>
41 
42 #define VPRINTF if (verbose || header) fprintf
43 
44 typedef unsigned char U_CHAR;
45 /*
46  * Global variables.
47  */
48 FILE *fp;
49 rle_hdr hdr;
50 int format;
52 int verbose = 0, header = 0, do_alpha = 0;
54 /*-----------------------------------------------------------------------------
55  * Read the ppm image file header.
56  */
57 void read_ppm_header()
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 }
66 /*-----------------------------------------------------------------------------
67  * Write the rle image file header.
68  */
69 void write_rle_header()
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 }
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 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");
135 }
136 /*-----------------------------------------------------------------------------
137  * Convert an PPM image file into an rle image file.
138  */
139 int
141 int argc;
142 char **argv;
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);
204  write_rle_header();
205  /*
206  * Write the rle file data.
207  */
208  write_rle_data();
209  }
210 
211  fclose(fp);
212  return 0;
213 }
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
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
#define RLE_GREEN
Definition: rle.h:63
int ymin
Definition: rle.h:100
#define RLE_BLUE
Definition: rle.h:64
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
static char rcsid[]
Definition: ppmtorle.c:29
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
unsigned char U_CHAR
Definition: ppmtorle.c:44
int alpha
Definition: rle.h:100
#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