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