Shinya Kitaoka 810553
#pragma once
Shinya Kitaoka 810553
Toshihiro Shimizu 890ddd
#ifndef IMAGEMANAGER_H
Toshihiro Shimizu 890ddd
#define IMAGEMANAGER_H
Toshihiro Shimizu 890ddd
Shinya Kitaoka 262a92
#include <memory></memory>
Shinya Kitaoka 262a92
Toshihiro Shimizu 890ddd
// TnzCore includes
Toshihiro Shimizu 890ddd
#include "timage.h"
Toshihiro Shimizu 890ddd
#include "timageinfo.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
// STD includes
Toshihiro Shimizu 890ddd
#include <string></string>
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
// Qt includes
Toshihiro Shimizu 890ddd
#include <qreadwritelock></qreadwritelock>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#undef DVAPI
Toshihiro Shimizu 890ddd
#undef DVVAR
Toshihiro Shimizu 890ddd
#ifdef TOONZLIB_EXPORTS
Toshihiro Shimizu 890ddd
#define DVAPI DV_EXPORT_API
Toshihiro Shimizu 890ddd
#define DVVAR DV_EXPORT_VAR
Toshihiro Shimizu 890ddd
#else
Toshihiro Shimizu 890ddd
#define DVAPI DV_IMPORT_API
Toshihiro Shimizu 890ddd
#define DVVAR DV_IMPORT_VAR
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//=====================================================
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//  Forward declarations
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
class TFrameId;
Toshihiro Shimizu 890ddd
class TImageInfo;
Toshihiro Shimizu 890ddd
class TImageReader;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
class QReadWriteLock;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
class ImageBuilder;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
class TXshSimpleLevel;
Toshihiro Shimizu 890ddd
//=====================================================
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//***************************************************************************************
Toshihiro Shimizu 890ddd
//    Image Manager declaration
Toshihiro Shimizu 890ddd
//***************************************************************************************
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
//! ImageManager is a singleton management class that associates string
Shinya Kitaoka 120a6e
//! identifiers to
Toshihiro Shimizu 890ddd
//! image-building objects, able to fetch images on request.
Toshihiro Shimizu 890ddd
/*!
Shinya Kitaoka 120a6e
    The ImageManager's purpose is similar to that of TImageCache - which is
Shinya Kitaoka 120a6e
   Toonz's other
Shinya Kitaoka 120a6e
    images storage class - with the difference that users do not supply it
Shinya Kitaoka 120a6e
   directly with
Shinya Kitaoka 120a6e
    the image to be stored, but rather with an object \a capable of fetching
Shinya Kitaoka 120a6e
   that image.
Toshihiro Shimizu 890ddd
    \n\n
Shinya Kitaoka 120a6e
    Said objects must be reimplemented from the base ImageBuilder class, and
Shinya Kitaoka 120a6e
   return an
Toshihiro Shimizu 890ddd
    image, and its specifications, upon request.
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
    The role of these image-building objects is that of providing the \a default
Shinya Kitaoka 120a6e
   image
Toshihiro Shimizu 890ddd
    for an identifier on-demand, without prior allocation of resources.
Toshihiro Shimizu 890ddd
    \n\n
Shinya Kitaoka 120a6e
    The ImageManager automatically stores built images in the global TImageCache
Shinya Kitaoka 120a6e
   instance
Shinya Kitaoka 120a6e
    (unless the user explicitly requires not to) in order to minimize the images
Shinya Kitaoka 120a6e
   building
Toshihiro Shimizu 890ddd
    process.
Toshihiro Shimizu 890ddd
    \n\n
Shinya Kitaoka 120a6e
    As long as an image bound in the ImageManager remains unmodified, users can
Shinya Kitaoka 120a6e
   rebuild
Shinya Kitaoka 120a6e
    default images according to different control modifiers (for example,
Shinya Kitaoka 120a6e
   requiring 64-bit
Toshihiro Shimizu 890ddd
    preference, or image subsampling).
Toshihiro Shimizu 890ddd
    \n\n
Shinya Kitaoka 120a6e
    Images stored in the ImageManager can be modified by the user (using the
Shinya Kitaoka 120a6e
   toBeModified
Toshihiro Shimizu 890ddd
    control flag) or even externally supplied using the setImage() method.
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
    When an image is modified, it is assumed to override any default-buildable
Shinya Kitaoka 120a6e
   images - thus
Shinya Kitaoka 120a6e
    it is stored in the cache for retrieval, and any control flag modifier is
Shinya Kitaoka 120a6e
   ignored as long as
Toshihiro Shimizu 890ddd
    the image is not invalidated().
Toshihiro Shimizu 890ddd
    \n\n
Shinya Kitaoka 120a6e
    Invalidation of an image can be requested to revert any changes to the image
Shinya Kitaoka 120a6e
   and clear
Toshihiro Shimizu 890ddd
    the cache.
Toshihiro Shimizu 890ddd
    \n\n
Shinya Kitaoka 120a6e
    Please, observe that ImageManager's image caching function is thread-safe -
Shinya Kitaoka 120a6e
   and
Shinya Kitaoka 120a6e
    ensures that image building happens only once in case multiple threads
Shinya Kitaoka 120a6e
   simultaneously
Shinya Kitaoka 120a6e
    require an image that was not cached before. Furthermore, there is no
Shinya Kitaoka 120a6e
   serialization
Shinya Kitaoka 120a6e
    overhead for already cached images or image specifications in read-only
Shinya Kitaoka 120a6e
   access.
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
    \sa The TImageCache and ImageBuilder classes.
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
class DVAPI ImageManager {
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  enum {
Shinya Kitaoka 120a6e
    none           = 0x0,
Shinya Kitaoka 120a6e
    dontPutInCache = 0x1,  // Prevents IM from storing built images in the cache
Shinya Kitaoka 120a6e
    forceRebuild   = 0x2,  // Forces rebuild of images (like an invalidate())
Shinya Kitaoka 120a6e
    toBeModified =
Shinya Kitaoka 120a6e
        0x4,          // Whether the retrieved image will be signed as modified
Shinya Kitaoka 120a6e
    toBeSaved = 0x8,  // User will save the image, reverts toBeModified
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
    is64bitEnabled = 0x10,  // Whether 64-bit rasters are allowed to return
shun-iwasawa 481b59
    isFloatEnabled = 0x20,  // Whether 128-bit float rasters are allowed to
shun-iwasawa 481b59
                            // return (for EXR format)
shun-iwasawa 481b59
    // isLinearEnabled = 0x40,  // Whether linear color space rasters are
shun-iwasawa 481b59
    // allowed to return (for EXR format)
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
    controlFlags = 0xF,  // Flags dealing with management control
Shinya Kitaoka 120a6e
    imageFlags =
Shinya Kitaoka 120a6e
        ~controlFlags  // ImageManager flags supportable by custom ImageBuilders
Shinya Kitaoka 120a6e
  };
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  static ImageManager *instance();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  /*!
Shinya Kitaoka 120a6e
Binds a string identifier to an ImageBuilder instance, which is necessary before
Shinya Kitaoka 120a6e
any image or
Shinya Kitaoka 120a6e
image data can be retrieved. If the specified id was already bound, it is first
Shinya Kitaoka 120a6e
unbound.
Shinya Kitaoka 120a6e
Binding an id to 0 is equivalent to unbinding it.
Shinya Kitaoka 120a6e
*/
Shinya Kitaoka 120a6e
  void bind(const std::string &id, ImageBuilder *builder);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  //! Unbinds the specified identifier, returning true if the operation
