REM ***********************************************
REM Title: Image Library
REM Author: Kenmo
REM Downloaded from: http://dbcc.zimnox.com/
REM ***********************************************
 
` DBPro Image Altering Example/Functions - kenmo, 05.21.2004
 
yourimage$="c:\whatever.bmp"
cls : hide mouse : sync on : sync : load image yourimage$,1,1
text 0,0,"DBC Image Altering Library by Toukatly (kenmo)" : sync : wait key
cls : paste image 1,0,0 : text 0,0, "Normal Image" : sync : wait key
cls : flip_image(1,2) : paste image 2,0,0 : text 0,0,"Flipped" : sync : wait key
cls : mirror_image(1,2) : paste image 2,0,0 : text 0,0,"Mirrored" : sync : wait key
cls : invert_image(1,2) : paste image 2,0,0 : text 0,0,"Inverted" : sync : wait key
cls : brighten_image(1,2,200) : paste image 2,0,0 : text 0,0,"200% brightness" : sync : wait key
cls : brighten_image(1,2,25) : paste image 2,0,0 : text 0,0,"25% brightness" : sync : wait key
cls : greyscale_image(1,2,100) : paste image 2,0,0 : text 0,0,"100% greyscale" : sync : wait key
cls : colorize_image(1,2,rgb(255,128,0),75) : paste image 2,0,0 : text 0,0,"75% colorized orange" : sync : wait key
cls : text 0,0,"THE END!" : sync : wait key
delete image 1 : delete image 2 : end
 
` DBC Image Altering Function Library, by Toukatly (kenmo)
` Simply copy these functions into your program (or save and include this file)
` and the rest is pretty obvious.
` Function return values:
` 1: Function was successful
` 0: Function hit the end (should never occur)
` -1: Source image does not exist
` -2: No free memblock(s)
` -3: Width or Height or Depth is 0 (??? should not occur)
` If you use these, feel free to give me credit, though none is needed.
` Toukatly (kenmo) 05.20-21.2004
 
function colorize_image(source_image,destination_image,color,percent)
   if image exist(source_image)=0 : exitfunction -1 : endif
   tmp=1 : while tmp<128 and memblock exist(tmp)=1 : tmp=tmp+1 : endwhile
   if tmp=128 : exitfunction -2 : endif : sm=tmp
   tmp=tmp+1 : while tmp<128 and memblock exist(tmp)=1 : tmp=tmp+1 : endwhile
   if tmp=128 : exitfunction -2 : endif : dm=tmp
   make memblock from image sm,source_image : make memblock from image dm,source_image
   colorr=rgbr(color) : colorg=rgbg(color) : colorb=rgbb(color)
   if percent>100 then percent=100
   if percent<=0 then make image from memblock destination_image,sm : delete memblock sm : delete memblock dm : exitfunction 1
   width=memblock dword(sm,0) : height=memblock dword(sm,4) : depth=memblock dword(sm,8)
   if width=0 or height=0 or depth=0 : delete memblock sm : delete memblock dm : exitfunction -3 : endif
   if depth=2 or depth=16
      for tmpy=0 to height-1 : for tmpx=0 to width-1
         slot=12+(tmpy*width+tmpx)*2 : tmp=memblock word(sm,slot)
         tmpr=((tmp/2048)&&31)*8
         tmpg=((tmp/32)&&63)*4
         tmpb=(tmp&&31)*8
         final=tmpr*0.3+tmpg*0.59+tmpb*.11
         final=rgb(((final*colorr/255.0)*percent+tmpr*(100-percent))/100,((final*colorg/255.0)*percent+tmpg*(100-percent))/100,((final*colorb/255.0)*percent+tmpb*(100-percent))/100)
         final=((RGBR(final)&&248)*256)+((RGBG(final)&&252)*8)+((RGBB(final)/8))
         write memblock word dm,slot,final
      next tmpx : next tmpy
   endif
   if depth=3 or depth=24
      for tmpy=0 to height-1 : for tmpx=0 to width-1
         slot=(tmpy*width+tmpx)*3 : tmpr=memblock byte(sm,12+slot) : tmpg=memblock byte(sm,13+slot) : tmpb=memblock byte(sm,14+slot)
         final=tmpr*0.3+tmpg*0.59+tmpb*0.11
         r=((final*colorr/255.0)*percent+tmpr*(100-percent))/100
         g=((final*colorg/255.0)*percent+tmpg*(100-percent))/100
         b=((final*colorb/255.0)*percent+tmpb*(100-percent))/100
         write memblock byte dm,12+slot,r : write memblock byte dm,13+slot,g : write memblock byte dm,14+slot,b
      next tmpx : next tmpy
   endif
   if depth=4 or depth=32
      for tmpy=0 to height-1 : for tmpx=0 to width-1
         slot=12+(tmpy*width+tmpx)*4 : tmprgb=memblock dword(sm,slot)
         tmpr=rgbr(tmprgb) : tmpg=rgbg(tmprgb) : tmpb=rgbb(tmprgb)
         final=tmpr*0.3+tmpg*0.59+tmpb*0.11
         final=rgb(((final*colorr/255.0)*percent+tmpr*(100-percent))/100,((final*colorg/255.0)*percent+tmpg*(100-percent))/100,((final*colorb/255.0)*percent+tmpb*(100-percent))/100)
         write memblock dword dm,slot,final
      next tmpx : next tmpy
   endif
   make image from memblock destination_image,dm : delete memblock sm : delete memblock dm : exitfunction 1
