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

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