tga.cpp

00001 /* This file is part of the KDE project
00002    Copyright (C) 2003 Dominik Seichter <domseichter@web.de>
00003    Copyright (C) 2004 Ignacio Castaņo <castano@ludicon.com>
00004 
00005    This program is free software; you can redistribute it and/or
00006    modify it under the terms of the Lesser GNU General Public
00007    License as published by the Free Software Foundation; either
00008    version 2 of the License, or (at your option) any later version.
00009 */
00010 
00011 /* this code supports:
00012  * reading:
00013  *     uncompressed and run length encoded indexed, grey and color tga files.
00014  *     image types 1, 2, 3, 9, 10 and 11.
00015  *     only RGB color maps with no more than 256 colors.
00016  *     pixel formats 8, 15, 24 and 32.
00017  * writing:
00018  *     uncompressed true color tga files
00019  */
00020 
00021 #include "tga.h"
00022 
00023 #include <assert.h>
00024 
00025 #include <qimage.h>
00026 #include <qdatastream.h>
00027 
00028 #include <kdebug.h>
00029 
00030 typedef Q_UINT32 uint;
00031 typedef Q_UINT16 ushort;
00032 typedef Q_UINT8 uchar;
00033 
00034 namespace { // Private.
00035 
00036     // Header format of saved files.
00037     uchar targaMagic[12] = { 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
00038 
00039     enum TGAType {
00040         TGA_TYPE_INDEXED        = 1,
00041         TGA_TYPE_RGB            = 2,
00042         TGA_TYPE_GREY           = 3,
00043         TGA_TYPE_RLE_INDEXED    = 9,
00044         TGA_TYPE_RLE_RGB        = 10,
00045         TGA_TYPE_RLE_GREY       = 11
00046     };
00047 
00048 #define TGA_INTERLEAVE_MASK 0xc0
00049 #define TGA_INTERLEAVE_NONE 0x00
00050 #define TGA_INTERLEAVE_2WAY 0x40
00051 #define TGA_INTERLEAVE_4WAY 0x80
00052 
00053 #define TGA_ORIGIN_MASK     0x30
00054 #define TGA_ORIGIN_LEFT     0x00
00055 #define TGA_ORIGIN_RIGHT    0x10
00056 #define TGA_ORIGIN_LOWER    0x00
00057 #define TGA_ORIGIN_UPPER    0x20
00058 
00060     struct TgaHeader {
00061         uchar id_length;
00062         uchar colormap_type;
00063         uchar image_type;
00064         ushort colormap_index;
00065         ushort colormap_length;
00066         uchar colormap_size;
00067         ushort x_origin;
00068         ushort y_origin;
00069         ushort width;
00070         ushort height;
00071         uchar pixel_size;
00072         uchar flags;
00073     
00074         enum { SIZE = 18 }; // const static int SIZE = 18;
00075     };
00076 
00077     static QDataStream & operator>> ( QDataStream & s, TgaHeader & head )
00078     {
00079         s >> head.id_length;
00080         s >> head.colormap_type;
00081         s >> head.image_type;
00082         s >> head.colormap_index;
00083         s >> head.colormap_length;
00084         s >> head.colormap_size;
00085         s >> head.x_origin;
00086         s >> head.y_origin;
00087         s >> head.width;
00088         s >> head.height;
00089         s >> head.pixel_size;
00090         s >> head.flags;
00091         return s;
00092     }
00093 
00094     static bool IsSupported( const TgaHeader & head )
00095     {
00096         if( head.image_type != TGA_TYPE_INDEXED &&
00097             head.image_type != TGA_TYPE_RGB &&
00098             head.image_type != TGA_TYPE_GREY &&
00099             head.image_type != TGA_TYPE_RLE_INDEXED &&
00100             head.image_type != TGA_TYPE_RLE_RGB &&
00101             head.image_type != TGA_TYPE_RLE_GREY )
00102         {
00103             return false;
00104         }
00105         if( head.image_type == TGA_TYPE_INDEXED ||
00106             head.image_type == TGA_TYPE_RLE_INDEXED )
00107         {
00108             if( head.colormap_length > 256 || head.colormap_size != 24 )
00109             {
00110                 return false;
00111             }
00112         }
00113         if( head.width == 0 || head.height == 0 )
00114         {
00115             return false;
00116         }
00117         if( head.pixel_size != 8 && head.pixel_size != 16 &&
00118             head.pixel_size != 24 && head.pixel_size != 32 )
00119         {
00120             return false;
00121         }
00122         return true;
00123     }
00124 
00125     struct Color555 {
00126         ushort b : 5;
00127         ushort g : 5;
00128         ushort r : 5;
00129     };
00130     
00131     struct TgaHeaderInfo {
00132         bool rle;
00133         bool pal;
00134         bool rgb;
00135         bool grey;
00136         bool supported;
00137     
00138         TgaHeaderInfo( const TgaHeader & tga ) : rle(false), pal(false), rgb(false), grey(false), supported(true)
00139         {
00140             switch( tga.image_type ) {
00141                 case TGA_TYPE_RLE_INDEXED:
00142                     rle = true;
00143                     // no break is intended!
00144                 case TGA_TYPE_INDEXED:
00145                     if( tga.colormap_type!=1 || tga.colormap_size!=24 || tga.colormap_length>256 ) {
00146                         supported = false;
00147                     }
00148                     pal = true;
00149                     break;
00150         
00151                 case TGA_TYPE_RLE_RGB:
00152                     rle = true;
00153                     // no break is intended!
00154                 case TGA_TYPE_RGB:
00155                     rgb = true;
00156                     break;
00157         
00158                 case TGA_TYPE_RLE_GREY:
00159                     rle = true;
00160                     // no break is intended!
00161                 case TGA_TYPE_GREY:
00162                     grey = true;
00163                     break;
00164         
00165                 default:
00166                     // Error, unknown image type.
00167                     supported = false;
00168             }
00169         }
00170     };
00171     
00172 
00173 
00174     static bool LoadTGA( QDataStream & s, const TgaHeader & tga, QImage &img )
00175     {
00176         // Create image.
00177         if( !img.create( tga.width, tga.height, 32 )) {
00178             return false;
00179         }
00180 
00181         TgaHeaderInfo info(tga);
00182         if( !info.supported ) {
00183             // File not supported.
00184             kdDebug(399) << "This TGA file is not supported." << endl;
00185             return false;
00186         }
00187         
00188                 // Bits 0-3 are the numbers of alpha bits (can be zero!)
00189                 const int numAlphaBits = tga.flags & 0xf;
00190                 // However alpha exists only in the 32 bit format.
00191         if( ( tga.pixel_size == 32 ) && ( tga.flags & 0xf ) ) {
00192             img.setAlphaBuffer( true );
00193         }
00194 
00195         uint pixel_size = (tga.pixel_size/8);
00196         uint size = tga.width * tga.height * pixel_size;
00197 
00198         
00199         // Read palette.
00200         char palette[768];
00201         if( info.pal ) {
00202             // @todo Support palettes in other formats!
00203             s.readRawBytes( palette, 3 * tga.colormap_length );
00204         }
00205 
00206         // Allocate image.
00207         uchar * const image = new uchar[size];
00208 
00209         if( info.rle ) {
00210             // Decode image.
00211             char * dst = (char *)image;
00212             int num = size;
00213     
00214             while (num > 0) {
00215                 // Get packet header.
00216                 uchar c; 
00217                 s >> c;
00218     
00219                 uint count = (c & 0x7f) + 1;
00220                 num -= count * pixel_size;
00221     
00222                 if (c & 0x80) {
00223                     // RLE pixels.
00224                                         assert(pixel_size <= 8);
00225                     char pixel[8];
00226                     s.readRawBytes( pixel, pixel_size );
00227                     do {
00228                         memcpy(dst, pixel, pixel_size);
00229                         dst += pixel_size;
00230                     } while (--count);
00231                 }
00232                 else {
00233                     // Raw pixels.
00234                     count *= pixel_size;
00235                     s.readRawBytes( dst, count );
00236                     dst += count;
00237                 }
00238             }
00239         }
00240         else {
00241             // Read raw image.
00242             s.readRawBytes( (char *)image, size );
00243         }
00244 
00245         // Convert image to internal format.                
00246         int y_start, y_step, y_end;
00247         if( tga.flags & TGA_ORIGIN_UPPER ) {
00248             y_start = 0;
00249             y_step = 1;
00250             y_end = tga.height;
00251         }
00252         else {
00253             y_start = tga.height - 1;
00254             y_step = -1;
00255             y_end = -1;
00256         }
00257 
00258         uchar * src = image;
00259                
00260         for( int y = y_start; y != y_end; y += y_step ) {
00261             QRgb * scanline = (QRgb *) img.scanLine( y );
00262         
00263             if( info.pal ) {
00264                 // Paletted.
00265                 for( int x = 0; x < tga.width; x++ ) {
00266                     uchar idx = *src++;
00267                     scanline[x] = qRgb( palette[3*idx+2], palette[3*idx+1], palette[3*idx+0] );
00268                 }
00269             }
00270             else if( info.grey ) {
00271                 // Greyscale.
00272                 for( int x = 0; x < tga.width; x++ ) {
00273                     scanline[x] = qRgb( *src, *src, *src );
00274                     src++;
00275                 }
00276             }
00277             else {
00278                 // True Color.
00279                 if( tga.pixel_size == 16 ) {
00280                     for( int x = 0; x < tga.width; x++ ) {
00281                         Color555 c = *reinterpret_cast<Color555 *>(src);
00282                         scanline[x] = qRgb( (c.r << 3) | (c.r >> 2), (c.g << 3) | (c.g >> 2), (c.b << 3) | (c.b >> 2) );
00283                         src += 2;
00284                     }
00285                 }
00286                 else if( tga.pixel_size == 24 ) {
00287                     for( int x = 0; x < tga.width; x++ ) {
00288                         scanline[x] = qRgb( src[2], src[1], src[0] );
00289                         src += 3;
00290                     }
00291                 }
00292                 else if( tga.pixel_size == 32 ) {
00293                     for( int x = 0; x < tga.width; x++ ) {
00294                                                 // ### TODO: verify with images having really some alpha data
00295                                                 const uchar alpha = ( src[3] << ( 8 - numAlphaBits ) );
00296                         scanline[x] = qRgba( src[2], src[1], src[0], alpha );
00297                         src += 4;
00298                     }
00299                 }
00300             }
00301         }
00302 
00303         // Free image.
00304         delete [] image;
00305         
00306         return true;
00307     }
00308     
00309 } // namespace
00310 
00311 
00312 KDE_EXPORT void kimgio_tga_read( QImageIO *io )
00313 {
00314     //kdDebug(399) << "Loading TGA file!" << endl;
00315     
00316     QDataStream s( io->ioDevice() );
00317     s.setByteOrder( QDataStream::LittleEndian );
00318 
00319 
00320     // Read image header.
00321     TgaHeader tga;
00322     s >> tga;
00323     s.device()->at( TgaHeader::SIZE + tga.id_length );
00324 
00325     // Check image file format.
00326     if( s.atEnd() ) {
00327         kdDebug(399) << "This TGA file is not valid." << endl;
00328         io->setImage( 0 );
00329         io->setStatus( -1 );
00330         return;
00331     }
00332 
00333     // Check supported file types.
00334     if( !IsSupported(tga) ) {
00335         kdDebug(399) << "This TGA file is not supported." << endl;
00336         io->setImage( 0 );
00337         io->setStatus( -1 );
00338         return;
00339     }
00340                 
00341 
00342     QImage img;
00343     bool result = LoadTGA(s, tga, img);
00344         
00345     if( result == false ) {
00346         kdDebug(399) << "Error loading TGA file." << endl;
00347         io->setImage( 0 );
00348         io->setStatus( -1 );
00349         return;
00350     }
00351 
00352 
00353     io->setImage( img );
00354     io->setStatus( 0 );
00355 }
00356 
00357 
00358 KDE_EXPORT void kimgio_tga_write( QImageIO *io )
00359 {
00360     QDataStream s( io->ioDevice() );
00361     s.setByteOrder( QDataStream::LittleEndian );
00362 
00363     const QImage img = io->image();
00364     const bool hasAlpha = img.hasAlphaBuffer();
00365     for( int i = 0; i < 12; i++ )
00366         s << targaMagic[i];
00367 
00368     // write header
00369     s << Q_UINT16( img.width() ); // width
00370     s << Q_UINT16( img.height() ); // height
00371     s << Q_UINT8( hasAlpha ? 32 : 24 ); // depth (24 bit RGB + 8 bit alpha)
00372     s << Q_UINT8( hasAlpha ? 0x24 : 0x20 ); // top left image (0x20) + 8 bit alpha (0x4)
00373 
00374     for( int y = 0; y < img.height(); y++ )
00375         for( int x = 0; x < img.width(); x++ ) {
00376             const QRgb color = img.pixel( x, y );
00377             s << Q_UINT8( qBlue( color ) );
00378             s << Q_UINT8( qGreen( color ) );
00379             s << Q_UINT8( qRed( color ) );
00380             if( hasAlpha )
00381                 s << Q_UINT8( qAlpha( color ) );
00382         }
00383 
00384     io->setStatus( 0 );
00385 }
00386  
KDE Home | KDE Accessibility Home | Description of Access Keys