HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
imagecache.h
Go to the documentation of this file.
1 // Copyright Contributors to the OpenImageIO project.
2 // SPDX-License-Identifier: Apache-2.0
3 // https://github.com/AcademySoftwareFoundation/OpenImageIO
4 
5 // clang-format off
6 
7 
8 
9 #pragma once
10 
11 #include <OpenImageIO/imageio.h>
12 #include <OpenImageIO/ustring.h>
13 
14 
15 // Define symbols that let client applications determine if newly added
16 // features are supported.
17 
18 // Is the close() method present?
19 #define OIIO_IMAGECACHE_SUPPORTS_CLOSE 1
20 
21 // Is the getattributetype() method present? (Added in 2.5)
22 #define OIIO_IMAGECACHE_SUPPORTS_GETATTRIBUTETYPE 1
23 
24 // Does invalidate() support the optional `force` flag?
25 #define OIIO_IMAGECACHE_INVALIDATE_FORCE 1
26 
27 
28 
30 
31 // Forward declaration
32 class TextureOpt;
33 
34 namespace pvt {
35 // Forward declaration
36 class ImageCacheImpl;
37 class ImageCacheFile;
38 class ImageCacheTile;
39 class ImageCachePerThreadInfo;
40 }; // namespace pvt
41 
42 
43 
44 /// Define an API to an abstract class that manages image files,
45 /// caches of open file handles as well as tiles of pixels so that truly
46 /// huge amounts of image data may be accessed by an application with low
47 /// memory footprint.
49 public:
50  /// @{
51  /// @name Creating and destroying an image cache
52  ///
53  /// ImageCache is an abstract API described as a pure virtual class.
54  /// The actual internal implementation is not exposed through the
55  /// external API of OpenImageIO. Because of this, you cannot construct
56  /// or destroy the concrete implementation, so two static methods of
57  /// ImageCache are provided:
58 
59  /// Create a ImageCache and return a raw pointer to it. This should
60  /// only be freed by passing it to `ImageCache::destroy()`!
61  ///
62  /// @param shared
63  /// If `true`, the pointer returned will be a shared ImageCache (so
64  /// that multiple parts of an application that request an ImageCache
65  /// will all end up with the same one). If `shared` is `false`, a
66  /// completely unique ImageCache will be created and returned.
67  ///
68  /// @returns
69  /// A raw pointer to an ImageCache, which can only be freed with
70  /// `ImageCache::destroy()`.
71  ///
72  /// @see ImageCache::destroy
73  static ImageCache* create(bool shared = true);
74 
75  /// Destroy an allocated ImageCache, including freeing all system
76  /// resources that it holds.
77  ///
78  /// It is safe to destroy even a shared ImageCache, as the implementation
79  /// of `destroy()` will recognize a shared one and only truly release
80  /// its resources if it has been requested to be destroyed as many times as
81  /// shared ImageCache's were created.
82  ///
83  /// @param cache
84  /// Raw pointer to the ImageCache to destroy.
85  ///
86  /// @param teardown
87  /// For a shared ImageCache, if the `teardown` parameter is
88  /// `true`, it will try to truly destroy the shared cache if
89  /// nobody else is still holding a reference (otherwise, it will
90  /// leave it intact). This parameter has no effect if `cache` was
91  /// not the single globally shared ImageCache.
92  static void destroy(ImageCache* cache, bool teardown = false);
93 
94  /// @}
95 
96 
97  /// @{
98  /// @name Setting options and limits for the image cache
99  ///
100  /// These are the list of attributes that can bet set or queried by
101  /// attribute/getattribute:
102  ///
103  /// - `int max_open_files` :
104  /// The approximate maximum number of file handles that the
105  /// image cache will hold open simultaneously. This is not an
106  /// iron-clad guarantee; the number of handles may momentarily
107  /// exceed this by a small percentage. (Default = 100)
108  /// - `float max_memory_MB` :
109  /// The approximate maximum amount of memory (measured in MB)
110  /// used for the internal "tile cache." (Default: 1024.0 MB)
111  /// - `string searchpath` :
112  /// The search path for images: a colon-separated list of
113  /// directories that will be searched in order for any image
114  /// filename that is not specified as an absolute path.
115  /// (Default: "")
116  /// - `string plugin_searchpath` :
117  /// The search path for plugins: a colon-separated list of
118  /// directories that will be searched in order for any OIIO
119  /// plugins, if not found in OIIO's `lib` directory.
120  /// (Default: "")
121  /// - `int autotile` ,
122  /// `int autoscanline` :
123  /// These attributes control how the image cache deals with
124  /// images that are not "tiled" (i.e., are stored as
125  /// scanlines).
126  ///
127  /// If `autotile` is set to 0 (the default), an untiled image
128  /// will be treated as if it were a single tile of the
129  /// resolution of the whole image. This is simple and fast,
130  /// but can lead to poor cache behavior if you are
131  /// simultaneously accessing many large untiled images.
132  ///
133  /// If `autotile` is nonzero (e.g., 64 is a good recommended
134  /// value), any untiled images will be read and cached as if
135  /// they were constructed in tiles of size:
136  ///
137  /// - `autotile * autotile`
138  /// if `autoscanline` is 0
139  /// - `width * autotile`
140  /// if `autoscanline` is nonzero.
141  ///
142  /// In both cases, this should lead more efficient caching.
143  /// The `autoscanline` determines whether the "virtual tiles"
144  /// in the cache are square (if `autoscanline` is 0, the
145  /// default) or if they will be as wide as the image (but only
146  /// `autotile` scanlines high). You should try in your
147  /// application to see which leads to higher performance.
148  /// - `int autoscanline` :
149  /// autotile using full width tiles
150  /// - `int automip` :
151  /// If 0 (the default), an untiled single-subimage file will
152  /// only be able to utilize that single subimage.
153  /// If nonzero, any untiled, single-subimage (un-MIP-mapped)
154  /// images will have lower-resolution MIP-map levels generated
155  /// on-demand if pixels are requested from the lower-res
156  /// subimages (that don't really exist). Essentially this
157  /// makes the ImageCache pretend that the file is MIP-mapped
158  /// even if it isn't.
159  /// - `int accept_untiled` :
160  /// When nonzero, ImageCache accepts untiled images as usual.
161  /// When zero, ImageCache will reject untiled images with an
162  /// error condition, as if the file could not be properly
163  /// read. This is sometimes helpful for applications that want
164  /// to enforce use of tiled images only. (default=1)
165  /// - `int accept_unmipped` :
166  /// When nonzero, ImageCache accepts un-MIPmapped images as
167  /// usual. When set to zero, ImageCache will reject
168  /// un-MIPmapped images with an error condition, as if the
169  /// file could not be properly read. This is sometimes helpful
170  /// for applications that want to enforce use of MIP-mapped
171  /// images only. (Default: 1)
172  /// - `int statistics:level` :
173  /// verbosity of statistics auto-printed.
174  /// - `int forcefloat` :
175  /// If set to nonzero, all image tiles will be converted to
176  /// `float` type when stored in the image cache. This can be
177  /// helpful especially for users of ImageBuf who want to
178  /// simplify their image manipulations to only need to
179  /// consider `float` data. The default is zero, meaning that
180  /// image pixels are not forced to be `float` when in cache.
181  /// - `int failure_retries` :
182  /// When an image file is opened or a tile/scanline is read but
183  /// a file error occurs, if this attribute is nonzero, it will
184  /// try the operation again up to this many times before giving
185  /// up and reporting a failure. Setting this to a small nonzero
186  /// number (like 3) may help make an application more robust to
187  /// occasional spurious networking or other glitches that would
188  /// otherwise cause the entire long-running application to fail
189  /// upon a single transient error. (Default: 0)
190  /// - `int deduplicate` :
191  /// When nonzero, the ImageCache will notice duplicate images
192  /// under different names if their headers contain a SHA-1
193  /// fingerprint (as is done with `maketx`-produced textures)
194  /// and handle them more efficiently by avoiding redundant
195  /// reads. The default is 1 (de-duplication turned on). The
196  /// only reason to set it to 0 is if you specifically want to
197  /// disable the de-duplication optimization.
198  /// - `int max_open_files_strict` :
199  /// If nonzero, work harder to make sure that we have
200  /// smaller possible overages to the max open files limit.
201  /// (Default: 0)
202  /// - `string substitute_image` :
203  /// When set to anything other than the empty string, the
204  /// ImageCache will use the named image in place of *all*
205  /// other images. This allows you to run an app using OIIO
206  /// and (if you can manage to get this option set)
207  /// automagically substitute a grid, zone plate, or other
208  /// special debugging image for all image/texture use.
209  /// - `int unassociatedalpha` :
210  /// When nonzero, will request that image format readers try
211  /// to leave input images with unassociated alpha as they are,
212  /// rather than automatically converting to associated alpha
213  /// upon reading the pixels. The default is 0, meaning that
214  /// the automatic conversion will take place.
215  /// - `int max_errors_per_file` :
216  /// The maximum number of errors that will be printed for each
217  /// file. The default is 100. If your output is cluttered with
218  /// error messages and after the first few for each file you
219  /// aren't getting any helpful additional information, this
220  /// can cut down on the clutter and the runtime. (default:
221  /// 100)
222  /// - `int trust_file_extensions` :
223  /// When nonzero, assume that the file extensions of any
224  /// texture requests correctly indicates the file format (when
225  /// enabled, this reduces the number of file opens, at the
226  /// expense of not being able to open files if their format do
227  /// not actually match their filename extension). Default: 0
228  /// - `string colorspace` :
229  /// The working colorspace of the texture system. Default: none.
230  /// - `string colorconfig` :
231  /// Name of the OCIO config to use. Default: "" (meaning to use
232  /// the default color config).
233  ///
234  /// - `string options`
235  /// This catch-all is simply a comma-separated list of
236  /// `name=value` settings of named options, which will be
237  /// parsed and individually set. Example:
238  ///
239  /// ic->attribute ("options", "max_memory_MB=512.0,autotile=1");
240  ///
241  /// Note that if an option takes a string value that must
242  /// itself contain a comma, it is permissible to enclose the
243  /// value in either single (` ' ' `) or double (` " " `) quotes.
244  ///
245  /// **Read-only attributes**
246  ///
247  /// Additionally, there are some read-only attributes that can be
248  /// queried with `getattribute()` even though they cannot be set via
249  /// `attribute()`:
250  ///
251  /// - `int total_files` :
252  /// The total number of unique file names referenced by calls
253  /// to the ImageCache.
254  ///
255  /// - `string[] all_filenames` :
256  /// An array that will be filled with the list of the names of
257  /// all files referenced by calls to the ImageCache. (The
258  /// array is of `ustring` or `char*`.)
259  ///
260  /// - `int64 stat:cache_memory_used` :
261  /// Total bytes used by tile cache.
262  ///
263  /// - `int stat:tiles_created` ,
264  /// `int stat:tiles_current` ,
265  /// `int stat:tiles_peak` :
266  /// Total times created, still allocated (at the time of the
267  /// query), and the peak number of tiles in memory at any
268  /// time.
269  ///
270  /// - `int stat:open_files_created` ,
271  /// `int stat:open_files_current` ,
272  /// `int stat:open_files_peak` :
273  /// Total number of times a file was opened, number still
274  /// opened (at the time of the query), and the peak number of
275  /// files opened at any time.
276  ///
277  /// - `int stat:find_tile_calls` :
278  /// Number of times a filename was looked up in the file cache.
279  ///
280  /// - `int64 stat:image_size` :
281  /// Total size (uncompressed bytes of pixel data) of all
282  /// images referenced by the ImageCache. (Note: Prior to 1.7,
283  /// this was called `stat:files_totalsize`.)
284  ///
285  /// - `int64 stat:file_size` :
286  /// Total size of all files (as on disk, possibly compressed)
287  /// of all images referenced by the ImageCache.
288  ///
289  /// - `int64 stat:bytes_read` :
290  /// Total size (uncompressed bytes of pixel data) read.
291  ///
292  /// - `int stat:unique_files` :
293  /// Number of unique files opened.
294  ///
295  /// - `float stat:fileio_time` :
296  /// Total I/O-related time (seconds).
297  ///
298  /// - `float stat:fileopen_time` :
299  /// I/O time related to opening and reading headers (but not
300  /// pixel I/O).
301  ///
302  /// - `float stat:file_locking_time` :
303  /// Total time (across all threads) that threads blocked
304  /// waiting for access to the file data structures.
305  ///
306  /// - `float stat:tile_locking_time` :
307  /// Total time (across all threads) that threads blocked
308  /// waiting for access to the tile cache data structures.
309  ///
310  /// - `float stat:find_file_time` :
311  /// Total time (across all threads) that threads spent looking
312  /// up files by name.
313  ///
314  /// - `float stat:find_tile_time` :
315  /// Total time (across all threads) that threads spent looking
316  /// up individual tiles.
317  ///
318  /// The following member functions of ImageCache allow you to set (and
319  /// in some cases retrieve) options that control the overall behavior of
320  /// the image cache:
321 
322  /// Set a named attribute (i.e., a property or option) of the
323  /// ImageCache.
324  ///
325  /// Example:
326  ///
327  /// ImageCache *ic;
328  /// ...
329  /// int maxfiles = 50;
330  /// ic->attribute ("max_open_files", TypeDesc::INT, &maxfiles);
331  ///
332  /// const char *path = "/my/path";
333  /// ic->attribute ("searchpath", TypeDesc::STRING, &path);
334  ///
335  /// // There are specialized versions for setting a single int,
336  /// // float, or string without needing types or pointers:
337  /// ic->attribute ("max_open_files", 50);
338  /// ic->attribute ("max_memory_MB", 4000.0f);
339  /// ic->attribute ("searchpath", "/my/path");
340  ///
341  /// Note: When passing a string, you need to pass a pointer to the
342  /// `char*`, not a pointer to the first character. (Rationale: for an
343  /// `int` attribute, you pass the address of the `int`. So for a
344  /// string, which is a `char*`, you need to pass the address of the
345  /// string, i.e., a `char**`).
346  ///
347  /// @param name Name of the attribute to set.
348  /// @param type TypeDesc describing the type of the attribute.
349  /// @param val Pointer to the value data.
350  /// @returns `true` if the name and type were recognized and the
351  /// attribute was set, or `false` upon failure
352  /// (including it being an unrecognized attribute or not
353  /// of the correct type).
354  ///
355  virtual bool attribute (string_view name, TypeDesc type,
356  const void *val) = 0;
357 
358  /// Specialized `attribute()` for setting a single `int` value.
359  virtual bool attribute (string_view name, int val) = 0;
360  /// Specialized `attribute()` for setting a single `float` value.
361  virtual bool attribute (string_view name, float val) = 0;
362  virtual bool attribute (string_view name, double val) = 0;
363  /// Specialized `attribute()` for setting a single string value.
364  virtual bool attribute (string_view name, string_view val) = 0;
365 
366  /// Get the named attribute, store it in `*val`. All of the attributes
367  /// that may be set with the `attribute() call` may also be queried with
368  /// `getattribute()`.
369  ///
370  /// Examples:
371  ///
372  /// ImageCache *ic;
373  /// ...
374  /// int maxfiles;
375  /// ic->getattribute ("max_open_files", TypeDesc::INT, &maxfiles);
376  ///
377  /// const char *path;
378  /// ic->getattribute ("searchpath", TypeDesc::STRING, &path);
379  ///
380  /// // There are specialized versions for retrieving a single int,
381  /// // float, or string without needing types or pointers:
382  /// int maxfiles;
383  /// ic->getattribute ("max_open_files", maxfiles);
384  /// const char *path;
385  /// ic->getattribute ("searchpath", &path);
386  ///
387  /// Note: When retrieving a string, you need to pass a pointer to the
388  /// `char*`, not a pointer to the first character. Also, the `char*`
389  /// will end up pointing to characters owned by the ImageCache; the
390  /// caller does not need to ever free the memory that contains the
391  /// characters.
392  ///
393  /// @param name Name of the attribute to retrieve.
394  /// @param type TypeDesc describing the type of the attribute.
395  /// @param val Pointer where the attribute value should be stored.
396  /// @returns `true` if the name and type were recognized and the
397  /// attribute was retrieved, or `false` upon failure
398  /// (including it being an unrecognized attribute or not
399  /// of the correct type).
400  virtual bool getattribute (string_view name, TypeDesc type,
401  void *val) const = 0;
402 
403  /// Specialized `attribute()` for retrieving a single `int` value.
404  virtual bool getattribute (string_view name, int &val) const = 0;
405  /// Specialized `attribute()` for retrieving a single `float` value.
406  virtual bool getattribute (string_view name, float &val) const = 0;
407  virtual bool getattribute (string_view name, double &val) const = 0;
408  /// Specialized `attribute()` for retrieving a single `string` value
409  /// as a `char*`.
410  virtual bool getattribute (string_view name, char **val) const = 0;
411  /// Specialized `attribute()` for retrieving a single `string` value
412  /// as a `std::string`.
413  virtual bool getattribute (string_view name, std::string &val) const = 0;
414 
415  /// If the named attribute is known, return its data type. If no such
416  /// attribute exists, return `TypeUnknown`.
417  ///
418  /// This was added in version 2.5.
419  virtual TypeDesc getattributetype(string_view name) const = 0;
420 
421  /// @}
422 
423 
424  /// @{
425  /// @name Opaque data for performance lookups
426  ///
427  /// The ImageCache implementation needs to maintain certain per-thread
428  /// state, and some methods take an opaque `Perthread` pointer to this
429  /// record. There are three options for how to deal with it:
430  ///
431  /// 1. Don't worry about it at all: don't use the methods that want
432  /// `Perthread` pointers, or always pass `nullptr` for any
433  /// `Perthread*1 arguments, and ImageCache will do
434  /// thread-specific-pointer retrieval as necessary (though at some
435  /// small cost).
436  ///
437  /// 2. If your app already stores per-thread information of its own, you
438  /// may call `get_perthread_info(nullptr)` to retrieve it for that
439  /// thread, and then pass it into the functions that allow it (thus
440  /// sparing them the need and expense of retrieving the
441  /// thread-specific pointer). However, it is crucial that this
442  /// pointer not be shared between multiple threads. In this case, the
443  /// ImageCache manages the storage, which will automatically be
444  /// released when the thread terminates.
445  ///
446  /// 3. If your app also wants to manage the storage of the `Perthread`,
447  /// it can explicitly create one with `create_perthread_info()`, pass
448  /// it around, and eventually be responsible for destroying it with
449  /// `destroy_perthread_info()`. When managing the storage, the app
450  /// may reuse the `Perthread` for another thread after the first is
451  /// terminated, but still may not use the same `Perthread` for two
452  /// threads running concurrently.
453 
454  /// Define an opaque data type that allows us to have a pointer to
455  /// certain per-thread information that the ImageCache maintains. Any
456  /// given one of these should NEVER be shared between running threads.
457  typedef pvt::ImageCachePerThreadInfo Perthread;
458 
459  /// Retrieve a Perthread, unique to the calling thread. This is a
460  /// thread-specific pointer that will always return the Perthread for a
461  /// thread, which will also be automatically destroyed when the thread
462  /// terminates.
463  ///
464  /// Applications that want to manage their own Perthread pointers (with
465  /// `create_thread_info` and `destroy_thread_info`) should still call
466  /// this, but passing in their managed pointer. If the passed-in
467  /// thread_info is not NULL, it won't create a new one or retrieve a
468  /// TSP, but it will do other necessary housekeeping on the Perthread
469  /// information.
470  virtual Perthread* get_perthread_info(Perthread* thread_info = NULL) = 0;
471 
472  /// Create a new Perthread. It is the caller's responsibility to
473  /// eventually destroy it using `destroy_thread_info()`.
474  virtual Perthread* create_thread_info() = 0;
475 
476  /// Destroy a Perthread that was allocated by `create_thread_info()`.
477  virtual void destroy_thread_info(Perthread* thread_info) = 0;
478 
479  /// Define an opaque data type that allows us to have a handle to an
480  /// image (already having its name resolved) but without exposing any
481  /// internals.
482  typedef pvt::ImageCacheFile ImageHandle;
483 
484  /// Retrieve an opaque handle for fast texture lookups, or nullptr upon
485  /// failure. The filename is presumed to be UTF-8 encoded. The `options`,
486  /// if not null, may be used to create a separate handle for certain
487  /// texture option choices. (Currently unused, but reserved for the future
488  /// or for alternate IC implementations.) The opaque pointer `thread_info`
489  /// is thread-specific information returned by `get_perthread_info()`.
490  virtual ImageHandle* get_image_handle(ustring filename,
491  Perthread* thread_info = nullptr,
492  const TextureOpt* options = nullptr) = 0;
493 
494  /// Get an ImageHandle using a UTF-16 encoded wstring filename.
495  ImageHandle* get_image_handle(const std::wstring& filename,
496  Perthread* thread_info = nullptr,
497  const TextureOpt* options = nullptr) {
498  return get_image_handle(ustring(Strutil::utf16_to_utf8(filename)),
499  thread_info, options);
500  }
501 
502  /// Return true if the image handle (previously returned by
503  /// `get_image_handle()`) is a valid image that can be subsequently read.
504  virtual bool good(ImageHandle* file) = 0;
505 
506  /// Given a handle, return the filename for that image.
507  ///
508  /// This method was added in OpenImageIO 2.3.
509  virtual ustring filename_from_handle(ImageHandle* handle) = 0;
510 
511  /// @}
512 
513 
514  /// @{
515  /// @name Getting information about images
516  ///
517 
518  /// Given possibly-relative `filename` (UTF-8 encoded), resolve it and use
519  /// the true path to the file, with searchpath logic applied.
520  virtual std::string resolve_filename(const std::string& filename) const = 0;
521 
522  /// Get information or metadata about the named image and store it in
523  /// `*data`.
524  ///
525  /// Data names may include any of the following:
526  ///
527  /// - `"exists"` : Stores the value 1 (as an `int`) if the file exists and
528  /// is an image format that OpenImageIO can read, or 0 if the file
529  /// does not exist, or could not be properly read as an image. Note
530  /// that unlike all other queries, this query will "succeed" (return
531  /// `true`) even if the file does not exist.
532  ///
533  /// - `"udim"` : Stores the value 1 (as an `int`) if the file is a
534  /// "virtual UDIM" or texture atlas file (as described in
535  /// :ref:`sec-texturesys-udim`) or 0 otherwise.
536  ///
537  /// - `"subimages"` : The number of subimages in the file, as an `int`.
538  ///
539  /// - `"resolution"` : The resolution of the image file, which is an
540  /// array of 2 integers (described as `TypeDesc(INT,2)`).
541  ///
542  /// - `"miplevels"` : The number of MIPmap levels for the specified
543  /// subimage (an integer).
544  ///
545  /// - `"texturetype"` : A string describing the type of texture of the
546  /// given file, which describes how the texture may be used (also
547  /// which texture API call is probably the right one for it). This
548  /// currently may return one of: `"unknown"`, `"Plain Texture"`,
549  /// `"Volume Texture"`, `"Shadow"`, or `"Environment"`.
550  ///
551  /// - `"textureformat"` : A string describing the format of the given
552  /// file, which describes the kind of texture stored in the file. This
553  /// currently may return one of: `"unknown"`, `"Plain Texture"`,
554  /// `"Volume Texture"`, `"Shadow"`, `"CubeFace Shadow"`,
555  /// `"Volume Shadow"`, `"LatLong Environment"`, or
556  /// `"CubeFace Environment"`. Note that there are several kinds of
557  /// shadows and environment maps, all accessible through the same API
558  /// calls.
559  ///
560  /// - `"channels"` : The number of color channels in the file (an
561  /// `int`).
562  ///
563  /// - `"format"` : The native data format of the pixels in the file (an
564  /// integer, giving the `TypeDesc::BASETYPE` of the data). Note that
565  /// this is not necessarily the same as the data format stored in the
566  /// image cache.
567  ///
568  /// - `"cachedformat"` : The native data format of the pixels as stored
569  /// in the image cache (an integer, giving the `TypeDesc::BASETYPE` of
570  /// the data). Note that this is not necessarily the same as the
571  /// native data format of the file.
572  ///
573  /// - `"datawindow"` : Returns the pixel data window of the image, which
574  /// is either an array of 4 integers (returning xmin, ymin, xmax,
575  /// ymax) or an array of 6 integers (returning xmin, ymin, zmin, xmax,
576  /// ymax, zmax). The z values may be useful for 3D/volumetric images;
577  /// for 2D images they will be 0).
578  ///
579  /// - `"displaywindow"` : Returns the display (a.k.a. "full") window of
580  /// the image, which is either an array of 4 integers (returning xmin,
581  /// ymin, xmax, ymax) or an array of 6 integers (returning xmin, ymin,
582  /// zmin, xmax, ymax, zmax). The z values may be useful for
583  /// 3D/volumetric images; for 2D images they will be 0).
584  ///
585  /// - `"worldtocamera"` : The viewing matrix, which is a 4x4 matrix (an
586  /// `Imath::M44f`, described as `TypeDesc(FLOAT,MATRIX)`), giving the
587  /// world-to-camera 3D transformation matrix that was used when the
588  /// image was created. Generally, only rendered images will have this.
589  ///
590  /// - `"worldtoscreen"` : The projection matrix, which is a 4x4 matrix
591  /// (an `Imath::M44f`, described as `TypeDesc(FLOAT,MATRIX)`), giving
592  /// the matrix that projected points from world space into a 2D screen
593  /// coordinate system where $x$ and $y$ range from -1 to +1.
594  /// Generally, only rendered images will have this.
595  ///
596  /// - `"worldtoNDC"` : The projection matrix, which is a 4x4 matrix
597  /// (an `Imath::M44f`, described as `TypeDesc(FLOAT,MATRIX)`), giving
598  /// the matrix that projected points from world space into a 2D NDC
599  /// coordinate system where $x$ and $y$ range from 0 to +1. Generally,
600  /// only rendered images will have this.
601  ///
602  /// - `"averagecolor"` : If available in the metadata (generally only
603  /// for files that have been processed by `maketx`), this will return
604  /// the average color of the texture (into an array of `float`).
605  ///
606  /// - `"averagealpha"` : If available in the metadata (generally only
607  /// for files that have been processed by `maketx`), this will return
608  /// the average alpha value of the texture (into a `float`).
609  ///
610  /// - `"constantcolor"` : If the metadata (generally only for files that
611  /// have been processed by `maketx`) indicates that the texture has
612  /// the same values for all pixels in the texture, this will retrieve
613  /// the constant color of the texture (into an array of floats). A
614  /// non-constant image (or one that does not have the special metadata
615  /// tag identifying it as a constant texture) will fail this query
616  /// (return `false`).
617  ///
618  /// - `"constantalpha"` : If the metadata indicates that the texture has
619  /// the same values for all pixels in the texture, this will retrieve
620  /// the constant alpha value of the texture (into a float). A
621  /// non-constant image (or one that does not have the special metadata
622  /// tag identifying it as a constant texture) will fail this query
623  /// (return `false`).
624  ///
625  /// - `"stat:tilesread"` : Number of tiles read from this file
626  /// (`int64`).
627  ///
628  /// - `"stat:bytesread"` : Number of bytes of uncompressed pixel data
629  /// read from this file (`int64`).
630  ///
631  /// - `"stat:redundant_tiles"` : Number of times a tile was read, where
632  /// the same tile had been rad before. (`int64`).
633  ///
634  /// - `"stat:redundant_bytesread"` : Number of bytes (of uncompressed
635  /// pixel data) in tiles that were read redundantly. (`int64`).
636  ///
637  /// - `"stat:redundant_bytesread"` : Number of tiles read from this file (`int`).
638  ///
639  /// - `"stat:image_size"` : Size of the uncompressed image pixel data
640  /// of this image, in bytes (`int64`).
641  ///
642  /// - `"stat:file_size"` : Size of the disk file (possibly compressed)
643  /// for this image, in bytes (`int64`).
644  ///
645  /// - `"stat:timesopened"` : Number of times this file was opened
646  /// (`int`).
647  ///
648  /// - `"stat:iotime"` : Time (in seconds) spent on all I/O for this file
649  /// (`float`).
650  ///
651  /// - `"stat:mipsused"` : Stores 1 if any MIP levels beyond the highest
652  /// resolution were accessed, otherwise 0. (`int`)
653  ///
654  /// - `"stat:is_duplicate"` : Stores 1 if this file was a duplicate of
655  /// another image, otherwise 0. (`int`)
656  ///
657  /// - *Anything else* : For all other data names, the the metadata of
658  /// the image file will be searched for an item that matches both the
659  /// name and data type.
660  ///
661  ///
662  ///
663  /// @param filename
664  /// The name of the image, as a UTF-8 encoded ustring.
665  /// @param subimage/miplevel
666  /// The subimage and MIP level to query.
667  /// @param dataname
668  /// The name of the metadata to retrieve.
669  /// @param datatype
670  /// TypeDesc describing the data type.
671  /// @param data
672  /// Pointer to the caller-owned memory where the values
673  /// should be stored. It is the caller's responsibility to
674  /// ensure that `data` points to a large enough storage area
675  /// to accommodate the `datatype` requested.
676  ///
677  /// @returns
678  /// `true` if `get_image_info()` is able to find the
679  /// requested `dataname` for the image and it matched the
680  /// requested `datatype`. If the requested data was not
681  /// found or was not of the right data type, return `false`.
682  /// Except for the `"exists"` query, a file that does not
683  /// exist or could not be read properly as an image also
684  /// constitutes a query failure that will return `false`.
685  virtual bool get_image_info (ustring filename, int subimage, int miplevel,
686  ustring dataname, TypeDesc datatype, void *data) = 0;
687  /// A more efficient variety of `get_image_info()` for cases where you
688  /// can use an `ImageHandle*` to specify the image and optionally have a
689  /// `Perthread*` for the calling thread.
690  virtual bool get_image_info (ImageHandle *file, Perthread *thread_info,
691  int subimage, int miplevel,
692  ustring dataname, TypeDesc datatype, void *data) = 0;
693 
694  /// Copy the ImageSpec associated with the named image (the first
695  /// subimage & miplevel by default, or as set by `subimage` and
696  /// `miplevel`).
697  ///
698  /// @param filename
699  /// The name of the image, as a UTF-8 encoded ustring.
700  /// @param spec
701  /// ImageSpec into which will be copied the spec for the
702  /// requested image.
703  /// @param subimage/miplevel
704  /// The subimage and MIP level to query.
705  /// @param native
706  /// If `false` (the default), then the spec retrieved will
707  /// accurately describe the image stored internally in the
708  /// cache, whereas if `native` is `true`, the spec retrieved
709  /// will reflect the contents of the original file. These
710  /// may differ due to use of certain ImageCache settings
711  /// such as `"forcefloat"` or `"autotile"`.
712  /// @returns
713  /// `true` upon success, `false` upon failure failure (such
714  /// as being unable to find, open, or read the file, or if
715  /// it does not contain the designated subimage or MIP
716  /// level).
717  virtual bool get_imagespec (ustring filename, ImageSpec &spec,
718  int subimage=0, int miplevel=0,
719  bool native=false) = 0;
720  /// A more efficient variety of `get_imagespec()` for cases where you
721  /// can use an `ImageHandle*` to specify the image and optionally have a
722  /// `Perthread*` for the calling thread.
723  virtual bool get_imagespec (ImageHandle *file, Perthread *thread_info,
724  ImageSpec &spec,
725  int subimage=0, int miplevel=0,
726  bool native=false) = 0;
727 
728  /// Return a pointer to an ImageSpec associated with the named image
729  /// (the first subimage & MIP level by default, or as set by `subimage`
730  /// and `miplevel`) if the file is found and is an image format that can
731  /// be read, otherwise return `nullptr`.
732  ///
733  /// This method is much more efficient than `get_imagespec()`, since it
734  /// just returns a pointer to the spec held internally by the ImageCache
735  /// (rather than copying the spec to the user's memory). However, the
736  /// caller must beware that the pointer is only valid as long as nobody
737  /// (even other threads) calls `invalidate()` on the file, or
738  /// `invalidate_all()`, or destroys the ImageCache.
739  ///
740  /// @param filename
741  /// The name of the image, as a UTF-8 encoded ustring.
742  /// @param subimage/miplevel
743  /// The subimage and MIP level to query.
744  /// @param native
745  /// If `false` (the default), then the spec retrieved will
746  /// accurately describe the image stored internally in the
747  /// cache, whereas if `native` is `true`, the spec retrieved
748  /// will reflect the contents of the original file. These
749  /// may differ due to use of certain ImageCache settings
750  /// such as `"forcefloat"` or `"autotile"`.
751  /// @returns
752  /// A pointer to the spec, if the image is found and able to
753  /// be opened and read by an available image format plugin,
754  /// and the designated subimage and MIP level exists.
755  virtual const ImageSpec *imagespec (ustring filename, int subimage=0,
756  int miplevel=0, bool native=false) = 0;
757  /// A more efficient variety of `imagespec()` for cases where you can
758  /// use an `ImageHandle*` to specify the image and optionally have a
759  /// `Perthread*` for the calling thread.
760  virtual const ImageSpec *imagespec (ImageHandle *file,
761  Perthread *thread_info,
762  int subimage=0, int miplevel=0,
763  bool native=false) = 0;
764 
765  /// Copy into `thumbnail` any associated thumbnail associated with this
766  /// image (for the first subimage by default, or as set by `subimage`).
767  ///
768  /// @param filename
769  /// The name of the image, as a UTF-8 encoded ustring.
770  /// @param thumbnail
771  /// ImageBuf into which will be copied the thumbnail, if it
772  /// exists. If no thumbnail can be retrieved, `thumb` will
773  /// be reset to an uninitialized (empty) ImageBuf.
774  /// @param subimage
775  /// The subimage to query.
776  /// @returns
777  /// `true` upon success, `false` upon failure failure (such
778  /// as being unable to find, open, or read the file, or if
779  /// it does not contain a thumbnail).
780  virtual bool get_thumbnail (ustring filename, ImageBuf& thumbnail,
781  int subimage=0) = 0;
782  /// A more efficient variety of `get_thumbnail()` for cases where you
783  /// can use an `ImageHandle*` to specify the image and optionally have a
784  /// `Perthread*` for the calling thread.
785  virtual bool get_thumbnail (ImageHandle *file, Perthread *thread_info,
786  ImageBuf& thumbnail, int subimage=0) = 0;
787  /// @}
788 
789  /// @{
790  /// @name Getting Pixels
791 
792  /// For an image specified by name, retrieve the rectangle of pixels
793  /// from the designated subimage and MIP level, storing the pixel values
794  /// beginning at the address specified by `result` and with the given
795  /// strides. The pixel values will be converted to the data type
796  /// specified by `format`. The rectangular region to be retrieved
797  /// includes `begin` but does not include `end` (much like STL begin/end
798  /// usage). Requested pixels that are not part of the valid pixel data
799  /// region of the image file will be filled with zero values.
800  ///
801  /// @param filename
802  /// The name of the image, as a UTF-8 encoded ustring.
803  /// @param subimage/miplevel
804  /// The subimage and MIP level to retrieve pixels from.
805  /// @param xbegin/xend/ybegin/yend/zbegin/zend
806  /// The range of pixels to retrieve. The pixels retrieved
807  /// include the begin value but not the end value (much like
808  /// STL begin/end usage).
809  /// @param chbegin/chend
810  /// Channel range to retrieve. To retrieve all channels, use
811  /// `chbegin = 0`, `chend = nchannels`.
812  /// @param format
813  /// TypeDesc describing the data type of the values you want
814  /// to retrieve into `result`. The pixel values will be
815  /// converted to this type regardless of how they were
816  /// stored in the file.
817  /// @param result
818  /// Pointer to the memory where the pixel values should be
819  /// stored. It is up to the caller to ensure that `result`
820  /// points to an area of memory big enough to accommodate
821  /// the requested rectangle (taking into consideration its
822  /// dimensions, number of channels, and data format).
823  /// @param xstride/ystride/zstride
824  /// The number of bytes between the beginning of successive
825  /// pixels, scanlines, and image planes, respectively. Any
826  /// stride values set to `AutoStride` will be assumed to
827  /// indicate a contiguous data layout in that dimension.
828  /// @param cache_chbegin/cache_chend These parameters can be used to
829  /// tell the ImageCache to read and cache a subset of
830  /// channels (if not specified or if they denote a
831  /// non-positive range, all the channels of the file will be
832  /// stored in the cached tile).
833  ///
834  /// @returns
835  /// `true` for success, `false` for failure.
836  virtual bool get_pixels (ustring filename,
837  int subimage, int miplevel, int xbegin, int xend,
838  int ybegin, int yend, int zbegin, int zend,
839  int chbegin, int chend, TypeDesc format, void *result,
840  stride_t xstride=AutoStride, stride_t ystride=AutoStride,
841  stride_t zstride=AutoStride,
842  int cache_chbegin = 0, int cache_chend = -1) = 0;
843  /// A more efficient variety of `get_pixels()` for cases where you can
844  /// use an `ImageHandle*` to specify the image and optionally have a
845  /// `Perthread*` for the calling thread.
846  virtual bool get_pixels (ImageHandle *file, Perthread *thread_info,
847  int subimage, int miplevel, int xbegin, int xend,
848  int ybegin, int yend, int zbegin, int zend,
849  int chbegin, int chend, TypeDesc format, void *result,
850  stride_t xstride=AutoStride, stride_t ystride=AutoStride,
851  stride_t zstride=AutoStride,
852  int cache_chbegin = 0, int cache_chend = -1) = 0;
853 
854  /// A simplified `get_pixels()` where all channels are retrieved,
855  /// strides are assumed to be contiguous.
856  virtual bool get_pixels (ustring filename, int subimage, int miplevel,
857  int xbegin, int xend, int ybegin, int yend,
858  int zbegin, int zend,
859  TypeDesc format, void *result) = 0;
860  /// A more efficient variety of `get_pixels()` for cases where you can
861  /// use an `ImageHandle*` to specify the image and optionally have a
862  /// `Perthread*` for the calling thread.
863  virtual bool get_pixels (ImageHandle *file, Perthread *thread_info,
864  int subimage, int miplevel,
865  int xbegin, int xend, int ybegin, int yend,
866  int zbegin, int zend,
867  TypeDesc format, void *result) = 0;
868  /// @}
869 
870  /// @{
871  /// @name Controlling the cache
872  ///
873 
874  /// Invalidate any loaded tiles or open file handles associated with the
875  /// filename (UTF-8 encoded), so that any subsequent queries will be
876  /// forced to re-open the file or re-load any tiles (even those that were
877  /// previously loaded and would ordinarily be reused). A client might do
878  /// this if, for example, they are aware that an image being held in the
879  /// cache has been updated on disk. This is safe to do even if other
880  /// procedures are currently holding reference-counted tile pointers from
881  /// the named image, but those procedures will not get updated pixels
882  /// until they release the tiles they are holding.
883  ///
884  /// If `force` is true, this invalidation will happen unconditionally; if
885  /// false, the file will only be invalidated if it has been changed since
886  /// it was first opened by the ImageCache.
887  virtual void invalidate(ustring filename, bool force = true) = 0;
888 
889  /// A more efficient variety of `invalidate()` for cases where you
890  /// already have an `ImageHandle*` for the file you want to invalidate.
891  virtual void invalidate(ImageHandle* file, bool force = true) = 0;
892 
893  /// Invalidate all loaded tiles and close open file handles. This is
894  /// safe to do even if other procedures are currently holding
895  /// reference-counted tile pointers from the named image, but those
896  /// procedures will not get updated pixels (if the images change) until
897  /// they release the tiles they are holding.
898  ///
899  /// If `force` is true, everything will be invalidated, no matter how
900  /// wasteful it is, but if `force` is false, in actuality files will
901  /// only be invalidated if their modification times have been changed
902  /// since they were first opened.
903  virtual void invalidate_all(bool force = false) = 0;
904 
905  /// Close any open file handles associated with a named file (UTF-8
906  /// encoded), but do not invalidate any image spec information or pixels
907  /// associated with the files. A client might do this in order to release
908  /// OS file handle resources, or to make it safe for other processes to
909  /// modify image files on disk.
910  virtual void close (ustring filename) = 0;
911 
912  /// `close()` all files known to the cache.
913  virtual void close_all () = 0;
914 
915  /// An opaque data type that allows us to have a pointer to a tile but
916  /// without exposing any internals.
917  typedef pvt::ImageCacheTile Tile;
918 
919  /// Find the tile specified by an image filename (UTF-8 encoded), subimage
920  /// & miplevel, the coordinates of a pixel, and optionally a channel
921  /// range. An opaque pointer to the tile will be returned, or `nullptr`
922  /// if no such file (or tile within the file) exists or can be read. The
923  /// tile will not be purged from the cache until after `release_tile()` is
924  /// called on the tile pointer the same number of times that `get_tile()`
925  /// was called (reference counting). This is thread-safe! If `chend <
926  /// chbegin`, it will retrieve a tile containing all channels in the file.
927  virtual Tile * get_tile (ustring filename, int subimage, int miplevel,
928  int x, int y, int z,
929  int chbegin = 0, int chend = -1) = 0;
930  /// A slightly more efficient variety of `get_tile()` for cases where
931  /// you can use an `ImageHandle*` to specify the image and optionally
932  /// have a `Perthread*` for the calling thread.
933  ///
934  /// @see `get_pixels()`
935  virtual Tile * get_tile (ImageHandle *file, Perthread *thread_info,
936  int subimage, int miplevel,
937  int x, int y, int z,
938  int chbegin = 0, int chend = -1) = 0;
939 
940  /// After finishing with a tile, release_tile will allow it to
941  /// once again be purged from the tile cache if required.
942  virtual void release_tile(Tile* tile) const = 0;
943 
944  /// Retrieve the data type of the pixels stored in the tile, which may
945  /// be different than the type of the pixels in the disk file.
946  virtual TypeDesc tile_format(const Tile* tile) const = 0;
947 
948  /// Retrieve the ROI describing the pixels and channels stored in the
949  /// tile.
950  virtual ROI tile_roi(const Tile* tile) const = 0;
951 
952  /// For a tile retrieved by `get_tile()`, return a pointer to the pixel
953  /// data itself, and also store in `format` the data type that the
954  /// pixels are internally stored in (which may be different than the
955  /// data type of the pixels in the disk file). This method should only
956  /// be called on a tile that has been requested by `get_tile()` but has
957  /// not yet been released with `release_tile()`.
958  virtual const void* tile_pixels(Tile* tile, TypeDesc& format) const = 0;
959 
960  /// The add_file() call causes a file to be opened or added to the
961  /// cache. There is no reason to use this method unless you are
962  /// supplying a custom creator, or configuration, or both.
963  ///
964  /// If creator is not NULL, it points to an ImageInput::Creator that
965  /// will be used rather than the default ImageInput::create(), thus
966  /// instead of reading from disk, creates and uses a custom ImageInput
967  /// to generate the image. The 'creator' is a factory that creates the
968  /// custom ImageInput and will be called like this:
969  ///
970  /// std::unique_ptr<ImageInput> in (creator());
971  ///
972  /// Once created, the ImageCache owns the ImageInput and is responsible
973  /// for destroying it when done. Custom ImageInputs allow "procedural"
974  /// images, among other things. Also, this is the method you use to set
975  /// up a "writable" ImageCache images (perhaps with a type of ImageInput
976  /// that's just a stub that does as little as possible).
977  ///
978  /// If `config` is not NULL, it points to an ImageSpec with configuration
979  /// options/hints that will be passed to the underlying
980  /// ImageInput::open() call. Thus, this can be used to ensure that the
981  /// ImageCache opens a call with special configuration options.
982  ///
983  /// This call (including any custom creator or configuration hints) will
984  /// have no effect if there's already an image by the same name in the
985  /// cache. Custom creators or configurations only "work" the FIRST time
986  /// a particular filename is referenced in the lifetime of the
987  /// ImageCache. But if replace is true, any existing entry will be
988  /// invalidated, closed and overwritten. So any subsequent access will
989  /// see the new file. Existing texture handles will still be valid.
990  virtual bool add_file (ustring filename, ImageInput::Creator creator=nullptr,
991  const ImageSpec *config=nullptr,
992  bool replace = false) = 0;
993 
994  /// Preemptively add a tile corresponding to the named image, at the
995  /// given subimage, MIP level, and channel range. The tile added is the
996  /// one whose corner is (x,y,z), and buffer points to the pixels (in the
997  /// given format, with supplied strides) which will be copied and
998  /// inserted into the cache and made available for future lookups.
999  /// If chend < chbegin, it will add a tile containing the full set of
1000  /// channels for the image. Note that if the 'copy' flag is false, the
1001  /// data is assumed to be in some kind of persistent storage and will
1002  /// not be copied, nor will its pixels take up additional memory in the
1003  /// cache.
1004  virtual bool add_tile (ustring filename, int subimage, int miplevel,
1005  int x, int y, int z, int chbegin, int chend,
1006  TypeDesc format, const void *buffer,
1007  stride_t xstride=AutoStride, stride_t ystride=AutoStride,
1008  stride_t zstride=AutoStride, bool copy = true) = 0;
1009 
1010  /// @}
1011 
1012  /// @{
1013  /// @name Errors and statistics
1014 
1015  /// Is there a pending error message waiting to be retrieved?
1016  virtual bool has_error() const = 0;
1017 
1018  /// Return the text of all pending error messages issued against this
1019  /// ImageCache, and clear the pending error message unless `clear` is
1020  /// false. If no error message is pending, it will return an empty
1021  /// string.
1022  virtual std::string geterror(bool clear = true) const = 0;
1023 
1024  /// Returns a big string containing useful statistics about the
1025  /// ImageCache operations, suitable for saving to a file or outputting
1026  /// to the terminal. The `level` indicates the amount of detail in
1027  /// the statistics, with higher numbers (up to a maximum of 5) yielding
1028  /// more and more esoteric information.
1029  virtual std::string getstats(int level = 1) const = 0;
1030 
1031  /// Reset most statistics to be as they were with a fresh ImageCache.
1032  /// Caveat emptor: this does not flush the cache itelf, so the resulting
1033  /// statistics from the next set of texture requests will not match the
1034  /// number of tile reads, etc., that would have resulted from a new
1035  /// ImageCache.
1036  virtual void reset_stats() = 0;
1037 
1038  /// @}
1039 
1040  virtual ~ImageCache() {}
1041 
1042 protected:
1043  // User code should never directly construct or destruct an ImageCache.
1044  // Always use ImageCache::create() and ImageCache::destroy().
1045  ImageCache(void) {}
1046 private:
1047  // Make delete private and unimplemented in order to prevent apps
1048  // from calling it. Instead, they should call ImageCache::destroy().
1049  void operator delete(void* /*todel*/) {}
1050 };
1051 
1052 
OIIO_API std::string geterror(bool clear=true)
OIIO_API bool has_error()
Is there a pending global error message waiting to be retrieved?
int64_t stride_t
Definition: imageio.h:48
GT_API const UT_StringHolder filename
OIIO_UTIL_API bool copy(string_view from, string_view to, std::string &err)
pvt::ImageCacheFile ImageHandle
Definition: imagecache.h:482
GLdouble GLdouble GLdouble z
Definition: glcorearb.h:848
GLint level
Definition: glcorearb.h:108
ImageCache(void)
Definition: imagecache.h:1045
GLint y
Definition: glcorearb.h:103
**But if you need a result
Definition: thread.h:622
void close() override
GLuint buffer
Definition: glcorearb.h:660
ImageInput *(* Creator)()
Definition: imageio.h:1779
GLint GLint GLsizei GLint GLenum GLenum type
Definition: glcorearb.h:108
std::string OIIO_UTIL_API replace(string_view str, string_view pattern, string_view replacement, bool global=false)
GLint GLint GLsizei GLint GLenum format
Definition: glcorearb.h:108
GLuint const GLchar * name
Definition: glcorearb.h:786
GLint GLenum GLint x
Definition: glcorearb.h:409
pvt::ImageCacheTile Tile
Definition: imagecache.h:917
Definition: imageio.h:85
OIIO_API bool getattribute(string_view name, TypeDesc type, void *val)
SIM_API const UT_StringHolder force
const stride_t AutoStride
Definition: imageio.h:56
GLuint GLfloat * val
Definition: glcorearb.h:1608
OIIO_API bool attribute(string_view name, TypeDesc type, const void *val)
pvt::ImageCachePerThreadInfo Perthread
Definition: imagecache.h:457
OutGridT XformOp bool bool shared
#define OIIO_NAMESPACE_END
Definition: oiioversion.h:127
virtual ~ImageCache()
Definition: imagecache.h:1040
std::string OIIO_UTIL_API utf16_to_utf8(const std::wstring &utf16str) noexcept
Definition: format.h:1821
ImageHandle * get_image_handle(const std::wstring &filename, Perthread *thread_info=nullptr, const TextureOpt *options=nullptr)
Get an ImageHandle using a UTF-16 encoded wstring filename.
Definition: imagecache.h:495
#define OIIO_NAMESPACE_BEGIN
Definition: oiioversion.h:126
#define OIIO_API
Definition: export.h:65