Mercurial > hg > orthanc-stone
annotate Framework/Layers/GrayscaleFrameRenderer.cpp @ 47:28956ed68280
agpl license
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Thu, 27 Apr 2017 11:00:15 +0200 |
parents | 7207a407bcd8 |
children | f5f54ed8d307 4cff7b1ed31d |
rev | line source |
---|---|
0 | 1 /** |
2 * Stone of Orthanc | |
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics | |
4 * Department, University Hospital of Liege, Belgium | |
40
7207a407bcd8
shared copyright with osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
16
diff
changeset
|
5 * Copyright (C) 2017 Osimis, Belgium |
0 | 6 * |
7 * This program is free software: you can redistribute it and/or | |
47 | 8 * modify it under the terms of the GNU Affero General Public License |
9 * as published by the Free Software Foundation, either version 3 of | |
10 * the License, or (at your option) any later version. | |
0 | 11 * |
12 * This program is distributed in the hope that it will be useful, but | |
13 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
47 | 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
15 * Affero General Public License for more details. | |
16 * | |
17 * You should have received a copy of the GNU Affero General Public License | |
0 | 18 * along with this program. If not, see <http://www.gnu.org/licenses/>. |
19 **/ | |
20 | |
21 | |
22 #include "GrayscaleFrameRenderer.h" | |
23 | |
16 | 24 #include "../../Resources/Orthanc/Core/OrthancException.h" |
0 | 25 |
26 namespace OrthancStone | |
27 { | |
28 CairoSurface* GrayscaleFrameRenderer::GenerateDisplay(const RenderStyle& style) | |
29 { | |
30 std::auto_ptr<CairoSurface> result; | |
31 | |
32 float windowCenter, windowWidth; | |
33 style.ComputeWindowing(windowCenter, windowWidth, | |
34 defaultWindowCenter_, defaultWindowWidth_); | |
35 | |
36 float x0 = windowCenter - windowWidth / 2.0f; | |
37 float x1 = windowCenter + windowWidth / 2.0f; | |
38 | |
39 //LOG(INFO) << "Window: " << x0 << " => " << x1; | |
40 | |
41 result.reset(new CairoSurface(frame_->GetWidth(), frame_->GetHeight())); | |
42 | |
43 const uint8_t* lut = NULL; | |
44 if (style.applyLut_) | |
45 { | |
46 if (Orthanc::EmbeddedResources::GetFileResourceSize(style.lut_) != 3 * 256) | |
47 { | |
48 // Invalid colormap | |
49 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); | |
50 } | |
51 | |
52 lut = reinterpret_cast<const uint8_t*>(Orthanc::EmbeddedResources::GetFileResourceBuffer(style.lut_)); | |
53 } | |
54 | |
55 Orthanc::ImageAccessor target = result->GetAccessor(); | |
56 for (unsigned int y = 0; y < target.GetHeight(); y++) | |
57 { | |
58 const float* p = reinterpret_cast<const float*>(frame_->GetConstRow(y)); | |
59 uint8_t* q = reinterpret_cast<uint8_t*>(target.GetRow(y)); | |
60 | |
61 for (unsigned int x = 0; x < target.GetWidth(); x++, p++, q += 4) | |
62 { | |
63 uint8_t v = 0; | |
64 if (windowWidth >= 0.001f) // Avoid division by zero | |
65 { | |
66 if (*p >= x1) | |
67 { | |
68 v = 255; | |
69 } | |
70 else if (*p <= x0) | |
71 { | |
72 v = 0; | |
73 } | |
74 else | |
75 { | |
76 // https://en.wikipedia.org/wiki/Linear_interpolation | |
77 v = static_cast<uint8_t>(255.0f * (*p - x0) / (x1 - x0)); | |
78 } | |
79 | |
80 if (style.reverse_) | |
81 { | |
82 v = 255 - v; | |
83 } | |
84 } | |
85 | |
86 if (style.applyLut_) | |
87 { | |
88 assert(lut != NULL); | |
89 q[3] = 255; | |
90 q[2] = lut[3 * v]; | |
91 q[1] = lut[3 * v + 1]; | |
92 q[0] = lut[3 * v + 2]; | |
93 } | |
94 else | |
95 { | |
96 q[3] = 255; | |
97 q[2] = v; | |
98 q[1] = v; | |
99 q[0] = v; | |
100 } | |
101 } | |
102 } | |
103 | |
104 return result.release(); | |
105 } | |
106 | |
107 | |
108 GrayscaleFrameRenderer::GrayscaleFrameRenderer(Orthanc::ImageAccessor* frame, | |
109 const DicomFrameConverter& converter, | |
110 const SliceGeometry& viewportSlice, | |
111 const SliceGeometry& frameSlice, | |
112 double pixelSpacingX, | |
113 double pixelSpacingY, | |
114 bool isFullQuality) : | |
115 FrameRenderer(viewportSlice, frameSlice, pixelSpacingX, pixelSpacingY, isFullQuality), | |
116 frame_(frame), | |
117 defaultWindowCenter_(converter.GetDefaultWindowCenter()), | |
118 defaultWindowWidth_(converter.GetDefaultWindowWidth()) | |
119 { | |
120 if (frame == NULL) | |
121 { | |
122 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange); | |
123 } | |
124 | |
125 converter.ConvertFrame(frame_); | |
126 assert(frame_.get() != NULL); | |
127 | |
128 if (frame_->GetFormat() != Orthanc::PixelFormat_Float32) | |
129 { | |
130 throw Orthanc::OrthancException(Orthanc::ErrorCode_IncompatibleImageFormat); | |
131 } | |
132 } | |
133 } |