-
Notifications
You must be signed in to change notification settings - Fork 0
/
vx_training_05.c
224 lines (177 loc) · 5.89 KB
/
vx_training_05.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/* Copyright (C) 2022 RidgeRun, LLC (http://www.ridgerun.com)
* All Rights Reserved.
*
* The contents of this software are proprietary and confidential to RidgeRun,
* LLC. No part of this program may be photocopied, reproduced or translated
* into another programming language without prior written consent of
* RidgeRun, LLC. The user is free to modify the source code after obtaining
* a software license from RidgeRun. All source code changes must be provided
* back to RidgeRun without any encumbrance.
*/
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb_image_write.h"
#include <stdio.h>
#include <VX/vx.h>
static int
populate_image (vx_image image, const unsigned char *img_data)
{
int ret = -1;
int width = 0;
int height = 0;
int channels = 3;
vxQueryImage (image, VX_IMAGE_WIDTH, &width, sizeof (width));
vxQueryImage (image, VX_IMAGE_WIDTH, &height, sizeof (height));
vx_imagepatch_addressing_t layout = { width, height, channels,
width*channels, 0, 0, 0, 0 };
const vx_rectangle_t rect = { 0, 0, width, height };
vx_status status = vxCopyImagePatch (image, &rect, 0, &layout, (void *)img_data,
VX_WRITE_ONLY, VX_MEMORY_TYPE_HOST);
if (VX_SUCCESS != status) {
fprintf (stderr, "vx-training: Unable to copy data into image: %d\n", status);
ret = -1;
} else {
ret = 0;
}
return ret;
}
static int
dump_image (vx_image image, const char *path)
{
int ret = -1;
int width = 0;
int height = 0;
vxQueryImage (image, VX_IMAGE_WIDTH, &width, sizeof (width));
vxQueryImage (image, VX_IMAGE_WIDTH, &height, sizeof (height));
const vx_rectangle_t rect = { 0, 0, width, height };
vx_uint32 plane = 0;
vx_map_id map_id = 0;
vx_imagepatch_addressing_t addr = { 0 };
unsigned char *ptr = 0;
vx_enum usage = VX_READ_ONLY;
vx_enum mem_type = VX_MEMORY_TYPE_HOST;
vx_uint32 flags = VX_NOGAP_X;
vx_status status = vxMapImagePatch (image, &rect, plane, &map_id, &addr, (void **)&ptr, usage, mem_type, flags);
if (VX_SUCCESS != status) {
fprintf (stderr, "vx-training: Unable to map image for reading: %d\n", status);
goto out;
}
if (0 == stbi_write_png (path, width, height, addr.stride_x, ptr, addr.stride_y)) {
fprintf (stderr, "vx-training: Unable to write image to %s\n", path);
goto unmap;
}
ret = 0;
unmap:
vxUnmapImagePatch (image, map_id);
out:
return ret;
}
static void VX_CALLBACK
context_log_callback(vx_context context, vx_reference ref, vx_status status,
const vx_char string[])
{
printf ("vx-training [dbg]: %s\n", string);
}
int
main (int argc, char *argv[])
{
int ret = -1;
const char *filename = "lena.png";
if (argc >= 2) {
filename = argv[1];
}
const char *outname = "out.png";
if (argc >= 3) {
outname = argv[2];
}
vx_context context = vxCreateContext ();
vx_status status = vxGetStatus ((vx_reference)context);
if (VX_SUCCESS != status) {
fprintf (stderr, "vx-training: Unable to create context: %d\n", status);;
goto free_context;
}
vx_bool reentrant = vx_false_e;
vxRegisterLogCallback(context, context_log_callback, reentrant);
int width = 0;
int height = 0;
int channels = 0;
unsigned char *img_data = stbi_load (filename, &width, &height, &channels, 3);
if (NULL == img_data) {
fprintf (stderr, "vx-training: Unable to load image \"%s\"\n", filename);
goto free_context;
}
vx_image in_image = vxCreateImage(context, width, height, VX_DF_IMAGE_RGB);
status = vxGetStatus ((vx_reference)in_image);
if (VX_SUCCESS != status) {
fprintf (stderr, "vx-training: Unable to create input image: %d\n", status);
goto free_in_img;
}
if (0 != populate_image (in_image, img_data)) {
fprintf (stderr, "vx-training: Unable to populate image\n");
goto free_in_img;
}
vx_image out_image = vxCreateImage(context, width, height, VX_DF_IMAGE_U8);
status = vxGetStatus ((vx_reference)out_image);
if (VX_SUCCESS != status) {
fprintf (stderr, "vx-training: Unable to create output image: %d\n", status);
goto free_out_img;
}
vx_graph graph = vxCreateGraph (context);
status = vxGetStatus ((vx_reference)graph);
if (VX_SUCCESS != status) {
fprintf (stderr, "vx-training: Unable to create graph: %d\n", status);
goto free_graph;
}
vx_image intermediate = vxCreateVirtualImage(graph, width, height, VX_DF_IMAGE_U8);
status = vxGetStatus ((vx_reference)intermediate);
if (VX_SUCCESS != status) {
fprintf (stderr, "vx-training: Unable to create virtual image: %d\n", status);
goto free_intermediate;
}
vx_node nodes[] = {
vxChannelExtractNode (graph, in_image, VX_CHANNEL_R, intermediate),
vxGaussian3x3Node (graph, intermediate, out_image),
};
for (int i = 0; i < sizeof (nodes)/sizeof(vx_node); i++) {
status = vxGetStatus ((vx_reference)nodes[i]);
if (VX_SUCCESS != status) {
fprintf (stderr, "vx-training: Unable to create processing node[%d]: %d\n", i, status);
goto free_node;
}
}
status = vxVerifyGraph (graph);
if (VX_SUCCESS != status) {
fprintf (stderr, "vx-training: Graph validation failed: %d\n", status);
goto free_node;
}
status = vxProcessGraph (graph);
if (VX_SUCCESS != status) {
fprintf (stderr, "vx-training: Error processing the graph: %d\n", status);
goto free_node;
}
if (0 != dump_image (out_image, outname)) {
fprintf (stderr, "vx-training: Error writing output image to \"%s\"\n", outname);
goto free_node;
}
dump_image (in_image, "test.png");
ret = 0;
free_node:
for (int i = 0; i < sizeof (nodes)/sizeof(vx_node); i++) {
vxReleaseNode (&nodes[i]);
}
free_intermediate:
vxReleaseImage (&intermediate);
free_graph:
vxReleaseGraph (&graph);
free_out_img:
vxReleaseImage (&out_image);
free_in_img:
vxReleaseImage (&in_image);
free_img_data:
free (img_data);
free_context:
vxReleaseContext (&context);
out:
return ret;
}