Skip to content

Commit

Permalink
Match thumbnail placeholders to background
Browse files Browse the repository at this point in the history
  • Loading branch information
cyanfish committed Feb 7, 2024
1 parent d392260 commit 80dc5c0
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 1 deletion.
54 changes: 54 additions & 0 deletions NAPS2.Images/Bitwise/FillColorImageOp.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
namespace NAPS2.Images.Bitwise;

public class FillColorImageOp : UnaryBitwiseImageOp
{
private readonly byte _r, _g, _b, _a;

public FillColorImageOp(byte r, byte g, byte b, byte a)
{
_r = r;
_g = g;
_b = b;
_a = a;
}

protected override void PerformCore(BitwiseImageData data, int partStart, int partEnd)
{
if (data.bytesPerPixel is 1 or 3 or 4)
{
PerformRgba(data, partStart, partEnd);
}
else
{
throw new InvalidOperationException("Unsupported pixel format");
}
}

private unsafe void PerformRgba(BitwiseImageData data, int partStart, int partEnd)
{
bool gray = data.bytesPerPixel == 1;
byte luma = (byte) ((_r * R_MULT + _g * G_MULT + _b * B_MULT) / 1000);
for (int i = partStart; i < partEnd; i++)
{
var row = data.ptr + data.stride * i;
for (int j = 0; j < data.w; j++)
{
var pixel = row + j * data.bytesPerPixel;
if (gray)
{
*pixel = luma;
}
else
{
*(pixel + data.rOff) = _r;
*(pixel + data.gOff) = _g;
*(pixel + data.bOff) = _b;
}
if (data.hasAlpha)
{
*(pixel + data.aOff) = _a;
}
}
}
}
}
6 changes: 6 additions & 0 deletions NAPS2.Lib/EtoForms/EtoExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Eto.Drawing;
using Eto.Forms;
using NAPS2.Images.Bitwise;

namespace NAPS2.EtoForms;

Expand Down Expand Up @@ -40,4 +41,9 @@ public static bool IsChecked(this CheckBox checkBox)
{
return checkBox.Checked == true;
}

public static void Fill(this IMemoryImage image, Color color)
{
new FillColorImageOp((byte) color.Rb, (byte) color.Gb, (byte) color.Bb, (byte) color.Ab).Perform(image);
}
}
5 changes: 4 additions & 1 deletion NAPS2.Lib/Images/UiThumbnailProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ namespace NAPS2.Images;
public class UiThumbnailProvider
{
private readonly ImageContext _imageContext;
private readonly ColorScheme _colorScheme;
private IMemoryImage? _placeholder;

public UiThumbnailProvider(ImageContext imageContext)
public UiThumbnailProvider(ImageContext imageContext, ColorScheme colorScheme)
{
_imageContext = imageContext;
_colorScheme = colorScheme;
}

public IMemoryImage GetThumbnail(UiImage img, int thumbnailSize)
Expand Down Expand Up @@ -43,6 +45,7 @@ private IMemoryImage RenderPlaceholder(int thumbnailSize)
}
_placeholder?.Dispose();
_placeholder = _imageContext.Create(thumbnailSize, thumbnailSize, ImagePixelFormat.RGB24);
_placeholder.Fill(_colorScheme.BackgroundColor);
_placeholder = EtoPlatform.Current.DrawHourglass(_imageContext, _placeholder);
return _placeholder;
}
Expand Down

0 comments on commit 80dc5c0

Please sign in to comment.