/* changed getstring to get number. */

/*
**      Batchconverter.rexx
**
**      $VER Batchconverter.rexx 1.0 (27 May 1996)
**
**      ©1996 Ramiro Garcia
**
**      This program allows the user to select via requesters:
**
**      A source directory full of pictures to batch process,
**      A destination directory to put them in,
**      and IFF, IFF-HAM, GIF, or JPEG output formats.
**
**      There are also options to control the number of colors
**      in the output IFFs, and the picture quality/compression
**      of the JPEGs.
**
**
*/


/*******************************************************************/
/* Here we set the default number of colors to render in,          */
/* default Vertical Resolution, and other important parameters     */
/*******************************************************************/

NUMBER_OF_COLORS=256
HALVE=NO

INTERLACED=2
NONINTERLACED=0

JPQUAL=32

VRES = INTERLACED
TELLRES="NonInterlaced"

BITBUCKET=0

NL = '0A'X
SQ = '27'X
DQ = '22'X
TRUE  = 1
FALSE = 0

HIGHLIGHT_ON ='1B'X||'[0;43;32m'
HIGHLIGHT_OFF ='1B'X||'[0;40;31m'
SYSTEM_ALERT='07'x

options results

/* trace all */

/*******************************************************************/
/* Find ADPro's AREXX port, start ADPro otherwise.                 */
/* You must have "ADPro:" assigned, or ADPro already running       */
/*******************************************************************/

SAY "                                            " HIGHLIGHT_ON "If you clicked on the icon"  HIGHLIGHT_OFF
SAY "                                            " HIGHLIGHT_ON "to run BatchIFF, you can"  HIGHLIGHT_OFF
SAY "                                            " HIGHLIGHT_ON "drag this window down to"  HIGHLIGHT_OFF
SAY "                                            " HIGHLIGHT_ON "See some useful messages." HIGHLIGHT_OFF
say ""

ADDRESS COMMAND "C:wait 2 secs"


IF ~SHOW('P','ADPro') THEN DO
                            ADDRESS COMMAND "run < nil: > nil: adpro:adpro MAXMEM=4500000 MINIMIZED LISTINTERFACE NL NS"

/* Wait up to 30 secs (10 seconds each line) for ADPro to appear. */

                            ADDRESS COMMAND "WAITFORPORT ADPro"
                            ADDRESS COMMAND "WAITFORPORT ADPro"
                            ADDRESS COMMAND "WAITFORPORT ADPro"
                      END
                      ELSE DO
                          address 'ADPro'
                          INTERFACE_SIZE TINY
                          SET_ADPRO_PUBLIC
                          IF ADPRO_RESULT~="Workbench" THEN SET_ADPRO_PUBLIC Workbench
                          say ADPRO_RESULT
                      END


IF ~SHOW('P','ADPro') THEN DO
        SAY "Could not locate ADPro."
        Call Exit_Routine
END

address 'ADPro'

version

say "ADPro Version="HIGHLIGHT_ON  ADPRO_RESULT HIGHLIGHT_OFF
say "Version must be "HIGHLIGHT_ON "2.5.0 "HIGHLIGHT_OFF "or greater."

/*******************************************************************/
/* Select IFF, IFF-HAM, GIF, or JPEG                               */
/*******************************************************************/


SAY  "Please select IFF, HAM, GIF, or JPEG for your output pictures."

/*OKAYN Title                            Text               GadgetLabels */

OKAYN '"Output Picture Format"' '"Select the output picture format"',
       "IFF|IFF-HAM|GIF|JPEG"


IF (RC=0) THEN DO
                  SAVETYPE=JPEG
                  Call JPEG_Quality
               END


IF (RC=1) THEN DO
                  SAVETYPE=IFF
                  Call How_Many_Colors
                  Call Interlace_mode
               END

