home *** CD-ROM | disk | FTP | other *** search
/ Falcon 030 Power 2 / F030_POWER2.iso / ST_STE / MAGS / ICTARI05.ARJ / ictari.05 / C / GEM_TUT / GEM_006.TXT < prev    next >
Text File  |  1993-05-12  |  21KB  |  434 lines

  1.                         *PROFESSIONAL GEM*
  2.                            by Tim Oren
  3.                   Column #6 - Raster operations
  4.  
  5.                         SEASONS GREETINGS!
  6.  
  7.      This is the Yuletide installment of ST PRO GEM, devoted to
  8. explaining the raster, or "bit-blit" portion of the Atari ST's VDI
  9. functions.
  10.  
  11.      Please note that this is NOT an attempt to show how to write
  12. directly to the video memory, although you will be able to deduce a
  13. great deal from the discussion.
  14.  
  15.      As usual, there is a download with this column.  You will find
  16. it in ATARI16 (PCS-58) in DL3 under the name of GEMCL6.C.
  17.  
  18.                           DEFINING TERMS
  19.  
  20.      To understand VDI raster operations, you need  to understand the
  21. jargon used to describe them.  (Many programmers will be tempted to
  22. skip this section and go directly to the code.  Please don't do it
  23. this time:  Learning the jargon is the larger half of understanding
  24. the raster operations!)
  25.  
  26.      In VDI terms a raster area is simply a chunk of contiguous words
  27. of memory, defining a bit image.  This chunk is called a "form".  A
  28. form  may reside in the ST's video map area or it may be in the data
  29. area of  your application.  Forms are roughly analogous to "blits" or
  30. "sprites"  on other systems.  (Note, however, that there is no sprite
  31. hardware on the ST.)
  32.  
  33.      Unlike other systems, there is NO predefined organization of the
  34. raster form.  Instead, you determine the internal layout of the form
  35. with an auxiliary data structure called the MFDB, or Memory Form
  36. Definition Block.  Before going into the details of the MFDB, we need
  37. to look at the various format options.  Their distinguishing features
  38. are monochrome vs. color, standard vs. device-specific and even-word
  39. vs. fringed.
  40.  
  41.                        MONOCHROME VS. COLOR
  42.  
  43.      Although these terms are standard, it might be better to say
  44. "single-color vs. multi-color".  What we are actually defining is the
  45. number of bits which correspond to each dot, or pixel, on the screen.
  46. In the ST, there are three possible answers. The high-resolution mode
  47. has one bit per pixel, because there is only one "color": white.
  48.  
  49.      In the medium resolution color mode, there are four possible
  50. colors for each pixel.  Therefore, it takes two bits to represent
  51. each dot on the screen.  (The actual colors which appear are
  52. determined by  the settings of the ST's pallette registers.)
  53.  
  54.      In the low resolution color mode, sixteen colors are generated,
  55. requiring four bits per pixel.  Notice that as the number of bits per
  56. pixel has been doubled for each mode, so the number of pixels on the
  57. screen has been halved: 640 by 400 for monochrome, 640 by 200 for
  58. medium-res, and 320 by 200 by low-res.  In this way the ST always
  59. uses  the same amount of video RAM: 32K.
  60.  
  61.      Now we have determined how many bits are needed for each pixel,
  62. but not how they are laid out within the form.  To find this out, we
  63. have  to see whether the form is device-dependent or not.
  64.  
  65.                STANDARD VS. DEVICE-SPECIFIC FORMAT
  66.  
  67.      The standard raster form format is a constant layout which is
  68. the same for all GEM systems.  A device-specific form is one which is
  69. stored in the internal format of a particular GEM system.  Just as
  70. the ST has three different screen modes, so it has three different
  71. device-specific form formats.  We will look at standard form first,
  72. then the ST-specific forms.
  73.  
  74.      First, it's reasonable to ask why a standard format is used. Its
  75. main function is to establish a portability method between various
  76. GEM systems.  For instance, an icon created in standard format on an
  77. IBM PC GEM setup can be moved to the ST, or a GEM Paint picture from
  78. an AT&T 6300 could be loaded into the ST version of Paint.
  79.  
  80.      The standard format has some uses even if you only work with the
  81. ST, because it gives a method of moving your application's icons and
  82. images amongst the three different screen modes.  To be sure,  there
  83. are limits to this.  Since there are different numbers of pixels  in
  84. the different modes, an icon built in the high-resolution mode will
  85. appear twice as large in low-res mode, and would appear oblong in
  86. medium-res.  (You can see this effect in the ST Desktop's icons.)
  87. Also,  colors defined in the lower resolutions will be useless in
  88. monochrome.
  89.  
  90.      The standard monochrome format uses a one-bit to represent
  91. black, and uses a zero for white.  It is assumed that the form begins
  92. at the  upper left of the raster area, and is written a word at a
  93. time left to right on each row, with the rows being output top to
  94. bottom.  Within each word, the most significant bit is the left-most
  95. on the screen.
  96.  
  97.      The standard color form uses a storage method called "color
  98. planes". The high-order bits for all of the pixels are stored just as
  99. for monochrome, followed by the next-lowest bit in another contiguous
  100. block, and so on until all of the necessary color bits have been
  101. stored.
  102.  
  103.      For example, on a 16-color system, there would be four different
  104. planes.  The color of the upper-leftmost bit in the form would be
  105. determined by concatenating the high-order bit in the first word of
  106. each plane of the form.
  107.  
  108.      The system dependent form for the ST's monochrome mode is very
  109. simple: it is identical to the standard form!  This occurs because
  110. the  ST uses a "reverse-video" setup in monochrome mode, with the
  111. background  set to white.
  112.  
  113.      The video organization of the ST's color modes is more
  114. complicated. It uses an "interleaved plane" system to store the bits
  115. which make up a pixel.  In the low-resolution mode, every four words
  116. define the values of 16 pixels.  The high-order bits of the four
  117. words are merged to form the left-most pixel, followed by the next
  118. lower bit of each word, and so on.  This method is called
  119. interleaving because the usually separate color planes described
  120. above have been shuffled together in memory.
  121.  
  122.      The organization of the ST's medium-resolution mode is similar
  123. to low-res, except the only two words are taken at a time.  These are
  124. merged to create the two bits needed to address four colors.
  125.  
  126.      You should note that the actual color produced by a particular
  127. pixel value is NOT fixed.  The ST uses a color remapping system
  128. called a palette.  The pixel value in memory is used to address a
  129. hardware  register in the palette which contains the actual RGB
  130. levels to be sent to the display.  Programs may set the palette
  131. registers with  BIOS calls, or the user may alter its settings with
  132. the Control Panel desk accessory.  Generally, palette zero
  133. (background) is left as white, and the highest numbered palette is
  134. black.
  135.  
  136.                       EVEN-WORD VS. FRINGES
  137.  
  138.      A form always begins on a word boundary, and is always stored
  139. with an integral number of words per row.  However,  it is possible
  140. to use only a portion of the final word.  This partial word is called
  141. a "fringe".  If, for instance, you had a form 40 pixels wide,  it
  142. would be stored with four words per row: three whole words, and one
  143. word with the eight pixel fringe in its upper byte.
  144.  
  145.                               MFDBs
  146.  
  147.      Now we can intelligently define the elements of the MFDB.  Its
  148. exact C structure definition will be found in the download. The
  149. fd_nplanes entry determines the color scheme: a value of one is
  150. monochrome, more than one denotes a color form.  If fd_stand is zero,
  151. then the form is device-specific, otherwise it is in standard format.
  152.  
  153.      The fd_w and fd_h fields contain the pixel width and height of
  154. the form respectively.  Fd_wdwidth is the width of a row in words.
  155. If fd_w is not exactly equal to sixteen times fd_wdwidth, then the
  156. form has a fringe.
  157.  
  158.      Finally, fd_addr is the 32-bit memory address of the form
  159. itself. Zero is a special value for fd_addr.  It denotes that this
  160. MFDB is for the video memory itself.  In this case, the VDI
  161. substitutes the actual address of the screen, and it ignores ALL of
  162. the other parameters. They are replaced with the size of the whole
  163. screen and number of planes in the current mode, and the form is (of
  164. course) in device-specific format.
  165.  
  166.      This implies that any MFDB which points at the screen can only
  167. address the entire screen.  This is not a problem, however, since the
  168. the VDI raster calls allow you to select a rectangular region within
  169. the form.  (A note to advanced programmers:  If this situation is
  170. annoying, you can retrieve the address of the ST's video area from
  171. low memory, add an appropriate offset, and substitute it into the
  172. MFDB yourself to address a portion of the screen.)
  173.  
  174.                           LET'S OPERATE
  175.  
  176.      Now we can look at the VDI raster operations themselves.  There
  177. are actually three: transform form, copy raster opaque, and copy
  178. raster transparent.  Both copy raster functions can perform a variety
  179. of logic operatoins during the copy.
  180.  
  181.                           TRANSFORM FORM
  182.  
  183.      The purpose of this operation is to change the format of a form:
  184. from standard to device-specific, or vice-versa.  The calling
  185. sequence is:
  186.  
  187.      vr_trnfm(vdi_handle, source, dest);
  188.  
  189. where source and dest are each pointers to MFDBs.  They ARE allowed
  190. to be the same.  Transform form checks the fd_stand flag in the
  191. source MFDB, toggles it and writes it into the destination MFDB after
  192. rewriting the form itself.  Note that transform form CANNOT change
  193. the number of color planes in a form: fd_nplanes must be identical in
  194. the two MFDBs.
  195.  
  196.      If you are writing an application to run on the ST only, you
  197. will probably be able to avoid transform form entirely.  Images and
  198. icons are stored within resources as standard forms, but since they
  199. are monochrome, they will work "as is" with the ST.
  200.  
  201.      If you may want to move your program or picture files to another
  202. GEM system, then you will need transform form.  Screen images can be
  203. transformed to standard format and stored to disk.  Another system
  204. with the same number of color planes could the read the files, and
  205. transform the image to ITS internal format with transform form.
  206.  
  207.      A GEM application which will be moved to other systems needs to
  208. contain code to transform the images and icons within its resource,
  209. since standard and device-specific formats will not always coincide.
  210.  
  211.      If you are in this situation, you will find several utilities in
  212. the download which you can use to transform G_ICON and G_IMAGE
  213. objects. There is also a routine which may be used with map_tree()
  214. from the  last column in order to transform all of the images and
  215. icons in a resource tree at once.
  216.  
  217.                         COPY RASTER OPAQUE
  218.  
  219.      This operation copies all or part of the source form into the
  220. destination form.  Both the source and destination forms must be in
  221. device-specific form.  Copy raster opaque is for moving information
  222. between "like" forms, that is, it can copy from monochrome to
  223. monochrome, or between color forms with the same number of planes.
  224. The calling format is:
  225.  
  226.      vro_cpyfm(vdi_handle, mode, pxy, source, dest);
  227.  
  228.   As above, the source and dest parameters are pointers to MFDBs
  229. (which in turn point to the actual forms).  The two MFDBs may point
  230. to memory areas  which overlap.  In this case, the VDI will perform
  231. the move in a  non-destructive order.  Mode determines how the pixel
  232. values in the source and destination areas will be combined.  I will
  233. discuss it separately later on.
  234.  
  235.      The pxy parameter is a pointer to an eight-word integer array.
  236. This array defines the area within each form which will be affected.
  237. Pxy[0] and pxy[1] contain, respectively, the X and Y coordinates of
  238. the upper left corner of the source rectangle.  These are given as
  239. positive  pixel displacements from the upper left of the form.
  240. Pxy[2] and pxy[3]  contain the X and Y displacements for the lower
  241. right of the source rectangle.
  242.  
  243.      Pxy[4] through pxy[7] contain the destination rectangle in the
  244. same format.  Normally, the destination and source should be the same
  245. size.  If not, the size given for the source rules, and the whole are
  246. is transferred beginning at the upper left given for the destination.
  247.  
  248.      This all sounds complex, but is quite simple in many cases.
  249. Consider an example where you want to move a 32 by 32 pixel area
  250. from one part of the display to another.  You would need to allocate
  251. only one MFDB, with a zero in the fd_addr field.  The VDI will take
  252. care of counting color planes and so on.  The upper left raster
  253. coordinates of the source and destination rectangles go into pxy[0],
  254. pxy[1] and pxy[4], pxy[5] respectively.  You add 32 to each of these
  255. values and insert the results in the corresponding lower right
  256. entries, then make the copy call using the same MFDB for both source
  257. and destination.  The VDI takes care of any overlaps.
  258.  
  259.                      COPY RASTER TRANSPARENT
  260.  
  261.      This operation is used for copying from a monochrome form to a
  262. color form.  It is called transparent because it "writes through" to
  263. all of the color planes.  Again, the forms need to be in
  264. device-specific form.  The calling format is:
  265.  
  266.      vrt_cpyfm(vdi_handle, mode, pxy, source, dest, color);
  267.  
  268.   All of the parameters are the same as copy opaque, except that
  269. color has been added.  Color is a pointer to a two word integer
  270. array. Color[0] contains the color index which will be used when a
  271. one appears in the source form, and color[1] contains the index for
  272. use when a zero occurs.
  273.  
  274.      Incidentally, copy transparent is used by the AES to draw
  275. G_ICONs and G_IMAGEs onto the screen.  This explains why you do not
  276. need to convert them to color forms yourself.
  277.  
  278.      (A note for advanced VDI programmers: The pxy parameter in both
  279. copy opaque and transparent may be given in normalized device
  280. coordinates (NDC) if the workstation associated with vdi_handle was
  281. opened for NDC work.)
  282.  
  283.                         THE MODE PARAMETER
  284.  
  285.      The mode variable used in both of the copy functions is an
  286. integer with a value between zero and fifteen. It is used to select
  287. how the copy function will merge the pixel values of the source and
  288. destination forms.  The complete table of functions is given in the
  289. download.  Since a number of these are of obscure or questionable
  290. usefulness, I will only discuss the most commonly used modes.
  291.  
  292.                            REPLACE MODE
  293.  
  294.      A mode of 3 results in a straight-forward copy: every
  295. destination pixel is replaced with the corresponding source form
  296. value.
  297.  
  298.                             ERASE MODE
  299.  
  300.      A mode value of 4 will erase every destination pixel which
  301. corresponds to a one in the source form.  (This mode corresponds to
  302. the "eraser" in a Paint program.)  A mode value of 1 will erase every
  303. destination pixel which DOES NOT correspond to a one in the source.
  304.  
  305.                              XOR MODE
  306.  
  307.      A mode value of 6 will cause the destination pixel to be toggled
  308. if the corresponding source bit is a one. This operation is
  309. invertable, that is, executing it again will reverse the effects.
  310. For this reason it is often used for "software sprites" which must be
  311. shown and then removed from the screens.  There are some problems
  312. with this in color operations, though - see below.
  313.  
  314.                          TRANSPARENT MODE
  315.  
  316.      Don't confuse this term with the copy  transparent function
  317. itself.  In this case it simply means that ONLY  those destination
  318. pixels corresponding with ones in the source form will  be modified
  319. by the operation.  If a copy transparent is being performed, the
  320. value of color[0] is substituted for each one bit in the source form.
  321. A mode value of 7 selects transparent mode.
  322.  
  323.                      REVERSE TRANSPARENT MODE
  324.  
  325.      This is like transparent mode except that only those destination
  326. pixels corresponding to source ZEROS are modified.  In a copy
  327. transparent, the value of color[1] is substituted for each zero bit.
  328. Mode 13 selects reverse transparent.
  329.  
  330.                        THE PROBLEM OF COLOR
  331.  
  332.      I have discussed the various modes as if they deal with one and
  333. zero pixel values only.  This is exactly true when both forms are
  334. monochrome, but is more complex when one or both are color forms.
  335.  
  336.      When both forms are color, indicating that a copy opaque is
  337. being performed, then the color planes are combined bit-by-bit using
  338. the rule for that mode.  That is, for each corresponding source and
  339. destination pixel, the VDI extracts the top order bits and processes
  340. them, then operates on the next lower bit, and so on, stuffing each
  341. bit back into the destination form as the copy progresses.  For
  342. example, an XOR operation on pixels valued 7 and 10 would result in a
  343. pixel value of 13.
  344.  
  345.      In the case of a copy transparent, the situation is more
  346. complex. The source form consists of one plane, and the destination
  347. form has two  or more.  In order to match these up, the color[] array
  348. is used.  Whenever a one pixel is found, the value of color[0] is
  349. extracted and used in the bit-by-bit merge process described in the
  350. last paragraph.  When a zero is found, the value of color[1] is
  351. merged into the destination form.
  352.  
  353.      As you can probably see, a raster copy using a mode which
  354. combines the source and destination can be quite complex when color
  355. planes are used!  The situation is compounded on the ST, since the
  356. actual color values may be remapped by the palette at any time.   In
  357. many cases, just using black and white in color[] may achieve the
  358. effects you desire.  If need to use full color, experimentation is
  359. the  best guide to what looks good on the screen and what is garish
  360. or  illegible.
  361.  
  362.                    OPTIMIZING RASTER OPERATIONS
  363.  
  364.      Because the VDI raster functions are extremely generalized, they
  365. are also slower than hand-coded screen drivers which you might write
  366. for your own special cases.  If you want to speed up your
  367. application's raster operations without writing assembl language
  368. drivers, the following hints will help you increase the VDI's
  369. performance.
  370.  
  371.                        AVOID MERGED COPIES
  372.  
  373.      These are copy modes, such as XOR,  which  require that words be
  374. read from the destination form.  This extra memory access increases
  375. the running time by up to fifty percent.
  376.  
  377.                    MOVE TO CORRESPONDING PIXELS
  378.  
  379.      The bit position within a word of the destination rectangle
  380. should correspond with the bit position of the source rectangle's
  381. left edge.  For instance, if the source's left edge is one pixel in,
  382. then the destination's edge could be at one, seventeen, thirty-three,
  383. and so.  Copies which do not obey this rule force the VDI to shift
  384. each word of the form as it is moved.
  385.  
  386.                           AVOID FRINGES
  387.  
  388.      Put the left edge of the source and destination  rectangles on
  389. an even word boundary, and make their widths even multiples  of
  390. sixteen.  The VDI then does not have to load and modify partial words
  391. within the destination forms.
  392.  
  393.                         USE ANOTHER METHOD
  394.  
  395.      Sometimes a raster operation is not the fastest way to
  396. accomplish your task.  For instance, filling a rectangle with zeros
  397. or ones may be accomplished by using raster copy modes zero and
  398. fifteen, but it is faster to use the VDI v_bar function instead.
  399. Likewise, inverting an area on the screen may be done more quickly
  400. with v_bar by using BLACK in XOR mode.  Unfortunately, v_bar cannot
  401. affect  memory which is not in the video map, so these alternatives
  402. do not always work.
  403.  
  404.                          FEEDBACK RESULTS
  405.  
  406.      The results of the poll on keeping or  dropping the use of
  407. portability macros are in.  By a slim margin, you have voted to keep
  408. them.  The vote was close enough that in future columns I will try to
  409. include ST-only versions of routines which make  heavy use of the
  410. macros.  C purists and dedicated Atarians may then use the alternate
  411. code.
  412.  
  413.                         THE NEXT QUESTION
  414.  
  415.      This time I'd like to ask you to drop by the Feedback Section
  416. and tell me whether the technical level of the columns has been:
  417.  
  418.      A)  Too hard!  Who do you think we are, anyway?
  419.      B)  Too easy!  Don't underestimate Atarians.
  420.      C)  About right, on the average.
  421.  
  422.      If you have the time, it would also help to know a little about
  423. your background, for instance, whether you are a professional
  424. programmer, how long you have been computing, if you owned an 8-bit
  425. Atari, and so on.
  426.  
  427.                           COMING UP SOON
  428.  
  429.      The next column will deal with GEM menus: How they are
  430. constructed, how to decipher menu messages, and how to change menu
  431. entries at run-time.  The following issue will contain more feedback
  432. response, and a discussion on designing user interfaces for GEM
  433. programs.
  434.