home *** CD-ROM | disk | FTP | other *** search
- /* pgmtoppm.c - merge 3 pgm files (r,g,b) into a single ppm file
- **
- ** Copyright (C) 1990 by Mark W. Snitily
- **
- ** Permission to use, copy, modify, and distribute this software and its
- ** documentation for any purpose and without fee is hereby granted, provided
- ** that the above copyright notice appear in all copies and that both that
- ** copyright notice and this permission notice appear in supporting
- ** documentation. This software is provided "as is" without express or
- ** implied warranty.
- */
-
- #include <stdio.h>
- #include "ppm.h"
-
- main(argc, argv)
- int argc;
- char *argv[];
- {
- FILE *fr, *fg, *fb;
- gray *rpgmrow, *gpgmrow, *bpgmrow, *rgP, *ggP, *bgP;
- gray rmaxval, gmaxval, bmaxval;
- int rrows, rcols, rformat;
- int grows, gcols, gformat;
- int brows, bcols, bformat;
- int row, col;
- pixel *ppmrow, *pP;
-
- char *usage = "red.pgm green.pgm blue.pgm >rgb.ppm\nwhere:\n\
- {red,green,blue}.pgm are files in PGM format that\n\
- contain the image's red, green, and blue components\n\n\
- rgb.ppm is the merged rgb output in PPM format\n";
-
-
- pm_progname = argv[0];
-
- if (argc != 4) {
- pm_usage(usage);
- exit(1);
- }
-
- fr = pm_openr(argv[1]); /* Open red file. */
- fg = pm_openr(argv[2]); /* Open green file. */
- fb = pm_openr(argv[3]); /* Open blue file. */
-
- /* Read the columns, rows, max color, and format of the input files. */
- pgm_readpgminit(fr, &rcols, &rrows, &rmaxval, &rformat);
- pgm_readpgminit(fg, &gcols, &grows, &gmaxval, &gformat);
- pgm_readpgminit(fb, &bcols, &brows, &bmaxval, &bformat);
-
- /* Abort if the input files' header info don't match. */
- if (rcols != gcols || rcols != bcols ||
- rrows != grows || rrows != brows ||
- rmaxval != gmaxval || rmaxval != bmaxval ||
- rformat != bformat || rformat != gformat)
- pm_error("columns, rows, maxcolor, or format of input files don't match",
- 0,0,0,0,0);
-
- /* Allocate a buffer for each pgm input file. */
- rpgmrow = pgm_allocrow(rcols);
- gpgmrow = pgm_allocrow(gcols);
- bpgmrow = pgm_allocrow(bcols);
-
- /* Allocate a buffer for the ppm output file. */
- ppmrow = ppm_allocrow(rcols);
-
- /* Write the ppm header. */
- ppm_writeppminit(stdout, rcols, rrows, rmaxval);
-
- /* Read the data from the input files a row at a time.
- Write it to stdout as (r,g,b) triples (i.e. ppm format). */
- for (row = 0; row < rrows; row++) {
- pgm_readpgmrow(fr, rpgmrow, rcols, rmaxval, rformat);
- pgm_readpgmrow(fg, gpgmrow, gcols, gmaxval, gformat);
- pgm_readpgmrow(fb, bpgmrow, bcols, bmaxval, bformat);
-
- for (col=0, pP=ppmrow, rgP=rpgmrow, ggP=gpgmrow, bgP=bpgmrow;
- col < rcols;
- col++, pP++, rgP++, ggP++, bgP++)
- PPM_ASSIGN(*pP, *rgP, *ggP, *bgP);
-
- ppm_writeppmrow(stdout, ppmrow, rcols, rmaxval);
- }
-
- /* Close the input files. */
- pm_close(fr);
- pm_close(fg);
- pm_close(fb);
-
- exit(0);
-
- } /* main */
-