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

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