iTGA.h
Go to the documentation of this file.
00001 /*
00002 imagen - Simple object-oriented imaging library
00003 Copyright (C) 2011-2012 Mark D. Procarione
00004 
00005 imagen is free software: you can redistribute it and/or
00006 modify it under the terms of the GNU Lesser General Public
00007 License as published by the Free Software Foundation, either
00008 version 3 of the License, or (at your option) any later version.
00009 
00010 imagen is distributed in the hope that it will be useful,
00011 but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00013 GNU General Public License for more details.
00014 */
00015 
00016 #ifndef __ITGA_H__
00017 #define __ITGA_H__
00018 
00019 #include "imagen.h"
00020 
00021 #ifdef IMAGEN_TGA
00022 
00023 #include "iAbstractImage.h"
00024 #include "iLinkedList.h"
00025 
00026 namespace imagen
00027 {
00029 
00063     class iTGA : public iAbstractImage
00064     {
00065         private:
00066             #pragma pack(1)
00067 
00068             struct TGA_HEADER
00069             {
00070                 TGA_HEADER();
00071                 void clear();
00072 
00073                 iBYTE IDLength;        // 00h  Size of Image ID field
00074                 iBYTE ColorMapType;    // 01h  Color map type
00075                 iBYTE ImageType;       // 02h  Image type code
00076                 iWORD CMapStart;       // 03h  Color map origin
00077                 iWORD CMapLength;      // 05h  Color map length
00078                 iBYTE CMapDepth;       // 07h  Depth of color map entries
00079                 iWORD XOffset;         // 08h  X origin of image
00080                 iWORD YOffset;         // 0Ah  Y origin of image
00081                 iWORD Width;           // 0Ch  Width of image
00082                 iWORD Height;          // 0Eh  Height of image
00083                 iBYTE PixelDepth;      // 10h  Image pixel size
00084                 iBYTE ImageDescriptor; // 11h  Image descriptor byte
00085             }; // __attribute__((packed)); // This might hurt portability, as it's exclusive to GCC
00086 
00088             struct TGA_FOOTER
00089             {
00090                 TGA_FOOTER();
00091                 void clear();
00092 
00093                 iDWORD ExtensionOffset;  // Extension Area Offset
00094                 iDWORD DeveloperOffset;  // Developer Directory Offset
00095                 iCHAR  Signature[16];    // TGA Signature
00096                 iCHAR  ReservedChar;     // Reserved Character
00097                 iCHAR  StringTerminator; // Binary Zero String Terminator
00098             };
00099 
00101             struct TGA_EXTENSION
00102             {
00103                 TGA_EXTENSION();
00104                 void clear();
00105 
00106                 iWORD  Size;               // Extension Size
00107                 iCHAR  AuthorName[41];     // Author Name
00108                 iCHAR  AuthorComment[324]; // Author Comment
00109                 iWORD  StampMonth;         // Date/Time Stamp: Month
00110                 iWORD  StampDay;           // Date/Time Stamp: Day
00111                 iWORD  StampYear;          // Date/Time Stamp: Year
00112                 iWORD  StampHour;          // Date/Time Stamp: Hour
00113                 iWORD  StampMinute;        // Date/Time Stamp: Minute
00114                 iWORD  StampSecond;        // Date/Time Stamp: Second
00115                 iCHAR  JobName[41];        // Job Name/ID
00116                 iWORD  JobHour;            // Job Time: Hours
00117                 iWORD  JobMinute;          // Job Time: Minutes
00118                 iWORD  JobSecond;          // Job Time: Seconds
00119                 iCHAR  SoftwareId[41];     // Software ID
00120                 iWORD  VersionNumber;      // Software Version Number
00121                 iBYTE  VersionLetter;      // Software Version Letter
00122                 iDWORD KeyColor;           // Key Color
00123                 iWORD  PixelNumerator;     // Pixel Aspect Ratio
00124                 iWORD  PixelDenominator;   // Pixel Aspect Ratio
00125                 iWORD  GammaNumerator;     // Gamma Value
00126                 iWORD  GammaDenominator;   // Gamma Value
00127                 iDWORD ColorOffset;        // Color Correction Offset
00128                 iDWORD StampOffset;        // Postage Stamp Offset
00129                 iDWORD ScanOffset;         // Scan-Line Table Offset
00130                 iBYTE  AttributesType;     // Attributes Types
00131             };
00132 
00134             struct TGA_DEVTAG
00135             {
00136                 iWORD  TagNumber;  // ID Number of the tag
00137                 iDWORD DataOffset; // Offset location of the tag data
00138                 iDWORD DataSize;   // Size of the tag data in bytes
00139             };
00140             #pragma pack()
00141 
00142         public:
00144             struct TargaDevField
00145             {
00147                 TargaDevField() : data(NULL), size(0), tagID(0) {};
00148 
00150 
00153                 TargaDevField( Iubyte *d, int sz, int id ) : data(d), size(sz), tagID(id) {};
00154 
00155                 Iubyte *data;
00156                 int tagID;
00157                 int size;
00158             };
00159 
00160             enum TargaType
00161             {
00162                 NoData = 0,
00163                 ColMap = 1,
00164                 TrueCol = 2,
00165                 BW = 3,
00166                 RLE_ColMap = 9,
00167                 RLE_TrueCol = 10,
00168                 RLE_BW = 11
00169             };
00170 
00171             enum TargaOrigin
00172             {
00173                 OriginBL = 0,
00174                 OriginBR = 1,
00175                 OriginUL = 2,
00176                 OriginUR = 3
00177             };
00178 
00179             enum TargaVersion
00180             {
00181                 v1_0 = 1,
00182                 v2_0 = 2
00183             };
00184 
00185         public:
00187             iTGA();
00188 
00190 
00191             iTGA( const iTGA &img );
00192 
00194             iTGA( const iAbstractImage &img );
00195 
00197 
00201             iTGA( int w, int h, iImageFormat f );
00202 
00204 
00206             iTGA( Iubyte *d, int w, int h, iImageFormat f, int b );
00207 
00209 
00210             iTGA( const char *filename );
00211 
00213 
00214             iTGA( Iubyte *buf, int len );
00215 
00217 
00218             iTGA( iDataStream *s );
00219 
00221             ~iTGA();
00222 
00223             void clear();
00224 
00225             bool isFormatSupported( iImageFormat fmt ) const;
00226 
00227             static bool isValid( iDataStream *s );
00228 
00230             void setImageID( const char id[255] );
00231 
00233             const char *getImageID() const { return imageID; }
00234 
00236 
00238             void setTargaVersion( TargaVersion ver ) { tgaVer = ver; }
00239 
00241             TargaVersion getTargaVersion() const { return tgaVer; }
00242 
00244 
00246             void addDevField( const TargaDevField &field ) { devField.append(field); }
00247 
00249 
00250             void setJobName( const char *name ) { jobName = name; }
00251 
00253 
00254             const char *getJobName() const { return jobName; }
00255 
00257 
00258             void enableRLE( bool brle ) { useRle = brle; }
00259 
00261 
00262             void enableExtension( bool bext ) { useExt = bext; }
00263 
00265 
00266             void enableDevArea( bool bdev ) { useDev = bdev; }
00267 
00268             iTGA &operator=( const iTGA &img );
00269 
00270             iTGA &operator=( const iAbstractImage &img );
00271 
00272         protected:
00273             void init();
00274             bool loadImage();
00275             bool saveImage();
00276 
00277         private:
00278             bool readHeader();
00279             bool readFooter();
00280             bool readDevArea();
00281             bool readExtension();
00282             bool readImageID();
00283             bool readColMapData();
00284             bool validateHeader();
00285             bool readImageData();
00286             bool readRaw();
00287             bool readRLE();
00288 
00289             bool writeHeader();
00290             bool writeFooter();
00291             bool writeDevArea();
00292             bool writeExtension();
00293             bool writeImageID();
00294             bool writeColMapData();
00295             bool writeImageData();
00296             bool writeRaw( Iubyte *buf );
00297             bool writeRLE( Iubyte *buf );
00298 
00299             int compressRLELine( Iubyte *in, Iuint n, Iubyte *out );
00300             int countDiffPixels( Iubyte *buf, Iuint count );
00301             int countSamePixels( Iubyte *buf, Iuint count );
00302 
00303             Iuint getDevAreaSize();
00304             Iuint getTargaSize();
00305 
00306             iImageOrigin TargaOrigin2iImageOrigin( TargaOrigin org );
00307             TargaOrigin iImageOrigin2TargaOrigin( iImageOrigin org );
00308 
00309         private:
00310             TGA_HEADER header;
00311             TGA_FOOTER footer;
00312             TGA_EXTENSION extension;
00313             char imageID[255];
00314             TargaVersion tgaVer;
00315             int alphabits;
00316             const char *jobName;
00317 
00318             iLinkedList<TargaDevField> devField;
00319 
00320             // Write members
00321             bool useRle;
00322             bool useExt;
00323             bool useDev;
00324     };
00325 }
00326 
00327 #endif // IMAGEN_TGA
00328 
00329 #endif // __ITGA_H__