IF (RC=2) THEN DO
                  SAVETYPE=IFF
                  NUMBER_OF_COLORS=HAM
                  say "The output pictures will be rendered with a "HIGHLIGHT_ON" 4096 color, HAM "HIGHLIGHT_OFF" palette."
                  Call Halve_Horizontal
                  Call Interlace_mode
               END


IF (RC=3) THEN DO
                  SAVETYPE=GIF
                  NUMBER_OF_COLORS=256
                END

IF (RC>3) THEN Say "A problem has occurred while choosing the file type."



say "The output pictures will be saved as "HIGHLIGHT_ON SAVETYPE HIGHLIGHT_OFF" pictures."


/*******************************************************************/
/*Select the source directory, with the pictures to be processed.  */
/*******************************************************************/


PARSE ARG Arguments
PARSE VAR Arguments '"'Title'"' '"'DefaultDir'"' IsRequired


SAY  "Please select the directory with the pictures to be processed."

Title = DQ || "Please select the directory with the source pictures." || DQ


/* DefaultDir = DQ || DefaultDir || DQ */
DefaultDir = DQ  DQ

IsRequired = TRUE

continue = TRUE

DO WHILE (continue = TRUE)
        ADPRO_TO_FRONT

        GETDIR Title DefaultDir
        EnteredDir = ADPRO_RESULT
        IF (RC ~= 0) THEN DO
                IF (IsRequired = TRUE) THEN DO
                        ADPRO_TO_FRONT

                        OKAYN '"GetADir"' '"This value is required."' '"Retry|Cancel"'
                        IF (RC = 0) THEN DO
                                EnteredDir = DQ || DQ
                                continue = FALSE
                        END
                END
                ELSE DO
                        EnteredDir = DQ || DQ
                        continue = FALSE
                END
        END
        ELSE
                continue = FALSE
END


/* If the user has clicked on the "Cancel" button, or */
/* If the user has clicked on "OK" without selecting anything, then: */

IF ((EnteredDir = DQ || DQ)|(EnteredDir = '')) THEN DO
   SAY "You need to select a source directory."
   Call Exit_Routine
END


Call Check_directory_format

SourceDirectory = EnteredDir

ADPRO_TO_FRONT


/*******************************************************************/
/* Select a destination directory for the processed pictures.      */
/*******************************************************************/

PARSE ARG Arguments
PARSE VAR Arguments '"'Title'"' '"'DefaultDir'"' IsRequired


SAY "Please select a different directory for the finished pictures."

Title = DQ || "Select a different directory to place the processed pictures." || DQ

/* Force the ASL requester to display available volumes. */

DefaultDir = DQ  DQ

IsRequired = TRUE

continue = TRUE

DO WHILE (continue = TRUE)
        ADPRO_TO_FRONT

        GETDIR Title DefaultDir
        EnteredDir = ADPRO_RESULT
        IF (RC ~= 0) THEN DO
                IF (IsRequired = TRUE) THEN DO
                        ADPRO_TO_FRONT

                        OKAYN '"GetADir"' '"This value is required."' '"Retry|Cancel"'
                        IF (RC = 0) THEN DO
                                EnteredDir = DQ || DQ
                                continue = FALSE
                        END
                END
                ELSE DO
                        EnteredDir = DQ || DQ
                        continue = FALSE
                END
        END
        ELSE
                continue = FALSE
END


/**
 ** If the user has clicked on the "Cancel" button, or
 ** If the user has clicked on the "OK" button
 ** without selecting anything, then:
 **/


IF ((EnteredDir = DQ || DQ)|(EnteredDir = '')) THEN DO
   SAY "You need to select a destination directory."
   Call Exit_Routine
END

Call Check_directory_format

DestinationDirectory = EnteredDir

/* Now we have a place to put the pictures. */

/* Keep source and destination directories separate. */

If (DestinationDirectory = SourceDirectory) THEN DO
   SAY ""
   SAY "The destination directory must be different from the source directory."
   say "Please put the processed pictures someplace other than "HIGHLIGHT_ON SourceDirectory HIGHLIGHT_OFF""
   Call Exit_Routine
   END


