Rotate Image 180 makes black line?

I cannot figure out why this function is making a black line on the side of the image after rotating. If 1 is subtracted from the height or width (or we begin with 1 instead of 0), the line disappears, but so does the first line of pixels… Where am I going wrong?

  Dim p as Picture = theimage
  dim height as integer = p.Height
  dim Width as integer = p.Width
  
  dim newpic as new picture(width,height,32)
  
  dim origsurface as RGBSurface = p.rgbsurface
  dim newsurface as RGBSurface = newpic.RGBSurface
  
  for w as integer = 0 to width
    for h as integer = 0 to height
      newsurface.Pixel(Width-w,height -h) = origsurface.Pixel(w,h)
    next h
  next w
  
  canvas1.Backdrop = newpic
for w as integer = 0 to width-1
    for h as integer = 0 to height-1

[quote=80670:@Tim Hare] for w as integer = 0 to width-1 for h as integer = 0 to height-1 [/quote]

I will go back and try that again, I believe it removed an entire line of pixels my last attempt…will return momentarily

It indeed eliminates the black line, but also removes the first row of pixels from the original image. Id like to maintain all pixels within the image. This has stumped me all day :-/

So after getting a little grub in my tummy I had an epiphany, the original image starts at line 0 when you read from an image, but when you write to an image we must start at line 1 so thus:

for w as integer = 0 to width
for h as integer = 0 to height
newsurface.Pixel(height-(h+1),w) = origsurface.Pixel(w,h)
next h
next w

Not true. The y values go from 0 to Height-1. I just tested it. I drew alternating colored lines. Y=0 shows up fine, Y=Height is not visible.

This, however, does work.

  Dim p as Picture = theimage
  dim height as integer = p.Height
  dim Width as integer = p.Width
  
  dim newpic as new picture(width,height,32)
  
  dim origsurface as RGBSurface = p.rgbsurface
  dim newsurface as RGBSurface = newpic.RGBSurface
  
  for w as integer = 0 to width-1
    for h as integer = 0 to height-1
      newsurface.Pixel(Width-w-1,height -h-1) = origsurface.Pixel(w,h)
    next h
  next w
  
  canvas1.Backdrop = newpic

Is there any reason you aren’t using a PixmapShape with a rotation factor to image your picture onto another one? It’s probably faster (although I haven’t tested this).

For performance, you should reverse the w and h for loops. It’s more efficient to work across a row than down a column (caching issues).

For performance reasons, you should keep on doing it the way you’re doing it. :slight_smile: The rotated PixmapShape is something like four times slower. Swapping the H and W loops has little effect on the speed.

It used to make a big difference, but you’re right, it’s no longer as big a diff. In today’s test, swapping the order was only about 2% faster. Consistent, but not huge.