{ "id": "sam-prompter", "name": "SAM Prompter", "description": "Interactive SAM prompt input and mask visualization", "author": "hysts", "tags": [ "sam", "interactive segmentation" ], "html_template": "
\n \n
\n
\n
\n \n
\n
\n \n \n \n \n \n \n \n \n \n \n \n
\n
\n
\n
\n Object color\n
\n
\n
\n Point size\n \n \n
\n
\n Box outline\n \n \n
\n
\n Mask opacity\n \n \n
\n
\n
\n \n
\n
\n
\n
\n
\n \n Drop image here or click to upload\n
\n
\n
\n
\n

Keyboard Shortcuts

\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
Left ClickAdd foreground point
Right ClickAdd background point
Left DragDraw bounding box
Alt + ClickDelete nearest point / box
1\u20138Switch object
NAdd new object
ZUndo last prompt
DeleteDelete active object
HToggle active object visibility
MToggle mask display
Space (hold)Temporary move mode
ScrollZoom in/out
+ / - / 0Zoom in / out / reset
Middle DragPan
Double-clickReset zoom (move mode)
VToggle view settings
IToggle image visibility
CToggle cutout view (foreground only)
?Toggle help
FToggle maximize
EscapeCancel box drawing / close help / exit maximize
\n \n
\n
\n
\n", "css_template": ".sam-prompter-container,\n.sam-prompter-container * {\n box-sizing: border-box;\n}\n\n.sam-prompter-container {\n position: relative;\n width: 100%;\n background: var(--background-fill-primary, #ffffff);\n border: 1px solid var(--border-color-primary, #e0e0e0);\n border-radius: 8px;\n overflow: hidden;\n font-family: var(--font, system-ui, sans-serif);\n font-size: 13px;\n color: var(--body-text-color, #333333);\n}\n\n/* Maximized state (class on element wrapper, outside morph scope) */\n&.sp-maximized .sam-prompter-container {\n position: fixed;\n top: 0;\n left: 0;\n width: 100vw;\n height: 100vh;\n z-index: 9999;\n border-radius: 0;\n display: flex;\n flex-direction: column;\n}\n\n&.sp-maximized .canvas-wrapper {\n flex: 1;\n min-height: 0;\n border-radius: 0;\n}\n\n&.sp-maximized .maximize-btn {\n background: var(--color-accent, #2196F3);\n color: #fff;\n border-color: var(--color-accent, #2196F3);\n}\n\n/* Toolbar */\n.sam-prompter-container .toolbar {\n display: flex;\n align-items: center;\n justify-content: space-between;\n padding: 6px 8px;\n gap: 6px;\n background: var(--background-fill-secondary, #f7f7f7);\n border-bottom: 1px solid var(--border-color-primary, #e0e0e0);\n flex-wrap: wrap-reverse;\n}\n\n.sam-prompter-container .toolbar-left,\n.sam-prompter-container .toolbar-right {\n display: flex;\n align-items: center;\n gap: 4px;\n flex-wrap: wrap;\n}\n\n.sam-prompter-container .object-tabs {\n display: flex;\n align-items: center;\n gap: 2px;\n}\n\n.sam-prompter-container .object-tab {\n all: unset;\n display: inline-flex;\n align-items: center;\n gap: 4px;\n height: 26px;\n padding: 0 8px;\n border-radius: 4px;\n cursor: pointer;\n font-size: 12px;\n color: var(--body-text-color, #333333);\n background: transparent;\n border: 1px solid transparent;\n transition: background 0.15s, border-color 0.15s;\n}\n\n.sam-prompter-container .object-tab:hover {\n background: var(--background-fill-primary, #ffffff);\n}\n\n.sam-prompter-container .object-tab.active {\n background: var(--background-fill-primary, #ffffff);\n border-color: var(--border-color-primary, #e0e0e0);\n font-weight: 600;\n box-shadow: 0 0 0 2px var(--color-accent, #2196F3);\n}\n\n.sam-prompter-container .object-tab .color-dot {\n width: 10px;\n height: 10px;\n border-radius: 50%;\n flex-shrink: 0;\n}\n\n.sam-prompter-container .object-tab.hidden-object {\n opacity: 0.5;\n}\n\n.sam-prompter-container .object-tab .visibility-toggle {\n all: unset;\n display: inline-flex;\n align-items: center;\n justify-content: center;\n width: 16px;\n height: 16px;\n border-radius: 50%;\n cursor: pointer;\n color: var(--body-text-color-subdued, #888888);\n}\n\n.sam-prompter-container .object-tab .visibility-toggle:hover {\n color: var(--body-text-color, #333333);\n}\n\n.sam-prompter-container .object-tab .visibility-toggle svg {\n display: block;\n}\n\n.sam-prompter-container .object-tab .delete-tab {\n all: unset;\n display: inline-flex;\n align-items: center;\n justify-content: center;\n width: 16px;\n height: 16px;\n border-radius: 50%;\n font-size: 11px;\n line-height: 1;\n cursor: pointer;\n color: var(--body-text-color-subdued, #888888);\n opacity: 0;\n transition: opacity 0.15s;\n}\n\n.sam-prompter-container .object-tab:hover .delete-tab {\n opacity: 1;\n}\n\n.sam-prompter-container .object-tab .delete-tab:hover {\n background: rgba(255, 0, 0, 0.1);\n color: #ff4444;\n}\n\n/* Buttons */\n.sam-prompter-container .sp-btn {\n all: unset;\n display: inline-flex;\n align-items: center;\n justify-content: center;\n height: 26px;\n padding: 0 8px;\n border-radius: 4px;\n cursor: pointer;\n font-size: 12px;\n color: var(--body-text-color, #333333);\n background: var(--background-fill-primary, #ffffff);\n border: 1px solid var(--border-color-primary, #e0e0e0);\n transition: background 0.15s, border-color 0.15s;\n}\n\n.sam-prompter-container .sp-btn:hover {\n background: var(--background-fill-secondary, #f0f0f0);\n border-color: var(--color-accent, #2196F3);\n}\n\n.sam-prompter-container .sp-btn.active {\n background: var(--color-accent, #2196F3);\n color: white;\n border-color: var(--color-accent, #2196F3);\n}\n\n.sam-prompter-container .sp-btn:disabled {\n opacity: 0.4;\n cursor: default;\n pointer-events: none;\n}\n\n.sam-prompter-container .help-btn {\n width: 26px;\n padding: 0;\n font-weight: bold;\n}\n\n.sam-prompter-container .clear-image-btn {\n width: 26px;\n padding: 0;\n font-size: 16px;\n line-height: 1;\n}\n\n/* Settings bar */\n.sam-prompter-container .settings-bar {\n display: flex;\n align-items: center;\n padding: 6px 8px;\n gap: 6px;\n background: var(--background-fill-secondary, #f7f7f7);\n border-bottom: 1px solid var(--border-color-primary, #e0e0e0);\n flex-wrap: wrap;\n}\n\n.sam-prompter-container .settings-bar.hidden {\n display: none;\n}\n\n.sam-prompter-container .settings-group {\n display: flex;\n align-items: center;\n gap: 6px;\n margin-right: 16px;\n}\n\n.sam-prompter-container .settings-label {\n font-size: 11px;\n white-space: nowrap;\n color: var(--body-text-color-subdued, #888888);\n}\n\n.sam-prompter-container .color-swatches {\n display: flex;\n gap: 3px;\n}\n\n.sam-prompter-container .color-swatch {\n all: unset;\n width: 18px;\n height: 18px;\n border-radius: 50%;\n cursor: pointer;\n border: 2px solid transparent;\n transition: border-color 0.15s;\n}\n\n.sam-prompter-container .color-swatch:hover {\n border-color: var(--color-accent, #2196F3);\n}\n\n.sam-prompter-container .color-swatch.active {\n box-shadow: inset 0 0 0 2px #ffffff, inset 0 0 0 4px rgba(0, 0, 0, 0.35);\n}\n\n.sam-prompter-container .sp-slider {\n -webkit-appearance: none;\n appearance: none;\n width: 100px;\n height: 4px;\n margin: 0;\n padding: 0;\n background: var(--border-color-primary, #d0d0d0);\n border: none;\n border-radius: 2px;\n}\n\n.sam-prompter-container .sp-slider::-webkit-slider-thumb {\n -webkit-appearance: none;\n appearance: none;\n width: 14px;\n height: 14px;\n margin-top: -5px;\n border: none;\n border-radius: 50%;\n background: var(--color-accent, #2196F3);\n cursor: pointer;\n}\n\n.sam-prompter-container .sp-slider::-webkit-slider-runnable-track {\n height: 4px;\n border-radius: 2px;\n}\n\n.sam-prompter-container .sp-slider::-moz-range-thumb {\n width: 14px;\n height: 14px;\n border: none;\n border-radius: 50%;\n background: var(--color-accent, #2196F3);\n cursor: pointer;\n}\n\n.sam-prompter-container .sp-slider::-moz-range-track {\n height: 4px;\n border-radius: 2px;\n background: var(--border-color-primary, #d0d0d0);\n}\n\n.sam-prompter-container .slider-value {\n font-size: 11px;\n font-family: monospace;\n min-width: 28px;\n}\n\n.sam-prompter-container .move-btn {\n width: 26px;\n padding: 0;\n}\n\n.sam-prompter-container .move-btn svg {\n display: block;\n}\n\n.sam-prompter-container .maximize-btn {\n width: 26px;\n padding: 0;\n}\n\n.sam-prompter-container .settings-btn {\n width: 26px;\n padding: 0;\n font-weight: bold;\n}\n\n/* Canvas */\n.sam-prompter-container .canvas-wrapper {\n position: relative;\n width: 100%;\n min-height: 300px;\n background: var(--background-fill-secondary, #f7f7f7);\n display: flex;\n align-items: center;\n justify-content: center;\n overflow: hidden;\n}\n\n.sam-prompter-container .canvas-wrapper canvas {\n display: block;\n cursor: crosshair;\n max-width: 100%;\n}\n\n/* Coord display */\n.sam-prompter-container .coord-display {\n position: absolute;\n bottom: 4px;\n right: 8px;\n font-size: 11px;\n color: var(--body-text-color-subdued, #888888);\n pointer-events: none;\n font-family: monospace;\n}\n\n/* Help overlay */\n.sam-prompter-container .help-overlay {\n position: absolute;\n top: 0;\n left: 0;\n right: 0;\n bottom: 0;\n background: rgba(0, 0, 0, 0.7);\n display: flex;\n align-items: center;\n justify-content: center;\n z-index: 10;\n}\n\n.sam-prompter-container .help-overlay.hidden {\n display: none;\n}\n\n.sam-prompter-container .help-content {\n background: var(--background-fill-primary, #ffffff);\n border-radius: 8px;\n padding: 20px 24px;\n max-width: 420px;\n width: 90%;\n max-height: 80%;\n overflow-y: auto;\n}\n\n.sam-prompter-container .help-content h3 {\n margin: 0 0 12px 0;\n font-size: 15px;\n}\n\n.sam-prompter-container .help-content table {\n width: 100%;\n border-collapse: collapse;\n}\n\n.sam-prompter-container .help-content td {\n padding: 3px 8px;\n font-size: 12px;\n}\n\n.sam-prompter-container .help-content td:first-child {\n white-space: nowrap;\n text-align: right;\n width: 40%;\n}\n\n.sam-prompter-container .help-content kbd {\n display: inline-block;\n padding: 1px 5px;\n border: 1px solid var(--border-color-primary, #ccc);\n border-radius: 3px;\n background: var(--background-fill-secondary, #f5f5f5);\n font-family: monospace;\n font-size: 11px;\n}\n\n.sam-prompter-container .help-close-btn {\n margin-top: 12px;\n width: 100%;\n}\n\n/* Processing lock (class on element wrapper, outside morph scope) */\n&.sp-processing .canvas-wrapper {\n cursor: wait;\n}\n\n&.sp-processing canvas {\n pointer-events: none;\n}\n\n/* Loading */\n.sam-prompter-container .loading-indicator {\n position: absolute;\n top: 0;\n left: 0;\n right: 0;\n bottom: 0;\n background: color-mix(in srgb, var(--background-fill-primary, #ffffff) 70%, transparent);\n display: flex;\n align-items: center;\n justify-content: center;\n z-index: 5;\n}\n\n.sam-prompter-container .loading-indicator.hidden {\n display: none;\n}\n\n@keyframes sam-prompter-spin {\n to { transform: rotate(360deg); }\n}\n\n.sam-prompter-container .spinner {\n width: 32px;\n height: 32px;\n border: 3px solid var(--border-color-primary, #e0e0e0);\n border-top-color: var(--color-accent, #2196F3);\n border-radius: 50%;\n animation: sam-prompter-spin 0.8s linear infinite;\n}\n\n/* Drop zone */\n.sam-prompter-container .drop-zone {\n position: absolute;\n top: 0;\n left: 0;\n right: 0;\n bottom: 0;\n display: flex;\n align-items: center;\n justify-content: center;\n color: var(--body-text-color-subdued, #888888);\n font-size: 14px;\n cursor: pointer;\n border: 2px dashed transparent;\n transition: border-color 0.2s, background 0.2s;\n}\n\n.sam-prompter-container .drop-zone.hidden {\n display: none;\n}\n\n.sam-prompter-container .drop-zone.drag-over {\n border-color: var(--color-accent, #2196F3);\n background: rgba(33, 150, 243, 0.05);\n}\n\n.sam-prompter-container .drop-zone .file-input {\n position: absolute;\n top: 0;\n left: 0;\n width: 100%;\n height: 100%;\n opacity: 0;\n cursor: pointer;\n}\n", "js_on_load": "(function () {\n \"use strict\";\n\n var container = element.querySelector(\".sam-prompter-container\");\n var dataScript = element.querySelector(\"script.prompt-data\");\n var canvasWrapper = container.querySelector(\".canvas-wrapper\");\n var canvas = canvasWrapper.querySelector(\"canvas\");\n var ctx = canvas.getContext(\"2d\");\n var coordDisplay = container.querySelector(\".coord-display\");\n var objectTabsEl = container.querySelector(\".object-tabs\");\n var addObjectBtn = container.querySelector(\".add-object-btn\");\n var undoBtn = container.querySelector(\".undo-btn\");\n var clearBtn = container.querySelector(\".clear-btn\");\n var clearAllBtn = container.querySelector(\".clear-all-btn\");\n var maskToggleBtn = container.querySelector(\".mask-toggle-btn\");\n var helpBtn = container.querySelector(\".help-btn\");\n var helpOverlay = container.querySelector(\".help-overlay\");\n var helpCloseBtn = container.querySelector(\".help-close-btn\");\n var dropZone = container.querySelector(\".drop-zone\");\n var fileInput = container.querySelector(\".file-input\");\n var clearImageBtn = container.querySelector(\".clear-image-btn\");\n var settingsBar = container.querySelector(\".settings-bar\");\n var settingsBtn = container.querySelector(\".settings-btn\");\n var objColorSwatches = container.querySelector(\".obj-color-swatches\");\n var pointSizeSlider = container.querySelector(\".point-size-slider\");\n var pointSizeValue = container.querySelector(\".point-size-value\");\n var maskOpacitySlider = container.querySelector(\".mask-opacity-slider\");\n var maskOpacityValue = container.querySelector(\".mask-opacity-value\");\n var boxLineWidthSlider = container.querySelector(\".box-line-width-slider\");\n var boxLineWidthValue = container.querySelector(\".box-line-width-value\");\n var imageToggleBtn = container.querySelector(\".image-toggle-btn\");\n var moveBtn = container.querySelector(\".move-btn\");\n var maximizeBtn = container.querySelector(\".maximize-btn\");\n var cutoutBtn = container.querySelector(\".cutout-btn\");\n\n var isMac = /Mac|iPhone|iPad/i.test(navigator.userAgent);\n var ALT_KEY_LABEL = isMac ? \"\u2325 Option\" : \"Alt\";\n\n // Patch help overlay to show platform-appropriate modifier key\n helpOverlay.querySelectorAll(\"kbd\").forEach(function (kbd) {\n if (kbd.textContent.indexOf(\"Alt\") !== -1) {\n kbd.textContent = kbd.textContent.replace(\"Alt\", ALT_KEY_LABEL);\n }\n });\n\n var DRAG_THRESHOLD = 5;\n var MIN_ZOOM = 1;\n var MAX_ZOOM = 20;\n var ZOOM_SENSITIVITY = 0.001;\n var VIEW_COLORS = [\n \"#00CC00\", \"#0066FF\", \"#FF0000\", \"#FFCC00\",\n \"#FF00FF\", \"#00CCCC\", \"#FF6600\", \"#9933FF\",\n \"#FFFFFF\", \"#000000\", \"#FF69B4\", \"#00FF80\"\n ];\n var maxObjects = props.max_objects;\n var pointRadius = props.point_radius;\n var maskAlpha = props.mask_alpha;\n var boxLineWidth = 2;\n\n var _renderFrameId = null;\n function requestRender() {\n if (_renderFrameId) return;\n _renderFrameId = requestAnimationFrame(function () {\n _renderFrameId = null;\n renderAll();\n });\n }\n\n // Deferred processing-flag reset: waits until the next animation\n // frame so that Gradio's Svelte re-evaluation, mask decoding, and\n // canvas rendering are all complete before accepting new clicks.\n var _processingResetFrameId = null;\n function scheduleProcessingReset() {\n if (_processingResetFrameId) cancelAnimationFrame(_processingResetFrameId);\n _processingResetFrameId = requestAnimationFrame(function () {\n _processingResetFrameId = null;\n if (state.isProcessing) {\n state.isProcessing = false;\n updateCanvasCursor();\n }\n });\n }\n\n // Initialize sliders\n pointSizeSlider.value = pointRadius;\n pointSizeValue.textContent = pointRadius;\n maskOpacitySlider.value = Math.round(maskAlpha * 100);\n maskOpacityValue.textContent = Math.round(maskAlpha * 100) + \"%\";\n boxLineWidthSlider.value = boxLineWidth;\n boxLineWidthValue.textContent = boxLineWidth + \"px\";\n\n var state = {\n image: null,\n imageUrl: null,\n naturalWidth: 0,\n naturalHeight: 0,\n objects: [createEmptyObject(0)],\n activeObjectIndex: 0,\n masks: [],\n maskCanvases: [],\n showMasks: true,\n isDrawingBox: false,\n boxStartX: 0,\n boxStartY: 0,\n boxCurrentX: 0,\n boxCurrentY: 0,\n mouseDownX: 0,\n mouseDownY: 0,\n mouseDownButton: -1,\n didDrag: false,\n isPanning: false,\n panStartX: 0,\n panStartY: 0,\n panStartPanX: 0,\n panStartPanY: 0,\n zoom: 1,\n panX: 0,\n panY: 0,\n moveMode: false,\n spaceHeld: false,\n showImage: true,\n settingsVisible: true,\n rawMasks: [],\n // Upload state\n objectUrl: null,\n filePath: null,\n pendingEmit: false,\n imageSource: null, // \"upload\" or \"python\"\n altHoverPointIndex: -1,\n altHoverBoxIndex: -1,\n maximized: false,\n cutoutMode: false,\n isProcessing: false\n };\n\n container.__samPrompterState = state;\n\n // Pre-computed 16x16 checkerboard tile (8px squares, white + light gray)\n var _checkerTile = (function () {\n var tile = document.createElement(\"canvas\");\n tile.width = 16;\n tile.height = 16;\n var tCtx = tile.getContext(\"2d\");\n tCtx.fillStyle = \"#ffffff\";\n tCtx.fillRect(0, 0, 16, 16);\n tCtx.fillStyle = \"#cccccc\";\n tCtx.fillRect(8, 0, 8, 8);\n tCtx.fillRect(0, 8, 8, 8);\n return tile;\n })();\n\n // Lazily-allocated offscreen canvas for cutout compositing\n var _cutoutCanvas = null;\n var _cutoutCtx = null;\n function getCutoutCanvas(w, h) {\n if (!_cutoutCanvas || _cutoutCanvas.width !== w || _cutoutCanvas.height !== h) {\n _cutoutCanvas = document.createElement(\"canvas\");\n _cutoutCanvas.width = w;\n _cutoutCanvas.height = h;\n _cutoutCtx = _cutoutCanvas.getContext(\"2d\");\n }\n return { canvas: _cutoutCanvas, ctx: _cutoutCtx };\n }\n\n function createEmptyObject(index) {\n return {\n points: [],\n labels: [],\n boxes: [],\n color: VIEW_COLORS[index % VIEW_COLORS.length],\n history: [],\n visible: true\n };\n }\n\n // --- Color swatches ---\n\n function renderColorSwatches() {\n // Object color swatches\n var activeColor = state.objects[state.activeObjectIndex].color;\n objColorSwatches.innerHTML = \"\";\n for (var i = 0; i < VIEW_COLORS.length; i++) {\n (function (color) {\n var btn = document.createElement(\"button\");\n btn.className = \"color-swatch\" + (color === activeColor ? \" active\" : \"\");\n btn.style.background = color;\n btn.style.borderColor = color === activeColor ? color : \"transparent\";\n btn.title = color;\n btn.addEventListener(\"click\", function () {\n state.objects[state.activeObjectIndex].color = color;\n redecodeMask(state.activeObjectIndex);\n renderToolbar();\n requestRender();\n });\n objColorSwatches.appendChild(btn);\n })(VIEW_COLORS[i]);\n }\n\n }\n\n function redecodeMask(index) {\n if (index < state.rawMasks.length && state.rawMasks[index]) {\n var raw = state.rawMasks[index];\n var color = (index < state.objects.length) ? state.objects[index].color : raw.color;\n state.maskCanvases[index] = decodeMask(raw.rle, color, 1.0);\n }\n }\n\n // --- Coordinate transforms ---\n\n function clientToNatural(clientX, clientY) {\n var rect = canvas.getBoundingClientRect();\n var displayX = clientX - rect.left;\n var displayY = clientY - rect.top;\n var scaleDisplay = canvas.width / rect.width;\n var canvasX = displayX * scaleDisplay;\n var canvasY = displayY * scaleDisplay;\n var natX = (canvasX - state.panX) / state.zoom;\n var natY = (canvasY - state.panY) / state.zoom;\n return { x: natX, y: natY };\n }\n\n function naturalToCanvas(natX, natY) {\n return {\n x: natX * state.zoom + state.panX,\n y: natY * state.zoom + state.panY\n };\n }\n\n function isInImageBounds(natX, natY) {\n return natX >= 0 && natX <= state.naturalWidth && natY >= 0 && natY <= state.naturalHeight;\n }\n\n // Ratio of canvas internal pixels to CSS display pixels.\n // Multiplying a screen-pixel size by this factor gives the\n // equivalent size in canvas (= natural image) coordinates.\n function getDisplayScale() {\n var rect = canvas.getBoundingClientRect();\n if (!rect.width) return 1;\n return canvas.width / rect.width;\n }\n\n // --- Zoom/Pan helpers ---\n\n function clampPan() {\n if (state.zoom <= 1) {\n state.panX = 0;\n state.panY = 0;\n return;\n }\n var maxPanX = 0;\n var minPanX = canvas.width - canvas.width * state.zoom;\n var maxPanY = 0;\n var minPanY = canvas.height - canvas.height * state.zoom;\n if (state.panX > maxPanX) state.panX = maxPanX;\n if (state.panX < minPanX) state.panX = minPanX;\n if (state.panY > maxPanY) state.panY = maxPanY;\n if (state.panY < minPanY) state.panY = minPanY;\n }\n\n function zoomToCenter(newZoom) {\n newZoom = Math.max(MIN_ZOOM, Math.min(MAX_ZOOM, newZoom));\n var cx = canvas.width / 2;\n var cy = canvas.height / 2;\n state.panX = cx - (cx - state.panX) * (newZoom / state.zoom);\n state.panY = cy - (cy - state.panY) * (newZoom / state.zoom);\n state.zoom = newZoom;\n clampPan();\n requestRender();\n }\n\n function resetZoom() {\n state.zoom = 1;\n state.panX = 0;\n state.panY = 0;\n requestRender();\n }\n\n // Pin opacity on ancestor elements so Gradio's \"pending\" class\n // (which sets opacity < 1) cannot create a stacking context that\n // traps the fullscreen overlay.\n var _pinnedAncestors = [];\n function pinAncestorOpacity() {\n // Remove previous pins\n for (var i = 0; i < _pinnedAncestors.length; i++) {\n _pinnedAncestors[i].style.removeProperty(\"opacity\");\n }\n _pinnedAncestors = [];\n if (!state.maximized) return;\n var node = element.parentElement;\n while (node && node !== document.body && node !== document.documentElement) {\n node.style.setProperty(\"opacity\", \"1\", \"important\");\n _pinnedAncestors.push(node);\n node = node.parentElement;\n }\n }\n\n function toggleMaximize() {\n state.maximized = !state.maximized;\n element.classList.toggle(\"sp-maximized\", state.maximized);\n document.body.style.overflow = state.maximized ? \"hidden\" : \"\";\n pinAncestorOpacity();\n if (state.image) {\n requestAnimationFrame(function () {\n resizeCanvas();\n resetZoom();\n });\n }\n }\n\n function isMoveModeActive() {\n return state.moveMode || state.spaceHeld;\n }\n\n function updateCanvasCursor() {\n element.classList.toggle(\"sp-processing\", state.isProcessing);\n if (state.isProcessing) return;\n if (isMoveModeActive()) {\n canvas.style.cursor = state.isPanning ? \"grabbing\" : (state.zoom > 1 ? \"grab\" : \"default\");\n } else {\n canvas.style.cursor = \"crosshair\";\n }\n }\n\n // --- RLE mask decode ---\n\n function decodeMask(rle, color, alpha) {\n var h = rle.size[0], w = rle.size[1];\n var offscreen = document.createElement(\"canvas\");\n offscreen.width = w;\n offscreen.height = h;\n var offCtx = offscreen.getContext(\"2d\");\n var imgData = offCtx.createImageData(w, h);\n var d = imgData.data;\n var r, g, b;\n if (Array.isArray(color)) {\n r = color[0]; g = color[1]; b = color[2];\n } else {\n r = parseInt(color.slice(1, 3), 16);\n g = parseInt(color.slice(3, 5), 16);\n b = parseInt(color.slice(5, 7), 16);\n }\n var a = Math.round((alpha !== undefined ? alpha : maskAlpha) * 255);\n var pos = 0;\n for (var i = 0; i < rle.counts.length; i++) {\n var c = rle.counts[i];\n if (i % 2 === 1) {\n for (var j = pos; j < pos + c; j++) {\n var row = j % h;\n var col = (j / h) | 0;\n var idx = (row * w + col) * 4;\n d[idx] = r;\n d[idx + 1] = g;\n d[idx + 2] = b;\n d[idx + 3] = a;\n }\n }\n pos += c;\n }\n offCtx.putImageData(imgData, 0, 0);\n return offscreen;\n }\n\n // --- Canvas sizing ---\n\n function resizeCanvas() {\n if (!state.image) return;\n var wrapperWidth = canvasWrapper.clientWidth;\n var aspect = state.naturalWidth / state.naturalHeight;\n var displayWidth = wrapperWidth;\n var displayHeight = displayWidth / aspect;\n\n // In maximized mode the wrapper has a fixed height (flex: 1).\n // Fit the canvas within both dimensions to prevent overflow.\n if (state.maximized) {\n var wrapperHeight = canvasWrapper.clientHeight;\n if (wrapperHeight > 0 && displayHeight > wrapperHeight) {\n displayHeight = wrapperHeight;\n displayWidth = displayHeight * aspect;\n }\n }\n\n // Only update internal dimensions when they actually changed;\n // setting canvas.width/height (even to the same value) clears\n // the pixel buffer which causes a visible flicker.\n if (canvas.width !== state.naturalWidth || canvas.height !== state.naturalHeight) {\n canvas.width = state.naturalWidth;\n canvas.height = state.naturalHeight;\n }\n canvas.style.width = displayWidth + \"px\";\n canvas.style.height = displayHeight + \"px\";\n canvasWrapper.style.minHeight = \"\";\n }\n\n // --- Visibility sync (survives Gradio template re-rendering) ---\n\n function syncVisibility() {\n if (state.image) {\n dropZone.classList.add(\"hidden\");\n } else {\n dropZone.classList.remove(\"hidden\");\n }\n // Restore settings bar visibility from state\n if (state.settingsVisible) {\n settingsBar.classList.remove(\"hidden\");\n settingsBtn.classList.add(\"active\");\n } else {\n settingsBar.classList.add(\"hidden\");\n settingsBtn.classList.remove(\"active\");\n }\n // Restore maximized state\n var wasMaximized = element.classList.contains(\"sp-maximized\");\n element.classList.toggle(\"sp-maximized\", state.maximized);\n document.body.style.overflow = state.maximized ? \"hidden\" : \"\";\n pinAncestorOpacity();\n // After restoring the class the wrapper dimensions change;\n // schedule a resize so the canvas fits the new layout.\n if (state.maximized && !wasMaximized && state.image) {\n requestAnimationFrame(function () {\n resizeCanvas();\n requestRender();\n });\n }\n }\n\n // --- Rendering ---\n\n function renderAll() {\n syncVisibility();\n if (!state.image) return;\n\n var ds = getDisplayScale();\n\n // Clear in screen space\n ctx.setTransform(1, 0, 0, 1, 0, 0);\n ctx.clearRect(0, 0, canvas.width, canvas.height);\n\n // Dark background visible when zoomed/panned\n if (state.zoom > 1) {\n ctx.fillStyle = \"#1a1a1a\";\n ctx.fillRect(0, 0, canvas.width, canvas.height);\n }\n\n // Apply zoom+pan transform\n ctx.save();\n ctx.setTransform(state.zoom, 0, 0, state.zoom, state.panX, state.panY);\n\n if (!state.cutoutMode) {\n // 1. Image (conditionally)\n if (state.showImage) {\n ctx.drawImage(state.image, 0, 0, state.naturalWidth, state.naturalHeight);\n } else {\n ctx.fillStyle = \"#222222\";\n ctx.fillRect(0, 0, state.naturalWidth, state.naturalHeight);\n }\n\n // 2. Masks (with dynamic opacity via globalAlpha)\n if (state.showMasks) {\n ctx.globalAlpha = maskAlpha;\n for (var m = 0; m < state.maskCanvases.length; m++) {\n if (state.maskCanvases[m] && m < state.objects.length && state.objects[m].visible) {\n ctx.drawImage(state.maskCanvases[m], 0, 0, state.naturalWidth, state.naturalHeight);\n }\n }\n ctx.globalAlpha = 1.0;\n }\n } else {\n // Cutout mode: checkerboard + foreground pixels only where masks exist\n var checkerPattern = ctx.createPattern(_checkerTile, \"repeat\");\n ctx.fillStyle = checkerPattern;\n ctx.fillRect(0, 0, state.naturalWidth, state.naturalHeight);\n\n // Composite visible masks then clip the original image\n var co = getCutoutCanvas(state.naturalWidth, state.naturalHeight);\n co.ctx.clearRect(0, 0, state.naturalWidth, state.naturalHeight);\n\n // Union all visible mask canvases (source-over)\n co.ctx.globalCompositeOperation = \"source-over\";\n for (var cm = 0; cm < state.maskCanvases.length; cm++) {\n if (state.maskCanvases[cm] && cm < state.objects.length && state.objects[cm].visible) {\n co.ctx.drawImage(state.maskCanvases[cm], 0, 0, state.naturalWidth, state.naturalHeight);\n }\n }\n\n // Keep image pixels only where mask alpha > 0\n co.ctx.globalCompositeOperation = \"source-in\";\n co.ctx.drawImage(state.image, 0, 0, state.naturalWidth, state.naturalHeight);\n co.ctx.globalCompositeOperation = \"source-over\";\n\n ctx.drawImage(co.canvas, 0, 0, state.naturalWidth, state.naturalHeight);\n }\n\n // 3. Prompts\n for (var oi = 0; oi < state.objects.length; oi++) {\n if (!state.objects[oi].visible) continue;\n drawObjectPrompts(state.objects[oi], oi === state.activeObjectIndex, ds);\n }\n\n // 4. Alt+hover delete indicator (point)\n if (state.altHoverPointIndex >= 0) {\n var aObj = state.objects[state.activeObjectIndex];\n if (aObj && state.altHoverPointIndex < aObj.points.length) {\n var apt = aObj.points[state.altHoverPointIndex];\n var hoverRadius = (pointRadius + 4) * ds / state.zoom;\n ctx.beginPath();\n ctx.arc(apt[0], apt[1], hoverRadius, 0, Math.PI * 2);\n ctx.strokeStyle = \"#FF0000\";\n ctx.lineWidth = 2 * ds / state.zoom;\n ctx.setLineDash([4 * ds / state.zoom, 3 * ds / state.zoom]);\n ctx.stroke();\n ctx.setLineDash([]);\n }\n }\n\n // 5. Alt+hover delete indicator (box)\n if (state.altHoverBoxIndex >= 0) {\n var bObj = state.objects[state.activeObjectIndex];\n if (bObj && state.altHoverBoxIndex < bObj.boxes.length) {\n var hBox = bObj.boxes[state.altHoverBoxIndex];\n ctx.strokeStyle = \"#FF0000\";\n ctx.lineWidth = 2 * ds / state.zoom;\n ctx.setLineDash([4 * ds / state.zoom, 3 * ds / state.zoom]);\n ctx.strokeRect(hBox[0], hBox[1], hBox[2] - hBox[0], hBox[3] - hBox[1]);\n ctx.setLineDash([]);\n }\n }\n\n ctx.restore();\n\n // 6. Rubber-band box (in canvas pixel space, not zoomed)\n if (state.isDrawingBox) {\n drawRubberBand(ds);\n }\n }\n\n function contrastStroke(hexColor) {\n var r = parseInt(hexColor.slice(1, 3), 16);\n var g = parseInt(hexColor.slice(3, 5), 16);\n var b = parseInt(hexColor.slice(5, 7), 16);\n // Perceived luminance\n var lum = (0.299 * r + 0.587 * g + 0.114 * b) / 255;\n return lum > 0.7 ? \"#000000\" : \"#ffffff\";\n }\n\n function drawObjectPrompts(obj, isActive, ds) {\n var color = obj.color;\n var stroke = contrastStroke(color);\n var baseRadius = pointRadius * ds / state.zoom;\n\n // Dim inactive objects so the active one stands out\n if (!isActive) {\n ctx.globalAlpha = 0.35;\n }\n\n // Draw boxes\n for (var b = 0; b < obj.boxes.length; b++) {\n var box = obj.boxes[b];\n ctx.strokeStyle = color;\n ctx.lineWidth = boxLineWidth * ds / state.zoom;\n ctx.setLineDash([6 * ds / state.zoom, 4 * ds / state.zoom]);\n ctx.strokeRect(box[0], box[1], box[2] - box[0], box[3] - box[1]);\n ctx.setLineDash([]);\n }\n\n // Draw points\n for (var p = 0; p < obj.points.length; p++) {\n var pt = obj.points[p];\n var label = obj.labels[p];\n var px = pt[0];\n var py = pt[1];\n\n if (label === 1) {\n // Foreground: colored circle with contrast outline\n ctx.beginPath();\n ctx.arc(px, py, baseRadius, 0, Math.PI * 2);\n ctx.fillStyle = color;\n ctx.fill();\n ctx.strokeStyle = stroke;\n ctx.lineWidth = 1.5 * ds / state.zoom;\n ctx.stroke();\n } else {\n // Background: object-colored circle with X mark\n ctx.beginPath();\n ctx.arc(px, py, baseRadius, 0, Math.PI * 2);\n ctx.fillStyle = color;\n ctx.fill();\n ctx.strokeStyle = stroke;\n ctx.lineWidth = 1.5 * ds / state.zoom;\n ctx.stroke();\n\n // X mark\n var xSize = baseRadius * 0.8;\n ctx.beginPath();\n ctx.moveTo(px - xSize, py - xSize);\n ctx.lineTo(px + xSize, py + xSize);\n ctx.moveTo(px + xSize, py - xSize);\n ctx.lineTo(px - xSize, py + xSize);\n ctx.strokeStyle = stroke;\n ctx.lineWidth = 2 * ds / state.zoom;\n ctx.stroke();\n }\n }\n\n // Restore full opacity after drawing inactive object\n if (!isActive) {\n ctx.globalAlpha = 1.0;\n }\n }\n\n function drawRubberBand(ds) {\n var p1 = naturalToCanvas(state.boxStartX, state.boxStartY);\n var p2 = naturalToCanvas(state.boxCurrentX, state.boxCurrentY);\n var color = state.objects[state.activeObjectIndex].color;\n ctx.save();\n ctx.strokeStyle = color;\n ctx.lineWidth = boxLineWidth * ds;\n ctx.setLineDash([6 * ds, 4 * ds]);\n // Parse hex to rgba for fill\n var r = parseInt(color.slice(1, 3), 16);\n var g = parseInt(color.slice(3, 5), 16);\n var b = parseInt(color.slice(5, 7), 16);\n ctx.fillStyle = \"rgba(\" + r + \",\" + g + \",\" + b + \",0.1)\";\n var x = Math.min(p1.x, p2.x);\n var y = Math.min(p1.y, p2.y);\n var w = Math.abs(p2.x - p1.x);\n var h = Math.abs(p2.y - p1.y);\n ctx.fillRect(x, y, w, h);\n ctx.strokeRect(x, y, w, h);\n ctx.setLineDash([]);\n ctx.restore();\n }\n\n function renderToolbar() {\n var html = \"\";\n for (var i = 0; i < state.objects.length; i++) {\n var obj = state.objects[i];\n var activeClass = i === state.activeObjectIndex ? \" active\" : \"\";\n var hiddenClass = !obj.visible ? \" hidden-object\" : \"\";\n var promptCount = obj.points.length + obj.boxes.length;\n var tabStyle = activeClass ? ' style=\"border-left: 3px solid ' + obj.color + ';\"' : \"\";\n html += '\";\n }\n objectTabsEl.innerHTML = html;\n\n // Bind tab clicks\n var tabs = objectTabsEl.querySelectorAll(\".object-tab\");\n for (var t = 0; t < tabs.length; t++) {\n (function (tab) {\n tab.addEventListener(\"click\", function (e) {\n if (e.target.classList.contains(\"delete-tab\") || e.target.getAttribute(\"data-delete\") !== null) {\n return;\n }\n if (e.target.classList.contains(\"visibility-toggle\") || e.target.getAttribute(\"data-vis\") !== null) {\n return;\n }\n var idx = parseInt(tab.getAttribute(\"data-index\"), 10);\n state.activeObjectIndex = idx;\n renderToolbar();\n requestRender();\n });\n })(tabs[t]);\n }\n\n // Bind visibility toggle clicks\n var visBtns = objectTabsEl.querySelectorAll(\".visibility-toggle\");\n for (var vi = 0; vi < visBtns.length; vi++) {\n (function (btn) {\n btn.addEventListener(\"click\", function (e) {\n e.stopPropagation();\n var idx = parseInt(btn.getAttribute(\"data-vis\"), 10);\n state.objects[idx].visible = !state.objects[idx].visible;\n renderToolbar();\n requestRender();\n });\n })(visBtns[vi]);\n }\n\n // Bind delete clicks\n var deleteBtns = objectTabsEl.querySelectorAll(\".delete-tab\");\n for (var d = 0; d < deleteBtns.length; d++) {\n (function (btn) {\n btn.addEventListener(\"click\", function (e) {\n e.stopPropagation();\n var idx = parseInt(btn.getAttribute(\"data-delete\"), 10);\n deleteObject(idx);\n });\n })(deleteBtns[d]);\n }\n\n // Update add button state\n addObjectBtn.disabled = state.objects.length >= maxObjects;\n\n // Update undo/clear button states\n var activeObj = state.objects[state.activeObjectIndex];\n undoBtn.disabled = !activeObj || activeObj.history.length === 0;\n clearBtn.disabled = !activeObj || (activeObj.points.length === 0 && activeObj.boxes.length === 0);\n\n var hasAnyPrompts = state.objects.some(function (o) {\n return o.points.length > 0 || o.boxes.length > 0;\n });\n clearAllBtn.disabled = !hasAnyPrompts;\n\n // Mask toggle\n if (state.showMasks) {\n maskToggleBtn.classList.add(\"active\");\n } else {\n maskToggleBtn.classList.remove(\"active\");\n }\n\n // Move mode\n if (isMoveModeActive()) {\n moveBtn.classList.add(\"active\");\n } else {\n moveBtn.classList.remove(\"active\");\n }\n\n // Clear image button\n clearImageBtn.disabled = !state.image;\n\n // Image toggle\n if (state.showImage) {\n imageToggleBtn.classList.add(\"active\");\n } else {\n imageToggleBtn.classList.remove(\"active\");\n }\n\n // Cutout mode\n if (state.cutoutMode) {\n cutoutBtn.classList.add(\"active\");\n } else {\n cutoutBtn.classList.remove(\"active\");\n }\n\n // Restore slider value labels (Svelte template re-evaluation clears textContent)\n pointSizeValue.textContent = pointRadius;\n maskOpacityValue.textContent = Math.round(maskAlpha * 100) + \"%\";\n boxLineWidthValue.textContent = boxLineWidth + \"px\";\n\n // Color swatches\n renderColorSwatches();\n }\n\n // --- Object management ---\n\n function addObject() {\n if (state.isProcessing) return;\n if (state.objects.length >= maxObjects) return;\n var newObj = createEmptyObject(state.objects.length);\n state.objects.push(newObj);\n state.activeObjectIndex = state.objects.length - 1;\n renderToolbar();\n requestRender();\n }\n\n function deleteObject(index) {\n if (state.isProcessing) return;\n if (state.objects.length <= 1) {\n clearActiveObject();\n return;\n }\n state.objects.splice(index, 1);\n if (state.activeObjectIndex >= state.objects.length) {\n state.activeObjectIndex = state.objects.length - 1;\n } else if (state.activeObjectIndex > index) {\n state.activeObjectIndex--;\n }\n // Reassign colors and re-decode affected masks\n for (var i = 0; i < state.objects.length; i++) {\n state.objects[i].color = VIEW_COLORS[i % VIEW_COLORS.length];\n redecodeMask(i);\n }\n renderToolbar();\n requestRender();\n emitPromptData();\n }\n\n function clearActiveObject() {\n if (state.isProcessing) return;\n var obj = state.objects[state.activeObjectIndex];\n if (obj.points.length === 0 && obj.boxes.length === 0) return;\n obj.history.push({\n type: \"clear\",\n points: obj.points.slice(),\n labels: obj.labels.slice(),\n boxes: obj.boxes.map(function (b) { return b.slice(); })\n });\n obj.points = [];\n obj.labels = [];\n obj.boxes = [];\n // Clear stale mask so it doesn't render with wrong color\n if (state.activeObjectIndex < state.maskCanvases.length) {\n state.maskCanvases[state.activeObjectIndex] = null;\n }\n if (state.activeObjectIndex < state.rawMasks.length) {\n state.rawMasks[state.activeObjectIndex] = null;\n }\n renderToolbar();\n requestRender();\n emitPromptData();\n }\n\n function clearAll() {\n if (state.isProcessing) return;\n state.objects = [createEmptyObject(0)];\n state.activeObjectIndex = 0;\n state.rawMasks = [];\n state.maskCanvases = [];\n renderToolbar();\n requestRender();\n emitPromptData();\n }\n\n // --- Prompt operations ---\n\n function addPoint(natX, natY, label) {\n if (state.isProcessing) return;\n var obj = state.objects[state.activeObjectIndex];\n obj.history.push({ type: \"point\" });\n obj.points.push([Math.round(natX), Math.round(natY)]);\n obj.labels.push(label);\n renderToolbar();\n requestRender();\n emitPromptData();\n }\n\n function addBox(x1, y1, x2, y2) {\n if (state.isProcessing) return;\n var bx1 = Math.round(Math.min(x1, x2));\n var by1 = Math.round(Math.min(y1, y2));\n var bx2 = Math.round(Math.max(x1, x2));\n var by2 = Math.round(Math.max(y1, y2));\n // Ignore tiny boxes\n if (Math.abs(bx2 - bx1) < 3 && Math.abs(by2 - by1) < 3) return;\n var obj = state.objects[state.activeObjectIndex];\n obj.history.push({ type: \"box\" });\n obj.boxes.push([bx1, by1, bx2, by2]);\n renderToolbar();\n requestRender();\n emitPromptData();\n }\n\n function undoLastPrompt() {\n if (state.isProcessing) return;\n var obj = state.objects[state.activeObjectIndex];\n if (obj.history.length === 0) return;\n var last = obj.history.pop();\n if (last.type === \"point\") {\n obj.points.pop();\n obj.labels.pop();\n } else if (last.type === \"box\") {\n obj.boxes.pop();\n } else if (last.type === \"delete-point\") {\n obj.points.splice(last.index, 0, last.point);\n obj.labels.splice(last.index, 0, last.label);\n } else if (last.type === \"delete-box\") {\n obj.boxes.splice(last.index, 0, last.box);\n } else if (last.type === \"clear\") {\n obj.points = last.points;\n obj.labels = last.labels;\n obj.boxes = last.boxes;\n }\n renderToolbar();\n requestRender();\n emitPromptData();\n }\n\n function findNearestPoint(natX, natY) {\n var obj = state.objects[state.activeObjectIndex];\n var ds = getDisplayScale();\n var hitRadius = pointRadius * 2 * ds / state.zoom;\n var bestDist = Infinity;\n var bestIdx = -1;\n for (var i = 0; i < obj.points.length; i++) {\n var dx = obj.points[i][0] - natX;\n var dy = obj.points[i][1] - natY;\n var dist = Math.sqrt(dx * dx + dy * dy);\n if (dist < hitRadius && dist < bestDist) {\n bestDist = dist;\n bestIdx = i;\n }\n }\n return bestIdx;\n }\n\n function deletePointAt(index) {\n if (state.isProcessing) return;\n var obj = state.objects[state.activeObjectIndex];\n if (index < 0 || index >= obj.points.length) return;\n var point = obj.points[index].slice();\n var label = obj.labels[index];\n obj.history.push({ type: \"delete-point\", index: index, point: point, label: label });\n obj.points.splice(index, 1);\n obj.labels.splice(index, 1);\n renderToolbar();\n requestRender();\n emitPromptData();\n }\n\n function distToSegment(px, py, ax, ay, bx, by) {\n var dx = bx - ax, dy = by - ay;\n var lenSq = dx * dx + dy * dy;\n if (lenSq === 0) return Math.sqrt((px - ax) * (px - ax) + (py - ay) * (py - ay));\n var t = ((px - ax) * dx + (py - ay) * dy) / lenSq;\n t = Math.max(0, Math.min(1, t));\n var projX = ax + t * dx, projY = ay + t * dy;\n return Math.sqrt((px - projX) * (px - projX) + (py - projY) * (py - projY));\n }\n\n function findNearestBox(natX, natY) {\n var obj = state.objects[state.activeObjectIndex];\n var ds = getDisplayScale();\n var hitRadius = boxLineWidth * 3 * ds / state.zoom;\n var bestDist = Infinity;\n var bestIdx = -1;\n for (var i = 0; i < obj.boxes.length; i++) {\n var b = obj.boxes[i];\n var x1 = b[0], y1 = b[1], x2 = b[2], y2 = b[3];\n // Distance to each of the four edges\n var d = Math.min(\n distToSegment(natX, natY, x1, y1, x2, y1), // top\n distToSegment(natX, natY, x2, y1, x2, y2), // right\n distToSegment(natX, natY, x2, y2, x1, y2), // bottom\n distToSegment(natX, natY, x1, y2, x1, y1) // left\n );\n if (d < hitRadius && d < bestDist) {\n bestDist = d;\n bestIdx = i;\n }\n }\n return { index: bestIdx, dist: bestDist };\n }\n\n function deleteBoxAt(index) {\n if (state.isProcessing) return;\n var obj = state.objects[state.activeObjectIndex];\n if (index < 0 || index >= obj.boxes.length) return;\n var box = obj.boxes[index].slice();\n obj.history.push({ type: \"delete-box\", index: index, box: box });\n obj.boxes.splice(index, 1);\n renderToolbar();\n requestRender();\n emitPromptData();\n }\n\n // --- JS \u2192 Python communication ---\n\n function hasAnyPrompts() {\n return state.objects.some(function (obj) {\n return obj.points.length > 0 || obj.boxes.length > 0;\n });\n }\n\n function emitPromptData() {\n // Skip backend call when no object has actual prompts (points/boxes)\n if (!hasAnyPrompts()) return;\n // Defer if user uploaded a file but server path not yet available\n if (state.imageSource === \"upload\" && state.image && !state.filePath) {\n state.pendingEmit = true;\n return;\n }\n var prompts = state.objects.map(function (obj) {\n return {\n points: obj.points.slice(),\n labels: obj.labels.slice(),\n boxes: obj.boxes.map(function (b) { return b.slice(); })\n };\n });\n var payload = { prompts: prompts };\n if (state.imageSource === \"upload\" && state.filePath) {\n payload.imagePath = state.filePath;\n payload.imageSize = { width: state.naturalWidth, height: state.naturalHeight };\n }\n if (state.imageSource === \"python\" && state.imageUrl) {\n var prefix = \"/gradio_api/file=\";\n if (state.imageUrl.startsWith(prefix)) {\n payload.imagePath = state.imageUrl.slice(prefix.length);\n }\n payload.imageSize = { width: state.naturalWidth, height: state.naturalHeight };\n }\n props.value = JSON.stringify(payload);\n trigger(\"input\");\n state.isProcessing = true;\n updateCanvasCursor();\n }\n\n // --- MutationObserver (Python \u2192 JS) ---\n\n function handleDataUpdate() {\n var raw = dataScript.textContent.trim();\n if (!raw || raw === \"null\") {\n // Only reset if image was from Python (not user upload)\n if (state.imageSource !== \"upload\") {\n state.image = null;\n state.imageUrl = null;\n state.rawMasks = [];\n state.maskCanvases = [];\n }\n // Restore dynamically-populated toolbar elements (swatches,\n // object tabs) that Gradio's Svelte template re-evaluation\n // clears when it resets ${value} after the Python handler\n // output is applied.\n scheduleProcessingReset();\n if (state.image) {\n resizeCanvas();\n renderToolbar();\n }\n requestRender();\n return;\n }\n\n var data;\n try {\n data = JSON.parse(raw);\n } catch (e) {\n return;\n }\n\n // Schedule deferred processing reset only for actual Python\n // responses (which contain \"masks\", \"image\", or \"clearPrompts\" keys).\n // The Svelte echo of props.value (which only has \"prompts\") must\n // NOT reset the flag, otherwise isProcessing would be cleared\n // immediately via microtask before Python even responds.\n if (state.isProcessing && (\"masks\" in data || \"image\" in data || \"clearPrompts\" in data)) {\n scheduleProcessingReset();\n }\n\n // Update maxObjects limit if Python specified it\n if (typeof data.maxObjects === \"number\" && data.maxObjects > 0) {\n maxObjects = data.maxObjects;\n // Trim objects if exceeding new limit\n while (state.objects.length > maxObjects) {\n state.objects.pop();\n }\n if (state.activeObjectIndex >= state.objects.length) {\n state.activeObjectIndex = Math.max(0, state.objects.length - 1);\n }\n }\n\n // Python requested clearing all user-drawn prompts (points & boxes)\n if (data.clearPrompts) {\n state.objects = [createEmptyObject(0)];\n state.activeObjectIndex = 0;\n // If this is a clear-only payload (no image), just re-render\n if (!(\"image\" in data)) {\n state.rawMasks = [];\n state.maskCanvases = [];\n if (state.image) {\n resizeCanvas();\n }\n renderToolbar();\n requestRender();\n return;\n }\n }\n\n // Decode masks (store raw for re-decoding, decode at alpha=1.0 for globalAlpha control)\n if (data.masks && data.masks.length > 0) {\n var numMasks = data.masks.length;\n // Ensure enough objects exist to hold all server-sent masks\n // (e.g. initial value with multiple masks on a fresh component).\n while (state.objects.length < numMasks) {\n state.objects.push(createEmptyObject(state.objects.length));\n }\n var numObjects = state.objects.length;\n\n // Initialize sparse arrays (one slot per object, null = no mask)\n var newRaw = [];\n var newCanvases = [];\n for (var si = 0; si < numObjects; si++) {\n newRaw.push(null);\n newCanvases.push(null);\n }\n\n if (numMasks >= numObjects) {\n // 1:1 (or more) mapping \u2014 direct index\n for (var di = 0; di < numObjects; di++) {\n newRaw[di] = data.masks[di];\n newCanvases[di] = decodeMask(data.masks[di].rle, state.objects[di].color, 1.0);\n }\n } else {\n // Fewer masks than objects \u2014 backend likely skipped empty\n // prompts. Map returned masks to objects that have prompts.\n var promptedIndices = [];\n for (var pi = 0; pi < numObjects; pi++) {\n var pObj = state.objects[pi];\n if (pObj.points.length > 0 || pObj.boxes.length > 0) {\n promptedIndices.push(pi);\n }\n }\n\n if (numMasks === promptedIndices.length) {\n // Perfect match \u2014 assign each mask to its prompted object\n for (var mi = 0; mi < numMasks; mi++) {\n var idx = promptedIndices[mi];\n newRaw[idx] = data.masks[mi];\n newCanvases[idx] = decodeMask(data.masks[mi].rle, state.objects[idx].color, 1.0);\n }\n } else {\n // Fallback: direct index mapping (original behaviour)\n for (var fi = 0; fi < numMasks && fi < numObjects; fi++) {\n newRaw[fi] = data.masks[fi];\n newCanvases[fi] = decodeMask(data.masks[fi].rle, state.objects[fi].color, 1.0);\n }\n }\n }\n\n state.rawMasks = newRaw;\n state.maskCanvases = newCanvases;\n } else if (\"masks\" in data) {\n // Python explicitly returned empty masks \u2014 clear.\n // Skip when the key is absent (JS echo in Svelte Phase 1).\n state.rawMasks = [];\n state.maskCanvases = [];\n }\n\n // If the user uploaded an image, keep displaying it (blob URL)\n // and only update masks \u2014 do NOT reload from the Python cache URL.\n // Re-render the toolbar because Gradio's template re-evaluation\n // resets dynamically-populated DOM elements (swatches, object tabs).\n if (state.imageSource === \"upload\" && state.image) {\n resizeCanvas();\n renderToolbar();\n requestRender();\n return;\n }\n\n // Load image if URL changed (Python-provided image)\n if (data.image && data.image !== state.imageUrl) {\n // Detect mask-only updates for the same underlying image: lossy\n // re-encoding produces a different cache URL each time, so we\n // check whether the response is really just new masks rather\n // than a genuinely new image.\n var isMaskUpdate = state.imageSource === \"python\" &&\n state.image &&\n data.masks && data.masks.length > 0 &&\n (data.width || 0) === state.naturalWidth &&\n (data.height || 0) === state.naturalHeight;\n\n state.imageUrl = data.image;\n state.imageSource = \"python\";\n // Clean up previous blob URL if any\n if (state.objectUrl) {\n URL.revokeObjectURL(state.objectUrl);\n state.objectUrl = null;\n }\n state.filePath = null;\n var img = new Image();\n img.crossOrigin = \"anonymous\";\n img.onload = function () {\n state.image = img;\n state.naturalWidth = data.width || img.naturalWidth;\n state.naturalHeight = data.height || img.naturalHeight;\n if (!isMaskUpdate) {\n var initMasks = (data.masks && data.masks.length) || 0;\n if (initMasks > 1) {\n state.objects = [];\n for (var oi = 0; oi < initMasks; oi++) {\n state.objects.push(createEmptyObject(oi));\n }\n } else {\n state.objects = [createEmptyObject(0)];\n }\n state.activeObjectIndex = 0;\n state.zoom = 1;\n state.panX = 0;\n state.panY = 0;\n }\n resizeCanvas();\n renderToolbar();\n requestRender();\n };\n img.src = data.image;\n } else if (data.image === state.imageUrl && state.image) {\n // Same image, just masks updated \u2014 re-render toolbar to restore\n // dynamically-populated elements after Gradio template re-evaluation.\n resizeCanvas();\n renderToolbar();\n requestRender();\n }\n }\n\n var observer = new MutationObserver(function () {\n handleDataUpdate();\n // Re-pin after every Gradio round-trip in case the \"pending\"\n // class was re-applied to an ancestor during the update.\n pinAncestorOpacity();\n // In maximized mode, Gradio's morph may alter layout before\n // resizeCanvas() runs above. Schedule a deferred resize so\n // the canvas fits the settled wrapper dimensions.\n if (state.maximized && state.image) {\n requestAnimationFrame(function () {\n resizeCanvas();\n requestRender();\n });\n }\n });\n observer.observe(dataScript, { childList: true, characterData: true, subtree: true });\n\n // Gradio may reset DOM attributes when re-rendering the template.\n // Re-apply drop-zone visibility if Gradio resets the class.\n var domObserver = new MutationObserver(function () {\n var shouldBeHidden = !!state.image;\n var isHidden = dropZone.classList.contains(\"hidden\");\n if (shouldBeHidden !== isHidden) {\n syncVisibility();\n }\n });\n domObserver.observe(dropZone, { attributes: true, attributeFilter: [\"class\"] });\n\n // Similarly, restore settings-bar visibility if Gradio resets the class.\n var settingsObserver = new MutationObserver(function () {\n var shouldBeVisible = state.settingsVisible;\n var isHidden = settingsBar.classList.contains(\"hidden\");\n if (shouldBeVisible === isHidden) {\n syncVisibility();\n }\n });\n settingsObserver.observe(settingsBar, { attributes: true, attributeFilter: [\"class\"] });\n\n // --- Mouse events ---\n\n canvas.addEventListener(\"mousedown\", function (e) {\n if (!state.image || state.isProcessing) return;\n\n // Middle button \u2192 always pan\n if (e.button === 1) {\n e.preventDefault();\n state.isPanning = true;\n state.didDrag = false;\n state.panStartX = e.clientX;\n state.panStartY = e.clientY;\n state.panStartPanX = state.panX;\n state.panStartPanY = state.panY;\n canvas.style.cursor = \"grabbing\";\n return;\n }\n\n // Left button in move mode \u2192 pan\n if (e.button === 0 && isMoveModeActive()) {\n state.isPanning = true;\n state.didDrag = false;\n state.panStartX = e.clientX;\n state.panStartY = e.clientY;\n state.panStartPanX = state.panX;\n state.panStartPanY = state.panY;\n if (state.zoom > 1) {\n canvas.style.cursor = \"grabbing\";\n }\n return;\n }\n\n // Normal mode: record mousedown for point/box\n state.mouseDownX = e.clientX;\n state.mouseDownY = e.clientY;\n state.mouseDownButton = e.button;\n state.didDrag = false;\n\n if (e.button === 0) {\n var nat = clientToNatural(e.clientX, e.clientY);\n if (!isInImageBounds(nat.x, nat.y)) return;\n state.boxStartX = nat.x;\n state.boxStartY = nat.y;\n state.boxCurrentX = nat.x;\n state.boxCurrentY = nat.y;\n }\n });\n\n // Window-level mousemove for panning and box drawing outside canvas\n window.addEventListener(\"mousemove\", function (e) {\n // Handle panning\n if (state.isPanning) {\n var dx = e.clientX - state.panStartX;\n var dy = e.clientY - state.panStartY;\n\n if (!state.didDrag && (Math.abs(dx) > DRAG_THRESHOLD || Math.abs(dy) > DRAG_THRESHOLD)) {\n state.didDrag = true;\n }\n\n if (state.didDrag && state.zoom > 1) {\n var rect = canvas.getBoundingClientRect();\n var cssToCanvasX = canvas.width / rect.width;\n var cssToCanvasY = canvas.height / rect.height;\n state.panX = state.panStartPanX + dx * cssToCanvasX;\n state.panY = state.panStartPanY + dy * cssToCanvasY;\n clampPan();\n requestRender();\n }\n return;\n }\n\n // Handle box drawing (works both inside and outside canvas)\n if (state.mouseDownButton === 0 && !isMoveModeActive()) {\n var dragDist = Math.sqrt(\n Math.pow(e.clientX - state.mouseDownX, 2) +\n Math.pow(e.clientY - state.mouseDownY, 2)\n );\n if (dragDist > DRAG_THRESHOLD) {\n state.didDrag = true;\n state.isDrawingBox = true;\n }\n if (state.isDrawingBox) {\n var natCur = clientToNatural(e.clientX, e.clientY);\n state.boxCurrentX = Math.max(0, Math.min(natCur.x, state.naturalWidth));\n state.boxCurrentY = Math.max(0, Math.min(natCur.y, state.naturalHeight));\n requestRender();\n }\n }\n });\n\n // Window-level mouseup for panning and box drawing\n window.addEventListener(\"mouseup\", function (e) {\n if (state.isPanning && (e.button === 0 || e.button === 1)) {\n state.isPanning = false;\n updateCanvasCursor();\n }\n\n // Finalize box drawing if mouse released outside canvas\n if (e.button === 0 && state.isDrawingBox && state.didDrag) {\n addBox(state.boxStartX, state.boxStartY, state.boxCurrentX, state.boxCurrentY);\n }\n\n // Clean up interaction state (no-op if canvas handler already reset)\n if (e.button === state.mouseDownButton) {\n state.isDrawingBox = false;\n state.didDrag = false;\n state.mouseDownButton = -1;\n }\n });\n\n // Canvas-local mousemove for coord display, alt-hover, and box drawing\n canvas.addEventListener(\"mousemove\", function (e) {\n if (!state.image) return;\n if (state.isPanning) return;\n\n // Update coord display\n var nat = clientToNatural(e.clientX, e.clientY);\n if (isInImageBounds(nat.x, nat.y)) {\n coordDisplay.textContent = Math.round(nat.x) + \", \" + Math.round(nat.y);\n } else {\n coordDisplay.textContent = \"\";\n }\n\n // Move mode: no alt-hover or box drawing\n if (isMoveModeActive()) return;\n\n // Alt+hover: highlight nearest point or box for deletion\n if (e.altKey && !state.isDrawingBox) {\n var hoverPtIdx = -1;\n var hoverBoxIdx = -1;\n if (isInImageBounds(nat.x, nat.y)) {\n var ptIdx = findNearestPoint(nat.x, nat.y);\n var ptDist = Infinity;\n if (ptIdx >= 0) {\n var hp = state.objects[state.activeObjectIndex].points[ptIdx];\n ptDist = Math.sqrt((hp[0] - nat.x) * (hp[0] - nat.x) + (hp[1] - nat.y) * (hp[1] - nat.y));\n }\n var boxResult = findNearestBox(nat.x, nat.y);\n if (ptIdx >= 0 && boxResult.index >= 0) {\n if (ptDist <= boxResult.dist) {\n hoverPtIdx = ptIdx;\n } else {\n hoverBoxIdx = boxResult.index;\n }\n } else if (ptIdx >= 0) {\n hoverPtIdx = ptIdx;\n } else if (boxResult.index >= 0) {\n hoverBoxIdx = boxResult.index;\n }\n }\n var changed = false;\n if (hoverPtIdx !== state.altHoverPointIndex) {\n state.altHoverPointIndex = hoverPtIdx;\n changed = true;\n }\n if (hoverBoxIdx !== state.altHoverBoxIndex) {\n state.altHoverBoxIndex = hoverBoxIdx;\n changed = true;\n }\n if (changed) requestRender();\n canvas.style.cursor = (hoverPtIdx >= 0 || hoverBoxIdx >= 0) ? \"not-allowed\" : \"crosshair\";\n } else if (state.altHoverPointIndex >= 0 || state.altHoverBoxIndex >= 0) {\n state.altHoverPointIndex = -1;\n state.altHoverBoxIndex = -1;\n canvas.style.cursor = \"crosshair\";\n requestRender();\n }\n\n // Box drawing mousemove is handled by window-level handler\n });\n\n canvas.addEventListener(\"mouseup\", function (e) {\n if (!state.image) return;\n\n // Pan ended via window handler; skip prompt actions in move mode\n if (isMoveModeActive()) return;\n\n // Alt+click \u2192 delete nearest point or box\n if (e.altKey && (e.button === 0 || e.button === 2) && !state.didDrag) {\n var natAlt = clientToNatural(e.clientX, e.clientY);\n if (isInImageBounds(natAlt.x, natAlt.y)) {\n var delPtIdx = findNearestPoint(natAlt.x, natAlt.y);\n var delPtDist = Infinity;\n if (delPtIdx >= 0) {\n var dp = state.objects[state.activeObjectIndex].points[delPtIdx];\n delPtDist = Math.sqrt((dp[0] - natAlt.x) * (dp[0] - natAlt.x) + (dp[1] - natAlt.y) * (dp[1] - natAlt.y));\n }\n var delBoxResult = findNearestBox(natAlt.x, natAlt.y);\n if (delPtIdx >= 0 && delBoxResult.index >= 0) {\n if (delPtDist <= delBoxResult.dist) {\n deletePointAt(delPtIdx);\n } else {\n deleteBoxAt(delBoxResult.index);\n }\n } else if (delPtIdx >= 0) {\n deletePointAt(delPtIdx);\n } else if (delBoxResult.index >= 0) {\n deleteBoxAt(delBoxResult.index);\n }\n state.altHoverPointIndex = -1;\n state.altHoverBoxIndex = -1;\n }\n state.isDrawingBox = false;\n state.didDrag = false;\n state.mouseDownButton = -1;\n return;\n }\n\n if (e.button === 0) {\n if (state.isDrawingBox && state.didDrag) {\n // Finalize box\n addBox(state.boxStartX, state.boxStartY, state.boxCurrentX, state.boxCurrentY);\n } else if (!state.didDrag) {\n // Left click \u2192 fg point\n var nat = clientToNatural(e.clientX, e.clientY);\n if (isInImageBounds(nat.x, nat.y)) {\n addPoint(nat.x, nat.y, 1);\n }\n }\n state.isDrawingBox = false;\n state.didDrag = false;\n state.mouseDownButton = -1;\n } else if (e.button === 2) {\n // Right click \u2192 bg point\n if (!state.didDrag) {\n var natR = clientToNatural(e.clientX, e.clientY);\n if (isInImageBounds(natR.x, natR.y)) {\n addPoint(natR.x, natR.y, 0);\n }\n }\n state.mouseDownButton = -1;\n }\n });\n\n canvas.addEventListener(\"mouseleave\", function () {\n coordDisplay.textContent = \"\";\n if (state.altHoverPointIndex >= 0 || state.altHoverBoxIndex >= 0) {\n state.altHoverPointIndex = -1;\n state.altHoverBoxIndex = -1;\n requestRender();\n }\n // Don't cancel pan or box drawing \u2014 window handlers will finish them\n });\n\n canvas.addEventListener(\"contextmenu\", function (e) {\n e.preventDefault();\n });\n\n // Double-click to reset zoom (only in move mode)\n canvas.addEventListener(\"dblclick\", function (e) {\n if (!state.image) return;\n if (!isMoveModeActive()) return;\n e.preventDefault();\n resetZoom();\n updateCanvasCursor();\n });\n\n document.addEventListener(\"keyup\", function (e) {\n if (e.key === \" \") {\n state.spaceHeld = false;\n updateCanvasCursor();\n renderToolbar();\n }\n if (e.key === \"Alt\" && (state.altHoverPointIndex >= 0 || state.altHoverBoxIndex >= 0)) {\n state.altHoverPointIndex = -1;\n state.altHoverBoxIndex = -1;\n updateCanvasCursor();\n requestRender();\n }\n });\n\n // --- Zoom ---\n\n canvas.addEventListener(\"wheel\", function (e) {\n if (!state.image) return;\n e.preventDefault();\n\n var delta = e.deltaY;\n if (e.deltaMode === 1) delta *= 16;\n else if (e.deltaMode === 2) delta *= 100;\n\n var newZoom = state.zoom * (1 - delta * ZOOM_SENSITIVITY);\n newZoom = Math.max(MIN_ZOOM, Math.min(MAX_ZOOM, newZoom));\n\n // Zoom toward cursor position\n var rect = canvas.getBoundingClientRect();\n var mx = (e.clientX - rect.left) * (canvas.width / rect.width);\n var my = (e.clientY - rect.top) * (canvas.height / rect.height);\n\n state.panX = mx - (mx - state.panX) * (newZoom / state.zoom);\n state.panY = my - (my - state.panY) * (newZoom / state.zoom);\n state.zoom = newZoom;\n clampPan();\n updateCanvasCursor();\n requestRender();\n }, { passive: false });\n\n // --- Keyboard shortcuts ---\n\n // Track hover state so shortcuts work without requiring element focus.\n // Listening on document level avoids the Gradio focus-management issue\n // where container.focus() is ineffective after canvas clicks.\n var _mouseOverContainer = false;\n container.addEventListener(\"mouseenter\", function () { _mouseOverContainer = true; });\n container.addEventListener(\"mouseleave\", function () { _mouseOverContainer = false; });\n\n document.addEventListener(\"keydown\", function (e) {\n // Only handle when mouse is over this container \u2014 but always\n // allow Escape while drawing a box (mouse may be outside canvas).\n if (!_mouseOverContainer && !(e.key === \"Escape\" && state.isDrawingBox)) return;\n\n // Ignore if inside a text-like input or textarea (not range sliders)\n if (e.target.tagName === \"TEXTAREA\") return;\n if (e.target.tagName === \"INPUT\" && e.target.type !== \"range\") return;\n\n // Space hold \u2192 temporary move mode\n if (e.key === \" \") {\n e.preventDefault();\n if (!e.repeat) {\n state.spaceHeld = true;\n updateCanvasCursor();\n renderToolbar();\n }\n return;\n }\n\n var key = e.key;\n\n // Number keys 1-8: switch object\n if (key >= \"1\" && key <= \"8\") {\n var idx = parseInt(key, 10) - 1;\n if (idx < state.objects.length) {\n state.activeObjectIndex = idx;\n renderToolbar();\n requestRender();\n }\n e.preventDefault();\n return;\n }\n\n switch (key.toLowerCase()) {\n case \"n\":\n addObject();\n e.preventDefault();\n break;\n case \"z\":\n undoLastPrompt();\n e.preventDefault();\n break;\n case \"m\":\n state.showMasks = !state.showMasks;\n renderToolbar();\n requestRender();\n e.preventDefault();\n break;\n case \"v\":\n state.settingsVisible = !state.settingsVisible;\n syncVisibility();\n e.preventDefault();\n break;\n case \"h\":\n state.objects[state.activeObjectIndex].visible = !state.objects[state.activeObjectIndex].visible;\n renderToolbar();\n requestRender();\n e.preventDefault();\n break;\n case \"i\":\n state.showImage = !state.showImage;\n renderToolbar();\n requestRender();\n e.preventDefault();\n break;\n case \"c\":\n state.cutoutMode = !state.cutoutMode;\n renderToolbar();\n requestRender();\n e.preventDefault();\n break;\n case \"delete\":\n case \"backspace\":\n deleteObject(state.activeObjectIndex);\n e.preventDefault();\n break;\n case \"?\":\n helpOverlay.classList.toggle(\"hidden\");\n e.preventDefault();\n break;\n case \"f\":\n toggleMaximize();\n e.preventDefault();\n break;\n case \"escape\":\n if (state.isDrawingBox) {\n state.isDrawingBox = false;\n // Reset mouseDownButton so window mousemove won't\n // re-enable isDrawingBox. Keep didDrag=true so the\n // subsequent mouseup is treated as a drag (no-op)\n // rather than a click that would add a point.\n state.mouseDownButton = -1;\n requestRender();\n } else if (state.maximized) {\n toggleMaximize();\n } else {\n helpOverlay.classList.add(\"hidden\");\n }\n e.preventDefault();\n break;\n case \"+\":\n case \"=\":\n zoomToCenter(state.zoom * 1.25);\n e.preventDefault();\n break;\n case \"-\":\n zoomToCenter(state.zoom / 1.25);\n e.preventDefault();\n break;\n case \"0\":\n resetZoom();\n updateCanvasCursor();\n e.preventDefault();\n break;\n }\n });\n\n // --- Button events ---\n\n addObjectBtn.addEventListener(\"click\", function () { addObject(); });\n moveBtn.addEventListener(\"click\", function () {\n state.moveMode = !state.moveMode;\n updateCanvasCursor();\n renderToolbar();\n });\n undoBtn.addEventListener(\"click\", function () { undoLastPrompt(); });\n clearBtn.addEventListener(\"click\", function () { clearActiveObject(); });\n clearAllBtn.addEventListener(\"click\", function () { clearAll(); });\n maskToggleBtn.addEventListener(\"click\", function () {\n state.showMasks = !state.showMasks;\n renderToolbar();\n requestRender();\n });\n maximizeBtn.addEventListener(\"click\", function () { toggleMaximize(); });\n helpBtn.addEventListener(\"click\", function () { helpOverlay.classList.toggle(\"hidden\"); });\n helpCloseBtn.addEventListener(\"click\", function () { helpOverlay.classList.add(\"hidden\"); });\n\n // Settings panel\n settingsBtn.addEventListener(\"click\", function () {\n state.settingsVisible = !state.settingsVisible;\n syncVisibility();\n });\n pointSizeSlider.addEventListener(\"input\", function () {\n pointRadius = parseInt(pointSizeSlider.value, 10);\n pointSizeValue.textContent = pointRadius;\n requestRender();\n });\n maskOpacitySlider.addEventListener(\"input\", function () {\n maskAlpha = parseInt(maskOpacitySlider.value, 10) / 100;\n maskOpacityValue.textContent = parseInt(maskOpacitySlider.value, 10) + \"%\";\n requestRender();\n });\n boxLineWidthSlider.addEventListener(\"input\", function () {\n boxLineWidth = parseFloat(boxLineWidthSlider.value);\n boxLineWidthValue.textContent = boxLineWidth + \"px\";\n requestRender();\n });\n imageToggleBtn.addEventListener(\"click\", function () {\n state.showImage = !state.showImage;\n renderToolbar();\n requestRender();\n });\n cutoutBtn.addEventListener(\"click\", function () {\n state.cutoutMode = !state.cutoutMode;\n renderToolbar();\n requestRender();\n });\n\n function clearImage() {\n if (state.objectUrl) URL.revokeObjectURL(state.objectUrl);\n state.image = null;\n state.imageUrl = null;\n state.objectUrl = null;\n state.filePath = null;\n state.pendingEmit = false;\n state.imageSource = null;\n state.rawMasks = [];\n state.maskCanvases = [];\n state.objects = [createEmptyObject(0)];\n state.activeObjectIndex = 0;\n state.zoom = 1;\n state.panX = 0;\n state.panY = 0;\n state.moveMode = false;\n state.cutoutMode = false;\n state.isProcessing = false;\n if (state.maximized) {\n toggleMaximize();\n }\n ctx.clearRect(0, 0, canvas.width, canvas.height);\n syncVisibility();\n renderToolbar();\n props.value = null;\n trigger(\"input\");\n }\n\n clearImageBtn.addEventListener(\"click\", function () { clearImage(); });\n\n // --- File upload (input) ---\n\n function uploadToServer(file) {\n var capturedUrl = state.objectUrl;\n\n upload(file)\n .then(function (result) {\n if (state.objectUrl !== capturedUrl) return;\n state.filePath = result.path;\n if (state.pendingEmit) {\n state.pendingEmit = false;\n emitPromptData();\n }\n })\n .catch(function () {\n if (state.objectUrl !== capturedUrl) return;\n });\n }\n\n function loadImageFile(file) {\n if (!file || !file.type.startsWith(\"image/\")) return;\n\n // Revoke previous blob URL\n if (state.objectUrl) URL.revokeObjectURL(state.objectUrl);\n\n var url = URL.createObjectURL(file);\n state.objectUrl = url;\n state.filePath = null;\n state.pendingEmit = false;\n state.imageSource = \"upload\";\n state.imageUrl = null;\n state.rawMasks = [];\n state.maskCanvases = [];\n\n var img = new Image();\n img.onload = function () {\n state.image = img;\n state.naturalWidth = img.naturalWidth;\n state.naturalHeight = img.naturalHeight;\n state.objects = [createEmptyObject(0)];\n state.activeObjectIndex = 0;\n state.zoom = 1;\n state.panX = 0;\n state.panY = 0;\n resizeCanvas();\n renderToolbar();\n requestRender();\n emitPromptData();\n };\n img.src = url;\n\n uploadToServer(file);\n }\n\n fileInput.addEventListener(\"change\", function () {\n if (fileInput.files && fileInput.files[0]) {\n loadImageFile(fileInput.files[0]);\n fileInput.value = \"\";\n }\n });\n\n dropZone.addEventListener(\"dragover\", function (e) {\n e.preventDefault();\n dropZone.classList.add(\"drag-over\");\n });\n\n dropZone.addEventListener(\"dragleave\", function () {\n dropZone.classList.remove(\"drag-over\");\n });\n\n dropZone.addEventListener(\"drop\", function (e) {\n e.preventDefault();\n dropZone.classList.remove(\"drag-over\");\n var files = e.dataTransfer && e.dataTransfer.files;\n if (files && files[0]) {\n loadImageFile(files[0]);\n }\n });\n\n // Also allow drop on the whole container when image is loaded\n container.addEventListener(\"dragover\", function (e) {\n e.preventDefault();\n });\n\n container.addEventListener(\"drop\", function (e) {\n e.preventDefault();\n var files = e.dataTransfer && e.dataTransfer.files;\n if (files && files[0] && files[0].type.startsWith(\"image/\")) {\n loadImageFile(files[0]);\n }\n });\n\n // --- Resize ---\n\n var resizeObserver = new ResizeObserver(function () {\n if (state.image) {\n resizeCanvas();\n requestRender();\n }\n });\n resizeObserver.observe(canvasWrapper);\n\n // --- Init ---\n\n renderToolbar();\n handleDataUpdate();\n\n})();\n", "default_props": { "value": "{\"image\": \"https://huggingface.co/datasets/gradio/custom-html-gallery/resolve/main/assets/sam-prompter.png\", \"width\": 1920, \"height\": 1067, \"masks\": [{\"rle\": {\"counts\": [60031, 10, 1055, 13, 1053, 15, 1050, 18, 1048, 20, 1045, 23, 1043, 25, 1040, 28, 1039, 29, 1037, 31, 1036, 32, 1034, 34, 1031, 37, 1029, 39, 1027, 41, 1025, 43, 1024, 44, 1023, 45, 1022, 46, 1020, 48, 1019, 48, 145, 2, 872, 49, 133, 14, 871, 49, 78, 1, 6, 1, 43, 20, 870, 50, 66, 21, 8, 5, 12, 4, 2, 30, 869, 52, 56, 50, 2, 39, 868, 53, 50, 97, 867, 54, 48, 100, 865, 56, 45, 103, 864, 56, 40, 108, 6, 1, 856, 56, 39, 110, 4, 2, 856, 57, 36, 119, 856, 57, 34, 121, 4, 6, 845, 58, 32, 136, 841, 59, 30, 141, 838, 58, 29, 149, 831, 58, 27, 158, 824, 59, 25, 162, 822, 58, 23, 166, 820, 59, 21, 169, 818, 60, 19, 172, 817, 60, 15, 178, 814, 61, 11, 182, 814, 62, 7, 187, 811, 63, 4, 207, 793, 64, 1, 213, 789, 286, 781, 295, 773, 295, 772, 296, 771, 296, 772, 295, 773, 294, 773, 294, 774, 293, 774, 293, 775, 293, 774, 293, 775, 292, 775, 293, 775, 292, 775, 293, 775, 293, 774, 293, 775, 293, 774, 294, 773, 295, 773, 294, 773, 295, 772, 295, 773, 294, 773, 294, 773, 294, 774, 294, 773, 294, 773, 294, 774, 293, 774, 294, 774, 293, 774, 294, 773, 295, 773, 294, 773, 295, 772, 295, 772, 296, 772, 295, 772, 296, 771, 296, 771, 297, 770, 298, 769, 298, 769, 299, 768, 299, 768, 299, 768, 300, 767, 300, 766, 301, 766, 301, 766, 302, 765, 302, 764, 303, 764, 303, 764, 304, 763, 304, 763, 304, 762, 305, 762, 306, 761, 306, 761, 306, 761, 306, 761, 306, 761, 307, 760, 307, 760, 307, 759, 308, 759, 308, 759, 308, 759, 308, 758, 309, 758, 309, 758, 309, 757, 310, 757, 309, 758, 309, 758, 309, 758, 309, 758, 308, 758, 309, 758, 309, 758, 309, 758, 309, 758, 308, 758, 309, 758, 308, 759, 308, 759, 308, 759, 308, 759, 307, 760, 307, 760, 306, 761, 306, 761, 305, 762, 305, 762, 305, 761, 306, 761, 306, 761, 306, 761, 306, 761, 306, 761, 304, 763, 302, 765, 301, 766, 301, 766, 300, 767, 300, 767, 300, 767, 299, 767, 300, 767, 298, 769, 295, 772, 295, 772, 294, 773, 294, 773, 293, 774, 293, 774, 292, 775, 291, 776, 290, 777, 290, 777, 290, 777, 290, 777, 290, 777, 290, 777, 290, 777, 290, 777, 290, 777, 290, 777, 291, 776, 291, 776, 291, 775, 292, 775, 293, 774, 293, 774, 293, 774, 293, 774, 293, 773, 293, 774, 293, 774, 294, 773, 294, 773, 295, 772, 295, 772, 296, 771, 296, 771, 297, 770, 297, 770, 299, 768, 300, 767, 301, 766, 301, 766, 302, 765, 303, 764, 304, 764, 305, 762, 306, 761, 308, 759, 309, 758, 311, 757, 310, 757, 313, 754, 315, 752, 317, 750, 320, 747, 349, 4, 12, 702, 371, 695, 374, 693, 381, 686, 386, 681, 390, 677, 395, 672, 399, 668, 404, 663, 405, 662, 406, 661, 406, 661, 407, 660, 407, 660, 408, 659, 408, 659, 408, 659, 409, 658, 409, 658, 410, 657, 412, 654, 413, 654, 413, 654, 413, 655, 411, 656, 411, 656, 411, 656, 410, 657, 410, 657, 410, 657, 409, 658, 408, 659, 408, 659, 407, 660, 407, 660, 407, 660, 406, 660, 406, 661, 405, 662, 404, 662, 404, 663, 402, 664, 401, 666, 399, 668, 398, 669, 397, 670, 395, 672, 394, 673, 392, 675, 391, 676, 390, 677, 389, 678, 388, 679, 386, 681, 385, 682, 384, 683, 383, 684, 382, 685, 381, 686, 380, 686, 380, 687, 380, 687, 380, 688, 378, 689, 378, 689, 378, 690, 377, 690, 376, 691, 375, 693, 373, 695, 371, 696, 370, 698, 368, 699, 367, 701, 364, 703, 361, 707, 358, 709, 355, 713, 351, 716, 347, 721, 344, 723, 342, 726, 339, 729, 338, 730, 336, 732, 335, 733, 334, 734, 333, 735, 332, 735, 332, 735, 332, 735, 333, 734, 333, 734, 333, 734, 333, 734, 334, 732, 335, 731, 337, 730, 337, 729, 338, 729, 339, 727, 340, 727, 340, 726, 341, 725, 342, 725, 342, 724, 343, 724, 343, 723, 344, 723, 344, 722, 346, 721, 346, 720, 347, 719, 349, 718, 349, 717, 350, 717, 350, 716, 352, 715, 352, 714, 353, 714, 353, 714, 353, 713, 355, 712, 355, 712, 355, 711, 356, 711, 357, 709, 358, 709, 358, 709, 358, 709, 358, 708, 359, 708, 359, 708, 358, 708, 359, 708, 359, 707, 360, 707, 360, 706, 361, 706, 361, 705, 362, 705, 362, 704, 363, 704, 362, 704, 363, 704, 362, 704, 363, 704, 362, 705, 362, 704, 362, 705, 361, 706, 361, 706, 360, 706, 361, 706, 360, 707, 360, 706, 360, 707, 360, 127, 4, 576, 360, 124, 10, 572, 361, 122, 15, 569, 361, 120, 20, 566, 360, 120, 24, 563, 360, 119, 28, 559, 361, 118, 31, 557, 360, 118, 34, 554, 361, 117, 38, 551, 360, 116, 41, 550, 360, 115, 44, 547, 360, 114, 52, 541, 361, 112, 54, 540, 362, 109, 57, 539, 363, 106, 60, 538, 364, 103, 63, 537, 364, 100, 68, 535, 369, 93, 70, 535, 370, 90, 73, 533, 377, 80, 77, 533, 383, 57, 2, 3, 89, 533, 388, 6, 5, 1, 4, 34, 97, 532, 408, 29, 98, 532, 413, 19, 103, 532, 420, 5, 1, 4, 106, 530, 537, 530, 536, 531, 536, 531, 536, 531, 536, 531, 536, 530, 536, 531, 536, 531, 536, 531, 535, 532, 535, 532, 534, 533, 534, 533, 534, 532, 534, 533, 534, 533, 533, 534, 533, 534, 532, 535, 532, 534, 532, 535, 531, 536, 531, 536, 530, 537, 529, 538, 528, 539, 527, 540, 526, 541, 524, 543, 523, 544, 522, 545, 520, 547, 519, 548, 517, 550, 516, 551, 515, 552, 515, 552, 515, 551, 516, 551, 516, 551, 515, 552, 512, 555, 511, 556, 510, 557, 509, 558, 508, 559, 507, 561, 505, 562, 504, 563, 503, 564, 502, 565, 502, 566, 500, 567, 500, 568, 498, 570, 496, 572, 494, 575, 491, 579, 486, 584, 481, 590, 475, 596, 470, 600, 465, 604, 106, 6, 350, 606, 103, 11, 1, 4, 19, 3, 318, 609, 79, 2, 4, 6, 1, 5, 2, 21, 1, 21, 4, 1, 308, 612, 78, 71, 303, 616, 75, 79, 293, 621, 71, 84, 287, 627, 67, 111, 257, 634, 63, 115, 251, 638, 62, 118, 246, 642, 60, 123, 2, 9, 7, 3, 217, 647, 37, 3, 18, 138, 3, 7, 210, 652, 29, 2, 5, 10, 6, 3, 1, 152, 204, 655, 27, 6, 1, 14, 1, 167, 190, 661, 22, 199, 178, 670, 19, 205, 1, 6, 161, 676, 16, 218, 153, 682, 6, 1, 5, 232, 125, 1, 11, 688, 2, 242, 121, 6, 2, 945, 111, 978, 83, 995, 70, 1011, 52, 1026, 37, 1034, 20, 1055, 10, 1475040], \"size\": [1067, 1920]}, \"color\": [255, 107, 107], \"alpha\": 0.4}, {\"rle\": {\"counts\": [505863, 3, 1050, 26, 1037, 35, 1029, 43, 1023, 55, 1010, 63, 1003, 67, 998, 74, 992, 79, 986, 84, 982, 89, 977, 96, 970, 99, 967, 103, 963, 106, 960, 112, 955, 114, 952, 117, 950, 119, 947, 121, 946, 123, 943, 126, 940, 130, 937, 132, 935, 134, 932, 138, 929, 142, 925, 145, 921, 150, 917, 154, 913, 155, 912, 161, 906, 164, 903, 168, 899, 174, 893, 179, 888, 183, 884, 186, 881, 188, 879, 191, 876, 196, 871, 199, 869, 201, 866, 203, 864, 205, 862, 206, 861, 207, 93, 6, 8, 4, 12, 1, 12, 4, 720, 208, 86, 17, 2, 7, 8, 36, 703, 210, 76, 84, 698, 211, 73, 95, 688, 212, 71, 97, 687, 213, 69, 99, 686, 214, 67, 103, 4, 1, 7, 1, 670, 215, 61, 132, 659, 217, 52, 140, 658, 218, 44, 148, 8, 1, 649, 218, 36, 156, 5, 5, 4, 2, 641, 219, 33, 177, 638, 220, 19, 194, 13, 2, 619, 221, 17, 218, 611, 230, 6, 232, 599, 474, 593, 494, 573, 499, 569, 501, 566, 507, 560, 512, 555, 532, 536, 533, 535, 535, 531, 540, 527, 545, 521, 554, 513, 556, 511, 557, 510, 561, 1, 3, 502, 566, 501, 568, 499, 573, 494, 577, 490, 585, 9, 1, 472, 589, 2, 5, 471, 598, 469, 600, 467, 605, 462, 610, 457, 613, 454, 615, 452, 618, 449, 621, 446, 628, 439, 631, 436, 633, 434, 634, 432, 637, 430, 639, 428, 640, 123, 5, 299, 644, 116, 8, 299, 648, 110, 10, 299, 655, 102, 11, 299, 658, 98, 12, 298, 665, 91, 13, 298, 671, 81, 17, 298, 673, 78, 18, 298, 677, 74, 18, 297, 691, 59, 20, 297, 699, 49, 22, 297, 703, 43, 24, 296, 710, 35, 27, 295, 712, 31, 25, 3, 2, 293, 718, 24, 26, 299, 720, 20, 26, 300, 723, 13, 30, 301, 727, 7, 31, 301, 766, 301, 765, 302, 765, 301, 766, 301, 766, 301, 766, 300, 767, 300, 766, 300, 767, 9, 3, 288, 767, 300, 768, 298, 770, 297, 770, 296, 771, 296, 771, 295, 772, 295, 772, 295, 773, 294, 773, 293, 774, 293, 776, 291, 778, 289, 779, 288, 763, 2, 13, 288, 763, 3, 8, 293, 757, 11, 5, 294, 757, 310, 757, 29, 2, 279, 756, 29, 3, 278, 757, 30, 1, 279, 757, 310, 756, 311, 756, 311, 756, 310, 757, 310, 757, 310, 757, 310, 756, 311, 756, 311, 756, 310, 756, 311, 756, 311, 756, 311, 757, 309, 758, 309, 759, 307, 760, 307, 761, 305, 762, 305, 762, 304, 763, 304, 763, 303, 764, 303, 765, 302, 765, 301, 766, 301, 766, 301, 767, 299, 768, 299, 768, 299, 768, 299, 768, 299, 768, 299, 768, 299, 769, 297, 770, 297, 770, 297, 770, 297, 770, 297, 770, 297, 770, 297, 770, 297, 770, 297, 770, 297, 769, 298, 769, 298, 769, 298, 769, 298, 673, 16, 79, 299, 652, 58, 6, 1, 51, 299, 648, 83, 37, 299, 645, 92, 30, 300, 638, 3, 2, 97, 27, 300, 635, 108, 23, 302, 632, 113, 19, 303, 630, 117, 16, 304, 608, 2, 16, 123, 13, 305, 607, 5, 6, 4, 2, 129, 5, 309, 607, 7, 2, 451, 607, 8, 1, 451, 607, 8, 1, 451, 607, 461, 606, 15, 1, 445, 606, 14, 3, 444, 607, 10, 6, 444, 607, 9, 7, 444, 613, 4, 6, 445, 613, 3, 3, 448, 613, 454, 613, 48, 1, 405, 613, 18, 3, 26, 3, 404, 614, 14, 8, 23, 4, 404, 613, 15, 8, 24, 3, 405, 612, 16, 6, 433, 612, 17, 4, 434, 612, 18, 2, 435, 612, 455, 612, 455, 612, 456, 611, 456, 611, 456, 612, 455, 613, 13, 2, 440, 613, 11, 4, 439, 613, 11, 4, 439, 613, 11, 4, 439, 613, 11, 4, 439, 612, 12, 4, 439, 612, 13, 2, 441, 609, 15, 1, 442, 608, 459, 607, 460, 607, 460, 607, 461, 605, 462, 605, 462, 605, 9, 3, 6, 1, 443, 605, 7, 5, 6, 1, 443, 605, 5, 8, 5, 1, 443, 605, 5, 8, 449, 606, 4, 7, 451, 605, 4, 7, 451, 605, 4, 7, 451, 605, 4, 6, 452, 563, 1, 36, 1, 5, 3, 6, 452, 562, 2, 6, 1, 21, 1, 7, 1, 5, 3, 6, 452, 562, 2, 42, 2, 9, 8, 1, 441, 607, 1, 11, 6, 1, 441, 627, 440, 627, 440, 627, 440, 627, 440, 589, 1, 38, 439, 629, 438, 630, 437, 630, 437, 630, 437, 631, 436, 631, 436, 632, 435, 632, 435, 632, 435, 633, 434, 633, 434, 633, 434, 633, 434, 633, 434, 634, 433, 634, 433, 634, 433, 634, 433, 635, 432, 635, 432, 634, 433, 634, 433, 634, 433, 634, 432, 635, 432, 596, 2, 36, 433, 636, 431, 639, 428, 640, 427, 641, 425, 644, 423, 646, 421, 468, 1, 178, 420, 468, 1, 178, 419, 649, 418, 649, 418, 650, 416, 651, 415, 653, 414, 654, 412, 656, 411, 656, 410, 658, 409, 658, 408, 593, 1, 65, 3, 2, 403, 445, 1, 6, 1, 6, 2, 131, 2, 66, 1, 4, 402, 451, 2, 6, 2, 131, 2, 66, 1, 4, 401, 452, 3, 138, 2, 50, 2, 14, 2, 2, 402, 452, 4, 138, 1, 49, 3, 13, 407, 452, 6, 176, 3, 5, 6, 12, 406, 452, 8, 131, 2, 40, 6, 2, 9, 10, 407, 443, 1, 7, 9, 130, 3, 28, 1, 11, 21, 1, 412, 398, 2, 42, 2, 7, 9, 129, 5, 26, 2, 11, 434, 397, 3, 42, 2, 6, 11, 129, 3, 41, 5, 2, 425, 398, 4, 41, 3, 4, 12, 129, 3, 42, 3, 4, 12, 1, 411, 397, 5, 41, 4, 2, 14, 129, 2, 43, 1, 6, 11, 1, 411, 393, 12, 38, 23, 126, 2, 53, 7, 2, 410, 393, 15, 8, 1, 27, 24, 125, 2, 57, 2, 3, 410, 391, 18, 6, 5, 13, 3, 8, 24, 189, 410, 389, 21, 4, 7, 10, 9, 2, 27, 188, 410, 388, 35, 5, 42, 187, 409, 387, 86, 184, 410, 257, 2, 126, 91, 181, 410, 255, 8, 102, 6, 2, 5, 4, 96, 179, 410, 253, 11, 100, 116, 111, 1, 58, 1, 5, 410, 253, 14, 98, 116, 110, 2, 58, 1, 5, 410, 253, 15, 88, 5, 2, 121, 3, 2, 29, 3, 3, 1, 7, 2, 56, 10, 49, 5, 2, 411, 252, 17, 82, 146, 4, 41, 3, 5, 27, 1, 8, 16, 4, 1, 40, 420, 251, 19, 79, 209, 2, 5, 2, 5, 2, 36, 1, 12, 9, 435, 250, 21, 64, 732, 249, 23, 62, 732, 250, 24, 59, 734, 249, 26, 56, 736, 248, 28, 54, 736, 245, 34, 50, 737, 244, 38, 47, 738, 242, 41, 46, 737, 236, 50, 43, 737, 229, 4, 1, 54, 42, 736, 62, 8, 153, 67, 40, 736, 61, 12, 32, 9, 109, 69, 38, 736, 60, 19, 11, 13, 1, 14, 104, 74, 34, 737, 58, 23, 4, 35, 98, 79, 32, 737, 58, 66, 93, 82, 31, 736, 58, 69, 89, 85, 30, 735, 58, 72, 86, 88, 27, 736, 55, 77, 82, 92, 25, 735, 54, 82, 77, 96, 22, 735, 52, 91, 20, 14, 34, 100, 21, 734, 50, 102, 10, 19, 26, 108, 13, 1, 4, 734, 48, 269, 1, 13, 1, 734, 47, 1020, 46, 1020, 47, 1020, 45, 1022, 44, 1022, 43, 1024, 41, 1026, 39, 1027, 39, 1028, 37, 1030, 36, 1031, 35, 1032, 34, 1033, 33, 1034, 32, 1035, 31, 1036, 30, 1037, 29, 1039, 27, 1040, 27, 1040, 26, 1041, 25, 1043, 23, 1045, 21, 1047, 19, 1050, 16, 1053, 13, 1057, 8, 1061, 5, 1141654], \"size\": [1067, 1920]}, \"color\": [78, 205, 196], \"alpha\": 0.4}, {\"rle\": {\"counts\": [841479, 1, 1066, 1, 1066, 2, 1065, 1, 3002, 6, 1059, 10, 4, 2, 1049, 19, 5, 1, 1038, 31, 997, 2, 33, 41, 35, 2, 952, 6, 2, 4, 20, 55, 4, 1, 7, 1, 6, 2, 4, 5, 3, 2, 933, 1, 5, 19, 19, 62, 3, 6, 1, 6, 1, 13, 930, 26, 18, 94, 928, 28, 4, 4, 8, 97, 127, 2, 797, 37, 7, 105, 72, 7, 40, 2, 791, 2, 3, 39, 5, 112, 44, 13, 2, 16, 5, 1, 17, 6, 8, 5, 788, 48, 2, 141, 4, 90, 776, 2, 2, 289, 10, 9, 755, 317, 4, 3, 742, 328, 727, 6, 5, 332, 722, 11, 1, 337, 707, 1, 8, 356, 701, 5, 1, 369, 2, 10, 679, 397, 664, 408, 656, 419, 647, 429, 637, 440, 627, 446, 620, 450, 617, 452, 611, 459, 607, 463, 604, 468, 592, 1, 4, 472, 589, 481, 585, 484, 577, 4, 1, 486, 574, 494, 508, 5, 54, 501, 499, 17, 48, 504, 494, 24, 41, 508, 487, 34, 33, 513, 484, 45, 6, 5, 2, 525, 481, 586, 479, 587, 478, 589, 476, 591, 474, 593, 473, 593, 471, 596, 466, 601, 460, 607, 458, 608, 458, 609, 456, 611, 454, 613, 453, 614, 450, 616, 449, 618, 447, 620, 444, 623, 443, 624, 441, 626, 440, 627, 439, 628, 437, 630, 436, 630, 436, 631, 435, 632, 434, 633, 433, 634, 432, 635, 431, 636, 430, 636, 431, 636, 430, 636, 430, 636, 430, 637, 430, 636, 430, 638, 428, 640, 427, 641, 425, 643, 423, 644, 423, 645, 421, 646, 421, 646, 420, 648, 419, 649, 418, 649, 418, 649, 418, 650, 417, 650, 417, 650, 417, 650, 417, 651, 416, 651, 416, 652, 415, 652, 415, 652, 415, 652, 415, 651, 417, 650, 417, 649, 418, 649, 418, 476, 1, 3, 5, 3, 4, 155, 421, 488, 3, 156, 420, 648, 419, 649, 419, 649, 418, 651, 416, 652, 416, 652, 415, 652, 415, 653, 415, 653, 414, 653, 415, 574, 1, 78, 414, 528, 4, 3, 6, 28, 17, 67, 415, 527, 4, 3, 12, 2, 14, 1, 27, 62, 415, 535, 12, 1, 43, 7, 3, 50, 416, 537, 58, 1, 10, 44, 418, 537, 73, 38, 420, 537, 75, 34, 422, 536, 78, 30, 423, 536, 80, 27, 424, 536, 82, 23, 427, 535, 83, 21, 428, 535, 85, 17, 430, 536, 87, 11, 433, 536, 531, 536, 531, 536, 531, 535, 532, 534, 533, 517, 1, 14, 535, 516, 3, 12, 536, 516, 4, 10, 537, 516, 4, 3, 4, 3, 537, 515, 6, 1, 6, 1, 538, 515, 552, 515, 551, 516, 551, 516, 551, 516, 551, 516, 551, 516, 551, 516, 551, 516, 550, 517, 550, 518, 549, 518, 549, 518, 548, 519, 548, 519, 548, 519, 548, 519, 547, 520, 547, 521, 546, 521, 546, 521, 545, 523, 544, 524, 543, 525, 542, 525, 542, 526, 541, 526, 540, 527, 540, 526, 541, 526, 541, 526, 540, 527, 540, 526, 541, 527, 540, 527, 539, 529, 538, 530, 537, 530, 536, 532, 535, 532, 535, 532, 535, 531, 535, 532, 535, 531, 536, 531, 535, 531, 536, 530, 537, 529, 538, 528, 539, 528, 539, 527, 539, 528, 539, 527, 540, 527, 540, 528, 539, 528, 539, 528, 539, 528, 539, 528, 538, 529, 538, 529, 538, 528, 539, 528, 538, 529, 538, 529, 538, 529, 538, 529, 94, 4, 440, 529, 91, 11, 436, 529, 89, 14, 435, 530, 86, 16, 435, 530, 4, 2, 6, 2, 71, 18, 434, 530, 3, 4, 4, 6, 66, 20, 434, 531, 1, 7, 1, 10, 61, 23, 433, 552, 58, 24, 433, 554, 54, 26, 433, 557, 48, 29, 433, 562, 36, 36, 433, 563, 32, 40, 432, 564, 17, 6, 1, 47, 432, 570, 5, 60, 432, 635, 432, 634, 433, 634, 433, 634, 433, 634, 433, 634, 433, 634, 433, 634, 433, 635, 432, 635, 432, 635, 432, 635, 432, 635, 433, 634, 433, 634, 433, 634, 433, 634, 433, 634, 433, 634, 433, 634, 434, 633, 434, 633, 434, 634, 433, 634, 434, 633, 434, 633, 434, 634, 433, 634, 433, 634, 433, 634, 434, 633, 434, 633, 434, 633, 434, 633, 434, 632, 435, 632, 435, 631, 436, 631, 436, 630, 438, 629, 438, 629, 438, 630, 3, 1, 433, 630, 2, 3, 432, 635, 432, 635, 432, 635, 433, 634, 433, 634, 433, 633, 434, 632, 435, 632, 436, 631, 436, 631, 436, 631, 436, 631, 436, 631, 436, 632, 435, 632, 435, 633, 434, 633, 434, 634, 433, 635, 432, 635, 432, 635, 432, 635, 432, 636, 431, 635, 432, 635, 432, 635, 432, 635, 432, 635, 433, 633, 434, 633, 433, 634, 433, 633, 434, 633, 434, 632, 435, 631, 436, 631, 436, 630, 437, 629, 438, 628, 439, 626, 441, 601, 5, 19, 442, 599, 10, 2, 456, 598, 468, 599, 468, 598, 469, 598, 469, 597, 470, 597, 470, 596, 471, 596, 471, 595, 472, 594, 473, 594, 473, 593, 474, 592, 475, 590, 477, 565, 3, 20, 479, 562, 9, 16, 480, 560, 23, 2, 482, 556, 511, 551, 516, 545, 522, 539, 528, 538, 529, 537, 530, 536, 531, 535, 532, 535, 532, 534, 533, 533, 534, 533, 534, 532, 535, 531, 536, 531, 536, 530, 536, 530, 537, 530, 537, 529, 538, 529, 538, 528, 539, 528, 539, 527, 539, 528, 539, 527, 540, 527, 540, 171, 2, 354, 540, 169, 7, 351, 540, 168, 9, 350, 540, 166, 12, 349, 540, 165, 14, 348, 539, 165, 16, 346, 540, 164, 18, 345, 540, 162, 21, 343, 541, 161, 23, 341, 542, 160, 26, 338, 542, 160, 28, 337, 542, 159, 30, 335, 543, 159, 32, 332, 543, 159, 34, 331, 543, 158, 36, 328, 544, 159, 37, 325, 546, 158, 39, 323, 547, 158, 41, 320, 547, 158, 44, 316, 549, 157, 46, 310, 554, 156, 48, 269, 2, 4, 6, 27, 554, 156, 51, 266, 4, 2, 9, 23, 556, 156, 53, 263, 21, 17, 556, 156, 61, 256, 23, 14, 557, 155, 65, 253, 25, 6, 563, 155, 65, 252, 595, 154, 67, 250, 595, 154, 68, 247, 598, 153, 70, 142, 1, 100, 601, 151, 72, 141, 3, 97, 602, 150, 75, 7, 2, 129, 6, 3, 2, 90, 603, 132, 4, 2, 3, 4, 93, 124, 8, 1, 3, 85, 608, 132, 4, 4, 102, 120, 7, 2, 3, 83, 610, 79, 5, 48, 4, 5, 106, 114, 8, 2, 3, 80, 613, 76, 15, 33, 3, 4, 5, 5, 111, 109, 8, 2, 3, 68, 625, 74, 20, 28, 6, 3, 5, 5, 113, 8, 3, 96, 8, 2, 3, 47, 2, 13, 630, 72, 24, 25, 9, 1, 6, 5, 115, 5, 11, 12, 5, 10, 1, 60, 9, 2, 3, 45, 647, 69, 29, 22, 17, 5, 116, 3, 15, 7, 12, 3, 5, 58, 9, 3, 2, 6, 2, 27, 4, 3, 650, 67, 32, 20, 11, 1, 6, 5, 135, 4, 23, 1, 7, 5, 3, 41, 9, 2, 3, 5, 5, 15, 3, 2, 662, 66, 35, 16, 13, 1, 7, 4, 135, 3, 44, 35, 17, 3, 9, 3, 676, 64, 40, 7, 19, 2, 6, 4, 135, 2, 50, 30, 708, 63, 67, 3, 5, 4, 135, 2, 57, 22, 709, 60, 70, 4, 4, 4, 135, 2, 58, 20, 710, 59, 71, 6, 2, 4, 136, 1, 65, 12, 7, 1, 702, 58, 73, 7, 1, 4, 221, 1, 702, 57, 74, 7, 1, 3, 925, 55, 76, 11, 925, 54, 77, 9, 927, 53, 78, 8, 928, 53, 78, 8, 928, 52, 81, 6, 928, 51, 83, 5, 928, 50, 84, 5, 928, 49, 84, 7, 927, 48, 85, 7, 926, 48, 86, 7, 926, 47, 87, 7, 926, 46, 88, 8, 925, 45, 1023, 43, 1024, 41, 1026, 39, 1028, 37, 1030, 36, 1031, 35, 1033, 32, 1035, 29, 1039, 27, 1040, 26, 1042, 23, 1045, 21, 1047, 19, 1051, 14, 1055, 10, 1059, 5, 754170], \"size\": [1067, 1920]}, \"color\": [69, 183, 209], \"alpha\": 0.4}, {\"rle\": {\"counts\": [1223147, 1, 1064, 7, 1059, 11, 1055, 14, 1052, 16, 1050, 18, 1048, 20, 1047, 21, 1044, 24, 1042, 26, 1040, 28, 1038, 30, 467, 1, 569, 31, 466, 1, 569, 32, 461, 3, 570, 35, 452, 2, 2, 7, 568, 38, 448, 13, 2, 2, 563, 40, 439, 5, 2, 14, 2, 3, 561, 42, 433, 26, 2, 3, 561, 44, 430, 27, 2, 2, 562, 46, 369, 2, 56, 30, 564, 48, 327, 1, 28, 3, 7, 5, 3, 3, 3, 8, 35, 32, 564, 49, 324, 15, 13, 38, 30, 34, 564, 51, 321, 17, 4, 55, 20, 35, 564, 52, 319, 84, 11, 37, 563, 54, 317, 88, 6, 39, 563, 57, 313, 134, 563, 58, 299, 8, 5, 133, 563, 61, 295, 13, 1, 134, 563, 64, 280, 1, 6, 152, 564, 66, 263, 1, 12, 5, 2, 154, 564, 72, 4, 2, 235, 3, 11, 5, 1, 5, 3, 161, 4, 1, 560, 81, 230, 7, 1, 3, 5, 12, 2, 161, 564, 85, 222, 19, 1, 177, 563, 87, 214, 204, 524, 2, 36, 89, 211, 205, 523, 4, 34, 92, 206, 208, 523, 5, 26, 2, 5, 93, 203, 210, 522, 6, 26, 2, 5, 93, 197, 216, 522, 6, 26, 2, 4, 96, 192, 219, 520, 8, 32, 98, 175, 1, 6, 219, 2, 2, 522, 10, 29, 104, 170, 227, 4, 1, 518, 13, 30, 108, 164, 229, 4, 1, 515, 16, 29, 111, 156, 234, 5, 1, 514, 17, 24, 1, 4, 113, 149, 239, 5, 1, 513, 18, 24, 1, 4, 115, 146, 240, 5, 1, 512, 19, 24, 1, 4, 117, 143, 241, 517, 20, 24, 1, 4, 119, 140, 242, 516, 21, 29, 121, 134, 246, 516, 21, 29, 122, 127, 252, 515, 16, 1, 5, 28, 124, 124, 254, 513, 18, 1, 5, 27, 127, 51, 1, 69, 255, 16, 1, 496, 18, 1, 6, 25, 131, 45, 7, 64, 256, 17, 1, 495, 26, 23, 141, 36, 12, 6, 2, 48, 260, 17, 2, 494, 26, 23, 143, 33, 22, 45, 262, 17, 2, 493, 27, 22, 150, 21, 1, 4, 23, 44, 263, 17, 3, 491, 28, 22, 152, 12, 3, 1, 6, 2, 24, 38, 3, 2, 262, 17, 5, 489, 30, 21, 153, 10, 12, 1, 24, 8, 1, 28, 263, 4, 1, 17, 6, 487, 31, 7, 1, 13, 157, 5, 39, 6, 3, 26, 264, 4, 1, 16, 7, 487, 31, 21, 202, 4, 4, 26, 265, 1, 4, 15, 7, 486, 37, 16, 211, 24, 272, 14, 7, 485, 39, 15, 212, 1, 5, 5, 1, 9, 275, 13, 6, 486, 39, 4, 1, 10, 228, 4, 279, 10, 5, 486, 40, 3, 3, 9, 229, 3, 287, 2, 5, 485, 41, 3, 3, 9, 230, 1, 288, 2, 4, 486, 42, 1, 4, 8, 521, 1, 4, 486, 48, 7, 525, 486, 50, 5, 525, 487, 580, 487, 579, 487, 580, 487, 580, 486, 581, 486, 581, 486, 581, 486, 581, 486, 580, 487, 579, 487, 580, 487, 580, 487, 580, 487, 580, 486, 581, 486, 577, 2, 2, 485, 578, 3, 1, 485, 578, 489, 578, 488, 580, 487, 581, 485, 582, 485, 582, 485, 582, 485, 582, 484, 583, 484, 582, 485, 581, 486, 581, 485, 581, 486, 581, 486, 581, 485, 581, 486, 580, 486, 578, 489, 569, 497, 529, 7, 27, 504, 528, 25, 8, 505, 527, 31, 3, 506, 525, 542, 525, 541, 520, 2, 3, 541, 521, 3, 2, 541, 513, 2, 5, 547, 513, 2, 5, 546, 515, 1, 5, 546, 521, 546, 521, 546, 511, 1, 8, 547, 504, 1, 4, 4, 6, 548, 503, 4, 1, 5, 5, 549, 502, 11, 5, 549, 501, 13, 2, 550, 500, 567, 499, 568, 499, 568, 499, 13, 1, 553, 499, 14, 1, 553, 499, 568, 499, 568, 498, 569, 497, 569, 498, 569, 497, 6, 1, 563, 497, 570, 498, 569, 498, 569, 499, 569, 499, 568, 499, 7, 1, 560, 499, 7, 1, 560, 499, 568, 499, 567, 501, 566, 501, 566, 502, 565, 503, 564, 501, 566, 499, 568, 497, 570, 496, 571, 488, 1, 5, 573, 487, 580, 486, 582, 485, 582, 484, 583, 480, 587, 479, 588, 479, 589, 478, 589, 477, 590, 477, 590, 477, 590, 477, 590, 477, 590, 471, 2, 4, 12, 2, 576, 464, 1, 5, 4, 3, 12, 2, 576, 463, 3, 3, 6, 1, 13, 2, 576, 463, 4, 1, 600, 462, 605, 463, 604, 463, 605, 462, 605, 462, 605, 461, 606, 460, 607, 459, 608, 459, 608, 458, 609, 455, 612, 454, 613, 454, 613, 455, 612, 456, 611, 457, 611, 456, 611, 457, 610, 446, 1, 10, 610, 446, 3, 7, 611, 445, 5, 6, 612, 444, 5, 1, 617, 444, 623, 444, 624, 442, 625, 442, 625, 442, 625, 442, 625, 441, 10, 1, 616, 440, 9, 3, 615, 440, 9, 3, 615, 439, 10, 3, 616, 439, 9, 3, 616, 440, 627, 440, 628, 440, 627, 440, 627, 440, 627, 440, 628, 440, 627, 440, 628, 442, 3, 3, 619, 443, 1, 5, 619, 449, 618, 449, 618, 449, 619, 448, 620, 446, 621, 446, 622, 443, 624, 442, 625, 442, 626, 441, 626, 442, 625, 443, 625, 442, 625, 442, 625, 442, 13, 1, 612, 441, 13, 1, 612, 441, 626, 441, 626, 440, 628, 438, 629, 438, 629, 438, 4, 1, 14, 1, 609, 438, 4, 1, 625, 437, 630, 438, 629, 439, 628, 439, 628, 440, 627, 440, 628, 439, 628, 439, 629, 438, 629, 437, 631, 435, 632, 435, 3, 1, 628, 435, 2, 2, 628, 439, 628, 439, 629, 438, 629, 438, 629, 438, 629, 438, 629, 438, 629, 438, 629, 438, 629, 439, 628, 439, 628, 439, 628, 439, 629, 438, 629, 438, 629, 438, 630, 437, 630, 437, 630, 437, 9, 3, 3, 3, 612, 437, 8, 11, 611, 438, 7, 11, 611, 439, 6, 11, 612, 438, 7, 12, 610, 439, 7, 12, 609, 441, 5, 12, 609, 443, 4, 6, 1, 4, 609, 459, 12, 1, 596, 458, 5, 1, 5, 2, 596, 459, 2, 4, 3, 4, 595, 478, 589, 479, 588, 480, 591, 477, 591, 6, 1, 474, 586, 5, 2, 475, 586, 4, 3, 475, 586, 1, 6, 474, 594, 473, 3, 3, 588, 474, 1, 5, 588, 480, 587, 484, 582, 492, 575, 494, 573, 496, 571, 501, 566, 504, 564, 504, 563, 507, 561, 509, 1, 7, 550, 529, 538, 530, 537, 530, 4, 1, 532, 531, 3, 2, 531, 531, 2, 3, 532, 535, 532, 534, 533, 534, 534, 533, 534, 533, 534, 533, 534, 533, 534, 535, 533, 535, 532, 540, 528, 8, 1, 531, 529, 5, 2, 531, 529, 5, 2, 532, 4, 1, 523, 6, 1, 533, 2, 3, 523, 544, 521, 1, 4, 542, 520, 1, 4, 543, 519, 1, 5, 545, 516, 1, 5, 546, 521, 547, 520, 547, 520, 547, 521, 546, 521, 546, 522, 545, 522, 545, 521, 546, 521, 546, 521, 546, 520, 547, 520, 547, 520, 546, 521, 546, 521, 546, 521, 546, 520, 547, 520, 547, 520, 547, 520, 547, 520, 544, 523, 543, 525, 542, 525, 541, 526, 540, 527, 516, 1, 14, 1, 6, 529, 515, 3, 12, 3, 4, 530, 513, 6, 11, 5, 1, 530, 512, 9, 9, 536, 512, 13, 5, 536, 513, 13, 2, 538, 513, 15, 1, 538, 509, 19, 1, 538, 510, 18, 1, 539, 510, 17, 1, 540, 509, 559, 508, 560, 507, 560, 508, 12, 1, 5, 2, 539, 507, 13, 2, 4, 3, 538, 507, 13, 3, 2, 4, 538, 505, 15, 9, 538, 504, 16, 10, 538, 502, 18, 10, 538, 501, 18, 11, 533, 1, 3, 500, 19, 12, 532, 1, 3, 500, 19, 12, 536, 497, 23, 11, 536, 496, 24, 12, 535, 483, 7, 4, 26, 13, 534, 470, 51, 15, 531, 465, 56, 16, 529, 404, 1, 54, 64, 16, 529, 402, 2, 51, 67, 16, 529, 402, 2, 48, 71, 15, 529, 400, 5, 45, 74, 14, 529, 399, 9, 17, 5, 14, 3, 2, 75, 15, 528, 399, 11, 12, 9, 12, 82, 14, 528, 398, 12, 3, 2, 6, 11, 10, 84, 13, 528, 385, 1, 12, 13, 1, 4, 4, 18, 4, 85, 13, 3, 2, 522, 384, 2, 6, 1, 5, 19, 3, 20, 1, 88, 10, 4, 2, 521, 384, 4, 3, 4, 3, 21, 1, 117, 2, 5, 2, 519, 384, 15, 1, 146, 2, 518, 383, 163, 6, 514, 383, 162, 9, 513, 355, 3, 20, 4, 1, 29, 1, 132, 4, 1, 5, 512, 347, 4, 3, 5, 18, 35, 1, 137, 4, 513, 346, 6, 1, 7, 2, 3, 11, 176, 1, 514, 342, 24, 9, 693, 1, 3, 336, 26, 7, 41, 1, 657, 334, 29, 5, 41, 2, 656, 333, 30, 5, 41, 2, 5, 2, 650, 331, 31, 4, 42, 2, 5, 2, 650, 331, 31, 4, 42, 2, 5, 3, 650, 307, 3, 21, 30, 4, 41, 4, 3, 4, 654, 302, 8, 17, 30, 4, 41, 4, 3, 4, 656, 298, 11, 16, 30, 4, 40, 6, 1, 6, 657, 295, 12, 16, 30, 4, 38, 16, 657, 294, 12, 1, 3, 12, 29, 5, 37, 18, 656, 293, 18, 6, 1, 4, 29, 5, 37, 19, 655, 293, 20, 2, 5, 2, 78, 10, 657, 292, 110, 6, 658, 292, 113, 3, 659, 290, 116, 2, 659, 289, 118, 1, 651, 1, 6, 289, 771, 1, 5, 287, 781, 286, 781, 286, 781, 285, 783, 284, 779, 1, 3, 284, 779, 2, 2, 283, 785, 280, 787, 280, 787, 279, 789, 277, 791, 275, 129, 1, 662, 274, 129, 2, 663, 272, 130, 3, 662, 272, 130, 3, 662, 271, 131, 3, 662, 267, 135, 3, 662, 263, 139, 3, 663, 261, 141, 2, 663, 260, 143, 3, 661, 260, 145, 1, 661, 239, 1, 18, 809, 238, 4, 14, 811, 236, 831, 233, 834, 232, 184, 1, 650, 228, 187, 3, 649, 228, 182, 1, 4, 3, 650, 227, 182, 2, 3, 3, 651, 225, 183, 3, 2, 3, 652, 220, 186, 7, 654, 219, 187, 6, 656, 205, 3, 3, 5, 1, 188, 6, 9, 1, 646, 204, 5, 1, 6, 1, 188, 6, 8, 3, 645, 204, 201, 7, 7, 3, 645, 203, 202, 9, 5, 2, 646, 202, 203, 4, 2, 4, 5, 1, 646, 198, 207, 3, 3, 5, 4, 1, 6, 2, 639, 195, 209, 3, 3, 5, 4, 1, 6, 2, 640, 190, 213, 2, 4, 5, 10, 3, 640, 188, 216, 1, 4, 5, 10, 3, 640, 186, 224, 3, 11, 4, 639, 182, 229, 1, 12, 4, 639, 180, 243, 5, 638, 175, 1, 3, 244, 6, 638, 167, 2, 4, 249, 10, 635, 166, 5, 1, 244, 1, 5, 13, 633, 159, 1, 3, 258, 13, 633, 148, 273, 13, 634, 145, 276, 12, 635, 141, 279, 7, 2, 4, 634, 137, 284, 6, 3, 3, 634, 129, 292, 6, 3, 3, 634, 5, 1, 116, 299, 6, 3, 3, 635, 2, 3, 114, 303, 3, 4, 2, 641, 113, 305, 1, 6, 1, 642, 111, 313, 1, 642, 110, 958, 103, 966, 94, 974, 5, 1, 80, 3, 3, 976, 2, 4, 57, 1, 14, 3, 2, 996, 43, 4, 2, 5, 10, 1008, 15, 4, 4, 12, 1, 16, 4, 130431, 1, 1066, 1, 1066, 1, 1066, 1, 193510], \"size\": [1067, 1920]}, \"color\": [150, 206, 180], \"alpha\": 0.4}, {\"rle\": {\"counts\": [1303086, 1, 1066, 1, 1066, 1, 216611, 3, 1064, 5, 1061, 10, 1057, 11, 1056, 11, 1056, 12, 1054, 18, 1049, 18, 1048, 19, 1048, 19, 1048, 20, 1047, 20, 1048, 19, 1050, 17, 1052, 15, 1052, 16, 1052, 15, 1052, 15, 1052, 15, 1052, 18, 1048, 22, 1043, 27, 1040, 27, 1040, 28, 1038, 29, 1039, 28, 1039, 28, 1039, 28, 1039, 28, 1039, 28, 1039, 28, 1039, 28, 1038, 29, 1038, 29, 1038, 29, 1037, 30, 1037, 30, 1037, 30, 1036, 31, 1035, 31, 1035, 32, 1034, 33, 1031, 35, 1031, 36, 1030, 36, 1031, 36, 1030, 37, 1029, 38, 1028, 38, 1028, 39, 1027, 39, 1028, 39, 1027, 40, 1026, 41, 1025, 42, 1024, 43, 1023, 44, 1019, 48, 1018, 49, 1017, 50, 1016, 51, 1014, 53, 1012, 56, 1009, 59, 1007, 61, 1006, 58, 1008, 59, 1006, 60, 1005, 62, 1004, 63, 1002, 64, 1002, 65, 1001, 67, 999, 68, 997, 70, 996, 71, 994, 73, 992, 75, 990, 77, 989, 78, 988, 79, 987, 81, 986, 81, 984, 83, 982, 86, 981, 86, 980, 87, 979, 88, 978, 90, 972, 97, 968, 99, 968, 99, 967, 100, 967, 100, 966, 101, 966, 101, 965, 102, 399, 4, 561, 103, 398, 6, 559, 104, 397, 7, 558, 105, 396, 8, 557, 106, 395, 9, 555, 107, 395, 5, 2, 4, 550, 109, 384, 1, 11, 6, 4, 2, 3, 4, 543, 108, 384, 4, 4, 3, 2, 6, 5, 8, 542, 109, 383, 5, 3, 12, 5, 9, 541, 112, 380, 20, 5, 9, 540, 114, 379, 22, 4, 9, 538, 116, 378, 23, 3, 14, 533, 116, 377, 24, 3, 15, 15, 3, 513, 118, 367, 33, 3, 17, 7, 10, 510, 121, 366, 11, 2, 13, 2, 5, 3, 35, 3, 2, 503, 123, 365, 12, 1, 21, 1, 36, 2, 3, 503, 123, 365, 76, 502, 126, 356, 2, 6, 74, 503, 135, 347, 2, 8, 71, 503, 136, 347, 3, 8, 11, 2, 57, 501, 139, 346, 5, 6, 11, 2, 13, 2, 28, 1, 13, 500, 140, 346, 6, 5, 11, 2, 13, 2, 28, 1, 13, 500, 140, 345, 7, 5, 11, 2, 56, 501, 140, 345, 8, 3, 70, 501, 139, 345, 82, 500, 140, 329, 1, 14, 82, 500, 141, 326, 5, 8, 68, 1, 18, 500, 141, 325, 6, 8, 67, 2, 17, 500, 142, 284, 2, 39, 5, 9, 67, 2, 17, 500, 142, 284, 2, 59, 62, 1, 17, 499, 143, 284, 4, 60, 77, 499, 143, 284, 5, 62, 74, 498, 145, 283, 6, 61, 3, 1, 71, 496, 146, 283, 7, 66, 69, 496, 146, 284, 6, 66, 69, 496, 146, 283, 7, 67, 67, 496, 147, 283, 7, 69, 64, 497, 147, 283, 7, 71, 61, 497, 148, 282, 7, 76, 57, 496, 150, 278, 10, 78, 5, 1, 48, 496, 151, 278, 10, 78, 4, 3, 5, 2, 6, 1, 29, 3, 1, 495, 152, 277, 11, 79, 3, 3, 5, 2, 6, 2, 5, 2, 20, 500, 152, 277, 11, 80, 1, 4, 5, 2, 6, 1, 29, 499, 152, 276, 12, 85, 43, 3, 1, 495, 153, 273, 15, 84, 47, 494, 154, 272, 16, 84, 47, 493, 155, 271, 17, 84, 39, 1, 7, 493, 155, 271, 18, 84, 23, 2, 13, 2, 6, 492, 156, 269, 20, 84, 23, 1, 14, 2, 6, 492, 156, 267, 22, 86, 36, 1, 7, 492, 156, 259, 3, 2, 25, 87, 43, 492, 156, 249, 5, 4, 31, 89, 41, 2, 2, 489, 155, 246, 9, 2, 32, 91, 39, 1, 10, 482, 155, 236, 2, 6, 45, 91, 38, 2, 11, 481, 155, 233, 7, 3, 46, 91, 38, 2, 11, 481, 155, 233, 56, 92, 50, 6, 1, 474, 155, 234, 55, 97, 45, 6, 1, 474, 155, 234, 55, 102, 41, 5, 1, 473, 156, 230, 1, 3, 55, 104, 39, 5, 1, 473, 157, 227, 4, 3, 54, 105, 38, 4, 2, 472, 158, 226, 5, 3, 54, 108, 36, 3, 2, 470, 161, 224, 7, 2, 54, 109, 28, 1, 6, 2, 3, 470, 161, 224, 7, 1, 54, 110, 35, 475, 161, 224, 62, 111, 34, 475, 161, 223, 56, 2, 4, 113, 32, 476, 161, 223, 56, 3, 2, 115, 31, 476, 161, 223, 49, 1, 5, 122, 29, 477, 161, 222, 5, 1, 7, 1, 35, 3, 4, 122, 16, 6, 6, 478, 162, 220, 14, 1, 35, 3, 4, 122, 15, 7, 5, 479, 162, 218, 53, 2, 5, 121, 13, 492, 164, 216, 54, 1, 6, 122, 11, 8, 1, 483, 166, 191, 1, 11, 1, 11, 66, 117, 11, 7, 3, 482, 166, 187, 7, 8, 2, 11, 67, 116, 10, 8, 3, 481, 168, 185, 10, 6, 3, 4, 3, 3, 13, 1, 53, 117, 8, 9, 3, 6, 1, 473, 170, 183, 11, 6, 3, 3, 4, 3, 13, 2, 53, 116, 8, 9, 3, 6, 2, 3, 3, 466, 170, 177, 18, 4, 5, 1, 21, 1, 53, 118, 7, 9, 3, 6, 3, 1, 5, 465, 170, 170, 3, 3, 19, 4, 80, 121, 6, 8, 3, 5, 10, 465, 170, 169, 108, 126, 4, 6, 4, 4, 11, 464, 172, 154, 2, 5, 101, 1, 13, 126, 30, 463, 174, 138, 1, 9, 112, 2, 11, 128, 10, 2, 20, 460, 175, 135, 5, 5, 112, 5, 10, 128, 10, 3, 21, 458, 176, 120, 3, 9, 123, 7, 9, 128, 10, 3, 21, 458, 176, 120, 4, 8, 122, 7, 10, 142, 20, 458, 176, 119, 7, 6, 122, 6, 11, 146, 16, 458, 177, 118, 134, 6, 12, 147, 10, 3, 3, 457, 177, 112, 3, 3, 132, 7, 14, 147, 9, 3, 3, 457, 177, 109, 7, 1, 132, 7, 17, 145, 9, 3, 3, 458, 177, 106, 9, 1, 14, 1, 117, 7, 18, 145, 7, 5, 2, 458, 178, 103, 143, 8, 18, 146, 6, 5, 1, 458, 181, 100, 145, 7, 12, 2, 3, 617, 188, 11, 5, 18, 3, 5, 1, 47, 148, 8, 10, 622, 243, 1, 4, 24, 156, 7, 2, 4, 3, 622, 251, 9, 170, 13, 2, 621, 426, 2, 4, 13, 1, 621, 426, 2, 4, 635, 426, 2, 4, 634, 427, 2, 4, 634, 432, 635, 432, 6, 2, 7, 7, 612, 431, 7, 5, 5, 8, 611, 417, 1, 13, 6, 9, 2, 9, 609, 418, 1, 13, 4, 11, 1, 11, 608, 418, 1, 13, 4, 22, 609, 432, 2, 24, 610, 439, 5, 12, 611, 438, 6, 11, 612, 425, 5, 8, 6, 10, 613, 423, 7, 8, 6, 10, 613, 422, 8, 8, 6, 10, 612, 424, 7, 7, 7, 11, 611, 428, 4, 6, 6, 14, 609, 430, 13, 17, 607, 431, 11, 18, 606, 432, 11, 18, 606, 432, 10, 19, 12, 3, 591, 431, 11, 19, 13, 2, 591, 430, 12, 19, 606, 429, 13, 18, 607, 423, 1, 5, 13, 17, 608, 422, 3, 3, 13, 11, 1, 6, 608, 422, 3, 3, 13, 11, 2, 4, 5, 2, 602, 422, 4, 2, 13, 3, 2, 5, 3, 4, 5, 2, 602, 422, 3, 3, 12, 11, 3, 4, 609, 423, 1, 4, 11, 12, 2, 6, 608, 429, 10, 13, 1, 6, 608, 430, 8, 10, 3, 9, 607, 431, 6, 11, 3, 10, 606, 432, 5, 11, 3, 10, 606, 432, 4, 12, 3, 10, 606, 449, 1, 11, 606, 462, 605, 468, 599, 460, 1, 8, 599, 459, 1, 8, 599, 363, 1, 94, 2, 8, 599, 459, 1, 9, 598, 459, 1, 10, 597, 472, 595, 474, 593, 461, 3, 11, 592, 359, 1, 101, 3, 11, 9, 2, 581, 359, 1, 101, 4, 11, 7, 4, 580, 359, 1, 102, 2, 11, 8, 4, 580, 475, 9, 2, 581, 474, 10, 2, 581, 473, 11, 2, 581, 466, 1, 6, 11, 2, 581, 355, 1, 110, 2, 4, 12, 2, 581, 355, 1, 110, 2, 4, 12, 2, 580, 474, 11, 2, 579, 476, 10, 2, 578, 479, 7, 3, 578, 479, 7, 4, 576, 480, 7, 5, 575, 480, 7, 6, 574, 481, 3, 9, 574, 494, 573, 494, 573, 495, 572, 497, 570, 499, 568, 499, 568, 499, 568, 499, 568, 500, 567, 501, 566, 59, 1, 441, 565, 58, 3, 442, 564, 58, 3, 443, 563, 59, 2, 444, 562, 506, 561, 507, 560, 507, 560, 312, 21, 174, 560, 310, 24, 173, 560, 309, 28, 171, 559, 307, 31, 171, 558, 305, 34, 171, 557, 303, 37, 171, 556, 302, 39, 170, 556, 301, 40, 171, 555, 297, 45, 170, 555, 297, 46, 169, 555, 297, 47, 170, 553, 297, 49, 170, 551, 296, 52, 168, 551, 296, 52, 168, 551, 296, 52, 168, 551, 296, 53, 167, 551, 295, 55, 167, 550, 295, 57, 165, 549, 296, 58, 164, 549, 296, 59, 164, 547, 297, 61, 162, 547, 297, 63, 5, 2, 154, 545, 298, 65, 1, 5, 153, 545, 298, 72, 153, 544, 298, 80, 146, 542, 299, 85, 142, 541, 299, 88, 139, 540, 300, 89, 139, 538, 301, 90, 138, 538, 301, 91, 1, 4, 133, 536, 302, 97, 132, 536, 302, 102, 128, 535, 302, 104, 126, 534, 303, 106, 124, 534, 303, 107, 124, 533, 303, 108, 4, 3, 116, 533, 303, 116, 115, 533, 303, 117, 114, 533, 303, 122, 110, 532, 302, 124, 110, 531, 302, 126, 108, 531, 302, 127, 108, 530, 302, 128, 107, 530, 302, 129, 106, 530, 301, 132, 104, 530, 301, 133, 102, 530, 301, 135, 101, 530, 301, 136, 100, 530, 300, 138, 98, 531, 301, 140, 96, 530, 301, 142, 2, 3, 90, 529, 302, 148, 88, 529, 302, 151, 85, 529, 302, 157, 79, 529, 302, 158, 79, 528, 302, 159, 77, 529, 303, 160, 75, 529, 303, 163, 72, 529, 303, 164, 70, 530, 303, 165, 68, 532, 302, 166, 67, 532, 302, 167, 65, 533, 302, 168, 64, 533, 302, 170, 63, 532, 302, 172, 61, 532, 302, 173, 60, 533, 301, 174, 59, 533, 301, 174, 59, 533, 301, 174, 59, 533, 301, 176, 55, 536, 300, 178, 52, 538, 298, 181, 49, 539, 298, 182, 47, 541, 297, 184, 44, 542, 296, 186, 4, 1, 36, 544, 296, 192, 28, 2, 3, 547, 294, 195, 25, 553, 294, 199, 4, 6, 7, 559, 292, 776, 291, 777, 290, 777, 290, 782, 284, 785, 281, 792, 27, 3, 244, 795, 18, 2, 3, 8, 240, 801, 12, 16, 236, 808, 2, 22, 234, 834, 232, 837, 229, 838, 228, 840, 227, 841, 225, 843, 223, 845, 221, 847, 220, 848, 217, 851, 215, 853, 213, 855, 211, 857, 209, 859, 208, 862, 201, 868, 198, 870, 196, 872, 194, 875, 192, 876, 190, 879, 188, 883, 183, 886, 116, 4, 60, 891, 110, 11, 54, 895, 104, 16, 50, 901, 97, 23, 46, 906, 89, 30, 41, 910, 85, 33, 39, 917, 77, 36, 36, 922, 71, 40, 30, 929, 66, 45, 25, 934, 8, 7, 46, 50, 21, 963, 29, 56, 18, 966, 26, 61, 14, 970, 22, 69, 4, 974, 19, 1056, 7, 107438], \"size\": [1067, 1920]}, \"color\": [255, 234, 167], \"alpha\": 0.4}]}", "max_objects": 8, "point_radius": 6, "mask_alpha": 0.4 }, "python_code": "class SamPrompter(gr.HTML):\n def __init__(\n self,\n value: str | Path | Image.Image | np.ndarray | tuple[Any, list[dict[str, Any]]] | None = None,\n *,\n label: str | None = None,\n max_objects: int = 8,\n point_radius: int = 6,\n mask_alpha: float = 0.4,\n **kwargs: Any, # noqa: ANN401 - forwarded to gr.HTML\n ) -> None:\n self.max_objects = max_objects\n self.point_radius = point_radius\n self.mask_alpha = mask_alpha\n\n html_template = (_STATIC_DIR / \"template.html\").read_text(encoding=\"utf-8\")\n css_template = (_STATIC_DIR / \"style.css\").read_text(encoding=\"utf-8\")\n js_on_load = (_STATIC_DIR / \"script.js\").read_text(encoding=\"utf-8\")\n\n super().__init__(\n value=value,\n label=label,\n show_label=label is not None,\n container=label is not None,\n html_template=html_template,\n css_template=css_template,\n js_on_load=js_on_load,\n max_objects=max_objects,\n point_radius=point_radius,\n mask_alpha=mask_alpha,\n **kwargs,\n )\n\n @staticmethod\n def clear(\n value: str | Path | Image.Image | np.ndarray | tuple[Any, list[dict[str, Any]]] | None = None,\n *,\n max_objects: int | None = None,\n ) -> _ClearPrompts:\n \"\"\"Return a value that clears all user-drawn prompts (points and boxes).\n\n Pass this as a return value from an event handler to clear the prompts\n while optionally keeping (or replacing) the displayed image and masks.\n\n * ``clear()`` \u2014 clears prompts and masks; the current image stays.\n * ``clear(image)`` \u2014 clears prompts and masks; re-sends the image.\n * ``clear((image, masks))`` \u2014 clears prompts; re-sends image with\n new masks.\n\n Parameters\n ----------\n value:\n Image or (image, masks) tuple to display after clearing.\n max_objects:\n If given, dynamically update the maximum number of objects the\n frontend allows.\n\n Example usage::\n\n prompter = SamPrompter()\n\n\n # Clear prompts and masks, keep the current image as-is\n def on_clear():\n return SamPrompter.clear()\n\n\n # Clear prompts and masks, re-send the image\n def on_clear_with_image(image):\n return SamPrompter.clear(image)\n \"\"\"\n return _ClearPrompts(value, max_objects=max_objects)\n\n def postprocess(\n self,\n value: str | Path | Image.Image | np.ndarray | tuple[Any, list[dict[str, Any]]] | _ClearPrompts | None,\n ) -> str | None:\n clear_prompts = False\n max_objects_override: int | None = None\n if isinstance(value, _ClearPrompts):\n clear_prompts = True\n max_objects_override = value.max_objects\n value = value.value\n\n if value is None:\n if clear_prompts:\n result: dict[str, Any] = {\"clearPrompts\": True}\n if max_objects_override is not None:\n result[\"maxObjects\"] = max_objects_override\n return json.dumps(result)\n return None\n\n if isinstance(value, tuple):\n image_source, masks_list = value\n else:\n image_source, masks_list = value, []\n img = _load_image(image_source)\n image_url = _save_image_to_cache(img, self.GRADIO_CACHE)\n\n encoded_masks = []\n for i, mask_info in enumerate(masks_list):\n mask_array = mask_info[\"mask\"]\n if isinstance(mask_array, Image.Image):\n mask_array = np.array(mask_array)\n binary = (mask_array > 0).astype(np.uint8)\n color = mask_info.get(\"color\") or _hex_to_rgb(_COLOR_PALETTE[i % len(_COLOR_PALETTE)])\n alpha = mask_info.get(\"alpha\", self.mask_alpha)\n encoded_masks.append(\n {\n \"rle\": _encode_mask_to_rle(binary),\n \"color\": color,\n \"alpha\": alpha,\n }\n )\n\n payload = {\n \"image\": image_url,\n \"width\": img.width,\n \"height\": img.height,\n \"masks\": encoded_masks,\n }\n if clear_prompts:\n payload[\"clearPrompts\"] = True\n if max_objects_override is not None:\n payload[\"maxObjects\"] = max_objects_override\n return json.dumps(payload)\n\n def preprocess(self, payload: Any) -> dict[str, Any] | None: # noqa: ANN401 - Gradio override\n \"\"\"Parse the raw JSON string from the frontend into a dict.\n\n Delegates to :func:`parse_prompt_value` so that event handlers\n receive a ready-to-use ``dict | None`` instead of a raw string.\n \"\"\"\n return parse_prompt_value(payload)\n\n def process_example(self, value: Any) -> str | None: # noqa: ANN401 - Gradio override\n \"\"\"Return an HTML ```` tag for the ``gr.Examples`` gallery.\n\n ``gr.Examples`` calls ``process_example()`` (via ``as_example()``) to\n obtain display HTML for the examples gallery. The default\n implementation delegates to ``postprocess()``, which returns a JSON\n payload \u2014 resulting in raw JSON text in the gallery. This override\n produces a proper thumbnail instead.\n \"\"\"\n if value is None:\n return None\n image_source = value[0] if isinstance(value, tuple) else value\n img = _load_image(image_source)\n url = _save_image_to_cache(img, self.GRADIO_CACHE)\n safe_url = html.escape(url, quote=True)\n return f'\"example\"'\n\n def api_info(self) -> dict[str, Any]:\n return {\n \"type\": \"object\",\n \"description\": (\n \"JSON string with SAM prompter data. \"\n \"Input from JS: {imagePath?: string, imageSize?: {width, height}, \"\n \"prompts: [{points: [[x,y],...], labels: [1,0,...], boxes: [[x1,y1,x2,y2],...]},...]}. \"\n \"Output from Python: a plain image (str path, PIL Image, or ndarray) \"\n \"or a tuple (image, masks_list) where masks_list is \"\n \"[{rle: {counts: [int,...], size: [H,W]}, color: [R,G,B], alpha: float},...]. \"\n \"Serialized as {image: string, width: int, height: int, masks: [...], colors: [...]}\"\n ),\n }\n", "head": "", "repo_url": "https://github.com/hysts/gradio-sam-prompter" }