Today I worked on some Direct-Draw functions for draw-tools like in paint. It's possible to draw dots onto images and to spray, radial or squares. With this functions I made a small program, in which you can create greyscale-images with the given tools, load color-templates, smooth your image and "render" coloured images. It's very usefull for effects like explosions.
And with changed colour-settings:
Here the latest code.
remstart
This file contains many different functions to change images
function ______ IMAGEFUNCTIONS ______
Functions included:
-Black-White
-Negative
-Light
-Alpha from Light
-Global Alpha
remend
function de_img_Setup()
rem useMemblocks=0 -> ImageIDs are taken and memblocks created, =1 -> memblocks are taken directly
global de_img_UseMemblocks as boolean
global de_img_FirstMemblock as integer = 0
global de_img_SizeX as integer
global de_img_SizeY as integer
global de_img_Size as integer
global de_img_ddMem as integer `DirectDraw-Memblock
global de_img_R as byte = 255
global de_img_G as byte = 255
global de_img_B as byte = 255
global de_img_A as byte = 255
endfunction
remstart
function ________ DirectDraw ________
remend
function de_img_Ink(r,g,b,a)
de_img_R = r
de_img_G = g
de_img_B = b
de_img_A = a
endfunction
function de_img_SetDDMem(ID)
de_img_ddMem = ID
endfunction
function de_img_mSpray(x,y,r,i)
for s = 1 to i
xp = x - r + rnd(r*2)
yp = y - r + rnd(r*2)
de_img_mDot(xp,yp)
next s
endfunction
function de_img_mSprayRadial(x,y,r,i)
for s = 1 to i
xp = rnd(r*2)-r
maxy = sqrt(r*r-xp*xp)
yp = y + rnd(maxy)*(rnd(1)*2-1)
inc xp,x
de_img_mDot(xp,yp)
next s
endfunction
function de_img_mDot(x,y)
if x > 0
if y > 0
if x <= de_img_SizeX
if y <= de_img_SizeY
pos = de_img_GetMemPos(x,y)
write memblock byte de_img_ddMem, pos , de_img_B
write memblock byte de_img_ddMem, pos+1, de_img_G
write memblock byte de_img_ddMem, pos+2, de_img_R
write memblock byte de_img_ddMem, pos+3, de_img_A
endif
endif
endif
endif
endfunction
remstart
function ________ Filters ________
remend
function de_img_BlackWhite(ID1,ID2)
rem Create memblock if neccessary and get x and y-size
rem ID1 and ID2 just store memblock-IDs!
M1 = de_img_MemFromImg(ID1)
if ID2=ID1 then M2=M1 else M2 = de_img_MemFromImg(ID2)
rem For all pixels: R/G/B = Average of R,G,B
for pos = 12 to de_img_Size step 4
c = (memblock byte(M1,pos)+memblock byte(M1,pos+1)+memblock byte(M1,pos+2))/3.0
write memblock byte M2, pos , c
write memblock byte M2, pos+1, c
write memblock byte M2, pos+2, c
next pos
rem Update image
de_img_UpdateImage(ID2,M2)
rem Delete memblocks
de_img_DeleteMemblock(M1)
de_img_DeleteMemblock(M2)
endfunction
function de_img_Negative(ID1,ID2)
rem Create memblock if neccessary and get x and y-size
rem ID1 and ID2 just store memblock-IDs!
M1 = de_img_MemFromImg(ID1)
if ID2=ID1 then M2=M1 else M2 = de_img_MemFromImg(ID2)
rem For all pixels: R/G/B = Average of R,G,B
for pos = 12 to de_img_Size step 4
r = 255-memblock byte(M1,pos+2)
g = 255-memblock byte(M1,pos+1)
b = 255-memblock byte(M1,pos )
write memblock byte M2, pos+2, r
write memblock byte M2, pos+1, g
write memblock byte M2, pos , b
next pos
rem Update image
de_img_UpdateImage(ID2,M2)
rem Delete memblocks
de_img_DeleteMemblock(M1)
de_img_DeleteMemblock(M2)
endfunction
function de_img_Light(ID1,ID2,Factor#)
rem Create memblock if neccessary and get x and y-size
rem ID1 and ID2 just store memblock-IDs!
M1 = de_img_MemFromImg(ID1)
if ID2=ID1 then M2=M1 else M2 = de_img_MemFromImg(ID2)
rem For all pixels: R/G/B = Average of R,G,B
for pos = 12 to de_img_Size step 4
r = de_img_Byte(memblock byte(M1,pos+2)*Factor#)
g = de_img_Byte(memblock byte(M1,pos+1)*Factor#)
b = de_img_Byte(memblock byte(M1,pos )*Factor#)
write memblock byte M2, pos+2, r
write memblock byte M2, pos+1, g
write memblock byte M2, pos , b
next pos
rem Update image
de_img_UpdateImage(ID2,M2)
rem Delete memblocks
de_img_DeleteMemblock(M1)
de_img_DeleteMemblock(M2)
endfunction
function de_img_AlphaFromLight(ID1,ID2)
rem Create memblock if neccessary and get x and y-size
rem ID1 and ID2 just store memblock-IDs!
M1 = de_img_MemFromImg(ID1)
if ID2=ID1 then M2=M1 else M2 = de_img_MemFromImg(ID1) `<- memblock for ID2 must have size of ID1, and ID2 needn't exist, so ID1 is taken here instead
rem For all pixels: R/G/B = Average of R,G,B
for pos = 12 to de_img_Size step 4
r = memblock byte(M1,pos+2)
g = memblock byte(M1,pos+1)
b = memblock byte(M1,pos )
c = (r+g+b)/3.0
write memblock byte M2, pos+3, c
next pos
rem Update image
de_img_UpdateImage(ID2,M2)
rem Delete memblocks
de_img_DeleteMemblock(M1)
de_img_DeleteMemblock(M2)
endfunction
remstart - Use "de_img_SetColorChannel(ImgID,3)" instead!
function de_img_SetAlpha(ID1,ID2,Alpha)
rem Create memblock if neccessary and get x and y-size
rem ID1 and ID2 just store memblock-IDs!
M1 = de_img_MemFromImg(ID1)
if ID2=ID1 then M2=M1 else M2 = de_img_MemFromImg(ID1) `<- memblock for ID2 must have size of ID1, and ID2 needn't exist, so ID1 is taken here instead
rem For all pixels: R/G/B = Average of R,G,B
for pos = 15 to de_img_Size step 4
write memblock byte M2, pos, Alpha
next pos
rem Update image
de_img_UpdateImage(ID2,M2)
rem Delete memblocks
de_img_DeleteMemblock(M1)
de_img_DeleteMemblock(M2)
endfunction
remend
function de_img_Blur(ID1,ID2,R)
rem Create memblock if neccessary and get x and y-size
rem ID1 and ID2 just store memblock-IDs!
M1 = de_img_MemFromImg(ID1)
if ID2=ID1 then M2=M1 else M2 = de_img_MemFromImg(ID1) `<- memblock for ID2 must have size of ID1, and ID2 needn't exist, so ID1 is taken here instead
rem For all pixels: R/G/B = Average of R,G,B
x = 0 : y = 1
pxl_Count = (2*r+1)^2
for pos = 12 to de_img_Size step 4
if escapekey() then exit
inc x : if x > de_img_SizeX then x = 1 : inc y
x1 = x - R : x2 = x + R
y1 = y - R : y2 = y + R
rem Comparison-Pixels
CompR = 0 : CompG = 0 : CompB = 0
for actx = x1 to x2
for acty = y1 to y2
xpos = de_img_Mod(actx,de_img_SizeX)
ypos = de_img_Mod(acty,de_img_SizeY)
actpos = de_img_GetMemPos(xpos,ypos)
inc CompR, memblock byte(M1,actpos+2)
inc CompG, memblock byte(M1,actpos+1)
inc CompB, memblock byte(M1,actpos )
next y
next x
CompR = CompR/pxl_Count
CompG = CompG/pxl_Count
CompB = CompB/pxl_Count
rem Write Result to result-memblock
write memblock byte M2, pos , CompB
write memblock byte M2, pos+1, CompG
write memblock byte M2, pos+2, CompR
next pos
rem Update image
de_img_UpdateImage(ID2,M2)
rem Delete memblocks
de_img_DeleteMemblock(M1)
de_img_DeleteMemblock(M2)
endfunction
function de_img_Bloom(ID1,ID2,R)
rem Create memblock if neccessary and get x and y-size
rem ID1 and ID2 just store memblock-IDs!
M1 = de_img_MemFromImg(ID1)
if ID2=ID1 then M2=M1 else M2 = de_img_MemFromImg(ID1) `<- memblock for ID2 must have size of ID1, and ID2 needn't exist, so ID1 is taken here instead
rem For all pixels: R/G/B = Average of R,G,B
x = 0 : y = 1
pxl_Count = (2*r+1)^2
for pos = 12 to de_img_Size step 4
if escapekey() then exit
inc x : if x > de_img_SizeX then x = 1 : inc y
x1 = x - R : x2 = x + R
y1 = y - R : y2 = y + R
rem Comparison-Pixels
CompR = 0 : CompG = 0 : CompB = 0
for actx = x1 to x2
for acty = y1 to y2
xpos = de_img_Mod(actx,de_img_SizeX)
ypos = de_img_Mod(acty,de_img_SizeY)
actpos = de_img_GetMemPos(xpos,ypos)
inc CompR, memblock byte(M1,actpos+2)
inc CompG, memblock byte(M1,actpos+1)
inc CompB, memblock byte(M1,actpos )
next y
next x
CompR = CompR/pxl_Count
CompG = CompG/pxl_Count
CompB = CompB/pxl_Count
rem Write Result to result-memblock
if CompB>memblock byte(M1,pos ) then write memblock byte M2, pos , CompB
if CompG>memblock byte(M1,pos+1) then write memblock byte M2, pos+1, CompG
if CompR>memblock byte(M1,pos+2) then write memblock byte M2, pos+2, CompR
next pos
rem Update image
de_img_UpdateImage(ID2,M2)
rem Delete memblocks
de_img_DeleteMemblock(M1)
de_img_DeleteMemblock(M2)
endfunction
function de_img_MultiplyImages(ID1,ID2,ID3)
rem Create memblock if neccessary and get x and y-size
rem ID1 and ID2 just store memblock-IDs!
M1 = de_img_MemFromImg(ID1)
if ID2=ID1 then M2=M1 else M2 = de_img_MemFromImg(ID2) `<- memblock for ID2 must have size of ID1, and ID2 needn't exist, so ID1 is taken here instead
if ID3=ID2 then M3=M2 else M3 = de_img_MemFromImg(ID2) `<- result-image
rem For all pixels: R/G/B = Average of R,G,B
x = 0 : y = 1
for pos = 12 to de_img_Size step 4
inc x : if x > de_img_SizeX then x = 1 : inc y
rem Comparison-Pixels
NewR = memblock byte(M1,pos+2)*memblock byte(M2,pos+2)*(1.0/255.0)
NewG = memblock byte(M1,pos+1)*memblock byte(M2,pos+1)*(1.0/255.0)
NewB = memblock byte(M1,pos )*memblock byte(M2,pos )*(1.0/255.0)
rem Write Result to result-memblock
write memblock byte M3, pos , NewB
write memblock byte M3, pos+1, NewG
write memblock byte M3, pos+2, NewR
next pos
rem Update image
de_img_UpdateImage(ID3,M3)
rem Delete memblocks
de_img_DeleteMemblock(M1)
de_img_DeleteMemblock(M2)
de_img_DeleteMemblock(M3)
endfunction
rem Offset: 0=Blue, 1=Green, 2=Red, 3=Alpha
function de_img_SetColorChannel(ID1,Offset,Value)
rem Create memblock if neccessary and get x and y-size
rem ID1 and ID2 just store memblock-IDs!
M1 = de_img_MemFromImg(ID1)
rem For all pixels: R/G/B = Average of R,G,B
for pos = 12+Offset to de_img_Size step 4
write memblock byte M1, pos, Value
next pos
rem Update image
de_img_UpdateImage(ID1,M1)
rem Delete memblocks
de_img_DeleteMemblock(M1)
endfunction
function de_img_AddColorChannel(ID1,Offset,Add)
rem Create memblock if neccessary and get x and y-size
rem ID1 and ID2 just store memblock-IDs!
M1 = de_img_MemFromImg(ID1)
rem For all pixels: R/G/B = Average of R,G,B
for pos = 12+Offset to de_img_Size step 4
write memblock byte M1, pos, de_img_Byte(memblock byte(M1,pos)+Add)
next pos
rem Update image
de_img_UpdateImage(ID1,M1)
rem Delete memblocks
de_img_DeleteMemblock(M1)
endfunction
function de_img_MultiplyColorChannel(ID1,Offset,Factor#)
rem Create memblock if neccessary and get x and y-size
rem ID1 and ID2 just store memblock-IDs!
M1 = de_img_MemFromImg(ID1)
rem For all pixels: R/G/B = Average of R,G,B
for pos = 12+Offset to de_img_Size step 4
write memblock byte M1, pos, de_img_Byte(memblock byte(M1,pos)*Factor#)
next pos
rem Update image
de_img_UpdateImage(ID1,M1)
rem Delete memblocks
de_img_DeleteMemblock(M1)
endfunction
rem This function multiplies all color-values with 2 and decreases them by 255
function de_img_TopHalf(ID1,ID2)
rem Create memblock if neccessary and get x and y-size
rem ID1 and ID2 just store memblock-IDs!
M1 = de_img_MemFromImg(ID1)
if ID2=ID1 then M2=M1 else M2 = de_img_MemFromImg(ID2) `<- memblock for ID2 must have size of ID1, and ID2 needn't exist, so ID1 is taken here instead
rem For all pixels: R/G/B = Average of R,G,B
x = 0 : y = 1
for pos = 12 to de_img_Size step 4
inc x : if x > de_img_SizeX then x = 1 : inc y
rem Comparison-Pixels
NewR = memblock byte(M1,pos+2)*2-255 : if NewR < 0 then NewR = 0
NewG = memblock byte(M1,pos+1)*2-255 : if NewG < 0 then NewG = 0
NewB = memblock byte(M1,pos )*2-255 : if NewB < 0 then NewB = 0
rem Write Result to result-memblock
write memblock byte M2, pos , NewB
write memblock byte M2, pos+1, NewG
write memblock byte M2, pos+2, NewR
next pos
rem Update image
de_img_UpdateImage(ID2,M2)
rem Delete memblocks
de_img_DeleteMemblock(M1)
de_img_DeleteMemblock(M2)
endfunction
rem This function multiplies all color-values with 2 and decreases them by 255
function de_img_ScaleToMid(ID1,ID2,f#)
rem Create memblock if neccessary and get x and y-size
rem ID1 and ID2 just store memblock-IDs!
M1 = de_img_MemFromImg(ID1)
if ID2=ID1 then M2=M1 else M2 = de_img_MemFromImg(ID2) `<- memblock for ID2 must have size of ID1, and ID2 needn't exist, so ID1 is taken here instead
rem For all pixels: R/G/B = Average of R,G,B
x = 0 : y = 1
for pos = 12 to de_img_Size step 4
inc x : if x > de_img_SizeX then x = 1 : inc y
rem Comparison-Pixels
NewR = de_img_Byte((memblock byte(M1,pos+2)-127.5)*f#+127.5)
NewG = de_img_Byte((memblock byte(M1,pos+1)-127.5)*f#+127.5)
NewB = de_img_Byte((memblock byte(M1,pos )-127.5)*f#+127.5)
rem Write Result to result-memblock
write memblock byte M2, pos , NewB
write memblock byte M2, pos+1, NewG
write memblock byte M2, pos+2, NewR
next pos
rem Update image
de_img_UpdateImage(ID2,M2)
rem Delete memblocks
de_img_DeleteMemblock(M1)
de_img_DeleteMemblock(M2)
endfunction
rem ******** Generation of images ********
remstart
function ________ Generation ________
remend
rem The Color-Values must include their alpha-value!!! (Using the rgb()-Function alpha is always 255 -> no transparency)
function de_img_MakeHUDArea(ID1,SX,SY,C_Main,C_Light,C_Dark)
rem Define used memblock
if de_Img_UseMemblocks=0 then Mem=de_img_MakeMemblock(SX,SY) else Mem=ID1
de_img_UpdateSize(Mem)
rem Draw image into memblock
rem 1. Light border
for x = 1 to SX
write memblock dword Mem, de_img_GetMemPos(x,1), C_Dark
next x
for y = 2 to SY
write memblock dword Mem, de_img_GetMemPos(1,y), C_Dark
next y
rem 2. Dark border
for x = 2 to SX
write memblock dword Mem, de_img_GetMemPos(x,SY), C_Light
next x
for y = 2 to SY
write memblock dword Mem, de_img_GetMemPos(SX,y), C_Light
next y
rem 3. Main-Area
for x = 2 to SX-1
for y = 2 to SY-1
write memblock dword Mem, de_img_GetMemPos(x,y), C_Main
next y
next x
rem Update image
de_img_UpdateImage(Id1,Mem)
rem Delete memblocks
de_img_DeleteMemblock(Mem)
endfunction
rem The Color-Values must include their alpha-value!!! (Using the rgb()-Function alpha is always 255 -> no transparency)
function de_img_MakeCirclePart(ID1,Size,Col as dword,A1#,A2#)
rem Define used memblock
if de_Img_UseMemblocks=0 then Mem=de_img_MakeMemblock(Size,Size) else Mem=ID1
de_img_UpdateSize(Mem)
rem Define used variables
centerx = Size/2
centery = Size/2
SqrSize = (Size/2)^2
rem Draw image into memblock
rem 1. Light border
for x = 1 to Size
for y = 1 to Size
rem Check distance
xdis = (x-centerx)
ydis = (centery-y)
dis = xdis^2+ydis^2
if dis < SqrSize
rem Check Angle
angle# = wrapvalue(atanfull(xdis,ydis))
if (angle#>A1# and angle#<A2#) or (angle#<A1# and angle#>A2#)
write memblock dword Mem, de_img_GetMemPos(x,y), Col
endif
endif
next y
next x
rem Update image
de_img_UpdateImage(Id1,Mem)
rem Delete memblocks
de_img_DeleteMemblock(Mem)
endfunction
function de_img_MakeSun(ID1,Size,InnerSize,Col as dword)
rem Define used memblock
if de_Img_UseMemblocks=0 then Mem=de_img_MakeMemblock(Size,Size) else Mem=ID1
de_img_UpdateSize(Mem)
rem Define used variables
centerx = Size/2
centery = Size/2
SqrSize = (Size/2)^2
SqrInner = (InnerSize/2)^2
SizeDis = SqrSize-SqrInner
Alpha as dword
Alpha = Col*(1.0/(65536*256))
rem Draw image into memblock
rem 1. Light border
for x = 1 to Size
for y = 1 to Size
rem Check distance
xdis = (x-centerx)
ydis = (centery-y)
dis = xdis^2+ydis^2
if dis < SqrSize
if dis < SqrInner `Middle of sun
write memblock dword Mem, de_img_GetMemPos(x,y), Col
else `Fading out sun-border
pos = de_img_GetMemPos(x,y)
write memblock dword Mem, pos, Col
af# = 1-(dis-SqrInner)/(1.0*SizeDis)
write memblock byte Mem, pos+3, Alpha*af#
endif
endif
next y
next x
rem Update image
de_img_UpdateImage(Id1,Mem)
rem Delete memblocks
de_img_DeleteMemblock(Mem)
endfunction
function de_img_MakeWindowsButton(ID1,SX,SY)
C_Main = rgb(236,233,216)
C_Light = rgb(255,255,255)
C_Light2 = rgb(241,239,226)
C_Dark = rgb(113,111,100)
C_Dark2 = rgb(172,168,153)
rem Define used memblock
if de_Img_UseMemblocks=0 then Mem=de_img_MakeMemblock(SX,SY) else Mem=ID1
de_img_UpdateSize(Mem)
rem Draw image into memblock
rem 1. Light border
for x = 1 to SX-1
write memblock dword Mem, de_img_GetMemPos(x,1), C_Light
next x
for y = 2 to SY-1
write memblock dword Mem, de_img_GetMemPos(1,y), C_Light
next y
rem 2. Light border
for x = 2 to SX-2
write memblock dword Mem, de_img_GetMemPos(x,2), C_Light2
next x
for y = 2 to SY-2
write memblock dword Mem, de_img_GetMemPos(2,y), C_Light2
next y
rem 1. Dark border
for x = 1 to SX
write memblock dword Mem, de_img_GetMemPos(x,SY), C_Dark
next x
for y = 1 to SY
write memblock dword Mem, de_img_GetMemPos(SX,y), C_Dark
next y
rem 2. Dark border
for x = 2 to SX-1
write memblock dword Mem, de_img_GetMemPos(x,SY-1), C_Dark2
next x
for y = 2 to SY-1
write memblock dword Mem, de_img_GetMemPos(SX-1,y), C_Dark2
next y
rem 3. Main-Area
for x = 3 to SX-2
for y = 3 to SY-2
write memblock dword Mem, de_img_GetMemPos(x,y), C_Main
next y
next x
rem Update image
de_img_UpdateImage(Id1,Mem)
rem Delete memblocks
de_img_DeleteMemblock(Mem)
endfunction
function de_img_MakeAACircle(ID1,Size,Col as dword)
rem Define used memblock
if de_Img_UseMemblocks=0 then M1=de_img_MakeMemblock(Size,Size) else M1=ID1
de_img_UpdateSize(M1)
rem Define used variables
centerx = Size/2
centery = Size/2
Radius = Size/2 -1
SqrSize = Radius^2`(Size/2)^2
SqrInner = (InnerSize/2)^2
SizeDis = SqrSize-SqrInner
Alpha as dword
Alpha = Col*(1.0/(65536*256))
rem Draw image into memblock
rem 1. Light border
for x = 1 to Size
for y = 1 to Size
rem Check distance
xdis = (x-centerx)
ydis = (centery-y)
dis1 = (abs(xdis)-2)^2 + (abs(ydis)-2)^2 `(xdis-2)*(xids-2)+(ydis-2)*(ydis-2)
dis2 = (abs(xdis)+2)^2 + (abs(ydis)+2)^2 `(xdis+2)*(xdis+2)+(ydis+2)*(ydis+2)
if dis1 < SqrSize
if dis2 > SqrSize
dis# = sqrt(xdis*xdis+ydis*ydis)
dif# = abs(dis#-Radius)
if dif#<1
af# = 1.0 - dif#
pos = de_img_GetMemPos(x,y)
write memblock dword M1, pos, Col
write memblock byte M1, pos+3, Alpha*af#
endif
endif
endif
next y
next x
rem Update image
de_img_UpdateImage(Id1,M1)
rem Delete memblocks
de_img_DeleteMemblock(M1)
endfunction
rem FreqX/Y: How much height-points are set in x- and y-direction
function de_img_MakeHeightmap(ID1,SizeX,SizeY,FreqX,FreqY,Amplitude,Layers,LayerFactor#)
rem Define used memblock
if de_Img_UseMemblocks=0 then M1=de_img_MakeMemblock(SizeX,SizeY) else M1=ID1
de_img_UpdateSize(M1)
rem --- Heightmap-Creation ---
rem Pixel-Array
dim de_img_Height(SizeX,SizeY) as float
F# = 1.0
rem For each layer
for l = 1 to Layers
rem Not the first layer: Change properties
if l<>1
rem Change factor
F# = F#*LayerFactor#
rem Change frequency
FreqX = FreqX/LayerFactor#
FreqY = FreqY/LayerFactor#
endif
rem Variables: Pixels per hill
PixelsX# = SizeX/(1.0*FreqX)
PixelsY# = SizeY/(1.0*FreqY)
rem Create hill-array
dim de_img_Hill(FreqX+1,FreqY+1) as byte
rem Assign Hill-Height-Values
for hillx = 1 to FreqX+1
for hilly = 1 to FreqY+1
de_img_Hill(hillx,hilly) = 128 + de_img_PseudoRnd(Amplitude) - Amplitude*0.5 `de_img_PseudoRnd(255)
next hilly
next hillx
rem Hills at top left border get values of bottom right border -> seamless terrain :)
for hx = 0 to FreqX
de_img_Hill(hx,0) = de_img_Hill(hx,FreqY+0)
next hx
for hy = 0 to FreqY
de_img_Hill(0,hy) = de_img_Hill(FreqX+0,hy)
next hy
rem Add Heights depending on hills and properties
for x = 1 to SizeX
for y = 1 to SizeY
rem Relative position between 2 Hills
if escapekey() then end
rem Get Hills
HillX = x/PixelsX#
HillY = y/PixelsY#
rem Get hor/ver factor
HillStartX# = (HillX*PixelsX#)
HillStartY# = (HillY*PixelsY#)
f_v# = (x-HillStartX#)/(PixelsX#*1.0)
f_h# = (y-HillStartY#)/(PixelsY#*1.0)
rem Interpolate
`f_v# = de_img_interpolate(0,1,f_v#)
`f_h# = de_img_interpolate(0,1,f_h#)
rem Get Corner-Factors: top-left, top-right, bottom-right, bottom-left
f_c1# = 1 - f_v# * f_h#
f_c2# = 1 - (1-f_v#)* f_h#
f_c3# = 1 - (1-f_v#)*(1-f_h#)
f_c4# = 1 - f_v# *(1-f_h#)
rem Get height
v1# = de_img_Hill(HillX,HillY )*(1-f_v#) + de_img_Hill(HillX+1,HillY )*f_v#
v2# = de_img_Hill(HillX,HillY+1)*(1-f_v#) + de_img_Hill(HillX+1,HillY+1)*f_v#
add# = v1# * (1-f_h#) + v2# * f_h#
`add# = f_c1#*de_img_Hill(HillX,HillY) + f_c2#*de_img_Hill(HillX+1,HillY) + f_c3#*de_img_Hill(HillX+1,HillY+1) + f_c4#*de_img_Hill(HillX,HillY+1)
if add# > 255 then add# = 255 else if add# < 0 then add# = 0
if l <> 1
add# = (add# - 127.5)*F# `<- F# is always mutliplied with layerfactor#
endif
inc de_img_Height(x,y), add#
next y
next x
next l
rem --- End ---
rem Write to memblock
for x = 1 to SizeX
for y = 1 to SizeY
pos = de_img_GetMemPos(x,y)
h = de_img_Height(x,y)
if h > 255 then h = 255 else if h < 0 then h = 0
write memblock byte M1, pos , h
write memblock byte M1, pos+1, h
write memblock byte M1, pos+2, h
write memblock byte M1, pos+3, 255
next y
next x
rem Update image
de_img_UpdateImage(Id1,M1)
rem Delete memblocks
de_img_DeleteMemblock(M1)
rem Delete arrays
undim de_img_Hill()
undim de_img_Height()
endfunction
function de_img_CreateStarSky(ID1,SizeX,SizeY,Stars,MinSize,MaxSize)
rem Add memblock
if de_Img_UseMemblocks=0 then M1=de_img_MakeMemblock(SizeX,SizeY) else M1=ID1
de_img_UpdateSize(M1)
difsize = MaxSize-MinSize
rem Add stars/suns
for s = 1 to Stars
col = 100+rnd(100)
r = col + rnd(55)
b = col + rnd(55)
g = col
c = rgb(r,g,b)
x = rnd(SizeX)
y = rnd(SizeY)
size = de_img_SqrReduce(minsize+rnd(difsize),maxsize,3) `<- value, maxvalue, calculation-depth
de_img_DrawSun(M1,x,y,size,c,1)
next s
rem Update image
de_img_UpdateImage(ID1,M1)
rem Delete memblocks
de_img_DeleteMemblock(M1)
endfunction
function de_img_DrawSun(Mem,starX,starY,Size,Col as dword,Seamless as boolean)
xs = starx - size
ys = stary - size
xe = starx + size
ye = stary + size
if seamless = 0
if xs < 1 then xs = 1
if ys < 1 then ys = 1
if xe > de_img_SizeX then xe = de_img_SizeX
if ye > de_img_SizeY then ye = de_img_SizeY
endif
cr = rgbr(col)
cg = rgbg(col)
cb = rgbb(col)
ca = 255`de_img_GetAlpha(col)
f# = 1.0/(1.0*size*size)
for x = xs to xe
for y = ys to ye
if seamless=1
xp = de_img_mod(x,de_img_SizeX)
yp = de_img_mod(y,de_img_SizeY)
else
xp = x
yp = y
endif
xdis = starx-x
ydis = stary-y
dis = xdis*xdis + ydis*ydis
`dis = de_img_SqrReduce(dis,Size,5)
l# = (size-sqrt(dis))^2*f#
pos = de_img_GetMemPos(xp,yp)
`af# = de_img_Byte(ca*l#)/255.0
ar = de_img_MidVal(memblock byte(mem, pos+2),cr,l#)
ag = de_img_MidVal(memblock byte(mem, pos+1),cg,l#)
ab = de_img_MidVal(memblock byte(mem, pos ),cb,l#)
aa = de_img_MidVal(memblock byte(mem, pos+3),ca,l#)
write memblock byte mem, pos , ab
write memblock byte mem, pos+1, ag
write memblock byte mem, pos+2, ar
write memblock byte mem, pos+3, aa
next y
next x
endfunction
function de_img_CreateMandelbrot(ID1,SX,SY,viewx1 as float, viewy1 as float, viewxdis as float, viewydis as float)
rem Define used memblock
if de_Img_UseMemblocks=0 then Mem=de_img_MakeMemblock(SX,SY) else Mem=ID1
de_img_UpdateSize(Mem)
rem Draw image into memblock
rem Position-Iteration-Variables (for-next)
x as float
y as float
vx as float
vy as float
rem Camera-Position
xfrom# = 1
yfrom# = 1
xto# = sx
yto# = sy
imagedisx# = xto#-xfrom#
imagedisy# = yto#-yfrom#
rem Complex whatever-values
xb1# = -2.5
xb2# = 2.5
yb1# = -2
yb2# = 2
xbd# = xb2#-xb1#
ybd# = yb2#-yb1#
rem Maximum iterations
maxIts = 256
Its as integer
rem Loop for all pixels
for x = xfrom# to xto#
print (100*x)/imagedisx# : sync
for y = yfrom# to yto#
rem Calculate rel pos
vx = x`437.2+400*0.00003+x*0.000003`viewx1 + viewxdis*(x-xfrom#)/imagedisx#
vy = y`377.7+300*0.00003+y*0.000003`viewy1 + viewydis*(y-yfrom#)/imagedisy#
rem Calculate factors
xf# = xb1# + (vx*xbd#)/sx
yf# = yb1# + (vy*ybd#)/sy
its = 0
a# = 0
b# = 0
repeat
inc its
at# = xf# + a#*a# - b#*b#
bt# = yf# + 2*a#*b#
a# = at#
b# = bt#
until (a#^2 + b#^2 > 4) or (its = MaxIts)
r = 256-sqrt(its)*15`(its*36 mod 256)
g = (its* 8 mod 256)
b = (its* 3 mod 256)
position = de_img_GetMemPos(x,y)
write memblock byte Mem, position , b
write memblock byte Mem, position+1, g
write memblock byte Mem, position+2, r
next y
sync
next x
rem Update image
de_img_UpdateImage(Id1,Mem)
rem Delete memblocks
de_img_DeleteMemblock(Mem)
endfunction
rem ******** General image-functions ********
remstart
function ________ General ________
remend
function de_img_SizeImage(ID1,ID2,SX,SY)
rem Create memblock if neccessary and get x and y-size
rem ID1 and ID2 just store memblock-IDs!
M1 = de_img_MemFromImg(ID1)
M2 = de_img_MakeMemblock(SX,SY)
rem Variables
xf# = (1.0*de_img_SizeX)/(1.0*SX)
yf# = (1.0*de_img_SizeY)/(1.0*SY)
NewPos = 8
rem For all pixels
for y = 1 to SX
for x = 1 to SY
inc NewPos, 4
xp# = (x*de_img_SizeX)/(1.0*SX)
yp# = (y*de_img_SizeY)/(1.0*SY)
pos = de_img_GetMemPos(xp#,yp#)
r = memblock byte(M1,pos+2)
g = memblock byte(M1,pos+1)
b = memblock byte(M1,pos )
a = memblock byte(M1,pos+3)
write memblock byte M2, Newpos+2, r
write memblock byte M2, Newpos+1, g
write memblock byte M2, Newpos , b
write memblock byte M2, NewPos+3, a
next y
next x
rem Update image
de_img_UpdateImage(ID2,M2)
rem Delete memblocks
de_img_DeleteMemblock(M1)
de_img_DeleteMemblock(M2)
endfunction
rem ******** Internal functions ********
function de_img_UpdateImage(I,M)
if de_img_UseMemblocks=0
make image from memblock I, M
endif
endfunction
function de_img_DeleteMemblock(M)
if de_img_UseMemblocks=0
if memblock exist(M)
delete memblock M
endif
endif
endfunction
function de_img_MemFromImg(ID)
if de_img_UseMemblocks=0
Mem = de_img_FreeMemblock()
make memblock from image Mem, ID
else
Mem = ID
endif
de_img_UpdateSize(Mem)
endfunction Mem
function de_img_MakeMemblock(SX,SY)
sz = 12 + 4*SX*SY
Mem = de_img_FreeMemblock()
make memblock Mem, sz
write memblock dword Mem, 0, SX
write memblock dword Mem, 4, SY
write memblock dword Mem, 8, 32
endfunction Mem
function de_img_UpdateSize(Mem)
de_img_Size = get memblock size(Mem)-1
de_img_SizeX = memblock dword(Mem,0)
de_img_SizeY = memblock dword(Mem,4)
endfunction
function de_img_GetMemPos(x,y)
pos = 12 + 4 * ((x-1) + (y-1)*de_img_SizeX)
endfunction pos
function de_img_mod(v,max)
dec v
if v < 0
v = max - (abs(v) mod max)
endif
v = (v mod max)
inc v
endfunction v
function de_img_FreeMemblock()
ID = de_img_FirstMemblock
repeat
inc ID
until memblock exist(ID)=0
endfunction ID
function de_img_Byte(V)
if V > 255 then V = 255 else if V < 0 then V = 0
endfunction V
function de_img_interpolate(V1 as float,V2 as float,f#)
result# = V1*((cos(180.0*f#)+1)/2.0) + V2*(1-(cos(180.0*f#)+1)/2.0)
endfunction result#
function de_img_PseudoRnd(Max)
R = rnd(max)
endfunction R
function de_img_SqrReduce(V#,Max#,Depth)
f# = V#/Max#
for d = 1 to Depth
V# = V#*f#
next d
endfunction V#
function de_img_GetAlpha(Col as dword)
Alpha as dword
Alpha = Col*(1.0/(65536.0*256.0))
Alpha = de_img_Byte(Alpha)
endfunction Alpha
function de_img_MidVal(v1,v2,p#)
v# = (1-p#)*v1 + p#*v2
endfunction v#