
REM 2 rems

REM GhostScript options
SET gs_includes=-I"%relpath%\software\gs\gs8.54\lib" -I"%relpath%\software\gs\gs8.54\Resource" -I"%relpath%\software\gs\fonts"
SET gs_params=-q -dBATCH -dSAFER -dNOPAUSE -dMaxBitmap=200000000

IF EXIST "%relpath%\temp\pr*" del /Q "%relpath%\temp\pr*"




ECHO ...Creating EPS file from PDF page
IF "%autocrop%"=="y" (
	"%relpath%\software\pdftops.exe" -f %page% -l %page% -eps -pagecrop "%inputFile%" "%relpath%\temp\prv_1.eps"
	ECHO ...Performing autocropping (this step may take several minutes^)
	"%relpath%\software\gs\gs8.54\bin\gswin32c.exe" %gs_includes% %gs_params% -r100 -sDEVICE=bbox "%relpath%\temp\prv_1.eps" 2> "%relpath%\temp\prv_2.txt"
	"%relpath%\software\gawk\gawk.exe" "/%%HiResBoundingBox:/ {print  $2,$3,$4,$5}" "%relpath%\temp\prv_2.txt" > "%relpath%\temp\prv_3.txt"
	call :setBBox
) ELSE (
	"%relpath%\software\pdftops.exe" -f %page% -l %page% -eps -pagecrop "%inputFile%" "%relpath%\temp\prv_4.eps"
)




ECHO ...Rastering PNG file from EPS file (this step may take several minutes^)

IF "%orientation%"=="1" (
	SET hRes=565
	SET vRes=754
) ELSE IF "%orientation%"=="2" (
	SET hRes=759
	SET vRes=1012
) ELSE IF "%orientation%"=="3" (
	SET /A hRes=^(565*200^)/^(100+hOverlap^)
	SET /A vRes=^(754*200^)/^(100+vOverlap^)
)

IF "%processing%"=="1" (
	SET /A temp=resMultiplier*565
	SET /A temp=temp/hRes
	SET /A hRasRes=hRes*temp
	SET /A vRasRes=vRes*temp
) ELSE IF "%processing%"=="2" (
	SET /A hRasRes=hRes
	SET /A vRasRes=vRes
) ELSE IF "%processing%"=="3" (
	SET /A hRasRes=hRes
	SET /A vRasRes=vRes
)

SET resolution=-g%hRasRes%x%vRasRes%

IF "%processing%"=="1" SET gs_alpha=-dTextAlphaBits=4 -dGraphicsAlphaBits=4
IF "%processing%"=="2" SET gs_alpha=-dTextAlphaBits=2 -dGraphicsAlphaBits=4
IF "%processing%"=="3" SET gs_alpha=-dTextAlphaBits=2 -dGraphicsAlphaBits=4

IF "%processing%"=="1" SET gs_align=-dAlignToPixels=0
IF "%processing%"=="2" (
	IF "%orientation%"=="1" (
		SET gs_align=-dAlignToPixels=0
	) ELSE (
		SET gs_align=-dAlignToPixels=0
	)
)
IF "%processing%"=="3" SET gs_align=-dAlignToPixels=0

SET TMPDIR="%relpath%\temp"
"%relpath%\software\gs\gs8.54\bin\gswin32c.exe" %gs_includes% %gs_params% -sDEVICE=pnggray %resolution% %gs_alpha% %gs_align% -dEPSFitPage -sOutputFile="%relpath%\temp\prv_5.png" -f"%relpath%\temp\prv_4.eps"





IF "%processing%"=="1" (
	ECHO ...Running PNG file through Dilation filter
	"%relpath%\software\AutoImager\AutoImager.exe" /f %relpath%\temp\prv_5.png /p Dilate /d /os
	echo ...Downsampling PNG file
	"%relpath%\software\ImageMagick-6.3.1-Q8\convert.exe" "%relpath%\temp\prv_5NEW.png" -resize %hRes%x%vRes% "%relpath%\temp\prv_6.png"
	echo ...Running PNG file through Sharpening filter
	"%relpath%\software\ImageMagick-6.3.1-Q8\convert.exe" "%relpath%\temp\prv_6.png" -unsharp 2 "%relpath%\temp\prv_7.png"
) ELSE IF "%processing%"=="2" (
	echo ...Running PNG file through Sharpening filter
	"%relpath%\software\ImageMagick-6.3.1-Q8\convert.exe" "%relpath%\temp\prv_5.png" -unsharp 2 "%relpath%\temp\prv_7.png"
) ELSE IF "%processing%"=="3" (
	COPY /Y "%relpath%\temp\prv_5.png" "%relpath%\temp\prv_7.png"
)



ECHO ...Reducing the colors of the PNG file
"%relpath%\software\ImageMagick-6.3.1-Q8\convert.exe" "%relpath%\temp\prv_7.png" -colorspace GRAY -colors 4 "%relpath%\temp\%previewFilename%"



IF "%orientation%"=="3" (
	ECHO ...Drawing overlap indicator lines
	call :drawQuarterLines
)


GOTO :eof


	


REM the following functions exist because of the dumb behavior of CMD scripts. Variables are not updated within () and then used. A call must be made out of the parantheses.
:drawQuarterLines
	SET /A temp1=hRes-565
	SET /A temp2=vRes-754
	"%relpath%\software\ImageMagick-6.3.1-Q8\convert.exe" "%relpath%\temp\%previewFilename%" -draw "line 565,0 565,754" -draw "line 0,754 565,754" "%relpath%\temp\%previewFilename%"
	"%relpath%\software\ImageMagick-6.3.1-Q8\convert.exe" "%relpath%\temp\%previewFilename%" -draw "line %temp1%,0 %temp1%,754" -draw "line %temp1%,754 %hRes%,754" "%relpath%\temp\%previewFilename%"
	"%relpath%\software\ImageMagick-6.3.1-Q8\convert.exe" "%relpath%\temp\%previewFilename%" -draw "line 565,%temp2% 565,%vRes%" -draw "line 0,%temp2% 565,%temp2%" "%relpath%\temp\%previewFilename%"
	"%relpath%\software\ImageMagick-6.3.1-Q8\convert.exe" "%relpath%\temp\%previewFilename%" -draw "line %temp1%,%temp2% %temp1%,%vRes%" -draw "line %temp1%,%temp2% %hRes%,%temp2%" "%relpath%\temp\%previewFilename%"
GOTO :eof

:setBBox
	SET /P bbox=<"%relpath%\temp\prv_3.txt"
	"%relpath%\software\gawk\gawk.exe" -v bbox="%bbox%" -f "%relpath%\modules\crop_ps.awk" "%relpath%\temp\prv_1.eps" > "%relpath%\temp\prv_4.eps"
GOTO :eof