image.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // ======================================================================== //
  2. // Copyright 2009-2019 Intel Corporation //
  3. // //
  4. // Licensed under the Apache License, Version 2.0 (the "License"); //
  5. // you may not use this file except in compliance with the License. //
  6. // You may obtain a copy of the License at //
  7. // //
  8. // http://www.apache.org/licenses/LICENSE-2.0 //
  9. // //
  10. // Unless required by applicable law or agreed to in writing, software //
  11. // distributed under the License is distributed on an "AS IS" BASIS, //
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. //
  13. // See the License for the specific language governing permissions and //
  14. // limitations under the License. //
  15. // ======================================================================== //
  16. #pragma once
  17. #include "common.h"
  18. #include "buffer.h"
  19. namespace oidn {
  20. struct Image
  21. {
  22. static constexpr int maxSize = 65536;
  23. char* ptr; // pointer to the first pixel
  24. int width; // width in number of pixels
  25. int height; // height in number of pixels
  26. size_t bytePixelStride; // pixel stride in number of *bytes*
  27. size_t rowStride; // row stride in number of *pixel strides*
  28. Format format; // pixel format
  29. Ref<Buffer> buffer; // buffer containing the image data
  30. Image() : ptr(nullptr), width(0), height(0), bytePixelStride(0), rowStride(0), format(Format::Undefined) {}
  31. Image(void* ptr, Format format, int width, int height, size_t byteOffset, size_t inBytePixelStride, size_t inByteRowStride)
  32. {
  33. if (ptr == nullptr)
  34. throw Exception(Error::InvalidArgument, "buffer pointer null");
  35. init((char*)ptr + byteOffset, format, width, height, inBytePixelStride, inByteRowStride);
  36. }
  37. Image(const Ref<Buffer>& buffer, Format format, int width, int height, size_t byteOffset, size_t inBytePixelStride, size_t inByteRowStride)
  38. {
  39. init(buffer->data() + byteOffset, format, width, height, inBytePixelStride, inByteRowStride);
  40. if (byteOffset + height * rowStride * bytePixelStride > buffer->size())
  41. throw Exception(Error::InvalidArgument, "buffer region out of range");
  42. }
  43. void init(char* ptr, Format format, int width, int height, size_t inBytePixelStride, size_t inByteRowStride)
  44. {
  45. assert(width >= 0);
  46. assert(height >= 0);
  47. if (width > maxSize || height > maxSize)
  48. throw Exception(Error::InvalidArgument, "image size too large");
  49. this->ptr = ptr;
  50. this->width = width;
  51. this->height = height;
  52. const size_t pixelSize = getFormatBytes(format);
  53. if (inBytePixelStride != 0)
  54. {
  55. if (inBytePixelStride < pixelSize)
  56. throw Exception(Error::InvalidArgument, "pixel stride smaller than pixel size");
  57. this->bytePixelStride = inBytePixelStride;
  58. }
  59. else
  60. {
  61. this->bytePixelStride = pixelSize;
  62. }
  63. if (inByteRowStride != 0)
  64. {
  65. if (inByteRowStride < width * this->bytePixelStride)
  66. throw Exception(Error::InvalidArgument, "row stride smaller than width * pixel stride");
  67. if (inByteRowStride % this->bytePixelStride != 0)
  68. throw Exception(Error::InvalidArgument, "row stride not integer multiple of pixel stride");
  69. this->rowStride = inByteRowStride / this->bytePixelStride;
  70. }
  71. else
  72. {
  73. this->rowStride = width;
  74. }
  75. this->format = format;
  76. }
  77. __forceinline char* get(int y, int x)
  78. {
  79. return ptr + ((size_t(y) * rowStride + size_t(x)) * bytePixelStride);
  80. }
  81. __forceinline const char* get(int y, int x) const
  82. {
  83. return ptr + ((size_t(y) * rowStride + size_t(x)) * bytePixelStride);
  84. }
  85. operator bool() const
  86. {
  87. return ptr != nullptr;
  88. }
  89. };
  90. } // namespace oidn