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