Merge pull request #5042 from somethingSTRANGE/master

Added "Color depth" filter
This commit is contained in:
Jaex 2020-09-15 03:09:38 +03:00 committed by GitHub
commit 18ed2567b4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 70 additions and 6 deletions

View file

@ -62,7 +62,7 @@ public async Task<string> Start(string filePath, HashType hashType)
{ {
return HashCheckThread(filePath, hashType, progress, cts.Token); return HashCheckThread(filePath, hashType, progress, cts.Token);
} }
catch (OperationCanceledException e) catch (OperationCanceledException)
{ {
} }

View file

@ -1378,6 +1378,37 @@ private static void BoxBlurVertical(UnsafeBitmap unsafeBitmap, int range, Rectan
} }
} }
public static void ColorDepth(Bitmap bmp, int bitsPerChannel = 4)
{
if (bitsPerChannel < 1 || bitsPerChannel > 8)
{
return;
}
double colorsPerChannel = Math.Pow(2, bitsPerChannel);
double colorInterval = 255 / (colorsPerChannel - 1);
byte Remap(byte color, double interval)
{
return (byte) Math.Round((Math.Round(color / interval) * interval));
}
using (UnsafeBitmap unsafeBitmap = new UnsafeBitmap(bmp, true))
{
for (int y = 0; y < unsafeBitmap.Height; y++)
{
for (int x = 0; x < unsafeBitmap.Width; x++)
{
ColorBgra color = unsafeBitmap.GetPixel(x, y);
color.Red = Remap(color.Red, colorInterval);
color.Green = Remap(color.Green, colorInterval);
color.Blue = Remap(color.Blue, colorInterval);
unsafeBitmap.SetPixel(x, y, color);
}
}
}
}
// http://incubator.quasimondo.com/processing/superfast_blur.php // http://incubator.quasimondo.com/processing/superfast_blur.php
public static void FastBoxBlur(Bitmap bmp, int radius) public static void FastBoxBlur(Bitmap bmp, int radius)
{ {

View file

@ -0,0 +1,30 @@
using System.ComponentModel;
using System.Drawing;
using ShareX.HelpersLib;
namespace ShareX.ImageEffectsLib
{
[Description("Color depth")]
internal class ColorDepth : ImageEffect
{
private int _bitsPerChannel;
[DefaultValue(4)]
public int BitsPerChannel
{
get => this._bitsPerChannel;
set => this._bitsPerChannel = value.Max(1).Min(8);
}
public ColorDepth()
{
this.ApplyDefaultPropertyValues();
}
public override Bitmap Apply(Bitmap bmp)
{
ImageHelpers.ColorDepth(bmp, this.BitsPerChannel);
return bmp;
}
}
}

View file

@ -120,9 +120,9 @@ private void AddAllEffectsToContextMenu()
typeof(DrawBorder), typeof(DrawBorder),
typeof(DrawCheckerboard), typeof(DrawCheckerboard),
typeof(DrawImage), typeof(DrawImage),
typeof(DrawParticles),
typeof(DrawTextEx), typeof(DrawTextEx),
typeof(DrawText), typeof(DrawText));
typeof(DrawParticles));
AddEffectToContextMenu(Resources.ImageEffectsForm_AddAllEffectsToTreeView_Manipulations, AddEffectToContextMenu(Resources.ImageEffectsForm_AddAllEffectsToTreeView_Manipulations,
typeof(AutoCrop), typeof(AutoCrop),
@ -140,13 +140,13 @@ private void AddAllEffectsToContextMenu()
typeof(Alpha), typeof(Alpha),
typeof(BlackWhite), typeof(BlackWhite),
typeof(Brightness), typeof(Brightness),
typeof(MatrixColor), // "Color matrix"
typeof(Colorize), typeof(Colorize),
typeof(Contrast), typeof(Contrast),
typeof(Gamma), typeof(Gamma),
typeof(Grayscale), typeof(Grayscale),
typeof(Hue), typeof(Hue),
typeof(Inverse), typeof(Inverse),
typeof(MatrixColor),
typeof(Polaroid), typeof(Polaroid),
typeof(Saturation), typeof(Saturation),
typeof(SelectiveColor), typeof(SelectiveColor),
@ -154,10 +154,11 @@ private void AddAllEffectsToContextMenu()
AddEffectToContextMenu(Resources.ImageEffectsForm_AddAllEffectsToTreeView_Filters, AddEffectToContextMenu(Resources.ImageEffectsForm_AddAllEffectsToTreeView_Filters,
typeof(Blur), typeof(Blur),
typeof(ColorDepth),
typeof(MatrixConvolution), // "Convolution matrix"
typeof(EdgeDetect), typeof(EdgeDetect),
typeof(Emboss), typeof(Emboss),
typeof(GaussianBlur), typeof(GaussianBlur),
typeof(MatrixConvolution),
typeof(MeanRemoval), typeof(MeanRemoval),
typeof(Outline), typeof(Outline),
typeof(Pixelate), typeof(Pixelate),

View file

@ -114,6 +114,7 @@
<Compile Include="Drawings\DrawText.cs" /> <Compile Include="Drawings\DrawText.cs" />
<Compile Include="Drawings\DrawTextEx.cs" /> <Compile Include="Drawings\DrawTextEx.cs" />
<Compile Include="Enums.cs" /> <Compile Include="Enums.cs" />
<Compile Include="Filters\ColorDepth.cs" />
<Compile Include="Filters\EdgeDetect.cs" /> <Compile Include="Filters\EdgeDetect.cs" />
<Compile Include="Filters\Emboss.cs" /> <Compile Include="Filters\Emboss.cs" />
<Compile Include="Filters\GaussianBlur.cs" /> <Compile Include="Filters\GaussianBlur.cs" />

View file

@ -174,7 +174,7 @@ private bool StartEncoding()
private Task<bool> StartEncodingAsync() private Task<bool> StartEncodingAsync()
{ {
return Task.Run(StartEncoding); return Task.Run(() => StartEncoding());
} }
private void Manager_EncodeProgressChanged(float percentage) private void Manager_EncodeProgressChanged(float percentage)

View file

@ -34,6 +34,7 @@
<UseApplicationTrust>false</UseApplicationTrust> <UseApplicationTrust>false</UseApplicationTrust>
<BootstrapperEnabled>true</BootstrapperEnabled> <BootstrapperEnabled>true</BootstrapperEnabled>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects> <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<LangVersion>7.3</LangVersion>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols> <DebugSymbols>true</DebugSymbols>