/*
** $VER: IE_BatchProcess.dopus5 1.21 (2/9 Stockholm/Sweden)
** Copyright © 1996 by Patrik M Nydensten 
**
** Processes all selected image files in the SRCE directory and
** saves them into DEST directory. Uses IE Batch scripts.
** Requires Image Engineer 3.0 or higher and DOpus 5.0 or higher.
** Call: (AREXX) DOpus5:arexx/IE_BatchProcess.dopus5 {Qp}
*/

CFG_BATCH_DIR = 'IE:Arexx/Batch/'
CFG_IE_PRIORITY = '-3'

options results
signal on syntax

lf='0a'x ; dq='22'x ; sq='27'x
old_ie_pri = '0'

parse arg portname
if left(portname,5)='DOPUS' then address value portname
else exit

if ~show(ports,'IMAGEENGINEER') then do
 	dopus request '"Could not locate IE'sq's arexx port!" OK'
  exit                 
end                 

/* Get info from DOpus */

lister query source                /* get source handler */
parse var result handle nil
if (RC~= 0) then exit

lister query dest                  /* get destination handler */
parse var result dhandle nil
if (RC~= 0) then do
  dopus request '"No DEST directory selected." Use SOURCE dir|Cancel'
  if (RC=1) then dhandle = handle
  else exit
end

lister query handle path           /* get current source path */
src_path = strip(result,B,'"')

lister query dhandle path           /* get current dest path */
dst_path = strip(result,B,'"')

lister query handle selfiles ':'   /* get selected file names */
sel_files = result

lister query handle numselfiles    /* get number of selected files */
num_files = result
if num_files = 0 then exit

if num_files = 1 then do  /* Ask for cloning of single image */
  dopus request '"You have only selected one single image.'lf||,
            'Would you like to clone it?" Yes, clone|No, single'
  if (rc = 1) then do
    do while ~datatype(cnumber,'N')
      dopus getstring '"Set number of clone images."' '4' '"10"' 'Clone'
      cnumber = result

      if ~datatype(cnumber,'N') then do
        dopus request '"Bad number, try again!" I'sq'll try'
      end
    end /* end while */
    num_files = cnumber ; clone_image = sel_files
  end /* do clone ? */
end

/* Get batch script and check it  -------------------------------------- */

lister new CFG_BATCH_DIR
bhandle = result

lister wait bhandle
lister set bhandle source
command none
lister set bhandle show '(#?.ieb)'
lister set bhandle title 'Select IE batch script.'
lister refresh bhandle 'full'
lister wait bhandle

do forever   /* get batch script */
  address COMMAND 'wait'
  lister query bhandle numselfiles
  if result = 1 then do
    lister query bhandle selfiles
    parse var result bscript nil
    bscript = CFG_BATCH_DIR||strip(bscript,B,'"')
    leave
  end
  lister query source
  if result ~= bhandle then do
    lister close bhandle
    dopus request '"User aborted action!" OK'
    exit
  end
end

lister close bhandle

if ~exists(bscript) then do  
  dopus request '"Unable to find IE batch script!" OK'
  exit
end
else do
  binfo = bei('INFO')  /* get extra info from batch */ 
  if word(binfo,1) ~= 'OK' then do
    dopus request '"Unable to access IE batch script!" OK'
    exit
  end
end
nil = info_add_setup(binfo)  /* setup optional extra info for batch */

/* Get config(s) from batch script  ------------------------------------ */

if num_files > 1 then do
  dopus request '"Select settings for first image." Continue'
  bopt_f = bei('CONFIG') ; dopus front
  dopus request '"Select new settings for last image?" Yes, new|No, use same'
  if (rc = 0) then bopt_l = bopt_f
  else do
    dopus request '"Select settings for last image." Continue'
    bopt_l = bei('CONFIG' sq'"'bopt_f'"'sq) ; dopus front
    dopus request '"Select settings calculation mode." Linear|Spline'
    calc_type = rc
    if calc_type ~= 1 then nil = add_mathlib()
  end
