use of org.dcm4che3.img.stream.BytesWithImageDescriptor in project karnak by OsiriX-Foundation.
the class ForwardService method transferOther.
public void transferOther(ForwardDicomNode fwdNode, WebForwardDestination destination, Attributes copy, Params p) throws IOException {
Attributes attributesToSend = new Attributes();
Attributes attributesOriginal = new Attributes();
try {
List<AttributeEditor> editors = destination.getDicomEditors();
DicomStowRS stow = destination.getStowrsSingleFile();
var syntax = new AdaptTransferSyntax(p.getTsuid(), destination.getOutputTransferSyntax(p.getTsuid()));
if (syntax.getRequested().equals(p.getTsuid()) && editors.isEmpty()) {
attributesToSend = copy;
attributesOriginal.addAll(copy);
stow.uploadDicom(copy, syntax.getRequested());
} else {
AttributeEditorContext context = new AttributeEditorContext(p.getTsuid(), fwdNode, null);
Attributes attributes = new Attributes(copy);
attributesToSend = attributes;
attributesOriginal.addAll(attributes);
editors.forEach(e -> e.apply(attributes, context));
if (context.getAbort() == Abort.FILE_EXCEPTION) {
throw new AbortException(context.getAbort(), context.getAbortMessage());
} else if (context.getAbort() == Abort.CONNECTION_EXCEPTION) {
throw new AbortException(context.getAbort(), "DICOM associtation abort. " + context.getAbortMessage());
}
BytesWithImageDescriptor desc = ImageAdapter.imageTranscode(attributes, syntax, context);
if (desc == null) {
stow.uploadDicom(attributes, syntax.getOriginal());
} else {
Editable<PlanarImage> editable = transformImage(attributes, context);
stow.uploadPayload(ImageAdapter.preparePlayload(attributes, syntax, desc, editable));
}
progressNotify(destination, p.getIuid(), p.getCuid(), false, 0);
monitor(fwdNode.getId(), destination.getId(), attributesOriginal, attributesToSend, true, null);
}
} catch (HttpException httpException) {
if (httpException.getStatusCode() != 409) {
progressNotify(destination, p.getIuid(), p.getCuid(), true, 0);
monitor(fwdNode.getId(), destination.getId(), attributesOriginal, attributesToSend, false, httpException.getMessage());
throw new AbortException(Abort.FILE_EXCEPTION, "DICOMWeb forward", httpException);
} else {
progressNotify(destination, p.getIuid(), p.getCuid(), false, 0);
monitor(fwdNode.getId(), destination.getId(), attributesOriginal, attributesToSend, true, null);
LOGGER.debug("File already present in destination");
}
} catch (AbortException e) {
progressNotify(destination, p.getIuid(), p.getCuid(), true, 0);
monitor(fwdNode.getId(), destination.getId(), attributesOriginal, attributesToSend, false, e.getMessage());
if (e.getAbort() == Abort.CONNECTION_EXCEPTION) {
throw e;
}
} catch (IOException e) {
progressNotify(destination, p.getIuid(), p.getCuid(), true, 0);
monitor(fwdNode.getId(), destination.getId(), attributesOriginal, attributesToSend, false, e.getMessage());
throw e;
} catch (Exception e) {
progressNotify(destination, p.getIuid(), p.getCuid(), true, 0);
monitor(fwdNode.getId(), destination.getId(), attributesOriginal, attributesToSend, false, e.getMessage());
LOGGER.error(ERROR_WHEN_FORWARDING, e);
}
}
use of org.dcm4che3.img.stream.BytesWithImageDescriptor in project karnak by OsiriX-Foundation.
the class ForwardService method transfer.
public List<File> transfer(ForwardDicomNode sourceNode, DicomForwardDestination destination, Attributes copy, Params p) throws IOException {
StoreFromStreamSCU streamSCU = destination.getStreamSCU();
DicomInputStream in = null;
List<File> files;
Attributes attributesOriginal = new Attributes();
Attributes attributesToSend = new Attributes();
try {
if (!streamSCU.isReadyForDataTransfer()) {
throw new IllegalStateException("Association not ready for transfer.");
}
DataWriter dataWriter;
String cuid = p.getCuid();
String iuid = p.getIuid();
String tsuid = p.getTsuid();
var syntax = new AdaptTransferSyntax(tsuid, streamSCU.selectTransferSyntax(cuid, destination.getOutputTransferSyntax(tsuid)));
List<AttributeEditor> editors = destination.getDicomEditors();
if (copy == null && editors.isEmpty() && syntax.getRequested().equals(tsuid)) {
dataWriter = new InputStreamDataWriter(p.getData());
attributesToSend = new DicomInputStream(p.getData()).readDataset();
attributesOriginal.addAll(attributesToSend);
} else {
AttributeEditorContext context = new AttributeEditorContext(tsuid, sourceNode, streamSCU.getRemoteDicomNode());
in = new DicomInputStream(p.getData(), tsuid);
in.setIncludeBulkData(IncludeBulkData.URI);
Attributes attributes = in.readDataset();
attributesOriginal.addAll(attributes);
attributesToSend = attributes;
if (copy != null) {
copy.addAll(attributes);
}
if (!editors.isEmpty()) {
editors.forEach(e -> e.apply(attributes, context));
iuid = attributes.getString(Tag.SOPInstanceUID);
cuid = attributes.getString(Tag.SOPClassUID);
}
if (context.getAbort() == Abort.FILE_EXCEPTION) {
if (p.getData() instanceof PDVInputStream) {
((PDVInputStream) p.getData()).skipAll();
}
throw new AbortException(context.getAbort(), context.getAbortMessage());
} else if (context.getAbort() == Abort.CONNECTION_EXCEPTION) {
if (p.getAs() != null) {
p.getAs().abort();
}
throw new AbortException(context.getAbort(), "DICOM association abort: " + context.getAbortMessage());
}
BytesWithImageDescriptor desc = ImageAdapter.imageTranscode(attributes, syntax, context);
Editable<PlanarImage> editable = transformImage(attributes, context);
dataWriter = ImageAdapter.buildDataWriter(attributes, syntax, editable, desc);
}
streamSCU.cstore(cuid, iuid, p.getPriority(), dataWriter, syntax.getSuitable());
progressNotify(destination, p.getIuid(), p.getCuid(), false, streamSCU.getNumberOfSuboperations());
monitor(sourceNode.getId(), destination.getId(), attributesOriginal, attributesToSend, true, null);
} catch (AbortException e) {
progressNotify(destination, p.getIuid(), p.getCuid(), true, streamSCU.getNumberOfSuboperations());
monitor(sourceNode.getId(), destination.getId(), attributesOriginal, attributesToSend, false, e.getMessage());
if (e.getAbort() == Abort.CONNECTION_EXCEPTION) {
throw e;
}
} catch (IOException e) {
progressNotify(destination, p.getIuid(), p.getCuid(), true, streamSCU.getNumberOfSuboperations());
monitor(sourceNode.getId(), destination.getId(), attributesOriginal, attributesToSend, false, e.getMessage());
throw e;
} catch (Exception e) {
if (e instanceof InterruptedException) {
Thread.currentThread().interrupt();
}
progressNotify(destination, p.getIuid(), p.getCuid(), true, streamSCU.getNumberOfSuboperations());
monitor(sourceNode.getId(), destination.getId(), attributesOriginal, attributesToSend, false, e.getMessage());
LOGGER.error(ERROR_WHEN_FORWARDING, e);
} finally {
streamSCU.triggerCloseExecutor();
files = cleanOrGetBulkDataFiles(in, copy == null);
}
return files;
}
use of org.dcm4che3.img.stream.BytesWithImageDescriptor in project karnak by OsiriX-Foundation.
the class ForwardService method transfer.
public List<File> transfer(ForwardDicomNode fwdNode, WebForwardDestination destination, Attributes copy, Params p) throws IOException {
DicomInputStream in = null;
List<File> files;
Attributes attributesToSend = new Attributes();
Attributes attributesOriginal = new Attributes();
try {
List<AttributeEditor> editors = destination.getDicomEditors();
DicomStowRS stow = destination.getStowrsSingleFile();
var syntax = new AdaptTransferSyntax(p.getTsuid(), destination.getOutputTransferSyntax(p.getTsuid()));
if (syntax.getRequested().equals(p.getTsuid()) && copy == null && editors.isEmpty()) {
Attributes fmi = Attributes.createFileMetaInformation(p.getIuid(), p.getCuid(), syntax.getRequested());
try (InputStream stream = p.getData()) {
attributesToSend = new DicomInputStream(p.getData()).readDataset();
attributesOriginal.addAll(attributesToSend);
stow.uploadDicom(stream, fmi);
} catch (HttpException httpException) {
if (httpException.getStatusCode() != 409) {
throw new AbortException(Abort.FILE_EXCEPTION, httpException.getMessage());
} else {
LOGGER.debug("File already present in destination");
}
}
} else {
AttributeEditorContext context = new AttributeEditorContext(p.getTsuid(), fwdNode, null);
in = new DicomInputStream(p.getData(), p.getTsuid());
in.setIncludeBulkData(IncludeBulkData.URI);
Attributes attributes = in.readDataset();
attributesToSend = attributes;
attributesOriginal.addAll(attributes);
if (copy != null) {
copy.addAll(attributes);
}
if (!editors.isEmpty()) {
editors.forEach(e -> e.apply(attributes, context));
}
if (context.getAbort() == Abort.FILE_EXCEPTION) {
if (p.getData() instanceof PDVInputStream) {
((PDVInputStream) p.getData()).skipAll();
}
throw new AbortException(context.getAbort(), context.getAbortMessage());
} else if (context.getAbort() == Abort.CONNECTION_EXCEPTION) {
if (p.getAs() != null) {
p.getAs().abort();
}
throw new AbortException(context.getAbort(), "STOW-RS abort: " + context.getAbortMessage());
}
BytesWithImageDescriptor desc = ImageAdapter.imageTranscode(attributes, syntax, context);
if (desc == null) {
stow.uploadDicom(attributes, syntax.getOriginal());
} else {
Editable<PlanarImage> editable = transformImage(attributes, context);
stow.uploadPayload(ImageAdapter.preparePlayload(attributes, syntax, desc, editable));
}
}
progressNotify(destination, p.getIuid(), p.getCuid(), false, 0);
monitor(fwdNode.getId(), destination.getId(), attributesOriginal, attributesToSend, true, null);
} catch (AbortException e) {
progressNotify(destination, p.getIuid(), p.getCuid(), true, 0);
monitor(fwdNode.getId(), destination.getId(), attributesOriginal, attributesToSend, false, e.getMessage());
if (e.getAbort() == Abort.CONNECTION_EXCEPTION) {
throw e;
}
} catch (IOException e) {
progressNotify(destination, p.getIuid(), p.getCuid(), true, 0);
monitor(fwdNode.getId(), destination.getId(), attributesOriginal, attributesToSend, false, e.getMessage());
throw e;
} catch (Exception e) {
progressNotify(destination, p.getIuid(), p.getCuid(), true, 0);
monitor(fwdNode.getId(), destination.getId(), attributesOriginal, attributesToSend, false, e.getMessage());
LOGGER.error(ERROR_WHEN_FORWARDING, e);
} finally {
files = cleanOrGetBulkDataFiles(in, copy == null);
}
return files;
}
use of org.dcm4che3.img.stream.BytesWithImageDescriptor in project karnak by OsiriX-Foundation.
the class ForwardService method transferOther.
public void transferOther(ForwardDicomNode fwdNode, DicomForwardDestination destination, Attributes copy, Params p) throws IOException {
StoreFromStreamSCU streamSCU = destination.getStreamSCU();
Attributes attributesToSend = new Attributes();
Attributes attributesOriginal = new Attributes();
try {
if (!streamSCU.isReadyForDataTransfer()) {
throw new IllegalStateException("Association not ready for transfer.");
}
DataWriter dataWriter;
String tsuid = p.getTsuid();
String iuid = p.getIuid();
String cuid = p.getCuid();
var syntax = new AdaptTransferSyntax(tsuid, streamSCU.selectTransferSyntax(cuid, destination.getOutputTransferSyntax(tsuid)));
List<AttributeEditor> editors = destination.getDicomEditors();
if (editors.isEmpty() && syntax.getRequested().equals(tsuid)) {
dataWriter = new DataWriterAdapter(copy);
attributesOriginal.addAll(copy);
attributesToSend = copy;
} else {
AttributeEditorContext context = new AttributeEditorContext(tsuid, fwdNode, streamSCU.getRemoteDicomNode());
Attributes attributes = new Attributes(copy);
attributesOriginal.addAll(attributes);
attributesToSend = attributes;
if (!editors.isEmpty()) {
editors.forEach(e -> e.apply(attributes, context));
iuid = attributes.getString(Tag.SOPInstanceUID);
cuid = attributes.getString(Tag.SOPClassUID);
}
if (context.getAbort() == Abort.FILE_EXCEPTION) {
throw new AbortException(context.getAbort(), context.getAbortMessage());
} else if (context.getAbort() == Abort.CONNECTION_EXCEPTION) {
throw new AbortException(context.getAbort(), "DICOM association abort. " + context.getAbortMessage());
}
BytesWithImageDescriptor desc = ImageAdapter.imageTranscode(attributes, syntax, context);
Editable<PlanarImage> editable = transformImage(attributes, context);
dataWriter = ImageAdapter.buildDataWriter(attributes, syntax, editable, desc);
}
streamSCU.cstore(cuid, iuid, p.getPriority(), dataWriter, syntax.getSuitable());
progressNotify(destination, p.getIuid(), p.getCuid(), false, streamSCU.getNumberOfSuboperations());
monitor(fwdNode.getId(), destination.getId(), attributesOriginal, attributesToSend, true, null);
} catch (AbortException e) {
progressNotify(destination, p.getIuid(), p.getCuid(), true, streamSCU.getNumberOfSuboperations());
monitor(fwdNode.getId(), destination.getId(), attributesOriginal, attributesToSend, false, e.getMessage());
if (e.getAbort() == Abort.CONNECTION_EXCEPTION) {
throw e;
}
} catch (IOException e) {
progressNotify(destination, p.getIuid(), p.getCuid(), true, streamSCU.getNumberOfSuboperations());
monitor(fwdNode.getId(), destination.getId(), attributesOriginal, attributesToSend, false, e.getMessage());
throw e;
} catch (Exception e) {
if (e instanceof InterruptedException) {
Thread.currentThread().interrupt();
}
progressNotify(destination, p.getIuid(), p.getCuid(), true, streamSCU.getNumberOfSuboperations());
monitor(fwdNode.getId(), destination.getId(), attributesOriginal, attributesToSend, false, e.getMessage());
LOGGER.error(ERROR_WHEN_FORWARDING, e);
} finally {
streamSCU.triggerCloseExecutor();
}
}
use of org.dcm4che3.img.stream.BytesWithImageDescriptor in project Weasis by nroduit.
the class DicomImageElement method saveToFile.
@Override
public Attributes saveToFile(File output, DicomExportParameters params) {
boolean hasTransformation = params.dicomEditors() != null && !params.dicomEditors().isEmpty();
if (!hasTransformation && params.syntax() == null) {
super.saveToFile(output);
return new Attributes();
}
DicomMetaData metaData = getMediaReader().getDicomMetaData();
String outputTsuid = params.syntax() == null ? metaData.getTransferSyntaxUID() : params.syntax().getTransferSyntaxUID();
outputTsuid = getOutputTransferSyntax(true, metaData.getTransferSyntaxUID(), outputTsuid);
var adaptTransferSyntax = new AdaptTransferSyntax(metaData.getTransferSyntaxUID(), outputTsuid);
adaptTransferSyntax.setJpegQuality(params.compressionQuality());
adaptTransferSyntax.setCompressionRatioFactor(params.compressionRatioFactor());
Attributes attributes = new Attributes(metaData.getDicomObject());
AttributeEditorContext context = new AttributeEditorContext(adaptTransferSyntax.getOriginal(), null, null);
if (hasTransformation) {
params.dicomEditors().forEach(e -> e.apply(attributes, context));
}
BytesWithImageDescriptor desc = ImageAdapter.imageTranscode(attributes, adaptTransferSyntax, context);
if (ImageAdapter.writeDicomFile(attributes, adaptTransferSyntax, context.getEditable(), desc, output)) {
return attributes;
} else {
LOGGER.error("Cannot export DICOM file: {}", getFileCache().getOriginalFile().orElse(null));
return null;
}
}
Aggregations