Panda3D
pnmFileTypeJPG.cxx
1 // Filename: pnmFileTypeJPG.cxx
2 // Created by: mike (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 "pnmFileTypeJPG.h"
16 
17 #ifdef HAVE_JPEG
18 
19 #include "config_pnmimagetypes.h"
20 
21 #include "pnmFileTypeRegistry.h"
22 #include "bamReader.h"
23 
24 static const char *const extensions_jpg[] = {
25  "jpg", "jpeg"
26 };
27 static const int num_extensions_jpg = sizeof(extensions_jpg) / sizeof(const char *);
28 
29 TypeHandle PNMFileTypeJPG::_type_handle;
30 
31 ////////////////////////////////////////////////////////////////////
32 // Function: PNMFileTypeJPG::Constructor
33 // Access: Public
34 // Description:
35 ////////////////////////////////////////////////////////////////////
36 PNMFileTypeJPG::
37 PNMFileTypeJPG() {
38 }
39 
40 ////////////////////////////////////////////////////////////////////
41 // Function: PNMFileTypeJPG::get_name
42 // Access: Public, Virtual
43 // Description: Returns a few words describing the file type.
44 ////////////////////////////////////////////////////////////////////
45 string PNMFileTypeJPG::
46 get_name() const {
47  return "JPEG";
48 }
49 
50 ////////////////////////////////////////////////////////////////////
51 // Function: PNMFileTypeJPG::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 PNMFileTypeJPG::
57 get_num_extensions() const {
58  return num_extensions_jpg;
59 }
60 
61 ////////////////////////////////////////////////////////////////////
62 // Function: PNMFileTypeJPG::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 PNMFileTypeJPG::
69 get_extension(int n) const {
70  nassertr(n >= 0 && n < num_extensions_jpg, string());
71  return extensions_jpg[n];
72 }
73 
74 ////////////////////////////////////////////////////////////////////
75 // Function: PNMFileTypeJPG::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 PNMFileTypeJPG::
82 get_suggested_extension() const {
83  return "jpg";
84 }
85 
86 ////////////////////////////////////////////////////////////////////
87 // Function: PNMFileTypeJPG::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 PNMFileTypeJPG::
93 has_magic_number() const {
94  return true;
95 }
96 
97 ////////////////////////////////////////////////////////////////////
98 // Function: PNMFileTypeJPG::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 PNMFileTypeJPG::
105 matches_magic_number(const string &magic_number) const {
106  nassertr(magic_number.size() >= 2, false);
107  return ((char)magic_number[0] == (char)0xff &&
108  (char)magic_number[1] == (char)0xd8);
109 }
110 
111 ////////////////////////////////////////////////////////////////////
112 // Function: PNMFileTypeJPG::make_reader
113 // Access: Public, Virtual
114 // Description: Allocates and returns a new PNMReader suitable for
115 // reading from this file type, if possible. If reading
116 // from this file type is not supported, returns NULL.
117 ////////////////////////////////////////////////////////////////////
118 PNMReader *PNMFileTypeJPG::
119 make_reader(istream *file, bool owns_file, const string &magic_number) {
120  init_pnm();
121  return new Reader(this, file, owns_file, magic_number);
122 }
123 
124 ////////////////////////////////////////////////////////////////////
125 // Function: PNMFileTypeJPG::make_writer
126 // Access: Public, Virtual
127 // Description: Allocates and returns a new PNMWriter suitable for
128 // reading from this file type, if possible. If writing
129 // files of this type is not supported, returns NULL.
130 ////////////////////////////////////////////////////////////////////
131 PNMWriter *PNMFileTypeJPG::
132 make_writer(ostream *file, bool owns_file) {
133  init_pnm();
134  return new Writer(this, file, owns_file);
135 }
136 
137 
138 ////////////////////////////////////////////////////////////////////
139 // Function: PNMFileTypeJPG::register_with_read_factory
140 // Access: Public, Static
141 // Description: Registers the current object as something that can be
142 // read from a Bam file.
143 ////////////////////////////////////////////////////////////////////
144 void PNMFileTypeJPG::
145 register_with_read_factory() {
147  register_factory(get_class_type(), make_PNMFileTypeJPG);
148 }
149 
150 ////////////////////////////////////////////////////////////////////
151 // Function: PNMFileTypeJPG::make_PNMFileTypeJPG
152 // Access: Protected, Static
153 // Description: This method is called by the BamReader when an object
154 // of this type is encountered in a Bam file; it should
155 // allocate and return a new object with all the data
156 // read.
157 //
158 // In the case of the PNMFileType objects, since these
159 // objects are all shared, we just pull the object from
160 // the registry.
161 ////////////////////////////////////////////////////////////////////
162 TypedWritable *PNMFileTypeJPG::
163 make_PNMFileTypeJPG(const FactoryParams &params) {
164  return PNMFileTypeRegistry::get_global_ptr()->get_type_by_handle(get_class_type());
165 }
166 
167 #endif // HAVE_JPEG
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