diff --git a/panda/src/pnmimage/pnmImage.I b/panda/src/pnmimage/pnmImage.I index bcd533c74c..0213df23aa 100644 --- a/panda/src/pnmimage/pnmImage.I +++ b/panda/src/pnmimage/pnmImage.I @@ -375,7 +375,19 @@ make_rgb() { // Description: Returns the RGB color at the indicated pixel. Each // component is in the range 0..maxval. //////////////////////////////////////////////////////////////////// -INLINE const xel &PNMImage:: +INLINE xel &PNMImage:: +get_xel_val(int x, int y) { + nassertr(x >= 0 && x < _x_size && y >= 0 && y < _y_size, _array[0]); + return row(y)[x]; +} + +//////////////////////////////////////////////////////////////////// +// Function: PNMImage::get_xel_val +// Access: Published +// Description: Returns the RGB color at the indicated pixel. Each +// component is in the range 0..maxval. +//////////////////////////////////////////////////////////////////// +INLINE xel PNMImage:: get_xel_val(int x, int y) const { nassertr(x >= 0 && x < _x_size && y >= 0 && y < _y_size, _array[0]); return row(y)[x]; @@ -1049,28 +1061,6 @@ blend(int x, int y, const LRGBColorf &val, float alpha) { blend(x, y, val[0], val[1], val[2], alpha); } -//////////////////////////////////////////////////////////////////// -// Function: PNMImage::Array Operator -// Access: Published -// Description: Allows the PNMImage to appear to be a 2-d array of -// xels. -//////////////////////////////////////////////////////////////////// -INLINE xel *PNMImage:: -operator [] (int y) { - return row(y); -} - -//////////////////////////////////////////////////////////////////// -// Function: PNMImage::Array Operator -// Access: Published -// Description: Allows the PNMImage to appear to be a 2-d array of -// xels. -//////////////////////////////////////////////////////////////////// -INLINE const xel *PNMImage:: -operator [] (int y) const { - return row(y); -} - //////////////////////////////////////////////////////////////////// // Function: PNMImage::box_filter // Access: Published @@ -1160,6 +1150,157 @@ apply_exponent(float red_exponent, float green_exponent, float blue_exponent) { apply_exponent(red_exponent, green_exponent, blue_exponent, 1.0); } +//////////////////////////////////////////////////////////////////// +// Function: PNMImage::Row Constructor +//////////////////////////////////////////////////////////////////// +INLINE PNMImage::Row:: +Row(PNMImage &image, int y) : _image(image), _y(y) { + nassertv(y >= 0 && y < _image._y_size); +} + +//////////////////////////////////////////////////////////////////// +// Function: PNMImage::Row::size +// Access: Published +// Description: Get the number of pixels in the row. +//////////////////////////////////////////////////////////////////// +INLINE size_t PNMImage::Row:: +size() const { + return _image.get_x_size(); +} + +//////////////////////////////////////////////////////////////////// +// Function: PNMImage::Row::Array Operator +// Access: Published +// Description: Fetch the RGB value at the given column in the row. +//////////////////////////////////////////////////////////////////// +INLINE LColorf PNMImage::Row:: +operator[](int x) const { + return _image.get_xel_a(x, _y); +} + +#ifdef HAVE_PYTHON +//////////////////////////////////////////////////////////////////// +// Function: PNMImage::Row::Array Operator +// Access: Published +// Description: Set the pixel at the given column in the row. If +// the image has no alpha channel, the alpha component +// is ignored. +//////////////////////////////////////////////////////////////////// +INLINE void PNMImage::Row:: +__setitem__(int x, const LColorf &v) { + _image.set_xel_a(x, _y, v); +} +#endif + +//////////////////////////////////////////////////////////////////// +// Function: PNMImage::Row::Array Operator +// Access: Published +// Description: Fetch the pixel at the given column in the row. +//////////////////////////////////////////////////////////////////// +INLINE xel &PNMImage::Row:: +get_xel_val(int x) { + return _image.get_xel_val(x, _y); +} + +//////////////////////////////////////////////////////////////////// +// Function: PNMImage::Row::Array Operator +// Access: Published +// Description: Set the pixel at the given column in the row. +//////////////////////////////////////////////////////////////////// +INLINE void PNMImage::Row:: +set_xel_val(int x, const xel &v) { + _image.set_xel_val(x, _y, v); +} + +//////////////////////////////////////////////////////////////////// +// Function: PNMImage::Row::Array Operator +// Access: Published +// Description: Fetch the alpha value at the given column in the row. +//////////////////////////////////////////////////////////////////// +INLINE xelval PNMImage::Row:: +get_alpha_val(int x) const { + return _image.get_alpha_val(x, _y); +} + +//////////////////////////////////////////////////////////////////// +// Function: PNMImage::Row::Array Operator +// Access: Published +// Description: Set the alpha value at the given column in the row. +//////////////////////////////////////////////////////////////////// +INLINE void PNMImage::Row:: +set_alpha_val(int x, xelval v) { + _image.set_alpha_val(x, _y, v); +} + +//////////////////////////////////////////////////////////////////// +// Function: PNMImage::CRow Constructor +//////////////////////////////////////////////////////////////////// +INLINE PNMImage::CRow:: +CRow(const PNMImage &image, int y) : _image(image), _y(y) { + nassertv(y >= 0 && y < _image._y_size); +} + +//////////////////////////////////////////////////////////////////// +// Function: PNMImage::CRow::size +// Access: Published +// Description: Get the number of pixels in the row. +//////////////////////////////////////////////////////////////////// +INLINE size_t PNMImage::CRow:: +size() const { + return _image.get_x_size(); +} + +//////////////////////////////////////////////////////////////////// +// Function: PNMImage::CRow::Array Operator +// Access: Published +// Description: Fetch the RGB value at the given column in the row. +//////////////////////////////////////////////////////////////////// +INLINE LColorf PNMImage::CRow:: +operator[](int x) const { + return _image.get_xel_a(x, _y); +} + +//////////////////////////////////////////////////////////////////// +// Function: PNMImage::CRow::Array Operator +// Access: Published +// Description: Fetch the pixel at the given column in the row. +//////////////////////////////////////////////////////////////////// +INLINE xel PNMImage::CRow:: +get_xel_val(int x) const { + return _image.get_xel_val(x, _y); +} + +//////////////////////////////////////////////////////////////////// +// Function: PNMImage::CRow::Array Operator +// Access: Published +// Description: Fetch the alpha value at the given column in the row. +//////////////////////////////////////////////////////////////////// +INLINE xelval PNMImage::CRow:: +get_alpha_val(int x) const { + return _image.get_alpha_val(x, _y); +} + +//////////////////////////////////////////////////////////////////// +// Function: PNMImage::Array Operator +// Access: Published +// Description: Allows the PNMImage to appear to be a 2-d array of +// xels. +//////////////////////////////////////////////////////////////////// +INLINE PNMImage::Row PNMImage:: +operator [] (int y) { + return Row(*this, y); +} + +//////////////////////////////////////////////////////////////////// +// Function: PNMImage::Array Operator +// Access: Published +// Description: Allows the PNMImage to appear to be a 2-d array of +// xels. +//////////////////////////////////////////////////////////////////// +INLINE PNMImage::CRow PNMImage:: +operator [] (int y) const { + return CRow(*this, y); +} //////////////////////////////////////////////////////////////////// // Function: PNMImage::get_array diff --git a/panda/src/pnmimage/pnmImage.h b/panda/src/pnmimage/pnmImage.h index ed5ab05a1c..b01d5ec974 100644 --- a/panda/src/pnmimage/pnmImage.h +++ b/panda/src/pnmimage/pnmImage.h @@ -144,7 +144,8 @@ PUBLISHED: // in the normalized range [0..1]. These return values in the // image's stored color space. - INLINE const xel &get_xel_val(int x, int y) const; + INLINE xel &get_xel_val(int x, int y); + INLINE xel get_xel_val(int x, int y) const; INLINE void set_xel_val(int x, int y, const xel &value); INLINE void set_xel_val(int x, int y, xelval r, xelval g, xelval b); INLINE void set_xel_val(int x, int y, xelval gray); @@ -203,13 +204,6 @@ PUBLISHED: INLINE void blend(int x, int y, const LRGBColorf &val, float alpha); void blend(int x, int y, float r, float g, float b, float alpha); - // If you're used to the NetPBM library and like working with a 2-d - // array of xels, and using the PNM macros to access their components, - // you may treat the PNMImage as such directly. - - INLINE xel *operator [] (int y); - INLINE const xel *operator [] (int y) const; - void copy_sub_image(const PNMImage ©, int xto, int yto, int xfrom = 0, int yfrom = 0, int x_size = -1, int y_size = -1); @@ -281,6 +275,49 @@ PUBLISHED: void do_fill_distance(int xi, int yi, int d); +PUBLISHED: + // Provides an accessor for reading or writing the contents of one row + // of the image in-place. + class EXPCL_PANDA_PNMIMAGE Row { + PUBLISHED: + INLINE size_t size() const; + INLINE LColorf operator[](int x) const; +#ifdef HAVE_PYTHON + INLINE void __setitem__(int x, const LColorf &v); +#endif + INLINE xel &get_xel_val(int x); + INLINE void set_xel_val(int x, const xel &v); + INLINE xelval get_alpha_val(int x) const; + INLINE void set_alpha_val(int x, xelval v); + + public: + INLINE Row(PNMImage &image, int y); + + private: + PNMImage &_image; + int _y; + }; + + // Provides an accessor for reading the contents of one row of the + // image in-place. + class EXPCL_PANDA_PNMIMAGE CRow { + PUBLISHED: + INLINE size_t size() const; + INLINE LColorf operator[](int x) const; + INLINE xel get_xel_val(int x) const; + INLINE xelval get_alpha_val(int x) const; + + public: + INLINE CRow(const PNMImage &image, int y); + + private: + const PNMImage &_image; + int _y; + }; + + INLINE Row operator [] (int y); + INLINE CRow operator [] (int y) const; + public: // Know what you are doing if you access the underlying data arrays // directly. @@ -331,6 +368,8 @@ PUBLISHED: void operator *= (const LColorf &other); private: + friend class Row; + xel *_array; xelval *_alpha; float _default_rc, _default_gc, _default_bc; diff --git a/panda/src/pnmimage/pnmimage_base.h b/panda/src/pnmimage/pnmimage_base.h index 9adea47dd9..b75e6b0b74 100644 --- a/panda/src/pnmimage/pnmimage_base.h +++ b/panda/src/pnmimage/pnmimage_base.h @@ -59,6 +59,12 @@ PUBLISHED: void operator *= (const double mult) { r *= mult; g *= mult; b *= mult; } +#ifdef HAVE_PYTHON + void output(ostream &out) { + out << "pixel(r=" << r << ", g=" << g << ", b=" << b << ")"; + } +#endif + gray r, g, b; };