]> git.rm.cloudns.org Git - xonotic/netradiant.git/commitdiff
Add grayscale RLE TGA support 14/head
authorSiPlus <hwguy.siplus@gmail.com>
Thu, 2 Jul 2015 12:20:51 +0000 (15:20 +0300)
committerSiPlus <hwguy.siplus@gmail.com>
Thu, 2 Jul 2015 12:20:51 +0000 (15:20 +0300)
plugins/image/tga.cpp

index bc498a8fff6972afae3c6e4597492d68fb617c49..4fa8731f5a3b7600ead4d9e16982b0002567bb58 100644 (file)
@@ -165,6 +165,42 @@ inline TargaPacketSize targa_packet_size( const TargaPacket& packet ){
 }
 
 
+class TargaDecodeGrayPixelRLE
+{
+TargaPacketSize m_packetSize;
+RGBAPixel m_pixel;
+TargaPacket m_packet;
+public:
+TargaDecodeGrayPixelRLE() : m_packetSize( 0 ){
+}
+void operator()( PointerInputStream& istream, RGBAPixel& pixel ){
+       if ( m_packetSize == 0 ) {
+               targa_packet_read_istream( m_packet, istream );
+               m_packetSize = targa_packet_size( m_packet );
+
+               if ( targa_packet_is_rle( m_packet ) ) {
+                       istream_read_gray( istream, m_pixel );
+               }
+       }
+
+       if ( targa_packet_is_rle( m_packet ) ) {
+               pixel = m_pixel;
+       }
+       else
+       {
+               istream_read_gray( istream, pixel );
+       }
+
+       --m_packetSize;
+}
+};
+
+template<typename Flip>
+void targa_decode_rle_grayscale( PointerInputStream& istream, RGBAImage& image, const Flip& flip ){
+       TargaDecodeGrayPixelRLE decode;
+       image_decode( istream, decode, image, flip );
+}
+
 class TargaDecodeRGBPixelRLE
 {
 TargaPacketSize m_packetSize;
@@ -305,9 +341,12 @@ Image* Targa_decodeImageData( const TargaHeader& targa_header, PointerInputStrea
                        return 0;
                }
        }
-       else if ( targa_header.image_type == 10 ) {
+       else if ( targa_header.image_type == 10 || targa_header.image_type == 11 ) {
                switch ( targa_header.pixel_size )
                {
+               case 8:
+                       targa_decode_rle_grayscale( istream, *image, flip );
+                       break;
                case 24:
                        targa_decode_rle_rgb( istream, *image, flip );
                        break;
@@ -333,9 +372,9 @@ Image* LoadTGABuff( const byte* buffer ){
 
        targa_header_read_istream( targa_header, istream );
 
-       if ( targa_header.image_type != 2 && targa_header.image_type != 10 && targa_header.image_type != 3 ) {
+       if ( targa_header.image_type != 2 && targa_header.image_type != 10 && targa_header.image_type != 3 && targa_header.image_type != 11 ) {
                globalErrorStream() << "LoadTGA: TGA type " << targa_header.image_type << " not supported\n";
-               globalErrorStream() << "LoadTGA: Only type 2 (RGB), 3 (gray), and 10 (RGB) TGA images supported\n";
+               globalErrorStream() << "LoadTGA: Only type 2 (RGB), 3 (gray), 10 (RGB), and 11 (gray) TGA images supported\n";
                return 0;
        }
 
@@ -344,9 +383,9 @@ Image* LoadTGABuff( const byte* buffer ){
                return 0;
        }
 
-       if ( ( targa_header.pixel_size != 32 && targa_header.pixel_size != 24 )
-                && targa_header.image_type != 3 ) {
-               globalErrorStream() << "LoadTGA: Only 32 or 24 bit images supported\n";
+       if ( ( ( targa_header.image_type == 2 || targa_header.image_type == 10 ) && targa_header.pixel_size != 32 && targa_header.pixel_size != 24 ) ||
+            ( ( targa_header.image_type == 3 || targa_header.image_type == 11 ) && targa_header.pixel_size != 8 ) ) {
+               globalErrorStream() << "LoadTGA: Only 32, 24 or 8 bit images supported\n";
                return 0;
        }