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