-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathProgram.cs
More file actions
126 lines (112 loc) · 5.28 KB
/
Program.cs
File metadata and controls
126 lines (112 loc) · 5.28 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
namespace CamTimer {
static class Program {
[STAThread]
static void Main(string[] args) {
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
#region create thumbnail option
for (int x = 0; x < args.Length; x++) {
if ((args[x].Trim().Equals("/displace", StringComparison.OrdinalIgnoreCase)) || (args[x].Trim().Equals("-displace", StringComparison.OrdinalIgnoreCase))) {
OpenFileDialog openDialog = new OpenFileDialog();
openDialog.Title = "Select displacement map";
openDialog.Filter = "All files (*.*)|*.*";
openDialog.FilterIndex = 1;
openDialog.CheckFileExists = true;
openDialog.DereferenceLinks = true;
openDialog.ShowReadOnly = false;
openDialog.FileName = string.Empty;
if (openDialog.ShowDialog() == DialogResult.OK) {
// load displacement map
string mapName = openDialog.FileName;
Bitmap mapBitmap;
try {
mapBitmap = new Bitmap(mapName);
} catch (Exception) {
MessageBox.Show("Error loading displacement map. Application ended.", Application.ProductName, MessageBoxButtons.OK);
return;
}
if ((mapBitmap.PixelFormat != System.Drawing.Imaging.PixelFormat.Format24bppRgb) || (mapBitmap.Size.Width != 1024) || (mapBitmap.Size.Height != 768)) {
MessageBox.Show("The displacement map must be 1024x768 and 24bpp. Application ended.", Application.ProductName, MessageBoxButtons.OK);
return;
}
openDialog.Title = "Select image to displace";
openDialog.FileName = string.Empty;
if (openDialog.ShowDialog() == DialogResult.OK) {
// load source bitmap
string sourceName = openDialog.FileName;
Bitmap sourceBitmap;
try {
sourceBitmap = new Bitmap(sourceName);
} catch (Exception) {
MessageBox.Show("Error loading image. Application ended.", Application.ProductName, MessageBoxButtons.OK);
return;
}
if ((sourceBitmap.PixelFormat != System.Drawing.Imaging.PixelFormat.Format16bppRgb565) && (sourceBitmap.PixelFormat != System.Drawing.Imaging.PixelFormat.Format24bppRgb) && (sourceBitmap.PixelFormat != System.Drawing.Imaging.PixelFormat.Format32bppRgb)) {
MessageBox.Show("This image to displace must be 16bpp(565), 24bpp(888) or 32bpp. Application ended.", Application.ProductName, MessageBoxButtons.OK);
return;
}
// create distorted bitmap
Bitmap destBitmap = new Bitmap(sourceBitmap.Width, sourceBitmap.Height, sourceBitmap.PixelFormat);
Bitmap destThumb = new Bitmap((int) (320*0.5), (int) (240*0.5), sourceBitmap.PixelFormat);
unsafe {
BitmapData sourceData = sourceBitmap.LockBits(new Rectangle(0, 0, sourceBitmap.Width, sourceBitmap.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, sourceBitmap.PixelFormat);
byte* sourceScan0 = (byte*)sourceData.Scan0;
WebcamWithPreview.DistortedDraw((byte*)sourceScan0, mapBitmap, destBitmap);
sourceBitmap.UnlockBits(sourceData);
}
using (Graphics g = Graphics.FromImage(destThumb)) {
g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
g.DrawImage(destBitmap, g.VisibleClipBounds);
}
// write distorted jpeg
EncoderParameters eargs = new EncoderParameters();
eargs.Param = new EncoderParameter[] { new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 95L) };
ImageCodecInfo[] arrCodecs = ImageCodecInfo.GetImageEncoders();
foreach (ImageCodecInfo i in arrCodecs) {
if (i.FormatID.Equals(ImageFormat.Jpeg.Guid)) {
destThumb.Save(Path.Combine(Path.GetDirectoryName(mapName), Path.GetFileNameWithoutExtension(mapName)) + "-thumb.jpg", i, eargs);
break;
}
}
MessageBox.Show("Displaced bitmap created successfully! Application ended.", Application.ProductName, MessageBoxButtons.OK);
}
}
return;
}
}
#endregion
// one instance limit
IntPtr mutexPtr = NativeMethods.CreateMutexA(IntPtr.Zero, false, "FJRCamTimerMutex");
if (mutexPtr != IntPtr.Zero) {
if (Marshal.GetLastWin32Error() == NativeMethods.ERROR_ALREADY_EXISTS) {
NativeMethods.ReleaseMutex(mutexPtr);
MessageBox.Show(Language.FormatString(Language.LanguageString.App_AlreadyRunning), Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
}
Kernel kernel = new Kernel(args);
Application.Run();
kernel = null;
if (mutexPtr != IntPtr.Zero) {
NativeMethods.ReleaseMutex(mutexPtr);
}
}
static class NativeMethods {
internal const int ERROR_ALREADY_EXISTS = 183;
[DllImport("kernel32.dll", SetLastError = true)]
internal static extern IntPtr CreateMutexA(IntPtr lpMutexAttributes, bool bInitialOwner, string lpName);
[DllImport("kernel32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool ReleaseMutex(IntPtr hMutex);
}
}
}