home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / unix / volume19 / fbm / part04 / fbhalf.c next >
C/C++ Source or Header  |  1989-06-08  |  6KB  |  210 lines

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