tofi_util.c
Go to the documentation of this file.
1 /*
2  * BSD 3-Clause License
3  *
4  * Copyright (c) 2019, Analog Devices, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright notice, this
11  * list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright notice,
14  * this list of conditions and the following disclaimer in the documentation
15  * and/or other materials provided with the distribution.
16  *
17  * 3. Neither the name of the copyright holder nor the names of its
18  * contributors may be used to endorse or promote products derived from
19  * this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #include "tofi_util.h"
34 
35 #include <math.h>
36 
37 #include "string.h"
38 
39 #define MAX_PATH_SIZE 512
40 #ifdef _WIN32
41 #include <Windows.h>
42 #elif __unix
43 #include <unistd.h>
44 #endif
45 
46 #ifdef HEXAGON
47 #ifndef ION_HEAP_ID_SYSTEM
48 #define ION_HEAP_ID_SYSTEM 25
49 #endif
50 #define rpcmem_alloc(a, b, c) memalign(VLEN * 2, (c))
51 #endif
52 
53 uint32_t TransformationXyzToZ(uint32_t n_rows, uint32_t n_cols,
54  const Point3I *p_xyz_image_data,
55  uint16_t *p_zdepth_image_data) {
56  if (p_xyz_image_data == NULL || p_zdepth_image_data == NULL ||
57  n_rows == 0 || n_cols == 0)
59 
60  for (int i = 0; i < n_cols * n_rows; i++) {
61  p_zdepth_image_data[i] = p_xyz_image_data[i].c;
62  }
63  return ADI_TOFI_SUCCESS;
64 }
65 
66 uint32_t GetDataFileSize(char *file_name) {
67  uint32_t size = 0;
68  FILE *fp = fopen(file_name, "rb");
69  if (fp != NULL) {
70  fseek(fp, 0, SEEK_END); // seek to end of file
71  size = ftell(fp); // get current file pointer
72  rewind(fp); // seek back to beginning of file
73  fclose(fp);
74  }
75  return size;
76 }
77 
78 FileData LoadFileContents(char *filename) {
79  const unsigned char *p = NULL;
80  size_t size = 0;
81  FILE *f = fopen(filename, "rb");
82 
83  if (f) {
84  if (0 == fseek(f, 0L, SEEK_END)) {
85  size = ftell(f);
86  rewind(f);
87 #ifdef HEXAGON
88  p = (unsigned char *)rpcmem_alloc(ION_HEAP_ID_SYSTEM,
89  RPCMEM_DEFAULT_FLAGS, size);
90 #else
91  p = (unsigned char *)malloc(size);
92 #endif
93  if (p) {
94  if (1 != fread((void *)p, size, 1, f) || size == 0) {
95  fprintf(stderr, "Failed to read data file %s.\n", filename);
96  free((void *)p);
97  p = 0;
98  size = 0;
99  }
100  } else {
101  fprintf(stderr,
102  "Failed to allocate memory for reading data file %s.\n",
103  filename);
104  }
105  } else {
106  fprintf(stderr, "Failed to seek in data file %s.\n", filename);
107  }
108  fclose(f);
109  } else {
110  fprintf(stderr, "Failed to open data file %s.\n", filename);
111  }
112  FileData ret_val = {(unsigned char *)p, size};
113  return (ret_val);
114 }
115 
116 uint32_t WriteDataToFile(char *file_name, uint8_t *buffer,
117  uint32_t buffer_size) {
118  uint32_t status = ADI_TOFI_ERROR;
119 
120  FILE *fp = fopen(file_name, "wb");
121 
122  if (fp != NULL) {
123  size_t n_bytes_written =
124  fwrite(buffer, sizeof(uint8_t), buffer_size, fp);
125  fclose(fp);
126 
127  if (n_bytes_written == buffer_size) {
128  status = ADI_TOFI_SUCCESS;
129  } else {
130  status = ADI_TOFI_FILE_WRITE;
131  }
132  } else
133  status = ADI_TOFI_OPEN_FILE;
134 
135  return status;
136 }
137 
138 uint32_t GetProcessPath(char *process_path, uint32_t path_size) {
139 #ifdef _WIN32
140  uint32_t status = 0;
141  char path[MAX_PATH_SIZE];
142  status = GetModuleFileName(NULL, path, path_size);
143  if (status == 0 || status == path_size)
144  return ADI_TOFI_ERROR;
145  char *last_slash = strrchr(path, '\\');
146  strncpy(process_path, path, last_slash - path + 1);
147 #elif __unix
148  uint32_t status = 0;
149  char path[MAX_PATH_SIZE];
150  status = readlink("/proc/self/exe", path, path_size);
151  if (status == 0 || status == path_size)
152  return ADI_TOFI_ERROR;
153  char *last_slash = strrchr(path, '/');
154  strncpy(process_path, path, last_slash - path + 1);
155 #endif
156  return ADI_TOFI_SUCCESS;
157 }
158 
159 uint32_t Gcd(uint32_t a, uint32_t b) {
160  if (a == 0)
161  return b;
162  return Gcd(b % a, a);
163 }
FileData
Definition: tofi_util.h:64
Gcd
uint32_t Gcd(uint32_t a, uint32_t b)
Definition: tofi_util.c:159
NULL
NULL
Definition: test_security_zap.cpp:405
ION_HEAP_ID_SYSTEM
#define ION_HEAP_ID_SYSTEM
Definition: tofi_util.h:31
LoadFileContents
FileData LoadFileContents(char *filename)
Definition: tofi_util.c:78
b
GLboolean GLboolean GLboolean b
Definition: glcorearb.h:3228
ADI_TOFI_FILE_WRITE
@ ADI_TOFI_FILE_WRITE
15
Definition: tofi_error.h:28
WriteDataToFile
uint32_t WriteDataToFile(char *file_name, uint8_t *buffer, uint32_t buffer_size)
Definition: tofi_util.c:116
path
GLsizei const GLchar ** path
Definition: glcorearb.h:3658
ADI_TOFI_OPEN_FILE
@ ADI_TOFI_OPEN_FILE
13
Definition: tofi_error.h:26
TransformationXyzToZ
uint32_t TransformationXyzToZ(uint32_t n_rows, uint32_t n_cols, const Point3I *p_xyz_image_data, uint16_t *p_zdepth_image_data)
Definition: tofi_util.c:53
MAX_PATH_SIZE
#define MAX_PATH_SIZE
Definition: tofi_util.c:39
Point3I::c
int16_t c
Definition: tofi_util.h:55
p
const char * p
Definition: gmock-matchers_test.cc:3863
size
#define size
Definition: glcorearb.h:2944
buffer
Definition: buffer_processor.h:43
GetProcessPath
uint32_t GetProcessPath(char *process_path, uint32_t path_size)
Definition: tofi_util.c:138
i
int i
Definition: gmock-matchers_test.cc:764
versiongenerate.buffer_size
int buffer_size
Definition: versiongenerate.py:65
size
GLsizeiptr size
Definition: glcorearb.h:2943
ADI_TOFI_ERROR
@ ADI_TOFI_ERROR
2
Definition: tofi_error.h:13
tofi_util.h
GetDataFileSize
uint32_t GetDataFileSize(char *file_name)
Definition: tofi_util.c:66
f
GLfloat f
Definition: glcorearb.h:3964
Point3I
Definition: tofi_util.h:52
ADI_TOFI_SUCCESS
@ ADI_TOFI_SUCCESS
0
Definition: tofi_error.h:11
a
GLboolean GLboolean GLboolean GLboolean a
Definition: glcorearb.h:3228
ADI_TOFI_NULL_ARGUMENT
@ ADI_TOFI_NULL_ARGUMENT
9
Definition: tofi_error.h:21


libaditof
Author(s):
autogenerated on Wed May 21 2025 02:07:00