Skip to content

Commit 0cf21c5

Browse files
committed
update(PDF-2231): Address PR Review Feedbacks
1 parent 6c3a9c9 commit 0cf21c5

2 files changed

Lines changed: 44 additions & 6 deletions

File tree

IronSoftware.Drawing/IronSoftware.Drawing.Common.Tests/UnitTests/AnyBitmapFunctionality.cs

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -676,6 +676,23 @@ public void CreateMultiFrameTiffStream_Empty_Sequence_Throws()
676676
act.Should().Throw<ArgumentException>();
677677
}
678678

679+
[FactWithAutomaticDisplayName]
680+
public void CreateMultiFrameTiffBytes_PreservesResolution_FromPixelsPerMeterSource()
681+
{
682+
const double dpi = 300d;
683+
double pixelsPerMetre = dpi / 0.0254d; // 300 DPI expressed in pixels per metre
684+
685+
using var page = MakeBitmapWithResolution(220, 300, new Rgb24(40, 90, 160),
686+
pixelsPerMetre, PixelResolutionUnit.PixelsPerMeter);
687+
688+
byte[] tiff = AnyBitmap.CreateMultiFrameTiffBytes(new[] { page });
689+
var pages = ReadTiffDirectories(tiff);
690+
691+
pages.Should().ContainSingle();
692+
ToDotsPerInch(pages[0].XResolution, pages[0].ResolutionUnit)
693+
.Should().BeApproximately(dpi, 2d);
694+
}
695+
679696
[FactWithAutomaticDisplayName]
680697
public void CreateMultiFrameTiff_Preserves_Rgb24_Pixels()
681698
{
@@ -708,11 +725,17 @@ public void CreateMultiFrameTiff_Preserves_Rgb24_Pixels()
708725
}
709726

710727
private static AnyBitmap CreateSolidBitmap(int width, int height, Rgb24 color, int dpi)
728+
{
729+
return MakeBitmapWithResolution(width, height, color, dpi, PixelResolutionUnit.PixelsPerInch);
730+
}
731+
732+
private static AnyBitmap MakeBitmapWithResolution(int width, int height, Rgb24 color,
733+
double resolution, PixelResolutionUnit unit)
711734
{
712735
var image = new SixLabors.ImageSharp.Image<Rgb24>(width, height, color);
713-
image.Metadata.HorizontalResolution = dpi;
714-
image.Metadata.VerticalResolution = dpi;
715-
image.Metadata.ResolutionUnits = PixelResolutionUnit.PixelsPerInch;
736+
image.Metadata.HorizontalResolution = resolution;
737+
image.Metadata.VerticalResolution = resolution;
738+
image.Metadata.ResolutionUnits = unit;
716739
return image;
717740
}
718741

IronSoftware.Drawing/IronSoftware.Drawing.Common/AnyBitmap.cs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3420,10 +3420,20 @@ private static void InternalSaveAsMultiPageTiff(IEnumerable<Image> images, Strea
34203420
}
34213421
break;
34223422
case Image<Abgr32> imageAsFormat:
3423-
imageAsFormat.CopyPixelDataTo(buffer);
3423+
{
3424+
// 4 bytes/pixel, but the bytes are A,B,G,R. The wrong sample
3425+
// order for PHOTOMETRIC.RGB (which expects R,G,B,A). Convert to
3426+
// Rgba32 so the channels are not written swapped.
3427+
using var rgba = imageAsFormat.CloneAs<Rgba32>();
3428+
rgba.CopyPixelDataTo(buffer);
3429+
}
34243430
break;
34253431
case Image<Argb32> imageAsFormat:
3426-
imageAsFormat.CopyPixelDataTo(buffer);
3432+
{
3433+
// Bytes are A,R,G,B; convert to Rgba32 for correct channel order.
3434+
using var rgba = imageAsFormat.CloneAs<Rgba32>();
3435+
rgba.CopyPixelDataTo(buffer);
3436+
}
34273437
break;
34283438
case Image<Bgr24> imageAsFormat:
34293439
{
@@ -3435,7 +3445,12 @@ private static void InternalSaveAsMultiPageTiff(IEnumerable<Image> images, Strea
34353445
}
34363446
break;
34373447
case Image<Bgra32> imageAsFormat:
3438-
imageAsFormat.CopyPixelDataTo(buffer);
3448+
{
3449+
// Bytes are B,G,R,A; convert to Rgba32 so they are not written
3450+
// channel-swapped under PHOTOMETRIC.RGB.
3451+
using var rgba = imageAsFormat.CloneAs<Rgba32>();
3452+
rgba.CopyPixelDataTo(buffer);
3453+
}
34393454
break;
34403455
default:
34413456
(image as Image<Rgba32>).CopyPixelDataTo(buffer);

0 commit comments

Comments
 (0)