Search in sources :

Example 1 with DicomOutputStream

use of org.dcm4che3.io.DicomOutputStream in project karnak by OsiriX-Foundation.

the class DicomPaneLogic method getWorklistItemInputStreamInDicom.

public InputStream getWorklistItemInputStreamInDicom(Attributes attributes) {
    InputStream inputStream = null;
    if (attributes != null) {
        try (ByteArrayOutputStream tmp = new ByteArrayOutputStream();
            DicomOutputStream out = new DicomOutputStream(tmp, UID.ImplicitVRLittleEndian)) {
            out.writeDataset(null, attributes);
            inputStream = new ByteArrayInputStream(tmp.toByteArray());
        } catch (IOException e) {
            // $NON-NLS-1$
            LOGGER.error("Cannot write dicom file: {}", e.getMessage());
        }
    }
    return inputStream;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) DicomOutputStream(org.dcm4che3.io.DicomOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Example 2 with DicomOutputStream

use of org.dcm4che3.io.DicomOutputStream in project dcm4che by dcm4che.

the class IanSCP method create.

private Attributes create(Association as, Attributes rq, Attributes rqAttrs) throws DicomServiceException {
    if (storageDir == null)
        return null;
    String cuid = rq.getString(Tag.AffectedSOPClassUID);
    String iuid = rq.getString(Tag.AffectedSOPInstanceUID);
    File file = new File(storageDir, iuid);
    if (file.exists())
        throw new DicomServiceException(Status.DuplicateSOPinstance).setUID(Tag.AffectedSOPInstanceUID, iuid);
    DicomOutputStream out = null;
    LOG.info("{}: M-WRITE {}", as, file);
    try {
        out = new DicomOutputStream(file);
        out.writeDataset(Attributes.createFileMetaInformation(iuid, cuid, UID.ExplicitVRLittleEndian), rqAttrs);
    } catch (IOException e) {
        LOG.warn(as + ": Failed to store Instance Available Notification:", e);
        throw new DicomServiceException(Status.ProcessingFailure, e);
    } finally {
        SafeClose.close(out);
    }
    return null;
}
Also used : DicomOutputStream(org.dcm4che3.io.DicomOutputStream) IOException(java.io.IOException) File(java.io.File) DicomServiceException(org.dcm4che3.net.service.DicomServiceException)

Example 3 with DicomOutputStream

use of org.dcm4che3.io.DicomOutputStream in project dcm4che by dcm4che.

the class Jpg2Dcm method convert.

private void convert(Path srcFilePath, Path destFilePath) throws Exception {
    ContentType fileType = ContentType.probe(srcFilePath);
    Attributes fileMetadata = SAXReader.parse(StreamUtils.openFileOrURL(fileType.getSampleMetadataFile(photo)));
    fileMetadata.addAll(staticMetadata);
    supplementMissingValue(fileMetadata, Tag.SOPClassUID, fileType.getSOPClassUID(photo));
    try (SeekableByteChannel channel = Files.newByteChannel(srcFilePath);
        DicomOutputStream dos = new DicomOutputStream(destFilePath.toFile())) {
        XPEGParser parser = fileType.newParser(channel);
        parser.getAttributes(fileMetadata);
        dos.writeDataset(fileMetadata.createFileMetaInformation(parser.getTransferSyntaxUID()), fileMetadata);
        dos.writeHeader(Tag.PixelData, VR.OB, -1);
        dos.writeHeader(Tag.Item, null, 0);
        if (noAPPn && parser.getPositionAfterAPPSegments() > 0) {
            copyPixelData(channel, parser.getPositionAfterAPPSegments(), dos, (byte) 0xFF, (byte) JPEG.SOI);
        } else {
            copyPixelData(channel, parser.getCodeStreamPosition(), dos);
        }
        dos.writeHeader(Tag.SequenceDelimitationItem, null, 0);
    }
    System.out.println(MessageFormat.format(rb.getString("converted"), srcFilePath, destFilePath));
}
Also used : SeekableByteChannel(java.nio.channels.SeekableByteChannel) XPEGParser(org.dcm4che3.imageio.codec.XPEGParser) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes) DicomOutputStream(org.dcm4che3.io.DicomOutputStream)

Example 4 with DicomOutputStream

use of org.dcm4che3.io.DicomOutputStream in project dcm4che by dcm4che.

the class Json2Dcm method writeTo.

public void writeTo(OutputStream out) throws IOException {
    if (nofmi)
        fmi = null;
    else if (fmi == null ? withfmi : tsuid != null && !tsuid.equals(fmi.getString(Tag.TransferSyntaxUID, null))) {
        fmi = dataset.createFileMetaInformation(tsuid);
    }
    @SuppressWarnings("resource") DicomOutputStream dos = new DicomOutputStream(new BufferedOutputStream(out), fmi != null ? UID.ExplicitVRLittleEndian : tsuid != null ? tsuid : UID.ImplicitVRLittleEndian);
    dos.setEncodingOptions(encOpts);
    dos.writeDataset(fmi, dataset);
    dos.finish();
    dos.flush();
}
Also used : DicomOutputStream(org.dcm4che3.io.DicomOutputStream)

Example 5 with DicomOutputStream

use of org.dcm4che3.io.DicomOutputStream in project dcm4che by dcm4che.

the class StoreSCP method storeTo.

private void storeTo(Association as, Attributes fmi, PDVInputStream data, File file) throws IOException {
    LOG.info("{}: M-WRITE {}", as, file);
    file.getParentFile().mkdirs();
    DicomOutputStream out = new DicomOutputStream(file);
    try {
        out.writeFileMetaInformation(fmi);
        data.copyTo(out);
    } finally {
        SafeClose.close(out);
    }
}
Also used : DicomOutputStream(org.dcm4che3.io.DicomOutputStream)

Aggregations

DicomOutputStream (org.dcm4che3.io.DicomOutputStream)28 IOException (java.io.IOException)11 File (java.io.File)7 Attributes (org.dcm4che3.data.Attributes)5 DicomServiceException (org.dcm4che3.net.service.DicomServiceException)5 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 DicomInputStream (org.dcm4che3.io.DicomInputStream)3 Path (java.nio.file.Path)2 BasicFileAttributes (java.nio.file.attribute.BasicFileAttributes)2 GeneralSecurityException (java.security.GeneralSecurityException)2 ParseException (org.apache.commons.cli.ParseException)2 ValidationResult (org.dcm4che3.data.ValidationResult)2 DicomEncodingOptions (org.dcm4che3.io.DicomEncodingOptions)2 ComponentSampleModel (java.awt.image.ComponentSampleModel)1 BufferedOutputStream (java.io.BufferedOutputStream)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 DataOutputStream (java.io.DataOutputStream)1 InputStream (java.io.InputStream)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 MalformedURLException (java.net.MalformedURLException)1