#!/bin/bash

# DEFRES is default resolution, when input does not have its own.
# If input is PDF, then DEFRES is always applied.
# MINRES and MAXRES set warning messages for images under/over usual limits.
# If you did not export values for these, the following are used:
if [ "$DEFRES" == "" ]; then DEFRES="300"; fi
if [ "$MINRES" == "" ]; then MINRES="150"; fi
if [ "$MAXRES" == "" ]; then MAXRES="600"; fi
# The default image output is png format. Best to leave it that way.
# However, if you have a lot of images, and the resulting pdf is too large,
#   you can use jpg instead. This saves bytes without much loss (95% quality).
# To permanently change to jpg output, change this from no to yes:
if [ "$JPG" == "" ]; then JPG="no"; fi

# makegray
# This is a Linux/MacOS BASH command script.
# Tested with BASH 3.2.57 and 4.4.12.
# FREE SOFTWARE, WITHOUT WARRANTY EXPRESS OR IMPLIED. USE AT OWN RISK.
# Copyright 2018 Robert Allgeyer.
# This file may be distributed and/or modified under the
# conditions of the LaTeX Project Public License, either version 1.3c
# of this license or [at your option] any later version.
# The latest version of this license is in
#    http://www.latex-project.org/lppl.txt
# and version 1.3c or later is part of all distributions of LaTeX
# version 2005/12/01 or later.
#
# This file is distributed with the "novel" LuaLaTeX document class.
# https://ctan.org/pkg/novel  [get the one zip archive]
# But you do not need a TeX installation to use this script.

THISVER="1.4"
VERMSG="makegray version $THISVER."
USAGEMSG="Usage: ./makegray [-digit] filename.ext"
HELPMSG="Help:  ./makegray -h"
DEMOMSG="Demo:  ./makegray [-digit] demo"

# Provides version, using -v or --v or equivalent:
if [ "$1" == "" ] || [[ "$1" =~ ^-v.* ]] || [[ "$1" =~ ^--v.* ]] || [[ "$1" =~ ^-V.* ]] || [[ "$1" =~ ^--V.* ]]
then
  echo "$VERMSG"
  echo 
  exit 0
fi

# Provides help, using -h or --h or equivalent:
if [[ "$1" =~ ^-h.* ]] || [[ "$1" =~ ^--h.* ]] || [[ "$1" =~ ^-H.* ]] || [[ "$1" =~ ^--H.* ]]
then
  echo 
  echo "$VERMSG"
  echo "WITHOUT WARRANTY EXPRESS OR IMPLIED. USE AT OWN RISK."
  echo "Converts Color or Grayscale to single-channel Grayscale png or jpg."
  echo 
  echo "$USAGEMSG"
  echo "  Where .ext may be .png, .jpg, .jpeg, .tif, .tiff, .pdf [also capitalized]."
  echo "$DEMOMSG"
  echo 
  echo "Option is an integer 1 to 9, preceded by hyphen."
  echo "  Performs sigmoidal contrast adjustment. No adjustment if no option."
  echo "  Low number increases contrast in darker areas, at expense of lighter."
  echo "  High number increases contrast in lighter areas, at expense of darker."
  echo "  Middle number increases midrange contrast, at expense of both extremes."
  echo 
  echo "Requires ImageMagick. Also Ghostscript, if pdf input."
  echo 
  echo "Place input image in input folder."
  echo "  Input image may be Color or Grayscale. No spaces in filename."
  echo "  Must be exact size and resolution [typ 300 pixels/inch]."
  echo "  If input is PDF it will be forced to 300 pixels/inch,"
  echo "    or your alternative default resolution. See the README file."
  echo "  If input is PDF, only its first page will be processed."
  echo 
  echo "Output will appear in output folder, named filename-P-GRAY.png [or jpg]."
  echo "  P=0: contrast unchanged."
  echo "  P=1 through 9: number provided as optional argument."
  echo "  It will be single-channel 8-bit Grayscale."
  echo "  Color inputs are weighted, typical of photos."
  echo "  Resolution will be reported, but not resized, resampled, or cropped."
  echo 
  echo "See novel-scripts-README.html for more information."
  echo 
  exit 0
fi

# Welcome message:
echo "Converts color or grayscale image to single-channel grayscale png or jpg."
echo "WITHOUT WARRANTY EXPRESS OR IMPLIED. USE AT OWN RISK."

if [ ! -d "temp" ]; then mkdir temp; fi
if [ ! -d "output" ]; then mkdir output; fi

# Checks for input arguments:
printf "Parsing arguments... "
IL="0"
if [ ! "$2" == "" ]
then
  if [[ $1 =~ ^-[1-9]$ ]]
  then
    IL=$((0 - $1))
  else
    echo "ERROR."
    echo "Bad optional argument. May use -1 to -9. Do not omit hyphen."
    echo "$USAGEMSG"
    echo "$HELPMSG"
    echo 
    exit 1
  fi