/*******************************************************************/
/* Tell the user what we are about to do, and                      */
/* Give the user a last chance to bail out                         */
/*******************************************************************/

/* ADDRESS COMMAND 'ECHO "We will process the following pictures in the *E[0m*E[43;32m'SourceDirectory'*E[0m*E[40;31m directory:"' */
say "We will process the following pictures in the "HIGHLIGHT_ON SourceDirectory HIGHLIGHT_OFF" directory:"

/* Enable highlighting */

say ""HIGHLIGHT_ON

ADDRESS COMMAND 'dir 'DQ||SourceDirectory||"~(pic.list)"||DQ' FILES'

/* Disable highlighting */

say  HIGHLIGHT_OFF""

ADDRESS COMMAND  'list TO T:pic.list 'DQ||SourceDirectory||DQ' P=~(#?.info|pic.list) NOHEAD FILES QUICK'

say "And place the processed  pictures in the "HIGHLIGHT_ON DestinationDirectory HIGHLIGHT_OFF" directory."

OKAYN '"Ready to Process Pictures"' '"Do you wish to proceed?"' '"Yes|No"'
IF (RC = 0) THEN Call Exit_Routine

SAY "Hold on to your Pixels!"

/*******************************************************************/
/* Read the list of files, load them, process them, and save them. */
/*******************************************************************/

/* Read the list */

PIC_LIST = "t:pic.list"


if OPEN('in' , PIC_LIST , 'r') ~= 1 then do
        okay1 "Could Not Open" PIC_LIST
        Call Exit_Routine
end

/* Load the files */

AVAIL_MODES_ONLY OFF   /* This allows us to render AGA pics on ECS machine */

in_file = READLN('in')

/* Test to see if we can touch the first file on the list */

IF ~(OPEN('canaryfile',SourceDirectory||in_file,'R')=1) THEN DO
 SAY "I tried to open "SourceDirectory||in_file
 okay1 "Could Not Open" SourceDirectory||in_file
 Call Exit_Routine
END

BITBUCKET=CLOSE('canaryfile')

/* Try to load the first file on the list into ADPro */

do while in_file ~= ""
        lformat "UNIVERSAL"
        load DQ||SourceDirectory||in_file||DQ
        if RC ~= 0 then do
                okay1 "Could Not Load" SourceDirectory||in_file
                Call Exit_Routine
        end

/* Process and save the pictures */

        SFORMAT SAVETYPE

        IF (SAVETYPE=JPEG) THEN DO
                   save DQ||DestinationDirectory||in_file||DQ "RAW" JPQUAL
                   END

                   ELSE DO
                   screen_type VRES
                   render_type NUMBER_OF_COLORS
                   dither 1
                   IF (HALVE=YES) THEN PCT_SCALE 50 100
                   execute
                   save DQ||DestinationDirectory||in_file||DQ "IMAGE"
                   END

        in_file = READLN('in')
end

say SYSTEM_ALERT

say HIGHLIGHT_ON "All Done!" HIGHLIGHT_OFF

okay1 "All Done!"

Call Exit_Routine

/*******************************************************************/
/* Select the JPEG Quality for the saved pictures.                 */
/*******************************************************************/

JPEG_Quality:

SAY  "Please select the the JPEG quality for your output pictures."
SAY  "Higher=Better."

/*GETNUMBER Title                      Default  Min  Max */

GETNUMBER '"Select the JPEG Quality"' JPQUAL    2    1000
IF RC~=0 THEN Call Exit_Routine

 JQUAL=ADPRO_RESULT

say "The output pictures will have a "HIGHLIGHT_ON JPQUAL HIGHLIGHT_OFF" JPEG Quality."


RETURN

/*******************************************************************/
/* Select the number of colors for the output pictures.            */
/*******************************************************************/

How_Many_Colors:

SAY  "Please select the number of colors for your output pictures."

