#!/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="800"; fi
if [ "$MINRES" == "" ]; then MINRES="300"; fi
if [ "$MAXRES" == "" ]; then MAXRES="1200"; fi


# makebw
# 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="makebw version $THISVER."
USAGEMSG="Usage: ./makebw [-threshold] filename.ext"
HELPMSG="Help:  ./makebw -h"
DEMOMSG="Demo:  ./makebw [-threshold] 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 1-bit black/white png."
  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 99, preceded by hyphen."
  echo "  Set threshold. Default 50 sets threshold at mid-gray."
  echo "  Low number makes more white. High number makes more black."
  echo "  Useful for tweaking widths of lines in line art."
  echo 
  echo "This script 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 800 pixels/inch for line art]."
  echo "  If input is PDF it will be forced to 800 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-t-BW.png."
  echo "  t=threshold. If option not used, =50."
  echo "  It will be single-channel 1-bit black/white."
  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 "This script converts color or grayscale image to 1-bit monochrome black/white."
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... "
THRESH="50"
if [ ! "$2" == "" ]
then
  if [[ $1 =~ ^-[1-9][0-9]?$ ]]
  then
    THRESH=$((0 - $1))
  else
    echo "ERROR."
    echo "Bad optional argument. May use -1 to -99. Do not omit hyphen."
    echo "$USAGEMSG"
    echo "$HELPMSG"
    echo 
    exit 1
  fi
fi
echo "OK"
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

# Now do conversion:
# Requires more than one step. Do not condense to fewer steps.
# Reason: Syntax differences in IM versions and platforms.
echo 
echo "Converting, this takes awhile..."
DR="-density $IR -units PixelsPerInch"
BK="-background White -flatten -alpha off $TI"
TG="-define PNG:exclude-chunk=gAMA,bKGD,zTXt,iTXt"
TH="-threshold $THRESH% -colorspace Gray -type Bilevel"
CO="-set comment ""novelscripts-makebw-$THISVER-l"" "
convert $DR -strip input/$FN$PDFZ temp/temp-$CN-$THRESH-BW.png
convert $DR temp/temp-$CN-$THRESH-BW.png $TG $CO $BK $TH output/$CN-$THRESH-BW.png
if [ -f "temp/temp-$CN-$THRESH-BW.png" ]; then rm temp/temp-$CN-$THRESH-BW.png; 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 "output/$CN-$THRESH-BW.png" ]; then rm output/$CN-$THRESH-BW.png; fi
  echo 
  if [ -f "temp/temp-identify.txt" ]; then rm temp/temp-identify.txt; fi
  exit 1
fi
# end conversion routine

# Done:
echo "Verifying, this takes awhile..."
echo 
identify -verbose output/$CN-$THRESH-BW.png >temp/temp-identify.txt
echo  >>temp/temp-identify.txt
echo "The Monochrome black-white image is output/$CN-$THRESH-BW.png."
echo "Used threshold $THRESH percent."
grep "Colorspace" temp/temp-identify.txt 2>/dev/null
grep "Channel depth" temp/temp-identify.txt 2>/dev/null
grep "Gray: 1-bit" temp/temp-identify.txt 2>/dev/null
grep "gray: 1-bit" 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. [png format reports metric]"
  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. [png format reports metric]"
  echo "    - Multiply by 2.54 to get Pixels Per Inch."
fi
echo "$WM1"
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


