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

  1. /* pbmpaste.c - paste a rectangle into a portable bitmap
  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. main( argc, argv )
  17. int argc;
  18. char *argv[];
  19.     {
  20.     FILE *ifd;
  21.     bit **bits1, **bits2;
  22.     int argn, rows1, cols1, x, y, rows2, cols2, row, col;
  23.     char function;
  24.     char *usage = "usage:  %s [-r]/[-o]/[-a]/[-x] frompbmfile x y [intopbmfile]\n";
  25.  
  26.     argn = 1;
  27.     function = 'r';
  28.  
  29.     /* Check for flags. */
  30.     if ( argn < argc )
  31.     {
  32.     if ( argv[argn][0] == '-' )
  33.         {
  34.         if ( ( argv[argn][1] == 'r' || argv[argn][1] == 'R' ||
  35.            argv[argn][1] == 'o' || argv[argn][1] == 'O' ||
  36.            argv[argn][1] == 'a' || argv[argn][1] == 'A' ||
  37.            argv[argn][1] == 'x' || argv[argn][1] == 'X' ) &&
  38.          argv[argn][2] == '\0' )
  39.         function = argv[argn][1];
  40.         else
  41.         {
  42.         fprintf( stderr, usage, argv[0] );
  43.         exit( 1 );
  44.         }
  45.         argn++;
  46.         }
  47.     }
  48.  
  49.     if ( argn == argc )
  50.     {
  51.     fprintf( stderr, usage, argv[0] );
  52.     exit( 1 );
  53.     }
  54.     ifd = fopen( argv[argn], "r" );
  55.     if ( ifd == NULL )
  56.     {
  57.     fprintf( stderr, "%s: can't open.\n", argv[argn] );
  58.     exit( 1 );
  59.     }
  60.     bits1 = pbm_readpbm( ifd, &cols1, &rows1 );
  61.     fclose( ifd );
  62.     argn++;
  63.  
  64.     if ( argn == argc )
  65.     {
  66.     fprintf( stderr, usage, argv[0] );
  67.     exit( 1 );
  68.     }
  69.     if ( sscanf( argv[argn], "%d", &x ) != 1 )
  70.     {
  71.     fprintf( stderr, usage, argv[0] );
  72.     exit( 1 );
  73.     }
  74.     argn++;
  75.     if ( argn == argc )
  76.     {
  77.     fprintf( stderr, usage, argv[0] );
  78.     exit( 1 );
  79.     }
  80.     if ( sscanf( argv[argn], "%d", &y ) != 1 )
  81.     {
  82.     fprintf( stderr, usage, argv[0] );
  83.     exit( 1 );
  84.     }
  85.     argn++;
  86.  
  87.     if ( x < 0 )
  88.     {
  89.     fprintf( stderr, "x is less than 0\n" );
  90.     exit( 1 );
  91.     }
  92.     if ( y < 0 )
  93.     {
  94.     fprintf( stderr, "y is less than 0\n" );
  95.     exit( 1 );
  96.     }
  97.  
  98.     if ( argn == argc )
  99.     ifd = stdin;
  100.     else
  101.     {
  102.         ifd = fopen( argv[argn], "r" );
  103.         if ( ifd == NULL )
  104.         {
  105.         fprintf( stderr, "%s: can't open.\n", argv[argn] );
  106.         exit( 1 );
  107.         }
  108.     argn++;
  109.     }
  110.     bits2 = pbm_readpbm( ifd, &cols2, &rows2 );
  111.     if ( ifd != stdin )
  112.     fclose( ifd );
  113.  
  114.     if ( x >= cols2 )
  115.     {
  116.     fprintf(
  117.         stderr, "x is too large -- the second bitmap has only %d cols\n",
  118.         cols2 );
  119.     exit( 1 );
  120.     }
  121.     if ( y >= rows2 )
  122.     {
  123.     fprintf(
  124.         stderr, "y is too large -- the second bitmap has only %d rows\n",
  125.         rows2 );
  126.     exit( 1 );
  127.     }
  128.     if ( x + cols1 > cols2 )
  129.     {
  130.     fprintf(
  131.         stderr, "x + width is too large by %d pixels\n",
  132.         x + cols1 - cols2 );
  133.     exit( 1 );
  134.     }
  135.     if ( y + rows1 > rows2 )
  136.     {
  137.     fprintf(
  138.         stderr, "y + height is too large by %d pixels\n",
  139.         y + rows1 - rows2 );
  140.     exit( 1 );
  141.     }
  142.  
  143.     if ( argn != argc )
  144.     {
  145.     fprintf( stderr, usage, argv[0] );
  146.     exit( 1 );
  147.     }
  148.  
  149.     for ( row = 0; row < rows1; row++ )
  150.         for ( col = 0; col < cols1; col++ )
  151.         switch ( function )
  152.         {
  153.         case 'r':
  154.         case 'R':
  155.         bits2[row+y][col+x] = bits1[row][col];
  156.         break;
  157.  
  158.         case 'o':
  159.         case 'O':
  160.         bits2[row+y][col+x] |= bits1[row][col];
  161.         break;
  162.  
  163.         case 'a':
  164.         case 'A':
  165.         bits2[row+y][col+x] &= bits1[row][col];
  166.         break;
  167.  
  168.         case 'x':
  169.         case 'X':
  170.         bits2[row+y][col+x] ^= bits1[row][col];
  171.         break;
  172.         }
  173.  
  174.     pbm_writepbm( stdout, bits2, cols2, rows2 );
  175.  
  176.     exit( 0 );
  177.     }
  178.