annotate Framework/Scene2D/Internals/CairoFloatTextureRenderer.cpp @ 920:5ca418d6579e refactor-viewport-controller

Close branch refactor-viewport-controller.
author Alain Mazy <am@osimis.io>
date Fri, 19 Jul 2019 14:15:49 +0000
parents 9e51fb773bbd
children 55411e7da2f7 cfb4d39065a4
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
597
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1 /**
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
2 * Stone of Orthanc
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
4 * Department, University Hospital of Liege, Belgium
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
5 * Copyright (C) 2017-2019 Osimis S.A., Belgium
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
6 *
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
7 * This program is free software: you can redistribute it and/or
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
8 * modify it under the terms of the GNU Affero General Public License
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
9 * as published by the Free Software Foundation, either version 3 of
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
10 * the License, or (at your option) any later version.
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
11 *
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
12 * This program is distributed in the hope that it will be useful, but
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
15 * Affero General Public License for more details.
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
16 *
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
17 * You should have received a copy of the GNU Affero General Public License
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
19 **/
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
20
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
21
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
22 #include "CairoFloatTextureRenderer.h"
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
23
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
24 #include "../FloatTextureSceneLayer.h"
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
25
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
26 namespace OrthancStone
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
27 {
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
28 namespace Internals
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
29 {
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
30 void CairoFloatTextureRenderer::Update(const ISceneLayer& layer)
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
31 {
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
32 const FloatTextureSceneLayer& l = dynamic_cast<const FloatTextureSceneLayer&>(layer);
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
33
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
34 textureTransform_ = l.GetTransform();
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
35 isLinearInterpolation_ = l.IsLinearInterpolation();
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
36
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
37 float windowCenter, windowWidth;
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
38 l.GetWindowing(windowCenter, windowWidth);
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
39
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
40 const float a = windowCenter - windowWidth;
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
41 const float slope = 256.0f / (2.0f * windowWidth);
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
42
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
43 const Orthanc::ImageAccessor& source = l.GetTexture();
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
44 const unsigned int width = source.GetWidth();
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
45 const unsigned int height = source.GetHeight();
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
46 texture_.SetSize(width, height, false);
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
47
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
48 Orthanc::ImageAccessor target;
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
49 texture_.GetWriteableAccessor(target);
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
50
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
51 assert(source.GetFormat() == Orthanc::PixelFormat_Float32 &&
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
52 target.GetFormat() == Orthanc::PixelFormat_BGRA32 &&
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
53 sizeof(float) == 4);
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
54
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
55 for (unsigned int y = 0; y < height; y++)
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
56 {
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
57 const float* p = reinterpret_cast<const float*>(source.GetConstRow(y));
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
58 uint8_t* q = reinterpret_cast<uint8_t*>(target.GetRow(y));
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
59
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
60 for (unsigned int x = 0; x < width; x++)
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
61 {
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
62 float v = (*p - a) * slope;
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
63 if (v <= 0)
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
64 {
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
65 v = 0;
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
66 }
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
67 else if (v >= 255)
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
68 {
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
69 v = 255;
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
70 }
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
71
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
72 uint8_t vv = static_cast<uint8_t>(v);
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
73
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
74 q[0] = vv;
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
75 q[1] = vv;
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
76 q[2] = vv;
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
77
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
78 p++;
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
79 q += 4;
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
80 }
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
81 }
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
82 }
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
83
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
84
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
85 void CairoFloatTextureRenderer::Render(const AffineTransform2D& transform)
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
86 {
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
87 cairo_t* cr = target_.GetCairoContext();
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
88
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
89 AffineTransform2D t =
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
90 AffineTransform2D::Combine(transform, textureTransform_);
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
91 Matrix h = t.GetHomogeneousMatrix();
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
92
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
93 cairo_save(cr);
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
94
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
95 cairo_matrix_t m;
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
96 cairo_matrix_init(&m, h(0, 0), h(1, 0), h(0, 1), h(1, 1), h(0, 2), h(1, 2));
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
97 cairo_transform(cr, &m);
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
98
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
99 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
100 cairo_set_source_surface(cr, texture_.GetObject(), 0, 0);
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
101
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
102 if (isLinearInterpolation_)
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
103 {
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
104 cairo_pattern_set_filter(cairo_get_source(cr), CAIRO_FILTER_BILINEAR);
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
105 }
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
106 else
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
107 {
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
108 cairo_pattern_set_filter(cairo_get_source(cr), CAIRO_FILTER_NEAREST);
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
109 }
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
110
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
111 cairo_paint(cr);
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
112
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
113 cairo_restore(cr);
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
114 }
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
115 }
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
116 }