TIOVX User Guide
vx_tutorial_image_crop_roi.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 
105 #include <stdio.h>
106 #include <VX/vx.h>
107 #include <utility.h>
108 
110 #define IN_FILE_NAME "${VX_TEST_DATA_PATH}/colors.bmp"
111 
113 #define OUT_FILE_NAME "${VX_TEST_DATA_PATH}/vx_tutorial_image_crop_roi.bmp"
114 
116  vx_context context,
117  char *filename,
118  vx_bool convert_to_gray_scale,
119  tivx_utils_bmp_image_params_t *imgParams);
120 
125 {
133  vx_context context;
134  vx_image image, roi_image;
135  vx_uint32 width, height;
136  vx_rectangle_t rect;
137  tivx_utils_bmp_image_params_t imgParams;
140  printf(" vx_tutorial_image_crop_roi: Tutorial Started !!! \n");
141 
149  context = vxCreateContext();
152  printf(" Loading file %s ...\n", IN_FILE_NAME);
153 
154  image = load_image_from_handle_from_file(context, IN_FILE_NAME, (vx_bool)vx_false_e, &imgParams);
155 
156  vxSetReferenceName((vx_reference)image, "ORIGINAL");
157 
165  show_image_attributes(image);
168  vxQueryImage(image, (vx_enum)VX_IMAGE_WIDTH, &width, sizeof(vx_uint32));
169  vxQueryImage(image, (vx_enum)VX_IMAGE_HEIGHT, &height, sizeof(vx_uint32));
170 
171  rect.start_x = width/4;
172  rect.start_y = height/4;
173  rect.end_x = rect.start_x + width/2;
174  rect.end_y = rect.start_y + height/2;
175 
182  roi_image = vxCreateImageFromROI(image, &rect);
183 
184  vxSetReferenceName((vx_reference)roi_image, "CROP_ROI");
185 
193  show_image_attributes(roi_image);
196  printf(" Saving to file %s ...\n", OUT_FILE_NAME);
197 
215  vxReleaseImage(&roi_image);
216  vxReleaseImage(&image);
219  /* now it is safe to release the memory passed to the image
220  * NOTE: "roi_image" also holds a reference to the memory since
221  * its created from the "image"
222  * Hence memory passed to "image" should be freed after "roi_image"
223  * is released
224  */
225  tivx_utils_bmp_read_release(&imgParams);
226 
235  vxReleaseContext(&context);
238  printf(" vx_tutorial_image_crop_roi: Tutorial Done !!! \n");
239  printf(" \n");
240 }
241 
254  vx_context context,
255  char *filename,
256  vx_bool convert_to_gray_scale,
257  tivx_utils_bmp_image_params_t *imgParams)
258 {
259  char outFilePath[TIOVX_UTILS_MAXPATHLENGTH];
260  vx_image image = NULL;
261  uint32_t width, height, stride;
262  vx_df_image df;
263  vx_status vxStatus;
264  int32_t status;
265  void *data_ptr;
266  int32_t dcn = (convert_to_gray_scale != (vx_bool)(vx_bool)vx_false_e) ? 1 : -1;
267 
268  status = tivx_utils_expand_file_path(filename, outFilePath);
269 
270  status = tivx_utils_bmp_read(outFilePath, dcn, imgParams);
271 
272  if (status == 0)
273  {
274  vxStatus = (vx_status)VX_SUCCESS;
275  width = imgParams->width;
276  height = imgParams->height;
277  stride = imgParams->stride_y;
278  df = imgParams->format;
279  data_ptr = imgParams->data;
280  }
281  else
282  {
283  vxStatus = (vx_status)VX_FAILURE;
284  }
285 
286  if(vxStatus == (vx_status)VX_SUCCESS)
287  {
288  vx_imagepatch_addressing_t image_addr[1];
289  void *ptrs[1];
290  uint32_t bpp;
291 
292  if( df == (vx_df_image)VX_DF_IMAGE_U8 )
293  bpp = 1;
294  else
295  if( df == (vx_df_image)VX_DF_IMAGE_RGB )
296  bpp = 3;
297  else
298  if( df == (vx_df_image)VX_DF_IMAGE_RGBX )
299  bpp = 4;
300  else
301  bpp = 1; /* it should not reach here for BMP files */
302 
303  image_addr[0].dim_x = width;
304  image_addr[0].dim_y = height;
305  image_addr[0].stride_x = bpp;
306  image_addr[0].stride_y = stride;
307  image_addr[0].scale_x = VX_SCALE_UNITY;
308  image_addr[0].scale_y = VX_SCALE_UNITY;
309  image_addr[0].step_x = 1;
310  image_addr[0].step_y = 1;
311 
312  ptrs[0] = data_ptr;
313 
320  image = vxCreateImageFromHandle(context, df, image_addr, ptrs, (vx_enum)VX_MEMORY_TYPE_HOST);
323  /* MUST not free memory that is passed to vxCreateImageFromHandle, until
324  * that memory ptr is swapped out from the image
325  * or the image handle is released
326  */
327  /* bmp_file_read_release(bmp_file_context); */
328  }
329  return image;
330 }
331 
struct _vx_image * vx_image
vx_bool
vx_false_e
vx_status VX_API_CALL vxQueryImage(vx_image image, vx_enum attribute, void *ptr, vx_size size)
VX_MEMORY_TYPE_HOST
#define VX_SCALE_UNITY
int32_t vx_enum
VX_SUCCESS
#define IN_FILE_NAME
Input file name.
VX_DF_IMAGE_U8
vx_status VX_API_CALL vxReleaseContext(vx_context *context)
vx_enum vx_status
struct _vx_context * vx_context
vx_uint32 start_x
struct _vx_reference * vx_reference
uint32_t vx_df_image
VX_DF_IMAGE_RGB
VX_IMAGE_WIDTH
VX_DF_IMAGE_RGBX
vx_image VX_API_CALL vxCreateImageFromHandle(vx_context context, vx_df_image color, const vx_imagepatch_addressing_t addrs[], void *const ptrs[], vx_enum memory_type)
vx_status VX_API_CALL vxSetReferenceName(vx_reference ref, const vx_char *name)
vx_uint32 start_y
vx_uint32 end_x
VX_FAILURE
vx_image load_image_from_handle_from_file(vx_context context, char *filename, vx_bool convert_to_gray_scale, tivx_utils_bmp_image_params_t *imgParams)
Load image from handle from file.
void show_image_attributes(vx_image image)
Show attributes of previously created image.
#define OUT_FILE_NAME
Output file name.
uint32_t vx_uint32
vx_context VX_API_CALL vxCreateContext()
void vx_tutorial_image_crop_roi()
Tutorial Entry Point.
vx_image VX_API_CALL vxCreateImageFromROI(vx_image img, const vx_rectangle_t *rect)
vx_status tivx_utils_save_vximage_to_bmpfile(const char *filename, vx_image image)
Save data from image object to PNG file.
vx_status VX_API_CALL vxReleaseImage(vx_image *image)
vx_uint32 end_y
VX_IMAGE_HEIGHT