NVIDIA NvNeural SDK  2022.2
GPU inference framework for NVIDIA Nsight Deep Learning Designer
CpuImage.h
Go to the documentation of this file.
1 /*
2 * SPDX-FileCopyrightText: Copyright (c) 2020-2021 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
3 * SPDX-License-Identifier: MIT
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 */
23 
25 
26 #ifndef NVNEURAL_CPUIMAGE_H
27 #define NVNEURAL_CPUIMAGE_H
28 
29 #include <nvneural/CoreTypes.h>
30 #include <nvneural/CoreHelpers.h>
31 #include <memory>
32 #include <string>
33 #include <vector>
34 #include <stb/stb_image.h>
35 #include <stb/stb_image_write.h>
36 
37 namespace nvneural {
38 
46 {
47  static const std::size_t DefaultChannels = 0;
48 
50  std::string filename;
52  std::size_t channels = DefaultChannels;
53 
57  ImageDescriptor(std::string filename_, std::size_t channels_)
58  : filename(filename_)
59  , channels(channels_)
60  {}
61 };
62 
69 class IFileImage : public IRefObject
70 {
71 public:
73  static const IRefObject::TypeId typeID = 0x6c7a3cb3f81db69bul;
74 
78  virtual NeuralResult loadFromFile(const std::string& filename) noexcept = 0;
79 
86  virtual NeuralResult loadFromDescriptors(const std::vector<ImageDescriptor>& descriptors) noexcept = 0;
87 
94  virtual NeuralResult fromFill(std::size_t height, std::size_t width, std::size_t channels, std::uint8_t fillValue) noexcept = 0;
95 
101  virtual NeuralResult fromRandomValues(std::size_t height, std::size_t width, std::size_t channels) noexcept = 0;
102 
106  virtual NeuralResult saveToFile(const std::string& filename) noexcept = 0;
107 };
108 
111  nvneural::refobj::Implements<nvneural::IFileImage>,
112  nvneural::refobj::Implements<nvneural::IImage>>
113 {
114 public:
115  // IFileImage members
116 
118  NeuralResult loadFromFile(const std::string& filename) noexcept override;
120  NeuralResult loadFromDescriptors(const std::vector<ImageDescriptor>& descriptors) noexcept override;
122  NeuralResult fromFill(std::size_t height, std::size_t width, std::size_t channels, std::uint8_t fillValue) noexcept override;
124  NeuralResult fromRandomValues(std::size_t height, std::size_t width, std::size_t channels) noexcept override;
126  NeuralResult saveToFile(const std::string& filename) noexcept override;
127 
128  // IImage members
130  NeuralResult resize(std::size_t height, std::size_t width, std::size_t channels) noexcept override;
132  std::size_t height() const noexcept override;
134  std::size_t width() const noexcept override;
136  std::size_t channels() const noexcept override;
138  std::size_t elements() const noexcept override;
140  std::uint8_t* data() noexcept override;
142  const std::uint8_t* data() const noexcept override;
143 
144 private:
145  static const std::uint8_t FillValue = 0u;
146 
147  std::unique_ptr<std::uint8_t[], decltype(&stbi_image_free)> m_data{nullptr, stbi_image_free};
148  std::size_t m_height = 0;
149  std::size_t m_width = 0;
150  std::size_t m_channels = 0;
151 
152  NeuralResult resetBuffer(std::size_t height, std::size_t width, std::size_t channels) noexcept;
153 
154  NeuralResult addChannelPadding(std::size_t extraChannels) noexcept;
155  NeuralResult mergeImage(const Image& image) noexcept;
156  NeuralResult loadFromFile(const std::string& filename, std::size_t forceChannels) noexcept;
157 };
158 
159 } // namespace nvneural
160 
161 #endif // NVNEURAL_CPUIMAGE_H
Common helper classes and template function implementations.
Fundamental NvNeural data types are declared here.
NeuralResult
NeuralResult is a generic success/failure result type similar to COM HRESULT.
Definition: CoreTypes.h:275
Defines file I/O operations for IImage objects.
Definition: CpuImage.h:70
static const IRefObject::TypeId typeID
Interface TypeId for InterfaceOf purposes.
Definition: CpuImage.h:73
virtual NeuralResult loadFromDescriptors(const std::vector< ImageDescriptor > &descriptors) noexcept=0
Loads an image from a chained set of descriptors.
virtual NeuralResult fromFill(std::size_t height, std::size_t width, std::size_t channels, std::uint8_t fillValue) noexcept=0
Loads an image by tiling a fill value.
virtual NeuralResult fromRandomValues(std::size_t height, std::size_t width, std::size_t channels) noexcept=0
Loads an image by generating normally distributed noise in the range [0, 255].
virtual NeuralResult loadFromFile(const std::string &filename) noexcept=0
Loads an image from a single bitmap file.
virtual NeuralResult saveToFile(const std::string &filename) noexcept=0
Saves the resulting image as a file.
Base class for all objects, similar to COM's IUnknown.
Definition: CoreTypes.h:343
std::uint64_t TypeId
Every interface must define a unique TypeId. This should be randomized.
Definition: CoreTypes.h:349
Standard implementation for IFileImage.
Definition: CpuImage.h:113
NeuralResult loadFromDescriptors(const std::vector< ImageDescriptor > &descriptors) noexcept
Loads an image from a chained set of descriptors.
Definition: CpuImage.cpp:57
NeuralResult fromFill(std::size_t height, std::size_t width, std::size_t channels, std::uint8_t fillValue) noexcept
Loads an image by tiling a fill value.
Definition: CpuImage.cpp:268
NeuralResult saveToFile(const std::string &filename) noexcept
Saves the resulting image as a file.
Definition: CpuImage.cpp:280
NeuralResult fromRandomValues(std::size_t height, std::size_t width, std::size_t channels) noexcept
Loads an image by generating normally distributed noise in the range [0, 255].
Definition: CpuImage.cpp:253
std::size_t height() const noexcept
Returns the height of the image in pixels.
Definition: CpuImage.cpp:394
NeuralResult loadFromFile(const std::string &filename) noexcept
Loads an image from a single bitmap file.
Definition: CpuImage.cpp:52
std::size_t width() const noexcept
Returns the width of the image in pixels.
Definition: CpuImage.cpp:399
NeuralResult resize(std::size_t height, std::size_t width, std::size_t channels) noexcept
Clears the image and reserves storage.
Definition: CpuImage.cpp:97
std::size_t channels() const noexcept
Returns the number of color channels in the image.
Definition: CpuImage.cpp:404
std::uint8_t * data() noexcept
Returns a pointer to the raw pixel data in HWC layout.
Definition: CpuImage.cpp:414
std::size_t elements() const noexcept
Returns the number of data elements in the image.
Definition: CpuImage.cpp:409
Defines one image component to load into a tensor.
Definition: CpuImage.h:46
std::size_t channels
Number of channels to take from the image.
Definition: CpuImage.h:52
ImageDescriptor(std::string filename_, std::size_t channels_)
Constructs an ImageDescriptor with filename and channel count.
Definition: CpuImage.h:57
static const std::size_t DefaultChannels
Use the channel count from the image file.
Definition: CpuImage.h:47
std::string filename
Filename of an image.
Definition: CpuImage.h:50
Parameterized base class implementing common IRefObject operations.
Definition: RefObject.h:336