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

  1. /*****************************************************************
  2.  * flshrp.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.  * flshrp.c: 
  11.  *
  12.  * CONTENTS
  13.  *    sharpen_fbm (input, output, beta)
  14.  *
  15.  * EDITLOG
  16.  *    LastEditDate = Mon Jun 25 00:18:02 1990 - Michael Mauldin
  17.  *    LastFileName = /usr2/mlm/src/misc/fbm/flshrp.c
  18.  *
  19.  * HISTORY
  20.  * 25-Jun-90  Michael Mauldin (mlm@cs.cmu.edu) Carnegie Mellon
  21.  *    Package for Release 1.0
  22.  *
  23.  * 05-Sep-89  Michael Mauldin (mlm) at Carnegie Mellon University
  24.  *    Beta release (version 0.96) mlm@cs.cmu.edu
  25.  *
  26.  * 12-Nov-88  Michael Mauldin (mlm) at Carnegie-Mellon University
  27.  *    Created.
  28.  *****************************************************************/
  29.  
  30. # include <stdio.h>
  31. # include <math.h>
  32. # include <ctype.h>
  33. # include "fbm.h"
  34.  
  35. /****************************************************************
  36.  * sharpen_fbm: determine whether image is in color, and call the
  37.  *            appropriate sharpening routine.
  38.  ****************************************************************/
  39.  
  40. #ifndef lint
  41. static char *fbmid =
  42. "$FBM flshrp.c <1.0> 25-Jun-90  (C) 1989,1990 by Michael Mauldin, source \
  43. code available free from MLM@CS.CMU.EDU and from UUNET archives$";
  44. #endif
  45.  
  46. sharpen_fbm (input, output, beta)
  47. FBM *input, *output;
  48. double beta;
  49. {
  50.   if (input->hdr.planes == 1 && input->hdr.clrlen == 0)
  51.   { return (sharpen_bw (input, output, beta)); }
  52.   else
  53.   { return (sharpen_clr (input, output, beta)); }
  54. }
  55.  
  56. /****************************************************************
  57.  * sharpen_bw: use a digital Laplacian filter to sharpen a BW image
  58.  ****************************************************************/
  59.  
  60. sharpen_bw (input, output, beta)
  61. FBM *input, *output;
  62. double beta;
  63. { register unsigned char *bmp, *obm;
  64.   register int i, j, rowlen, w, h, sum;
  65.   int new, delta, beta100 = beta * 100;
  66.  
  67.   if (input->hdr.planes != 1)
  68.   { fprintf (stderr, "sharpen_bw: can't sharpen color images\n");
  69.     return (0);
  70.   }
  71.  
  72.   /* Allocate output */
  73.   output->hdr = input->hdr;
  74.   alloc_fbm (output);
  75.  
  76.   w = input->hdr.cols;
  77.   h = input->hdr.rows;
  78.   rowlen = input->hdr.rowlen;
  79.  
  80.   /* Copy edges directly */
  81.   for (j=0; j<h; j++)
  82.   { output->bm[j*rowlen] = input->bm[j*rowlen];
  83.     output->bm[j*rowlen + w-1] = input->bm[j*rowlen + w-1];
  84.   }
  85.  
  86.   for (i=0; i<w; i++)
  87.   { output->bm[i] = input->bm[i];
  88.     output->bm[(h-1)*rowlen + i] = input->bm[(h-1)*rowlen + i];
  89.   }
  90.  
  91.   for (j=1; j < h-1; j++)
  92.   { bmp = &(input->bm[j*rowlen]);
  93.     obm = &(output->bm[j*rowlen]);
  94.     
  95.     for (i=1; i < w-1; i++)
  96.     { sum = bmp[i-rowlen-1] +     bmp[i-rowlen] + bmp[i-rowlen+1] +
  97.         bmp[i-1]        - 8 * bmp[i]        + bmp[i+1]        +
  98.         bmp[i+rowlen-1] +     bmp[i+rowlen] + bmp[i+rowlen+1];
  99.  
  100.       if (sum < 0)
  101.       { delta = - (beta100 * bmp[i] * -sum / (8*WHITE*100)); }
  102.       else
  103.       { delta = beta100 * bmp[i] * sum / (8*WHITE*100); }
  104.  
  105.       new = bmp[i] - delta;
  106.  
  107.       if (new < BLACK) new = BLACK;
  108.       else if (new > WHITE) new = WHITE;
  109.       
  110.       obm[i] = new;
  111.     }
  112.   }
  113.   
  114.   return (1);
  115. }
  116.  
  117. /****************************************************************
  118.  * sharpen_clr: use a digital Laplacian filter to sharpen a CLR image
  119.  ****************************************************************/
  120.  
  121. sharpen_clr (input, output, beta)
  122. FBM *input, *output;
  123. double beta;
  124. { register unsigned char *bmp, *obm, *avg;
  125.   register int i, j, k, rowlen, plnlen, w, h, p, sum;
  126.   int new, delta, beta100 = beta * 100;
  127.   unsigned char *gray;
  128.  
  129.   if (input->hdr.clrlen > 0)
  130.   { fprintf (stderr,
  131.      "cannot sharpen mapped color images, use 'gray2clr -u' first\n");
  132.     return (0);
  133.   }
  134.  
  135.   /* Allocate output */
  136.   output->hdr = input->hdr;
  137.   alloc_fbm (output);
  138.  
  139.   w = input->hdr.cols;
  140.   h = input->hdr.rows;
  141.   p = input->hdr.planes;
  142.   rowlen = input->hdr.rowlen;
  143.   plnlen = input->hdr.plnlen;
  144.   
  145.   /* Calculate the intensity plane */
  146.   gray = (unsigned char *) malloc (plnlen);
  147.  
  148.   for (j=0; j<h; j++)  
  149.   { bmp = &(input->bm[j*rowlen]);
  150.     avg = &(gray[j*rowlen]);    
  151.  
  152.     for (i=0; i<w; i++)
  153.     { sum = 0;
  154.       for (k=0; k<p; k++)
  155.       { sum += bmp[i+k*plnlen]; }
  156.       avg[i] = sum/p;
  157.     }
  158.   }
  159.   
  160.   /* Copy edges directly */
  161.   for (k=0; k<p; k++)
  162.   {  for (j=0; j<h; j++)
  163.     { output->bm[k*plnlen + j*rowlen] =
  164.     input->bm[k*plnlen + j*rowlen];
  165.       output->bm[k*plnlen + j*rowlen + w-1] =
  166.     input->bm[k*plnlen + j*rowlen + w-1];
  167.     }
  168.   
  169.     for (i=0; i<w; i++)
  170.     { output->bm[k*plnlen + i] =
  171.     input->bm[k*plnlen + i];
  172.       output->bm[k*plnlen + (h-1)*rowlen + i] =
  173.     input->bm[k*plnlen + (h-1)*rowlen + i];
  174.     }
  175.   }
  176.  
  177.   for (j=1; j < h-1; j++)
  178.   { avg = &(gray[j*rowlen]);
  179.     
  180.     for (i=1; i < w-1; i++)
  181.     { sum = avg[i-rowlen-1] +     avg[i-rowlen] + avg[i-rowlen+1] +
  182.         avg[i-1]        - 8 * avg[i]        + avg[i+1]        +
  183.         avg[i+rowlen-1] +     avg[i+rowlen] + avg[i+rowlen+1];
  184.  
  185.       for (k=0; k<p; k++)
  186.       { bmp =  &(input->bm[k*plnlen + j*rowlen + i]);
  187.         obm = &(output->bm[k*plnlen + j*rowlen + i]);
  188.         
  189.     if (sum < 0)
  190.     { delta = - (beta100 * *bmp * -sum / (8*WHITE*100)); }
  191.     else
  192.     { delta = beta100 * *bmp * sum / (8*WHITE*100); }
  193.   
  194.     new = *bmp - delta;
  195.   
  196.     if (new < BLACK) new = BLACK;
  197.     else if (new > WHITE) new = WHITE;
  198.     
  199.     *obm = new;
  200.       }
  201.     }
  202.   }
  203.   
  204.   return (1);
  205. }
  206.