home *** CD-ROM | disk | FTP | other *** search
- /*
- * histexx.c
- *
- * Practical Algorithms for Image Analysis
- *
- * Copyright (c) 1997, 1998, 1999 MLMSoftwareGroup, LLC
- */
-
- /* HISTEXX: program expands intensity range about a chosen input
- * intensity, X, and by a chosen level of expansion
- * usage: histexx inimg outimg [-x X_CENTER] [-s SLOPE] [-t TAPERING_SLOPE] [-L]
- *
- *
- */
-
- #include <stdio.h>
- #include <math.h>
- #include <string.h>
- #include <stdlib.h>
- #include <tiffimage.h> /* tiff info on images */
- #include <images.h> /* contains image format information */
- extern void print_sos_lic ();
-
- #define NBINS 256 /* no. of histogram bins */
- #define XMAX 255.0 /* maximum x-value */
- #define YMAX 255.0 /* maximum y-value */
-
- #define XCENTER_DFLT 128 /* default center of transform */
- #define SLOPE_DFLT 2.0 /* default transform slope */
- #define SLOPE_TAPER_DFLT 0.2 /* default taper slope */
-
- int input (int, char **, long *, double *, double *);
- int usage (short);
-
- int argc;
- char *argv[];
- long *xCenter; /* intensity center of transformation */
- double *slope, /* slope of mapping transform */
- *slopeTaper; /* slope of tapering transform */
-
-
- main (argc, argv)
- int argc;
- char *argv[];
-
- {
- register long x, y; /* image coordinates */
- Image *imgI; /* I/O image structures */
- unsigned char **image, /* input/output data array */
- transform[NBINS]; /* transform array */
- long nCol, nRow; /* image dimensions */
- long xCenter; /* intensity center of transformation */
- double xC; /* (double) of xCenter */
- double slope, /* slope of mapping transform */
- slopeTaper; /* slope of tapering transform */
- double y1, y2; /* tapered y-bounds */
- double a, b, c, temp; /* intermediate variables */
- double xD; /* (double) of x */
-
- /* read user parameter values */
- if ((input (argc, argv, &xCenter, &slope, &slopeTaper)) < 0)
- return (-1);
-
- /* open input file */
- imgI = ImageIn (argv[1]);
- if (imgI->bps == 8 && imgI->spp == 3) {
- printf ("Got RGB image!!!\nInput image must be Grayscale or B&W!!\n");
- exit (1);
- }
- image = imgI->img;
- nRow = ImageGetHeight (imgI);
- nCol = ImageGetWidth (imgI);
- printf ("image size is %dx%d\n", nCol, nRow);
-
- /* calculate transform */
- xC = (double) xCenter;
- a = (1.0 - slope) / (XMAX * XMAX - 3.0 * xC * XMAX + 3.0 * xC * xC);
- b = -3.0 * a * xC;
- c = slope + 3.0 * a * xC * xC;
- for (x = 0; x < NBINS; x++) {
- xD = (double) x;
- temp = a * xD * xD * xD + b * xD * xD + c * xD;
- y1 = slopeTaper * xD;
- y2 = slopeTaper * xD + YMAX - slopeTaper * XMAX;
- if (temp < y1)
- temp = y1;
- else if (temp > y2)
- temp = y2;
- transform[x] = (unsigned char) (temp + 0.5);
- }
-
- /* transform image intensities */
- for (y = 0; y < nRow; y++)
- for (x = 0; x < nCol; x++)
- image[y][x] = transform[image[y][x]];
-
- /* write output image */
- ImageOut (argv[2], imgI);
- return (0);
- }
-
- /* USAGE: function gives instructions on usage of program
- * usage: usage (flag)
- * When flag is 1, the long message is given, 0 gives short.
- */
-
- int
- usage (flag)
- short flag; /* flag =1 for long message; =0 for short message */
- {
-
- /* print short usage message or long */
- printf ("USAGE: histexx inimg outimg [-x X_CENTER] [-s SLOPE] [-t TAPERING_SLOPE] [-L]\n");
- if (flag == 0)
- return (-1);
-
- printf ("\nhistexx expands intensity range about a chosen value.\n\n");
- printf ("ARGUMENTS:\n");
- printf (" inimg: input image filename (TIF)\n");
- printf (" outimg: output image filename (TIF)\n\n");
- printf ("OPTIONS:\n");
- printf (" -x X_CENTER: intensity value about which to increase contrast.\n");
- printf (" (default = %d)\n", XCENTER_DFLT);
- printf (" -s SLOPE: slope of enhancement transform. As the value is\n");
- printf (" set above 1, contrast becomes greater about X_CENTER,\n");
- printf (" but contrast is reduced at other intensities. If slope is\n");
- printf (" set below 1, contrast is reduced at X_CENTER and increased\n");
- printf (" at high and low intensities. If slope is 1, there is no\n");
- printf (" change in the intensities. (Default = %4.1f)\n", SLOPE_DFLT);
- printf ("-t TAPERING_SLOPE: slope of bounds of transformation. Slope\n");
- printf (" values larger than zero produce more tapering.\n");
- printf (" (Default = %4.1f)\n", SLOPE_TAPER_DFLT);
- printf (" -L: print Software License for this module\n");
-
- return (-1);
- }
-
-
- /* INPUT: function reads input parameters
- * usage: input (argc, argv, &xCenter, &slope, &slopeTaper)
- */
-
- #define USAGE_EXIT(VALUE) {usage (VALUE); return (-1);}
-
- input (argc, argv, xCenter, slope, slopeTaper)
- int argc;
- char *argv[];
- long *xCenter; /* intensity center of transformation */
- double *slope, /* slope of mapping transform */
- *slopeTaper; /* slope of tapering transform */
- {
- long n;
-
- if (argc == 1)
- USAGE_EXIT (1);
- if (argc == 2)
- USAGE_EXIT (2);
-
- *xCenter = XCENTER_DFLT;
- *slope = SLOPE_DFLT;
- *slopeTaper = SLOPE_TAPER_DFLT;
-
- for (n = 3; n < argc; n++) {
- if (strcmp (argv[n], "-x") == 0) {
- if (++n == argc)
- USAGE_EXIT (0);
- *xCenter = (long) atol (argv[n]);
- }
- else if (strcmp (argv[n], "-s") == 0) {
- if (++n == argc)
- USAGE_EXIT (0);
- *slope = atof (argv[n]);
- }
- else if (strcmp (argv[n], "-t") == 0) {
- if (++n == argc)
- USAGE_EXIT (0);
- *slopeTaper = atof (argv[n]);
- }
- else if (strcmp (argv[n], "-L") == 0) {
- print_sos_lic ();
- exit (0);
- }
- else
- USAGE_EXIT (0);
- }
-
- return (0);
- }
-