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

  1. /*****************************************************************
  2.  * fbsample.c: FBM Library 0.94 (Beta test) 20-May-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.  * fbsample.c:  1 bit to 8 bit conversion by sampling
  9.  *
  10.  * USAGE
  11.  *    % fbsample [ title ] < foo.pbm > foo.fbm
  12.  *
  13.  * EDITLOG
  14.  *    LastEditDate = Sat May 20 19:05:33 1989 - Michael Mauldin
  15.  *    LastFileName = /usr2/mlm/src/misc/fbm/fbsample.c
  16.  *
  17.  * HISTORY
  18.  * 20-May-89  Michael Mauldin (mlm) at Carnegie Mellon University
  19.  *    Bug fix from Dave Cohrs <dave@cs.wisc.edu>
  20.  *
  21.  * 07-Mar-89  Michael Mauldin (mlm) at Carnegie Mellon University
  22.  *    Beta release (version 0.9) mlm@cs.cmu.edu
  23.  *
  24.  *  5-Sep-88  Michael Mauldin (mlm) at Carnegie-Mellon University
  25.  *    Created.
  26.  *****************************************************************/
  27.  
  28. #include <stdio.h>
  29. #include <ctype.h>
  30. #include "fbm.h"
  31.  
  32. int width, height;
  33.  
  34. # define USAGE \
  35.   "fbsample [ -t'title' -c'credits' -g'grain' -n'nbr' ]\n\
  36.      [ -<type> ] < bitmap > image"
  37.  
  38. #ifndef lint
  39. static char *fbmid =
  40.     "$FBM fbsample.c <0.94> 20-May-89  (C) 1989 by Michael Mauldin$";
  41. #endif
  42.  
  43. main(argc, argv)
  44.   int             argc;
  45.   char           *argv[];
  46. {
  47.   char           *title = NULL, *credits = NULL;
  48.   register unsigned char *bmp;
  49.   register int      i, j, r, c, irow, orow, grain=5, nbr=5, sum;
  50.   int          outtype = DEF_1BIT;
  51.   FBM          input, output;
  52.  
  53.   /* Clear the memory pointers so alloc_fbm won't be confused */
  54.   input.cm  = input.bm  = (unsigned char *) NULL;
  55.   output.cm = output.bm = (unsigned char *) NULL;
  56.  
  57.   /* Get the options */
  58.   while (--argc > 0 && (*++argv)[0] == '-')
  59.   { while (*++(*argv))
  60.     { switch (**argv)
  61.       { case 'g':    grain = atoi (*argv+1); SKIPARG; break;
  62.         case 'n':    nbr = atoi (*argv+1); SKIPARG; break;
  63.     case 't':    title = *argv+1; SKIPARG; break;
  64.     case 'c':    credits = *argv+1; SKIPARG; break;
  65.     case 'A':    outtype = FMT_ATK; break;
  66.     case 'B':    outtype = FMT_FACE; break;
  67.     case 'F':    outtype = FMT_FBM; break;
  68.     case 'G':    outtype = FMT_GIF; break;
  69.     case 'I':    outtype = FMT_IFF; break;
  70.     case 'L':    outtype = FMT_LEAF; break;
  71.     case 'M':    outtype = FMT_MCP; break;
  72.     case 'P':    outtype = FMT_PBM; break;
  73.     case 'S':    outtype = FMT_SUN; break;
  74.     case 'T':    outtype = FMT_TIFF; break;
  75.     case 'X':    outtype = FMT_X11; break;
  76.     case 'Z':    outtype = FMT_PCX; break;
  77.     default:    fprintf (stderr, "Usage: %s\n", USAGE);
  78.             exit (1);
  79.       }
  80.     }
  81.   }
  82.  
  83.   if (grain < 1 || grain > 16)
  84.   { fprintf (stderr,
  85.          "Usage: %s\n%s\n",
  86.          "       (grain must be between 1 and 16)\n");
  87.     exit (0);
  88.   }
  89.  
  90.   /* Read pbm bitmap */
  91.   if (! read_bitmap (&input, (char *) NULL))
  92.   { fprintf (stderr, "%s: input garbled or not in PBM format\n", argv[0]);
  93.     exit(1);
  94.   }
  95.  
  96.   if (title == NULL && input.hdr.title[0])
  97.   { title = input.hdr.title; }
  98.  
  99.   if (grain > 1)
  100.   { width = input.hdr.cols / grain;
  101.     height = input.hdr.rows / grain;
  102.   }
  103.   else
  104.   { width = input.hdr.cols - grain + 1;
  105.     height = input.hdr.rows - grain + 1;
  106.   }
  107.  
  108.   orow = 2 * ((width * 8 + 15) / 16);
  109.   irow = input.hdr.rowlen;
  110.  
  111.   fprintf (stderr, "Sample (1bit to 8bit) \"%s\": [%dx%dx1] ==> [%dx%dx8]\n",
  112.       title ? title : "(untitled)",
  113.       input.hdr.cols, input.hdr.rows, width, height);
  114.  
  115.   /* Set up output header */
  116.   output.hdr.cols = width;
  117.   output.hdr.rows = height;
  118.   output.hdr.planes = 1;
  119.   output.hdr.bits = 8;
  120.   output.hdr.physbits = 8;
  121.   output.hdr.rowlen = orow;
  122.   output.hdr.plnlen = orow * height;
  123.   output.hdr.aspect = 1.0;
  124.   output.hdr.clrlen = 0;
  125.  
  126.   if (title) strcpy (output.hdr.title, title);
  127.   if (credits) strcpy (output.hdr.credits, credits);
  128.   
  129.   alloc_fbm (&output);
  130.  
  131.   fprintf (stderr, "width %d, height %d, grain %d, nbr %d, irow %d, orow %d\n",
  132.        width, height, grain, nbr, irow, orow);
  133.   
  134.   for (r=0; r<height; r++)
  135.   { for (c=0; c<width; c++)
  136.     { sum = 0;
  137.       bmp = &(input.bm[(r*grain) * irow + (c*grain)]);
  138.  
  139.       for (j=0; j<nbr; j++, bmp += irow)
  140.       { for (i=0; i<nbr; i++)
  141.         { if (bmp[i]) sum++; }
  142.       }
  143.  
  144.       if (sum > 255) sum = 255;
  145.       
  146.       output.bm[r * orow + c] = sum;
  147.     }
  148.   }
  149.  
  150.   write_bitmap (&output, stdout, outtype);
  151.  
  152.   exit (0);
  153. }
  154.