Skip to content

Commit

Permalink
In-Code Dithering Solutions
Browse files Browse the repository at this point in the history
lots of algorithms
  • Loading branch information
RinLovesYou committed Mar 1, 2021
1 parent 549473b commit 8c27e88
Show file tree
Hide file tree
Showing 11 changed files with 365 additions and 108 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
/EncodeAndSign/bin/Debug
/EncodeAndSign/obj/Debug
/EncodeAndSign/packages
/Flipnote Tools Desktop
Binary file modified EncodeAndSign/.vs/EncodeAndSign/v16/.suo
Binary file not shown.
14 changes: 14 additions & 0 deletions EncodeAndSign/Config.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using Newtonsoft.Json;

namespace EncodeAndSign
{
[JsonObject]
public class Config
{
public int DitheringMode { get; set; }
public bool Accurate { get; set; }
public bool Split { get; set; }

public Config() { }
}
}
13 changes: 7 additions & 6 deletions EncodeAndSign/Data/DecodedFrame.cs
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ public void CreateThumbnail()
}
}
Thumbnail.WritePixels(new Int32Rect(0, 0, 128, 96), thumbnailPixels, 128, 0);

}

byte GetCompositeThumbnailPixel(int x, int y)
Expand Down Expand Up @@ -529,7 +529,6 @@ public byte[] CreateThumbnailW64()
var bruhh = BitmapImage2Bitmap(Thumbnail);
var resized = new System.Drawing.Bitmap(bruhh, new System.Drawing.Size(bruhh.Width / 2, bruhh.Height / 2));
var res = new byte[1536];
/// TO DO : change with the actual frame thumbnail
for (int x = 0; x < 64; x++)
{
for (int y = 0; y < 48; y++)
Expand All @@ -538,17 +537,19 @@ public byte[] CreateThumbnailW64()
if (b < 0.1)
{
w64SetPixel(res, x, y, TColor.Black);
} else if(b > 0.8)
}
else if (b > 0.8)
{
w64SetPixel(res, x, y, TColor.White);
} else
}
else
{
w64SetPixel(res, x, y, TColor.Gray);
}
}
}


return res;
}

Expand Down
4 changes: 2 additions & 2 deletions EncodeAndSign/Data/Flipnote.cs
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ public void Save(string fn)
{
AnimationDataSize = (uint)(AnimationDataSize + 8 + Frames.Count() * 4);
var AllignSize = (uint)(4 - ((0x6A0 + AnimationDataSize + Frames.Count()) % 4));
if (AllignSize != 4 && AllignSize !=2)
if (AllignSize != 4)
AnimationDataSize += AllignSize;
w.Write(FileMagic);
w.Write(AnimationDataSize);
Expand Down Expand Up @@ -337,7 +337,7 @@ public void Save(string fn)
for (int i = 0; i < Frames.Length; i++)
w.Write((byte)0);

if (AllignSize != 4 && AllignSize != 2)
if (AllignSize != 4)
w.Write(new byte[AllignSize]);

// make the next offset dividable by 4;
Expand Down
17 changes: 17 additions & 0 deletions EncodeAndSign/Data/FrameDecoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,23 @@ public FrameDecoder(string[] filenames)
BoolFrames = bools;
}

public FrameDecoder(List<Bitmap> bitmaps)
{
//frames to bool arrays
List<bool[,]> bools = new List<bool[,]>();
int i = 0;
bitmaps.ToList().ForEach(frame =>
{
var color = frame;
var bw = color.Clone(new Rectangle(0, 0, color.Width, color.Height), PixelFormat.Format1bppIndexed);
bools.Add(BitmapToBoolArray(bw));

});
BoolFrames = bools;
}




