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 RANDOM (
00017 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 ) { register short j, k, l;
00026     double noise, factor, bias;
00027     char msg[SLEN];
00028 
00029     if (TIMES) TIMING(T_RANDOM);
00030     if (NAMES) {
00031         MESSAGE('I',"");
00032         MESSAGE('I',"RANDOM");
00033         MESSAGE('I',"");
00034         sprintf(msg," Image size: lines:    %d",nlin);
00035         MESSAGE('I',msg);
00036         sprintf(msg,"             pixels:   %d",npix);
00037         MESSAGE('I',msg);
00038         if (option == UNIFORM) {
00039             sprintf(msg," Uniform distribution, range: %12.4e",sigma);
00040         } else if (option == GAUSSIAN) {
00041             sprintf(msg," Gaussian distribution, standard deviation: %12.4e",sigma);
00042         }
00043         MESSAGE('I',msg);
00044         MESSAGE('I'," ...............");
00045     }
00046 
00047     /* check input */
00048     if (nlin <= 0) {
00049         MESSAGE('E'," Number of lines must be greater than zero.");
00050         goto the_end;
00051     } else if (npix <= 0) {
00052         MESSAGE('E'," Number of pixels/line must be greater than zero.");
00053         goto the_end;
00054     } else if (option != UNIFORM  &&  option != GAUSSIAN) {
00055         MESSAGE('E'," Option must be one of UNIFORM or GAUSSIAN.");
00056         goto the_end;
00057     } else if (sigma <= 0.0) {
00058         MESSAGE('E'," Noise variance must be greater than zero.");
00059         goto the_end;
00060     }
00061 
00062     /* create image of appropriate size */
00063     if (!CHECKIMG(*out)) GETMEM(1,nlin,npix,out);
00064     if (!*out) goto the_end;
00065         
00066     factor = (double)RAND_MAX;
00067     if (option == UNIFORM) {
00068         bias = 0.5; 
00069         for (j=0; j<nlin; j++) {
00070             for (k=0; k<npix; k++) {
00071 
00072                 /* convert uniform distribution to range zero to one */
00073                 noise = (double)rand() / factor;
00074 
00075                 /* zero mean and scale to sigma */
00076                 (*out)->data[0][j][k] = (PIXEL)(sigma*(noise - bias));
00077             }
00078         }
00079     } else /* (option == GAUSSIAN) */ {
00080         bias = (double)NTIMES / 2.0;
00081         for (j=0; j<nlin; j++) {
00082             for (k=0; k<npix; k++) {
00083 
00084                 /* convert uniform distribution to gaussian over range zero to one */
00085                 for (noise=0.0,l=0; l<NTIMES; l++) {
00086                     noise += (double)rand();
00087                 }
00088                 noise /= factor;
00089 
00090                 /* zero mean and scale to sigma */
00091                 (*out)->data[0][j][k] = (PIXEL)(sigma*(noise - bias));
00092             }
00093         }
00094     }
00095 
00096     the_end:
00097     if (TIMES) TIMING(T_EXIT);
00098 }

Generated on Wed Apr 9 08:56:10 2003 for TREES by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002