annotate Framework/Scene2D/Internals/CairoLookupTableTextureRenderer.cpp @ 925:4639d0bf6390

Added support for Grayscale16 in the multiframe loader (Victor)
author Benjamin Golinvaux <bgo@osimis.io>
date Mon, 22 Jul 2019 16:00:03 +0200
parents 6e888cf6a48b
children ba9db2ad317c 2d8ab34c8c91
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
768
55411e7da2f7 LookupTableTextureSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1 /**
55411e7da2f7 LookupTableTextureSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
2 * Stone of Orthanc
55411e7da2f7 LookupTableTextureSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
55411e7da2f7 LookupTableTextureSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
4 * Department, University Hospital of Liege, Belgium
55411e7da2f7 LookupTableTextureSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
5 * Copyright (C) 2017-2019 Osimis S.A., Belgium
55411e7da2f7 LookupTableTextureSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
6 *
55411e7da2f7 LookupTableTextureSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
7 * This program is free software: you can redistribute it and/or
55411e7da2f7 LookupTableTextureSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
8 * modify it under the terms of the GNU Affero General Public License
55411e7da2f7 LookupTableTextureSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
9 * as published by the Free Software Foundation, either version 3 of
55411e7da2f7 LookupTableTextureSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
10 * the License, or (at your option) any later version.
55411e7da2f7 LookupTableTextureSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
11 *
55411e7da2f7 LookupTableTextureSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
12 * This program is distributed in the hope that it will be useful, but
55411e7da2f7 LookupTableTextureSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
55411e7da2f7 LookupTableTextureSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
55411e7da2f7 LookupTableTextureSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
15 * Affero General Public License for more details.
55411e7da2f7 LookupTableTextureSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
16 *
55411e7da2f7 LookupTableTextureSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
17 * You should have received a copy of the GNU Affero General Public License
55411e7da2f7 LookupTableTextureSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
55411e7da2f7 LookupTableTextureSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
19 **/
55411e7da2f7 LookupTableTextureSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
20
55411e7da2f7 LookupTableTextureSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
21
55411e7da2f7 LookupTableTextureSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
22 #include "CairoLookupTableTextureRenderer.h"
55411e7da2f7 LookupTableTextureSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
23
55411e7da2f7 LookupTableTextureSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
24 #include "CairoColorTextureRenderer.h"
55411e7da2f7 LookupTableTextureSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
25 #include "../LookupTableTextureSceneLayer.h"
55411e7da2f7 LookupTableTextureSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
26
55411e7da2f7 LookupTableTextureSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
27 #include <Core/OrthancException.h>
55411e7da2f7 LookupTableTextureSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
28
55411e7da2f7 LookupTableTextureSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
29 namespace OrthancStone
55411e7da2f7 LookupTableTextureSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
30 {
55411e7da2f7 LookupTableTextureSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
31 namespace Internals
55411e7da2f7 LookupTableTextureSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
32 {
55411e7da2f7 LookupTableTextureSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
33 void CairoLookupTableTextureRenderer::Update(const ISceneLayer& layer)
55411e7da2f7 LookupTableTextureSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
34 {
55411e7da2f7 LookupTableTextureSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
35 const LookupTableTextureSceneLayer& l = dynamic_cast<const LookupTableTextureSceneLayer&>(layer);
55411e7da2f7 LookupTableTextureSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
36
55411e7da2f7 LookupTableTextureSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
37 textureTransform_ = l.GetTransform();
55411e7da2f7 LookupTableTextureSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
38 isLinearInterpolation_ = l.IsLinearInterpolation();
55411e7da2f7 LookupTableTextureSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
39
55411e7da2f7 LookupTableTextureSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
40 const float a = l.GetMinValue();
55411e7da2f7 LookupTableTextureSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
41 float slope;
55411e7da2f7 LookupTableTextureSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
42
55411e7da2f7 LookupTableTextureSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
43 if (l.GetMinValue() >= l.GetMaxValue())
55411e7da2f7 LookupTableTextureSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
44 {
55411e7da2f7 LookupTableTextureSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
45 slope = 0;
55411e7da2f7 LookupTableTextureSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
46 }
55411e7da2f7 LookupTableTextureSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
47 else
55411e7da2f7 LookupTableTextureSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
48 {
55411e7da2f7 LookupTableTextureSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
49 slope = 256.0f / (l.GetMaxValue() - l.GetMinValue());
55411e7da2f7 LookupTableTextureSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
50 }
55411e7da2f7 LookupTableTextureSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
51
55411e7da2f7 LookupTableTextureSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
52 const Orthanc::ImageAccessor& source = l.GetTexture();
55411e7da2f7 LookupTableTextureSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
53 const unsigned int width = source.GetWidth();
55411e7da2f7 LookupTableTextureSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
54 const unsigned int height = source.GetHeight();
55411e7da2f7 LookupTableTextureSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
55 texture_.SetSize(width, height, true /* alpha channel is enabled */);
55411e7da2f7 LookupTableTextureSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
56
55411e7da2f7 LookupTableTextureSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
57 Orthanc::ImageAccessor target;
55411e7da2f7 LookupTableTextureSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
58 texture_.GetWriteableAccessor(target);
55411e7da2f7 LookupTableTextureSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
59
55411e7da2f7 LookupTableTextureSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
60 const std::vector<uint8_t>& lut = l.GetLookupTable();
55411e7da2f7 LookupTableTextureSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
61 if (lut.size() != 4 * 256)
55411e7da2f7 LookupTableTextureSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
62 {
55411e7da2f7 LookupTableTextureSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
63 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError);
55411e7da2f7 LookupTableTextureSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
64 }
55411e7da2f7 LookupTableTextureSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
65
55411e7da2f7 LookupTableTextureSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
66 assert(source.GetFormat() == Orthanc::PixelFormat_Float32 &&
55411e7da2f7 LookupTableTextureSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
67 target.GetFormat() == Orthanc::PixelFormat_BGRA32 &&
55411e7da2f7 LookupTableTextureSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
68 sizeof(float) == 4);
55411e7da2f7 LookupTableTextureSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
69
55411e7da2f7 LookupTableTextureSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
70 for (unsigned int y = 0; y < height; y++)
55411e7da2f7 LookupTableTextureSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
71 {
55411e7da2f7 LookupTableTextureSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
72 const float* p = reinterpret_cast<const float*>(source.GetConstRow(y));
55411e7da2f7 LookupTableTextureSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
73 uint8_t* q = reinterpret_cast<uint8_t*>(target.GetRow(y));
55411e7da2f7 LookupTableTextureSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
74
55411e7da2f7 LookupTableTextureSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
75 for (unsigned int x = 0; x < width; x++)
55411e7da2f7 LookupTableTextureSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
76 {
55411e7da2f7 LookupTableTextureSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
77 float v = (*p - a) * slope;
55411e7da2f7 LookupTableTextureSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
78 if (v <= 0)
55411e7da2f7 LookupTableTextureSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
79 {
55411e7da2f7 LookupTableTextureSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
80 v = 0;
55411e7da2f7 LookupTableTextureSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
81 }
55411e7da2f7 LookupTableTextureSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
82 else if (v >= 255)
55411e7da2f7 LookupTableTextureSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
83 {
55411e7da2f7 LookupTableTextureSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
84 v = 255;
55411e7da2f7 LookupTableTextureSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
85 }
55411e7da2f7 LookupTableTextureSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
86
55411e7da2f7 LookupTableTextureSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
87 uint8_t vv = static_cast<uint8_t>(v);
55411e7da2f7 LookupTableTextureSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
88
55411e7da2f7 LookupTableTextureSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
89 q[0] = lut[4 * vv + 2]; // B
55411e7da2f7 LookupTableTextureSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
90 q[1] = lut[4 * vv + 1]; // G
55411e7da2f7 LookupTableTextureSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
91 q[2] = lut[4 * vv + 0]; // R
55411e7da2f7 LookupTableTextureSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
92 q[3] = lut[4 * vv + 3]; // A
55411e7da2f7 LookupTableTextureSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
93
55411e7da2f7 LookupTableTextureSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
94 p++;
55411e7da2f7 LookupTableTextureSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
95 q += 4;
55411e7da2f7 LookupTableTextureSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
96 }
55411e7da2f7 LookupTableTextureSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
97 }
55411e7da2f7 LookupTableTextureSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
98
55411e7da2f7 LookupTableTextureSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
99 cairo_surface_mark_dirty(texture_.GetObject());
55411e7da2f7 LookupTableTextureSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
100 }
55411e7da2f7 LookupTableTextureSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
101
888
6e888cf6a48b renderers now have access to canvas width/height
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 768
diff changeset
102 void CairoLookupTableTextureRenderer::Render(const AffineTransform2D& transform,
6e888cf6a48b renderers now have access to canvas width/height
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 768
diff changeset
103 unsigned int canvasWidth,
6e888cf6a48b renderers now have access to canvas width/height
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 768
diff changeset
104 unsigned int canvasHeight)
768
55411e7da2f7 LookupTableTextureSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
105 {
55411e7da2f7 LookupTableTextureSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
106 CairoColorTextureRenderer::RenderColorTexture(target_, transform, texture_,
55411e7da2f7 LookupTableTextureSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
107 textureTransform_, isLinearInterpolation_);
55411e7da2f7 LookupTableTextureSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
108 }
55411e7da2f7 LookupTableTextureSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
109 }
55411e7da2f7 LookupTableTextureSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
110 }