Skip to content

Commit

Permalink
Added a CustomPixelSource (disabled because corrupted image are created)
Browse files Browse the repository at this point in the history
Trying to reduce going from and to bitmapand byte[] and speed up the resizing.
  • Loading branch information
maforget committed Jun 16, 2024
1 parent 4792c12 commit 4a0f84a
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 2 deletions.
5 changes: 3 additions & 2 deletions cYo.Common/Drawing/BitmapExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,10 @@ private static Bitmap MagicScalerResampler(Bitmap bmp, Size size, BitmapResampli

//var IsAlpha = ((ImageFlags)bmp.Flags & ImageFlags.HasAlpha) != 0;
var IsAlpha = bmp.HasTransparency();
byte[] imgArray = IsAlpha ? bmp.ImageToBytes(ImageFormat.Png) : bmp.ImageToJpegBytes();
byte[] imgArray = IsAlpha ? bmp.ImageToBytes(ImageFormat.Png) : bmp.ImageToJpegBytes();
//IPixelSource imgArray = new CustomPixelSource(bmp, format);

Task.Run(() => MagicImageProcessor.ProcessImage(imgArray, ms, ps)).Wait();
Task.Run(() => MagicImageProcessor.ProcessImage(imgArray, ms, ps)).Wait();

bmp?.Dispose();
return Image.FromStream(ms) as Bitmap;
Expand Down
80 changes: 80 additions & 0 deletions cYo.Common/Drawing/CustomPixelSource.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Runtime;
using System.Threading;
using System.Threading.Tasks;
using PhotoSauce.MagicScaler;

namespace cYo.Common.Drawing
{
public class CustomPixelSource : BitmapPixelSource
{
private readonly Bitmap _bitmap;
private readonly BitmapData _bitmapData;
private readonly int dataLength;
private byte[] byteArray = null;

public CustomPixelSource(Bitmap bitmap, PixelFormat format = PixelFormat.Format32bppArgb)
: base(ConvertPixelFormat(format), bitmap.Width, bitmap.Height, GetStride(bitmap))
{
_bitmap = bitmap;
var rect = new Rectangle(0, 0, bitmap.Width, bitmap.Height);
_bitmapData = bitmap.LockBits(rect, ImageLockMode.ReadOnly, bitmap.PixelFormat);
dataLength = Math.Abs(base.Stride) * bitmap.Height;
}

private static Guid ConvertPixelFormat(PixelFormat format)
{
switch (format)
{
case PixelFormat.Format24bppRgb:
return PixelFormats.Bgr24bpp;
case PixelFormat.Format8bppIndexed:
return PixelFormats.Grey8bpp;
default:
return PixelFormats.Bgra32bpp;
}
}

private static int GetStride(Bitmap bitmap)
{
var rect = new Rectangle(0, 0, bitmap.Width, bitmap.Height);
BitmapData _bitmapData = bitmap.LockBits(rect, ImageLockMode.ReadOnly, bitmap.PixelFormat);

try
{
return _bitmapData.Stride;
}
finally
{
bitmap.UnlockBits(_bitmapData);
}
}

private ReadOnlySpan<byte> GetSpan()
{
if (byteArray == null)
{
// Store bitmap data inside byte array
byteArray = new byte[dataLength];

// Copy the bitmap data to the byte array.
System.Runtime.InteropServices.Marshal.Copy(_bitmapData.Scan0, byteArray, 0, dataLength);

// Return a ReadOnlySpan<byte> from the byte array.
return new ReadOnlySpan<byte>(byteArray);
}
return byteArray;
}

protected override ReadOnlySpan<byte> Span => GetSpan();
}
}

1 change: 1 addition & 0 deletions cYo.Common/cYo.Common.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@
<Compile Include="Drawing\ColorSchema.cs" />
<Compile Include="Drawing\ColorSchemaCollection.cs" />
<Compile Include="Drawing\ConvolutionMatrix.cs" />
<Compile Include="Drawing\CustomPixelSource.cs" />
<Compile Include="Drawing\EnumExtensions.cs" />
<Compile Include="Drawing\FastBitmap.cs" />
<Compile Include="Drawing\FastBitmapLock.cs" />
Expand Down

0 comments on commit 4a0f84a

Please sign in to comment.