Image.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. // Description : Image implementation,
  9. #include "EditorDefs.h"
  10. #include "Image.h"
  11. bool CImageEx::ConvertToFloatImage(CFloatImage& dstImage)
  12. {
  13. uint32 pixelMask;
  14. float intToFloat;
  15. switch (GetFormat())
  16. {
  17. case eTF_Unknown:
  18. case eTF_R8G8B8A8:
  19. pixelMask = std::numeric_limits<uint8>::max();
  20. intToFloat = static_cast<float>(pixelMask);
  21. break;
  22. case eTF_R16G16:
  23. pixelMask = std::numeric_limits<uint16>::max();
  24. intToFloat = static_cast<float>(pixelMask);
  25. break;
  26. default:
  27. return false;
  28. break;
  29. }
  30. dstImage.Allocate(GetWidth(), GetHeight());
  31. uint32* srcPixel = GetData();
  32. float* dstPixel = dstImage.GetData();
  33. for (int pixel = 0; pixel < (GetHeight() * GetWidth()); pixel++)
  34. {
  35. dstPixel[pixel] = clamp_tpl(static_cast<float>(srcPixel[pixel] & pixelMask) / intToFloat, 0.0f, 1.0f);
  36. }
  37. return true;
  38. }
  39. void CImageEx::SwapRedAndBlue()
  40. {
  41. if (!IsValid())
  42. {
  43. return;
  44. }
  45. // Set the loop pointers
  46. uint32* pPixData = GetData();
  47. uint32* pPixDataEnd = pPixData + GetWidth() * GetHeight();
  48. // Switch R and B
  49. while (pPixData != pPixDataEnd)
  50. {
  51. // Extract the bits, shift them, put them back and advance to the next pixel
  52. *pPixData = (*pPixData & 0xFF000000) | ((*pPixData & 0x00FF0000) >> 16) | (*pPixData & 0x0000FF00) | ((*pPixData & 0x000000FF) << 16);
  53. ++pPixData;
  54. }
  55. }
  56. void CImageEx::ReverseUpDown()
  57. {
  58. if (!IsValid())
  59. {
  60. return;
  61. }
  62. uint32* pPixData = GetData();
  63. const int height = GetHeight();
  64. const int width = GetWidth();
  65. for (int i = 0; i < height / 2; i++)
  66. {
  67. for (int j = 0; j < width; j++)
  68. {
  69. AZStd::swap(pPixData[i * width + j], pPixData[(height - 1 - i) * width + j]);
  70. }
  71. }
  72. }
  73. void CImageEx::FillAlpha(unsigned char value)
  74. {
  75. if (!IsValid())
  76. {
  77. return;
  78. }
  79. // Set the loop pointers
  80. uint32* pPixData = GetData();
  81. uint32* pPixDataEnd = pPixData + GetWidth() * GetHeight();
  82. while (pPixData != pPixDataEnd)
  83. {
  84. *pPixData = (*pPixData & 0x00FFFFFF) | (value << 24);
  85. ++pPixData;
  86. }
  87. }