Skip to content

Commit

Permalink
SpectrogramGenerator: separate SaveImage() and GetImageBytes() (#62)
Browse files Browse the repository at this point in the history
resolves #57

Co-authored-by: John McMeen <[email protected]>
  • Loading branch information
swharden and jmcmeen authored Nov 1, 2024
1 parent ab81058 commit 058e604
Showing 1 changed file with 22 additions and 21 deletions.
43 changes: 22 additions & 21 deletions src/Spectrogram/SpectrogramGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -262,32 +262,33 @@ public SKBitmap GetBitmapMel(int melBinCount = 25, double intensity = 1, bool dB
/// <param name="intensity">Multiply the output by a fixed value to change its brightness.</param>
/// <param name="dB">If true, output will be log-transformed.</param>
/// <param name="dBScale">If dB scaling is in use, this multiplier will be applied before log transformation.</param>
/// <param name="roll">Behavior of the spectrogram when it is full of data.
/// Roll (true) adds new columns on the left overwriting the oldest ones.
/// Scroll (false) slides the whole image to the left and adds new columns to the right.</param>
/// <param name="roll">Controls overflow behavior. True wraps new data around to the start. False slides new data in.</param>
public void SaveImage(string fileName, double intensity = 1, bool dB = false, double dBScale = 1, bool roll = false)
{
string extension = Path.GetExtension(fileName).ToLower();
byte[] bytes = GetImageBytes(extension, intensity, dB, dBScale, roll);
File.WriteAllBytes(fileName, bytes);
}

public byte[] GetImageBytes(string extension, double intensity = 1, bool dB = false, double dBScale = 1, bool roll = false)
{
if (FFTs.Count == 0)
throw new InvalidOperationException("Spectrogram contains no data. Use Add() to add signal data.");

string extension = Path.GetExtension(fileName).ToLower();

SKEncodedImageFormat fmt;
if (extension == ".bmp")
fmt = SKEncodedImageFormat.Bmp;
else if (extension == ".png")
fmt = SKEncodedImageFormat.Png;
else if (extension == ".jpg" || extension == ".jpeg")
fmt = SKEncodedImageFormat.Jpeg;
else if (extension == ".gif")
fmt = SKEncodedImageFormat.Gif;
else
throw new ArgumentException("unknown file extension");

using var image = Image.GetBitmap(FFTs, Colormap, intensity, dB, dBScale, roll, NextColumnIndex);
using var encodedImage = image.Encode(fmt, 80);
using var fileStream = new FileStream(fileName, FileMode.Create);
encodedImage.SaveTo(fileStream);
SKEncodedImageFormat fmt = extension.ToLower() switch
{
".bmp" => SKEncodedImageFormat.Bmp,
".png" => SKEncodedImageFormat.Png,
".gif" => SKEncodedImageFormat.Gif,
".jpg" => SKEncodedImageFormat.Jpeg,
".jpeg" => SKEncodedImageFormat.Jpeg,
_ => throw new ArgumentException("unknown file extension"),
};

using SKBitmap image = Image.GetBitmap(FFTs, Colormap, intensity, dB, dBScale, roll, NextColumnIndex);
using SKData encodedImage = image.Encode(fmt, 80);
byte[] bytes = encodedImage.ToArray();
return bytes;
}

/// <summary>
Expand Down

0 comments on commit 058e604

Please sign in to comment.