diff options
author | pacien | 2020-01-23 23:16:07 +0100 |
---|---|---|
committer | Notkea | 2020-01-26 22:06:24 +0100 |
commit | cf91102432b1196b8f3c1fa388b3963948ad49a6 (patch) | |
tree | eb7bfdcaca87f6233f15887cadcf92586fdec7fc | |
parent | 987eb81cb5d98262299c7917d752c54907cbbc33 (diff) | |
download | ldgallery-cf91102432b1196b8f3c1fa388b3963948ad49a6.tar.gz |
compiler: add jpeg export quality setting
GitHub: closes #2
-rw-r--r-- | compiler/src/Compiler.hs | 16 | ||||
-rw-r--r-- | compiler/src/Config.hs | 2 | ||||
-rw-r--r-- | compiler/src/Processors.hs | 80 | ||||
-rw-r--r-- | example/gallery.yaml | 2 | ||||
-rw-r--r-- | ldgallery.1.md | 3 |
5 files changed, 59 insertions, 44 deletions
diff --git a/compiler/src/Compiler.hs b/compiler/src/Compiler.hs index 13e9232..aca96bc 100644 --- a/compiler/src/Compiler.hs +++ b/compiler/src/Compiler.hs | |||
@@ -114,8 +114,8 @@ compileGallery inputDirPath outputDirPath rebuildAll = | |||
114 | inputTree <- readInputTree sourceTree | 114 | inputTree <- readInputTree sourceTree |
115 | 115 | ||
116 | let cache = if rebuildAll then skipCached else withCached | 116 | let cache = if rebuildAll then skipCached else withCached |
117 | let itemProc = itemProcessor (pictureMaxResolution config) cache | 117 | let itemProc = itemProcessor config cache |
118 | let thumbnailProc = thumbnailProcessor (thumbnailMaxResolution config) cache | 118 | let thumbnailProc = thumbnailProcessor config cache |
119 | let galleryBuilder = buildGalleryTree itemProc thumbnailProc (tagsFromDirectories config) | 119 | let galleryBuilder = buildGalleryTree itemProc thumbnailProc (tagsFromDirectories config) |
120 | resources <- galleryBuilder (galleryName config) inputTree | 120 | resources <- galleryBuilder (galleryName config) inputTree |
121 | 121 | ||
@@ -128,7 +128,11 @@ compileGallery inputDirPath outputDirPath rebuildAll = | |||
128 | outputIndex = outputDirPath </> indexFile | 128 | outputIndex = outputDirPath </> indexFile |
129 | outputViewerConf = outputDirPath </> viewerConfFile | 129 | outputViewerConf = outputDirPath </> viewerConfFile |
130 | 130 | ||
131 | itemProcessor maxRes cache = | 131 | itemProcessor config cache = |
132 | itemFileProcessor maxRes cache inputDirPath outputDirPath itemsDir | 132 | itemFileProcessor |
133 | thumbnailProcessor thumbRes cache = | 133 | (pictureMaxResolution config) (jpegExportQuality config) cache |
134 | thumbnailFileProcessor thumbRes cache inputDirPath outputDirPath thumbnailsDir | 134 | inputDirPath outputDirPath itemsDir |
135 | thumbnailProcessor config cache = | ||
136 | thumbnailFileProcessor | ||
137 | (thumbnailMaxResolution config) (jpegExportQuality config) cache | ||
138 | inputDirPath outputDirPath thumbnailsDir | ||
diff --git a/compiler/src/Config.hs b/compiler/src/Config.hs index d670aae..4446c14 100644 --- a/compiler/src/Config.hs +++ b/compiler/src/Config.hs | |||
@@ -41,6 +41,7 @@ data CompilerConfig = CompilerConfig | |||
41 | , tagsFromDirectories :: Int | 41 | , tagsFromDirectories :: Int |
42 | , thumbnailMaxResolution :: Resolution | 42 | , thumbnailMaxResolution :: Resolution |
43 | , pictureMaxResolution :: Maybe Resolution | 43 | , pictureMaxResolution :: Maybe Resolution |
44 | , jpegExportQuality :: Int | ||
44 | } deriving (Generic, Show) | 45 | } deriving (Generic, Show) |
45 | 46 | ||
46 | instance FromJSON CompilerConfig where | 47 | instance FromJSON CompilerConfig where |
@@ -53,6 +54,7 @@ instance FromJSON CompilerConfig where | |||
53 | <*> v .:? "tagsFromDirectories" .!= 0 | 54 | <*> v .:? "tagsFromDirectories" .!= 0 |
54 | <*> v .:? "thumbnailMaxResolution" .!= (Resolution 400 400) | 55 | <*> v .:? "thumbnailMaxResolution" .!= (Resolution 400 400) |
55 | <*> v .:? "pictureMaxResolution" | 56 | <*> v .:? "pictureMaxResolution" |
57 | <*> v .:? "jpegExportQuality" .!= 80 | ||
56 | 58 | ||
57 | 59 | ||
58 | data GalleryConfig = GalleryConfig | 60 | data GalleryConfig = GalleryConfig |
diff --git a/compiler/src/Processors.hs b/compiler/src/Processors.hs index 159a425..1c4a791 100644 --- a/compiler/src/Processors.hs +++ b/compiler/src/Processors.hs | |||
@@ -80,43 +80,47 @@ copyFileProcessor inputPath outputPath = | |||
80 | (putStrLn $ "Copying:\t" ++ outputPath) | 80 | (putStrLn $ "Copying:\t" ++ outputPath) |
81 | >> ensureParentDir (flip System.Directory.copyFile) outputPath inputPath | 81 | >> ensureParentDir (flip System.Directory.copyFile) outputPath inputPath |
82 | 82 | ||
83 | resizeStaticImageUpTo :: PictureFileFormat -> Resolution -> FileProcessor | ||
84 | resizeStaticImageUpTo Bmp = resizeStaticGeneric readBitmap saveBmpImage | ||
85 | -- TODO: parameterise export quality for jpg | ||
86 | resizeStaticImageUpTo Jpg = resizeStaticGeneric readJpeg (saveJpgImage 80) | ||
87 | resizeStaticImageUpTo Png = resizeStaticGeneric readPng savePngImage | ||
88 | resizeStaticImageUpTo Tiff = resizeStaticGeneric readTiff saveTiffImage | ||
89 | resizeStaticImageUpTo Hdr = resizeStaticGeneric readHDR saveRadianceImage | ||
90 | resizeStaticImageUpTo Gif = resizeStaticGeneric readGif saveGifImage' | ||
91 | where | ||
92 | saveGifImage' :: StaticImageWriter | ||
93 | saveGifImage' outputPath image = | ||
94 | saveGifImage outputPath image | ||
95 | & either (throwIO . ProcessingException outputPath) id | ||
96 | |||
97 | 83 | ||
84 | type LossyExportQuality = Int | ||
98 | type StaticImageReader = FilePath -> IO (Either String DynamicImage) | 85 | type StaticImageReader = FilePath -> IO (Either String DynamicImage) |
99 | type StaticImageWriter = FilePath -> DynamicImage -> IO () | 86 | type StaticImageWriter = FilePath -> DynamicImage -> IO () |
100 | 87 | ||
101 | resizeStaticGeneric :: StaticImageReader -> StaticImageWriter -> Resolution -> FileProcessor | 88 | resizeStaticImageUpTo :: Resolution -> LossyExportQuality -> PictureFileFormat -> FileProcessor |
102 | resizeStaticGeneric reader writer maxRes inputPath outputPath = | 89 | resizeStaticImageUpTo maxResolution jpegExportQuality pictureFormat = |
103 | (putStrLn $ "Generating:\t" ++ outputPath) | 90 | resizerFor pictureFormat |
104 | >> reader inputPath | ||
105 | >>= either (throwIO . ProcessingException inputPath) return | ||
106 | >>= return . (fitDynamicImage maxRes) | ||
107 | >>= ensureParentDir writer outputPath | ||
108 | |||
109 | fitDynamicImage :: Resolution -> DynamicImage -> DynamicImage | ||
110 | fitDynamicImage (Resolution boxWidth boxHeight) image = | ||
111 | convertRGBA8 image | ||
112 | & scaleBilinear targetWidth targetHeight | ||
113 | & ImageRGBA8 | ||
114 | where | 91 | where |
115 | picWidth = dynamicMap imageWidth image | 92 | resizerFor :: PictureFileFormat -> FileProcessor |
116 | picHeight = dynamicMap imageHeight image | 93 | resizerFor Bmp = resizer readBitmap saveBmpImage |
117 | resizeRatio = min (boxWidth % picWidth) (boxHeight % picHeight) | 94 | resizerFor Jpg = resizer readJpeg (saveJpgImage jpegExportQuality) |
118 | targetWidth = floor $ resizeRatio * (picWidth % 1) | 95 | resizerFor Png = resizer readPng savePngImage |
119 | targetHeight = floor $ resizeRatio * (picHeight % 1) | 96 | resizerFor Tiff = resizer readTiff saveTiffImage |
97 | resizerFor Hdr = resizer readHDR saveRadianceImage | ||
98 | resizerFor Gif = resizer readGif saveGifImage' | ||
99 | where | ||
100 | saveGifImage' :: StaticImageWriter | ||
101 | saveGifImage' outputPath image = | ||
102 | saveGifImage outputPath image | ||
103 | & either (throwIO . ProcessingException outputPath) id | ||
104 | |||
105 | resizer :: StaticImageReader -> StaticImageWriter -> FileProcessor | ||
106 | resizer reader writer inputPath outputPath = | ||
107 | (putStrLn $ "Generating:\t" ++ outputPath) | ||
108 | >> reader inputPath | ||
109 | >>= either (throwIO . ProcessingException inputPath) return | ||
110 | >>= return . (fitDynamicImage maxResolution) | ||
111 | >>= ensureParentDir writer outputPath | ||
112 | |||
113 | fitDynamicImage :: Resolution -> DynamicImage -> DynamicImage | ||
114 | fitDynamicImage (Resolution boxWidth boxHeight) image = | ||
115 | convertRGBA8 image | ||
116 | & scaleBilinear targetWidth targetHeight | ||
117 | & ImageRGBA8 | ||
118 | where | ||
119 | picWidth = dynamicMap imageWidth image | ||
120 | picHeight = dynamicMap imageHeight image | ||
121 | resizeRatio = min (boxWidth % picWidth) (boxHeight % picHeight) | ||
122 | targetWidth = floor $ resizeRatio * (picWidth % 1) | ||
123 | targetHeight = floor $ resizeRatio * (picHeight % 1) | ||
120 | 124 | ||
121 | 125 | ||
122 | type Cache = FileProcessor -> FileProcessor | 126 | type Cache = FileProcessor -> FileProcessor |
@@ -152,8 +156,8 @@ type ItemFileProcessor = | |||
152 | -> FileName -- ^ Output class (subdir) | 156 | -> FileName -- ^ Output class (subdir) |
153 | -> ItemProcessor | 157 | -> ItemProcessor |
154 | 158 | ||
155 | itemFileProcessor :: Maybe Resolution -> Cache -> ItemFileProcessor | 159 | itemFileProcessor :: Maybe Resolution -> LossyExportQuality -> Cache -> ItemFileProcessor |
156 | itemFileProcessor maxResolution cached inputBase outputBase resClass inputRes = | 160 | itemFileProcessor maxResolution jpegExportQuality cached inputBase outputBase resClass inputRes = |
157 | cached processor inPath outPath | 161 | cached processor inPath outPath |
158 | >> return (props relOutPath) | 162 | >> return (props relOutPath) |
159 | where | 163 | where |
@@ -168,7 +172,7 @@ itemFileProcessor maxResolution cached inputBase outputBase resClass inputRes = | |||
168 | processorFor _ (PictureFormat Gif) = | 172 | processorFor _ (PictureFormat Gif) = |
169 | (copyFileProcessor, Picture) -- TODO: handle animated gif resizing | 173 | (copyFileProcessor, Picture) -- TODO: handle animated gif resizing |
170 | processorFor (Just maxRes) (PictureFormat picFormat) = | 174 | processorFor (Just maxRes) (PictureFormat picFormat) = |
171 | (resizeStaticImageUpTo picFormat maxRes, Picture) | 175 | (resizeStaticImageUpTo maxRes jpegExportQuality picFormat, Picture) |
172 | processorFor _ Unknown = | 176 | processorFor _ Unknown = |
173 | (copyFileProcessor, Other) -- TODO: handle video reencoding and others? | 177 | (copyFileProcessor, Other) -- TODO: handle video reencoding and others? |
174 | 178 | ||
@@ -179,8 +183,8 @@ type ThumbnailFileProcessor = | |||
179 | -> FileName -- ^ Output class (subdir) | 183 | -> FileName -- ^ Output class (subdir) |
180 | -> ThumbnailProcessor | 184 | -> ThumbnailProcessor |
181 | 185 | ||
182 | thumbnailFileProcessor :: Resolution -> Cache -> ThumbnailFileProcessor | 186 | thumbnailFileProcessor :: Resolution -> LossyExportQuality -> Cache -> ThumbnailFileProcessor |
183 | thumbnailFileProcessor maxRes cached inputBase outputBase resClass inputRes = | 187 | thumbnailFileProcessor maxRes jpegExportQuality cached inputBase outputBase resClass inputRes = |
184 | cached <$> processorFor (formatFromPath inputRes) | 188 | cached <$> processorFor (formatFromPath inputRes) |
185 | & process | 189 | & process |
186 | where | 190 | where |
@@ -196,6 +200,6 @@ thumbnailFileProcessor maxRes cached inputBase outputBase resClass inputRes = | |||
196 | 200 | ||
197 | processorFor :: Format -> Maybe FileProcessor | 201 | processorFor :: Format -> Maybe FileProcessor |
198 | processorFor (PictureFormat picFormat) = | 202 | processorFor (PictureFormat picFormat) = |
199 | Just $ resizeStaticImageUpTo picFormat maxRes | 203 | Just $ resizeStaticImageUpTo maxRes jpegExportQuality picFormat |
200 | processorFor _ = | 204 | processorFor _ = |
201 | Nothing | 205 | Nothing |
diff --git a/example/gallery.yaml b/example/gallery.yaml index 7fb2c92..dfb6265 100644 --- a/example/gallery.yaml +++ b/example/gallery.yaml | |||
@@ -20,5 +20,7 @@ compiler: | |||
20 | width: 1024 | 20 | width: 1024 |
21 | height: 768 | 21 | height: 768 |
22 | 22 | ||
23 | #jpegExportQuality: 80 # default | ||
24 | |||
23 | 25 | ||
24 | viewer: {} | 26 | viewer: {} |
diff --git a/ldgallery.1.md b/ldgallery.1.md index febe2e2..ba8d4f9 100644 --- a/ldgallery.1.md +++ b/ldgallery.1.md | |||
@@ -120,6 +120,9 @@ compiler.pictureMaxResolution.width | |||
120 | compiler.pictureMaxResolution.height | 120 | compiler.pictureMaxResolution.height |
121 | : Maximum height in pixels of the picture items, unlimited by default. | 121 | : Maximum height in pixels of the picture items, unlimited by default. |
122 | 122 | ||
123 | compiler.jpegExportQuality | ||
124 | : JPEG export quality percentage as an integer, 80 by default. | ||
125 | |||
123 | viewer.defaultSearchMode [TODO] | 126 | viewer.defaultSearchMode [TODO] |
124 | : Default search view mode ("highlight" or "filter"). Defaults to "filter". | 127 | : Default search view mode ("highlight" or "filter"). Defaults to "filter". |
125 | 128 | ||