From 06c9eea0d47d68528cb4dd7dcf218c7e03a4eb77 Mon Sep 17 00:00:00 2001 From: Noa Himesaka Date: Sun, 24 May 2026 15:14:27 +0900 Subject: [PATCH] feat: saturation control (#15381) --- base | 2 +- defaults.lua | 1 + .../apps/reader/modules/readerhinting.lua | 3 +- .../reader/modules/readerkoptlistener.lua | 1 + frontend/apps/reader/modules/readerpaging.lua | 9 ++++++ frontend/apps/reader/modules/readerview.lua | 18 ++++++++++-- frontend/document/djvudocument.lua | 12 ++++---- frontend/document/document.lua | 29 +++++++++++-------- frontend/document/koptinterface.lua | 18 ++++++------ frontend/document/pdfdocument.lua | 12 ++++---- frontend/ui/data/koptoptions.lua | 21 ++++++++++++++ spec/unit/cache_spec.lua | 8 ++--- spec/unit/readerlink_spec.lua | 2 ++ spec/unit/readerview_spec.lua | 2 ++ 14 files changed, 97 insertions(+), 41 deletions(-) diff --git a/base b/base index 372590341..4d5e13ee7 160000 --- a/base +++ b/base @@ -1 +1 @@ -Subproject commit 37259034110335af889510146b2667dc8933c30c +Subproject commit 4d5e13ee74b48b8368d380d23ba6bba915ec169e diff --git a/defaults.lua b/defaults.lua index da7f11a6f..ec10e1183 100644 --- a/defaults.lua +++ b/defaults.lua @@ -87,6 +87,7 @@ DKOPTREADER_CONFIG_AUTO_STRAIGHTEN = 0, -- range from 0 to 10 DKOPTREADER_CONFIG_JUSTIFICATION = 3, -- -1 = auto, 0 = left, 1 = center, 2 = right, 3 = full DKOPTREADER_CONFIG_MAX_COLUMNS = 2, -- range from 1 to 4 DKOPTREADER_CONFIG_CONTRAST = 1.0, -- range from 0.2 to 2.0 +DKOPTREADER_CONFIG_SATURATION = 1.0, -- range from 0.2 to 2.0 DKOPTREADER_CONFIG_WHITE_THRESHOLD = 255, -- range from 0 to 255 -- word spacing for reflow diff --git a/frontend/apps/reader/modules/readerhinting.lua b/frontend/apps/reader/modules/readerhinting.lua index 4dee4f112..26edf71c8 100644 --- a/frontend/apps/reader/modules/readerhinting.lua +++ b/frontend/apps/reader/modules/readerhinting.lua @@ -18,7 +18,8 @@ function ReaderHinting:onHintPage() self.view.state.page + i, self.zoom:getZoom(self.view.state.page + i), self.view.state.rotation, - self.view.state.gamma) + self.view.state.gamma, + self.view.state.saturation) end end return true diff --git a/frontend/apps/reader/modules/readerkoptlistener.lua b/frontend/apps/reader/modules/readerkoptlistener.lua index 045170f45..263cd0a4f 100644 --- a/frontend/apps/reader/modules/readerkoptlistener.lua +++ b/frontend/apps/reader/modules/readerkoptlistener.lua @@ -23,6 +23,7 @@ function ReaderKoptListener:onReadSettings(config) self.normal_zoom_mode = normal_zoom_mode self:setZoomMode(normal_zoom_mode) self.ui:handleEvent(Event:new("GammaUpdate", self.document.configurable.contrast, true)) -- no notification + self.ui:handleEvent(Event:new("SaturationUpdate", self.document.configurable.saturation, true)) -- no notification -- since K2pdfopt v2.21 negative value of word spacing is also used, for config -- compatibility we should manually change previous -1 to a more reasonable -0.2 if self.document.configurable.word_spacing == -1 then diff --git a/frontend/apps/reader/modules/readerpaging.lua b/frontend/apps/reader/modules/readerpaging.lua index 0fee89edc..e9423dba5 100644 --- a/frontend/apps/reader/modules/readerpaging.lua +++ b/frontend/apps/reader/modules/readerpaging.lua @@ -716,6 +716,13 @@ function ReaderPaging:onUpdateScrollPageGamma(gamma) return true end +function ReaderPaging:onUpdateScrollPageSaturation(saturation) + for _, state in ipairs(self.view.page_states) do + state.saturation = saturation + end + return true +end + function ReaderPaging:getNextPageState(blank_area, image_offset) local page_area = self.view:getPageArea( self.view.state.page, @@ -736,6 +743,7 @@ function ReaderPaging:getNextPageState(blank_area, image_offset) zoom = self.view.state.zoom, rotation = self.view.state.rotation, gamma = self.view.state.gamma, + saturation = self.view.state.saturation, offset = page_offset, visible_area = visible_area, page_area = page_area, @@ -763,6 +771,7 @@ function ReaderPaging:getPrevPageState(blank_area, image_offset) zoom = self.view.state.zoom, rotation = self.view.state.rotation, gamma = self.view.state.gamma, + saturation = self.view.state.saturation, offset = page_offset, visible_area = visible_area, page_area = page_area, diff --git a/frontend/apps/reader/modules/readerview.lua b/frontend/apps/reader/modules/readerview.lua index fe7750fe4..6130acc8b 100644 --- a/frontend/apps/reader/modules/readerview.lua +++ b/frontend/apps/reader/modules/readerview.lua @@ -86,6 +86,7 @@ function ReaderView:init() zoom = 1.0, rotation = 0, gamma = 1.0, + saturation = 1.0, offset = nil, bbox = nil, } @@ -384,7 +385,8 @@ function ReaderView:drawScrollPages(bb, x, y) state.page, state.zoom, state.rotation, - state.gamma) + state.gamma, + state.saturation) pos.y = pos.y + state.visible_area.h -- draw page gap if not the last part if page ~= #self.page_states then @@ -458,7 +460,8 @@ function ReaderView:drawSinglePage(bb, x, y) self.state.page, self.state.zoom, self.state.rotation, - self.state.gamma) + self.state.gamma, + self.state.saturation) UIManager:nextTick(self.emitHintPageEvent) end @@ -871,6 +874,7 @@ function ReaderView:getViewContext() zoom = self.state.zoom, rotation = self.state.rotation, gamma = self.state.gamma, + saturation = self.state.saturation, offset = self.state.offset:copy(), bbox = self.state.bbox, }, @@ -1108,6 +1112,16 @@ function ReaderView:onGammaUpdate(gamma, no_notification) end end +function ReaderView:onSaturationUpdate(saturation, no_notification) + self.state.saturation = saturation + if self.page_scroll then + self.ui:handleEvent(Event:new("UpdateScrollPageSaturation", saturation)) + end + if not no_notification then + Notification:notify(T(_("Saturation set to: %1."), saturation)) + end +end + -- For ReaderKOptListener function ReaderView:onDitheringUpdate() -- Do the device cap checks again, to avoid snafus when sharing configs between devices diff --git a/frontend/document/djvudocument.lua b/frontend/document/djvudocument.lua index a95b4c174..ccecf3db5 100644 --- a/frontend/document/djvudocument.lua +++ b/frontend/document/djvudocument.lua @@ -285,16 +285,16 @@ function DjvuDocument:findAllText(pattern, case_insensitive, nb_context_words, m return self.koptinterface:findAllText(self, pattern, case_insensitive, nb_context_words, max_hits) end -function DjvuDocument:renderPage(pageno, rect, zoom, rotation, gamma, hinting) - return self.koptinterface:renderPage(self, pageno, rect, zoom, rotation, gamma, hinting) +function DjvuDocument:renderPage(pageno, rect, zoom, rotation, gamma, saturation, hinting) + return self.koptinterface:renderPage(self, pageno, rect, zoom, rotation, gamma, saturation, hinting) end -function DjvuDocument:hintPage(pageno, zoom, rotation, gamma) - return self.koptinterface:hintPage(self, pageno, zoom, rotation, gamma) +function DjvuDocument:hintPage(pageno, zoom, rotation, gamma, saturation) + return self.koptinterface:hintPage(self, pageno, zoom, rotation, gamma, saturation) end -function DjvuDocument:drawPage(target, x, y, rect, pageno, zoom, rotation, gamma) - return self.koptinterface:drawPage(self, target, x, y, rect, pageno, zoom, rotation, gamma) +function DjvuDocument:drawPage(target, x, y, rect, pageno, zoom, rotation, gamma, saturation) + return self.koptinterface:drawPage(self, target, x, y, rect, pageno, zoom, rotation, gamma, saturation) end function DjvuDocument:register(registry) diff --git a/frontend/document/document.lua b/frontend/document/document.lua index 57c8aa39f..39a523a5c 100644 --- a/frontend/document/document.lua +++ b/frontend/document/document.lua @@ -390,32 +390,34 @@ function Document:resetTileCacheValidity() self.tile_cache_validity_ts = os.time() end -function Document:getFullPageHash(pageno, zoom, rotation, gamma) +function Document:getFullPageHash(pageno, zoom, rotation, gamma, saturation) return "renderpg|"..self.file.."|"..self.mod_time.."|"..pageno.."|" ..zoom.."|"..rotation.."|"..gamma.."|"..self.configurable.background_cleanup.."|" ..self.render_mode..(self.render_color and "|color" or "|bw") ..(self.reflowable_font_size and "|"..self.reflowable_font_size or "") + .."|"..saturation end -function Document:getPagePartHash(pageno, zoom, rotation, gamma, rect) +function Document:getPagePartHash(pageno, zoom, rotation, gamma, saturation, rect) return "renderpgpart|"..self.file.."|"..self.mod_time.."|"..pageno.."|" ..tostring(rect).."|"..zoom.."|"..tostring(rect.scaled_rect) .."|"..rotation.."|"..gamma.."|"..self.configurable.background_cleanup.."|" ..self.render_mode..(self.render_color and "|color" or "|bw") ..(self.reflowable_font_size and "|"..self.reflowable_font_size or "") + .."|"..saturation end -function Document:renderPage(pageno, rect, zoom, rotation, gamma, hinting) +function Document:renderPage(pageno, rect, zoom, rotation, gamma, saturation, hinting) -- If rect contains a nested scaled_rect object, our caller handled scaling itself (e.g., drawPagePart) local is_prescaled = rect and rect.scaled_rect ~= nil or false local hash, hash_excerpt, tile if is_prescaled then - hash = self:getPagePartHash(pageno, zoom, rotation, gamma, rect) + hash = self:getPagePartHash(pageno, zoom, rotation, gamma, saturation, rect) tile = DocCache:check(hash, TileCacheItem) else - hash = self:getFullPageHash(pageno, zoom, rotation, gamma) + hash = self:getFullPageHash(pageno, zoom, rotation, gamma, saturation) tile = DocCache:check(hash, TileCacheItem) @@ -502,6 +504,9 @@ function Document:renderPage(pageno, rect, zoom, rotation, gamma, hinting) if gamma ~= self.GAMMA_NO_GAMMA then dc:setGamma(gamma) end + if saturation ~= 1.0 and dc.setSaturation then + dc:setSaturation(saturation) + end dc:setIsolateSMask(self.configurable.background_cleanup) @@ -519,9 +524,9 @@ end -- a hint for the cache engine to paint a full page to the cache --- @todo this should trigger a background operation -function Document:hintPage(pageno, zoom, rotation, gamma) +function Document:hintPage(pageno, zoom, rotation, gamma, saturation) logger.dbg("hinting page", pageno) - self:renderPage(pageno, nil, zoom, rotation, gamma, true) + self:renderPage(pageno, nil, zoom, rotation, gamma, saturation, true) end --[[ @@ -532,8 +537,8 @@ Draw page content to blitbuffer. @target: target blitbuffer @rect: visible_area inside document page --]] -function Document:drawPage(target, x, y, rect, pageno, zoom, rotation, gamma) - local tile = self:renderPage(pageno, rect, zoom, rotation, gamma) +function Document:drawPage(target, x, y, rect, pageno, zoom, rotation, gamma, saturation) + local tile = self:renderPage(pageno, rect, zoom, rotation, gamma, saturation) -- Enable SW dithering if requested (only available in koptoptions) if self.sw_dithering then target:ditherblitFrom(tile.bb, @@ -550,8 +555,8 @@ function Document:drawPage(target, x, y, rect, pageno, zoom, rotation, gamma) end end -function Document:drawPageInverted(target, x, y, rect, pageno, zoom, rotation, gamma) - local tile = self:renderPage(pageno, rect, zoom, rotation, gamma) +function Document:drawPageInverted(target, x, y, rect, pageno, zoom, rotation, gamma, saturation) + local tile = self:renderPage(pageno, rect, zoom, rotation, gamma, saturation) target:invertblitFrom(tile.bb, x, y, rect.x - tile.excerpt.x, @@ -582,7 +587,7 @@ function Document:drawPagePart(pageno, native_rect, rotation) rect.scaled_rect = scaled_rect -- Enable SMP via the hinting flag - local tile = self:renderPage(pageno, rect, zoom, rotation, 1.0, true) + local tile = self:renderPage(pageno, rect, zoom, rotation, 1.0, 1.0, true) return tile.bb, rotate end diff --git a/frontend/document/koptinterface.lua b/frontend/document/koptinterface.lua index b7374d8d2..5b1e6815b 100644 --- a/frontend/document/koptinterface.lua +++ b/frontend/document/koptinterface.lua @@ -391,7 +391,7 @@ function KoptInterface:getCoverPageImage(doc) local native_size = Document.getNativePageDimensions(doc, 1) local canvas_size = CanvasContext:getSize() local zoom = math.min(canvas_size.w / native_size.w, canvas_size.h / native_size.h) - local tile = Document.renderPage(doc, 1, nil, zoom, 0, 1.0, false) + local tile = Document.renderPage(doc, 1, nil, zoom, 0, 1.0, 1.0, false) if tile then return tile.bb:copy() end @@ -401,13 +401,13 @@ function KoptInterface:is_optimizing_page(doc) return doc.configurable.page_opt == 1 or doc.configurable.auto_straighten > 0 or doc.configurable.white_threshold ~= 255 end -function KoptInterface:renderPage(doc, pageno, rect, zoom, rotation, gamma, hinting) +function KoptInterface:renderPage(doc, pageno, rect, zoom, rotation, gamma, saturation, hinting) if doc.configurable.text_wrap == 1 then return self:renderReflowedPage(doc, pageno, rect, zoom, rotation, hinting) elseif self:is_optimizing_page(doc) then return self:renderOptimizedPage(doc, pageno, rect, zoom, rotation, hinting) else - return Document.renderPage(doc, pageno, rect, zoom, rotation, gamma, hinting) + return Document.renderPage(doc, pageno, rect, zoom, rotation, gamma, saturation, hinting) end end @@ -545,7 +545,7 @@ function KoptInterface:renderOptimizedPage(doc, pageno, rect, zoom, rotation, hi end end -function KoptInterface:hintPage(doc, pageno, zoom, rotation, gamma) +function KoptInterface:hintPage(doc, pageno, zoom, rotation, gamma, saturation) --- @note: Crappy safeguard around memory issues like in #7627: if we're eating too much RAM, drop half the cache... DocCache:memoryPressureCheck() @@ -554,7 +554,7 @@ function KoptInterface:hintPage(doc, pageno, zoom, rotation, gamma) elseif self:is_optimizing_page(doc) then self:renderOptimizedPage(doc, pageno, nil, zoom, rotation, true) else - Document.hintPage(doc, pageno, zoom, rotation, gamma) + Document.hintPage(doc, pageno, zoom, rotation, gamma, saturation) end end @@ -587,16 +587,16 @@ function KoptInterface:hintReflowedPage(doc, pageno, zoom, rotation, hinting) end end -function KoptInterface:drawPage(doc, target, x, y, rect, pageno, zoom, rotation, gamma) +function KoptInterface:drawPage(doc, target, x, y, rect, pageno, zoom, rotation, gamma, saturation) local nightmode_invert = doc.configurable.nightmode_document == 1 and Screen.night_mode if doc.configurable.text_wrap == 1 then self:drawContextPage(doc, target, x, y, rect, pageno, zoom, rotation, nightmode_invert) elseif self:is_optimizing_page(doc) then self:drawContextPage(doc, target, x, y, rect, pageno, zoom, rotation, nightmode_invert) elseif nightmode_invert then - Document.drawPageInverted(doc, target, x, y, rect, pageno, zoom, rotation, gamma) + Document.drawPageInverted(doc, target, x, y, rect, pageno, zoom, rotation, gamma, saturation) else - Document.drawPage(doc, target, x, y, rect, pageno, zoom, rotation, gamma) + Document.drawPage(doc, target, x, y, rect, pageno, zoom, rotation, gamma, saturation) end end @@ -606,7 +606,7 @@ Draw cached tile pixels into target blitbuffer. Inherited from common document interface. --]] function KoptInterface:drawContextPage(doc, target, x, y, rect, pageno, zoom, rotation, nightmode_invert) - local tile = self:renderPage(doc, pageno, rect, zoom, rotation, 1.0, false) + local tile = self:renderPage(doc, pageno, rect, zoom, rotation, 1.0, 1.0, false) target:blitFrom(tile.bb, x, y, rect.x - tile.excerpt.x, diff --git a/frontend/document/pdfdocument.lua b/frontend/document/pdfdocument.lua index 279b2d4c9..7e20b0df8 100644 --- a/frontend/document/pdfdocument.lua +++ b/frontend/document/pdfdocument.lua @@ -357,16 +357,16 @@ function PdfDocument:findAllText(pattern, case_insensitive, nb_context_words, ma return self.koptinterface:findAllText(self, pattern, case_insensitive, nb_context_words, max_hits) end -function PdfDocument:renderPage(pageno, rect, zoom, rotation, gamma, hinting) - return self.koptinterface:renderPage(self, pageno, rect, zoom, rotation, gamma, hinting) +function PdfDocument:renderPage(pageno, rect, zoom, rotation, gamma, saturation, hinting) + return self.koptinterface:renderPage(self, pageno, rect, zoom, rotation, gamma, saturation, hinting) end -function PdfDocument:hintPage(pageno, zoom, rotation, gamma) - return self.koptinterface:hintPage(self, pageno, zoom, rotation, gamma) +function PdfDocument:hintPage(pageno, zoom, rotation, gamma, saturation) + return self.koptinterface:hintPage(self, pageno, zoom, rotation, gamma, saturation) end -function PdfDocument:drawPage(target, x, y, rect, pageno, zoom, rotation, gamma) - return self.koptinterface:drawPage(self, target, x, y, rect, pageno, zoom, rotation, gamma) +function PdfDocument:drawPage(target, x, y, rect, pageno, zoom, rotation, gamma, saturation) + return self.koptinterface:drawPage(self, target, x, y, rect, pageno, zoom, rotation, gamma, saturation) end function PdfDocument:register(registry) diff --git a/frontend/ui/data/koptoptions.lua b/frontend/ui/data/koptoptions.lua index ff4d370c0..3357f6198 100644 --- a/frontend/ui/data/koptoptions.lua +++ b/frontend/ui/data/koptoptions.lua @@ -506,6 +506,27 @@ Some of the other settings are only available when reflow mode is enabled.]]), precision = "%.1f", }, }, + { + name = "saturation", + name_text = _("Saturation"), + buttonprogress = true, + values = {0.2, 0.5, 1.0, 1.2, 1.4, 1.6, 1.8, 2.0}, + default_pos = 3, + default_value = G_defaults:readSetting("DKOPTREADER_CONFIG_SATURATION"), + event = "SaturationUpdate", + args = {0.2, 0.5, 1.0, 1.2, 1.4, 1.6, 1.8, 2.0}, + labels = {0.2, 0.5, 1.0, 1.2, 1.4, 1.6, 1.8, 2.0}, + show = Device:hasColorScreen(), + name_text_hold_callback = optionsutil.showValues, + more_options = true, + more_options_param = { + value_step = 0.1, value_hold_step = 0.2, + value_min = 0.2, value_max = 2.0, + precision = "%.1f", + }, + help_text = _([[Adjust color saturation for rendered pages. +Lower values desaturate colors, 1.0 keeps original colors, and higher values increase color intensity.]]), + }, { name = "white_threshold", name_text = _("White Threshold"), diff --git a/spec/unit/cache_spec.lua b/spec/unit/cache_spec.lua index 2021cd2bf..01332d6cf 100644 --- a/spec/unit/cache_spec.lua +++ b/spec/unit/cache_spec.lua @@ -20,7 +20,7 @@ describe("Cache module", function() it("should serialize blitbuffer", function() for pageno = 1, math.min(max_page, doc.info.number_of_pages) do - doc:renderPage(pageno, nil, 1, 0, 1.0) + doc:renderPage(pageno, nil, 1, 0, 1.0, 1.0) DocCache:serialize() end DocCache:clear() @@ -28,7 +28,7 @@ describe("Cache module", function() it("should deserialize blitbuffer", function() for pageno = 1, math.min(max_page, doc.info.number_of_pages) do - doc:hintPage(pageno, 1, 0, 1.0) + doc:hintPage(pageno, 1, 0, 1.0, 1.0) end DocCache:clear() end) @@ -36,7 +36,7 @@ describe("Cache module", function() it("should serialize koptcontext", function() doc.configurable.text_wrap = 1 for pageno = 1, math.min(max_page, doc.info.number_of_pages) do - doc:renderPage(pageno, nil, 1, 0, 1.0) + doc:renderPage(pageno, nil, 1, 0, 1.0, 1.0) doc:getPageDimensions(pageno) DocCache:serialize() end @@ -46,7 +46,7 @@ describe("Cache module", function() it("should deserialize koptcontext", function() for pageno = 1, math.min(max_page, doc.info.number_of_pages) do - doc:renderPage(pageno, nil, 1, 0, 1.0) + doc:renderPage(pageno, nil, 1, 0, 1.0, 1.0) end DocCache:clear() end) diff --git a/spec/unit/readerlink_spec.lua b/spec/unit/readerlink_spec.lua index 7102595f8..2609977a4 100644 --- a/spec/unit/readerlink_spec.lua +++ b/spec/unit/readerlink_spec.lua @@ -118,6 +118,7 @@ describe("ReaderLink module", function() h = 800, w = 566, }, rotation = 0, + saturation = 1, visible_area = { x = 0, y = 694, h = 106, w = 566, @@ -133,6 +134,7 @@ describe("ReaderLink module", function() x = 0, y = 0, }, rotation = 0, + saturation = 1, visible_area = { h = 686, w = 566, x = 0, y = 0, diff --git a/spec/unit/readerview_spec.lua b/spec/unit/readerview_spec.lua index 7271d2f5a..b25a49195 100644 --- a/spec/unit/readerview_spec.lua +++ b/spec/unit/readerview_spec.lua @@ -71,6 +71,7 @@ describe("Readerview module", function() page = 1, pos = 0, gamma = 1, + saturation = 1, offset = { x = 17, y = 0, h = 0, w = 0, @@ -124,6 +125,7 @@ describe("Readerview module", function() local saved_ctx = { { gamma = 1, + saturation = 1, offset = {x = 17, y = 0}, page = 1, page_area = {