REM Project: Bitmap manipulation
REM Created: 1/28/2005 11:57:55 PM
REM
REM By CodingFodder
REM ***** Main Source File *****
REM
sync on:sync rate 0
set display mode 1280,1024,16
 
load bitmap "YourFavoriteBitmap.bmp",1
 
Save_Bitmap_For_Editing(1,0,1,0)
make_black_and_white(1)
`bitmap_brightness(1,50)
`resize_bitmap(1,100,100)
`clear_bitmap_from_editing(1)
set current bitmap 0
copy bitmap 1,0
angle=1
do
   i=wrapvalue(i+1)
   rotate_bitmap(1,i)
   make_black_and_white(1)
   bitmap_brightness(1,200)
   copy bitmap 1,0
sync
loop
 
function save_bitmap_for_editing(number,copy,block1,block2)
   if number=1
      create bitmap number+100,bitmap width(number),bitmap height(number)
      copy bitmap number,number+100
   else
      if bitmap exist(number+100) then delete bitmap number+100
   endif
   if memblock exist(100+number) then delete memblock 100+number
   if memblock exist(200+number) then delete memblock 200+number
   if block1=1 then make memblock from bitmap 100+number,number
   if block2=1 then make memblock from bitmap 200+number,number
endfunction
 
function Clear_bitmap_from_editing(number)
   if bitmap exist(number+100) then delete bitmap number+100
   if memblock exist(100+number) then delete memblock 100+number
   if memblock exist(200+number) then delete memblock 200+number
endfunction
 
function restore_bitmap(number)
   if bitmap exist(number+100)
      copy bitmap number+100,number
   else
      if memblock exist(100+number)
         delete bitmap number
         make bitmap from memblock number,100+number
      endif
   endif
endfunction
 
Function resize_bitmap(num,width,height)
   mem=num+100
   if memblock exist(mem)=0
      make memblock from bitmap mem,num
      none_set=1
   endif
   O_width=bitmap width(num)-1
   O_height=bitmap height(num)-1
   delete bitmap num
   create bitmap num,width,height
   set current bitmap num
   lock pixels
   for i=0 to bitmap width(num)-1
      for j=0 to bitmap height(num)-1
         color=get_memblock_pixel(mem,i*O_width/(bitmap width(num)-1),j*O_height/(bitmap height(num)-1))
         set_locked_pixel(i,j,color)
      next j
   next i
   unlock pixels
   set current bitmap 0
   if none_set=1 then delete memblock mem
endfunction
 
function scale_colors(num,red,green,blue)
set current bitmap num
   red2#=red/100.0
   green2#=green/100.0
   blue2#=blue/100.0
   lock pixels
   start=get pixels pointer()
   repeat_number=get pixels pitch()
   bits_per_pixel=bitmap depth(num)/8
   for i=0 to bitmap height(num)-1
      pointy=start+i*repeat_number
      for j=0 to bitmap width(num)-1
         pointer=pointy+j*bits_per_pixel
         color=*pointer
         red=rgbr(color)*red2#
         green=rgbg(color)*green2#
         blue=rgbb(color)*blue2#
         if red>255 then red=255
         if green>255 then green=255
         if blue>255 then blue=255
         *pointer=rgb(red,green,blue)
      next j
   next i
   unlock pixels
endfunction
 
function bitmap_brightness(num,value)
set current bitmap num
   value2#=value/100.0
   lock pixels
   start=get pixels pointer()
   repeat_number=get pixels pitch()
   bits_per_pixel=bitmap depth(num)/8
   for i=0 to bitmap height(num)-1
      pointy=start+i*repeat_number
      for j=0 to bitmap width(num)-1
         pointer=pointy+j*bits_per_pixel
         color=*pointer
         red=rgbr(color)*value2#
         green=rgbg(color)*value2#
         blue=rgbb(color)*value2#
         if red>255 then red=255
         if green>255 then green=255
         if blue>255 then blue=255
         *pointer=rgb(red,green,blue)
      next j
   next i
   unlock pixels
endfunction
 
function shift_bitmap(bitmap_number,amountx,amounty)
   if bitmap exist(bitmap_number+100)=0
      create bitmap bitmap_number+100,bitmap width(bitmap_number),bitmap height(bitmap_number)
      copy bitmap bitmap_number,bitmap_number+100
 
      if amountx>=0 and amounty>=0
         copy bitmap bitmap_number+100,0,0,bitmap width(bitmap_number)-amountx,bitmap height(bitmap_number),bitmap_number,amountx,0,bitmap width(bitmap_number),bitmap height(bitmap_number)
         copy bitmap bitmap_number+100,bitmap width(bitmap_number)-amountx,0,bitmap width(bitmap_number),bitmap height(bitmap_number),bitmap_number,0,0,amountx,bitmap height(bitmap_number)
         copy bitmap bitmap_number,bitmap_number+100
         copy bitmap bitmap_number+100,0,0,bitmap width(bitmap_number),bitmap height(bitmap_number)-amounty,bitmap_number,0,amounty,bitmap width(bitmap_number),bitmap height(bitmap_number)
         copy bitmap bitmap_number+100,0,bitmap height(bitmap_number)-amounty,bitmap width(bitmap_number),bitmap height(bitmap_number),bitmap_number,0,0,bitmap width(bitmap_number),bitmap height(bitmap_number)
      endif
      delete bitmap bitmap_number+100
   else
      if amountx>=0 and amounty>=0
         copy bitmap bitmap_number+100,0,0,bitmap width(bitmap_number)-amountx,bitmap height(bitmap_number)-amounty,bitmap_number,amountx,amounty,bitmap width(bitmap_number),bitmap height(bitmap_number)
         copy bitmap bitmap_number+100,bitmap width(bitmap_number)-amountx,bitmap height(bitmap_number)-amounty,bitmap width(bitmap_number),bitmap height(bitmap_number),bitmap_number,0,0,amountx,amounty
         copy bitmap bitmap_number+100,bitmap width(bitmap_number)-amountx,0,bitmap width(bitmap_number),bitmap height(bitmap_number)-amounty,bitmap_number,0,amounty,amountx,bitmap height(bitmap_number)
         copy bitmap bitmap_number+100,0,bitmap height(bitmap_number)-amounty,bitmap width(bitmap_number)-amountx,bitmap height(bitmap_number),bitmap_number,amountx,0,bitmap width(bitmap_number),amounty
      endif
   endif
endfunction
 
function Make_black_and_white(num)
   set current bitmap num
   lock pixels
   start=get pixels pointer()
   repeat_number=get pixels pitch()
   bits_per_pixel=bitmap depth(num)/8
   for i=0 to bitmap height(num)-1
      for j=0 to bitmap width(num)-1
         pointer=start+i*repeat_number+j*bits_per_pixel
         color_num=*pointer
         value=(rgbr(color_num)+rgbg(color_num)+rgbb(color_num))/3
         *pointer=rgb(value,value,value)
      next j
   next i
   unlock pixels
endfunction
 
function rotate_bitmap(num,degrees#)
   set current bitmap num
   mem=num+100
   if memblock exist(mem)=0
      make memblock from bitmap mem,num
      none_set=1
   endif
   width=memblock dword(mem,0)
   height=memblock dword(mem,4)
   depth=memblock dword(mem,8)
   lock pixels
   start=get pixels pointer()
   repeat_number=get pixels pitch()
   bits_per_pixel=bitmap depth(num)/8
   basis1x#=cos(degrees#)
   basis2x#=cos(degrees#+90.0)
   basis1y#=sin(degrees#)
   basis2y#=sin(degrees#+90.0)
   for i=0 to bitmap height(num)-1
      for j=0 to bitmap width(num)-1
         x#=(j-(bitmap width(num)/2.0))*basis1x#+(i-(bitmap height(num)/2.0))*basis2x#+bitmap width(num)/2.0
         y#=(j-(bitmap width(num)/2.0))*basis1y#+(i-(bitmap height(num)/2.0))*basis2y#+bitmap height(num)/2.0
         if x#>0 and y#>0 and x#<width and y#<height
 
            color_value=interpolate_memblock_pixel(mem,x#,y#)
         else
            color_value=100
         endif
         pointer=start+i*repeat_number+j*bits_per_pixel
         *pointer=color_value
      next j
   next i
   unlock pixels
   if none_set=1 then delete memblock mem
endfunction
 
 
function get_memblock_pixel(memblock,x,y)
   width=memblock dword(memblock,0)
   height=memblock dword(memblock,4)
   depth=memblock dword(memblock,8)
   if x>0 and y>0 and x<width and y<height
      pointer=y*width*depth/8+x*depth/8+12
      value=memblock dword(memblock,pointer)
   endif
endfunction value
 
function set_memblock_pixel(memblock,x,y,color)
   width=memblock dword(memblock,0)
   height=memblock dword(memblock,4)
   depth=memblock dword(memblock,8)
   if x>0 and y>0 and x<width and y<height
      pointer=y*width*depth/8+x*depth/8+12
      write memblock dword memblock,pointer,color
   endif
endfunction
 
function get_locked_pixel(x,y)
   start=get pixels pointer()
   repeat_number=get pixels pitch()
   bits_per_pixel=bitmap depth(num)/8
   pointer=start+y*repeat_number+x*bits_per_pixel
   result=*pointer
endfunction result
 
function set_locked_pixel(x,y,color_value)
   start=get pixels pointer()
   repeat_number=get pixels pitch()
   bits_per_pixel=bitmap depth(num)/8
   pointer=start+y*repeat_number+x*bits_per_pixel
   *pointer=color_value
endfunction
 
Function Interpolate_memblock_pixel(MemBlock as integer,x as float, y as float)
   width=memblock dword(memblock,0)
   height=memblock dword(memblock,4)
   depth=memblock dword(memblock,8)
   Rvalue=0
   if x>0 and y>0 and x<width-1 and y<height-1
      y2#=y-int(y)
      x2#=x-int(x)
      iy#=1.0-y2#
      ix#=1.0-x2#
      val1=memblock dword(memblock,int(y)*width*depth/8+int(x)*depth/8+12)
      val2=memblock dword(memblock,int(y)*width*depth/8+int(x+1)*depth/8+12)
      val3=memblock dword(memblock,int(y+1)*width*depth/8+int(x+1)*depth/8+12)
      val4=memblock dword(memblock,int(y+1)*width*depth/8+int(x)*depth/8+12)
 
      Rvalue=rgb(int(iy#*(ix#*rgbr(val1)+x2#*rgbr(val2))+y2#*(ix#*rgbr(val4)+x2#*rgbr(val3))),int(iy#*(ix#*rgbg(val1)+x2#*rgbg(val2))+y2#*(ix#*rgbg(val4)+x2#*rgbg(val3))),int(iy#*(ix#*rgbb(val1)+x2#*rgbb(val2))+y2#*(ix#*rgbb(val4)+x2#*rgbb(val3))))
   endif
EndFunction Rvalue