From 64f9ae05cd81f2593fb5341b7e363382c2ad93c4 Mon Sep 17 00:00:00 2001 From: David Rose Date: Mon, 30 Jul 2001 16:41:59 +0000 Subject: [PATCH] retain fade images --- pandaapp/src/stitchbase/Sources.pp | 9 +- pandaapp/src/stitchbase/fadeImagePool.I | 30 +++++++ pandaapp/src/stitchbase/fadeImagePool.cxx | 100 ++++++++++++++++++++++ pandaapp/src/stitchbase/fadeImagePool.h | 65 ++++++++++++++ pandaapp/src/stitchbase/stitchImage.cxx | 30 +++---- 5 files changed, 212 insertions(+), 22 deletions(-) create mode 100644 pandaapp/src/stitchbase/fadeImagePool.I create mode 100644 pandaapp/src/stitchbase/fadeImagePool.cxx create mode 100644 pandaapp/src/stitchbase/fadeImagePool.h diff --git a/pandaapp/src/stitchbase/Sources.pp b/pandaapp/src/stitchbase/Sources.pp index 0ec5af464a..1ecae6ecac 100644 --- a/pandaapp/src/stitchbase/Sources.pp +++ b/pandaapp/src/stitchbase/Sources.pp @@ -11,7 +11,10 @@ m #define SOURCES \ - config_stitch.cxx config_stitch.h layeredImage.cxx layeredImage.h \ + config_stitch.cxx config_stitch.h \ + fadeImagePool.I fadeImagePool.cxx fadeImagePool.h \ + fixedPoint.h \ + layeredImage.cxx layeredImage.h \ morphGrid.cxx morphGrid.h stitchCommand.cxx stitchCommand.h \ stitchCommandReader.cxx stitchCommandReader.h \ stitchCylindricalLens.cxx stitchCylindricalLens.h stitchFile.cxx \ @@ -31,7 +34,9 @@ stitchScreen.cxx stitchScreen.h #define INSTALL_HEADERS \ - config_stitch.h fixedPoint.h layeredImage.h morphGrid.h stitchCommand.h \ + config_stitch.h \ + fadeImagePool.I fadeImagePool.h \ + fixedPoint.h layeredImage.h morphGrid.h stitchCommand.h \ stitchCommandReader.h stitchCylindricalLens.h stitchFile.h \ stitchFisheyeLens.h stitchImage.h stitchImageCommandOutput.h \ stitchImageOutputter.h stitchImageRasterizer.h stitchLens.h \ diff --git a/pandaapp/src/stitchbase/fadeImagePool.I b/pandaapp/src/stitchbase/fadeImagePool.I new file mode 100644 index 0000000000..3b8910093f --- /dev/null +++ b/pandaapp/src/stitchbase/fadeImagePool.I @@ -0,0 +1,30 @@ +// Filename: fadeImagePool.I +// Created by: drose (30Jul01) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) 2001, Disney Enterprises, Inc. All rights reserved +// +// All use of this software is subject to the terms of the Panda 3d +// Software license. You should have received a copy of this license +// along with this source code; you will also find a current copy of +// the license at http://www.panda3d.org/license.txt . +// +// To contact the maintainers of this program write to +// panda3d@yahoogroups.com . +// +//////////////////////////////////////////////////////////////////// + + +//////////////////////////////////////////////////////////////////// +// Function: FadeImagePool::get_image +// Access: Public +// Description: Returns a pointer to the fade image of the indicated +// filename, scaled to the indicated size, or NULL if +// the image cannot be loaded. +//////////////////////////////////////////////////////////////////// +INLINE const PNMImage *FadeImagePool:: +get_image(const Filename &filename, int x_size, int y_size) { + return get_ptr()->ns_get_image(filename, x_size, y_size); +} diff --git a/pandaapp/src/stitchbase/fadeImagePool.cxx b/pandaapp/src/stitchbase/fadeImagePool.cxx new file mode 100644 index 0000000000..0adba046f6 --- /dev/null +++ b/pandaapp/src/stitchbase/fadeImagePool.cxx @@ -0,0 +1,100 @@ +// Filename: fadeImagePool.cxx +// Created by: drose (30Jul01) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) 2001, Disney Enterprises, Inc. All rights reserved +// +// All use of this software is subject to the terms of the Panda 3d +// Software license. You should have received a copy of this license +// along with this source code; you will also find a current copy of +// the license at http://www.panda3d.org/license.txt . +// +// To contact the maintainers of this program write to +// panda3d@yahoogroups.com . +// +//////////////////////////////////////////////////////////////////// + +#include "fadeImagePool.h" +#include "pnmImage.h" + +FadeImagePool *FadeImagePool::_global_ptr = (FadeImagePool *)NULL; + +//////////////////////////////////////////////////////////////////// +// Function: FadeImagePool::Constructor +// Access: Private +// Description: The constructor is never called explicitly; there is +// only one FadeImagePool and it constructs itself. +//////////////////////////////////////////////////////////////////// +FadeImagePool:: +FadeImagePool() { +} + +//////////////////////////////////////////////////////////////////// +// Function: FadeImagePool::ns_get_image +// Access: Private +// Description: The nonstatic implementation of get_image(). This +// loads up the image if it is not already, scales it to +// the indicated size if it is not already, and returns +// it. +//////////////////////////////////////////////////////////////////// +const PNMImage *FadeImagePool:: +ns_get_image(const Filename &filename, int x_size, int y_size) { + Images::iterator ii = _images.find(filename); + if (ii == _images.end()) { + // The image has not yet been loaded. Load it. + cerr << "Reading fade image " << filename << "\n"; + PNMImage *image = new PNMImage(filename); + if (!image->is_valid()) { + cerr << "Unable to read fade image.\n"; + delete image; + return (const PNMImage *)NULL; + } + + // Make sure it's a grayscale image. This will save a bit of time + // later. + image->set_color_type(PNMImage::CT_grayscale); + + ii = _images.insert(Images::value_type(filename, ImageSizes())).first; + (*ii).second.push_back(image); + } + + // Now see if we have a fade image of the requested size. + ImageSizes &sizes = (*ii).second; + ImageSizes::iterator si; + for (si = sizes.begin(); si != sizes.end(); ++si) { + PNMImage *image = (*si); + if (image->get_x_size() == x_size && image->get_y_size() == y_size) { + // Here's one that suits! + return image; + } + } + + // None of our images were of a suitable size, so make one. + nassertr(!sizes.empty(), NULL); + + PNMImage *orig_image = sizes.front(); + cerr << "Resizing fade image to " << x_size << " by " << y_size << "\n"; + PNMImage *resized_image = + new PNMImage(x_size, y_size, PNMImage::CT_grayscale, + orig_image->get_maxval()); + resized_image->quick_filter_from(*orig_image); + + sizes.push_back(resized_image); + return resized_image; +} + +//////////////////////////////////////////////////////////////////// +// Function: FadeImagePool::get_ptr +// Access: Private, Static +// Description: Returns the global FadeImagePool pointer. +//////////////////////////////////////////////////////////////////// +FadeImagePool *FadeImagePool:: +get_ptr() { + if (_global_ptr == (FadeImagePool *)NULL) { + _global_ptr = new FadeImagePool; + } + return _global_ptr; +} + diff --git a/pandaapp/src/stitchbase/fadeImagePool.h b/pandaapp/src/stitchbase/fadeImagePool.h new file mode 100644 index 0000000000..61d8e888a5 --- /dev/null +++ b/pandaapp/src/stitchbase/fadeImagePool.h @@ -0,0 +1,65 @@ +// Filename: fadeImagePool.h +// Created by: drose (30Jul01) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) 2001, Disney Enterprises, Inc. All rights reserved +// +// All use of this software is subject to the terms of the Panda 3d +// Software license. You should have received a copy of this license +// along with this source code; you will also find a current copy of +// the license at http://www.panda3d.org/license.txt . +// +// To contact the maintainers of this program write to +// panda3d@yahoogroups.com . +// +//////////////////////////////////////////////////////////////////// + +#ifndef FADEIMAGEPOOL_H +#define FADEIMAGEPOOL_H + +#include "pandaappbase.h" + +#include "filename.h" +#include "pvector.h" +#include "pmap.h" + +class PNMImage; + +//////////////////////////////////////////////////////////////////// +// Class : FadeImagePool +// Description : This maintains a list of images loaded up as "fade" +// images--that is, grayscale images whose only purpose +// is to adjust the source image to dark at the edges. +// It guarantees that each named image is only loaded +// once. +// +// Images are never freed from this pool. The +// assumption is that you have plenty of RAM for dealing +// with images. +//////////////////////////////////////////////////////////////////// +class EXPCL_PANDA FadeImagePool { +public: + INLINE static const PNMImage *get_image(const Filename &filename, + int x_size, int y_size); + +private: + FadeImagePool(); + + const PNMImage *ns_get_image(const Filename &filename, int x_size, int y_size); + + static FadeImagePool *get_ptr(); + + typedef pvector ImageSizes; + typedef pmap Images; + Images _images; + + static FadeImagePool *_global_ptr; +}; + +#include "fadeImagePool.I" + +#endif + + diff --git a/pandaapp/src/stitchbase/stitchImage.cxx b/pandaapp/src/stitchbase/stitchImage.cxx index 1cc687b630..cc3df3a0f7 100644 --- a/pandaapp/src/stitchbase/stitchImage.cxx +++ b/pandaapp/src/stitchbase/stitchImage.cxx @@ -19,6 +19,7 @@ #include "stitchImage.h" #include "stitchLens.h" #include "layeredImage.h" +#include "fadeImagePool.h" #include "compose_matrix.h" #include "rotate_to.h" @@ -486,7 +487,7 @@ setup_pixel_scales() { // Access: Private // Description: Resizes the image generated in _data back to // _orig_size_pixels, if it has been generated larger, -// int preparation to writing it out. +// in preparation to writing it out. //////////////////////////////////////////////////////////////////// void StitchImage:: resize_data() { @@ -523,35 +524,24 @@ void StitchImage:: fade_out() { assert(_data != (PNMImage *)NULL); - cerr << "Reading fade image " << get_fade_filename() << "\n"; - PNMImage *fade = new PNMImage(get_fade_filename()); - if (!fade->is_valid()) { - cerr << "Unable to read fade image.\n"; + const PNMImage *fade = + FadeImagePool::get_image(get_fade_filename(), + _data->get_x_size(), _data->get_y_size()); + if (fade == (PNMImage *)NULL) { return; } - if (fade->get_x_size() != _data->get_x_size() || - fade->get_y_size() != _data->get_y_size()) { - cerr << "Resizing fade image to " << _data->get_x_size() - << " by " << _data->get_y_size() << "\n"; - PNMImage *resized_fade = - new PNMImage(_data->get_x_size(), _data->get_y_size(), - fade->get_num_channels(), fade->get_maxval()); - resized_fade->quick_filter_from(*fade); - delete fade; - fade = resized_fade; - } - // Now apply the fade factor to darken each of our pixels. cerr << "Applying fade image.\n"; for (int y = 0; y < _data->get_y_size(); y++) { for (int x = 0; x < _data->get_x_size(); x++) { - double bright = fade->get_bright(x, y); + // Because we know the fade image is guaranteed to be grayscale, + // we can simply ask for its gray component rather than + // computing its bright component. + double bright = fade->get_gray(x, y); _data->set_red_val(x, y, (xelval)_data->get_red_val(x, y) * bright); _data->set_green_val(x, y, (xelval)_data->get_green_val(x, y) * bright); _data->set_blue_val(x, y, (xelval)_data->get_blue_val(x, y) * bright); } } - - delete fade; }