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