ststeiger / PdfSharpCore

Port of the PdfSharp library to .NET Core - largely removed GDI+ (only missing GetFontData - which can be replaced with freetype2)
Other
1.05k stars 234 forks source link

DrawImage from TIFF 1bit source to PDF using CCITT #368

Open sommcz opened 1 year ago

sommcz commented 1 year ago

Is there any option how to DrawImage using CCITT tiff compression (using 1bit per pixel).

I can read and write 1bit tiff with ImageSharp with no problem, but when i create Image using XImage and DrawImage to PDF, it is stored as multi byte RGB image (which is much bigger and conversion takes longer).

I tried reading file directly with XImage.FromFile, but result is the same.

I tried the same scenario with iText7 and image is stored in PDF correctly in 1bit format and conversion takes about 3s.

Simple code for conversion with Ximage.FromFile (just single frame version):

            using var image = XImage.FromFile(@"d:\tmp.tiff");
            var gfx = XGraphics.FromPdfPage(page);
            page.Width = image.PointWidth;
            page.Height = image.PointHeight;

            gfx.DrawImage(image, 0, 0, image.PointWidth, image.PointHeight);

Conversion of all frames:

        using var outDoc = new PdfDocument();
        using var img = Image.Load(inputFile.OpenReadStream(),
            new TiffDecoder() {DecodingMode = FrameDecodingMode.All});

        for (var i = 0; i < img.Frames.Count; i++)
        {
            var frameImg = img.Frames.CloneFrame(i) as Image<Rgba32>; CloneFrame returns rgba32
            using var image =
                XImage.FromImageSource(
                    ImageSharpImageSource<Rgba32>.FromImageSharpImage(frameImg, TiffFormat.Instance, 90));

            var page = outDoc.AddPage();
            var gfx = XGraphics.FromPdfPage(page);
            page.Width = image.PointWidth;
            page.Height = image.PointHeight;

            gfx.DrawImage(image, 0, 0);
        }

Both versions result in the same big PDF.

sommcz commented 1 year ago

Is there anyone who can implement this feature? How much effort it could take? Please let me know somm@somm.cz. Thank you.