/*
 * $VER: Snapshot_Engine 3.0 (17.2.98)
 *
 * Arexx program for the ImageFX image processing system.
 * Written by Thomas Krehbiel (from an idea by Bob Fisher)
 *
 * Take a directory of images, and reduce them to small thumbnail
 * snapshot images, and place them on one or more images which are
 * then saved out somewhere.  This script also renders the snapshot
 * pages down to 16 color images for easy viewing.  We can also
 * optionally stamp the name of the file at the bottom of each
 * thumbnail.
 *
 * By default we make 640x400 24-bit renderings with a 5x5 grid
 * of thumbnails.
 *
 * This script is just the "engine" of the snapshot routine; it takes
 * a file of images supplied as a command line argument and snapshots
 * them.
 *
 */

OPTIONS RESULTS

/*
 * Useage:  Snapshot_Engine <title> <listfile>
 */
PARSE ARG sourcedir filelist .

SIGNAL ON BREAK_C

/* retreive defaults from any previous useage of this macro */
destbase = GETCLIP('IFX_Snapshot_DefDest')
numcols  = GETCLIP('IFX_Snapshot_Colors')
gridsize = GETCLIP('IFX_Snapshot_Grid')
width    = GETCLIP('IFX_Snapshot_Width')
height   = GETCLIP('IFX_Snapshot_Height')
labelit  = GETCLIP('IFX_Snapshot_Labels')
grey     = GETCLIP('IFX_Snapshot_Grey')
keepext  = GETCLIP('IFX_Snapshot_KeepExt')
saveform = GETCLIP('IFX_Snapshot_SaveForm')
saveopt  = GETCLIP('IFX_Snapshot_SaveOpts')
spacing  = GETCLIP('IFX_Snapshot_Spacing')
enhance  = GETCLIP('IFX_Snapshot_Enhance')
doback   = GETCLIP('IFX_Snapshot_DoBack')
backdrop = GETCLIP('IFX_Snapshot_Backdrop')
doshadow = GETCLIP('IFX_Snapshot_DoShadow')
red      = GETCLIP('IFX_Snapshot_Red')
green    = GETCLIP('IFX_Snapshot_Green')
blue     = GETCLIP('IFX_Snapshot_Blue')

/* some defaults - change as desired */

IF defdest  = '' THEN defdest  = "RAM:.Snap" /* default dest files */
IF numcols  = '' THEN numcols  = 8           /* colors in renderings */
IF gridsize = '' THEN gridsize = 5           /* thumbnail rows/cols */
IF width    = '' THEN width    = 640         /* dest width */
IF height   = '' THEN height   = 400         /* dest height */
IF labelit  = '' THEN labelit  = 1           /* label each thumbnail? */
IF grey     = '' THEN grey     = 0           /* convert to greyscale? */
IF keepext  = '' THEN keepext  = 1           /* keep file extensions? */
IF saveform = '' THEN saveform = 'ILBM'      /* save format */
IF spacing  = '' THEN spacing  = 10          /* spacing between nails */
IF enhance  = '' THEN enhance  = 1           /* enhance nails? */
IF doback   = '' THEN doback   = 0           /* do backdrop texture? */
IF backdrop = '' THEN backdrop = 'Textures/Leather'   /* backdrop texture */
IF doshadow = '' THEN doshadow = 1           /* do shadowed nails? */
IF red      = '' THEN red      = 128
IF green    = '' THEN green    = 100
IF blue     = '' THEN blue     = 140


SELECT
   WHEN numcols = 0 THEN numcols = 2
   WHEN numcols = 1 THEN numcols = 4
   WHEN numcols = 2 THEN numcols = 8
   WHEN numcols = 3 THEN numcols = 16
   WHEN numcols = 4 THEN numcols = 32
   WHEN numcols = 5 THEN numcols = 64
   WHEN numcols = 6 THEN numcols = 128
   WHEN numcols = 7 THEN numcols = 256
   WHEN numcols = 8 THEN numcols = 'DEEP'
   WHEN numcols = 9 THEN numcols = 'PRINT'
   OTHERWISE EXIT 0
   END


thumbxspace = (width / gridsize)
thumbyspace = ((height-20) / gridsize)

thumbwidth = thumbxspace - spacing
thumbheight = thumbyspace - spacing

picnum = 1


/* okay, process the files */

IF ~OPEN(infile, filelist, 'Read') THEN DO
   RequestNotify 'Cannot read file list' filelist ' - no files found?'
   EXIT
   END

IF numcols = 'DEEP' THEN DO
   END
ELSE IF numcols = 'PRINT' THEN DO
   SetPrinter 'SuperPrefs'
   END
ELSE DO
   SetRender 'Amiga'
   Render Mode HIRES LACE
   Render Colors numcols
   Render Dither 1 2 0     /* floyd zigzag none */
   END

