comparison Framework/Outputs/DicomPyramidWriter.cpp @ 0:4a7a53257c7d

initial commit
author Sebastien Jodogne <s.jodogne@gmail.com>
date Sat, 22 Oct 2016 21:48:33 +0200
parents
children bc3ca410b765
comparison
equal deleted inserted replaced
-1:000000000000 0:4a7a53257c7d
1 /**
2 * Orthanc - A Lightweight, RESTful DICOM Store
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
4 * Department, University Hospital of Liege, Belgium
5 *
6 * This program is free software: you can redistribute it and/or
7 * modify it under the terms of the GNU Affero General Public License
8 * as published by the Free Software Foundation, either version 3 of
9 * the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Affero General Public License for more details.
15 *
16 * You should have received a copy of the GNU Affero General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 **/
19
20
21 #include "DicomPyramidWriter.h"
22
23 #include "../DicomToolbox.h"
24
25 #include "../Orthanc/Core/Logging.h"
26 #include "../Orthanc/Core/OrthancException.h"
27 #include "../Orthanc/OrthancServer/FromDcmtkBridge.h"
28
29 #include <dcmtk/dcmdata/dcdeftag.h>
30 #include <boost/lexical_cast.hpp>
31
32 namespace OrthancWSI
33 {
34 void DicomPyramidWriter::FlushInternal(MultiframeDicomWriter& writer,
35 bool force)
36 {
37 if (writer.GetFramesCount() > 0 &&
38 writer.GetSize() > 0 &&
39 (force || (maxSize_ != 0 && writer.GetSize() >= maxSize_)))
40 {
41 countInstances_ += 1;
42
43 std::string dicom;
44 writer.Flush(dicom, countInstances_);
45 target_.Write(dicom);
46 }
47 }
48
49
50 DcmItem* DicomPyramidWriter::CreateFunctionalGroup(unsigned int frame,
51 unsigned int x,
52 unsigned int y,
53 unsigned int totalWidth,
54 unsigned int totalHeight,
55 float physicalZ) const
56 {
57 float physicalX, physicalY;
58 volume_.GetLocation(physicalX, physicalY, x, y, totalWidth, totalHeight);
59
60 std::string tmpX = boost::lexical_cast<std::string>(physicalX);
61 std::string tmpY = boost::lexical_cast<std::string>(physicalY);
62 std::string tmpZ = boost::lexical_cast<std::string>(physicalZ);
63
64 std::auto_ptr<DcmItem> dimension(new DcmItem);
65 if (!dimension->putAndInsertUint32(DCM_DimensionIndexValues, frame).good())
66 {
67 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError);
68 }
69
70 // From Supp 145: The column position of the top left pixel of the
71 // Total Pixel Matrix is 1. The row position of the top left pixel
72 // of the Total Pixel Matrix is 1.
73 std::auto_ptr<DcmItem> position(new DcmItem);
74 if (!position->putAndInsertSint32(DCM_ColumnPositionInTotalImagePixelMatrix, x + 1).good() ||
75 !position->putAndInsertSint32(DCM_RowPositionInTotalImagePixelMatrix, y + 1).good() ||
76 !position->putAndInsertString(DCM_XOffsetInSlideCoordinateSystem, tmpX.c_str()).good() ||
77 !position->putAndInsertString(DCM_YOffsetInSlideCoordinateSystem, tmpY.c_str()).good() ||
78 !position->putAndInsertString(DCM_ZOffsetInSlideCoordinateSystem, tmpZ.c_str()).good())
79 {
80 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError);
81 }
82
83
84 std::auto_ptr<DcmSequenceOfItems> sequencePosition(new DcmSequenceOfItems(DCM_PlanePositionSlideSequence));
85 if (!sequencePosition->insert(position.release(), false, false).good())
86 {
87 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError);
88 }
89
90 std::auto_ptr<DcmSequenceOfItems> sequenceDimension(new DcmSequenceOfItems(DCM_FrameContentSequence));
91 if (!sequenceDimension->insert(dimension.release(), false, false).good())
92 {
93 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError);
94 }
95
96 std::auto_ptr<DcmItem> item(new DcmItem);
97 if (!item->insert(sequencePosition.release(), false, false).good() ||
98 !item->insert(sequenceDimension.release(), false, false).good())
99 {
100 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError);
101 }
102
103 return item.release();
104 }
105
106
107 void DicomPyramidWriter::WriteRawTileInternal(const std::string& tile,
108 const Level& level,
109 unsigned int x,
110 unsigned int y)
111 {
112 if (x >= level.countTilesX_ ||
113 y >= level.countTilesY_)
114 {
115 LOG(ERROR) << "Tile index out of range: " << x << "," << y
116 << " (max: " << level.countTilesX_ << "," << level.countTilesY_ << ")";
117 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange);
118 }
119
120 const unsigned int z = level.z_;
121
122 {
123 boost::mutex::scoped_lock lock(mutex_);
124
125 if (z >= writers_.size())
126 {
127 writers_.resize(z + 1);
128 }
129
130 MultiframeDicomWriter* writer = writers_[z];
131
132 if (writer == NULL)
133 {
134 writer = new MultiframeDicomWriter
135 (dataset_, GetImageCompression(), GetPixelFormat(), level.width_, level.height_,
136 GetTileWidth(), GetTileHeight());
137 writers_[z] = writer;
138 }
139
140 std::auto_ptr<DcmItem> functionalGroup(CreateFunctionalGroup(writer->GetFramesCount() + 1,
141 x * GetTileWidth(),
142 y * GetTileHeight(),
143 writer->GetTotalWidth(),
144 writer->GetTotalHeight(),
145 0.0f /* TODO Z-plane */));
146
147 writer->AddFrame(tile, functionalGroup.release());
148 FlushInternal(*writer, false);
149
150 countTiles_ ++;
151 }
152 }
153
154
155 DicomPyramidWriter::DicomPyramidWriter(IFileTarget& target,
156 const DcmDataset& dataset,
157 Orthanc::PixelFormat pixelFormat,
158 ImageCompression compression,
159 unsigned int tileWidth,
160 unsigned int tileHeight,
161 size_t maxSize, // If "0", no automatic flushing
162 const ImagedVolumeParameters& volume) :
163 PyramidWriterBase(pixelFormat, compression, tileWidth, tileHeight),
164 target_(target),
165 dataset_(dataset),
166 maxSize_(maxSize),
167 countTiles_(0),
168 countInstances_(0),
169 volume_(volume)
170 {
171 }
172
173
174 DicomPyramidWriter::~DicomPyramidWriter()
175 {
176 LOG(WARNING) << "Closing the DICOM pyramid (" << countTiles_ << " tiles were written)";
177
178 for (size_t i = 0; i < writers_.size(); i++)
179 {
180 if (writers_[i] != NULL)
181 {
182 FlushInternal(*writers_[i], true);
183 delete writers_[i];
184 }
185 }
186 }
187
188
189 void DicomPyramidWriter::Flush()
190 {
191 boost::mutex::scoped_lock lock(mutex_);
192
193 for (size_t i = 0; i < writers_.size(); i++)
194 {
195 FlushInternal(*writers_[i], true);
196 }
197 }
198 }