endfunction 0
 
function flip_image(source_image,destination_image)
   if image exist(source_image)=0 : exitfunction -1 : endif
   tmp=1 : while tmp<128 and memblock exist(tmp)=1 : tmp=tmp+1 : endwhile
   if tmp=128 : exitfunction -2 : endif : sm=tmp
   tmp=tmp+1 : while tmp<128 and memblock exist(tmp)=1 : tmp=tmp+1 : endwhile
   if tmp=128 : exitfunction -2 : endif : dm=tmp
   make memblock from image sm,source_image : make memblock from image dm,source_image
   width=memblock dword(sm,0) : height=memblock dword(sm,4) : depth=memblock dword(sm,8)
   if width=0 or height=0 or depth=0 : delete memblock sm : delete memblock dm : exitfunction -3 : endif
   if depth=2 or depth=16
      for tmpy=0 to height-1 : for tmpx=0 to width-1
         old=12+(tmpy*width+tmpx)*2 : new=12+((height-1-tmpy)*width+tmpx)*2
         write memblock word dm,new,memblock word(sm,old)
      next tmpx : next tmpy
   endif
   if depth=3 or depth=24
         for tmpy=0 to height-1 : for tmpx=0 to width-1
         old=(tmpy*width+tmpx)*3 : new=((height-1-tmpy)*width+tmpx)*2
         for tmp=12 to 14 : write memblock byte dm,tmp+new,memblock byte(sm,tmp+old) : next tmp
      next tmpx : next tmpy
   endif
   if depth=4 or depth=32
      for tmpy=0 to height-1 : for tmpx=0 to width-1
         old=12+(tmpy*width+tmpx)*4 : new=12+((height-1-tmpy)*width+tmpx)*4
         write memblock dword dm,new,memblock dword(sm,old)
      next tmpx : next tmpy
   endif
   make image from memblock destination_image,dm : delete memblock sm : delete memblock dm : exitfunction 1
endfunction 0
 
function mirror_image(source_image,destination_image)
   if image exist(source_image)=0 : exitfunction -1 : endif
   tmp=1 : while tmp<128 and memblock exist(tmp)=1 : tmp=tmp+1 : endwhile
   if tmp=128 : exitfunction -2 : endif : sm=tmp
   tmp=tmp+1 : while tmp<128 and memblock exist(tmp)=1 : tmp=tmp+1 : endwhile
   if tmp=128 : exitfunction -2 : endif : dm=tmp
   make memblock from image sm,source_image : make memblock from image dm,source_image
   width=memblock dword(sm,0) : height=memblock dword(sm,4) : depth=memblock dword(sm,8)
   if width=0 or height=0 or depth=0 : delete memblock sm : delete memblock dm : exitfunction -3 : endif
   if depth=2 or depth=16
      for tmpy=0 to height-1 : for tmpx=0 to width-1
         old=12+(tmpy*width+tmpx)*2 : new=12+(tmpy*width+(width-1-tmpx))*2
         write memblock word dm,new,memblock word(sm,old)
      next tmpx : next tmpy
   endif
   if depth=3 or depth=24
         for tmpy=0 to height-1 : for tmpx=0 to width-1
         old=(tmpy*width+tmpx)*3 : new=(tmpy*width+(width-1-tmpx))*2
         for tmp=12 to 14 : write memblock byte dm,tmp+new,memblock byte(sm,tmp+old) : next tmp
      next tmpx : next tmpy
   endif
   if depth=4 or depth=32
      for tmpy=0 to height-1 : for tmpx=0 to width-1
         old=12+(tmpy*width+tmpx)*4 : new=12+(tmpy*width+(width-1-tmpx))*4
         write memblock dword dm,new,memblock dword(sm,old)
      next tmpx : next tmpy
   endif
   make image from memblock destination_image,dm : delete memblock sm : delete memblock dm : exitfunction 1
