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

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