diff --git a/content/collections/tools/object-eraser.json b/content/collections/tools/object-eraser.json index 040745c..0221c58 100644 --- a/content/collections/tools/object-eraser.json +++ b/content/collections/tools/object-eraser.json @@ -17,14 +17,13 @@ "pageType": "masking", "showThumbnailsSection": false, "thumbnailPlacement": "after-upload", - "twoImagesConfig": { - "firstImageLabel": { - "en": "1. Primary Subject Photo", - "hi": "1. मुख्य विषय फोटो" - }, - "secondImageLabel": { - "en": "2. Reference / Target Photo", - "hi": "2. संदर्भ / लक्ष्य फोटो" + "maskingConfig": { + "maskParamName": "mask", + "imageParamName": "image", + "defaultBrushSize": 32, + "allowCustomMaskUpload": true, + "autoMask": { + "enabled": false } }, "tryIt": { diff --git a/management/pages/tools/object-eraser/tool.config.json b/management/pages/tools/object-eraser/tool.config.json index c63e0fd..0221c58 100644 --- a/management/pages/tools/object-eraser/tool.config.json +++ b/management/pages/tools/object-eraser/tool.config.json @@ -14,17 +14,16 @@ "homeApp": "bg-cleaner-app", "alsoIn": [], "featured": false, - "pageType": "two-images", + "pageType": "masking", "showThumbnailsSection": false, "thumbnailPlacement": "after-upload", - "twoImagesConfig": { - "firstImageLabel": { - "en": "1. Primary Subject Photo", - "hi": "1. मुख्य विषय फोटो" - }, - "secondImageLabel": { - "en": "2. Reference / Target Photo", - "hi": "2. संदर्भ / लक्ष्य फोटो" + "maskingConfig": { + "maskParamName": "mask", + "imageParamName": "image", + "defaultBrushSize": 32, + "allowCustomMaskUpload": true, + "autoMask": { + "enabled": false } }, "tryIt": { diff --git a/src/components/tools/DynamicToolWorkspace.tsx b/src/components/tools/DynamicToolWorkspace.tsx index e28b4f2..2c0ccf7 100644 --- a/src/components/tools/DynamicToolWorkspace.tsx +++ b/src/components/tools/DynamicToolWorkspace.tsx @@ -14,7 +14,10 @@ import { RefreshCw, FolderOpen, Zap, - Crop + Crop, + Camera, + CheckCircle2, + Trash2 } from 'lucide-react'; import { ToolItem, @@ -165,9 +168,11 @@ export function DynamicToolWorkspace({ setResultUrl(null); setErrorMessage(''); setStatus('idle'); - setViewMode('studio'); // Transition straight into the editing workspace! - if (typeof window !== 'undefined') { - window.scrollTo({ top: 0, behavior: 'smooth' }); + if (pageType !== 'two-images') { + setViewMode('studio'); // Transition straight into the editing workspace! + if (typeof window !== 'undefined') { + window.scrollTo({ top: 0, behavior: 'smooth' }); + } } }; @@ -177,8 +182,14 @@ export function DynamicToolWorkspace({ setErrorMessage('Please upload a valid JPG, PNG, or WebP photo.'); return; } + const maxBytes = (tool.tryIt?.limits?.maxMB || 12) * 1024 * 1024; + if (file.size > maxBytes) { + setErrorMessage(`File exceeds max size of ${tool.tryIt?.limits?.maxMB || 12}MB.`); + return; + } setSecondaryFile(file); setSecondaryPreview(URL.createObjectURL(file)); + setErrorMessage(''); }; const handleLoadSample = (sampleUrl: string) => { @@ -272,14 +283,18 @@ export function DynamicToolWorkspace({ // 3. Mask Image (for masking archetype) if (pageType === 'masking') { const maskParamName = tool.maskingConfig?.maskParamName || 'mask'; + let maskBlob: Blob | null = null; if (getMaskBlobRef.current) { - const maskBlob = await getMaskBlobRef.current(); - if (maskBlob) { - formData.append(maskParamName, maskBlob, 'mask.png'); - if (maskParamName !== 'mask') { - formData.append('mask', maskBlob, 'mask.png'); - } - } + maskBlob = await getMaskBlobRef.current(); + } + if (!maskBlob || !hasMaskDrawn) { + setStatus('error'); + setErrorMessage('Please paint over the object or upload a custom mask to erase.'); + return; + } + formData.append(maskParamName, maskBlob, 'mask.png'); + if (maskParamName !== 'mask') { + formData.append('mask', maskBlob, 'mask.png'); } } @@ -526,12 +541,24 @@ export function DynamicToolWorkspace({ {pageType === 'masking' && primaryPreview && (
- - Inpainting & Object Masking Studio - - - Paint over areas to remove or replace - +
+ + Inpainting & Object Masking Studio + + + — Paint over areas with brush or upload custom mask + +
+
+ +
{/* Primary Image Slot */}
- - {resolveLocalised(tool.twoImagesConfig?.firstImageLabel, locale) || 'Primary Photo'} - -
+
+ + {resolveLocalised(tool.twoImagesConfig?.firstImageLabel, locale) || '1. Primary Photo'} + + {primaryPreview && ( + + )} +
+ { + if (e.target.files?.[0]) handlePrimaryFileSelect(e.target.files[0]); + }} + /> +
{primaryPreview ? ( - // eslint-disable-next-line @next/next/no-img-element - Primary + <> + {/* eslint-disable-next-line @next/next/no-img-element */} + Primary + + ) : ( -
+
fileInputRef.current?.click()} + className="w-full h-full flex flex-col items-center justify-center p-4 text-muted text-xs cursor-pointer hover:bg-paper transition-colors" + > Upload primary photo
@@ -642,27 +703,52 @@ export function DynamicToolWorkspace({ {/* Secondary Image Slot */}
- - {resolveLocalised(tool.twoImagesConfig?.secondImageLabel, locale) || 'Reference / Style Photo'} - -
+
+ + {resolveLocalised(tool.twoImagesConfig?.secondImageLabel, locale) || '2. Reference / Style Photo'} + + {secondaryPreview && ( + + )} +
+ { + if (e.target.files?.[0]) handleSecondaryFileSelect(e.target.files[0]); + }} + /> +
{secondaryPreview ? ( - // eslint-disable-next-line @next/next/no-img-element - Secondary + <> + {/* eslint-disable-next-line @next/next/no-img-element */} + Secondary + + ) : ( -
@@ -909,47 +995,117 @@ export function DynamicToolWorkspace({ {pageType === 'two-images' && (
- {/* Image 1 Dropzone */} -
fileInputRef.current?.click()} - className="p-5 rounded-2xl border-2 border-dashed border-rule hover:border-accent bg-paper hover:bg-accent/5 transition-all cursor-pointer flex flex-col items-center justify-center text-center space-y-2 min-h-[160px]" - > - { - if (e.target.files?.[0]) handlePrimaryFileSelect(e.target.files[0]); - }} - /> - - - {resolveLocalised(tool.twoImagesConfig?.firstImageLabel, locale) || '1. Source Face'} - - Click to choose -
+ {/* Image 1 Slot */} + { + if (e.target.files?.[0]) handlePrimaryFileSelect(e.target.files[0]); + }} + /> + {primaryPreview ? ( +
+
+ + + {resolveLocalised(tool.twoImagesConfig?.firstImageLabel, locale) || '1. Primary Photo'} + + +
+
+ {/* eslint-disable-next-line @next/next/no-img-element */} + Primary preview +
+ +
+ ) : ( +
fileInputRef.current?.click()} + className="p-5 rounded-2xl border-2 border-dashed border-rule hover:border-accent bg-paper hover:bg-accent/5 transition-all cursor-pointer flex flex-col items-center justify-center text-center space-y-2 min-h-[180px]" + > + + + {resolveLocalised(tool.twoImagesConfig?.firstImageLabel, locale) || '1. Primary Photo'} + + Click to choose image +
+ )} - {/* Image 2 Dropzone */} -
secondaryFileInputRef.current?.click()} - className="p-5 rounded-2xl border-2 border-dashed border-rule hover:border-accent bg-paper hover:bg-accent/5 transition-all cursor-pointer flex flex-col items-center justify-center text-center space-y-2 min-h-[160px]" - > - { - if (e.target.files?.[0]) handleSecondaryFileSelect(e.target.files[0]); - }} - /> - - - {resolveLocalised(tool.twoImagesConfig?.secondImageLabel, locale) || '2. Target Body/Photo'} - - Click to choose -
+ {/* Image 2 Slot */} + { + if (e.target.files?.[0]) handleSecondaryFileSelect(e.target.files[0]); + }} + /> + {secondaryPreview ? ( +
+
+ + + {resolveLocalised(tool.twoImagesConfig?.secondImageLabel, locale) || '2. Reference Photo'} + + +
+
+ {/* eslint-disable-next-line @next/next/no-img-element */} + Secondary preview +
+ +
+ ) : ( +
secondaryFileInputRef.current?.click()} + className="p-5 rounded-2xl border-2 border-dashed border-rule hover:border-accent bg-paper hover:bg-accent/5 transition-all cursor-pointer flex flex-col items-center justify-center text-center space-y-2 min-h-[180px]" + > + + + {resolveLocalised(tool.twoImagesConfig?.secondImageLabel, locale) || '2. Reference Photo'} + + Click to choose image +
+ )}
)} diff --git a/src/components/tools/MaskCanvasEditor.tsx b/src/components/tools/MaskCanvasEditor.tsx index 44866cc..39e032e 100644 --- a/src/components/tools/MaskCanvasEditor.tsx +++ b/src/components/tools/MaskCanvasEditor.tsx @@ -11,7 +11,8 @@ import { Eye, EyeOff, RotateCcw, - Maximize2 + Maximize2, + UploadCloud } from 'lucide-react'; import { MaskingConfig, resolveLocalised } from '@/types'; import { ProcessingOverlay } from './ProcessingOverlay'; @@ -38,6 +39,7 @@ export function MaskCanvasEditor({ const containerRef = useRef(null); const imageRef = useRef(null); const maskCanvasRef = useRef(null); + const maskFileInputRef = useRef(null); const [tool, setTool] = useState<'brush' | 'eraser'>('brush'); const [brushSize, setBrushSize] = useState(config?.defaultBrushSize || 32); @@ -169,6 +171,85 @@ export function MaskCanvasEditor({ saveHistory(); }; + // Upload and parse custom mask file (supports B&W or transparent PNG) + const handleCustomMaskUpload = (e: React.ChangeEvent) => { + const file = e.target.files?.[0]; + if (!file) return; + + const reader = new FileReader(); + reader.onload = () => { + const img = new Image(); + img.onload = () => { + const canvas = maskCanvasRef.current; + if (!canvas) return; + + const ctx = canvas.getContext('2d', { willReadFrequently: true }); + if (!ctx) return; + + // Sample and scale uploaded mask to match base canvas dimensions + const tempCanvas = document.createElement('canvas'); + tempCanvas.width = canvas.width; + tempCanvas.height = canvas.height; + const tempCtx = tempCanvas.getContext('2d', { willReadFrequently: true }); + if (!tempCtx) return; + + tempCtx.drawImage(img, 0, 0, canvas.width, canvas.height); + const uploadedData = tempCtx.getImageData(0, 0, canvas.width, canvas.height); + const uD = uploadedData.data; + + // Check if uploaded mask has alpha transparency + let hasAlphaVariation = false; + for (let i = 0; i < uD.length; i += 16) { + if (uD[i + 3] < 240) { + hasAlphaVariation = true; + break; + } + } + + // Apply interactive red overlay on canvas + ctx.clearRect(0, 0, canvas.width, canvas.height); + const maskOverlay = ctx.createImageData(canvas.width, canvas.height); + const mD = maskOverlay.data; + + for (let i = 0; i < uD.length; i += 4) { + const r = uD[i]; + const g = uD[i + 1]; + const b = uD[i + 2]; + const a = uD[i + 3]; + + let isMasked = false; + if (hasAlphaVariation) { + // Non-transparent pixels count as mask + isMasked = a > 40; + } else { + // Solid B&W mask: bright/white pixels (> 50 brightness) count as mask + const brightness = 0.299 * r + 0.587 * g + 0.114 * b; + isMasked = brightness > 50; + } + + if (isMasked) { + mD[i] = 239; // R + mD[i + 1] = 68; // G + mD[i + 2] = 68; // B + mD[i + 3] = 160; // Alpha + } else { + mD[i + 3] = 0; + } + } + + ctx.putImageData(maskOverlay, 0, 0); + saveHistory(); + setHasDrawnMask(true); + onMaskChange?.(true); + + // Reset input value so re-selecting same file triggers onChange + e.target.value = ''; + }; + img.src = reader.result as string; + }; + reader.readAsDataURL(file); + }; + // Convert canvas pointer coordinate to natural canvas pixels const getCanvasCoords = (e: React.PointerEvent) => { const canvas = maskCanvasRef.current; @@ -213,7 +294,9 @@ export function MaskCanvasEditor({ const handlePointerDown = (e: React.PointerEvent) => { if (e.button !== 0 && e.pointerType === 'mouse') return; - e.currentTarget.setPointerCapture(e.pointerId); + try { + e.currentTarget.setPointerCapture(e.pointerId); + } catch {} setIsDrawing(true); const { x, y } = getCanvasCoords(e); @@ -396,6 +479,24 @@ export function MaskCanvasEditor({ Eraser (Sub) + {/* Upload Custom Mask Button */} + + + {/* Auto-Mask Detection Button (if configured) */} {config?.autoMask?.enabled !== false && (