@ECHO OFF

SET relpath=%~dp1
SET inputFile=%~f2
SET page=%3
SET autocrop=%4
SET orientation=%5
SET processing=%6
SET resMultiplier=%7

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



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



ECHO ...Rastering PNG file from EPS file (this step may take several minutes^) & TITLE %page%.. D
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"
SET gs_gridFit=-dGridFitTT=3

"%relpath%\software\gs\gs8.54\bin\gswin32c.exe" %gs_includes% %gs_params% -sDEVICE=pnggray %resolution% %gs_alpha% %gs_align% %gs_gridFit% -dEPSFitPage -sOutputFile="%relpath%\temp\stage5\%page%" -f"%relpath%\temp\stage4\%page%"


IF EXIST "%relpath%\temp\stage5\%page%" (
	IF "%processing%"=="1" (
		ECHO ...Running PNG file through Dilation and Shrink filters & TITLE %page%.. C
		"%relpath%\modules\Dilate.exe" "%relpath%\temp\stage5\%page%" "%relpath%\temp\stage6\%page%" %hRes% %vRes%
		echo ...Running PNG file through Sharpening filter & TITLE %page%.. B
		"%relpath%\software\ImageMagick-6.3.1-Q8\convert.exe" "%relpath%\temp\stage6\%page%" -unsharp 2x0.8+2.0 "%relpath%\temp\stage7\%page%"
	) ELSE IF "%processing%"=="2" (
		echo ...Running PNG file through Sharpening filter & TITLE %page%.. B
		"%relpath%\software\ImageMagick-6.3.1-Q8\convert.exe" "%relpath%\temp\stage5\%page%" -unsharp 2x0.8+2.0 "%relpath%\temp\stage7\%page%"
	) ELSE IF "%processing%"=="3" (
		COPY /Y "%relpath%\temp\stage5\%page%" "%relpath%\temp\stage7\%page%"
	)
	IF EXIST "%relpath%\temp\stage7\%page%" (
		ECHO ...Reducing the colors of the PNG file & TITLE %page%.. A
		"%relpath%\software\ImageMagick-6.3.1-Q8\convert.exe" "%relpath%\temp\stage7\%page%" -colorspace GRAY -colors 4 "%relpath%\temp\stage9\%page%"
	) ELSE (
		ECHO.
		ECHO ERROR!!! Post-Processing failed!
		ECHO.
		ECHO Please make sure you have .NET Framework 2.0 installed.
		ECHO.
		ECHO If the problem persists, please report it at the mobileread forums.
		ECHO If you don't know how to report it, 
		ECHO then be reassured that the problem is probably just with your pos computer, anyway.
		pause
	)
) ELSE (
	"%relpath%\software\ImageMagick-6.3.1-Q8\convert.exe" "%relpath%\modules\blank.png" -colorspace GRAY -colors 4 "%relpath%\temp\stage9\%page%"
)


del /Q "%relpath%\temp\stage1\%page%" & del /Q "%relpath%\temp\stage2\%page%" & del /Q "%relpath%\temp\stage3\%page%" & del /Q "%relpath%\temp\stage4\%page%" & del /Q "%relpath%\temp\stage5\%page%" & del /Q "%relpath%\temp\stage6\%page%" & del /Q "%relpath%\temp\stage7\%page%" & del /Q "%relpath%\temp\stage8\%page%"
exit


	
:setBBox
	SET /P bbox=<"%relpath%\temp\stage3\%page%"
	"%relpath%\software\gawk\gawk.exe" -v bbox="%bbox%" -f "%relpath%\modules\crop_ps.awk" "%relpath%\temp\stage1\%page%" > "%relpath%\temp\stage4\%page%"
GOTO :eof