#!/usr/bin/awk -f
# makehtmlgallery4 -- makes HTML image gallery and thumbnail page
#   - based on makehtmlgallery2
#   - based on makehtmlgallery and makesize-800
#   - based on makehtmlthumbs
#   - requires ImageMagick
#
#   http://www.tnt.uni-hannover.de/~purnhage/software/makehtmlgallery4
#   http://www.imagemagick.org/www/convert.html
#
# This program is available under the terms of the GNU General Public
# License (GPL). All usual disclaimers apply!
#
# Heiko Purnhagen   (purnhage@tnt.uni-hannover.de)
# HP 20021127 20021128 20030317 20030827 20040926
# modified by Carsten Stolzenbach
# CS 20030817 20030827

function makepict(img1, img2, img3, img2caption, title) {
  img2html = destdir "/" img2 key "_.html";
  if (curpicts == thumbcols) {
    print "</TR>";
    curpicts = 0;
    curcols = curcols + 1;
  }
  if (curpicts == 0)
    print "<TR ALIGN=center VALIGN=middle>";
  curpicts = curpicts+1;
  system ("rm -f " img2html);
  printf "<TD " tdstring;
#  if ((curpicts+curcols)%2) {
#    printf " BGCOLOR=\"#b0b0b0\"";
#  }
#  else {
#    printf " BGCOLOR=\"#d0d0d0\"";
#  }
  printf "><A HREF=\"%s\"><IMG ",img2 key "_.html";
# if you have ImageMagick 5.2 or newer, use
  system ("identify -format \"WIDTH=%w HEIGHT=%h\" " destdir "/thumbs/" img2 "_thumb.jpg");
# otherwise, stay with
#  system ("identify " destdir "/thumbs/" img2 "_thumb.jpg | awk '{split($2,y,\"[^0-9]\") ; printf \"WIDTH=%d HEIGHT=%d\",y[1],y[2]}'");
  printf " SRC=\"thumbs/%s_thumb.jpg\" ALT=\"%s\" BORDER=0></A>",img2,img2;
  if (idxname) {
    printf "<BR><TT>%s</TT>",img2;
  }
  printf "</TD>\n";
# if filenames should be links as well, use the following instead
#  printf " SRC=\"thumbs/%s_thumb.jpg\"><br>%s</A></TD>\n",img2,img2;

  print "<HTML>\n\
<HEAD>\n\
<TITLE>" title  " - " img2 "</TITLE>\n\
</HEAD>\n\
<BODY BGCOLOR=\"#c0c0c0\">\n\
<CENTER>" > img2html;
  printf "<P><TT>" > img2html;
  if (img1 == "") {
    printf "[prev] " > img2html;
  }
  else {
    printf "[<A HREF=\"" img1 key "_.html\">prev</A>] " > img2html;
  }
  printf "[<A HREF=\"index" key ".html\">index</A>] " > img2html;
  if (img3 == "") {
    printf "[next]" > img2html;
  }
  else {
    printf "[<A HREF=\"" img3 key "_.html\">next</A>]" > img2html;
  }
  printf "</TT></P>\n" > img2html;
  if (img2caption != "") {
    tmpcaption = img2caption;
  }
  else if  (defcap != "") {
    tmpcaption = defcap;
  }
  else {
    tmpcaption = img2;
  }
  print "<P><B>" tmpcaption "</B></P>" > img2html;
  printf "<IMG " > img2html;
  close(img2html);
# if you have ImageMagick 5.2 or newer, use
  system ("identify -format \"WIDTH=%w HEIGHT=%h\" " destdir "/" img2 " >> " img2html);
# otherwise, stay with
#  system ("identify " destdir "/" img2 " | awk '{split($2,y,\"[^0-9]\") ; printf \"WIDTH=%d HEIGHT=%d\",y[1],y[2]}' >> " img2html);
  printf " SRC=\"%s\" ALT=\"%s\" BORDER=0></A>\n",img2,img2 >> img2html;
  if (footer!="") {
    print "<P><I>" footer "</I></P>" >> img2html;
  }
  print "</CENTER>\n\
</BODY>\n\
</HTML>" >> img2html;
}

