00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #ifndef PNMFILETYPETGA_H
00020 #define PNMFILETYPETGA_H
00021
00022 #include "pandabase.h"
00023
00024 #include "pnmFileType.h"
00025 #include "pnmReader.h"
00026 #include "pnmWriter.h"
00027 #include "ppmcmap.h"
00028 #include "pnmimage_base.h"
00029
00030 struct ImageHeader;
00031
00032
00033
00034 #define TGA_MAXCOLORS 257
00035
00036
00037
00038
00039
00040 class EXPCL_PANDA PNMFileTypeTGA : public PNMFileType {
00041 public:
00042 PNMFileTypeTGA();
00043
00044 virtual string get_name() const;
00045
00046 virtual int get_num_extensions() const;
00047 virtual string get_extension(int n) const;
00048 virtual string get_suggested_extension() const;
00049
00050 virtual PNMReader *make_reader(istream *file, bool owns_file = true,
00051 const string &magic_number = string());
00052 virtual PNMWriter *make_writer(ostream *file, bool owns_file = true);
00053
00054 public:
00055 class Reader : public PNMReader {
00056 public:
00057 Reader(PNMFileType *type, istream *file, bool owns_file, string magic_number);
00058 virtual ~Reader();
00059
00060 virtual int read_data(xel *array, xelval *alpha);
00061
00062 private:
00063 void readtga ( istream* ifp, struct ImageHeader* tgaP, const string &magic_number );
00064 void get_map_entry ( istream* ifp, pixel* Value, int Size,
00065 gray* Alpha);
00066 void get_pixel ( istream* ifp, pixel* dest, int Size, gray* alpha_p);
00067 unsigned char getbyte ( istream* ifp );
00068
00069 int rows, cols, rlencoded, mapped;
00070 struct ImageHeader *tga_head;
00071 pixel ColorMap[TGA_MAXCOLORS];
00072 gray AlphaMap[TGA_MAXCOLORS];
00073 int RLE_count, RLE_flag;
00074 };
00075
00076 class Writer : public PNMWriter {
00077 public:
00078 Writer(PNMFileType *type, ostream *file, bool owns_file);
00079 virtual ~Writer();
00080
00081 virtual int write_data(xel *array, xelval *alpha);
00082
00083 private:
00084 void writetga ( struct ImageHeader* tgaP, char* id );
00085 void put_map_entry ( pixel* valueP, int size, pixval maxval );
00086 void compute_runlengths ( int cols, pixel* pixelrow, int* runlength );
00087 void put_pixel ( pixel* pP, int imgtype, pixval maxval, colorhash_table cht );
00088 void put_mono ( pixel* pP, pixval maxval );
00089 void put_map ( pixel* pP, colorhash_table cht );
00090 void put_rgb ( pixel* pP, pixval maxval );
00091
00092 int rle_flag;
00093 int rows, cols;
00094 struct ImageHeader *tgaHeader;
00095 colorhist_vector chv;
00096 colorhash_table cht;
00097 int ncolors;
00098 int *runlength;
00099 };
00100
00101
00102
00103 public:
00104 static void register_with_read_factory();
00105
00106 protected:
00107 static TypedWritable *make_PNMFileTypeTGA(const FactoryParams ¶ms);
00108
00109 public:
00110 static TypeHandle get_class_type() {
00111 return _type_handle;
00112 }
00113 static void init_type() {
00114 PNMFileType::init_type();
00115 register_type(_type_handle, "PNMFileTypeTGA",
00116 PNMFileType::get_class_type());
00117 }
00118 virtual TypeHandle get_type() const {
00119 return get_class_type();
00120 }
00121 virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
00122
00123 private:
00124 static TypeHandle _type_handle;
00125 };
00126
00127 #endif
00128
00129