!******************************************************************************!
! Test for plotting a Type 2000 SL & QB datafile over a map which contains the actual (ARGB) colors
! (with transparency alpha values) to display on the Layer2D.
! This macro also tests the unconfigured nxm.sys.prim.convertargb primitive.
! This macro also tests enhancement in Bug 1266 to allow control of alpha
! (tranparency) values on Layer2D's colormap.
!
! @bug     724, 943, 1266
! @author  Neon Ngo
! Since:   NeXtMidas 2.5.0
!******************************************************************************!
startmacro l:defaultAlpha[128]
  switch INTERACTIVE interAct GET DEF=TRUE  SDEF=TRUE
  switch BASELINE    baseline GET DEF=FALSE SDEF=TRUE

  call setDatDir

  set testfile           test_colorbar.tmp{AUX=RAM}
  set testfile2     test_layer2d_penny.tmp{AUX=RAM}
  set testlargefile     test_largefile.tmp{AUX=^AUX.WRITE}
  set testfileqb      test_colorbar_qb.tmp{AUX=RAM}

  if interAct then
    ! create colorbar with increasing alpha values and put in the middle of Canada
    call createColorBar testfile

    call createQBColorBar testfile testfileqb

    ! create semi-transparent penny from DC to Montreal
    call createSemiTransparentImage ^{datDir}.apenny.prm testfile2 defaultAlpha

    call createLargefile testlargefile defaultAlpha

    set instructions "Should see a colorbar (Red, Green, Blue, White, Yellow, Magenta, and Cyan) &
                      over the world plot that starts over the middle of Canada. &
                      The colorbar's transparency starts from translucent to opaque from bottom to &
                      top. Verify that the same colorbar with the specified (global) alpha value &
                      is rendered starting around silicon valley. Verify that a slightly translucent &
                      alaska.prm is plotted. A slightly translucent penny &
                      should be rendered starting at DC. Zoom into it and verify that it is semi-&
                      transparent. Change the alpha value and verify that the transparency of the &
                      penny, west coast colorbar, and Alaska changes accordingly (0=transparent, 255=opaque). &
                      The largefile raster should display over Mexico and the Caribbean, but there is &
                      a KNOWN issue that it is not layered correctly over the world shapefile."
    pipe init
      panel/msgid=MAIN/setup/controls=gc
      gcontrol label mainLabel "Main"
        gcontrol menu main "Exit with Status = " "PASS,FAIL" /tleft
        set gc.main.itemColors {PASS=GREEN,FAIL=RED}
        gcontrol text idtext "Instructions" "^{instructions}" 27 36 /title
        gcontrol label  tests   "TESTS"
        gcontrol lval alpha "Alpha" defaultAlpha 0 255

      ! this for to test new feature in bug 1266
      set test_setAlpha_file ^{datDir}.alaska.prm{TAG=ALASKA,LAYER={alpha=^defaultAlpha}}

      ! plot that is zoomed to North America with several Layer2D
      ! NTN: 2008-04-09 added /LPB=64 so that y-compression warning message won't be displayed
      !      if we change how we do compression, in a future verion, this may no longer be needed
      plot/id=map world.shp|^{datDir}.cities.prm|&
               ^test_setAlpha_file|&
                 ^testfile{LAYER={DIRECTCOLOR=true}}|&
                ^testfile2{LAYER={DIRECTCOLOR=true}}|&
            ^testlargefile{LAYER={DIRECTCOLOR=true}}|&
        ^testfileqb{TAG=QB,LAYER={DIRECTCOLOR=true,ALPHA=^defaultAlpha}} &
        scale=-auto x1=-170 x2=-40 y1=10 y2=90 /LPB=64

      ! can also use DC as short form for DirectColor
      plot/id=raster      ^testfile2{LAYER={DC=true}}                     view=YiX
      plot/id=rasterXY    ^testfile2{LAYER={DC=true}}                     view=XY
      plot/id=raster2      ^testfile{LAYER={DC=true,ALPHA=^defaultAlpha}} view=iYX

      ! NTN: 2008-04-09 added /LPB=64 so that y-compression warning message won't be displayed
      !      if we change how we do compression, in a future verion, this may no longer be needed
      plot/id=largeRaster ^testlargefile{LAYER={DC=true}} /LPB=64

    pipe off

    ! cleanup
    erase/quiet/warn=n testfile
    erase/quiet/warn=n testfile2
    erase/quiet/warn=n testlargefile
    erase/quiet/warn=n testfileqb
  endif
endmacro

