Skip to content

Commit

Permalink
fixed GetJpeg
Browse files Browse the repository at this point in the history
  • Loading branch information
IOL0ol1 committed Dec 14, 2022
1 parent 072500e commit 142b954
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 9 deletions.
1 change: 1 addition & 0 deletions example/SlideViewer/SlideViewer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<TargetFrameworks>net48;netcoreapp3.1</TargetFrameworks>
<UseWPF>true</UseWPF>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<Version>1.1.1</Version>
</PropertyGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion src/OpenSlideSharp.BruTile/OpenSlideSharp.BruTile.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<PackageReleaseNotes>OpenSlideSharp for BruTile</PackageReleaseNotes>
<Description>OpenSlideSharp for BruTile</Description>
<AllowedOutputExtensionsInPackageBuildOutputFolder>$(AllowedOutputExtensionsInPackageBuildOutputFolder);.pdb</AllowedOutputExtensionsInPackageBuildOutputFolder>
<Version>1.1.0</Version>
<Version>1.1.1</Version>
</PropertyGroup>

<ItemGroup>
Expand Down
18 changes: 14 additions & 4 deletions src/OpenSlideSharp.BruTile/Utilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Range = OpenCvSharp.Range;

namespace OpenSlideSharp.BruTile
{
Expand Down Expand Up @@ -53,6 +52,7 @@ public static unsafe byte[] GetJpeg(byte[] bgraBytes, int bytesPerPixel, int byt
var scalar = new Scalar((int)(background >> 24 & 0xFF), (int)(background >> 16 & 0xFF), (int)(background >> 8 & 0xFF), (int)(background & 0xFF));
using (var dst = new Mat(dstHeight, dstWidth, pixel, scalar))
{
src.SaveImage($"{Guid.NewGuid()}.jpg");
DrawImage(src, dst);
return dst.ToBytes(".jpg", prms);
}
Expand All @@ -71,11 +71,21 @@ private static void DrawImage(Mat src, Mat dst)
var fx = (double)dst.Width / src.Width;
var fy = (double)dst.Height / src.Height;
var fmin = Math.Min(fx, fy);
using (var srcResized = src.Resize(new Size(src.Width * fmin, src.Height * fmin)))
if (fmin < 1) // src > dst
{
using (var sub = new Mat(dst, new Rect(0, 0, srcResized.Width, srcResized.Height)))
using (var srcResized = src.Resize(new Size(src.Width * fmin, src.Height * fmin)))
{
srcResized.CopyTo(sub);
using (var sub = new Mat(dst, new Rect(0, 0, srcResized.Width, srcResized.Height)))
{
srcResized.CopyTo(sub);
}
}
}
else // src <= dst
{
using (var sub = new Mat(dst, new Rect(0, 0, src.Width, src.Height)))
{
src.CopyTo(sub);
}
}
}
Expand Down
16 changes: 13 additions & 3 deletions src/OpenSlideSharp.OpencvExtensions/JpegExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,21 @@ private static void DrawImage(Mat src, Mat dst)
var fx = (double)dst.Width / src.Width;
var fy = (double)dst.Height / src.Height;
var fmin = Math.Min(fx, fy);
using (var srcResized = src.Resize(new Size(src.Width * fmin, src.Height * fmin)))
if (fmin < 1) // src > dst
{
using (var sub = new Mat(dst, new Rect(0, 0, srcResized.Width, srcResized.Height)))
using (var srcResized = src.Resize(new Size(src.Width * fmin, src.Height * fmin)))
{
srcResized.CopyTo(sub);
using (var sub = new Mat(dst, new Rect(0, 0, srcResized.Width, srcResized.Height)))
{
srcResized.CopyTo(sub);
}
}
}
else // src <= dst
{
using (var sub = new Mat(dst, new Rect(0, 0, src.Width, src.Height)))
{
src.CopyTo(sub);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<PackageReleaseNotes>OpenSlideSharp opencv extensions</PackageReleaseNotes>
<Description>OpenSlideSharp opencv mat extensions</Description>
<AllowedOutputExtensionsInPackageBuildOutputFolder>$(AllowedOutputExtensionsInPackageBuildOutputFolder);.pdb</AllowedOutputExtensionsInPackageBuildOutputFolder>
<Version>1.1.0</Version>
<Version>1.1.1</Version>
</PropertyGroup>


Expand Down

0 comments on commit 142b954

Please sign in to comment.