Panda3D
|
00001 // Filename: pnmImageHeader.h 00002 // Created by: drose (14Jun00) 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 PNMIMAGEHEADER_H 00016 #define PNMIMAGEHEADER_H 00017 00018 #include "pandabase.h" 00019 00020 #include "pnmimage_base.h" 00021 00022 #include "typedObject.h" 00023 #include "filename.h" 00024 #include "pnotify.h" 00025 #include "pmap.h" 00026 #include "pvector.h" 00027 00028 class PNMFileType; 00029 class PNMReader; 00030 class PNMWriter; 00031 00032 //////////////////////////////////////////////////////////////////// 00033 // Class : PNMImageHeader 00034 // Description : This is the base class of PNMImage, PNMReader, and 00035 // PNMWriter. It encapsulates all the information 00036 // associated with an image that describes its size, 00037 // number of channels, etc; that is, all the information 00038 // about the image except the image data itself. It's 00039 // the sort of information you typically read from the 00040 // image file's header. 00041 //////////////////////////////////////////////////////////////////// 00042 class EXPCL_PANDA_PNMIMAGE PNMImageHeader { 00043 PUBLISHED: 00044 INLINE PNMImageHeader(); 00045 INLINE PNMImageHeader(const PNMImageHeader ©); 00046 INLINE void operator = (const PNMImageHeader ©); 00047 INLINE ~PNMImageHeader(); 00048 00049 // This enumerated type indicates the number of channels in the 00050 // image, and also implies an image type. You can treat it either 00051 // as an integer number of channels or as an enumerated image type. 00052 enum ColorType { 00053 CT_invalid = 0, 00054 CT_grayscale = 1, 00055 CT_two_channel = 2, 00056 CT_color = 3, 00057 CT_four_channel = 4, 00058 }; 00059 00060 INLINE ColorType get_color_type() const; 00061 INLINE int get_num_channels() const; 00062 00063 INLINE static bool is_grayscale(ColorType color_type); 00064 INLINE bool is_grayscale() const; 00065 00066 INLINE static bool has_alpha(ColorType color_type); 00067 INLINE bool has_alpha() const; 00068 00069 INLINE xelval get_maxval() const; 00070 00071 INLINE int get_x_size() const; 00072 INLINE int get_y_size() const; 00073 00074 INLINE string get_comment() const; 00075 INLINE void set_comment(const string &comment); 00076 00077 INLINE bool has_type() const; 00078 INLINE PNMFileType *get_type() const; 00079 INLINE void set_type(PNMFileType *type); 00080 00081 BLOCKING bool read_header(const Filename &filename, PNMFileType *type = NULL, 00082 bool report_unknown_type = true); 00083 BLOCKING bool read_header(istream &data, const string &filename = string(), 00084 PNMFileType *type = NULL, bool report_unknown_type = true); 00085 00086 PNMReader *make_reader(const Filename &filename, 00087 PNMFileType *type = NULL, 00088 bool report_unknown_type = true) const; 00089 PNMReader *make_reader(istream *file, bool owns_file = true, 00090 const Filename &filename = Filename(), 00091 string magic_number = string(), 00092 PNMFileType *type = NULL, 00093 bool report_unknown_type = true) const; 00094 00095 PNMWriter *make_writer(const Filename &filename, 00096 PNMFileType *type = NULL) const; 00097 PNMWriter *make_writer(ostream *file, bool owns_file = true, 00098 const Filename &filename = Filename(), 00099 PNMFileType *type = NULL) const; 00100 00101 static bool read_magic_number(istream *file, string &magic_number, 00102 int num_bytes); 00103 00104 void output(ostream &out) const; 00105 00106 // Contains a single pixel specification used in compute_histogram() 00107 // and make_histogram(). Note that pixels are stored by integer 00108 // value, not by floating-point scaled value. 00109 class PixelSpec { 00110 PUBLISHED: 00111 INLINE PixelSpec(xelval gray_value); 00112 INLINE PixelSpec(xelval gray_value, xelval alpha); 00113 INLINE PixelSpec(xelval red, xelval green, xelval blue); 00114 INLINE PixelSpec(xelval red, xelval green, xelval blue, xelval alpha); 00115 INLINE PixelSpec(const xel &rgb); 00116 INLINE PixelSpec(const xel &rgb, xelval alpha); 00117 INLINE PixelSpec(const PixelSpec ©); 00118 INLINE void operator = (const PixelSpec ©); 00119 00120 INLINE bool operator < (const PixelSpec &other) const; 00121 INLINE bool operator == (const PixelSpec &other) const; 00122 INLINE bool operator != (const PixelSpec &other) const; 00123 INLINE int compare_to(const PixelSpec &other) const; 00124 00125 INLINE xelval get_red() const; 00126 INLINE xelval get_green() const; 00127 INLINE xelval get_blue() const; 00128 INLINE xelval get_alpha() const; 00129 00130 INLINE void set_red(xelval red); 00131 INLINE void set_green(xelval green); 00132 INLINE void set_blue(xelval blue); 00133 INLINE void set_alpha(xelval alpha); 00134 00135 INLINE xelval operator [](int n) const; 00136 INLINE static int size(); 00137 00138 void output(ostream &out) const; 00139 00140 public: 00141 xelval _red, _green, _blue, _alpha; 00142 }; 00143 00144 // Associates a pixel specification with an appearance count, for 00145 // use in Histogram, below. 00146 class PixelSpecCount { 00147 public: 00148 INLINE PixelSpecCount(const PixelSpec &pixel, int count); 00149 INLINE bool operator < (const PixelSpecCount &other) const; 00150 00151 PixelSpec _pixel; 00152 int _count; 00153 }; 00154 00155 typedef pmap<PixelSpec, int> HistMap; 00156 typedef pvector<PixelSpecCount> PixelCount; 00157 typedef pvector<PixelSpec> Palette; 00158 00159 // Used to return a pixel histogram in PNMImage::get_histogram(). 00160 class Histogram { 00161 PUBLISHED: 00162 INLINE Histogram(); 00163 00164 INLINE int get_num_pixels() const; 00165 INLINE const PixelSpec &get_pixel(int n) const; 00166 INLINE int get_count(int n) const; 00167 INLINE int get_count(const PixelSpec &pixel) const; 00168 MAKE_SEQ(get_pixels, get_num_pixels, get_pixel); 00169 00170 void write(ostream &out) const; 00171 00172 public: 00173 INLINE void swap(PixelCount &pixels, HistMap &hist_map); 00174 00175 private: 00176 PixelCount _pixels; 00177 HistMap _hist_map; 00178 }; 00179 00180 protected: 00181 bool compute_histogram(HistMap &hist, xel *array, xelval *alpha, 00182 int max_colors = 0); 00183 bool compute_palette(Palette &palette, xel *array, xelval *alpha, 00184 int max_colors = 0); 00185 INLINE void record_color(HistMap &hist, const PixelSpec &color); 00186 00187 int _x_size, _y_size; 00188 int _num_channels; 00189 xelval _maxval; 00190 string _comment; 00191 PNMFileType *_type; 00192 }; 00193 00194 INLINE ostream &operator << (ostream &out, const PNMImageHeader &header) { 00195 header.output(out); 00196 return out; 00197 } 00198 00199 INLINE ostream &operator << (ostream &out, const PNMImageHeader::PixelSpec &pixel) { 00200 pixel.output(out); 00201 return out; 00202 } 00203 00204 #include "pnmImageHeader.I" 00205 00206 #endif