Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dcm2Dcm StreamUtils.skipFully() EOFException #1325

Open
qq1045561395 opened this issue Jul 12, 2023 · 1 comment
Open

Dcm2Dcm StreamUtils.skipFully() EOFException #1325

qq1045561395 opened this issue Jul 12, 2023 · 1 comment

Comments

@qq1045561395
Copy link

qq1045561395 commented Jul 12, 2023

version:dcm4che5.29.0

I want to compress my dicom.

The code is as follows.


        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(buffer);
        try (Transcoder transcoder = new Transcoder(new DicomInputStream(byteArrayInputStream))) {
            transcoder.setIncludeFileMetaInformation(true);
            transcoder.setRetainFileMetaInformation(false);
            transcoder.setEncodingOptions(DicomEncodingOptions.DEFAULT);
            // jpeg2000
            transcoder.setDestinationTransferSyntax(UID.JPEG2000Lossless);
            if (TransferSyntaxType.JPEG_2000.isPixeldataEncapsulated()) {
                transcoder.setCompressParams(params.toArray(new Property[params.size()]));
            }
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

            transcoder.transcode(new Transcoder.Handler() {
                @Override
                public OutputStream newOutputStream(Transcoder transcoder1, Attributes dataset) throws IOException {
                    return byteArrayOutputStream;
                }
            });
            byte[] bytes = byteArrayOutputStream.toByteArray();
            FileOutputStream fileOutputStream = new FileOutputStream(dest);
            fileOutputStream.write(bytes);
            fileOutputStream.close();
        } catch (Exception e) {
            throw e;
        }

Throw anomaly.

16:58:28.676 [main] DEBUG org.dcm4che3.imageio.codec.Transcoder - Compressor: org.dcm4che3.opencv.NativeJ2kImageWriter
16:58:28.834 [main] DEBUG org.dcm4che3.imageio.codec.Transcoder - Compressed frame #1 in 136 ms, ratio 33.67608:1
Failed to transcode C:\Users\Administrator\Desktop\test\1.dcm: null
java.io.EOFException
	at org.dcm4che3.util.StreamUtils.skipFully(StreamUtils.java:78)
	at org.dcm4che3.io.DicomInputStream.skipFully(DicomInputStream.java:469)
	at org.dcm4che3.imageio.codec.Transcoder.compressPixelData(Transcoder.java:596)
	at org.dcm4che3.imageio.codec.Transcoder.processPixelData(Transcoder.java:513)
	at org.dcm4che3.imageio.codec.Transcoder.access$800(Transcoder.java:71)
	at org.dcm4che3.imageio.codec.Transcoder$1.readValue(Transcoder.java:466)
	at org.dcm4che3.io.DicomInputStream.readAttributes(DicomInputStream.java:681)
	at org.dcm4che3.io.DicomInputStream.readAllAttributes(DicomInputStream.java:568)
	at org.dcm4che3.imageio.codec.Transcoder.transcode(Transcoder.java:440)
	at org.dcm4che3.tool.dcm2dcm.Dcm2Dcm.transcodeWithTranscoder(Dcm2Dcm.java:428)
	at org.dcm4che3.tool.dcm2dcm.Dcm2Dcm.transcode(Dcm2Dcm.java:343)
	at org.dcm4che3.tool.dcm2dcm.Dcm2Dcm.mtranscode(Dcm2Dcm.java:334)
	at org.dcm4che3.tool.dcm2dcm.Dcm2Dcm.mtranscode(Dcm2Dcm.java:316)
	at org.dcm4che3.tool.dcm2dcm.Dcm2Dcm.main(Dcm2Dcm.java:288)

Although this solves the problem, I only have a byte array
Transcoder transcoder = new Transcoder(new DicomInputStream(byteArrayInputStream))
Modify as follows
Transcoder transcoder = new Transcoder(new DicomInputStream(new File("c:\\test.dcm")))

@qq1045561395
Copy link
Author

dis.length() is 7515424 and imageDescriptor.getLength() is 7515423,so the padding result is equal to 1.

   private void compressPixelData() throws IOException {
        int padding = dis.length() - imageDescriptor.getLength();
        for (int i = 0; i < imageDescriptor.getFrames(); i++) {
            if (decompressor == null){
                readFrame();
            }
            else{
                decompressFrame(i);
            }

            if (i == 0) {
                extractEmbeddedOverlays();
                adjustDataset();
                writeDataset();
                dos.writeHeader(Tag.PixelData, VR.OB, -1);
                dos.writeHeader(Tag.Item, null, 0);
            }
            nullifyUnusedBits();
            bi = palette2rgb ? BufferedImageUtils.convertPalettetoRGB(originalBi, bi)
                    : ybr2rgb ? BufferedImageUtils.convertYBRtoRGB(originalBi, bi)
                    : imageDescriptor.is16BitsAllocated8BitsStored()
                    ? BufferedImageUtils.convertShortsToBytes(originalBi, bi) // workaround for JPEG codec issue
                    : originalBi;
            compressFrame(i);
        }
        dis.skipFully(padding);

        dos.writeHeader(Tag.SequenceDelimitationItem, null, 0);
    }

At this step, count is 0 and in.read() is -1, resulting in an error message.

public static void skipFully(InputStream in, long n) throws IOException {
        while (n > 0) {
            long count = in.skip(n);
            if (count == 0) {
                if (in.read() == -1) {
                    throw new EOFException();
                }
                count = 1;
            }
            n -= count;
        }
    }

Modify as follows.in.skip(n) is 1,Can be successfully executed
Transcoder transcoder = new Transcoder(new DicomInputStream(new File("c:\\test.dcm")))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant