Panda3D
 All Classes Functions Variables Enumerations
pnmFileTypeJPG.h
00001 // Filename: pnmFileTypeJPG.h
00002 // Created by:  mike (17Jun00)
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 PNMFILETYPEJPG_H
00016 #define PNMFILETYPEJPG_H
00017 
00018 #include "pandabase.h"
00019 
00020 #ifdef HAVE_JPEG
00021 
00022 #include "pnmFileType.h"
00023 #include "pnmReader.h"
00024 #include "pnmWriter.h"
00025 
00026 #if defined(_WIN32)
00027 #define WIN32_LEAN_AND_MEAN
00028 #include <windows.h>  // we need to include this before jpeglib.
00029 #endif
00030 
00031 #ifdef HAVE_PNG
00032 // If we are going to be including png.h (in the unrelated file
00033 // pnmFileTypePNG.h), be sure to include it before including setjmp.h.
00034 // Ugly hack due to png weirdness with setjmp.
00035 #include <png.h>
00036 #endif
00037 
00038 
00039 extern "C" {
00040 #include <stdio.h>  // jpeglib requires this to be included first.
00041 #include <jpeglib.h>
00042 #include <setjmp.h>
00043 }
00044 
00045 ////////////////////////////////////////////////////////////////////
00046 //       Class : PNMFileTypeJPG
00047 // Description : For reading and writing Jpeg files.
00048 ////////////////////////////////////////////////////////////////////
00049 class EXPCL_PANDA_PNMIMAGETYPES PNMFileTypeJPG : public PNMFileType {
00050 public:
00051   PNMFileTypeJPG();
00052 
00053   virtual string get_name() const;
00054 
00055   virtual int get_num_extensions() const;
00056   virtual string get_extension(int n) const;
00057   virtual string get_suggested_extension() const;
00058 
00059   virtual bool has_magic_number() const;
00060   virtual bool matches_magic_number(const string &magic_number) const;
00061 
00062   virtual PNMReader *make_reader(istream *file, bool owns_file = true,
00063                                  const string &magic_number = string());
00064   virtual PNMWriter *make_writer(ostream *file, bool owns_file = true);
00065 
00066 public:
00067   class Reader : public PNMReader {
00068   public:
00069     Reader(PNMFileType *type, istream *file, bool owns_file, string magic_number);
00070     ~Reader();
00071 
00072     virtual void prepare_read();
00073     virtual int read_data(xel *array, xelval *alpha);
00074 
00075   private:
00076     struct jpeg_decompress_struct _cinfo;
00077     struct my_error_mgr {
00078       struct jpeg_error_mgr pub;
00079       jmp_buf setjmp_buffer;
00080     };
00081     typedef struct my_error_mgr *_my_error_ptr;
00082     struct my_error_mgr _jerr;
00083     unsigned long       pos;
00084 
00085     unsigned long offBits;
00086 
00087     unsigned short  cBitCount;
00088     int             indexed;
00089     int             classv;
00090 
00091     pixval R[256];      /* reds */
00092     pixval G[256];      /* greens */
00093     pixval B[256];      /* blues */
00094 
00095     bool _is_valid;
00096   };
00097 
00098   class Writer : public PNMWriter {
00099   public:
00100     Writer(PNMFileType *type, ostream *file, bool owns_file);
00101 
00102     virtual int write_data(xel *array, xelval *alpha);
00103   };
00104 
00105 
00106   // The TypedWritable interface follows.
00107 public:
00108   static void register_with_read_factory();
00109 
00110 protected:
00111   static TypedWritable *make_PNMFileTypeJPG(const FactoryParams &params);
00112 
00113 public:
00114   static TypeHandle get_class_type() {
00115     return _type_handle;
00116   }
00117   static void init_type() {
00118     PNMFileType::init_type();
00119     register_type(_type_handle, "PNMFileTypeJPG",
00120                   PNMFileType::get_class_type());
00121   }
00122   virtual TypeHandle get_type() const {
00123     return get_class_type();
00124   }
00125   virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
00126 
00127 private:
00128   static TypeHandle _type_handle;
00129 };
00130 
00131 #endif  // HAVE_JPEG
00132 
00133 #endif
00134 
 All Classes Functions Variables Enumerations