TGC Codebase Backup



image : alpha and more by Graphiboc

14th Oct 2004 2:43
Summary

functions of images: alpha, color, etc.



Description

ghost_image() -> To precalculate the transparency of an image with the luminosity of each pixel. Can also be used in calculation direct but slowed down the program.

color_image(img,red#,green#,blue#) -> To change the colors of an image.

greyscale_image(img) -> To put an image in levels of gray.

invert_image(img) -> to reverse the colors of an image

light_image(img,light_img,pourcentage) -> To change the luminosity of each pixel of an image with a map of luminosity

blend_image(img1,img2,blend_img,result_img) -> To mix two images with a blendmap



Code
                                    ` This code was downloaded from The Game Creators
                                    ` It is reproduced here with full permission
                                    ` http://www.thegamecreators.com
                                    
                                    
function ghost_image(x,y,img,img2,img_free,alpha_base)
   if image exist(img)=1

   FOR a=1 to 3
   if memblock exist(a)=1 then delete memblock a
   NEXT a

   x1=x : y1=y
   if x1<0 then x1=0 : if y1<0 then y1=0
   if x1>screen width()-1 then x1=screen width()-1 : if y1>screen width()-1 then y1=screen width()-1

      width=img_width(img)
      height=img_height(img)
      if width+x>screen width() then width=screen width()-x
      if height+y>screen height() then height=screen height()-y

      get image img_free,x1,y1,x1+width,y1+height,1
      make memblock from image 3,img_free
      delete image img_free

      make memblock from image 1, img
      make memblock 2,get memblock size(1)
      sw=memblock dword(1,0) : `width
      sh=memblock dword(1,4) : `height
      write memblock dword 2,0,memblock dword(1,0) : `width
      write memblock dword 2,4,memblock dword(1,4) : `height
      write memblock dword 2,8,memblock dword(1,8) : `depht

      for a=0 to sh-1
         for b=0 to sw-1
            c=memblock dword(1,12+b*4+a*sw*4)
            d=memblock dword(3,12+b*4+a*sw*4)
            alpha#=alpha_base/((light_value(c)/255)*100)
            if alpha#<1 then alpha#=1

            r1#=rgbr(c)/alpha#
            g1#=rgbg(c)/alpha#
            b1#=rgbb(c)/alpha#

            r#=(rgbr(d)+r1#)
            if r#>255 then r#=255
            if r#<0 then r#=0
            g#=(rgbg(d)+g1#)
            if g#>255 then g#=255
            if g#<0 then g#=0
            b#=(rgbb(d)+b1#)
            if b#>255 then b#=255
            if b#<0 then b#=0

            global_color=RGB(r#,g#,b#)
            `global_color=rgb((rgbr(d)+rgbr(c)/alpha#),(rgbg(d)+rgbg(c)/alpha#),(rgbb(d)+rgbb(c)/alpha#))
            write memblock dword 2,12+b*4+a*sw*4,global_color
         next b
      next a

      make image from memblock img2,2
      delete memblock 1
      delete memblock 2
      delete memblock 3
   endif
endfunction




function color_image(img,red#,green#,blue#)
   if image exist(img)=1

   FOR a=1 to 1
   if memblock exist(a)=1 then delete memblock a
   NEXT a

      make memblock from image 1,img
      sw=memblock dword(1,0) : `width
      sh=memblock dword(1,4) : `height

      for a=0 to sh-1
         for b=0 to sw-1
            c=memblock dword(1,12+b*4+a*sw*4)

            r#=rgbr(c)+red#
            if r#>255 then r#=255
            if r#<0 then r#=0
            g#=rgbg(c)+green#
            if g#>255 then g#=255
            if g#<0 then g#=0
            b#=rgbb(c)+blue#
            if b#>255 then b#=255
            if b#<0 then b#=0

            color=RGB(r#,g#,b#)
            write memblock dword 1,12+b*4+a*sw*4,color
         next b
      next a

      make image from memblock img,1
      delete memblock 1
   endif
endfunction



function greyscale_image(img)
   if image exist(img)=1

   FOR a=1 to 1
   if memblock exist(a)=1 then delete memblock a
   NEXT a

      make memblock from image 1,img
      sw=memblock dword(1,0) : `width
      sh=memblock dword(1,4) : `height

      for a=0 to sh-1
         for b=0 to sw-1
            c=memblock dword(1,12+b*4+a*sw*4)
            value=light_value(c)
            color=rgb(value,value,value)
            write memblock dword 1,12+b*4+a*sw*4,color
         next b
      next a

      make image from memblock img,1
      delete memblock 1
   endif
endfunction



function invert_image(img)
   if image exist(img)=1

   FOR a=1 to 1
   if memblock exist(a)=1 then delete memblock a
   NEXT a

      make memblock from image 1,img
      sw=memblock dword(1,0) : `width
      sh=memblock dword(1,4) : `height

      for a=0 to sh-1
         for b=0 to sw-1
            c=memblock dword(1,12+b*4+a*sw*4)
            r#=128-(rgbr(c)-128)
            if r#>255 then r#=255
            if r#<0 then r#=0
            g#=128-(rgbg(c)-128)
            if g#>255 then g#=255
            if g#<0 then g#=0
            b#=128-(rgbb(c)-128)
            if b#>255 then b#=255
            if b#<0 then b#=0
            color=rgb(r#,g#,b#)
            write memblock dword 1,12+b*4+a*sw*4,color
         next b
      next a

      make image from memblock img,1
      delete memblock 1
   endif
endfunction



function light_image(img,light_img,pourcentage)
`LES DEUX IMAGES DOIVENT ETRES DE TAILLE IDENTIQUES!

   if image exist(img)=1 and image exist(light_img)=1

   FOR a=1 to 2
   if memblock exist(a)=1 then delete memblock a
   NEXT a

      make memblock from image 1,img
      make memblock from image 2,light_img
      sw=memblock dword(1,0) : `width
      sh=memblock dword(1,4) : `height

      for a=0 to sh-1
         for b=0 to sw-1
            c=memblock dword(1,12+b*4+a*sw*4)
            d=memblock dword(2,12+b*4+a*sw*4)
            value#=light_value(d)

            r#=rgbr(c)+value#*(pourcentage/100)
            if r#>255 then r#=255
            if r#<0 then r#=0
            g#=rgbg(c)+value#*(pourcentage/100)
            if g#>255 then g#=255
            if g#<0 then g#=0
            b#=rgbb(c)+value#*(pourcentage/100)
            if b#>255 then b#=255
            if b#<0 then b#=0

            color=rgb(r#,g#,b#)

            write memblock dword 1,12+b*4+a*sw*4,color
         next b
      next a

      make image from memblock img,1
      delete memblock 1
      delete memblock 2
   endif
endfunction



function blend_image(img1,img2,blend_img,result_img)
`LES IMAGES DOIVENT ETRES DE TAILLE IDENTIQUES!

   if image exist(img1)=1 and image exist(img2)=1 and image exist(blend_img)=1

   FOR a=1 to 2
   if memblock exist(a)=1 then delete memblock a
   NEXT a

      make memblock from image 1,img1
      make memblock from image 2,img2
      make memblock from image 3,blend_img
      make memblock 4,get memblock size(1)
      write memblock dword 4,0,memblock dword(1,0) : `width
      write memblock dword 4,4,memblock dword(1,4) : `height
      write memblock dword 4,8,memblock dword(1,8) : `depht
      sw=memblock dword(1,0) : `width
      sh=memblock dword(1,4) : `height

      for a=0 to sh-1
         for b=0 to sw-1
            c=memblock dword(1,12+b*4+a*sw*4)
            d=memblock dword(2,12+b*4+a*sw*4)

            e=memblock dword(3,12+b*4+a*sw*4)
            alpha#=100/((light_value(e)/255)*100)
            if alpha#<1 then alpha#=1

            r1#=rgbr(d)/alpha#
            g1#=rgbg(d)/alpha#
            b1#=rgbb(d)/alpha#

            r#=(rgbr(c)+r1#)
            if r#>255 then r#=255
            if r#<0 then r#=0
            g#=(rgbg(c)+g1#)
            if g#>255 then g#=255
            if g#<0 then g#=0
            b#=(rgbb(c)+b1#)
            if b#>255 then b#=255
            if b#<0 then b#=0

            color=rgb(r#,g#,b#)

            write memblock dword 4,12+b*4+a*sw*4,color
         next b
      next a

      make image from memblock result_img,4
      delete memblock 1
      delete memblock 2
      delete memblock 3
      delete memblock 4
   endif
endfunction






FUNCTION img_width(img)
   sprite 65500,0,0,img
   sx=sprite width(65500)
   delete sprite 65500
ENDFUNCTION sx



FUNCTION img_height(img)
   sprite 65500,0,0,img
   sy=sprite height(65500)
   delete sprite 65500
ENDFUNCTION sy



FUNCTION light_value(rgb_value)
   r=rgbr(rgb_value)
   g=rgbg(rgb_value)
   b=rgbb(rgb_value)
   value#=(r+g+b)/3
ENDFUNCTION value#