home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume19 / fbm / part04 / flshrp.c < prev   
Encoding:
C/C++ Source or Header  |  1989-06-08  |  5.2 KB  |  204 lines

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