Loading...
Searching...
No Matches
image.h
Go to the documentation of this file.
1//
2// Copyright 2016 Pixar
3//
4// Licensed under the Apache License, Version 2.0 (the "Apache License")
5// with the following modification; you may not use this file except in
6// compliance with the Apache License and the following modification to it:
7// Section 6. Trademarks. is deleted and replaced with:
8//
9// 6. Trademarks. This License does not grant permission to use the trade
10// names, trademarks, service marks, or product names of the Licensor
11// and its affiliates, except as required to comply with Section 4(c) of
12// the License and to reproduce the content of the NOTICE file.
13//
14// You may obtain a copy of the Apache License at
15//
16// http://www.apache.org/licenses/LICENSE-2.0
17//
18// Unless required by applicable law or agreed to in writing, software
19// distributed under the Apache License with the above modification is
20// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
21// KIND, either express or implied. See the Apache License for the specific
22// language governing permissions and limitations under the Apache License.
23//
24#ifndef PXR_IMAGING_HIO_IMAGE_H
25#define PXR_IMAGING_HIO_IMAGE_H
26
28
29#include "pxr/pxr.h"
30#include "pxr/imaging/hio/api.h"
31#include "pxr/imaging/hio/types.h"
32
33#include "pxr/base/tf/token.h"
34#include "pxr/base/tf/type.h"
36#include "pxr/base/vt/value.h"
37
38#include <memory>
39#include <string>
40
41PXR_NAMESPACE_OPEN_SCOPE
42
43
44using HioImageSharedPtr = std::shared_ptr<class HioImage>;
45
55{
56public:
57
61 {
62 OriginUpperLeft,
63 OriginLowerLeft
64 };
65
71 {
72 Raw,
73 SRGB,
74 Auto
75 };
76
82 {
83 public:
85 : width(0), height(0), depth(0)
86 , format(HioFormatInvalid)
87 , flipped(false)
88 , data(0) { }
89
90 int width, height, depth;
91 HioFormat format;
92 bool flipped;
93 void * data;
94 };
95
96public:
97 HioImage() = default;
98
99 HIO_API
100 virtual ~HioImage();
101
102 // Disallow copies
103 HioImage(const HioImage&) = delete;
104 HioImage& operator=(const HioImage&) = delete;
105
107 HIO_API
108 static bool IsSupportedImageFile(std::string const & filename);
109
112
116 HIO_API
117 static HioImageSharedPtr OpenForReading(std::string const & filename,
118 int subimage = 0,
119 int mip = 0,
120 SourceColorSpace sourceColorSpace =
121 SourceColorSpace::Auto,
122 bool suppressErrors = false);
123
125 virtual bool Read(StorageSpec const & storage) = 0;
126
128 virtual bool ReadCropped(int const cropTop,
129 int const cropBottom,
130 int const cropLeft,
131 int const cropRight,
132 StorageSpec const & storage) = 0;
133
135
138
140 HIO_API
141 static HioImageSharedPtr OpenForWriting(std::string const & filename);
142
144 virtual bool Write(StorageSpec const & storage,
145 VtDictionary const & metadata = VtDictionary()) = 0;
146
148
150 virtual std::string const & GetFilename() const = 0;
151
153 virtual int GetWidth() const = 0;
154
156 virtual int GetHeight() const = 0;
157
159 virtual HioFormat GetFormat() const = 0;
160
162 virtual int GetBytesPerPixel() const = 0;
163
165 virtual int GetNumMipLevels() const = 0;
166
168 virtual bool IsColorSpaceSRGB() const = 0;
169
172 template <typename T>
173 bool GetMetadata(TfToken const & key, T * value) const;
174
175 virtual bool GetMetadata(TfToken const & key, VtValue * value) const = 0;
176
177 virtual bool GetSamplerMetadata(HioAddressDimension dim,
178 HioAddressMode * param) const = 0;
179
181
182protected:
183 virtual bool _OpenForReading(std::string const & filename,
184 int subimage,
185 int mip,
186 SourceColorSpace sourceColorSpace,
187 bool suppressErrors) = 0;
188
189 virtual bool _OpenForWriting(std::string const & filename) = 0;
190};
191
192template <typename T>
193bool
194HioImage::GetMetadata(TfToken const & key, T * value) const
195{
196 VtValue any;
197 if (!GetMetadata(key, &any) || !any.IsHolding<T>()) {
198 return false;
199 }
200 *value = any.UncheckedGet<T>();
201 return true;
202}
203
204class HIO_API HioImageFactoryBase : public TfType::FactoryBase {
205public:
206 virtual HioImageSharedPtr New() const = 0;
207};
208
209template <class T>
210class HioImageFactory : public HioImageFactoryBase {
211public:
212 virtual HioImageSharedPtr New() const
213 {
214 return HioImageSharedPtr(new T);
215 }
216};
217
218
219PXR_NAMESPACE_CLOSE_SCOPE
220
221#endif // PXR_IMAGING_HIO_IMAGE_H
Describes the memory layout and storage of a texture image.
Definition: image.h:82
A base class for reading and writing texture image data.
Definition: image.h:55
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:71
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:61
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:88
Base class of all factory types.
Definition: type.h:73
A map with string keys and VtValue values.
Definition: dictionary.h:60
Provides a container which may hold any type, and provides introspection and iteration over array typ...
Definition: value.h:164
bool IsHolding() const
Return true if this value is holding an object of type T, false otherwise.
Definition: value.h:1081
T const & UncheckedGet() const &
Returns a const reference to the held object if the held object is of type T.
Definition: value.h:1121
TfToken class for efficient string referencing and hashing, plus conversions to and from stl string c...