1// Copyright 2008-present Contributors to the OpenImageIO project.
2// SPDX-License-Identifier: BSD-3-Clause
3// https://github.com/OpenImageIO/oiio/blob/master/LICENSE.md
4
5
6/// \file
7/// Declarations for things that are used privately by ImageIO.
8
9
10#ifndef OPENIMAGEIO_IMAGEIO_PVT_H
11#define OPENIMAGEIO_IMAGEIO_PVT_H
12
13#include <OpenImageIO/imageio.h>
14#include <OpenImageIO/thread.h>
15#include <OpenImageIO/timer.h>
16
17
18
19OIIO_NAMESPACE_BEGIN
20
21namespace pvt {
22
23/// Mutex allowing thread safety of ImageOutput internals
24///
25extern recursive_mutex imageio_mutex;
26extern atomic_int oiio_threads;
27extern atomic_int oiio_read_chunk;
28extern ustring plugin_searchpath;
29extern std::string format_list;
30extern std::string input_format_list;
31extern std::string output_format_list;
32extern std::string extension_list;
33extern std::string library_list;
34extern int oiio_print_debug;
35extern int oiio_log_times;
36
37
38// For internal use - use error() below for a nicer interface.
39void seterror (string_view message);
40
41/// Use error() privately only.  Prototype is conceptually printf-like, but
42/// also fully typesafe:
43template<typename... Args>
44inline void errorf (const char* fmt, const Args&... args) {
45    seterror(Strutil::sprintf (fmt, args...));
46}
47
48// Make sure all plugins are inventoried. For internal use only.
49void catalog_all_plugins (std::string searchpath);
50
51/// Given the format, set the default quantization range.
52void get_default_quantize (TypeDesc format, long long &quant_min,
53                           long long &quant_max) noexcept;
54
55/// Turn potentially non-contiguous-stride data (e.g. "RGBxRGBx") into
56/// contiguous-stride ("RGBRGB"), for any format or stride values
57/// (measured in bytes).  Caller must pass in a dst pointing to enough
58/// memory to hold the contiguous rectangle.  Return a ptr to where the
59/// contiguous data ended up, which is either dst or src (if the strides
60/// indicated that data were already contiguous).
61const void *contiguize (const void *src, int nchannels,
62                        stride_t xstride, stride_t ystride, stride_t zstride,
63                        void *dst, int width, int height, int depth,
64                        TypeDesc format);
65
66/// Turn contiguous data from any format into float data.  Return a
67/// pointer to the converted data (which may still point to src if no
68/// conversion was necessary).
69const float *convert_to_float (const void *src, float *dst, int nvals,
70                               TypeDesc format);
71
72/// Turn contiguous float data into any format.  Return a pointer to the
73/// converted data (which may still point to src if no conversion was
74/// necessary).
75const void *convert_from_float (const float *src, void *dst,
76                                size_t nvals, TypeDesc format);
77
78/// A version of convert_from_float that will break up big jobs with
79/// multiple threads.
80const void *parallel_convert_from_float (const float *src, void *dst,
81                                         size_t nvals, TypeDesc format);
82
83/// Internal utility: Error checking on the spec -- if it contains texture-
84/// specific metadata but there are clues it's not actually a texture file
85/// written by maketx or `oiiotool -otex`, then assume these metadata are
86/// wrong and delete them. Return true if we think it's one of these
87/// incorrect files and it was fixed.
88OIIO_API bool check_texture_metadata_sanity (ImageSpec &spec);
89
90/// Internal function to log time recorded by an OIIO::timer(). It will only
91/// trigger a read of the time if the "log_times" attribute is set or the
92/// OPENIMAGEIO_LOG_TIMES env variable is set.
93OIIO_API void log_time (string_view key, const Timer& timer);
94
95/// Get the timing report from log_time entries.
96OIIO_API std::string timing_report ();
97
98/// An object that, if oiio_log_times is nonzero, logs time until its
99/// destruction. If oiio_log_times is 0, it does nothing.
100class LoggedTimer {
101public:
102    LoggedTimer (string_view name) : m_timer(oiio_log_times) {
103        if (oiio_log_times)
104            m_name = name;
105    }
106    ~LoggedTimer () {
107        if (oiio_log_times)
108            log_time (m_name, m_timer);
109    }
110    void stop () { m_timer.stop(); }
111    void start () { m_timer.start(); }
112    void rename (string_view name) { m_name = name; }
113private:
114    Timer m_timer;
115    std::string m_name;
116};
117
118}  // namespace pvt
119
120OIIO_NAMESPACE_END
121
122//Define a default plugin search path
123#define OIIO_DEFAULT_PLUGIN_SEARCHPATH "@PLUGIN_SEARCH_PATH_NATIVE@"
124
125#endif // OPENIMAGEIO_IMAGEIO_PVT_H
126