end
else do
  bopt_f = bei('CONFIG') ; dopus front
  bopt_l = bopt_f
end

dopus getstring '"Enter output filename." 25 "Output" Go!'
dst_filename = result

/* Start processing  --------------------------------------------------- */

address 'IMAGEENGINEER' 'PRIORITY_SET' CFG_IE_PRIORITY
old_ie_pri = result

address value portname
dopus front

lister set handle progress num_files 'Processing images...'

do i = 1 to num_files  /* Process loop */

  address value portname

  parse var sel_files image_file ':' sel_files
  if clone_image ~= 'CLONE_IMAGE' then image_file = clone_image
  image_file = strip(image_file,B,'"')

  lister set handle progress name image_file  /* update progress display */
  lister query handle abort                   /* check if aborted */
  if result then do
    lister clear handle progress  /* exit progress display mode */
    lister refresh handle 'full'
    lister refresh dhandle 'full'
    lister read dhandle '"'||dst_path||'"' force
    address 'IMAGEENGINEER' 'PRIORITY_SET' old_ie_pri
    dopus front
    dopus request '"User aborted..." OK'
    lister clear handle abort
    exit
  end

  bei_proc = 'PROCESS' sq'"'src_path||image_file'"'sq sq'"'dst_path||dst_filename||'.'||right(i,4,'0')'"'sq sq'"'calc_bopt(i)'"'sq get_info_add()
  say bei_proc
  nil = bei(bei_proc)

  lister select handle '"'||image_file||'"' 0  /* unselect processed file */
  lister set handle progress count i           /* update progress display */
  lister refresh handle full                   /* update lister display   */
  lister refresh dhandle full                  /* update lister display   */

end  /* end of process loop */

lister clear handle progress        /* exits progress display mode */
lister refresh handle 'full'        /* update source */
lister refresh dhandle 'full'       /* update dest */
lister read dhandle '"'||dst_path||'"' force

address 'IMAGEENGINEER' 'PRIORITY_SET' old_ie_pri

dopus front

exit

/* Procedures  ------------------------------------------------------*/

BEI:  /* Batch script caller */
  parse arg bei_input

  interpret call '"'bscript'"' bei_input
  bei_output = result

  say bei_output        

  address(portname)
return bei_output

/* Config settings twiner  ---------------------------------------- */

Calc_bopt:
  parse arg cur_filenum
  calc_output = ''
  bopt_fx = bopt_f ; bopt_lx = bopt_l
  if (bopt_fx = bopt_lx) then calc_output = bopt_fx
  else do forever
    parse var bopt_fx val_fx bopt_fx 
    parse var bopt_lx val_lx bopt_lx 
    val_fx = strip(val_fx) ; val_lx = strip(val_lx)
    if ((val_fx='')|(val_lx='')) then leave
    if ((left(val_fx,1)='#')|(left(val_lx,1)='#')) then calc_output = calc_output val_fx
    else do   /* do linear/spline calculation */
      select
        when calc_type = 1 then calc_output = calc_output trunc( (val_fx+(val_lx - val_fx)*(cur_filenum-1)/(num_files-1)) )
        when calc_type = 0 then calc_output = calc_output trunc( val_lx-(((cos(3.14159265*(cur_filenum-1)/(num_files-1))+1)/2)*(val_lx-val_fx)) )
        otherwise nop
      end /* select */
    end  /* do calc */
  end
return strip(calc_output)

add_mathlib:
  if ~show(L,'rexxmathlib.library') then do
    if exists('LIBS:rexxmathlib.library') then do
      if ~addlib('rexxmathlib.library',0,-30,0) then do
        dopus request '"Failed to load libs:rexxmathlib.library!" OK'
      end
    end /* lib found on disk */
    else do
      dopus request '"Failed to find libs:rexxmathlib.library!" OK'
    end
  end /* lib exists in mem */
