/*------------------------------------------------------------------------------
 src/bin/djview/imageCache.h

 Copyright (c) 2004-2006 Darby Johnston.
 All rights reserved.
 Use is subject to license terms.
------------------------------------------------------------------------------*/

#ifndef DJVIEW_IMAGE_CACHE_H
#define DJVIEW_IMAGE_CACHE_H
#include "base.h"
#include <djImage.h>
#include <djImageIo.h>

namespace djview {

// Image reference.
//
class ImageRef : public Ref {
public:

  ImageRef(Image *, const Str & key);
  ~ImageRef();
  
  Image * get() { return _image; }
  
  void key(const Str &);
  const Str & key() const { return _key; }
  
private:
  void del();
  Image * _image;
  Str _key;
};

// Image cache.
//
class ImageCache {
public:

  ImageCache(int megabytes = 256);
  ~ImageCache();

  // Get.
  //
  ImageRef * create(Image *, const Str & key);
  ImageRef * get(const Str & key);

  // Key generation.
  //
  static const Str key(
    const Str & fileName,
    int proxy,
    int pixelFormat, int pixelType,
    int frame
  );

  // Cache.
  //
  void max(int megabytes);
  int max() const;
  void del(const Str & base);
  void del();
  void purge();

  // Debugging.
  //
  void dbg();

private:
  List<ImageRef *> _itemList;
  uint64_t _cacheMax, _cacheSize;
};

}

#endif
