degate  0.1.2
Public Types | Public Member Functions | Private Member Functions | Private Attributes
degate::ScalingManager< ImageType > Class Template Reference

The ScalingManager creates prescaled images for a master image. More...

#include <ScalingManager.h>

List of all members.

Public Types

typedef std::pair< double,
std::shared_ptr< ImageType > > 
image_map_element
typedef std::map< double,
std::shared_ptr< ImageType > > 
image_map
typedef std::list< double > zoom_step_list

Public Member Functions

 ScalingManager (std::shared_ptr< ImageType > img, std::string const &base_directory, int min_size=1024)
 Create a new ScalingManager object for an image.
 ~ScalingManager ()
 Destroy a scaling manager.
const zoom_step_list get_zoom_steps () const
 Get a list of available zoom steps.
void create_scalings ()
 Create the scaled images.
image_map_element get_image (double request_scaling)
 Get the image with the nearest scaling value to the requested scaling.

Private Member Functions

unsigned long get_nearest_power_of_two (unsigned int value)

Private Attributes

std::string base_directory
image_map images
unsigned int min_size

Detailed Description

template<class ImageType>
class degate::ScalingManager< ImageType >

The ScalingManager creates prescaled images for a master image.

You can use the ScalingManager only for images of type TileImage. If you want to scale images for yourself, please check out method scale_down().

See also:
ImageManipulation::scale_down()

Definition at line 42 of file ScalingManager.h.


Member Typedef Documentation

template<class ImageType>
typedef std::map<double, std::shared_ptr<ImageType> > degate::ScalingManager< ImageType >::image_map

Definition at line 48 of file ScalingManager.h.

template<class ImageType>
typedef std::pair<double, std::shared_ptr<ImageType> > degate::ScalingManager< ImageType >::image_map_element

Definition at line 46 of file ScalingManager.h.

template<class ImageType>
typedef std::list<double> degate::ScalingManager< ImageType >::zoom_step_list

Definition at line 74 of file ScalingManager.h.


Constructor & Destructor Documentation

template<class ImageType>
degate::ScalingManager< ImageType >::ScalingManager ( std::shared_ptr< ImageType >  img,
std::string const &  base_directory,
int  min_size = 1024 
) [inline]

Create a new ScalingManager object for an image.

Parameters:
imgThe background image.
base_directoryA directory where all files can be stored. You can use the directory of the master image for that. Make sure that the directory exist. if you compile libdegate with DEBUG=1 the existence is assert()ed.
min_sizeCreate down scalings until the edge length is becomes less than min_size.

Definition at line 86 of file ScalingManager.h.

References degate::ScalingManager< ImageType >::base_directory, degate::file_exists(), degate::ScalingManager< ImageType >::images, and degate::ScalingManager< ImageType >::min_size.

                                        {

      assert(img != NULL);
      this->base_directory = base_directory;
      images[1] = img;
      this->min_size = min_size;

      assert(file_exists(base_directory));
    }

Here is the call graph for this function:

template<class ImageType>
degate::ScalingManager< ImageType >::~ScalingManager ( ) [inline]

Destroy a scaling manager.

This will destroy images anf their files and directories if the imag is not persistent.

Definition at line 102 of file ScalingManager.h.

{}

Member Function Documentation

template<class ImageType>
void degate::ScalingManager< ImageType >::create_scalings ( ) [inline]

Create the scaled images.

Created prescaled images that have the same peristence state as the master image. The files are written into the directory, where the master image is stored.

Exceptions:
InvalidPathExceptionThis exception is thrown, if the directory (ctor param) doesn't exists.
Todo:
If the image was already scaled, do not do it again. Maybe we need a force option.

Definition at line 130 of file ScalingManager.h.

