Main Page   Data Structures   File List   Data Fields   Globals  

random.c

Go to the documentation of this file.
00001 #include        "sadie.h"
00002 
00003 #define         NTIMES          12
00004 
00005 /*-Copyright Information------------------------------------------------------*/
00006 /* Copyright (c) 1990 by the University of Arizona Digital Image Analysis Lab */
00007 /*----------------------------------------------------------------------------*/
00008 /*-General Information--------------------------------------------------------*/
00009 /*                                                                            */
00010 /*   This function creates an image consisting of random noise. The noise     */
00011 /*   either has a uniform distribution with a range of sigma, or it has a     */
00012 /*   Gaussian distribution with a standard deviation of sigma.                */
00013 /*                                                                            */
00014 /*----------------------------------------------------------------------------*/
00015 /*-Interface Information------------------------------------------------------*/
00016 void
00017 RANDOM (short nlin,             /*  I   Number of lines in the output image.                  */
00018         short npix,             /*  I   Number of pixels/line in the output image.            */
00019         short option,           /*  I   Switch, indicating the noise distribution:            */
00020         /*      UNIFORM    -   Uniform distribution.                  */
00021         /*      GAUSSIAN   -   Gaussian distribution.                 */
00022         double sigma,           /*  I   Standard deviation of the noise.                      */
00023         IMAGE ** out            /*  O   Address of a pointer to the output image.             */
00024 /*----------------------------------------------------------------------------*/
00025   )
00026 {
00027   register short j, k, l;
00028   double noise, factor, bias;
00029   char msg[SLEN];
00030 
00031   if (TIMES)
00032     TIMING (T_RANDOM);
00033   if (NAMES)
00034     {
00035       MESSAGE ('I', "");
00036       MESSAGE ('I', "RANDOM");
00037       MESSAGE ('I', "");
00038       sprintf (msg, " Image size: lines:    %d", nlin);
00039       MESSAGE ('I', msg);
00040       sprintf (msg, "             pixels:   %d", npix);
00041       MESSAGE ('I', msg);
00042       if (option == UNIFORM)
00043         {
00044           sprintf (msg, " Uniform distribution, range: %12.4e", sigma);
00045         }
00046       else if (option == GAUSSIAN)
00047         {
00048           sprintf (msg, " Gaussian distribution, standard deviation: %12.4e",
00049                    sigma);
00050         }
00051       MESSAGE ('I', msg);
00052       MESSAGE ('I', " ...............");
00053     }
00054 
00055   /* check input */
00056   if (nlin <= 0)
00057     {
00058       MESSAGE ('E', " Number of lines must be greater than zero.");
00059       goto the_end;
00060     }
00061   else if (npix <= 0)
00062     {
00063       MESSAGE ('E', " Number of pixels/line must be greater than zero.");
00064       goto the_end;
00065     }
00066   else if (option != UNIFORM && option != GAUSSIAN)
00067     {
00068       MESSAGE ('E', " Option must be one of UNIFORM or GAUSSIAN.");
00069       goto the_end;
00070     }
00071   else if (sigma <= 0.0)
00072     {
00073       MESSAGE ('E', " Noise variance must be greater than zero.");
00074       goto the_end;
00075     }
00076 
00077   /* create image of appropriate size */
00078   if (!CHECKIMG (*out))
00079     GETMEM (1, nlin, npix, out);
00080   if (!*out)
00081     goto the_end;
00082 
00083   factor = (double) RAND_MAX;
00084   if (option == UNIFORM)
00085     {
00086       bias = 0.5;
00087       for (j = 0; j < nlin; j++)
00088         {
00089           for (k = 0; k < npix; k++)
00090             {
00091 
00092               /* convert uniform distribution to range zero to one */
00093               noise = (double) rand () / factor;
00094 
00095               /* zero mean and scale to sigma */
00096               (*out)->data[0][j][k] = (PIXEL) (sigma * (noise - bias));
00097             }
00098         }
00099     }
00100   else                          /* (option == GAUSSIAN) */
00101     {
00102       bias = (double) NTIMES / 2.0;
00103       for (j = 0; j < nlin; j++)
00104         {
00105           for (k = 0; k < npix; k++)
00106             {
00107 
00108               /* convert uniform distribution to gaussian over range zero to one */
00109               for (noise = 0.0, l = 0; l < NTIMES; l++)
00110                 {
00111                   noise += (double) rand ();
00112                 }
00113               noise /= factor;
00114 
00115               /* zero mean and scale to sigma */
00116               (*out)->data[0][j][k] = (PIXEL) (sigma * (noise - bias));
00117             }
00118         }
00119     }
00120 
00121 the_end:
00122   if (TIMES)
00123     TIMING (T_EXIT);
00124 }

Generated on Sun May 18 15:36:12 2003 for tclSadie by doxygen1.3