[alt.sources] pgmtoppm - convert 3 pgm files into a ppm file

mark@zok.UUCP (Mark W. Snitily) (11/15/90)

Attached is the source to "pgmtoppm" which converts three PGM files (r,g,b)
into a single PPM file.  Useful for converting raw 24-bit images that are
stored as separate red, green, blue files.

Source should be placed into the ".../pbmplus/ppm" directory, (not the
".../pbmplus/pgm" directory).

-- Mark

Mark W. Snitily                 Consulting Services:
894 Brookgrove Lane             Graphics, Operating Systems, Compilers
Cupertino, CA 95014             (408) 252-0456
mark@zok.uucp                   West Coast UUCP X11 archive site

If your mailer doesn't like the .uucp domain, these also work:
...!{mips,sgi}!zok!mark, mark%zok@mips.com, mark%zok@sgi.com

---<cut here>------------------------------------------------------------------
#! /bin/sh
# This is a shell archive.  Remove anything before this line, then unpack
# it by saving it into a file and typing "sh file".  To overwrite existing
# files, type "sh file -c".  You can also feed this as standard input via
# unshar, or by typing "sh <file", e.g..  If this archive is complete, you
# will see the following message at the end:
#		"End of archive 1 (of 1)."
# Contents:  pgmtoppm.1 pgmtoppm.c
# Wrapped by mark@zok on Wed Nov 14 17:53:20 1990
PATH=/bin:/usr/bin:/usr/ucb ; export PATH
if test -f 'pgmtoppm.1' -a "${1}" != "-c" ; then 
  echo shar: Will not clobber existing file \"'pgmtoppm.1'\"
else
echo shar: Extracting \"'pgmtoppm.1'\" \(1137 characters\)
sed "s/^X//" >'pgmtoppm.1' <<'END_OF_FILE'
X.TH pgmtoppm 1 "14 November 1990"
X.SH NAME
Xpgmtoppm - convert three PGM files into a PPM file
X.SH SYNOPSIS
Xpgmtoppm <red pgm file> <green pgm file> <blue pgm file>
X.SH DESCRIPTION
XReads three portable grayscale maps (PGM) as input.
XProduces portable pixmap (PPM) as output.
X.PP
XThe three pgm input files must have the same size, max value, and format.
X.PP
XFor example, assume you have a raw 512x512 24-bit image in files "pic.red",
X"pic.grn", and "pic.blu".  One could convert these files into a gif
Ximage by:
X.IP
Xrawtopgm 512 512 pic.red >r.pgm
X.IP
Xrawtopgm 512 512 pic.grn >g.pgm
X.IP
Xrawtopgm 512 512 pic.blu >b.pgm
X.IP
Xpgmtoppm r.pgm g.pgm b.prm | ppmquant 256 | ppmtogif >pic.gif
X.PP
X.SH "SEE ALSO"
Xppm(5), pgm(5)
X.SH AUTHOR
XCopyright (C) 1990 by Mark W. Snitily.
X
XPermission to use, copy, modify, and distribute this software and its
Xdocumentation for any purpose and without fee is hereby granted, provided
Xthat the above copyright notice appear in all copies and that both that
Xcopyright notice and this permission notice appear in supporting
Xdocumentation.  This software is provided "as is" without express or
Ximplied warranty.
END_OF_FILE
if test 1137 -ne `wc -c <'pgmtoppm.1'`; then
    echo shar: \"'pgmtoppm.1'\" unpacked with wrong size!
fi
# end of 'pgmtoppm.1'
fi
if test -f 'pgmtoppm.c' -a "${1}" != "-c" ; then 
  echo shar: Will not clobber existing file \"'pgmtoppm.c'\"
