PdfSharpCore icon indicating copy to clipboard operation
PdfSharpCore copied to clipboard

DrawImage from TIFF 1bit source to PDF using CCITT

Open sommcz opened this issue 2 years ago • 1 comments

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 avatar Jun 17 '23 14:06 sommcz

Is there anyone who can implement this feature? How much effort it could take? Please let me know [email protected]. Thank you.

sommcz avatar Aug 19 '23 16:08 sommcz