Panda3D
pnmFileTypeTGA.h
1 // Filename: pnmFileTypeTGA.h
2 // Created by: drose (27Apr01)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 #ifndef PNMFILETYPETGA_H
16 #define PNMFILETYPETGA_H
17 
18 #include "pandabase.h"
19 
20 #ifdef HAVE_TGA
21 
22 #include "pnmFileType.h"
23 #include "pnmReader.h"
24 #include "pnmWriter.h"
25 #include "ppmcmap.h"
26 #include "pnmimage_base.h"
27 
28 struct ImageHeader;
29 
30 
31 ////////////////////////////////////////////////////////////////////
32 // Class : PNMFileTypeTGA
33 // Description : For reading and writing Targa image files.
34 ////////////////////////////////////////////////////////////////////
35 class EXPCL_PANDA_PNMIMAGETYPES PNMFileTypeTGA : public PNMFileType {
36 public:
37  PNMFileTypeTGA();
38 
39  virtual string get_name() const;
40 
41  virtual int get_num_extensions() const;
42  virtual string get_extension(int n) const;
43  virtual string get_suggested_extension() const;
44 
45  virtual PNMReader *make_reader(istream *file, bool owns_file = true,
46  const string &magic_number = string());
47  virtual PNMWriter *make_writer(ostream *file, bool owns_file = true);
48 
49 public:
50  class Reader : public PNMReader {
51  public:
52  Reader(PNMFileType *type, istream *file, bool owns_file, string magic_number);
53  virtual ~Reader();
54 
55  virtual int read_data(xel *array, xelval *alpha);
56 
57  private:
58  void readtga ( istream* ifp, struct ImageHeader* tgaP, const string &magic_number );
59  void get_map_entry ( istream* ifp, pixel* Value, int Size,
60  gray* Alpha);
61  void get_pixel ( istream* ifp, pixel* dest, int Size, gray* alpha_p);
62  unsigned char getbyte ( istream* ifp );
63 
64  int rows, cols, rlencoded, mapped;
65  struct ImageHeader *tga_head;
66  pixel *ColorMap;
67  gray *AlphaMap;
68  int RLE_count, RLE_flag;
69 
70  pixval Red, Grn, Blu;
71  pixval Alpha;
72  unsigned int l;
73  };
74 
75  class Writer : public PNMWriter {
76  public:
77  Writer(PNMFileType *type, ostream *file, bool owns_file);
78  virtual ~Writer();
79 
80  virtual int write_data(xel *array, xelval *alpha);
81 
82  private:
83  void writetga ( struct ImageHeader* tgaP, char* id );
84  void put_map_entry ( pixel* valueP, int size, pixval maxval );
85  void compute_runlengths ( int cols, pixel* pixelrow, int* runlength );
86  void put_pixel ( pixel* pP, int imgtype, pixval maxval, colorhash_table cht );
87  void put_mono ( pixel* pP, pixval maxval );
88  void put_map ( pixel* pP, colorhash_table cht );
89  void put_rgb ( pixel* pP, pixval maxval );
90 
91  int rle_flag;
92  int rows, cols;
93  struct ImageHeader *tgaHeader;
94  colorhist_vector chv;
95  colorhash_table cht;
96  int ncolors;
97  int *runlength;
98  };
99 
100 
101  // The TypedWritable interface follows.
102 public:
103  static void register_with_read_factory();
104 
105 protected:
106  static TypedWritable *make_PNMFileTypeTGA(const FactoryParams &params);
107 
108 public:
109  static TypeHandle get_class_type() {
110  return _type_handle;
111  }
112  static void init_type() {
113  PNMFileType::init_type();
114  register_type(_type_handle, "PNMFileTypeTGA",
115  PNMFileType::get_class_type());
116  }
117  virtual TypeHandle get_type() const {
118  return get_class_type();
119  }
120  virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
121 
122 private:
123  static TypeHandle _type_handle;
124 };
125 
126 #endif // HAVE_TGA
127 
128 #endif
129 
130 
virtual string get_extension(int n) const
Returns the nth possible filename extension associated with this particular file type, without a leading dot.
Definition: pnmFileType.cxx:62
virtual PNMWriter * make_writer(ostream *file, bool owns_file=true)
Allocates and returns a new PNMWriter suitable for reading from this file type, if possible...
Base class for objects that can be written to and read from Bam files.
Definition: typedWritable.h:37
This is the base class of a family of classes that represent particular image file types that PNMImag...
Definition: pnmFileType.h:35
virtual PNMReader * make_reader(istream *file, bool owns_file=true, const string &magic_number=string())
Allocates and returns a new PNMReader suitable for reading from this file type, if possible...
An instance of this class is passed to the Factory when requesting it to do its business and construc...
Definition: factoryParams.h:40
This is an abstract base class that defines the interface for reading image files of various types...
Definition: pnmReader.h:31
This is an abstract base class that defines the interface for writing image files of various types...
Definition: pnmWriter.h:31
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:85
virtual int get_num_extensions() const
Returns the number of different possible filename extensions associated with this particular file typ...
Definition: pnmFileType.cxx:50
virtual string get_suggested_extension() const
Returns a suitable filename extension (without a leading dot) to suggest for files of this type...
Definition: pnmFileType.cxx:75