| 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 |
|
| 17 |
#include "EtcFileHeader.h" |
| 18 |
|
| 19 |
#include "EtcBlock4x4EncodingBits.h" |
| 20 |
|
| 21 |
#include <assert.h> |
| 22 |
|
| 23 |
namespace Etc |
| 24 |
{ |
| 25 |
|
| 26 |
// ---------------------------------------------------------------------------------------------------- |
| 27 |
// |
| 28 |
FileHeader_Pkm::FileHeader_Pkm(File *a_pfile) |
| 29 |
{ |
| 30 |
m_pfile = a_pfile; |
| 31 |
|
| 32 |
static const char s_acMagicNumberData[4] = { 'P', 'K', 'M', ' ' }; |
| 33 |
static const char s_acVersionData[2] = { '1', '0' }; |
| 34 |
|
| 35 |
for (unsigned int ui = 0; ui < sizeof(s_acMagicNumberData); ui++) |
| 36 |
{ |
| 37 |
m_data.m_acMagicNumber[ui] = s_acMagicNumberData[ui]; |
| 38 |
} |
| 39 |
|
| 40 |
for (unsigned int ui = 0; ui < sizeof(s_acVersionData); ui++) |
| 41 |
{ |
| 42 |
m_data.m_acVersion[ui] = s_acVersionData[ui]; |
| 43 |
} |
| 44 |
|
| 45 |
m_data.m_ucDataType_msb = 0; // ETC1_RGB_NO_MIPMAPS |
| 46 |
m_data.m_ucDataType_lsb = 0; |
| 47 |
|
| 48 |
m_data.m_ucOriginalWidth_msb = (unsigned char)(m_pfile->GetSourceWidth() >> 8); |
| 49 |
m_data.m_ucOriginalWidth_lsb = m_pfile->GetSourceWidth() & 0xFF; |
| 50 |
m_data.m_ucOriginalHeight_msb = (unsigned char)(m_pfile->GetSourceHeight() >> 8); |
| 51 |
m_data.m_ucOriginalHeight_lsb = m_pfile->GetSourceHeight() & 0xFF; |
| 52 |
|
| 53 |
m_data.m_ucExtendedWidth_msb = (unsigned char)(m_pfile->GetExtendedWidth() >> 8); |
| 54 |
m_data.m_ucExtendedWidth_lsb = m_pfile->GetExtendedWidth() & 0xFF; |
| 55 |
m_data.m_ucExtendedHeight_msb = (unsigned char)(m_pfile->GetExtendedHeight() >> 8); |
| 56 |
m_data.m_ucExtendedHeight_lsb = m_pfile->GetExtendedHeight() & 0xFF; |
| 57 |
|
| 58 |
} |
| 59 |
|
| 60 |
// ---------------------------------------------------------------------------------------------------- |
| 61 |
// |
| 62 |
void FileHeader_Pkm::Write(FILE *a_pfile) |
| 63 |
{ |
| 64 |
|
| 65 |
fwrite(&m_data, sizeof(Data), 1, a_pfile); |
| 66 |
|
| 67 |
} |
| 68 |
|
| 69 |
// ---------------------------------------------------------------------------------------------------- |
| 70 |
// |
| 71 |
FileHeader_Ktx::FileHeader_Ktx(File *a_pfile) |
| 72 |
{ |
| 73 |
m_pfile = a_pfile; |
| 74 |
|
| 75 |
static const uint8_t s_au8Itentfier[12] = |
| 76 |
{ |
| 77 |
0xAB, 0x4B, 0x54, 0x58, // first four bytes of Byte[12] identifier |
| 78 |
0x20, 0x31, 0x31, 0xBB, // next four bytes of Byte[12] identifier |
| 79 |
0x0D, 0x0A, 0x1A, 0x0A // final four bytes of Byte[12] identifier |
| 80 |
}; |
| 81 |
|
| 82 |
for (unsigned int ui = 0; ui < sizeof(s_au8Itentfier); ui++) |
| 83 |
{ |
| 84 |
m_data.m_au8Identifier[ui] = s_au8Itentfier[ui]; |
| 85 |
} |
| 86 |
|
| 87 |
m_data.m_u32Endianness = 0x04030201; |
| 88 |
m_data.m_u32GlType = 0; |
| 89 |
m_data.m_u32GlTypeSize = 1; |
| 90 |
m_data.m_u32GlFormat = 0; |
| 91 |
|
| 92 |
switch (m_pfile->GetImageFormat()) |
| 93 |
{ |
| 94 |
case Image::Format::RGB8: |
| 95 |
case Image::Format::SRGB8: |
| 96 |
m_data.m_u32GlInternalFormat = (unsigned int)InternalFormat::ETC2_RGB8; |
| 97 |
m_data.m_u32GlBaseInternalFormat = (unsigned int)BaseInternalFormat::ETC2_RGB8; |
| 98 |
break; |
| 99 |
|
| 100 |
case Image::Format::RGBA8: |
| 101 |
case Image::Format::SRGBA8: |
| 102 |
m_data.m_u32GlInternalFormat = (unsigned int)InternalFormat::ETC2_RGBA8; |
| 103 |
m_data.m_u32GlBaseInternalFormat = (unsigned int)BaseInternalFormat::ETC2_RGBA8; |
| 104 |
break; |
| 105 |
|
| 106 |
case Image::Format::RGB8A1: |
| 107 |
case Image::Format::SRGB8A1: |
| 108 |
m_data.m_u32GlInternalFormat = (unsigned int)InternalFormat::ETC2_RGB8A1; |
| 109 |
m_data.m_u32GlBaseInternalFormat = (unsigned int)BaseInternalFormat::ETC2_RGB8A1; |
| 110 |
break; |
| 111 |
|
| 112 |
case Image::Format::R11: |
| 113 |
m_data.m_u32GlInternalFormat = (unsigned int)InternalFormat::ETC2_R11; |
| 114 |
m_data.m_u32GlBaseInternalFormat = (unsigned int)BaseInternalFormat::ETC2_R11; |
| 115 |
break; |
| 116 |
|
| 117 |
case Image::Format::SIGNED_R11: |
| 118 |
m_data.m_u32GlInternalFormat = (unsigned int)InternalFormat::ETC2_SIGNED_R11; |
| 119 |
m_data.m_u32GlBaseInternalFormat = (unsigned int)BaseInternalFormat::ETC2_R11; |
| 120 |
break; |
| 121 |
|
| 122 |
case Image::Format::RG11: |
| 123 |
m_data.m_u32GlInternalFormat = (unsigned int)InternalFormat::ETC2_RG11; |
| 124 |
m_data.m_u32GlBaseInternalFormat = (unsigned int)BaseInternalFormat::ETC2_RG11; |
| 125 |
break; |
| 126 |
|
| 127 |
case Image::Format::SIGNED_RG11: |
| 128 |
m_data.m_u32GlInternalFormat = (unsigned int)InternalFormat::ETC2_SIGNED_RG11; |
| 129 |
m_data.m_u32GlBaseInternalFormat = (unsigned int)BaseInternalFormat::ETC2_RG11; |
| 130 |
break; |
| 131 |
|
| 132 |
default: |
| 133 |
m_data.m_u32GlInternalFormat = (unsigned int)InternalFormat::ETC1_RGB8; |
| 134 |
m_data.m_u32GlBaseInternalFormat = (unsigned int)BaseInternalFormat::ETC1_RGB8; |
| 135 |
break; |
| 136 |
} |
| 137 |
|
| 138 |
m_data.m_u32PixelWidth = 0; |
| 139 |
m_data.m_u32PixelHeight = 0; |
| 140 |
m_data.m_u32PixelDepth = 0; |
| 141 |
m_data.m_u32NumberOfArrayElements = 0; |
| 142 |
m_data.m_u32NumberOfFaces = 0; |
| 143 |
m_data.m_u32BytesOfKeyValueData = 0; |
| 144 |
|
| 145 |
m_pkeyvaluepair = nullptr; |
| 146 |
|
| 147 |
m_u32Images = 0; |
| 148 |
m_u32KeyValuePairs = 0; |
| 149 |
|
| 150 |
m_data.m_u32PixelWidth = m_pfile->GetSourceWidth(); |
| 151 |
m_data.m_u32PixelHeight = m_pfile->GetSourceHeight(); |
| 152 |
m_data.m_u32PixelDepth = 0; |
| 153 |
m_data.m_u32NumberOfArrayElements = 0; |
| 154 |
m_data.m_u32NumberOfFaces = 1; |
| 155 |
m_data.m_u32NumberOfMipmapLevels = m_pfile->GetNumMipmaps(); |
| 156 |
|
| 157 |
} |
| 158 |
|
| 159 |
// ---------------------------------------------------------------------------------------------------- |
| 160 |
// |
| 161 |
void FileHeader_Ktx::Write(FILE *a_pfile) |
| 162 |
{ |
| 163 |
size_t szBytesWritten; |
| 164 |
|
| 165 |
// Write header |
| 166 |
szBytesWritten = fwrite(&m_data, 1, sizeof(Data), a_pfile); |
| 167 |
assert(szBytesWritten == sizeof(Data)); |
| 168 |
|
| 169 |
// Write KeyAndValuePairs |
| 170 |
if (m_u32KeyValuePairs) |
| 171 |
{ |
| 172 |
fwrite(m_pkeyvaluepair, m_pkeyvaluepair->u32KeyAndValueByteSize, 1, a_pfile); |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
// ---------------------------------------------------------------------------------------------------- |
| 177 |
// |
| 178 |
FileHeader_Ktx::Data *FileHeader_Ktx::GetData() |
| 179 |
{ |
| 180 |
return &m_data; |
| 181 |
} |
| 182 |
|
| 183 |
// ---------------------------------------------------------------------------------------------------- |
| 184 |
// |
| 185 |
} // namespace Etc |