Search in sources :

Example 11 with ImageDescriptor

use of org.dcm4che3.imageio.codec.ImageDescriptor in project dcm4che by dcm4che.

the class NativeImageReader method getNativeImage.

private PlanarImage getNativeImage(ImageReadParam param) throws IOException {
    StreamSegment seg = StreamSegment.getStreamSegment(iis, param);
    ImageDescriptor desc = seg.getImageDescriptor();
    int dcmFlags = (canEncodeSigned && desc.isSigned()) ? Imgcodecs.DICOM_FLAG_SIGNED : Imgcodecs.DICOM_FLAG_UNSIGNED;
    if (ybr2rgb(desc.getPhotometricInterpretation())) {
        dcmFlags |= Imgcodecs.DICOM_FLAG_YBR;
    }
    if (seg instanceof FileStreamSegment) {
        MatOfDouble positions = null;
        MatOfDouble lengths = null;
        try {
            positions = new MatOfDouble(ExtendSegmentedInputImageStream.getDoubleArray(seg.getSegPosition()));
            lengths = new MatOfDouble(ExtendSegmentedInputImageStream.getDoubleArray(seg.getSegLength()));
            return ImageCV.toImageCV(Imgcodecs.dicomJpgFileRead(((FileStreamSegment) seg).getFilePath(), positions, lengths, dcmFlags, Imgcodecs.IMREAD_UNCHANGED));
        } finally {
            closeMat(positions);
            closeMat(lengths);
        }
    } else if (seg instanceof MemoryStreamSegment) {
        Mat buf = null;
        try {
            ByteBuffer b = ((MemoryStreamSegment) seg).getCache();
            buf = new Mat(1, b.limit(), CvType.CV_8UC1);
            buf.put(0, 0, b.array());
            return ImageCV.toImageCV(Imgcodecs.dicomJpgMatRead(buf, dcmFlags, Imgcodecs.IMREAD_UNCHANGED));
        } finally {
            closeMat(buf);
        }
    }
    return null;
}
Also used : Mat(org.opencv.core.Mat) ImageDescriptor(org.dcm4che3.imageio.codec.ImageDescriptor) MatOfDouble(org.opencv.core.MatOfDouble) ByteBuffer(java.nio.ByteBuffer)

Example 12 with ImageDescriptor

use of org.dcm4che3.imageio.codec.ImageDescriptor in project dcm4che by dcm4che.

the class NativeJ2kImageWriter method write.

@Override
public void write(IIOMetadata streamMetadata, IIOImage image, ImageWriteParam param) throws IOException {
    if (output == null) {
        throw new IllegalStateException("input cannot be null");
    }
    if (!(output instanceof ImageOutputStream)) {
        throw new IllegalArgumentException("input is not an ImageInputStream!");
    }
    ImageOutputStream stream = (ImageOutputStream) output;
    stream.setByteOrder(ByteOrder.LITTLE_ENDIAN);
    J2kImageWriteParam j2kParams = (J2kImageWriteParam) param;
    if (!(stream instanceof BytesWithImageImageDescriptor)) {
        throw new IllegalArgumentException("stream does not implement BytesWithImageImageDescriptor!");
    }
    ImageDescriptor desc = ((BytesWithImageImageDescriptor) stream).getImageDescriptor();
    RenderedImage renderedImage = image.getRenderedImage();
    Mat buf = null;
    MatOfInt dicomParams = null;
    try {
        ImageCV mat = null;
        try {
            // Band interleaved mode (PlanarConfiguration = 1) is converted to pixel interleaved
            // So the input image has always a pixel interleaved mode mode((PlanarConfiguration = 0)
            mat = ImageConversion.toMat(renderedImage, param.getSourceRegion(), false);
            int cvType = mat.type();
            int channels = CvType.channels(cvType);
            boolean signed = desc.isSigned();
            int dcmFlags = signed ? Imgcodecs.DICOM_FLAG_SIGNED : Imgcodecs.DICOM_FLAG_UNSIGNED;
            int[] params = new int[16];
            // Image flags
            params[Imgcodecs.DICOM_PARAM_IMREAD] = Imgcodecs.IMREAD_UNCHANGED;
            // DICOM flags
            params[Imgcodecs.DICOM_PARAM_DCM_IMREAD] = dcmFlags;
            // Image width
            params[Imgcodecs.DICOM_PARAM_WIDTH] = mat.width();
            // Image height
            params[Imgcodecs.DICOM_PARAM_HEIGHT] = mat.height();
            // Type of compression
            params[Imgcodecs.DICOM_PARAM_COMPRESSION] = Imgcodecs.DICOM_CP_J2K;
            // Number of components
            params[Imgcodecs.DICOM_PARAM_COMPONENTS] = channels;
            // Bits per sample
            params[Imgcodecs.DICOM_PARAM_BITS_PER_SAMPLE] = desc.getBitsCompressed();
            // Interleave mode
            params[Imgcodecs.DICOM_PARAM_INTERLEAVE_MODE] = Imgcodecs.ILV_SAMPLE;
            // JPEG-2000 lossy ratio factor
            params[Imgcodecs.DICOM_PARAM_J2K_COMPRESSION_FACTOR] = j2kParams.getCompressionRatiofactor();
            dicomParams = new MatOfInt(params);
            buf = Imgcodecs.dicomJpgWrite(mat, dicomParams, "");
            if (buf.empty()) {
                throw new IIOException("Native JPEG2000 encoding error: null image");
            }
        } finally {
            if (mat != null) {
                mat.release();
            }
        }
        byte[] bSrcData = new byte[buf.width() * buf.height() * (int) buf.elemSize()];
        buf.get(0, 0, bSrcData);
        stream.write(bSrcData);
    } catch (Throwable t) {
        throw new IIOException("Native JPEG2000 encoding error", t);
    } finally {
        NativeImageReader.closeMat(dicomParams);
        NativeImageReader.closeMat(buf);
    }
}
Also used : ImageCV(org.weasis.opencv.data.ImageCV) Mat(org.opencv.core.Mat) MatOfInt(org.opencv.core.MatOfInt) IIOException(javax.imageio.IIOException) BytesWithImageImageDescriptor(org.dcm4che3.imageio.codec.BytesWithImageImageDescriptor) BytesWithImageImageDescriptor(org.dcm4che3.imageio.codec.BytesWithImageImageDescriptor) ImageDescriptor(org.dcm4che3.imageio.codec.ImageDescriptor) RenderedImage(java.awt.image.RenderedImage) ImageOutputStream(javax.imageio.stream.ImageOutputStream)

