-
-
Notifications
You must be signed in to change notification settings - Fork 20
PDF-2231: Per-page multi-page TIFF (mixed orientation) + resolution/Rgb24 writer fixes #150
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1073,6 +1073,64 @@ public static AnyBitmap CreateMultiFrameTiff(IEnumerable<AnyBitmap> images) | |
| return FromStream(stream); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Combines multiple images into a multi-page TIFF and returns the raw TIFF | ||
| /// as a <see cref="MemoryStream"/>. | ||
| /// <para>Unlike <see cref="CreateMultiFrameTiff(IEnumerable{AnyBitmap})"/>, each | ||
| /// page keeps its own dimensions, orientation and resolution. Pages are not | ||
| /// scaled to a common size. This makes it suitable for mixed-orientation | ||
| /// documents (for example portrait + landscape pages).</para> | ||
| /// <para>The result is the encoded TIFF itself, not an <see cref="AnyBitmap"/>, | ||
| /// so it avoids the lossy round-trip through a single ImageSharp image (which | ||
| /// cannot represent frames of differing sizes).</para> | ||
| /// </summary> | ||
| /// <param name="images">Images to combine, one per TIFF page.</param> | ||
| /// <returns>A <see cref="MemoryStream"/> positioned at 0 containing the multi-page TIFF.</returns> | ||
| public static MemoryStream CreateMultiFrameTiffStream(IEnumerable<AnyBitmap> images) | ||
| { | ||
| if (images == null) | ||
| throw new ArgumentNullException(nameof(images)); | ||
|
|
||
| var frames = new List<Image>(); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Low: Nice touch forcing every frame through
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The suggested lazy + per-frame-dispose can't be done safely in |
||
| try | ||
| { | ||
| foreach (var image in images) | ||
| { | ||
| if (image == null) | ||
| throw new ArgumentException("The image sequence contains a null element.", nameof(images)); | ||
|
|
||
| frames.Add(((Image)image).CloneAs<Rgba32>()); | ||
| } | ||
|
|
||
| if (frames.Count == 0) | ||
| throw new ArgumentException("No images provided to create multi-frame TIFF.", nameof(images)); | ||
|
|
||
| var stream = new MemoryStream(); | ||
| InternalSaveAsMultiPageTiff(frames, stream); // already resets stream.Position to 0 | ||
| return stream; | ||
| } | ||
| finally | ||
| { | ||
| foreach (var frame in frames) | ||
| { | ||
| frame.Dispose(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Combines multiple images into a multi-page TIFF and returns the raw TIFF bytes. | ||
| /// <para>Each page keeps its own dimensions, orientation and resolution. Pages are | ||
| /// not scaled to a common size, making it suitable for mixed-orientation documents.</para> | ||
| /// </summary> | ||
| /// <param name="images">Images to combine, one per TIFF page.</param> | ||
| /// <returns>A byte array containing the multi-page TIFF.</returns> | ||
| public static byte[] CreateMultiFrameTiffBytes(IEnumerable<AnyBitmap> images) | ||
| { | ||
| using var stream = CreateMultiFrameTiffStream(images); | ||
| return stream.ToArray(); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Creates a multi-frame GIF image from multiple AnyBitmaps. | ||
| /// <para>All images should have the same dimension.</para> | ||
|
|
@@ -3351,19 +3409,48 @@ private static void InternalSaveAsMultiPageTiff(IEnumerable<Image> images, Strea | |
| switch (image) | ||
| { | ||
| case Image<Rgb24> imageAsFormat: | ||
| imageAsFormat.CopyPixelDataTo(buffer); | ||
| { | ||
| // Rgb24 is 3 bytes/pixel, but the buffer/stride above and the | ||
| // TIFF tags below are 4 samples/pixel (RGBA). Copying the 3-bpp | ||
| // data directly would leave each row short by 'width' bytes, | ||
| // shifting every subsequent row and corrupting the page. Convert | ||
| // to Rgba32 so the bytes line up with the 4-bpp layout. | ||
| using var rgba = imageAsFormat.CloneAs<Rgba32>(); | ||
| rgba.CopyPixelDataTo(buffer); | ||
| } | ||
| break; | ||
| case Image<Abgr32> imageAsFormat: | ||
| imageAsFormat.CopyPixelDataTo(buffer); | ||
| { | ||
| // 4 bytes/pixel, but the bytes are A,B,G,R. The wrong sample | ||
| // order for PHOTOMETRIC.RGB (which expects R,G,B,A). Convert to | ||
| // Rgba32 so the channels are not written swapped. | ||
| using var rgba = imageAsFormat.CloneAs<Rgba32>(); | ||
| rgba.CopyPixelDataTo(buffer); | ||
| } | ||
| break; | ||
| case Image<Argb32> imageAsFormat: | ||
| imageAsFormat.CopyPixelDataTo(buffer); | ||
| { | ||
| // Bytes are A,R,G,B; convert to Rgba32 for correct channel order. | ||
| using var rgba = imageAsFormat.CloneAs<Rgba32>(); | ||
| rgba.CopyPixelDataTo(buffer); | ||
| } | ||
| break; | ||
| case Image<Bgr24> imageAsFormat: | ||
| imageAsFormat.CopyPixelDataTo(buffer); | ||
| { | ||
| // Bgr24 is likewise 3 bytes/pixel; convert to Rgba32 for the | ||
| // same reason as Rgb24 above (this also yields the correct | ||
| // R,G,B sample order under PHOTOMETRIC.RGB). | ||
| using var rgba = imageAsFormat.CloneAs<Rgba32>(); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Low/Nit: Good fix here (and on the Rgb24 case) — the 3-bpp source packed into the 4-bpp buffer was misaligning every scanline, and
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Normalized all three to |
||
| rgba.CopyPixelDataTo(buffer); | ||
| } | ||
| break; | ||
| case Image<Bgra32> imageAsFormat: | ||
| imageAsFormat.CopyPixelDataTo(buffer); | ||
| { | ||
| // Bytes are B,G,R,A; convert to Rgba32 so they are not written | ||
| // channel-swapped under PHOTOMETRIC.RGB. | ||
| using var rgba = imageAsFormat.CloneAs<Rgba32>(); | ||
| rgba.CopyPixelDataTo(buffer); | ||
| } | ||
| break; | ||
| default: | ||
| (image as Image<Rgba32>).CopyPixelDataTo(buffer); | ||
|
|
@@ -3393,8 +3480,8 @@ private static void InternalSaveAsMultiPageTiff(IEnumerable<Image> images, Strea | |
| output.SetField(TiffTag.RESOLUTIONUNIT, ResUnit.CENTIMETER); | ||
| break; | ||
| case SixLabors.ImageSharp.Metadata.PixelResolutionUnit.PixelsPerMeter: | ||
| output.SetField(TiffTag.XRESOLUTION, image.Metadata.HorizontalResolution * 100); | ||
| output.SetField(TiffTag.YRESOLUTION, image.Metadata.VerticalResolution * 100); | ||
| output.SetField(TiffTag.XRESOLUTION, image.Metadata.HorizontalResolution / 100); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Medium: This
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added |
||
| output.SetField(TiffTag.YRESOLUTION, image.Metadata.VerticalResolution / 100); | ||
| output.SetField(TiffTag.RESOLUTIONUNIT, ResUnit.CENTIMETER); | ||
| break; | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Low/Nit: This DPI assertion only ever sees the
INCHbranch becauseCreateSolidBitmapsetsPixelsPerInch. TheToDotsPerInchhelper already handlesCENTIMETER, so adding aPixelsPerMetersource here would extend this same test to cover the actual resolution fix (see the Medium onAnyBitmap.cs).