logo
down
shadow

Extracting image from PDF with /CCITTFaxDecode filter


Extracting image from PDF with /CCITTFaxDecode filter

By : bparees
Date : January 10 2021, 01:57 PM
this will help Actually, vbcrlfuser's answer did help me, but the code was not quite correct for the current version of BitMiracle.LibTiff.NET, as I could download it. In the current version, equivalent code looks like this:
code :


Share : facebook icon twitter icon
Save tiff CCITTFaxDecode (from PDF page) using iText and Java

Save tiff CCITTFaxDecode (from PDF page) using iText and Java


By : user3350012
Date : March 29 2020, 07:55 AM
Hope that helps I extracted a tiff image from scanned pdf (that is the every page as image) in the following way:
code :
...
PdfReader reader = new PdfReader("source.pdf");
PdfReaderContentParser parser = new PdfReaderContentParser(reader);
MyImageRenderListener listener = new MyImageRenderListener("destination.jpg");
for (int i = 1; i <= reader.getNumberOfPages(); i++) {
parser.processContent(i, listener);
 }
...
class MyImageRenderListener implements RenderListener {
    protected String path = "";

    public MyImageRenderListener(String path) {
        this.path = path;
    }

    public void beginTextBlock() {
    }

    public void endTextBlock() {
    }

