ConvertPixelFormat.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. #include <AzCore/std/time.h>
  9. #include <Processing/ImageFlags.h>
  10. #include <Processing/ImageObjectImpl.h>
  11. #include <Processing/ImageToProcess.h>
  12. #include <Processing/PixelFormatInfo.h>
  13. #include <Compressors/Compressor.h>
  14. #include <Converters/PixelOperation.h>
  15. ///////////////////////////////////////////////////////////////////////////////////
  16. //functions for maintaining alpha coverage.
  17. namespace ImageProcessingAtom
  18. {
  19. void ImageToProcess::ConvertFormat(EPixelFormat fmtDst)
  20. {
  21. //pixel format before convertion
  22. EPixelFormat fmtSrc = Get()->GetPixelFormat();
  23. //return directly if the image already has the desired pixel format
  24. if (fmtDst == fmtSrc)
  25. {
  26. return;
  27. }
  28. uint32 dwWidth, dwHeight;
  29. dwWidth = Get()->GetWidth(0);
  30. dwHeight = Get()->GetHeight(0);
  31. //if the output image size doesn't work the desired pixel format. set to fallback format
  32. const PixelFormatInfo* dstFmtInfo = CPixelFormats::GetInstance().GetPixelFormatInfo(fmtDst);
  33. if (!CPixelFormats::GetInstance().IsImageSizeValid(fmtDst, dwWidth, dwHeight, true))
  34. {
  35. AZ_Warning("Image Processing", false, "Output pixel format %d doesn't work with output image size %d x %d",
  36. fmtDst, dwWidth, dwHeight);
  37. //fall back to safe texture format
  38. if (dstFmtInfo->nChannels == 1)
  39. {
  40. fmtDst = dstFmtInfo->bHasAlpha ? ePixelFormat_A8 : ePixelFormat_R8;
  41. }
  42. else if (dstFmtInfo->nChannels == 2)
  43. {
  44. fmtDst = ePixelFormat_R8G8;
  45. }
  46. else
  47. {
  48. fmtDst = dstFmtInfo->bHasAlpha ? ePixelFormat_R8G8B8A8 : ePixelFormat_R8G8B8X8;
  49. }
  50. }
  51. //convert src image to uncompressed formats if it's compressed format
  52. bool isSrcUncompressed = CPixelFormats::GetInstance().IsPixelFormatUncompressed(fmtSrc);
  53. bool isDstUncompressed = CPixelFormats::GetInstance().IsPixelFormatUncompressed(fmtDst);
  54. if (isSrcUncompressed && isDstUncompressed)
  55. {//both are uncompressed
  56. ConvertFormatUncompressed(fmtDst);
  57. }
  58. else if (!isSrcUncompressed && !isDstUncompressed)
  59. { //both are compressed
  60. AZ_Assert(false, "unusual user case. but we can still handle it");
  61. }
  62. else
  63. { //one fmt is compressed format
  64. //use the compressed format to find right compressor
  65. EPixelFormat compressedFmt = isSrcUncompressed ? fmtDst : fmtSrc;
  66. EPixelFormat uncompressedFmt = isSrcUncompressed ? fmtSrc : fmtDst;
  67. ColorSpace sourceColorSpace = Get()->HasImageFlags(EIF_SRGBRead) ? ColorSpace::sRGB : ColorSpace::linear;
  68. ICompressorPtr compressor = ICompressor::FindCompressor(compressedFmt, sourceColorSpace, isSrcUncompressed);
  69. if (compressor == nullptr)
  70. {
  71. //no avaible compressor for compressed format
  72. AZ_Warning("Image Processing", false, "No avaliable compressor for pixel format %d", compressedFmt);
  73. return;
  74. }
  75. //check if the uncompressed fmt also supported by the compressor
  76. EPixelFormat desiredUncompressedFmt = compressor->GetSuggestedUncompressedFormat(compressedFmt, uncompressedFmt);
  77. if (desiredUncompressedFmt != uncompressedFmt)
  78. {
  79. //we need to do intermedia convertion to convert to the temperory format
  80. ConvertFormat(desiredUncompressedFmt);
  81. ConvertFormat(fmtDst);
  82. }
  83. else
  84. {
  85. IImageObjectPtr dstImage = nullptr;
  86. [[maybe_unused]] const PixelFormatInfo* compressedInfo = CPixelFormats::GetInstance().GetPixelFormatInfo(compressedFmt);
  87. if (isSrcUncompressed)
  88. {
  89. AZ::u64 startTime = AZStd::GetTimeUTCMilliSecond();
  90. dstImage = compressor->CompressImage(Get(), fmtDst, &m_compressOption);
  91. AZ::u64 endTime = AZStd::GetTimeUTCMilliSecond();
  92. [[maybe_unused]] double processTime = static_cast<double>(endTime - startTime) / 1000.0;
  93. if (dstImage)
  94. {
  95. AZ_TracePrintf("Image Processing", "Image [%dx%d] was compressed to [%s] format by [%s] in %.3f seconds\n",
  96. Get()->GetWidth(0), Get()->GetHeight(0), compressedInfo->szName, compressor->GetName(), processTime);
  97. }
  98. }
  99. else
  100. {
  101. dstImage = compressor->DecompressImage(Get(), fmtDst);
  102. }
  103. Set(dstImage);
  104. if (dstImage == nullptr)
  105. {
  106. AZ_Error("Image Processing", false, "Failed to use [%s] to %s [%s] format", compressor->GetName(),
  107. isSrcUncompressed ? "compress" : "decompress",
  108. compressedInfo->szName);
  109. }
  110. }
  111. }
  112. }
  113. void ImageToProcess::ConvertFormatUncompressed(EPixelFormat fmtTo)
  114. {
  115. IImageObjectPtr srcImage = m_img;
  116. EPixelFormat srcFmt = srcImage->GetPixelFormat();
  117. EPixelFormat dstFmt = fmtTo;
  118. if (!(CPixelFormats::GetInstance().IsPixelFormatUncompressed(srcFmt)
  119. && CPixelFormats::GetInstance().IsPixelFormatUncompressed(dstFmt)))
  120. {
  121. AZ_Assert(false, "both source and dest images' pixel format need to be uncompressed");
  122. return;
  123. }
  124. IImageObjectPtr dstImage(m_img->AllocateImage(fmtTo));
  125. AZ_Assert(srcImage->GetPixelCount(0) == dstImage->GetPixelCount(0), "dest image has different size than source image");
  126. //create pixel operation function for src and dst images
  127. IPixelOperationPtr srcOp = CreatePixelOperation(srcFmt);
  128. IPixelOperationPtr dstOp = CreatePixelOperation(dstFmt);
  129. //get count of bytes per pixel for both src and dst images
  130. uint32 srcPixelBytes = CPixelFormats::GetInstance().GetPixelFormatInfo(srcFmt)->bitsPerBlock / 8;
  131. uint32 dstPixelBytes = CPixelFormats::GetInstance().GetPixelFormatInfo(dstFmt)->bitsPerBlock / 8;
  132. const uint32 dwMips = dstImage->GetMipCount();
  133. float r, g, b, a;
  134. for (uint32 dwMip = 0; dwMip < dwMips; ++dwMip)
  135. {
  136. uint8* srcPixelBuf;
  137. uint32 srcPitch;
  138. srcImage->GetImagePointer(dwMip, srcPixelBuf, srcPitch);
  139. uint8* dstPixelBuf;
  140. uint32 dstPitch;
  141. dstImage->GetImagePointer(dwMip, dstPixelBuf, dstPitch);
  142. const uint32 pixelCount = srcImage->GetPixelCount(dwMip);
  143. for (uint32 i = 0; i < pixelCount; ++i, srcPixelBuf += srcPixelBytes, dstPixelBuf += dstPixelBytes)
  144. {
  145. srcOp->GetRGBA(srcPixelBuf, r, g, b, a);
  146. dstOp->SetRGBA(dstPixelBuf, r, g, b, a);
  147. }
  148. }
  149. m_img = dstImage;
  150. }
  151. } // namespace ImageProcessingAtom