TIOVX User Guide
vx_tutorial_image_histogram.c
Go to the documentation of this file.
1 /*
2 *
3 * Copyright (c) 2017 Texas Instruments Incorporated
4 *
5 * All rights reserved not granted herein.
6 *
7 * Limited License.
8 *
9 * Texas Instruments Incorporated grants a world-wide, royalty-free, non-exclusive
10 * license under copyrights and patents it now or hereafter owns or controls to make,
11 * have made, use, import, offer to sell and sell ("Utilize") this software subject to the
12 * terms herein. With respect to the foregoing patent license, such license is granted
13 * solely to the extent that any such patent is necessary to Utilize the software alone.
14 * The patent license shall not apply to any combinations which include this software,
15 * other than combinations with devices manufactured by or for TI ("TI Devices").
16 * No hardware patent is licensed hereunder.
17 *
18 * Redistributions must preserve existing copyright notices and reproduce this license
19 * (including the above copyright notice and the disclaimer and (if applicable) source
20 * code license limitations below) in the documentation and/or other materials provided
21 * with the distribution
22 *
23 * Redistribution and use in binary form, without modification, are permitted provided
24 * that the following conditions are met:
25 *
26 * * No reverse engineering, decompilation, or disassembly of this software is
27 * permitted with respect to any software provided in binary form.
28 *
29 * * any redistribution and use are licensed by TI for use only with TI Devices.
30 *
31 * * Nothing shall obligate TI to provide you with source code for the software
32 * licensed and provided to you in object code.
33 *
34 * If software source code is provided to you, modification and redistribution of the
35 * source code are permitted provided that the following conditions are met:
36 *
37 * * any redistribution and use of the source code, including any resulting derivative
38 * works, are licensed by TI for use only with TI Devices.
39 *
40 * * any redistribution and use of any object code compiled from the source code
41 * and any resulting derivative works, are licensed by TI for use only with TI Devices.
42 *
43 * Neither the name of Texas Instruments Incorporated nor the names of its suppliers
44 *
45 * may be used to endorse or promote products derived from this software without
46 * specific prior written permission.
47 *
48 * DISCLAIMER.
49 *
50 * THIS SOFTWARE IS PROVIDED BY TI AND TI'S LICENSORS "AS IS" AND ANY EXPRESS
51 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
52 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
53 * IN NO EVENT SHALL TI AND TI'S LICENSORS BE LIABLE FOR ANY DIRECT, INDIRECT,
54 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
55 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
56 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
57 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
58 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
59 * OF THE POSSIBILITY OF SUCH DAMAGE.
60 *
61 */
62 
63 
64 
109 #include <stdio.h>
110 #include <VX/vx.h>
111 #include <TI/tivx.h>
112 #include <utility.h>
113 
115 #define IN_FILE_NAME "${VX_TEST_DATA_PATH}/colors.bmp"
116 
118 #define OUT_FILE_NAME "${VX_TEST_DATA_PATH}/vx_tutorial_image_histogram_out.bmp"
119 
120 
122  uint32_t width, uint32_t height);
123 
128 {
136  vx_context context;
137  vx_image in_image = NULL, out_image = NULL;
138  vx_distribution histogram = NULL;
140  uint32_t num_bins = 256;
141  uint32_t histogram_image_width = 256;
142  uint32_t histogram_image_height = 128;
143  vx_node node0 = NULL;
144  vx_graph graph = NULL;
145  vx_status status;
146 
147  printf(" vx_tutorial_image_histogram: Tutorial Started !!! \n");
148 
156  context = vxCreateContext();
159  printf(" Loading file %s ...\n", IN_FILE_NAME);
160 
166  graph = vxCreateGraph(context);
168  vxSetReferenceName((vx_reference)graph, "MY_GRAPH");
169 
180  vxSetReferenceName((vx_reference)in_image, "INPUT");
188  show_image_attributes(in_image);
197  histogram = vxCreateDistribution(context, num_bins, 0, 256);
199  vxSetReferenceName((vx_reference)histogram, "HISTOGRAM");
200 
201  {
202  vx_reference refs[] = {(vx_reference)in_image, (vx_reference)histogram};
203 
209  node0 = tivxCreateNodeByKernelEnum(graph,
211  refs, sizeof(refs)/sizeof(refs[0])
212  );
214  vxSetReferenceName((vx_reference)node0, "HISTOGRAM");
215  }
216 
224  status = vxVerifyGraph(graph);
230  tivxExportGraphToDot(graph, ".", "vx_tutorial_image_histogram");
240  show_graph_attributes(graph);
249  show_node_attributes(node0);
252  if(status==(vx_status)VX_SUCCESS)
253  {
254  printf(" Executing graph ...\n");
255 
264  vxScheduleGraph(graph);
273  vxWaitGraph(graph);
276  printf(" Executing graph ... Done !!!\n");
277 
285  show_graph_attributes(graph);
294  show_node_attributes(node0);
297  out_image = convert_distribution_to_image(histogram,
298  histogram_image_width,
299  histogram_image_height);
300 
301  vxSetReferenceName((vx_reference)out_image, "HISTOGRAM_IMAGE");
309  show_image_attributes(out_image);
312  printf(" Saving to file %s ...\n", OUT_FILE_NAME);
330  vxReleaseImage(&out_image);
332  }
333 
340  vxReleaseImage(&in_image);
349  vxReleaseDistribution(&histogram);
358  vxReleaseNode(&node0);
367  vxReleaseGraph(&graph);
378  vxReleaseContext(&context);
381  printf(" vx_tutorial_image_histogram: Tutorial Done !!! \n");
382  printf(" \n");
383 }
384 
385 #define MAX_BINS (256u)
386 
397  uint32_t width, uint32_t height)
398 {
399  vx_image image;
400  vx_context context;
401  vx_status status;
402 
403  context = vxGetContext((vx_reference)distribution);
404 
412  image = vxCreateImage(context, width, height, (vx_df_image)VX_DF_IMAGE_U8);
416  {
417  vx_size num_bins;
418 
425  status = vxQueryDistribution(distribution, (vx_enum)VX_DISTRIBUTION_BINS, &num_bins, sizeof(vx_size));
428  if(status == (vx_status)VX_SUCCESS && num_bins <= MAX_BINS)
429  {
430  uint32_t histogram_data[MAX_BINS] = {0};
431  uint32_t i, max;
432 
439  vxCopyDistribution(distribution,
440  (void**)&histogram_data[0],
445  /* normalize bins */
446  max = 0;
447  for(i=0; i<num_bins; i++)
448  {
449  if(histogram_data[i] > max )
450  max = histogram_data[i];
451  }
452  /* scale to image height */
453  for(i=0; i<num_bins; i++)
454  {
455  histogram_data[i] = (uint32_t)(((float)histogram_data[i]/max)*height + 0.5);
456  if(histogram_data[i]>height)
457  histogram_data[i] = height;
458  }
459 
460  {
461  uint8_t *data_ptr = NULL;
462  vx_rectangle_t rect = { 0, 0, width, height};
463  vx_map_id map_id;
464  vx_imagepatch_addressing_t image_addr;
465 
472  status = vxMapImagePatch(image,
473  &rect,
474  0,
475  &map_id,
476  &image_addr,
477  (void**)&data_ptr,
482  if(status==(vx_status)VX_SUCCESS && data_ptr!=NULL)
483  {
484  uint32_t i, j, k, bin_width;
485  uint8_t *addr0 = NULL, *addr1 = NULL;
486 
487  bin_width = width/num_bins;
488 
489  for(i=0; i<num_bins; i++)
490  {
491  addr0 = data_ptr + i*bin_width;
492 
493  for(j=0; j<height-histogram_data[i]; j++)
494  {
495  addr1 = addr0 + j*image_addr.stride_y;
496  for(k=0; k<bin_width; k++)
497  {
498  addr1[k] = 0xFF;
499  }
500  }
501 
502  addr0 = data_ptr + j*image_addr.stride_y + i*bin_width;
503 
504  for(j=0; j<histogram_data[i]; j++)
505  {
506  addr1 = addr0 + j*image_addr.stride_y;
507  for(k=0; k<bin_width; k++)
508  {
509  addr1[k] = 0x0;
510  }
511  }
512  }
517  vxUnmapImagePatch(image, map_id);
519  }
520  }
521  }
522  else
523  {
524  /* more bins than allocated memory */
531  vxReleaseImage(&image);
533  }
534  }
535 
536  return image;
537 }
struct _vx_image * vx_image
vx_bool
#define IN_FILE_NAME
Input file name.
VX_MEMORY_TYPE_HOST
vx_status VX_API_CALL vxReleaseDistribution(vx_distribution *distribution)
Interface to TI extension APIs.
size_t vx_size
void show_node_attributes(vx_node node)
Show attributes of previously created node.
int32_t vx_enum
struct _vx_distribution * vx_distribution
VX_SUCCESS
vx_status VX_API_CALL vxUnmapImagePatch(vx_image image, vx_map_id map_id)
void show_graph_attributes(vx_graph graph)
Show attributes of previously created graph.
vx_true_e
#define OUT_FILE_NAME
Output file name.
VX_DF_IMAGE_U8
vx_status VX_API_CALL vxReleaseContext(vx_context *context)
void vx_tutorial_image_histogram()
Tutorial Entry Point.
vx_enum vx_status
struct _vx_context * vx_context
struct _vx_reference * vx_reference
uint32_t vx_df_image
VX_WRITE_ONLY
vx_status VX_API_CALL vxQueryDistribution(vx_distribution distribution, vx_enum attribute, void *ptr, vx_size size)
vx_node tivxCreateNodeByKernelEnum(vx_graph graph, vx_enum kernelenum, vx_reference params[], vx_uint32 num)
Utility function to create a node given parameter references and kernel enum.
VX_READ_ONLY
vx_image VX_API_CALL vxCreateImage(vx_context context, vx_uint32 width, vx_uint32 height, vx_df_image color)
vx_status VX_API_CALL vxCopyDistribution(vx_distribution distribution, void *user_ptr, vx_enum usage, vx_enum user_mem_type)
VX_NOGAP_X
vx_status VX_API_CALL vxMapImagePatch(vx_image image, const vx_rectangle_t *rect, vx_uint32 plane_index, vx_map_id *map_id, vx_imagepatch_addressing_t *addr, void **ptr, vx_enum usage, vx_enum mem_type, vx_uint32 flags)
vx_status VX_API_CALL vxWaitGraph(vx_graph graph)
vx_status VX_API_CALL vxReleaseGraph(vx_graph *graph)
vx_status VX_API_CALL vxSetReferenceName(vx_reference ref, const vx_char *name)
VX_DISTRIBUTION_BINS
VX_KERNEL_HISTOGRAM
vx_distribution VX_API_CALL vxCreateDistribution(vx_context context, vx_size numBins, vx_int32 offset, vx_uint32 range)
void show_image_attributes(vx_image image)
Show attributes of previously created image.
vx_image tivx_utils_create_vximage_from_bmpfile(vx_context context, const char *filename, vx_bool convert_to_gray_scale)
Create a image data object given BMP filename as input.
vx_graph VX_API_CALL vxCreateGraph(vx_context context)
vx_status VX_API_CALL vxVerifyGraph(vx_graph graph)
vx_status VX_API_CALL vxScheduleGraph(vx_graph graph)
vx_context VX_API_CALL vxCreateContext()
struct _vx_graph * vx_graph
vx_status VX_API_CALL tivxExportGraphToDot(vx_graph graph, const char *output_file_path, const char *output_file_prefix)
Export graph representation as DOT graph file.
vx_status VX_API_CALL vxReleaseNode(vx_node *node)
vx_status tivx_utils_save_vximage_to_bmpfile(const char *filename, vx_image image)
Save data from image object to PNG file.
struct _vx_node * vx_node
vx_status VX_API_CALL vxReleaseImage(vx_image *image)
vx_context VX_API_CALL vxGetContext(vx_reference reference)
vx_image convert_distribution_to_image(vx_distribution distribution, uint32_t width, uint32_t height)
Convert distribution given as input to image.
uintptr_t vx_map_id
vx_status VX_API_CALL vxGetStatus(vx_reference reference)