comparison OrthancStone/Sources/OpenGL/OpenGLTextureArray.cpp @ 2065:15f2e52835a1 deep-learning

removed OpenGLTextureArray::DownloadedArray()
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 04 May 2023 16:59:28 +0200
parents b6b5e1ca1cc2
children cf3d85eb291c
comparison
equal deleted inserted replaced
2064:4e31d76c7ecd 2065:15f2e52835a1
221 #endif 221 #endif
222 } 222 }
223 } 223 }
224 224
225 225
226 OpenGLTextureArray::DownloadedArray::DownloadedArray(const OpenGLTextureArray& texture) : 226 size_t OpenGLTextureArray::GetMemoryBufferSize() const
227 format_(texture.format_), 227 {
228 width_(texture.width_), 228 return static_cast<size_t>(Orthanc::GetBytesPerPixel(format_)) * width_ * height_ * depth_;
229 height_(texture.height_), 229 }
230 depth_(texture.depth_) 230
231 { 231
232 if (width_ != 0 && 232 void OpenGLTextureArray::Download(void* targetBuffer,
233 height_ != 0 && 233 size_t targetSize) const
234 depth_ != 0) 234 {
235 { 235 if (targetSize != GetMemoryBufferSize())
236 buffer_.resize(Orthanc::GetBytesPerPixel(format_) * width_ * height_ * depth_); 236 {
237 assert(buffer_.size() > 0); 237 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError);
238 238 }
239 else if (targetSize == 0)
240 {
241 return;
242 }
243 else if (targetBuffer == NULL)
244 {
245 throw Orthanc::OrthancException(Orthanc::ErrorCode_NullPointer);
246 }
247 else
248 {
239 #if 1 || defined(__EMSCRIPTEN__) 249 #if 1 || defined(__EMSCRIPTEN__)
240 /** 250 /**
241 * The "glGetTexImage()" function is unavailable in WebGL, it 251 * The "glGetTexImage()" function is unavailable in WebGL, it
242 * is necessary to use a framebuffer: 252 * is necessary to use a framebuffer:
243 * https://stackoverflow.com/a/15064957 253 * https://stackoverflow.com/a/15064957
244 **/ 254 **/
245 OpenGLFramebuffer framebuffer(texture.context_); 255 OpenGLFramebuffer framebuffer(context_);
246 256
247 Orthanc::Image tmp(texture.GetFormat(), texture.GetWidth(), texture.GetHeight(), true); 257 const size_t sliceSize = targetSize / depth_;
258
259 Orthanc::Image tmp(GetFormat(), GetWidth(), GetHeight(), true);
260 if (sliceSize != tmp.GetPitch() * height_)
261 {
262 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError);
263 }
248 264
249 for (unsigned int layer = 0; layer < depth_; layer++) 265 for (unsigned int layer = 0; layer < depth_; layer++)
250 { 266 {
251 framebuffer.ReadTexture(tmp, texture, layer); 267 framebuffer.ReadTexture(tmp, *this, layer);
252 memcpy(&buffer_[0] + layer * tmp.GetPitch() * height_, 268 memcpy(reinterpret_cast<uint8_t*>(targetBuffer) + layer * sliceSize, tmp.GetBuffer(), sliceSize);
253 tmp.GetBuffer(), tmp.GetPitch() * height_);
254 } 269 }
255 270
256 #else 271 #else
257 glBindTexture(GL_TEXTURE_2D_ARRAY, texture.texture_); 272 glBindTexture(GL_TEXTURE_3D, texture_);
258 273
259 switch (format_) 274 switch (format_)
260 { 275 {
261 case Orthanc::PixelFormat_Grayscale8: 276 case Orthanc::PixelFormat_Grayscale8:
262 glGetTexImage(GL_TEXTURE_2D_ARRAY, 0 /* base level */, GL_RED, GL_UNSIGNED_BYTE, &buffer_[0]); 277 glGetTexImage(GL_TEXTURE_3D, 0 /* base level */, GL_RED, GL_UNSIGNED_BYTE, targetBuffer);
263 break; 278 break;
264 279
265 case Orthanc::PixelFormat_RGB24: 280 case Orthanc::PixelFormat_RGB24:
266 glGetTexImage(GL_TEXTURE_2D_ARRAY, 0 /* base level */, GL_RGB, GL_UNSIGNED_BYTE, &buffer_[0]); 281 glGetTexImage(GL_TEXTURE_3D, 0 /* base level */, GL_RGB, GL_UNSIGNED_BYTE, targetBuffer);
267 break; 282 break;
268 283
269 case Orthanc::PixelFormat_RGBA32: 284 case Orthanc::PixelFormat_RGBA32:
270 glGetTexImage(GL_TEXTURE_2D_ARRAY, 0 /* base level */, GL_RGBA, GL_UNSIGNED_BYTE, &buffer_[0]); 285 glGetTexImage(GL_TEXTURE_3D, 0 /* base level */, GL_RGBA, GL_UNSIGNED_BYTE, targetBuffer);
271 break; 286 break;
272 287
273 case Orthanc::PixelFormat_Float32: 288 case Orthanc::PixelFormat_Float32:
274 glGetTexImage(GL_TEXTURE_2D_ARRAY, 0 /* base level */, GL_RED, GL_FLOAT, &buffer_[0]); 289 glGetTexImage(GL_TEXTURE_3D, 0 /* base level */, GL_RED, GL_FLOAT, targetBuffer);
275 break; 290 break;
276 291
277 default: 292 default:
278 throw Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented); 293 throw Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented);
279 } 294 }
280 #endif 295 #endif
281 } 296 }
282 } 297 }
283 298
284 299
285 Orthanc::ImageAccessor* OpenGLTextureArray::DownloadedArray::GetLayer(unsigned int layer) const 300 void OpenGLTextureArray::Download(std::string& target) const
286 { 301 {
287 if (layer >= depth_) 302 target.resize(GetMemoryBufferSize());
288 { 303 Download(target);
289 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange);
290 }
291 else if (width_ == 0 ||
292 height_ == 0 ||
293 depth_ == 0)
294 {
295 return new Orthanc::Image(format_, 0, 0, true);
296 }
297 else
298 {
299 const size_t rowSize = width_ * Orthanc::GetBytesPerPixel(format_);
300
301 std::unique_ptr<Orthanc::ImageAccessor> target(new Orthanc::ImageAccessor);
302 target->AssignReadOnly(format_, width_, height_, rowSize,
303 reinterpret_cast<const uint8_t*>(buffer_.c_str()) + layer * height_ * rowSize);
304 return target.release();
305 }
306 } 304 }
307 } 305 }
308 } 306 }