-- The University of New Mexico's Haskell Image Processing Library-- Copyright (C) 2013 Joseph Collard---- This program is free software: you can redistribute it and/or modify-- it under the terms of the GNU General Public License as published by-- the Free Software Foundation, either version 3 of the License, or-- (at your option) any later version.---- This program is distributed in the hope that it will be useful,-- but WITHOUT ANY WARRANTY; without even the implied warranty of-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the-- GNU General Public License for more details.---- You should have received a copy of the GNU General Public License-- along with this program. If not, see <http://www.gnu.org/licenses/>.moduleData.Image.Interactive(display,setDisplayProgram,plotHistograms)whereimportData.Image.IOimportData.Image.Binary(areas)importData.Image.Internal--base>=4importData.List(intercalate)importData.IORefimportSystem.IO.UnsafeimportSystem.IO--vector>=0.10.0.2importqualifiedData.Vector.UnboxedasV--process>=1.1.0.2importSystem.Process{-| Sets the program to use when making a call to display and specifies if
the program can accept an image via stdin. If it cannot, then a temporary
file will be created and passed as an argument instead. By default,
ImageMagick ("display") is the default program to use and it is read
using stdin.
>>>setDisplayProgram "gimp" False
>>>setDisplayProgram "xv" False
>>>setDisplayProgram "display" True
-}setDisplayProgram::String->Bool->IO()setDisplayProgramprogramstdin=writeIORefdisplayProgramprogram>>writeIORefuseStdinstdin{-| Makes a call to the current display program to be displayed. If the
program cannot read from standard in, a file named ".tmp-img" is created
and used as an argument to the program.
>>>frog <- readImage "images/frog.pgm"
>>>display frog
-}display::(DisplayFormatdf)=>df->IO(Handle,Handle,Handle,ProcessHandle)displayimg=dousestdin<-readIORefuseStdindisplay<-readIORefdisplayProgramifusestdinthenrunCommandWithStdIndisplay.format$imgelsedowriteImage".tmp-img"imgrunInteractiveCommand(display++" .tmp-img")displayProgram::IORefStringdisplayProgram=unsafePerformIO$dodp<-newIORef"display"returndpuseStdin::IORefBooluseStdin=unsafePerformIO$dousestdin<-newIORefTruereturnusestdin-- Run a command via the shell with the input given as stdinrunCommandWithStdIn::String->String->IO(Handle,Handle,Handle,ProcessHandle)runCommandWithStdIncmdstdin=doioval<-runInteractiveCommandcmdletstdInput=(\(x,_,_,_)->x)iovalhPutStrstdInputstdinhFlushstdInputhClosestdInputreturnioval{-| Takes a list, pair, or triple of images and passes them to
gnuplot to be displayed as histograms.
>>>frog <- readImage "images/frog.pgm"
>>>plotHistograms $ [frog]
<https://raw.github.com/jcollard/unm-hip/master/examples/frog.jpg>
<https://raw.github.com/jcollard/unm-hip/master/examples/frogplot.jpg>
>>>cactii <- readColorImage "images/cactii.ppm"
>>>plotHistograms . colorImageToRGB $ cactii
<https://raw.github.com/jcollard/unm-hip/master/examples/cactii.jpg>
<https://raw.github.com/jcollard/unm-hip/master/examples/cactiiplot.jpg>
-}plotHistogramsimages=runCommandWithStdIn"gnuplot -persist"$inputwhereinput=intercalate"\n"[plotCommanddatasmax,histogramList,"exit"]datas=["data"++(showx)|x<-[0..n]]n=length.toList$imageshistogramList=intercalate"\ne".maphistogramString.toList$imagesmax=floor.maximum.mapmaxIntensity.toList$images-- Creates a plot command for gnuplot containing a title for each of the titles-- and sets the width of the plot to be from 0 to maxplotCommand::[String]->Int->StringplotCommandtitlesmax="plot [0:"++(showmax)++"]"++(intercalate","$vals)wherevals=[" '-' title '"++title++"' with lines"|title<-titles]-- Takes an image and creates a gnuplot command of the points for the-- histogram.histogramStringimg=intercalate"\n"$mapshow(V.toListarr)wherearr=areasimg