Bitmap.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (assimp)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2025, assimp team
  6. All rights reserved.
  7. Redistribution and use of this software in source and binary forms,
  8. with or without modification, are permitted provided that the following
  9. conditions are met:
  10. * Redistributions of source code must retain the above
  11. copyright notice, this list of conditions and the
  12. following disclaimer.
  13. * Redistributions in binary form must reproduce the above
  14. copyright notice, this list of conditions and the
  15. following disclaimer in the documentation and/or other
  16. materials provided with the distribution.
  17. * Neither the name of the assimp team, nor the names of its
  18. contributors may be used to endorse or promote products
  19. derived from this software without specific prior
  20. written permission of the assimp team.
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  25. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  26. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  27. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  28. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  29. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. ---------------------------------------------------------------------------
  33. */
  34. /** @file Bitmap.cpp
  35. * @brief Defines bitmap format helper for textures
  36. *
  37. * Used for file formats which embed their textures into the model file.
  38. */
  39. #include <assimp/Bitmap.h>
  40. #include <assimp/ByteSwapper.h>
  41. #include <assimp/texture.h>
  42. #include <assimp/IOStream.hpp>
  43. namespace Assimp {
  44. bool Bitmap::Save(aiTexture *texture, IOStream *file) {
  45. if (file == nullptr) {
  46. return false;
  47. }
  48. Header header;
  49. DIB dib;
  50. dib.size = DIB::dib_size;
  51. dib.width = texture->mWidth;
  52. dib.height = texture->mHeight;
  53. dib.planes = 1;
  54. dib.bits_per_pixel = 8 * mBytesPerPixel;
  55. dib.compression = 0;
  56. dib.image_size = (((dib.width * mBytesPerPixel) + 3) & 0x0000FFFC) * dib.height;
  57. dib.x_resolution = 0;
  58. dib.y_resolution = 0;
  59. dib.nb_colors = 0;
  60. dib.nb_important_colors = 0;
  61. header.type = 0x4D42; // 'BM'
  62. header.offset = Header::header_size + DIB::dib_size;
  63. header.size = header.offset + dib.image_size;
  64. header.reserved1 = 0;
  65. header.reserved2 = 0;
  66. WriteHeader(header, file);
  67. WriteDIB(dib, file);
  68. WriteData(texture, file);
  69. return true;
  70. }
  71. template <typename T>
  72. inline std::size_t Copy(uint8_t *data, const T &field) {
  73. #ifdef AI_BUILD_BIG_ENDIAN
  74. T field_swapped = AI_BE(field);
  75. std::memcpy(data, &field_swapped, sizeof(field));
  76. return sizeof(field);
  77. #else
  78. std::memcpy(data, &AI_BE(field), sizeof(field));
  79. return sizeof(field);
  80. #endif
  81. }
  82. void Bitmap::WriteHeader(Header &header, IOStream *file) {
  83. uint8_t data[Header::header_size];
  84. std::size_t offset = 0;
  85. offset += Copy(&data[offset], header.type);
  86. offset += Copy(&data[offset], header.size);
  87. offset += Copy(&data[offset], header.reserved1);
  88. offset += Copy(&data[offset], header.reserved2);
  89. Copy(&data[offset], header.offset);
  90. file->Write(data, Header::header_size, 1);
  91. }
  92. void Bitmap::WriteDIB(DIB &dib, IOStream *file) {
  93. uint8_t data[DIB::dib_size];
  94. std::size_t offset = 0;
  95. offset += Copy(&data[offset], dib.size);
  96. offset += Copy(&data[offset], dib.width);
  97. offset += Copy(&data[offset], dib.height);
  98. offset += Copy(&data[offset], dib.planes);
  99. offset += Copy(&data[offset], dib.bits_per_pixel);
  100. offset += Copy(&data[offset], dib.compression);
  101. offset += Copy(&data[offset], dib.image_size);
  102. offset += Copy(&data[offset], dib.x_resolution);
  103. offset += Copy(&data[offset], dib.y_resolution);
  104. offset += Copy(&data[offset], dib.nb_colors);
  105. Copy(&data[offset], dib.nb_important_colors);
  106. file->Write(data, DIB::dib_size, 1);
  107. }
  108. void Bitmap::WriteData(aiTexture *texture, IOStream *file) {
  109. static const std::size_t padding_offset = 4;
  110. static const uint8_t padding_data[padding_offset] = { 0x0, 0x0, 0x0, 0x0 };
  111. unsigned int padding = (padding_offset - ((mBytesPerPixel * texture->mWidth) % padding_offset)) % padding_offset;
  112. uint8_t pixel[mBytesPerPixel];
  113. for (std::size_t i = 0; i < texture->mHeight; ++i) {
  114. for (std::size_t j = 0; j < texture->mWidth; ++j) {
  115. const aiTexel &texel = texture->pcData[(texture->mHeight - i - 1) * texture->mWidth + j]; // Bitmap files are stored in bottom-up format
  116. pixel[0] = texel.r;
  117. pixel[1] = texel.g;
  118. pixel[2] = texel.b;
  119. pixel[3] = texel.a;
  120. file->Write(pixel, mBytesPerPixel, 1);
  121. }
  122. // When padding is 0, passing it as an argument will cause an assertion failure in DefaultIOStream::Write.
  123. if (padding) {
  124. file->Write(padding_data, padding, 1);
  125. }
  126. }
  127. }
  128. } // namespace Assimp