This program will flip an image in any direction using either memblocks or a shader
feel free to use
// Project: imageManipulation
// Created: 2023-02-03
// show all errors
SetErrorMode(2)
// set window properties
SetWindowTitle( "imageManipulation" )
SetWindowSize( 1024, 768, 0 )
SetWindowAllowResize( 1 ) // allow the user to resize the window
// set display properties
SetVirtualResolution( 1024, 768 ) // doesn't have to match the window
SetOrientationAllowed( 1, 1, 1, 1 ) // allow both portrait and landscape on mobile devices
SetSyncRate( 30, 0 ) // 30fps instead of 60 to save battery
SetScissor( 0,0,0,0 ) // use the maximum available screen space, no black borders
UseNewDefaultFonts( 1 ) // since version 2.0.22 we can use nicer default fonts
speedtest as float
//path="raw:"+getreadpath()+"media/"
test=loadimage("test.png"):resettimer() : rem timer here is set to 0 so as speed tests acurate to milliseconds can be displayed
a=createsprite(test) //reading from left to right and top down a = first sprite
b=createsprite(flip(test,1)) //b second sprite
c=createsprite(flip(test,2)) //c third sprite
d=createsprite(flip(test,3)) //d fourth sprite
SetSpritePosition(a,20,40)
setspriteposition(b,276,40)
setspriteposition(c,532,40)
setspriteposition(d,776,40)
speedtest1$=str(timer())
createShaderFile("flip.ps")
e_s=LoadSpriteShader("flip.ps")
f_s=LoadSpriteShader("flip.ps")
g_s=LoadSpriteShader("flip.ps")
h_s=LoadSpriteShader("flip.ps")
resettimer()
e=createSprite(test) //the second row is for the shader flip the sprite
SetSpritePosition(e,20,220) //from left to right
SetSpriteShader(e,e_s)
f=createSprite(test)
SetSPritePosition(f,276,220)
SetSpriteShader(f,f_s)
g=createSprite(test)
SetSpritePosition(g,532,220)
SetSpriteShader(g,g_s)
h=createSprite(test)
SetSpritePosition(h,776,220)
SetSpriteShader(h,h_s)
SetShaderConstantByName(e_s, "rot", 0.0, 0.0, 0.0, 0.0 ) //the first two passed values may be set to 1.0 to rotate or 0 not to rotate they may only be 1.0 or 0.0
SetShaderConstantByName(f_s, "rot", 1.0, 0.0, 0.0, 0.0 ) //the first two passed values may be set to 1.0 to rotate or 0 not to rotate
SetShaderConstantByName(g_s, "rot", 0.0, 1.0, 0.0, 0.0 ) //the first two passed values may be set to 1.0 to rotate or 0 not to rotate
SetShaderConstantByName(h_s, "rot", 1.0, 1.0, 0.0, 0.0 ) //the first two passed values may be set to 1.0 to rotate or 0 not to rotate
// the first value passed to the shader is flip on x axis while the secong is the y axis
swap():render()
newEI=GetImage(getspritex(e),getspritey(e),getspritex(e)+getspritewidth(e),getspritey(e)+getSpriteheight(e)) //here are four more images to be created that make up the bottom 4th row
newFI=GetImage(getspritex(f),getspritey(f),getspritex(f)+getspritewidth(f),getspritey(f)+getSpriteheight(f))
newGI=GetImage(getspritex(g),getspritey(g),getspritex(g)+getspritewidth(g),getspritey(g)+getSpriteheight(g))
newHI=GetImage(getspritex(h),getspritey(h),getspritex(h)+getspritewidth(h),getspritey(h)+getSpriteheight(h))
speedtest=timer()
speedtest2$=str(speedtest)
resetTimer()
newE=createsprite(newEI)
newF=createsprite(newFI)
newG=createsprite(newGI)
newH=createsprite(newHI)
SetSpritePosition(newE,20,580)
SetSpritePosition(newF,276,580)
SetSpritePosition(newG,532,580)
SetSpritePosition(newH,776,580)
speedtest4$=str(timer()+speedtest)
resettimer()
i=createSprite(test)
SetSpritePosition(i,20,400) //the 3rd row demonostrating getint instead of retrieving the colours individually
j=createSprite(flip2(test,1))
SetSPritePosition(j,276,400)
k=createSprite(flip2(test,2))
SetSpritePosition(k,532,400)
l=createSprite(flip2(test,3))
SetSpritePosition(l,776,400)
speedtest3$=(str(timer()))
//SetRawWritePath(getreadpath())
//saveimage(getspriteimageID(h),"testsave.png")
do
print("flip1="+speedtest1$+" shader"+speedtest2$+" flip2="+speedtest3$+" shader/total="+speedtest4$)
Sync()
loop
function flip(img as integer,flip as integer)
imgmemblock = CreateMemblockFromImage(img)
newmemblock = CreateMemblockFromImage(img)
local size as integer
width = GetMemblockInt(imgmemblock,0)
height = GetMemblockInt(imgmemblock,4)
size=abs(width*height)
for y= 0 to height-1
for x= 0 to width-1 //to 0 step -1
Offset = (12+((y * width) + x) * 4) - 4
r=GetMemblockByte(imgmemblock,Offset)
g=GetMemblockByte(imgmemblock,Offset+1)
b=GetMemblockByte(imgmemblock,Offset+2)
a=GetMemblockByte(imgmemblock,Offset+3)
color#=(r+g+b)
if flip=1
xx=width-1 -x
Offset = (12+((y * width) + xx) * 4) - 4
elseif flip=2
yy=height-1 -y
Offset = (12+((yy * width) + x) * 4) - 4
else
xx=width-1 -x
yy=height-1-y
Offset = (12+((yy * width) + xx) * 4) - 4
endif
SetMemblockByte(newmemblock,Offset,r)
setMemblockByte(newmemblock,Offset+1,g)
setMemblockByte(newmemblock,Offset+2,b)
setMemblockByte(newmemblock,Offset+3,a)
next x
next y
newImg=CreateImageFromMemblock(newmemblock)
deletememblock(newmemblock)
deletememblock(imgmemblock)
endfunction newimg
function createShaderFile(name$ as string)
fw=OpenToWrite(name$)
WriteLine(fw,"#ifdef GL_ES")
WriteLine(fw,"precision mediump float;")
WriteLine(fw,"precision mediump int;")
WriteLine(fw,"#endif")
WriteLine(fw,"#define PROCESSING_TEXTURE_SHADER")
WriteLine(fw,"varying mediump vec2 uvVarying;")
WriteLine(fw,"uniform sampler2D texture0;")
WriteLine(fw,"uniform vec2 rot;")
WriteLine(fw,"void main(void)")
WriteLine(fw,"{")
//WriteLine(fw," vec2 p = uvVarying;")
WriteLine(fw," vec2 p = mix(uvVarying, rot - uvVarying, rot);")
//WriteLine(fw," if (rot.x ==1.0)")
//WriteLine(fw," {p.x=rot.x-p.x;}") ///p.x -= mod(p.x, 1.0 / pixels.x);")
//WriteLine(fw," if (rot.y==1.0)")
//WriteLine(fw," {p.y=rot.y-p.y;}") ////p.y -= mod(p.y, 1.0 / pixels.y);")
WriteLine(fw," vec3 col = texture2D(texture0, p).rgb;")
WriteLine(fw," gl_FragColor = vec4(col, 1.0);")
WriteLine(fw,"}")
CloseFile(fw)
endfunction
function flip2(img as integer,flip as integer)
imgmemblock = CreateMemblockFromImage(img)
newmemblock = CreateMemblockFromImage(img)
local size as integer
width = GetMemblockInt(imgmemblock,0)
height = GetMemblockInt(imgmemblock,4)
size=abs(width*height)
for y= 0 to height-1
SrcOffset = (12+ ((y * width) *4))
for x= 0 to width-1 //to 0 step -1
// Read Source pixel
ARGB=GetMemblockInt(imgmemblock,SrcOffset)
// move to the next pixel along this row
SrcOffset=SrcOffset+4
if flip=1
xx=width-1 -x
Offset = (12+((y * width) + xx) * 4) - 4
elseif flip=2
yy=height-1 -y
Offset = (12+((yy * width) + x) * 4) - 4
else
xx=width-1 -x
yy=height-1-y
Offset = (12+((yy * width) + xx) * 4) - 4
endif
SetMemblockInt(newmemblock,Offset,ARGB)
next x
next y
newImg=CreateImageFromMemblock(newmemblock)
deletememblock(newmemblock)
deletememblock(imgmemblock)
endfunction newimg
ive also included a test image to demonstrate
fubarpk
https://fubarpk.itch.io/