home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 309.lha / PBM_PLUS / pnm / pnmcat.c < prev    next >
C/C++ Source or Header  |  1980-12-04  |  4KB  |  174 lines

  1. /* pnmcatlr.c - concatenate portable anymaps left to right
  2. **
  3. ** Copyright (C) 1989 by Jef Poskanzer.
  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. #ifdef    SYSV
  15. #include <string.h>
  16. #else    SYSV
  17. #include <strings.h>
  18. #endif    SYSV
  19. #include "pnm.h"
  20.  
  21. #define MAXFILES 100
  22. #define max(a,b) ((a) > (b) ? (a) : (b))
  23.  
  24. main( argc, argv )
  25. int argc;
  26. char *argv[];
  27.     {
  28.     FILE *ifd[MAXFILES];
  29.     xel **xels[MAXFILES], background;
  30.     register xel **newxels;
  31.     xelval maxval[MAXFILES], newmaxval;
  32.     int argn, backdefault, backblack, lrflag, tbflag, nfiles, i;
  33.     int rows[MAXFILES], cols[MAXFILES], format[MAXFILES], newformat, row, col;
  34.     int newrows, newcols, new, pad;
  35.     char *usage = "[-white|-black] pnmfile pnmfile ...";
  36.  
  37.     pm_progname = argv[0];
  38.  
  39.     argn = 1;
  40.     backdefault = 1;
  41.     lrflag = tbflag = 0;
  42.  
  43.     /* Check for flags. */
  44.     while ( argn < argc && argv[argn][0] == '-' && argv[argn][1] != '\0' )
  45.     {
  46.     if ( strncmp(argv[argn],"-white",max(strlen(argv[argn]),2)) == 0 )
  47.         {
  48.         backdefault = 0;
  49.         backblack = 0;
  50.         }
  51.     else if ( strncmp(argv[argn],"-black",max(strlen(argv[argn]),2)) == 0 )
  52.         {
  53.         backdefault = 0;
  54.         backblack = 1;
  55.         }
  56.     else if ( strncmp(argv[argn],"-lr",max(strlen(argv[argn]),2)) == 0 ||
  57.               strncmp(argv[argn],"-leftright",max(strlen(argv[argn]),2)) == 0 )
  58.         lrflag = 1;
  59.     else if ( strncmp(argv[argn],"-tb",max(strlen(argv[argn]),2)) == 0 ||
  60.               strncmp(argv[argn],"-topbottom",max(strlen(argv[argn]),2)) == 0 )
  61.         tbflag = 1;
  62.     else
  63.         pm_usage( usage );
  64.     argn++;
  65.     }
  66.  
  67.     if ( lrflag && tbflag )
  68.     pm_error( "only one of -lr and -tb may be specified", 0,0,0,0,0 );
  69.     if ( ! ( lrflag || tbflag ) )
  70.     pm_error( "one of -lr or -tb must be specified", 0,0,0,0,0 );
  71.  
  72.     if ( argn < argc )
  73.     {
  74.     nfiles = argc - argn;
  75.     for ( i = 0; i < nfiles; i++ )
  76.         ifd[i] = pm_openr( argv[argn+i] );
  77.     }
  78.     else
  79.     {
  80.     nfiles = 1;
  81.     ifd[0] = stdin;
  82.     }
  83.  
  84.     newcols = 0;
  85.     newrows = 0;
  86.     for ( i = 0; i < nfiles; i++ )
  87.     {
  88.     xels[i] =
  89.         pnm_readpnm( ifd[i], &cols[i], &rows[i], &maxval[i], &format[i] );
  90.     pm_close( ifd[i] );
  91.     if ( i == 0 )
  92.         {
  93.         newmaxval = maxval[i];
  94.         newformat = format[i];
  95.         }
  96.     else if ( format[i] > newformat )
  97.         {
  98.         newmaxval = maxval[i];
  99.         newformat = format[i];
  100.         }
  101.     if ( lrflag )
  102.         {
  103.         newcols += cols[i];
  104.         if ( rows[i] > newrows )
  105.         newrows = rows[i];
  106.         }
  107.     else
  108.         {
  109.         newrows += rows[i];
  110.         if ( cols[i] > newcols )
  111.         newcols = cols[i];
  112.         }
  113.     }
  114.  
  115.     for ( i = 0; i < nfiles; i++ )
  116.     pnm_promoteformat(
  117.         xels[i], cols[i], rows[i], maxval[i], format[i], newmaxval,
  118.         newformat );
  119.  
  120.     if ( ! backdefault )
  121.     if ( backblack )
  122.         background = pnm_blackxel( newmaxval, newformat );
  123.     else
  124.         background = pnm_whitexel( newmaxval, newformat );
  125.  
  126.     newxels = pnm_allocarray( newcols, newrows );
  127.  
  128.     new = 0;
  129.  
  130.     for ( i = 0; i < nfiles; i++ )
  131.     {
  132.     if ( backdefault )
  133.         background =
  134.         pnm_backgroundxel(
  135.             xels[i], cols[i], rows[i], newmaxval, newformat );
  136.  
  137.     if ( lrflag )
  138.         {
  139.         pad = (newrows - rows[i]) / 2;
  140.         for ( col = 0; col < cols[i]; col++ )
  141.         {
  142.         for ( row = 0; row < pad; row++ )
  143.             newxels[row][new+col] = background;
  144.         for ( row = 0; row < rows[i]; row++ )
  145.             newxels[pad+row][new+col] = xels[i][row][col];
  146.         for ( row = pad+rows[i]; row < newrows; row++ )
  147.             newxels[row][new+col] = background;
  148.         }
  149.  
  150.         new += cols[i];
  151.         }
  152.     else
  153.         {
  154.         pad = (newcols - cols[i]) / 2;
  155.         for ( row = 0; row < rows[i]; row++ )
  156.         {
  157.         for ( col = 0; col < pad; col++ )
  158.             newxels[new+row][col] = background;
  159.         for ( col = 0; col < cols[i]; col++ )
  160.             newxels[new+row][pad+col] = xels[i][row][col];
  161.         for ( col = pad+cols[i]; col < newcols; col++ )
  162.             newxels[new+row][col] = background;
  163.         }
  164.  
  165.         new += rows[i];
  166.         }
  167.  
  168.     }
  169.  
  170.     pnm_writepnm( stdout, newxels, newcols, newrows, newmaxval, newformat );
  171.  
  172.     exit( 0 );
  173.     }
  174.