forked from swharden/Spectrogram
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImage.cs
More file actions
57 lines (49 loc) · 1.92 KB
/
Image.cs
File metadata and controls
57 lines (49 loc) · 1.92 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
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
namespace Spectrogram
{
public static class Image
{
public static Bitmap GetBitmap(List<double[]> ffts, Colormap cmap, double intensity = 1, bool dB = false, bool roll = false, int rollOffset = 0)
{
if (ffts.Count == 0)
throw new ArgumentException("This Spectrogram contains no FFTs (likely because no signal was added)");
int Width = ffts.Count;
int Height = ffts[0].Length;
Bitmap bmp = new Bitmap(Width, Height, PixelFormat.Format8bppIndexed);
cmap.Apply(bmp);
var lockRect = new Rectangle(0, 0, Width, Height);
BitmapData bitmapData = bmp.LockBits(lockRect, ImageLockMode.ReadOnly, bmp.PixelFormat);
int stride = bitmapData.Stride;
byte[] bytes = new byte[bitmapData.Stride * bmp.Height];
Parallel.For(0, Width, col =>
{
int sourceCol = col;
if (roll)
{
sourceCol += Width - rollOffset % Width;
if (sourceCol >= Width)
sourceCol -= Width;
}
for (int row = 0; row < Height; row++)
{
double value = ffts[sourceCol][row];
if (dB)
value = 20 * Math.Log10(value + 1);
value *= intensity;
value = Math.Min(value, 255);
int bytePosition = (Height - 1 - row) * stride + col;
bytes[bytePosition] = (byte)value;
}
});
Marshal.Copy(bytes, 0, bitmapData.Scan0, bytes.Length);
bmp.UnlockBits(bitmapData);
return bmp;
}
}
}