view Framework/Oracle/GetOrthancImageCommand.cpp @ 1205:6009c59d8676 broker

fix to sdl
author Sebastien Jodogne <s.jodogne@gmail.com>
date Mon, 02 Dec 2019 14:32:05 +0100
parents 78b8bfe154bc
children c1c83c1fb837
line wrap: on
line source

/**
 * Stone of Orthanc
 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
 * Department, University Hospital of Liege, Belgium
 * Copyright (C) 2017-2019 Osimis S.A., Belgium
 *
 * This program is free software: you can redistribute it and/or
 * modify it under the terms of the GNU Affero General Public License
 * as published by the Free Software Foundation, either version 3 of
 * the License, or (at your option) any later version.
 *
 * 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
 * Affero General Public License for more details.
 * 
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 **/


#include "GetOrthancImageCommand.h"

#include <Core/Images/JpegReader.h>
#include <Core/Images/PamReader.h>
#include <Core/Images/PngReader.h>
#include <Core/OrthancException.h>
#include <Core/Toolbox.h>

namespace OrthancStone
{
  GetOrthancImageCommand::GetOrthancImageCommand() :
    uri_("/"),
    timeout_(600),
    hasExpectedFormat_(false)
  {
  }


  void GetOrthancImageCommand::SetExpectedPixelFormat(Orthanc::PixelFormat format)
  {
    hasExpectedFormat_ = true;
    expectedFormat_ = format;
  }


  static std::string GetFormatSuffix(Orthanc::PixelFormat pixelFormat)
  {
    switch (pixelFormat)
    {
      case Orthanc::PixelFormat_RGB24:
        return "preview";
      
      case Orthanc::PixelFormat_Grayscale16:
        return "image-uint16";
      
      case Orthanc::PixelFormat_SignedGrayscale16:
        return "image-int16";
      
      default:
        throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange);
    }
  }


  void GetOrthancImageCommand::SetInstanceUri(const std::string& instance,
                                              Orthanc::PixelFormat pixelFormat)
  {
    uri_ = "/instances/" + instance + "/" + GetFormatSuffix(pixelFormat);
  }


  void GetOrthancImageCommand::SetFrameUri(const std::string& instance,
                                           unsigned int frame,
                                           Orthanc::PixelFormat pixelFormat)
  {
    uri_ = ("/instances/" + instance + "/frames/" +
            boost::lexical_cast<std::string>(frame) + "/" + GetFormatSuffix(pixelFormat));
  }


  void GetOrthancImageCommand::ProcessHttpAnswer(boost::weak_ptr<IObserver> receiver,
                                                 IMessageEmitter& emitter,
                                                 const std::string& answer,
                                                 const HttpHeaders& answerHeaders) const
  {
    Orthanc::MimeType contentType = Orthanc::MimeType_Binary;

    for (HttpHeaders::const_iterator it = answerHeaders.begin(); 
         it != answerHeaders.end(); ++it)
    {
      std::string s;
      Orthanc::Toolbox::ToLowerCase(s, it->first);

      if (s == "content-type")
      {
        contentType = Orthanc::StringToMimeType(it->second);
        break;
      }
    }

    std::auto_ptr<Orthanc::ImageAccessor> image;

    switch (contentType)
    {
      case Orthanc::MimeType_Png:
      {
        image.reset(new Orthanc::PngReader);
        dynamic_cast<Orthanc::PngReader&>(*image).ReadFromMemory(answer);
        break;
      }

      case Orthanc::MimeType_Pam:
      {
        image.reset(new Orthanc::PamReader);
        dynamic_cast<Orthanc::PamReader&>(*image).ReadFromMemory(answer);
        break;
      }

      case Orthanc::MimeType_Jpeg:
      {
        image.reset(new Orthanc::JpegReader);
        dynamic_cast<Orthanc::JpegReader&>(*image).ReadFromMemory(answer);
        break;
      }

      default:
        throw Orthanc::OrthancException(Orthanc::ErrorCode_NetworkProtocol,
                                        "Unsupported HTTP Content-Type for an image: " + 
                                        std::string(Orthanc::EnumerationToString(contentType)));
    }

    if (hasExpectedFormat_)
    {
      if (expectedFormat_ == Orthanc::PixelFormat_SignedGrayscale16 &&
          image->GetFormat() == Orthanc::PixelFormat_Grayscale16)
      {
        image->SetFormat(Orthanc::PixelFormat_SignedGrayscale16);
      }

      if (expectedFormat_ != image->GetFormat())
      {
        throw Orthanc::OrthancException(Orthanc::ErrorCode_IncompatibleImageFormat);
      }
    }

    SuccessMessage message(*this, *image, contentType);
    emitter.EmitMessage(receiver, message);
  }
}