diff --git a/Nodes/Rasterix.py b/Nodes/Rasterix.py index 298cf2d..d4eedd6 100644 --- a/Nodes/Rasterix.py +++ b/Nodes/Rasterix.py @@ -188,6 +188,7 @@ class PrimereRasterix: if use_ai_detection_bypasser: pil_img = isgen_detect_ext_full.bypass_ai_detector(image=pil_img, freq_strength=adb_freq_strength, variance_strength=adb_variance_strength, unsharp_percent=adb_unsharp_percent, jpeg_cycles=adb_jpeg_cycles) + histogram.rasterix_hist_cache_store(pil_img_input, pil_img, precision) if show_histogram: histogram.rasterix_hist_cache_store(pil_img_input, pil_img, precision) active_hist = histogram.rasterix_hist_render_selected(pil_img_input, pil_img, precision, histogram_source, histogram_channel, histogram_style,) @@ -762,6 +763,8 @@ class PrimereHistogram: pil_img = utility.tensor_to_image(image) pil_img_input = pil_img.copy() + histogram.rasterix_hist_cache_store(pil_img_input, pil_img, precision) + if show_histogram: histogram.rasterix_hist_cache_store(pil_img_input, pil_img, precision) active_hist = histogram.rasterix_hist_render_selected(pil_img_input, pil_img, precision, histogram_source, histogram_channel, histogram_style,) diff --git a/components/images/histogram.py b/components/images/histogram.py index 8de563e..ab382ea 100644 --- a/components/images/histogram.py +++ b/components/images/histogram.py @@ -6,6 +6,7 @@ import json from ..tree import PRIMERE_ROOT from .. import utility import os +from PIL import Image # ───────────────────────────────────────────────────────────────────────────── # Channel definitions @@ -553,7 +554,7 @@ def rasterix_hist_render_selected(pil_input, pil_output, precision, histogram_so source_prefix = "input" if histogram_source else "output" target_file = os.path.join(hist_dir, f'{source_prefix}_histogram_{histogram_channel.lower()}_{histogram_style}.jpg') if os.path.isfile(target_file): - return utility.ImageLoaderFromPath(target_file)[0] + return Image.open(target_file).convert("RGB") source_image = pil_input if histogram_source else pil_output rendered = rasterix_histogram_render(source_image, histogram_channel, histogram_style, precision) rendered.save(target_file, quality=90) diff --git a/components/primereserver.py b/components/primereserver.py index 2c297b9..2020057 100644 --- a/components/primereserver.py +++ b/components/primereserver.py @@ -578,6 +578,7 @@ async def primere_rasterix_histogram_generate(request): histogram_channel = post.get('histogram_channel', 'RGB') histogram_style = post.get('histogram_style', 'bars') precision = bool(post.get('precision', False)) + force = bool(post.get('force', False)) hist_dir = os.path.join(PRIMERE_ROOT, 'front_end', 'images') input_cache = os.path.join(hist_dir, "rasterix_hist_cache_input.png") @@ -590,7 +591,7 @@ async def primere_rasterix_histogram_generate(request): source_img = Image.open(source_path).convert("RGB") source_prefix = "input" if histogram_source else "output" target_file = os.path.join(hist_dir, f'{source_prefix}_histogram_{histogram_channel.lower()}_{histogram_style}.jpg') - if not os.path.isfile(target_file): + if force or not os.path.isfile(target_file): rendered = histogram.rasterix_histogram_render(source_img, histogram_channel, histogram_style, precision) rendered.save(target_file, quality=90) return web.json_response({"success": True, "filename": os.path.basename(target_file)}) \ No newline at end of file diff --git a/front_end/primere_rasterix.js b/front_end/primere_rasterix.js index a8cf2f8..bac6e6a 100644 --- a/front_end/primere_rasterix.js +++ b/front_end/primere_rasterix.js @@ -147,6 +147,27 @@ app.registerExtension({ img.src = url; } + function histogramFileUrl(showInput, channel, style) { + const prefix = showInput ? "input" : "output"; + const ch = (channel || "RGB").toLowerCase(); + const st = style || "bars"; + return `/extensions/ComfyUI_Primere_Nodes/images/${prefix}_histogram_${ch}_${st}.jpg`; + } + + async function histogramFileExists(showInput, channel, style) { + const url = `${histogramFileUrl(showInput, channel, style)}?t=${Date.now()}`; + try { + const headResp = await fetch(url, { method: "HEAD" }); + if (headResp.ok) return true; + } catch (_) {} + try { + const getResp = await fetch(url, { method: "GET" }); + return getResp.ok; + } catch (_) { + return false; + } + } + async function generateHistogram(showInput, channel, style) { try { await fetch('/primere_rasterix_histogram_generate', { @@ -172,6 +193,15 @@ app.registerExtension({ }, delayMs); } + async function requestHistogramSwitch(showInput, channel, style) { + if (await histogramFileExists(showInput, channel, style)) { + if (histogramDebounceTimer) clearTimeout(histogramDebounceTimer); + updateHistogramDisplay(showInput, channel, style); + return; + } + scheduleHistogramGenerate(showInput, channel, style, 1000); + } + function currentHistState() { return { enabled: fw("show_histogram")?.value ?? false, @@ -187,6 +217,10 @@ app.registerExtension({ showHistogramOffImage(); return; } + if (await histogramFileExists(showInput, channel, style)) { + updateHistogramDisplay(showInput, channel, style); + return; + } await generateHistogram(showInput, channel, style); updateHistogramDisplay(showInput, channel, style); }; @@ -314,17 +348,17 @@ app.registerExtension({ } else if (name === "histogram_source") { const { enabled, channel, style } = currentHistState(); - if (enabled) scheduleHistogramGenerate(value, channel, style); + if (enabled) requestHistogramSwitch(value, channel, style); } else if (name === "histogram_channel") { const { enabled, showInput, style } = currentHistState(); - if (enabled) scheduleHistogramGenerate(showInput, value, style); + if (enabled) requestHistogramSwitch(showInput, value, style); } else if (name === "histogram_style") { const { enabled, showInput, channel } = currentHistState(); - if (enabled) scheduleHistogramGenerate(showInput, channel, value); + if (enabled) requestHistogramSwitch(showInput, channel, value); } else if (name === "show_histogram") { const { showInput, channel, style } = currentHistState(); if (value) { - scheduleHistogramGenerate(showInput, channel, style, 50); + requestHistogramSwitch(showInput, channel, style); } else { if (histogramDebounceTimer) clearTimeout(histogramDebounceTimer); showHistogramOffImage();