ImageObjectImpl.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #pragma once
  9. #include <Atom/ImageProcessing/ImageObject.h>
  10. #include <AzCore/Memory/SystemAllocator.h>
  11. #include <Processing/DDSHeader.h>
  12. namespace ImageProcessingAtom
  13. {
  14. // ImageObject allows the abstraction of different kinds of
  15. // images generated during conversion. Supports 3D Image.
  16. class CImageObject
  17. : public IImageObject
  18. {
  19. public:
  20. AZ_CLASS_ALLOCATOR(CImageObject, AZ::SystemAllocator);
  21. public:
  22. // Constructors
  23. CImageObject(AZ::u32 width, AZ::u32 height, AZ::u32 maxMipCount, EPixelFormat pixelFormat);
  24. CImageObject(AZ::u32 width, AZ::u32 height, AZ::u32 depth, AZ::u32 maxMipCount, EPixelFormat pixelFormat);
  25. ~CImageObject();
  26. //virtual functions from IImageObject
  27. IImageObject* AllocateImage(EPixelFormat pixelFormat, uint32_t maxMipCount = (std::numeric_limits<uint32_t>::max)()) const override;
  28. IImageObject* AllocateImage(uint32_t maxMipCount = (std::numeric_limits<uint32_t>::max)()) const override;
  29. IImageObject* Clone(uint32_t maxMipCount = (std::numeric_limits<uint32_t>::max)()) const override;
  30. EPixelFormat GetPixelFormat() const override;
  31. AZ::u32 GetPixelCount(AZ::u32 mip) const override;
  32. AZ::u32 GetWidth(AZ::u32 mip) const override;
  33. AZ::u32 GetHeight(AZ::u32 mip) const override;
  34. AZ::u32 GetDepth(AZ::u32 mip) const override;
  35. AZ::u32 GetMipCount() const override;
  36. void GetImagePointer(AZ::u32 mip, AZ::u8*& pMem, AZ::u32& pitch) const override;
  37. AZ::u32 GetMipBufSize(AZ::u32 mip) const override;
  38. void SetMipData(AZ::u32 mip, AZ::u8* mipBuf, AZ::u32 bufSize, AZ::u32 pitch) override;
  39. AZ::u32 GetImageFlags() const override;
  40. void SetImageFlags(AZ::u32 imageFlags) override;
  41. void AddImageFlags(AZ::u32 imageFlags) override;
  42. void RemoveImageFlags(AZ::u32 imageFlags) override;
  43. bool HasImageFlags(AZ::u32 imageFlags) const override;
  44. //image data operations and calculations
  45. void ScaleAndBiasChannels(AZ::u32 firstMip, AZ::u32 maxMipCount, const AZ::Vector4& scale, const AZ::Vector4& bias) override;
  46. void ClampChannels(AZ::u32 firstMip, AZ::u32 maxMipCount, const AZ::Vector4& min, const AZ::Vector4& max) override;
  47. void TransferAlphaCoverage(const TextureSettings* textureSetting, const IImageObjectPtr srcImg) override;
  48. float ComputeAlphaCoverageScaleFactor(AZ::u32 mip, float fDesiredCoverage, float fAlphaRef) const override;
  49. float ComputeAlphaCoverage(AZ::u32 firstMip, float fAlphaRef) const override;
  50. bool CompareImage(const IImageObjectPtr otherImage) const override;
  51. uint32_t GetTextureMemory() const override;
  52. EAlphaContent GetAlphaContent() const override;
  53. void NormalizeVectors(AZ::u32 firstMip, AZ::u32 maxMipCount) override;
  54. void CopyPropertiesFrom(const IImageObjectPtr src) override;
  55. void Swizzle(const char channels[4]) override;
  56. void GetColorRange(AZ::Color& minColor, AZ::Color& maxColor) const override;
  57. void SetColorRange(const AZ::Color& minColor, const AZ::Color& maxColor) override;
  58. float GetAverageBrightness() const override;
  59. void SetAverageBrightness(float avgBrightness) override;
  60. AZ::Color GetAverageColor() const override;
  61. void SetAverageColor(const AZ::Color& averageColor) override;
  62. AZ::u32 GetNumPersistentMips() const override;
  63. void SetNumPersistentMips(AZ::u32 nMips) override;
  64. void GlossFromNormals(bool hasAuthoredGloss) override;
  65. void ClearColor(float r, float g, float b, float a) override;
  66. //end virtual functions from IImageObject
  67. private:
  68. enum EColorNormalization
  69. {
  70. eColorNormalization_Normalize,
  71. eColorNormalization_PassThrough,
  72. };
  73. enum EAlphaNormalization
  74. {
  75. eAlphaNormalization_SetToZero,
  76. eAlphaNormalization_Normalize,
  77. eAlphaNormalization_PassThrough,
  78. };
  79. private:
  80. class MipLevel
  81. {
  82. public:
  83. AZ_CLASS_ALLOCATOR(MipLevel, AZ::SystemAllocator);
  84. AZ::u32 m_width;
  85. AZ::u32 m_height;
  86. AZ::u32 m_depth;
  87. // m_rowCount is the number of rows for each depth slice.
  88. AZ::u32 m_rowCount; // for compressed textures m_rowCount is usually less than m_height
  89. AZ::u32 m_pitch; // row size in bytes
  90. AZ::u8* m_pData;
  91. public:
  92. MipLevel()
  93. : m_width(0)
  94. , m_height(0)
  95. , m_depth(1)
  96. , m_rowCount(0)
  97. , m_pitch(0)
  98. , m_pData(0)
  99. {
  100. }
  101. ~MipLevel()
  102. {
  103. delete[] m_pData;
  104. m_pData = 0;
  105. }
  106. void Alloc()
  107. {
  108. AZ_Assert(m_pData == 0, "Mip data must be empty before Allocation!");
  109. m_pData = new AZ::u8[GetSize()];
  110. }
  111. AZ::u32 GetSize() const
  112. {
  113. AZ_Assert(m_pitch, "Pitch must be greater than zero!");
  114. return m_pitch * m_rowCount * m_depth;
  115. }
  116. bool operator==(const MipLevel& other) const
  117. {
  118. if (m_width == other.m_width && m_height == other.m_height && m_depth == other.m_depth
  119. && m_rowCount == other.m_rowCount && m_pitch == other.m_pitch)
  120. {
  121. return (memcmp(m_pData, other.m_pData, GetSize()) == 0);
  122. }
  123. return false;
  124. }
  125. };
  126. private:
  127. EPixelFormat m_pixelFormat;
  128. AZStd::vector<MipLevel*> m_mips; // stores *pointers* to avoid reallocations when elements are erase()'d
  129. AZ::Color m_colMinARGB; // ARGB will be added the properties of the DDS file
  130. AZ::Color m_colMaxARGB; // ARGB will be added the properties of the DDS file
  131. AZ::Color m_averageColor;
  132. float m_averageBrightness; // will be added to the properties of the DDS file
  133. AZ::u32 m_imageFlags; //
  134. AZ::u32 m_numPersistentMips; // number of mipmaps won't be splitted
  135. public:
  136. // Reset this image object to specified format and size. Calling this function on a pre-existing
  137. // 3D Image, will result in a new 2D image.
  138. void ResetImage(AZ::u32 width, AZ::u32 height, AZ::u32 maxMipCount, EPixelFormat pixelFormat);
  139. void ResetImage(AZ::u32 width, AZ::u32 height, AZ::u32 depth, AZ::u32 maxMipCount, EPixelFormat pixelFormat);
  140. //get mip count and the origin (top mip) size
  141. void GetExtent(AZ::u32& width, AZ::u32& height, AZ::u32& mipCount) const;
  142. void GetExtent(AZ::u32& width, AZ::u32& height, AZ::u32& depth, AZ::u32& mipCount) const;
  143. AZ::u32 GetMipDataSize(AZ::u32 mip) const;
  144. //! calculates the average brightness for a texture
  145. float CalculateAverageBrightness() const;
  146. bool HasPowerOfTwoSizes() const;
  147. void CopyPropertiesFrom(const CImageObject* src);
  148. // Computes the dynamically used range for the texture and expands it to use the
  149. // full range [0,2^(2^ExponentBits-1)] for better quality.
  150. void NormalizeImageRange(EColorNormalization eColorNorm, EAlphaNormalization eAlphaNorm, bool bMaintainBlack = false, int nExponentBits = 0);
  151. // Brings normalized ranges back to it's original range.
  152. void ExpandImageRange(EColorNormalization eColorNorm, EAlphaNormalization eAlphaNorm, int nExponentBits = 0);
  153. private:
  154. //build image file header from this image object
  155. bool BuildSurfaceHeader(DDS_HEADER_LEGACY& header) const;
  156. bool BuildSurfaceExtendedHeader(DDS_HEADER_DXT10& exthead) const;
  157. };
  158. } // namespace ImageProcessingAtom