Through a simple equation as used by Photoshop, now you can use the following code to create all sorts of image filters without the need for specific code for each one. If you used Photoshop before, then perhaps you've seen the custom filter dialog which displays a 5x5 grid of numbers. The link below explains it and the code implements it.
http://ian-albert.com/graphics/customfilters.php
load image "H:\Programming\Dark Basic\DBPro Source\michelangelo57.jpg", 1
make memblock from image 1, 1
make memblock from image 2, 1
rem get image data
imageWidth = memblock dword(1, 0)
imageHeight = memblock dword(1, 4)
imageDepth = memblock dword(1, 8)
memSize = get memblock size(1)
rem filter matrix
dim filter(5,5)
restore Emboss
for x = 1 to 5
for y = 1 to 5
read z
filter(x, y) = z
next y
next x
read filterScale
read filterOffset
rem loop through every pixel in source image
for x = 1 to imageWidth
for y = 1 to imageHeight
rem variables used for storing the added weights
r0 = 0
g0 = 0
b0 = 0
rem apply filter
for i = -2 to 2
for j = -2 to 2
x3 = x+i
y3 = y+j
weight = filter(i+3, j+3)
rem cells with 0 weight in the filter are not calculated into filtering process
rem it would not influence the result in anyway, but it would be needless calculations
if weight <> 0
rem make sure to stay within image bounds when checking surrounding pixels
if x3 > 0 and x3 <= imageWidth and y3 > 0 and y3 <= imageHeight
rem extract the color components for this pixel
pos = ((y3-1)*imageWidth + x3-1)*4 + 12
b = memblock byte(1, pos)
g = memblock byte(1, pos+1)
r = memblock byte(1, pos+2)
rem multiple pixel by the filter weight
r0 = r0 + r*weight
g0 = g0 + g*weight
b0 = b0 + b*weight
endif
endif
next j
next i
rem divide the weighted total by the filter scale and add the filter offset
r0 = r0 / filterScale + filterOffset
g0 = g0 / filterScale + filterOffset
b0 = b0 / filterScale + filterOffset
rem set the filter pixel into memblock 2
pos = ((y-1)*imageWidth + x-1)*4 + 12
write memblock dword 2, pos, rgb(r0,g0,b0)
next y
next x
make image from memblock 2, 2
rem ======================
rem For demo purposes only
rem ======================
sync on
autocam off
backdrop on
position camera 0,0,0
make object plain 1, 300,300
position object 1, -160,0,400
texture object 1, 1
make object plain 2, 300,300
position object 2, 160,0,400
texture object 2,2
do
set cursor 0,0
print "FPS: ",screen fps()
sync
loop
rem ======================
rem End demo-only code
rem ======================
rem ======================
rem Filter data
rem ======================
Blur:
data 1, 1, 1, 1, 1
data 1, 1, 1, 1, 1
data 1, 1, 1, 1, 1
data 1, 1, 1, 1, 1
data 1, 1, 1, 1, 1
data 25,0
Gaussian:
data 1, 4, 6, 4, 1
data 4, 16, 24, 16, 4
data 6, 24, 36, 24, 6
data 4, 16, 24, 16, 4
data 1, 4, 6, 4, 1
data 256, 0
Sharpen:
data 0, 0, 0, 0, 0
data 0, 0,-1, 0, 0
data 0,-1, 5,-1, 0
data 0, 0,-1, 0, 0
data 0, 0, 0, 0, 0
data 1, 0
Emboss:
data 0, 0, 0, 0, 0
data 0, 0, 0, 0, 0
data 0, 0, 1, 0, 0
data 0, 0, 0,-1, 0
data 0, 0, 0, 0, 0
data 1, 128
Invert:
data 0, 0, 0, 0, 0
data 0, 0, 0, 0, 0
data 0, 0,-1, 0, 0
data 0, 0, 0, 0, 0
data 0, 0, 0, 0, 0
data 1, 255
rem increase scale to darken image
rem 100 = normal, 200 = half brightness, 300 = 1/3 brightness
Darken:
data 0, 0, 0, 0, 0
data 0, 0, 0, 0, 0
data 0, 0,100,0, 0
data 0, 0, 0, 0, 0
data 0, 0, 0, 0, 0
data 300,0
Example screen shot of the "emboss" filter
"Any sufficiently advanced technology is indistinguishable from magic" ~ Arthur C. Clarke