Panda3D
pnmFileTypePNG.h
Go to the documentation of this file.
1 /**
2  * PANDA 3D SOFTWARE
3  * Copyright (c) Carnegie Mellon University. All rights reserved.
4  *
5  * All use of this software is subject to the terms of the revised BSD
6  * license. You should have received a copy of this license along
7  * with this source code in a file named "LICENSE."
8  *
9  * @file pnmFileTypePNG.h
10  * @author drose
11  * @date 2004-03-16
12  */
13 
14 #ifndef PNMFILETYPEPNG_H
15 #define PNMFILETYPEPNG_H
16 
17 #include "pandabase.h"
18 
19 #ifdef HAVE_PNG
20 
21 // Must be first.
22 #include <png.h>
23 
24 #include "pnmFileType.h"
25 #include "pnmReader.h"
26 #include "pnmWriter.h"
27 
28 /**
29  * For reading and writing PNG files.
30  */
31 class EXPCL_PANDA_PNMIMAGETYPES PNMFileTypePNG : public PNMFileType {
32 public:
33  PNMFileTypePNG();
34 
35  virtual std::string get_name() const;
36 
37  virtual int get_num_extensions() const;
38  virtual std::string get_extension(int n) const;
39  virtual std::string get_suggested_extension() const;
40 
41  virtual bool has_magic_number() const;
42  virtual bool matches_magic_number(const std::string &magic_number) const;
43 
44  virtual PNMReader *make_reader(std::istream *file, bool owns_file = true,
45  const std::string &magic_number = std::string());
46  virtual PNMWriter *make_writer(std::ostream *file, bool owns_file = true);
47 
48 public:
49  class Reader : public PNMReader {
50  public:
51  Reader(PNMFileType *type, std::istream *file, bool owns_file, std::string magic_number);
52  virtual ~Reader();
53 
54  virtual int read_data(xel *array, xelval *alpha_data);
55 
56  private:
57  void free_png();
58  static void png_read_data(png_structp png_ptr, png_bytep data,
59  png_size_t length);
60 
61  static void png_error(png_structp png_ptr, png_const_charp error_msg);
62  static void png_warning(png_structp png_ptr, png_const_charp warning_msg);
63 
64  png_structp _png;
65  png_infop _info;
66 
67  // We need a jmp_buf to support libpng's fatal error handling, in which
68  // the error handler must not immediately leave libpng code, but must
69  // return to the caller in Panda.
70  jmp_buf _jmpbuf;
71  };
72 
73  class Writer : public PNMWriter {
74  public:
75  Writer(PNMFileType *type, std::ostream *file, bool owns_file);
76  virtual ~Writer();
77 
78  virtual int write_data(xel *array, xelval *alpha);
79 
80  private:
81  void free_png();
82  static int make_png_bit_depth(int bit_depth);
83  static void png_write_data(png_structp png_ptr, png_bytep data,
84  png_size_t length);
85  static void png_flush_data(png_structp png_ptr);
86 
87  static void png_error(png_structp png_ptr, png_const_charp error_msg);
88  static void png_warning(png_structp png_ptr, png_const_charp warning_msg);
89 
90  png_structp _png;
91  png_infop _info;
92 
93  // We need a jmp_buf to support libpng's fatal error handling, in which
94  // the error handler must not immediately leave libpng code, but must
95  // return to the caller in Panda.
96  jmp_buf _jmpbuf;
97  };
98 
99  // The TypedWritable interface follows.
100 public:
101  static void register_with_read_factory();
102 
103 protected:
104  static TypedWritable *make_PNMFileTypePNG(const FactoryParams &params);
105 
106 public:
107  static TypeHandle get_class_type() {
108  return _type_handle;
109  }
110  static void init_type() {
111  PNMFileType::init_type();
112  register_type(_type_handle, "PNMFileTypePNG",
113  PNMFileType::get_class_type());
114  }
115  virtual TypeHandle get_type() const {
116  return get_class_type();
117  }
118  virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
119 
120 private:
121  static TypeHandle _type_handle;
122 };
123 
124 #endif // HAVE_PNG
125 
126 #endif
virtual PNMReader * make_reader(std::istream *file, bool owns_file=true, const std::string &magic_number=std::string())
Allocates and returns a new PNMReader suitable for reading from this file type, if possible.
Definition: pnmFileType.cxx:96
Base class for objects that can be written to and read from Bam files.
Definition: typedWritable.h:35
void register_type(TypeHandle &type_handle, const std::string &name)
This inline function is just a convenient way to call TypeRegistry::register_type(),...
Definition: register_type.I:22
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
This is the base class of a family of classes that represent particular image file types that PNMImag...
Definition: pnmFileType.h:32
An instance of this class is passed to the Factory when requesting it to do its business and construc...
Definition: factoryParams.h:36
This is an abstract base class that defines the interface for reading image files of various types.
Definition: pnmReader.h:27
get_extension
Returns the nth possible filename extension associated with this particular file type,...
Definition: pnmFileType.h:44
This is an abstract base class that defines the interface for writing image files of various types.
Definition: pnmWriter.h:27
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
virtual bool matches_magic_number(const std::string &magic_number) const
Returns true if the indicated "magic number" byte stream (the initial few bytes read from the file) m...
Definition: pnmFileType.cxx:86
virtual bool has_magic_number() const
Returns true if this particular file type uses a magic number to identify it, false otherwise.
Definition: pnmFileType.cxx:76
virtual PNMWriter * make_writer(std::ostream *file, bool owns_file=true)
Allocates and returns a new PNMWriter suitable for reading from this file type, if possible.
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:81
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.