Consistently use VersionCheck instead of CheckNewer
[TortoiseGit.git] / doc / images / adjustimages.bat
blob40199da8de012d119961bbbf365eeb7105b456df
1 @echo off & setlocal enableextensions enabledelayedexpansion\r
2 :: ============================================================ \r
3 :: $Id: adjustimages.bat 22184 2011-10-25 15:02:18Z luebbe.tortoisesvn $\r
4 :: ============================================================ \r
5 :: This script verifies all .png images to see if they fit\r
6 :: in the PDF version of the documentation. The FOP version we\r
7 :: use doesn't scale images automatically when they are too\r
8 :: wide or high.\r
9 :: We want images to be at most 5.75 inches wide and 9 inches\r
10 :: high.\r
11 :: The script retrieves the DPI setting in the image and\r
12 :: and calculates the actual size of the image. If it exceeds\r
13 :: the limits, the DPI setting of the image is increased to\r
14 :: fit the limits.\r
15 ::\r
16 :: By default all images in all languages will be checked.\r
17 :: You can specify a group of files to check using the first\r
18 :: parameter, e.g.\r
19 ::     adjustimages en\Rev*.png\r
20 ::\r
21 :: The image manipulation is done using NConvert.exe from\r
22 :: http://www.xnview.com\r
23 :: ============================================================ \r
24 :: Set image size limits in 1000ths of an inch\r
25 set /a w_limit = 5750\r
26 set /a h_limit = 9000\r
27 ::\r
28 ::\r
29 if '%1' == '' goto :DoDefault\r
30 for %%? in (%1) do Call :ProcAdjustFile %%?\r
31 endlocal & goto:EOF\r
32 ::\r
33 :DoDefault\r
34 for %%? in (en\*.png) do Call :ProcAdjustFile %%?\r
35 :: Cleanup\r
36 for %%? in (f_info.txt) do if exist %%? del %%?\r
37 endlocal & goto :EOF\r
38 ::===============================================================\r
39 :ProcAdjustFile FileName\r
40 :: Extract resolution info from file\r
41 setlocal enableextensions enabledelayedexpansion\r
42 nconvert.exe -info %1>f_info.txt\r
43 ::\r
44 :: Extract image width, height and dpi\r
45 :: Width   = 3rd word on 10th line\r
46 :: Height  = 3rd word on 11th line\r
47 :: Channel = 5th word on 12th line\r
48 :: XDPI    = 3rd word on 19th line\r
49 :: YDPI    = 3rd word on 20th line\r
50 :: Do the test. Get the third word of the tenth line.\r
51 call :ProcGetLine f_info.txt 10 getLine\r
52 for /f "tokens=3" %%? in ("%getLine%") do set /a w_image = %%?\r
53 call :ProcGetLine f_info.txt 11 getLine\r
54 for /f "tokens=3" %%? in ("%getLine%") do set /a h_image = %%?\r
55 call :ProcGetLine f_info.txt 12 getLine\r
56 for /f "tokens=5" %%? in ("%getLine%") do set /a channels = %%?\r
57 call :ProcGetLine f_info.txt 19 getLine\r
58 for /f "tokens=3" %%? in ("%getLine%") do set /a xdpi = %%?\r
59 call :ProcGetLine f_info.txt 20 getLine\r
60 for /f "tokens=3" %%? in ("%getLine%") do set /a ydpi = %%?\r
61 :: Set default dpi if no dpi was found\r
62 set /a must_convert = 0\r
63 set /a must_channel = 0\r
64 if %xdpi% equ 0 (\r
65    set /a xdpi = 96\r
66    set /a must_convert = 1\r
67 )\r
68 if %ydpi% equ 0 (\r
69    set /a ydpi = 96\r
70    set /a must_convert = 1\r
71 )\r
72 if %channels% geq 4 (\r
73     echo %1: alpha channel detected\r
74     set /a must_channel = 1\r
75     set /a must_convert = 1\r
76 )\r
77 ::\r
78 :: Calculate image width and height (factor 1000 is used because\r
79 :: of the integer math.\r
80 set /a w_image_inch = w_image * 1000 / xdpi\r
81 set /a h_image_inch = h_image * 1000 / ydpi\r
82 rem echo Image (w - h): %w_image% - %h_image%\r
83 rem echo DPI (x - y)  : %xdpi% - %ydpi%\r
84 rem echo Size (w - h) : %w_image_inch% - %h_image_inch%\r
85 set /a w_delta = w_limit - w_image_inch\r
86 set /a h_delta = h_limit - h_image_inch\r
87 rem echo Delta (w - h): %w_delta% - %h_delta%\r
88 :: If height and width are within limits, we're done\r
89 :: If not, determine direction with largest overdraw and\r
90 ::   calculate a dpi setting that will fit the image into the\r
91 ::   available space\r
92 ::   Calculation method: new_dpi = (pixels * 1000) / limit\r
93 if %w_delta% geq 0 (\r
94    if %h_delta% geq 0 (\r
95       if %must_convert% equ 1 (\r
96          echo %1: image fits with default dpi\r
97          set /a new_dpi = 95\r
98       ) else (\r
99          echo %1: image is ok\r
100          goto :Done\r
101       )\r
102    ) else (\r
103       echo %1: image too high\r
104       set /a new_dpi = h_image * 1000 / h_limit\r
105    )\r
106 ) else (\r
107    if %h_delta% geq 0 (\r
108       echo %1: image too wide\r
109       set /a new_dpi = w_image * 1000 / w_limit\r
110    ) else (\r
111       echo %1: image too wide and too high\r
112       set /a new_xdpi = w_image * 1000 / w_limit\r
113       set /a new_ydpi = h_image * 1000 / h_limit\r
114       if !new_xdpi! leq !new_ydpi! (\r
115          set /a new_dpi = !new_ydpi!\r
116       ) else (\r
117          set /a new_dpi = !new_xdpi!\r
118       )\r
119       rem echo dpi required for width !new_xdpi!\r
120       rem echo dpi required for height !new_ydpi!\r
121    )\r
123 :: Make sure the dpi is large enough (integer arithmetic truncates)\r
124 set /a new_dpi = new_dpi + 1\r
125 echo adjust dpi to %new_dpi%\r
126 nconvert.exe -o %1 -dpi %new_dpi% %1 >nul\r
127 :Done\r
128 if %must_channel% equ 1 (\r
129     nconvert.exe -o %1 -ctype rgb %1 >nul\r
131 optipng.exe -o7 -quiet %1\r
132 endlocal & goto :EOF\r
133 ::===============================================================\r
134 :ProcGetLine FileName LineNro returnText\r
135 setlocal enableextensions\r
136 set lineNro_=%2\r
137 set /a lineNro_-=1\r
138 set return_=\r
139 for /f "tokens=* skip=%lineNro_% delims=" %%r in ('type %1') do (\r
140   if not defined return_ set return_=%%r)\r
141 endlocal & set %3=%return_% & goto :EOF\r