home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume2 / pbm / Part4 / pbmpaste.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-08-07  |  2.3 KB  |  115 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 rows1, cols1, x, y, rows2, cols2, row, col;
  23.     char *usage = "usage:  %s frompbmfile x y [intopbmfile]\n";
  24.  
  25.  
  26.     if ( argc < 4 || argc > 5 )
  27.     {
  28.     fprintf( stderr, usage, argv[0] );
  29.     exit( 1 );
  30.     }
  31.  
  32.     ifd = fopen( argv[1], "r" );
  33.     if ( ifd == NULL )
  34.     {
  35.     fprintf( stderr, "%s: can't open.\n", argv[1] );
  36.     exit( 1 );
  37.     }
  38.     bits1 = pbm_readpbm( ifd, &cols1, &rows1 );
  39.     fclose( ifd );
  40.  
  41.     if ( sscanf( argv[2], "%d", &x ) != 1 )
  42.     {
  43.     fprintf( stderr, usage, argv[0] );
  44.     exit( 1 );
  45.     }
  46.     if ( sscanf( argv[3], "%d", &y ) != 1 )
  47.     {
  48.     fprintf( stderr, usage, argv[0] );
  49.     exit( 1 );
  50.     }
  51.  
  52.     if ( x < 0 )
  53.     {
  54.     fprintf( stderr, "x is less than 0\n" );
  55.     exit( 1 );
  56.     }
  57.     if ( y < 0 )
  58.     {
  59.     fprintf( stderr, "y is less than 0\n" );
  60.     exit( 1 );
  61.     }
  62.  
  63.     if ( argc == 5 )
  64.     {
  65.         ifd = fopen( argv[4], "r" );
  66.         if ( ifd == NULL )
  67.         {
  68.         fprintf( stderr, "%s: can't open.\n", argv[4] );
  69.         exit( 1 );
  70.         }
  71.     }
  72.     else
  73.     ifd = stdin;
  74.     bits2 = pbm_readpbm( ifd, &cols2, &rows2 );
  75.     if ( ifd != stdin )
  76.     fclose( ifd );
  77.  
  78.     if ( x >= cols2 )
  79.     {
  80.     fprintf(
  81.         stderr, "x is too large -- the second bitmap has only %d cols\n",
  82.         cols2 );
  83.     exit( 1 );
  84.     }
  85.     if ( y >= rows2 )
  86.     {
  87.     fprintf(
  88.         stderr, "y is too large -- the second bitmap has only %d rows\n",
  89.         rows2 );
  90.     exit( 1 );
  91.     }
  92.     if ( x + cols1 > cols2 )
  93.     {
  94.     fprintf(
  95.         stderr, "x + width is too large by %d pixels\n",
  96.         x + cols1 - cols2 );
  97.     exit( 1 );
  98.     }
  99.     if ( y + rows1 > rows2 )
  100.     {
  101.     fprintf(
  102.         stderr, "y + height is too large by %d pixels\n",
  103.         y + rows1 - rows2 );
  104.     exit( 1 );
  105.     }
  106.  
  107.     for ( row = 0; row < rows1; row++ )
  108.         for ( col = 0; col < cols1; col++ )
  109.         bits2[row+y][col+x] = bits1[row][col];
  110.  
  111.     pbm_writepbm( stdout, bits2, cols2, rows2 );
  112.  
  113.     exit( 0 );
  114.     }
  115.