home *** CD-ROM | disk | FTP | other *** search
/ Photo CD Demo 1 / Demo.bin / fbm / src / fbhalf.c < prev    next >
C/C++ Source or Header  |  1990-06-24  |  7KB  |  217 lines

  1. /*****************************************************************
  2.  * fbhalf.c: FBM Release 1.0 25-Feb-90 Michael Mauldin
  3.  *
  4.  * Copyright (C) 1989,1990 by Michael Mauldin.  Permission is granted
  5.  * to use this file in whole or in part for any purpose, educational,
  6.  * recreational or commercial, provided that this copyright notice
  7.  * is retained unchanged.  This software is available to all free of
  8.  * charge by anonymous FTP and in the UUNET archives.
  9.  *
  10.  * fbhalf.c: Take an 8bit gray image, resize it to a maximum total
  11.  *          number of pixels, optionally sharpen it with a digital
  12.  *          Laplacian filter, and halftone it using one of three
  13.  *          standard algorithms.  Output the result in PBM format.
  14.  *
  15.  * USAGE
  16.  *    % fbhalf [ -args ]  [ size ] < 8bit > 1bit
  17.  *
  18.  *    size    Choose a width and height as large as possible so that
  19.  *        width is a factor of 8 and width*height <= size (default
  20.  *        is width and height of original 8bit file, ignoring aspect
  21.  *        ratio).
  22.  *
  23.  *    -f    Do Floyd-Steinberg halftoning (the default algorithm)
  24.  *    -b<int>    Do Blue noise halftoning (-b50 or 50% noise is default)
  25.  *    -c<int>    Do Constained average halftoning (-c4 is the default)
  26.  *    -s<int>    Sharpen the image with a given beta (-s2.0 is default)
  27.  *    -t<int>    Use a threshhold of <int> to halftone (127 is default)
  28.  *
  29.  *    -C<int>,-N<int>
  30.  *        Clean up image by flipping isolated pixels.  A pixel is
  31.  *        isolated if there are fewer than C like pixels in the
  32.  *        nearby NxN square.
  33.  *
  34.  * EDITLOG
  35.  *    LastEditDate = Mon Jun 25 00:02:44 1990 - Michael Mauldin
  36.  *    LastFileName = /usr2/mlm/src/misc/fbm/fbhalf.c
  37.  *
  38.  * HISTORY
  39.  * 25-Jun-90  Michael Mauldin (mlm@cs.cmu.edu) Carnegie Mellon
  40.  *    Package for Release 1.0
  41.  *
  42.  * 03-May-89  Michael Mauldin (mlm) at Carnegie Mellon University
  43.  *    Beta release (version 0.93) mlm@cs.cmu.edu
  44.  *
  45.  *  8-Sep-88  Michael Mauldin (mlm) at Carnegie-Mellon University
  46.  *    Created.
  47.  *****************************************************************/
  48.  
  49. # include <stdio.h>
  50. # include <math.h>
  51. # include "fbm.h"
  52.  
  53. # define USAGE\
  54. "Usage: fbhalf [ -fbct<parm> ] [-s<sharpen> ] [ -<type> ]\n\
  55.               [ -C<clean> -N<nbr>] [ size ] < 8bit > 1bit"
  56.  
  57. #ifndef lint
  58. static char *fbmid =
  59. "$FBM fbhalf.c <1.0> 25-Jun-90  (C) 1989,1990 by Michael Mauldin, source \
  60. code available free from MLM@CS.CMU.EDU and from UUNET archives$";
  61. #endif
  62.  
  63. main (argc, argv)
  64. char *argv[];
  65. { int w, h, ow = -1, oh = -1, size = -1, alg = 'b';
  66.   int clean = -1, nbr = 5, outtype = DEF_1BIT;
  67.   double beta = -1e9, parm = -1e9;
  68.   char *title, *credits;
  69.   FBM input, resized, sharpened, halftoned, cleaned, *image;
  70.  
  71.   /* Clear pointers */
  72.   input.bm     = input.cm     = (unsigned char *) NULL;
  73.   resized.bm   = resized.cm   = (unsigned char *) NULL;
  74.   sharpened.bm = sharpened.cm = (unsigned char *) NULL;
  75.   halftoned.bm = halftoned.cm = (unsigned char *) NULL;
  76.   cleaned.bm   = cleaned.cm   = (unsigned char *) NULL;
  77.  
  78.   if (read_bitmap (&input, (char *) NULL))
  79.   {
  80.     if (input.hdr.bits != 8 || input.hdr.physbits != 8)
  81.     { fprintf (stderr,
  82.            "Can't handle images with %d bits and %d physbits per pixel\n",
  83.            input.hdr.bits, input.hdr.physbits);
  84.       exit (1);
  85.     }
  86.  
  87.     if (input.hdr.title[0]) title = input.hdr.title;
  88.     if (input.hdr.credits[0]) credits = input.hdr.credits;
  89.  
  90.     /* Get the options */
  91.     while (--argc > 0 && (*++argv)[0] == '-')
  92.     { while (*++(*argv))
  93.       { switch (**argv)
  94.         { case 's':    if (argv[0][1]) { beta = atof (*argv+1); SKIPARG; }
  95.             else        beta = 2.0;
  96.             break;
  97.             
  98.       case 'f':    alg = 'f'; parm = 0.0; break;
  99.             
  100.       case 'b':    alg = 'b';
  101.             if (argv[0][1])    { parm = atof (*argv+1); SKIPARG; }
  102.             break;
  103.             
  104.       case 'c':    alg = 'c';
  105.             if (argv[0][1])    { parm = atof (*argv+1); SKIPARG; }
  106.             break;
  107.             
  108.       case 't':    alg = 't';
  109.             if (argv[0][1])    { parm = atoi (*argv+1); SKIPARG; }
  110.             else        { parm = 127; }
  111.             break;
  112.             
  113.       case 'C':    if (argv[0][1])    { clean = atoi (*argv+1); SKIPARG; }
  114.               else        { clean = 10; }
  115.             break;
  116.             
  117.       case 'N':    if (argv[0][1])    { nbr = atoi (*argv+1); SKIPARG; }
  118.               else        { nbr = 5; }
  119.             
  120.             if (clean < 0)    { clean = 10; }
  121.             break;
  122.             
  123.       case 'A':    outtype = FMT_ATK; break;
  124.       case 'B':    outtype = FMT_FACE; break;
  125.       case 'F':    outtype = FMT_FBM; break;
  126.       case 'G':    outtype = FMT_GIF; break;
  127.       case 'I':    outtype = FMT_IFF; break;
  128.       case 'L':    outtype = FMT_LEAF; break;
  129.       case 'M':    outtype = FMT_MCP; break;
  130.       case 'P':    outtype = FMT_PBM; break;
  131.       case 'R':    outtype = FMT_RLE; break;
  132.       case 'S':    outtype = FMT_SUN; break;
  133.       case 'T':    outtype = FMT_TIFF; break;
  134.       case 'X':    outtype = FMT_X11; break;
  135.       case 'Z':    outtype = FMT_PCX; break;
  136.  
  137.  
  138.       default:    fprintf (stderr, "%s", USAGE);
  139.             exit (1);
  140.         }
  141.       }
  142.     }
  143.     
  144.     if (argc > 0)    size = atoi (argv[0]);
  145.  
  146.     /* Default parms for algorithms */
  147.     if (parm <= -1e9)
  148.     { if      (alg == 'b') parm = 50.0;
  149.       else if (alg == 'c') parm = 4.0;
  150.       else if (alg == 't') parm = 128.0;
  151.     }
  152.  
  153.     /* Determine output height & width (oh*ow <= size) */
  154.     h = input.hdr.rows;
  155.     w = input.hdr.cols;
  156.  
  157.     if (size < 0)
  158.     { oh = h; ow = w; }
  159.     else
  160.     { ow = sqrt ((double) size * w / (h * input.hdr.aspect));
  161.       ow &= ~7;            /* Make width multiple of 8 */
  162.       oh = ow * input.hdr.aspect * h / w;
  163.     }
  164.  
  165.     fprintf (stderr,
  166.          "Halftone \"%s\" size [%dx%d] => %d pixels\n",
  167.          input.hdr.title[0] ? input.hdr.title : "(untitled)",
  168.          ow, oh, ow*oh);
  169.  
  170.     /* Start with image in variable 'input' */
  171.     image = &input;
  172.  
  173.     /* If necessary, resize it */
  174.     if (w != ow || h != oh)
  175.     { if (extract_fbm (image, &resized, 0, 0, w, h, ow, oh, title, credits))
  176.       { free_fbm (image);
  177.     image = &resized;
  178.       }
  179.       else
  180.       { exit (1); }
  181.     }
  182.  
  183.     /* Sharpen the image if requested */    
  184.     if (beta > -1e9)
  185.     { if (sharpen_fbm (image, &sharpened, beta))
  186.       { free_fbm (image);
  187.     image = &sharpened;
  188.       }
  189.       else
  190.       { exit (1); }
  191.     }
  192.     
  193.     /* Now use the appropriate algorithm to halftone it */
  194.     switch (alg)
  195.     { case 'b':    bluenoise_fbm (image, &halftoned, parm); break;
  196.       case 'c': constravg_fbm (image, &halftoned, parm); break;
  197.       case 't': thesh_fbm (image, &halftoned, (int) parm); break;
  198.       default:    floyd_fbm (image, &halftoned);
  199.     }
  200.  
  201.     /* free_fbm (image); */
  202.     image = &halftoned;
  203.  
  204.     if (clean >= 0)
  205.     { if (!clean_fbm (image, &cleaned, clean, 1, nbr))
  206.       { exit (1); }
  207.  
  208.       free_fbm (image);
  209.       image = &cleaned;      
  210.     }
  211.  
  212.     if (write_bitmap (image, stdout, outtype)) exit (0);
  213.   }
  214.   
  215.   exit (1);
  216. }
  217.