597
|
1 /**
|
|
2 * Stone of Orthanc
|
|
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
|
|
4 * Department, University Hospital of Liege, Belgium
|
|
5 * Copyright (C) 2017-2019 Osimis S.A., Belgium
|
|
6 *
|
|
7 * This program is free software: you can redistribute it and/or
|
|
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.
|
|
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
|
|
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
|
|
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
19 **/
|
|
20
|
|
21
|
|
22 #include "CairoFloatTextureRenderer.h"
|
|
23
|
|
24 #include "../FloatTextureSceneLayer.h"
|
|
25
|
|
26 namespace OrthancStone
|
|
27 {
|
|
28 namespace Internals
|
|
29 {
|
|
30 void CairoFloatTextureRenderer::Update(const ISceneLayer& layer)
|
|
31 {
|
|
32 const FloatTextureSceneLayer& l = dynamic_cast<const FloatTextureSceneLayer&>(layer);
|
|
33
|
|
34 textureTransform_ = l.GetTransform();
|
|
35 isLinearInterpolation_ = l.IsLinearInterpolation();
|
|
36
|
|
37 float windowCenter, windowWidth;
|
|
38 l.GetWindowing(windowCenter, windowWidth);
|
|
39
|
|
40 const float a = windowCenter - windowWidth;
|
|
41 const float slope = 256.0f / (2.0f * windowWidth);
|
|
42
|
|
43 const Orthanc::ImageAccessor& source = l.GetTexture();
|
|
44 const unsigned int width = source.GetWidth();
|
|
45 const unsigned int height = source.GetHeight();
|
|
46 texture_.SetSize(width, height, false);
|
|
47
|
|
48 Orthanc::ImageAccessor target;
|
|
49 texture_.GetWriteableAccessor(target);
|
|
50
|
|
51 assert(source.GetFormat() == Orthanc::PixelFormat_Float32 &&
|
|
52 target.GetFormat() == Orthanc::PixelFormat_BGRA32 &&
|
|
53 sizeof(float) == 4);
|
|
54
|
|
55 for (unsigned int y = 0; y < height; y++)
|
|
56 {
|
|
57 const float* p = reinterpret_cast<const float*>(source.GetConstRow(y));
|
|
58 uint8_t* q = reinterpret_cast<uint8_t*>(target.GetRow(y));
|
|
59
|
|
60 for (unsigned int x = 0; x < width; x++)
|
|
61 {
|
|
62 float v = (*p - a) * slope;
|
|
63 if (v <= 0)
|
|
64 {
|
|
65 v = 0;
|
|
66 }
|
|
67 else if (v >= 255)
|
|
68 {
|
|
69 v = 255;
|
|
70 }
|
|
71
|
|
72 uint8_t vv = static_cast<uint8_t>(v);
|
|
73
|
|
74 q[0] = vv;
|
|
75 q[1] = vv;
|
|
76 q[2] = vv;
|
|
77
|
|
78 p++;
|
|
79 q += 4;
|
|
80 }
|
|
81 }
|
|
82 }
|
|
83
|
|
84
|
|
85 void CairoFloatTextureRenderer::Render(const AffineTransform2D& transform)
|
|
86 {
|
|
87 cairo_t* cr = target_.GetCairoContext();
|
|
88
|
|
89 AffineTransform2D t =
|
|
90 AffineTransform2D::Combine(transform, textureTransform_);
|
|
91 Matrix h = t.GetHomogeneousMatrix();
|
|
92
|
|
93 cairo_save(cr);
|
|
94
|
|
95 cairo_matrix_t m;
|
|
96 cairo_matrix_init(&m, h(0, 0), h(1, 0), h(0, 1), h(1, 1), h(0, 2), h(1, 2));
|
|
97 cairo_transform(cr, &m);
|
|
98
|
|
99 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
|
|
100 cairo_set_source_surface(cr, texture_.GetObject(), 0, 0);
|
|
101
|
|
102 if (isLinearInterpolation_)
|
|
103 {
|
|
104 cairo_pattern_set_filter(cairo_get_source(cr), CAIRO_FILTER_BILINEAR);
|
|
105 }
|
|
106 else
|
|
107 {
|
|
108 cairo_pattern_set_filter(cairo_get_source(cr), CAIRO_FILTER_NEAREST);
|
|
109 }
|
|
110
|
|
111 cairo_paint(cr);
|
|
112
|
|
113 cairo_restore(cr);
|
|
114 }
|
|
115 }
|
|
116 }
|