home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume2 / pbm / Part3 / pbmcattb.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-08-07  |  2.7 KB  |  127 lines

  1. /* pbmcattb.c - concatenate portable bitmaps top to bottom
  2. **
  3. ** Copyright (C) 1988 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. #include "pbm.h"
  15.  
  16. #define MAXFILES 100
  17.  
  18. main( argc, argv )
  19. int argc;
  20. char *argv[];
  21.     {
  22.     FILE *ifd[MAXFILES];
  23.     bit **bits[MAXFILES], **newbits, background;
  24.     int argn, backdefault, nfiles, i, c;
  25.     int rows[MAXFILES], cols[MAXFILES], row, col;
  26.     int newrows, newcols, newrow, padleft;
  27.     char *usage = "usage:  %s [-0] [-1] pbmfile pbmfile ...\n";
  28.  
  29.     argn = 1;
  30.     backdefault = 1;
  31.  
  32.     /* Check for flags. */
  33.     if ( argc > argn )
  34.     {
  35.     if ( argv[argn][0] == '-' )
  36.         {
  37.         if ( strcmp( argv[argn], "-0" ) == 0 )
  38.         {
  39.         backdefault = 0;
  40.         background = 0;
  41.         argn++;
  42.         }
  43.         else if ( strcmp( argv[argn], "-1" ) == 0 )
  44.         {
  45.         backdefault = 0;
  46.         background = 1;
  47.         argn++;
  48.         }
  49.         else
  50.         {
  51.         fprintf( stderr, usage, argv[0] );
  52.         exit( 1 );
  53.         }
  54.         }
  55.     }
  56.  
  57.     if ( argc > argn )
  58.     {
  59.     nfiles = argc - argn;
  60.     for ( i = 0; i < nfiles; i++ )
  61.         {
  62.         if ( strcmp( argv[argn+i], "-" ) == 0 )
  63.         ifd[i] = stdin;
  64.         else
  65.         {
  66.         ifd[i] = fopen( argv[argn+i], "r" );
  67.         if ( ifd[i] == NULL )
  68.             {
  69.             fprintf( stderr, "%s: can't open.\n", argv[argn+i] );
  70.             exit( 1 );
  71.             }
  72.         }
  73.         }
  74.     }
  75.     else
  76.     {
  77.     nfiles = 1;
  78.     ifd[0] = stdin;
  79.     }
  80.  
  81.     newcols = 0;
  82.     newrows = 0;
  83.     for ( i = 0; i < nfiles; i++ )
  84.     {
  85.     bits[i] = pbm_readpbm( ifd[i], &cols[i], &rows[i] );
  86.     if ( ifd[i] != stdin )
  87.         fclose( ifd[i] );
  88.     if ( cols[i] > newcols )
  89.         newcols = cols[i];
  90.     newrows += rows[i];
  91.     }
  92.  
  93.     newbits = pbm_allocarray( newcols, newrows );
  94.  
  95.     newrow = 0;
  96.  
  97.     for ( i = 0; i < nfiles; i++ )
  98.     {
  99.     if ( backdefault )
  100.         {
  101.         /* Make a reasonable guess as to what the background is. */
  102.         c = (int) bits[i][0][0] + (int) bits[i][0][cols[i]-1] +
  103.         (int) bits[i][rows[i]-1][0] +
  104.         (int) bits[i][rows[i]-1][cols[i]-1];
  105.         background = ( c <= 2 ) ? 0 : 1;
  106.         }
  107.  
  108.     padleft = (newcols - cols[i]) / 2;
  109.  
  110.     for ( row = 0; row < rows[i]; row++ )
  111.         {
  112.         for ( col = 0; col < padleft; col++ )
  113.         newbits[newrow+row][col] = background;
  114.         for ( col = 0; col <= cols[i]; col++ )
  115.         newbits[newrow+row][padleft+col] = bits[i][row][col];
  116.         for ( col = padleft+cols[i]; col < newcols; col++ )
  117.         newbits[newrow+row][col] = background;
  118.         }
  119.  
  120.     newrow += rows[i];
  121.     }
  122.  
  123.     pbm_writepbm( stdout, newbits, newcols, newrows );
  124.  
  125.     exit( 0 );
  126.     }
  127.