Skip to content

Commit a040538

Browse files
committed
fixed argb to bitmap error
1 parent 2480b02 commit a040538

9 files changed

Lines changed: 232 additions & 280 deletions

File tree

OpenSlideSharp.Common.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
<!--- Package information, Version -->
55
<PropertyGroup>
6-
<PackageVersion>1.0.0.2</PackageVersion>
6+
<PackageVersion>1.0.0.3</PackageVersion>
77
<Authors>IOL0ol1</Authors>
88
<Copyright>Copyright © 2021-2022</Copyright>
99
<PackageLicenseExpression>MIT</PackageLicenseExpression>

src/OpenSlideSharp.BitmapExtensions/AssociatedImageExtensions.cs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,10 @@ public static class AssociatedImageExtensions
1919
public unsafe static Bitmap ToBitmap(this AssociatedImage image)
2020
{
2121
if (image == null) throw new NullReferenceException();
22-
var bitmap = new Bitmap((int)image.Dimensions.Width, (int)image.Dimensions.Height);
23-
var bitmapData = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);
24-
var bytesPerLine = (int)Math.Min(image.Dimensions.Width * 4, bitmapData.Stride);
25-
for (int i = 0; i < bitmap.Height; i++)
22+
fixed (byte* scan0 = image.Data)
2623
{
27-
Marshal.Copy(image.Data, (int)image.Dimensions.Width * 4 * i, (IntPtr)((byte*)bitmapData.Scan0 + (bitmapData.Stride * i)), bytesPerLine);
24+
return new Bitmap((int)image.Dimensions.Width, (int)image.Dimensions.Height, (int)image.Dimensions.Width * 4, PixelFormat.Format32bppArgb, (IntPtr)scan0);
2825
}
29-
bitmap.UnlockBits(bitmapData);
30-
return bitmap;
3126
}
3227

3328
/// <summary>

src/OpenSlideSharp.BitmapExtensions/BitmapExtensions.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public static MemoryStream ToStream(this Image bitmap, ImageFormat format = null
3939
var ms = new MemoryStream();
4040
EncoderParameters parameters = quality.HasValue ? new EncoderParameters() { Param = new[] { new EncoderParameter(Encoder.Quality, quality.Value) } } : null;
4141
bitmap.Save(ms, (format??ImageFormat.Jpeg).FindCodec(), parameters);
42+
ms.Seek(0, SeekOrigin.Begin);
4243
return ms;
4344
}
4445

src/OpenSlideSharp.BitmapExtensions/DeepZoomGeneratorExtensions.cs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
using System.Drawing;
33
using System.Drawing.Imaging;
44
using System.IO;
5-
5+
using System.Runtime.InteropServices;
66
using static OpenSlideSharp.DeepZoomGenerator;
77

