view Framework/Toolbox/DicomFrameConverter.cpp @ 0:351ab0da0150

initial commit
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 14 Oct 2016 15:34:11 +0200
parents
children 4b7e0244881f
line wrap: on
line source

/**
 * Stone of Orthanc
 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
 * Department, University Hospital 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 "DicomFrameConverter.h"

#include "../Orthanc/Core/Images/Image.h"
#include "../Orthanc/Core/Images/ImageProcessing.h"
#include "../Orthanc/Core/OrthancException.h"
#include "../Orthanc/Core/Toolbox.h"

namespace OrthancStone
{
  void DicomFrameConverter::SetDefaultParameters()
  {
    isSigned_ = true;
    isColor_ = false;
    hasRescale_ = false;
    rescaleIntercept_ = 0;
    rescaleSlope_ = 1;
    defaultWindowCenter_ = 128;
    defaultWindowWidth_ = 256;
  }


  Orthanc::PixelFormat DicomFrameConverter::GetExpectedPixelFormat() const
  {
    // TODO Add more checks, e.g. on the number of bytes per value
    // (cf. DicomImageInformation.h in Orthanc)

    if (isColor_)
    {
      return Orthanc::PixelFormat_RGB24;
    }
    else if (isSigned_)
    {
      return Orthanc::PixelFormat_SignedGrayscale16;
    }
    else
    {
      return Orthanc::PixelFormat_Grayscale16;
    }
  }


  void DicomFrameConverter::ReadParameters(const DicomDataset& dicom)
  {
    SetDefaultParameters();

    if (dicom.HasTag(DICOM_TAG_WINDOW_CENTER))
    {
      Vector c, w;
      dicom.GetVectorValue(c, DICOM_TAG_WINDOW_CENTER);
      dicom.GetVectorValue(w, DICOM_TAG_WINDOW_WIDTH);

      if (c.size() > 0 && w.size() > 0)
      {
        defaultWindowCenter_ = static_cast<float>(c[0]);
        defaultWindowWidth_ = static_cast<float>(w[0]);
      }
    }

    isSigned_ = (dicom.GetIntegerValue(DICOM_TAG_PIXEL_REPRESENTATION) == 1);  // Type 1 tag, must be present

    if (dicom.HasTag(DICOM_TAG_RESCALE_INTERCEPT))
    {
      rescaleIntercept_ = dicom.GetFloatValue(DICOM_TAG_RESCALE_INTERCEPT);
      rescaleSlope_ = dicom.GetFloatValue(DICOM_TAG_RESCALE_SLOPE);
      hasRescale_ = true;
    }

    std::string photometric = dicom.GetStringValue(DICOM_TAG_PHOTOMETRIC_INTERPRETATION);  // Type 1 tag, must be present
    photometric = Orthanc::Toolbox::StripSpaces(photometric);
    isColor_ = (photometric != "MONOCHROME1" &&
                photometric != "MONOCHROME2");
  }


  void DicomFrameConverter::ConvertFrame(std::auto_ptr<Orthanc::ImageAccessor>& source) const
  {
    assert(sizeof(float) == 4);

    if (source.get() == NULL)
    {
      throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange);
    }

    Orthanc::PixelFormat sourceFormat = source->GetFormat();

    if (sourceFormat != GetExpectedPixelFormat())
    {
      throw Orthanc::OrthancException(Orthanc::ErrorCode_IncompatibleImageFormat);
    }

    if (sourceFormat == Orthanc::PixelFormat_RGB24)
    {
      // No conversion has to be done
      return;
    }

    assert(sourceFormat == Orthanc::PixelFormat_Grayscale16 ||
           sourceFormat == Orthanc::PixelFormat_SignedGrayscale16);

    // This is the case of a grayscale frame. Convert it to Float32.
    std::auto_ptr<Orthanc::Image> converted(new Orthanc::Image(Orthanc::PixelFormat_Float32, 
                                                               source->GetWidth(), 
                                                               source->GetHeight()));
    Orthanc::ImageProcessing::Convert(*converted, *source);

    source.reset(NULL);  // We don't need the source frame anymore

    // Correct rescale slope/intercept if need be
    if (hasRescale_)
    {
      for (unsigned int y = 0; y < converted->GetHeight(); y++)
      {
        float* p = reinterpret_cast<float*>(converted->GetRow(y));
        for (unsigned int x = 0; x < converted->GetWidth(); x++, p++)
        {
          float value = *p;

          if (hasRescale_)
          {
            value = value * rescaleSlope_ + rescaleIntercept_;
          }
            
          *p = value;
        }
      }
    }
      
    source = converted;
  }
}