process.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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 "process.h"
  21. #include "io.h"
  22. #define LINEAR_INTERPOLATION(l_value, r_value, coff) \
  23. ({ (1 - (coff)) * (l_value) + (coff) * (r_value); })
  24. /******************************************************************************
  25. * *
  26. * Static Functions *
  27. * *
  28. * ***************************************************************************/
  29. /*!
  30. * \brief Clip data to range: [v_min, v_max]
  31. *
  32. * \param data The value will be clip
  33. * \param v_min The left boundary
  34. * \param v_max The right boundary
  35. * \return The clipped value
  36. *
  37. */
  38. static float _clip(float data, float v_min, float v_max) {
  39. data = data >= v_min ? data : v_min;
  40. data = data <= v_max ? data : v_max;
  41. return data;
  42. }
  43. /*!
  44. * \brief Get data from tensor file or text file.
  45. * Note that: Only One data in a line in the file.
  46. *
  47. * \param filename The file path, the suffix is .tensor or .txt
  48. * \param size The number of data items
  49. *
  50. */
  51. static float* _get_data_from_file(const char* filename, uint32_t size) {
  52. uint32_t j;
  53. float fval = 0.0;
  54. float* buffer = NULL;
  55. FILE* fp = fopen(filename, "rb");
  56. if (fp == NULL) {
  57. printf("Invalid input file: %s\n", filename);
  58. return NULL;
  59. }
  60. buffer = malloc(size * sizeof(float));
  61. if (buffer == NULL) {
  62. printf("Malloc fail\n");
  63. return NULL;
  64. }
  65. for (j = 0; j < size; j++) {
  66. if (fscanf(fp, "%f ", &fval) != 1) {
  67. printf("Invalid input file\n");
  68. return NULL;
  69. } else {
  70. buffer[j] = fval;
  71. }
  72. }
  73. fclose(fp);
  74. return buffer;
  75. }
  76. /*!
  77. * \brief Obain the number of pixels in given image.
  78. *
  79. * \param img The object of struct image_data
  80. * \return The number of pixels
  81. */
  82. uint32_t get_size(struct image_data img) {
  83. uint32_t i;
  84. uint32_t sz = 1;
  85. for (i = 0; i < img.dim; i++) {
  86. sz *= img.shape[i];
  87. }
  88. return sz;
  89. }
  90. /*!
  91. * \brief Get Value of image at (h_idx, w_idx, c_idx)
  92. *
  93. * \param img The pointer of struct image_data
  94. * \param h_idx The index value of the point's height
  95. * \param w_idx The index value of the point's width
  96. * \param c_idx The index value of the point's channel
  97. * \return The pixel value of image at (h_idx, w_idx, c_idx)
  98. *
  99. */
  100. float get_value(struct image_data img, uint32_t h_idx, uint32_t w_idx, uint32_t c_idx) {
  101. int32_t height = img.shape[0];
  102. int32_t width = img.shape[1];
  103. int32_t channel = img.shape[2];
  104. if (h_idx < 0 || h_idx >= height || w_idx < 0 || w_idx >= width || c_idx < 0 ||
  105. c_idx >= channel) {
  106. printf("Invalid shape index! (%d, %d, %d)\n", h_idx, w_idx, c_idx);
  107. exit(1);
  108. }
  109. uint32_t idx = h_idx * (width * channel) + w_idx * channel + c_idx;
  110. return img.data[idx];
  111. }
  112. /*!
  113. * \brief Get the data of the specified file
  114. * Generally, the data obtained from tensor file can be directly used for model
  115. * inference while the data obtained from image file needs further preprocessing.
  116. *
  117. * \param filename The path of data file
  118. * \param size The expected number of data. If the file is image, this param will
  119. * be ignored
  120. * \return The object struct image_data that contain the loaded image data
  121. */
  122. struct image_data* get_input_data(const char* filename, uint32_t size) {
  123. enum file_type type;
  124. struct image_data* img = calloc(1, sizeof(struct image_data));
  125. type = get_file_type(filename);
  126. if (type == FILE_TENSOR) {
  127. // read data from tensor or txt file.
  128. img->data = _get_data_from_file(filename, size);
  129. } else if (type == FILE_BIN) {
  130. img->data = (float*)get_binary_from_file(filename, NULL);
  131. } else {
  132. free(img);
  133. return NULL;
  134. }
  135. return img;
  136. }
  137. void free_image_data(struct image_data* img) {
  138. if (img->shape) {
  139. free(img->shape);
  140. }
  141. free(img);
  142. }
  143. /*!
  144. * \brief Substract mean values(RGB). If the channel of data is 1, then use
  145. * r_mean only.
  146. *
  147. * \param img The pointer of struct image_data
  148. * \param r_mean The mean value of r-channel in img->data
  149. * \param g_mean The mean value of g-channel in img->data that will be ignored
  150. * if the dim of original image's channel is 1
  151. * \param b_mean The mean value of b-channel in img->data that will be ignored
  152. * if the dim of original image's channel is 1
  153. */
  154. void sub_mean(struct image_data* img, float r_mean, float g_mean, float b_mean) {
  155. uint32_t sz, channel;
  156. uint32_t idx;
  157. channel = img->shape[2];
  158. if (channel != 1 && channel != 3) {
  159. printf("Don't know how to sub mean with channel=%d\n", channel);
  160. exit(1);
  161. }
  162. sz = get_size(*img);
  163. for (idx = 0; idx < sz; idx += channel) {
  164. if (channel == 1) {
  165. img->data[idx] -= r_mean;
  166. } else {
  167. img->data[idx + 0] -= r_mean;
  168. img->data[idx + 1] -= g_mean;
  169. img->data[idx + 2] -= b_mean;
  170. }
  171. }
  172. }
  173. /*!
  174. * \brief Scale the image data with specified value.
  175. *
  176. * \param img The pointer of struct image_data
  177. * \param scale All the data in image will be multiplied by this value
  178. */
  179. void data_scale(struct image_data* img, float scale) {
  180. uint32_t idx;
  181. for (idx = 0; idx < get_size(*img); idx++) {
  182. img->data[idx] *= scale;
  183. }
  184. }
  185. /**
  186. * \brief Crop the image data with specified shape, using central crop method.
  187. *
  188. * \param img The pointer of struct image_data
  189. * \param height crop the height of data by height value
  190. * \param width crop the width of data by width value
  191. *
  192. */
  193. void data_crop(struct image_data* img, uint32_t height, uint32_t width) {
  194. uint32_t ori_width, ori_height, ori_channel;
  195. uint32_t row, col, c;
  196. uint32_t start_row, start_col;
  197. if (img->shape[0] == height && img->shape[1] == width) {
  198. return;
  199. }
  200. ori_height = img->shape[0];
  201. ori_width = img->shape[1];
  202. ori_channel = img->shape[2];
  203. if (width > ori_width || height > ori_height) {
  204. printf("Can not crop data by (%d, %d)\n", height, width);
  205. exit(1);
  206. }
  207. float* new_data = (float*)malloc(sizeof(float) * (height * width * ori_channel)); // NOLINT
  208. start_row = ori_height / 2 - height / 2;
  209. start_col = ori_width / 2 - width / 2;
  210. for (row = 0; row < height; row++) {
  211. for (col = 0; col < width; col++) {
  212. for (c = 0; c < ori_channel; c++) {
  213. new_data[row * (width * ori_channel) + col * ori_channel + c] =
  214. get_value(*img, start_row + row, start_col + col, c);
  215. }
  216. }
  217. }
  218. free(img->data);
  219. img->data = new_data;
  220. img->shape[0] = height;
  221. img->shape[1] = width;
  222. }
  223. /*!
  224. * \brief Resize the image into target image size with bilinear interpolation method.
  225. *
  226. * | | |
  227. * | | |
  228. * ---p00(srcY_i, srcX_i)--------f1------p01(srcY_i, srcX_i+1)-----
  229. * | | |
  230. * | p(srcY_i+h_coff, srcX_i+w_coff) |
  231. * | | |
  232. * ---p10(srcY_i+1, srcX_i)------f2-------p11(srcY_i+1, srcY_i)-----
  233. * | | |
  234. * | | |
  235. *
  236. * srcX(or srcY) can be got by:
  237. * src = (dst + 0.5) * scale - 0.5
  238. * and
  239. * coff = src - floor(src) which denotes the weight in single Linear interpolation.
  240. * Finaly, we can get the value as following:
  241. * f1 = p00 * (1-coff1) + coff1 * p01
  242. * f2 = p10 * (1-coff1) + coff1 * p11
  243. * p = f1 * (1-coff2) + coff2 * f2
  244. *
  245. * \param img The pointer of struct image_data, which denote the image data that will
  246. * be resized.
  247. * \param dst_height The height of image after resizing it.
  248. * \param dst_widht The width of image after resize it.
  249. *
  250. */
  251. void imresize(struct image_data* img, uint32_t dst_height, uint32_t dst_width) {
  252. uint32_t srcX, srcY, dstX, dstY;
  253. float srcX_f, srcY_f; // float index
  254. int srcX_i, srcY_i; // integer index
  255. float w_coff, h_coff;
  256. float scaleX = (float)img->shape[1] / (float)dst_width; // NOLINT
  257. float scaleY = (float)img->shape[0] / (float)dst_height; // NOLINT
  258. float up_left, bottom_left, up_right, bottom_right;
  259. uint32_t c; // index of channel
  260. float f1, f2;
  261. float* resized_data;
  262. if (img->shape[0] == dst_height && img->shape[1] == dst_width) {
  263. return;
  264. }
  265. resized_data =
  266. (float*)malloc(sizeof(float) * (dst_height * dst_width * img->shape[2])); // NOLINT
  267. for (dstY = 0; dstY < dst_height; dstY++) {
  268. for (dstX = 0; dstX < dst_width; dstX++) {
  269. // Get the mapping position of the current point in the original image
  270. srcX_f = ((float)dstX + 0.5) * scaleX - 0.5; // NOLINT
  271. srcY_f = ((float)dstY + 0.5) * scaleY - 0.5; // NOLINT
  272. // Get weight in interpolation
  273. w_coff = srcX_f - floor(srcX_f);
  274. h_coff = srcY_f - floor(srcY_f);
  275. srcX_i = floor(srcX_f);
  276. srcY_i = floor(srcY_f);
  277. for (c = 0; c < img->shape[2]; c++) {
  278. // Get the pixel values of four points around
  279. up_left = get_value(*img, _clip(srcY_i, 0, img->shape[0] - 1),
  280. _clip(srcX_i, 0, img->shape[1] - 1), c);
  281. up_right = get_value(*img, _clip(srcY_i, 0, img->shape[0] - 1),
  282. _clip(srcX_i + 1, 0, img->shape[1] - 1), c);
  283. bottom_left = get_value(*img, _clip(srcY_i + 1, 0, img->shape[0] - 1),
  284. _clip(srcX_i, 0, img->shape[1] - 1), c);
  285. bottom_right = get_value(*img, _clip(srcY_i + 1, 0, img->shape[0] - 1),
  286. _clip(srcX_i + 1, 0, img->shape[1] - 1), c);
  287. // Horizontal linear interpolation
  288. f1 = LINEAR_INTERPOLATION(up_left, up_right, w_coff);
  289. f2 = LINEAR_INTERPOLATION(bottom_left, bottom_right, w_coff);
  290. // Vertical linear interpolation
  291. resized_data[dstY * (dst_width * img->shape[2]) + dstX * img->shape[2] + c] =
  292. LINEAR_INTERPOLATION(f1, f2, h_coff);
  293. }
  294. }
  295. }
  296. // Updata data in place
  297. free(img->data);
  298. img->data = NULL;
  299. img->shape[0] = dst_height;
  300. img->shape[1] = dst_width;
  301. img->data = resized_data;
  302. }
  303. /*!
  304. * \brief Convert image from RGB to BGR.
  305. *
  306. * \param img The pointer of struct image_data
  307. */
  308. void imrgb2bgr(struct image_data* img) {
  309. uint32_t idx;
  310. float tmp;
  311. if (img->dim != 3) {
  312. printf("Invalid dim: %d\n", img->dim);
  313. return;
  314. }
  315. if (img->shape[2] == 1) {
  316. return;
  317. } else if (img->shape[2] != 3) {
  318. printf("Invalid channel: %d\n", img->shape[2]);
  319. return;
  320. } else {
  321. for (idx = 0; idx < get_size(*img); idx += 3) {
  322. tmp = img->data[idx];
  323. img->data[idx] = img->data[idx + 2];
  324. img->data[idx + 2] = tmp;
  325. }
  326. }
  327. }
  328. /*!
  329. * \brief Convert image data from HWC to CHW.
  330. *
  331. * \param img The pointer of struct image_data
  332. *
  333. */
  334. void imhwc2chw(struct image_data* img) {
  335. uint32_t row, col, channel;
  336. float* transposed_data = NULL;
  337. uint32_t H, W, C;
  338. if (img->dim != 3) {
  339. printf("Invalid dim: %d\n", img->dim);
  340. return;
  341. }
  342. H = img->shape[0];
  343. W = img->shape[1];
  344. C = img->shape[2];
  345. transposed_data = (float*)malloc(sizeof(float) * get_size(*img)); // NOLINT
  346. for (channel = 0; channel < C; channel++) {
  347. for (row = 0; row < H; row++) {
  348. for (col = 0; col < W; col++) {
  349. transposed_data[channel * (H * W) + row * W + col] = get_value(*img, row, col, channel);
  350. }
  351. }
  352. }
  353. // Updata image data
  354. free(img->data);
  355. img->data = transposed_data;
  356. img->shape[0] = C;
  357. img->shape[1] = H;
  358. img->shape[2] = W;
  359. }
  360. /*!
  361. * \brief Convert non-RGB data to rgb data.
  362. * For example, the shape of gray image data is (h ,w, 1) and the shape of
  363. * RGBA image data is (h, w, 4), all of these image data should be convert
  364. * to (h, w, 3) if neccesary.
  365. *
  366. * \param img The pointer of struct image_data
  367. *
  368. */
  369. void im2rgb(struct image_data* img) {
  370. uint32_t idx, cnt = 0;
  371. float* new_data = NULL;
  372. uint32_t new_size, ori_size;
  373. uint32_t ori_channel;
  374. ori_channel = img->shape[2];
  375. if (ori_channel == 3) {
  376. return;
  377. }
  378. if (ori_channel == 2 || ori_channel > 4) {
  379. printf("Invalid dim: %d\n", ori_channel);
  380. exit(1);
  381. }
  382. ori_size = get_size(*img);
  383. new_size = img->shape[0] * img->shape[1] * 3;
  384. new_data = (float*)malloc(sizeof(float) * new_size); // NOLINT
  385. for (idx = 0; idx < ori_size; idx++) {
  386. if (ori_channel == 1) {
  387. new_data[idx * 3 + 0] = img->data[idx];
  388. new_data[idx * 3 + 1] = img->data[idx];
  389. new_data[idx * 3 + 2] = img->data[idx];
  390. } else if (ori_channel == 4) {
  391. if ((idx + 1) % 4 == 0) continue;
  392. new_data[cnt] = img->data[idx];
  393. cnt++;
  394. }
  395. }
  396. free(img->data);
  397. img->data = new_data;
  398. img->shape[2] = 3;
  399. }