Mercurial > hg > orthanc-stone
annotate Framework/Layers/GrayscaleFrameRenderer.cpp @ 293:017044be141b am-2
renaming
author | am@osimis.io |
---|---|
date | Thu, 30 Aug 2018 17:15:22 +0200 |
parents | 5412adf19980 |
children | ce48c3b3b0e9 3a4ca166fafa |
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 | |
134
4cff7b1ed31d
upgrade to year 2018
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
47
diff
changeset
|
5 * Copyright (C) 2017-2018 Osimis S.A., 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 | |
212
5412adf19980
resort to OrthancFramework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
201
diff
changeset
|
24 #include <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 } | |
106 | 54 |
0 | 55 Orthanc::ImageAccessor target = result->GetAccessor(); |
106 | 56 const unsigned int width = target.GetWidth(); |
57 const unsigned int height = target.GetHeight(); | |
58 | |
59 for (unsigned int y = 0; y < height; y++) | |
0 | 60 { |
61 const float* p = reinterpret_cast<const float*>(frame_->GetConstRow(y)); | |
62 uint8_t* q = reinterpret_cast<uint8_t*>(target.GetRow(y)); | |
63 | |
106 | 64 for (unsigned int x = 0; x < width; x++, p++, q += 4) |
0 | 65 { |
66 uint8_t v = 0; | |
67 if (windowWidth >= 0.001f) // Avoid division by zero | |
68 { | |
69 if (*p >= x1) | |
70 { | |
71 v = 255; | |
72 } | |
73 else if (*p <= x0) | |
74 { | |
75 v = 0; | |
76 } | |
77 else | |
78 { | |
79 // https://en.wikipedia.org/wiki/Linear_interpolation | |
80 v = static_cast<uint8_t>(255.0f * (*p - x0) / (x1 - x0)); | |
81 } | |
82 | |
83 if (style.reverse_) | |
84 { | |
85 v = 255 - v; | |
86 } | |
87 } | |
88 | |
89 if (style.applyLut_) | |
90 { | |
91 assert(lut != NULL); | |
92 q[3] = 255; | |
93 q[2] = lut[3 * v]; | |
94 q[1] = lut[3 * v + 1]; | |
95 q[0] = lut[3 * v + 2]; | |
96 } | |
97 else | |
98 { | |
99 q[3] = 255; | |
100 q[2] = v; | |
101 q[1] = v; | |
102 q[0] = v; | |
103 } | |
104 } | |
105 } | |
106 | |
107 return result.release(); | |
108 } | |
109 | |
110 | |
111 GrayscaleFrameRenderer::GrayscaleFrameRenderer(Orthanc::ImageAccessor* frame, | |
112 const DicomFrameConverter& converter, | |
110
53025eecbc95
renamed SliceGeometry as CoordinateSystem3D
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
106
diff
changeset
|
113 const CoordinateSystem3D& frameSlice, |
0 | 114 double pixelSpacingX, |
115 double pixelSpacingY, | |
116 bool isFullQuality) : | |
77 | 117 FrameRenderer(frameSlice, pixelSpacingX, pixelSpacingY, isFullQuality), |
0 | 118 frame_(frame), |
119 defaultWindowCenter_(converter.GetDefaultWindowCenter()), | |
120 defaultWindowWidth_(converter.GetDefaultWindowWidth()) | |
121 { | |
122 if (frame == NULL) | |
123 { | |
124 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange); | |
125 } | |
126 | |
127 converter.ConvertFrame(frame_); | |
128 assert(frame_.get() != NULL); | |
129 | |
130 if (frame_->GetFormat() != Orthanc::PixelFormat_Float32) | |
131 { | |
132 throw Orthanc::OrthancException(Orthanc::ErrorCode_IncompatibleImageFormat); | |
133 } | |
134 } | |
135 } |