1. rotate routine

Hi,

the rotate_image() function below is something I cooked up last night for Einar
Mogen. It rotates an image in its own space about its centre. I do not think it
is particularly useful, because it is not fast (and yes, I did use look up
tables, so every trig function and every multiplication is performed only once,
but those pixel routines are a bit of a pain), and images come out badly mangled
when rotated at angles that are not multiples of 90 degrees. Raster based images
are notoriously difficult to rotate!

I hope somebody will eventually find some use for it. Jiri.


-- imrot.ex: image rotation
-- Jiri Babor
-- j.babor at gns.cri.nz

include graphics.e
include image.e
include get.e     --wait_key()

sequence s
object junk

function rotate_image(sequence s,atom a)
   -- rotate image in sequence s in its own space about its cetre.
   -- a is angle of rotation, in degrees, positive in clockwise direction
   -- unused locations are set to zeros (transparent)
   sequence rs,xca,xsa,yca,ysa
   atom ca,sa,xc,yc
   integer lx,ly,x,y
   ly=length(s)
   lx=length(s[1])
   rs=repeat(repeat(0,lx),ly)
   xc=(lx+1)/2
   yc=(ly+1)/2
   a=0.01745329*a     -- degrees to radians (pi/180)
   sa=sin(a)   ca=cos(a)
   xca={}   xsa={}
   for i=1 to lx do
      xca=append(xca,(i-xc)*ca)
      xsa=append(xsa,(i-xc)*sa)
   end for
   yca={}   ysa={}
   for i=1 to ly do
      yca=append(yca,(i-yc)*ca)
      ysa=append(ysa,(i-yc)*sa)
   end for
   for i=1 to ly do
      for j=1 to lx do
         x=floor(xca[j]-ysa[i]+xc+0.00001)  -- to eliminate ugly rounding errors
         y=floor(xsa[j]+yca[i]+yc+0.00001)
         if x>0 and x<=lx and y>0 and y<=ly then
            rs[y][x]=s[i][j]
         end if
      end for
   end for
   return rs
end function

procedure rect(integer i1,integer i2,sequence s1,sequence s2)
   -- rectangle given by its diagonal points s1,s2
   -- i1 is color, i2 is fill flag (0/1 -> empty/filled)
   polygon(i1,i2,{s1,{s2[1],s1[2]},s2,{s1[1],s2[2]}})
end procedure

procedure merge_image(sequence s1, sequence s2)
   -- merge image in sequence s2 with screen background at point s1
   -- zero values in s2 (see-through points) will not be copied
   sequence s
   integer co,w
   w=length(s2[1])
   for r=1 to length(s2) do
      s=get_pixel({s1[1],s1[2]+r-1,w}) -- get background row
      for c=1 to w do
         co=s2[r][c]
         if co then s[c]=co end if
      end for
      pixel(s, {s1[1],s1[2]+r-1})
   end for
end procedure

-- main ------------------------------------------------------------------------
junk=graphics_mode(19)
rect(4,1,{0,100},{319,199})     -- bottom half of screen with blue background

rect(14,1,{10,30},{15,50})
rect( 9,1,{25,30},{30,50})

s=save_image({5,25},{35,55})
display_image({5,105},rotate_image(s,0))
display_image({55,105},rotate_image(s,90))
display_image({105,105},rotate_image(s,180))
display_image({155,105},rotate_image(s,270))
merge_image({205,105},rotate_image(s,  0))
merge_image({205,105},rotate_image(s, 90))
merge_image({255,105},rotate_image(s,180))
merge_image({255,105},rotate_image(s,270))

display_image({5,155},rotate_image(s,30))
display_image({55,155},rotate_image(s,45))
display_image({105,155},rotate_image(s,60))
merge_image({155,155},rotate_image(s, 30))
merge_image({205,155},rotate_image(s, 45))
merge_image({255,155},rotate_image(s, 60))

junk=wait_key()
junk=graphics_mode(-1)

new topic     » topic index » view message » categorize

2. Re: rotate routine

>the rotate_image() function below is something I cooked up last night
>for Einar Mogen. It rotates an image in its own space about its centre.
I do
>not think it is particularly useful, because it is not fast (and yes, I
did
>use look up tables, so every trig function and every multiplication is
>performed only once, but those pixel routines are a bit of a pain), and
images >come out badly mangled when rotated at angles that are not
multiples of 90
>degrees. Raster based images are notoriously difficult to rotate!

>I hope somebody will eventually find some use for it. Jiri.

I know this reply is VERY late........
But I was thinking, most programs need to have a picture of each possible
rotation, so that it can display all the rotations for the picture. (Like
a top-down racing game like Super Sprint or Micro Machines.)
If a rotate routine that works perfectly, even slowly, like those in
paint programs, were to just save the information to a look up table,
then each rotation could be accessed as if they were loaded up from the
file normally. But only one image would be needed!

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu