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

  1. /*****************************************************************
  2.  * fbgamma.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.  * fbgamma.c: Apply a gamma transform to a colormapped image
  11.  *
  12.  * USAGE
  13.  *     % fbgamma [ -<type> ] [ amount ] < image1 > image2
  14.  *
  15.  * EDITLOG
  16.  *     LastEditDate = Mon Jun 25 00:02:30 1990 - Michael Mauldin
  17.  *     LastFileName = /usr2/mlm/src/misc/fbm/fbgamma.c
  18.  *
  19.  * HISTORY
  20.  * 25-Jun-90  Michael Mauldin (mlm@cs.cmu.edu) Carnegie Mellon
  21.  *    Package for Release 1.0
  22.  *
  23.  * 05-Apr-89  Michael Mauldin (mlm) at Carnegie-Mellon University
  24.  *     Created.
  25.  *****************************************************************/
  26.  
  27. # include <stdio.h>
  28. # include <math.h>
  29. # include "fbm.h"
  30.  
  31. # define USAGE \
  32.   "Usage: fbgamma [ -<type> ] [ amount ] < image > image"
  33.  
  34. #ifndef lint
  35. static char *fbmid =
  36. "$FBM fbgamma.c <1.0> 25-Jun-90  (C) 1989,1990 by Michael Mauldin, source \
  37. code available free from MLM@CS.CMU.EDU and from UUNET archives$";
  38. #endif
  39.  
  40. int debug = 0;
  41. double expon = 1.0;
  42.  
  43. main (argc, argv)
  44. char *argv[];
  45. { FBM image;
  46.   int outtype = DEF_8BIT;
  47.   double amount = 2.2, atof ();
  48.   char *fname = NULL;
  49.  
  50.   /* Get the options */
  51.   while (--argc > 0 && (*++argv)[0] == '-')
  52.   { while (*++(*argv))
  53.     { switch (**argv)
  54.       {
  55.     case 'd':    debug++; break;
  56.     case 'A':    outtype = FMT_ATK; break;
  57.     case 'B':    outtype = FMT_FACE; break;
  58.     case 'F':    outtype = FMT_FBM; break;
  59.     case 'G':    outtype = FMT_GIF; break;
  60.     case 'I':    outtype = FMT_IFF; break;
  61.     case 'L':    outtype = FMT_LEAF; break;
  62.     case 'M':    outtype = FMT_MCP; break;
  63.     case 'P':    outtype = FMT_PBM; break;
  64.     case 'R':    outtype = FMT_RLE; break;
  65.     case 'S':    outtype = FMT_SUN; break;
  66.     case 'T':    outtype = FMT_TIFF; break;
  67.     case 'X':    outtype = FMT_X11; break;
  68.     case 'Z':    outtype = FMT_PCX; break;
  69.     default:        fprintf (stderr, "%s\n", USAGE);
  70.                         exit (1);
  71.       }
  72.     }
  73.   }
  74.  
  75.   if (argc > 0)
  76.   { amount = atof (argv[0]); }
  77.  
  78.   if (argc > 1)
  79.   { fname = argv[1]; }
  80.  
  81.   /* Clear the memory pointers so alloc_fbm won't be confused */
  82.   image.cm  = image.bm  = (unsigned char *) NULL;
  83.  
  84.   /* Read the image */
  85.   if (read_bitmap (&image, fname))
  86.   { int h, w, k;
  87.  
  88.     /* Determine output height & width (oh*ow <= size) */
  89.     h = image.hdr.rows;
  90.     w = image.hdr.cols;
  91.     k = image.hdr.planes;
  92.  
  93.     fprintf (stderr,
  94.          "Gamma correct \"%s\", gamma %1.3lf [%dx%dx%d]\n",
  95.          image.hdr.title[0] ? image.hdr.title : "(untitled)",
  96.          amount, w, h, k);
  97.  
  98.     if (gamma_fbm (&image, amount) &&
  99.         write_bitmap (&image, stdout, outtype))
  100.     { exit (0); }
  101.   }
  102.  
  103.   exit (1);
  104. }
  105.  
  106. gamma_fbm (image, amount)
  107. FBM *image;
  108. double amount;
  109. { double clr;
  110.   register int i, w, h, k;
  111.   register unsigned char *bmp, *btail;
  112.   int cmap[256];
  113.  
  114.   /* Determine output height & width (oh*ow <= size) */
  115.   h = image->hdr.rows;
  116.   w = image->hdr.cols;
  117.   k = image->hdr.planes;
  118.  
  119.   expon = 1.0 / amount;
  120.  
  121.   if (image->hdr.clrlen > 0)
  122.   {
  123.     for (i=0; i<image->hdr.clrlen; i++)
  124.     {
  125.       if (debug) fprintf (stderr, "value %3d => ", image->cm[i]);
  126.       image->cm[i] = gamma_clr (image->cm[i]);
  127.       if (debug) fprintf (stderr, "%3d\n", image->cm[i]);
  128.     }
  129.   }
  130.   else
  131.   {
  132.     for (i=0; i<255; i++)
  133.     { cmap[i] = gamma_clr (i);
  134.  
  135.       if (debug) fprintf (stderr, "value %3d => %3d\n", i, cmap[i]);
  136.     }
  137.     
  138.     bmp = image->bm;
  139.     btail = bmp + image->hdr.plnlen * image->hdr.planes;
  140.     
  141.     while (bmp < btail)
  142.     { *bmp++ = cmap[*bmp]; }
  143.   }
  144.  
  145.   
  146.   return (1);
  147. }
  148.  
  149. int gamma_clr (value)
  150. int value;
  151. { double clr, floor ();
  152.   int newclr;
  153.  
  154.   /* First compute new color */
  155.   clr = 255.0 * pow ((value / 255.0), expon);
  156.  
  157.   /* Round to nearest integer */
  158.   newclr = (int) floor (clr + 0.5);
  159.   
  160.   return (newclr);
  161. }
  162.