summaryrefslogtreecommitdiff
path: root/thirdparty/etc2comp/EtcFileHeader.cpp
blob: f02fcab0114624b9866562128a9b9ed512a33238 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*
 * Copyright 2015 The Etc2Comp Authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "EtcFileHeader.h"

#include "EtcBlock4x4EncodingBits.h"

#include <assert.h>

namespace Etc
{

	// ----------------------------------------------------------------------------------------------------
	//
	FileHeader_Pkm::FileHeader_Pkm(File *a_pfile)
	{
		m_pfile = a_pfile;

		static const char s_acMagicNumberData[4] = { 'P', 'K', 'M', ' ' };
		static const char s_acVersionData[2] = { '1', '0' };

		for (unsigned int ui = 0; ui < sizeof(s_acMagicNumberData); ui++)
		{
			m_data.m_acMagicNumber[ui] = s_acMagicNumberData[ui];
		}

		for (unsigned int ui = 0; ui < sizeof(s_acVersionData); ui++)
		{
			m_data.m_acVersion[ui] = s_acVersionData[ui];
		}

		m_data.m_ucDataType_msb = 0;        // ETC1_RGB_NO_MIPMAPS
		m_data.m_ucDataType_lsb = 0;

		m_data.m_ucOriginalWidth_msb = (unsigned char)(m_pfile->GetSourceWidth() >> 8);
		m_data.m_ucOriginalWidth_lsb = m_pfile->GetSourceWidth() & 0xFF;
		m_data.m_ucOriginalHeight_msb = (unsigned char)(m_pfile->GetSourceHeight() >> 8);
		m_data.m_ucOriginalHeight_lsb = m_pfile->GetSourceHeight() & 0xFF;

		m_data.m_ucExtendedWidth_msb = (unsigned char)(m_pfile->GetExtendedWidth() >> 8);
		m_data.m_ucExtendedWidth_lsb = m_pfile->GetExtendedWidth() & 0xFF;
		m_data.m_ucExtendedHeight_msb = (unsigned char)(m_pfile->GetExtendedHeight() >> 8);
		m_data.m_ucExtendedHeight_lsb = m_pfile->GetExtendedHeight() & 0xFF;

	}

	// ----------------------------------------------------------------------------------------------------
	//
	void FileHeader_Pkm::Write(FILE *a_pfile)
	{

		fwrite(&m_data, sizeof(Data), 1, a_pfile);

	}

	// ----------------------------------------------------------------------------------------------------
	//
	FileHeader_Ktx::FileHeader_Ktx(File *a_pfile)
	{
		m_pfile = a_pfile;

		static const uint8_t s_au8Itentfier[12] =
		{ 
			0xAB, 0x4B, 0x54, 0x58, // first four bytes of Byte[12] identifier
			0x20, 0x31, 0x31, 0xBB, // next four bytes of Byte[12] identifier
			0x0D, 0x0A, 0x1A, 0x0A  // final four bytes of Byte[12] identifier
		};

		for (unsigned int ui = 0; ui < sizeof(s_au8Itentfier); ui++)
		{
			m_data.m_au8Identifier[ui] = s_au8Itentfier[ui];
		}

		m_data.m_u32Endianness				= 0x04030201;
		m_data.m_u32GlType					= 0;
		m_data.m_u32GlTypeSize				= 1;
		m_data.m_u32GlFormat				= 0;

		switch (m_pfile->GetImageFormat())
		{
		case Image::Format::RGB8:
		case Image::Format::SRGB8:
			m_data.m_u32GlInternalFormat = (unsigned int)InternalFormat::ETC2_RGB8;
			m_data.m_u32GlBaseInternalFormat = (unsigned int)BaseInternalFormat::ETC2_RGB8;
			break;

		case Image::Format::RGBA8:
		case Image::Format::SRGBA8:
			m_data.m_u32GlInternalFormat = (unsigned int)InternalFormat::ETC2_RGBA8;
			m_data.m_u32GlBaseInternalFormat = (unsigned int)BaseInternalFormat::ETC2_RGBA8;
			break;

		case Image::Format::RGB8A1:
		case Image::Format::SRGB8A1:
			m_data.m_u32GlInternalFormat = (unsigned int)InternalFormat::ETC2_RGB8A1;
			m_data.m_u32GlBaseInternalFormat = (unsigned int)BaseInternalFormat::ETC2_RGB8A1;
			break;
		
		case Image::Format::R11:
			m_data.m_u32GlInternalFormat = (unsigned int)InternalFormat::ETC2_R11;
			m_data.m_u32GlBaseInternalFormat = (unsigned int)BaseInternalFormat::ETC2_R11;
			break;

		case Image::Format::SIGNED_R11:
			m_data.m_u32GlInternalFormat = (unsigned int)InternalFormat::ETC2_SIGNED_R11;
			m_data.m_u32GlBaseInternalFormat = (unsigned int)BaseInternalFormat::ETC2_R11;
			break;
		
		case Image::Format::RG11:
			m_data.m_u32GlInternalFormat = (unsigned int)InternalFormat::ETC2_RG11;
			m_data.m_u32GlBaseInternalFormat = (unsigned int)BaseInternalFormat::ETC2_RG11;
			break;

		case Image::Format::SIGNED_RG11:
			m_data.m_u32GlInternalFormat = (unsigned int)InternalFormat::ETC2_SIGNED_RG11;
			m_data.m_u32GlBaseInternalFormat = (unsigned int)BaseInternalFormat::ETC2_RG11;
			break;

		default:
			m_data.m_u32GlInternalFormat = (unsigned int)InternalFormat::ETC1_RGB8;
			m_data.m_u32GlBaseInternalFormat = (unsigned int)BaseInternalFormat::ETC1_RGB8;
			break;
		}

		m_data.m_u32PixelWidth				= 0;
		m_data.m_u32PixelHeight				= 0;
		m_data.m_u32PixelDepth				= 0;
		m_data.m_u32NumberOfArrayElements	= 0;
		m_data.m_u32NumberOfFaces			= 0;
		m_data.m_u32BytesOfKeyValueData		= 0;

		m_pkeyvaluepair = nullptr;

		m_u32Images = 0;
		m_u32KeyValuePairs = 0;

		m_data.m_u32PixelWidth = m_pfile->GetSourceWidth();
		m_data.m_u32PixelHeight = m_pfile->GetSourceHeight();
		m_data.m_u32PixelDepth = 0;
		m_data.m_u32NumberOfArrayElements = 0;
		m_data.m_u32NumberOfFaces = 1;
		m_data.m_u32NumberOfMipmapLevels = m_pfile->GetNumMipmaps();

	}

	// ----------------------------------------------------------------------------------------------------
	//
	void FileHeader_Ktx::Write(FILE *a_pfile)
	{
		size_t szBytesWritten;

		// Write header
		szBytesWritten = fwrite(&m_data, 1, sizeof(Data), a_pfile);
		assert(szBytesWritten == sizeof(Data));

		// Write KeyAndValuePairs
		if (m_u32KeyValuePairs)
		{
			fwrite(m_pkeyvaluepair, m_pkeyvaluepair->u32KeyAndValueByteSize, 1, a_pfile);
		}
	}

	// ----------------------------------------------------------------------------------------------------
	//
	FileHeader_Ktx::Data *FileHeader_Ktx::GetData()
	{
		return &m_data;
	}

	// ----------------------------------------------------------------------------------------------------
	//
} // namespace Etc