1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527
|
/**
* Orthanc - A Lightweight, RESTful DICOM Store
* Copyright (C) 2012-2014 Medical Physics Department, CHU of Liege,
* Belgium
*
* This program is free software: you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* In addition, as a special exception, the copyright holders of this
* program give permission to link the code of its release with the
* OpenSSL project's "OpenSSL" library (or with modified versions of it
* that use the same license as the "OpenSSL" library), and distribute
* the linked executables. You must obey the GNU General Public License
* in all respects for all of the code used other than "OpenSSL". If you
* modify file(s) with this exception, you may extend this exception to
* your version of the file(s), but you are not obligated to do so. If
* you do not wish to do so, delete this exception statement from your
* version. If you delete this exception statement from all source files
* in the program, then also delete it here.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
**/
#include "../Core/PrecompiledHeaders.h"
#include "Series.h"
#include "OrthancConnection.h"
#include <set>
#include <boost/lexical_cast.hpp>
namespace OrthancClient
{
namespace
{
class SliceLocator
{
private:
float normal_[3];
public:
SliceLocator(Instance& someSlice)
{
/**
* Compute the slice normal from Image Orientation Patient.
* http://nipy.sourceforge.net/nibabel/dicom/dicom_orientation.html#dicom-z-from-slice
* http://dicomiseasy.blogspot.be/2013/06/getting-oriented-using-image-plane.html
* http://www.itk.org/pipermail/insight-users/2003-September/004762.html
**/
std::vector<float> cosines;
someSlice.SplitVectorOfFloats(cosines, "ImageOrientationPatient"); // 0020-0037
if (cosines.size() != 6)
{
throw OrthancClientException(Orthanc::ErrorCode_BadFileFormat);
}
normal_[0] = cosines[1] * cosines[5] - cosines[2] * cosines[4];
normal_[1] = cosines[2] * cosines[3] - cosines[0] * cosines[5];
normal_[2] = cosines[0] * cosines[4] - cosines[1] * cosines[3];
}
/**
* Compute the distance of some slice along the slice normal.
**/
float ComputeSliceLocation(Instance& instance) const
{
std::vector<float> ipp;
instance.SplitVectorOfFloats(ipp, "ImagePositionPatient"); // 0020-0032
if (ipp.size() != 3)
{
throw OrthancClientException(Orthanc::ErrorCode_BadFileFormat);
}
float dist = 0;
for (int i = 0; i < 3; i++)
{
dist += normal_[i] * ipp[i];
}
return dist;
}
};
class ImageDownloadCommand : public Orthanc::ICommand
{
private:
Orthanc::PixelFormat format_;
Orthanc::ImageExtractionMode mode_;
Instance& instance_;
void* target_;
size_t lineStride_;
public:
ImageDownloadCommand(Instance& instance,
Orthanc::PixelFormat format,
Orthanc::ImageExtractionMode mode,
void* target,
size_t lineStride) :
format_(format),
mode_(mode),
instance_(instance),
target_(target),
lineStride_(lineStride)
{
instance_.SetImageExtractionMode(mode);
}
virtual bool Execute()
{
using namespace Orthanc;
unsigned int width = instance_.GetHeight();
for (unsigned int y = 0; y < instance_.GetHeight(); y++)
{
uint8_t* p = reinterpret_cast<uint8_t*>(target_) + y * lineStride_;
if (instance_.GetPixelFormat() == format_)
{
memcpy(p, instance_.GetBuffer(y), GetBytesPerPixel(instance_.GetPixelFormat()) * instance_.GetWidth());
}
else if (instance_.GetPixelFormat() == PixelFormat_Grayscale8 &&
format_ == PixelFormat_RGB24)
{
const uint8_t* s = reinterpret_cast<const uint8_t*>(instance_.GetBuffer(y));
for (unsigned int x = 0; x < width; x++, s++, p += 3)
{
p[0] = *s;
p[1] = *s;
p[2] = *s;
}
}
else
{
throw OrthancClientException(ErrorCode_NotImplemented);
}
}
// Do not keep the image in memory, as we are loading 3D images
instance_.DiscardImage();
return true;
}
};
class ProgressToFloatListener : public Orthanc::ThreadedCommandProcessor::IListener
{
private:
float* target_;
public:
ProgressToFloatListener(float* target) : target_(target)
{
}
virtual void SignalProgress(unsigned int current,
unsigned int total)
{
if (total == 0)
{
*target_ = 0;
}
else
{
*target_ = static_cast<float>(current) / static_cast<float>(total);
}
}
virtual void SignalSuccess(unsigned int total)
{
*target_ = 1;
}
virtual void SignalFailure()
{
*target_ = 0;
}
virtual void SignalCancel()
{
*target_ = 0;
}
};
}
void Series::Check3DImage()
{
if (!Is3DImage())
{
throw OrthancClientException(Orthanc::ErrorCode_NotImplemented);
}
}
bool Series::Is3DImageInternal()
{
try
{
if (GetInstanceCount() == 0)
{
// Empty image, use some default value (should never happen)
voxelSizeX_ = 1;
voxelSizeY_ = 1;
voxelSizeZ_ = 1;
sliceThickness_ = 1;
return true;
}
// Choose a reference slice
Instance& reference = GetInstance(0);
// Check that all the child instances share the same 3D parameters
for (unsigned int i = 0; i < GetInstanceCount(); i++)
{
Instance& i2 = GetInstance(i);
if (std::string(reference.GetTagAsString("Columns")) != std::string(i2.GetTagAsString("Columns")) ||
std::string(reference.GetTagAsString("Rows")) != std::string(i2.GetTagAsString("Rows")) ||
std::string(reference.GetTagAsString("ImageOrientationPatient")) != std::string(i2.GetTagAsString("ImageOrientationPatient")) ||
std::string(reference.GetTagAsString("SliceThickness")) != std::string(i2.GetTagAsString("SliceThickness")) ||
std::string(reference.GetTagAsString("PixelSpacing")) != std::string(i2.GetTagAsString("PixelSpacing")))
{
return false;
}
}
// Extract X/Y voxel size and slice thickness
std::string s = GetInstance(0).GetTagAsString("PixelSpacing"); // 0028-0030
size_t pos = s.find('\\');
assert(pos != std::string::npos);
std::string sy = s.substr(0, pos);
std::string sx = s.substr(pos + 1);
try
{
voxelSizeX_ = boost::lexical_cast<float>(sx);
voxelSizeY_ = boost::lexical_cast<float>(sy);
}
catch (boost::bad_lexical_cast)
{
throw OrthancClientException(Orthanc::ErrorCode_BadFileFormat);
}
sliceThickness_ = GetInstance(0).GetTagAsFloat("SliceThickness"); // 0018-0050
// Compute the location of each slice to extract the voxel size along Z
voxelSizeZ_ = std::numeric_limits<float>::infinity();
SliceLocator locator(reference);
float referenceSliceLocation = locator.ComputeSliceLocation(reference);
std::set<float> l;
for (unsigned int i = 0; i < GetInstanceCount(); i++)
{
float location = locator.ComputeSliceLocation(GetInstance(i));
float distanceToReferenceSlice = fabs(location - referenceSliceLocation);
l.insert(location);
if (distanceToReferenceSlice > std::numeric_limits<float>::epsilon() &&
distanceToReferenceSlice < voxelSizeZ_)
{
voxelSizeZ_ = distanceToReferenceSlice;
}
}
// Make sure that 2 slices do not share the same Z location
return l.size() == GetInstanceCount();
}
catch (OrthancClientException)
{
return false;
}
}
void Series::ReadSeries()
{
Orthanc::HttpClient client(connection_.GetHttpClient());
client.SetUrl(std::string(connection_.GetOrthancUrl()) + "/series/" + id_);
Json::Value v;
if (!client.Apply(series_))
{
throw OrthancClientException(Orthanc::ErrorCode_NetworkProtocol);
}
}
Orthanc::IDynamicObject* Series::GetFillerItem(size_t index)
{
Json::Value::ArrayIndex tmp = static_cast<Json::Value::ArrayIndex>(index);
std::string id = series_["Instances"][tmp].asString();
return new Instance(connection_, id.c_str());
}
Series::Series(const OrthancConnection& connection,
const char* id) :
connection_(connection),
id_(id),
instances_(*this)
{
ReadSeries();
status_ = Status3DImage_NotTested;
url_ = std::string(connection_.GetOrthancUrl()) + "/series/" + id_;
voxelSizeX_ = 0;
voxelSizeY_ = 0;
voxelSizeZ_ = 0;
sliceThickness_ = 0;
instances_.SetThreadCount(connection.GetThreadCount());
}
bool Series::Is3DImage()
{
if (status_ == Status3DImage_NotTested)
{
status_ = Is3DImageInternal() ? Status3DImage_True : Status3DImage_False;
}
return status_ == Status3DImage_True;
}
unsigned int Series::GetInstanceCount()
{
return instances_.GetSize();
}
Instance& Series::GetInstance(unsigned int index)
{
return dynamic_cast<Instance&>(instances_.GetItem(index));
}
unsigned int Series::GetWidth()
{
Check3DImage();
if (GetInstanceCount() == 0)
return 0;
else
return GetInstance(0).GetTagAsInt("Columns");
}
unsigned int Series::GetHeight()
{
Check3DImage();
if (GetInstanceCount() == 0)
return 0;
else
return GetInstance(0).GetTagAsInt("Rows");
}
const char* Series::GetMainDicomTag(const char* tag, const char* defaultValue) const
{
if (series_["MainDicomTags"].isMember(tag))
{
return series_["MainDicomTags"][tag].asCString();
}
else
{
return defaultValue;
}
}
void Series::Load3DImageInternal(void* target,
Orthanc::PixelFormat format,
size_t lineStride,
size_t stackStride,
Orthanc::ThreadedCommandProcessor::IListener* listener)
{
using namespace Orthanc;
// Choose the extraction mode, depending on the format of the
// target image.
uint8_t bytesPerPixel;
ImageExtractionMode mode;
switch (format)
{
case PixelFormat_RGB24:
bytesPerPixel = 3;
mode = ImageExtractionMode_Preview;
break;
case PixelFormat_Grayscale8:
bytesPerPixel = 1;
mode = ImageExtractionMode_UInt8; // Preview ???
break;
case PixelFormat_Grayscale16:
bytesPerPixel = 2;
mode = ImageExtractionMode_UInt16;
break;
case PixelFormat_SignedGrayscale16:
bytesPerPixel = 2;
mode = ImageExtractionMode_UInt16;
format = PixelFormat_Grayscale16;
break;
default:
throw OrthancClientException(ErrorCode_NotImplemented);
}
// Check that the target image is properly sized
unsigned int sx = GetWidth();
unsigned int sy = GetHeight();
if (lineStride < sx * bytesPerPixel ||
stackStride < sx * sy * bytesPerPixel)
{
throw OrthancClientException(ErrorCode_BadRequest);
}
if (sx == 0 || sy == 0 || GetInstanceCount() == 0)
{
// Empty image, nothing to do
if (listener)
listener->SignalSuccess(0);
return;
}
/**
* Order the stacks according to their distance along the slice
* normal (using the "Image Position Patient" tag). This works
* even if the "SliceLocation" tag is absent.
**/
SliceLocator locator(GetInstance(0));
typedef std::map<float, Instance*> Instances;
Instances instances;
for (unsigned int i = 0; i < GetInstanceCount(); i++)
{
float dist = locator.ComputeSliceLocation(GetInstance(i));
instances[dist] = &GetInstance(i);
}
if (instances.size() != GetInstanceCount())
{
// Several instances have the same Z coordinate
throw OrthancClientException(ErrorCode_NotImplemented);
}
// Submit the download of each stack as a set of commands
ThreadedCommandProcessor processor(connection_.GetThreadCount());
if (listener != NULL)
{
processor.SetListener(*listener);
}
uint8_t* stackTarget = reinterpret_cast<uint8_t*>(target);
for (Instances::iterator it = instances.begin(); it != instances.end(); ++it)
{
processor.Post(new ImageDownloadCommand(*it->second, format, mode, stackTarget, lineStride));
stackTarget += stackStride;
}
// Wait for all the stacks to be downloaded
if (!processor.Join())
{
throw OrthancClientException(ErrorCode_NetworkProtocol);
}
}
float Series::GetVoxelSizeX()
{
Check3DImage(); // Is3DImageInternal() will compute the voxel sizes
return voxelSizeX_;
}
float Series::GetVoxelSizeY()
{
Check3DImage(); // Is3DImageInternal() will compute the voxel sizes
return voxelSizeY_;
}
float Series::GetVoxelSizeZ()
{
Check3DImage(); // Is3DImageInternal() will compute the voxel sizes
return voxelSizeZ_;
}
float Series::GetSliceThickness()
{
Check3DImage(); // Is3DImageInternal() will compute the voxel sizes
return sliceThickness_;
}
void Series::Load3DImage(void* target,
Orthanc::PixelFormat format,
int64_t lineStride,
int64_t stackStride,
float* progress)
{
ProgressToFloatListener listener(progress);
Load3DImageInternal(target, format, static_cast<size_t>(lineStride),
static_cast<size_t>(stackStride), &listener);
}
}
|