Spaces:
Running
Running
Download script.js from sendax/pixel-perfection-wizard: direct link, hf CLI and curl.
- Browser
- Download file 11.8 kB
-
https://huggingface.co/spaces/sendax/pixel-perfection-wizard/resolve/main/script.js
- Command line
-
hf download hf://spaces/sendax/pixel-perfection-wizard/script.js
-
curl -L -o script.js https://huggingface.co/spaces/sendax/pixel-perfection-wizard/resolve/main/script.js
11.8 kB
| document.addEventListener('DOMContentLoaded', function() { | |
| // DOM Elements | |
| const imageUpload = document.getElementById('imageUpload'); | |
| const originalPreview = document.getElementById('originalPreview'); | |
| const originalImage = document.getElementById('originalImage'); | |
| const editedPreview = document.getElementById('editedPreview'); | |
| const editedImage = document.getElementById('editedImage'); | |
| const emptyEdited = document.getElementById('emptyEdited'); | |
| const processingOverlay = document.getElementById('processingOverlay'); | |
| const hdrIntensity = document.getElementById('hdrIntensity'); | |
| const hdrValue = document.getElementById('hdrValue'); | |
| const brightness = document.getElementById('brightness'); | |
| const brightnessValue = document.getElementById('brightnessValue'); | |
| const contrast = document.getElementById('contrast'); | |
| const contrastValue = document.getElementById('contrastValue'); | |
| const saturation = document.getElementById('saturation'); | |
| const saturationValue = document.getElementById('saturationValue'); | |
| const temperature = document.getElementById('temperature'); | |
| const temperatureValue = document.getElementById('temperatureValue'); | |
| const applyBtn = document.getElementById('applyBtn'); | |
| const savePresetBtn = document.getElementById('savePresetBtn'); | |
| const resetBtn = document.getElementById('resetBtn'); | |
| const downloadBtn = document.getElementById('downloadBtn'); | |
| let originalImageData = null; | |
| let canvas = document.createElement('canvas'); | |
| let ctx = canvas.getContext('2d'); | |
| let currentImageData = null; | |
| // Event Listeners | |
| imageUpload.addEventListener('change', handleImageUpload); | |
| hdrIntensity.addEventListener('input', function() { | |
| updateHDRValue(); | |
| if (originalImageData) applyHDREffect(); | |
| }); | |
| brightness.addEventListener('input', function() { | |
| updateBrightnessValue(); | |
| if (originalImageData) applyHDREffect(); | |
| }); | |
| contrast.addEventListener('input', function() { | |
| updateContrastValue(); | |
| if (originalImageData) applyHDREffect(); | |
| }); | |
| saturation.addEventListener('input', function() { | |
| updateSaturationValue(); | |
| if (originalImageData) applyHDREffect(); | |
| }); | |
| temperature.addEventListener('input', function() { | |
| updateTemperatureValue(); | |
| if (originalImageData) applyHDREffect(); | |
| }); | |
| savePresetBtn.addEventListener('click', savePreset); | |
| resetBtn.addEventListener('click', resetControls); | |
| // Save image functionality | |
| const downloadBtn = document.getElementById('downloadBtn'); | |
| const saveOptions = document.getElementById('saveOptions'); | |
| const quickSaveBtn = document.getElementById('quickSaveBtn'); | |
| const saveWithNameBtn = document.getElementById('saveWithNameBtn'); | |
| downloadBtn.addEventListener('click', function(e) { | |
| e.preventDefault(); | |
| e.stopPropagation(); | |
| saveOptions.classList.toggle('hidden'); | |
| }); | |
| quickSaveBtn.addEventListener('click', function(e) { | |
| e.preventDefault(); | |
| downloadImage(); | |
| saveOptions.classList.add('hidden'); | |
| }); | |
| saveWithNameBtn.addEventListener('click', function(e) { | |
| e.preventDefault(); | |
| saveImageWithName(); | |
| saveOptions.classList.add('hidden'); | |
| }); | |
| // Close dropdown when clicking elsewhere | |
| document.addEventListener('click', function(e) { | |
| if (!downloadBtn.contains(e.target) && !saveOptions.contains(e.target)) { | |
| saveOptions.classList.add('hidden'); | |
| } | |
| }); | |
| // Drag and drop functionality | |
| const dropArea = document.querySelector('.border-dashed'); | |
| ['dragenter', 'dragover', 'dragleave', 'drop'].forEach(eventName => { | |
| dropArea.addEventListener(eventName, preventDefaults, false); | |
| }); | |
| function preventDefaults(e) { | |
| e.preventDefault(); | |
| e.stopPropagation(); | |
| } | |
| ['dragenter', 'dragover'].forEach(eventName => { | |
| dropArea.addEventListener(eventName, highlight, false); | |
| }); | |
| ['dragleave', 'drop'].forEach(eventName => { | |
| dropArea.addEventListener(eventName, unhighlight, false); | |
| }); | |
| function highlight() { | |
| dropArea.classList.add('border-blue-500', 'bg-blue-50'); | |
| } | |
| function unhighlight() { | |
| dropArea.classList.remove('border-blue-500', 'bg-blue-50'); | |
| } | |
| dropArea.addEventListener('drop', handleDrop, false); | |
| function handleDrop(e) { | |
| const dt = e.dataTransfer; | |
| const files = dt.files; | |
| if (files.length > 0 && files[0].type.match('image.*')) { | |
| imageUpload.files = files; | |
| handleImageUpload({ target: imageUpload }); | |
| } | |
| } | |
| // Functions | |
| function handleImageUpload(e) { | |
| const file = e.target.files[0]; | |
| if (!file || !file.type.match('image.*')) { | |
| alert('Please select a valid image file (JPEG, PNG)'); | |
| return; | |
| } | |
| const reader = new FileReader(); | |
| reader.onload = function(event) { | |
| originalImage.src = event.target.result; | |
| editedImage.src = event.target.result; | |
| originalImageData = event.target.result; | |
| currentImageData = event.target.result; | |
| originalPreview.classList.remove('hidden'); | |
| editedPreview.classList.remove('hidden'); | |
| emptyEdited.classList.add('hidden'); | |
| // Enable apply button | |
| applyBtn.disabled = false; | |
| applyBtn.classList.remove('opacity-50', 'cursor-not-allowed'); | |
| // Reset controls | |
| resetControls(); | |
| }; | |
| reader.onerror = function() { | |
| alert('Error reading the image file. Please try another image.'); | |
| }; | |
| reader.readAsDataURL(file); | |
| } | |
| function updateHDRValue() { | |
| hdrValue.textContent = `${hdrIntensity.value}%`; | |
| } | |
| function updateBrightnessValue() { | |
| brightnessValue.textContent = `${brightness.value}%`; | |
| } | |
| function updateContrastValue() { | |
| contrastValue.textContent = `${contrast.value}%`; | |
| } | |
| function updateSaturationValue() { | |
| saturationValue.textContent = `${saturation.value}%`; | |
| } | |
| function updateTemperatureValue() { | |
| temperatureValue.textContent = temperature.value; | |
| } | |
| function applyHDREffect() { | |
| if (!originalImageData) return; | |
| processingOverlay.classList.remove('hidden'); | |
| const img = new Image(); | |
| img.onload = function() { | |
| canvas.width = img.width; | |
| canvas.height = img.height; | |
| // Draw original image | |
| ctx.drawImage(img, 0, 0); | |
| // Get image data | |
| const imageData = ctx.getImageData(0, 0, canvas.width, canvas.height); | |
| const data = imageData.data; | |
| // Apply HDR effect (simplified version) | |
| const intensity = hdrIntensity.value / 100; | |
| const bright = brightness.value / 100; | |
| const cont = contrast.value / 100; | |
| const sat = saturation.value / 100; | |
| const temp = temperature.value / 100; | |
| for (let i = 0; i < data.length; i += 4) { | |
| // Brightness adjustment | |
| data[i] = data[i] + (255 * bright); | |
| data[i + 1] = data[i + 1] + (255 * bright); | |
| data[i + 2] = data[i + 2] + (255 * bright); | |
| // Contrast adjustment | |
| const factor = (259 * (cont + 255)) / (255 * (259 - cont)); | |
| data[i] = factor * (data[i] - 128) + 128; | |
| data[i + 1] = factor * (data[i + 1] - 128) + 128; | |
| data[i + 2] = factor * (data[i + 2] - 128) + 128; | |
| // Saturation adjustment | |
| if (sat !== 0) { | |
| const gray = 0.2989 * data[i] + 0.5870 * data[i + 1] + 0.1140 * data[i + 2]; | |
| data[i] = gray + (data[i] - gray) * (1 + sat); | |
| data[i + 1] = gray + (data[i + 1] - gray) * (1 + sat); | |
| data[i + 2] = gray + (data[i + 2] - gray) * (1 + sat); | |
| } | |
| // Temperature adjustment (warm/cool) | |
| if (temp !== 0) { | |
| data[i] = data[i] * (1 + (temp > 0 ? temp * 0.5 : 0)); // red | |
| data[i + 2] = data[i + 2] * (1 + (temp < 0 ? -temp * 0.5 : 0)); // blue | |
| } | |
| // Simple HDR effect (increase saturation and contrast) | |
| if (intensity > 0) { | |
| const avg = (data[i] + data[i + 1] + data[i + 2]) / 3; | |
| data[i] = avg + (data[i] - avg) * (1 + intensity); | |
| data[i + 1] = avg + (data[i + 1] - avg) * (1 + intensity); | |
| data[i + 2] = avg + (data[i + 2] - avg) * (1 + intensity); | |
| } | |
| } | |
| ctx.putImageData(imageData, 0, 0); | |
| const result = canvas.toDataURL('image/jpeg', 0.9); | |
| editedImage.src = result; | |
| currentImageData = result; | |
| processingOverlay.classList.add('hidden'); | |
| }; | |
| img.src = originalImageData; | |
| }, 100); | |
| } | |
| function resetControls() { | |
| hdrIntensity.value = 50; | |
| brightness.value = 0; | |
| contrast.value = 0; | |
| saturation.value = 0; | |
| temperature.value = 0; | |
| updateHDRValue(); | |
| updateBrightnessValue(); | |
| updateContrastValue(); | |
| updateSaturationValue(); | |
| updateTemperatureValue(); | |
| if (originalImageData) { | |
| editedImage.src = originalImageData; | |
| } | |
| } | |
| function downloadImage(filename = 'hdr-enhanced-photo.jpg') { | |
| if (!currentImageData || currentImageData === originalImageData) { | |
| alert('Please upload and edit an image first!'); | |
| return; | |
| } | |
| try { | |
| const link = document.createElement('a'); | |
| link.download = filename; | |
| link.href = currentImageData; | |
| document.body.appendChild(link); | |
| link.click(); | |
| document.body.removeChild(link); | |
| } catch (error) { | |
| console.error('Download error:', error); | |
| alert('Error downloading image. Please try again.'); | |
| } | |
| } | |
| function saveImageWithName() { | |
| if (!editedImage.src || editedImage.src === originalImageData) { | |
| alert('Please upload and edit an image first!'); | |
| return; | |
| } | |
| const filename = prompt('Enter a name for your image (without extension):', 'hdr-enhanced-photo'); | |
| if (filename) { | |
| // Ensure filename has .jpg extension | |
| const finalFilename = filename.endsWith('.jpg') ? filename : `${filename}.jpg`; | |
| downloadImage(finalFilename); | |
| } | |
| } | |
| function savePreset() { | |
| const preset = { | |
| hdrIntensity: hdrIntensity.value, | |
| brightness: brightness.value, | |
| contrast: contrast.value, | |
| saturation: saturation.value, | |
| temperature: temperature.value, | |
| timestamp: new Date().toISOString() | |
| }; | |
| // In a real app, you would save to localStorage or backend | |
| alert(`Preset saved!\nHDR: ${preset.hdrIntensity}%\nBrightness: ${preset.brightness}%\nContrast: ${preset.contrast}%\nSaturation: ${preset.saturation}%\nTemperature: ${preset.temperature}`); | |
| } | |
| // Initialize values | |
| updateHDRValue(); | |
| updateBrightnessValue(); | |
| updateContrastValue(); | |
| updateSaturationValue(); | |
| updateTemperatureValue(); | |
| }); |