io.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. /* auto generate by HHB_VERSION "2.0.21" */
  20. #include "io.h"
  21. /******************************************************************************
  22. * *
  23. * Utils for process data *
  24. * *
  25. * ***************************************************************************/
  26. /*!
  27. * \brief Get the type of file (JPEG PNG or txt/tensor by suffix
  28. *
  29. * \param filename Image file name.
  30. * \return FILE_PNG FILE_JPEG or FILE_TENSOR of enum file_type
  31. *
  32. */
  33. enum file_type get_file_type(const char* filename) {
  34. enum file_type type = 0;
  35. const char* ptr;
  36. char sep = '.';
  37. uint32_t pos, n;
  38. char buff[32] = {0};
  39. ptr = strrchr(filename, sep);
  40. pos = ptr - filename;
  41. n = strlen(filename) - (pos + 1);
  42. strncpy(buff, filename + (pos + 1), n);
  43. if (strcmp(buff, "jpg") == 0 || strcmp(buff, "jpeg") == 0 || strcmp(buff, "JPG") == 0 ||
  44. strcmp(buff, "JPEG") == 0) {
  45. type = FILE_JPEG;
  46. } else if (strcmp(buff, "png") == 0 || strcmp(buff, "PNG") == 0) {
  47. type = FILE_PNG;
  48. } else if (strcmp(buff, "tensor") == 0) {
  49. type = FILE_TENSOR;
  50. } else if (strcmp(buff, "txt") == 0) {
  51. type = FILE_TXT;
  52. } else if (strcmp(buff, "bin") == 0) {
  53. type = FILE_BIN;
  54. } else if (strcmp(buff, "rgb") == 0) {
  55. type = 0;
  56. } else if (strcmp(buff, "bgr") == 0) {
  57. type = 0;
  58. } else {
  59. printf("Unsupport for .%s file\n", buff);
  60. exit(1);
  61. }
  62. return type;
  63. }
  64. /*!
  65. * \brief Save float data into file.
  66. *
  67. * \param filename The file that you will put the data into.
  68. * \param data The float data that you will put into file.
  69. * \param size The size of data.
  70. */
  71. void save_data_to_file(const char* filename, float* data, uint32_t size) {
  72. int i = 0;
  73. FILE* fp = fopen(filename, "w+");
  74. for (i = 0; i < size; i++) {
  75. if (i == size - 1) {
  76. fprintf(fp, "%f", data[i]);
  77. } else {
  78. fprintf(fp, "%f\n", data[i]);
  79. }
  80. }
  81. fclose(fp);
  82. }
  83. void save_uint8_to_file(const char* filename, uint8_t* data, uint32_t size) {
  84. int i = 0;
  85. FILE* fp = fopen(filename, "w+");
  86. for (i = 0; i < size; i++) {
  87. if (i == size - 1) {
  88. fprintf(fp, "%d", data[i]);
  89. } else {
  90. fprintf(fp, "%d\n", data[i]);
  91. }
  92. }
  93. fclose(fp);
  94. }
  95. void save_uint8_to_binary(const char* filename, uint8_t* data, uint32_t size) {
  96. int i = 0;
  97. FILE* fp = fopen(filename, "w+");
  98. fwrite(data, 1, size, fp);
  99. fclose(fp);
  100. }
  101. /*!
  102. * \brief Get the binary params char from model.params
  103. *
  104. * \param filename It is generally model.params for anole
  105. * \return The char data of params
  106. *
  107. */
  108. int fill_buffer_from_file(const char* filename, char* buffer) {
  109. int file_size;
  110. int ret;
  111. FILE* fp = fopen(filename, "rb");
  112. if (fp == NULL) {
  113. printf("Invalid input file: %s\n", filename);
  114. return -1;
  115. }
  116. fseek(fp, 0, SEEK_END);
  117. file_size = ftell(fp);
  118. rewind(fp);
  119. ret = fread(buffer, 1, file_size, fp);
  120. if (ret != file_size) {
  121. printf("Read input file error\n");
  122. return -1;
  123. }
  124. fclose(fp);
  125. return 0;
  126. }
  127. char* get_binary_from_file(const char* filename, int *size) {
  128. char* buffer = NULL;
  129. int file_size;
  130. int ret;
  131. FILE* fp = fopen(filename, "rb");
  132. if (fp == NULL) {
  133. printf("Invalid input file: %s\n", filename);
  134. return NULL;
  135. }
  136. fseek(fp, 0, SEEK_END);
  137. file_size = ftell(fp);
  138. rewind(fp);
  139. buffer = (char*)malloc(file_size); // NOLINT
  140. if (buffer == NULL) {
  141. printf("Malloc fail\n");
  142. return NULL;
  143. }
  144. ret = fread(buffer, 1, file_size, fp);
  145. if (ret != file_size) {
  146. printf("Read input file error\n");
  147. return NULL;
  148. }
  149. fclose(fp);
  150. if (size) {
  151. *size = file_size;
  152. }
  153. return buffer;
  154. }
  155. char** read_string_from_file(const char* filename, int* len) {
  156. char buff[MAX_FILENAME_LEN];
  157. char** result = (char**)malloc(sizeof(char*) * (MAX_FILE_LINE * MAX_INPUT_NUMBER)); // NOLINT
  158. char *find, *sep, *inter;
  159. FILE* fp = fopen(filename, "r");
  160. if (fp == NULL) {
  161. return NULL;
  162. }
  163. int cnt = 0;
  164. while (fgets(buff, sizeof(buff), fp)) {
  165. if (strcmp(buff, "\n") == 0) continue;
  166. find = strchr(buff, '\n');
  167. if (find) {
  168. *find = '\0';
  169. }
  170. sep = strtok(buff, " "); // NOLINT
  171. inter = (char*)malloc((strlen(sep) + 2) * sizeof(char)); // NOLINT
  172. memcpy(inter, sep, strlen(sep) + 1);
  173. result[cnt++] = inter;
  174. while (sep != NULL) {
  175. sep = strtok(NULL, " "); // NOLINT
  176. if (sep) {
  177. inter = (char*)malloc((strlen(sep) + 2) * sizeof(char)); // NOLINT
  178. memcpy(inter, sep, strlen(sep) + 1);
  179. result[cnt++] = inter;
  180. }
  181. }
  182. }
  183. *len = cnt;
  184. fclose(fp);
  185. return result;
  186. }
  187. uint32_t shape2string(uint32_t* shape, uint32_t dim_num, char* buf, uint32_t buf_sz) {
  188. uint32_t s;
  189. uint32_t count;
  190. if (NULL == shape || NULL == buf || dim_num == 0 || buf_sz == 0) {
  191. return 0;
  192. }
  193. count = 0;
  194. for (s = 0; s < dim_num; s++) {
  195. if (count >= buf_sz) {
  196. break;
  197. }
  198. count += snprintf(&buf[count], buf_sz - count, "%d_", shape[s]);
  199. }
  200. buf[count - 1] = 0;
  201. return count;
  202. }