endfunction 0
 
function greyscale_image(source_image,destination_image,percent)
   if image exist(source_image)=0 : exitfunction -1 : endif
   tmp=1 : while tmp<128 and memblock exist(tmp)=1 : tmp=tmp+1 : endwhile
   if tmp=128 : exitfunction -2 : endif : sm=tmp
   tmp=tmp+1 : while tmp<128 and memblock exist(tmp)=1 : tmp=tmp+1 : endwhile
   if tmp=128 : exitfunction -2 : endif : dm=tmp
   make memblock from image sm,source_image : make memblock from image dm,source_image
   if percent>100 then percent=100
   if percent<=0 then make image from memblock destination_image,sm : delete memblock sm : delete memblock dm : exitfunction 1
   width=memblock dword(sm,0) : height=memblock dword(sm,4) : depth=memblock dword(sm,8)
   if width=0 or height=0 or depth=0 : delete memblock sm : delete memblock dm : exitfunction -3 : endif
   if depth=2 or depth=16
      for tmpy=0 to height-1 : for tmpx=0 to width-1
         slot=12+(tmpy*width+tmpx)*2 : tmp=memblock word(sm,slot)
         tmpr=((tmp/2048)&&31)*8
         tmpg=((tmp/32)&&63)*4
         tmpb=(tmp&&31)*8
         final=tmpr*0.3+tmpg*0.59+tmpb*.11
         final=rgb((final*percent+tmpr*(100-percent))/100,(final*percent+tmpg*(100-percent))/100,(final*percent+tmpb*(100-percent))/100)
         final=((RGBR(final)&&248)*256)+((RGBG(final)&&252)*8)+((RGBB(final)/8))
         write memblock word dm,slot,final
      next tmpx : next tmpy
   endif
   if depth=3 or depth=24
      for tmpy=0 to height-1 : for tmpx=0 to width-1
         slot=(tmpy*width+tmpx)*3 : tmpr=memblock byte(sm,12+slot) : tmpg=memblock byte(sm,13+slot) : tmpb=memblock byte(sm,14+slot)
         final=tmpr*0.3+tmpg*0.59+tmpb*0.11
         r=(final*percent+tmpr*(100-percent))/100
         g=(final*percent+tmpg*(100-percent))/100
         b=(final*percent+tmpb*(100-percent))/100
         write memblock byte dm,12+slot,r : write memblock byte dm,13+slot,g : write memblock byte dm,14+slot,b
      next tmpx : next tmpy
   endif
   if depth=4 or depth=32
      for tmpy=0 to height-1 : for tmpx=0 to width-1
         slot=12+(tmpy*width+tmpx)*4
         tmprgb=memblock dword(sm,slot)
         tmpr=rgbr(tmprgb) : tmpg=rgbg(tmprgb) : tmpb=rgbb(tmprgb)
         final=tmpr*0.3+tmpg*0.59+tmpb*0.11
         final=rgb((final*percent+tmpr*(100-percent))/100,(final*percent+tmpg*(100-percent))/100,(final*percent+tmpb*(100-percent))/100)
         write memblock dword dm,slot,final
      next tmpx : next tmpy
   endif
   make image from memblock destination_image,dm : delete memblock sm : delete memblock dm : exitfunction 1
endfunction 0
 
