Mercurial > hg > orthanc-wsi
view ViewerPlugin/OrthancPyramidFrameFetcher.cpp @ 355:a307ec25a008
padding for on-the-fly pyramids
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Fri, 20 Dec 2024 13:48:34 +0100 |
parents | c36a1fee7a7d |
children | 9e4dcbb578e3 |
line wrap: on
line source
/** * Orthanc - A Lightweight, RESTful DICOM Store * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics * Department, University Hospital of Liege, Belgium * Copyright (C) 2017-2023 Osimis S.A., Belgium * Copyright (C) 2024-2024 Orthanc Team SRL, Belgium * Copyright (C) 2021-2024 Sebastien Jodogne, ICTEAM UCLouvain, 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 "../Framework/PrecompiledHeadersWSI.h" #include "OrthancPyramidFrameFetcher.h" #include "../Framework/Inputs/OnTheFlyPyramid.h" #include <Images/Image.h> #include <Images/ImageProcessing.h> #include "../Resources/Orthanc/Plugins/OrthancPluginCppWrapper.h" namespace OrthancWSI { void OrthancPyramidFrameFetcher::RenderGrayscale(Orthanc::ImageAccessor& target, const Orthanc::ImageAccessor& source) { Orthanc::Image converted(Orthanc::PixelFormat_Float32, source.GetWidth(), source.GetHeight(), false); Orthanc::ImageProcessing::Convert(converted, source); float minValue, maxValue; Orthanc::ImageProcessing::GetMinMaxFloatValue(minValue, maxValue, converted); assert(minValue <= maxValue); if (std::abs(maxValue - minValue) < 0.0001) { Orthanc::ImageProcessing::Set(target, 0); } else { const float scaling = 255.0f / (maxValue - minValue); const float offset = -minValue; Orthanc::Image rescaled(Orthanc::PixelFormat_Grayscale8, source.GetWidth(), source.GetHeight(), false); Orthanc::ImageProcessing::ShiftScale(rescaled, converted, static_cast<float>(offset), static_cast<float>(scaling), false); Orthanc::ImageProcessing::Convert(target, rescaled); } } OrthancPyramidFrameFetcher::OrthancPyramidFrameFetcher(OrthancStone::IOrthancConnection* orthanc, bool smooth) : orthanc_(orthanc), smooth_(smooth), tileWidth_(512), tileHeight_(512), paddingX_(0), paddingY_(0), backgroundRed_(0), backgroundGreen_(0), backgroundBlue_(0) { if (orthanc == NULL) { throw Orthanc::OrthancException(Orthanc::ErrorCode_NullPointer); } } void OrthancPyramidFrameFetcher::SetTileWidth(unsigned int tileWidth) { if (tileWidth <= 2) { throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange); } else { tileWidth_ = tileWidth; } } void OrthancPyramidFrameFetcher::SetTileHeight(unsigned int tileHeight) { if (tileHeight <= 2) { throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange); } else { tileHeight_ = tileHeight; } } void OrthancPyramidFrameFetcher::SetBackgroundColor(uint8_t red, uint8_t green, uint8_t blue) { backgroundRed_ = red; backgroundGreen_ = green; backgroundBlue_ = blue; } DecodedTiledPyramid* OrthancPyramidFrameFetcher::Fetch(const std::string &instanceId, unsigned frameNumber) { OrthancPlugins::MemoryBuffer buffer; buffer.GetDicomInstance(instanceId.c_str()); OrthancPlugins::DicomInstance dicom(buffer.GetData(), buffer.GetSize()); std::unique_ptr<OrthancPlugins::OrthancImage> frame(dicom.GetDecodedFrame(frameNumber)); Orthanc::PixelFormat format; switch (frame->GetPixelFormat()) { case OrthancPluginPixelFormat_RGB24: format = Orthanc::PixelFormat_RGB24; break; case OrthancPluginPixelFormat_Grayscale8: format = Orthanc::PixelFormat_Grayscale8; break; case OrthancPluginPixelFormat_Grayscale16: format = Orthanc::PixelFormat_Grayscale16; break; default: throw Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented); } Orthanc::ImageAccessor source; source.AssignReadOnly(format, frame->GetWidth(), frame->GetHeight(), frame->GetPitch(), frame->GetBuffer()); unsigned int paddedWidth, paddedHeight; if (paddingX_ >= 2) { paddedWidth = OrthancWSI::CeilingDivision(source.GetWidth(), paddingX_) * paddingX_; } else { paddedWidth = source.GetWidth(); } if (paddingY_ >= 2) { paddedHeight = OrthancWSI::CeilingDivision(source.GetHeight(), paddingY_) * paddingY_; } else { paddedHeight = source.GetHeight(); } std::unique_ptr<Orthanc::ImageAccessor> rendered(new Orthanc::Image(Orthanc::PixelFormat_RGB24, paddedWidth, paddedHeight, false)); if (paddedWidth != source.GetWidth() || paddedHeight != source.GetHeight()) { Orthanc::ImageProcessing::Set(*rendered, backgroundRed_, backgroundGreen_, backgroundBlue_, 255 /* alpha */); } Orthanc::ImageAccessor region; rendered->GetRegion(region, 0, 0, source.GetWidth(), source.GetHeight()); switch (format) { case Orthanc::PixelFormat_RGB24: Orthanc::ImageProcessing::Copy(region, source); break; case Orthanc::PixelFormat_Grayscale8: Orthanc::ImageProcessing::Convert(region, source); break; case Orthanc::PixelFormat_Grayscale16: RenderGrayscale(region, source); break; default: throw Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented); } return new OnTheFlyPyramid(rendered.release(), tileWidth_, tileHeight_, smooth_); } }