return 'OK'

/* Output optional extra info if necessary  ---------------------------- */

get_info_add:
  info_output = ''

  if pos('S',add_input)~=0 then do
    if sec_force = 1 then info_output = sq'"'sec_path||strip(sec_filelist,B,'"')'"'sq
    else do
      parse var sec_filelist file ':' sec_filelist
      info_output = sq'"'sec_path||strip(file,B,'"')'"'sq
    end
  end
  if pos('A',add_input)~=0 then do
    if alp_force = 1 then info_output = info_output sq'"'alp_path||strip(alp_filelist,B,'"')'"'sq
    else do
      parse var alp_filelist file ':' alp_filelist
      info_output = info_output sq'"'alp_path||strip(file,B,'"')'"'sq
    end
  end
  if pos('F',add_input)~=0 then do
    info_output = info_output sq'"'i':'num_files'"'sq
  end

return strip(info_output)

info_add_setup:
  parse arg add_input

/* Retrieve Secondary/Alpha image(s)  ---------------------------------- */ 

  if pos('S',add_input)~=0 then do
    if substr(add_input,pos('S',add_input)+1,1) = '1' then sec_files = 1
    else sec_files = num_files

    if sec_files > 1 then do  /* select multifile */
      dopus request '"This Batch script requires that you select'lf,
                      sec_files 'SECONDARY images." Continue|Force single'
      if rc = 0 then do
        sec_files = 1 ; sec_force = 1
      end
    end
    else do  /* select single file */
      dopus request '"This Batch script requires that you select'lf,
                      'one SECONDARY image." Continue'
    end

    lister new src_path
    bhandle = result
    
    lister wait bhandle
    lister set bhandle source
    command none
    lister set bhandle title 'Select' sec_files 'SECONDARY image(s).'
    lister refresh bhandle 'full'
    lister wait bhandle
    
    do forever   /* get batch script */
      address COMMAND 'wait'
      lister query bhandle numselfiles
      if result = sec_files then do
        lister query bhandle selfiles ':'
        parse var result sec_filelist
        leave
      end
    end
    lister query bhandle path
    sec_path = strip(result,'B','"')
    lister close bhandle
  end  /* end of get SEC */

  if pos('A',add_input)~=0 then do
    if substr(add_input,pos('A',add_input)+1,1) = '1' then alp_files = 1
    else alp_files = num_files

    if alp_files > 1 then do  /* select multifile */
      dopus request '"This Batch script requires that you select'lf,
                      alp_files 'ALPHA images." Continue|Force single'
      if rc = 0 then do
        alp_files = 1 ; alp_force = 1
      end
    end
    else do  /* select single file */
      dopus request '"This Batch script requires that you select'lf,
                      'one ALPHA image." Continue'
    end

    lister new src_path
    bhandle = result
    
    lister wait bhandle
    lister set bhandle source
    command none
    lister set bhandle title 'Select' alp_files 'ALPHA image(s).'
    lister refresh bhandle 'full'
    lister wait bhandle

    do forever   /* get batch script */
      address COMMAND 'wait'
      lister query bhandle numselfiles
      if result = alp_files then do
        lister query bhandle selfiles ':'
        parse var result alp_filelist
        leave
      end
    end
    lister query bhandle path
    alp_path = strip(result,'B','"')
    lister close bhandle
  end  /* end of get ALP */

return 'OK'

Syntax:
  syntax_rc = rc
  lister clear handle progress        /* exits progress display mode */
  lister refresh handle 'full'        /* update source */
  lister refresh dhandle 'full'       /* update dest */
  lister read dhandle '"'||dst_path||'"' force
  address 'IMAGEENGINEER' 'PRIORITY_SET' old_ie_pri
  dopus front
  dopus request '"Internal error on line' SIGL || lf || 'Error:' syntax_rc errortext(syntax_rc) '" OK'
  exit
return 0
