Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add missing nvJPEG header in cuda preset #1195

Merged
merged 4 commits into from
Jun 30, 2022
Merged

Add missing nvJPEG header in cuda preset #1195

merged 4 commits into from
Jun 30, 2022

Conversation

devjeonghwan
Copy link
Member

import org.bytedeco.cuda.nvjpeg.*;
import org.bytedeco.javacpp.Pointer;
import org.bytedeco.javacpp.PointerPointer;

import static org.bytedeco.cuda.global.cudart.*;
import static org.bytedeco.cuda.global.nvjpeg.*;

public class SampleJpeg {
    static class dev_malloc extends tDevMalloc {
        final static dev_malloc instance = new dev_malloc().retainReference();

        @Override
        public int call(PointerPointer pointerPointer, long l) {
            return cudaMalloc(pointerPointer, l);
        }
    }

    static class dev_free extends tDevFree {
        final static dev_free instance = new dev_free().retainReference();

        @Override
        public int call(Pointer pointer) {
            return cudaFree(pointer);
        }
    }

    static class host_malloc extends tPinnedMalloc {
        final static host_malloc instance = new host_malloc().retainReference();

        @Override
        public int call(PointerPointer pointerPointer, long l, int i) {
            return cudaHostAlloc(pointerPointer, l, i);
        }
    }

    static class host_free extends tPinnedFree {
        final static host_free instance = new host_free().retainReference();

        @Override
        public int call(Pointer pointer) {
            return cudaFreeHost(pointer);
        }
    }

    public static void CHECK_NVJPEG(String functionName, int result) {
        if (result != NVJPEG_STATUS_SUCCESS) {
            throw new IllegalStateException(String.format("%s returned '%d'", functionName, result));
        }
    }

    public static void main(String[] args) {
        nvjpegDevAllocator_t devAllocator = new nvjpegDevAllocator_t();
        devAllocator.dev_malloc(dev_malloc.instance);
        devAllocator.dev_free(dev_free.instance);

        nvjpegPinnedAllocator_t pinnedAllocator = new nvjpegPinnedAllocator_t();
        pinnedAllocator.pinned_malloc(host_malloc.instance);
        pinnedAllocator.pinned_free(host_free.instance);

        nvjpegHandle handle = new nvjpegHandle();
        nvjpegJpegState state = new nvjpegJpegState();
        nvjpegJpegDecoder decoder = new nvjpegJpegDecoder();
        nvjpegBufferDevice bufferDevice = new nvjpegBufferDevice();
        nvjpegDecodeParams params = new nvjpegDecodeParams();

        nvjpegBufferPinned[] pinned_buffers = new nvjpegBufferPinned[]{new nvjpegBufferPinned(), new nvjpegBufferPinned()};

        nvjpegJpegStream[] streams = new nvjpegJpegStream[]{new nvjpegJpegStream(), new nvjpegJpegStream()};

        // Create Components
        CHECK_NVJPEG("nvjpegCreateEx", nvjpegCreateEx(NVJPEG_BACKEND_DEFAULT, devAllocator, pinnedAllocator, NVJPEG_FLAGS_DEFAULT, handle));
        CHECK_NVJPEG("nvjpegJpegStateCreate", nvjpegJpegStateCreate(handle, state));

        CHECK_NVJPEG("nvjpegDecoderCreate", nvjpegDecoderCreate(handle, NVJPEG_BACKEND_DEFAULT, decoder));
        CHECK_NVJPEG("nvjpegDecoderStateCreate", nvjpegDecoderStateCreate(handle, decoder, state));

        CHECK_NVJPEG("nvjpegBufferPinnedCreate", nvjpegBufferPinnedCreate(handle, null, pinned_buffers[0]));
        CHECK_NVJPEG("nvjpegBufferPinnedCreate", nvjpegBufferPinnedCreate(handle, null, pinned_buffers[1]));
        CHECK_NVJPEG("nvjpegBufferDeviceCreate", nvjpegBufferDeviceCreate(handle, null, bufferDevice));

        CHECK_NVJPEG("nvjpegJpegStreamCreate", nvjpegJpegStreamCreate(handle, streams[0]));
        CHECK_NVJPEG("nvjpegJpegStreamCreate", nvjpegJpegStreamCreate(handle, streams[1]));

        CHECK_NVJPEG("nvjpegDecodeParamsCreate", nvjpegDecodeParamsCreate(handle, params));

        // Destroy Components
        CHECK_NVJPEG("nvjpegDecodeParamsDestroy", nvjpegDecodeParamsDestroy(params));

        CHECK_NVJPEG("nvjpegJpegStreamDestroy", nvjpegJpegStreamDestroy(streams[0]));
        CHECK_NVJPEG("nvjpegJpegStreamDestroy", nvjpegJpegStreamDestroy(streams[1]));

        CHECK_NVJPEG("nvjpegBufferPinnedDestroy", nvjpegBufferPinnedDestroy(pinned_buffers[0]));
        CHECK_NVJPEG("nvjpegBufferPinnedDestroy", nvjpegBufferPinnedDestroy(pinned_buffers[1]));
        CHECK_NVJPEG("nvjpegBufferDeviceDestroy", nvjpegBufferDeviceDestroy(bufferDevice));

        CHECK_NVJPEG("nvjpegJpegStateDestroy", nvjpegJpegStateDestroy(state));
        CHECK_NVJPEG("nvjpegDecoderDestroy", nvjpegDecoderDestroy(decoder));
    }
}

Fixed #1193

@devjeonghwan devjeonghwan changed the title Add nvJPEG in cuda presets Add missing nvJPEG header in cuda presets Jun 28, 2022
@devjeonghwan devjeonghwan changed the title Add missing nvJPEG header in cuda presets Add missing nvJPEG header in cuda preset Jun 28, 2022
@saudet
Copy link
Member

saudet commented Jun 29, 2022

Since you took the time to create it, can you put that sample code under the samples subdirectory?

@devjeonghwan
Copy link
Member Author

devjeonghwan commented Jun 29, 2022

@saudet Yeap, i did it

@saudet saudet merged commit 12bb414 into bytedeco:master Jun 30, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Question: Is nvjpeg supported
2 participants