Skip to content

Commit

Permalink
Merge pull request rithik-b#25 from rithik-b/dev/optimizations
Browse files Browse the repository at this point in the history
Optimized Cover Sprite Generation
  • Loading branch information
Zingabopp authored Mar 24, 2022
2 parents 00007a5 + 6f8808b commit 7ac8950
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 32 deletions.
4 changes: 2 additions & 2 deletions BeatSaberPlaylistsLib.BeatSaber/BeatSaberPlaylistsLib.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 15 additions & 13 deletions BeatSaberPlaylistsLib.BeatSaber/Types/Playlist.BeatSaber.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using System.Threading;
using System.Threading.Tasks;
using static BeatSaber::SliderController.Pool;
using IBeatmapLevelCollection = BeatSaber::IBeatmapLevelCollection;

namespace BeatSaberPlaylistsLib.Types
{
Expand Down Expand Up @@ -317,33 +318,34 @@ public BeatSaber.IPreviewBeatmapLevel[] BeatmapLevels
}

var ms = new MemoryStream();
var beatmapLevels = ((IBeatmapLevelCollection) this).beatmapLevels;

if (BeatmapLevels.Length == 1)
if (beatmapLevels.Count == 1)
{
using var coverStream = Utilities.GetStreamFromSprite(await BeatmapLevels[0].GetCoverImageAsync(CancellationToken.None));
using var coverStream = Utilities.GetStreamFromBeatmap(beatmapLevels[0]);
if (coverStream != null) await coverStream.CopyToAsync(ms);
}
else if (BeatmapLevels.Length == 2)
else if (beatmapLevels.Count == 2)
{
using var imageStream1 = Utilities.GetStreamFromSprite(await BeatmapLevels[0].GetCoverImageAsync(CancellationToken.None));
using var imageStream2 = Utilities.GetStreamFromSprite(await BeatmapLevels[1].GetCoverImageAsync(CancellationToken.None));
using var imageStream1 = Utilities.GetStreamFromBeatmap(beatmapLevels[0]);
using var imageStream2 = Utilities.GetStreamFromBeatmap(beatmapLevels[1]);
using var coverStream = await ImageUtilities.GenerateCollage(imageStream1 ?? Stream.Null, imageStream2 ?? Stream.Null);
await coverStream.CopyToAsync(ms);
}
else if (BeatmapLevels.Length == 3)
else if (beatmapLevels.Count == 3)
{
using var imageStream1 = Utilities.GetStreamFromSprite(await BeatmapLevels[0].GetCoverImageAsync(CancellationToken.None));
using var imageStream2 = Utilities.GetStreamFromSprite(await BeatmapLevels[1].GetCoverImageAsync(CancellationToken.None));
using var imageStream3 = Utilities.GetStreamFromSprite(await BeatmapLevels[2].GetCoverImageAsync(CancellationToken.None));
using var imageStream1 = Utilities.GetStreamFromBeatmap(beatmapLevels[0]);
using var imageStream2 = Utilities.GetStreamFromBeatmap(beatmapLevels[1]);
using var imageStream3 = Utilities.GetStreamFromBeatmap(beatmapLevels[2]);
using var coverStream = await ImageUtilities.GenerateCollage(imageStream1 ?? Stream.Null, imageStream2 ?? Stream.Null, imageStream3 ?? Stream.Null);
await coverStream.CopyToAsync(ms);
}
else
{
using var imageStream1 = Utilities.GetStreamFromSprite(await BeatmapLevels[0].GetCoverImageAsync(CancellationToken.None));
using var imageStream2 = Utilities.GetStreamFromSprite(await BeatmapLevels[1].GetCoverImageAsync(CancellationToken.None));
using var imageStream3 = Utilities.GetStreamFromSprite(await BeatmapLevels[2].GetCoverImageAsync(CancellationToken.None));
using var imageStream4 = Utilities.GetStreamFromSprite(await BeatmapLevels[3].GetCoverImageAsync(CancellationToken.None));
using var imageStream1 = Utilities.GetStreamFromBeatmap(beatmapLevels[0]);
using var imageStream2 = Utilities.GetStreamFromBeatmap(beatmapLevels[1]);
using var imageStream3 = Utilities.GetStreamFromBeatmap(beatmapLevels[2]);
using var imageStream4 = Utilities.GetStreamFromBeatmap(beatmapLevels[3]);
using var coverStream = await ImageUtilities.GenerateCollage(imageStream1 ?? Stream.Null, imageStream2 ?? Stream.Null, imageStream3 ?? Stream.Null, imageStream4 ?? Stream.Null);
await coverStream.CopyToAsync(ms);
}
Expand Down
10 changes: 6 additions & 4 deletions BeatSaberPlaylistsLib.BeatSaber/Utilities.BeatSaber.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Drawing.Imaging;
using System.IO;
using IPA.Loader;
using CustomPreviewBeatmapLevel = BeatSaber::CustomPreviewBeatmapLevel;
using Graphics = System.Drawing.Graphics;

