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;
54 class KTX_Decoder_A8 : public KTX_Decoder
57 virtual void Decode( PointerInputStream& istream, byte* out ){
58 out[0] = out[1] = out[2] = 0;
59 out[3] = istream_read_byte( istream );
61 virtual unsigned int GetPixelSize(){
66 class KTX_Decoder_RGB8 : public KTX_Decoder
69 virtual void Decode( PointerInputStream& istream, byte* out ){
70 istream.read( out, 3 );
73 virtual unsigned int GetPixelSize(){
78 class KTX_Decoder_RGBA8 : public KTX_Decoder
81 virtual void Decode( PointerInputStream& istream, byte* out ){
82 istream.read( out, 4 );
84 virtual unsigned int GetPixelSize(){
89 class KTX_Decoder_L8 : public KTX_Decoder
92 virtual void Decode( PointerInputStream& istream, byte* out ){
93 byte l = istream_read_byte( istream );
94 out[0] = out[1] = out[2] = l;
97 virtual unsigned int GetPixelSize(){
102 class KTX_Decoder_LA8 : public KTX_Decoder
105 virtual void Decode( PointerInputStream& istream, byte* out ){
107 istream.read( la, 2 );
108 out[0] = out[1] = out[2] = la[0];
111 virtual unsigned int GetPixelSize(){
116 class KTX_Decoder_BGR8 : public KTX_Decoder
119 virtual void Decode( PointerInputStream& istream, byte* out ){
121 istream.read( bgr, 3 );
127 virtual unsigned int GetPixelSize(){
132 class KTX_Decoder_BGRA8 : public KTX_Decoder
135 virtual void Decode( PointerInputStream& istream, byte* out ){
137 istream.read( bgra, 4 );
143 virtual unsigned int GetPixelSize(){
148 class KTX_Decoder_RGBA4 : public KTX_Decoder
153 KTX_Decoder_RGBA4( bool bigEndian ) : m_bigEndian( bigEndian ){}
154 virtual void Decode( PointerInputStream& istream, byte* out ){
157 rgba = istream_read_uint16_be( istream );
160 rgba = istream_read_uint16_le( istream );
162 int r = ( rgba >> 12 ) & 0xf;
163 int g = ( rgba >> 8 ) & 0xf;
164 int b = ( rgba >> 4 ) & 0xf;
166 out[0] = ( r << 4 ) | r;
167 out[1] = ( g << 4 ) | g;
168 out[2] = ( b << 4 ) | b;
169 out[3] = ( a << 4 ) | a;
171 virtual unsigned int GetPixelSize(){
176 class KTX_Decoder_RGBA5 : public KTX_Decoder
181 KTX_Decoder_RGBA5( bool bigEndian ) : m_bigEndian( bigEndian ){}
182 virtual void Decode( PointerInputStream& istream, byte* out ){
185 rgba = istream_read_uint16_be( istream );
188 rgba = istream_read_uint16_le( istream );
190 int r = ( rgba >> 11 ) & 0x1f;
191 int g = ( rgba >> 6 ) & 0x1f;
192 int b = ( rgba >> 1 ) & 0x1f;
193 out[0] = ( r << 3 ) | ( r >> 2 );
194 out[1] = ( g << 3 ) | ( g >> 2 );
195 out[2] = ( b << 3 ) | ( b >> 2 );
196 out[3] = ( rgba & 1 ) * 255;
198 virtual unsigned int GetPixelSize(){
203 class KTX_Decoder_RGB5 : public KTX_Decoder
208 KTX_Decoder_RGB5( bool bigEndian ) : m_bigEndian( bigEndian ){}
209 virtual void Decode( PointerInputStream& istream, byte* out ){
212 rgb = istream_read_uint16_be( istream );
215 rgb = istream_read_uint16_le( istream );
217 int r = ( rgb >> 11 ) & 0x1f;
218 int g = ( rgb >> 5 ) & 0x3f;
220 out[0] = ( r << 3 ) | ( r >> 2 );
221 out[1] = ( g << 2 ) | ( g >> 4 );
222 out[2] = ( b << 3 ) | ( b >> 2 );
225 virtual unsigned int GetPixelSize(){
230 static void KTX_DecodeETC1( PointerInputStream& istream, Image& image ){
231 unsigned int width = image.getWidth(), height = image.getHeight();
232 unsigned int stride = width * 4;
233 byte* pixbuf = image.getRGBAPixels();
234 byte etc[8], rgba[64];
236 for ( unsigned int y = 0; y < height; y += 4, pixbuf += stride * 4 )
238 unsigned int blockrows = height - y;
239 if ( blockrows > 4 ) {
244 for ( unsigned int x = 0; x < width; x += 4, p += 16 )
246 istream.read( etc, 8 );
247 ETC_DecodeETC1Block( etc, rgba, qtrue );
249 unsigned int blockrowsize = width - x;
250 if ( blockrowsize > 4 ) {
254 for ( unsigned int blockrow = 0; blockrow < blockrows; blockrow++ )
256 memcpy( p + blockrow * stride, rgba + blockrow * 16, blockrowsize );
262 Image* LoadKTXBuff( PointerInputStream& istream ){
264 istream.read( identifier, 12 );
265 if ( memcmp( identifier, "\xABKTX 11\xBB\r\n\x1A\n", 12 ) ) {
266 globalErrorStream() << "LoadKTX: Image has the wrong identifier\n";
270 bool bigEndian = ( istream_read_uint32_le( istream ) == 0x01020304 );
274 type = istream_read_uint32_be( istream );
277 type = istream_read_uint32_le( istream );
280 // For compressed textures, the format is in glInternalFormat.
281 // For uncompressed textures, it's in glBaseInternalFormat.
282 istream.seek( ( type ? 3 : 2 ) * sizeof( uint32_t ) );
285 format = istream_read_uint32_be( istream );
288 format = istream_read_uint32_le( istream );
291 istream.seek( sizeof( uint32_t ) );
294 unsigned int width, height;
296 width = istream_read_uint32_be( istream );
297 height = istream_read_uint32_be( istream );
300 width = istream_read_uint32_le( istream );
301 height = istream_read_uint32_le( istream );
304 globalErrorStream() << "LoadKTX: Image has zero width\n";
311 // Skip the key/values and load the first 2D image in the texture.
312 // Since KTXorientation is only a hint and has no effect on the texture data and coordinates, it must be ignored.
313 istream.seek( 4 * sizeof( uint32_t ) );
314 unsigned int bytesOfKeyValueData;
316 bytesOfKeyValueData = istream_read_uint32_be( istream );
319 bytesOfKeyValueData = istream_read_uint32_le( istream );
321 istream.seek( bytesOfKeyValueData + sizeof( uint32_t ) );
323 RGBAImage* image = new RGBAImage( width, height );
326 KTX_Decoder* decoder = NULL;
329 case KTX_TYPE_UNSIGNED_BYTE:
332 case KTX_FORMAT_ALPHA:
333 decoder = new KTX_Decoder_A8();
336 decoder = new KTX_Decoder_RGB8();
338 case KTX_FORMAT_RGBA:
339 decoder = new KTX_Decoder_RGBA8();
341 case KTX_FORMAT_LUMINANCE:
342 decoder = new KTX_Decoder_L8();
344 case KTX_FORMAT_LUMINANCE_ALPHA:
345 decoder = new KTX_Decoder_LA8();
348 decoder = new KTX_Decoder_BGR8();
350 case KTX_FORMAT_BGRA:
351 decoder = new KTX_Decoder_BGRA8();
355 case KTX_TYPE_UNSIGNED_SHORT_4_4_4_4:
356 if ( format == KTX_FORMAT_RGBA ) {
357 decoder = new KTX_Decoder_RGBA4( bigEndian );
360 case KTX_TYPE_UNSIGNED_SHORT_5_5_5_1:
361 if ( format == KTX_FORMAT_RGBA ) {
362 decoder = new KTX_Decoder_RGBA5( bigEndian );
365 case KTX_TYPE_UNSIGNED_SHORT_5_6_5:
366 if ( format == KTX_FORMAT_RGB ) {
367 decoder = new KTX_Decoder_RGB5( bigEndian );
373 globalErrorStream() << "LoadKTX: Image has an unsupported pixel type " << type << " or format " << format << "\n";
378 unsigned int inRowLength = width * decoder->GetPixelSize();
379 unsigned int inPadding = ( ( inRowLength + 3 ) & ~3 ) - inRowLength;
380 byte* out = image->getRGBAPixels();
381 for ( unsigned int y = 0; y < height; y++ )
383 for ( unsigned int x = 0; x < width; x++, out += 4 )
385 decoder->Decode( istream, out );
389 istream.seek( inPadding );
398 case KTX_FORMAT_ETC1_RGB8:
399 KTX_DecodeETC1( istream, *image );
402 globalErrorStream() << "LoadKTX: Image has an unsupported compressed format " << format << "\n";
411 Image* LoadKTX( ArchiveFile& file ){
412 ScopedArchiveBuffer buffer( file );
413 PointerInputStream istream( buffer.buffer );
414 return LoadKTXBuff( istream );