From 0bf44b8cd5c8ee6a927f99924bad3cfe4781ac4a Mon Sep 17 00:00:00 2001 From: David Rose Date: Wed, 17 Mar 2004 06:55:01 +0000 Subject: [PATCH] add blend_sub_image --- panda/src/pnmimage/pnmImage.cxx | 50 +++++++++++++++++++++++++++++++++ panda/src/pnmimage/pnmImage.h | 3 ++ 2 files changed, 53 insertions(+) diff --git a/panda/src/pnmimage/pnmImage.cxx b/panda/src/pnmimage/pnmImage.cxx index 92febc0a17..b0663ecfc6 100644 --- a/panda/src/pnmimage/pnmImage.cxx +++ b/panda/src/pnmimage/pnmImage.cxx @@ -546,6 +546,56 @@ copy_sub_image(const PNMImage ©, int xto, int yto, } } +//////////////////////////////////////////////////////////////////// +// Function: PNMImage::blend_sub_image +// Access: Public +// Description: Behaves like copy_sub_image(), except the alpha +// channel of the copy is used to blend the copy into +// the destination image, instead of overwriting pixels +// unconditionally. +// +// If the copy has no alpha channel, this degenerates +// into copy_sub_image(). +//////////////////////////////////////////////////////////////////// +void PNMImage:: +blend_sub_image(const PNMImage ©, int xto, int yto, + int xfrom, int yfrom, int x_size, int y_size) { + if (!copy.has_alpha()) { + copy_sub_image(copy, xto, yto, xfrom, yfrom, x_size, y_size); + return; + } + + if (xfrom < 0) { + xto += -xfrom; + xfrom = 0; + } + if (yfrom < 0) { + yto += -yfrom; + yfrom = 0; + } + + x_size = (x_size < 0) ? + copy.get_x_size() : + min(x_size, copy.get_x_size() - xfrom); + y_size = (y_size < 0) ? + copy.get_y_size() : + min(y_size, copy.get_y_size() - yfrom); + + int xmin = max(0, xto); + int ymin = max(0, yto); + + int xmax = min(xmin + x_size, get_x_size()); + int ymax = min(ymin + y_size, get_y_size()); + + int x, y; + for (y = ymin; y < ymax; y++) { + for (x = xmin; x < xmax; x++) { + blend(x, y, copy.get_xel(x - xmin + xfrom, y - ymin + yfrom), + copy.get_alpha(x - xmin + xfrom, y - ymin + yfrom)); + } + } +} + //////////////////////////////////////////////////////////////////// // Function: PNMImage::setup_rc diff --git a/panda/src/pnmimage/pnmImage.h b/panda/src/pnmimage/pnmImage.h index 3263fab09a..460eee86b9 100644 --- a/panda/src/pnmimage/pnmImage.h +++ b/panda/src/pnmimage/pnmImage.h @@ -175,6 +175,9 @@ public: void copy_sub_image(const PNMImage ©, int xto, int yto, int xfrom = 0, int yfrom = 0, int x_size = -1, int y_size = -1); + void blend_sub_image(const PNMImage ©, int xto, int yto, + int xfrom = 0, int yfrom = 0, + int x_size = -1, int y_size = -1); // The bodies for the non-inline *_filter() functions can be found // in the file pnm-image-filter.cxx.