Flip Image
Blitz3D Forums/Blitz3D Programming/Flip Image
| ||
Hello. I have written a little routine to flip a image and save it to a new file. Everything works fine except one little bug, and I am not able to find it. If I flip an image vertical the new image will have wrong pixels at the first line (y=0). If I flip an image horizontal I have wrong pixels at the first column (x=0). I'm really going mad - hope someone could help me. Here is the code: Function FlipImg(infile$,outfile$,horz, verz) Local img = LoadImage(infile$) Local w = ImageWidth(img) Local h = ImageHeight(img) Local newimage = CreateImage(w,h) LockBuffer ImageBuffer(img) LockBuffer ImageBuffer(newimage) For y=0 To h For x=0 To w pix = ReadPixelFast(x,y,ImageBuffer(img)) If horz=1 And verz=0 Then WritePixelFast(w-x,y,pix,ImageBuffer(newimage)) If horz=0 And verz=1 Then WritePixelFast(x,h-y,pix,ImageBuffer(newimage)) If horz=1 And verz=1 Then WritePixelFast(w-x,h-y,pix,ImageBuffer(newimage)) Next Next UnlockBuffer ImageBuffer(img) UnlockBuffer ImageBuffer(newimage) SaveBuffer (ImageBuffer(newimage),outfile$) FreeImage img FreeImage newimage End Function |
| ||
For y=0 To h-1 For x=0 To w-1 pix = ReadPixelFast(x,y,ImageBuffer(img)) If horz=1 And verz=0 Then WritePixelFast(w-x-1,y,pix,ImageBuffer(newimage)) If horz=0 And verz=1 Then WritePixelFast(x,h-y-1,pix,ImageBuffer(newimage)) If horz=1 And verz=1 Then WritePixelFast(w-x-1,h-y-1,pix,ImageBuffer(newimage)) Next Next ;or h=h-1 : w=w-1 For y=0 To h For x=0 To w pix = ReadPixelFast(x,y,ImageBuffer(img)) If horz=1 And verz=0 Then WritePixelFast(w-x,y,pix,ImageBuffer(newimage)) If horz=0 And verz=1 Then WritePixelFast(x,h-y,pix,ImageBuffer(newimage)) If horz=1 And verz=1 Then WritePixelFast(w-x,h-y,pix,ImageBuffer(newimage)) Next Next ;or Function FlipImg(infile$,outfile$,horz, verz) Local img = LoadImage(infile$) TFormFilter False ScaleImage img,1-2*(horz=True),1-2*(verz=True) SaveBuffer (ImageBuffer(img),outfile$) FreeImage img End Function |
| ||
Thank you. I have tried various things now and the following code seems to work, but I don't know why. If someone knows it would be nice if he can post it here :-)If horz=1 And verz=0 Then pix = ReadPixelFast(x-1,y,ImageBuffer(img)) : WritePixelFast(w-x,y,pix,ImageBuffer(newimage)) If horz=0 And verz=1 Then pix = ReadPixelFast(x,y-1,ImageBuffer(img)) : WritePixelFast(x,h-y,pix,ImageBuffer(newimage)) If horz=1 And verz=1 Then pix = ReadPixelFast(x-1,y-1,ImageBuffer(img)) : WritePixelFast(w-x,h-y,pix,ImageBuffer(newimage)) |
| ||
Can't see how that works correctly since it reads before the first pixel? Here are some other possibilites. May still need some work though. Function FlipImg2(infile$,outfile$,horz, verz) Local image,w,h,himg,vimg,ibuf,hbuf,vbuf,centre image=LoadImage(infile) w = ImageWidth(image) h = ImageHeight(image) himg = CreateImage(w,1) vimg = CreateImage(1,h) ibuf = ImageBuffer(image) hbuf = ImageBuffer(himg) vbuf = ImageBuffer(vimg) If horz Then centre = (w-1)/2 For x=0 To centre CopyRect x,0,1,h, 0,0,ibuf,vbuf CopyRect w-x-1,0,1,h, x,0,ibuf,ibuf CopyRect 0,0,1,h, w-x-1,0,vbuf,ibuf Next EndIf If verz Then centre = (h-1)/2 For y=0 To centre CopyRect 0,y,w,1, 0,0,ibuf,hbuf CopyRect 0,h-y-1,w,1, 0,y,ibuf,ibuf CopyRect 0,0,w,1, 0,h-y-1,hbuf,ibuf Next EndIf SaveBuffer (ibuf,outfile$) FreeImage himg FreeImage vimg Freeimage image End Function ; or a simpler version, but slower here for large images due to use of copyimage. Function FlipImg2a(infile$,outfile$,horz, verz) Local image,w,h,himg,vimg,ibuf,hbuf,vbuf image=LoadImage(infile) w = ImageWidth(image) h = ImageHeight(image) tempimage = CopyImage(image) ibuf = ImageBuffer(image) tbuf = ImageBuffer(tempimage) If horz Then For x=0 To w-1 CopyRect x,0,1,h, w-x-1,0,ibuf,tbuf Next EndIf If verz Then For y=0 To h-1 CopyRect 0,y,w,1, 0,h-y-1,tbuf,ibuf Next EndIf If verz Then SaveBuffer (ibuf,outfile$) Else SaveBuffer(tbuf,outfile$) Freeimage image Freeimage tempimage End Function ; the little used copypixelfast. Function FlipImg4(infile$,outfile$,horz, verz) Local img,w,h,newimage,ibuf,nbuf img=LoadImage(infile$) ibuf = ImageBuffer(img) w = ImageWidth(img) h = ImageHeight(img) newimage = CreateImage(w,h) nbuf=ImageBuffer(newimage) LockBuffer ibuf LockBuffer nbuf h=h-1 : w=w-1 If horz=1 And verz=0 Then For y=0 To h : For x=0 To w : CopyPixelFast x,y,ibuf,w-x,y,nbuf : Next : Next If horz=0 And verz=1 Then For y=0 To h : For x=0 To w : CopyPixelFast x,y,ibuf,x,h-y,nbuf : Next : Next If horz=1 And verz=1 Then For y=0 To h : For x=0 To w : CopyPixelFast x,y,ibuf,w-x,h-y,nbuf : Next : Next UnlockBuffer nbuf UnlockBuffer ibuf SaveBuffer (ImageBuffer(newimage),outfile$) FreeImage img Freeimage newimage |