else
echo shar: Extracting \"'pgmtoppm.c'\" \(2962 characters\)
sed "s/^X//" >'pgmtoppm.c' <<'END_OF_FILE'
X/* pgmtoppm.c - merge 3 pgm files (r,g,b) into a single ppm file
X**
X** Copyright (C) 1990 by Mark W. Snitily
X**
X** Permission to use, copy, modify, and distribute this software and its
X** documentation for any purpose and without fee is hereby granted, provided
X** that the above copyright notice appear in all copies and that both that
X** copyright notice and this permission notice appear in supporting
X** documentation.  This software is provided "as is" without express or
X** implied warranty.
X*/
X
X#include <stdio.h>
X#include "ppm.h"
X
Xmain(argc, argv)
Xint argc;
Xchar *argv[];
X{
X   FILE *fr, *fg, *fb;
X   gray *rpgmrow, *gpgmrow, *bpgmrow, *rgP, *ggP, *bgP;
X   gray rmaxval, gmaxval, bmaxval;
X   int rrows, rcols, rformat;
X   int grows, gcols, gformat;
X   int brows, bcols, bformat;
X   int row, col;
X   pixel *ppmrow, *pP;
X
X   char *usage = "red.pgm green.pgm blue.pgm >rgb.ppm\nwhere:\n\
X   {red,green,blue}.pgm are files in PGM format that\n\
X   contain the image's red, green, and blue components\n\n\
X   rgb.ppm is the merged rgb output in PPM format\n";
X
X
X   pm_progname = argv[0];
X
X   if (argc != 4) {
X      pm_usage(usage);
X      exit(1);
X   }
X
X   fr = pm_openr(argv[1]);   /* Open red   file. */
X   fg = pm_openr(argv[2]);   /* Open green file. */
X   fb = pm_openr(argv[3]);   /* Open blue  file. */
X
X   /* Read the columns, rows, max color, and format of the input files. */
X   pgm_readpgminit(fr, &rcols, &rrows, &rmaxval, &rformat);
X   pgm_readpgminit(fg, &gcols, &grows, &gmaxval, &gformat);
X   pgm_readpgminit(fb, &bcols, &brows, &bmaxval, &bformat);
X
X   /* Abort if the input files' header info don't match. */
X   if (rcols   != gcols   || rcols   != bcols   ||
X       rrows   != grows   || rrows   != brows   ||
X       rmaxval != gmaxval || rmaxval != bmaxval ||
X       rformat != bformat || rformat != gformat)
X      pm_error("columns, rows, maxcolor, or format of input files don't match",
X               0,0,0,0,0);
X
X   /* Allocate a buffer for each pgm input file. */
X   rpgmrow = pgm_allocrow(rcols);
X   gpgmrow = pgm_allocrow(gcols);
X   bpgmrow = pgm_allocrow(bcols);
X
X   /* Allocate a buffer for the ppm output file. */
X   ppmrow = ppm_allocrow(rcols);
X
X   /* Write the ppm header. */
X   ppm_writeppminit(stdout, rcols, rrows, rmaxval);
X
X   /* Read the data from the input files a row at a time.
X      Write it to stdout as (r,g,b) triples (i.e. ppm format). */
X   for (row = 0;  row < rrows;  row++) {
X      pgm_readpgmrow(fr, rpgmrow, rcols, rmaxval, rformat);
X      pgm_readpgmrow(fg, gpgmrow, gcols, gmaxval, gformat);
X      pgm_readpgmrow(fb, bpgmrow, bcols, bmaxval, bformat);
X
X      for (col=0, pP=ppmrow, rgP=rpgmrow, ggP=gpgmrow, bgP=bpgmrow;
X           col < rcols;
X           col++, pP++, rgP++, ggP++, bgP++)
X         PPM_ASSIGN(*pP, *rgP, *ggP, *bgP);
X
X      ppm_writeppmrow(stdout, ppmrow, rcols, rmaxval);
X   }
X
X   /* Close the input files. */
X   pm_close(fr);
X   pm_close(fg);
X   pm_close(fb);
X
X   exit(0);
X
X} /* main */
END_OF_FILE
if test 2962 -ne `wc -c <'pgmtoppm.c'`; then
    echo shar: \"'pgmtoppm.c'\" unpacked with wrong size!
fi
# end of 'pgmtoppm.c'
fi
echo shar: End of archive 1 \(of 1\).
cp /dev/null ark1isdone
MISSING=""
for I in 1 ; do
    if test ! -f ark${I}isdone ; then
	MISSING="${MISSING} ${I}"
    fi
done
if test "${MISSING}" = "" ; then
    echo You have the archive.
    rm -f ark[1-9]isdone
else
    echo You still need to unpack the following archives:
    echo "        " ${MISSING}
fi
##  End of shell archive.
exit 0