Mercurial > hg > orthanc-stone
annotate OrthancStone/Sources/Scene2D/Internals/OpenGLFloatTextureProgram.cpp @ 2177:4d21befb1501 default tip
clarify DICOMweb version check
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Wed, 23 Oct 2024 19:27:56 +0200 |
parents | 16c01cc201e7 |
children |
rev | line source |
---|---|
591 | 1 /** |
2 * Stone of Orthanc | |
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics | |
4 * Department, University Hospital of Liege, Belgium | |
2124
16c01cc201e7
updated copyright, as Osimis is not active on Orthanc anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2114
diff
changeset
|
5 * Copyright (C) 2017-2023 Osimis S.A., Belgium |
2114
c23eef785569
update year to 2024
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2077
diff
changeset
|
6 * Copyright (C) 2021-2024 Sebastien Jodogne, ICTEAM UCLouvain, Belgium |
591 | 7 * |
8 * This program is free software: you can redistribute it and/or | |
1598
8563ea5d8ae4
relicensing some files, cf. osimis bm26 and chu agreement on 2020-05-20
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1596
diff
changeset
|
9 * modify it under the terms of the GNU Lesser General Public License |
591 | 10 * as published by the Free Software Foundation, either version 3 of |
11 * the License, or (at your option) any later version. | |
12 * | |
13 * This program is distributed in the hope that it will be useful, but | |
14 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
1598
8563ea5d8ae4
relicensing some files, cf. osimis bm26 and chu agreement on 2020-05-20
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1596
diff
changeset
|
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
8563ea5d8ae4
relicensing some files, cf. osimis bm26 and chu agreement on 2020-05-20
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1596
diff
changeset
|
16 * Lesser General Public License for more details. |
1596
4fb8fdf03314
removed annoying whitespace
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1576
diff
changeset
|
17 * |
1598
8563ea5d8ae4
relicensing some files, cf. osimis bm26 and chu agreement on 2020-05-20
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1596
diff
changeset
|
18 * You should have received a copy of the GNU Lesser General Public |
8563ea5d8ae4
relicensing some files, cf. osimis bm26 and chu agreement on 2020-05-20
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1596
diff
changeset
|
19 * License along with this program. If not, see |
8563ea5d8ae4
relicensing some files, cf. osimis bm26 and chu agreement on 2020-05-20
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1596
diff
changeset
|
20 * <http://www.gnu.org/licenses/>. |
591 | 21 **/ |
22 | |
23 | |
24 #include "OpenGLFloatTextureProgram.h" | |
611
e3f21a265be5
Added version directive to GLSL shader code + glew init function in sample code
Benjamin Golinvaux <bgo@osimis.io>
parents:
592
diff
changeset
|
25 #include "OpenGLShaderVersionDirective.h" |
591 | 26 |
1455
30deba7bc8e2
simplifying include_directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1270
diff
changeset
|
27 #include <OrthancException.h> |
30deba7bc8e2
simplifying include_directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1270
diff
changeset
|
28 #include <Images/Image.h> |
30deba7bc8e2
simplifying include_directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1270
diff
changeset
|
29 #include <Images/ImageProcessing.h> |
591 | 30 |
31 | |
32 static const char* FRAGMENT_SHADER = | |
611
e3f21a265be5
Added version directive to GLSL shader code + glew init function in sample code
Benjamin Golinvaux <bgo@osimis.io>
parents:
592
diff
changeset
|
33 ORTHANC_STONE_OPENGL_SHADER_VERSION_DIRECTIVE |
771 | 34 "uniform float u_offset; \n" |
35 "uniform float u_slope; \n" | |
36 "uniform float u_windowCenter; \n" | |
37 "uniform float u_windowWidth; \n" | |
914
4d1f57773b5b
Added image inversion support in GrayscaleStyleConfigurator + OpenGLFloatTextureProgram
Benjamin Golinvaux <bgo@osimis.io>
parents:
771
diff
changeset
|
38 "uniform bool u_invert; \n" |
771 | 39 "uniform sampler2D u_texture; \n" |
40 "varying vec2 v_texcoord; \n" | |
41 "void main() \n" | |
42 "{ \n" | |
43 " vec4 t = texture2D(u_texture, v_texcoord); \n" | |
1969
ee68ee732aa7
fix OpenGL shader for floating-point textures
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1871
diff
changeset
|
44 " float v = (t.r * 256.0 + t.g) * 255.0; \n" |
771 | 45 " v = v * u_slope + u_offset; \n" // (*) |
46 " float a = u_windowCenter - u_windowWidth / 2.0; \n" | |
47 " float dy = 1.0 / u_windowWidth; \n" | |
48 " if (v <= a) \n" | |
49 " v = 0.0; \n" | |
50 " else \n" | |
51 " { \n" | |
52 " v = (v - a) * dy; \n" | |
53 " if (v >= 1.0) \n" | |
54 " v = 1.0; \n" | |
55 " } \n" | |
914
4d1f57773b5b
Added image inversion support in GrayscaleStyleConfigurator + OpenGLFloatTextureProgram
Benjamin Golinvaux <bgo@osimis.io>
parents:
771
diff
changeset
|
56 " if (u_invert) \n" |
4d1f57773b5b
Added image inversion support in GrayscaleStyleConfigurator + OpenGLFloatTextureProgram
Benjamin Golinvaux <bgo@osimis.io>
parents:
771
diff
changeset
|
57 " v = 1.0 - v; \n" |
771 | 58 " gl_FragColor = vec4(v, v, v, 1); \n" |
591 | 59 "}"; |
60 | |
61 | |
62 namespace OrthancStone | |
63 { | |
64 namespace Internals | |
65 { | |
947
1091b2adeb5a
Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents:
914
diff
changeset
|
66 OpenGLFloatTextureProgram::Data::Data( |
1571 | 67 OpenGL::IOpenGLContext& context, |
68 const Orthanc::ImageAccessor& texture, | |
69 bool isLinearInterpolation) : | |
70 texture_(context), | |
71 offset_(0.0f), | |
72 slope_(0.0f) | |
591 | 73 { |
74 if (texture.GetFormat() != Orthanc::PixelFormat_Float32) | |
75 { | |
76 throw Orthanc::OrthancException(Orthanc::ErrorCode_IncompatibleImageFormat); | |
77 } | |
78 | |
79 float minValue, maxValue; | |
80 Orthanc::ImageProcessing::GetMinMaxFloatValue(minValue, maxValue, texture); | |
81 | |
82 offset_ = minValue; | |
83 | |
84 if (LinearAlgebra::IsCloseToZero(maxValue - minValue)) | |
85 { | |
86 slope_ = 1; | |
87 } | |
88 else | |
89 { | |
90 slope_ = (maxValue - minValue) / 65536.0f; | |
91 assert(!LinearAlgebra::IsCloseToZero(slope_)); | |
92 } | |
93 | |
94 const unsigned int width = texture.GetWidth(); | |
95 const unsigned int height = texture.GetHeight(); | |
96 | |
97 Orthanc::Image converted(Orthanc::PixelFormat_RGB24, width, height, true); | |
98 | |
99 for (unsigned int y = 0; y < height; y++) | |
100 { | |
101 const float *p = reinterpret_cast<const float*>(texture.GetConstRow(y)); | |
102 uint8_t *q = reinterpret_cast<uint8_t*>(converted.GetRow(y)); | |
103 | |
104 for (unsigned int x = 0; x < width; x++) | |
105 { | |
106 /** | |
107 * At (*), the floating-point "value" is reconstructed as | |
108 * "value = texture * slope + offset". | |
109 * <=> texture = (value - offset) / slope | |
110 **/ | |
111 | |
1571 | 112 float value = (*p - offset_) / slope_; |
113 if (value < 0) | |
591 | 114 { |
1571 | 115 value = 0; |
591 | 116 } |
1571 | 117 else if (value >= 65535.0f) |
591 | 118 { |
1571 | 119 value = 65535.0f; |
591 | 120 } |
121 | |
1571 | 122 uint16_t t = static_cast<uint16_t>(value); |
591 | 123 |
124 q[0] = t / 256; // red | |
125 q[1] = t % 256; // green | |
126 q[2] = 0; // blue is unused | |
127 | |
128 p++; | |
129 q += 3; | |
130 } | |
131 } | |
132 | |
133 texture_.Load(converted, isLinearInterpolation); | |
134 } | |
135 | |
136 | |
947
1091b2adeb5a
Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents:
914
diff
changeset
|
137 OpenGLFloatTextureProgram::OpenGLFloatTextureProgram(OpenGL::IOpenGLContext& context) |
1091b2adeb5a
Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents:
914
diff
changeset
|
138 : program_(context, FRAGMENT_SHADER) |
1091b2adeb5a
Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents:
914
diff
changeset
|
139 , context_(context) |
592 | 140 { |
141 } | |
142 | |
143 | |
591 | 144 void OpenGLFloatTextureProgram::Apply(Data& data, |
145 const AffineTransform2D& transform, | |
1576
92fca2b3ba3d
sanitizing the handling of canvas size
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1571
diff
changeset
|
146 unsigned int canvasWidth, |
92fca2b3ba3d
sanitizing the handling of canvas size
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1571
diff
changeset
|
147 unsigned int canvasHeight, |
591 | 148 float windowCenter, |
914
4d1f57773b5b
Added image inversion support in GrayscaleStyleConfigurator + OpenGLFloatTextureProgram
Benjamin Golinvaux <bgo@osimis.io>
parents:
771
diff
changeset
|
149 float windowWidth, |
4d1f57773b5b
Added image inversion support in GrayscaleStyleConfigurator + OpenGLFloatTextureProgram
Benjamin Golinvaux <bgo@osimis.io>
parents:
771
diff
changeset
|
150 bool invert) |
591 | 151 { |
947
1091b2adeb5a
Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents:
914
diff
changeset
|
152 if (!context_.IsContextLost()) |
1091b2adeb5a
Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents:
914
diff
changeset
|
153 { |
1576
92fca2b3ba3d
sanitizing the handling of canvas size
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1571
diff
changeset
|
154 OpenGLTextureProgram::Execution execution( |
92fca2b3ba3d
sanitizing the handling of canvas size
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1571
diff
changeset
|
155 program_, data.GetTexture(), transform, canvasWidth, canvasHeight); |
591 | 156 |
947
1091b2adeb5a
Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents:
914
diff
changeset
|
157 glUniform1f(execution.GetUniformLocation("u_slope"), data.GetSlope()); |
1091b2adeb5a
Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents:
914
diff
changeset
|
158 glUniform1f(execution.GetUniformLocation("u_offset"), data.GetOffset()); |
1091b2adeb5a
Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents:
914
diff
changeset
|
159 glUniform1f(execution.GetUniformLocation("u_windowCenter"), windowCenter); |
1091b2adeb5a
Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents:
914
diff
changeset
|
160 glUniform1f(execution.GetUniformLocation("u_windowWidth"), windowWidth); |
1091b2adeb5a
Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents:
914
diff
changeset
|
161 glUniform1f(execution.GetUniformLocation("u_invert"), invert); |
591 | 162 |
947
1091b2adeb5a
Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents:
914
diff
changeset
|
163 execution.DrawTriangles(); |
1091b2adeb5a
Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents:
914
diff
changeset
|
164 } |
591 | 165 } |
166 } | |
167 } |