/*
 $VER: ImageFX.dopus5 2.0 (27.5.98)
 Written by Jan-Erik Karlsson
 Send images to ImageFX from DirectoryOpus
 Call as:
 <AREXX>DOpus5:ARexx/ImageFX.dopus5 {Qp} {f!}
*/

/* Change this to the path where ImageFX is located (Don't remove the brackets) */
ImageFXPath = 'ImageFX:ImageFX WINDOWEDCGFX PREFS=ImageFX:default.prefs'

options results
PARSE ARG portname '"'picfile'"' .
IF portname == '' THEN DO
  portname = "DOPUS.1"
END
portname = strip(portname, "B",' ')
portname = strip(portname, "B", '"')

IF ~SHOW('L','rexxsupport.library') THEN DO
  addlib('rexxsupport.library',0,-30)
END
IF ~SHOW('L','datatypes.library') THEN DO
  addlib('datatypes.library',0,-30)
END

IF picfile ~= '' THEN DO
  picfile = strip(picfile,"B",' ')
  picfile = strip(picfile,"B",'"')
  filename = picfile
  rc = max(lastpos(':',picfile),lastpos('/',picfile))+1
  file1= substr(picfile,rc)
  picpath=strip(left(picfile,rc-1),'T','/')
  picfile = file1
END
lf='0a'x

IF Show("P", portname) THEN DO
 address value portname
END
ELSE DO
 exit
END


dopus version
if(result ~= 'RESULT') then do
  vers=translate(result,'.',' ')
  if(vers==5.8) then vers = 5.8000
end
else vers = 'RESULT'
IF (vers == 'RESULT' | vers < 5.1218 ) THEN DO
  dopus request '"This script requires DOpus v5.5 or greater." OK'
 exit
END

pic = picfile
popup=1
filename = filename

CALL ExamineDT(filename,dtstem., STEM)
picftype = dtstem.DataType
filesize = dtstem.Size
picGroup = dtstem.GroupID
picGroup2 = dtstem.GroupIDName

IF length(picftype) >= 7 THEN DO
  picftype2 = left(picftype,6)
  picftype2 = strip(picftype2,"B",' ')
END
ELSE DO
  picftype2 = picftype
END

dopus progress name info info2 title abort
handler = result
dopus progress handler title 'ImageFX loader V2.0 by Janne Karlsson 98'
dopus progress handler info 'Group: 'picGroup2'  FileType: 'picftype
dopus progress handler info2 'Looking for ImageFX...'
dopus progress handler name pic

IF Show('p','IMAGEFX.1') THEN DO
  call CHECKIFX
END
ELSE DO
  dopus front
  call dorequest('"ImageFX does not seem to be loaded.'lf'Should I load ImageFX?" _Yes|_No')

  IF returnflag ==1  THEN DO
    call LOADIFX
  END
  ELSE DO
    dopus progress handler info 'Group: 'picGroup2'  FileType: 'picftype
    dopus progress handler info2 'Aborting...'
    address command Wait
    call EXIT_ME
  END
call EXIT_ME
END

CHECKIFX:
  address "IMAGEFX.1"
  options failat 3000
  GetMain
  mainselection = rc

  IF mainselection > 0 THEN DO
    erase = 1
    call SENDIFX
  END
  ELSE DO
    PARSE VAR RESULT '"' mainname '"'
    address value portname
    dopus front
    call dorequest('"The ImageFX main buffer is not empty.'lf'project 'mainname lf'Push The Picture!?" _Push|_Erase|_Swap|_Abort')

    IF returnflag >=1 THEN DO

      IF returnflag == 1 THEN DO
        erase = 0
        call SENDIFX
      END

      IF returnflag == 2 THEN DO
        erase = 1
        call SENDIFX
      END

      IF returnflag == 3 THEN DO
        address "IMAGEFX.1"
        GetSwap
        swapselection = rc

        IF swapselection > 0 THEN DO
          erase = 1
          swap
          call SENDIFX
        END
        ELSE DO
          PARSE VAR RESULT '"'swapname'"'
          address value portname
          call dorequest('"The ImageFX swap buffer is not empty.'lf'Project 'swapname lf' will be pushed to buffer list" _Push|_Erase|_Abort')
          IF returnflag >= 1 THEN DO
            IF returnflag == 1 THEN DO
              erase = 0
              swap
              call SENDIFX
            END
            IF returnflag == 2 THEN DO
              erase = 1
              swap
              call SENDIFX
            END
          END
          ELSE DO
            dopus progress handler info 'Group: 'picGroup2'  FileType: 'picftype
            dopus progress handler info2 'Aborting...'
            call EXIT_ME
          END /* END OF ELSE DO */
      END
    END
    ELSE DO
      dopus progress handler info 'Group: 'picGroup2'  FileType: 'picftype
      dopus progress handler info2 'Aborting...'
      call EXIT_ME
    END
  END
call EXIT_ME
END

LOADIFX:
  IF picGroup == 'anim' | picGroup == 'movi' | picGroup == 'pict' THEN DO
    dopus front
    dopus progress handler info 'Group: 'picGroup2'  FileType: 'picftype
    dopus progress handler info2 'Launching ImageFX...'
    address command "RUN >NIL: "ImageFXPath
    port = ''
    DO UNTIL port == 'IMAGEFX.1'
      address command "Wait 1 secs"
      IF show('p','IMAGEFX.1') THEN port = 'IMAGEFX.1'
      ELSE port = ''
    END
    dopus progress handler info 'Group: 'picGroup2'  FileType: 'picftype
    dopus progress handler info2 'ImageFX loaded...'
    dopus front
    call SENDIFX
  END
  ELSE call EXIT_ME
  call EXIT_ME
END

SENDIFX:
  options failat 2
  address value portname

  dopus progress handler info 'Group: 'picGroup2'  FileType: 'picftype
  dopus progress handler info2 'Loading file...'
	filename2 = filename
  filename = '"'||filename||'"'
  address "IMAGEFX.1"
  IF erase == 0 THEN DO
    Buffer2Back
  END
  IF picGroup == 'pict' THEN DO
    IF picftype == 'REKO Cardset' THEN DO
      LOADBUFFERAS FORMAT DataType FILE filename FORCE
    END
		IF picftype == 'BMP' THEN DO
      Open(seekfile,filename2,'r')
      rv = seek(seekfile,28,'B')
			chars=readch(seekfile,1)
			close(seekfile)
			IF (chars == '04'X) | (chars == '08'X) THEN DO
				LOADBUFFERAS FORMAT Datatype FILE filename FORCE
      END
			IF (chars == '0F'X) | (chars  == '10'X) | (chars ==  '18'X) | (chars == '20'X) THEN DO
				LOADBUFFER FILE filename FORCE
			END
		END
    ELSE DO
	  	LOADBUFFER FILE filename FORCE
    END
  END
  IF picGroup == 'anim' | picGroup == 'movi' THEN DO
	  LOADBUFFER FILE filename FORCE
  END
  SCREENTOFRONT
  SETPREFS LoadPath picpath
  IF picfile == '' THEN DO
  END
  call EXIT_ME


EXIT_ME:
	address value portname
  dopus progress handler info 'Group: 'picGroup2' FileType: 'picftype
  dopus progress handler off
  exit
END

dorequest:
   parse arg reqargs
   dopus request reqargs
   returnflag = rc
   return
