Panda3D
 All Classes Functions Variables Enumerations
pnmFileTypeJPG.cxx
00001 // Filename: pnmFileTypeJPG.cxx
00002 // Created by:  mike (19Jun00)
00003 //
00004 ////////////////////////////////////////////////////////////////////
00005 //
00006 // PANDA 3D SOFTWARE
00007 // Copyright (c) Carnegie Mellon University.  All rights reserved.
00008 //
00009 // All use of this software is subject to the terms of the revised BSD
00010 // license.  You should have received a copy of this license along
00011 // with this source code in a file named "LICENSE."
00012 //
00013 ////////////////////////////////////////////////////////////////////
00014 
00015 #include "pnmFileTypeJPG.h"
00016 
00017 #ifdef HAVE_JPEG
00018 
00019 #include "config_pnmimagetypes.h"
00020 
00021 #include "pnmFileTypeRegistry.h"
00022 #include "bamReader.h"
00023 
00024 static const char *const extensions_jpg[] = {
00025   "jpg", "jpeg"
00026 };
00027 static const int num_extensions_jpg = sizeof(extensions_jpg) / sizeof(const char *);
00028 
00029 TypeHandle PNMFileTypeJPG::_type_handle;
00030 
00031 ////////////////////////////////////////////////////////////////////
00032 //     Function: PNMFileTypeJPG::Constructor
00033 //       Access: Public
00034 //  Description:
00035 ////////////////////////////////////////////////////////////////////
00036 PNMFileTypeJPG::
00037 PNMFileTypeJPG() {
00038 }
00039 
00040 ////////////////////////////////////////////////////////////////////
00041 //     Function: PNMFileTypeJPG::get_name
00042 //       Access: Public, Virtual
00043 //  Description: Returns a few words describing the file type.
00044 ////////////////////////////////////////////////////////////////////
00045 string PNMFileTypeJPG::
00046 get_name() const {
00047   return "JPEG";
00048 }
00049 
00050 ////////////////////////////////////////////////////////////////////
00051 //     Function: PNMFileTypeJPG::get_num_extensions
00052 //       Access: Public, Virtual
00053 //  Description: Returns the number of different possible filename
00054 //               extensions associated with this particular file type.
00055 ////////////////////////////////////////////////////////////////////
00056 int PNMFileTypeJPG::
00057 get_num_extensions() const {
00058   return num_extensions_jpg;
00059 }
00060 
00061 ////////////////////////////////////////////////////////////////////
00062 //     Function: PNMFileTypeJPG::get_extension
00063 //       Access: Public, Virtual
00064 //  Description: Returns the nth possible filename extension
00065 //               associated with this particular file type, without a
00066 //               leading dot.
00067 ////////////////////////////////////////////////////////////////////
00068 string PNMFileTypeJPG::
00069 get_extension(int n) const {
00070   nassertr(n >= 0 && n < num_extensions_jpg, string());
00071   return extensions_jpg[n];
00072 }
00073 
00074 ////////////////////////////////////////////////////////////////////
00075 //     Function: PNMFileTypeJPG::get_suggested_extension
00076 //       Access: Public, Virtual
00077 //  Description: Returns a suitable filename extension (without a
00078 //               leading dot) to suggest for files of this type, or
00079 //               empty string if no suggestions are available.
00080 ////////////////////////////////////////////////////////////////////
00081 string PNMFileTypeJPG::
00082 get_suggested_extension() const {
00083   return "jpg";
00084 }
00085 
00086 ////////////////////////////////////////////////////////////////////
00087 //     Function: PNMFileTypeJPG::has_magic_number
00088 //       Access: Public, Virtual
00089 //  Description: Returns true if this particular file type uses a
00090 //               magic number to identify it, false otherwise.
00091 ////////////////////////////////////////////////////////////////////
00092 bool PNMFileTypeJPG::
00093 has_magic_number() const {
00094   return true;
00095 }
00096 
00097 ////////////////////////////////////////////////////////////////////
00098 //     Function: PNMFileTypeJPG::matches_magic_number
00099 //       Access: Public, Virtual
00100 //  Description: Returns true if the indicated "magic number" byte
00101 //               stream (the initial few bytes read from the file)
00102 //               matches this particular file type, false otherwise.
00103 ////////////////////////////////////////////////////////////////////
00104 bool PNMFileTypeJPG::
00105 matches_magic_number(const string &magic_number) const {
00106   nassertr(magic_number.size() >= 2, false);
00107   return ((char)magic_number[0] == (char)0xff &&
00108           (char)magic_number[1] == (char)0xd8);
00109 }
00110 
00111 ////////////////////////////////////////////////////////////////////
00112 //     Function: PNMFileTypeJPG::make_reader
00113 //       Access: Public, Virtual
00114 //  Description: Allocates and returns a new PNMReader suitable for
00115 //               reading from this file type, if possible.  If reading
00116 //               from this file type is not supported, returns NULL.
00117 ////////////////////////////////////////////////////////////////////
00118 PNMReader *PNMFileTypeJPG::
00119 make_reader(istream *file, bool owns_file, const string &magic_number) {
00120   init_pnm();
00121   return new Reader(this, file, owns_file, magic_number);
00122 }
00123 
00124 ////////////////////////////////////////////////////////////////////
00125 //     Function: PNMFileTypeJPG::make_writer
00126 //       Access: Public, Virtual
00127 //  Description: Allocates and returns a new PNMWriter suitable for
00128 //               reading from this file type, if possible.  If writing
00129 //               files of this type is not supported, returns NULL.
00130 ////////////////////////////////////////////////////////////////////
00131 PNMWriter *PNMFileTypeJPG::
00132 make_writer(ostream *file, bool owns_file) {
00133   init_pnm();
00134   return new Writer(this, file, owns_file);
00135 }
00136 
00137 
00138 ////////////////////////////////////////////////////////////////////
00139 //     Function: PNMFileTypeJPG::register_with_read_factory
00140 //       Access: Public, Static
00141 //  Description: Registers the current object as something that can be
00142 //               read from a Bam file.
00143 ////////////////////////////////////////////////////////////////////
00144 void PNMFileTypeJPG::
00145 register_with_read_factory() {
00146   BamReader::get_factory()->
00147     register_factory(get_class_type(), make_PNMFileTypeJPG);
00148 }
00149 
00150 ////////////////////////////////////////////////////////////////////
00151 //     Function: PNMFileTypeJPG::make_PNMFileTypeJPG
00152 //       Access: Protected, Static
00153 //  Description: This method is called by the BamReader when an object
00154 //               of this type is encountered in a Bam file; it should
00155 //               allocate and return a new object with all the data
00156 //               read.
00157 //
00158 //               In the case of the PNMFileType objects, since these
00159 //               objects are all shared, we just pull the object from
00160 //               the registry.
00161 ////////////////////////////////////////////////////////////////////
00162 TypedWritable *PNMFileTypeJPG::
00163 make_PNMFileTypeJPG(const FactoryParams &params) {
00164   return PNMFileTypeRegistry::get_global_ptr()->get_type_by_handle(get_class_type());
00165 }
00166 
00167 #endif  // HAVE_JPEG
 All Classes Functions Variables Enumerations