public static bool[,] BitmapToBoolArray(Bitmap PiecesBitmap)
{
Expand Down
27 changes: 27 additions & 0 deletions EncodeAndSign/EncodeAndSign.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@
<StartupObject />
</PropertyGroup>
<ItemGroup>
<Reference Include="LibDithering, Version=0.9.3.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>packages\LibDithering.0.9.3\lib\netstandard2.0\LibDithering.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Win32.Registry, Version=4.1.3.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>packages\Microsoft.Win32.Registry.4.7.0\lib\net461\Microsoft.Win32.Registry.dll</HintPath>
</Reference>
Expand All @@ -61,13 +64,35 @@
<Reference Include="NAudio.WinMM, Version=2.0.0.0, Culture=neutral, PublicKeyToken=e279aa5131008a41, processorArchitecture=MSIL">
<HintPath>packages\NAudio.WinMM.2.0.0\lib\netstandard2.0\NAudio.WinMM.dll</HintPath>
</Reference>
<Reference Include="NDesk.Options, Version=1.2.5.0, Culture=neutral, PublicKeyToken=11d31d3c6b89255c, processorArchitecture=MSIL">
<HintPath>packages\NDesk.Options.Core.1.2.5\lib\net472\NDesk.Options.dll</HintPath>
</Reference>
<Reference Include="Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>packages\Newtonsoft.Json.12.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="PresentationCore" />
<Reference Include="ShellProgressBar, Version=5.1.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>packages\ShellProgressBar.5.1.0\lib\net461\ShellProgressBar.dll</HintPath>
</Reference>
<Reference Include="SixLabors.ImageSharp, Version=1.0.0.0, Culture=neutral, PublicKeyToken=d998eea7b14cab13, processorArchitecture=MSIL">
<HintPath>packages\SixLabors.ImageSharp.1.0.3\lib\net472\SixLabors.ImageSharp.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Buffers, Version=4.0.3.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>packages\System.Buffers.4.5.1\lib\net461\System.Buffers.dll</HintPath>
</Reference>
<Reference Include="System.Core" />
<Reference Include="System.Drawing" />
<Reference Include="System.Memory, Version=4.0.1.1, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>packages\System.Memory.4.5.4\lib\net461\System.Memory.dll</HintPath>
</Reference>
<Reference Include="System.Numerics" />
<Reference Include="System.Numerics.Vectors, Version=4.1.4.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>packages\System.Numerics.Vectors.4.5.0\lib\net46\System.Numerics.Vectors.dll</HintPath>
</Reference>
<Reference Include="System.Runtime.CompilerServices.Unsafe, Version=4.0.4.1, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>packages\System.Runtime.CompilerServices.Unsafe.4.5.3\lib\net461\System.Runtime.CompilerServices.Unsafe.dll</HintPath>
</Reference>
<Reference Include="System.Runtime.InteropServices.RuntimeInformation, Version=4.0.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>packages\System.Runtime.InteropServices.RuntimeInformation.4.3.0\lib\net45\System.Runtime.InteropServices.RuntimeInformation.dll</HintPath>
<Private>True</Private>
Expand All @@ -93,13 +118,15 @@
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="Config.cs" />
<Compile Include="Data\DecodedFrame.cs" />
<Compile Include="Data\Flipnote.cs" />
<Compile Include="Data\FrameDecoder.cs" />
<Compile Include="Data\LayerColor.cs" />
<Compile Include="Data\ManualFlipnoteSigner.cs" />
<Compile Include="Encoder\AdpcmEncoder.cs" />
<Compile Include="Encoder\Encoder.cs" />
<Compile Include="Encoder\ImageEncoder.cs" />
<Compile Include="Extensions\Utils.cs" />
<Compile Include="Extensions\BinaryReaderExtensions.cs" />
<Compile Include="Program.cs" />
Expand Down
12 changes: 12 additions & 0 deletions EncodeAndSign/Encoder/Encoder.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using EncodeAndSign.Data;
using System.Collections.Generic;
using System.Drawing;

namespace EncodeAndSign.Encoder
{
Expand All @@ -17,6 +18,17 @@ public Encoder(string[] frames, Flipnote dummy)
ResultNote = encoded;
}

public Encoder(List<Bitmap> frames, Flipnote dummy)
{

FrameDecoder FrameDecoder = new FrameDecoder(frames);

Flipnote encoded = Flipnote.New(dummy.Metadata.CurrentAuthorName, dummy.Metadata.CurrentAuthorId, ToDecodedFrame(FrameDecoder.BoolFrames), false);

//todo: use this for metadata display i guess
ResultNote = encoded;
}

public List<DecodedFrame> ToDecodedFrame(List<bool[,]> input)
{
List<DecodedFrame> buffer = new List<DecodedFrame>();
Expand Down
83 changes: 83 additions & 0 deletions EncodeAndSign/Encoder/ImageEncoder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Processing;
using SixLabors.ImageSharp.Processing.Processors.Dithering;
using System.Collections.Generic;
using System.Drawing;

namespace EncodeAndSign.Encoder
{
public class ImageEncoder
{
public ImageEncoder()
{

}

public void DoRinDithering(string[] filenames, int type)
{
List<Bitmap> bitmaps = new List<Bitmap>();
IDither DitheringType = null;
switch (type)
{
case 0:
DitheringType = KnownDitherings.Bayer8x8;
break;
case 1:
DitheringType = KnownDitherings.Bayer4x4;
break;
case 2:
DitheringType = KnownDitherings.Bayer2x2;
break;
case 3:
DitheringType = KnownDitherings.FloydSteinberg;
break;
case 4:
DitheringType = KnownDitherings.Atkinson;
break;
case 5:
DitheringType = KnownDitherings.Burks;
break;
case 6:
DitheringType = KnownDitherings.JarvisJudiceNinke;
break;
case 7:
DitheringType = KnownDitherings.Sierra3;
break;
case 8:
DitheringType = KnownDitherings.StevensonArce;
break;
case 9:
DitheringType = KnownDitherings.Sierra2;
break;
case 10:
DitheringType = KnownDitherings.Sierra3;
break;
case 11:
DitheringType = KnownDitherings.SierraLite;
break;
case 12:
DitheringType = KnownDitherings.Stucki;
break;
case 13:
DitheringType = KnownDitherings.Ordered3x3;
break;
default:
//this one is my favorite :)
DitheringType = KnownDitherings.Bayer8x8;
break;
}
for (int i = 0; i < filenames.Length; i++)
{
using (SixLabors.ImageSharp.Image image = SixLabors.ImageSharp.Image.Load(filenames[i]))
{
image.Mutate(x =>
{
x.BinaryDither(DitheringType);
});
image.SaveAsPng($"frames/frame_{i}.png");
image.Dispose();
}
}
}
}
}
Loading

0 comments on commit 8c27e88

Please sign in to comment.