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

  1. /* pbmcut.c - cut a rectangle out of 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 **bits, **newbits;
  22.     int rows, cols, x, y, width, height, row, col;
  23.     char *usage = "usage:  %s x y width height [pbmfile]\n";
  24.  
  25.  
  26.     if ( argc < 5 || argc > 6 )
  27.     {
  28.     fprintf( stderr, usage, argv[0] );
  29.     exit( 1 );
  30.     }
  31.  
  32.     if ( sscanf( argv[1], "%d", &x ) != 1 )
  33.     {
  34.     fprintf( stderr, usage, argv[0] );
  35.     exit( 1 );
  36.     }
  37.     if ( sscanf( argv[2], "%d", &y ) != 1 )
  38.     {
  39.     fprintf( stderr, usage, argv[0] );
  40.     exit( 1 );
  41.     }
  42.     if ( sscanf( argv[3], "%d", &width ) != 1 )
  43.     {
  44.     fprintf( stderr, usage, argv[0] );
  45.     exit( 1 );
  46.     }
  47.     if ( sscanf( argv[4], "%d", &height ) != 1 )
  48.     {
  49.     fprintf( stderr, usage, argv[0] );
  50.     exit( 1 );
  51.     }
  52.  
  53.     if ( x < 0 )
  54.     {
  55.     fprintf( stderr, "x is less than 0\n" );
  56.     exit( 1 );
  57.     }
  58.     if ( y < 0 )
  59.     {
  60.     fprintf( stderr, "y is less than 0\n" );
  61.     exit( 1 );
  62.     }
  63.     if ( width < 1 )
  64.     {
  65.     fprintf( stderr, "width is less than 1\n" );
  66.     exit( 1 );
  67.     }
  68.     if ( height < 1 )
  69.     {
  70.     fprintf( stderr, "height is less than 1\n" );
  71.     exit( 1 );
  72.     }
  73.  
  74.     if ( argc == 6 )
  75.     {
  76.         ifd = fopen( argv[5], "r" );
  77.         if ( ifd == NULL )
  78.         {
  79.         fprintf( stderr, "%s: can't open.\n", argv[5] );
  80.         exit( 1 );
  81.         }
  82.     }
  83.     else
  84.     ifd = stdin;
  85.  
  86.     bits = pbm_readpbm( ifd, &cols, &rows );
  87.  
  88.     if ( ifd != stdin )
  89.     fclose( ifd );
  90.  
  91.     if ( x >= cols )
  92.     {
  93.     fprintf(
  94.         stderr, "x is too large -- the bitmap has only %d cols\n", cols );
  95.     exit( 1 );
  96.     }
  97.     if ( y >= rows )
  98.     {
  99.     fprintf(
  100.         stderr, "y is too large -- the bitmap has only %d rows\n", rows );
  101.     exit( 1 );
  102.     }
  103.     if ( x + width > cols )
  104.     {
  105.     fprintf(
  106.         stderr, "x + width is too large by %d pixels\n", x + width - cols );
  107.     exit( 1 );
  108.     }
  109.     if ( y + height > rows )
  110.     {
  111.     fprintf(
  112.         stderr, "y + height is too large by %d pixels\n",
  113.         y + height - rows );
  114.     exit( 1 );
  115.     }
  116.  
  117.     newbits = pbm_allocarray( width, height );
  118.     for ( row = y; row < y + height; row++ )
  119.         for ( col = x; col < x + width; col++ )
  120.         newbits[row-y][col-x] = bits[row][col];
  121.  
  122.     pbm_writepbm( stdout, newbits, width, height );
  123.  
  124.     exit( 0 );
  125.     }
  126.