BEGIN {
  print "usage: \\ls *.jpg | makehtmlgallery4 >! index.html" > "/dev/stderr";
  print "or:    makehtmlgallery4 title=\"TITLE\" < imglist >! index.html" > "/dev/stderr";
  print "or:    makehtmlgallery4 key=_xxx title=\"TITLE\" < imglist >! index_xxx.html" > "/dev/stderr";
  print "or:    makehtmlgallery4 key=_xxx srcdir=\"???canon\" destdir=size-800 \\" > "/dev/stderr";
  print "         imgsize=800x800 highqual=1 \\" > "/dev/stderr";
  print "         thumbcols=6 thumbsize=100x100 idxname=1 \\" > "/dev/stderr";
  print "         title=\"All My Images\" idxcap=\"<a href=\"../\">[back]</a>\" \\" > "/dev/stderr";
  print "         defcap=\"My Image\" footer=\"Photo: A. Nonymus 2004\" \\" > "/dev/stderr";
  print "         < imglist >! size-800/index_xxx.html" > "/dev/stderr";
  print "  imglist is a text file with a single image file name per line," > "/dev/stderr";
  print "  optionally followed by a rotation angle (deg cw) and/or comment/caption." > "/dev/stderr";
  print "  If imgsize is specified, images in srcdir are scaled & rotated to destdir." > "/dev/stderr";
  print "  If highqual=1 then 85% 4:4:4 is used instead of 75% 4:2:0 for scaled JPGs." > "/dev/stderr";
  print "  title specifies the title of the index HTML page (default \"Image Galery\")." > "/dev/stderr";
  print "  idxname=1 adds filenames to thumbnails on index page." > "/dev/stderr";
  print "  defcap is default (instead of filename) if no caption in imglist." > "/dev/stderr";
  print "  footer is appened below all HTML pages." > "/dev/stderr";
  print "" > "/dev/stderr";
  thumbcols = 6;
  thumbsize = "100x100";
  srcdir = ".";
  destdir = ".";
  imgsize = "";
  title = "Image Gallery";
#  srcdir = "???canon";
#  destdir = "size-800";
#  imgsize = "800x800";
  idxcap = "";
  highqual = 0;
  idxname = 0;
  defcap = "";
  footer = "";
  img1 = "";
  img2 = "";
  img3 = "";
  img2caption = "";
  img3caption = "";
  curpicts = 0;
  curcols = 0;
}

{
  if (NR==1) {
    split(thumbsize,twh,"x");
    if (idxname) {
      tdstring = "WIDTH=" twh[1]+20 " HEIGHT=" twh[2]+35;
    }
    else {
      tdstring = "WIDTH=" twh[1]+20 " HEIGHT=" twh[2]+20;
    }
    if (system("test -e " destdir "/thumbs")) {
      print "making directory thumbs" > "/dev/stderr";
      system ("mkdir -p " destdir "/thumbs");
    }
    print "<HTML>\n\
<HEAD>\n\
<TITLE>" title "</TITLE>\n\
</HEAD>\n\
<BODY BGCOLOR=\"#c0c0c0\">\n\
<CENTER>\n\
<H1>" title "</H1>";
      if (idxcap!="") {
 print "<P><B>" idxcap "</B></P>";
}
  print "<TABLE BORDER=1>";
  }

  if (imgsize != "") {
    rot = 0+$2;
    if (!system("test " srcdir "/" $1 " -nt " destdir "/" $1)) {
      print "generating " destdir "/" $1 > "/dev/stderr";
      if (highqual) {
	system ("convert -size " imgsize " " srcdir "/" $1 " -resize " imgsize " -rotate " rot " +profile \"*\" -quality 85 -sampling_factor 1x1 " destdir "/" $1);
      }
      else {
	system ("convert -size " imgsize " " srcdir "/" $1 " -resize " imgsize " -rotate " rot " +profile \"*\" " destdir "/" $1);
      }
    }
  }

  if (system("test -e " destdir "/thumbs/" $1 "_thumb.jpg") ||
      !system("test " destdir "/" $1 " -nt " destdir "/thumbs/" $1 "_thumb.jpg")) {
    print "converting " destdir "/" $1 " to " destdir "/thumbs/" $1 "_thumb.jpg" > "/dev/stderr";
    
# make JPEG thumbnail with max dimension 100 pixel, remove profile
#	system ("convert -geometry " thumbsize " +profile \"*\" " destdir "/" $1 " " destdir "/thumbs/" $1 "_thumb.jpg");
    system ("convert -size " thumbsize " " destdir "/" $1 " -resize " thumbsize " +profile \"*\" " destdir "/thumbs/" $1 "_thumb.jpg");

  }
  if (!system("test -e " destdir "/thumbs/" $1 "_thumb.jpg")) {
    print "adding " $1 " (thumbs/" $1 "_thumb.jpg) to gallery" > "/dev/stderr";
    img1 = img2;
    img2 = img3;
    img2caption = img3caption;
    img3 = $1;
    captionstart = length($1)+2;
    if (match($2,"[0-9+\\-]")) {
      captionstart += length($2)+1;
    }
    img3caption = substr($0,captionstart);
    if (img2 != "") {
      makepict(img1, img2, img3, img2caption, title);
    }
  }
}

END {
  if (img3 != "") {
    makepict(img2, img3, "", img3caption, title);
    for (;curpicts<thumbcols; curpicts++)
      print "<TD " tdstring "> &nbsp; </TD>";
    print "</TR>"
  }
  print "</TABLE>";
#  if (footer!="") {
#    print "<P><I>" footer "</I></P>\n"
#  }
  print "</CENTER>\n\
<HR>\n\
<ADDRESS>\n\
generated by \
<A HREF=\"http://www.tnt.uni-hannover.de/~purnhage/software/makehtmlgallery4\">\
<TT>makehtmlgallery4</TT></A> (v20040926) on";
  system ("date -R");
  print "</ADDRESS>\n\
</BODY>\n\
</HTML>";
}