home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / NETWORK / netpbm_src.lzh / NETPBM / PBM / pbmreduce.c < prev    next >
C/C++ Source or Header  |  1996-11-18  |  5KB  |  201 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.  
  101.     srandom( (int) ( time( 0 ) ^ getpid( ) ) );
  102.     for ( col = 0; col < newcols + 2; ++col )
  103.         thiserr[col] = ( random( ) % SCALE - HALFSCALE ) / 4;
  104.         /* (random errors in [-SCALE/8 .. SCALE/8]) */
  105.     }
  106.     direction = 1;
  107.  
  108.     for ( row = 0; row < newrows; ++row )
  109.     {
  110.     for ( subrow = 0; subrow < n; ++subrow )
  111.         pbm_readpbmrow( ifp, bitslice[subrow], cols, format );
  112.  
  113.     if ( halftone == QT_FS )
  114.         for ( col = 0; col < newcols + 2; ++col )
  115.         nexterr[col] = 0;
  116.     if ( direction )
  117.         {
  118.         col = 0;
  119.         limitcol = newcols;
  120.         nbP = newbitrow;
  121.         }
  122.     else
  123.         {
  124.         col = newcols - 1;
  125.         limitcol = -1;
  126.         nbP = &(newbitrow[col]);
  127.         }
  128.  
  129.     do
  130.         {
  131.         sum = 0;
  132.         count = 0;
  133.         for ( subrow = 0; subrow < n; ++subrow )
  134.         for ( subcol = 0; subcol < n; ++subcol )
  135.             if ( row * n + subrow < rows && col * n + subcol < cols )
  136.             {
  137.             count += 1;
  138.             if ( bitslice[subrow][col * n + subcol] == PBM_WHITE )
  139.                 sum += 1;
  140.             }
  141.         sum = ( sum * SCALE ) / count;
  142.  
  143.         if ( halftone == QT_FS )
  144.         sum += thiserr[col + 1];
  145.  
  146.         if ( sum >= threshval )
  147.         {
  148.         *nbP = PBM_WHITE;
  149.         if ( halftone == QT_FS )
  150.             sum = sum - threshval - HALFSCALE;
  151.         }
  152.         else
  153.         *nbP = PBM_BLACK;
  154.  
  155.         if ( halftone == QT_FS )
  156.         {
  157.         if ( direction )
  158.             {
  159.             thiserr[col + 2] += ( sum * 7 ) / 16;
  160.             nexterr[col    ] += ( sum * 3 ) / 16;
  161.             nexterr[col + 1] += ( sum * 5 ) / 16;
  162.             nexterr[col + 2] += ( sum     ) / 16;
  163.             }
  164.         else
  165.             {
  166.             thiserr[col    ] += ( sum * 7 ) / 16;
  167.             nexterr[col + 2] += ( sum * 3 ) / 16;
  168.             nexterr[col + 1] += ( sum * 5 ) / 16;
  169.             nexterr[col    ] += ( sum     ) / 16;
  170.             }
  171.         }
  172.         if ( direction )
  173.         {
  174.         ++col;
  175.         ++nbP;
  176.         }
  177.         else
  178.         {
  179.         --col;
  180.         --nbP;
  181.         }
  182.         }
  183.     while ( col != limitcol );
  184.  
  185.     pbm_writepbmrow( stdout, newbitrow, newcols, 0 );
  186.  
  187.     if ( halftone == QT_FS )
  188.         {
  189.         temperr = thiserr;
  190.         thiserr = nexterr;
  191.         nexterr = temperr;
  192.         direction = ! direction;
  193.         }
  194.     }
  195.  
  196.     pm_close( ifp );
  197.     pm_close( stdout );
  198.  
  199.     exit( 0 );
  200.     }
  201.