Skip to content

Commit

Permalink
somewhat successfull resize and crop
Browse files Browse the repository at this point in the history
  • Loading branch information
wiegell committed Nov 16, 2022
1 parent 3eff80f commit 6652123
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 20 deletions.
10 changes: 8 additions & 2 deletions AstroWall/ApplicationLayer/Helpers/Screen.Model.Macos.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
using Newtonsoft.Json;
using System.Linq;
using System.Collections.Generic;
using Foundation;
using GameController;

namespace AstroWall.ApplicationLayer.Helpers
{
Expand All @@ -21,8 +23,8 @@ public static Screen Main()
private Screen(NSScreen nSscreen)
{
Id = nSscreen.LocalizedName;
xRes = (int)nSscreen.Frame.Size.Width;
yRes = (int)nSscreen.Frame.Size.Height;
xRes = (int)nSscreen.Frame.Size.Width * 2;
yRes = (int)nSscreen.Frame.Size.Height * 2;
isMainScreen = NSScreen.MainScreen == nSscreen;
}

Expand Down Expand Up @@ -56,6 +58,10 @@ public bool isVertical()
{
return yRes > xRes;
}
public double calcRatio()
{
return (double)xRes / (double)yRes;
}
}


Expand Down
2 changes: 1 addition & 1 deletion AstroWall/ApplicationLayer/View/AppDelegate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
using AstroWall.BusinessLayer;
using AstroWall.BusinessLayer.Preferences;
using Foundation;
using GameController;



namespace AstroWall.ApplicationLayer
Expand Down
27 changes: 15 additions & 12 deletions AstroWall/BusinessLayer/ImgWrap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

