comparison OrthancStone/Sources/OpenGL/OpenGLTexture.cpp @ 2032:d10bab7cc396 deep-learning

added OpenGLTexture::Download()
author Sebastien Jodogne <s.jodogne@gmail.com>
date Sat, 21 Jan 2023 16:48:56 +0100
parents 7053b8a0aaec
children 23b0a42eea85
comparison
equal deleted inserted replaced
2031:a56f7ed0cdf9 2032:d10bab7cc396
22 22
23 23
24 #include "OpenGLTexture.h" 24 #include "OpenGLTexture.h"
25 #include "IOpenGLContext.h" 25 #include "IOpenGLContext.h"
26 26
27 #include <Images/Image.h>
27 #include <Logging.h> 28 #include <Logging.h>
28 #include <OrthancException.h> 29 #include <OrthancException.h>
29 30
30 namespace OrthancStone 31 namespace OrthancStone
31 { 32 {
32 namespace OpenGL 33 namespace OpenGL
33 { 34 {
34 OpenGLTexture::OpenGLTexture(OpenGL::IOpenGLContext& context) 35 OpenGLTexture::OpenGLTexture(OpenGL::IOpenGLContext& context) :
35 : width_(0) 36 texture_(0),
36 , height_(0) 37 width_(0),
37 , context_(context) 38 height_(0),
39 context_(context)
38 { 40 {
39 if (!context_.IsContextLost()) 41 if (!context_.IsContextLost())
40 { 42 {
41 // Generate a texture object 43 // Generate a texture object
42 glGenTextures(1, &texture_); 44 glGenTextures(1, &texture_);
43 if (texture_ == 0) 45 if (texture_ == 0)
44 { 46 {
45 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError, 47 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError,
46 "Cannot create an OpenGL program"); 48 "Cannot create an OpenGL texture");
47 } 49 }
48 } 50 }
49 } 51 }
50 52
51 OpenGLTexture::~OpenGLTexture() 53 OpenGLTexture::~OpenGLTexture()
95 97
96 // Bind it 98 // Bind it
97 glActiveTexture(GL_TEXTURE0); 99 glActiveTexture(GL_TEXTURE0);
98 glBindTexture(GL_TEXTURE_2D, texture_); 100 glBindTexture(GL_TEXTURE_2D, texture_);
99 101
100 GLenum sourceFormat, internalFormat; 102 GLenum sourceFormat, internalFormat, pixelType;
101 103
102 switch (image.GetFormat()) 104 switch (image.GetFormat())
103 { 105 {
104 case Orthanc::PixelFormat_Grayscale8: 106 case Orthanc::PixelFormat_Grayscale8:
105 sourceFormat = GL_RED; 107 sourceFormat = GL_RED;
106 internalFormat = GL_RED; 108 internalFormat = GL_RED;
109 pixelType = GL_UNSIGNED_BYTE;
107 break; 110 break;
108 111
109 case Orthanc::PixelFormat_RGB24: 112 case Orthanc::PixelFormat_RGB24:
110 sourceFormat = GL_RGB; 113 sourceFormat = GL_RGB;
111 internalFormat = GL_RGB; 114 internalFormat = GL_RGB;
115 pixelType = GL_UNSIGNED_BYTE;
112 break; 116 break;
113 117
114 case Orthanc::PixelFormat_RGBA32: 118 case Orthanc::PixelFormat_RGBA32:
115 sourceFormat = GL_RGBA; 119 sourceFormat = GL_RGBA;
116 internalFormat = GL_RGBA; 120 internalFormat = GL_RGBA;
121 pixelType = GL_UNSIGNED_BYTE;
117 break; 122 break;
118 123
119 default: 124 default:
120 throw Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented, 125 throw Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented,
121 "No support for this format in OpenGL textures: " + 126 "No support for this format in OpenGL textures: " +
127 132
128 GLint interpolation = (isLinearInterpolation ? GL_LINEAR : GL_NEAREST); 133 GLint interpolation = (isLinearInterpolation ? GL_LINEAR : GL_NEAREST);
129 134
130 // Load the texture from the image buffer 135 // Load the texture from the image buffer
131 glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, image.GetWidth(), image.GetHeight(), 136 glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, image.GetWidth(), image.GetHeight(),
132 0, sourceFormat, GL_UNSIGNED_BYTE, image.GetConstBuffer()); 137 0, sourceFormat, pixelType, image.GetConstBuffer());
133 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, interpolation); 138 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, interpolation);
134 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, interpolation); 139 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, interpolation);
135 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); 140 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
136 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); 141 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
137 } 142 }
142 { 147 {
143 glActiveTexture(GL_TEXTURE0); 148 glActiveTexture(GL_TEXTURE0);
144 glBindTexture(GL_TEXTURE_2D, texture_); 149 glBindTexture(GL_TEXTURE_2D, texture_);
145 glUniform1i(location, 0 /* texture unit */); 150 glUniform1i(location, 0 /* texture unit */);
146 } 151 }
152
153
154 Orthanc::ImageAccessor* OpenGLTexture::Download(Orthanc::PixelFormat format)
155 {
156 if (context_.IsContextLost())
157 {
158 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError,
159 "OpenGL context is lost");
160 }
161
162 std::unique_ptr<Orthanc::ImageAccessor> target(new Orthanc::Image(format, width_, height_, true));
163
164 glBindTexture(GL_TEXTURE_2D, texture_);
165
166 switch (format)
167 {
168 case Orthanc::PixelFormat_Grayscale8:
169 glGetTexImage(GL_TEXTURE_2D, 0 /* base level */, GL_RED, GL_UNSIGNED_BYTE, target->GetBuffer());
170 break;
171
172 case Orthanc::PixelFormat_RGB24:
173 glGetTexImage(GL_TEXTURE_2D, 0 /* base level */, GL_RGB, GL_UNSIGNED_BYTE, target->GetBuffer());
174 break;
175
176 case Orthanc::PixelFormat_RGBA32:
177 glGetTexImage(GL_TEXTURE_2D, 0 /* base level */, GL_RGBA, GL_UNSIGNED_BYTE, target->GetBuffer());
178 break;
179
180 default:
181 throw Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented);
182 }
183
184 return target.release();
185 }
147 } 186 }
148 } 187 }