Requesters Off    /* disable error requesters */
Redraw Off
Undo Off

Blend 100
EdgeMode Normal
Transparency Include 0 Exclude 0

/* force new buffer the first time: */
x = 999
y = 999
pagenum = 0

DO WHILE ~EOF(infile)

   x = x + 1
   IF x >= gridsize THEN DO
      x = 0
      y = y + 1
      IF y >= gridsize THEN DO

         GetMain
         IF rc = 0 THEN DO
            IF numcols = 'DEEP' THEN DO
               SaveBufferAs saveform destbase||RIGHT('000'||picnum,3) saveopt
               END
            ELSE IF numcols = 'PRINT' THEN DO
               Printer Print
               END
            ELSE DO
               Render Go
               SaveRenderedAs saveform destbase||RIGHT('000'||picnum,3) saveopt
               Render Close
               END
            END

         pagenum = pagenum + 1
         picnum = picnum + 1

         IF numcols = 'PRINT' THEN DO
            dpix = 100
            dpiy = 100
            aspx = 1
            aspy = 1
            END
         ELSE DO
            dpix = 100
            dpiy = 100
            aspx = 22
            aspy = 26
            END

         IF grey THEN CreateBuffer width height 'GREY' red green blue aspx aspy dpix dpiy FORCE
         ELSE         CreateBuffer width height red green blue aspx aspy dpix dpiy FORCE

         IF doback THEN DO
            Hook ApplyTexture backdrop 0 80 Shiny
            END

         title = sourcedir||' ('||pagenum||')'

         ActiveColor 0     /* usually black */
         Text 'topaz.font' 11 100 BOLD title
         Blend 50
         Point width%2+2 9
         Blend 100
         KillBrush

         ActiveColor 1     /* usually white */
         Text 'topaz.font' 11 100 BOLD title
         Point width%2 7
         KillBrush

         Redraw
         y = 0

         END
      END

   nextfile = READLN(infile)
   IF nextfile = "" THEN ITERATE
   IF RIGHT(nextfile,5) = '.info' THEN ITERATE
   Message nextfile

   xp = x*thumbxspace
   yp = y*thumbyspace+20

   LoadBrush nextfile 1       /* 1 in case we find an anim */
   FlattenLayers              /* new in V109 */

   IF rc = 0 THEN DO

      GetBrush
      PARSE VAR result name w h d ax ay .

      IF grey THEN DO
         IF (d > 1) THEN Color2Grey Luma
         END

      IF (ax > 0) THEN h = h * ay % ax

      IF (w > h) THEN DO
         nw = thumbwidth
         nh = h * nw % w
         IF (nh > thumbheight) THEN nh = thumbheight
         END
      ELSE DO
         nh = thumbheight
         nw = w * nh % h
         IF (nw > thumbwidth) THEN nw = thumbwidth
         END

      IF (w < thumbwidth) & (h < thumbheight) THEN
         Scale nw nh FAST
      ELSE
         Scale nw nh

      /* a little enhancement - makes 'em stand out more */
      IF enhance THEN DO
         Contrast 10
         UnsharpMask 8
         END

      IF doshadow THEN DO
         DrawMode Darken
         EdgeMode FeatherIn 4
         Point xp+thumbxspace%2+4 yp+thumbyspace%2+4
         DrawMode Normal
         EdgeMode Normal
         END

      Point xp+thumbxspace%2 yp+thumbyspace%2
      KillBrush

      END

   IF labelit THEN DO

      /* strip path and optionally the trailing extension */
      n = LASTPOS('/', nextfile) + 1
      label = SUBSTR(nextfile,n)
      IF ~keepext THEN DO
         n = 1
         DO WHILE n > 0
            n = LASTPOS('.', label)
            IF (n > 0) THEN label = LEFT(label,n-1)
            END
         END

      ActiveColor 0     /* usually black */
      Text 'topaz.font' 8 100 label
      Blend 50
      Point xp+thumbxspace%2+1 yp+thumbheight-6
      Blend 100
      KillBrush

      ActiveColor 1     /* usually white */
      Text 'topaz.font' 8 100 label
      Point xp+thumbxspace%2 yp+thumbheight-7
      KillBrush

      END

   Redraw xp yp thumbxspace thumbyspace

   END

IF (x > 0) | (y > 0) THEN DO
   IF numcols = 'DEEP' THEN DO
      SaveBufferAs saveform destbase||RIGHT('000'||picnum,3) saveopt
      END
   ELSE IF numcols = 'PRINT' THEN DO
      Printer Print
      END
   ELSE DO
      Render Go
      SaveRenderedAs saveform destbase||RIGHT('000'||picnum,3) saveopt
      Render Close
      END
   END

KillBuffer Force

BREAK_C:

CALL CLOSE(infile)

KillBrush
Undo On
Redraw On
Requesters On

EXIT