function invert_image(source_image,destination_image)
   if image exist(source_image)=0 : exitfunction -1 : endif
   tmp=1 : while tmp<128 and memblock exist(tmp)=1 : tmp=tmp+1 : endwhile
   if tmp=128 : exitfunction -2 : endif : sm=tmp
   tmp=tmp+1 : while tmp<128 and memblock exist(tmp)=1 : tmp=tmp+1 : endwhile
   if tmp=128 : exitfunction -2 : endif : dm=tmp
   make memblock from image sm,source_image : make memblock from image dm,source_image
   width=memblock dword(sm,0) : height=memblock dword(sm,4) : depth=memblock dword(sm,8)
   if width=0 or height=0 or depth=0 : delete memblock sm : delete memblock dm : exitfunction -3 : endif
   if depth=2 or depth=16
      for tmpy=0 to height-1 : for tmpx=0 to width-1
         slot=12+(tmpy*width+tmpx)*2 : tmp=memblock word(sm,slot)
         tmpr=((tmp/2048)&&31)*8
         tmpg=((tmp/32)&&63)*4
         tmpb=(tmp&&31)*8
         r=255-tmpr : g=255-tmpg : b=255-tmpb
         final=rgb(r,g,b) : final=((RGBR(final)&&248)*256)+((RGBG(final)&&252)*8)+((RGBB(final)/8))
         write memblock word dm,slot,final
      next tmpx : next tmpy
   endif
   if depth=3 or depth=24
      for tmpy=0 to height-1 : for tmpx=0 to width-1
         slot=(tmpy*width+tmpx)*3 : tmpr=memblock byte(sm,12+slot) : tmpg=memblock byte(sm,13+slot) : tmpb=memblock byte(sm,14+slot)
         r=255-tmpr : g=255-tmpg : b=255-tmpb
         write memblock byte dm,12+slot,r : write memblock byte dm,13+slot,g : write memblock byte dm,14+slot,b
      next tmpx : next tmpy
   endif
   if depth=4 or depth=32
      for tmpy=0 to height-1 : for tmpx=0 to width-1
         slot=12+(tmpy*width+tmpx)*4
         tmprgb=memblock dword(sm,slot)
         tmpr=rgbr(tmprgb) : tmpg=rgbg(tmprgb) : tmpb=rgbb(tmprgb) : max=1
         r=255-tmpr : g=255-tmpg : b=255-tmpb
         final=rgb(r,g,b)
         write memblock dword dm,slot,final
      next tmpx : next tmpy
   endif
   make image from memblock destination_image,dm : delete memblock sm : delete memblock dm : exitfunction 1
endfunction 0
 
function brighten_image(source_image,destination_image,percent)
   if image exist(source_image)=0 : exitfunction -1 : endif
   tmp=1 : while tmp<128 and memblock exist(tmp)=1 : tmp=tmp+1 : endwhile
   if tmp=128 : exitfunction -2 : endif : sm=tmp
   tmp=tmp+1 : while tmp<128 and memblock exist(tmp)=1 : tmp=tmp+1 : endwhile
   if tmp=128 : exitfunction -2 : endif : dm=tmp
   make memblock from image sm,source_image : make memblock from image dm,source_image
   if percent=100 then make image from memblock destination_image,sm : delete memblock sm : delete memblock dm : exitfunction 1
   width=memblock dword(sm,0) : height=memblock dword(sm,4) : depth=memblock dword(sm,8)
   if width=0 or height=0 or depth=0 : delete memblock sm : delete memblock dm : exitfunction -3 : endif
   if depth=2 or depth=16
      for tmpy=0 to height-1 : for tmpx=0 to width-1
         slot=12+(tmpy*width+tmpx)*2 : tmp=memblock word(sm,slot)
         r=((tmp/2048)&&31)*8
         g=((tmp/32)&&63)*4
         b=(tmp&&31)*8
         r=r*percent/100.0 : if r>255 then r=255
         g=g*percent/100.0 : if g>255 then g=255
         b=b*percent/100.0 : if b>255 then b=255
         final=rgb(r,g,b) : final=((RGBR(final)&&248)*256)+((RGBG(final)&&252)*8)+((RGBB(final)/8)) : write memblock word dm,slot,final
      next tmpx : next tmpy
   endif
   if depth=3 or depth=24
      for tmpy=0 to height-1 : for tmpx=0 to width-1
         slot=(tmpy*width+tmpx)*3 : r=memblock byte(sm,12+slot) : g=memblock byte(sm,13+slot) : b=memblock byte(sm,14+slot)
         r=r*percent/100.0 : if r>255 then r=255
         g=g*percent/100.0 : if g>255 then g=255
         b=b*percent/100.0 : if b>255 then b=255
         write memblock byte dm,12+slot,r : write memblock byte dm,13+slot,g : write memblock byte dm,14+slot,b
      next tmpx : next tmpy
   endif
   if depth=4 or depth=32
      for tmpy=0 to height-1 : for tmpx=0 to width-1
         slot=12+(tmpy*width+tmpx)*4 : tmp=memblock dword(sm,slot)
         r=rgbr(tmp) : g=rgbg(tmp) : b=rgbb(tmp)
         r=r*percent/100.0 : if r>255 then r=255
         g=g*percent/100.0 : if g>255 then g=255
         b=b*percent/100.0 : if b>255 then b=255
         final=rgb(r,g,b) : write memblock dword dm,slot,final
      next tmpx : next tmpy
   endif
   make image from memblock destination_image,dm : delete memblock sm : delete memblock dm : exitfunction 1
endfunction 0