Panda3D
 All Classes Functions Variables Enumerations
pnmimage_base.h
00001 // Filename: pnmimage_base.h
00002 // Created by:  drose (14Jun00)
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 #ifndef PNMIMAGE_BASE_H
00016 #define PNMIMAGE_BASE_H
00017 
00018 // This header file make a few typedefs and other definitions
00019 // essential to everything in the PNMImage package.
00020 
00021 #include "pandabase.h"
00022 #include "pnotify.h"
00023 
00024 // Since we no longer include pnm.h directly, we have to provide our
00025 // own definitions for xel and xelval.
00026 
00027 // For now, we have PGM_BIGGRAYS defined, which gives us 16-bit
00028 // channels.  Undefine this if you have memory problems and need to
00029 // use 8-bit channels instead.
00030 #define PGM_BIGGRAYS
00031 
00032 #ifdef PGM_BIGGRAYS
00033 typedef unsigned short gray;
00034 #define PGM_MAXMAXVAL 65535
00035 #else  // PGM_BIGGRAYS
00036 typedef unsigned char gray;
00037 #define PGM_MAXMAXVAL 255
00038 #endif  // PGM_BIGGRAYS
00039 
00040 #define PNM_MAXMAXVAL PGM_MAXMAXVAL
00041 
00042 struct pixel {
00043 PUBLISHED:
00044   pixel() { }
00045   pixel(gray r, gray g, gray b) : r(r), g(g), b(b) { }
00046   static int size() { return 3; }
00047   gray operator [](int i) const { nassertr(i >= 0 && i < 3, 0); return *(&r + i); }
00048   gray &operator [](int i) { nassertr(i >= 0 && i < 3, r); return *(&r + i); }
00049 #ifdef HAVE_PYTHON
00050   void __setitem__(int i, gray v) { operator[](i) = v; }
00051 #endif
00052   pixel operator + (const pixel &other) const
00053     { return pixel(r + other.r, g + other.g, b + other.b); }
00054   pixel operator - (const pixel &other) const
00055     { return pixel(r - other.r, g - other.g, b - other.b); }
00056   pixel operator * (const double mult) const
00057     { return pixel(r * mult, g * mult, b * mult); }
00058   void operator += (const pixel &other)
00059     { r += other.r; g += other.g; b += other.b; }
00060   void operator -= (const pixel &other)
00061     { r -= other.r; g -= other.g; b -= other.b; }
00062   void operator *= (const double mult)
00063     { r *= mult; g *= mult; b *= mult; }
00064 
00065   gray r, g, b;
00066 };
00067 
00068 typedef gray pixval;
00069 typedef pixel xel;
00070 typedef gray xelval;
00071 
00072 // These macros are borrowed from ppm.h.
00073 
00074 #define PPM_GETR(p) ((p).r)
00075 #define PPM_GETG(p) ((p).g)
00076 #define PPM_GETB(p) ((p).b)
00077 
00078 #define PPM_PUTR(p,red) ((p).r = (red))
00079 #define PPM_PUTG(p,grn) ((p).g = (grn))
00080 #define PPM_PUTB(p,blu) ((p).b = (blu))
00081 
00082 #define PPM_ASSIGN(p,red,grn,blu) { (p).r = (red); (p).g = (grn); (p).b = (blu); }
00083 #define PPM_EQUAL(p,q) ( (p).r == (q).r && (p).g == (q).g && (p).b == (q).b )
00084 #define PNM_ASSIGN1(x,v) PPM_ASSIGN(x,0,0,v)
00085 
00086 #define PPM_DEPTH(newp,p,oldmaxval,newmaxval) \
00087     PPM_ASSIGN( (newp), \
00088         ( (int) PPM_GETR(p) * (newmaxval) + (oldmaxval) / 2 ) / (oldmaxval), \
00089         ( (int) PPM_GETG(p) * (newmaxval) + (oldmaxval) / 2 ) / (oldmaxval), \
00090         ( (int) PPM_GETB(p) * (newmaxval) + (oldmaxval) / 2 ) / (oldmaxval) )
00091 
00092 
00093 // pnm defines these functions, and it's easier to emulate them than
00094 // to rewrite the code that calls them.
00095 EXPCL_PANDA_PNMIMAGE void pm_message(const char *format, ...);
00096 EXPCL_PANDA_PNMIMAGE void pm_error(const char *format, ...);  // doesn't return.
00097 
00098 EXPCL_PANDA_PNMIMAGE int pm_maxvaltobits(int maxval);
00099 EXPCL_PANDA_PNMIMAGE int pm_bitstomaxval(int bits);
00100 
00101 EXPCL_PANDA_PNMIMAGE char *pm_allocrow(int cols, int size);
00102 EXPCL_PANDA_PNMIMAGE void pm_freerow(char *itrow);
00103 
00104 EXPCL_PANDA_PNMIMAGE int pm_readbigshort(istream *in, short *sP);
00105 EXPCL_PANDA_PNMIMAGE int pm_writebigshort(ostream *out, short s);
00106 EXPCL_PANDA_PNMIMAGE int pm_readbiglong(istream *in, long *lP);
00107 EXPCL_PANDA_PNMIMAGE int pm_writebiglong(ostream *out, long l);
00108 EXPCL_PANDA_PNMIMAGE int pm_readlittleshort(istream *in, short *sP);
00109 EXPCL_PANDA_PNMIMAGE int pm_writelittleshort(ostream *out, short s);
00110 EXPCL_PANDA_PNMIMAGE int pm_readlittlelong(istream *in, long *lP);
00111 EXPCL_PANDA_PNMIMAGE int pm_writelittlelong(ostream *out, long l);
00112 
00113 
00114 // These ratios are used to compute the brightness of a colored pixel; they
00115 // define the relative contributions of each of the components.
00116 static const double lumin_red = 0.299;
00117 static const double lumin_grn = 0.587;
00118 static const double lumin_blu = 0.114;
00119 
00120 
00121 #endif
 All Classes Functions Variables Enumerations