Panda3D
Loading...
Searching...
No Matches
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
32typedef unsigned short gray;
33#define PGM_MAXMAXVAL 65535
34#else // PGM_BIGGRAYS
35typedef unsigned char gray;
36#define PGM_MAXMAXVAL 255
37#endif // PGM_BIGGRAYS
38
39#define PNM_MAXMAXVAL PGM_MAXMAXVAL
40
41struct pixel {
42PUBLISHED:
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 bool operator == (const pixel &other) {
63 return r == other.r && g == other.g && r == other.r;
64 }
65 bool operator != (const pixel &other) {
66 return r != other.r || g != other.g || r != other.r;
67 }
68 bool operator < (const pixel &other) const {
69 if (r != other.r) {
70 return r < other.r;
71 }
72 if (g != other.g) {
73 return g < other.g;
74 }
75 return b < other.b;
76 }
77
78#ifdef HAVE_PYTHON
79 static int size() { return 3; }
80 void output(std::ostream &out) {
81 out << "pixel(r=" << r << ", g=" << g << ", b=" << b << ")";
82 }
83#endif
84
85 gray r, g, b;
86};
87
88typedef gray pixval;
89typedef pixel xel;
90typedef gray xelval;
91
92// These macros are borrowed from ppm.h.
93
94#define PPM_GETR(p) ((p).r)
95#define PPM_GETG(p) ((p).g)
96#define PPM_GETB(p) ((p).b)
97
98#define PPM_PUTR(p,red) ((p).r = (red))
99#define PPM_PUTG(p,grn) ((p).g = (grn))
100#define PPM_PUTB(p,blu) ((p).b = (blu))
101
102#define PPM_ASSIGN(p,red,grn,blu) { (p).r = (red); (p).g = (grn); (p).b = (blu); }
103#define PPM_EQUAL(p,q) ( (p).r == (q).r && (p).g == (q).g && (p).b == (q).b )
104#define PNM_ASSIGN1(x,v) PPM_ASSIGN(x,0,0,v)
105
106#define PPM_DEPTH(newp,p,oldmaxval,newmaxval) \
107 PPM_ASSIGN( (newp), \
108 ( (int) PPM_GETR(p) * (newmaxval) + (oldmaxval) / 2 ) / (oldmaxval), \
109 ( (int) PPM_GETG(p) * (newmaxval) + (oldmaxval) / 2 ) / (oldmaxval), \
110 ( (int) PPM_GETB(p) * (newmaxval) + (oldmaxval) / 2 ) / (oldmaxval) )
111
112
113// pnm defines these functions, and it's easier to emulate them than to
114// rewrite the code that calls them.
115EXPCL_PANDA_PNMIMAGE void pm_message(const char *format, ...);
116EXPCL_PANDA_PNMIMAGE void pm_error(const char *format, ...); // doesn't return.
117
118EXPCL_PANDA_PNMIMAGE int pm_maxvaltobits(int maxval);
119EXPCL_PANDA_PNMIMAGE int pm_bitstomaxval(int bits);
120
121EXPCL_PANDA_PNMIMAGE char *pm_allocrow(int cols, int size);
122EXPCL_PANDA_PNMIMAGE void pm_freerow(char *itrow);
123
124EXPCL_PANDA_PNMIMAGE int pm_readbigshort(std::istream *in, short *sP);
125EXPCL_PANDA_PNMIMAGE int pm_writebigshort(std::ostream *out, short s);
126EXPCL_PANDA_PNMIMAGE int pm_readbiglong(std::istream *in, long *lP);
127EXPCL_PANDA_PNMIMAGE int pm_writebiglong(std::ostream *out, long l);
128EXPCL_PANDA_PNMIMAGE int pm_readlittleshort(std::istream *in, short *sP);
129EXPCL_PANDA_PNMIMAGE int pm_writelittleshort(std::ostream *out, short s);
130EXPCL_PANDA_PNMIMAGE int pm_readlittlelong(std::istream *in, long *lP);
131EXPCL_PANDA_PNMIMAGE int pm_writelittlelong(std::ostream *out, long l);
132
133
134// These ratios are used to compute the brightness of a colored pixel; they
135// define the relative contributions of each of the components.
136static const float lumin_red = 0.299f;
137static const float lumin_grn = 0.587f;
138static const float lumin_blu = 0.114f;
139
140
141#endif
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
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_freerow(char *itrow)
Frees the row previously allocated withm pm_allocrow().
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_error(const char *format,...)
Outputs the given printf-style message to the user and terminates messily.
EXPCL_PANDA_PNMIMAGE char * pm_allocrow(int cols, int size)
Allocates a row of cols * size bytes.
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.