Example 13 with ImageDescriptor

use of org.dcm4che3.imageio.codec.ImageDescriptor in project dcm4che by dcm4che.

the class DicomImageReader method initPixelDataFromAttributes.

private void initPixelDataFromAttributes(Attributes ds) {
    VR.Holder holder = new VR.Holder();
    Object value = ds.getValue(Tag.PixelData, holder);
    if (value != null) {
        imageDescriptor = new ImageDescriptor(ds);
        pixelDataVR = holder.vr;
        if (value instanceof BulkData) {
            pixelData = (BulkData) value;
            pixelDataLength = pixelData.length();
        } else if (value instanceof byte[]) {
            pixeldataBytes = (byte[]) value;
            pixelDataLength = pixeldataBytes.length;
        } else {
            // value instanceof Fragments
            pixelDataFragments = (Fragments) value;
            pixelDataLength = -1;
        }
    }
}
Also used : Fragments(org.dcm4che3.data.Fragments) ImageDescriptor(org.dcm4che3.imageio.codec.ImageDescriptor) BulkData(org.dcm4che3.data.BulkData) IncludeBulkData(org.dcm4che3.io.DicomInputStream.IncludeBulkData) VR(org.dcm4che3.data.VR)

Aggregations

ImageDescriptor (org.dcm4che3.imageio.codec.ImageDescriptor)10 BytesWithImageImageDescriptor (org.dcm4che3.imageio.codec.BytesWithImageImageDescriptor)4 Mat (org.opencv.core.Mat)4 RenderedImage (java.awt.image.RenderedImage)3 IOException (java.io.IOException)3 IIOException (javax.imageio.IIOException)3 ImageOutputStream (javax.imageio.stream.ImageOutputStream)3 PhotometricInterpretation (org.dcm4che3.image.PhotometricInterpretation)3 EncapsulatedPixelDataImageInputStream (org.dcm4che3.imageio.stream.EncapsulatedPixelDataImageInputStream)3 MatOfInt (org.opencv.core.MatOfInt)3 ImageCV (org.weasis.opencv.data.ImageCV)3 Attributes (org.dcm4che3.data.Attributes)2 BulkData (org.dcm4che3.data.BulkData)2 TransferSyntaxType (org.dcm4che3.imageio.codec.TransferSyntaxType)2 PatchJPEGLSImageInputStream (org.dcm4che3.imageio.codec.jpeg.PatchJPEGLSImageInputStream)2 SegmentedInputImageStream (org.dcm4che3.imageio.stream.SegmentedInputImageStream)2 RetrieveService (org.dcm4chee.arc.retrieve.RetrieveService)2 EOFException (java.io.EOFException)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1