Panda3D
 All Classes Functions Variables Enumerations
pnmImageHeader.I
1 // Filename: pnmImageHeader.I
2 // Created by: drose (15Jun00)
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 ////////////////////////////////////////////////////////////////////
16 // Function: PNMImageHeader::Constructor
17 // Access: Published
18 // Description:
19 ////////////////////////////////////////////////////////////////////
20 INLINE PNMImageHeader::
21 PNMImageHeader() {
22  _x_size = 0;
23  _y_size = 0;
24  _num_channels = 0;
25  _maxval = 255;
26  _color_space = CS_unspecified;
27  _type = (PNMFileType *)NULL;
28 }
29 
30 ////////////////////////////////////////////////////////////////////
31 // Function: PNMImageHeader::Copy Constructor
32 // Access: Published
33 // Description:
34 ////////////////////////////////////////////////////////////////////
35 INLINE PNMImageHeader::
36 PNMImageHeader(const PNMImageHeader &copy) :
37  _x_size(copy._x_size),
38  _y_size(copy._y_size),
39  _num_channels(copy._num_channels),
40  _maxval(copy._maxval),
41  _color_space(copy._color_space),
42  _type(copy._type)
43 {
44 }
45 
46 ////////////////////////////////////////////////////////////////////
47 // Function: PNMImageHeader::Copy Assignment Operator
48 // Access: Published
49 // Description:
50 ////////////////////////////////////////////////////////////////////
51 INLINE void PNMImageHeader::
52 operator = (const PNMImageHeader &copy) {
53  _x_size = copy._x_size;
54  _y_size = copy._y_size;
55  _num_channels = copy._num_channels;
56  _maxval = copy._maxval;
57  _color_space = copy._color_space;
58  _comment = copy._comment;
59  _type = copy._type;
60 }
61 
62 ////////////////////////////////////////////////////////////////////
63 // Function: PNMImageHeader::Destructor
64 // Access: Published
65 // Description:
66 ////////////////////////////////////////////////////////////////////
67 INLINE PNMImageHeader::
68 ~PNMImageHeader() {
69 }
70 
71 ////////////////////////////////////////////////////////////////////
72 // Function: PNMImageHeader::get_color_type
73 // Access: Published
74 // Description: Returns the image type of the image, as an enumerated
75 // value. This is really just the number of channels
76 // cast to the enumerated type.
77 ////////////////////////////////////////////////////////////////////
78 INLINE PNMImageHeader::ColorType PNMImageHeader::
79 get_color_type() const {
80  nassertr(_num_channels >= 1 && _num_channels <= 4, CT_invalid);
81  return (ColorType)_num_channels;
82 }
83 
84 ////////////////////////////////////////////////////////////////////
85 // Function: PNMImageHeader::get_num_channels
86 // Access: Published
87 // Description: Returns the number of channels in the image.
88 ////////////////////////////////////////////////////////////////////
89 INLINE int PNMImageHeader::
91  nassertr(_num_channels >= 1 && _num_channels <= 4, 0);
92  return _num_channels;
93 }
94 
95 ////////////////////////////////////////////////////////////////////
96 // Function: PNMImageHeader::is_grayscale
97 // Access: Published, Static
98 // Description: This static variant of is_grayscale() returns true if
99 // the indicated image type represents a grayscale
100 // image, false otherwise.
101 ////////////////////////////////////////////////////////////////////
102 INLINE bool PNMImageHeader::
103 is_grayscale(PNMImageHeader::ColorType color_type) {
104  return (color_type == CT_grayscale || color_type == CT_two_channel);
105 }
106 
107 ////////////////////////////////////////////////////////////////////
108 // Function: PNMImageHeader::is_grayscale
109 // Access: Published
110 // Description: Returns false if the image is a full-color image, and
111 // has red, green, and blue components; true if it is a
112 // grayscale image and has only a gray component. (The
113 // gray color is actually stored in the blue channel,
114 // and the red and green channels are ignored.)
115 ////////////////////////////////////////////////////////////////////
116 INLINE bool PNMImageHeader::
117 is_grayscale() const {
118  return is_grayscale(get_color_type());
119 }
120 
121 ////////////////////////////////////////////////////////////////////
122 // Function: PNMImageHeader::has_alpha
123 // Access: Published, Static
124 // Description: This static variant of has_alpha() returns true if
125 // the indicated image type includes an alpha channel,
126 // false otherwise.
127 ////////////////////////////////////////////////////////////////////
128 INLINE bool PNMImageHeader::
129 has_alpha(PNMImageHeader::ColorType color_type) {
130  return (color_type == CT_two_channel || color_type == CT_four_channel);
131 }
132 
133 ////////////////////////////////////////////////////////////////////
134 // Function: PNMImageHeader::has_alpha
135 // Access: Published
136 // Description: Returns true if the image includes an alpha channel,
137 // false otherwise. Unlike is_grayscale(), if this
138 // returns false it is an error to call any of the
139 // functions accessing the alpha channel.
140 ////////////////////////////////////////////////////////////////////
141 INLINE bool PNMImageHeader::
142 has_alpha() const {
143  return has_alpha(get_color_type());
144 }
145 
146 ////////////////////////////////////////////////////////////////////
147 // Function: PNMImageHeader::get_maxval
148 // Access: Published
149 // Description: Returns the maximum channel value allowable for any
150 // pixel in this image; for instance, 255 for a typical
151 // 8-bit-per-channel image. A pixel with this value is
152 // full on.
153 ////////////////////////////////////////////////////////////////////
154 INLINE xelval PNMImageHeader::
155 get_maxval() const {
156  return _maxval;
157 }
158 
159 ////////////////////////////////////////////////////////////////////
160 // Function: PNMImageHeader::get_color_space
161 // Access: Published
162 // Description: Returns the color space that the image is encoded
163 // in, or CS_unspecified if unknown.
164 ////////////////////////////////////////////////////////////////////
165 INLINE ColorSpace PNMImageHeader::
167  return _color_space;
168 }
169 
170 ////////////////////////////////////////////////////////////////////
171 // Function: PNMImageHeader::get_x_size
172 // Access: Published
173 // Description: Returns the number of pixels in the X direction.
174 // This is one more than the largest allowable X
175 // coordinate.
176 ////////////////////////////////////////////////////////////////////
177 INLINE int PNMImageHeader::
178 get_x_size() const {
179  return _x_size;
180 }
181 
182 ////////////////////////////////////////////////////////////////////
183 // Function: PNMImageHeader::get_y_size
184 // Access: Published
185 // Description: Returns the number of pixels in the Y direction.
186 // This is one more than the largest allowable Y
187 // coordinate.
188 ////////////////////////////////////////////////////////////////////
189 INLINE int PNMImageHeader::
190 get_y_size() const {
191  return _y_size;
192 }
193 
194 ////////////////////////////////////////////////////////////////////
195 // Function: PNMImageHeader::get_comment
196 // Access: Published
197 // Description: Gets the user comment from the file.
198 ////////////////////////////////////////////////////////////////////
199 INLINE string PNMImageHeader::
200 get_comment() const {
201  return _comment;
202 }
203 
204 ////////////////////////////////////////////////////////////////////
205 // Function: PNMImageHeader::set_comment
206 // Access: Published
207 // Description: Writes a user comment string to the image (header).
208 ////////////////////////////////////////////////////////////////////
209 INLINE void PNMImageHeader::
210 set_comment(const string& comment) {
211  _comment = comment;
212 }
213 
214 ////////////////////////////////////////////////////////////////////
215 // Function: PNMImageHeader::has_type
216 // Access: Published
217 // Description: Returns true if the PNMImageHeader knows what type it
218 // is, false otherwise.
219 ////////////////////////////////////////////////////////////////////
220 INLINE bool PNMImageHeader::
221 has_type() const {
222  return _type != (PNMFileType *)NULL;
223 }
224 
225 ////////////////////////////////////////////////////////////////////
226 // Function: PNMImageHeader::get_type
227 // Access: Published
228 // Description: If the file type is known (e.g. has_type() returns
229 // true), returns its PNMFileType pointer; otherwise,
230 // returns NULL.
231 ////////////////////////////////////////////////////////////////////
233 get_type() const {
234  return _type;
235 }
236 
237 ////////////////////////////////////////////////////////////////////
238 // Function: PNMImageHeader::set_type
239 // Access: Published
240 // Description: Sets the file type of this PNMImage. This will be
241 // the default type used when an image is read, if the
242 // type cannot be determined by magic number or inferred
243 // by extension, or the type used when the image is
244 // written, if the type cannot be inferred from the
245 // filename extension.
246 ////////////////////////////////////////////////////////////////////
247 INLINE void PNMImageHeader::
249  _type = type;
250 }
251 
252 ////////////////////////////////////////////////////////////////////
253 // Function: PNMImageHeader::record_color
254 // Access: Protected
255 // Description: Records the indicated color in the histogram.
256 ////////////////////////////////////////////////////////////////////
257 INLINE void PNMImageHeader::
258 record_color(PNMImageHeader::HistMap &hist,
260  // First, try to add the color with a count of 0, in case it does
261  // not already exist in the table.
262  HistMap::iterator hi = hist.insert(HistMap::value_type(color, 0)).first;
263 
264  // Now that either succeeded or failed, but either way hi is now the
265  // iterator to the count value in the table associated with the
266  // given color. Increment that count.
267  (*hi).second++;
268 }
269 
270 ////////////////////////////////////////////////////////////////////
271 // Function: PNMImageHeader::PixelSpec::Constructor
272 // Access: Published
273 // Description:
274 ////////////////////////////////////////////////////////////////////
275 INLINE PNMImageHeader::PixelSpec::
276 PixelSpec(xelval gray_value) :
277  _red(gray_value),
278  _green(gray_value),
279  _blue(gray_value),
280  _alpha(0)
281 {
282 }
283 
284 ////////////////////////////////////////////////////////////////////
285 // Function: PNMImageHeader::PixelSpec::Constructor
286 // Access: Published
287 // Description:
288 ////////////////////////////////////////////////////////////////////
289 INLINE PNMImageHeader::PixelSpec::
290 PixelSpec(xelval gray_value, xelval alpha) :
291  _red(gray_value),
292  _green(gray_value),
293  _blue(gray_value),
294  _alpha(alpha)
295 {
296 }
297 
298 ////////////////////////////////////////////////////////////////////
299 // Function: PNMImageHeader::PixelSpec::Constructor
300 // Access: Published
301 // Description:
302 ////////////////////////////////////////////////////////////////////
303 INLINE PNMImageHeader::PixelSpec::
304 PixelSpec(xelval red, xelval green, xelval blue) :
305  _red(red),
306  _green(green),
307  _blue(blue),
308  _alpha(0)
309 {
310 }
311 
312 ////////////////////////////////////////////////////////////////////
313 // Function: PNMImageHeader::PixelSpec::Constructor
314 // Access: Published
315 // Description:
316 ////////////////////////////////////////////////////////////////////
317 INLINE PNMImageHeader::PixelSpec::
318 PixelSpec(xelval red, xelval green, xelval blue, xelval alpha) :
319  _red(red),
320  _green(green),
321  _blue(blue),
322  _alpha(alpha)
323 {
324 }
325 
326 ////////////////////////////////////////////////////////////////////
327 // Function: PNMImageHeader::PixelSpec::Constructor
328 // Access: Published
329 // Description:
330 ////////////////////////////////////////////////////////////////////
331 INLINE PNMImageHeader::PixelSpec::
332 PixelSpec(const xel &rgb) :
333  _red(PPM_GETR(rgb)),
334  _green(PPM_GETG(rgb)),
335  _blue(PPM_GETB(rgb)),
336  _alpha(0)
337 {
338 }
339 
340 ////////////////////////////////////////////////////////////////////
341 // Function: PNMImageHeader::PixelSpec::Constructor
342 // Access: Published
343 // Description:
344 ////////////////////////////////////////////////////////////////////
345 INLINE PNMImageHeader::PixelSpec::
346 PixelSpec(const xel &rgb, xelval alpha) :
347  _red(PPM_GETR(rgb)),
348  _green(PPM_GETG(rgb)),
349  _blue(PPM_GETB(rgb)),
350  _alpha(alpha)
351 {
352 }
353 
354 ////////////////////////////////////////////////////////////////////
355 // Function: PNMImageHeader::PixelSpec::Copy Constructor
356 // Access: Published
357 // Description:
358 ////////////////////////////////////////////////////////////////////
359 INLINE PNMImageHeader::PixelSpec::
360 PixelSpec(const PixelSpec &copy) :
361  _red(copy._red),
362  _green(copy._green),
363  _blue(copy._blue),
364  _alpha(copy._alpha)
365 {
366 }
367 
368 ////////////////////////////////////////////////////////////////////
369 // Function: PNMImageHeader::PixelSpec::Copy Assignment Operator
370 // Access: Published
371 // Description:
372 ////////////////////////////////////////////////////////////////////
373 INLINE void PNMImageHeader::PixelSpec::
374 operator = (const PixelSpec &copy) {
375  _red = copy._red;
376  _green = copy._green;
377  _blue = copy._blue;
378  _alpha = copy._alpha;
379 }
380 
381 ////////////////////////////////////////////////////////////////////
382 // Function: PNMImageHeader::PixelSpec::operator <
383 // Access: Published
384 // Description:
385 ////////////////////////////////////////////////////////////////////
386 INLINE bool PNMImageHeader::PixelSpec::
387 operator < (const PixelSpec &other) const {
388  return compare_to(other) < 0;
389 }
390 
391 ////////////////////////////////////////////////////////////////////
392 // Function: PNMImageHeader::PixelSpec::operator ==
393 // Access: Published
394 // Description:
395 ////////////////////////////////////////////////////////////////////
396 INLINE bool PNMImageHeader::PixelSpec::
397 operator == (const PixelSpec &other) const {
398  return compare_to(other) == 0;
399 }
400 
401 ////////////////////////////////////////////////////////////////////
402 // Function: PNMImageHeader::PixelSpec::operator !=
403 // Access: Published
404 // Description:
405 ////////////////////////////////////////////////////////////////////
406 INLINE bool PNMImageHeader::PixelSpec::
407 operator != (const PixelSpec &other) const {
408  return compare_to(other) != 0;
409 }
410 
411 ////////////////////////////////////////////////////////////////////
412 // Function: PNMImageHeader::PixelSpec::compare_to
413 // Access: Published
414 // Description:
415 ////////////////////////////////////////////////////////////////////
416 INLINE int PNMImageHeader::PixelSpec::
417 compare_to(const PixelSpec &other) const {
418  if (_red != other._red) {
419  return _red < other._red ? -1 : 1;
420  }
421  if (_green != other._green) {
422  return _green < other._green ? -1 : 1;
423  }
424  if (_blue != other._blue) {
425  return _blue < other._blue ? -1 : 1;
426  }
427  if (_alpha != other._alpha) {
428  return _alpha < other._alpha ? -1 : 1;
429  }
430  return 0;
431 }
432 
433 ////////////////////////////////////////////////////////////////////
434 // Function: PNMImageHeader::PixelSpec::get_red
435 // Access: Published
436 // Description:
437 ////////////////////////////////////////////////////////////////////
438 INLINE xelval PNMImageHeader::PixelSpec::
439 get_red() const {
440  return _red;
441 }
442 
443 ////////////////////////////////////////////////////////////////////
444 // Function: PNMImageHeader::PixelSpec::get_green
445 // Access: Published
446 // Description:
447 ////////////////////////////////////////////////////////////////////
448 INLINE xelval PNMImageHeader::PixelSpec::
449 get_green() const {
450  return _green;
451 }
452 
453 ////////////////////////////////////////////////////////////////////
454 // Function: PNMImageHeader::PixelSpec::get_blue
455 // Access: Published
456 // Description:
457 ////////////////////////////////////////////////////////////////////
458 INLINE xelval PNMImageHeader::PixelSpec::
459 get_blue() const {
460  return _blue;
461 }
462 
463 ////////////////////////////////////////////////////////////////////
464 // Function: PNMImageHeader::PixelSpec::get_alpha
465 // Access: Published
466 // Description:
467 ////////////////////////////////////////////////////////////////////
468 INLINE xelval PNMImageHeader::PixelSpec::
469 get_alpha() const {
470  return _alpha;
471 }
472 
473 ////////////////////////////////////////////////////////////////////
474 // Function: PNMImageHeader::PixelSpec::set_red
475 // Access: Published
476 // Description:
477 ////////////////////////////////////////////////////////////////////
478 INLINE void PNMImageHeader::PixelSpec::
479 set_red(xelval red) {
480  _red = red;
481 }
482 
483 ////////////////////////////////////////////////////////////////////
484 // Function: PNMImageHeader::PixelSpec::set_green
485 // Access: Published
486 // Description:
487 ////////////////////////////////////////////////////////////////////
488 INLINE void PNMImageHeader::PixelSpec::
489 set_green(xelval green) {
490  _green = green;
491 }
492 
493 ////////////////////////////////////////////////////////////////////
494 // Function: PNMImageHeader::PixelSpec::set_blue
495 // Access: Published
496 // Description:
497 ////////////////////////////////////////////////////////////////////
498 INLINE void PNMImageHeader::PixelSpec::
499 set_blue(xelval blue) {
500  _blue = blue;
501 }
502 
503 ////////////////////////////////////////////////////////////////////
504 // Function: PNMImageHeader::PixelSpec::set_alpha
505 // Access: Published
506 // Description:
507 ////////////////////////////////////////////////////////////////////
508 INLINE void PNMImageHeader::PixelSpec::
509 set_alpha(xelval alpha) {
510  _alpha = alpha;
511 }
512 
513 ////////////////////////////////////////////////////////////////////
514 // Function: PNMImageHeader::PixelSpec::operator []
515 // Access: Published
516 // Description: Indexes numerically into the components, in the order
517 // R, G, B, A. This also makes the PixelSpec work like
518 // a tuple in Python.
519 ////////////////////////////////////////////////////////////////////
520 INLINE xelval PNMImageHeader::PixelSpec::
521 operator [](int n) const {
522  nassertr(n >= 0 && n < size(), 0);
523  return (&_red)[n];
524 }
525 
526 ////////////////////////////////////////////////////////////////////
527 // Function: PNMImageHeader::PixelSpec::size
528 // Access: Published, Static
529 // Description: Specifies the number of components in the PixelSpec;
530 // this is always 4, regardless of the type of image it
531 // was taken from.
532 ////////////////////////////////////////////////////////////////////
534 size() {
535  return 4;
536 }
537 
538 // Interrogate seems to have some problem with the syntax of this
539 // method. Whatever, we don't need it.
540 #ifndef CPPPARSER
541 ////////////////////////////////////////////////////////////////////
542 // Function: PNMImageHeader::PixelSpecCount::Constructor
543 // Access: Public
544 // Description:
545 ////////////////////////////////////////////////////////////////////
546 INLINE PNMImageHeader::PixelSpecCount::
547 PixelSpecCount(const PNMImageHeader::PixelSpec &pixel, int count) :
548  _pixel(pixel),
549  _count(count)
550 {
551 }
552 #endif // CPPPARSER
553 
554 ////////////////////////////////////////////////////////////////////
555 // Function: PNMImageHeader::PixelSpecCount::operator <
556 // Access: Public
557 // Description: Used to sort the pixels in order from most common to
558 // least common.
559 ////////////////////////////////////////////////////////////////////
562  return _count > other._count;
563 }
564 
565 ////////////////////////////////////////////////////////////////////
566 // Function: PNMImageHeader::Histogram::Constructor
567 // Access: Published
568 // Description:
569 ////////////////////////////////////////////////////////////////////
570 INLINE PNMImageHeader::Histogram::
571 Histogram() {
572 }
573 
574 ////////////////////////////////////////////////////////////////////
575 // Function: PNMImageHeader::Histogram::get_num_pixels
576 // Access: Published
577 // Description: Returns the number of unique pixel colors in the
578 // histogram.
579 ////////////////////////////////////////////////////////////////////
581 get_num_pixels() const {
582  return _pixels.size();
583 }
584 
585 ////////////////////////////////////////////////////////////////////
586 // Function: PNMImageHeader::Histogram::get_pixel
587 // Access: Published
588 // Description: Returns the nth unique pixel color in the histogram.
589 // These are ordered by default from most common to
590 // least common.
591 ////////////////////////////////////////////////////////////////////
593 get_pixel(int n) const {
594  nassertr(n >= 0 && n < (int)_pixels.size(), _pixels[0]._pixel);
595  return _pixels[n]._pixel;
596 }
597 
598 ////////////////////////////////////////////////////////////////////
599 // Function: PNMImageHeader::Histogram::get_count
600 // Access: Published
601 // Description: Returns the number of occurrences in the image of the
602 // nth unique pixel color in the histogram.
603 ////////////////////////////////////////////////////////////////////
605 get_count(int n) const {
606  nassertr(n >= 0 && n < (int)_pixels.size(), 0);
607  return _pixels[n]._count;
608 }
609 
610 ////////////////////////////////////////////////////////////////////
611 // Function: PNMImageHeader::Histogram::get_count
612 // Access: Published
613 // Description: Returns the number of occurrences in the image of the
614 // indicated pixel color.
615 ////////////////////////////////////////////////////////////////////
618  HistMap::const_iterator hi;
619  hi = _hist_map.find(pixel);
620  if (hi == _hist_map.end()) {
621  return 0;
622  }
623  return (*hi).second;
624 }
625 
626 ////////////////////////////////////////////////////////////////////
627 // Function: PNMImageHeader::Histogram::swap
628 // Access: Public
629 // Description: Swaps the data in the Histogram with the indicated
630 // data. This is normally used to load the Histogram
631 // data initially in PNMImage::make_histogram().
632 ////////////////////////////////////////////////////////////////////
633 INLINE void PNMImageHeader::Histogram::
634 swap(PixelCount &pixels, HistMap &hist_map) {
635  _pixels.swap(pixels);
636  _hist_map.swap(hist_map);
637 }
bool operator<(const PixelSpecCount &other) const
Used to sort the pixels in order from most common to least common.
ColorSpace get_color_space() const
Returns the color space that the image is encoded in, or CS_unspecified if unknown.
PNMFileType * get_type() const
If the file type is known (e.g.
const PixelSpec & get_pixel(int n) const
Returns the nth unique pixel color in the histogram.
int get_num_channels() const
Returns the number of channels in the image.
void set_comment(const string &comment)
Writes a user comment string to the image (header).
This is the base class of a family of classes that represent particular image file types that PNMImag...
Definition: pnmFileType.h:35
int get_x_size() const
Returns the number of pixels in the X direction.
int get_y_size() const
Returns the number of pixels in the Y direction.
void set_type(PNMFileType *type)
Sets the file type of this PNMImage.
string get_comment() const
Gets the user comment from the file.
void swap(PixelCount &pixels, HistMap &hist_map)
Swaps the data in the Histogram with the indicated data.
xelval operator[](int n) const
Indexes numerically into the components, in the order R, G, B, A.
int get_num_pixels() const
Returns the number of unique pixel colors in the histogram.
bool has_alpha() const
Returns true if the image includes an alpha channel, false otherwise.
ColorType get_color_type() const
Returns the image type of the image, as an enumerated value.
bool is_grayscale() const
Returns false if the image is a full-color image, and has red, green, and blue components; true if it...
An STL function object class, this is intended to be used on any ordered collection of class objects ...
Definition: stl_compares.h:79
xelval get_maxval() const
Returns the maximum channel value allowable for any pixel in this image; for instance, 255 for a typical 8-bit-per-channel image.
This is the base class of PNMImage, PNMReader, and PNMWriter.
bool has_type() const
Returns true if the PNMImageHeader knows what type it is, false otherwise.
static int size()
Specifies the number of components in the PixelSpec; this is always 4, regardless of the type of imag...
int get_count(int n) const
Returns the number of occurrences in the image of the nth unique pixel color in the histogram...