Skip to content

Commit 2e8b735

Browse files
committed
Added Brightness/Contrast Settings
1 parent fc714f7 commit 2e8b735

23 files changed

+2273
-275
lines changed

Assets/Scripts/Managers/ImageCropManager.cs

Lines changed: 2 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,11 @@ public class ImageCropManager : MonoBehaviour
2727
static Camera mainCam;
2828
static Texture2D warpedTexture;
2929

30-
//CropHandle[] cropHandles;
3130
public static Texture2D CroppingTexture { private set; get; }
3231

3332
void Awake()
3433
{
3534
if (!mainCam) mainCam = Camera.main;
36-
//cropHandles = FindObjectsOfType<CropHandle>();
3735
SetupFrame();
3836
}
3937

@@ -58,7 +56,6 @@ void SetupFrame()
5856
targetRatioFitter.aspectRatio = StreamManager.WebcamSize.x / (float)StreamManager.WebcamSize.y;
5957
}
6058
else Debug.LogError("Unable to capture the texture");
61-
//StartCoroutine(Capture(CroppingTexture = new Texture2D(Screen.width, Screen.height), new UnityEngine.Rect(0, 0, Screen.width, Screen.height)));
6259
}
6360

6461
IEnumerator Cropping()
@@ -68,21 +65,6 @@ IEnumerator Cropping()
6865
Vector2 rightBottomPos = GetResolutionBasedPosition(rightBottom.position);
6966
Vector2 rightTopPos = GetResolutionBasedPosition(rightTop.position);
7067

71-
//float minValX, minValY, maxValX, maxValY;
72-
//minValX = minValY = float.MaxValue;
73-
//maxValX = maxValY = float.MinValue;
74-
75-
//foreach (CropHandle handle in cropHandles)
76-
//{
77-
// Vector3 handlePos = handle.transform.position;
78-
79-
// if (handlePos.x < minValX) minValX = handlePos.x;
80-
// if (handlePos.y < minValY) minValY = handlePos.y;
81-
82-
// if (handlePos.x > maxValX) maxValX = handlePos.x;
83-
// if (handlePos.y > maxValY) maxValY = handlePos.y;
84-
//}
85-
8668
Mat mainMat = new Mat(StreamManager.WebcamSize.y, StreamManager.WebcamSize.x, CvType.CV_8UC3);
8769

8870
Utils.texture2DToMat(CroppingTexture, mainMat);
@@ -93,10 +75,6 @@ IEnumerator Cropping()
9375
new Point(rightBottomPos.x, StreamManager.WebcamSize.y - rightBottomPos.y),
9476
new Point(rightTopPos.x, StreamManager.WebcamSize.y - rightTopPos.y),
9577
new Point(leftTopPos.x, StreamManager.WebcamSize.y - leftTopPos.y),
96-
//new Point(leftBottom.position.x, StreamManager.WebcamSize.y - leftBottom.position.y),
97-
//new Point(rightBottom.position.x, StreamManager.WebcamSize.y - rightBottom.position.y),
98-
//new Point(rightTop.position.x, StreamManager.WebcamSize.y - rightTop.position.y),
99-
//new Point(leftTop.position.x, StreamManager.WebcamSize.y - leftTop.position.y),
10078
};
10179

10280
Mat srcPointsMat = Converters.vector_Point_to_Mat(srcPoints, CvType.CV_32F);
@@ -107,10 +85,6 @@ IEnumerator Cropping()
10785
new Point(CropSizeManager.CurrentDimmension.width, 0),
10886
new Point(CropSizeManager.CurrentDimmension.width, CropSizeManager.CurrentDimmension.height),
10987
new Point(0, CropSizeManager.CurrentDimmension.height),
110-
//new Point(0, 0),
111-
//new Point(StreamManager.WebcamSize.x, 0),
112-
//new Point(StreamManager.WebcamSize.x, StreamManager.WebcamSize.y),
113-
//new Point(0, StreamManager.WebcamSize.y),
11488
};
11589

11690
Mat dstPointsMat = Converters.vector_Point_to_Mat(dstPoints, CvType.CV_32F);
@@ -147,8 +121,7 @@ public void ColorEnhanced()
147121
Mat initMat = new Mat(CropSizeManager.CurrentDimmension.height, CropSizeManager.CurrentDimmension.width, CvType.CV_8UC3);
148122
Utils.texture2DToMat(warpedTexture, initMat);
149123

150-
initMat *= 1.25f;
151-
initMat += Scalar.all(15);
124+
initMat *= 1.15f;
152125

153126
Utils.matToTexture2D(initMat, warpedTexture);
154127
initMat.Dispose();
@@ -238,13 +211,4 @@ public void EdgedWhite()
238211
System.GC.Collect();
239212
filteredImage.gameObject.SetActive(true);
240213
}
241-
242-
//IEnumerator Capture(Texture2D capturedTexture, UnityEngine.Rect rect)
243-
//{
244-
// cropControlPanel.SetActive(false);
245-
// yield return new WaitForEndOfFrame();
246-
// capturedTexture.ReadPixels(rect, 0, 0);
247-
// capturedTexture.Apply();
248-
// cropControlPanel.SetActive(true);
249-
//}
250-
}
214+
}

Assets/Scripts/Managers/PreviewManager.cs

Lines changed: 85 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,27 @@ public class PreviewManager : MonoBehaviour
1414
public Sprite successSprite;
1515
public Sprite errorSprite;
1616

