ImageRgbaU8.h 4.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // zlib open source license
  2. //
  3. // Copyright (c) 2017 to 2019 David Forsgren Piuva
  4. //
  5. // This software is provided 'as-is', without any express or implied
  6. // warranty. In no event will the authors be held liable for any damages
  7. // arising from the use of this software.
  8. //
  9. // Permission is granted to anyone to use this software for any purpose,
  10. // including commercial applications, and to alter it and redistribute it
  11. // freely, subject to the following restrictions:
  12. //
  13. // 1. The origin of this software must not be misrepresented; you must not
  14. // claim that you wrote the original software. If you use this software
  15. // in a product, an acknowledgment in the product documentation would be
  16. // appreciated but is not required.
  17. //
  18. // 2. Altered source versions must be plainly marked as such, and must not be
  19. // misrepresented as being the original software.
  20. //
  21. // 3. This notice may not be removed or altered from any source
  22. // distribution.
  23. #ifndef DFPSR_IMAGE_RGBA_U8
  24. #define DFPSR_IMAGE_RGBA_U8
  25. #include "Color.h"
  26. #include "Image.h"
  27. #include "ImageU8.h"
  28. namespace dsr {
  29. // Pointing to the parent image using raw pointers for fast rendering. May not exceed the lifetime of the parent image!
  30. struct TextureRgbaLayer {
  31. const uint8_t *data = 0;
  32. int32_t strideShift = 0;
  33. uint32_t widthMask = 0, heightMask = 0;
  34. int32_t width = 0, height = 0;
  35. float subWidth = 0.0f, subHeight = 0.0f; // TODO: Better names?
  36. float halfPixelOffsetU = 0.0f, halfPixelOffsetV = 0.0f;
  37. TextureRgbaLayer();
  38. TextureRgbaLayer(const uint8_t *data, int32_t width, int32_t height);
  39. // Can it be sampled as a texture
  40. bool exists() const { return this->data != nullptr; }
  41. };
  42. #define MIP_BIN_COUNT 5
  43. // Pointing to the parent image using raw pointers for fast rendering. Not not separate from the image!
  44. struct TextureRgba {
  45. Buffer pyramidBuffer; // Storing the smaller mip levels
  46. TextureRgbaLayer mips[MIP_BIN_COUNT]; // Pointing to all mip levels including the original image
  47. // Can it be sampled as a texture
  48. bool exists() const { return this->mips[0].exists(); }
  49. // Does it have a mip pyramid generated for smoother sampling
  50. bool hasMipBuffer() const { return this->pyramidBuffer.get() != nullptr; }
  51. };
  52. class ImageRgbaU8Impl : public ImageImpl {
  53. public:
  54. static const int32_t channelCount = 4;
  55. static const int32_t pixelSize = channelCount;
  56. PackOrder packOrder;
  57. // Macro defined functions
  58. IMAGE_DECLARATION(ImageRgbaU8Impl, 4, Color4xU8, uint8_t);
  59. // Constructors
  60. ImageRgbaU8Impl(int32_t newWidth, int32_t newHeight, int32_t newStride, Buffer buffer, intptr_t startOffset, PackOrder packOrder);
  61. ImageRgbaU8Impl(int32_t newWidth, int32_t newHeight, int32_t alignment = 16);
  62. // Native canvas constructor
  63. ImageRgbaU8Impl(int32_t newWidth, int32_t newHeight, PackOrderIndex packOrderIndex);
  64. // Fast reading
  65. TextureRgba texture; // The texture view
  66. void initializeRgbaImage(); // Points to level 0 from all bins to allow rendering
  67. void generatePyramid(); // Fills the following bins with smaller images
  68. void removePyramid();
  69. bool isTexture() const;
  70. static bool isTexture(const ImageRgbaU8Impl* image); // Null cannot be sampled as a texture
  71. public:
  72. // Conversion to monochrome by extracting a channel
  73. ImageU8Impl getChannel(int32_t channelIndex) const;
  74. // Clone the image without padding or return the same instance if there is no padding
  75. // TODO: Return the unaligned image type, which is incompatible with SIMD operations
  76. ImageRgbaU8Impl getWithoutPadding() const;
  77. // Packs/unpacks the channels of an RGBA color in an unsigned 32-bit integer
  78. Color4xU8 packRgba(uint8_t red, uint8_t green, uint8_t blue, uint8_t alpha) const;
  79. Color4xU8 packRgba(ColorRgbaI32 rgba) const;
  80. static ColorRgbaI32 unpackRgba(Color4xU8 rgba, const PackOrder& order);
  81. ColorRgbaI32 unpackRgba(Color4xU8 rgba) const;
  82. // Packs/unpacks the channels of an RGB color in an unsigned 32-bit integer
  83. Color4xU8 packRgb(uint8_t red, uint8_t green, uint8_t blue) const;
  84. Color4xU8 packRgb(ColorRgbI32 rgb) const;
  85. static ColorRgbI32 unpackRgb(Color4xU8 rgb, const PackOrder& order);
  86. ColorRgbI32 unpackRgb(Color4xU8 rgb) const;
  87. };
  88. }
  89. #endif