forked from swharden/Spectrogram
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImageMaker.cs
More file actions
105 lines (86 loc) · 3.49 KB
/
ImageMaker.cs
File metadata and controls
105 lines (86 loc) · 3.49 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
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
{
/// <summary>
/// This class converts a collection of FFTs to a colormapped spectrogram image
/// </summary>
public class ImageMaker
{
/// <summary>
/// Colormap used to translate intensity to pixel color
/// </summary>
public Colormap Colormap;
/// <summary>
/// Intensity is multiplied by this number before converting it to the pixel color according to the colormap
/// </summary>
public double Intensity = 1;
/// <summary>
/// If True, intensity will be log-scaled to represent Decibels
/// </summary>
public bool IsDecibel = false;
/// <summary>
/// If <see cref="IsDecibel"/> is enabled, intensity will be scaled by this value prior to log transformation
/// </summary>
public double DecibelScaleFactor = 1;
/// <summary>
/// If False, the spectrogram will proceed in time from left to right across the whole image.
/// If True, the image will be broken and the newest FFTs will appear on the left and oldest on the right.
/// </summary>
public bool IsRoll = false;
/// <summary>
/// If <see cref="IsRoll"/> is enabled, this value indicates the pixel position of the break point.
/// </summary>
public int RollOffset = 0;
/// <summary>
/// If True, the spectrogram will flow top-down (oldest to newest) rather than left-right.
/// </summary>
public bool IsRotated = false;
public ImageMaker()
{
}
public Bitmap GetBitmap(List<double[]> ffts)
{
if (ffts.Count == 0)
throw new ArgumentException("Not enough data in FFTs to generate an image yet.");
int Width = IsRotated ? ffts[0].Length : ffts.Count;
int Height = IsRotated ? ffts.Count : ffts[0].Length;
Bitmap bmp = new(Width, Height, PixelFormat.Format8bppIndexed);
Colormap.Apply(bmp);
Rectangle lockRect = new(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 (IsRoll)
{
sourceCol += Width - RollOffset % Width;
if (sourceCol >= Width)
sourceCol -= Width;
}
for (int row = 0; row < Height; row++)
{
double value = IsRotated
? ffts[Height - row - 1][sourceCol]
: ffts[sourceCol][row];
if (IsDecibel)
value = 20 * Math.Log10(value * DecibelScaleFactor + 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;
}
}
}