DriveWorks SDK Reference
3.5.78 Release
For Test and Development only

image/docs/usecase5.md
Go to the documentation of this file.
1 # Copyright (c) 2019-2020 NVIDIA CORPORATION. All rights reserved.
2 
3 @page image_usecase5 Image Creation and Conversion
4 @tableofcontents
5 
6 @note SW Release Applicability: This tutorial is applicable to modules in both **NVIDIA DriveWorks** and **NVIDIA DRIVE Software** releases.
7 
8 This section demonstrates the different methods to create an @ref image_mainsection.
9 
10 Once an image is created, you can stream it from host (CPU) memory to device (CUDA) memory across:
11 - A [single process and thread](@ref image_usecase2).
12 - [Multiple processes](@ref image_usecase3).
13 - [Multiple threads](@ref image_usecase4).
14 
15 @section image_usecase5_creation Creating an Image
16 
17 @subsection image_usecase5_basic Basic Creation
18 
19 This is the simplest method to create an image.<br>
20 The ownership for the image is transferred to the application, which is responsible for destroying the object when no longer needed.
21 
22 ```{.cpp}
23  dwImageProperties imageProperties{};
24  imageProperties.type = DW_IMAGE_CPU;
25  imageProperties.format = DW_IMAGE_FORMAT_RGB_UINT8_PLANAR;
26  imageProperties.width = 1200;
27  imageProperties.height = 800;
28  dwImageHandle_t image;
29  dwImage_create(&image, imageProperties, ...);
30 
31  // CODE: Use image
32  dwSomeDriveWorksModule_compute(image,...);
33 
34  dwImage_destroy(&image);
35 ```
36 
37 @subsection image_usecase5_buffer Creation with Provided Buffer
38 
39 This method is similar to the Basic Creation. In this case, the application provides the memory buffer used to store the image.
40 
41 ```{.cpp}
42  uint8_t* buffer[3];
43  // CODE: Read some data source into buffer
44  size_t* pitch[3];
45  // CODE: Calculate pitch
46 
47  dwImageProperties imageProperties{};
48  imageProperties.type = DW_IMAGE_CPU;
49  imageProperties.format = DW_IMAGE_FORMAT_RGB_UINT8_PLANAR;
50  imageProperties.width = 1200;
51  imageProperties.height = 800;
52 
53  dwImageHandle_t image;
54  dwImage_createAndBindBuffer(&image, imageProperties, buffer, pitch, ...);
55 
56  // CODE: Use image
57  dwSomeDriveWorksModule_compute(image,...); // NOTE, buffer might be modified here
58 
59  // Destroy the image handle
60  dwImage_destroy(&image);
61 
62  // Note, the user provided buffer is still available for usage
63 ```
64 
65 @section image_usecase5_access_data Accessing Data Explicitly
66 
67 After creating an image through the demonstrated methods above, its content can be accessed in the following way:
68 
69 ```{cpp}
70  dwImageCPU *imageCPU;
71  dwImage_getCPU(&imageCPU, image);
72 
73  size_t planeCount;
74  dwImage_getPlaneCount(&planeCount, DW_IMAGE_FORMAT_RGB_UINT8_PLANAR);
75 
76  for (uint32_t plane = 0; plane < planeCount; ++p) {
77  for (uint32_t i = 0; i < imageProperties.height; ++i) {
78  for (uint32_t j = 0; j < imageProperties.width; ++j) {
79  imageCPU->data[plane][i * imageCPU->pitch[plane] + j] = ...; // this affects the image
80  }
81  }
82  }
83 ```
84 
85 @note This snippet assumes the type of the image created is ::DW_IMAGE_CPU.
86 
87 @section image_usecase5_conversion Converting Format
88 
89 An image's format and properties can be converted in the following way:
90 
91 ```{.cpp}
92  dwImageProperties imageProperties{};
93  imageProperties.type = DW_IMAGE_CUDA;
94  imageProperties.format = DW_IMAGE_FORMAT_RGBA_UINT8;
95  imageProperties.width = 1200;
96  imageProperties.height = 800;
97  dwImageHandle_t outputImage;
98  dwImage_create(&outputImage, imageProperties, ...);
99 
100  // CODE: Get some CUDA image
101  dwImageHandle_t inputImage
102  dwSomeDriveWorksModule_getImage(inputImage,...);
103 
104  dwImage_copyConvert(outputImage, inputImage,...);
105 
106  // CODE: Use outputImage
107  ...
108 
109  // Destroy the outputing image
110  dwImage_destroy(&outputImage);
111 ```
112 
113 @note The input and output images must be of the same type.
114 @note This operation applies to ::DW_IMAGE_CUDA and ::DW_IMAGE_NVMEDIA only.