comparison Framework/Scene2D/Internals/OpenGLFloatTextureProgram.cpp @ 591:b66ced2c43d4

OpenGLTextureProgram
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 26 Apr 2019 12:05:38 +0200
parents
children bbe29efd3d1c
comparison
equal deleted inserted replaced
590:5430bcffba57 591:b66ced2c43d4
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 "OpenGLFloatTextureProgram.h"
23
24 #include <Core/OrthancException.h>
25 #include <Core/Images/Image.h>
26 #include <Core/Images/ImageProcessing.h>
27
28
29 static const char* FRAGMENT_SHADER =
30 "uniform float u_offset; \n"
31 "uniform float u_slope; \n"
32 "uniform float u_windowCenter; \n"
33 "uniform float u_windowWidth; \n"
34 "uniform sampler2D u_texture; \n"
35 "varying vec2 v_texcoord; \n"
36 "void main() \n"
37 "{ \n"
38 " vec4 t = texture2D(u_texture, v_texcoord); \n"
39 " float v = (t.r * 256.0 + t.g) * 256.0; \n"
40 " v = v * u_slope + u_offset; \n" // (*)
41 " float a = u_windowCenter - u_windowWidth; \n"
42 " float dy = 1.0 / (2.0 * u_windowWidth); \n"
43 " if (v <= a) \n"
44 " v = 0.0; \n"
45 " else \n"
46 " { \n"
47 " v = (v - a) * dy; \n"
48 " if (v >= 1.0) \n"
49 " v = 1.0; \n"
50 " } \n"
51 " gl_FragColor = vec4(v, v, v, 1); \n"
52 "}";
53
54
55 namespace OrthancStone
56 {
57 namespace Internals
58 {
59 OpenGLFloatTextureProgram::OpenGLFloatTextureProgram(OpenGL::IOpenGLContext& context) :
60 program_(context, FRAGMENT_SHADER)
61 {
62 }
63
64
65 OpenGLFloatTextureProgram::Data::Data(const Orthanc::ImageAccessor& texture,
66 bool isLinearInterpolation)
67 {
68 if (texture.GetFormat() != Orthanc::PixelFormat_Float32)
69 {
70 throw Orthanc::OrthancException(Orthanc::ErrorCode_IncompatibleImageFormat);
71 }
72
73 float minValue, maxValue;
74 Orthanc::ImageProcessing::GetMinMaxFloatValue(minValue, maxValue, texture);
75
76 offset_ = minValue;
77
78 if (LinearAlgebra::IsCloseToZero(maxValue - minValue))
79 {
80 slope_ = 1;
81 }
82 else
83 {
84 slope_ = (maxValue - minValue) / 65536.0f;
85 assert(!LinearAlgebra::IsCloseToZero(slope_));
86 }
87
88 const unsigned int width = texture.GetWidth();
89 const unsigned int height = texture.GetHeight();
90
91 Orthanc::Image converted(Orthanc::PixelFormat_RGB24, width, height, true);
92
93 for (unsigned int y = 0; y < height; y++)
94 {
95 const float *p = reinterpret_cast<const float*>(texture.GetConstRow(y));
96 uint8_t *q = reinterpret_cast<uint8_t*>(converted.GetRow(y));
97
98 for (unsigned int x = 0; x < width; x++)
99 {
100 /**
101 * At (*), the floating-point "value" is reconstructed as
102 * "value = texture * slope + offset".
103 * <=> texture = (value - offset) / slope
104 **/
105
106 float texture = (*p - offset_) / slope_;
107 if (texture < 0)
108 {
109 texture = 0;
110 }
111 else if (texture >= 65535.0f)
112 {
113 texture = 65535.0f;
114 }
115
116 uint16_t t = static_cast<uint16_t>(texture);
117
118 q[0] = t / 256; // red
119 q[1] = t % 256; // green
120 q[2] = 0; // blue is unused
121
122 p++;
123 q += 3;
124 }
125 }
126
127 texture_.Load(converted, isLinearInterpolation);
128 }
129
130
131 void OpenGLFloatTextureProgram::Apply(Data& data,
132 const AffineTransform2D& transform,
133 float windowCenter,
134 float windowWidth)
135 {
136 OpenGLTextureProgram::Execution execution(program_, data.GetTexture(), transform);
137
138 glUniform1f(execution.GetUniformLocation("u_slope"), data.GetSlope());
139 glUniform1f(execution.GetUniformLocation("u_offset"), data.GetOffset());
140 glUniform1f(execution.GetUniformLocation("u_windowCenter"), windowCenter);
141 glUniform1f(execution.GetUniformLocation("u_windowWidth"), windowWidth);
142
143 execution.DrawTriangles();
144 }
145 }
146 }