# HG changeset patch # User Sebastien Jodogne # Date 1388767771 -3600 # Node ID aa5ca7a2166f4f1aaa59fda201f163769d12560e # Parent 9a8716686f18f826c96fb8b518501d9738240be2 experiments with pixel data diff -r 9a8716686f18 -r aa5ca7a2166f OrthancServer/FromDcmtkBridge.cpp --- a/OrthancServer/FromDcmtkBridge.cpp Tue Dec 03 13:50:21 2013 +0100 +++ b/OrthancServer/FromDcmtkBridge.cpp Fri Jan 03 17:49:31 2014 +0100 @@ -121,6 +121,10 @@ #include #include #include +#include +#include +#include + #include #include @@ -209,13 +213,65 @@ output.AnswerJson(v); } + + static void AnswerPixelData(RestApiOutput& output, + DcmPixelData& pixelData, + E_TransferSyntax transferSyntax) + { + DcmPixelSequence* pixelSequence = NULL; + if (pixelData.getEncapsulatedRepresentation + (transferSyntax, NULL, pixelSequence).good() && pixelSequence) + { + for (unsigned long i = 0; i < pixelSequence->card(); i++) + { + DcmPixelItem* pixelItem = NULL; + if (pixelSequence->getItem(pixelItem, i).good() && pixelItem) + { + Uint8* b = NULL; + if (pixelItem->getUint8Array(b).good() && b) + { + // JPEG-LS (lossless) + // http://gdcm.sourceforge.net/wiki/index.php/Tools/ffmpeg#JPEG_LS + // http://www.stat.columbia.edu/~jakulin/jpeg-ls/ + // http://itohws03.ee.noda.sut.ac.jp/~matsuda/mrp/ + + printf("ITEM: %d\n", pixelItem->getLength()); + char buf[64]; + sprintf(buf, "/tmp/toto-%06ld.jpg", i); + FILE* fp = fopen(buf, "wb"); + fwrite(b, pixelItem->getLength(), 1, fp); + fclose(fp); + } + } + } + } + } + + static void AnswerDicomField(RestApiOutput& output, - DcmElement& element) + DcmElement& element, + E_TransferSyntax transferSyntax) { - // This element is not a sequence + // This element is not a sequence. Test if it is pixel data. + if (element.getTag().getGTag() == DICOM_TAG_PIXEL_DATA.GetGroup() && + element.getTag().getETag() == DICOM_TAG_PIXEL_DATA.GetElement()) + { + try + { + DcmPixelData& pixelData = dynamic_cast(element); + AnswerPixelData(output, pixelData, transferSyntax); + return; + } + catch (std::bad_cast&) + { + } + } + + + // This element is nor a sequence, neither a pixel-data std::string buffer; buffer.resize(65536); - Uint32 length = element.getLength(); + Uint32 length = element.getLength(transferSyntax); Uint32 offset = 0; output.GetLowLevelOutput().SendOkHeader("application/octet-stream", true, length, NULL); @@ -232,14 +288,16 @@ nbytes = buffer.size(); } - if (element.getPartialValue(&buffer[0], offset, nbytes).good()) + OFCondition cond = element.getPartialValue(&buffer[0], offset, nbytes); + + if (cond.good()) { output.GetLowLevelOutput().Send(&buffer[0], nbytes); offset += nbytes; } else { - LOG(ERROR) << "Error while sending a DICOM field"; + LOG(ERROR) << "Error while sending a DICOM field: " << cond.text(); return; } } @@ -249,7 +307,8 @@ static void SendPathValueForLeaf(RestApiOutput& output, const std::string& tag, - DcmItem& dicom) + DcmItem& dicom, + E_TransferSyntax transferSyntax) { DcmTagKey k; ParseTagAndGroup(k, tag); @@ -269,7 +328,7 @@ //element->getVR() != EVR_UNKNOWN && // This would forbid private tags element->getVR() != EVR_SQ) { - AnswerDicomField(output, *element); + AnswerDicomField(output, *element, transferSyntax); } } @@ -310,7 +369,7 @@ } else { - SendPathValueForLeaf(output, uri.back(), *dicom); + SendPathValueForLeaf(output, uri.back(), *dicom, file_->getDataset()->getOriginalXfer()); } }