TIOVX User Guide
vx_tutorial_image_load_save.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 
91 #include <stdio.h>
92 #include <stdlib.h>
93 #include <string.h>
94 #include <stdint.h>
95 #include <VX/vx.h>
96 #include <utility.h>
97 
98 /* in some systems file IO is not present, so skip it using below flag */
99 /* #define SKIP_FILEIO */
100 
102 #define IN_FILE_NAME "${VX_TEST_DATA_PATH}/colors.bmp"
103 
105 #define OUT_FILE_NAME "${VX_TEST_DATA_PATH}/vx_tutorial_image_load_save_out.bmp"
106 
111 {
119  vx_context context;
120  vx_image image;
122  vx_uint32 width, height;
123 
124  printf(" vx_tutorial_image_load_save: Tutorial Started !!! \n");
125 
133  context = vxCreateContext();
136  printf(" Loading file %s ...\n", IN_FILE_NAME);
137 
154  vxQueryImage(image, (vx_enum)VX_IMAGE_WIDTH, &width, sizeof(vx_uint32));
155  vxQueryImage(image, (vx_enum)VX_IMAGE_HEIGHT, &height, sizeof(vx_uint32));
158  printf(" Loaded image of dimensions %d x %d\n", width, height);
159 
160  printf(" Saving to file %s ...\n", OUT_FILE_NAME);
161 
179  vxReleaseImage(&image);
190  vxReleaseContext(&context);
193  printf(" vx_tutorial_image_load_save: Tutorial Done !!! \n");
194  printf(" \n");
195 }
196 
197 void ascii_file_read(char *filename, int num_elements, void* buffer, vx_enum data_type){
198  FILE* ptr_file;
199 
200  // Initialize buffer for reading each line of file
201  int nbytes = sizeof(float);
202  char* buff = malloc(nbytes);
203 
204  if (NULL == buff)
205  {
206  printf("FAILED ALLOCATING MEMORY\n");
207  return;
208  }
209 
210  // Initialize variables for tokenizing each line based on delimiter values
211  char* token;
212  char* delim = " \n\t,";
213  float val;
214 
215  // Initialize different data type arrays
216  vx_uint8* u8;
217  vx_int32* i32;
218  vx_float32* f32;
219  uint8_t val_u8;
220  int32_t val_int32;
221 
222  // Try reading in file
223  ptr_file = fopen(filename, "r");
224  if(!ptr_file){
225  printf("FAILED READING FILE\n");
226  free(buff);
227  return;
228  }
229 
230  // If file read is successful, try to populate matrix
231  int i = 0;
232  while(fgets(buff, nbytes, ptr_file) > 0){
233  // Split string into tokens at whitespaces, commas, newlines, or tabs
234  for(token = strtok(buff, delim); token != NULL; token = strtok(NULL, delim)){
235  val = strtod(token, NULL);
236 
237  switch(data_type){
238  case (vx_enum)VX_TYPE_FLOAT32:
239  f32 = buffer;
240  f32[i] = val;
241  break;
242 
243  case (vx_enum)VX_TYPE_UINT8:
244  u8 = buffer;
245  val_u8 = val;
246  u8[i] = val_u8;
247  break;
248 
249  case (vx_enum)VX_TYPE_INT32:
250  i32 = buffer;
251  val_int32 = val;
252  i32[i] = val_int32;
253  break;
254 
255  default:
256  break;
257  }
258 
259  i++;
260  if (i >= num_elements){
261  break;
262  }
263  }
264  // Need to break out of both loops when this condition is met
265  if (i >= num_elements){
266  break;
267  }
268  }
269  fclose(ptr_file);
270  free(buff);
271  //printf("val of first element: %d\n", u8[0]);
272 }
273 
274 vx_matrix create_matrix_from_file(vx_context context, vx_enum data_type, int cols, int rows, char *filename){
275  // Create vx_float32 buffer object
276  vx_float32 mat[rows*cols];
277 
278  // Call generic function that will populate data object
279  ascii_file_read(filename, rows*cols, mat, data_type);
280 
281  // Create vx_matrix object
282  vx_matrix matrix = vxCreateMatrix(context, data_type, cols, rows);
283 
284  // Set pointer reference between vx_float32 data object and
286 
287  return matrix;
288 }
289 
290 vx_status save_pyramid_to_file(char *filename, vx_pyramid pyr, vx_size levels){
291  vx_status status = (vx_status)VX_SUCCESS;
292  vx_uint32 index;
293  vx_image image;
294 
295  printf("Saving pyramid object...\n");
296  for (index=0; index<levels; index++){
297  // Create buffer for filename
298  char img_filename[1000] = "";
299  strcat(img_filename, filename);
300 
301  // Create new filename for each level
302  char img_index[16] = "";
303  sprintf(img_index, "%d.bmp", index);
304  strcat(img_filename, img_index);
305  printf("New filename: %s", img_filename);
306 
307  // Save each level
308  image = vxGetPyramidLevel(pyr, index);
309  status = tivx_utils_save_vximage_to_bmpfile(img_filename, image);
310  status = vxReleaseImage(&image);
311  }
312 
313  return status;
314 }
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)
uint8_t vx_uint8
VX_MEMORY_TYPE_HOST
size_t vx_size
vx_status VX_API_CALL vxCopyMatrix(vx_matrix matrix, void *user_ptr, vx_enum usage, vx_enum user_mem_type)
int32_t vx_enum
VX_SUCCESS
int32_t vx_int32
float vx_float32
vx_status VX_API_CALL vxReleaseContext(vx_context *context)
VX_TYPE_FLOAT32
vx_enum vx_status
struct _vx_context * vx_context
#define OUT_FILE_NAME
Output file name.
VX_IMAGE_WIDTH
VX_WRITE_ONLY
struct _vx_pyramid * vx_pyramid
void vx_tutorial_image_load_save()
Tutorial Entry Point.
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.
uint32_t vx_uint32
#define IN_FILE_NAME
Input file name.
vx_context VX_API_CALL vxCreateContext()
vx_status tivx_utils_save_vximage_to_bmpfile(const char *filename, vx_image image)
Save data from image object to PNG file.
VX_TYPE_INT32
vx_status VX_API_CALL vxReleaseImage(vx_image *image)
vx_image VX_API_CALL vxGetPyramidLevel(vx_pyramid pyr, vx_uint32 index)
struct _vx_matrix * vx_matrix
VX_IMAGE_HEIGHT
VX_TYPE_UINT8
vx_matrix VX_API_CALL vxCreateMatrix(vx_context c, vx_enum data_type, vx_size columns, vx_size rows)