Shinya Kitaoka 120a6e
  //! succeeded.
Shinya Kitaoka 120a6e
  bool unbind(const std::string &id);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  //! Returns true if the specified id is bound to an image builder.
Shinya Kitaoka 120a6e
  bool isBound(const std::string &id) const;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  //! Rebinds to a different builder identifier, returning true if the operation
Shinya Kitaoka 120a6e
  //! succeeded.
Shinya Kitaoka 120a6e
  bool rebind(const std::string &srcId, const std::string &dstId);
Shinya Kitaoka 120a6e
manongjohn 88ffb4
  bool renumber(const std::string &srcId, const TFrameId &fid);
manongjohn 88ffb4
Shinya Kitaoka 120a6e
  //! Unbinds all known identifiers, resetting the image manager to its empty
Shinya Kitaoka 120a6e
  //! state.
Shinya Kitaoka 120a6e
  void clear();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  // load icon (and image) data of all frames into cache
Shinya Kitaoka 120a6e
  void loadAllTlvIconsAndPutInCache(TXshSimpleLevel *, std::vector<tframeid>,</tframeid>
Shinya Kitaoka 120a6e
                                    std::vector<std::string>, bool);</std::string>
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  /*!
Shinya Kitaoka 120a6e
Returns the image built by the object associated with the specified identifier,
Shinya Kitaoka 120a6e
using the
Shinya Kitaoka 120a6e
supplied control flags for additional options. Provided the builder type
Shinya Kitaoka 120a6e
associated to the
Shinya Kitaoka 120a6e
identifier is known, a further external reference can be supplied to the
Shinya Kitaoka 120a6e
request. In this case,
Shinya Kitaoka 120a6e
users should enforce manual invalidate() invocations whenever the supplied data
Shinya Kitaoka 120a6e
changes.
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
\warning Users take responsibility in modifying the returned image's data \b
Shinya Kitaoka 120a6e
only when imFlags
Shinya Kitaoka 120a6e
contains the \c toBeModified bit.
Shinya Kitaoka 120a6e
*/
Shinya Kitaoka 120a6e
  TImageP getImage(const std::string &id, int imFlags, void *extData);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  /*!
Shinya Kitaoka 120a6e
Returns the image info associated to the specified identifier.
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
\warning The return type is not explicitly \c const since some of TImageInfo's
Shinya Kitaoka 120a6e
data (namely,
Shinya Kitaoka 120a6e
the image's bounding box) may need to be modified by the user together with the
Shinya Kitaoka 120a6e
associated image.
Shinya Kitaoka 120a6e
Like in getImage(), users take responsibility in modifying the returned data \b
Shinya Kitaoka 120a6e
only when imFlags
Shinya Kitaoka 120a6e
contains the \c toBeModified bit.
Shinya Kitaoka 120a6e
*/
Shinya Kitaoka 120a6e
  TImageInfo *getInfo(const std::string &id, int imFlags, void *extData);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  //! Invalidates cached data associated to the specified id, forcing the next