fi
echo "OK"
IL="$((10 * $IL))"
NEEDSGS=""
DEMOMODE=""
FN=""
if [ -f "resource/internal/commonscript" ]
then
  source resource/internal/commonscript
else
  echo "ERROR."
  echo "File resource/internal/commonscript is missing. Needed to proceed."
  echo 
  exit 1
fi
IE="png"
if [ "$JPG" == "yes" ] || [ "$JPG" == "YES" ]
then
  IE="jpg"
fi


# Check input colorspace:
printf "Inspecting the input image... "
ISCMYK=""
identify -verbose input/$FN$PDFZ >temp/temp-identify.txt
echo  >>temp/temp-identify.txt
grep "Colorspace: CMYK" temp/temp-identify.txt 1>/dev/null 2>/dev/null
if [ $? -eq 0 ]
then
  ISCMYK="yes"
  echo "CMYK."
else
  echo "RGB."
fi


# Now do conversion:
echo 
echo "Converting..."
DR="-density $IR -units PixelsPerInch"
BK="-flatten -background White -alpha off -depth 8"
TG="-define PNG:exclude-chunk=gAMA,bKGD,zTXt,iTXt"
HG="-fx ""luminance"" -colorspace Gray"
CO="-set comment ""novelscripts-makegray-$THISVER-l"" "
if [ ! "$IL" == "0" ]; then SC="-sigmoidal-contrast 4,$IL%"; fi

# Requires more than one step. Do not condense to fewer steps.
# Reason: Syntax differences in IM versions and platforms.
if [ "$ISCMYK" == "yes" ]
then
  convert $DR -strip input/$FN$PDFZ $BK $TI temp/temp-$CN-rgb.png
  echo  >>temp/temp-identify.txt
  mogrify -strip temp/temp-$CN-rgb.png
  convert $DR temp/temp-$CN-rgb.png $TG $CO $BK $HG $SC temp/$CN-$IL-GRAY.png
  if [ -f "temp/temp-$CN-rgb.png" ]; then rm "temp/temp-$CN-rgb.png"; fi
else
  convert $DR -strip input/$FN$PDFZ temp/temp-$CN-$IL-GRAY.png
  convert $DR temp/temp-$CN-$IL-GRAY.png $TG $CO $BK $HG $SC temp/$CN-$IL-GRAY.png
  echo  >>temp/temp-identify.txt
  if [ -f "temp/temp-$CN-$IL-GRAY.png" ]; then rm "temp/temp-$CN-$IL-GRAY.png"; fi
fi

grep "geometry does not contain image" temp/temp-identify.txt 1>/dev/null 2>/dev/null
if [ $? -eq 0 ]
then
  echo 
  echo "The page your requested is a blank page. No output produced."
  echo "Try again with a different page number."
  if [ -f "temp/$CN-$IL-GRAY.png" ]; then rm temp/$CN-$IL-GRAY.png; fi
  echo 
  if [ -f "temp/temp-identify.txt" ]; then rm temp/temp-identify.txt; fi
  exit 1
fi
if [ "$IE" == "jpg" ]
then
  convert -quality 95 temp/$CN-$IL-GRAY.png output/$CN-$IL-GRAY.jpg
  if [ -f "temp/$CN-$IL-GRAY.png" ]; then rm temp/$CN-$IL-GRAY.png; fi
else
  mv "temp/$CN-$IL-GRAY.png" "output/$CN-$IL-GRAY.png" 1>/dev/null
fi

# Done:
echo "Verifying..."
echo
echo "The Grayscale image is output/$CN-$IL-GRAY.$IE."
if [ "$IL" == "0" ]
then
  echo "No contrast adjustment applied."
else
  echo "Contrast adjustment value $IL applied."
fi
identify -verbose output/$CN-$IL-GRAY.$IE >temp/temp-identify.txt
echo  >>temp/temp-identify.txt
grep "Colorspace" temp/temp-identify.txt 2>/dev/null
grep "Print size" temp/temp-identify.txt 2>/dev/null
grep "PixelsPerInch" temp/temp-identify.txt 1>/dev/null 2>/dev/null
if [ $? -eq 0 ]
then
  echo "    - Measured in Inches."
else
  echo "    - Measured in Centimeters."
  echo "    - Divide by 2.54 to get Inches."
fi
grep "Resolution" temp/temp-identify.txt 2>/dev/null
grep "PixelsPerInch" temp/temp-identify.txt 1>/dev/null 2>/dev/null
if [ $? -eq 0 ]
then
  echo "    - Measured in Pixels Per Inch."
else
  echo "    - Measured in Pixels Per Centimeter."
  echo "    - Multiply by 2.54 to get Pixels Per Inch."
fi
if [ ! "$WM1" == "" ]; then echo "$WM1"; fi
if [ ! "$WM2" == "" ]; then echo "$WM2"; fi
if [ ! "$WM3" == "" ]; then echo "$WM3"; fi
if [ -f "temp/temp-identify.txt" ]; then rm temp/temp-identify.txt; fi
echo 
echo "Done."
echo "You may now close this window."
echo
exit 0 
# end of file


