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