Panda3D
 All Classes Functions Variables Enumerations
pnmFileTypeBMP.cxx
00001 // Filename: pnmFileTypeBMP.cxx
00002 // Created by:  drose (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 "pnmFileTypeBMP.h"
00016 
00017 #ifdef HAVE_BMP
00018 
00019 #include "config_pnmimagetypes.h"
00020 
00021 #include "pnmFileTypeRegistry.h"
00022 #include "bamReader.h"
00023 
00024 static const char * const extensions_bmp[] = {
00025   "bmp"
00026 };
00027 static const int num_extensions_bmp = sizeof(extensions_bmp) / sizeof(const char *);
00028 
00029 TypeHandle PNMFileTypeBMP::_type_handle;
00030 
00031 ////////////////////////////////////////////////////////////////////
00032 //     Function: PNMFileTypeBMP::Constructor
00033 //       Access: Public
00034 //  Description:
00035 ////////////////////////////////////////////////////////////////////
00036 PNMFileTypeBMP::
00037 PNMFileTypeBMP() {
00038 }
00039 
00040 ////////////////////////////////////////////////////////////////////
00041 //     Function: PNMFileTypeBMP::get_name
00042 //       Access: Public, Virtual
00043 //  Description: Returns a few words describing the file type.
00044 ////////////////////////////////////////////////////////////////////
00045 string PNMFileTypeBMP::
00046 get_name() const {
00047   return "Windows BMP";
00048 }
00049 
00050 ////////////////////////////////////////////////////////////////////
00051 //     Function: PNMFileTypeBMP::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 PNMFileTypeBMP::
00057 get_num_extensions() const {
00058   return num_extensions_bmp;
00059 }
00060 
00061 ////////////////////////////////////////////////////////////////////
00062 //     Function: PNMFileTypeBMP::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 PNMFileTypeBMP::
00069 get_extension(int n) const {
00070   nassertr(n >= 0 && n < num_extensions_bmp, string());
00071   return extensions_bmp[n];
00072 }
00073 
00074 ////////////////////////////////////////////////////////////////////
00075 //     Function: PNMFileTypeBMP::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 PNMFileTypeBMP::
00082 get_suggested_extension() const {
00083   return "bmp";
00084 }
00085 
00086 ////////////////////////////////////////////////////////////////////
00087 //     Function: PNMFileTypeBMP::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 PNMFileTypeBMP::
00093 has_magic_number() const {
00094   return true;
00095 }
00096 
00097 ////////////////////////////////////////////////////////////////////
00098 //     Function: PNMFileTypeBMP::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 PNMFileTypeBMP::
00105 matches_magic_number(const string &magic_number) const {
00106   nassertr(magic_number.size() >= 2, false);
00107   return (magic_number.substr(0, 2) == "BM");
00108 }
00109 
00110 ////////////////////////////////////////////////////////////////////
00111 //     Function: PNMFileTypeBMP::make_reader
00112 //       Access: Public, Virtual
00113 //  Description: Allocates and returns a new PNMReader suitable for
00114 //               reading from this file type, if possible.  If reading
00115 //               from this file type is not supported, returns NULL.
00116 ////////////////////////////////////////////////////////////////////
00117 PNMReader *PNMFileTypeBMP::
00118 make_reader(istream *file, bool owns_file, const string &magic_number) {
00119   init_pnm();
00120   return new Reader(this, file, owns_file, magic_number);
00121 }
00122 
00123 ////////////////////////////////////////////////////////////////////
00124 //     Function: PNMFileTypeBMP::make_writer
00125 //       Access: Public, Virtual
00126 //  Description: Allocates and returns a new PNMWriter suitable for
00127 //               reading from this file type, if possible.  If writing
00128 //               files of this type is not supported, returns NULL.
00129 ////////////////////////////////////////////////////////////////////
00130 PNMWriter *PNMFileTypeBMP::
00131 make_writer(ostream *file, bool owns_file) {
00132   init_pnm();
00133   return new Writer(this, file, owns_file);
00134 }
00135 
00136 
00137 ////////////////////////////////////////////////////////////////////
00138 //     Function: PNMFileTypeBMP::register_with_read_factory
00139 //       Access: Public, Static
00140 //  Description: Registers the current object as something that can be
00141 //               read from a Bam file.
00142 ////////////////////////////////////////////////////////////////////
00143 void PNMFileTypeBMP::
00144 register_with_read_factory() {
00145   BamReader::get_factory()->
00146     register_factory(get_class_type(), make_PNMFileTypeBMP);
00147 }
00148 
00149 ////////////////////////////////////////////////////////////////////
00150 //     Function: PNMFileTypeBMP::make_PNMFileTypeBMP
00151 //       Access: Protected, Static
00152 //  Description: This method is called by the BamReader when an object
00153 //               of this type is encountered in a Bam file; it should
00154 //               allocate and return a new object with all the data
00155 //               read.
00156 //
00157 //               In the case of the PNMFileType objects, since these
00158 //               objects are all shared, we just pull the object from
00159 //               the registry.
00160 ////////////////////////////////////////////////////////////////////
00161 TypedWritable *PNMFileTypeBMP::
00162 make_PNMFileTypeBMP(const FactoryParams &params) {
00163   return PNMFileTypeRegistry::get_global_ptr()->get_type_by_handle(get_class_type());
00164 }
00165 
00166 #endif  // HAVE_BMP
 All Classes Functions Variables Enumerations