PdfPig icon indicating copy to clipboard operation
PdfPig copied to clipboard

page.AddPng() does not handle images without a Transparency channel

Open cspwcspw opened this issue 3 years ago • 4 comments
trafficstars

Since these are both your packages (thanks) 👍 :

I build a png file using BigGustave and use the byte array to add the image to a pdf. If the PngBuilder Transparency channel is false when I create the png, AddPng seems to get the pixel stride wrong, with interlaced lines, etc. My other tools seem work fine with either png (e.g. file previewer, Paint.Net)

bug

cspwcspw avatar Oct 15 '22 04:10 cspwcspw

I tried a few variations of the image and workflow described but cannot seem to reproduce this. If you're able to share a source.png that causes the issue I'm happy to reopen and take another look but the problem may have been resolved by changes since it was opened? Closing for now.

EliotJones avatar May 28 '23 17:05 EliotJones

Thanks Eliot. I should have submitted a proper minimal test case when I submitted the ticket. I've replicated the issue so there is a small command-line program inline here, with the two resulting png images (one generated with and one without the alpha channel), and then the PDF with both images.

using BigGustave;
using System;
using System.IO;
using UglyToad.PdfPig.Content;
using UglyToad.PdfPig.Core;
using UglyToad.PdfPig.Writer;

namespace PngBug
{
    internal class Program
    {
        const string PathPrefix = "C:\\temp\\";   // change this
        const int imgSize = 64;

        static void Main(string[] args)
        {
            File.WriteAllBytes(PathPrefix + "TestPngWithAlpha.png", MakePng(true));
            File.WriteAllBytes(PathPrefix + "TestPngNoAlpha.png", MakePng(false));
          
            // Now write both these into a PDF.  They come out different on my system.  
            File.WriteAllBytes(PathPrefix + "TestPdf.pdf", MakePdf(MakePng(true), MakePng(false)));
        }

        static byte[] MakePdf(byte[] imgWithAlpha, byte[] imgWithoutAlpha)
        {
            PdfDocumentBuilder builder = new PdfDocumentBuilder();
            PdfPageBuilder page = builder.AddPage(PageSize.A4);
            var placementWith = new PdfRectangle(100, 700, 100+imgSize, 700+imgSize);
            page.AddPng(imgWithAlpha, placementWith);
            var placementWithout = new PdfRectangle(300, 700, 300 + imgSize, 700 + imgSize);
            page.AddPng(imgWithoutAlpha, placementWithout);
            byte[] buf = builder.Build();
            return buf;
        }

        public static byte[] MakePng(Boolean useAlphaChannel)
        {
            PngBuilder builder = PngBuilder.Create(imgSize, imgSize, useAlphaChannel);

            for (int r = 0; r < imgSize; r++)
            {
                for (int c = 0; c < imgSize; c++)
                {
                    Pixel px = r < c ? new Pixel(0, 0, 255) : r > c ? new Pixel(255, 0, 0) : new Pixel(0, 0, 0);
                    builder.SetPixel(px, r, c);
                }
            }

            using (var memory = new MemoryStream())
            {
                builder.Save(memory);
                return memory.ToArray();
            }
        }
    }
}

TestPdf.pdf TestPngNoAlpha TestPngWithAlpha

cspwcspw avatar May 30 '23 16:05 cspwcspw

I've had another look with the example you provided, thanks.

It appears the issue was to do with calculation of BitsPerComponent which was fixed here https://github.com/UglyToad/PdfPig/commit/65ff0cb54edbf1f16861bb903faad9652e7a752f#diff-ffb8dc0dfd0b8bc578f2ecd4517965154295f4b8900e71ed5948bc2325dd12ee

Running your sample code with the latest version results in the left file, versus the right hand file which was generated before this fix I assume:

image

If you can try with the latest pre-release versions the problem should now be fixed.

EliotJones avatar Jun 04 '23 10:06 EliotJones

Thanks Eliot. I confirm this is now fixed.

Kind regards Peter

On Sun, Jun 4, 2023 at 12:35 PM Eliot Jones @.***> wrote:

I've had another look with the example you provided, thanks.

It appears the issue was to do with calculation of BitsPerComponent which was fixed here 65ff0cb #diff-ffb8dc0dfd0b8bc578f2ecd4517965154295f4b8900e71ed5948bc2325dd12ee https://github.com/UglyToad/PdfPig/commit/65ff0cb54edbf1f16861bb903faad9652e7a752f#diff-ffb8dc0dfd0b8bc578f2ecd4517965154295f4b8900e71ed5948bc2325dd12ee

Running your sample code with the latest version results in the left file, versus the right hand file which was generated before this fix I assume:

[image: image] https://user-images.githubusercontent.com/7862346/243171007-76c3efea-826d-44d4-86a2-dee37779234f.png

If you can try with the latest pre-release versions the problem should now be fixed.

— Reply to this email directly, view it on GitHub https://github.com/UglyToad/PdfPig/issues/499#issuecomment-1575510330, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAL7LTQIKZTRG67TPYD7SETXJRQHRANCNFSM6AAAAAARFXXAEA . You are receiving this because you authored the thread.Message ID: @.***>

cspwcspw avatar Jun 06 '23 09:06 cspwcspw