15 #include "pnmFileTypePfm.h" 17 #include "config_pnmimage.h" 19 #include "pnmFileTypeRegistry.h" 20 #include "bamReader.h" 40 return "Portable Float Map";
98 return (magic_number.size() >= 2) &&
99 (magic_number.substr(0, 2) ==
"PF" ||
100 magic_number.substr(0, 2) ==
"Pf" ||
101 magic_number.substr(0, 2) ==
"pf");
112 make_reader(istream *file,
bool owns_file,
const string &magic_number) {
113 return new Reader(
this, file, owns_file, magic_number);
125 return new Writer(
this, file, owns_file);
134 PNMFileTypePfm::Reader::
135 Reader(
PNMFileType *type, istream *file,
bool owns_file,
string magic_number) :
138 read_magic_number(_file, magic_number, 2);
140 if (magic_number ==
"pf") {
144 read_magic_number(_file, magic_number, 4);
147 if (magic_number ==
"PF") {
150 }
else if (magic_number ==
"Pf") {
153 }
else if (magic_number ==
"pf2c") {
157 }
else if (magic_number ==
"pf4c") {
163 <<
"Not a PFM file\n";
168 _maxval = PGM_MAXMAXVAL;
170 (*_file) >> _x_size >> _y_size >> _scale;
173 <<
"Error parsing PFM header\n";
208 bool little_endian =
false;
211 little_endian =
true;
213 if (pfm_force_littleendian) {
214 little_endian =
true;
216 if (pfm_reverse_dimensions) {
222 pfm.
clear(_x_size, _y_size, _num_channels);
226 int size = _x_size * _y_size * _num_channels;
229 pfm.swap_table(table);
231 (*_file).read((
char *)&table[0],
sizeof(PN_float32) * size);
232 if ((*_file).fail() && !(*_file).eof()) {
238 #ifdef WORDS_BIGENDIAN 239 bool endian_reversed = little_endian;
241 bool endian_reversed = !little_endian;
244 if (endian_reversed) {
245 for (
int ti = 0; ti < size; ++ti) {
251 pfm.swap_table(table);
261 PNMFileTypePfm::Writer::
262 Writer(
PNMFileType *type, ostream *file,
bool owns_file) :
301 nassertr(pfm.is_valid(),
false);
309 (*_file) <<
"pf2c\n";
317 (*_file) <<
"pf4c\n";
321 nassertr(
false,
false);
325 PN_float32 scale = cabs(pfm.
get_scale());
329 #ifndef WORDS_BIGENDIAN 334 (*_file) << scale <<
"\n";
338 (*_file).write((
const char *)&table[0],
sizeof(PN_float32) * size);
340 if ((*_file).fail()) {
343 nassertr(
sizeof(PN_float32) == 4,
false);
356 register_factory(get_class_type(), make_PNMFileTypePfm);
virtual string get_name() const
Returns a few words describing the file type.
virtual string get_extension(int n) const
Returns the nth possible filename extension associated with this particular file type, without a leading dot.
virtual bool write_pfm(const PfmFile &pfm)
Writes floating-point data from the indicated PfmFile.
virtual PNMReader * make_reader(istream *file, bool owns_file=true, const string &magic_number=string())
Allocates and returns a new PNMReader suitable for reading from this file type, if possible...
Base class for objects that can be written to and read from Bam files.
This is the base class of a family of classes that represent particular image file types that PNMImag...
static PNMFileTypeRegistry * get_global_ptr()
Returns a pointer to the global PNMFileTypeRegistry object.
This is our own Panda specialization on the default STL vector.
virtual bool is_floating_point()
Returns true if this PNMFileType represents a floating-point image type, false if it is a normal...
void set_scale(PN_float32 scale)
The "scale" is reported in the pfm header and is probably meaningless.
virtual bool has_magic_number() const
Returns true if this particular file type uses a magic number to identify it, false otherwise...
virtual bool supports_floating_point()
Returns true if this PNMFileType can accept a floating-point image type, false if it can only accept ...
Defines a pfm file, a 2-d table of floating-point numbers, either 3-component or 1-component, or with a special extension, 2- or 4-component.
virtual bool read_pfm(PfmFile &pfm)
Reads floating-point data directly into the indicated PfmFile.
An instance of this class is passed to the Factory when requesting it to do its business and construc...
PNMFileType * get_type_by_handle(TypeHandle handle) const
Returns the PNMFileType instance stored in the registry for the given TypeHandle, e...
virtual bool supports_integer()
Returns true if this PNMFileType can accept an integer image type, false if it can only accept a floa...
virtual string get_suggested_extension() const
Returns a suitable filename extension (without a leading dot) to suggest for files of this type...
PN_float32 get_scale() const
The "scale" is reported in the pfm header and is probably meaningless.
This is an abstract base class that defines the interface for reading image files of various types...
This is an abstract base class that defines the interface for writing image files of various types...
NativeNumericData and ReversedNumericData work together to provide a sneaky interface for automatical...
virtual int get_num_extensions() const
Returns the number of different possible filename extensions associated with this particular file typ...
static WritableFactory * get_factory()
Returns the global WritableFactory for generating TypedWritable objects.
virtual bool matches_magic_number(const string &magic_number) const
Returns true if the indicated "magic number" byte stream (the initial few bytes read from the file) m...
static void register_with_read_factory()
Registers the current object as something that can be read from a Bam file.
TypeHandle is the identifier used to differentiate C++ class types.
void store_value(void *dest, size_t length) const
Copies the data, with byte reversal if appropriate, into the indicated numeric variable, whose address and sizeof are given.
virtual PNMWriter * make_writer(ostream *file, bool owns_file=true)
Allocates and returns a new PNMWriter suitable for reading from this file type, if possible...
void clear()
Eliminates all data in the file.