12-30-2005, 04:14 AM
I wrote a simple program that can flip an image vertically and horizontally, scale it by two, invert the colors, and greyscale it.
Enjoy.
edit:optimized invert color routine to run slightly faster
Enjoy.
edit:optimized invert color routine to run slightly faster
Code:
enum
IMG_NONE=1
IMG_SCALE_2X=2
IMG_FLIP_VERT=4
IMG_FLIP_HORZ=8
IMG_INVERT=16
IMG_GREY=32
end enum
union color32
c as integer
b(3) as ubyte
end union
sub ImageMorph(i as integer ptr,i2 as integer ptr,flags as integer)
dim as integer wid,hei,tX,tY
dim as color32 col
wid=(i[0] shl 16) shr 19
hei=(i[0] shr 16)
if flags and IMG_SCALE_2X then i2=imagecreate(wid shl 1,hei shl 1,0) else i2=imagecreate(wid,hei,0)
for tX = 0 to wid-1
for tY = 0 to hei-1
if flags and IMG_FLIP_HORZ then tX=wid-tX-1
if flags and IMG_FLIP_VERT then tY=hei-tY-1
if flags and IMG_GREY then
col.c=i[tX+tY*wid+1]
col.c=col.b(2)*.299+col.b(1)*.587+col.b(0)*.114
col.b(0)=col.c
col.b(1)=col.c
col.b(2)=col.c
else
col.c=i[tX+tY*wid+1]
end if
if flags and IMG_INVERT then
col.b(0)=col.b(0) xor 255
col.b(1)=col.b(1) xor 255
col.b(2)=col.b(2) xor 255
end if
if flags and IMG_FLIP_HORZ then tX=wid-tX-1
if flags and IMG_FLIP_VERT then tY=hei-tY-1
if flags and IMG_SCALE_2X then
i2[(tX shl 1)+(tY shl 1)*(wid shl 1)+1]=col.c
i2[((tX shl 1) or 1)+((tY shl 1) or 1)*(wid shl 1)+1]=col.c
i2[(tX shl 1)+((tY shl 1) or 1)*(wid shl 1)+1]=col.c
i2[((tX shl 1) or 1)+(tY shl 1)*(wid shl 1)+1]=col.c
else
i2[tX+tY*wid+1]=col.c
end if
next
next
end sub
randomize timer
screenres 640,480,32
dim as integer ptr img,img2
dim as integer tC,w=64,h=64,x
dim as integer ptr bla
dim as single tm
'create an example image
img=imagecreate(w,h)
for tC = 1 to 123904
x=int(rnd*64)
y=int(rnd*64)
pset img,(x,y),rgb(x*4,(63-x)*4,y*4)
next
put(0,0),img
tm = timer
for t = 1 to 1000
ImageMorph(img,img2,IMG_SCALE_2X or IMG_FLIP_VERT or IMG_FLIP_HORZ or IMG_INVERT)' or IMG_GREY)
next
'show fps
tm=timer-tm
locate h/4+1,1
print 1000/tm;" fps"
put(w,0),img2
sleep
imagedestroy(img2)