-
Notifications
You must be signed in to change notification settings - Fork 1
/
HomePage.cs
executable file
·180 lines (155 loc) · 5.4 KB
/
HomePage.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
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace YUVDecoder
{
public partial class HomePage : Form
{
byte[] buffer;
MemoryStream memory;
static byte[][] frames;
private static Bitmap[] bitmaps;
public HomePage()
{
InitializeComponent();
}
/// Functions:
#region Functions
private void parseBuffer()
{
double yuvRate = 3.0 / 2.0;
switch (FileInfo.yuv)
{
case FileInfo.yuvType.y420:
yuvRate = 3.0 / 2.0;
break;
case FileInfo.yuvType.y422:
yuvRate = 2.0;
break;
case FileInfo.yuvType.y444:
yuvRate = 3.0;
break;
}
double frameCount = buffer.Length / (FileInfo.width * FileInfo.height * yuvRate);
int frameLenght = (int)(buffer.Length / frameCount);
frames = new byte[(int)frameCount][];
for (int i = 0; i < (int)frameCount; i++)
{
frames[i] = new byte[frameLenght];
memory.Read(frames[i], 0, frameLenght);
}
}
private static Bitmap renderFrame(byte[] yuv, int width, int height)
{
Bitmap bitmap = new Bitmap(width, height);
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
int index = y * width + x;
byte value = clip(yuv[index]);
Color c = Color.FromArgb(value, value, value);
bitmap.SetPixel(x, y, c);
}
}
return bitmap;
}
private static byte clip(float input)
{
if (input < 0) input = 0;
if (input > 255) input = 255;
return (byte)Math.Abs(input);
}
private async void saveBitmaps(string path)
{
label1.Visible = true;
menuStrip1.Enabled = false;
await Task.Run(() =>
{
for (int i = 0; i < bitmaps.Length; i++)
bitmaps[i].Save(path + $"{fileName.Text}_" + (i + 1) + ".bmp", ImageFormat.Bmp);
});
label1.Visible = false;
menuStrip1.Enabled = true;
MessageBox.Show(this, "Kaydedildi.");
}
#endregion
/// Buttons Click Events:
#region Buttons Clicks
private async void openFile_Click(object sender, EventArgs e)
{
OpenFileDialog dialog = new OpenFileDialog();
dialog.Filter = "YUV Files (*.yuv)|*.yuv";
dialog.ShowDialog();
if (File.Exists(dialog.FileName))
{
fileName.Text = dialog.SafeFileName;
buffer = File.ReadAllBytes(dialog.FileName);
memory = new MemoryStream(buffer);
parseBuffer();
Bitmap firstFrame = renderFrame(frames[0], FileInfo.width, FileInfo.height);
bitmaps = new Bitmap[frames.Length];
menuStrip1.Enabled = false;
label1.Visible = true;
await Task.Run(() =>
{
Parallel.For(0, frames.Length,
i => {
bitmaps[i] = renderFrame(frames[i], FileInfo.width, FileInfo.height);
});
});
label1.Visible = false;
menuStrip1.Enabled = true;
saveFile.Enabled = true;
playButton.Enabled = true;
pictureBox.Image = firstFrame;
}
}
private void settings_Click(object sender, EventArgs e)
{
Settings frm = new Settings();
Enabled = false;
switch (frm.ShowDialog())
{
case DialogResult.OK:
yuvFormatLabel.Text = frm.yuvFormat;
frameSizeLabel.Text = frm.frameSize;
Enabled = true;
break;
case DialogResult.Cancel:
Enabled = true;
break;
}
}
private async void playButton_Click(object sender, EventArgs e)
{
menuStrip1.Enabled = false;
for (int i = 0; i < bitmaps.Length; i++)
{
pictureBox.Image = bitmaps[i];
await Task.Delay(20);
}
pictureBox.Image = bitmaps[0];
menuStrip1.Enabled = true;
}
private void saveFile_Click(object sender, EventArgs e)
{
FolderBrowserDialog dialog = new FolderBrowserDialog();
if (dialog.ShowDialog() == DialogResult.OK)
{
string path = dialog.SelectedPath + "\\";
saveBitmaps(path);
}
}
#endregion
}
}