!******************************************************************************!
procedure processMessage m:msg
  !say "msg.name ^msg.name, data ^msg.data"

  if msg.name eqs "MAIN" then
    assert/text="^reg.main.name: User indicated tests for Layer2D using Direct Colors" msg.data eqss "PASS"
    pipe stop

  elseif msg.name eqs "ALPHA" then
    ! re-create the image with specified alpha value and re-open the files on the plots
    call createSemiTransparentImage ^{datDir}.apenny.prm ^testfile2 ^msg.data
    message FUNC=SEND ID=map    NAME=OPENFILE DATA=^testfile2{LAYER={DIRECTCOLOR=true}}
    message FUNC=SEND ID=raster NAME=OPENFILE DATA=^testfile2{LAYER={DIRECTCOLOR=true}}

    ! update alpha on QB Colorbar raster on main map plot (over Silicon Valley)
    set reg.map.layers.QB.alpha ^msg.data
    ! update alpha on Colorbar raster (3rd plot on bottom pane)
    set reg.raster2.alpha ^msg.data
    ! update alpha on regular (non-directcolor) Layer2D layer - test case for bug 1266
    set reg.map.layers.ALASKA.alpha ^msg.data

  else
    ! do nothing
    say "Unhandled message name: ^msg.name from fid: ^msg.fid data: ^msg.data"
  endif

return

!******************************************************************************!
! Set the dat directory
!******************************************************************************!
procedure setDatDir
  invoke nxmMajor env.nmversion.subString(0,1)
  invoke major java.lang.Integer.valueOf(nxmMajor)
  if major GE 4 then
    invoke s:datDir           nxm.sys.test.DatLocator.getDatDirLocation()
    invoke s:datDirSlash      nxm.sys.test.DatLocator.getDatDirSlashLocation()
    invoke z:sysDatFull       nxm.sys.test.DatLocator.sysDatFull()
    invoke z:sysTestDatExists nxm.sys.test.DatLocator.sysTestDatExists()
  else
    set s:datDir           "nxm.sys.dat"
    set s:datDirSlash      "nxm/sys/dat"
    set z:sysDatFull       true
    set z:sysTestDatExists false
  endif
return

!******************************************************************************!
! Create an RGB color bar (Red, Green, Blue, White, Yellow, and Cyan) image with increasing alpha
! values from completely translucent (alpha=0) to completely opaque (alpha=255).
! Put image in the middle of Canada.
procedure createColorBar FN:outfilename
  set l:fs 7
  file open/d/n outfile ^outfilename{TYPE=2000,FORM=SL,FS=^fs}

  ! generate RGB colors from translucent to opaque
  do l:i 0 255 1
    table tab create
    new java.awt.Color(i,0,0,i) r
    new java.awt.Color(0,i,0,i) g
    new java.awt.Color(0,0,i,i) b
    new java.awt.Color(i,i,i,i) a0
    new java.awt.Color(i,i,0,i) a1
    new java.awt.Color(i,0,i,i) a2
    new java.awt.Color(0,i,i,i) a3
    set tab.F1 r.rgb
    set tab.F2 g.rgb
    set tab.F3 b.rgb
    set tab.F4 a0.rgb
    set tab.F5 a1.rgb
    set tab.F6 a2.rgb
    set tab.F7 a3.rgb

    file write outfile tab
  enddo
  file close outfile

  ! put in middle of Cananda
  headermod outfilename XS=-100 YS=55 YD=0.1 XD=2
return

! Create a QB file from SL input file
! Put image in the west coast (silicon valley) of USA
procedure createQBColorBar FN:infile FN:outfilename
  noop ^infile ^outfilename

  ! put in silicon valley of USA
  headermod outfilename FORM=QB XS=-122 YS=37
return

!******************************************************************************!
! create a copy of infname with specified transparency to outfname
! make location of outfname start from Washington DC to Montreal, CA
procedure createSemiTransparentImage FN:infname FN:outfname I:alpha
  STATISTICS ^infname table=stat
  nxm.sys.prim.convertargb IN=^infname OUT=^outfname MIN=^stat.min MAX=^stat.max ALPHA=^alpha
  headermod ^outfname XS=-76.95 YS=38.89 YD=0.053 XD=0.053
return

!******************************************************************************!
! Create a large data file
! values from completely translucent (alpha=0) to completely opaque (alpha=255).
! Put image to cover Mexico
procedure createLargefile FN:outfilename I:alpha
  info "^reg.main.name: generating large input file..."
  WAVEFORM OUT=temp_file FORM=SF ELEM=2M SHAPE=white
  info "^reg.main.name: computing FFT of large file..."
  fft/dbf IN1=temp_file OUT1=temp_file2
  info "^reg.main.name: converting large file to Type 2000 SF with DirectColors..."
  STATISTICS temp_file2 table=stat
  nxm.sys.prim.convertargb/TL=100 IN=temp_file2 OUT=outfilename MIN=stat.min MAX=stat.max ALPHA=alpha NUMCOLORS=64
  headermod outfilename XS=-90 YS=20 YD=0.1 XD=0.1

  erase/quiet/warn=n "temp_file"
  erase/quiet/warn=n "temp_file2"

  ! Put image to cover Mexico
  headermod outfilename XS=-120 YS=15 YD=0.008 XD=0.1
return
