Mercurial > hg > orthanc-stone
annotate OrthancStone/Sources/Scene2D/Internals/OpenGLFloatTextureProgram.cpp @ 1835:6c252e51008d
no more need of "-DORTHANC_FRAMEWORK_ADDITIONAL_LIBRARIES" to dynamically link against system-wide orthanc framework
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Sat, 12 Jun 2021 09:01:40 +0200 |
parents | 9ac2a65d4172 |
children | 3889ae96d2e9 |
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 | |
1739
9ac2a65d4172
upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1598
diff
changeset
|
5 * Copyright (C) 2017-2021 Osimis S.A., Belgium |
591 | 6 * |
7 * 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
|
8 * modify it under the terms of the GNU Lesser General Public License |
591 | 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 | |
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
|
14 * 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
|
15 * Lesser General Public License for more details. |
1596
4fb8fdf03314
removed annoying whitespace
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1576
diff
changeset
|
16 * |
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
|
17 * 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
|
18 * 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
|
19 * <http://www.gnu.org/licenses/>. |
591 | 20 **/ |
21 | |
22 | |
23 #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
|
24 #include "OpenGLShaderVersionDirective.h" |
591 | 25 |
1455
30deba7bc8e2
simplifying include_directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1270
diff
changeset
|
26 #include <OrthancException.h> |
30deba7bc8e2
simplifying include_directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1270
diff
changeset
|
27 #include <Images/Image.h> |
30deba7bc8e2
simplifying include_directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1270
diff
changeset
|
28 #include <Images/ImageProcessing.h> |
591 | 29 |
30 | |
31 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
|
32 ORTHANC_STONE_OPENGL_SHADER_VERSION_DIRECTIVE |
771 | 33 "uniform float u_offset; \n" |
34 "uniform float u_slope; \n" | |
35 "uniform float u_windowCenter; \n" | |
36 "uniform float u_windowWidth; \n" | |
914
4d1f57773b5b
Added image inversion support in GrayscaleStyleConfigurator + OpenGLFloatTextureProgram
Benjamin Golinvaux <bgo@osimis.io>
parents:
771
diff
changeset
|
37 "uniform bool u_invert; \n" |
771 | 38 "uniform sampler2D u_texture; \n" |
39 "varying vec2 v_texcoord; \n" | |
40 "void main() \n" | |
41 "{ \n" | |
42 " vec4 t = texture2D(u_texture, v_texcoord); \n" | |
43 " float v = (t.r * 256.0 + t.g) * 256.0; \n" | |
44 " v = v * u_slope + u_offset; \n" // (*) | |
45 " float a = u_windowCenter - u_windowWidth / 2.0; \n" | |
46 " float dy = 1.0 / u_windowWidth; \n" | |
47 " if (v <= a) \n" | |
48 " v = 0.0; \n" | |
49 " else \n" | |
50 " { \n" | |
51 " v = (v - a) * dy; \n" | |
52 " if (v >= 1.0) \n" | |
53 " v = 1.0; \n" | |
54 " } \n" | |
914
4d1f57773b5b
Added image inversion support in GrayscaleStyleConfigurator + OpenGLFloatTextureProgram
Benjamin Golinvaux <bgo@osimis.io>
parents:
771
diff
changeset
|
55 " if (u_invert) \n" |
4d1f57773b5b
Added image inversion support in GrayscaleStyleConfigurator + OpenGLFloatTextureProgram
Benjamin Golinvaux <bgo@osimis.io>
parents:
771
diff
changeset
|
56 " v = 1.0 - v; \n" |
771 | 57 " gl_FragColor = vec4(v, v, v, 1); \n" |
591 | 58 "}"; |
59 | |
60 | |
61 namespace OrthancStone | |
62 { | |
63 namespace Internals | |
64 { | |
947
1091b2adeb5a
Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents:
914
diff
changeset
|
65 OpenGLFloatTextureProgram::Data::Data( |
1571 | 66 OpenGL::IOpenGLContext& context, |
67 const Orthanc::ImageAccessor& texture, | |
68 bool isLinearInterpolation) : | |
69 texture_(context), | |
70 offset_(0.0f), | |
71 slope_(0.0f) | |
591 | 72 { |
73 if (texture.GetFormat() != Orthanc::PixelFormat_Float32) | |
74 { | |
75 throw Orthanc::OrthancException(Orthanc::ErrorCode_IncompatibleImageFormat); | |
76 } | |
77 | |
78 float minValue, maxValue; | |
79 Orthanc::ImageProcessing::GetMinMaxFloatValue(minValue, maxValue, texture); | |
80 | |
81 offset_ = minValue; | |
82 | |
83 if (LinearAlgebra::IsCloseToZero(maxValue - minValue)) | |
84 { | |
85 slope_ = 1; | |
86 } | |
87 else | |
88 { | |
89 slope_ = (maxValue - minValue) / 65536.0f; | |
90 assert(!LinearAlgebra::IsCloseToZero(slope_)); | |
91 } | |
92 | |
93 const unsigned int width = texture.GetWidth(); | |
94 const unsigned int height = texture.GetHeight(); | |
95 | |
96 Orthanc::Image converted(Orthanc::PixelFormat_RGB24, width, height, true); | |
97 | |
98 for (unsigned int y = 0; y < height; y++) | |
99 { | |
100 const float *p = reinterpret_cast<const float*>(texture.GetConstRow(y)); | |
101 uint8_t *q = reinterpret_cast<uint8_t*>(converted.GetRow(y)); | |
102 | |
103 for (unsigned int x = 0; x < width; x++) | |
104 { | |
105 /** | |
106 * At (*), the floating-point "value" is reconstructed as | |
107 * "value = texture * slope + offset". | |
108 * <=> texture = (value - offset) / slope | |
109 **/ | |
110 | |
1571 | 111 float value = (*p - offset_) / slope_; |
112 if (value < 0) | |
591 | 113 { |
1571 | 114 value = 0; |
591 | 115 } |
1571 | 116 else if (value >= 65535.0f) |
591 | 117 { |
1571 | 118 value = 65535.0f; |
591 | 119 } |
120 | |
1571 | 121 uint16_t t = static_cast<uint16_t>(value); |
591 | 122 |
123 q[0] = t / 256; // red | |
124 q[1] = t % 256; // green | |
125 q[2] = 0; // blue is unused | |
126 | |
127 p++; | |
128 q += 3; | |
129 } | |
130 } | |
131 | |
132 texture_.Load(converted, isLinearInterpolation); | |
133 } | |
134 | |
135 | |
947
1091b2adeb5a
Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents:
914
diff
changeset
|
136 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
|
137 : 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
|
138 , context_(context) |
592 | 139 { |
140 } | |
141 | |
142 | |
591 | 143 void OpenGLFloatTextureProgram::Apply(Data& data, |
144 const AffineTransform2D& transform, | |
1576
92fca2b3ba3d
sanitizing the handling of canvas size
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1571
diff
changeset
|
145 unsigned int canvasWidth, |
92fca2b3ba3d
sanitizing the handling of canvas size
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1571
diff
changeset
|
146 unsigned int canvasHeight, |
591 | 147 float windowCenter, |
914
4d1f57773b5b
Added image inversion support in GrayscaleStyleConfigurator + OpenGLFloatTextureProgram
Benjamin Golinvaux <bgo@osimis.io>
parents:
771
diff
changeset
|
148 float windowWidth, |
4d1f57773b5b
Added image inversion support in GrayscaleStyleConfigurator + OpenGLFloatTextureProgram
Benjamin Golinvaux <bgo@osimis.io>
parents:
771
diff
changeset
|
149 bool invert) |
591 | 150 { |
947
1091b2adeb5a
Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents:
914
diff
changeset
|
151 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
|
152 { |
1576
92fca2b3ba3d
sanitizing the handling of canvas size
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1571
diff
changeset
|
153 OpenGLTextureProgram::Execution execution( |
92fca2b3ba3d
sanitizing the handling of canvas size
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1571
diff
changeset
|
154 program_, data.GetTexture(), transform, canvasWidth, canvasHeight); |
591 | 155 |
947
1091b2adeb5a
Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents:
914
diff
changeset
|
156 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
|
157 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
|
158 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
|
159 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
|
160 glUniform1f(execution.GetUniformLocation("u_invert"), invert); |
591 | 161 |
947
1091b2adeb5a
Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents:
914
diff
changeset
|
162 execution.DrawTriangles(); |
1091b2adeb5a
Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents:
914
diff
changeset
|
163 } |
591 | 164 } |
165 } | |
166 } |