home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / netpbma.zip / pnm / pnmcut.c < prev    next >
C/C++ Source or Header  |  1993-10-04  |  2KB  |  91 lines

  1. /* pnmcut.c - cut a rectangle out of a portable anymap
  2. **
  3. ** Copyright (C) 1989 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 "pnm.h"
  14.  
  15. int
  16. main( argc, argv )
  17.     int argc;
  18.     char* argv[];
  19.     {
  20.     FILE* ifp;
  21.     register xel* xelrow;
  22.     xelval maxval;
  23.     int rows, cols, format, x, y, width, height, row;
  24.     char* usage = "x y width height [pnmfile]";
  25.  
  26.     pnm_init( &argc, argv );
  27.  
  28.     if ( argc < 5 || argc > 6 )
  29.     pm_usage( usage );
  30.  
  31.     if ( sscanf( argv[1], "%d", &x ) != 1 )
  32.     pm_usage( usage );
  33.     if ( sscanf( argv[2], "%d", &y ) != 1 )
  34.     pm_usage( usage );
  35.     if ( sscanf( argv[3], "%d", &width ) != 1 )
  36.     pm_usage( usage );
  37.     if ( sscanf( argv[4], "%d", &height ) != 1 )
  38.     pm_usage( usage );
  39.  
  40.     if ( width < 1 )
  41.     pm_error( "width is less than 1" );
  42.     if ( height < 1 )
  43.     pm_error( "height is less than 1" );
  44.  
  45.     if ( argc == 6 )
  46.     ifp = pm_openr( argv[5] );
  47.     else
  48.     ifp = stdin;
  49.  
  50.     pnm_readpnminit( ifp, &cols, &rows, &maxval, &format );
  51.     xelrow = pnm_allocrow( cols );
  52.  
  53.     if ( x <= -cols )
  54.     pm_error(
  55.         "x is too negative -- the anymap has only %d cols", cols );
  56.     else if ( x >= cols )
  57.     pm_error(
  58.         "x is too large -- the anymap has only %d cols", cols );
  59.     if ( y <= -rows )
  60.     pm_error(
  61.         "y is too negative -- the anymap has only %d rows", rows );
  62.     else if ( y >= rows )
  63.     pm_error(
  64.         "y is too large -- the anymap has only %d rows", rows );
  65.  
  66.     if ( x < 0 )
  67.     x += cols;
  68.     if ( y < 0 )
  69.     y += rows;
  70.  
  71.     if ( x + width > cols )
  72.     pm_error(
  73.         "x + width is too large by %d xels", x + width - cols );
  74.     if ( y + height > rows )
  75.     pm_error(
  76.         "y + height is too large by %d xels", y + height - rows );
  77.  
  78.     pnm_writepnminit( stdout, width, height, maxval, format, 0 );
  79.     for ( row = 0; row < y + height; row++ )
  80.     {
  81.     pnm_readpnmrow( ifp, xelrow, cols, maxval, format );
  82.     if ( row >= y )
  83.         pnm_writepnmrow( stdout, &(xelrow[x]), width, maxval, format, 0 );
  84.     }
  85.  
  86.     pm_close( ifp );
  87.     pm_close( stdout );
  88.  
  89.     exit( 0 );
  90.     }
  91.