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

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