Shinya Kitaoka 120a6e
  //! getInfo() or
Shinya Kitaoka 120a6e
  //! getImage() to rebuild it.
Shinya Kitaoka 120a6e
  bool invalidate(const std::string &id);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  /*!
Shinya Kitaoka 120a6e
Overrides the image builder, and manually associates an image to the specified
Shinya Kitaoka 120a6e
identifier,
Shinya Kitaoka 120a6e
putting the image in cache and updating the stored image info. If the image
Shinya Kitaoka 120a6e
reference is
Shinya Kitaoka 120a6e
empty, the result is equivalent to invalidate().
Shinya Kitaoka 120a6e
*/
Shinya Kitaoka 120a6e
  bool setImage(const std::string &id, const TImageP &img);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  //! Returns the ImageBuilder instance associate to specified id, or 0 if none
Shinya Kitaoka 120a6e
  //! was found.
Shinya Kitaoka 120a6e
  //! \warning The ImageManger retains ownership of the returned object, which
Shinya Kitaoka 120a6e
  //! must \b not be deleted.
Shinya Kitaoka 120a6e
  ImageBuilder *getBuilder(const std::string &id);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  //! Returns whether the identifier has an associated image in cache.
Shinya Kitaoka 120a6e
  bool isCached(const std::string &id);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  /*!
Shinya Kitaoka 120a6e
Returns whether the image associated to the specified id has been modified.
Shinya Kitaoka 120a6e
Observe that
Shinya Kitaoka 120a6e
modifying an image overrides the build-up of the associated default image - ie
Shinya Kitaoka 120a6e
any subsequent
Shinya Kitaoka 120a6e
call to getImage() will ignore control flags and return the stored modified
Shinya Kitaoka 120a6e
image.
Shinya Kitaoka 120a6e
*/
Shinya Kitaoka 120a6e
  bool isModified(const std::string &id);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
private:
Shinya Kitaoka 120a6e
  struct Imp;
Shinya Kitaoka 120a6e
  std::unique_ptr<imp> m_imp;</imp>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
private:
Shinya Kitaoka 120a6e
  ImageManager();
Shinya Kitaoka 120a6e
  ~ImageManager();
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  // Not copyable
Shinya Kitaoka 120a6e
  ImageManager(const ImageManager &);