References degate::ScalingManager< ImageType >::base_directory, degate::create_directory(), debug(), degate::file_exists(), degate::ScalingManager< ImageType >::images, degate::is_directory(), degate::join_pathes(), degate::ScalingManager< ImageType >::min_size, and TM.

                           {
      if(!(file_exists(base_directory) && is_directory(base_directory)))
        throw InvalidPathException("The directory for prescaled images must exist. but it is not there.");

      std::shared_ptr<ImageType> last_img = images[1];
      unsigned int w = last_img->get_width();
      unsigned int h = last_img->get_height();

      for(int i = 2; ((h > min_size) || (w > min_size)) &&
            (i < (1<<24));  // max 24 scaling levels
          i*=2) {

        w >>= 1;
        h >>= 1;

        // create a new image
        char dir_name[PATH_MAX];
        snprintf(dir_name, sizeof(dir_name), "scaling_%d.dimg", i);
        std::string dir_path = join_pathes(images[1]->get_directory(), std::string(dir_name));

        debug(TM, "create scaled image in %s for scaling factor %d?", dir_path.c_str(), i);
        if(!file_exists(dir_path)) {
          debug(TM, "yes");
          create_directory(dir_path);

          std::shared_ptr<ImageType> new_img(new ImageType(w, h, dir_path,
                                                                images[1]->is_persistent()));

          scale_down_by_2<ImageType, ImageType>(new_img, last_img);
          last_img = new_img;
        }
        else {
          debug(TM, "no");
          std::shared_ptr<ImageType> new_img(new ImageType(w, h, dir_path,
                                                                images[1]->is_persistent()));

          last_img = new_img;
        }
        images[i] = last_img;
      }
    }

Here is the call graph for this function:

template<class ImageType>
image_map_element degate::ScalingManager< ImageType >::get_image ( double  request_scaling) [inline]

Get the image with the nearest scaling value to the requested scaling.

Returns:
Returns a std::pair<double, shared_ptr> with the scaling factor and a shared pointer to the image.

Definition at line 177 of file ScalingManager.h.

References degate::ScalingManager< ImageType >::get_nearest_power_of_two(), and degate::ScalingManager< ImageType >::images.

                                                        {
      unsigned int factor;
      if(request_scaling > 1) {
        factor = std::min(get_nearest_power_of_two(lrint(request_scaling)),
                          (unsigned long)lrint(images.rbegin()->first));
        typename image_map::iterator found = images.find(factor);
        assert(found != images.end());
        //debug(TM, "requested scaling is %f. nearest scaling is %d. found image with scaling %f", request_scaling, factor, found->first);
        return *found;
      }

      //debug(TM, "return normal image");
      return image_map_element(1, images[1]);
    }

Here is the call graph for this function:

template<class ImageType>
unsigned long degate::ScalingManager< ImageType >::get_nearest_power_of_two ( unsigned int  value) [inline, private]

Definition at line 59 of file ScalingManager.h.

Referenced by degate::ScalingManager< ImageType >::get_image().

                                                               {
      unsigned int i = 1;

      if (value == 0) return 1;
      for (;;) {
        if (value == 1) return i;
        else if (value == 3) return i*4;
        value >>= 1;
        i *= 2;
      }
    }

Here is the caller graph for this function:

template<class ImageType>
const zoom_step_list degate::ScalingManager< ImageType >::get_zoom_steps ( ) const [inline]

Get a list of available zoom steps.

Returns:
Returns a std::list<double> with the zoom steps. A value larger than 1 indicates a downscaled version. The non scaled version with scale factor 1.0 is stored in the list, too.

Definition at line 110 of file ScalingManager.h.

References degate::ScalingManager< ImageType >::images.

                                                {
      zoom_step_list steps;

      for(typename image_map::const_iterator iter = images.begin();
          iter != images.end(); ++iter)
        steps.push_back((*iter).first);

      steps.sort();
      return steps;
    }

Member Data Documentation

template<class ImageType>
std::string degate::ScalingManager< ImageType >::base_directory [private]
template<class ImageType>
image_map degate::ScalingManager< ImageType >::images [private]
template<class ImageType>
unsigned int degate::ScalingManager< ImageType >::min_size [private]

The documentation for this class was generated from the following file: