forked from swharden/Spectrogram
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathImageMaker.cs
More file actions
104 lines (84 loc) · 3.42 KB
/
ImageMaker.cs
File metadata and controls
104 lines (84 loc) · 3.42 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
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using SkiaSharp;
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 SKBitmap 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;
var imageInfo = new SKImageInfo(width, height, SKColorType.Gray8);
var bitmap = new SKBitmap(imageInfo);
int pixelCount = width * height;
byte[] pixelBuffer = new byte[pixelCount];
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) * width + col;
pixelBuffer[bytePosition] = (byte)value;
}
});
IntPtr pixelPtr = bitmap.GetPixels();
Marshal.Copy(pixelBuffer, 0, pixelPtr, pixelBuffer.Length);
SKBitmap newBitmap = Colormap.ApplyFilter(bitmap);
bitmap.Dispose();
return newBitmap;
}
}
}