You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@pdfbox.apache.org by Jiří Syrový <sy...@gmail.com> on 2010/05/04 14:50:56 UTC

Deflate compression

Hi,

I'm just trying to implement tool processing full color RGB images
which also works with the pdf files and to load and save these images
it uses pdfbox. I've implemented class that does the reverse of
PDPixelMap, so it just stores raw compressed data into PDFStream.
Problem is that between the following piece of code and DeflateFilter
input it gots somehow mangled and the first 32 bytes are changed to
something else (but constant values) and I'm unable to track where
exactly. Any ideas?

public PDDeflate(PDDocument doc, BufferedImage bi) throws IOException {
        super(new PDStream(doc), "rawlzw");

        COSDictionary dic = getCOSStream();

        dic.setItem(COSName.FILTER, COSName.FLATE_DECODE);
        dic.setItem(COSName.SUBTYPE, COSName.IMAGE);
        dic.setItem(COSName.TYPE, COSName.getPDFName("XObject"));

        getCOSStream().setFilters(COSName.FLATE_DECODE);

        java.io.OutputStream os = getCOSStream().createUnfilteredStream();

        // optimize - not efficient enough - few ms can be saved ...
        byte[] imageData = ((DataBufferByte)
                bi.getRaster().getDataBuffer()).getData();
        byte[] data = new byte[imageData.length];

        System.arraycopy(imageData, 0, data, 0, data.length);

        for (int i = 0; i < data.length-1; i += 3) {
            byte tmp = data[i+2];
            data[i+2] = data[i];
            data[i] = tmp;
        }

        os.write(data);

        ColorModel cm = bi.getColorModel();
        ColorSpace cs = cm.getColorSpace();

        PDColorSpace pdColorSpace =
PDColorSpaceFactory.createColorSpace(doc, cs);
        setColorSpace(pdColorSpace);
        setBitsPerComponent(8);
        setWidth(bi.getWidth());
        setHeight(bi.getHeight());

    }

Re: Deflate compression

Posted by Jiří Syrový <sy...@gmail.com>.
Hi,

that's not usable for me, because I need exact copy of that inserted
image after extraction and only two supported "compressions" are jpeg
(PDJpeg) that is lossy (at least should be) and citt (PDcitt) that is
AFAIK usable only for gray-level/BW images. It would be great to have
some other compression, but I'm afraid that only way is two implement
another filter. If it is usable after some testing I can also put it
into PDPixelMap instead of current implementation - see:

    /**
     * Construct a pixel map image from an AWT image.
     *
     * @param doc The PDF document to embed the image in.
     * @param awtImage The image to read data from.
     *
     * @throws IOException If there is an error while embedding this image.
     */
    /*
     * This method is broken and needs to be implemented, any takers?
    public PDPixelMap(PDDocument doc, BufferedImage awtImage) throws IOException
    {

Regards,
Jiri Syrovy

On Tue, May 4, 2010 at 7:51 PM, Andreas Lehmkuehler <an...@lehmi.de> wrote:
> Hi
>
> Jir(í Syrový schrieb:
>>
>> Hi,
>>
>> I'm just trying to implement tool processing full color RGB images
>> which also works with the pdf files and to load and save these images
>> it uses pdfbox. I've implemented class that does the reverse of
>> PDPixelMap, so it just stores raw compressed data into PDFStream.
>> Problem is that between the following piece of code and DeflateFilter
>> input it gots somehow mangled and the first 32 bytes are changed to
>> something else (but constant values) and I'm unable to track where
>> exactly. Any ideas?
>
> It's probably easier to use PDFBox itself. Have a look at [1] as an example.
>
> BR
> Andreas Lehmkühler
>
> http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/examples/pdmodel/ImageToPDF.java?view=log
>

Re: Deflate compression

Posted by Andreas Lehmkuehler <an...@lehmi.de>.
Hi

Jir(í Syrový schrieb:
> Hi,
> 
> I'm just trying to implement tool processing full color RGB images
> which also works with the pdf files and to load and save these images
> it uses pdfbox. I've implemented class that does the reverse of
> PDPixelMap, so it just stores raw compressed data into PDFStream.
> Problem is that between the following piece of code and DeflateFilter
> input it gots somehow mangled and the first 32 bytes are changed to
> something else (but constant values) and I'm unable to track where
> exactly. Any ideas?
It's probably easier to use PDFBox itself. Have a look at [1] as an example.

BR
Andreas Lehmkühler

http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/examples/pdmodel/ImageToPDF.java?view=log