ImageConvert.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 <QRect>
  10. #include <BuilderSettings/PresetSettings.h>
  11. #include <BuilderSettings/TextureSettings.h>
  12. #include <Atom/ImageProcessing/ImageObject.h>
  13. #include <Atom/ImageProcessing/ImageProcessingDefines.h>
  14. #include <Compressors/Compressor.h>
  15. #include <AzCore/Jobs/Job.h>
  16. #include <AzCore/std/string/string.h>
  17. #include <AzCore/Asset/AssetCommon.h>
  18. #include <AssetBuilderSDK/AssetBuilderSDK.h>
  19. #include <ImageBuilderBaseType.h>
  20. namespace ImageProcessingAtom
  21. {
  22. class IImageObject;
  23. class ImageToProcess;
  24. //Convert image file with its image export setting and save to specified folder.
  25. //this function can be useful for a cancelable job
  26. class ImageConvertProcess* CreateImageConvertProcess(const AZStd::string& imageFilePath,
  27. const AZStd::string& exportDir, const PlatformName& platformName, AZStd::vector<AssetBuilderSDK::JobProduct>& jobProducts, AZ::SerializeContext* context = nullptr);
  28. //Convert image file with its image export setting and save to specified folder. it will return when the whole conversion is done.
  29. //Could be used for command mode or test
  30. bool ConvertImageFile(const AZStd::string& imageFilePath, const AZStd::string& exportDir, AZStd::vector<AZStd::string>& outPaths,
  31. const PlatformName& platformName = "", AZ::SerializeContext* context = nullptr);
  32. //image filter function
  33. void FilterImage(MipGenType genType, MipGenEvalType evalType, float blurH, float blurV, const IImageObjectPtr srcImg, int srcMip,
  34. IImageObjectPtr dstImg, int dstMip, QRect* srcRect, QRect* dstRect);
  35. //get compression error for an image converting to certain format
  36. void GetBC1CompressionErrors(IImageObjectPtr originImage, float& errorLinear, float& errorSrgb,
  37. ICompressor::CompressOption option);
  38. float GetErrorBetweenImages(IImageObjectPtr inputImage1, IImageObjectPtr inputImage2);
  39. //Converts the image to a RGBA8 format that can be displayed in a preview UI.
  40. IImageObjectPtr ConvertImageForPreview(IImageObjectPtr image);
  41. //Convert a (potentially compressed, potentially sRGB) image to linear RGBA32.
  42. IImageObjectPtr GetUncompressedLinearImage(IImageObjectPtr image);
  43. //get output image size and mip count based on the texture setting and preset setting
  44. //other helper functions
  45. //Get desired output image size based on the texture settings
  46. void GetOutputExtent(AZ::u32 inputWidth, AZ::u32 inputHeight, AZ::u32& outWidth, AZ::u32& outHeight, AZ::u32& outReduce,
  47. const TextureSettings* textureSettings, const PresetSettings* presetSettings);
  48. // The structure used to create a ImageConvertProcess
  49. struct ImageConvertProcessDescriptor
  50. {
  51. // The input image object
  52. IImageObjectPtr m_inputImage;
  53. TextureSettings m_textureSetting;
  54. PresetSettings m_presetSetting;
  55. // If the process is for preview convert result. Some steps will be optimized if it's true
  56. bool m_isPreview = false;
  57. // The target platform for the product asset
  58. AZStd::string m_platform;
  59. // Path to the original preset file, for debug output
  60. AZStd::string m_filePath;
  61. // The following parameters are required if it's not for preview mode
  62. // If the process is for preview convert result. Some steps will be optimized if it's true
  63. bool m_isStreaming = true;
  64. // The file name of the image which includes file extension. This is used to generate output file path.
  65. AZStd::string m_imageName;
  66. // The folder to save all output asset files. This is used to generate output file path.
  67. AZStd::string m_outputFolder;
  68. // Asset id of the source image file. This is mainly used to generate AssetId of ImageMipChainAsset which is referenced in StreamingPoolAsset
  69. AZ::Data::AssetId m_sourceAssetId;
  70. // List of output products for the job, appended to by the ImageConvertProcess
  71. AZStd::vector<AssetBuilderSDK::JobProduct>* m_jobProducts = nullptr;
  72. // Should the step to save resulting file to disk be skipped. Disabling this can be useful for in-memory image processing.
  73. bool m_shouldSaveFile = true;
  74. };
  75. /**
  76. * ImageConvertProcess is the class to handle the full convertion process to convert an input image object
  77. * to a new image object which is used in 3d renderer.
  78. */
  79. class ImageConvertProcess
  80. {
  81. public:
  82. //constructor
  83. ImageConvertProcess(AZStd::unique_ptr<ImageConvertProcessDescriptor>&& description);
  84. ~ImageConvertProcess();
  85. //doing image conversion, this function need to be called repeatly until the process is done
  86. //it could used for a working thread which may need to cancel a process
  87. void UpdateProcess();
  88. //doing all conversion in one step. This function will call UpdateProcess in a while loop until it's done.
  89. void ProcessAll();
  90. //for multi-thread
  91. //get percentage of image convertion progress
  92. float GetProgress();
  93. bool IsFinished();
  94. bool IsSucceed();
  95. //get output images
  96. IImageObjectPtr GetOutputImage();
  97. IImageObjectPtr GetOutputIBLSpecularCubemap();
  98. IImageObjectPtr GetOutputIBLDiffuseCubemap();
  99. // Get output JobProducts and append them to the outProducts vector.
  100. void GetAppendOutputProducts(AZStd::vector<AssetBuilderSDK::JobProduct>& outProducts);
  101. const ImageConvertProcessDescriptor* GetInputDesc() const;
  102. private:
  103. //input image and settings
  104. AZStd::shared_ptr<ImageConvertProcessDescriptor> m_input;
  105. //for alpha
  106. //to indicate the current alpha channel content
  107. EAlphaContent m_alphaContent;
  108. //output results of IBL cubemap generation, used in unit tests
  109. IImageObjectPtr m_iblSpecularCubemapImage;
  110. IImageObjectPtr m_iblDiffuseCubemapImage;
  111. //image for processing
  112. ImageToProcess* m_image;
  113. //progress
  114. uint32 m_progressStep;
  115. bool m_isFinished;
  116. bool m_isSucceed;
  117. //for get processing time
  118. AZStd::sys_time_t m_startTime;
  119. double m_processTime; //in seconds
  120. // All JobProducts from the process
  121. AZStd::vector<AssetBuilderSDK::JobProduct> m_jobProducts;
  122. private:
  123. //validate the input image and settings
  124. bool ValidateInput();
  125. //mipmap generation
  126. bool FillMipmaps();
  127. //mipmap generation for cubemap
  128. bool FillCubemapMipmaps();
  129. //set (alpha-weighted) average color computed from given mip
  130. bool SetAverageColor(AZ::u32 mip);
  131. //IBL cubemap generation, this creates a separate ImageConvertProcess
  132. bool CreateIBLCubemap(PresetName preset, const char* fileNameSuffix, IImageObjectPtr& cubemapImage);
  133. //convert color space to linear with pixel format rgba32f
  134. bool ConvertToLinear();
  135. //convert to output color space before compression
  136. bool ConvertToOuputColorSpace();
  137. //pixel format convertion/compression
  138. bool ConvertPixelformat();
  139. //save output image to a file
  140. bool SaveOutput();
  141. //if it's converting for cubemap
  142. bool IsConvertToCubemap();
  143. //if the input image is a pre-convolved cubemap
  144. bool IsPreconvolvedCubemap();
  145. };
  146. }// namespace ImageProcessingAtom