/*
** $VER: Blur_quick.ieb 1.01, IE Arexx script
** Image Engineer Batch Processing script
** © by Patrik M Nydensten 
** 6/11 1996 Stockholm/Sweden
**
** Blurs image with simple scaling method.
*/

options results
signal on error

parse arg input command
input = upper(strip(input))
address 'IMAGEENGINEER'

select  /* Required batch script commands */
  when input = 'INFO' then    return get_info()
  when input = 'CONFIG' then  return get_config(command)
  when input = 'PROCESS' then return process_image(command)
  otherwise do
    'REQUEST' '"Failure in call to batch script!"' '" Quit "'
    return '<ERROR>'
  end
end

exit 0

/* Required "Get_info" procedure  ------------------------------------ */
/* S = SECONDARY, A = ALPHA, 1 = Single file, 2 = Multiple files       */

get_info:
  back = 'OK'
return back

/* Required "Get_config" procedure  ---------------------------------- */

get_config:
  parse arg '"'command'"'

  Blur=20
  
  if command ~= '' then parse var command Blur

  'IE_TO_FRONT'

  'GET_NUMBER' '"Select blur amount."' '0 300' '" OK "' Blur 'SLIDER'
  Blur = Result

  back = Blur
return back

/* Required "Process_image" procedure  ------------------------------- */

process_image:
  parse arg '"'src_image'"' '"'dst_image'"' '"'options'"'
  parse var options Blur

  'OPEN' '"'src_image'"' '24'
  if (RC ~= 0) then do
    'IE_TO_FRONT'
    'REQUEST' '"Failed to load image:' d2c(10)||src_image'"' '" OK "'
    return '<ERROR>'
  end
  else LoadImage = Result

  if Blur ~= 0 then do
    'PROJECT_INFO' LoadImage 'WIDTH'
    IW = Result
    'PROJECT_INFO' LoadImage 'HEIGHT'
    IH = Result

    NewIW = trunc(IW/(Blur+1))
    NewIH = trunc(IH/(Blur+1))

    if NewIW < 2 then NewIW = 2
    if NewIH < 2 then NewIH = 2

    'SCALE' LoadImage NewIW NewIH 'BEST'
    SmallImage = Result
    'CLOSE' LoadImage

    'SCALE' SmallImage IW IH 'BEST'
    OutputImage = Result
    'CLOSE' SmallImage
  end
  else OutputImage = LoadImage
  
  'SAVE_DATA' OutputImage '"'dst_image'"' '"ILBM CmpByteRun1"'
  if (RC ~= 0) then do
    'IE_TO_FRONT'
    'REQUEST' '"Failed to save image:' d2c(10)||dst_image'"' '" OK "'
    return '<ERROR>'
  end
  'CLOSE' OutputImage

  back = 'OK'
return back

/* Internal procedures  ---------------------------------------------- */

/*******************************************************************/
/* 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||'"'
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!'
end

return '<ERROR>'