namespace BeatSaberPlaylistsLib
Expand Down Expand Up @@ -83,13 +84,14 @@ public static partial class Utilities
/// <summary>
/// Gets a <see cref="Stream"/> from a <see cref="Sprite"/>
/// </summary>
/// <param name="sprite"></param>
/// <param name="previewBeatmapLevel"></param>
/// <returns></returns>
public static Stream? GetStreamFromSprite(Sprite sprite)
public static Stream? GetStreamFromBeatmap(BeatSaber.IPreviewBeatmapLevel? previewBeatmapLevel)
{
if (sprite.texture.isReadable)
if (previewBeatmapLevel is CustomPreviewBeatmapLevel customPreviewBeatmapLevel)
{
return new MemoryStream(sprite.texture.EncodeToPNG());
var fileName = customPreviewBeatmapLevel.standardLevelInfoSaveData.coverImageFilename;
return new FileStream(Path.Combine(customPreviewBeatmapLevel.customLevelPath, fileName), FileMode.Open, FileAccess.Read, FileShare.Read, 0x4096, true);
}
return GetDefaultImageStream();
}
Expand Down
29 changes: 16 additions & 13 deletions Shared/ImageUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,12 @@ public static class ImageUtilities
public static async Task<Stream> GenerateCollage(Stream imageStream1, Stream imageStream2)
{
var image = new Image<Rgba32>(kImageSize, kImageSize);
using var image1 = await Image.LoadAsync(imageStream1);
using var image2 = await Image.LoadAsync(imageStream2);

await Task.Run(() =>

await Task.Run(async () =>
{
using var image1 = await Image.LoadAsync(imageStream1);
using var image2 = await Image.LoadAsync(imageStream2);

image.Mutate(i =>
{
image1.Mutate(i1 =>
Expand Down Expand Up @@ -67,12 +68,13 @@ await Task.Run(() =>
public static async Task<Stream> GenerateCollage(Stream imageStream1, Stream imageStream2, Stream imageStream3)
{
var image = new Image<Rgba32>(kImageSize, kImageSize);
using var image1 = await Image.LoadAsync(imageStream1);
using var image2 = await Image.LoadAsync(imageStream2);
using var image3 = await Image.LoadAsync(imageStream3);

await Task.Run(() =>
await Task.Run(async () =>
{
using var image1 = await Image.LoadAsync(imageStream1);
using var image2 = await Image.LoadAsync(imageStream2);
using var image3 = await Image.LoadAsync(imageStream3);

image.Mutate(i =>
{
image1.Mutate(i1 =>
Expand Down Expand Up @@ -116,13 +118,14 @@ await Task.Run(() =>
public static async Task<Stream> GenerateCollage(Stream imageStream1, Stream imageStream2, Stream imageStream3, Stream imageStream4)
{
var image = new Image<Rgba32>(kImageSize, kImageSize);
using var image1 = await Image.LoadAsync(imageStream1);
using var image2 = await Image.LoadAsync(imageStream2);
using var image3 = await Image.LoadAsync(imageStream3);
using var image4 = await Image.LoadAsync(imageStream4);

await Task.Run(() =>
await Task.Run(async () =>
{
using var image1 = await Image.LoadAsync(imageStream1);
using var image2 = await Image.LoadAsync(imageStream2);
using var image3 = await Image.LoadAsync(imageStream3);
using var image4 = await Image.LoadAsync(imageStream4);

image.Mutate(i =>
{
image1.Mutate(i1 =>
Expand Down

0 comments on commit 7ac8950

Please sign in to comment.