/*

   v1.24 Visual Surface

   Marko Seppänen
   marko.seppanen@wwnet.fi

*/


address IMAGEENGINEER

Options results
signal on error			/* Setup a place for errors to go */

if arg()=0 then exit
x=arg(1)
pic=subword(x,1,1)
surfacetype=subword(x,2,1)
checkbit=subword(x,3,1)
space=0


MARK pic PRIMARY
PROJECT_INFO pic WIDTH
picwidth=result
PROJECT_INFO pic HEIGHT
picheight=result
PROJECT_INFO pic ZOOM
origzoomvalue=result


if surfacetype="" then do

  if exists("ie:prefs/visualsurface.cfg") == "1" then
    do
      call open("temp","ie:prefs/visualsurface.cfg","R")
      values=readln("temp")
      parse var values ok surfacetype .
      call close("temp")
    end
  else
    do
      surfacetype=0
    end

  ' FORM "Visual Surface" "Use|Cancel"',
  ' CYCLE,"Surfacetype","Noise|Maximum",'surfacetype''

  values=result
  parse var values ok surfacetype .

  if ok = 0 then exit

  call open("temp","ie:prefs/visualsurface.cfg","W")
  res=writeln("temp",values)
  call close("temp")

end


select

  when surfacetype == 0 then

    do

      if exists("ie:prefs/visualsurfacenoise.cfg") == "1" then
        do
          call open("temp","ie:prefs/visualsurfacenoise.cfg","R")
          values=readln("temp")
          parse var values ok noisevalue noisecolor aftereffect .
          call close("temp")
        end
      else
        do
          noisevalue=15
          noisecolor=0
          aftereffect=0
        end

      'FORM "Visual Surface - noise" "Use|Cancel"',
      'INTEGER,"Noisevalue",1,100,'noisevalue',SLIDER',
      'CYCLE,"Noisecolor","White|Black",'noisecolor'',
      'CYCLE,"Aftereffect","Carbonated|Concrete|Leather|Crunchy|Dirty|Blotchy|Scribble|Noise|Bump|Stardust|Brushed metal|Dots|Rock",'aftereffect''


      values=result
      parse var values ok noisevalue noisecolor aftereffect .

      if ok = 0 then exit

      call open("temp","ie:prefs/visualsurfacenoise.cfg","W")
      res=writeln("temp",values)
      call close("temp")



      BRIGHTNESS pic "-255"
      black=result

      NOISE black 100 INTENSITY ADDITIVE noisevalue GAUSSIAN
      noisebox=result

      CLOSE black



      if aftereffect=="0" then convolvetype="DeepPress"
      if aftereffect=="1" then convolvetype="Emboss_High"
      if aftereffect=="2" then convolvetype="Emboss_High_0"
      if aftereffect=="3" then convolvetype="Emboss_Ultra"
      if aftereffect=="4" then convolvetype="FeatherWhite"
      if aftereffect=="5" then convolvetype="GaussianBlur5x5"
      if aftereffect=="6" then convolvetype="Reflect_NorthWest"
      if aftereffect=="7" then convolvetype="RoughSurface_Low"
      if aftereffect=="8" then convolvetype="Sobel"
      if aftereffect=="9" then convolvetype="Spark"
      if aftereffect=="10" then convolvetype="Wet_WhiteOnBlack"
      if aftereffect=="11" then convolvetype="Bump_NorthWest"
      if aftereffect=="12" then convolvetype="Carve_NorthWest"


      if exists("ie:convolves/"convolvetype) == "0" then do
        'REQUEST "Please download VE-!Extras.lha" "Ok"'
        exit
      end

      CONVOLVE noisebox "IE:Convolves/"convolvetype
      noisebox2=result
      CLOSE noisebox
      noisebox=noisebox2


      MARK pic PRIMARY
      MARK noisebox SECONDARY

      if noisecolor=="0" then method="ADD"
      else do

        NEGATIVE noisebox
        whitenoise=result

        CLOSE noisebox
        noisebox=whitenoise

        MARK whitenoise SECONDARY
        method="MULTIPLY"

      end


      COMPOSITE 0 0 method
      final=result

      CLOSE noisebox

    end



  when surfacetype == 1 then

    do

      if exists("ie:prefs/visualsurfacemaximum.cfg") == "1" then
        do
          call open("temp","ie:prefs/visualsurfacemaximum.cfg","R")
          values=readln("temp")
          parse var values ok maxvalue aftereffect .
          call close("temp")
        end
      else
        do
          maxvalue=7
          aftereffect=0
        end

      'FORM "Visual Surface - maximum" "Use|Cancel"',
      'INTEGER,"Noisevalue",1,25,'maxvalue',SLIDER',
      'CYCLE,"Aftereffect","Tekno|Sunshine|Watercolor|Bump|Raised|Psycho|Woodcut",'aftereffect''


      values=result
      parse var values ok maxvalue aftereffect .

      if ok = 0 then exit

      call open("temp","ie:prefs/visualsurfacemaximum.cfg","W")
      res=writeln("temp",values)
      call close("temp")



      MAXIMUM pic maxvalue maxvalue
      noisebox=result



      if aftereffect=="0" then convolvetype="ColourFragment_Colour"
      if aftereffect=="1" then convolvetype="FeatherWhite"
      if aftereffect=="2" then convolvetype="GaussianBlur5x5"
      if aftereffect=="3" then convolvetype="Reflect_NorthWest"
      if aftereffect=="4" then convolvetype="RoughSurface_Medium"
      if aftereffect=="5" then convolvetype="SmoothEdge_Normal"
      if aftereffect=="6" then convolvetype="Woodcut"


      if exists("ie:convolves/"convolvetype) == "0" then do
        'REQUEST "Please download VE-!Extras.lha" "Ok"'
        exit
      end


      CONVOLVE noisebox "IE:Convolves/"convolvetype
      noisebox2=result
      CLOSE noisebox
      noisebox=noisebox2


      MARK pic PRIMARY
      MARK noisebox SECONDARY

      COMPOSITE 0 0 ADD
      final=result

      CLOSE noisebox

    end


otherwise exit

end





exit





addspace:

  temppic=arg(1)
  space=arg(2)
  bg=arg(3)

  if exists("ie:arexx/addspace.rexx") == "0" then do
    'REQUEST "AddSpace.rexx is missing!" "I will get it... sorry"'
    exit
  end
  address command "rexx:rx ie:arexx/AddSpace.rexx" temppic space bg

  PROJECT_LIST
  xyz=result
  x0=subword(xyz,1,1)

  return x0


/*******************************************************************/
/* This is where control goes when an error code is returned by IE */
/* It puts up a message saying what happened and on which line     */
/*******************************************************************/
error:
if RC=5 then do			/* Did the user just cancel us? */
	IE_TO_FRONT
	LAST_ERROR
	'REQUEST "'||RESULT||'"'
	exit
end
else do
	IE_TO_FRONT
	LAST_ERROR
	'REQUEST "Error detected!!!'||D2C(10)||'Image Engineer error message is as follows'||D2C(10)||result||D2C(10)||'Script failed on line '||SIGL||'"' 'Doh!'
	exit
end