Shinya Kitaoka 120a6e
  ImageManager &operator=(const ImageManager &);
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//***************************************************************************************
Toshihiro Shimizu 890ddd
//    ImageBuilder declaration
Toshihiro Shimizu 890ddd
//***************************************************************************************
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
//! ImageBuilder is the class model for self-creating image objects bound in the
Shinya Kitaoka 120a6e
//! ImageManager.
Shinya Kitaoka 120a6e
class DVAPI ImageBuilder : public TSmartObject {
Shinya Kitaoka 120a6e
  DECLARE_CLASS_CODE
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  ImageBuilder();
Shinya Kitaoka 120a6e
  virtual ~ImageBuilder();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  bool imageCached() { return m_cached; }
Shinya Kitaoka 120a6e
  bool imageModified() { return m_modified; }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  /*!
Shinya Kitaoka 120a6e
Returns whether any currently cached image is compatible with requested
Shinya Kitaoka 120a6e
specifications,
Shinya Kitaoka 120a6e
Returning true prevents ImageBuilder::build() invocations, fetching the cached
Shinya Kitaoka 120a6e
data instead.
Shinya Kitaoka 120a6e
The default implementation returns true if m_info has positive m_lx and m_ly.
Shinya Kitaoka 120a6e
*/
Shinya Kitaoka 120a6e
  virtual bool isImageCompatible(int imFlags, void *extData);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  /*!
Shinya Kitaoka 120a6e
Returns whether any currently cached image infos are compatible with requested
Shinya Kitaoka 120a6e
specifications.
Shinya Kitaoka 120a6e
Returning true prevents ImageBuilder::build() invocations, fetching the cached
Shinya Kitaoka 120a6e
data instead.
Shinya Kitaoka 120a6e
The default implementation returns m_info.m_valid.
Shinya Kitaoka 120a6e
*/
Shinya Kitaoka 120a6e
  virtual bool areInfosCompatible(int imFlags, void *extData);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  // Helper functions to fill in a TImageInfo structure.
Shinya Kitaoka 120a6e
  // Each of these sets info.m_valid at true if the associated infos could be
Shinya Kitaoka 120a6e
  // loaded.
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  static bool setImageInfo(TImageInfo &info, const TDimension &size);
Shinya Kitaoka 120a6e
  static bool setImageInfo(TImageInfo &info, TImage *img);
Shinya Kitaoka 120a6e
  static bool setImageInfo(TImageInfo &info, TImageReader *ir);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
protected:
Shinya Kitaoka 120a6e
  TImageInfo m_info;  //!< Currently cached image data - size, bpp, etc
Shinya Kitaoka 120a6e
  int m_imFlags;      //!< Currently cached image control flags
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
protected:
Shinya Kitaoka 120a6e
  //! Builds the image as specified by the supplied control flags and external
Shinya Kitaoka 120a6e
  //! data.
Shinya Kitaoka 120a6e
  virtual TImageP build(int imFlags, void *extData) = 0;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  //! Returns the image infos as specified by the supplied control flags and
Shinya Kitaoka 120a6e
  //! external data.
Shinya Kitaoka 120a6e
  virtual bool getInfo(TImageInfo &info, int imFlags, void *extData) = 0;
Shinya Kitaoka 120a6e
luz paz 6454c4
  // load icon (and image) data of all frames into cache. implemented in
Shinya Kitaoka 120a6e
  // ImageLoader
Shinya Kitaoka 120a6e
  virtual void buildAllIconsAndPutInCache(TXshSimpleLevel *,
Shinya Kitaoka 120a6e
                                          std::vector<tframeid>,</tframeid>
Shinya Kitaoka 120a6e
                                          std::vector<std::string>, bool){};</std::string>
Shinya Kitaoka 120a6e
manongjohn 88ffb4
  virtual void setFid(const TFrameId &fid){};
manongjohn 88ffb4
Shinya Kitaoka 120a6e
  //! Clears the builder's cached data.
Shinya Kitaoka 120a6e
  virtual void invalidate() {
Shinya Kitaoka 120a6e
    m_info    = TImageInfo();
Shinya Kitaoka 120a6e
    m_imFlags = ImageManager::none;
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
private:
Shinya Kitaoka 120a6e
  friend class ImageManager;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  QReadWriteLock
Shinya Kitaoka 120a6e
      m_imageBuildingLock;  //!< Image building is mutexed by the ImageManager
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  bool m_cached;    //!< Whether the associated image was stored in cache
Shinya Kitaoka 120a6e
  bool m_modified;  //!< Whether the associated image was modified
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
private:
Shinya Kitaoka 120a6e
  // Not copyable
Shinya Kitaoka 120a6e
  ImageBuilder(const ImageBuilder &);
Shinya Kitaoka 120a6e
  const ImageBuilder &operator=(const ImageBuilder &);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  void setImageCachedAndModified() {
Shinya Kitaoka 120a6e
    m_cached   = true;
Shinya Kitaoka 120a6e
    m_modified = true;
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 9f5a1b
#ifdef _WIN32
Toshihiro Shimizu 890ddd
template class DV_EXPORT_API TSmartPointerT<imagebuilder>;</imagebuilder>
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
typedef TSmartPointerT<imagebuilder> ImageBuilderP;</imagebuilder>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#endif