-
Notifications
You must be signed in to change notification settings - Fork 0
/
vx_training_03.c
142 lines (110 loc) · 3.66 KB
/
vx_training_03.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
/* 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"
#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 void VX_CALLBACK
context_log_callback(vx_context context, vx_reference ref, vx_status status,
const vx_char string[])
{
printf ("vx-training: %s\n", string);
}
int
main (int argc, char *argv[])
{
int ret = -1;
const char *filename = "lena.png";
if (argc >= 2) {
filename = argv[1];
}
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_node node = vxChannelExtractNode(graph, in_image, VX_CHANNEL_Y, out_image);
status = vxGetStatus ((vx_reference)node);
if (VX_SUCCESS != status) {
fprintf (stderr, "vx-training: Unable to create processing node: %d\n", status);
goto free_node;
}
ret = 0;
free_node:
vxReleaseNode (&node);
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;
}