home *** CD-ROM | disk | FTP | other *** search
/ Resource Library: Graphics / graphics-16000.iso / msdos / animutil / fastgfx / fg303b / manuals.arj / USER09.DOC < prev    next >
Text File  |  1993-10-02  |  45KB  |  982 lines

  1. Chapter 9
  2.  
  3.  
  4.  
  5.  
  6.  
  7. Image Files
  8. 172   Fastgraph User's Guide
  9.  
  10. Overview
  11.  
  12.      Within the context of Fastgraph, an image is a rectangular area
  13. containing some type of picture.  An image might be something as simple as a
  14. pointing hand icon, or as detailed as the dashboard of a sports car.
  15. Fastgraph includes several routines to display, retrieve, and manipulate
  16. images.  In this chapter we'll begin our discussion of images by looking at
  17. the image file formats Fastgraph supports, as well as the routines available
  18. for displaying and creating image files.
  19.  
  20.  
  21. PCX Files
  22.  
  23.      The PCX file format was originally developed by ZSoft Corporation for
  24. their commercial paint program, PC Paintbrush.  It has evolved into one of
  25. the more popular image file formats because so many products can read and
  26. write PCX files to at least some extent.  Fastgraph includes routines for
  27. displaying and creating PCX files, as well as other PCX support functions.
  28.  
  29.      The fg_showpcx routine displays an image stored in a PCX file.  It can
  30. position the image using the coordinate information in the PCX header, or
  31. such that its upper left corner is at the graphics cursor position on the
  32. active video page.  The first argument to fg_showpcx is the name of the PCX
  33. file (it may include a path name), and its second argument is a 16-bit mask
  34. that controls how the image is displayed.  The file name must be terminated
  35. with a null character, so BASIC, FORTRAN, and Pascal programmers will need to
  36. store a zero byte as the last character of the file name string.
  37.  
  38.      In the current version of Fastgraph, only the low-order two bits (bits 0
  39. and 1) of the bit mask argument are meaningful.  The following table
  40. summarizes the meanings of these bits.
  41.  
  42.               Bit Value  Meaning
  43.  
  44.                0    0    Use palette values stored in the PCX file
  45.                0    1    Use the current palette settings
  46.                1    0    Display image at position indicated in PCX header
  47.                1    1    Display image at current graphics position
  48.  
  49. All other bits are reserved and should be zero to guarantee compatibility
  50. with future releases.  The fg_showpcx routine returns a value of 0 if
  51. successful, 1 if the specified file wasn't found, and 2 if the file is not a
  52. PCX file.
  53.  
  54.      The fg_makepcx routine creates a PCX file from the specified rectangular
  55. region of the active video page.  Its first four arguments define the minimum
  56. x, maximum x, minimum y, and maximum y screen space coordinates of the region
  57. (the minimum x coordinate is reduced to a byte boundary if necessary).  Its
  58. fifth argument is the name of the PCX file to create (it may include a path
  59. name).  As with fg_showpcx, the file name must be terminated with a null
  60. character.  If an identically named file exists, it is overwritten.  The
  61. fg_makepcx routine returns a value of 0 if successful, and 1 if the PCX file
  62. was not created.
  63.  
  64.      Example 9-1 uses the fg_showpcx and fg_makepcx routines to create a new
  65. PCX file from selected rows of an existing 256-color 320 by 200 PCX file.  As
  66.                                                  Chapter 9:  Image Files   173
  67.  
  68. written, the program uses the file CORAL.PCX to create NEW.PCX, but it could
  69. easily be extended to work with any PCX files.  The call to fg_showpcx
  70. displays the image in CORAL.PCX using the screen position and palette
  71. settings defined in the PCX file.  After waiting for a keystroke, the program
  72. calls fg_makepcx to create a PCX file named NEW.PCX from pixel rows 80
  73. through 99 of the original image.  In case the program encounters any
  74. problems, it prints an error message before exiting.
  75.  
  76.                                  Example 9-1.
  77.  
  78.            #include <fastgraf.h>
  79.            #include <stdio.h>
  80.            #include <stdlib.h>
  81.            void main(void);
  82.  
  83.            void main()
  84.            {
  85.               int old_mode;
  86.               int read_status, write_status;
  87.  
  88.               if (fg_testmode(19,1) == 0) {
  89.                  printf("This program requires a 320 ");
  90.                  printf("x 200 MCGA graphics mode.\n");
  91.                  exit(1);
  92.                  }
  93.               old_mode = fg_getmode();
  94.               fg_setmode(19);
  95.  
  96.               read_status = fg_showpcx("CORAL.PCX",0);
  97.               fg_waitkey();
  98.               if (read_status == 0)
  99.                  write_status = fg_makepcx(0,319,80,99,"NEW.PCX");
  100.               else
  101.                  write_status = 1;
  102.  
  103.               fg_setmode(old_mode);
  104.               fg_reset();
  105.  
  106.               if (read_status == 1)
  107.                  printf("CORAL.PCX not found.\n");
  108.               else if (read_status == 2)
  109.                  printf("CORAL.PCX is not a PCX file.\n");
  110.               if (write_status == 1)
  111.                  printf("NEW.PCX not created.\n");
  112.            }
  113.  
  114.  
  115.      In the Tandy/PCjr 16-color graphics mode (mode 9) and the native EGA
  116. graphics modes (modes 13 through 16), the palette registers are not readable.
  117. Hence, fg_makepcx will use the default palette settings when used in these
  118. video modes on Tandy and EGA systems.  Displaying a PCX file at a lower
  119. resolution (for example, a 640x480 PCX file at 320x200) will truncate the
  120. display on the right and on the bottom.  This effectively displays the upper
  121. left corner of the image.  The fg_showpcx and fg_makepcx routines have no
  122. effect in text video modes or in the Hercules low-resolution graphics mode.
  123. 174   Fastgraph User's Guide
  124.      Because their structure parallels that of video memory, PCX files are
  125. specific to certain video modes.  If you try to display a PCX file in an
  126. incompatible video mode, fg_showpcx will still display something, but it will
  127. be garbled.  The following table summarizes the compatible video modes for
  128. PCX files.
  129.  
  130.           If PCX file was     You can display
  131.           created in mode     it in these modes
  132.  
  133.           4, 5                4, 5
  134.           6, 11               6, 11, 13-18, 28, 29
  135.           9                   9
  136.           13-18               13-18, 28, 29
  137.           19-27               19-27
  138.           28-29               13-18, 28, 29
  139.  
  140.      Fastgraph includes a function fg_pcxmode that determines the optimal
  141. video mode for displaying a PCX file.  By optimal, we mean the compatible
  142. video mode having the lowest resolution larger than or equal to the image
  143. dimensions.  The fg_pcxmode routine has a single argument -- the address of a
  144. buffer that contains a 128-byte PCX file header.  Specific values defined in
  145. certain fields of the PCX header determine which video mode is optimal.  A
  146. positive return value from fg_pcxmode represents the optimal video mode
  147. number.  The possible error returns are -1 if the buffer does not contain a
  148. valid PCX header, and -2 if fg_pcxmode cannot find any compatible video mode.
  149.  
  150.      How do we get the header from a PCX file into the buffer passed to
  151. fg_pcxmode?  The easiest way is with the fg_pcxhead function.  Its first
  152. argument is the name of the PCX file from which to retrieve the header (as
  153. before, the file name must be null-terminated).  The second argument is the
  154. address of the buffer that will receive the PCX header.  The size of this
  155. buffer must be at least 128 bytes.  If successful, fg_pcxmode returns zero.
  156. Otherwise, the return value is -1 if the specified file could not be opened,
  157. or -2 if the specified file is not a PCX file.  After successfully reading
  158. the PCX header, you can pass it to fg_pcxmode to determine the optimal video
  159. mode for the PCX file.  Example 9-2 demonstrates this process.
  160.  
  161.                                  Example 9-2.
  162.  
  163.                #include <fastgraf.h>
  164.                #include <stdio.h>
  165.                #include <stdlib.h>
  166.                void main(void);
  167.  
  168.                main()
  169.                {
  170.                   int mode, status;
  171.                   char header[128];
  172.  
  173.                   status = fg_pcxhead("CORAL.PCX",header);
  174.                   if (status == -1) {
  175.                      printf("Can't open CORAL.PCX.\n");
  176.                      exit(1);
  177.                   }
  178.                   else if (status == -2) {
  179.                      printf("CORAL.PCX is not a PCX file.\n");
  180.                      exit(1);
  181.                                                  Chapter 9:  Image Files   175
  182.  
  183.                   }
  184.  
  185.                   mode = fg_pcxmode(header);
  186.                   printf("Optimal display mode is %d.\n",mode);
  187.               }
  188.  
  189.  
  190. GIF Files
  191.  
  192.      The GIF file format was originally created by CompuServe, Inc., as a
  193. transmission format for images and graphics data across the CompuServe
  194. network.  It has evolved into what is probably the most popular image file
  195. format in use today.  GIF files are especially prevalent on bulletin boards
  196. and electronic data networks because their efficient image compression
  197. results in less storage space and faster transmission times than other image
  198. file formats.  GIF, pronounced "jif", is an acronym for Graphics Interchange
  199. Format.  The format is the copyright property of CompuServe, Inc., whose GIF
  200. specification "grants a limited, non-exclusive, royalty-free license for the
  201. use of the Graphics Interchange Format in computer software; computer
  202. software utilizing GIF must acknowledge ownership of the Graphics Interchange
  203. Format and its Service Mark by CompuServe, Inc., in user and technical
  204. documentation".
  205.  
  206.      Two GIF specifications, released in 1987 and 1989, are defined.  The
  207. 1989 specification (known as "89a") is a superset of the original 1987
  208. specification (known as "87a").  Fastgraph's GIF file display routine can
  209. handle either 87a or 89a files.  For maximum portability, the GIF file
  210. creation routine always produces files conforming to the 87a specification.
  211.  
  212.      The fg_showgif routine displays an image stored in a GIF file.  It can
  213. position the image using the coordinate information in the GIF header, or
  214. such that its upper left corner is at the graphics cursor position on the
  215. active video page.  The fg_showgif arguments are the same as for fg_showpcx,
  216. except the file name must of course reference a GIF file rather than a PCX
  217. file.  Likewise, the fg_showgif return values are analogous to those of
  218. fg_showpcx.
  219.  
  220.      The fg_makegif routine creates a GIF file from the specified rectangular
  221. region of the active video page.  Its arguments and return value are
  222. analogous to those of fg_makepcx.
  223.  
  224.      Example 9-3 uses the fg_showgif and fg_makegif routines to create a new
  225. GIF file from selected rows of an existing 256-color 320 by 200 GIF file.
  226. Similar to example 9-1, the program uses the file CORAL.GIF to create
  227. NEW.GIF, but it could easily be extended to work with any GIF files.  The
  228. call to fg_showgif displays the image in CORAL.GIF using the screen position
  229. and palette settings defined in the GIF file.  After waiting for a keystroke,
  230. the program calls fg_makegif to create a GIF file named NEW.GIF from pixel
  231. rows 80 through 99 of the original image.  In case the program encounters any
  232. problems, it prints an error message before exiting.
  233.  
  234.                                  Example 9-3.
  235.  
  236.            #include <fastgraf.h>
  237.            #include <stdio.h>
  238.            #include <stdlib.h>
  239. 176   Fastgraph User's Guide
  240.  
  241.            void main(void);
  242.  
  243.            void main()
  244.            {
  245.               int old_mode;
  246.               int read_status, write_status;
  247.  
  248.               if (fg_testmode(19,1) == 0) {
  249.                  printf("This program requires a 320 ");
  250.                  printf("x 200 MCGA graphics mode.\n");
  251.                  exit(1);
  252.                  }
  253.               old_mode = fg_getmode();
  254.               fg_setmode(19);
  255.  
  256.               read_status = fg_showgif("CORAL.GIF",0);
  257.               fg_waitkey();
  258.               if (read_status == 0)
  259.                  write_status = fg_makegif(0,319,80,99,"NEW.GIF");
  260.               else
  261.                  write_status = 1;
  262.  
  263.               fg_setmode(old_mode);
  264.               fg_reset();
  265.  
  266.               if (read_status == 1)
  267.                  printf("CORAL.GIF not found.\n");
  268.               else if (read_status == 2)
  269.                  printf("CORAL.GIF is not a GIF file.\n");
  270.               if (write_status == 1)
  271.                  printf("NEW.GIF not created.\n");
  272.            }
  273.  
  274.  
  275.      Like fg_makepcx, the fg_makegif routine will use the default palette
  276. settings when used on Tandy and EGA systems.  Displaying a GIF file at a
  277. lower resolution (for example, a 640x480 GIF file at 320x200) will truncate
  278. the display on the right and on the bottom.  This effectively displays the
  279. upper left corner of the image.  Unlike PCX files, GIF files do not exhibit
  280. compatibility problems between 16-color and 256-color graphics modes.  When
  281. fg_showgif displays a 256-color GIF in a 16-color mode, it displays pixels of
  282. color c in color c mod 16.  The fg_showgif and fg_makegif routines have no
  283. effect in text video modes, or in CGA and Hercules graphics modes.
  284.  
  285.  
  286. Pixel Run Files
  287.  
  288.      Fastgraph also provides its own mode-independent image file format
  289. called pixel run format.  Pixel run files are useful in programs that must
  290. run in different video modes (but with the same resolution) because you can
  291. use the same image files for two-color modes as for 256-color modes.  Two
  292. variations of the pixel run format exist -- standard pixel run files (SPR
  293. files) and packed pixel run files (PPR files).  The packed pixel run format
  294. does not support 256-color images but will produce a smaller file if the
  295. image has 16 colors or less.  Pixel run files do not include a header or any
  296. color palette information.
  297.                                                  Chapter 9:  Image Files   177
  298.  
  299.      The best way to illustrate the pixel run file format is with an example.
  300. Suppose we want to display a small triangle whose perimeter is a different
  301. color than its interior.  To create the standard pixel run equivalent of this
  302. image, we must inscribe the triangle in a rectangular area.  Hence, the pixel
  303. representation of our triangle might appear as shown below.
  304.  
  305.                               . . . . * . . . .
  306.                               . . . * x * . . .
  307.                               . . * x x x * . .
  308.                               . * x x x x x * .
  309.                               * * * * * * * * *
  310.  
  311.      As shown in this diagram, our triangle image is nine pixels wide at its
  312. base and five pixels high.  The pixels indicated by an asterisk (*) are the
  313. triangle's perimeter, while those indicated by an x represent its interior
  314. points.  The pixels shown as periods (.) are not part of the triangle itself,
  315. but they are part of the image.  In this example, we can treat them as
  316. background pixels.
  317.  
  318.      If we start at the lower left corner of the image and proceed to the
  319. right, we could represent the first row of the image as nine pixels of color
  320. "asterisk".  Such a group of consecutive identically colored pixels is called
  321. a pixel run, so a single pixel run describes the first row of the image.  The
  322. row above this one is a bit more complex.  It consists of five pixel runs:
  323. one pixel of color "period", followed by one of color "asterisk", then five
  324. of color "x", one of color "asterisk", and finally one of color "period".
  325.  
  326.      While we could construct separate pixel runs for each row of the image,
  327. notice that three of the five rows in our triangle begin with the same color
  328. pixel as the rightmost pixel in the previous row.  Fastgraph's pixel run
  329. formats let you take advantage of this property by allowing pixel runs to
  330. wrap from one row to the next.  This means we can represent the pixel run of
  331. color "period" extending from the right side of the second row to the left
  332. side of the third row as a single run of three pixels.
  333.  
  334.      An standard pixel run (SPR) file is nothing more than such a sequence of
  335. (color,count) pairs, as shown in the following diagram.
  336.  
  337.  
  338.                           byte 0   color for run 1
  339.  
  340.                                1   count for run 1
  341.  
  342.                                2   color for run 2
  343.  
  344.                                3   count for run 2
  345.                                           .
  346.                                           .
  347.                                           .
  348.                                           .
  349.                                           .
  350.                             2n-2   color for run n
  351.  
  352.                             2n-1   count for run n
  353.  
  354. 178   Fastgraph User's Guide
  355. Each color is a value between 0 and 255 specifying the color index for that
  356. pixel run.  Each count is a value between 0 and 255 specifying the length in
  357. pixels of that pixel run.  If a single run exceeds 255 pixels, it must be
  358. broken into two or more runs.  For example, we could represent a pixel run of
  359. length 265 as a run of length 255 followed by a run of length 10 of the same
  360. color.  Note that the space in bytes needed to store an SPR image is twice
  361. the number of runs.
  362.  
  363.      Fastgraph's fg_showspr routine displays an SPR file.  Its first argument
  364. is the name of the file containing the image (it may include a path name).
  365. The file name must be terminated with a null character, so BASIC, FORTRAN,
  366. and Pascal programmers will need to store a zero byte as the last character
  367. of the file name string.  The second argument is the width in pixels of the
  368. image.  The fg_showspr routine displays the image such that its lower left
  369. corner is at the graphics cursor position.  The possible return values for
  370. fg_showspr are success (0) and file not found (1).  To create an SPR file,
  371. use the fg_makespr routine.  Its arguments and return values are the same as
  372. for fg_makepcx and fg_makegif.
  373.  
  374.      Example 9-4 uses the fg_showspr and fg_makespr routines to create a new
  375. SPR file from selected rows of an existing 16-color 320 by 200 SPR file.
  376. Similar to examples 9-1 and 9-3, the program uses the file CORAL.SPR to
  377. create NEW.SPR, but it could easily be extended to work with any SPR files.
  378. The call to fg_move to establishes the lower left corner of the screen as the
  379. graphics cursor position (contrast this with the upper left corner being the
  380. reference point for PCX and GIF files).  Then program then calls fg_showspr
  381. to display the image.  After waiting for a keystroke, the program calls
  382. fg_makespr to create an SPR file named NEW.SPR from pixel rows 80 through 99
  383. of the original image.  In case the program encounters any problems, it
  384. prints an error message before exiting.
  385.  
  386.                                  Example 9-4.
  387.  
  388.            #include <fastgraf.h>
  389.            #include <stdio.h>
  390.            #include <stdlib.h>
  391.            void main(void);
  392.  
  393.            void main()
  394.            {
  395.               int new_mode, old_mode;
  396.               int read_status, write_status;
  397.  
  398.               new_mode = fg_bestmode(320,200,1);
  399.               if (new_mode < 0 || new_mode == 12) {
  400.                  printf("This program requires a 320 ");
  401.                  printf("x 200 color graphics mode.\n");
  402.                  exit(1);
  403.                  }
  404.               old_mode = fg_getmode();
  405.               fg_setmode(new_mode);
  406.               fg_move(0,199);
  407.  
  408.               read_status = fg_showspr("CORAL.SPR",320);
  409.               fg_waitkey();
  410.               if (read_status == 0)
  411.                  write_status = fg_makespr(0,319,80,99,"NEW.SPR");
  412.                                                  Chapter 9:  Image Files   179
  413.  
  414.               else
  415.                  write_status = 1;
  416.  
  417.               fg_setmode(old_mode);
  418.               fg_reset();
  419.  
  420.               if (read_status == 1)
  421.                  printf("CORAL.SPR not found.\n");
  422.               if (write_status == 1)
  423.                  printf("NEW.SPR not created.\n");
  424.            }
  425.  
  426.  
  427.      If you have an image that only uses the first 16 color indices (0 to
  428. 15), you can use Fastgraph's packed pixel run (PPR) image format.  This
  429. format packs two color values into each color byte, so it takes three bytes
  430. instead of four to represent two pixel runs.  This means a PPR file is 25%
  431. smaller than its SPR equivalent.  In each set of three bytes, the high four
  432. bits of the first byte contain the color of the first run, and the low four
  433. bits contain the color of the second run.  The second byte contains the
  434. length of the first run, and the third byte contains the length of the second
  435. run.
  436.  
  437.      The following diagram illustrates the structure of the packed pixel
  438. file.  In this example, the file is assumed to contain n pixel runs, where n
  439. is an even number.  If n is odd, the byte offset for the last element is 3n/2
  440. (truncated) instead of 3n/2-1, and the low four bits of the last color byte
  441. (that is, the color for pixel run n+1) are ignored.
  442.  
  443.                       7                4   3                0
  444.  
  445.                byte 0    color for run 1     color for run 2
  446.  
  447.                     1              count for run 1
  448.  
  449.                     2              count for run 2
  450.  
  451.                     3    color for run 3     color for run 4
  452.  
  453.                     4              count for run 3
  454.  
  455.                     5              count for run 4
  456.                                        .
  457.                                        .
  458.                                        .
  459.                                        .
  460.                                        .
  461.                3n/2-3   color for run n-1    color for run n
  462.  
  463.                3n/2-2             count for run n-1
  464.  
  465.                3n/2-1              count for run n
  466.  
  467.  
  468.      The structure of the PPR file allows for color values between 0 and 15,
  469. and run lengths between 0 and 255.  The space in bytes needed to store an
  470. 180   Fastgraph User's Guide
  471.  
  472. image in PPR format is 1.5 times the number of runs, compared to twice the
  473. number of runs for the SPR format.
  474.  
  475.      The fg_showppr and fg_makeppr routines display and create PPR files,
  476. respectively.  Their arguments and return values are the same as those of
  477. fg_showspr and fg_makespr.  If we wanted to display PPR files instead of SPR
  478. files in example 9-4, all that's necessary is changing the fg_showspr and
  479. fg_makespr calls to fg_showppr and fg_makeppr.
  480.  
  481.      Fastgraph's fg_dispfile routine displays both SPR and PPR files.  The
  482. first of its three arguments is the name of the image file (it may include a
  483. path name).  The file name must be terminated with a null character, so
  484. BASIC, FORTRAN, and Pascal programmers will need to store a zero byte as the
  485. last character of the file name string.  The second argument is the image
  486. width in pixels, and the third argument defines the image format (that is,
  487. SPR or PPR).  As with other pixel run display routines, fg_dispfile displays
  488. the image such that its lower left corner is at the graphics cursor position.
  489.  
  490.      Example 9-5 illustrates how to use the fg_dispfile routine to display an
  491. image stored in a pixel run file.  The program displays two identical images,
  492. one in an SPR file and the other in a PPR file.  Each image is a picture of
  493. the sea floor and some coral, as might be used for the background in an
  494. aquarium.  The program runs in a 320 by 200 graphics mode, and the images
  495. fill the entire screen.
  496.  
  497.      The SPR image is in file CORAL.SPR.  The program uses the fg_move
  498. routine to establish the lower left corner of the screen as the graphics
  499. cursor position and then calls fg_dispfile to display the image.  The value
  500. of fg_dispfile's third argument tells Fastgraph the image format.  A value of
  501. 0 indicates the file contains an image in SPR format, while a value of 1
  502. indicates an image in PPR format.  As mentioned earlier, the image fills the
  503. entire screen, so its width is 320 pixels.
  504.  
  505.      After waiting for a keystroke, the program clears the previous image
  506. from the screen and then calls fg_dispfile to display the PPR image from the
  507. file CORAL.PPR.  The program leaves the second image on the screen until
  508. another keypress, at which time it restores the original video mode and
  509. screen attributes and returns to DOS.
  510.  
  511.                                  Example 9-5.
  512.  
  513.                 #include <fastgraf.h>
  514.                 #include <stdio.h>
  515.                 #include <stdlib.h>
  516.                 void main(void);
  517.  
  518.                 void main()
  519.                 {
  520.                    int old_mode, new_mode;
  521.  
  522.                    new_mode = fg_bestmode(320,200,1);
  523.                    if (new_mode < 0 || new_mode == 12) {
  524.                       printf("This program requires a 320 ");
  525.                       printf("x 200 color graphics mode.\n");
  526.                       exit(1);
  527.                       }
  528.                                                  Chapter 9:  Image Files   181
  529.  
  530.                    old_mode = fg_getmode();
  531.                    fg_setmode(new_mode);
  532.  
  533.                    fg_move(0,199);
  534.                    fg_dispfile("CORAL.SPR",320,0);
  535.                    fg_waitkey();
  536.  
  537.                    fg_erase();
  538.                    fg_dispfile("CORAL.PPR",320,1);
  539.                    fg_waitkey();
  540.  
  541.                    fg_setmode(old_mode);
  542.                    fg_reset();
  543.                 }
  544.  
  545.  
  546.      The SNAPSHOT utility distributed with Fastgraph is a terminate and stay
  547. resident program (TSR) that can capture graphics mode screen images and save
  548. them in SPR files.  Thus, you can easily create files with SNAPSHOT and
  549. display them with the fg_showspr or fg_dispfile routines.  Another TSR
  550. utility, GrabRGB, is useful for capturing RGB color values from 256-color
  551. images.  Appendix A contains complete descriptions of the SNAPSHOT and
  552. GrabRGB utilities.
  553.  
  554.  
  555. Display Patterns
  556.  
  557.      Example 9-5 works well in the graphics video modes with 16 or 256
  558. available colors.  However, in the four-color CGA graphics modes the
  559. resulting image is not too good because of our limited color choices, and it
  560. would look even worse in the Hercules graphics mode.  The Fastgraph routine
  561. fg_pattern allows you to associate a dither pattern (actually, any pixel
  562. sequence) with one of Fastgraph's 256 color indices appearing in a pixel run
  563. map.  When displaying an SPR or PPR file, Fastgraph will use the pattern
  564. associated with that color index instead of displaying the color itself.
  565.  
  566.      The fg_pattern routine requires two integer arguments -- a color index
  567. (between 0 and 255) and the display pattern defined for that color index.  A
  568. display pattern's structure resembles the structure of video memory and is
  569. thus dependent on the current video mode.  The following sections list the
  570. initial display patterns and explain how to construct new display patterns
  571. for different graphics video modes.
  572.  
  573. CGA four-color graphics modes
  574.  
  575.      In the four-color CGA graphics modes (modes 4 and 5), the display
  576. pattern is a 16-bit quantity consisting of an 8-bit shift count followed by
  577. an 8-bit pixel pattern.  Each pixel assumes a value between 0 and 3, so the
  578. pattern represents four pixels.  In even-numbered pixel rows, Fastgraph uses
  579. the pixel pattern itself.  In odd-numbered pixel rows, Fastgraph rotates the
  580. original pattern to the left by the number of bits specified by the shift
  581. count.
  582.  
  583.      For example, if we are using the default CGA color palette, we could
  584. create a darker shade of cyan by alternating cyan pixels (color 1, 01 binary)
  585. with white pixels (color 3, 11 binary), as shown below.
  586. 182   Fastgraph User's Guide
  587.  
  588.  
  589.                                  01 11 01 11
  590.  
  591.  
  592. If we convert this pixel pattern to its hexadecimal equivalent, we get the
  593. value 77.
  594.  
  595.      To complete the display pattern, we need to determine the shift count.
  596. If we use a shift count of zero, the resulting display will simply be a
  597. series of cyan and white vertical lines.  What we really need is a
  598. checkerboard effect where a white pixel is above and below each cyan pixel,
  599. and vice versa.  If we rotate the pattern one pixel (two bits) to the left,
  600. we will achieve the desired effect.  That is, a shift count of two produces
  601. the following pixel patterns:
  602.  
  603.                        even-numbered rows   01 11 01 11
  604.                         odd-numbered rows   11 01 11 01
  605.  
  606. Combining the shift count with the pixel pattern yields the display pattern
  607. 0277 hex.  The shift count is normally a multiple of two; note that a zero
  608. shift count results in the same pattern being applied to all pixel rows.
  609.  
  610.      For the CGA four-color graphics modes, the fg_setmode routine
  611. establishes the following initial display patterns:
  612.  
  613.                   color  shift count    hexadecimal
  614.                   index  and pattern    equivalent
  615.  
  616.                     0    0 00000000        0000
  617.                     1    0 01010101        0055
  618.                     2    0 10101010        00AA
  619.                     3    0 11111111        00FF
  620.  
  621. These values are repeated as necessary to define color indices 4 to 255.
  622. That is, colors 4, 8, 12, ... , 252 use the same defaults as color 0.  Colors
  623. 5, 9, 13, ... , 253 use the same defaults as color 1, and so forth.  Also
  624. note that pattern 0000 represents four pixels of color 0, 0055 represents
  625. four pixels of color 1, 00AA represents four pixels of color 2, and 00FF
  626. represents four pixels of color 3.
  627.  
  628. CGA two-color graphics mode
  629.  
  630.      In the two-color CGA graphics mode (mode 6), the display pattern is also
  631. a 16-bit quantity consisting of an 8-bit shift count followed by an 8-bit
  632. pixel pattern.  Each pixel assumes the value 0 or 1, so the pattern
  633. represents eight pixels.  In even-numbered pixel rows, Fastgraph uses the
  634. pixel pattern itself.  In odd-numbered pixel rows, Fastgraph rotates the
  635. original pattern to the left by the number of bits specified by the shift
  636. count.
  637.  
  638.      For example, we could create a lighter shade of white by alternating
  639. black pixels (color 0) with white pixels (color 1), as shown below.
  640.  
  641.  
  642.                                0 1 0 1 0 1 0 1
  643.  
  644.                                                  Chapter 9:  Image Files   183
  645.  
  646. If we convert this pixel pattern to its hexadecimal equivalent, we get the
  647. value 55.
  648.  
  649.      To complete the display pattern, we need to determine the shift count.
  650. We must rotate the pattern one pixel (one bit) to the left to achieve the
  651. checkerboard effect as in the CGA four color graphics modes.  That is, a
  652. shift count of one produces the following pixel patterns:
  653.  
  654.                      even-numbered rows   0 1 0 1 0 1 0 1
  655.                       odd-numbered rows   1 0 1 0 1 0 1 0
  656.  
  657. Combining the shift count with the pixel pattern yields the display pattern
  658. 0155 hex.
  659.  
  660.      For the CGA two-color graphics mode, the fg_setmode routine establishes
  661. the initial display patterns such that all even-numbered color indices are
  662. assigned the value 0000, while all odd-numbered color indices are assigned
  663. the value 00FF.  Note that pattern 0000 represents eight pixels of color 0,
  664. and 00FF represents eight pixels of color 1.
  665.  
  666. Tandy/PCjr 16-color graphics mode
  667.  
  668.      In the Tandy/PCjr 16-color graphics mode (mode 9), the display pattern
  669. is also 16-bit quantity consisting of an 8-bit shift count followed by an 8-
  670. bit pixel pattern.  Each pixel assumes a value between 0 and 15, so the
  671. pattern represents two pixels.  In even-numbered pixel rows, Fastgraph uses
  672. the pixel pattern itself.  In odd-numbered pixel rows, Fastgraph rotates the
  673. original pattern to the left by the number of bits specified by the shift
  674. count.
  675.  
  676.      For example, we could create a lighter shade of blue by alternating blue
  677. pixels (color 1, 0001 binary) with white pixels (color 15, 1111 binary), as
  678. shown below.
  679.  
  680.                                   0001 1111
  681.  
  682. If we convert this pixel pattern to its hexadecimal equivalent, we get the
  683. value 1F.
  684.  
  685.      To complete the display pattern, we need to determine the shift count.
  686. Using the same process as in the CGA graphics modes, we must rotate the
  687. pattern one pixel (four bits) to the left to achieve the checkerboard effect.
  688. That is, a shift count of four produces the following pixel patterns:
  689.  
  690.                         even-numbered rows   0001 1111
  691.                          odd-numbered rows   1111 0001
  692.  
  693. Combining the shift count with the pixel pattern yields the display pattern
  694. 041F hex.  The shift count is normally zero or four; note that a zero shift
  695. count results in the same pattern being applied to all pixel rows.
  696.  
  697.      For the Tandy/PCjr 16-color graphics modes, the fg_setmode routine
  698. establishes the initial display patterns such that color 0 is assigned the
  699. value 0000 (two pixels of color 0), color 1 is assigned the value 0011 (two
  700. pixels of color 1), color 2 is assigned the value 0022 (two pixels of color
  701. 184   Fastgraph User's Guide
  702.  
  703. 2), and so forth.  These values are repeated as necessary to define color
  704. indices 16 to 255.  That is, colors 0, 16, 32, ... , 240 use the same
  705. defaults as color 0.  Colors 1, 17, 33, ... , 241 use the same defaults as
  706. color 1, and so forth.
  707.  
  708. Hercules graphics modes
  709.  
  710.      The structure of the display patterns for the Hercules graphics modes
  711. (modes 11 and 12) is the same as two of the CGA graphics modes.  For the
  712. standard Hercules graphics mode (mode 11), please refer to the discussion of
  713. CGA two-color (mode 6) display patterns.  For the low-resolution Hercules
  714. graphics mode (mode 12), please refer to the discussion of the CGA four-color
  715. (mode 4) display patterns.
  716.  
  717. EGA/VGA/SVGA 16-color graphics modes
  718.  
  719.      In the EGA/VGA/SVGA 16-color graphics modes (modes 13 to 16, 18, 28, and
  720. 29), the display pattern is an 8-bit quantity consisting of two 4-bit color
  721. values (for consistency with the other video modes, we still pass the display
  722. pattern as a 16-bit quantity).  Each pixel assumes a value between 0 and 15
  723. (0 and 5 in the EGA monochrome graphics mode), so the pattern represents two
  724. pixels.  In even-numbered pixel rows, Fastgraph uses the pixel pattern
  725. itself.  In odd-numbered pixel rows, Fastgraph rotates the original pattern
  726. one pixel (four bits) to the left.
  727.  
  728.      For example, we could create a lighter shade of blue by alternating blue
  729. pixels (color 1, 0001 binary) with white pixels (color 15, 1111 binary), as
  730. shown below.
  731.  
  732.                                   0001 1111
  733.  
  734. If we convert this pixel pattern to its hexadecimal equivalent, we get the
  735. value 1F.  The implied four-bit shift count produces the following pixel
  736. patterns:
  737.  
  738.                         even-numbered rows   0001 1111
  739.                          odd-numbered rows   1111 0001
  740.  
  741. Extending the pixel pattern to a 16-bit quantity yields the display pattern
  742. 001F hex.
  743.  
  744.      For the EGA/VGA/SVGA 16-color graphics modes, the fg_setmode routine
  745. establishes the initial display patterns such that color 0 is assigned the
  746. value 0000 (two pixels of color 0), color 1 is assigned the value 0011 (two
  747. pixels of color 1), color 2 is assigned the value 0022 (two pixels of color
  748. 2), and so forth.  These values are repeated as necessary to define color
  749. indices 16 to 255.  That is, colors 0, 16, 32, ... , 240 use the same
  750. defaults as color 0.  Colors 1, 17, 33, ... , 241 use the same defaults as
  751. color 1, and so forth.
  752.  
  753. MCGA/VGA 2-color graphics mode
  754.  
  755.      In the two-color MCGA/VGA graphics mode (mode 17), the display pattern
  756. is a 2-bit quantity consisting of two 1-bit color values (for consistency
  757. with the other video modes, we still pass the display pattern as a 16-bit
  758.                                                  Chapter 9:  Image Files   185
  759.  
  760. quantity).  Each pixel assumes the value 0 or 1, so the pattern represents
  761. two pixels.  In even-numbered pixel rows, Fastgraph uses the pixel pattern
  762. itself.  In odd-numbered pixel rows, Fastgraph rotates the original pattern
  763. one pixel (one bit) to the left.
  764.  
  765.      For example, we could create a lighter shade of white by alternating
  766. black pixels (color 0) with white pixels (color 1), as shown below.
  767.  
  768.                                      0 1
  769.  
  770. If we convert this pixel pattern to its hexadecimal equivalent, we get the
  771. value 01.  The implied one-bit shift count produces the following pixel
  772. patterns:
  773.  
  774.                            even-numbered rows   0 1
  775.                             odd-numbered rows   1 0
  776.  
  777. Extending the pixel pattern to a 16-bit quantity yields the display pattern
  778. 0001 hex.
  779.  
  780.      For the MCGA/VGA two-color graphics mode, the fg_setmode routine
  781. establishes the initial display patterns such that all even-numbered color
  782. indices are assigned the value 0000 (two pixels of color 0), while all odd-
  783. numbered color indices are assigned the value 0003 (11 binary, or two pixels
  784. of color 1).
  785.  
  786. 256-color graphics modes
  787.  
  788.      The 256-color graphics modes (modes 19 through 27) offer 262,144
  789. different colors, so dithering is seldom (if ever) required.  For this
  790. reason, the fg_pattern routine has no effect in these video modes.
  791.  
  792. An example
  793.  
  794.      Example 9-6 illustrates the use of display patterns in several graphics
  795. modes.  This program runs in any 320 by 200 color graphics mode and displays
  796. the CORAL.PPR image with one or more of the color indices redefined.  If the
  797. program runs in the standard CGA four-color mode (mode 4), it redefines the
  798. first 16 display patterns using the fg_pattern routine and the values in the
  799. CGApatterns array.  In the Tandy/PCjr 16-color graphics mode (mode 9) and the
  800. EGA low-resolution graphics mode (mode 13), the program redefines color index
  801. 15 to produce an alternating gray and white dither pattern.  In the MCGA 256-
  802. color mode (mode 19), display patterns are not available, so the program uses
  803. fg_setrgb to define color index 15 as slightly darker shade of gray than the
  804. default for color 7.
  805.  
  806.                                  Example 9-6.
  807.  
  808.                 #include <fastgraf.h>
  809.                 #include <stdio.h>
  810.                 #include <stdlib.h>
  811.                 void main(void);
  812.  
  813.                 int CGApatterns[] = {
  814.                    0x0000,0x00FF,0x00FF,0x00FF,
  815.  
  816. 186   Fastgraph User's Guide
  817.  
  818.                    0x02BB,0x0000,0x0222,0x0255,
  819.                    0x00FF,0x00FF,0x00FF,0x0055,
  820.                    0x00AA,0x00AA,0x00FF,0x0277
  821.                    };
  822.  
  823.                 void main()
  824.                 {
  825.                    int color;
  826.                    int old_mode, new_mode;
  827.  
  828.                    new_mode = fg_bestmode(320,200,1);
  829.                    if (new_mode < 0 || new_mode == 12) {
  830.                       printf("This program requires a 320 ");
  831.                       printf("x 200 color graphics mode.\n");
  832.                       exit(1);
  833.                       }
  834.  
  835.                    old_mode = fg_getmode();
  836.                    fg_setmode(new_mode);
  837.  
  838.                    if (new_mode == 4) {
  839.                       fg_palette(0,0);
  840.                       for (color = 0; color < 16; color++)
  841.                          fg_pattern(color,CGApatterns[color]);
  842.                       }
  843.                    else if (new_mode == 9 || new_mode == 13)
  844.                       fg_pattern(15,0x04F7);
  845.                    else
  846.                       fg_setrgb(15,38,38,38);
  847.  
  848.                    fg_move(0,199);
  849.                    fg_showppr("CORAL.PPR",320);
  850.                    fg_waitkey();
  851.  
  852.                    fg_setmode(old_mode);
  853.                    fg_reset();
  854.                 }
  855.  
  856.  
  857. Controlling the Image Buffer Size
  858.  
  859.      By default, all of Fastgraph's image file display and creation routines
  860. use an internal 4,096-byte buffer.  This buffer provides an intermediate
  861. storage area, making it possible to perform more efficient buffered I/O when
  862. reading or writing the image files.  The fg_imagebuf routine lets you define
  863. your own buffer (up to 64K bytes) for this purpose.  Larger buffers generally
  864. make image display and creation faster.
  865.  
  866.      The fg_imagebuf routine does not allocate storage for the internal
  867. buffer.  Rather, it just defines the array or dynamically allocated memory
  868. block to be used as the buffer.  The first argument passed to fg_imagebuf is
  869. the far address of this array or memory block.  Using a far address doesn't
  870. waste valuable space in the default data segment, and almost always results
  871. in the ability to use a larger buffer.  In Pascal programs, the space for the
  872. internal buffer must be allocated dynamically with the GetMem procedure
  873. because it provides the only way to pass something by far reference in
  874.                                                  Chapter 9:  Image Files   187
  875.  
  876. Pascal.  The second fg_imagebuf argument is the size of the internal buffer,
  877. represented as an unsigned integer.  QuickBASIC programs must pass the image
  878. buffer as a fixed-length string because that language does not support far
  879. pointers.
  880.  
  881.      Example 9-7 shows how to use fg_imagebuf to define a larger buffer when
  882. displaying the CORAL.PCX file.  In this example, we'll use a 20,000-byte
  883. static array as the image buffer.  This size was chosen because it's larger
  884. than the PCX file size, so fg_showpcx can read the entire PCX file in one
  885. pass.
  886.  
  887.                                  Example 9-7.
  888.  
  889.                 #include <fastgraf.h>
  890.                 #include <stdio.h>
  891.                 #include <stdlib.h>
  892.                 void main(void);
  893.  
  894.                 char far buffer[20000];
  895.  
  896.                 void main()
  897.                 {
  898.                    int old_mode;
  899.  
  900.                    if (fg_testmode(19,1) == 0) {
  901.                       printf("This program requires a 320 ");
  902.                       printf("x 200 MCGA graphics mode.\n");
  903.                       exit(1);
  904.                       }
  905.                    old_mode = fg_getmode();
  906.                    fg_setmode(19);
  907.                    fg_imagebuf(buffer,20000);
  908.                    fg_showpcx("CORAL.PCX",0);
  909.                    fg_waitkey();
  910.  
  911.                    fg_setmode(old_mode);
  912.                    fg_reset();
  913.                 }
  914.  
  915.  
  916.      You don't need to create separate buffers for each image you display or
  917. create.  Once you define an internal image buffer with fg_imagebuf, Fastgraph
  918. will use that buffer until your program exits, or until you call fg_imagebuf
  919. with a buffer size equal to zero.
  920.  
  921.  
  922. Summary of Image File Routines
  923.  
  924.      This section summarizes the functional descriptions of the Fastgraph
  925. routines presented in this chapter.  More detailed information about these
  926. routines, including their arguments and return values, may be found in the
  927. Fastgraph Reference Manual.  The image display and creation functions are
  928. applicable only to graphics video modes.
  929.  
  930.      FG_IMAGEBUF specifies the size and address of the buffer used internally
  931. when creating or displaying GIF, PCX, PPR, or SPR files.  Fastgraph's default
  932. 188   Fastgraph User's Guide
  933.  
  934. internal buffer size is 4,096 bytes.  Image display or creation is typically
  935. faster when a larger buffer is used.
  936.  
  937.      FG_DISPFILE displays an image stored in a standard or packed pixel run
  938. file.  The image is positioned so that its lower left corner is at the
  939. current graphics position.
  940.  
  941.      FG_MAKEGIF creates a GIF file from the specified rectangular region of
  942. the active video page.  The region's extremes are expressed in screen space
  943. units.  This routine is meaningful only in 16-color and 256-color graphics
  944. modes.
  945.  
  946.      FG_MAKEPCX creates a PCX file from the specified rectangular region of
  947. the active video page.  The region's extremes are expressed in screen space
  948. units.
  949.  
  950.      FG_MAKEPPR creates a packed pixel run file from the specified
  951. rectangular region of the active video page.  The region's extremes are
  952. expressed in screen space units.
  953.  
  954.      FG_MAKESPR creates a standard pixel run file from the specified
  955. rectangular region of the active video page.  The region's extremes are
  956. expressed in screen space units.
  957.  
  958.      FG_PATTERN defines a display pattern for use when displaying pixel run
  959. files in video modes that offer 16 or less colors.
  960.  
  961.      FG_PCXHEAD reads a PCX file header into a 128-byte buffer.
  962.  
  963.      FG_PCXMODE determines the optimal video mode for displaying a PCX file.
  964. The optimal mode is the compatible video mode having the lowest resolution
  965. larger than or equal to the image dimensions.
  966.  
  967.      FG_SHOWGIF displays an image stored in a GIF file.  By default, the
  968. image will be positioned so its upper left corner is at the graphics cursor
  969. position of the active video page.  This routine is meaningful only in 16-
  970. color and 256-color graphics modes.
  971.  
  972.      FG_SHOWPCX displays an image stored in a PCX file.  By default, the
  973. image will be positioned so its upper left corner is at the graphics cursor
  974. position of the active video page.
  975.  
  976.      FG_SHOWPPR displays an image stored in a packed pixel run file.  The
  977. image will be positioned so its lower left corner is at the graphics cursor
  978. position of the active video page.
  979.  
  980.      FG_SHOWSPR displays an image stored in a standard pixel run file.  The
  981. image will be positioned so its lower left corner is at the graphics cursor
  982. position of the active video page.