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;
}
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);
}
}
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;
}
}
}
Aggregations