Program Listing for File Image.hpp

Return to documentation for file (nvcv_types/include/nvcv/Image.hpp)

/*
 * SPDX-FileCopyrightText: Copyright (c) 2022-2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
 * SPDX-License-Identifier: Apache-2.0
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef NVCV_IMAGE_HPP
#define NVCV_IMAGE_HPP

#include "CoreResource.hpp"
#include "HandleWrapper.hpp"
#include "Image.h"
#include "ImageData.hpp"
#include "ImageFormat.hpp"
#include "Size.hpp"
#include "alloc/Allocator.hpp"
#include "detail/Callback.hpp"

#include <functional>

namespace nvcv {

NVCV_IMPL_SHARED_HANDLE(Image);

class Image : public CoreResource<NVCVImageHandle, Image>
{
public:
    using HandleType   = NVCVImageHandle;
    using Base         = CoreResource<NVCVImageHandle, Image>;
    using Requirements = NVCVImageRequirements;

    static Requirements CalcRequirements(const Size2D &size, ImageFormat fmt, const MemAlignment &bufAlign = {});

    NVCV_IMPLEMENT_SHARED_RESOURCE(Image, Base);

    explicit Image(const Requirements &reqs, const Allocator &alloc = nullptr);

    explicit Image(const Size2D &size, ImageFormat fmt, const Allocator &alloc = nullptr,
                   const MemAlignment &bufAlign = {});

    Size2D size() const;

    ImageFormat format() const;

    ImageData exportData() const;

    template<typename DATA>
    Optional<DATA> exportData() const;

    void setUserPointer(void *ptr);

    void *userPointer() const;
};

// ImageWrapData definition -------------------------------------
// Image that wraps an image data allocated outside cv-cuda

using ImageDataCleanupFunc = void(const ImageData &);

struct TranslateImageDataCleanup
{
    template<typename CppCleanup>
    void operator()(CppCleanup &&c, const NVCVImageData *data) const noexcept
    {
        c(ImageData(*data));
    }
};

using ImageDataCleanupCallback
    = CleanupCallback<ImageDataCleanupFunc, detail::RemovePointer_t<NVCVImageDataCleanupFunc>,
                      TranslateImageDataCleanup>;

// For API backward-compatibility
inline Image ImageWrapData(const ImageData &data, ImageDataCleanupCallback &&cleanup = ImageDataCleanupCallback{});

using ImageWrapHandle = NonOwningResource<Image>;

} // namespace nvcv

#include "detail/ImageImpl.hpp"

#endif // NVCV_IMAGE_HPP