Panda3D
 All Classes Functions Variables Enumerations
pnmImageHeader.I
00001 // Filename: pnmImageHeader.I
00002 // Created by:  drose (15Jun00)
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 ////////////////////////////////////////////////////////////////////
00016 //     Function: PNMImageHeader::Constructor
00017 //       Access: Published
00018 //  Description:
00019 ////////////////////////////////////////////////////////////////////
00020 INLINE PNMImageHeader::
00021 PNMImageHeader() {
00022   _x_size = 0;
00023   _y_size = 0;
00024   _num_channels = 0;
00025   _maxval = 255;
00026   _type = (PNMFileType *)NULL;
00027 }
00028 
00029 ////////////////////////////////////////////////////////////////////
00030 //     Function: PNMImageHeader::Copy Constructor
00031 //       Access: Published
00032 //  Description:
00033 ////////////////////////////////////////////////////////////////////
00034 INLINE PNMImageHeader::
00035 PNMImageHeader(const PNMImageHeader &copy) :
00036   _x_size(copy._x_size),
00037   _y_size(copy._y_size),
00038   _num_channels(copy._num_channels),
00039   _maxval(copy._maxval),
00040   _type(copy._type)
00041 {
00042 }
00043 
00044 ////////////////////////////////////////////////////////////////////
00045 //     Function: PNMImageHeader::Copy Assignment Operator
00046 //       Access: Published
00047 //  Description:
00048 ////////////////////////////////////////////////////////////////////
00049 INLINE void PNMImageHeader::
00050 operator = (const PNMImageHeader &copy) {
00051   _x_size = copy._x_size;
00052   _y_size = copy._y_size;
00053   _num_channels = copy._num_channels;
00054   _maxval = copy._maxval;
00055   _comment = copy._comment;
00056   _type = copy._type;
00057 }
00058 
00059 ////////////////////////////////////////////////////////////////////
00060 //     Function: PNMImageHeader::Destructor
00061 //       Access: Published
00062 //  Description:
00063 ////////////////////////////////////////////////////////////////////
00064 INLINE PNMImageHeader::
00065 ~PNMImageHeader() {
00066 }
00067 
00068 ////////////////////////////////////////////////////////////////////
00069 //     Function: PNMImageHeader::get_color_type
00070 //       Access: Published
00071 //  Description: Returns the image type of the image, as an enumerated
00072 //               value.  This is really just the number of channels
00073 //               cast to the enumerated type.
00074 ////////////////////////////////////////////////////////////////////
00075 INLINE PNMImageHeader::ColorType PNMImageHeader::
00076 get_color_type() const {
00077   nassertr(_num_channels >= 1 && _num_channels <= 4, CT_invalid);
00078   return (ColorType)_num_channels;
00079 }
00080 
00081 ////////////////////////////////////////////////////////////////////
00082 //     Function: PNMImageHeader::get_num_channels
00083 //       Access: Published
00084 //  Description: Returns the number of channels in the image.
00085 ////////////////////////////////////////////////////////////////////
00086 INLINE int PNMImageHeader::
00087 get_num_channels() const {
00088   nassertr(_num_channels >= 1 && _num_channels <= 4, 0);
00089   return _num_channels;
00090 }
00091 
00092 ////////////////////////////////////////////////////////////////////
00093 //     Function: PNMImageHeader::is_grayscale
00094 //       Access: Published, Static
00095 //  Description: This static variant of is_grayscale() returns true if
00096 //               the indicated image type represents a grayscale
00097 //               image, false otherwise.
00098 ////////////////////////////////////////////////////////////////////
00099 INLINE bool PNMImageHeader::
00100 is_grayscale(PNMImageHeader::ColorType color_type) {
00101   return (color_type == CT_grayscale || color_type == CT_two_channel);
00102 }
00103 
00104 ////////////////////////////////////////////////////////////////////
00105 //     Function: PNMImageHeader::is_grayscale
00106 //       Access: Published
00107 //  Description: Returns false if the image is a full-color image, and
00108 //               has red, green, and blue components; true if it is a
00109 //               grayscale image and has only a gray component.  (The
00110 //               gray color is actually stored in the blue channel,
00111 //               and the red and green channels are ignored.)
00112 ////////////////////////////////////////////////////////////////////
00113 INLINE bool PNMImageHeader::
00114 is_grayscale() const {
00115   return is_grayscale(get_color_type());
00116 }
00117 
00118 ////////////////////////////////////////////////////////////////////
00119 //     Function: PNMImageHeader::has_alpha
00120 //       Access: Published, Static
00121 //  Description: This static variant of has_alpha() returns true if
00122 //               the indicated image type includes an alpha channel,
00123 //               false otherwise.
00124 ////////////////////////////////////////////////////////////////////
00125 INLINE bool PNMImageHeader::
00126 has_alpha(PNMImageHeader::ColorType color_type) {
00127   return (color_type == CT_two_channel || color_type == CT_four_channel);
00128 }
00129 
00130 ////////////////////////////////////////////////////////////////////
00131 //     Function: PNMImageHeader::has_alpha
00132 //       Access: Published
00133 //  Description: Returns true if the image includes an alpha channel,
00134 //               false otherwise.  Unlike is_grayscale(), if this
00135 //               returns false it is an error to call any of the
00136 //               functions accessing the alpha channel.
00137 ////////////////////////////////////////////////////////////////////
00138 INLINE bool PNMImageHeader::
00139 has_alpha() const {
00140   return has_alpha(get_color_type());
00141 }
00142 
00143 ////////////////////////////////////////////////////////////////////
00144 //     Function: PNMImageHeader::get_maxval
00145 //       Access: Published
00146 //  Description: Returns the maximum channel value allowable for any
00147 //               pixel in this image; for instance, 255 for a typical
00148 //               8-bit-per-channel image.  A pixel with this value is
00149 //               full on.
00150 ////////////////////////////////////////////////////////////////////
00151 INLINE xelval PNMImageHeader::
00152 get_maxval() const {
00153   return _maxval;
00154 }
00155 
00156 ////////////////////////////////////////////////////////////////////
00157 //     Function: PNMImageHeader::get_x_size
00158 //       Access: Published
00159 //  Description: Returns the number of pixels in the X direction.
00160 //               This is one more than the largest allowable X
00161 //               coordinate.
00162 ////////////////////////////////////////////////////////////////////
00163 INLINE int PNMImageHeader::
00164 get_x_size() const {
00165   return _x_size;
00166 }
00167 
00168 ////////////////////////////////////////////////////////////////////
00169 //     Function: PNMImageHeader::get_y_size
00170 //       Access: Published
00171 //  Description: Returns the number of pixels in the Y direction.
00172 //               This is one more than the largest allowable Y
00173 //               coordinate.
00174 ////////////////////////////////////////////////////////////////////
00175 INLINE int PNMImageHeader::
00176 get_y_size() const {
00177   return _y_size;
00178 }
00179 
00180 ////////////////////////////////////////////////////////////////////
00181 //     Function: PNMImageHeader::get_comment
00182 //       Access: Published
00183 //  Description: Gets the user comment from the file.
00184 ////////////////////////////////////////////////////////////////////
00185 INLINE string PNMImageHeader::
00186 get_comment() const {
00187   return _comment;
00188 }
00189 
00190 ////////////////////////////////////////////////////////////////////
00191 //     Function: PNMImageHeader::set_comment
00192 //       Access: Published
00193 //  Description: Writes a user comment string to the image (header).
00194 ////////////////////////////////////////////////////////////////////
00195 INLINE void PNMImageHeader::
00196 set_comment(const string& comment) {
00197   _comment = comment;
00198 }
00199 
00200 ////////////////////////////////////////////////////////////////////
00201 //     Function: PNMImageHeader::has_type
00202 //       Access: Published
00203 //  Description: Returns true if the PNMImageHeader knows what type it
00204 //               is, false otherwise.
00205 ////////////////////////////////////////////////////////////////////
00206 INLINE bool PNMImageHeader::
00207 has_type() const {
00208   return _type != (PNMFileType *)NULL;
00209 }
00210 
00211 ////////////////////////////////////////////////////////////////////
00212 //     Function: PNMImageHeader::get_type
00213 //       Access: Published
00214 //  Description: If the file type is known (e.g. has_type() returns
00215 //               true), returns its PNMFileType pointer; otherwise,
00216 //               returns NULL.
00217 ////////////////////////////////////////////////////////////////////
00218 INLINE PNMFileType *PNMImageHeader::
00219 get_type() const {
00220   return _type;
00221 }
00222 
00223 ////////////////////////////////////////////////////////////////////
00224 //     Function: PNMImageHeader::set_type
00225 //       Access: Published
00226 //  Description: Sets the file type of this PNMImage.  This will be
00227 //               the default type used when an image is read, if the
00228 //               type cannot be determined by magic number or inferred
00229 //               by extension, or the type used when the image is
00230 //               written, if the type cannot be inferred from the
00231 //               filename extension.
00232 ////////////////////////////////////////////////////////////////////
00233 INLINE void PNMImageHeader::
00234 set_type(PNMFileType *type) {
00235   _type = type;
00236 }
00237 
00238 ////////////////////////////////////////////////////////////////////
00239 //     Function: PNMImageHeader::record_color
00240 //       Access: Protected
00241 //  Description: Records the indicated color in the histogram.
00242 ////////////////////////////////////////////////////////////////////
00243 INLINE void PNMImageHeader::
00244 record_color(PNMImageHeader::HistMap &hist, 
00245              const PNMImageHeader::PixelSpec &color) {
00246   // First, try to add the color with a count of 0, in case it does
00247   // not already exist in the table.
00248   HistMap::iterator hi = hist.insert(HistMap::value_type(color, 0)).first;
00249 
00250   // Now that either succeeded or failed, but either way hi is now the
00251   // iterator to the count value in the table associated with the
00252   // given color.  Increment that count.
00253   (*hi).second++;
00254 }
00255 
00256 ////////////////////////////////////////////////////////////////////
00257 //     Function: PNMImageHeader::PixelSpec::Constructor
00258 //       Access: Published
00259 //  Description: 
00260 ////////////////////////////////////////////////////////////////////
00261 INLINE PNMImageHeader::PixelSpec::
00262 PixelSpec(xelval gray_value) :
00263   _red(gray_value),
00264   _green(gray_value),
00265   _blue(gray_value),
00266   _alpha(0)
00267 {
00268 }
00269 
00270 ////////////////////////////////////////////////////////////////////
00271 //     Function: PNMImageHeader::PixelSpec::Constructor
00272 //       Access: Published
00273 //  Description: 
00274 ////////////////////////////////////////////////////////////////////
00275 INLINE PNMImageHeader::PixelSpec::
00276 PixelSpec(xelval gray_value, xelval alpha) :
00277   _red(gray_value),
00278   _green(gray_value),
00279   _blue(gray_value),
00280   _alpha(alpha)
00281 {
00282 }
00283 
00284 ////////////////////////////////////////////////////////////////////
00285 //     Function: PNMImageHeader::PixelSpec::Constructor
00286 //       Access: Published
00287 //  Description: 
00288 ////////////////////////////////////////////////////////////////////
00289 INLINE PNMImageHeader::PixelSpec::
00290 PixelSpec(xelval red, xelval green, xelval blue) :
00291   _red(red),
00292   _green(green),
00293   _blue(blue),
00294   _alpha(0)
00295 {
00296 }
00297 
00298 ////////////////////////////////////////////////////////////////////
00299 //     Function: PNMImageHeader::PixelSpec::Constructor
00300 //       Access: Published
00301 //  Description: 
00302 ////////////////////////////////////////////////////////////////////
00303 INLINE PNMImageHeader::PixelSpec::
00304 PixelSpec(xelval red, xelval green, xelval blue, xelval alpha) :
00305   _red(red),
00306   _green(green),
00307   _blue(blue),
00308   _alpha(alpha)
00309 {
00310 }
00311 
00312 ////////////////////////////////////////////////////////////////////
00313 //     Function: PNMImageHeader::PixelSpec::Constructor
00314 //       Access: Published
00315 //  Description: 
00316 ////////////////////////////////////////////////////////////////////
00317 INLINE PNMImageHeader::PixelSpec::
00318 PixelSpec(const xel &rgb) :
00319   _red(PPM_GETR(rgb)),
00320   _green(PPM_GETG(rgb)),
00321   _blue(PPM_GETB(rgb)),
00322   _alpha(0)
00323 {
00324 }
00325 
00326 ////////////////////////////////////////////////////////////////////
00327 //     Function: PNMImageHeader::PixelSpec::Constructor
00328 //       Access: Published
00329 //  Description: 
00330 ////////////////////////////////////////////////////////////////////
00331 INLINE PNMImageHeader::PixelSpec::
00332 PixelSpec(const xel &rgb, xelval alpha) :
00333   _red(PPM_GETR(rgb)),
00334   _green(PPM_GETG(rgb)),
00335   _blue(PPM_GETB(rgb)),
00336   _alpha(alpha)
00337 {
00338 }
00339 
00340 ////////////////////////////////////////////////////////////////////
00341 //     Function: PNMImageHeader::PixelSpec::Copy Constructor
00342 //       Access: Published
00343 //  Description: 
00344 ////////////////////////////////////////////////////////////////////
00345 INLINE PNMImageHeader::PixelSpec::
00346 PixelSpec(const PixelSpec &copy) :
00347   _red(copy._red),
00348   _green(copy._green),
00349   _blue(copy._blue),
00350   _alpha(copy._alpha)
00351 {
00352 }
00353 
00354 ////////////////////////////////////////////////////////////////////
00355 //     Function: PNMImageHeader::PixelSpec::Copy Assignment Operator
00356 //       Access: Published
00357 //  Description: 
00358 ////////////////////////////////////////////////////////////////////
00359 INLINE void PNMImageHeader::PixelSpec::
00360 operator = (const PixelSpec &copy) {
00361   _red = copy._red;
00362   _green = copy._green;
00363   _blue = copy._blue;
00364   _alpha = copy._alpha;
00365 }
00366 
00367 ////////////////////////////////////////////////////////////////////
00368 //     Function: PNMImageHeader::PixelSpec::operator <
00369 //       Access: Published
00370 //  Description: 
00371 ////////////////////////////////////////////////////////////////////
00372 INLINE bool PNMImageHeader::PixelSpec::
00373 operator < (const PixelSpec &other) const {
00374   return compare_to(other) < 0;
00375 }
00376 
00377 ////////////////////////////////////////////////////////////////////
00378 //     Function: PNMImageHeader::PixelSpec::operator ==
00379 //       Access: Published
00380 //  Description: 
00381 ////////////////////////////////////////////////////////////////////
00382 INLINE bool PNMImageHeader::PixelSpec::
00383 operator == (const PixelSpec &other) const {
00384   return compare_to(other) == 0;
00385 }
00386 
00387 ////////////////////////////////////////////////////////////////////
00388 //     Function: PNMImageHeader::PixelSpec::operator !=
00389 //       Access: Published
00390 //  Description: 
00391 ////////////////////////////////////////////////////////////////////
00392 INLINE bool PNMImageHeader::PixelSpec::
00393 operator != (const PixelSpec &other) const {
00394   return compare_to(other) != 0;
00395 }
00396 
00397 ////////////////////////////////////////////////////////////////////
00398 //     Function: PNMImageHeader::PixelSpec::compare_to
00399 //       Access: Published
00400 //  Description: 
00401 ////////////////////////////////////////////////////////////////////
00402 INLINE int PNMImageHeader::PixelSpec::
00403 compare_to(const PixelSpec &other) const {
00404   if (_red != other._red) {
00405     return _red < other._red ? -1 : 1;
00406   }
00407   if (_green != other._green) {
00408     return _green < other._green ? -1 : 1;
00409   }
00410   if (_blue != other._blue) {
00411     return _blue < other._blue ? -1 : 1;
00412   }
00413   if (_alpha != other._alpha) {
00414     return _alpha < other._alpha ? -1 : 1;
00415   }
00416   return 0;
00417 }
00418 
00419 ////////////////////////////////////////////////////////////////////
00420 //     Function: PNMImageHeader::PixelSpec::get_red
00421 //       Access: Published
00422 //  Description: 
00423 ////////////////////////////////////////////////////////////////////
00424 INLINE xelval PNMImageHeader::PixelSpec::
00425 get_red() const {
00426   return _red;
00427 }
00428 
00429 ////////////////////////////////////////////////////////////////////
00430 //     Function: PNMImageHeader::PixelSpec::get_green
00431 //       Access: Published
00432 //  Description: 
00433 ////////////////////////////////////////////////////////////////////
00434 INLINE xelval PNMImageHeader::PixelSpec::
00435 get_green() const {
00436   return _green;
00437 }
00438 
00439 ////////////////////////////////////////////////////////////////////
00440 //     Function: PNMImageHeader::PixelSpec::get_blue
00441 //       Access: Published
00442 //  Description: 
00443 ////////////////////////////////////////////////////////////////////
00444 INLINE xelval PNMImageHeader::PixelSpec::
00445 get_blue() const {
00446   return _blue;
00447 }
00448 
00449 ////////////////////////////////////////////////////////////////////
00450 //     Function: PNMImageHeader::PixelSpec::get_alpha
00451 //       Access: Published
00452 //  Description: 
00453 ////////////////////////////////////////////////////////////////////
00454 INLINE xelval PNMImageHeader::PixelSpec::
00455 get_alpha() const {
00456   return _alpha;
00457 }
00458 
00459 ////////////////////////////////////////////////////////////////////
00460 //     Function: PNMImageHeader::PixelSpec::set_red
00461 //       Access: Published
00462 //  Description: 
00463 ////////////////////////////////////////////////////////////////////
00464 INLINE void PNMImageHeader::PixelSpec::
00465 set_red(xelval red) {
00466   _red = red;
00467 }
00468 
00469 ////////////////////////////////////////////////////////////////////
00470 //     Function: PNMImageHeader::PixelSpec::set_green
00471 //       Access: Published
00472 //  Description: 
00473 ////////////////////////////////////////////////////////////////////
00474 INLINE void PNMImageHeader::PixelSpec::
00475 set_green(xelval green) {
00476   _green = green;
00477 }
00478 
00479 ////////////////////////////////////////////////////////////////////
00480 //     Function: PNMImageHeader::PixelSpec::set_blue
00481 //       Access: Published
00482 //  Description: 
00483 ////////////////////////////////////////////////////////////////////
00484 INLINE void PNMImageHeader::PixelSpec::
00485 set_blue(xelval blue) {
00486   _blue = blue;
00487 }
00488 
00489 ////////////////////////////////////////////////////////////////////
00490 //     Function: PNMImageHeader::PixelSpec::set_alpha
00491 //       Access: Published
00492 //  Description: 
00493 ////////////////////////////////////////////////////////////////////
00494 INLINE void PNMImageHeader::PixelSpec::
00495 set_alpha(xelval alpha) {
00496   _alpha = alpha;
00497 }
00498 
00499 ////////////////////////////////////////////////////////////////////
00500 //     Function: PNMImageHeader::PixelSpec::operator []
00501 //       Access: Published
00502 //  Description: Indexes numerically into the components, in the order
00503 //               R, G, B, A.  This also makes the PixelSpec work like
00504 //               a tuple in Python.
00505 ////////////////////////////////////////////////////////////////////
00506 INLINE xelval PNMImageHeader::PixelSpec::
00507 operator [](int n) const {
00508   nassertr(n >= 0 && n < size(), 0);
00509   return (&_red)[n];
00510 }
00511 
00512 ////////////////////////////////////////////////////////////////////
00513 //     Function: PNMImageHeader::PixelSpec::size
00514 //       Access: Published, Static
00515 //  Description: Specifies the number of components in the PixelSpec;
00516 //               this is always 4, regardless of the type of image it
00517 //               was taken from.
00518 ////////////////////////////////////////////////////////////////////
00519 INLINE int PNMImageHeader::PixelSpec::
00520 size() {
00521   return 4;
00522 }
00523 
00524 // Interrogate seems to have some problem with the syntax of this
00525 // method.  Whatever, we don't need it.
00526 #ifndef CPPPARSER
00527 ////////////////////////////////////////////////////////////////////
00528 //     Function: PNMImageHeader::PixelSpecCount::Constructor
00529 //       Access: Public
00530 //  Description: 
00531 ////////////////////////////////////////////////////////////////////
00532 INLINE PNMImageHeader::PixelSpecCount::
00533 PixelSpecCount(const PNMImageHeader::PixelSpec &pixel, int count) :
00534   _pixel(pixel),
00535   _count(count)
00536 {
00537 }
00538 #endif  // CPPPARSER
00539 
00540 ////////////////////////////////////////////////////////////////////
00541 //     Function: PNMImageHeader::PixelSpecCount::operator <
00542 //       Access: Public
00543 //  Description: Used to sort the pixels in order from most common to
00544 //               least common.
00545 ////////////////////////////////////////////////////////////////////
00546 INLINE bool PNMImageHeader::PixelSpecCount::
00547 operator < (const PNMImageHeader::PixelSpecCount &other) const {
00548   return _count > other._count;
00549 }
00550 
00551 ////////////////////////////////////////////////////////////////////
00552 //     Function: PNMImageHeader::Histogram::Constructor
00553 //       Access: Published
00554 //  Description: 
00555 ////////////////////////////////////////////////////////////////////
00556 INLINE PNMImageHeader::Histogram::
00557 Histogram() {
00558 }
00559 
00560 ////////////////////////////////////////////////////////////////////
00561 //     Function: PNMImageHeader::Histogram::get_num_pixels
00562 //       Access: Published
00563 //  Description: Returns the number of unique pixel colors in the
00564 //               histogram.
00565 ////////////////////////////////////////////////////////////////////
00566 INLINE int PNMImageHeader::Histogram::
00567 get_num_pixels() const {
00568   return _pixels.size();
00569 }
00570 
00571 ////////////////////////////////////////////////////////////////////
00572 //     Function: PNMImageHeader::Histogram::get_pixel
00573 //       Access: Published
00574 //  Description: Returns the nth unique pixel color in the histogram.
00575 //               These are ordered by default from most common to
00576 //               least common.
00577 ////////////////////////////////////////////////////////////////////
00578 INLINE const PNMImageHeader::PixelSpec &PNMImageHeader::Histogram::
00579 get_pixel(int n) const {
00580   nassertr(n >= 0 && n < (int)_pixels.size(), _pixels[0]._pixel);
00581   return _pixels[n]._pixel;
00582 }
00583 
00584 ////////////////////////////////////////////////////////////////////
00585 //     Function: PNMImageHeader::Histogram::get_count
00586 //       Access: Published
00587 //  Description: Returns the number of occurrences in the image of the
00588 //               nth unique pixel color in the histogram.
00589 ////////////////////////////////////////////////////////////////////
00590 INLINE int PNMImageHeader::Histogram::
00591 get_count(int n) const {
00592   nassertr(n >= 0 && n < (int)_pixels.size(), 0);
00593   return _pixels[n]._count;
00594 }
00595 
00596 ////////////////////////////////////////////////////////////////////
00597 //     Function: PNMImageHeader::Histogram::get_count
00598 //       Access: Published
00599 //  Description: Returns the number of occurrences in the image of the
00600 //               indicated pixel color.
00601 ////////////////////////////////////////////////////////////////////
00602 INLINE int PNMImageHeader::Histogram::
00603 get_count(const PNMImageHeader::PixelSpec &pixel) const {
00604   HistMap::const_iterator hi;
00605   hi = _hist_map.find(pixel);
00606   if (hi == _hist_map.end()) {
00607     return 0;
00608   }
00609   return (*hi).second;
00610 }
00611 
00612 ////////////////////////////////////////////////////////////////////
00613 //     Function: PNMImageHeader::Histogram::swap
00614 //       Access: Public
00615 //  Description: Swaps the data in the Histogram with the indicated
00616 //               data.  This is normally used to load the Histogram
00617 //               data initially in PNMImage::make_histogram().
00618 ////////////////////////////////////////////////////////////////////
00619 INLINE void PNMImageHeader::Histogram::
00620 swap(PixelCount &pixels, HistMap &hist_map) {
00621   _pixels.swap(pixels);
00622   _hist_map.swap(hist_map);
00623 }
 All Classes Functions Variables Enumerations