Example usage for javax.imageio ImageWriter getDefaultStreamMetadata

List of usage examples for javax.imageio ImageWriter getDefaultStreamMetadata

Introduction

In this page you can find the example usage for javax.imageio ImageWriter getDefaultStreamMetadata.

Prototype

public abstract IIOMetadata getDefaultStreamMetadata(ImageWriteParam param);

Source Link

Document

Returns an IIOMetadata object containing default values for encoding a stream of images.

Usage

From source file:org.dcm4che2.tool.dcm2dcm.Dcm2Dcm.java

/**
 * Recodes the images from the source transfer syntax, as read from the src
 * file, to the specified destination syntax.
 *///from   w  w w  .  java 2  s.  c o m
public void recodeImages(File src, File dest) throws IOException {
    ImageReader reader = new DicomImageReaderSpi().createReaderInstance();
    ImageWriter writer = new DicomImageWriterSpi().createWriterInstance();

    DicomStreamMetaData writeMeta = (DicomStreamMetaData) writer.getDefaultStreamMetadata(null);

    DicomObject ds;
    int frames;

    FileImageOutputStream output = null;

    try {
        FileImageInputStream input = new FileImageInputStream(src);

        try {
            reader.setInput(input);
            if (dest.exists())
                dest.delete();
            output = new FileImageOutputStream(dest);
            writer.setOutput(output);
            DicomStreamMetaData streamMeta = (DicomStreamMetaData) reader.getStreamMetadata();
            ds = streamMeta.getDicomObject();

            DicomObject newDs = new BasicDicomObject();
            ds.copyTo(newDs);
            writeMeta.setDicomObject(newDs);
            frames = ds.getInt(Tag.NumberOfFrames, 1);
            newDs.putString(Tag.TransferSyntaxUID, VR.UI, destinationSyntax.uid());
            newDs.putString(Tag.ImplementationClassUID, VR.UI, Implementation.classUID());
            newDs.putString(Tag.ImplementationVersionName, VR.SH, Implementation.versionName());
            if (overwriteObject != null) {
                overwriteObject.copyTo(newDs);
            }
        } finally {
            reader.dispose();
        }
        writer.prepareWriteSequence(writeMeta);
        for (int i = 0; i < frames; i++) {
            FileInputStream inputStream = new FileInputStream(src);
            try {
                WritableRaster r = (WritableRaster) readRaster(inputStream, i);

                ColorModel cm = ColorModelFactory.createColorModel(ds);
                BufferedImage bi = new BufferedImage(cm, r, false, null);
                IIOImage iioimage = new IIOImage(bi, null, null);
                writer.writeToSequence(iioimage, null);
            } catch (NoSuchFieldException ex) {
                System.err.println(ex.toString());
            } catch (IllegalAccessException ex) {
                System.err.println(ex.toString());
            } finally {
                inputStream.close();
            }
        }
        writer.endWriteSequence();
    } finally {
        if (output != null) {
            output.close();
        }
    }
}

From source file:org.psystems.dicomweb.Dcm2Dcm.java

/**
 * Recodes the images from the source transfer syntax, as read from the src
 * file, to the specified destination syntax.
 *///from   w ww. j  a  v a2s  . c  o  m
public void recodeImages(File src, File dest) throws IOException {

    ImageReader reader = new DicomImageReaderSpi().createReaderInstance();
    ImageWriter writer = new DicomImageWriterSpi().createWriterInstance();
    FileImageInputStream input = new FileImageInputStream(src);
    reader.setInput(input);
    if (dest.exists())
        dest.delete();
    FileImageOutputStream output = new FileImageOutputStream(dest);
    writer.setOutput(output);
    DicomStreamMetaData streamMeta = (DicomStreamMetaData) reader.getStreamMetadata();
    DicomObject ds = streamMeta.getDicomObject();
    DicomStreamMetaData writeMeta = (DicomStreamMetaData) writer.getDefaultStreamMetadata(null);
    DicomObject newDs = new BasicDicomObject();
    ds.copyTo(newDs);
    writeMeta.setDicomObject(newDs);
    int frames = ds.getInt(Tag.NumberOfFrames, 1);
    LookupTable lut = prepareBitStrip(writeMeta, reader);
    newDs.putString(Tag.TransferSyntaxUID, VR.UI, destinationSyntax.uid());
    if (overwriteObject != null) {
        overwriteObject.copyTo(newDs);
    }
    writer.prepareWriteSequence(writeMeta);
    for (int i = 0; i < frames; i++) {
        WritableRaster r = (WritableRaster) reader.readRaster(i, null);
        ColorModel cm = ColorModelFactory.createColorModel(ds);
        BufferedImage bi = new BufferedImage(cm, r, false, null);
        if (lut != null) {
            lut.lookup(bi.getRaster(), bi.getRaster());
        }
        IIOImage iioimage = new IIOImage(bi, null, null);
        writer.writeToSequence(iioimage, null);
    }
    writer.endWriteSequence();
    output.close();
    input.close();
}

From source file:org.psystems.dicomweb.Dcm2DcmCopy.java

/**
 * Recodes the images from the source transfer syntax, as read from the src
 * file, to the specified destination syntax.
 *///w w w .jav  a2  s.c  o  m
public void recodeImages(File src, File dest) throws IOException {
    ImageReader reader = new DicomImageReaderSpi().createReaderInstance();
    ImageWriter writer = new DicomImageWriterSpi().createWriterInstance();
    FileImageInputStream input = new FileImageInputStream(src);
    reader.setInput(input);
    if (dest.exists())
        dest.delete();
    FileImageOutputStream output = new FileImageOutputStream(dest);
    writer.setOutput(output);
    DicomStreamMetaData streamMeta = (DicomStreamMetaData) reader.getStreamMetadata();
    DicomObject ds = streamMeta.getDicomObject();
    DicomStreamMetaData writeMeta = (DicomStreamMetaData) writer.getDefaultStreamMetadata(null);
    DicomObject newDs = new BasicDicomObject();
    ds.copyTo(newDs);
    writeMeta.setDicomObject(newDs);
    int frames = ds.getInt(Tag.NumberOfFrames, 1);
    LookupTable lut = prepareBitStrip(writeMeta, reader);
    newDs.putString(Tag.TransferSyntaxUID, VR.UI, destinationSyntax.uid());
    if (overwriteObject != null) {
        overwriteObject.copyTo(newDs);
    }
    writer.prepareWriteSequence(writeMeta);
    for (int i = 0; i < frames; i++) {
        WritableRaster r = (WritableRaster) reader.readRaster(i, null);
        ColorModel cm = ColorModelFactory.createColorModel(ds);
        BufferedImage bi = new BufferedImage(cm, r, false, null);
        if (lut != null) {
            lut.lookup(bi.getRaster(), bi.getRaster());
        }
        IIOImage iioimage = new IIOImage(bi, null, null);
        writer.writeToSequence(iioimage, null);
    }
    writer.endWriteSequence();
    output.close();
    input.close();
}