switch_core_video.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. /*
  2. * FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
  3. * Copyright (C) 2005-2018, Anthony Minessale II <anthm@freeswitch.org>
  4. *
  5. * Version: MPL 1.1
  6. *
  7. * The contents of this file are subject to the Mozilla Public License Version
  8. * 1.1 (the "License"); you may not use this file except in compliance with
  9. * the License. You may obtain a copy of the License at
  10. * http://www.mozilla.org/MPL/
  11. *
  12. * Software distributed under the License is distributed on an "AS IS" basis,
  13. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  14. * for the specific language governing rights and limitations under the
  15. * License.
  16. *
  17. * The Original Code is FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
  18. *
  19. * The Initial Developer of the Original Code is
  20. * Anthony Minessale II <anthm@freeswitch.org>
  21. * Portions created by the Initial Developer are Copyright (C)
  22. * the Initial Developer. All Rights Reserved.
  23. *
  24. * Contributor(s):
  25. * Chris Rienzo <chris@signalwire.com>
  26. * Seven Du <seven@signalwire.com>
  27. *
  28. *
  29. * switch_core_video.c -- tests core_video
  30. *
  31. */
  32. #include <switch.h>
  33. #include <stdlib.h>
  34. #include <test/switch_test.h>
  35. FST_CORE_BEGIN("./conf")
  36. {
  37. FST_SUITE_BEGIN(switch_core_video)
  38. {
  39. FST_SETUP_BEGIN()
  40. {
  41. }
  42. FST_SETUP_END()
  43. FST_TEARDOWN_BEGIN()
  44. {
  45. }
  46. FST_TEARDOWN_END()
  47. #ifdef SWITCH_HAVE_YUV
  48. FST_TEST_BEGIN(data_url_test)
  49. {
  50. char *data_url = NULL;
  51. char *rgb_filename = switch_core_sprintf(fst_pool, "%s" SWITCH_PATH_SEPARATOR "test-rgb.png", SWITCH_GLOBAL_dirs.temp_dir);
  52. char *argb_filename = switch_core_sprintf(fst_pool, "%s" SWITCH_PATH_SEPARATOR "test-argb.png", SWITCH_GLOBAL_dirs.temp_dir);
  53. switch_image_t *img = switch_img_alloc(NULL, SWITCH_IMG_FMT_I420, 120, 60, 1);
  54. switch_image_t *argb_img = switch_img_alloc(NULL, SWITCH_IMG_FMT_ARGB, 120, 60, 1);
  55. switch_rgb_color_t color = { 0 };
  56. color.r = 255;
  57. // color.g = 255;
  58. // color.b = 255;
  59. switch_img_fill(img, 0, 0, img->d_w, img->d_h, &color);
  60. switch_img_add_text(img->planes[0], img->d_w, 10, 10, "-1234567890");
  61. switch_img_write_png(img, rgb_filename);
  62. switch_img_data_url_png(img, &data_url);
  63. switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "I420: %s\n", data_url);
  64. free(data_url);
  65. data_url = NULL;
  66. switch_img_copy(img, &argb_img);
  67. {
  68. uint8_t *p = argb_img->planes[0];
  69. switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "%d %d %d %d\n", *p, *(p+1), *(p+2), *(p+3));
  70. }
  71. switch_img_write_png(argb_img, argb_filename);
  72. switch_img_data_url_png(argb_img, &data_url);
  73. switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "ARGB: %s\n", data_url);
  74. free(data_url);
  75. switch_img_free(&img);
  76. switch_img_free(&argb_img);
  77. unlink(rgb_filename);
  78. unlink(argb_filename);
  79. }
  80. FST_TEST_END()
  81. #endif /* SWITCH_HAVE_YUV */
  82. FST_TEST_BEGIN(img_patch)
  83. {
  84. char *filename = switch_core_sprintf(fst_pool, "%s" SWITCH_PATH_SEPARATOR "test.png", SWITCH_GLOBAL_dirs.temp_dir);
  85. char *text_filename = switch_core_sprintf(fst_pool, "%s" SWITCH_PATH_SEPARATOR "test_text.png", SWITCH_GLOBAL_dirs.temp_dir);
  86. char *patch_filename = switch_core_sprintf(fst_pool, "%s" SWITCH_PATH_SEPARATOR "test_patched.png", SWITCH_GLOBAL_dirs.temp_dir);
  87. int width = 320;
  88. int height = 240;
  89. switch_status_t status;
  90. switch_image_t *img = NULL;
  91. switch_rgb_color_t bgcolor = {0, 0, 0}; // red
  92. switch_image_t *timg = switch_img_write_text_img(width, height, SWITCH_FALSE, "#ffffff:transparent:FreeMono.ttf:24:This is a test!");
  93. fst_requires(timg != NULL);
  94. status = switch_img_write_png(timg, text_filename);
  95. fst_check(status == SWITCH_STATUS_SUCCESS);
  96. unlink(text_filename);
  97. width *=2;
  98. height *=2;
  99. bgcolor.b = 255;
  100. img = switch_img_alloc(NULL, SWITCH_IMG_FMT_I420, width, height, 1);
  101. fst_requires(img);
  102. switch_img_fill(img, 0, 0, width, height, &bgcolor);
  103. status = switch_img_write_png(img, filename);
  104. fst_check(status == SWITCH_STATUS_SUCCESS);
  105. unlink(filename);
  106. switch_img_patch(img, timg, 0, 0);
  107. status = switch_img_write_png(img, patch_filename);
  108. fst_check(status == SWITCH_STATUS_SUCCESS);
  109. unlink(patch_filename);
  110. switch_img_free(&img);
  111. switch_img_free(&timg);
  112. }
  113. FST_TEST_END()
  114. FST_TEST_BEGIN(img_patch_alpha)
  115. {
  116. char *text_filename = switch_core_sprintf(fst_pool, "%s" SWITCH_PATH_SEPARATOR "test_text.png", SWITCH_GLOBAL_dirs.temp_dir);
  117. char *patch_filename_alpha = switch_core_sprintf(fst_pool, "%s" SWITCH_PATH_SEPARATOR "test_patched_alpha.png", SWITCH_GLOBAL_dirs.temp_dir);
  118. char *patch_filename = switch_core_sprintf(fst_pool, "%s" SWITCH_PATH_SEPARATOR "test_patched_noalpha.png", SWITCH_GLOBAL_dirs.temp_dir);
  119. int width = 320;
  120. int height = 240;
  121. switch_image_t *img = NULL;
  122. switch_status_t status;
  123. switch_image_t *timg = switch_img_write_text_img(width, height, SWITCH_FALSE, "#ffffff:transparent:FreeMono.ttf:24:This is a test!");
  124. fst_requires(timg != NULL);
  125. status = switch_img_write_png(timg, text_filename);
  126. fst_check(status == SWITCH_STATUS_SUCCESS);
  127. switch_img_free(&timg);
  128. timg = switch_img_read_png(text_filename, SWITCH_IMG_FMT_ARGB);
  129. unlink(text_filename);
  130. fst_requires(timg != NULL);
  131. img = switch_img_read_png("images/banner.png", SWITCH_IMG_FMT_ARGB);
  132. fst_requires(img);
  133. switch_img_patch(img, timg, 0, 0);
  134. status = switch_img_write_png(img, patch_filename_alpha);
  135. fst_check(status == SWITCH_STATUS_SUCCESS);
  136. unlink(patch_filename_alpha);
  137. switch_img_free(&img);
  138. img = switch_img_read_png("images/banner.png", SWITCH_IMG_FMT_ARGB);
  139. switch_img_patch_rgb(img, timg, 0, 0, SWITCH_TRUE);
  140. status = switch_img_write_png(img, patch_filename);
  141. fst_check(status == SWITCH_STATUS_SUCCESS);
  142. unlink(patch_filename);
  143. switch_img_free(&img);
  144. switch_img_free(&timg);
  145. }
  146. FST_TEST_END()
  147. FST_TEST_BEGIN(img_patch_banner_alpha)
  148. {
  149. char *patch_filename = switch_core_sprintf(fst_pool, "%s" SWITCH_PATH_SEPARATOR "test_patched_banner_noalpha.png", SWITCH_GLOBAL_dirs.temp_dir);
  150. char *patch_filename_alpha = switch_core_sprintf(fst_pool, "%s" SWITCH_PATH_SEPARATOR "test_patched_banner_alpha.png", SWITCH_GLOBAL_dirs.temp_dir);
  151. switch_status_t status;
  152. switch_image_t *img = switch_img_read_png("images/banner.png", SWITCH_IMG_FMT_ARGB);
  153. switch_image_t *img2 = switch_img_read_png("images/banner.png", SWITCH_IMG_FMT_ARGB);
  154. fst_requires(img);
  155. fst_requires(img2);
  156. switch_img_patch(img, img2, 80, 20);
  157. status = switch_img_write_png(img, patch_filename_alpha);
  158. fst_check(status == SWITCH_STATUS_SUCCESS);
  159. unlink(patch_filename_alpha);
  160. switch_img_free(&img);
  161. img = switch_img_read_png("images/banner.png", SWITCH_IMG_FMT_ARGB);
  162. switch_img_patch_rgb(img, img2, 80, 20, SWITCH_TRUE);
  163. status = switch_img_write_png(img, patch_filename);
  164. fst_check(status == SWITCH_STATUS_SUCCESS);
  165. unlink(patch_filename);
  166. switch_img_free(&img);
  167. switch_img_free(&img2);
  168. }
  169. FST_TEST_END()
  170. FST_TEST_BEGIN(img_patch_signalwire_alpha)
  171. {
  172. char *patch_filename = switch_core_sprintf(fst_pool, "%s" SWITCH_PATH_SEPARATOR "test_patched_signalwire_alpha.png", SWITCH_GLOBAL_dirs.temp_dir);
  173. switch_image_t *timg_small = NULL;
  174. switch_image_t *timg = switch_img_read_png("images/signalwire.png", SWITCH_IMG_FMT_ARGB);
  175. switch_image_t *img = switch_img_read_png("images/banner.png", SWITCH_IMG_FMT_ARGB);
  176. switch_status_t status;
  177. fst_requires(timg != NULL);
  178. fst_requires(img);
  179. switch_img_scale(timg, &timg_small, timg->d_w / 5, timg->d_h / 5);
  180. switch_img_patch(img, timg_small, 80, 20);
  181. status = switch_img_write_png(img, patch_filename);
  182. fst_check(status == SWITCH_STATUS_SUCCESS);
  183. switch_img_free(&img);
  184. switch_img_free(&timg);
  185. switch_img_free(&timg_small);
  186. unlink(patch_filename);
  187. }
  188. FST_TEST_END()
  189. FST_TEST_BEGIN(img_patch_signalwire_no_alpha)
  190. {
  191. char *patch_filename = switch_core_sprintf(fst_pool, "%s" SWITCH_PATH_SEPARATOR "test_patched_signalwire_no_alpha.png", SWITCH_GLOBAL_dirs.temp_dir);
  192. switch_image_t *timg_small = NULL;
  193. switch_image_t *timg = switch_img_read_png("images/signalwire.png", SWITCH_IMG_FMT_ARGB);
  194. switch_image_t *img = switch_img_read_png("images/banner.png", SWITCH_IMG_FMT_ARGB);
  195. switch_status_t status;
  196. fst_requires(timg != NULL);
  197. fst_requires(img);
  198. switch_img_scale(timg, &timg_small, timg->d_w / 5, timg->d_h / 5);
  199. switch_img_patch_rgb(img, timg_small, 80, 20, SWITCH_TRUE);
  200. status = switch_img_write_png(img, patch_filename);
  201. fst_check(status == SWITCH_STATUS_SUCCESS);
  202. switch_img_free(&img);
  203. switch_img_free(&timg);
  204. switch_img_free(&timg_small);
  205. unlink(patch_filename);
  206. }
  207. FST_TEST_END()
  208. #ifdef SWITCH_HAVE_YUV
  209. FST_TEST_BEGIN(stb_data_url)
  210. {
  211. switch_image_t *img = switch_img_alloc(NULL, SWITCH_IMG_FMT_I420, 120, 60, 1);
  212. char *data_url = NULL;
  213. switch_rgb_color_t color = { 0 };
  214. color.r = 255;
  215. // color.g = 255;
  216. // color.b = 255;
  217. switch_img_fill(img, 0, 0, img->d_w, img->d_h, &color);
  218. switch_img_add_text(img->planes[0], img->d_w, 10, 10, "-1234567890");
  219. switch_img_data_url(img, &data_url, "png", 0);
  220. switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "PNG: %s\n", data_url);
  221. free(data_url);
  222. data_url = NULL;
  223. switch_img_data_url(img, &data_url, "jpeg", 50);
  224. switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "JPG: %s\n", data_url);
  225. free(data_url);
  226. switch_img_free(&img);
  227. }
  228. FST_TEST_END()
  229. FST_TEST_BEGIN(read_from_file)
  230. {
  231. switch_image_t *img;
  232. char *rgb_write_filename = switch_core_sprintf(fst_pool, "%s" SWITCH_PATH_SEPARATOR "cluecon-rgb.png", SWITCH_GLOBAL_dirs.temp_dir);
  233. char *argb_write_filename = switch_core_sprintf(fst_pool, "%s" SWITCH_PATH_SEPARATOR "cluecon-argb.png", SWITCH_GLOBAL_dirs.temp_dir);
  234. char *jpg_write_filename = switch_core_sprintf(fst_pool, "%s" SWITCH_PATH_SEPARATOR "cluecon-jpg.png", SWITCH_GLOBAL_dirs.temp_dir);
  235. img = switch_img_read_from_file("../../images/cluecon.png", SWITCH_IMG_FMT_I420);
  236. fst_requires(img);
  237. switch_img_write_png(img, rgb_write_filename);
  238. switch_img_free(&img);
  239. unlink(rgb_write_filename);
  240. img = switch_img_read_from_file("../../images/cluecon.png", SWITCH_IMG_FMT_ARGB);
  241. fst_requires(img);
  242. switch_img_write_png(img, argb_write_filename);
  243. switch_img_free(&img);
  244. unlink(argb_write_filename);
  245. img = switch_img_read_from_file("../../images/cluecon.jpg", SWITCH_IMG_FMT_I420);
  246. fst_requires(img);
  247. switch_img_write_png(img, jpg_write_filename);
  248. switch_img_free(&img);
  249. unlink(jpg_write_filename);
  250. }
  251. FST_TEST_END()
  252. FST_TEST_BEGIN(write_to_file)
  253. {
  254. switch_image_t *img;
  255. switch_status_t status;
  256. char *rgb_write_filename = switch_core_sprintf(fst_pool, "%s" SWITCH_PATH_SEPARATOR "cluecon-rgb-write.png", SWITCH_GLOBAL_dirs.temp_dir);
  257. char *argb_write_filename = switch_core_sprintf(fst_pool, "%s" SWITCH_PATH_SEPARATOR "cluecon-argb-write.png", SWITCH_GLOBAL_dirs.temp_dir);
  258. char *jpg_write_filename = switch_core_sprintf(fst_pool, "%s" SWITCH_PATH_SEPARATOR "cluecon-jpg-write.png", SWITCH_GLOBAL_dirs.temp_dir);
  259. img = switch_img_read_from_file("../../images/cluecon.png", SWITCH_IMG_FMT_I420);
  260. fst_requires(img);
  261. status = switch_img_write_to_file(img, rgb_write_filename, 0);
  262. switch_img_free(&img);
  263. fst_check(status == SWITCH_STATUS_SUCCESS);
  264. unlink(rgb_write_filename);
  265. img = switch_img_read_from_file("../../images/cluecon.png", SWITCH_IMG_FMT_ARGB);
  266. fst_requires(img);
  267. status = switch_img_write_to_file(img, argb_write_filename, 0);
  268. switch_img_free(&img);
  269. fst_check(status == SWITCH_STATUS_SUCCESS);
  270. unlink(argb_write_filename);
  271. img = switch_img_read_from_file("../../images/cluecon.jpg", SWITCH_IMG_FMT_I420);
  272. fst_requires(img);
  273. status = switch_img_write_to_file(img, jpg_write_filename, 100);
  274. switch_img_free(&img);
  275. fst_check(status == SWITCH_STATUS_SUCCESS);
  276. unlink(jpg_write_filename);
  277. }
  278. FST_TEST_END()
  279. #endif /* SWITCH_HAVE_YUV */
  280. }
  281. FST_SUITE_END()
  282. }
  283. FST_CORE_END()