2 Copyright (C) 2015, SiPlus, Chasseur de bots.
5 This file is part of GtkRadiant.
7 GtkRadiant is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 GtkRadiant is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GtkRadiant; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
26 #include "bytestreamutils.h"
28 #include "ifilesystem.h"
32 #define KTX_TYPE_UNSIGNED_BYTE 0x1401
33 #define KTX_TYPE_UNSIGNED_SHORT_4_4_4_4 0x8033
34 #define KTX_TYPE_UNSIGNED_SHORT_5_5_5_1 0x8034
35 #define KTX_TYPE_UNSIGNED_SHORT_5_6_5 0x8363
37 #define KTX_FORMAT_ALPHA 0x1906
38 #define KTX_FORMAT_RGB 0x1907
39 #define KTX_FORMAT_RGBA 0x1908
40 #define KTX_FORMAT_LUMINANCE 0x1909
41 #define KTX_FORMAT_LUMINANCE_ALPHA 0x190A
42 #define KTX_FORMAT_BGR 0x80E0
43 #define KTX_FORMAT_BGRA 0x80E1
45 #define KTX_FORMAT_ETC1_RGB8 0x8D64
50 virtual void Decode( PointerInputStream& istream, byte* out ) = 0;
51 virtual unsigned int GetPixelSize() = 0;
56 class KTX_Decoder_A8 : public KTX_Decoder
59 virtual void Decode( PointerInputStream& istream, byte* out ){
60 out[0] = out[1] = out[2] = 0;
61 out[3] = istream_read_byte( istream );
63 virtual unsigned int GetPixelSize(){
68 class KTX_Decoder_RGB8 : public KTX_Decoder
71 virtual void Decode( PointerInputStream& istream, byte* out ){
72 istream.read( out, 3 );
75 virtual unsigned int GetPixelSize(){
80 class KTX_Decoder_RGBA8 : public KTX_Decoder
83 virtual void Decode( PointerInputStream& istream, byte* out ){
84 istream.read( out, 4 );
86 virtual unsigned int GetPixelSize(){
91 class KTX_Decoder_L8 : public KTX_Decoder
94 virtual void Decode( PointerInputStream& istream, byte* out ){
95 byte l = istream_read_byte( istream );
96 out[0] = out[1] = out[2] = l;
99 virtual unsigned int GetPixelSize(){
104 class KTX_Decoder_LA8 : public KTX_Decoder
107 virtual void Decode( PointerInputStream& istream, byte* out ){
109 istream.read( la, 2 );
110 out[0] = out[1] = out[2] = la[0];
113 virtual unsigned int GetPixelSize(){
118 class KTX_Decoder_BGR8 : public KTX_Decoder
121 virtual void Decode( PointerInputStream& istream, byte* out ){
123 istream.read( bgr, 3 );
129 virtual unsigned int GetPixelSize(){
134 class KTX_Decoder_BGRA8 : public KTX_Decoder
137 virtual void Decode( PointerInputStream& istream, byte* out ){
139 istream.read( bgra, 4 );
145 virtual unsigned int GetPixelSize(){
150 class KTX_Decoder_RGBA4 : public KTX_Decoder
155 KTX_Decoder_RGBA4( bool bigEndian ) : m_bigEndian( bigEndian ){}
156 virtual void Decode( PointerInputStream& istream, byte* out ){
159 rgba = istream_read_uint16_be( istream );
162 rgba = istream_read_uint16_le( istream );
164 int r = ( rgba >> 12 ) & 0xf;
165 int g = ( rgba >> 8 ) & 0xf;
166 int b = ( rgba >> 4 ) & 0xf;
168 out[0] = ( r << 4 ) | r;
169 out[1] = ( g << 4 ) | g;
170 out[2] = ( b << 4 ) | b;
171 out[3] = ( a << 4 ) | a;
173 virtual unsigned int GetPixelSize(){
178 class KTX_Decoder_RGBA5 : public KTX_Decoder
183 KTX_Decoder_RGBA5( bool bigEndian ) : m_bigEndian( bigEndian ){}
184 virtual void Decode( PointerInputStream& istream, byte* out ){
187 rgba = istream_read_uint16_be( istream );
190 rgba = istream_read_uint16_le( istream );
192 int r = ( rgba >> 11 ) & 0x1f;
193 int g = ( rgba >> 6 ) & 0x1f;
194 int b = ( rgba >> 1 ) & 0x1f;
195 out[0] = ( r << 3 ) | ( r >> 2 );
196 out[1] = ( g << 3 ) | ( g >> 2 );
197 out[2] = ( b << 3 ) | ( b >> 2 );
198 out[3] = ( rgba & 1 ) * 255;
200 virtual unsigned int GetPixelSize(){
205 class KTX_Decoder_RGB5 : public KTX_Decoder
210 KTX_Decoder_RGB5( bool bigEndian ) : m_bigEndian( bigEndian ){}
211 virtual void Decode( PointerInputStream& istream, byte* out ){
214 rgb = istream_read_uint16_be( istream );
217 rgb = istream_read_uint16_le( istream );
219 int r = ( rgb >> 11 ) & 0x1f;
220 int g = ( rgb >> 5 ) & 0x3f;
222 out[0] = ( r << 3 ) | ( r >> 2 );
223 out[1] = ( g << 2 ) | ( g >> 4 );
224 out[2] = ( b << 3 ) | ( b >> 2 );
227 virtual unsigned int GetPixelSize(){
232 static void KTX_DecodeETC1( PointerInputStream& istream, Image& image ){
233 unsigned int width = image.getWidth(), height = image.getHeight();
234 unsigned int stride = width * 4;
235 byte* pixbuf = image.getRGBAPixels();
236 byte etc[8], rgba[64];
238 for ( unsigned int y = 0; y < height; y += 4, pixbuf += stride * 4 )
240 unsigned int blockrows = height - y;
241 if ( blockrows > 4 ) {
246 for ( unsigned int x = 0; x < width; x += 4, p += 16 )
248 istream.read( etc, 8 );
249 ETC_DecodeETC1Block( etc, rgba, qtrue );
251 unsigned int blockrowsize = width - x;
252 if ( blockrowsize > 4 ) {
256 for ( unsigned int blockrow = 0; blockrow < blockrows; blockrow++ )
258 memcpy( p + blockrow * stride, rgba + blockrow * 16, blockrowsize );
264 Image* LoadKTXBuff( PointerInputStream& istream ){
266 istream.read( identifier, 12 );
267 if ( memcmp( identifier, "\xABKTX 11\xBB\r\n\x1A\n", 12 ) ) {
268 globalErrorStream() << "LoadKTX: Image has the wrong identifier\n";
272 bool bigEndian = ( istream_read_uint32_le( istream ) == 0x01020304 );
276 type = istream_read_uint32_be( istream );
279 type = istream_read_uint32_le( istream );
282 // For compressed textures, the format is in glInternalFormat.
283 // For uncompressed textures, it's in glBaseInternalFormat.
284 istream.seek( ( type ? 3 : 2 ) * sizeof( uint32_t ) );
287 format = istream_read_uint32_be( istream );
290 format = istream_read_uint32_le( istream );
293 istream.seek( sizeof( uint32_t ) );
296 unsigned int width, height;
298 width = istream_read_uint32_be( istream );
299 height = istream_read_uint32_be( istream );
302 width = istream_read_uint32_le( istream );
303 height = istream_read_uint32_le( istream );
306 globalErrorStream() << "LoadKTX: Image has zero width\n";
313 // Skip the key/values and load the first 2D image in the texture.
314 // Since KTXorientation is only a hint and has no effect on the texture data and coordinates, it must be ignored.
315 istream.seek( 4 * sizeof( uint32_t ) );
316 unsigned int bytesOfKeyValueData;
318 bytesOfKeyValueData = istream_read_uint32_be( istream );
321 bytesOfKeyValueData = istream_read_uint32_le( istream );
323 istream.seek( bytesOfKeyValueData + sizeof( uint32_t ) );
325 RGBAImage* image = new RGBAImage( width, height );
328 KTX_Decoder* decoder = NULL;
331 case KTX_TYPE_UNSIGNED_BYTE:
334 case KTX_FORMAT_ALPHA:
335 decoder = new KTX_Decoder_A8();
338 decoder = new KTX_Decoder_RGB8();
340 case KTX_FORMAT_RGBA:
341 decoder = new KTX_Decoder_RGBA8();
343 case KTX_FORMAT_LUMINANCE:
344 decoder = new KTX_Decoder_L8();
346 case KTX_FORMAT_LUMINANCE_ALPHA:
347 decoder = new KTX_Decoder_LA8();
350 decoder = new KTX_Decoder_BGR8();
352 case KTX_FORMAT_BGRA:
353 decoder = new KTX_Decoder_BGRA8();
357 case KTX_TYPE_UNSIGNED_SHORT_4_4_4_4:
358 if ( format == KTX_FORMAT_RGBA ) {
359 decoder = new KTX_Decoder_RGBA4( bigEndian );
362 case KTX_TYPE_UNSIGNED_SHORT_5_5_5_1:
363 if ( format == KTX_FORMAT_RGBA ) {
364 decoder = new KTX_Decoder_RGBA5( bigEndian );
367 case KTX_TYPE_UNSIGNED_SHORT_5_6_5:
368 if ( format == KTX_FORMAT_RGB ) {
369 decoder = new KTX_Decoder_RGB5( bigEndian );
375 globalErrorStream() << "LoadKTX: Image has an unsupported pixel type " << type << " or format " << format << "\n";
380 unsigned int inRowLength = width * decoder->GetPixelSize();
381 unsigned int inPadding = ( ( inRowLength + 3 ) & ~3 ) - inRowLength;
382 byte* out = image->getRGBAPixels();
383 for ( unsigned int y = 0; y < height; y++ )
385 for ( unsigned int x = 0; x < width; x++, out += 4 )
387 decoder->Decode( istream, out );
391 istream.seek( inPadding );
400 case KTX_FORMAT_ETC1_RGB8:
401 KTX_DecodeETC1( istream, *image );
404 globalErrorStream() << "LoadKTX: Image has an unsupported compressed format " << format << "\n";
413 Image* LoadKTX( ArchiveFile& file ){
414 ScopedArchiveBuffer buffer( file );
415 PointerInputStream istream( buffer.buffer );
416 return LoadKTXBuff( istream );