home *** CD-ROM | disk | FTP | other *** search
/ Photo CD Demo 1 / Demo.bin / gems / graphics / fastjitt.c < prev    next >
C/C++ Source or Header  |  1992-04-09  |  2KB  |  64 lines

  1. /*
  2.  * Efficient Generation of Sampling Jitter Using Look-up Tables
  3.  * by Joseph M. Cychosz
  4.  * from "Graphics Gems", Academic Press, 1990
  5.  */
  6.  
  7. /*      Jitter.c - Sampling jitter generation routines.          
  8. /*
  9. /*    Description:
  10. /*        Jitter.c contains the routines for generation of sampling  
  11. /*        jitter using look-up tables.
  12. /*
  13. /*    Contents:
  14. /*        Jitter1    Generate random jitter function 1.
  15. /*        Jitter2    Generate random jitter function 2.            
  16. /*        JitterInit    Initialize look-up tables.
  17. /* */
  18.  
  19. #define            NRAN    1024    /* Random number table length    */
  20.  
  21. static    double    URANX[NRAN],        /* Random number tables        */
  22.                 URANY[NRAN];
  23. static    int        IRAND[NRAN];    /* Shuffle table    */
  24. static    int        MASK = NRAN-1;    /* Mask for jitter mod function */
  25. extern    double    xranf();        /* Random number generator pro- */
  26.                     /* ducing uniform numbers 0 to 1 */
  27.  
  28. /*      Jitter1 - Generate random jitter.     */
  29.  
  30. void    Jitter1    (x,y,s,xj,yj)
  31.         int    x, y;        /* Pixel location     */
  32.         int    s;        /* Sample number for the pixel */
  33.         double    *xj, *yj;    /* Jitter (x,y)    */
  34. {
  35.         *xj = URANX[ (x + (y<<2) + IRAND[(x+s)&MASK]) & MASK ];
  36.         *yj = URANY[ (y + (x<<2) + IRAND[(y+s)&MASK]) & MASK ];
  37. }
  38.  
  39.  
  40.  
  41. /*      Jitter2 - Generate random jitter.      */
  42.  
  43. void    Jitter2    (x,y,s,xj,yj)
  44.         int    x, y;        /* Pixel location  */
  45.         int    s;        /* Sample number for the pixel */
  46.         double    *xj, *yj;    /* Jitter (x,y)    */
  47. {
  48.         *xj = URANX[ ((x | (y<<2)) + IRAND[(x+s)&MASK]) & MASK ];
  49.         *yj = URANY[ ((y | (x<<2)) + IRAND[(y+s)&MASK]) & MASK ];
  50. }
  51.  
  52.  
  53. /*      JitterInit - Initialize look-up tables.      */
  54.  
  55. void    JitterInit    ()
  56. {
  57.         int    i;
  58.  
  59.         for ( i = 0 ; i < NRAN ; i++ ) URANX[i] = xranf();
  60.         for ( i = 0 ; i < NRAN ; i++ ) URANY[i] = xranf();
  61.         for ( i = 0 ; i < NRAN ; i++ ) IRAND[i] = (int) (NRAN *
  62.                         xranf());
  63. }
  64.