home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume19 / fbm / part02 / flcavg.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-06-08  |  3.1 KB  |  104 lines

  1. /*****************************************************************
  2.  * flcavg.c: FBM Library 0.94 (Beta test) 20-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.  * flcavg.c: Constrained average halftoning
  9.  *
  10.  * CONTENTS
  11.  *    constravg_fbm (input, output, gamma)
  12.  *
  13.  * EDITLOG
  14.  *    LastEditDate = Sat May 20 19:07:06 1989 - Michael Mauldin
  15.  *    LastFileName = /usr2/mlm/src/misc/fbm/flcavg.c
  16.  *
  17.  * HISTORY
  18.  * 20-May-89  Michael Mauldin (mlm) at Carnegie Mellon University
  19.  *    Bug fix from Dave Cohrs <dave@cs.wisc.edu>
  20.  *
  21.  * 07-Mar-89  Michael Mauldin (mlm) at Carnegie Mellon University
  22.  *    Beta release (version 0.9) mlm@cs.cmu.edu
  23.  *
  24.  * 12-Nov-88  Michael Mauldin (mlm) at Carnegie-Mellon University
  25.  *    Created.
  26.  *****************************************************************/
  27.  
  28. # include <stdio.h>
  29. # include <math.h>
  30. # include <ctype.h>
  31. # include "fbm.h"
  32.  
  33. /****************************************************************
  34.  * constravg_fbm: Constrained Average halftoning
  35.  * Reference: Jarvis & Roberts, IEEE Trans. on Commun., v24n8, p 891-898
  36.  ****************************************************************/
  37.  
  38. # define NBR 9
  39.  
  40. #ifndef lint
  41. static char *fbmid =
  42.     "$FBM flcavg.c <0.94> 20-May-89  (C) 1989 by Michael Mauldin$";
  43. #endif
  44.  
  45. constravg_fbm (input, output, gamma)
  46. FBM *input, *output;
  47. double gamma;
  48. { register unsigned char *bmp, *obm;
  49.   register int i, j, rowlen, w, h, sum, thresh, outrow;
  50.   int gamma100 = gamma * 100;
  51.  
  52.   if (input->hdr.planes != 1)
  53.   { fprintf (stderr, "constravg_fbm: can't halftone color images\n");
  54.     return (0);
  55.   }
  56.  
  57.   fprintf (stderr, "Constrained average halftoning, gamma %1.2lf\n", gamma);
  58.  
  59.   /* Allocate output */
  60.   output->hdr = input->hdr;
  61.   output->hdr.bits = 1;
  62.   output->hdr.physbits = 8;
  63.   outrow = 16 * ((input->hdr.cols + 15) / 16); /* Pad to even byte boundary */
  64.   output->hdr.rowlen = outrow;
  65.   output->hdr.plnlen = outrow*output->hdr.rows;
  66.   alloc_fbm (output);
  67.  
  68.   w = input->hdr.cols;
  69.   h = input->hdr.rows;
  70.   rowlen = input->hdr.rowlen;
  71.  
  72.   /* Use threshold of 1/2 in the output border */
  73.   for (j=0; j<h; j++)
  74.   { output->bm[j*outrow]         = input->bm[j*rowlen]         > (WHITE/2);
  75.     output->bm[j*outrow + (w-1)] = input->bm[j*rowlen + (w-1)] > (WHITE/2);
  76.   }
  77.  
  78.   for (i=0; i<w; i++)
  79.   { output->bm[i]                = input->bm[i]                > (WHITE/2);
  80.     output->bm[outrow*(h-1) + i] = input->bm[rowlen*(h-1) + i] > (WHITE/2);
  81.   }
  82.  
  83.   /*
  84.    * Now process the interior bits (use sum instead of average and divide
  85.    * by 9 when we are all done -- this allows fixed point arithmetic)
  86.    */
  87.  
  88.   for (j=1; j<h-1; j++)
  89.   { bmp = &input->bm[j*rowlen];
  90.     obm = &output->bm[j*outrow];
  91.  
  92.     for (i=1; i<w-1; i++)
  93.     { sum = bmp[(i-w) - 1] + bmp[(i-w)] + bmp[(i-w) + 1] +
  94.         bmp[i-1]       + bmp[i]     + bmp[i+1] +
  95.         bmp[i+w-1]     + bmp[i+w]   + bmp[i+w+1];
  96.       
  97.       thresh = gamma100/100 + (sum*WHITE - 2*gamma100*sum/100) / (NBR*WHITE);
  98.       obm[i] = (bmp[i] > thresh) ? 1 : 0;
  99.     }
  100.   }
  101.  
  102.   return (1);
  103. }
  104.