88
namespace OpenSlideSharp
@@ -42,7 +42,7 @@ public static byte[] GetTileAsJpeg(this DeepZoomGenerator generator, int level,
4242
/// <returns></returns>
4343
public static MemoryStream GetTileAsJpegStream(this DeepZoomGenerator generator, int level, int col, int row, out TileInfo tileInfo, int? quality = null)
4444
{
45-
return GetTileImageStream(generator, level, col, row, out tileInfo, quality).ToStream(ImageFormat.Jpeg, quality);
45+
return GetTileImage(generator, level, col, row, out tileInfo, quality).ToStream(ImageFormat.Jpeg, quality);
4646

4747
}
4848

@@ -76,7 +76,7 @@ public static byte[] GetTileAsPng(this DeepZoomGenerator generator, int level, i
7676
/// <returns></returns>
7777
public static MemoryStream GetTileAsPngStream(this DeepZoomGenerator generator, int level, int col, int row, out TileInfo tileInfo, int? quality = null)
7878
{
79-
return GetTileImageStream(generator, level, col, row, out tileInfo, quality).ToStream(ImageFormat.Png, quality);
79+
return GetTileImage(generator, level, col, row, out tileInfo, quality).ToStream(ImageFormat.Png, quality);
8080
}
8181

8282
/// <summary>
@@ -89,14 +89,15 @@ public static MemoryStream GetTileAsPngStream(this DeepZoomGenerator generator,
8989
/// <param name="tileInfo"></param>
9090
/// <param name="quality"></param>
9191
/// <returns></returns>
92-
public static Bitmap GetTileImageStream(this DeepZoomGenerator generator, int level, int col, int row, out TileInfo tileInfo, int? quality = null)
92+
public unsafe static Bitmap GetTileImage(this DeepZoomGenerator generator, int level, int col, int row, out TileInfo tileInfo, int? quality = null)
9393
{
9494
if (generator == null)
9595
throw new NullReferenceException();
96-
var data = generator.GetTile(level, col, row, out tileInfo);
97-
return new Bitmap(new MemoryStream(data));
96+
var raw = generator.GetTile(level, col, row, out tileInfo);
97+
fixed (byte* scan0 = raw)
98+
{
99+
return new Bitmap((int)tileInfo.Width, (int)tileInfo.Height, (int)tileInfo.Width * 4, PixelFormat.Format32bppArgb, (IntPtr)scan0);
100+
}
98101
}
99102
}
100-
101-
102103
}

src/OpenSlideSharp.BitmapExtensions/OpenSlideImageExtensions.cs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,10 @@ public unsafe static Bitmap ReadRegionImage(this OpenSlideImage image, int level
2525
{
2626
if (image == null) throw new NullReferenceException();
2727
var data = image.ReadRegion(level, x, y, width, height);
28-
var bitmap = new Bitmap((int)width, (int)height);
29-
var bitmapData = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);
30-
var bytesPerLine = (int)Math.Min(width * 4, bitmapData.Stride);
31-
for (int i = 0; i < bitmap.Height; i++)
28+
fixed (byte* scan0 = data)
3229
{
33-
Marshal.Copy(data, (int)width * 4 * i, (IntPtr)((byte*)bitmapData.Scan0 + (bitmapData.Stride * i)), bytesPerLine);
30+
return new Bitmap((int)width, (int)height, (int)width * 4, PixelFormat.Format32bppArgb, (IntPtr)scan0);
3431
}
35-
bitmap.UnlockBits(bitmapData);
36-
return bitmap;
3732
}
3833

3934
/// <summary>

src/OpenSlideSharp.BruTile/Util.cs

Lines changed: 17 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -104,35 +104,31 @@ public class ImageUtil
104104
/// <param name="height">height</param>
105105
/// <param name="dstWidth">dst width</param>
106106
/// <param name="dstHeight">dst height</param>
107+
/// <param name="quality">jpeg quality</param>
107108
/// <returns></returns>
108-
public static byte[] Raw2Jpeg(byte[] raw, int bytesPerPixel, int bytesPerLine, int width, int height, int dstWidth = 0, int dstHeight = 0)
109+
public static unsafe byte[] Raw2Jpeg(byte[] raw, int bytesPerPixel, int bytesPerLine, int width, int height, int dstWidth = 0, int dstHeight = 0, int? quality = null)
109110
{
110111
if (raw == null) return null;
111112
if (bytesPerPixel != 3 && bytesPerPixel != 4) throw new ArgumentException(nameof(bytesPerPixel));
112113
var pixel = bytesPerPixel == 3 ? PixelFormat.Format24bppRgb : PixelFormat.Format32bppRgb;
113-
using (var bmp = new Bitmap(width, height))
114+
fixed (byte* scan0 = raw)
114115
{
115-
var data = bmp.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.WriteOnly, pixel);
116-
var lineSize = Math.Min(bytesPerLine, data.Stride);
117-
for (int i = 0; i < height; i++)
116+
using (var bmp = new Bitmap(width, height, bytesPerLine, pixel, (IntPtr)scan0))
118117
{
119-
Marshal.Copy(raw, bytesPerLine * i, data.Scan0 + data.Stride * i, lineSize);
120-
}
121-
bmp.UnlockBits(data);
122118

123-
// fill
124-
if ((dstWidth <= 0 && dstHeight <= 0) || (dstWidth == width && dstHeight == height))
125-
{
126-
return bmp.ToArray(ImageFormat.Jpeg);
127-
}
128-
else
129-
{
130-
using (var dstImage = new Bitmap(dstWidth, dstHeight))
131-
using (var g = Graphics.FromImage(dstImage))
119+
if ((dstWidth <= 0 && dstHeight <= 0) || (dstWidth == width && dstHeight == height))
132120
{
133-
g.Clear(Color.White);
134-
g.DrawImage(bmp, new Point(0, 0));
135-
return dstImage.ToArray(ImageFormat.Jpeg);
121+
return bmp.ToArray(ImageFormat.Jpeg, quality);
122+
}
123+
else // fill
124+
{
125+
using (var dstImage = new Bitmap(dstWidth, dstHeight))
126+
using (var g = Graphics.FromImage(dstImage))
127+
{
128+
g.Clear(Color.White);
129+
g.DrawImage(bmp, new Point(0, 0));
130+
return dstImage.ToArray(ImageFormat.Jpeg, quality);
131+
}
136132
}
137133
}
138134
}
@@ -142,17 +138,7 @@ public static BgraData Jpeg2Bgra(byte[] jpeg)
142138
{
143139
return new BgraData(jpeg, false);
144140
}
145-
146-
public static BgraData Jpeg2Bgra2(byte[] jpeg)
147-
{
148-
using (var mat = Mat.ImDecode(jpeg, ImreadModes.Color))
149-
{
150-
byte[] bgra = new byte[mat.DataEnd.ToInt64() - mat.DataStart.ToInt64()];
151-
Marshal.Copy(mat.Data, bgra, 0, bgra.Length);
152-
return new BgraData(mat.Width, mat.Height, (int)mat.Step(), bgra, mat.Channels());
153-
}
154-
}
155-
141+
156142
/// <summary>
157143
/// Join by <paramref name="srcPixelTiles"/> and cut by <paramref name="srcPixelExtent"/> then scale to <paramref name="dstPixelExtent"/>(only height an width is useful).
158144
/// </summary>

src/OpenSlideSharp/AssociatedImage.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public AssociatedImage(ImageDimension dimensions, byte[] data)
2222
public ImageDimension Dimensions { get; private set; }
2323

2424
/// <summary>
25-
/// Associated image argb data
25+
/// Associated image ARGB data
2626
/// </summary>
2727
public byte[] Data { get; private set; }
2828
}

0 commit comments

Comments
 (0)