comparison OrthancStone/Sources/OpenGL/OpenGLTexture.cpp @ 2034:4b24b7533346 deep-learning

cont
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 27 Jan 2023 15:43:24 +0100
parents 23b0a42eea85
children a73a8415780f
comparison
equal deleted inserted replaced
2033:23b0a42eea85 2034:4b24b7533346
80 { 80 {
81 LOG(ERROR) << "Unknown exception in ~OpenGLTexture"; 81 LOG(ERROR) << "Unknown exception in ~OpenGLTexture";
82 } 82 }
83 } 83 }
84 84
85 void OpenGLTexture::Load(const Orthanc::ImageAccessor& image, 85 void OpenGLTexture::Setup(Orthanc::PixelFormat format,
86 bool isLinearInterpolation) 86 unsigned int width,
87 { 87 unsigned int height,
88 if (!context_.IsContextLost()) 88 bool isLinearInterpolation,
89 const void* data)
90 {
91 if (context_.IsContextLost())
92 {
93 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError,
94 "OpenGL context has been lost");
95 }
96 else
89 { 97 {
90 glPixelStorei(GL_UNPACK_ALIGNMENT, 1); // Disable byte-alignment restriction 98 glPixelStorei(GL_UNPACK_ALIGNMENT, 1); // Disable byte-alignment restriction
91
92 if (image.GetPitch() != image.GetBytesPerPixel() * image.GetWidth())
93 {
94 throw Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented,
95 "Pitch is not the same as the row size");
96 }
97 99
98 // Bind it 100 // Bind it
99 glActiveTexture(GL_TEXTURE0); 101 glActiveTexture(GL_TEXTURE0);
100 glBindTexture(GL_TEXTURE_2D, texture_); 102 glBindTexture(GL_TEXTURE_2D, texture_);
101 103
102 GLenum sourceFormat, internalFormat, pixelType; 104 GLenum sourceFormat, internalFormat, pixelType;
103 105
104 switch (image.GetFormat()) 106 switch (format)
105 { 107 {
106 case Orthanc::PixelFormat_Grayscale8: 108 case Orthanc::PixelFormat_Grayscale8:
107 sourceFormat = GL_RED; 109 sourceFormat = GL_RED;
108 internalFormat = GL_RED; 110 internalFormat = GL_RED;
109 pixelType = GL_UNSIGNED_BYTE; 111 pixelType = GL_UNSIGNED_BYTE;
128 break; 130 break;
129 131
130 default: 132 default:
131 throw Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented, 133 throw Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented,
132 "No support for this format in OpenGL textures: " + 134 "No support for this format in OpenGL textures: " +
133 std::string(EnumerationToString(image.GetFormat()))); 135 std::string(EnumerationToString(format)));
134 } 136 }
135 137
136 width_ = image.GetWidth(); 138 width_ = width;
137 height_ = image.GetHeight(); 139 height_ = height;
138 140
139 GLint interpolation = (isLinearInterpolation ? GL_LINEAR : GL_NEAREST); 141 GLint interpolation = (isLinearInterpolation ? GL_LINEAR : GL_NEAREST);
140 142
141 // Load the texture from the image buffer 143 // Load the texture from the image buffer
142 glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, image.GetWidth(), image.GetHeight(), 144 glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, width, height,
143 0, sourceFormat, pixelType, image.GetConstBuffer()); 145 0, sourceFormat, pixelType, data);
144 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, interpolation); 146 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, interpolation);
145 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, interpolation); 147 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, interpolation);
146 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); 148 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
147 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); 149 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
148 } 150 }
149 } 151 }
150 152
153 void OpenGLTexture::Load(const Orthanc::ImageAccessor& image,
154 bool isLinearInterpolation)
155 {
156 if (image.GetPitch() != image.GetBytesPerPixel() * image.GetWidth())
157 {
158 throw Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented,
159 "Pitch is not the same as the row size");
160 }
161 else
162 {
163 Setup(image.GetFormat(), image.GetWidth(), image.GetHeight(),
164 isLinearInterpolation, image.GetConstBuffer());
165 }
166 }
167
151 168
152 void OpenGLTexture::Bind(GLint location) 169 void OpenGLTexture::Bind(GLint location)
153 { 170 {
154 glActiveTexture(GL_TEXTURE0); 171 glActiveTexture(GL_TEXTURE0);
155 glBindTexture(GL_TEXTURE_2D, texture_); 172 glBindTexture(GL_TEXTURE_2D, texture_);