Loading...
Searching...
No Matches
image.h
Go to the documentation of this file.
1//
2// Copyright 2016 Pixar
3//
4// Licensed under the terms set forth in the LICENSE.txt file available at
5// https://openusd.org/license.
6//
7#ifndef PXR_IMAGING_HIO_IMAGE_H
8#define PXR_IMAGING_HIO_IMAGE_H
9
11
12#include "pxr/pxr.h"
13#include "pxr/imaging/hio/api.h"
14#include "pxr/imaging/hio/types.h"
15
16#include "pxr/base/tf/token.h"
17#include "pxr/base/tf/type.h"
19#include "pxr/base/vt/value.h"
20
21#include <memory>
22#include <string>
23
24PXR_NAMESPACE_OPEN_SCOPE
25
26
27using HioImageSharedPtr = std::shared_ptr<class HioImage>;
28
38{
39public:
40
44 {
45 OriginUpperLeft,
46 OriginLowerLeft
47 };
48
54 {
55 Raw,
56 SRGB,
57 Auto
58 };
59
65 {
66 public:
68 : width(0), height(0), depth(0)
69 , format(HioFormatInvalid)
70 , flipped(false)
71 , data(0) { }
72
73 int width, height, depth;
74 HioFormat format;
75 bool flipped;
76 void * data;
77 };
78
79public:
80 HioImage() = default;
81
82 HIO_API
83 virtual ~HioImage();
84
85 // Disallow copies
86 HioImage(const HioImage&) = delete;
87 HioImage& operator=(const HioImage&) = delete;
88
90 HIO_API
91 static bool IsSupportedImageFile(std::string const & filename);
92
95
99 HIO_API
100 static HioImageSharedPtr OpenForReading(std::string const & filename,
101 int subimage = 0,
102 int mip = 0,
103 SourceColorSpace sourceColorSpace =
104 SourceColorSpace::Auto,
105 bool suppressErrors = false);
106
108 virtual bool Read(StorageSpec const & storage) = 0;
109
111 virtual bool ReadCropped(int const cropTop,
112 int const cropBottom,
113 int const cropLeft,
114 int const cropRight,
115 StorageSpec const & storage) = 0;
116
118
121
123 HIO_API
124 static HioImageSharedPtr OpenForWriting(std::string const & filename);
125
127 virtual bool Write(StorageSpec const & storage,
128 VtDictionary const & metadata = VtDictionary()) = 0;
129
131
133 virtual std::string const & GetFilename() const = 0;
134
136 virtual int GetWidth() const = 0;
137
139 virtual int GetHeight() const = 0;
140
142 virtual HioFormat GetFormat() const = 0;
143
145 virtual int GetBytesPerPixel() const = 0;
146
148 virtual int GetNumMipLevels() const = 0;
149
151 virtual bool IsColorSpaceSRGB() const = 0;
152
155 template <typename T>
156 bool GetMetadata(TfToken const & key, T * value) const;
157
158 virtual bool GetMetadata(TfToken const & key, VtValue * value) const = 0;
159
160 virtual bool GetSamplerMetadata(HioAddressDimension dim,
161 HioAddressMode * param) const = 0;
162
164
165protected:
166 virtual bool _OpenForReading(std::string const & filename,
167 int subimage,
168 int mip,
169 SourceColorSpace sourceColorSpace,
170 bool suppressErrors) = 0;
171
172 virtual bool _OpenForWriting(std::string const & filename) = 0;
173};
174
175template <typename T>
176bool
177HioImage::GetMetadata(TfToken const & key, T * value) const
178{
179 VtValue any;
180 if (!GetMetadata(key, &any) || !any.IsHolding<T>()) {
181 return false;
182 }
183 *value = any.UncheckedGet<T>();
184 return true;
185}
186
187class HIO_API HioImageFactoryBase : public TfType::FactoryBase {
188public:
189 virtual HioImageSharedPtr New() const = 0;
190};
191
192template <class T>
193class HioImageFactory : public HioImageFactoryBase {
194public:
195 virtual HioImageSharedPtr New() const
196 {
197 return HioImageSharedPtr(new T);
198 }
199};
200
201
202PXR_NAMESPACE_CLOSE_SCOPE
203
204#endif // PXR_IMAGING_HIO_IMAGE_H
Describes the memory layout and storage of a texture image.
Definition: image.h:65
A base class for reading and writing texture image data.
Definition: image.h:38
virtual bool ReadCropped(int const cropTop, int const cropBottom, int const cropLeft, int const cropRight, StorageSpec const &storage)=0
Reads the cropped sub-image into storage.
virtual bool Write(StorageSpec const &storage, VtDictionary const &metadata=VtDictionary())=0
Writes the image with metadata.
virtual bool _OpenForReading(std::string const &filename, int subimage, int mip, SourceColorSpace sourceColorSpace, bool suppressErrors)=0
}@
virtual std::string const & GetFilename() const =0
}@
virtual HioFormat GetFormat() const =0
Returns the destination HioFormat.
virtual int GetNumMipLevels() const =0
Returns the number of mips available.
virtual int GetWidth() const =0
Returns the image width.
virtual int GetBytesPerPixel() const =0
Returns the number of bytes per pixel.
virtual int GetHeight() const =0
Returns the image height.
static HIO_API HioImageSharedPtr OpenForReading(std::string const &filename, int subimage=0, int mip=0, SourceColorSpace sourceColorSpace=SourceColorSpace::Auto, bool suppressErrors=false)
Opens filename for reading from the given subimage at mip level mip, using sourceColorSpace to help d...
virtual bool Read(StorageSpec const &storage)=0
Reads the image file into storage.
SourceColorSpace
Specifies the source color space in which the texture is encoded, with "Auto" indicating the texture ...
Definition: image.h:54
static HIO_API bool IsSupportedImageFile(std::string const &filename)
Returns whether filename opened as a texture image.
static HIO_API HioImageSharedPtr OpenForWriting(std::string const &filename)
Opens filename for writing from the given storage.
ImageOriginLocation
Specifies whether to treat the image origin as the upper-left corner or the lower left.
Definition: image.h:44
virtual bool IsColorSpaceSRGB() const =0
Returns whether the image is in the sRGB color space.
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:71
Base class of all factory types.
Definition: type.h:56
A map with string keys and VtValue values.
Definition: dictionary.h:43
Provides a container which may hold any type, and provides introspection and iteration over array typ...
Definition: value.h:152
bool IsHolding() const
Return true if this value is holding an object of type T, false otherwise.
Definition: value.h:1050
T const & UncheckedGet() const &
Returns a const reference to the held object if the held object is of type T.
Definition: value.h:1094
TfToken class for efficient string referencing and hashing, plus conversions to and from stl string c...