Skip to content

Commit

Permalink
Drop netcoreapp3.1 and net5.0
Browse files Browse the repository at this point in the history
Also replace System.Drawing with SkiaSharp
  • Loading branch information
mcraiha committed Nov 18, 2023
1 parent 9df2394 commit fd80236
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 25 deletions.
4 changes: 2 additions & 2 deletions Dithery-cli.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFrameworks>netcoreapp3.1;net5.0;net6.0;net7.0;net8.0</TargetFrameworks>
<TargetFrameworks>net6.0;net7.0;net8.0</TargetFrameworks>
<RootNamespace>Dithery_cli</RootNamespace>

<PackAsTool>true</PackAsTool>
Expand All @@ -19,7 +19,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="System.Drawing.Common" Version="5.0.2" />
<PackageReference Include="SkiaSharp" Version="2.88.6" />
<PackageReference Include="LibDithering" Version="1.0.1" />
</ItemGroup>

Expand Down
11 changes: 6 additions & 5 deletions FileWriting.cs
Original file line number Diff line number Diff line change
@@ -1,28 +1,29 @@
using System;
using System.IO;
using System.Drawing;
using SkiaSharp;

namespace Dithery_cli
{
public static class FileWriting
{
public static void DitherAndWritePngFile(string outputFilename, Bitmap bitmap, DitheringBase<byte> ditherer, bool writeToSameBitmap = true)
public static void DitherAndWritePngFile(string outputFilename, SKBitmap bitmap, DitheringBase<byte> ditherer, bool writeToSameBitmap = true)
{
byte[] bytes = ReadWriteBitmaps.ReadBitmapToColorBytes(bitmap);

TempByteImageFormat temp = new TempByteImageFormat(bytes, bitmap.Width, bitmap.Height, 3);
temp = (TempByteImageFormat)ditherer.DoDithering(temp);

using FileStream fs = new(outputFilename, FileMode.Create);
if (writeToSameBitmap)
{
ReadWriteBitmaps.WriteToBitmap(bitmap, temp.GetPixelChannels);
bitmap.Save(outputFilename, System.Drawing.Imaging.ImageFormat.Png);
bitmap.Encode(fs, SKEncodedImageFormat.Png, quality: 100);
}
else
{
Bitmap tempBitmap = new Bitmap(bitmap.Width, bitmap.Height, bitmap.PixelFormat);
SKBitmap tempBitmap = new SKBitmap(bitmap.Width, bitmap.Height, bitmap.ColorType, bitmap.AlphaType);
ReadWriteBitmaps.WriteToBitmap(tempBitmap, temp.GetPixelChannels);
tempBitmap.Save(outputFilename, System.Drawing.Imaging.ImageFormat.Png);
tempBitmap.Encode(fs, SKEncodedImageFormat.Png, quality: 100);
}
}
}
Expand Down
11 changes: 6 additions & 5 deletions Program.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
using System;
using System.IO;
using System.Collections.Generic;
using System.Drawing;
using SkiaSharp;

using System.Linq;

namespace Dithery_cli
Expand Down Expand Up @@ -163,7 +164,7 @@ static void Main(string[] args)
valuesAsList.Remove(DitheringMethod.None);

using(FileStream bitmapStream = new FileStream(inputFile, FileMode.Open, FileAccess.Read))
using(var image = new Bitmap(bitmapStream))
using(var image = SKBitmap.Decode(bitmapStream))
{
if (outputFormat == OutputFormat.SingleImage)
{
Expand All @@ -184,7 +185,7 @@ static void Main(string[] args)
List<(MemoryStream pngData, string text)> images = new List<(MemoryStream pngData, string text)>();

MemoryStream originalImageMemoryStream = new MemoryStream();
image.Save(originalImageMemoryStream, System.Drawing.Imaging.ImageFormat.Png);
image.Encode(originalImageMemoryStream, SKEncodedImageFormat.Png, quality: 100);
images.Add((originalImageMemoryStream, "Original"));

foreach (DitheringMethod ditheringMethod in valuesAsList)
Expand All @@ -203,7 +204,7 @@ static void Main(string[] args)
{
DitheringBase<byte> ditherer = GetDitherer(dithering, colorReductionMethod);
using(FileStream bitmapStream = new FileStream(inputFile, FileMode.Open, FileAccess.Read))
using(var image = new Bitmap(bitmapStream))
using(var image = SKBitmap.Decode(bitmapStream))
{
if (outputFormat == OutputFormat.SingleImage)
{
Expand All @@ -212,7 +213,7 @@ static void Main(string[] args)
else if (outputFormat == OutputFormat.HTMLBasic)
{
MemoryStream originalImageMemoryStream = new MemoryStream();
image.Save(originalImageMemoryStream, System.Drawing.Imaging.ImageFormat.Png);
image.Encode(originalImageMemoryStream, SKEncodedImageFormat.Png, quality: 100);

MemoryStream ditheredImageMemoryStream = new MemoryStream();
StreamWriting.DitherAndWritePngStream(ditheredImageMemoryStream, image, ditherer);
Expand Down
16 changes: 8 additions & 8 deletions ReadWriteBitmaps.cs
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
using System;
using System.IO;
using System.Drawing;
using SkiaSharp;

namespace Dithery_cli
{
public static class ReadWriteBitmaps
{
public static void WriteToBitmap(Bitmap bitmap, Func<int, int, byte[]> reader)
public static void WriteToBitmap(SKBitmap bitmap, Func<int, int, byte[]> reader)
{
for (int y = 0; y < bitmap.Height; y++)
{
for (int x = 0; x < bitmap.Width; x++)
{
byte[] read = reader(x, y);
Color color = Color.FromArgb((byte)read[0], (byte)read[1], (byte)read[2]);
SKColor color = new SKColor((byte)read[0], (byte)read[1], (byte)read[2]);
bitmap.SetPixel(x, y, color);
}
}
}

public static byte[] ReadBitmapToColorBytes(Bitmap bitmap)
public static byte[] ReadBitmapToColorBytes(SKBitmap bitmap)
{
int width = bitmap.Width;
int height = bitmap.Height;
Expand All @@ -30,11 +30,11 @@ public static byte[] ReadBitmapToColorBytes(Bitmap bitmap)
{
for (int x = 0; x < bitmap.Width; x++)
{
Color color = bitmap.GetPixel(x, y);
SKColor color = bitmap.GetPixel(x, y);
int arrayIndex = y * width * channelsPerPixel + x * channelsPerPixel;
returnValue[arrayIndex + 0] = color.R;
returnValue[arrayIndex + 1] = color.G;
returnValue[arrayIndex + 2] = color.B;
returnValue[arrayIndex + 0] = color.Red;
returnValue[arrayIndex + 1] = color.Green;
returnValue[arrayIndex + 2] = color.Blue;
}
}

Expand Down
10 changes: 5 additions & 5 deletions StreamWriting.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
using System.IO;
using System.Drawing;
using SkiaSharp;

namespace Dithery_cli
{
public static class StreamWriting
{
public static void DitherAndWritePngStream(Stream outputStream, Bitmap bitmap, DitheringBase<byte> ditherer, bool writeToSameBitmap = true)
public static void DitherAndWritePngStream(Stream outputStream, SKBitmap bitmap, DitheringBase<byte> ditherer, bool writeToSameBitmap = true)
{
byte[] bytes = ReadWriteBitmaps.ReadBitmapToColorBytes(bitmap);

Expand All @@ -15,13 +15,13 @@ public static void DitherAndWritePngStream(Stream outputStream, Bitmap bitmap, D
if (writeToSameBitmap)
{
ReadWriteBitmaps.WriteToBitmap(bitmap, temp.GetPixelChannels);
bitmap.Save(outputStream, System.Drawing.Imaging.ImageFormat.Png);
bitmap.Encode(outputStream, SKEncodedImageFormat.Png, quality: 100);
}
else
{
Bitmap tempBitmap = new Bitmap(bitmap.Width, bitmap.Height, bitmap.PixelFormat);
SKBitmap tempBitmap = new SKBitmap(bitmap.Width, bitmap.Height, bitmap.ColorType, bitmap.AlphaType);
ReadWriteBitmaps.WriteToBitmap(tempBitmap, temp.GetPixelChannels);
tempBitmap.Save(outputStream, System.Drawing.Imaging.ImageFormat.Png);
tempBitmap.Encode(outputStream, SKEncodedImageFormat.Png, quality: 100);
}
}
}
Expand Down

0 comments on commit fd80236

Please sign in to comment.