    public void renderImage(ImageRenderInfo renderInfo) {
        try {
            String filename;
            FileOutputStream os;
            PdfImageObject image = renderInfo.getImage();
            PdfName filter = (PdfName) image.get(PdfName.FILTER);

                   if (PdfName.CCITTFAXDECODE.equals(filter)) {
                      BufferedImage bufferedImage = image.getBufferedImage();
                  ImageIO.write(bufferedImage, "jpg", new FileOutputStream(filename));// save tif image as jpg


            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void renderText(TextRenderInfo renderInfo) {
    }
}
iTextSharp Twisting CCITTFaxDecode extracted data with GetDrawingImage()

iTextSharp Twisting CCITTFaxDecode extracted data with GetDrawingImage()


By : zongkai
Date : March 29 2020, 07:55 AM
This might help you For anyone else that runs across this scenario here is my solution. The key to this puzzle was understanding that /K 0 is G3, /K -1 (or anything less than 0) is G4 /K 1 (or anything greater than 0) is G3-2D.
The twisting happens when you try to make G3 compressed data fit into a G4 image which it appears that is what iTextSharp may be doing. I know it definitely does not work with how I have iTextSharp implemented in my project. I confess that I cannot decipher all the decoding stuff that iTextSharp is doing so it could be something I'm missing too.
code :
int nK = 0;// Default to 0 like the PDF Spec
PdfObject oDecodeParms = stream.Get(PdfName.DECODEPARMS);
if (oDecodeParms is PdfDictionary)
{
    PdfObject oK0 = ((PdfDictionary)oDecodeParms).Get(PdfName.K);
    if (oK0 != null)
        nK = ((PdfNumber)oK0).IntValue;
}

using (MemoryStream ms = new MemoryStream())
{
    using (Tiff tiff = Tiff.ClientOpen("custom", "w", ms, new TiffStream()))
    {
        tiff.SetField(TiffTag.IMAGEWIDTH, width);
        tiff.SetField(TiffTag.IMAGELENGTH, height);
        if (nK == 0 || nK > 0) // 0 = Group 3, > 0 = Group 3 2D
            tiff.SetField(TiffTag.COMPRESSION, Compression.CCITTFAX3);
        else if (nK < 0) // < 0 = Group 4
            tiff.SetField(TiffTag.COMPRESSION, Compression.CCITTFAX4);
        tiff.SetField(TiffTag.BITSPERSAMPLE, bpc);
        tiff.SetField(TiffTag.SAMPLESPERPIXEL, 1);
        tiff.WriteRawStrip(0, rawBytes, rawBytes.Length); //saving the tiff file using the raw bytes retrieved from the PDF.
        tiff.Close();
    }

    TiffStreamForBytes byteStream = new TiffStreamForBytes(ms.ToArray());

    using (Tiff input = Tiff.ClientOpen("bytes", "r", null, byteStream))
    {
        int stride = input.ScanlineSize();

        Bitmap result = new Bitmap(width, height, pixelFormat);
        ColorPalette palette = result.Palette;
        palette.Entries[0] = System.Drawing.Color.White;
        palette.Entries[1] = System.Drawing.Color.Black;
        result.Palette = palette;

        for (int i = 0; i < height; i++)
        {
            Rectangle imgRect = new Rectangle(0, i, width, 1);
            BitmapData imgData = result.LockBits(imgRect, ImageLockMode.WriteOnly, pixelFormat);

            byte[] buffer = new byte[stride];
            input.ReadScanline(buffer, i);

            System.Runtime.InteropServices.Marshal.Copy(buffer, 0, imgData.Scan0, buffer.Length);
            result.UnlockBits(imgData);
        }
    }
}

/// <summary>
/// Custom read-only stream for byte buffer that can be used
/// with Tiff.ClientOpen method.
/// </summary>
public class TiffStreamForBytes : TiffStream
{
    private byte[] m_bytes;
    private int m_position;

    public TiffStreamForBytes(byte[] bytes)
    {
        m_bytes = bytes;
        m_position = 0;
    }

    public override int Read(object clientData, byte[] buffer, int offset, int count)
    {
        if ((m_position + count) > m_bytes.Length)
            return -1;

        Buffer.BlockCopy(m_bytes, m_position, buffer, offset, count);
        m_position += count;
        return count;
    }

    public override void Write(object clientData, byte[] buffer, int offset, int count)
    {
        throw new InvalidOperationException("This stream is read-only");
    }

    public override long Seek(object clientData, long offset, SeekOrigin origin)
    {
        switch (origin)
        {
            case SeekOrigin.Begin:
                if (offset > m_bytes.Length)
                    return -1;

                m_position = (int)offset;
                return m_position;

            case SeekOrigin.Current:
                if ((offset + m_position) > m_bytes.Length)
                    return -1;

                m_position += (int)offset;
                return m_position;

            case SeekOrigin.End:
                if ((m_bytes.Length - offset) < 0)
                    return -1;

                m_position = (int)(m_bytes.Length - offset);
                return m_position;
        }

        return -1;
    }

    public override void Close(object clientData)
    {
        // nothing to do
        return;
    }

    public override long Size(object clientData)
    {
        return m_bytes.Length;
    }
}
SVG Image with filter - filter covers area bigger than the image

SVG Image with filter - filter covers area bigger than the image


By : user2088481
Date : March 29 2020, 07:55 AM
I wish did fix the issue. Just give the filter explicit bounds, the exact size of the object being filled.
By default the bounds are 10% bigger than the object so that drop shadows work.
code :
image {
  outline: 3px solid red;
  outline-offset: -3px;
}

image.flood {
  filter: url(#floodFilter);
}
<svg width="500" height="200" viewBox="0 0 500 200">
  <defs>
    <filter id="floodFilter" filterUnits="objectBoundingBox" x="0" y="0" width="1" height="1">
      <feFlood flood-color="lightblue" flood-opacity="1" result="floodFill"></feFlood>
      <feBlend in="SourceGraphic" in2="floodFill" mode="multiply"></feBlend>
    </filter>
  </defs>
  <image x="0" y="30" height="140" width="200" href="https://www.fillmurray.com/g/200/140">   </image> 
  <image class="flood" x="230" y="30" height="140"  width="200" href="https://www.fillmurray.com/g/200/140"></image>
</svg>
Extracting Local Resource Image from Winforms Form/Button/Image Property

Extracting Local Resource Image from Winforms Form/Button/Image Property


By : John Moli
Date : March 29 2020, 07:55 AM
will help you I found a much easier method than manually decoding the Base64.
Assuming your form is called Form1:
Extracting video frame image with libavformat and libswscale gives flipped image

Extracting video frame image with libavformat and libswscale gives flipped image


By : Venkat
Date : March 29 2020, 07:55 AM
around this issue I am using following code to extract image frames outputted by avcodec_decode_video2() into BMP file: , So here is the working code:
shadow
Privacy Policy - Terms - Contact Us © 35dp-dentalpractice.co.uk