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

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