PpmFile.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 <Atom/Utils/PpmFile.h>
  9. #include <AzCore/IO/ByteContainerStream.h>
  10. namespace AZ
  11. {
  12. AZStd::vector<uint8_t> Utils::PpmFile::CreatePpmFromImageBuffer(AZStd::span<const uint8_t> buffer, const RHI::Size& size, RHI::Format format)
  13. {
  14. AZ_Assert(format == RHI::Format::R8G8B8A8_UNORM || format == RHI::Format::B8G8R8A8_UNORM, "CreatePpmFromImageReadbackResult only supports R8G8B8A8_UNORM");
  15. const uint32_t pixelHeight = size.m_height;
  16. const uint32_t pixelWidth = size.m_width;
  17. constexpr int rbgByteSize = 3;
  18. constexpr int rbgaByteSize = 4;
  19. // Header for binary .ppm format, RGB, 8 bit per color component
  20. const auto header = AZStd::string::format("P6\n%d %d\n255\n", pixelWidth, pixelHeight);
  21. AZStd::vector<uint8_t> outBuffer; // write this buffer out once at the end
  22. outBuffer.resize(header.size() + pixelWidth * pixelHeight * rbgByteSize);
  23. ::memcpy(outBuffer.data(), header.data(), header.size());
  24. // throwing away alpha channel, since .ppm doesn't support it
  25. const uint8_t* inData = buffer.data();
  26. uint8_t* outData = outBuffer.data() + header.size();
  27. for (uint32_t i = 0; i < pixelHeight * pixelWidth; ++i)
  28. {
  29. ::memcpy(outData, inData, rbgByteSize);
  30. if (format == RHI::Format::B8G8R8A8_UNORM)
  31. {
  32. AZStd::reverse(outData, outData + rbgByteSize);
  33. }
  34. outData += rbgByteSize;
  35. inData += rbgaByteSize;
  36. }
  37. return outBuffer;
  38. }
  39. bool Utils::PpmFile::CreateImageBufferFromPpm(AZStd::span<const uint8_t> ppmData, AZStd::vector<uint8_t>& buffer, RHI::Size& size, RHI::Format& format)
  40. {
  41. if (ppmData.size() < 2)
  42. {
  43. return false;
  44. }
  45. if (ppmData[0] != 'P' || ppmData[1] != '6')
  46. {
  47. return false;
  48. }
  49. size_t pos = 2;
  50. auto isWhitespace = [](char c)
  51. {
  52. return c == ' ' || c == '\t' || c == '\r' || c == '\n';
  53. };
  54. auto eatWhitepaceAndComments = [&]()
  55. {
  56. bool isComment = false;
  57. while (pos < ppmData.size())
  58. {
  59. uint8_t byte = ppmData[pos];
  60. if (byte == '#')
  61. {
  62. isComment = true;
  63. }
  64. else if (byte == '\n')
  65. {
  66. isComment = false;
  67. }
  68. if (isWhitespace(byte) || isComment)
  69. {
  70. ++pos;
  71. }
  72. else
  73. {
  74. break;
  75. }
  76. }
  77. };
  78. auto readInt = [&]()
  79. {
  80. eatWhitepaceAndComments();
  81. const char* startOfInt = reinterpret_cast<const char*>(&ppmData[pos]);
  82. char* endOfInt = nullptr;
  83. uint32_t value = static_cast<uint32_t>(strtoul(startOfInt, &endOfInt, 10));
  84. pos += endOfInt - startOfInt;
  85. return value;
  86. };
  87. size.m_width = readInt();
  88. size.m_height = readInt();
  89. uint32_t maxValue = readInt();
  90. if (size.m_width == 0)
  91. {
  92. AZ_Error("PpmFile", false, "Invalid dimensions");
  93. return false;
  94. }
  95. if (size.m_height == 0)
  96. {
  97. AZ_Error("PpmFile", false, "Invalid dimensions");
  98. return false;
  99. }
  100. if (maxValue != 255)
  101. {
  102. AZ_Error("PpmFile", false, "Invalid max channel value");
  103. return false;
  104. }
  105. ++pos; // Consume final header character, usually \n
  106. constexpr int rbgByteSize = 3;
  107. constexpr int rbgaByteSize = 4;
  108. const size_t pixelCount = size.m_width * size.m_height;
  109. const size_t expectedBytes = pixelCount * rbgByteSize;
  110. const size_t remainingBytes = ppmData.size() - pos;
  111. if (remainingBytes != expectedBytes)
  112. {
  113. AZ_Error("PpmFile", false, "Image payload size (%zu bytes) does not match size indicated in the header (%zu bytes, %u x %u)",
  114. remainingBytes, expectedBytes, size.m_width, size.m_height);
  115. return false;
  116. }
  117. buffer.resize(pixelCount * rbgaByteSize);
  118. format = RHI::Format::R8G8B8A8_UNORM;
  119. const uint8_t* inData = ppmData.data() + pos;
  120. uint8_t* outData = buffer.data();
  121. for (uint32_t i = 0; i < pixelCount; ++i)
  122. {
  123. ::memcpy(outData, inData, rbgByteSize);
  124. outData += rbgaByteSize;
  125. inData += rbgByteSize;
  126. }
  127. return true;
  128. }
  129. }