home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume19 / fbm / part02 / fbmask.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-06-08  |  2.7 KB  |  108 lines

  1. /*****************************************************************
  2.  * fbmask.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.  * fbmask.c: Mask a rectangle with a value (or average)
  9.  *
  10.  * USAGE
  11.  *    % fbmask [ flags ] arguments
  12.  *
  13.  * EDITLOG
  14.  *    LastEditDate = Tue Mar  7 19:56:33 1989 - Michael Mauldin
  15.  *    LastFileName = /usr2/mlm/src/misc/fbm/fbmask.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.  * 29-Aug-88  Michael Mauldin (mlm) at Carnegie-Mellon University
  22.  *    Created.
  23.  *****************************************************************/
  24.  
  25. # include <stdio.h>
  26. # include <math.h>
  27. # include "fbm.h"
  28.  
  29. # define USAGE "Usage: fbmask [ -<type> ] x0 y0 x1 y1 value < 8bit > 8bit"
  30.  
  31. #ifndef lint
  32. static char *fbmid =
  33.     "$FBM fbmask.c <0.9> 07-Mar-89  (C) 1989 by Michael Mauldin$";
  34. #endif
  35.  
  36. main (argc, argv)
  37. char *argv[];
  38. { FBM image;
  39.   register int j, i, val, rowlen;
  40.   int x1, x0, y1, y0, n;
  41.   int outtype = DEF_8BIT;
  42.  
  43.   /* Clear the memory pointer so alloc_fbm won't be confused */
  44.   image.cm  = image.bm  = (unsigned char *) NULL;
  45.  
  46.   /* Get the options */
  47.   while (--argc > 0 && (*++argv)[0] == '-')
  48.   { while (*++(*argv))
  49.     { switch (**argv)
  50.       {
  51.     case 'A':    outtype = FMT_ATK; break;
  52.     case 'B':    outtype = FMT_FACE; break;
  53.     case 'F':    outtype = FMT_FBM; break;
  54.     case 'G':    outtype = FMT_GIF; break;
  55.     case 'I':    outtype = FMT_IFF; break;
  56.     case 'L':    outtype = FMT_LEAF; break;
  57.     case 'M':    outtype = FMT_MCP; break;
  58.     case 'P':    outtype = FMT_PBM; 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.     default:        fprintf (stderr, "%s\n", USAGE);
  64.                         exit (1);
  65.       }
  66.     }
  67.   }
  68.  
  69.   /* Get arguments */
  70.   if (argc != 5)
  71.   { fprintf (stderr, "%s\n", USAGE);
  72.     exit (1);
  73.   }
  74.  
  75.   x0  = atoi (argv[0]);
  76.   y0  = atoi (argv[1]);
  77.   x1  = atoi (argv[2]);
  78.   y1  = atoi (argv[3]);
  79.   val = atoi (argv[4]);
  80.   
  81.   /* Assure x0<x1 and y0<y1, swap if necessary */
  82.   if (x0 > x1) { n=x0; x0=x1; x1=n; }
  83.   if (y0 > y1) { n=y0; y0=y1; y1=n; }
  84.  
  85.   if (read_bitmap (&image, (char *) NULL))
  86.   {
  87.     if (image.hdr.planes > 1 ||
  88.         image.hdr.clrlen > 0 ||
  89.         image.hdr.physbits != 8)
  90.     { fprintf (stderr, "fbmask works only for 8bit grayscale files\n");
  91.       exit (1);
  92.     }
  93.  
  94.     rowlen = image.hdr.rowlen;
  95.  
  96.     for (j=y0; j<=y1; j++)
  97.     { for (i=x0; i<=x1; i++)
  98.       { image.bm[j*rowlen + i] = val; }
  99.     }
  100.     
  101.     write_bitmap (&image, stdout, outtype);
  102.   }
  103.   else
  104.   { exit (1); }
  105.   
  106.   exit (0);
  107. }
  108.