imageInternal.h 3.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. 
  2. // zlib open source license
  3. //
  4. // Copyright (c) 2017 to 2019 David Forsgren Piuva
  5. //
  6. // This software is provided 'as-is', without any express or implied
  7. // warranty. In no event will the authors be held liable for any damages
  8. // arising from the use of this software.
  9. //
  10. // Permission is granted to anyone to use this software for any purpose,
  11. // including commercial applications, and to alter it and redistribute it
  12. // freely, subject to the following restrictions:
  13. //
  14. // 1. The origin of this software must not be misrepresented; you must not
  15. // claim that you wrote the original software. If you use this software
  16. // in a product, an acknowledgment in the product documentation would be
  17. // appreciated but is not required.
  18. //
  19. // 2. Altered source versions must be plainly marked as such, and must not be
  20. // misrepresented as being the original software.
  21. //
  22. // 3. This notice may not be removed or altered from any source
  23. // distribution.
  24. #ifndef DFPSR_IMAGE_INTERNAL
  25. #define DFPSR_IMAGE_INTERNAL
  26. #include "../Image.h"
  27. #include "../ImageRgbaU8.h"
  28. namespace dsr {
  29. namespace imageInternal {
  30. //inline int32_t getWidth(const ImageImpl &image) { return image.width; }
  31. inline int32_t getWidth(const ImageImpl *image) { return image ? image->width : 0; }
  32. //inline int32_t getHeight(const ImageImpl &image) { return image.height; }
  33. inline int32_t getHeight(const ImageImpl *image) { return image ? image->height : 0; }
  34. //inline int32_t getStride(const ImageImpl &image) { return image.stride; }
  35. inline int32_t getStride(const ImageImpl *image) { return image ? image->stride : 0; }
  36. inline int32_t getRowSize(const ImageImpl &image) { return image.width * image.pixelSize; }
  37. inline int32_t getRowSize(const ImageImpl *image) { return image ? getRowSize(*image) : 0; }
  38. inline int32_t getUsedBytes(const ImageImpl &image) { return (image.stride * (image.height - 1)) + (image.width * image.pixelSize); }
  39. inline int32_t getUsedBytes(const ImageImpl *image) { return image ? getUsedBytes(*image) : 0; }
  40. //inline int32_t getPixelSize(const ImageImpl &image) { return image.pixelSize; }
  41. inline int32_t getPixelSize(const ImageImpl *image) { return image ? image->pixelSize : 0; }
  42. //inline int32_t getStartOffset(const ImageImpl &image) { return image.startOffset; }
  43. inline int32_t getStartOffset(const ImageImpl *image) { return image ? image->startOffset : 0; }
  44. inline Buffer getBuffer(const ImageImpl &image) { return image.buffer; }
  45. inline Buffer getBuffer(const ImageImpl *image) { return image ? getBuffer(*image) : Buffer(); }
  46. inline IRect getBound(const ImageImpl &image) { return IRect(0, 0, image.width, image.height); }
  47. inline IRect getBound(const ImageImpl *image) { return image ? getBound(*image) : IRect(); }
  48. inline PackOrder getPackOrder(const ImageRgbaU8Impl *image) { return image ? image->packOrder : PackOrder(); }
  49. // Get data
  50. // The pointer has access to the whole parent buffer,
  51. // to allow aligning SIMD vectors outside of the used region.
  52. template <typename T>
  53. static inline const SafePointer<T> getSafeData(const ImageImpl &image, int rowIndex = 0) {
  54. auto result = buffer_getSafeData<T>(image.buffer, "Image buffer");
  55. result.increaseBytes(image.startOffset + image.stride * rowIndex);
  56. return result;
  57. }
  58. template <typename T>
  59. inline const SafePointer<T> getSafeData(const ImageImpl *image, int rowIndex = 0) {
  60. return image ? getSafeData<T>(*image, rowIndex) : SafePointer<T>("Null image buffer");
  61. }
  62. template <typename T>
  63. static inline SafePointer<T> getSafeData(ImageImpl &image, int rowIndex = 0) {
  64. auto result = buffer_getSafeData<T>(image.buffer, "Image buffer");
  65. result.increaseBytes(image.startOffset + image.stride * rowIndex);
  66. return result;
  67. }
  68. template <typename T>
  69. inline SafePointer<T> getSafeData(ImageImpl *image, int rowIndex = 0) {
  70. return image ? getSafeData<T>(*image, rowIndex) : SafePointer<T>("Null image buffer");
  71. }
  72. }
  73. }
  74. #endif