using UnityEngine; using UnityEngine.UI; /// /// Ajustements visuels en temps réel de l'image IA (brightness, contrast, saturation). /// /// Setup Inspector : /// • _targetImage → le RawImage du panneau résultat /// • Slider Brightness : onValueChanged → Brightness(float) | min=0 max=2 value=1 /// • Slider Contrast : onValueChanged → Contrast(float) | min=0 max=2 value=1 /// • Slider Color (sat) : onValueChanged → Color(float) | min=0 max=2 value=1 /// /// OpenAIImageEnhancer appelle SetSourceTexture() automatiquement après chaque image IA. /// public class SettingImageEnhance : MonoBehaviour { [Header("Cible")] [SerializeField] private RawImage _targetImage; // Texture originale non modifiée — référence pour recalculer depuis 0 private Texture2D _originalTex; // Texture de travail affichée (modifiée à chaque réglage) private Texture2D _editTex; // Valeurs courantes des réglages (neutre = 1) private float _brightness = 1f; private float _contrast = 1f; private float _saturation = 1f; // ───────────────────────────────────────────────────────────────────────── // API — appelée par OpenAIImageEnhancer.ShowResultWindow() // ───────────────────────────────────────────────────────────────────────── public void SetSourceTexture(Texture2D tex) { if (tex == null) return; if (_originalTex != null) { Destroy(_originalTex); _originalTex = null; } if (_editTex != null) { Destroy(_editTex); _editTex = null; } // Copie originale immuable _originalTex = new Texture2D(tex.width, tex.height, TextureFormat.RGBA32, false); _originalTex.SetPixels32(tex.GetPixels32()); _originalTex.Apply(); // Copie de travail affichée _editTex = new Texture2D(tex.width, tex.height, TextureFormat.RGBA32, false); _editTex.SetPixels32(tex.GetPixels32()); _editTex.Apply(); if (_targetImage != null) _targetImage.texture = _editTex; // Reset des réglages _brightness = 1f; _contrast = 1f; _saturation = 1f; Debug.Log($"[ImageEnhance] Source chargée : {tex.width}×{tex.height}"); } // ───────────────────────────────────────────────────────────────────────── // Réglages — connecter à Slider.onValueChanged (mode Dynamic float) // ───────────────────────────────────────────────────────────────────────── /// Luminosité — 0 = noir | 1 = normal | 2 = très lumineux public void Brightness(float value) { _brightness = value; ApplyAdjustments(); } /// Contraste — 0 = gris plat | 1 = normal | 2 = fort contraste public void Contrast(float value) { _contrast = value; ApplyAdjustments(); } /// Saturation — 0 = niveaux de gris | 1 = normal | 2 = couleurs vives public void Color(float value) { _saturation = value; ApplyAdjustments(); } /// Retourne les pixels ajustés encodés en PNG — utilisé par OpenAIImageEnhancer avant envoi IA. public byte[] GetEditedBytes() { if (_editTex == null) return null; return _editTex.EncodeToPNG(); } // ───────────────────────────────────────────────────────────────────────── // Traitement — applique brightness + contrast + saturation en un seul pass // ───────────────────────────────────────────────────────────────────────── private void ApplyAdjustments() { if (_originalTex == null || _editTex == null) return; Color32[] src = _originalTex.GetPixels32(); for (int i = 0; i < src.Length; i++) { float r = src[i].r / 255f; float g = src[i].g / 255f; float b = src[i].b / 255f; // 1. Brightness : multiplie chaque canal r *= _brightness; g *= _brightness; b *= _brightness; // 2. Contrast : pivote autour de 0.5 r = (r - 0.5f) * _contrast + 0.5f; g = (g - 0.5f) * _contrast + 0.5f; b = (b - 0.5f) * _contrast + 0.5f; // 3. Saturation : mélange vers la luminance perceptuelle float lum = 0.299f * r + 0.587f * g + 0.114f * b; r = lum + (r - lum) * _saturation; g = lum + (g - lum) * _saturation; b = lum + (b - lum) * _saturation; src[i].r = Clamp255(r); src[i].g = Clamp255(g); src[i].b = Clamp255(b); // alpha inchangé } _editTex.SetPixels32(src); _editTex.Apply(); } private static byte Clamp255(float v) => (byte)Mathf.Clamp(v * 255f, 0f, 255f); private void OnDestroy() { if (_originalTex != null) { Destroy(_originalTex); _originalTex = null; } if (_editTex != null) { Destroy(_editTex); _editTex = null; } } }