home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume5 / pbm3 / part2 / pbmcatlr.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-02-03  |  2.8 KB  |  128 lines

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