EtcFileHeader.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /*
  2. * Copyright 2015 The Etc2Comp Authors.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "EtcFileHeader.h"
  17. #include "EtcBlock4x4EncodingBits.h"
  18. #include <assert.h>
  19. namespace Etc
  20. {
  21. // ----------------------------------------------------------------------------------------------------
  22. //
  23. FileHeader_Pkm::FileHeader_Pkm(File *a_pfile)
  24. {
  25. m_pfile = a_pfile;
  26. static const char s_acMagicNumberData[4] = { 'P', 'K', 'M', ' ' };
  27. static const char s_acVersionData[2] = { '1', '0' };
  28. for (unsigned int ui = 0; ui < sizeof(s_acMagicNumberData); ui++)
  29. {
  30. m_data.m_acMagicNumber[ui] = s_acMagicNumberData[ui];
  31. }
  32. for (unsigned int ui = 0; ui < sizeof(s_acVersionData); ui++)
  33. {
  34. m_data.m_acVersion[ui] = s_acVersionData[ui];
  35. }
  36. m_data.m_ucDataType_msb = 0; // ETC1_RGB_NO_MIPMAPS
  37. m_data.m_ucDataType_lsb = 0;
  38. m_data.m_ucOriginalWidth_msb = (unsigned char)(m_pfile->GetSourceWidth() >> 8);
  39. m_data.m_ucOriginalWidth_lsb = m_pfile->GetSourceWidth() & 0xFF;
  40. m_data.m_ucOriginalHeight_msb = (unsigned char)(m_pfile->GetSourceHeight() >> 8);
  41. m_data.m_ucOriginalHeight_lsb = m_pfile->GetSourceHeight() & 0xFF;
  42. m_data.m_ucExtendedWidth_msb = (unsigned char)(m_pfile->GetExtendedWidth() >> 8);
  43. m_data.m_ucExtendedWidth_lsb = m_pfile->GetExtendedWidth() & 0xFF;
  44. m_data.m_ucExtendedHeight_msb = (unsigned char)(m_pfile->GetExtendedHeight() >> 8);
  45. m_data.m_ucExtendedHeight_lsb = m_pfile->GetExtendedHeight() & 0xFF;
  46. }
  47. // ----------------------------------------------------------------------------------------------------
  48. //
  49. void FileHeader_Pkm::Write(FILE *a_pfile)
  50. {
  51. fwrite(&m_data, sizeof(Data), 1, a_pfile);
  52. }
  53. // ----------------------------------------------------------------------------------------------------
  54. //
  55. FileHeader_Ktx::FileHeader_Ktx(File *a_pfile)
  56. {
  57. m_pfile = a_pfile;
  58. static const uint8_t s_au8Itentfier[12] =
  59. {
  60. 0xAB, 0x4B, 0x54, 0x58, // first four bytes of Byte[12] identifier
  61. 0x20, 0x31, 0x31, 0xBB, // next four bytes of Byte[12] identifier
  62. 0x0D, 0x0A, 0x1A, 0x0A // final four bytes of Byte[12] identifier
  63. };
  64. for (unsigned int ui = 0; ui < sizeof(s_au8Itentfier); ui++)
  65. {
  66. m_data.m_au8Identifier[ui] = s_au8Itentfier[ui];
  67. }
  68. m_data.m_u32Endianness = 0x04030201;
  69. m_data.m_u32GlType = 0;
  70. m_data.m_u32GlTypeSize = 1;
  71. m_data.m_u32GlFormat = 0;
  72. switch (m_pfile->GetImageFormat())
  73. {
  74. case Image::Format::RGB8:
  75. case Image::Format::SRGB8:
  76. m_data.m_u32GlInternalFormat = (unsigned int)InternalFormat::ETC2_RGB8;
  77. m_data.m_u32GlBaseInternalFormat = (unsigned int)BaseInternalFormat::ETC2_RGB8;
  78. break;
  79. case Image::Format::RGBA8:
  80. case Image::Format::SRGBA8:
  81. m_data.m_u32GlInternalFormat = (unsigned int)InternalFormat::ETC2_RGBA8;
  82. m_data.m_u32GlBaseInternalFormat = (unsigned int)BaseInternalFormat::ETC2_RGBA8;
  83. break;
  84. case Image::Format::RGB8A1:
  85. case Image::Format::SRGB8A1:
  86. m_data.m_u32GlInternalFormat = (unsigned int)InternalFormat::ETC2_RGB8A1;
  87. m_data.m_u32GlBaseInternalFormat = (unsigned int)BaseInternalFormat::ETC2_RGB8A1;
  88. break;
  89. case Image::Format::R11:
  90. m_data.m_u32GlInternalFormat = (unsigned int)InternalFormat::ETC2_R11;
  91. m_data.m_u32GlBaseInternalFormat = (unsigned int)BaseInternalFormat::ETC2_R11;
  92. break;
  93. case Image::Format::SIGNED_R11:
  94. m_data.m_u32GlInternalFormat = (unsigned int)InternalFormat::ETC2_SIGNED_R11;
  95. m_data.m_u32GlBaseInternalFormat = (unsigned int)BaseInternalFormat::ETC2_R11;
  96. break;
  97. case Image::Format::RG11:
  98. m_data.m_u32GlInternalFormat = (unsigned int)InternalFormat::ETC2_RG11;
  99. m_data.m_u32GlBaseInternalFormat = (unsigned int)BaseInternalFormat::ETC2_RG11;
  100. break;
  101. case Image::Format::SIGNED_RG11:
  102. m_data.m_u32GlInternalFormat = (unsigned int)InternalFormat::ETC2_SIGNED_RG11;
  103. m_data.m_u32GlBaseInternalFormat = (unsigned int)BaseInternalFormat::ETC2_RG11;
  104. break;
  105. default:
  106. m_data.m_u32GlInternalFormat = (unsigned int)InternalFormat::ETC1_RGB8;
  107. m_data.m_u32GlBaseInternalFormat = (unsigned int)BaseInternalFormat::ETC1_RGB8;
  108. break;
  109. }
  110. m_data.m_u32PixelWidth = 0;
  111. m_data.m_u32PixelHeight = 0;
  112. m_data.m_u32PixelDepth = 0;
  113. m_data.m_u32NumberOfArrayElements = 0;
  114. m_data.m_u32NumberOfFaces = 0;
  115. m_data.m_u32BytesOfKeyValueData = 0;
  116. m_pkeyvaluepair = nullptr;
  117. m_u32Images = 0;
  118. m_u32KeyValuePairs = 0;
  119. m_data.m_u32PixelWidth = m_pfile->GetSourceWidth();
  120. m_data.m_u32PixelHeight = m_pfile->GetSourceHeight();
  121. m_data.m_u32PixelDepth = 0;
  122. m_data.m_u32NumberOfArrayElements = 0;
  123. m_data.m_u32NumberOfFaces = 1;
  124. m_data.m_u32NumberOfMipmapLevels = m_pfile->GetNumMipmaps();
  125. }
  126. // ----------------------------------------------------------------------------------------------------
  127. //
  128. void FileHeader_Ktx::Write(FILE *a_pfile)
  129. {
  130. size_t szBytesWritten;
  131. // Write header
  132. szBytesWritten = fwrite(&m_data, 1, sizeof(Data), a_pfile);
  133. assert(szBytesWritten == sizeof(Data));
  134. // Write KeyAndValuePairs
  135. if (m_u32KeyValuePairs)
  136. {
  137. fwrite(m_pkeyvaluepair, m_pkeyvaluepair->u32KeyAndValueByteSize, 1, a_pfile);
  138. }
  139. }
  140. // ----------------------------------------------------------------------------------------------------
  141. //
  142. FileHeader_Ktx::Data *FileHeader_Ktx::GetData()
  143. {
  144. return &m_data;
  145. }
  146. // ----------------------------------------------------------------------------------------------------
  147. //
  148. } // namespace Etc