The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

128 lines
5.0KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. #ifndef __JUCE_IMAGECACHE_JUCEHEADER__
  18. #define __JUCE_IMAGECACHE_JUCEHEADER__
  19. #include "juce_Image.h"
  20. //==============================================================================
  21. /**
  22. A global cache of images that have been loaded from files or memory.
  23. If you're loading an image and may need to use the image in more than one
  24. place, this is used to allow the same image to be shared rather than loading
  25. multiple copies into memory.
  26. Another advantage is that after images are released, they will be kept in
  27. memory for a few seconds before it is actually deleted, so if you're repeatedly
  28. loading/deleting the same image, it'll reduce the chances of having to reload it
  29. each time.
  30. @see Image, ImageFileFormat
  31. */
  32. class JUCE_API ImageCache
  33. {
  34. public:
  35. //==============================================================================
  36. /** Loads an image from a file, (or just returns the image if it's already cached).
  37. If the cache already contains an image that was loaded from this file,
  38. that image will be returned. Otherwise, this method will try to load the
  39. file, add it to the cache, and return it.
  40. Remember that the image returned is shared, so drawing into it might
  41. affect other things that are using it! If you want to draw on it, first
  42. call Image::duplicateIfShared()
  43. @param file the file to try to load
  44. @returns the image, or null if it there was an error loading it
  45. @see getFromMemory, getFromCache, ImageFileFormat::loadFrom
  46. */
  47. static Image getFromFile (const File& file);
  48. /** Loads an image from an in-memory image file, (or just returns the image if it's already cached).
  49. If the cache already contains an image that was loaded from this block of memory,
  50. that image will be returned. Otherwise, this method will try to load the
  51. file, add it to the cache, and return it.
  52. Remember that the image returned is shared, so drawing into it might
  53. affect other things that are using it! If you want to draw on it, first
  54. call Image::duplicateIfShared()
  55. @param imageData the block of memory containing the image data
  56. @param dataSize the data size in bytes
  57. @returns the image, or an invalid image if it there was an error loading it
  58. @see getFromMemory, getFromCache, ImageFileFormat::loadFrom
  59. */
  60. static Image getFromMemory (const void* imageData, int dataSize);
  61. //==============================================================================
  62. /** Checks the cache for an image with a particular hashcode.
  63. If there's an image in the cache with this hashcode, it will be returned,
  64. otherwise it will return an invalid image.
  65. @param hashCode the hash code that was associated with the image by addImageToCache()
  66. @see addImageToCache
  67. */
  68. static Image getFromHashCode (int64 hashCode);
  69. /** Adds an image to the cache with a user-defined hash-code.
  70. The image passed-in will be referenced (not copied) by the cache, so it's probably
  71. a good idea not to draw into it after adding it, otherwise this will affect all
  72. instances of it that may be in use.
  73. @param image the image to add
  74. @param hashCode the hash-code to associate with it
  75. @see getFromHashCode
  76. */
  77. static void addImageToCache (const Image& image, int64 hashCode);
  78. /** Changes the amount of time before an unused image will be removed from the cache.
  79. By default this is about 5 seconds.
  80. */
  81. static void setCacheTimeout (int millisecs);
  82. /** Releases any images in the cache that aren't being referenced by active
  83. Image objects.
  84. */
  85. static void releaseUnusedImages();
  86. private:
  87. //==============================================================================
  88. class Pimpl;
  89. friend class Pimpl;
  90. ImageCache();
  91. ~ImageCache();
  92. JUCE_DECLARE_NON_COPYABLE (ImageCache)
  93. };
  94. #endif // __JUCE_IMAGECACHE_JUCEHEADER__