17+
[Header("Adjustments")]
18+
public GameObject contrastPanel;
19+
public GameObject brightnessPanel;
20+
public UnityEngine.UI.Text contrastText;
21+
public UnityEngine.UI.Text brightnessText;
22+
public Slider contrastSlider;
23+
public Slider brightnessSlider;
24+
25+
static Texture2D warpedTexture;
26+
27+
void OnDisable()
28+
{
29+
contrastSlider.value = brightnessSlider.value = 0;
30+
}
31+
1732
public void ShowPreview()
1833
{
1934
int width = filteredRawImage.mainTexture.width;
2035
int height = filteredRawImage.mainTexture.height;
2136

22-
Texture2D warpedTexture = new Texture2D(width, height, TextureFormat.RGB24, false);
37+
warpedTexture = new Texture2D(width, height, TextureFormat.RGB24, false);
2338
Graphics.CopyTexture(filteredRawImage.mainTexture, warpedTexture);
2439

2540
Mat warpedMat = new Mat(height, width, CvType.CV_8UC3);
@@ -35,6 +50,7 @@ public void ShowPreview()
3550

3651
warpedMat.Dispose();
3752
newTexture = null;
53+
warpedTexture = null;
3854
System.GC.Collect();
3955

4056
gameObject.SetActive(true);
@@ -48,7 +64,7 @@ public void RotateTexture(bool clockWise)
4864

4965
public void Sharpen()
5066
{
51-
Texture2D warpedTexture = new Texture2D(previewRawImage.mainTexture.width, previewRawImage.mainTexture.height, TextureFormat.RGB24, false);
67+
warpedTexture = new Texture2D(previewRawImage.mainTexture.width, previewRawImage.mainTexture.height, TextureFormat.RGB24, false);
5268
Graphics.CopyTexture(previewRawImage.texture, warpedTexture);
5369

5470
Mat initMat = new Mat(warpedTexture.height, warpedTexture.width, CvType.CV_8UC3);
@@ -63,11 +79,13 @@ public void Sharpen()
6379
initMat.Dispose();
6480
finalMat.Dispose();
6581
previewRawImage.texture = warpedTexture;
82+
warpedTexture = null;
83+
System.GC.Collect();
6684
}
6785

6886
public void SaveTextureToDisk()
6987
{
70-
Texture2D warpedTexture = new Texture2D(previewRawImage.mainTexture.width, previewRawImage.mainTexture.height, TextureFormat.RGB24, false);
88+
warpedTexture = new Texture2D(previewRawImage.mainTexture.width, previewRawImage.mainTexture.height, TextureFormat.RGB24, false);
7189
Graphics.CopyTexture(previewRawImage.texture, warpedTexture);
7290

7391
string path = GetStorageDirectory();
@@ -115,6 +133,70 @@ IEnumerator ShowDialog(bool saveSuccessful)
115133
saveDialog.SetActive(false);
116134
}
117135

136+
//public void UpdateContrast()
137+
//{
138+
// warpedTexture = new Texture2D(filteredRawImage.mainTexture.width, filteredRawImage.mainTexture.height, TextureFormat.RGB24, false);
139+
// Graphics.CopyTexture(filteredRawImage.texture, warpedTexture);
140+
141+
// Mat initMat = new Mat(warpedTexture.height, warpedTexture.width, CvType.CV_8UC3);
142+
// Utils.texture2DToMat(warpedTexture, initMat);
143+
144+
// int val = (int)contrastSlider.value;
145+
146+
// Debug.Log(1 + val / 10f);
147+
148+
// initMat *= (1 + val / 10f);
149+
150+
// Utils.matToTexture2D(initMat, warpedTexture);
151+
152+
// contrastText.text = string.Format("Contrast +{0}0%", val);
153+
154+
// initMat.Dispose();
155+
// previewRawImage.texture = warpedTexture;
156+
// warpedTexture = null;
157+
// System.GC.Collect();
158+
//}
159+
160+
public void UpdateContrastBrightness()
161+
{
162+
warpedTexture = new Texture2D(filteredRawImage.mainTexture.width, filteredRawImage.mainTexture.height, TextureFormat.RGB24, false);
163+
Graphics.CopyTexture(filteredRawImage.texture, warpedTexture);
164+
165+
Mat initMat = new Mat(warpedTexture.height, warpedTexture.width, CvType.CV_8UC3);
166+
Utils.texture2DToMat(warpedTexture, initMat);
167+
168+
float brightnessVal = brightnessSlider.value * 5;
169+
float contrastVal = 1 + contrastSlider.value / 10f;
170+
171+
Debug.Log(brightnessVal);
172+
Debug.Log(contrastVal);
173+
174+
initMat += Scalar.all(brightnessVal);
175+
initMat *= (contrastVal);
176+
177+
Utils.matToTexture2D(initMat, warpedTexture);
178+
179+
brightnessText.text = string.Format("Brightness +{0}", brightnessVal);
180+
contrastText.text = string.Format("Contrast +{0}0%", contrastVal);
181+
182+
initMat.Dispose();
183+
previewRawImage.texture = warpedTexture;
184+
warpedTexture = null;
185+
System.GC.Collect();
186+
}
187+
188+
public void ToggleContrast()
189+
{
190+
brightnessPanel.SetActive(false);
191+
contrastPanel.SetActive(!contrastPanel.activeInHierarchy);
192+
}
193+
194+
public void ToggleBrightness()
195+
{
196+
contrastPanel.SetActive(false);
197+
brightnessPanel.SetActive(!brightnessPanel.activeInHierarchy);
198+
}
199+
118200
void ScanMedia(string fileName)
119201
{
120202
if (Application.platform != RuntimePlatform.Android)
4.88 KB
Loading

Assets/Sprites/Button_Sprites/brightness.png.meta

Lines changed: 110 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Sprites/Button_Sprites/capture.png.meta

Lines changed: 22 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
-5.86 KB
Loading

Assets/Sprites/Button_Sprites/close.png.meta

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
7.52 KB
Loading

0 commit comments

Comments
 (0)