home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / netpbma.zip / pbm / pbmreduce.c < prev    next >
C/C++ Source or Header  |  1996-11-10  |  6KB  |  200 lines

  1. /* pbmreduce.c - read a portable bitmap and reduce it N times
  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 "pbm.h"
  14.  
  15. int
  16. main( argc, argv )
  17.     int argc;
  18.     char* argv[];
  19.     {
  20.     FILE* ifp;
  21.     register bit** bitslice;
  22.     register bit* newbitrow;
  23.     register bit* nbP;
  24.     int argn, n, rows, cols, format, newrows, newcols;
  25.     int row, col, limitcol, subrow, subcol, count, direction;
  26.     char* usage = "[-floyd|-fs | -threshold] [-value <val>] N [pbmfile]";
  27.     int halftone;
  28. #define QT_FS 1
  29. #define QT_THRESH 2
  30. #define SCALE 1024
  31. #define HALFSCALE 512
  32.     long threshval, sum;
  33.     long* thiserr;
  34.     long* nexterr;
  35.     long* temperr;
  36.  
  37.  
  38.     pbm_init( &argc, argv );
  39.  
  40.     argn = 1;
  41.     halftone = QT_FS;
  42.     threshval = HALFSCALE;
  43.  
  44.     while ( argn < argc && argv[argn][0] == '-' && argv[argn][1] != '\0' )
  45.         {
  46.         if ( pm_keymatch( argv[argn], "-fs", 2 ) ||
  47.              pm_keymatch( argv[argn], "-floyd", 2 ) )
  48.             halftone = QT_FS;
  49.         else if ( pm_keymatch( argv[argn], "-threshold", 2 ) )
  50.             halftone = QT_THRESH;
  51.         else if ( pm_keymatch( argv[argn], "-value", 2 ) )
  52.             {
  53.             float f;
  54.  
  55.             ++argn;
  56.             if ( argn == argc || sscanf( argv[argn], "%f", &f ) != 1 ||
  57.                  f < 0.0 || f > 1.0 )
  58.                 pm_usage( usage );
  59.             threshval = f * SCALE;
  60.             }
  61.         else
  62.             pm_usage( usage );
  63.         ++argn;
  64.         }
  65.  
  66.     if ( argn == argc )
  67.         pm_usage( usage );
  68.     if ( sscanf( argv[argn], "%d", &n ) != 1 )
  69.         pm_usage( usage );
  70.     if ( n < 2 )
  71.         pm_error( "N must be greater than 1" );
  72.     ++argn;
  73.  
  74.     if ( argn == argc )
  75.         ifp = stdin;
  76.     else
  77.         {
  78.         ifp = pm_openr( argv[argn] );
  79.         ++argn;
  80.         }
  81.  
  82.     if ( argn != argc )
  83.         pm_usage( usage );
  84.  
  85.     pbm_readpbminit( ifp, &cols, &rows, &format );
  86.     bitslice = pbm_allocarray( cols, n );
  87.  
  88.     newrows = rows / n;
  89.     newcols = cols / n;
  90.     pbm_writepbminit( stdout, newcols, newrows, 0 );
  91.     newbitrow = pbm_allocrow( newcols );
  92.  
  93.     if ( halftone == QT_FS )
  94.         {
  95.         /* Initialize Floyd-Steinberg. */
  96.         thiserr = (long*) malloc( ( newcols + 2 ) * sizeof(long) );
  97.         nexterr = (long*) malloc( ( newcols + 2 ) * sizeof(long) );
  98.         if ( thiserr == 0 || nexterr == 0 )
  99.             pm_error( "out of memory" );
  100.         srandom( (int) ( time( 0 ) ^ getpid( ) ) );
  101.         for ( col = 0; col < newcols + 2; ++col )
  102.             thiserr[col] = ( random( ) % SCALE - HALFSCALE ) / 4;
  103.             /* (random errors in [-SCALE/8 .. SCALE/8]) */
  104.         }
  105.     direction = 1;
  106.  
  107.     for ( row = 0; row < newrows; ++row )
  108.         {
  109.         for ( subrow = 0; subrow < n; ++subrow )
  110.             pbm_readpbmrow( ifp, bitslice[subrow], cols, format );
  111.  
  112.         if ( halftone == QT_FS )
  113.             for ( col = 0; col < newcols + 2; ++col )
  114.                 nexterr[col] = 0;
  115.         if ( direction )
  116.             {
  117.             col = 0;
  118.             limitcol = newcols;
  119.             nbP = newbitrow;
  120.             }
  121.         else
  122.             {
  123.             col = newcols - 1;
  124.             limitcol = -1;
  125.             nbP = &(newbitrow[col]);
  126.             }
  127.  
  128.         do
  129.             {
  130.             sum = 0;
  131.             count = 0;
  132.             for ( subrow = 0; subrow < n; ++subrow )
  133.                 for ( subcol = 0; subcol < n; ++subcol )
  134.                     if ( row * n + subrow < rows && col * n + subcol < cols )
  135.                         {
  136.                         count += 1;
  137.                         if ( bitslice[subrow][col * n + subcol] == PBM_WHITE )
  138.                             sum += 1;
  139.                         }
  140.             sum = ( sum * SCALE ) / count;
  141.  
  142.             if ( halftone == QT_FS )
  143.                 sum += thiserr[col + 1];
  144.  
  145.             if ( sum >= threshval )
  146.                 {
  147.                 *nbP = PBM_WHITE;
  148.                 if ( halftone == QT_FS )
  149.                     sum = sum - threshval - HALFSCALE;
  150.                 }
  151.             else
  152.                 *nbP = PBM_BLACK;
  153.  
  154.             if ( halftone == QT_FS )
  155.                 {
  156.                 if ( direction )
  157.                     {
  158.                     thiserr[col + 2] += ( sum * 7 ) / 16;
  159.                     nexterr[col    ] += ( sum * 3 ) / 16;
  160.                     nexterr[col + 1] += ( sum * 5 ) / 16;
  161.                     nexterr[col + 2] += ( sum     ) / 16;
  162.                     }
  163.                 else
  164.                     {
  165.                     thiserr[col    ] += ( sum * 7 ) / 16;
  166.                     nexterr[col + 2] += ( sum * 3 ) / 16;
  167.                     nexterr[col + 1] += ( sum * 5 ) / 16;
  168.                     nexterr[col    ] += ( sum     ) / 16;
  169.                     }
  170.                 }
  171.             if ( direction )
  172.                 {
  173.                 ++col;
  174.                 ++nbP;
  175.                 }
  176.             else
  177.                 {
  178.                 --col;
  179.                 --nbP;
  180.                 }
  181.             }
  182.         while ( col != limitcol );
  183.  
  184.         pbm_writepbmrow( stdout, newbitrow, newcols, 0 );
  185.  
  186.         if ( halftone == QT_FS )
  187.             {
  188.             temperr = thiserr;
  189.             thiserr = nexterr;
  190.             nexterr = temperr;
  191.             direction = ! direction;
  192.             }
  193.         }
  194.  
  195.     pm_close( ifp );
  196.     pm_close( stdout );
  197.  
  198.     exit( 0 );
  199.     }
  200.