home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / dos / grafik / sega / pvquant / quant.doc < prev    next >
Encoding:
Text File  |  1991-11-28  |  22.7 KB  |  555 lines

  1.  
  2.                             QUANT Version 1.30
  3.  
  4.                            A Colour Quantiser
  5.                                 for the
  6.                     Persistence of Vision Raytracer
  7.  
  8.  
  9.  
  10.  
  11.  
  12.  
  13.  
  14.  
  15.  
  16.  
  17.  
  18.  
  19.  
  20.  
  21.  
  22.  
  23. Written and Distributed by:
  24.  
  25. Frank van der Hulst,
  26. 101 Epuni St,
  27. Lower Hutt,
  28. New Zealand
  29.  
  30. Electronic mail to:
  31. UseNet:   frankv@cit.ac.nz, frank@cavebbs.gen.nz
  32. FidoNet:  frank vanderhulst at GenBoard (Node 3:771/160)
  33.  
  34. 19 November 1991
  35. ───────────────────────────────────────────────────────────────────────────
  36.  
  37. This program is freely distributable.  The author retain the copyright
  38. to the program but authorizes free distribution by BBS'es, networks,
  39. magnetic media, etc.  The distributor may charge no more than five
  40. dollars ($5) U.S. for this software.
  41.  
  42. The images and data files generated by the quantiser are the property of
  43. the user of the software and may be used for any purpose without
  44. restriction.
  45.  
  46. The author makes no guarantees or warranties with this program and
  47. claims no responsibility for any damage or loss of time caused by this
  48. program. Bug reports may be sent to the author but the author is under
  49. no obligation to provide bug fixes, features, or any support for this
  50. software.
  51.  
  52. The following conditions are placed on the use of this program:
  53.  
  54. 1) That it should not be used as part of any commercial package without
  55.     the explicit written consent of the author.
  56.  
  57. 2) If you make any changes to the source code, please let me know.
  58.     That's the way this program was built!
  59.  
  60. 3) This text file must accompany the program.
  61.  
  62. This manual is divided into the following sections:
  63.  
  64.    1) Program Description
  65.    2) Background
  66.    3) Getting Started
  67.    4) Command Line Parameters
  68.    5) Output File Format
  69.    6) Source Code Information
  70.    7) Displaying the Images
  71.    8) Animation
  72.    9) Compiling the Code
  73.   10) Porting to Different Platforms
  74.  
  75. 1) Program Description
  76. ~~~~~~~~~~~~~~~~~~~~~~
  77. QUANT is a program to convert image files produced by DKB-Trace or
  78. Persistence of Vision raytracer into a format suitable for display on a
  79. VGA screen.
  80.  
  81. To do this, it must select 256 colours from a palette of 256K colours to
  82. best represent the colours in the image file which are selected from a
  83. palette of 16 million. In other words, it must "quantise" the colours in
  84. the image file (which are selected from a palette of 16 million) into a
  85. set of 256 colours selected from a palette of 256K colours.
  86.  
  87. QUANT is a colour quantisation program which post-processes raw image
  88. files produced by the Persistence of Vision Ray Tracer (PoV Ray). Two
  89. versions can be compiled: one uses Heckbert's median-splitting
  90. algorithm, whereas the other uses Octree quantisation. The Heckbert
  91. version takes much longer to run, especially on a 286. On the other
  92. hand, it produces much better results. The Octree version is faster, but
  93. not as good.
  94.  
  95. As an additional feature, the program allows the user to generate a single
  96. palette from several image files, thus making it possible to produce a
  97. stereoscopic 3D image from two views of a scene, or animation.
  98.  
  99.  
  100. 2) Background
  101. ~~~~~~~~~~~~~
  102. I have a 286 PC clone with a VGA screen, and am interested in virtual
  103. reality. I realise that a 286 doesn't have the horsepower to be good for
  104. that, but that's all I got. Attached to this I have a pair of Sega 3D
  105. glasses, which allows me to view see a 3D image by displaying
  106. stereoscopic pairs of images on the screen.
  107.  
  108. I started off drawing images with Turbo C, but that was time-consuming
  109. and not very effective. I then got a copy of DBW_RENDER and
  110. used it (over many nights over many weeks) to produce images. In the
  111. meantime, Craig Kolb had sent me a copy of his COLORQUANT code, which I
  112. ported to Turbo C. I also found code to display images in 320x400 mode
  113. on my VGA. Finally I could display some good images. I modified the
  114. quantiser to read two input files to produce one palette, so that I
  115. could display a 3D image.
  116.  
  117. The primitives available in DBW_RENDER were very limited, so I switched
  118. to DKB-TRACE v2.12. And ran my machine day and night for months.
  119.  
  120. Then I got access to a 486 running SCO Unix. Great! My 286 took a rest
  121. at nights, because I could generate the most complex scenes in only a
  122. few hours. Unfortunately, I couldn't display the images, or even move
  123. them easily from the 486 (the only direct interchange medium is 360K
  124. floppies, and the QRT-format files are too big). Also working with the
  125. QRT-format was clumsy, so I converted the quantiser to read raw format
  126. files (each of which is only 128K long, thus easily transported). Then I
  127. saw a copy of the Octree quantisation code on UseNet, so I tried that.
  128. It was much faster (a real advantage on the PC) but produced inferior
  129. results. Next step was to move the quantiser to Unix, and suddenly the
  130. Heckbert technique was best.
  131.  
  132. The extra speed of the Unix box opened up some new possibilities -- I
  133. started to look at animation. I could generate about seven images per
  134. hour, then use a display program to flip them up one by one. That works
  135. fine (although it needs a lot of memory on the PC).
  136.  
  137. Then I received an assembler source file by K. Heidenstrom which did
  138. page flipping in 320*200*256 mode. This makes animation easier, since
  139. each image requires less memory. It also makes 3D animation possible.
  140.  
  141. Next is Floyd-Steinberg dithering, to remove unwanted "banding" of coloured
  142. regions (called false contours).
  143.  
  144. Or perhaps converting the GIF files to a standard animation format such
  145. as .FLI or GRASP.
  146.  
  147. Or perhaps 3d animation....
  148.  
  149. 3) Getting Started
  150. ~~~~~~~~~~~~~~~~~~
  151. Before quantising an image, you must create one! Use either DKB-TRACE or
  152. PoVRAY to produce output in the "raw" format.
  153.  
  154. I use the following command line:
  155.         PVRAY -ipacman -opacman -w320 -h400 -v +fr +p +x -a
  156.  
  157. See the raytracer documentation on what all the above mean. Most
  158. important is the "+fr" option which tells the raytracer to produce "raw"
  159. files. These are simply three files with no header information and no
  160. line number information - just the raw data.
  161.  
  162. If you ran PVRAY on a PC, you'll probably need to rename the files
  163. produced. By default it gives extensions of .R8, .G8, and .B8, whereas
  164. QUANT needs extensions of .RED, .GRN, and .BLU.
  165.  
  166. Now try running the OCTREE quantiser program:
  167.         OCTREE pacman
  168.  
  169. 4) Command Line Parameters
  170. ~~~~~~~~~~~~~~~~~~~~~~~~~~
  171.  
  172. Command line format:
  173.         HECKBERT [/C=colours][/W=width][/H=height][/D=display][/T=type]
  174.                  [/O=outputbits][/S=speed] file_1 [file_n]*
  175. or
  176.         OCTREE   [/C=colours][/W=width][/H=height][/D=display][/T=type]
  177.                  [/O=outputbits] file_1 [file_n]*
  178.  
  179.                                                                  Default
  180.                                                                  value
  181.               outputbits   = bits per colour being output          [6]
  182.               type         = 0, 1 or 2 to select output format     [1]
  183.                              0 selects 4-planar output for 320x400
  184.                                or 320x200 images
  185.                              1 selects linear output
  186.                              2 selects GIF output. Only one image at a
  187.                                time can be converted to GIF format.
  188.               display      = 1 to display while outputting         [1]
  189.                              Only available on the PC.
  190.               colours      = number of separate colours to produce [256]
  191.               width        = number of pixels wide the image is    [320]
  192.               height       = number of pixels high the image is    [200]
  193.               speed        = 1 or 0 for fast or slow quantisation  [1]
  194.                              Heckbert speed switch. If zero, the program
  195.                              does another pass through the data, to find
  196.                              the optimal palette selection. This pass is
  197.                              much slower than the earlier passes, and
  198.                              produces slightly better results.
  199.  
  200.               file_1, file_n... are the names (without extensions) of
  201.                              files to be displayed -- the extensions
  202.                              .RED, .GRN, and .BLU are added
  203.                              automatically.
  204.  
  205. If only one file is given to QUANT, it calculates a palette for that
  206. file and writes it to disk with a .2D extension. If two files are given
  207. to QUANT, it calculates one combined palette from both images, then
  208. writes this palette, as well as the data from both files to an output
  209. file with a .3D extension. If more than two files are given, QUANT
  210. assumes this is an animation sequence, and generates one palette for all
  211. the files, then writes the output in the form of several 2D-format files
  212. (one for each input file), all with the same palette. The files are
  213. named FILE_1.0, FILE_1.1, FILE_1.2, ...
  214.  
  215. For example,
  216.  
  217.     C:\>OCTREE GLASS /c=16
  218.  
  219. will open the files GLASS.RED, GLASS.GRN, and GLASS.BLU, use Octree
  220. quantisation to find the 16 best colours, then write the output to to
  221. the file GLASS.2D. As it creates the file, it will display the image on
  222. the screen.
  223.  
  224.     C:\>HECKBERT PACMAN PACMAN_R /c=64 /d=0
  225.  
  226. will open the files PACMAN.RED, PACMAN.GRN, and PACMAN.BLU, then the
  227. files PACMAN_R.RED, PACMAN_R.GRN, and PACMAN_R.BLU, then create the file
  228. PACMAN.3D. This will be done using Heckbert's median-splitting
  229. algorithm.
  230.  
  231.     C:\>HECKBERT TEST_0 TEST_1 TEST_2 TEST_3
  232.  
  233. will read the files TEST_0.RED/GRN/BLU, TEST_1.RED/GRN/BLU, etc., then
  234. create the files TEST_0.0, TEST_0.1, TEST_0.2, and TEST_0.3. This will
  235. be done using Heckbert's median-splitting algorithm.
  236.  
  237. Note that both versions of QUANT require large amounts of memory.
  238.  
  239.  
  240. 5) Output File Format
  241. ~~~~~~~~~~~~~~~~~~~~~
  242. QUANT produces output in one of four possible output formats:
  243.  
  244.         GIF
  245.         2D -- a single image
  246.         3D -- two stereoscopic images of the same scene
  247.         nn -- animation
  248.  
  249. The GIF output file can be displayed using a GIF display program such as
  250. CSHOW or Image Alchemy. The 2D and 3D format files can be displayed
  251. using the provided DISPLAY program. The animation output is in the form
  252. of several 2D-format files (one for each input file), all with the same
  253. palette. The files are named FILE_1.1, FILE_1.2, FILE_1.3, ...
  254.  
  255. The 2D and 3D files have the following format:
  256.     7-byte header
  257.     256*3-byte palette
  258.     one or two screen images (one byte per pixel).
  259.  
  260. The header consists of:
  261.  
  262.         <signature>      - 2 bytes long, value '2D' or '3D', depending
  263.                            on the image type.
  264.         <num columns>   - 2 bytes long (LS byte first)
  265.         <num rows>      - 2 bytes long (LS byte first)
  266.         <num colours>   - 1 byte
  267.  
  268. The palette consists of a series of 3-byte fields, each corresponding to
  269. the combination of blue, green, and red signals for the corresponding
  270. colour. A zero value in the <num colours> field represents 256 colours.
  271.  
  272. A screen image can be in one of two formats: 4-planar or linear
  273. (corresponding to setting the type command line parameter to 0 or 1).
  274. The 4-planar format was designed to make it fast and easy to display an
  275. image in either 320x400x256 or 320x200x256 mode. To facilitate this, the
  276. format of screen image(s) is similar to the memory organisation of the
  277. VGA card in that mode. For any other image size, the program
  278. automatically outputs images in the linear format.
  279.  
  280. Each byte is used by the VGA card as an index into the palette, which is
  281. then used to display the correct colour on the screen.
  282.  
  283. For the 4-planar format of a 320x400 image, each screen image is made up
  284. of 128,000 bytes, one per pixel. Because of the memory organisation of
  285. the VGA card, each image is made up of 4 banks of 32,000 bytes. A VGA
  286. register is set, then a bank is loaded into VGA memory. Each bank
  287. consists of 80 bytes per scan line by 400 scan lines. The bytes from the
  288. four banks illuminate adjacent pixels. For a 4-planar 320x200 image, the
  289. total image size is 64,000 bytes, made up of 4 banks of 16,000.
  290.  
  291. In the linear format, each byte in turn is stored.
  292.  
  293. 6) Source Code Information
  294. ~~~~~~~~~~~~~~~~~~~~~~~~~~
  295. This program is the amalgamation of several pieces of work by different
  296. authors:
  297.  
  298. The colour quantisation algorithms used in QUANT.EXE come from two
  299. sources:
  300. 1. The files OCTREE.C and OCTREE.H are based on code written by Wolfgang
  301. Stuerzlinger who placed it in the public domain. The code implements the
  302. algorithm described in:
  303.  
  304.         Graphic Gems, edited by Andrew Glassner
  305.         Article: A Simple Method for Color Quantisation: Octree Quantisation,
  306.         pg. 287 ff
  307.         by:      Michael Gervautz, Werner Purgathofer
  308.                  Technical University Vienna, Austria
  309.  
  310. 2. The files HECKBERT.C and HECKBERT.H are based on COLORQUANT.C written
  311. by Craig E Kolb who placed it in the public domain.
  312.  
  313. The GIF output code is based on code in version 1.1 (Sep. 1990) of
  314. Gif-Lib, a package written by Gershon Elber.
  315.  
  316. The VGA 320*400 output is based on a program published by Michael Abrash
  317. in Programmer's Journal.
  318.  
  319. The VGA 320*200 output is based on a program (FLIPDEMO.ASM) sent to me
  320. by K. Heidenstrom.
  321.  
  322. All of the above programs are in the public domain.
  323.  
  324. I took all these bits and chopped them round a bit to make them work the
  325. way I like, and added my own code to do things like outputting to my
  326. 2D/3D file, virtual memory management, handle command line parameters, etc.
  327.  
  328.  
  329. 7) Displaying the Images
  330. ~~~~~~~~~~~~~~~~~~~~~~~~
  331. The DISPLAY program provided will display either 320x400 or 320x200
  332. images, either 2D, or stereo 3D. According to Abrash's article, this
  333. should work on any VGA or SVGA card. (On one machine which has a VGA
  334. card, this didn't work). It has the following command line
  335. options:
  336.  
  337. Command line format:
  338.         display FILE1 [FILE2]... [/W] [/C] [/R] [/H]
  339.  
  340.         where FILE1, FILE2... are the names (with extensions) of files
  341.                               to be displayed.
  342.  
  343.         /W    => wait after each following image has been displayed
  344.     /C    => clear screen after each image
  345.         /R    => reverse eye-image sync for following 3D images
  346.         /H    => toggle screen height between 400 and 200 line modes
  347.  
  348. Each of these switches acts as a toggle. The program has the following
  349. defaults:
  350.          wait after each image for a keypress before displaying the next
  351.          clear the screen after each image
  352.          first image of a 3D-pair is for left eye, second is for right eye
  353.          400 line mode
  354.  
  355. 8) Animation
  356. ~~~~~~~~~~~~
  357. Three programs are provided to support animation:
  358.         1)      MAKEANIM generates .DAT files for PoVRay
  359.         2)      ANIMGIF  converts multiple 2D files produced by QUANT
  360.                 into a single multi-image GIF file
  361.         3)      ANIM displays the resulting animation.
  362.  
  363. MAKEANIM:
  364.     This program reads an animation source file (*.ANM), and uses it to
  365.     generate data files for the PoV ray tracer. An initial data file
  366.     must also be given.
  367.  
  368.     Command format:
  369.          MAKEANIM data animation
  370.  
  371.         The animation file is a text file with the following format:
  372.         Line 1: time, sound, name1  [,name2]...
  373.         Line 2:   t1,    s1, value1 [,value2]...
  374.  
  375.     name1, name2, etc. are the names of items #declared in the initial data
  376.     file, separated by commas. The names must be in the same order as they
  377.     are #defined in the data file.
  378.  
  379.         t1 is the time in milliseconds, expressed as an integer, that
  380.         this image is to be displayed for.
  381.  
  382.         s1 is the frequency of sound, expressed as an integer, to be
  383.         produced while this image is displayed. A frequency of 0
  384.         represents no sound.
  385.  
  386.         The t1 and s1 values are ignored by MAKEANIM -- they are used by
  387.         ANIM (see below).
  388.  
  389.         value1, value2, etc. are the values to be assigned to those
  390.         #defined items, separated by commas. Each line represents one
  391.         screen.
  392.  
  393.     This program reads the initial data file, searching for the lines where
  394.     the items named in the animation file are defined.
  395.  
  396.     Data files are then generated, replacing the lines containing the
  397.     #defines identified with new values.
  398.  
  399.         For example, assuming the following is contained in a file
  400.         called PACMAN.ANM.
  401.  
  402. time, sound, lip_upper_angle, lip_lower_angle
  403.   10,     0,             -35,             +10
  404.   20,     0,             -28,              +8
  405.   10,   500,             -20,              +6
  406.   10,   800,             -12,              +4
  407.  
  408.         Executing
  409.  
  410. MAKEANIM PAC_BASE PACMAN
  411.  
  412.         would make 4 copies of PAC_BASE.DAT, generate 4 data files,
  413.         named PACMAN_1.DAT, PACMAN_2.DAT, PACMAN_3.DAT, and
  414.         PACMAN_4.DAT, with the given values being inserted into the
  415.         lines which define lip_upper_angle and lip_lower_angle. These 4
  416.         files are then given to PoVRay to generate 4 scenes.
  417.  
  418. ANIMGIF:
  419.   This program produces a .GIF file containing a series of images to be
  420.   displayed. The images to be read in must be generated by QUANT (either
  421.   version) with the output format set to "type 0" (4-planar). Images are
  422.   stored in the GIF file in a 4-planar format. For example, a 320x200
  423.   image appears to a conventional GIF display program as if it is
  424.   80x800.
  425.  
  426.   The first image in the file is complete, but the others only contain
  427.   part of the image. The program calculates the location of the first
  428.   and last lines which are different between two graphic images. The
  429.   segment between these two lines is stored in GIF format. This file can
  430.   then be read by ANIM to produce animation.
  431.  
  432.   The differences stored in the GIF file are designed to use two frame
  433.   buffers, so they correspond to the following:
  434.  
  435.   GIF Image 1 contains the entire first frame (320x200)
  436.             2 contains the difference between image 1 & frame 2
  437.             3                                       1         3
  438.             4                                       2         4
  439.             5                                       3         5
  440.             6                                       4         6
  441.             etc.
  442.  
  443. Command line format:
  444.         ANIMGIF INPUT_FILE OUTPUT_FILE
  445.  
  446.         where INPUT_FILE is the name (without any extension) of the
  447.         sequence of input files to be loaded into the GIF
  448.  
  449.         OUTPUT_FILE is the name (without extension) of the GIF file to
  450.         be created.
  451.  
  452. ANIM:
  453.    This program produces animation on a 320*200 screen using page flipping.
  454.    The command line parameter is the name (without extension) of the GIF
  455.    file (produced by ANIMGIF above) containing the sequence of images.
  456.  
  457. Command line format:
  458.         ANIM INPUT_FILE [-B[D/G/N] [-T[1/0]]
  459.  
  460.    Note that ANIM *must* be run on a PC with a VGA card. It expands the
  461.    GIF contents to memory, using all available memory (conventional
  462.    memory first, then XMS, then EMS). Note that to use XMS a suitable
  463.    driver such as HIMEM.SYS must be loaded first. Similarly, to use EMS
  464.    a driver must first be loaded.
  465.  
  466.    INPUT_FILE specifies the name of the file to be displayed without any
  467.    extension. ANIM opens the corresponding .GIF file to extract the
  468.    images, and also reads the corresponding .ANM file to find the time
  469.    and sound information.
  470.  
  471.    The -B(uffer) parameter controls what the program will do if it runs
  472.    out of memory.
  473.  
  474.    -BD instructs it to continue to expand the GIF images to a file
  475.    called ANIM.TMP. The resulting animation will be very slow, though,
  476.    unless you have a very fast hard disk with a large cache.
  477.  
  478.    -BG instructs it to expand the remaining images "on-the-fly" from the
  479.    original GIF file. This is even slower than using -BD, and only
  480.    useful on a very fast machine.
  481.  
  482.    -BN instructs the program to stop expanding images.
  483.  
  484.    The -T(ext) parameter controls whether the program displays the
  485.    images as it decompresses them.
  486.  
  487.    -T0 (the default) instructs it to display images.
  488.  
  489.    -T1 instructs it to display text information, such as the destination
  490.    of each image, on the screen while decompressing images.
  491.  
  492.    While images are being decompressed, pressing the ESC key aborts the
  493.    decompression phase and moves immediately to the display phase.
  494.  
  495.    When all the images have been read (or if you decide not to use a
  496.    disk buffer, or press ESC), it will display the animation on the
  497.    screen. Pressing any key will stop the program and return to DOS.
  498.  
  499. 9) Compiling the Code
  500. ~~~~~~~~~~~~~~~~~~~~~
  501.     1) Create a subdirectory for each ZIP file contained in this
  502.        archive (QUANT, ANIM, VGA, GIFLIB), and unZIP each of the ZIP
  503.        files to its directory. UnZIP the INCLUDE.ZIP to your include
  504.        directory.
  505.  
  506.     2) Move to the VGA directory, and edit MAKEFILE to use your compiler
  507.        and library manager. Then use your make utility to create VGA.LIB
  508.        in your LIB directory.
  509.  
  510.     3) Move to the GIFLIB directory, and edit MAKEFILE to use your
  511.        compiler and library manager. Then use your make utility to
  512.        create GIFLIB.LIB in your LIB directory.
  513.  
  514.     4) Move to the QUANT directory, and edit the makefiles OCTREE.MAK,
  515.        HECKBERT.MAK, and DISPLAY.MAK as above, and again make the
  516.        various programs. Alternatively, in Turbo C use the IDE with the
  517.        appropriate project files. To compile the Heckbert version using
  518.        Turbo C's IDE, you need to define the symbol HECKBERT in the
  519.        Compiler/Defines menu.
  520.  
  521.        Note that OCTREE and HECKBERT have some filenames in common which
  522.        are not compatible from one version to another. After
  523.        successfully compiling and linking one version, you should delete
  524.        all the object files produced by that before compiling and
  525.        linking the other version.
  526.  
  527.     5) Move to the ANIM directory, and edit the makefiles MAKEANIM.MAK,
  528.        ANIMGIF.MAK, and ANIM.MAK as above, and again make the various
  529.        programs. Alternatively, in Turbo C use the IDE with the appropriate
  530.        project files.
  531.  
  532.        ANIM also requires the XMSLIB.H and EMSLIB.H files, as well as
  533.        XMSLIBC.OBJ and EMSLIBC.OBJ.
  534.  
  535.        These all relate to the XMSLIB and EMSLIB packages written by
  536.        James W Birdsall. I used XMSLIB v1.21 and EMSLIB v2.15.
  537.  
  538.        The licence conditions for these does not allow them to be
  539.        distributed. They are available from various sources, including
  540.        the SIMTEL archive.
  541.  
  542. 10) Porting to Different Platforms
  543. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  544. I've tried to make QUANT portable. So far, it's working out fairly well.
  545. All the programs (except ANIM -- it is specific to MS-DOS) compile
  546. cleanly and run properly (no warnings, no errors) using either Turbo-C v2.01
  547. or SCO Unix.
  548.  
  549. If you want to port QUANT to another system, please try to modify the
  550. core as little as possible.
  551.  
  552. Similarly, if you implement a different quantisation algorithm, try to
  553. keep it as a separate module.
  554.  
  555.