-
Notifications
You must be signed in to change notification settings - Fork 325
adding support to tif images #1227
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
Merged
Merged
Changes from 17 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
d61deb0
adding support to tif images
c9b40d6
autoversion
7d24967
Merge branch 'UglyToad:master' into master
username77 bbda0fb
Merge branch 'master' of https://github.com/username77/PdfPig
519fd45
removed assembly version
099d0c8
removed generateonbuild
ef66439
cleaned
d5649a6
addung tests
f1902eb
CcittG4 tests
8a3df68
tiff to pdf write test
8494c64
cleaned
15ab6db
last clean
fee38d7
removed blackis to avoid double inverse and added EndOfBlock for some…
611406c
Merge branch 'UglyToad:master' into master
username77 20e6c9c
Merge upstream/master and preserve CCITT G4 writer tests
username77 6fcd3e6
Merge remote-tracking branch 'origin/master'
username77 21591bf
Merge branch 'UglyToad:master' into master
username77 fb477c4
Remove LibTiff-based CCITT fixture generator
username77 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
src/UglyToad.PdfPig.DocumentLayoutAnalysis/UglyToad.PdfPig.DocumentLayoutAnalysis.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
src/UglyToad.PdfPig.Tests/Images/Files/Tif/TiffCcittG4.ccitt.base64
Large diffs are not rendered by default.
Oops, something went wrong.
9 changes: 9 additions & 0 deletions
9
src/UglyToad.PdfPig.Tests/Images/Files/Tif/TiffCcittG4.fixture.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| { | ||
| "Width": 1800, | ||
| "Height": 3113, | ||
| "BlackIs1": false, | ||
| "Photometric": 0, | ||
| "FillOrder": 1, | ||
| "PayloadLength": 329546, | ||
| "SourceFile": "TiffCcittG4.tif" | ||
| } |
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
src/UglyToad.PdfPig.Tests/Writer/PdfPageBuilderCcittG4Tests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| namespace UglyToad.PdfPig.Tests.Writer | ||
| { | ||
| using System.IO; | ||
| using UglyToad.PdfPig.Content; | ||
| using System.Linq; | ||
| using UglyToad.PdfPig; | ||
| using UglyToad.PdfPig.Tests.Writer.TestImages; | ||
| using UglyToad.PdfPig.Tokens; | ||
| using UglyToad.PdfPig.Writer; | ||
| using Xunit; | ||
|
|
||
| public class PdfPageBuilderCcittG4Tests | ||
| { | ||
| [Fact] | ||
| public void FixtureProvidesRawCcittData() | ||
| { | ||
| var fixture = CcittG4TestImage.Load(); | ||
|
|
||
| Assert.True(fixture.Width > 0); | ||
| Assert.True(fixture.Height > 0); | ||
| Assert.NotEmpty(fixture.RawCcittData); | ||
| Assert.False(fixture.BlackIs1); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void AddCcittG4AddsFaxImage() | ||
| { | ||
| var fixture = CcittG4TestImage.Load(); | ||
|
|
||
| byte[] pdfBytes; | ||
| using (var documentBuilder = new PdfDocumentBuilder()) | ||
| { | ||
| var pageBuilder = documentBuilder.AddPage(fixture.Width, fixture.Height); | ||
|
|
||
| pageBuilder.AddCcittG4(fixture.RawCcittData, fixture.Width, fixture.Height, blackIs1: fixture.BlackIs1); | ||
|
|
||
| pdfBytes = documentBuilder.Build(); | ||
| } | ||
|
|
||
| File.WriteAllBytes(@"PdfPageBuilderTests_CanAddTifCcittG4.pdf", pdfBytes); | ||
|
|
||
| using (var document = PdfDocument.Open(pdfBytes)) | ||
| { | ||
| var page = document.GetPage(1); | ||
| var image = Assert.Single(page.GetImages()); | ||
|
|
||
| Assert.Equal(fixture.Width, image.WidthInSamples); | ||
| Assert.Equal(fixture.Height, image.HeightInSamples); | ||
| Assert.Equal(1, image.BitsPerComponent); | ||
| } | ||
| } | ||
|
|
||
| [Fact] | ||
| public void FixtureWritesDecodeZeroOne() | ||
| { | ||
| var fixture = CcittG4TestImage.Load(); | ||
| var image = BuildFaxImage(fixture.BlackIs1); | ||
|
|
||
| var decode = Assert.IsType<ArrayToken>(image.ImageDictionary.Data[NameToken.Decode]); | ||
|
|
||
| // The generated fixture comes from a TIFF with Photometric.MINISWHITE, which must map to | ||
| // blackIs1 = false for AddCcittG4. Reversing this to match TIFF naming reintroduces negative PDFs. | ||
| Assert.Equal([0, 1], decode.Data.OfType<NumericToken>().Select(x => x.Data).ToArray()); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void AddCcittG4WithBlackIs1TrueWritesDecodeOneZero() | ||
| { | ||
| var image = BuildFaxImage(blackIs1: true); | ||
|
|
||
| var decode = Assert.IsType<ArrayToken>(image.ImageDictionary.Data[NameToken.Decode]); | ||
|
|
||
| Assert.Equal([1, 0], decode.Data.OfType<NumericToken>().Select(x => x.Data).ToArray()); | ||
| Assert.True(image.ImageDictionary.TryGet(NameToken.DecodeParms, out DictionaryToken? decodeParms)); | ||
| Assert.False(decodeParms!.Data.ContainsKey(NameToken.BlackIs1)); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void AddCcittG4WithBlackIs1FalseWritesDecodeZeroOne() | ||
| { | ||
| var image = BuildFaxImage(blackIs1: false); | ||
|
|
||
| var decode = Assert.IsType<ArrayToken>(image.ImageDictionary.Data[NameToken.Decode]); | ||
|
|
||
| Assert.Equal([0, 1], decode.Data.OfType<NumericToken>().Select(x => x.Data).ToArray()); | ||
| Assert.True(image.ImageDictionary.TryGet(NameToken.DecodeParms, out DictionaryToken? decodeParms)); | ||
| Assert.False(decodeParms!.Data.ContainsKey(NameToken.BlackIs1)); | ||
| } | ||
|
|
||
| private static IPdfImage BuildFaxImage(bool blackIs1) | ||
| { | ||
| var fixture = CcittG4TestImage.Load(); | ||
|
|
||
| byte[] pdfBytes; | ||
| using (var documentBuilder = new PdfDocumentBuilder()) | ||
| { | ||
| var pageBuilder = documentBuilder.AddPage(fixture.Width, fixture.Height); | ||
|
|
||
| pageBuilder.AddCcittG4(fixture.RawCcittData, fixture.Width, fixture.Height, blackIs1: blackIs1); | ||
|
|
||
| pdfBytes = documentBuilder.Build(); | ||
| } | ||
|
|
||
| using var document = PdfDocument.Open(pdfBytes); | ||
| var page = document.GetPage(1); | ||
|
|
||
| return Assert.Single(page.GetImages()); | ||
| } | ||
| } | ||
| } |
92 changes: 92 additions & 0 deletions
92
src/UglyToad.PdfPig.Tests/Writer/TestImages/CcittG4TestImage.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| namespace UglyToad.PdfPig.Tests.Writer.TestImages | ||
| { | ||
| using System; | ||
| using System.IO; | ||
| using System.Text; | ||
| using System.Text.RegularExpressions; | ||
|
|
||
| /// <summary> | ||
| /// Helper for loading the CCITT Group 4 fixture used by PDF page builder tests. | ||
| /// </summary> | ||
| internal sealed class CcittG4TestImage | ||
| { | ||
| private CcittG4TestImage(int width, int height, byte[] rawCcittData, bool blackIs1) | ||
| { | ||
| Width = width; | ||
| Height = height; | ||
| RawCcittData = rawCcittData; | ||
| BlackIs1 = blackIs1; | ||
| } | ||
|
|
||
| public int Width { get; } | ||
|
|
||
| public int Height { get; } | ||
|
|
||
| public byte[] RawCcittData { get; } | ||
|
|
||
| public bool BlackIs1 { get; } | ||
|
|
||
| /// <summary> | ||
| /// Loads the CCITT Group 4 sample payload relative to the test output directory. | ||
| /// The payload is already raw CCITT data; width, height and polarity come from the generated metadata | ||
| /// derived from the original TIFF using the same extraction logic as the external converter. | ||
| /// </summary> | ||
| public static CcittG4TestImage Load() | ||
| { | ||
| var metadataPath = Path.GetFullPath(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Images", "Files", "Tif", | ||
| "TiffCcittG4.fixture.json")); | ||
| var base64Path = Path.GetFullPath(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Images", "Files", "Tif", | ||
| "TiffCcittG4.ccitt.base64")); | ||
| var metadataJson = File.ReadAllText(metadataPath, Encoding.UTF8); | ||
| var metadata = CcittG4FixtureMetadata.Parse(metadataJson); | ||
| var base64 = File.ReadAllText(base64Path, Encoding.ASCII) | ||
| .Replace("\r", string.Empty) | ||
| .Replace("\n", string.Empty) | ||
| .Trim(); | ||
| var rawCcittData = Convert.FromBase64String(base64); | ||
|
|
||
| return new CcittG4TestImage(metadata.Width, metadata.Height, rawCcittData, metadata.BlackIs1); | ||
| } | ||
|
|
||
| private sealed class CcittG4FixtureMetadata | ||
| { | ||
| public int Width { get; private set; } | ||
|
|
||
| public int Height { get; private set; } | ||
|
|
||
| public bool BlackIs1 { get; private set; } | ||
|
|
||
| public static CcittG4FixtureMetadata Parse(string json) | ||
| { | ||
| return new CcittG4FixtureMetadata | ||
| { | ||
| Width = ReadInt(json, "Width"), | ||
| Height = ReadInt(json, "Height"), | ||
| BlackIs1 = ReadBool(json, "BlackIs1") | ||
| }; | ||
| } | ||
|
|
||
| private static int ReadInt(string json, string propertyName) | ||
| { | ||
| var match = Regex.Match(json, $"\"{propertyName}\"\\s*:\\s*(\\d+)"); | ||
| if (!match.Success) | ||
| { | ||
| throw new InvalidOperationException($"Missing integer property '{propertyName}' in CCITT fixture metadata."); | ||
| } | ||
|
|
||
| return int.Parse(match.Groups[1].Value); | ||
| } | ||
|
|
||
| private static bool ReadBool(string json, string propertyName) | ||
| { | ||
| var match = Regex.Match(json, $"\"{propertyName}\"\\s*:\\s*(true|false)", RegexOptions.IgnoreCase); | ||
| if (!match.Success) | ||
| { | ||
| throw new InvalidOperationException($"Missing boolean property '{propertyName}' in CCITT fixture metadata."); | ||
| } | ||
|
|
||
| return bool.Parse(match.Groups[1].Value); | ||
| } | ||
| } | ||
| } | ||
| } |
2 changes: 1 addition & 1 deletion
2
src/UglyToad.PdfPig.Tokenization/UglyToad.PdfPig.Tokenization.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
| <PropertyGroup> | ||
| <OutputType>Exe</OutputType> | ||
| <TargetFramework>net8.0</TargetFramework> | ||
| <ImplicitUsings>enable</ImplicitUsings> | ||
| <Nullable>enable</Nullable> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <PackageReference Include="BitMiracle.LibTiff.NET" Version="2.4.660" /> | ||
| </ItemGroup> | ||
| </Project> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.