Panda3D
pnmimage_base.h
Go to the documentation of this file.
1 /**
2  * PANDA 3D SOFTWARE
3  * Copyright (c) Carnegie Mellon University. All rights reserved.
4  *
5  * All use of this software is subject to the terms of the revised BSD
6  * license. You should have received a copy of this license along
7  * with this source code in a file named "LICENSE."
8  *
9  * @file pnmimage_base.h
10  * @author drose
11  * @date 2000-06-14
12  */
13 
14 #ifndef PNMIMAGE_BASE_H
15 #define PNMIMAGE_BASE_H
16 
17 // This header file make a few typedefs and other definitions essential to
18 // everything in the PNMImage package.
19 
20 #include "pandabase.h"
21 #include "pnotify.h"
22 
23 // Since we no longer include pnm.h directly, we have to provide our own
24 // definitions for xel and xelval.
25 
26 // For now, we have PGM_BIGGRAYS defined, which gives us 16-bit channels.
27 // Undefine this if you have memory problems and need to use 8-bit channels
28 // instead.
29 #define PGM_BIGGRAYS
30 
31 #ifdef PGM_BIGGRAYS
32 typedef unsigned short gray;
33 #define PGM_MAXMAXVAL 65535
34 #else // PGM_BIGGRAYS
35 typedef unsigned char gray;
36 #define PGM_MAXMAXVAL 255
37 #endif // PGM_BIGGRAYS
38 
39 #define PNM_MAXMAXVAL PGM_MAXMAXVAL
40 
41 struct pixel {
42 PUBLISHED:
43  pixel() = default;
44  pixel(gray fill) : r(fill), g(fill), b(fill) { }
45  pixel(gray r, gray g, gray b) : r(r), g(g), b(b) { }
46 
47  gray operator [](int i) const { nassertr(i >= 0 && i < 3, 0); return *(&r + i); }
48  gray &operator [](int i) { nassertr(i >= 0 && i < 3, r); return *(&r + i); }
49  pixel operator + (const pixel &other) const
50  { return pixel(r + other.r, g + other.g, b + other.b); }
51  pixel operator - (const pixel &other) const
52  { return pixel(r - other.r, g - other.g, b - other.b); }
53  pixel operator * (const double mult) const
54  { return pixel(r * mult, g * mult, b * mult); }
55  void operator += (const pixel &other)
56  { r += other.r; g += other.g; b += other.b; }
57  void operator -= (const pixel &other)
58  { r -= other.r; g -= other.g; b -= other.b; }
59  void operator *= (const double mult)
60  { r *= mult; g *= mult; b *= mult; }
61 
62 #ifdef HAVE_PYTHON
63  static int size() { return 3; }
64  void output(std::ostream &out) {
65  out << "pixel(r=" << r << ", g=" << g << ", b=" << b << ")";
66  }
67 #endif
68 
69  gray r, g, b;
70 };
71 
72 typedef gray pixval;
73 typedef pixel xel;
74 typedef gray xelval;
75 
76 // These macros are borrowed from ppm.h.
77 
78 #define PPM_GETR(p) ((p).r)
79 #define PPM_GETG(p) ((p).g)
80 #define PPM_GETB(p) ((p).b)
81 
82 #define PPM_PUTR(p,red) ((p).r = (red))
83 #define PPM_PUTG(p,grn) ((p).g = (grn))
84 #define PPM_PUTB(p,blu) ((p).b = (blu))
85 
86 #define PPM_ASSIGN(p,red,grn,blu) { (p).r = (red); (p).g = (grn); (p).b = (blu); }
87 #define PPM_EQUAL(p,q) ( (p).r == (q).r && (p).g == (q).g && (p).b == (q).b )
88 #define PNM_ASSIGN1(x,v) PPM_ASSIGN(x,0,0,v)
89 
90 #define PPM_DEPTH(newp,p,oldmaxval,newmaxval) \
91  PPM_ASSIGN( (newp), \
92  ( (int) PPM_GETR(p) * (newmaxval) + (oldmaxval) / 2 ) / (oldmaxval), \
93  ( (int) PPM_GETG(p) * (newmaxval) + (oldmaxval) / 2 ) / (oldmaxval), \
94  ( (int) PPM_GETB(p) * (newmaxval) + (oldmaxval) / 2 ) / (oldmaxval) )
95 
96 
97 // pnm defines these functions, and it's easier to emulate them than to
98 // rewrite the code that calls them.
99 EXPCL_PANDA_PNMIMAGE void pm_message(const char *format, ...);
100 EXPCL_PANDA_PNMIMAGE void pm_error(const char *format, ...); // doesn't return.
101 
102 EXPCL_PANDA_PNMIMAGE int pm_maxvaltobits(int maxval);
103 EXPCL_PANDA_PNMIMAGE int pm_bitstomaxval(int bits);
104 
105 EXPCL_PANDA_PNMIMAGE char *pm_allocrow(int cols, int size);
106 EXPCL_PANDA_PNMIMAGE void pm_freerow(char *itrow);
107 
108 EXPCL_PANDA_PNMIMAGE int pm_readbigshort(std::istream *in, short *sP);
109 EXPCL_PANDA_PNMIMAGE int pm_writebigshort(std::ostream *out, short s);
110 EXPCL_PANDA_PNMIMAGE int pm_readbiglong(std::istream *in, long *lP);
111 EXPCL_PANDA_PNMIMAGE int pm_writebiglong(std::ostream *out, long l);
112 EXPCL_PANDA_PNMIMAGE int pm_readlittleshort(std::istream *in, short *sP);
113 EXPCL_PANDA_PNMIMAGE int pm_writelittleshort(std::ostream *out, short s);
114 EXPCL_PANDA_PNMIMAGE int pm_readlittlelong(std::istream *in, long *lP);
115 EXPCL_PANDA_PNMIMAGE int pm_writelittlelong(std::ostream *out, long l);
116 
117 
118 // These ratios are used to compute the brightness of a colored pixel; they
119 // define the relative contributions of each of the components.
120 static const float lumin_red = 0.299f;
121 static const float lumin_grn = 0.587f;
122 static const float lumin_blu = 0.114f;
123 
124 
125 #endif
EXPCL_PANDA_PNMIMAGE int pm_maxvaltobits(int maxval)
Returns the number of bits sufficient to hold the indicated maxval value.
EXPCL_PANDA_PNMIMAGE void pm_message(const char *format,...)
Outputs the given printf-style message to the user and returns.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
EXPCL_PANDA_PNMIMAGE int pm_bitstomaxval(int bits)
Returns the highest maxval that can be represented in the indicated number of bits.
EXPCL_PANDA_PNMIMAGE void pm_freerow(char *itrow)
Frees the row previously allocated withm pm_allocrow().
EXPCL_PANDA_PNMIMAGE char * pm_allocrow(int cols, int size)
Allocates a row of cols * size bytes.
EXPCL_PANDA_PNMIMAGE void pm_error(const char *format,...)
Outputs the given printf-style message to the user and terminates messily.