#!/bin/bash
#
# IM Examples Logo Generator...
#
####
#
# Anthony Thyssen  <A.Thyssen@grffith.edu.au>
#
# Debuging Level: 1 Text Selection   2 Logo Manipulation
#                 3 Text/Curve Positions
#                 4 'Gel' Effect     5 'Aqua' Effect
#                 9 Full Logo Test   0 Final Logo Save
testing=0
type=1      # 1 = logo   2 = avatar   3 = splash


# Output what type of image.
case $type in
1) # IM Examples Logo (180x180)
   file="logo.gif"
   bgnd='( logo: -resize x180 -gravity center -crop 180x180+0+0! )'
   ;;
2) # Anthony's Avatar for IM forum (Size 90x90, 12Kb max)
   file="avatar.gif"
   avatar="$HOME/www/images/dragon_faerie.gif"
   bgnd="-resize 45% ( $avatar -resize 90x90 -background white -splice 18x0 )"
   ;;
3) # Simple Splash for IM forum (Size 90x90, 12Kb max, grey bgnd)
   file="splash.gif"    grey='#DCE1E5'
   bgnd="-resize 90% ( -size 90x90 xc:$grey )"
   post="-transparent $grey -transparent-color $grey"
   ;;
4) # Rotated logo
   file="logo.gif"
   bgnd="logo: -rotate 20 -trim +repage
         -resize x180 -gravity center -crop 180x180+0+0
         -flop -repage 180x180+0+0 info: -background white -flatten -flop"
   ;;
esac

# Splash text, color, fonts, offset
case 1 in
1) text='IM\nExamples'
   c_color=Green  f_color=Gold
   font=ArtBrushM f_size=20  wave=-15 rotate=-30  geom=+19+10
   ;;
2) text="Fred's\\nMagick"
   c_color=Yellow  f_color=Navy
   font=Harquil  f_size=20  wave=-15 rotate=-30  geom=+17+12
   ;;
esac

# testing outputs to screen
[ $testing -gt 0 ] && file="x:"


# Curve object, in 100x100 box
# Also generate a magick display of control lines
curve='M 12,27  C 7,37  18,50 18,60  S  0,80 10,94
       S 40,74 50,78  S 60,99 76,95  S 72,70 75,65
       S 95,55 95,42  S 69,37 66,32  S 67,2  53,7
       S 43,17 35,22  S 17,17 12,27  Z'
c_ctrls=`echo $curve |\
           sed '1s/\([0-9]\)  *\([0-9]\)/\1 M \2/;
                s/S/M/g; s/C/ /;' -`

# -------------------------

case $testing in
1)  # Font Readability Check:
    magick -background $c_color  -fill $f_color \
            -font $font -pointsize $f_size   -size 100x \
            -gravity center    caption:'IM\nExamples' \
            -wave ${wave}x200 -rotate $rotate  -trim +repage \
            $file &
    exit
    ;;
2)  # Logo Manipluation (2 seems good, but currenly not used)
    case 2 in
    1) magick $bgnd -swirl 70  $file & ;;
    2) magick $bgnd -wave -10x70  $file & ;;
    9) magick  \
            logo: -rotate 20 -trim +repage \
            -resize x180 -gravity center -crop 180x180+0+0 \
            -flop -repage 180x180+0+0 info: -background white -flatten -flop \
            $file & ;;
    *) magick $bgnd
            logo: -resize x180 -gravity center -crop 180x180+0+0\! \
            $file & ;;
    esac
    exit
    ;;
3)  # Check the draw path curves and control points, and logo position...
    magick -size 100x100 xc:none -fill $c_color \
        -draw "path '$curve'" \
        -draw "fill none stroke red stroke-width 1 \
                path '$c_ctrls'" \
          \( logo: -resize x180 -gravity center -crop 180x180+0+0\! \
            \) +swap -gravity SouthWest -composite \
          \( -background None  -fill $f_color \
            -font $font -pointsize $f_size   -size 100x \
            -gravity center    caption:"$text" \
            -wave ${wave}x200 -rotate $rotate  -trim +repage \
            \) -gravity SouthWest -geometry $geom  -composite \
          $file &
    exit
    ;;
4)  # 'Gel' Effect testing
    magick -size 100x100 xc:none -fill $c_color \
        -draw "path '$curve'" \
          \( +clone -fx A +matte  -blur 0x15  -shade 130x0 -normalize \
            -sigmoidal-contrast 12,70% -evaluate Multiply .3 -roll +10+13 \
            +clone  -compose Screen -composite \
            \) -matte  -compose In  -composite \
          -background white -flatten \
          $file &
    exit
    ;;
5)  # 'Aqua' Effect
    magick -size 100x100 xc:none -fill $c_color \
        -draw "path '$curve'" \
          \( +clone  -fx A  +matte -blur 0x15  -shade 120x45  -normalize \
            -fx "`im_fx_curves -p  0,20  50,50  80,50  100,90 `" \
            +clone  -compose Overlay -composite \
            \) -matte  -compose In  -composite \
          -background white -flatten \
          $file &
    exit
    ;;
esac


# ----------------------

# Generate the fill IM logo with a Green 'Aqua' splash for "IM Examples"
magick -size 100x100 xc:none -fill $c_color \
        -draw "path '$curve'" \
        \( +clone  -fx A  +matte -blur 0x15  -shade 120x45  -normalize \
           -fx "`im_fx_curves -p  0,20  50,50  80,50  100,90 `" \
           +clone  -compose Overlay -composite \
           \) -matte  -compose In  -composite \
        \( +clone -fx A  +matte -blur 0x3  -shade 0x90 -normalize \
           -blur 0x1  -fx '1-(1-u)*.4' -roll -1-2 \
           +clone  -compose Multiply -composite \) \
        -matte  -compose In  -composite \
        \( -background None -fill $f_color \
           -font $font -pointsize $f_size   -size 100x \
           -gravity center    caption:"$text" \
           -wave ${wave}x200 -rotate $rotate  -trim +repage \
           \) -gravity SouthWest -geometry $geom -compose over -composite \
        $bgnd +swap +geometry -gravity SouthWest -composite \
        $post $file

expr "$file" : '.*:' >/dev/null && exit

chmod 644 $file

# output a favicon  as well.
  magick -size 100x100 xc:none -fill $c_color \
       -draw "path '$curve'" \
        \( -background None -fill $f_color \
           -font $font -pointsize $f_size   -size 100x \
           -gravity center    caption:"$text" \
           -wave ${wave}x200 -rotate $rotate  -trim +repage \
           \) -gravity SouthWest -geometry $geom -compose over -composite \
        -resize 16x16  ico:- | magick ico:- favicon.ico
chmod 644 favicon.ico

