Panda3D
pnmFileTypeJPG.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 pnmFileTypeJPG.cxx
10  * @author mike
11  * @date 2000-06-19
12  */
13 
14 #include "pnmFileTypeJPG.h"
15 
16 #ifdef HAVE_JPEG
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_jpg[] = {
26  "jpg", "jpeg"
27 };
28 static const int num_extensions_jpg = sizeof(extensions_jpg) / sizeof(const char *);
29 
30 TypeHandle PNMFileTypeJPG::_type_handle;
31 
32 /**
33  *
34  */
35 PNMFileTypeJPG::
36 PNMFileTypeJPG() {
37 }
38 
39 /**
40  * Returns a few words describing the file type.
41  */
42 string PNMFileTypeJPG::
43 get_name() const {
44  return "JPEG";
45 }
46 
47 /**
48  * Returns the number of different possible filename extensions associated
49  * with this particular file type.
50  */
51 int PNMFileTypeJPG::
52 get_num_extensions() const {
53  return num_extensions_jpg;
54 }
55 
56 /**
57  * Returns the nth possible filename extension associated with this particular
58  * file type, without a leading dot.
59  */
60 string PNMFileTypeJPG::
61 get_extension(int n) const {
62  nassertr(n >= 0 && n < num_extensions_jpg, string());
63  return extensions_jpg[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 PNMFileTypeJPG::
71 get_suggested_extension() const {
72  return "jpg";
73 }
74 
75 /**
76  * Returns true if this particular file type uses a magic number to identify
77  * it, false otherwise.
78  */
79 bool PNMFileTypeJPG::
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 PNMFileTypeJPG::
90 matches_magic_number(const string &magic_number) const {
91  nassertr(magic_number.size() >= 2, false);
92  return ((char)magic_number[0] == (char)0xff &&
93  (char)magic_number[1] == (char)0xd8);
94 }
95 
96 /**
97  * Allocates and returns a new PNMReader suitable for reading from this file
98  * type, if possible. If reading from this file type is not supported,
99  * returns NULL.
100  */
101 PNMReader *PNMFileTypeJPG::
102 make_reader(std::istream *file, bool owns_file, const string &magic_number) {
103  init_pnm();
104  return new Reader(this, file, owns_file, magic_number);
105 }
106 
107 /**
108  * Allocates and returns a new PNMWriter suitable for reading from this file
109  * type, if possible. If writing files of this type is not supported, returns
110  * NULL.
111  */
112 PNMWriter *PNMFileTypeJPG::
113 make_writer(std::ostream *file, bool owns_file) {
114  init_pnm();
115  return new Writer(this, file, owns_file);
116 }
117 
118 
119 /**
120  * Registers the current object as something that can be read from a Bam file.
121  */
122 void PNMFileTypeJPG::
123 register_with_read_factory() {
125  register_factory(get_class_type(), make_PNMFileTypeJPG);
126 }
127 
128 /**
129  * This method is called by the BamReader when an object of this type is
130  * encountered in a Bam file; it should allocate and return a new object with
131  * all the data read.
132  *
133  * In the case of the PNMFileType objects, since these objects are all shared,
134  * we just pull the object from the registry.
135  */
136 TypedWritable *PNMFileTypeJPG::
137 make_PNMFileTypeJPG(const FactoryParams &params) {
138  return PNMFileTypeRegistry::get_global_ptr()->get_type_by_handle(get_class_type());
139 }
140 
141 #endif // HAVE_JPEG
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
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
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:81