Skip to content

Commit

Permalink
Merge pull request #1263 from stevencohn/1258-stylize-images
Browse files Browse the repository at this point in the history
Stylize Images command
  • Loading branch information
stevencohn authored Jan 17, 2024
2 parents f6835c6 + 0953888 commit 5d47f14
Show file tree
Hide file tree
Showing 21 changed files with 3,924 additions and 33 deletions.
5 changes: 5 additions & 0 deletions OneMore/AddInCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -847,6 +847,11 @@ public async Task StrikeoutTasksCmd(IRibbonControl control)
=> await factory.Run<StrikeoutTasksCommand>();


[Command("ribStylizeImagesButton_Label", Keys.None, "ribImagesMenu")]
public async Task StylizeImagesCmd(IRibbonControl control)
=> await factory.Run<StylizeImagesCommand>();


[Command("ribTaggedButton_Label", Keys.Control | Keys.Alt | Keys.T, "ribSearchMenu")]
public async Task TaggedCmd(IRibbonControl control)
=> await factory.Run<TaggedCommand>();
Expand Down
5 changes: 3 additions & 2 deletions OneMore/Commands/Images/ResizeImagesDialog.Designer.cs

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

20 changes: 8 additions & 12 deletions OneMore/Commands/Images/ResizeImagesDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -303,20 +303,16 @@ public Image Adjust(Image image)
adjusted = p.SetSaturation((float)saturationBox.Value / 100f);
}

if (styleBox.SelectedIndex == 1)
if (styleBox.SelectedIndex > 0)
{
using var p = adjusted;
adjusted = p.ToGrayscale();
}
else if (styleBox.SelectedIndex == 2)
{
using var p = adjusted;
adjusted = p.ToSepia();
}
else if (styleBox.SelectedIndex == 3)
{
using var p = adjusted;
adjusted = p.ToPolaroid();
adjusted = styleBox.SelectedIndex switch
{
1 => p.ToGrayscale(),
2 => p.ToSepia(),
3 => p.ToPolaroid(),
_ => p.Inverted()
};
}

// opacity must be set last
Expand Down
138 changes: 138 additions & 0 deletions OneMore/Commands/Images/StylizeImagesCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
//************************************************************************************************
// Copyright © 2023 Steven M Cohn. All rights reserved.
//************************************************************************************************

namespace River.OneMoreAddIn.Commands
{
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Linq;
using Resx = Properties.Resources;


/// <summary>
/// Resize and adjust images on the page
/// </summary>
internal class StylizeImagesCommand : Command
{
private XNamespace ns;

public StylizeImagesCommand()
{
}


public override async Task Execute(params object[] args)
{
using var one = new OneNote(out var page, out ns, OneNote.PageDetail.All);

var foreElements = page.Root
.Elements(ns + "Outline")
.Descendants(ns + "Image")
.ToList();

var backElements = page.Root
.Elements(ns + "Image")
.ToList();

if (!foreElements.Any() && !backElements.Any())
{
UIHelper.ShowMessage(Resx.StylizeImagesCommand_noImages);
return;
}

var foreSelected = foreElements
.Where(e => e.Attribute("selected")?.Value == "all")
.ToList();

var backSelected = backElements
.Where(e => e.Attribute("selected")?.Value == "all")
.ToList();

using var dialog = new StylizeImagesDialog(
foreElements.Count, foreSelected.Count,
backElements.Count, backSelected.Count);

var result = dialog.ShowDialog(owner);
if (result != DialogResult.OK)
{
return;
}

var updated = false;
if (foreSelected.Any() && dialog.ApplyForeground)
{
Stylize(foreSelected, dialog.Style);
updated = true;
}

if (backSelected.Any() && dialog.ApplyBackground)
{
Stylize(backSelected, dialog.Style);
updated = true;
}

if (!updated)
{
if (foreElements.Any() && dialog.ApplyForeground)
{
Stylize(foreElements, dialog.Style);
updated = true;
}

if (backElements.Any() && dialog.ApplyBackground)
{
Stylize(backElements, dialog.Style);
updated = true;
}
}

if (updated)
{
await one.Update(page);
}
}


private void Stylize(
IEnumerable<XElement> elements, StylizeImagesDialog.ImageStyle style)
{
foreach (var element in elements)
{
using var image = ReadImage(element);

var stylized = style switch
{
StylizeImagesDialog.ImageStyle.GrayScale => image.ToGrayscale(),
StylizeImagesDialog.ImageStyle.Sepia => image.ToSepia(),
StylizeImagesDialog.ImageStyle.Polaroid => image.ToPolaroid(),
_ => image.Inverted()
};

WriteImage(element, stylized);
}
}


private Image ReadImage(XElement image)
{
var data = Convert.FromBase64String(image.Element(ns + "Data").Value);
using var stream = new MemoryStream(data, 0, data.Length);
return Image.FromStream(stream);
}


private void WriteImage(XElement element, Image image)
{
var bytes = (byte[])new ImageConverter().ConvertTo(image, typeof(byte[]));

var data = element.Element(ns + "Data");
data.Value = Convert.ToBase64String(bytes);
}
}
}
166 changes: 166 additions & 0 deletions OneMore/Commands/Images/StylizeImagesDialog.Designer.cs

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

Loading

0 comments on commit 5d47f14

Please sign in to comment.