Panda3D
 All Classes Functions Variables Enumerations
pnmFileTypePNG.h
00001 // Filename: pnmFileTypePNG.h
00002 // Created by:  drose (16Mar04)
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 #ifndef PNMFILETYPEPNG_H
00016 #define PNMFILETYPEPNG_H
00017 
00018 #include "pandabase.h"
00019 
00020 #ifdef HAVE_PNG
00021 
00022 // Must be first.
00023 #include <png.h>
00024 
00025 #include "pnmFileType.h"
00026 #include "pnmReader.h"
00027 #include "pnmWriter.h"
00028 
00029 ////////////////////////////////////////////////////////////////////
00030 //       Class : PNMFileTypePNG
00031 // Description : For reading and writing PNG files.
00032 ////////////////////////////////////////////////////////////////////
00033 class EXPCL_PANDA_PNMIMAGETYPES PNMFileTypePNG : public PNMFileType {
00034 public:
00035   PNMFileTypePNG();
00036 
00037   virtual string get_name() const;
00038 
00039   virtual int get_num_extensions() const;
00040   virtual string get_extension(int n) const;
00041   virtual string get_suggested_extension() const;
00042 
00043   virtual bool has_magic_number() const;
00044   virtual bool matches_magic_number(const string &magic_number) const;
00045 
00046   virtual PNMReader *make_reader(istream *file, bool owns_file = true,
00047                                  const string &magic_number = string());
00048   virtual PNMWriter *make_writer(ostream *file, bool owns_file = true);
00049 
00050 public:
00051   class Reader : public PNMReader {
00052   public:
00053     Reader(PNMFileType *type, istream *file, bool owns_file, string magic_number);
00054     virtual ~Reader();
00055 
00056     virtual int read_data(xel *array, xelval *alpha_data);
00057 
00058   private:
00059     void free_png();
00060     static void png_read_data(png_structp png_ptr, png_bytep data, 
00061                               png_size_t length);
00062 
00063     static void png_error(png_structp png_ptr, png_const_charp error_msg);
00064     static void png_warning(png_structp png_ptr, png_const_charp warning_msg);
00065 
00066     png_structp _png;
00067     png_infop _info;
00068 
00069     // We need a jmp_buf to support libpng's fatal error handling, in
00070     // which the error handler must not immediately leave libpng code,
00071     // but must return to the caller in Panda.
00072     jmp_buf _jmpbuf;
00073   };
00074 
00075   class Writer : public PNMWriter {
00076   public:
00077     Writer(PNMFileType *type, ostream *file, bool owns_file);
00078     virtual ~Writer();
00079 
00080     virtual int write_data(xel *array, xelval *alpha);
00081 
00082   private:
00083     void free_png();
00084     static int make_png_bit_depth(int bit_depth);
00085     static void png_write_data(png_structp png_ptr, png_bytep data, 
00086                                png_size_t length);
00087     static void png_flush_data(png_structp png_ptr);
00088 
00089     static void png_error(png_structp png_ptr, png_const_charp error_msg);
00090     static void png_warning(png_structp png_ptr, png_const_charp warning_msg);
00091 
00092     png_structp _png;
00093     png_infop _info;
00094 
00095     // We need a jmp_buf to support libpng's fatal error handling, in
00096     // which the error handler must not immediately leave libpng code,
00097     // but must return to the caller in Panda.
00098     jmp_buf _jmpbuf;
00099   };
00100 
00101   // The TypedWritable interface follows.
00102 public:
00103   static void register_with_read_factory();
00104 
00105 protected:
00106   static TypedWritable *make_PNMFileTypePNG(const FactoryParams &params);
00107 
00108 public:
00109   static TypeHandle get_class_type() {
00110     return _type_handle;
00111   }
00112   static void init_type() {
00113     PNMFileType::init_type();
00114     register_type(_type_handle, "PNMFileTypePNG",
00115                   PNMFileType::get_class_type());
00116   }
00117   virtual TypeHandle get_type() const {
00118     return get_class_type();
00119   }
00120   virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
00121 
00122 private:
00123   static TypeHandle _type_handle;
00124 };
00125 
00126 #endif  // HAVE_PNG
00127 
00128 #endif
 All Classes Functions Variables Enumerations