home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD2.iso / Programming / Assembler / dse-src3.dms / in.adf / Source / Vectors / Textures.lha / TextureMapping.txt
Encoding:
Internet Message Format  |  1993-01-06  |  14.1 KB

  1. From: mikem@uhccux.uhcc.hawaii.edu (Mike Morton)
  2. Newsgroups: comp.graphics
  3. Subject: summary: rotating bitmaps
  4. Date: 11 Nov 88 23:05:12 GMT
  5. Organization: University of Hawaii
  6.  
  7. Not long ago, I asked for ideas on how to quickly rotate a bitmapped
  8. image by an arbitrary angle.  Quite a response!
  9.  
  10. Thanks to all, including several whose messages don't appear here
  11. because they were subsets of other messages I got.
  12.  
  13. First, for those of you who prefer the library, here's a summary of
  14. the references I got.  Following are some of the responses, several
  15. of which are quite detailed.
  16. ----------------------------------------------------------------------
  17. REFERENCES
  18.  
  19. Fant, K.M.   (1986)  IEEE Computer Graphics & Applications 6,1  71-80.
  20.     "A nonaliasing, real-time spatial transform technique."
  21.  
  22. Foley & Van Dam "Fundamentals of Interactive Computer Graphics" chapter 8
  23.  
  24. Jackson, T.    (1987)  Graphics and Image Processing Algorithms for the
  25.     miniDAP.  BSc. Project Report, Dept. Computer SCience & Statistics,
  26.     Queen Mary College, London University, Mile End Rd., London.
  27.  
  28. Alan Paeth, ``A Fast Algorithm for General Raster Rotation,'' Graphics
  29.     Interface '86, pp. 77-81, May 1986.
  30.  
  31. A. Tanaka, M. Kameyama, S. Kazama, and O. Watanabe, ``A Rotation Method
  32.     for Raster Image Using Skew Transformation,'' Proc. IEEE Conference
  33.     on Computer Vision and Pattern Recognition, pp. 272-277, June 1986.
  34. ----------------------------------------------------------------------
  35. From: David Fox <fox@garfield.cs.columbia.edu>
  36. Organization: Columbia University CS Department
  37.  
  38. The idea is that you compose the functions "shear" and "transpose".
  39. "Shear" takes a bitmap like this
  40. a-----b
  41. |     |
  42. |     |
  43. |     |
  44. c-----d
  45. and shears it by a given angle x to give this
  46. a-----b
  47.  \     \
  48. ->\     \
  49.    \     \
  50. --> c-----d
  51. Transpose rotates that about the diagonal to give this
  52. a
  53.  |\
  54.  | \
  55.  |  \
  56. b|   |c
  57.   \  |
  58.    \ |
  59.     \|d
  60. Then a shear in the other direction gives this
  61. -->   /\a
  62.      /  \
  63.     /    \
  64. ->b/      \c
  65.    \      /
  66.     \    /
  67.      \  /
  68.      d\/
  69. Finally, another transpose yields the rotated bitmap
  70.  
  71.     /\b
  72.    /  \
  73.   /    \
  74. a/      \d
  75.  \      /
  76.   \    /
  77.    \  /
  78.    c\/
  79.  
  80. I did not invent this algorithm, so please don't attribute it to me.
  81. Let me know what kind of replies you get, I'm curious whether this
  82. algorithm is widely known (and whose it is), or unknown, or whether
  83. there are other algorithms to do this.  Enjoy.
  84.  
  85. David Fox
  86. fox@cs.columbia.edu
  87. ----------------------------------------------------------------------
  88. From: jeff@lorrie.atmos.washington.edu (Jeff Bowden)
  89.  
  90. See Foley & Van Dam "Fundamentals of Interactive Computer Graphics" chapter 8
  91. (I think).
  92.  
  93. For a 2D rotate about the origin, multiply all coordinates by this matrix:
  94.  
  95. ( x y )    (                   )
  96.     (  cos(r)    sin(r) )
  97.     (             )
  98.     ( -sin(r)    cos(r) )
  99.     (            )
  100.  
  101. method 1:  (slow) Pass each pixel through the above matrix.
  102.  
  103. method 2: (faster) Pass the endpoints of each horizontal line in your image
  104. through the matrix.  Use bresenham's algorithm to plot the points in between
  105. but instead of plotting all of the pixels the same color, iterate through your
  106. source line and use the values you find there.
  107.  
  108. method 3: (fastest) Rotate the corner coordinates of your rectangle, use
  109. bresenham to generate the coordinates of the endpoints of your rotated
  110. (previously horizontal) lines, and apply method 2 using these endpoints.
  111.  
  112. NOTE: I have no actual experience with these methods on bitmaps.  I only use
  113. the coordinate transformation matrix on lines and triangles.  There may be
  114. problems with bresenham not generating the same number of points for the
  115. rotated line as the horizontal source line.  If there is a difference it
  116. should be only 1 or 2 pixels, you need to study its behaviour before you will
  117. know.  I contemplated implementing method 3 at one point and actually coded
  118. some of it before I got sidetracked with other things.  No, I don't have the
  119. code anywhere anymore.
  120. ----------------------------------------------------------------------
  121. From: oster%SOE.Berkeley.EDU@jade.berkeley.edu (David Phillip Oster)
  122.  
  123. for gray scale images, of course, this reduces to a simple interpolation
  124. problem: each destination pixel is the weighted sum of some small
  125. neighborhood of source pixels. You need to compute the weights, and come
  126. up with an efficient algorithm for marching through the bitmap.
  127. ----------------------------------------------------------------------
  128. From: mcvax!cwi.nl!edwin@uunet.UU.NET (Edwin Blake)
  129. Organization: CWI, Amsterdam
  130.  
  131. In reply to your question I have made the following summary (its still
  132. too long!).  If you would like more implementation details please
  133. contact me.
  134.  
  135. What I did not mention is that there is a three-pass method (suited to
  136. SIMD machines) which Jackson [see below] develops but this is probably
  137. irrelevant for you as well.
  138.  
  139. -=-=-=-=-=-=-=-=-=-=-=-=-=-
  140.  
  141. 8th November, 1988            E.H.Blake  <edwin@cwi.nl>
  142.  
  143.     Two-dimensional Image Transformations.
  144.     ======================================
  145.  
  146. The general linear transformations of images (translations, rotations
  147. shear etc) can be achieved by a vector addition, and multiplication by a
  148. 2x2 matrix.
  149.  
  150. Clearly we wish to separate out translations of images since they are by
  151. far the easiest and cheapest transformation.
  152.  
  153. We are now left with the transformations represented by the same two-
  154. dimensional matrix applied to the coordinates of each point in the
  155. image.  It might be convenient to separate this into components which
  156. can be dealt with in different ways: i.e. change in the size in the x
  157. and y directions, shear in those directions and pure rotation.
  158.  
  159. Since we are going to deal with images on a raster questions about re-
  160. sampling and aliasing will arise.  We shall present two fundamentally
  161. different approaches.  The one approach deals with fractional pixels and
  162. attempts to minimise sampling errors by distributing parts of a source
  163. pixel amongst the destination pixels.  The other approach deals only
  164. with whole pixels and reduces temporal aliasing (i.e. flicker) at the
  165. expense of spatial resolution.  When shearing images the whole pixel
  166. method produces pixels might not be quite right for their position but
  167. no pixels are lost or duplicated.
  168.  
  169. The whole pixel approach can also be used for overall size changes in
  170. the images.  When the size of the image changes we either cull or dupli-
  171. cate certain pixels.  Particularly with size reduction this can lead to
  172. unacceptable results because certain features in the image may disap-
  173. pear.  This leads us to adopt the fractional pixel resampling approach.
  174.  
  175. There is another important difference between the two approaches how-
  176. ever:  the fractional pixel approach has to examine and interpret pixel
  177. values and must be able to combine them.  The whole pixel approach for
  178. shearing combined with a duplicate/cull method of size change is
  179. independent of the values of the pixels.  This value independence (which
  180. is really a type independence) is important where the pixel values are
  181. themselves isolated points in a much bigger colour space.  For example,
  182. if the pixels are pointers into a colour table then we cannot in general
  183. expect to be able to average the two pointers and come up with a pointer
  184. to the intermediate colour.
  185.  
  186. These simple operations repeated over a large number of pixels make them
  187. ideal candidates for special purpose VLSI or SIMD machines.  In fact the
  188. routines described below are partly inspired by algorithms originally
  189. intended for the miniDAP (a 32x32 SIMD machine) [Jackson, 1987] or for a
  190. special purpose processor [Fant, 1986].
  191.  
  192.  
  193. Composition of Image Transformations.
  194. =====================================
  195.  
  196. The 2x2 transformation matrix can be written as a composition of
  197. transformations in a number of ways.  The decomposition used depends on
  198. the order in which the transformations are applied.  The basic image
  199. transformation which leaves its source unchanged is the unit matrix.
  200.  
  201. If we first shear in the y direction, then in the x direction and
  202. finally scale the image the decomposition is as follows:
  203.  
  204.        | a   b |   =   | m   0 |  *  | 1   x |  *  | 1   0 |            (1)
  205.        | c   d |       | 0   n |     | 0   1 |     | y   1 |
  206.  
  207.                    =   | m + mxy   mx |
  208.                        |   ny      n  |
  209.  
  210. In the decomposition y is a shear parallel to the y-axis, x is a shear
  211. parallel to the x-axis, n represents a scaling in the y dimension and m
  212. a scaling in the x dimension.  By identifying terms we can see that:
  213.  
  214.       n  =  d
  215.      and provided d  !=  0
  216.       y  =   c/d        &      m  =  a -  bc/d                          (2)
  217.      and provided ad - bc is non-zero
  218.       x  =   db/(ad - bc)
  219.  
  220. If the determinant, ad - bc, is zero then the transformation is singu-
  221. lar.  This means that the image need not be treated as a plane.  If the
  222. transformation is non-singular but d = 0, then we can rotate the image
  223. by ninety degrees and exchange -b for d.
  224.  
  225. If on the other hand we first apply all the y-direction changes (shear
  226. and scaling) and then the x-direction changes, the decomposition is as
  227. follows:
  228.  
  229.        | a   b |  =  | e   f |  *  | 1   0 |                           (3)
  230.        | c   d |     | 0   1 |     | g   h |
  231.  
  232.                   =  | e + fg  fh |
  233.                      |   g      h |
  234.  
  235. In the decomposition g is a shear parallel to the y-axis and h the scal-
  236. ing in that direction, f is a shear parallel to the x-axis and e the
  237. corresponding scaling. Then:
  238.  
  239.       h  =  d
  240.       g  =  d                                                         (4)
  241. and provided d  !=  0
  242.       f  =  b/d
  243.       e  =  bc/(a -  d)
  244.  
  245. Similar considerations as in (2) regarding d apply to (4).
  246.  
  247.  
  248. Fast, Pixel Preserving Image Transformation:  "QUICK"
  249. =====================================================
  250.  
  251. In order to refer to this method of image transformation easily it will
  252. be called the "Quick" method.  The quick method always moves whole pix-
  253. els without interpretation, it is thus independent of the underlying
  254. type of the representation.  There are four basic kinds of operation we
  255. shall need:
  256.  
  257. 1.   Pixel shear (translation) according to position.
  258.  
  259. 2.   Shearing of pixels in two dimensions together, without writing out
  260.      the intermediate results.
  261.  
  262. 3.   Selective pixel duplication to achieve fractional size increases.
  263.  
  264. 4.   Selective pixel culling to achieve fractional decreases in image
  265.      size.
  266.  
  267. A common theme to all these transformations is distributing a fraction
  268. of an integral set of operations over an interval.  For example, choos-
  269. ing which pixels to duplicate to get 10% size increase, or which lines
  270. to shift for a fractional shear.  This has a very efficient solution in
  271. Bresenham's well known algorithm, which is mainly used for line drawing
  272. [see any textbook, e.g. Newman & Sproull].
  273.  
  274. The other problem is combining two shears in two-dimensions.  The sim-
  275. plest way would be to write out the intermediate image and then
  276. transform it a second time.  However, by tracing the jagged path which
  277. an output line follows we can derive a scanline algorithm which produces
  278. the sheared image.  By duplicating or neglecting selected pixels and
  279. then duplicating or neglecting selected lines we have an efficient and
  280. general image transformation method.  The algorithm notionally employed
  281. the decomposition of equation 2 but without writing out any intermediate
  282. results.
  283.  
  284. This algorithm was fully implemented and its combination of features may
  285. very well be unique.
  286.  
  287.  
  288. Two-Pass, Anti-Aliased Image Transformation:  "PERFECT"
  289. =======================================================
  290.  
  291. This image transformation writes out its intermediate results.  The
  292. pixel values are interpreted, which means that colour errors can occur
  293. from resampling.  For grey level images however the output is much
  294. better, because anti-aliased, than the previous method.  For brevity it
  295. will be referred to as the "Perfect" method.
  296.  
  297. The algorithm interpolates a selected window of input pixels to produce
  298. the desired view of output pixels.  The columns of the image are first
  299. processed and then the rows of the resulting intermediate image.
  300. Because of the two pass nature of the algorithm shears can be done
  301. easily for each dimension separately.  The "perfect" method is well
  302. suited to VLSI implementations [Fant, 1986], and the reader is referred
  303. to Fant's article for more details.  The decomposition derived in equa-
  304. tions 3 and 4 apply to this algorithm.  Using the decomposition into the
  305. shear and scaling components avoids the rather laborious procedure used
  306. by Fant which involved calculating the positions of the four corners of
  307. an image.
  308.  
  309.  
  310. Simple Averaging Image Transformation.    "PRETTY"
  311. ==================================================
  312.  
  313. Because the two pass algorithm was so slow there was an attempt to
  314. produce an intermediate algorithm.  This algorithm is essentially the
  315. same as "Quick" except that pixels are not culled when the image
  316. decreases in size.  Instead a simple averaging is performed.  Successive
  317. pixels which would be discarded are accumulated and averaged with the
  318. normal output pixel of a location.  The algorithm was only used for
  319. images which decreased in size.  This was the most common bad case for
  320. the "Quick" method.  Whether this method is worth the extra code is not
  321. clear.
  322. ----------------------------------------------------------------------
  323. From: Alan Wm Paeth <awpaeth%watcgl.waterloo.edu@RELAY.CS.NET>
  324. Organization: U. of Waterloo, Ontario
  325.  
  326. I presented a paper on this in Graphics Interface '86. The code can run on a
  327. framebuffer (we have an Adage/Ikonas) or a mainframe (Vax implementation).
  328. Three passes over the data are made; each "shear" the data without scaling.
  329. This allows simple resampling and all integer math (as opposed to the
  330. previously known 2-pass varients). It also is good to ninety degrees (the two
  331. pass version breaks down rapidly after 45). In the 90 deg case, the rotate
  332. becomes a simple "shuffle", as done on the "Blit".
  333.  
  334. I can send you the source code or the "troff" text of the paper.  [Alan
  335. said in a later note that this offer is for anyone, not just me. - MM]
  336.  
  337.     /Alan "Aha! Planet!" Paeth
  338.     Computer Graphics Laboratory
  339.     University of Waterloo
  340. ----------------------------------------------------------------------
  341. Enjoy...
  342.  
  343.  -- Mike Morton // P.O. Box 11378, Honolulu, HI  96828, (808) 676-6966 HST
  344.       Internet: msm@ceta.ics.hawaii.edu
  345.     (anagrams): Mr. Machine Tool; Ethical Mormon; Chosen Immortal; etc.
  346.