/*
   TESTSCAN.PRG
   by Lorenzo Succi 2024
   view properties
   change format
*/

#pragma library( "ascom10.lib" )
#pragma library( "xbtbase1.lib" )
#pragma library( "xbtbase2.lib" )

#include "activex.ch"

procedure Main
   local oDialog, oScanner, oDevice

   wiaFormatBMP  := "{B96B3CAB-0728-11D3-9D7B-0000F81EF32E}"
   wiaFormatPNG  := "{B96B3CAF-0728-11D3-9D7B-0000F81EF32E}"
   wiaFormatGIF  := "{B96B3CB0-0728-11D3-9D7B-0000F81EF32E}"
   wiaFormatJPEG := "{B96B3CAE-0728-11D3-9D7B-0000F81EF32E}"
   wiaFormatTIFF := "{B96B3CB1-0728-11D3-9D7B-0000F81EF32E}"

   cls

   oDialog  := createobject("WIA.CommonDialog")
   oDevice  := createobject("WIA.DeviceManager")
   nDevice  := oDevice:DeviceInfos:Count 

   if nDevice = 0
      msgbox( "No scanners connected " , "Attention!!!" )
      return
   else 
      set alternate to "WIA.TXT"
      set alternate on

      ? "Devices"
      cnt:=oDevice:deviceinfos(1):properties:count
      i:=1
      do while i<=cnt
         ? str(i,4)+" = "
         ?? oDevice:deviceinfos(1):properties(i):name+" "
         ?? oDevice:deviceinfos(1):properties(i):value
         ?? " "
         ?? oDevice:deviceinfos(1):properties(i):type
         i++
      enddo

      ?
      ? "Device Properties"

//    oImage := oDialog:ShowAcquireImage(
//                  1,
//                  WiaImageIntent.GrayscaleIntent,
//                  WiaImageBias.MaximizeQuality,
//                  wiaFormatJPEG, .t., .f., .f.)
//    oImage := oDialog:ShowAcquireImage()

      oScanner := oDialog:ShowSelectDevice(1,.t.,.f.)
    //oScanner:items(1):properties   // -> obj
      cntscan:=oScanner:items(1):properties:count
      i:=1
      do while i<=cntscan
         oProp := oScanner:items(1):properties(i)   // obj
         ?  str(I,4)+" = "
         ?? oProp:name+" "
         ?? oProp:value
         ?? " "
         ?? oProp:type
         ?? oProp:subtype
         ?? " "
         ?? oProp:propertyid
         i++
      enddo

    //oImage:=oDialog:showacquireimage()    // ok
      STARTH :=2.0
      STARTV :=4.0
      EXTENTH:=600
      EXTENTV:=900
      XXX:=oScanner:items(1):properties("6147"):subtype   // -> 1
      if XXX==1   // RangeSubType
         MIN:=oScanner:items(1):properties("6147"):subtypemin
         MAX:=oScanner:items(1):properties("6147"):subtypemax
      endif
      if XXX==2   // ListSubType
      endif
      if XXX==3   // FlagSubType
      endif

      // get resolution hor
      oScanner:items(1):properties("6147"):value   // -> 300

      oImage:=oScanner:items(1):transfer("{B96B3CB1-0728-11D3-9D7B-0000F81EF32E}")
      // created?

      // format?
      if oImage:FormatID<>wiaFormatJPEG
         oImage2:=createobject("WIA.ImageProcess")
         oImF:=oImage2:FilterInfos   // -> obj
         cnt:=oImage2:filterinfos:count()
         ? "Filter infos"
         PIPTR:=0
         I:=1
         AFI:=array(0)
         do while I<=cnt
            PINAME:=oImage2:filterinfos(I):name
            ? PINAME
            aadd(AFI,PINAME)
            ?? " = "
            ?? oImage2:filterinfos(I):filterid
            XXX:=oImage2:filterinfos(PINAME)
            oImage2:filters:add(XXX:FilterID)
            cntp:=oImage2:filters(1):properties:count
            ? space(10)+"Filter properties"
            J:=1
            do while J<=cntp
               ? space(20)+oImage2:filters(1):properties(J):name   
               J++
            enddo
            oImage2:filters:remove(1)
            I++
         enddo
         oFInfo:=oImage2:filterinfos("Convert")
       //oFInfo:FilterID   // -> {42A6E907-1D2F-4B38-AC50-31ADBE2AB3C2}
         oImage2:filters:add(oFInfo:FilterID)
       //oImage2:filters(1):properties("FormatID"):value   // -> {00000000-0000 ....}
         oImage2:filters(1):properties("FormatID"):value:=wiaFormatJPEG

       //oImage2:apply(oImage)   // -> obj
         if file("PROVA1.JPG")
            delete file "PROVA1.JPG"
         endif
         oImage2:apply(oImage):savefile("PROVA1.JPG")
      else
         oImage:savefile("PROVA0.JPG")
      endif

   endif

return
// end TESTSCAN.PRG
