Panda3D
pnmFileTypeSGI.cxx
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 pnmFileTypeSGI.cxx
10  * @author drose
11  * @date 2000-06-17
12  */
13 
14 #include "pnmFileTypeSGI.h"
15 
16 #ifdef HAVE_SGI_RGB
17 
18 #include "config_pnmimagetypes.h"
19 #include "sgi.h"
20 
21 #include "pnmFileTypeRegistry.h"
22 #include "bamReader.h"
23 
24 using std::string;
25 
26 static const char * const extensions_sgi[] = {
27  "rgb", "rgba", "sgi"
28 };
29 static const int num_extensions_sgi = sizeof(extensions_sgi) / sizeof(const char *);
30 
31 TypeHandle PNMFileTypeSGI::_type_handle;
32 
33 /**
34  *
35  */
36 PNMFileTypeSGI::
37 PNMFileTypeSGI() {
38 }
39 
40 /**
41  * Returns a few words describing the file type.
42  */
43 string PNMFileTypeSGI::
44 get_name() const {
45  return "SGI RGB";
46 }
47 
48 /**
49  * Returns the number of different possible filename extensions associated
50  * with this particular file type.
51  */
52 int PNMFileTypeSGI::
53 get_num_extensions() const {
54  return num_extensions_sgi;
55 }
56 
57 /**
58  * Returns the nth possible filename extension associated with this particular
59  * file type, without a leading dot.
60  */
61 string PNMFileTypeSGI::
62 get_extension(int n) const {
63  nassertr(n >= 0 && n < num_extensions_sgi, string());
64  return extensions_sgi[n];
65 }
66 
67 /**
68  * Returns a suitable filename extension (without a leading dot) to suggest
69  * for files of this type, or empty string if no suggestions are available.
70  */
71 string PNMFileTypeSGI::
72 get_suggested_extension() const {
73  return "rgb";
74 }
75 
76 /**
77  * Returns true if this particular file type uses a magic number to identify
78  * it, false otherwise.
79  */
80 bool PNMFileTypeSGI::
81 has_magic_number() const {
82  return true;
83 }
84 
85 /**
86  * Returns true if the indicated "magic number" byte stream (the initial few
87  * bytes read from the file) matches this particular file type, false
88  * otherwise.
89  */
90 bool PNMFileTypeSGI::
91 matches_magic_number(const string &magic_number) const {
92  nassertr(magic_number.size() >= 2, false);
93  int mn =
94  ((unsigned char)magic_number[0] << 8) |
95  ((unsigned char)magic_number[1]);
96  return (mn == SGI_MAGIC);
97 }
98 
99 /**
100  * Allocates and returns a new PNMReader suitable for reading from this file
101  * type, if possible. If reading from this file type is not supported,
102  * returns NULL.
103  */
104 PNMReader *PNMFileTypeSGI::
105 make_reader(std::istream *file, bool owns_file, const string &magic_number) {
106  init_pnm();
107  return new Reader(this, file, owns_file, magic_number);
108 }
109 
110 /**
111  * Allocates and returns a new PNMWriter suitable for reading from this file
112  * type, if possible. If writing files of this type is not supported, returns
113  * NULL.
114  */
115 PNMWriter *PNMFileTypeSGI::
116 make_writer(std::ostream *file, bool owns_file) {
117  init_pnm();
118  return new Writer(this, file, owns_file);
119 }
120 
121 
122 /**
123  * Registers the current object as something that can be read from a Bam file.
124  */
125 void PNMFileTypeSGI::
126 register_with_read_factory() {
128  register_factory(get_class_type(), make_PNMFileTypeSGI);
129 }
130 
131 /**
132  * This method is called by the BamReader when an object of this type is
133  * encountered in a Bam file; it should allocate and return a new object with
134  * all the data read.
135  *
136  * In the case of the PNMFileType objects, since these objects are all shared,
137  * we just pull the object from the registry.
138  */
139 TypedWritable *PNMFileTypeSGI::
140 make_PNMFileTypeSGI(const FactoryParams &params) {
141  return PNMFileTypeRegistry::get_global_ptr()->get_type_by_handle(get_class_type());
142 }
143 
144 #endif // HAVE_SGI_RGB
PNMFileTypeRegistry::get_type_by_handle
PNMFileType * get_type_by_handle(TypeHandle handle) const
Returns the PNMFileType instance stored in the registry for the given TypeHandle, e....
Definition: pnmFileTypeRegistry.cxx:233
BamReader::get_factory
static WritableFactory * get_factory()
Returns the global WritableFactory for generating TypedWritable objects.
Definition: bamReader.I:177
bamReader.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
TypedWritable
Base class for objects that can be written to and read from Bam files.
Definition: typedWritable.h:35
TypeHandle
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:81
FactoryParams
An instance of this class is passed to the Factory when requesting it to do its business and construc...
Definition: factoryParams.h:36
pnmFileTypeSGI.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PNMFileTypeRegistry::get_global_ptr
static PNMFileTypeRegistry * get_global_ptr()
Returns a pointer to the global PNMFileTypeRegistry object.
Definition: pnmFileTypeRegistry.cxx:277
PNMReader
This is an abstract base class that defines the interface for reading image files of various types.
Definition: pnmReader.h:27
pnmFileTypeRegistry.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
sgi.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
config_pnmimagetypes.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PNMWriter
This is an abstract base class that defines the interface for writing image files of various types.
Definition: pnmWriter.h:27