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

  1. /*****************************************************************
  2.  * fbm2pod.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.  * fbm2pod.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 Diablo
  14.  *          graphics format.
  15.  *
  16.  * USAGE
  17.  *    % fbm2pod [ -args ]  [ size ] < foo.fbm > foo.pod
  18.  *
  19.  *    size    Choose a width and height as large as possible so that
  20.  *        width is a factor of 8 and width*height <= size (default
  21.  *        is width and height of original 8bit file, ignoring aspect
  22.  *        ratio).
  23.  *
  24.  *    -f    Do Floyd-Steinberg halftoning (the default algorithm)
  25.  *    -bNNN    Do Blue noise halftoning (-b50 or 50% noise is default)
  26.  *    -cNNN    Do Constained average halftoning (-c4 is the default)
  27.  *    -sNNN    Sharpen the image with a given beta (-s2.0 is default)
  28.  *
  29.  * EDITLOG
  30.  *    LastEditDate = Mon Jun 25 00:03:25 1990 - Michael Mauldin
  31.  *    LastFileName = /usr2/mlm/src/misc/fbm/fbm2pod.c
  32.  *
  33.  * HISTORY
  34.  * 25-Jun-90  Michael Mauldin (mlm@cs.cmu.edu) Carnegie Mellon
  35.  *    Package for Release 1.0
  36.  *
  37.  * 07-Mar-89  Michael Mauldin (mlm) at Carnegie Mellon University
  38.  *    Beta release (version 0.9) 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 PODASPECT 1.25
  49.  
  50. # define USAGE \
  51. "Usage: fbm2pod [ -fbc<parm> ] [-s<sharpen> ] [ size ] < 8bit > pod"
  52.  
  53. #ifndef lint
  54. static char *fbmid =
  55. "$FBM fbm2pod.c <1.0> 25-Jun-90  (C) 1989,1990 by Michael Mauldin, source \
  56. code available free from MLM@CS.CMU.EDU and from UUNET archives$";
  57. #endif
  58.  
  59. main (argc, argv)
  60. char *argv[];
  61. { int w, h, ow = -1, oh = -1, size = -1, alg = 'b';
  62.   double beta = -1e9, parm = -1e9;
  63.   char *title;
  64.   FBM input, resized, sharpened, output, *image;
  65.  
  66.   /* Clear the memory pointers so alloc_fbm won't be confused */
  67.   input.cm     = input.bm     = (unsigned char *) NULL;
  68.   resized.cm   = resized.bm   = (unsigned char *) NULL;
  69.   sharpened.cm = sharpened.bm = (unsigned char *) NULL;
  70.   output.cm    = output.bm    = (unsigned char *) NULL;
  71.  
  72.   /* Read the image */
  73.   if (read_bitmap (&input, (char *) NULL))
  74.   {
  75.     if (input.hdr.bits != 8 || input.hdr.physbits != 8)
  76.     { fprintf (stderr,
  77.            "Can't handle images with %d bits and %d physbits per pixel\n",
  78.            input.hdr.bits, input.hdr.physbits);
  79.       exit (1);
  80.     }
  81.  
  82.     if (input.hdr.title[0]) title = input.hdr.title;
  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'; 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.       default:    fprintf (stderr, "%s", USAGE);
  103.             exit (1);
  104.         }
  105.       }
  106.     }
  107.     
  108.     if (argc > 0)    size = atoi (argv[0]);
  109.  
  110.     /* Default parms for algorithms */
  111.     if (parm <= -1e9)
  112.     { if      (alg == 'b') parm = 50.0;
  113.       else if (alg == 'c') parm = 4.0;
  114.     }
  115.  
  116.     /* Determine output height & width (oh*ow <= size) */
  117.     h = input.hdr.rows;
  118.     w = input.hdr.cols;
  119.  
  120.     if (size < 0)
  121.     { oh = h; ow = w; }
  122.     else
  123.     { ow = sqrt ((double) size * w / (h * input.hdr.aspect / PODASPECT));
  124.       ow &= ~7;            /* Make width multiple of 8 */
  125.       oh = ow * input.hdr.aspect/PODASPECT * h / w;
  126.     }
  127.  
  128.     fprintf (stderr,
  129.          "Halftone \"%s\" size [%dx%d] => %d pixels\n",
  130.          input.hdr.title[0] ? input.hdr.title : "(untitled)",
  131.          ow, oh, ow*oh);
  132.  
  133.     /* Start with image in variable 'input' */
  134.     image = &input;
  135.  
  136.     /* If necessary, resize it */
  137.     if (w != ow || h != oh)
  138.     { if (extract_fbm (&input, &resized, 0, 0, w, h, ow, oh, title, (char *) NULL))
  139.       { image = &resized; }
  140.       else
  141.       { exit (1); }
  142.     }
  143.  
  144.     /* Sharpen the image if requested */    
  145.     if (beta > -1e9)
  146.     { if (sharpen_fbm (image, &sharpened, beta))
  147.       { image = &sharpened; }
  148.       else
  149.       { exit (1); }
  150.     }
  151.     
  152.     /* Now use the appropriate algorithm to halftone it */
  153.     switch (alg)
  154.     { case 'b':    bluenoise_fbm (image, &output, parm); break;
  155.       case 'c': constravg_fbm (image, &output, parm); break;
  156.       default:    floyd_fbm (image, &output);
  157.     }
  158.  
  159.     if (write_pod (&output, stdout)) exit (0);
  160.   }
  161.   
  162.   exit (1);
  163. }
  164.  
  165. /****************************************************************
  166.  * write_pod: Write out a binary bitmap as a Diablo file, for use
  167.  *          by the podtype or mp programs.
  168.  ****************************************************************/
  169.  
  170. # define FF "\014"
  171. # define LF "\012"
  172. # define CR "\015"
  173. # define GON    "\033\037\005\033\036\003"
  174. # define GOFF    "\033\037\015\033\036\011"
  175. # define ABSTAB "\033\011"
  176. # define STARTCOL 10
  177.  
  178. write_pod (image, stream)
  179. FBM *image;
  180. FILE *stream;
  181. { register int i, j, h, w;
  182.  
  183.   h = image->hdr.rows;
  184.   w = image->hdr.cols;
  185.  
  186.   /* Bracket commands with form feeds (for podtype) */
  187.   fprintf (stream, "%s%s%s", FF, CR, GOFF);
  188.   
  189.   for (j=0; j<h; j++)
  190.   { fprintf (stream, "%s%c%s", ABSTAB, STARTCOL+1, GON);
  191.     for (i=0; i<w; i++)
  192.     { putchar (image->bm[j*w + i] ? ' ' : '.'); }
  193.     fprintf (stream, "%s%s%s", LF, GOFF, CR);
  194.     
  195.   }
  196.   
  197.   fprintf (stream, "%s", FF);
  198.   
  199.   return (1);
  200. }
  201.