#! /bin/bash
# Last edited on 2010-08-09 21:20:58 by stolfilocal

# Creates a preliminary mask "rmsk/${NNN}-{RRR}.png" for each
# homogeneous text region (same font style and size,
# same foreground/background colors, same projective transform).

function extr() {

  inum="$1"; shift;   # Image id-number
  tnum="$1"; shift;   # Id-number of homogeneous text region in image
  
  # Parameters of main (positive) rectangle:
  hmin="$1"; shift;   # Region first column
  vmin="$1"; shift;   # Region first row
  hmax="$1"; shift;   # Region last column
  vmax="$1"; shift;   # Region last row
  maindraw=( -draw "rectangle ${hmin},${vmin} ${hmax},${vmax}" )
  
  # Parameters of excluded (negative) rectangles:
  holedraw=(  )
  while [[ $# -gt 0 ]]; do
    hmin2="$1"; shift;   # Hole first column
    vmin2="$1"; shift;   # Hole first row
    hmax2="$1"; shift;   # Hole last column
    vmax2="$1"; shift;   # Hole last row
    holedraw=( "${holedraw[@]}" -draw "rectangle ${hmin2},${vmin2} ${hmax2},${vmax2}" )
  done
  
  # Make sure that the output directories exist:
  mkdir -p rmsk
  
  # Compute the crop image dimensions:
  hsize=$(( ${hmax} + 1 - ${hmin} ))
  vsize=$(( ${vmax} + 1 - ${vmin} ))
  
  # Input and output files:
  origimg="orig/${inum}.png"
  trueimg="true/${inum}.png"
  rmskimg="rmsk/${inum}-${tnum}.png"
   
  # Work files:
  temp="/tmp/$$"
  
  convert \
    ${origimg} \
    -alpha Off \
    -fill Black -colorize 100% \
    -fill White -stroke None "${maindraw[@]}" \
    -fill Black -stroke None "${holedraw[@]}" \
    ${rmskimg}
  
  # Extract the text region from the "orig" and "true" images for checking:
  for kind in orig true ; do
    fullimg="${kind}/${inum}.png"
    tempimg="${temp}-${kind}.png"
    
    convert \
      -compose Multiply \
      ${rmskimg} \
      -alpha Off \
      -level '-50%,100%' \
      ${fullimg} \
      -alpha Off \
      -composite ${tempimg}
  done
  display -title "${inum}:%f" -filter Box -resize '200%' ${temp}-*.png ${rmskimg} ${origimg} ${trueimg}
  
  rm -f ${temp}-*.png
}

extr 001 000  236   11  1236  250 # "maedchen ... Diener zu be="
                                
extr 002 000    0   44  1148  166 # D secundu~ dubium An"
extr 002 001    0  166  1148  309 # liceat ... co~mu="
                                 
extr 003 000  146    0   373  279 # "S" (?)
extr 003 001  373    0   861  264 # "alau"
extr 003 002  482  260   540  307 # "im"
extr 003 003  118  308   907  389  455  383   564  420 # "Marggafthum Rieder=Lausik,"
extr 003 004  459  383   564  418 # "benebst"
extr 003 005  220  419   307  457 # "deren"
extr 003 006  305  419   802  460  331  458   692  489 # "Statuten, Recessen, Privilegien,"
extr 003 007  331  458   692  489 # "un andern alten Urtunden."

extr 004 000  801   85  1005  177  869   75   938  102 # "[ 2 ]"
extr 004 001   75  165  1795  356  801   85   881  178  965   85  1005  178 # "they are limited ... appointed by the"

extr 005 000  187    0  1185  246 # "der Natur, ... der Koerperwelt"
 
