Mercurial > hg > orthanc-stone
comparison Applications/Samples/SingleFrameEditorApplication.h @ 360:8262e4e9826d am-2
export to png
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Tue, 30 Oct 2018 10:47:13 +0100 |
parents | 100df90bf0ea |
children | f559ac66ef55 |
comparison
equal
deleted
inserted
replaced
359:100df90bf0ea | 360:8262e4e9826d |
---|---|
33 #include <Core/Images/PamReader.h> | 33 #include <Core/Images/PamReader.h> |
34 #include <Core/Logging.h> | 34 #include <Core/Logging.h> |
35 #include <Plugins/Samples/Common/DicomDatasetReader.h> | 35 #include <Plugins/Samples/Common/DicomDatasetReader.h> |
36 #include <Plugins/Samples/Common/FullOrthancDataset.h> | 36 #include <Plugins/Samples/Common/FullOrthancDataset.h> |
37 | 37 |
38 #include <Core/Images/PngWriter.h> | |
39 | |
38 | 40 |
39 #include <boost/math/constants/constants.hpp> | 41 #include <boost/math/constants/constants.hpp> |
40 | 42 |
41 namespace OrthancStone | 43 namespace OrthancStone |
42 { | 44 { |
2238 cairoBuffer_.reset(new CairoSurface(width, height)); | 2240 cairoBuffer_.reset(new CairoSurface(width, height)); |
2239 } | 2241 } |
2240 | 2242 |
2241 stack_.Render(*floatBuffer_, GetView(), interpolation); | 2243 stack_.Render(*floatBuffer_, GetView(), interpolation); |
2242 | 2244 |
2243 // Very similar to GrayscaleFrameRenderer => TODO MERGE? | 2245 // Conversion from Float32 to BGRA32 (cairo). Very similar to |
2246 // GrayscaleFrameRenderer => TODO MERGE? | |
2244 | 2247 |
2245 Orthanc::ImageAccessor target; | 2248 Orthanc::ImageAccessor target; |
2246 cairoBuffer_->GetAccessor(target); | 2249 cairoBuffer_->GetAccessor(target); |
2250 | |
2251 float scaling = 255.0f / (x1 - x0); | |
2247 | 2252 |
2248 for (unsigned int y = 0; y < height; y++) | 2253 for (unsigned int y = 0; y < height; y++) |
2249 { | 2254 { |
2250 const float* p = reinterpret_cast<const float*>(floatBuffer_->GetConstRow(y)); | 2255 const float* p = reinterpret_cast<const float*>(floatBuffer_->GetConstRow(y)); |
2251 uint8_t* q = reinterpret_cast<uint8_t*>(target.GetRow(y)); | 2256 uint8_t* q = reinterpret_cast<uint8_t*>(target.GetRow(y)); |
2262 v = 0; | 2267 v = 0; |
2263 } | 2268 } |
2264 else | 2269 else |
2265 { | 2270 { |
2266 // https://en.wikipedia.org/wiki/Linear_interpolation | 2271 // https://en.wikipedia.org/wiki/Linear_interpolation |
2267 v = static_cast<uint8_t>(255.0f * (*p - x0) / (x1 - x0)); // (*) | 2272 v = static_cast<uint8_t>(scaling * (*p - x0)); // (*) |
2268 } | 2273 } |
2269 | 2274 |
2270 if (invert_) | 2275 if (invert_) |
2271 { | 2276 { |
2272 v = 255 - v; | 2277 v = 255 - v; |
2596 case 'c': | 2601 case 'c': |
2597 tool_ = Tool_Crop; | 2602 tool_ = Tool_Crop; |
2598 break; | 2603 break; |
2599 | 2604 |
2600 case 'e': | 2605 case 'e': |
2601 Export(); | 2606 Export(GetStack(widget), 0.1, 0.1, GetWidget(widget).GetInterpolation()); |
2602 break; | 2607 break; |
2603 | 2608 |
2604 case 'i': | 2609 case 'i': |
2605 dynamic_cast<BitmapStackWidget&>(widget).SwitchInvert(); | 2610 GetWidget(widget).SwitchInvert(); |
2606 break; | 2611 break; |
2607 | 2612 |
2608 case 'm': | 2613 case 'm': |
2609 tool_ = Tool_Move; | 2614 tool_ = Tool_Move; |
2610 break; | 2615 break; |
2611 | 2616 |
2612 case 'n': | 2617 case 'n': |
2613 { | 2618 { |
2614 BitmapStackWidget& w = dynamic_cast<BitmapStackWidget&>(widget); | 2619 switch (GetWidget(widget).GetInterpolation()) |
2615 | |
2616 switch (w.GetInterpolation()) | |
2617 { | 2620 { |
2618 case ImageInterpolation_Nearest: | 2621 case ImageInterpolation_Nearest: |
2619 LOG(INFO) << "Switching to bilinear interpolation"; | 2622 LOG(INFO) << "Switching to bilinear interpolation"; |
2620 w.SetInterpolation(ImageInterpolation_Bilinear); | 2623 GetWidget(widget).SetInterpolation(ImageInterpolation_Bilinear); |
2621 break; | 2624 break; |
2622 | 2625 |
2623 case ImageInterpolation_Bilinear: | 2626 case ImageInterpolation_Bilinear: |
2624 LOG(INFO) << "Switching to nearest neighbor interpolation"; | 2627 LOG(INFO) << "Switching to nearest neighbor interpolation"; |
2625 w.SetInterpolation(ImageInterpolation_Nearest); | 2628 GetWidget(widget).SetInterpolation(ImageInterpolation_Nearest); |
2626 break; | 2629 break; |
2627 | 2630 |
2628 default: | 2631 default: |
2629 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); | 2632 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); |
2630 } | 2633 } |
2670 { | 2673 { |
2671 orthanc_ = &orthanc; | 2674 orthanc_ = &orthanc; |
2672 } | 2675 } |
2673 | 2676 |
2674 | 2677 |
2675 void Export() | 2678 void Export(const BitmapStack& stack, |
2676 { | 2679 double pixelSpacingX, |
2680 double pixelSpacingY, | |
2681 ImageInterpolation interpolation) | |
2682 { | |
2683 if (pixelSpacingX <= 0 || | |
2684 pixelSpacingY <= 0) | |
2685 { | |
2686 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange); | |
2687 } | |
2688 | |
2677 if (orthanc_ == NULL) | 2689 if (orthanc_ == NULL) |
2678 { | 2690 { |
2679 return; | 2691 return; |
2680 } | 2692 } |
2681 | 2693 |
2682 // TODO | |
2683 LOG(WARNING) << "Exporting DICOM"; | 2694 LOG(WARNING) << "Exporting DICOM"; |
2695 | |
2696 Extent2D extent = stack.GetSceneExtent(); | |
2697 | |
2698 int w = std::ceil(extent.GetWidth() / pixelSpacingX); | |
2699 int h = std::ceil(extent.GetHeight() / pixelSpacingY); | |
2700 | |
2701 if (w < 0 || h < 0) | |
2702 { | |
2703 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); | |
2704 } | |
2705 | |
2706 Orthanc::Image layers(Orthanc::PixelFormat_Float32, | |
2707 static_cast<unsigned int>(w), | |
2708 static_cast<unsigned int>(h), false); | |
2709 | |
2710 ViewportGeometry view; | |
2711 view.SetDisplaySize(layers.GetWidth(), layers.GetHeight()); | |
2712 view.SetSceneExtent(extent); | |
2713 view.FitContent(); | |
2714 | |
2715 stack.Render(layers, view, interpolation); | |
2716 | |
2717 Orthanc::Image rendered(Orthanc::PixelFormat_Grayscale16, | |
2718 layers.GetWidth(), layers.GetHeight(), false); | |
2719 Orthanc::ImageProcessing::Convert(rendered, layers); | |
2720 | |
2721 Orthanc::PngWriter png; | |
2722 png.WriteToFile("/tmp/a.png", rendered); | |
2684 } | 2723 } |
2685 }; | 2724 }; |
2686 | 2725 |
2687 | 2726 |
2688 | 2727 |