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