-
Notifications
You must be signed in to change notification settings - Fork 1
/
SetContrastForImage.cs
83 lines (65 loc) · 2.52 KB
/
SetContrastForImage.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Windows.Forms;
namespace ImageProcessing
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Bitmap InputImage;
Bitmap OutputImage;
InputImage = (Bitmap)Image.FromFile(@"C:\Users\david.rajkumar\Documents\Visual Studio 2015\Projects\David\ImageProcessing\Input\Image.jpg");
OutputImage = SetContrastForImage(InputImage, 50);
OutputImage.Save(@"C:\Users\david.rajkumar\Documents\Visual Studio 2015\Projects\David\ImageProcessing\Output\SetContrastForImage.jpg");
}
public Bitmap SetContrastForImage(Bitmap Image, sbyte Contrast)
{
if (Contrast < -100 || Contrast > 100) return null;
BitmapData ImageData = Image.LockBits(new Rectangle(0, 0, Image.Width, Image.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
unsafe
{
byte* Ptr = (byte*)ImageData.Scan0.ToPointer();
int StopPtr = (int)Ptr + ImageData.Stride * ImageData.Height;
double pixel = 0, contrast = (100.0 + Contrast) / 100.0;
contrast *= contrast;
while ((int)Ptr != StopPtr)
{
pixel = Ptr[0] / 255.0;
pixel -= 0.5;
pixel *= contrast;
pixel += 0.5;
pixel *= 255;
if (pixel < 0) pixel = 0;
else if (pixel > 255) pixel = 255;
Ptr[0] = (byte)pixel;
pixel = Ptr[1] / 255.0;
pixel -= 0.5;
pixel *= contrast;
pixel += 0.5;
pixel *= 255;
if (pixel < 0) pixel = 0;
else if (pixel > 255) pixel = 255;
Ptr[1] = (byte)pixel;
pixel = Ptr[2] / 255.0;
pixel -= 0.5;
pixel *= contrast;
pixel += 0.5;
pixel *= 255;
if (pixel < 0) pixel = 0;
else if (pixel > 255) pixel = 255;
Ptr[2] = (byte)pixel;
Ptr += 3;
}
}
Image.UnlockBits(ImageData);
return Image;
}
}
}