home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 2 / 2065 / pgmtoppm.c < prev   
Encoding:
C/C++ Source or Header  |  1990-12-28  |  2.9 KB  |  93 lines

  1. /* pgmtoppm.c - merge 3 pgm files (r,g,b) into a single ppm file
  2. **
  3. ** Copyright (C) 1990 by Mark W. Snitily
  4. **
  5. ** Permission to use, copy, modify, and distribute this software and its
  6. ** documentation for any purpose and without fee is hereby granted, provided
  7. ** that the above copyright notice appear in all copies and that both that
  8. ** copyright notice and this permission notice appear in supporting
  9. ** documentation.  This software is provided "as is" without express or
  10. ** implied warranty.
  11. */
  12.  
  13. #include <stdio.h>
  14. #include "ppm.h"
  15.  
  16. main(argc, argv)
  17. int argc;
  18. char *argv[];
  19. {
  20.    FILE *fr, *fg, *fb;
  21.    gray *rpgmrow, *gpgmrow, *bpgmrow, *rgP, *ggP, *bgP;
  22.    gray rmaxval, gmaxval, bmaxval;
  23.    int rrows, rcols, rformat;
  24.    int grows, gcols, gformat;
  25.    int brows, bcols, bformat;
  26.    int row, col;
  27.    pixel *ppmrow, *pP;
  28.  
  29.    char *usage = "red.pgm green.pgm blue.pgm >rgb.ppm\nwhere:\n\
  30.    {red,green,blue}.pgm are files in PGM format that\n\
  31.    contain the image's red, green, and blue components\n\n\
  32.    rgb.ppm is the merged rgb output in PPM format\n";
  33.  
  34.  
  35.    pm_progname = argv[0];
  36.  
  37.    if (argc != 4) {
  38.       pm_usage(usage);
  39.       exit(1);
  40.    }
  41.  
  42.    fr = pm_openr(argv[1]);   /* Open red   file. */
  43.    fg = pm_openr(argv[2]);   /* Open green file. */
  44.    fb = pm_openr(argv[3]);   /* Open blue  file. */
  45.  
  46.    /* Read the columns, rows, max color, and format of the input files. */
  47.    pgm_readpgminit(fr, &rcols, &rrows, &rmaxval, &rformat);
  48.    pgm_readpgminit(fg, &gcols, &grows, &gmaxval, &gformat);
  49.    pgm_readpgminit(fb, &bcols, &brows, &bmaxval, &bformat);
  50.  
  51.    /* Abort if the input files' header info don't match. */
  52.    if (rcols   != gcols   || rcols   != bcols   ||
  53.        rrows   != grows   || rrows   != brows   ||
  54.        rmaxval != gmaxval || rmaxval != bmaxval ||
  55.        rformat != bformat || rformat != gformat)
  56.       pm_error("columns, rows, maxcolor, or format of input files don't match",
  57.                0,0,0,0,0);
  58.  
  59.    /* Allocate a buffer for each pgm input file. */
  60.    rpgmrow = pgm_allocrow(rcols);
  61.    gpgmrow = pgm_allocrow(gcols);
  62.    bpgmrow = pgm_allocrow(bcols);
  63.  
  64.    /* Allocate a buffer for the ppm output file. */
  65.    ppmrow = ppm_allocrow(rcols);
  66.  
  67.    /* Write the ppm header. */
  68.    ppm_writeppminit(stdout, rcols, rrows, rmaxval);
  69.  
  70.    /* Read the data from the input files a row at a time.
  71.       Write it to stdout as (r,g,b) triples (i.e. ppm format). */
  72.    for (row = 0;  row < rrows;  row++) {
  73.       pgm_readpgmrow(fr, rpgmrow, rcols, rmaxval, rformat);
  74.       pgm_readpgmrow(fg, gpgmrow, gcols, gmaxval, gformat);
  75.       pgm_readpgmrow(fb, bpgmrow, bcols, bmaxval, bformat);
  76.  
  77.       for (col=0, pP=ppmrow, rgP=rpgmrow, ggP=gpgmrow, bgP=bpgmrow;
  78.            col < rcols;
  79.            col++, pP++, rgP++, ggP++, bgP++)
  80.          PPM_ASSIGN(*pP, *rgP, *ggP, *bgP);
  81.  
  82.       ppm_writeppmrow(stdout, ppmrow, rcols, rmaxval);
  83.    }
  84.  
  85.    /* Close the input files. */
  86.    pm_close(fr);
  87.    pm_close(fg);
  88.    pm_close(fb);
  89.  
  90.    exit(0);
  91.  
  92. } /* main */
  93.