jai-imageio / jai-imageio-jpeg2000

JPEG2000 support for Java Advanced Imaging Image I/O Tools API
Other
74 stars 56 forks source link

Infinite Loop Reading Image #7

Open afboteroz opened 8 years ago

afboteroz commented 8 years ago

I'm using jpedal to get an image of a pdf and where jpedal tries to read the imagen it's enter in an infinite loop.

https://github.com/jai-imageio/jai-imageio-jpeg2000/blob/master/src/main/java/jj2000/j2k/codestream/reader/PktDecoder.java#L1028

while(bin.readBit()!=0) {
    lblock[c][r][s][cbc.y][cbc.x]++;
}

Execution Code:

byte[] data;

...

try {
    in = new ByteArrayInputStream(data);
    iir = ImageIO.getImageReadersByFormatName("JPEG2000").next();
} catch (Exception ee) {
    ...
}
...
try{
    ImageInputStream iin = ImageIO.createImageInputStream(in);
    try{
        iir.setInput(iin, true);  
        image = iir.read(0);
        ...
    } catch (Exception ee) {
    ...
    }
} catch (Exception ee) {
    ...
}
stain commented 7 years ago

Do you have a copy of the data that causes an infinite loop? I can't see how readBit() would cause an infinite loop as it should progress through the stream until bpos reaches 0?

Or is the problem with bit stuffing..? It assumes nextbbuf has been updated, but if you have a stream with more than 7 0xFFs I guess it could fall over and just keep looking within the same bytes.

afboteroz commented 7 years ago

The PDF that was trying to extract the first image was this: 00006567119070520-1-original.pdf

clockard commented 6 years ago

I have been experiencing this issue as well.

clockard commented 6 years ago

I have pr up to prevent the infinite loop from happening on the codice fork of the project here https://github.com/codice/jai-imageio-jpeg2000/pull/3

clockard commented 6 years ago

This is the j2k file that was causing the issue for me. I gave it a pdf extension so github would let me upload it. p1_05.pdf

Lonzak commented 3 years ago

Seems to have been merged, so this can be closed now?

igorrogov commented 3 years ago

I have a PDF file where I can see a similar issue. I use PDFBox to extract an embedded image in JPEG2000 format, but it gets stuck in infinite loop in:

        at jj2000.j2k.codestream.reader.PktDecoder.readPktHead(PktDecoder.java:1027)
        at jj2000.j2k.codestream.reader.FileBitstreamReaderAgent.readResLyCompPos(FileBitstreamReaderAgent.java:1162)
        at jj2000.j2k.codestream.reader.FileBitstreamReaderAgent.readTilePkts(FileBitstreamReaderAgent.java:1963)
        at jj2000.j2k.codestream.reader.FileBitstreamReaderAgent.setTile(FileBitstreamReaderAgent.java:2228)
        at jj2000.j2k.wavelet.synthesis.MultiResImgDataAdapter.setTile(MultiResImgDataAdapter.java:291)
        at jj2000.j2k.wavelet.synthesis.MultiResImgDataAdapter.setTile(MultiResImgDataAdapter.java:291)
        at jj2000.j2k.quantization.dequantizer.Dequantizer.setTile(Dequantizer.java:226)
        at jj2000.j2k.wavelet.synthesis.InvWTAdapter.setTile(InvWTAdapter.java:339)
        at jj2000.j2k.wavelet.synthesis.InvWTFull.setTile(InvWTFull.java:578)
        at jj2000.j2k.image.ImgDataAdapter.setTile(ImgDataAdapter.java:288)
        at jj2000.j2k.image.invcomptransf.InvCompTransf.setTile(InvCompTransf.java:643)
        at com.sun.media.imageioimpl.plugins.jpeg2000.J2KReadState.readSubsampledRaster(J2KReadState.java:763)
        at com.sun.media.imageioimpl.plugins.jpeg2000.J2KReadState.readBufferedImage(J2KReadState.java:384)
        at com.sun.media.imageioimpl.plugins.jpeg2000.J2KImageReader.read(J2KImageReader.java:454)
        at org.apache.pdfbox.filter.JPXFilter.readJPX(JPXFilter.java:132)
        at org.apache.pdfbox.filter.JPXFilter.decode(JPXFilter.java:64)
        at org.apache.pdfbox.cos.COSInputStream.create(COSInputStream.java:80)
        at org.apache.pdfbox.cos.COSStream.createInputStream(COSStream.java:176)
        at org.apache.pdfbox.cos.COSStream.createInputStream(COSStream.java:164)
        at org.apache.pdfbox.pdmodel.common.PDStream.createInputStream(PDStream.java:236)
        at org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject.<init>(PDImageXObject.java:142)
        at org.apache.pdfbox.pdmodel.graphics.PDXObject.createXObject(PDXObject.java:70)
        at org.apache.pdfbox.pdmodel.PDResources.getXObject(PDResources.java:426)
        at org.apache.pdfbox.contentstream.operator.graphics.DrawObject.process(DrawObject.java:58)
        at org.apache.pdfbox.contentstream.PDFStreamEngine.processOperator(PDFStreamEngine.java:933)
        at org.apache.pdfbox.contentstream.PDFStreamEngine.processStreamOperators(PDFStreamEngine.java:515)
        at org.apache.pdfbox.contentstream.PDFStreamEngine.processStream(PDFStreamEngine.java:489)
        at org.apache.pdfbox.contentstream.PDFStreamEngine.processPage(PDFStreamEngine.java:156)
        at org.apache.pdfbox.rendering.PageDrawer.drawPage(PageDrawer.java:275)
        at org.apache.pdfbox.rendering.PDFRenderer.renderPageToGraphics(PDFRenderer.java:458)
        at org.apache.pdfbox.rendering.PDFRenderer.renderPageToGraphics(PDFRenderer.java:414)
        at org.apache.pdfbox.rendering.PDFRenderer.renderPageToGraphics(PDFRenderer.java:396)
        at org.apache.pdfbox.rendering.PDFRenderer.renderPageToGraphics(PDFRenderer.java:379)

I have tried the patch from the other fork where it's supposed to be fixed (https://github.com/codice/jai-imageio-jpeg2000), but it didn't work for me. The PDF looks fine when I look at it in Adobe.

Sample file is attached: The digital overhaul Exec Report.pdf