home *** CD-ROM | disk | FTP | other *** search
/ BCI NET / BCI NET Dec 94.iso / archives / programming / source / fbm12s.lha / fbconv.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-07-18  |  4.7 KB  |  172 lines

  1. /*****************************************************************
  2.  * fbconvolve.c: FBM Release 1.2 18-Apr-93 Michael Mauldin
  3.  *
  4.  * Copyright (C) 1989-1993 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.  * fbconvolve.c: Sharpen an image by using a Laplacian edge enhancement
  11.  *
  12.  * USAGE
  13.  *    % fbconvolve [ flags ] arguments
  14.  *
  15.  * EDITLOG
  16.  *    LastEditDate = Mon Jun 25 00:04:40 1990 - Michael Mauldin
  17.  *    LastFileName = /usr2/mlm/src/misc/fbm/fbconvolve.c
  18.  *
  19.  * HISTORY
  20.  * 18-Apr-93  Michael Mauldin (mlm) at Carnegie-Mellon University
  21.  *    Created from fbsharp.c
  22.  *****************************************************************/
  23.  
  24. # include <stdio.h>
  25. # include <math.h>
  26. # include "fbm.h"
  27.  
  28. # define USAGE "fbconvolve [ -<type> ] [ -c ] <n> <k00> <k01> <k02> ... < 8bit > 8bit"
  29.  
  30. #ifndef lint
  31. static char *fbmid =
  32. "$FBM fbconvolve.c <1.2> 18-Apr-93 (C) 1989-1993 by Michael Mauldin, source \
  33. code available free from MLM@CS.CMU.EDU and from UUNET archives$";
  34. #endif
  35.  
  36. main (argc, argv)
  37. char *argv[];
  38. { int w, h, k, n, absval=1, sobel=0, sobel5=0;
  39.   double **kernel;
  40.   FBM input, output;
  41.   int outtype = DEF_8BIT;
  42.   register int i, j;
  43.  
  44.   /* Get the options */
  45.   while (--argc > 0 && (*++argv)[0] == '-')
  46.   { while (*++(*argv))
  47.     { switch (**argv)
  48.       { 
  49.     case 'A':    outtype = FMT_ATK; break;
  50.     case 'B':    outtype = FMT_FACE; break;
  51.     case 'F':    outtype = FMT_FBM; break;
  52.     case 'G':    outtype = FMT_GIF; break;
  53.     case 'I':    outtype = FMT_IFF; break;
  54.     case 'J':    outtype = FMT_JPEG; break;
  55.     case 'L':    outtype = FMT_LEAF; break;
  56.     case 'M':    outtype = FMT_MCP; break;
  57.     case 'P':    outtype = FMT_PBM; break;
  58.     case 'R':    outtype = FMT_RLE; break;
  59.     case 'S':    outtype = FMT_SUN; break;
  60.     case 'T':    outtype = FMT_TIFF; break;
  61.     case 'X':    outtype = FMT_X11; break;
  62.     case 'Z':    outtype = FMT_PCX; break;
  63.     case 'c':    absval = 0; break;
  64.     case 's':    sobel++; break;
  65.     case '5':    sobel5++; break;
  66.     default:        fprintf (stderr, "%s\n", USAGE);
  67.                         exit (1);
  68.       }
  69.     }
  70.   }
  71.  
  72.   /* Clear the memory pointers so alloc_fbm wont be confused */
  73.   input.cm  = input.bm  = (unsigned char *) NULL;
  74.   output.cm = output.bm = (unsigned char *) NULL;
  75.  
  76.   /* Read the image first */
  77.   if (read_bitmap (&input, (char *) NULL))
  78.   {
  79.     if (input.hdr.bits != 8 || input.hdr.physbits != 8)
  80.     { fprintf (stderr,
  81.            "Can't handle images with %d bits and %d physbits per pixel\n",
  82.            input.hdr.bits, input.hdr.physbits);
  83.       exit (1);
  84.     }
  85.  
  86.     /* Determine output height & width (oh*ow <= size) */
  87.     h = input.hdr.rows;
  88.     w = input.hdr.cols;
  89.     k = input.hdr.planes;
  90.   }
  91.   else
  92.   { exit (1);
  93.   }
  94.  
  95.   /* Sobel 5x5 doesnt have any other arguments */
  96.   if (sobel5)
  97.   {
  98.     fprintf (stderr,
  99.          "Sobel 5x5 \"%s\" [%dx%dx%d]\n",
  100.          input.hdr.title[0] ? input.hdr.title : "(untitled)",
  101.          w, h, k);
  102.  
  103.     /* Convolve the image */
  104.     if (sobel_5x5_fbm (&input, &output) &&
  105.         write_bitmap (&output, stdout, outtype))
  106.     { exit (0); }
  107.  
  108.     exit (1);
  109.   }
  110.  
  111.   /* Sobel doesnt have any other arguments */
  112.   if (sobel)
  113.   {
  114.     fprintf (stderr,
  115.          "Sobel \"%s\" [%dx%dx%d]\n",
  116.          input.hdr.title[0] ? input.hdr.title : "(untitled)",
  117.          w, h, k);
  118.  
  119.     /* Convolve the image */
  120.     if (sobel_fbm (&input, &output) &&
  121.         write_bitmap (&output, stdout, outtype))
  122.     { exit (0); }
  123.  
  124.     exit (1);
  125.   }
  126.  
  127.   /* Otherwise, general convolution, first arg is size of kernel */
  128.   if (argc < 1 || (n = atoi (argv[0])) < 1)
  129.   { fprintf (stderr, "%s\n", USAGE);
  130.     exit (1);
  131.   }
  132.   
  133.   if (n*n !=  argc-1)
  134.   { fprintf (stderr, "fbconvolve: Mismatch between size and number of elements in kernel.\n");
  135.     fprintf (stderr, "        You specified a %dx%d kernel which should have %d elements, but", n, n, n*n);
  136.     fprintf (stderr, "        you only gave %d values.\n", argc-1);
  137.   }
  138.   
  139.   /* Now allocate the kernel array and reads the command line */
  140.   argv++;
  141.  
  142.   kernel = (double **) malloc (n * sizeof (double *));
  143.   for (j=0; j<n; j++)
  144.   { kernel[j] = (double *) malloc (n * sizeof (double));
  145.     
  146.     for (i=0; i<n; i++)
  147.     { if (index ("+-.1234567890", argv[0][0]))
  148.       { kernel[j][i] = atof (*argv++); }
  149.     }
  150.   }
  151.   
  152.   fprintf (stderr,
  153.        "Convolve \"%s\" [%dx%dx%d], kernel\n",
  154.        input.hdr.title[0] ? input.hdr.title : "(untitled)",
  155.        w, h, k);
  156.  
  157.   /* Print the kernel */
  158.   for (j=0; j<n; j++)
  159.   { fprintf (stderr, "\t");
  160.     for (i=0; i<n; i++)
  161.     { fprintf (stderr, "% 6.2lf", kernel[j][i]); }
  162.     fprintf (stderr, "\n");
  163.   }
  164.   
  165.   /* Convolve the image */
  166.   if (convolve_fbm (&input, &output, kernel, n, absval) &&
  167.       write_bitmap (&output, stdout, outtype))
  168.   { exit (0); }
  169.   
  170.   exit (1);
  171. }
  172.