home *** CD-ROM | disk | FTP | other *** search
/ TopWare 18: Liquid / Image.iso / liquid / top1089 / rdsgen.doc < prev    next >
Text File  |  1993-12-17  |  13KB  |  282 lines

  1.             
  2.              RDSGEN.C    ver. 1.1B
  3.  
  4.         Random Dot Stereogram Generator
  5.  
  6.  
  7.  
  8.  
  9.  
  10.         
  11.  
  12.  
  13. About RDSGEN:
  14.  
  15. RDSGEN creates Single Image Random Dot Stereograms (SIRDS) from
  16. "depth" files that can be created with various other programs.
  17.  
  18. RDSGEN is designed to take type 2 Targa input files from sources such
  19. as POVRAY and POLYRAY ray tracing programs.  It can also take
  20. non-interlaced GIF input files from various programs including
  21. FRACTINT.  The program can automatically detect the input type.
  22.  
  23. RDSGEN will output either a type 3 (gray-scale) Targa file or a GIF
  24. file.  The default output type is GIF.
  25.  
  26. RDSGEN is capable of displaying 320x200, 640x350, or 640x480 images
  27. on a VGA compatible graphics adapter.   Sorry no printer support yet. 
  28.  
  29. RDSGEN uses the red and green color attributes from the input image
  30. to calculate the relative depth value.  This value is used to generate
  31. the depth projection.  This depth encoding technique is consistent
  32. with output created by the POLYRAY ray tracing program.  Depth
  33. encoding can be    simulated with varying degrees of success with other
  34. ray tracing programs as well. 
  35.  
  36. RDSGEN can generate GIF files in Black and White, with 16 random
  37. colors, or with up to 256 colors coming from a background bit map.
  38. GIF background maps can come from any suitable source.
  39.  
  40.  
  41.  
  42. Using RDSGEN:
  43.  
  44. To use RDSGEN type:
  45.  
  46.    RDSGEN    input.ext output.ext options
  47.  
  48.    where:    input.ext  - full filename (path and extension) of the
  49.                  input file.  Data type must be either TGA    
  50.                  or GIF.
  51.  
  52.         output.ext - full filename (path and extension) of
  53.                  the output file.  Extension should be
  54.                  GIF unless the -t option is used in
  55.                  which case the extension should be TGA.
  56.  
  57.         options       - Can be entered in any order following
  58.                  output file name.  The valid program 
  59.                  options follow. 
  60.  
  61.                  a#     : Algorithm number, either 1, 2, or 3.
  62.  
  63.                  b###   : Adaptive background replacement
  64.                       value, range 1 to 64.
  65.  
  66.                          c      : Generate random color background.
  67.                       Uses the standard EGA/VGA palette.
  68.  
  69.                          d###   : Density value, range 0 to 100.
  70.                       Default value is 50 Higher values
  71.                       produce darker images.
  72.  
  73.                  f##    : Depth of field value, range 1 to 16.
  74.                       Limits depth projection to one over
  75.                       the field number times the strip
  76.                       width.  Default value is 2.
  77.  
  78.                  i      : Add indexing triangles to RDS image.
  79.                       Indexing triangles appear at top
  80.                       of the picture and can help some 
  81.                       people align their eyes correctly.
  82.  
  83.                  mc.ext : GIF file name to be used as a
  84.                       background map.  Background image    
  85.                       must have the same dimensions as
  86.                       the image being generated.
  87.  
  88.                  n      : Produces a precedence reversed 
  89.                       (negative) image.
  90.  
  91.                  r##### : Seed number for random generator.
  92.                       Valid values are in the range 1
  93.                       to 32767.  The default value is 1.
  94.                       Use this option if you are getting
  95.                       Tired of the same old background.
  96.  
  97.                  s###   : Strip count, range 1 to width of
  98.                       input image.  Default  value is 8
  99.                       which creates a strip 1/8 the 
  100.                       width of the screen.
  101.  
  102.                  t      : Generate type 3 TGA output file.
  103.  
  104.                  v      : Display image on video screen while
  105.                       processing. Only CGA, EGA, or VGA 
  106.                       graphics are available at this time.
  107.  
  108.  
  109.  
  110. RDS algorithms:
  111.  
  112. RDSGEN 1.1 contains three generating algorithms.  Algorithm one has come
  113. to be known as the "Emperor's New Clothes" algorithm because it was
  114. first documented on GRAPHDEV within a program by the same name.
  115. It can be more accurately attributed to the team of Tyler and Clarke
  116. who first reported the technique in 1990 and coined the term autostereogram.
  117.  
  118. RDSGEN 1.0 modified this algorithm so that depth ranges could be scaled
  119. so as not to exceed 3/4 of the strip width.  This turned out to be a
  120. less than adequate ratio for best viewing.  RDSGEN 1.1 defaults to 1/2 the
  121. strip with and provides a depth-of-field control for greater flexibility.
  122. To combat the persistent echo effects, I added the "right-most-left check".
  123. This technique, described below, was first developed for algorithm two but
  124. it's so fast and effective, I had to retro-fit it into algorithm one.
  125.  
  126. The second algorithm is something of my own invention.  It was created as
  127. a response to some of the shortcomings of algorithm one.  To counter
  128. the skewing effect (left shift), the transformation is applied to pixels
  129. at points equidistant laterally from the perceived point's position in space.  
  130. In other words, instead of copying a dot from some offset back, up to the
  131. current position; copy the dot from half the offset back to half the offset
  132. forward.  This centers the perceived image, thus avoiding the left shift.
  133. Algorithm two also uses the non-linear offset calculation found in algorithm
  134. three.  This produces a more realistic depth projection which helps fool
  135. your brain, and that's really what it's all about.
  136.  
  137. To avoid the echo or false image effect, I developed the "right-most-left
  138. check".  The echo occurs when extreme foreground surfaces are followed
  139. immediately by the background plane.  When the foreground feature is
  140. being calculated, the offset between corresponding pixels is very small
  141. (around half the strip width).  The offset for the background plane
  142. is exactly one strip width all the time.  When the transition from
  143. foreground to background occurs, groups of pixels that are already
  144. encoded with foreground depth information are copied to the right,
  145. repetitively.  This is observed as bands similar to and at the same depth
  146. as the right edges of the objects in the RDS.  The transition causes
  147. the current left pixel (d) to be further left than the previous left
  148. pixel (n).  By keeping track of how far right the left pixel has
  149. proceeded durring the scan, it is a simple matter to detect the transition
  150. point.  If, when the transition occurs, a random or background pixel is
  151. substituted for the right pixel, the echo is eliminated and the problem
  152. is solved.  
  153.  
  154. Algorithm three is based on a routine by Thimbleby, Inglis, and Witten.
  155. This algorithm employs a two pass technique, first determining the
  156. relationship between the pixels then encoding them with appropriate color.  
  157. The relationship between pixels is based on the requirement that two
  158. dots (one for each eye) be perceived in combination as a single dot.
  159. Both dots must therefore be colored the same.  The links between
  160. related (or in the authors terms "constrained") pixels are maintained in
  161. the same[] array.  The algorithm I've coded is the same as the original
  162. in this function, though I have yet to perceive any particular quality of
  163. the output image that is improved by this mechanization. 
  164.  
  165. The greatest image improvement actually comes from the hidden-surface
  166. elimination routine.  In essence, if a point cannot be seen by both
  167. eyes (because a closer surface intervenes) then the two dots don't
  168. have to be the same color (constrained).  Unfortunately the computational
  169. overhead of this routine is outright depressing.  I've taken the liberty
  170. of eliminating the floating point math and employing an incremental
  171. technique that takes the big divides out of the loop.  I also replaced
  172. their left-right pointer juggle (wholly absurd) with a straight-forward
  173. test.  The end result is a routine that is much faster than the original
  174. but still the slowest by far of the three algorithms presented.  The
  175. resulting image is, however, quite good.
  176.  
  177.  
  178.  
  179. Using RDSGEN with FRACTINT:
  180.  
  181. FRACTINT creates a GIF file that can be read right into RDSGEN.  The
  182. trick is to make the color information meaningful.  To do this simply
  183. create the fractal image with a color map composed entirely of shades
  184. of red.  A color map that cycles through the red scale can be made
  185. with an ascii editor.  Once you've created a color map you can create
  186. your fractal.  To use the new color map simply enter color cycling
  187. mode <c> then hit <l> for load.  You will be prompted with a list of
  188. color maps from the current directory.  Select the new color map and
  189. your done.  The file can now be saved as a GIF.    In batch mode just
  190. add map= and the new color map name to the command line.  Similar
  191. results are possible using the grey.map already supplied with FRACTINT.
  192.  
  193. It is also possible to have FRACTINT create a Targa output file. 
  194. Unfortunately this can only be done for 3D projections.  If you
  195. select Light Source Fill you will be prompted with the Color/Mono
  196. option on the next screen.  If you select 1, FRACTINT will create a
  197. 24-bit Targa file.  In batch mode use fullcolor=yes with 3d=yes.
  198.  
  199.  
  200.  
  201. Using RDSGEN with POLYRAY:
  202.  
  203. Virtually any scene that POLYRAY can make, RDSGEN can convert into an
  204. RDS.  Alexander Enzmann was clever enough to add a little option to
  205. POLYRAY that causes the output to contain depth data instead of color
  206. data.  The color for each pixel is calculated as a function of the
  207. distance from the object to the viewer.  The most significant portion
  208. of the depth information is contained in the 8 bits of red intensity.
  209. The least significant depth data is in the green byte.  The blue byte
  210. is always zero.  To use this feature simply enter -p z as a command
  211. line option.  Since RDSGEN cannot handle compressed TGA files, you
  212. should add the -u option as well.
  213.  
  214. The big problem with POLYRAY depth files is that the background is
  215. encoded with high values (0xFFFF).  Because this depth is usually way
  216. out of the range of the objects in the scene, the depth of field
  217. for those objects is reduced to virtually nothing.  In other words
  218. your images become flat.  To combat this problem you can either
  219. provide a background object that completely fills the screen and is
  220. in closer proximity to the other objects, or you can use RDSGEN's
  221. adaptive background feature.  This option allows you to replace the
  222. high values with another, more reasonable, background depth.  The new
  223. background depth is determined as a function of the depth ranges  of
  224. the other objects in the image based on the value passed with the -a
  225. option on the command line.  The new background is deeper than the
  226. deepest visible point in any object by a fraction of the distance to
  227. the least deep point in any object.  For example -b2 would replace
  228. all 0xFFFF depth values with a new value deeper than the lowest
  229. object by 1/2 the depth of all the objects.  This may seem awkward
  230. but something had to be done, trust me.  Another problem happens with
  231. anti-aliasing turned on.  The nice depth values around the edges are
  232. averaged with the bogus background values coming up with something
  233. inbetween.  This ruins the depth of field calculations as well as
  234. providing erroneous halos around your objects.    
  235.  
  236.  
  237. Using RDSGEN with POVRAY:
  238.  
  239. Creating RDS images from POVRAY output is not so simple as the
  240. POLYRAY  stuff.  Maybe somebody will add the depth encoding feature
  241. to POV someday.  Until then, the best technique available is to use
  242. black fog.  The fog command gradually decreases the visibility of the
  243. objects in the scene in direct proportion to the distance from the
  244. viewer.  Pretty much just what we need.  The command needs a color
  245. vector and a distance.  The color should be Black (<0,0,0>) so that
  246. it does not interfere with the color of the objects in the scene.
  247. The distance should be calculated so that the fog obliterates the
  248. scene just beyond the range of the objects being displayed.  If you
  249. use objects about the origin and a point of view along an axis (say Z)
  250. this is a simple matter, otherwise it's a test on the Pythagorean theorem.
  251. All the    objects should be the same color, preferably white, although red
  252. and even green works.  Use full ambient, no diffuse, no phongs, and no
  253. light source(s).  Output produced in this way has been shown to
  254. produce perfectly viewable results.
  255.  
  256. POVRAY is also a good source for background bit maps.  First, create a large
  257. flat object that completely fills the screen.  Select a texture for
  258. this object that will become your background fill pattern.  Best
  259. results are achieved with the more random patterns, stones and
  260. granites.  Use full ambient, no diffuse, no phongs, and no glossy
  261. finishes.  Most stone textures require some lighting to look correct,
  262. but you can't afford any highlights.  Generate the output file at the
  263. same resolution as your depth file.  Using one of the available
  264. utility programs, convert the TGA produced by POVRAY to GIF format.
  265. You now have a 256-color background map for RDSGEN.  Add to map file name
  266. to the /m switch and RDSGEN will make a full-color GIF output file.
  267. Because everybody wants money for their Borland SVGA drivers (BGI
  268. files), I cannot include the required viewing routines and still give away
  269. the code.  You'll have to use a GIF viewer on the output file to see what
  270. you've made.         
  271.  
  272. I'm still working on printer support, so you're on your own there.
  273. The improved algorithms and addition of color to the process has
  274. sidetracked me for the time.  I hope you can appreciate that. 
  275.  
  276. So there it is, try it out.  Please report any problems or bugs. 
  277. Comments will be accepted, money will not.
  278.  
  279. Fred Feucht
  280.  
  281. 74020,407
  282.