Panda3D
pnmimage_base.h
1 // Filename: pnmimage_base.h
2 // Created by: drose (14Jun00)
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 #ifndef PNMIMAGE_BASE_H
16 #define PNMIMAGE_BASE_H
17 
18 // This header file make a few typedefs and other definitions
19 // essential to everything in the PNMImage package.
20 
21 #include "pandabase.h"
22 #include "pnotify.h"
23 
24 // Since we no longer include pnm.h directly, we have to provide our
25 // own definitions for xel and xelval.
26 
27 // For now, we have PGM_BIGGRAYS defined, which gives us 16-bit
28 // channels. Undefine this if you have memory problems and need to
29 // use 8-bit channels instead.
30 #define PGM_BIGGRAYS
31 
32 #ifdef PGM_BIGGRAYS
33 typedef unsigned short gray;
34 #define PGM_MAXMAXVAL 65535
35 #else // PGM_BIGGRAYS
36 typedef unsigned char gray;
37 #define PGM_MAXMAXVAL 255
38 #endif // PGM_BIGGRAYS
39 
40 #define PNM_MAXMAXVAL PGM_MAXMAXVAL
41 
42 struct pixel {
43 PUBLISHED:
44  pixel() { }
45  pixel(gray r, gray g, gray b) : r(r), g(g), b(b) { }
46  static int size() { return 3; }
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  void output(ostream &out) {
64  out << "pixel(r=" << r << ", g=" << g << ", b=" << b << ")";
65  }
66 #endif
67 
68  gray r, g, b;
69 };
70 
71 typedef gray pixval;
72 typedef pixel xel;
73 typedef gray xelval;
74 
75 // These macros are borrowed from ppm.h.
76 
77 #define PPM_GETR(p) ((p).r)
78 #define PPM_GETG(p) ((p).g)
79 #define PPM_GETB(p) ((p).b)
80 
81 #define PPM_PUTR(p,red) ((p).r = (red))
82 #define PPM_PUTG(p,grn) ((p).g = (grn))
83 #define PPM_PUTB(p,blu) ((p).b = (blu))
84 
85 #define PPM_ASSIGN(p,red,grn,blu) { (p).r = (red); (p).g = (grn); (p).b = (blu); }
86 #define PPM_EQUAL(p,q) ( (p).r == (q).r && (p).g == (q).g && (p).b == (q).b )
87 #define PNM_ASSIGN1(x,v) PPM_ASSIGN(x,0,0,v)
88 
89 #define PPM_DEPTH(newp,p,oldmaxval,newmaxval) \
90  PPM_ASSIGN( (newp), \
91  ( (int) PPM_GETR(p) * (newmaxval) + (oldmaxval) / 2 ) / (oldmaxval), \
92  ( (int) PPM_GETG(p) * (newmaxval) + (oldmaxval) / 2 ) / (oldmaxval), \
93  ( (int) PPM_GETB(p) * (newmaxval) + (oldmaxval) / 2 ) / (oldmaxval) )
94 
95 
96 // pnm defines these functions, and it's easier to emulate them than
97 // to rewrite the code that calls them.
98 EXPCL_PANDA_PNMIMAGE void pm_message(const char *format, ...);
99 EXPCL_PANDA_PNMIMAGE void pm_error(const char *format, ...); // doesn't return.
100 
101 EXPCL_PANDA_PNMIMAGE int pm_maxvaltobits(int maxval);
102 EXPCL_PANDA_PNMIMAGE int pm_bitstomaxval(int bits);
103 
104 EXPCL_PANDA_PNMIMAGE char *pm_allocrow(int cols, int size);
105 EXPCL_PANDA_PNMIMAGE void pm_freerow(char *itrow);
106 
107 EXPCL_PANDA_PNMIMAGE int pm_readbigshort(istream *in, short *sP);
108 EXPCL_PANDA_PNMIMAGE int pm_writebigshort(ostream *out, short s);
109 EXPCL_PANDA_PNMIMAGE int pm_readbiglong(istream *in, long *lP);
110 EXPCL_PANDA_PNMIMAGE int pm_writebiglong(ostream *out, long l);
111 EXPCL_PANDA_PNMIMAGE int pm_readlittleshort(istream *in, short *sP);
112 EXPCL_PANDA_PNMIMAGE int pm_writelittleshort(ostream *out, short s);
113 EXPCL_PANDA_PNMIMAGE int pm_readlittlelong(istream *in, long *lP);
114 EXPCL_PANDA_PNMIMAGE int pm_writelittlelong(ostream *out, long l);
115 
116 
117 // These ratios are used to compute the brightness of a colored pixel; they
118 // define the relative contributions of each of the components.
119 static const float lumin_red = 0.299f;
120 static const float lumin_grn = 0.587f;
121 static const float lumin_blu = 0.114f;
122 
123 
124 #endif