GETNUMBER '"Enter 2, 4, 8, 16, 32, 64, 128, or 256 Colors"' NUMBER_OF_COLORS

IF RC~=0 THEN Call Exit_Routine

NUMBER_OF_COLORS=ADPRO_RESULT

IF ~((NUMBER_OF_COLORS=  2)|(NUMBER_OF_COLORS=   4)|(NUMBER_OF_COLORS=  8)|,
     (NUMBER_OF_COLORS= 16)|(NUMBER_OF_COLORS=  32)|(NUMBER_OF_COLORS= 64)|,
     (NUMBER_OF_COLORS=128)|(NUMBER_OF_COLORS= 256)|(NUMBER_OF_COLORS=EHB)|,
     (NUMBER_OF_COLORS=HAM)|(NUMBER_OF_COLORS=HAM8)) THEN DO
        SAY "You must enter only one of the following:"
        SAY "2 4 8 16 32 64 18 256"
        SAY "EHB HAM HAM8"
        SAY "With no leading spaces"
        SAY "You picked "HIGHLIGHT_ON NUMBER_OF_COLORS HIGHLIGHT_OFF
        address command 'wait 6 secs'
        Call Exit_Routine
END

say "The output pictures will be rendered in "HIGHLIGHT_ON NUMBER_OF_COLORS HIGHLIGHT_OFF" colors."

RETURN

/*******************************************************************/
/* Select Interlace or NonInterlaced Vertical Resolution           */
/*******************************************************************/

Interlace_mode:

SAY  "Please select Interlaced or NonInterlaced for your output pictures."

/*OKAYN Title                            Text               GadgetLabels */

OKAYN '"Interlaced or NonInterlaced?"' '"Select your vertical resolution"',
       "Interlaced|NonInterlaced"

IF (RC=0) THEN DO
      VRES=NONINTERLACED
      TELLRES="NonInterlaced"
      END
ELSE DO
      VRES=INTERLACED
      TELLRES="Interlaced"
END

say "The output pictures will be rendered in "HIGHLIGHT_ON TELLRES HIGHLIGHT_OFF" mode."

RETURN

/*******************************************************************/
/* Squeeze the pictures in half horizontally?                      */
/*******************************************************************/

Halve_Horizontal:

TELLSQUEEZE="not be"

SAY  "Would you like the output pictures halved in width?"

/*OKAYN Title                            Text               GadgetLabels */

OKAYN '"Halve Width?"' '"Shrink the pictures in half horizontally?"',
                        "Shrink|Don't_Shrink"

IF ~(RC=0) THEN DO
              HALVE=YES
              TELLSQUEEZE="be"
           END


say "The output pictures will "HIGHLIGHT_ON TELLSQUEEZE HIGHLIGHT_OFF"  halved."

RETURN


/*******************************************************************/
/* Check for :t, t, ram:t, ram:t/, and process accordingly         */
/*******************************************************************/

Check_directory_format:

If  (index(EnteredDir,':')=0 | index(EnteredDir,':')=1) then DO
   say ""
   say "The program has aborted."
   say "You must enter an absolute pathname, like ""DH1:pics"","

         say "or ""Work:graphics/framestores"", or ""SQ0:""."
         say ""
         say "Entering" '"'EnteredDir'"' "won't work."
         say ""
         say "Try clicking on the ""Volumes"" button in the requester,"
         say "and then selecting the directory you want."
         Call Exit_Routine
         END


/* If there is not a slash or a colon at the end of the pathname, append a slash */

IF   ~(RIGHT(EnteredDir,1)='/' | RIGHT(EnteredDir,1)=':'),
 THEN EnteredDir=EnteredDir||'/'

RETURN


/*******************************************************************/
/* Happy trails, and I hope your picture conversion turned out OK. */
/*******************************************************************/

/* This is a line that has been commented out. */

Exit_Routine:

ADPRO_EXIT

address command 'wait 3 secs'

EXIT

return