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