Panda3D
 All Classes Functions Variables Enumerations
pnmFileTypeSGI.cxx
1 // Filename: pnmFileTypeSGI.cxx
2 // Created by: drose (17Jun00)
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 #include "pnmFileTypeSGI.h"
16 
17 #ifdef HAVE_SGI_RGB
18 
19 #include "config_pnmimagetypes.h"
20 #include "sgi.h"
21 
22 #include "pnmFileTypeRegistry.h"
23 #include "bamReader.h"
24 
25 static const char * const extensions_sgi[] = {
26  "rgb", "rgba", "sgi"
27 };
28 static const int num_extensions_sgi = sizeof(extensions_sgi) / sizeof(const char *);
29 
30 TypeHandle PNMFileTypeSGI::_type_handle;
31 
32 ////////////////////////////////////////////////////////////////////
33 // Function: PNMFileTypeSGI::Constructor
34 // Access: Public
35 // Description:
36 ////////////////////////////////////////////////////////////////////
37 PNMFileTypeSGI::
38 PNMFileTypeSGI() {
39 }
40 
41 ////////////////////////////////////////////////////////////////////
42 // Function: PNMFileTypeSGI::get_name
43 // Access: Public, Virtual
44 // Description: Returns a few words describing the file type.
45 ////////////////////////////////////////////////////////////////////
46 string PNMFileTypeSGI::
47 get_name() const {
48  return "SGI RGB";
49 }
50 
51 ////////////////////////////////////////////////////////////////////
52 // Function: PNMFileTypeSGI::get_num_extensions
53 // Access: Public, Virtual
54 // Description: Returns the number of different possible filename
55 // extensions associated with this particular file type.
56 ////////////////////////////////////////////////////////////////////
57 int PNMFileTypeSGI::
58 get_num_extensions() const {
59  return num_extensions_sgi;
60 }
61 
62 ////////////////////////////////////////////////////////////////////
63 // Function: PNMFileTypeSGI::get_extension
64 // Access: Public, Virtual
65 // Description: Returns the nth possible filename extension
66 // associated with this particular file type, without a
67 // leading dot.
68 ////////////////////////////////////////////////////////////////////
69 string PNMFileTypeSGI::
70 get_extension(int n) const {
71  nassertr(n >= 0 && n < num_extensions_sgi, string());
72  return extensions_sgi[n];
73 }
74 
75 ////////////////////////////////////////////////////////////////////
76 // Function: PNMFileTypeSGI::get_suggested_extension
77 // Access: Public, Virtual
78 // Description: Returns a suitable filename extension (without a
79 // leading dot) to suggest for files of this type, or
80 // empty string if no suggestions are available.
81 ////////////////////////////////////////////////////////////////////
82 string PNMFileTypeSGI::
83 get_suggested_extension() const {
84  return "rgb";
85 }
86 
87 ////////////////////////////////////////////////////////////////////
88 // Function: PNMFileTypeSGI::has_magic_number
89 // Access: Public, Virtual
90 // Description: Returns true if this particular file type uses a
91 // magic number to identify it, false otherwise.
92 ////////////////////////////////////////////////////////////////////
93 bool PNMFileTypeSGI::
94 has_magic_number() const {
95  return true;
96 }
97 
98 ////////////////////////////////////////////////////////////////////
99 // Function: PNMFileTypeSGI::matches_magic_number
100 // Access: Public, Virtual
101 // Description: Returns true if the indicated "magic number" byte
102 // stream (the initial few bytes read from the file)
103 // matches this particular file type, false otherwise.
104 ////////////////////////////////////////////////////////////////////
105 bool PNMFileTypeSGI::
106 matches_magic_number(const string &magic_number) const {
107  nassertr(magic_number.size() >= 2, false);
108  int mn =
109  ((unsigned char)magic_number[0] << 8) |
110  ((unsigned char)magic_number[1]);
111  return (mn == SGI_MAGIC);
112 }
113 
114 ////////////////////////////////////////////////////////////////////
115 // Function: PNMFileTypeSGI::make_reader
116 // Access: Public, Virtual
117 // Description: Allocates and returns a new PNMReader suitable for
118 // reading from this file type, if possible. If reading
119 // from this file type is not supported, returns NULL.
120 ////////////////////////////////////////////////////////////////////
121 PNMReader *PNMFileTypeSGI::
122 make_reader(istream *file, bool owns_file, const string &magic_number) {
123  init_pnm();
124  return new Reader(this, file, owns_file, magic_number);
125 }
126 
127 ////////////////////////////////////////////////////////////////////
128 // Function: PNMFileTypeSGI::make_writer
129 // Access: Public, Virtual
130 // Description: Allocates and returns a new PNMWriter suitable for
131 // reading from this file type, if possible. If writing
132 // files of this type is not supported, returns NULL.
133 ////////////////////////////////////////////////////////////////////
134 PNMWriter *PNMFileTypeSGI::
135 make_writer(ostream *file, bool owns_file) {
136  init_pnm();
137  return new Writer(this, file, owns_file);
138 }
139 
140 
141 ////////////////////////////////////////////////////////////////////
142 // Function: PNMFileTypeSGI::register_with_read_factory
143 // Access: Public, Static
144 // Description: Registers the current object as something that can be
145 // read from a Bam file.
146 ////////////////////////////////////////////////////////////////////
147 void PNMFileTypeSGI::
148 register_with_read_factory() {
150  register_factory(get_class_type(), make_PNMFileTypeSGI);
151 }
152 
153 ////////////////////////////////////////////////////////////////////
154 // Function: PNMFileTypeSGI::make_PNMFileTypeSGI
155 // Access: Protected, Static
156 // Description: This method is called by the BamReader when an object
157 // of this type is encountered in a Bam file; it should
158 // allocate and return a new object with all the data
159 // read.
160 //
161 // In the case of the PNMFileType objects, since these
162 // objects are all shared, we just pull the object from
163 // the registry.
164 ////////////////////////////////////////////////////////////////////
165 TypedWritable *PNMFileTypeSGI::
166 make_PNMFileTypeSGI(const FactoryParams &params) {
167  return PNMFileTypeRegistry::get_global_ptr()->get_type_by_handle(get_class_type());
168 }
169 
170 #endif // HAVE_SGI_RGB
PNMFileType * get_type_by_handle(TypeHandle handle) const
Returns the PNMFileType instance stored in the registry for the given TypeHandle, e...
Base class for objects that can be written to and read from Bam files.
Definition: typedWritable.h:37
static PNMFileTypeRegistry * get_global_ptr()
Returns a pointer to the global PNMFileTypeRegistry object.
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
static WritableFactory * get_factory()
Returns the global WritableFactory for generating TypedWritable objects.
Definition: bamReader.I:213
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:85