-
Notifications
You must be signed in to change notification settings - Fork 1
/
SphereEffectForImage.cs
96 lines (73 loc) · 3.13 KB
/
SphereEffectForImage.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
84
85
86
87
88
89
90
91
92
93
94
95
96
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 = SphereEffectForImage(InputImage);
OutputImage.Save(@"C:\Users\david.rajkumar\Documents\Visual Studio 2015\Projects\David\ImageProcessing\Output\SphereEffectForImage.jpg");
}
public Bitmap SphereEffectForImage(Bitmap Image)
{
int ImageWidth = Image.Width;
int ImageHeight = Image.Height;
int ImageStride = 0;
Bitmap TempBmp = (Bitmap)Image.Clone();
BitmapData ImageData = Image.LockBits(new Rectangle(0, 0, ImageWidth, ImageHeight), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
BitmapData TempBmpData = TempBmp.LockBits(new Rectangle(0, 0, TempBmp.Width, TempBmp.Height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
ImageStride = ImageData.Stride;
unsafe
{
byte* Ptr = (byte*)ImageData.Scan0.ToPointer();
byte* TempPtr = (byte*)TempBmpData.Scan0.ToPointer();
int StopPtr = (int)Ptr + ImageStride * ImageHeight;
int Val = 0;
int MidX = ImageWidth / 2;
int MidY = ImageHeight / 2;
int i = 0, X = 0, Y = 0;
int TrueX = 0, TrueY = 0;
int NewX = 0, NewY = 0;
double NewRadius = 0;
double Theta = 0, Radius = 0;
while ((int)Ptr != StopPtr)
{
X = i % ImageWidth;
Y = i / ImageWidth;
TrueX = X - MidX;
TrueY = Y - MidY;
Theta = Math.Atan2(TrueY, TrueX);
Radius = Math.Sqrt(TrueX * TrueX + TrueY * TrueY);
NewRadius = Radius * Radius / Math.Max(MidX, MidY);
NewX = (int)(MidX + (NewRadius * Math.Cos(Theta)));
NewY = (int)(MidY + (NewRadius * Math.Sin(Theta)));
if (!(NewY >= 0 && NewY < ImageHeight && NewX >= 0 && NewX < ImageWidth))
NewX = NewY = 0;
if (NewY >= 0 && NewY < ImageHeight && NewX >= 0 && NewX < ImageWidth)
{
Val = (NewY * ImageStride) + (NewX * 3);
Ptr[0] = TempPtr[Val];
Ptr[1] = TempPtr[Val + 1];
Ptr[2] = TempPtr[Val + 2];
}
Ptr += 3;
i++;
}
}
Image.UnlockBits(ImageData);
TempBmp.UnlockBits(TempBmpData);
return Image;
}
}
}