namespace AstroWall.BusinessLayer
{
[JsonObject(MemberSerialization.OptOut)]
[JsonObject(MemberSerialization.OptIn)]
public class ImgWrap : IComparable
{
[JsonProperty]
Expand Down Expand Up @@ -60,6 +60,8 @@ public ImgWrap()

}

public string ImgLocalUrlNoExtension { get => System.IO.Path.ChangeExtension(ImgLocalUrl, null); }

public async Task LoadOnlineDataButNotImg()
{
if (OnlineDataIsLoaded())
Expand Down Expand Up @@ -191,7 +193,7 @@ public async Task createPostProcessedImages(Dictionary<string, Screen> screens,
}

// Prep Dictionary vars for postprocess
Dictionary<Screen, SKBitmap> unProcessedImagesByScreenId =
Dictionary<Screen, SKBitmap> unProcessedImagesByScreen =
screens.ToDictionary(
// Set Screen instance as key
screenKV => screenKV.Value,
Expand All @@ -203,17 +205,14 @@ public async Task createPostProcessedImages(Dictionary<string, Screen> screens,
// Prep postprocess chain
Func<Dictionary<Screen, SKBitmap>> postProcessChain =
Wallpaper.PostProcess.ComposePostProcess(
() => unProcessedImagesByScreenId,
//Wallpaper.PostProcess.AddTextCurry(
// (Preferences.AddText)postProcessPrefsDictionary[Preferences.PostProcessType.AddText],
// Title,
// Description
// ),
Wallpaper.PostProcess.AddTextCurry(
() => unProcessedImagesByScreen,
Wallpaper.PostProcess.ScaleAndCrop,
Wallpaper.PostProcess.AddTextCurry(
(Preferences.AddText)postProcessPrefsDictionary[Preferences.PostProcessType.AddText],
Title,
"TEEEEEEEEEST TEEEEEEEEEST TEEEEEEEEEST TEEEEEEEEEST TEEEEEEEEEST TEEEEEEEEEST TEEEEEEEEEST TEEEEEEEEEST TEEEEEEEEEST TEEEEEEEEEST TEEEEEEEEEST TEEEEEEEEEST TEEEEEEEEEST TEEEEEEEEEST TEEEEEEEEEST TEEEEEEEEEST TEEEEEEEEEST TEEEEEEEEEST TEEEEEEEEEST TEEEEEEEEEST TEEEEEEEEEST TEEEEEEEEEST TEEEEEEEEEST TEEEEEEEEEST TEEEEEEEEEST TEEEEEEEEEST TEEEEEEEEEST TEEEEEEEEEST TEEEEEEEEEST TEEEEEEEEEST TEEEEEEEEEST TEEEEEEEEEST TEEEEEEEEEST TEEEEEEEEEST TEEEEEEEEEST TEEEEEEEEEST TEEEEEEEEEST TEEEEEEEEEST TEEEEEEEEEST TEEEEEEEEEST TEEEEEEEEEST TEEEEEEEEEST TEEEEEEEEEST "
Description
)

);

// Do the postprocessing
Expand All @@ -235,10 +234,14 @@ public async Task createPostProcessedImages(Dictionary<string, Screen> screens,
bitmapKV => bitmapKV.Key.Id,
bitmapKV =>
{
string path = $"{ImgLocalUrl}_postprocessed_{bitmapKV.Key.Id}.{FileType}";
string path = $"{ImgLocalUrlNoExtension}_postprocessed_{bitmapKV.Key.Id}.{FileType}";

//Console.WriteLine($"Commencing save of postprocess of screen {bitmapKV.Key.Id} to path {path}");

FileStream f = File.Create(path);
bitmapKV.Value.Encode(f, (OnlineUrlIsJPG() ? SKEncodedImageFormat.Jpeg : SKEncodedImageFormat.Png), 90);
Console.WriteLine($"postprocess for screen {bitmapKV.Key} saved: {path}");
//Console.WriteLine($"Postprocess for screen {bitmapKV.Key} saved to path {path}");
f.Close();
return path;
}
);
Expand Down
2 changes: 1 addition & 1 deletion AstroWall/BusinessLayer/Wallpaper/PostProcess/AddText.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public PostProcess()
canvas.DrawBitmap(mainScreenBitmap, 0, 0);
canvas.ResetMatrix();

PaintToRect(canvas, 1000, 500, 80, returnBitmap.Height - 120, description
PaintToRect(canvas, 1000, 500, 80, returnBitmap.Height - 120, description.Replace("\n","")
);
canvas.Flush();
canvas.Dispose();
Expand Down
75 changes: 71 additions & 4 deletions AstroWall/BusinessLayer/Wallpaper/PostProcess/ScaleAndCrop.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using AstroWall.ApplicationLayer.Helpers;
using SkiaSharp;

Expand All @@ -8,18 +9,84 @@ namespace AstroWall.BusinessLayer.Wallpaper
partial class PostProcess

{
public Func<Dictionary<Screen, SkiaSharp.SKBitmap>, Dictionary<Screen, SkiaSharp.SKBitmap>> ScaleAndCropCurry()
public static Func<Dictionary<Screen, SKBitmap>, Dictionary<Screen, SKBitmap>> ScaleAndCropCurry()
{
// Not really needed atm, might be later
return (Dictionary<Screen, SkiaSharp.SKBitmap> dic) =>
return (Dictionary<Screen, SKBitmap> dic) =>
{
return ScaleAndCrop(dic);
};
}

public static Dictionary<Screen, SkiaSharp.SKBitmap> ScaleAndCrop(Dictionary<Screen, SkiaSharp.SKBitmap> dic)
public static Dictionary<Screen, SKBitmap> ScaleAndCrop(Dictionary<Screen, SKBitmap> dic)
{
return new Dictionary<Screen, SkiaSharp.SKBitmap>();
var returnDic = new Dictionary<Screen, SKBitmap>();
foreach (var KV in dic)
{
Screen screen = KV.Key;
SKBitmap inputBitmap = KV.Value;

SKImageInfo inputInfo = inputBitmap.Info;
double resizefactor;
//if (screen.isHorizontal())
//{
if (ratioFromInfo(inputInfo) > screen.calcRatio())
{
// Image has a wider aspect than screen
// Resize to match heights
// (width overflow will be cropped soon)
resizefactor = ((double)screen.yRes) / (double)inputInfo.Height;
}
else
{
// Image has a taller aspect than screen
// Resize to match widths
// (height overflow will be cropped soon)
resizefactor = ((double)screen.xRes) / (double)inputInfo.Width;
}

// Resize
SKImageInfo newInfo = new SKImageInfo(
((int)(inputInfo.Width * resizefactor)),
((int)(inputInfo.Height * resizefactor))
);
Console.WriteLine($"Resizing postprocess of screen {screen.Id} to size {newInfo.Width}x{newInfo.Height}");
SKBitmap newBitmap = inputBitmap.Resize(newInfo, SKFilterQuality.High);

// Crop
var image = SKImage.FromBitmap(newBitmap);
SKImage croppedImage;
if (ratioFromInfo(inputInfo) > screen.calcRatio())
{
// Image has a wider aspect than screen
// Get the width diff
int widthDiff = newBitmap.Info.Width - screen.xRes;
int xMargin = widthDiff / 2;
var rect = SKRectI.Create(xMargin, 0, screen.xRes, screen.yRes);
croppedImage = image.Subset(rect);
}
else
{
// Image is too tall, get the diff
int heightDiff = newBitmap.Info.Height - screen.yRes;
int yMargin = heightDiff / 2;
var rect = SKRectI.Create(0, yMargin, screen.xRes, screen.yRes);
//var rect = SKRectI.Create(0, yMargin, screen.xRes, screen.yRes);
croppedImage = image.Subset(rect);
}

// Return
var returnBM = SKBitmap.FromImage(croppedImage);
Console.WriteLine($"Cropped postprocess of screen {screen.Id} to {croppedImage.Info.Width}x{croppedImage.Info.Height}");

returnDic.Add(screen, returnBM);
}
return returnDic;
}

private static double ratioFromInfo(SKImageInfo info)
{
return (double)info.Width / (double)info.Height;
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions AstroWall/DataLayer/FileHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,12 @@ public async static Task<SKBitmap> LoadImageFromLocalUrl(string path)

using (MemoryStream memStream = new MemoryStream())
{
Console.WriteLine("Opening file: "+path);
FileStream fs = new FileStream(path, FileMode.Open);

await fs.CopyToAsync(memStream);
fs.Close();
Console.WriteLine("Closed file: "+path);
memStream.Seek(0, SeekOrigin.Begin);

SKImage img = SKImage.FromEncodedData(memStream);
Expand Down

0 comments on commit 6652123

Please sign in to comment.