diff --git a/panda/src/pnmimage/pnmImage.cxx b/panda/src/pnmimage/pnmImage.cxx index 465e11dc83..76a9747031 100644 --- a/panda/src/pnmimage/pnmImage.cxx +++ b/panda/src/pnmimage/pnmImage.cxx @@ -1159,6 +1159,41 @@ threshold(const PNMImage &select_image, int channel, double threshold, } } +//////////////////////////////////////////////////////////////////// +// Function: PNMImage::rescale +// Access: Published +// Description: Rescales the RGB channel values so that any values in +// the original image between min_val and max_val are +// expanded to the range 0 .. 1. Values below min_val +// are set to 0, and values above max_val are set to 1. +// Does not affect the alpha channel, if any. +//////////////////////////////////////////////////////////////////// +void PNMImage:: +rescale(double min_val, double max_val) { + double scale = max_val - min_val; + + if (_num_channels <= 2) { + // Grayscale. + for (int y = 0; y < get_y_size(); y++) { + for (int x = 0; x < get_x_size(); x++) { + double val = get_gray(x, y); + set_gray(x, y, (val - min_val) / scale); + } + } + } else { + // RGB(A). + for (int y = 0; y < get_y_size(); y++) { + for (int x = 0; x < get_x_size(); x++) { + LRGBColord xel = get_xel(x, y); + set_xel(x, y, + (xel[0] - min_val) / scale, + (xel[1] - min_val) / scale, + (xel[2] - min_val) / scale); + } + } + } +} + //////////////////////////////////////////////////////////////////// // Function: PNMImage::copy_channel // Access: Published diff --git a/panda/src/pnmimage/pnmImage.h b/panda/src/pnmimage/pnmImage.h index 33822a7261..166b3e3dc0 100644 --- a/panda/src/pnmimage/pnmImage.h +++ b/panda/src/pnmimage/pnmImage.h @@ -214,6 +214,8 @@ PUBLISHED: void threshold(const PNMImage &select_image, int channel, double threshold, const PNMImage <, const PNMImage &ge); + void rescale(double min_val, double max_val); + void copy_channel(const PNMImage ©, int xto, int yto, int cto, int xfrom = 0, int yfrom = 0, int cfrom = 0, int x_size = -1, int y_size = -1);