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

  1. /*****************************************************************
  2.  * fbmask.c: FBM Release 1.2 07-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.  * fbmask.c: Mask a rectangle with a value (or average)
  11.  *
  12.  * USAGE
  13.  *    % fbmask [ flags ] arguments
  14.  *
  15.  * EDITLOG
  16.  *    LastEditDate = Mon Jun 25 00:03:41 1990 - Michael Mauldin
  17.  *    LastFileName = /usr2/mlm/src/misc/fbm/fbmask.c
  18.  *
  19.  * HISTORY
  20.  * 07-Apr-93  Michael Mauldin (mlm) at Carnegie-Mellon University
  21.  *    Added -J switch
  22.  *
  23.  * 25-Jun-90  Michael Mauldin (mlm@cs.cmu.edu) Carnegie Mellon
  24.  *    Package for Release 1.0
  25.  *
  26.  * 07-Mar-89  Michael Mauldin (mlm) at Carnegie Mellon University
  27.  *    Beta release (version 0.9) mlm@cs.cmu.edu
  28.  *
  29.  * 29-Aug-88  Michael Mauldin (mlm) at Carnegie-Mellon University
  30.  *    Created.
  31.  *****************************************************************/
  32.  
  33. # include <stdio.h>
  34. # include <math.h>
  35. # include "fbm.h"
  36.  
  37. # define USAGE "Usage: fbmask [ -<type> ] x0 y0 x1 y1 value < 8bit > 8bit"
  38.  
  39. #ifndef lint
  40. static char *fbmid =
  41. "$FBM fbmask.c <1.2> 07-Apr-93 (C) 1989-1993 by Michael Mauldin, source \
  42. code available free from MLM@CS.CMU.EDU and from UUNET archives$";
  43. #endif
  44.  
  45. main (argc, argv)
  46. char *argv[];
  47. { FBM image;
  48.   register int j, i, val, rowlen;
  49.   int x1, x0, y1, y0, n;
  50.   int outtype = DEF_8BIT;
  51.  
  52.   /* Clear the memory pointer so alloc_fbm won't be confused */
  53.   image.cm  = image.bm  = (unsigned char *) NULL;
  54.  
  55.   /* Get the options */
  56.   while (--argc > 0 && (*++argv)[0] == '-')
  57.   { while (*++(*argv))
  58.     { switch (**argv)
  59.       {
  60.     case 'A':    outtype = FMT_ATK; break;
  61.     case 'B':    outtype = FMT_FACE; break;
  62.     case 'F':    outtype = FMT_FBM; break;
  63.     case 'G':    outtype = FMT_GIF; break;
  64.     case 'I':    outtype = FMT_IFF; break;
  65.     case 'J':    outtype = FMT_JPEG; break;
  66.     case 'L':    outtype = FMT_LEAF; break;
  67.     case 'M':    outtype = FMT_MCP; break;
  68.     case 'P':    outtype = FMT_PBM; break;
  69.     case 'R':    outtype = FMT_RLE; break;
  70.     case 'S':    outtype = FMT_SUN; break;
  71.     case 'T':    outtype = FMT_TIFF; break;
  72.     case 'X':    outtype = FMT_X11; break;
  73.     case 'Z':    outtype = FMT_PCX; break;
  74.     default:        fprintf (stderr, "%s\n", USAGE);
  75.                         exit (1);
  76.       }
  77.     }
  78.   }
  79.  
  80.   /* Get arguments */
  81.   if (argc != 5)
  82.   { fprintf (stderr, "%s\n", USAGE);
  83.     exit (1);
  84.   }
  85.  
  86.   x0  = atoi (argv[0]);
  87.   y0  = atoi (argv[1]);
  88.   x1  = atoi (argv[2]);
  89.   y1  = atoi (argv[3]);
  90.   val = atoi (argv[4]);
  91.   
  92.   /* Assure x0<x1 and y0<y1, swap if necessary */
  93.   if (x0 > x1) { n=x0; x0=x1; x1=n; }
  94.   if (y0 > y1) { n=y0; y0=y1; y1=n; }
  95.  
  96.   if (read_bitmap (&image, (char *) NULL))
  97.   {
  98.     if (image.hdr.planes > 1 ||
  99.         image.hdr.clrlen > 0 ||
  100.         image.hdr.physbits != 8)
  101.     { fprintf (stderr, "fbmask works only for 8bit grayscale files\n");
  102.       exit (1);
  103.     }
  104.  
  105.     rowlen = image.hdr.rowlen;
  106.  
  107.     for (j=y0; j<=y1; j++)
  108.     { for (i=x0; i<=x1; i++)
  109.       { image.bm[j*rowlen + i] = val; }
  110.     }
  111.     
  112.     write_bitmap (&image, stdout, outtype);
  113.   }
  114.   else
  115.   { exit (1); }
  116.   
  117.   exit (0);
  118. }
  119.