switch_core_video.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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. FST_TEST_BEGIN(data_url_test)
  48. {
  49. char *data_url = NULL;
  50. char *rgb_filename = switch_core_sprintf(fst_pool, "%s" SWITCH_PATH_SEPARATOR "test-rgb.png", SWITCH_GLOBAL_dirs.temp_dir);
  51. char *argb_filename = switch_core_sprintf(fst_pool, "%s" SWITCH_PATH_SEPARATOR "test-argb.png", SWITCH_GLOBAL_dirs.temp_dir);
  52. switch_image_t *img = switch_img_alloc(NULL, SWITCH_IMG_FMT_I420, 120, 60, 1);
  53. switch_image_t *argb_img = switch_img_alloc(NULL, SWITCH_IMG_FMT_ARGB, 120, 60, 1);
  54. switch_rgb_color_t color = { 0 };
  55. color.r = 255;
  56. // color.g = 255;
  57. // color.b = 255;
  58. switch_img_fill(img, 0, 0, img->d_w, img->d_h, &color);
  59. switch_img_add_text(img->planes[0], img->d_w, 10, 10, "-1234567890");
  60. switch_img_write_png(img, rgb_filename);
  61. switch_img_data_url_png(img, &data_url);
  62. switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "I420: %s\n", data_url);
  63. free(data_url);
  64. data_url = NULL;
  65. switch_img_copy(img, &argb_img);
  66. {
  67. uint8_t *p = argb_img->planes[0];
  68. switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "%d %d %d %d\n", *p, *(p+1), *(p+2), *(p+3));
  69. }
  70. switch_img_write_png(argb_img, argb_filename);
  71. switch_img_data_url_png(argb_img, &data_url);
  72. switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "ARGB: %s\n", data_url);
  73. free(data_url);
  74. switch_img_free(&img);
  75. switch_img_free(&argb_img);
  76. unlink(rgb_filename);
  77. unlink(argb_filename);
  78. }
  79. FST_TEST_END()
  80. FST_TEST_BEGIN(img_patch)
  81. {
  82. char *filename = switch_core_sprintf(fst_pool, "%s" SWITCH_PATH_SEPARATOR "test.png", SWITCH_GLOBAL_dirs.temp_dir);
  83. char *text_filename = switch_core_sprintf(fst_pool, "%s" SWITCH_PATH_SEPARATOR "test_text.png", SWITCH_GLOBAL_dirs.temp_dir);
  84. char *patch_filename = switch_core_sprintf(fst_pool, "%s" SWITCH_PATH_SEPARATOR "test_patched.png", SWITCH_GLOBAL_dirs.temp_dir);
  85. int width = 320;
  86. int height = 240;
  87. switch_status_t status;
  88. switch_image_t *img = NULL;
  89. switch_rgb_color_t bgcolor = {0, 0, 0}; // red
  90. switch_image_t *timg = switch_img_write_text_img(width, height, SWITCH_FALSE, "#ffffff:transparent:FreeMono.ttf:24:This is a test!");
  91. fst_requires(timg != NULL);
  92. status = switch_img_write_png(timg, text_filename);
  93. fst_check(status == SWITCH_STATUS_SUCCESS);
  94. unlink(text_filename);
  95. width *=2;
  96. height *=2;
  97. bgcolor.b = 255;
  98. img = switch_img_alloc(NULL, SWITCH_IMG_FMT_I420, width, height, 1);
  99. fst_requires(img);
  100. switch_img_fill(img, 0, 0, width, height, &bgcolor);
  101. status = switch_img_write_png(img, filename);
  102. fst_check(status == SWITCH_STATUS_SUCCESS);
  103. unlink(filename);
  104. switch_img_patch(img, timg, 0, 0);
  105. status = switch_img_write_png(img, patch_filename);
  106. fst_check(status == SWITCH_STATUS_SUCCESS);
  107. unlink(patch_filename);
  108. switch_img_free(&img);
  109. switch_img_free(&timg);
  110. }
  111. FST_TEST_END()
  112. FST_TEST_BEGIN(img_patch_alpha)
  113. {
  114. char *text_filename = switch_core_sprintf(fst_pool, "%s" SWITCH_PATH_SEPARATOR "test_text.png", SWITCH_GLOBAL_dirs.temp_dir);
  115. char *patch_filename_alpha = switch_core_sprintf(fst_pool, "%s" SWITCH_PATH_SEPARATOR "test_patched_alpha.png", SWITCH_GLOBAL_dirs.temp_dir);
  116. char *patch_filename = switch_core_sprintf(fst_pool, "%s" SWITCH_PATH_SEPARATOR "test_patched_noalpha.png", SWITCH_GLOBAL_dirs.temp_dir);
  117. int width = 320;
  118. int height = 240;
  119. switch_image_t *img = NULL;
  120. switch_status_t status;
  121. switch_image_t *timg = switch_img_write_text_img(width, height, SWITCH_FALSE, "#ffffff:transparent:FreeMono.ttf:24:This is a test!");
  122. fst_requires(timg != NULL);
  123. status = switch_img_write_png(timg, text_filename);
  124. fst_check(status == SWITCH_STATUS_SUCCESS);
  125. switch_img_free(&timg);
  126. timg = switch_img_read_png(text_filename, SWITCH_IMG_FMT_ARGB);
  127. unlink(text_filename);
  128. fst_requires(timg != NULL);
  129. img = switch_img_read_png("images/banner.png", SWITCH_IMG_FMT_ARGB);
  130. fst_requires(img);
  131. switch_img_patch(img, timg, 0, 0);
  132. status = switch_img_write_png(img, patch_filename_alpha);
  133. fst_check(status == SWITCH_STATUS_SUCCESS);
  134. unlink(patch_filename_alpha);
  135. switch_img_free(&img);
  136. img = switch_img_read_png("images/banner.png", SWITCH_IMG_FMT_ARGB);
  137. switch_img_patch_rgb(img, timg, 0, 0, SWITCH_TRUE);
  138. status = switch_img_write_png(img, patch_filename);
  139. fst_check(status == SWITCH_STATUS_SUCCESS);
  140. unlink(patch_filename);
  141. switch_img_free(&img);
  142. switch_img_free(&timg);
  143. }
  144. FST_TEST_END()
  145. FST_TEST_BEGIN(img_patch_banner_alpha)
  146. {
  147. char *patch_filename = switch_core_sprintf(fst_pool, "%s" SWITCH_PATH_SEPARATOR "test_patched_banner_noalpha.png", SWITCH_GLOBAL_dirs.temp_dir);
  148. char *patch_filename_alpha = switch_core_sprintf(fst_pool, "%s" SWITCH_PATH_SEPARATOR "test_patched_banner_alpha.png", SWITCH_GLOBAL_dirs.temp_dir);
  149. switch_status_t status;
  150. switch_image_t *img = switch_img_read_png("images/banner.png", SWITCH_IMG_FMT_ARGB);
  151. switch_image_t *img2 = switch_img_read_png("images/banner.png", SWITCH_IMG_FMT_ARGB);
  152. fst_requires(img);
  153. fst_requires(img2);
  154. switch_img_patch(img, img2, 80, 20);
  155. status = switch_img_write_png(img, patch_filename_alpha);
  156. fst_check(status == SWITCH_STATUS_SUCCESS);
  157. unlink(patch_filename_alpha);
  158. switch_img_free(&img);
  159. img = switch_img_read_png("images/banner.png", SWITCH_IMG_FMT_ARGB);
  160. switch_img_patch_rgb(img, img2, 80, 20, SWITCH_TRUE);
  161. status = switch_img_write_png(img, patch_filename);
  162. fst_check(status == SWITCH_STATUS_SUCCESS);
  163. unlink(patch_filename);
  164. switch_img_free(&img);
  165. switch_img_free(&img2);
  166. }
  167. FST_TEST_END()
  168. FST_TEST_BEGIN(img_patch_signalwire_alpha)
  169. {
  170. char *patch_filename = switch_core_sprintf(fst_pool, "%s" SWITCH_PATH_SEPARATOR "test_patched_signalwire_alpha.png", SWITCH_GLOBAL_dirs.temp_dir);
  171. switch_image_t *timg_small = NULL;
  172. switch_image_t *timg = switch_img_read_png("images/signalwire.png", SWITCH_IMG_FMT_ARGB);
  173. switch_image_t *img = switch_img_read_png("images/banner.png", SWITCH_IMG_FMT_ARGB);
  174. switch_status_t status;
  175. fst_requires(timg != NULL);
  176. fst_requires(img);
  177. switch_img_scale(timg, &timg_small, timg->d_w / 5, timg->d_h / 5);
  178. switch_img_patch(img, timg_small, 80, 20);
  179. status = switch_img_write_png(img, patch_filename);
  180. fst_check(status == SWITCH_STATUS_SUCCESS);
  181. switch_img_free(&img);
  182. switch_img_free(&timg);
  183. switch_img_free(&timg_small);
  184. unlink(patch_filename);
  185. }
  186. FST_TEST_END()
  187. FST_TEST_BEGIN(img_patch_signalwire_no_alpha)
  188. {
  189. char *patch_filename = switch_core_sprintf(fst_pool, "%s" SWITCH_PATH_SEPARATOR "test_patched_signalwire_no_alpha.png", SWITCH_GLOBAL_dirs.temp_dir);
  190. switch_image_t *timg_small = NULL;
  191. switch_image_t *timg = switch_img_read_png("images/signalwire.png", SWITCH_IMG_FMT_ARGB);
  192. switch_image_t *img = switch_img_read_png("images/banner.png", SWITCH_IMG_FMT_ARGB);
  193. switch_status_t status;
  194. fst_requires(timg != NULL);
  195. fst_requires(img);
  196. switch_img_scale(timg, &timg_small, timg->d_w / 5, timg->d_h / 5);
  197. switch_img_patch_rgb(img, timg_small, 80, 20, SWITCH_TRUE);
  198. status = switch_img_write_png(img, patch_filename);
  199. fst_check(status == SWITCH_STATUS_SUCCESS);
  200. switch_img_free(&img);
  201. switch_img_free(&timg);
  202. switch_img_free(&timg_small);
  203. unlink(patch_filename);
  204. }
  205. FST_TEST_END()
  206. FST_TEST_BEGIN(stb_data_url)
  207. {
  208. switch_image_t *img = switch_img_alloc(NULL, SWITCH_IMG_FMT_I420, 120, 60, 1);
  209. char *data_url = NULL;
  210. switch_rgb_color_t color = { 0 };
  211. color.r = 255;
  212. // color.g = 255;
  213. // color.b = 255;
  214. switch_img_fill(img, 0, 0, img->d_w, img->d_h, &color);
  215. switch_img_add_text(img->planes[0], img->d_w, 10, 10, "-1234567890");
  216. switch_img_data_url(img, &data_url, "png", 0);
  217. switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "PNG: %s\n", data_url);
  218. free(data_url);
  219. data_url = NULL;
  220. switch_img_data_url(img, &data_url, "jpeg", 50);
  221. switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "JPG: %s\n", data_url);
  222. free(data_url);
  223. switch_img_free(&img);
  224. }
  225. FST_TEST_END()
  226. FST_TEST_BEGIN(read_from_file)
  227. {
  228. switch_image_t *img;
  229. char *rgb_write_filename = switch_core_sprintf(fst_pool, "%s" SWITCH_PATH_SEPARATOR "cluecon-rgb.png", SWITCH_GLOBAL_dirs.temp_dir);
  230. char *argb_write_filename = switch_core_sprintf(fst_pool, "%s" SWITCH_PATH_SEPARATOR "cluecon-argb.png", SWITCH_GLOBAL_dirs.temp_dir);
  231. char *jpg_write_filename = switch_core_sprintf(fst_pool, "%s" SWITCH_PATH_SEPARATOR "cluecon-jpg.png", SWITCH_GLOBAL_dirs.temp_dir);
  232. img = switch_img_read_from_file("../../images/cluecon.png", SWITCH_IMG_FMT_I420);
  233. fst_requires(img);
  234. switch_img_write_png(img, rgb_write_filename);
  235. switch_img_free(&img);
  236. unlink(rgb_write_filename);
  237. img = switch_img_read_from_file("../../images/cluecon.png", SWITCH_IMG_FMT_ARGB);
  238. fst_requires(img);
  239. switch_img_write_png(img, argb_write_filename);
  240. switch_img_free(&img);
  241. unlink(argb_write_filename);
  242. img = switch_img_read_from_file("../../images/cluecon.jpg", SWITCH_IMG_FMT_I420);
  243. fst_requires(img);
  244. switch_img_write_png(img, jpg_write_filename);
  245. switch_img_free(&img);
  246. unlink(jpg_write_filename);
  247. }
  248. FST_TEST_END()
  249. FST_TEST_BEGIN(write_to_file)
  250. {
  251. switch_image_t *img;
  252. switch_status_t status;
  253. char *rgb_write_filename = switch_core_sprintf(fst_pool, "%s" SWITCH_PATH_SEPARATOR "cluecon-rgb-write.png", SWITCH_GLOBAL_dirs.temp_dir);
  254. char *argb_write_filename = switch_core_sprintf(fst_pool, "%s" SWITCH_PATH_SEPARATOR "cluecon-argb-write.png", SWITCH_GLOBAL_dirs.temp_dir);
  255. char *jpg_write_filename = switch_core_sprintf(fst_pool, "%s" SWITCH_PATH_SEPARATOR "cluecon-jpg-write.png", SWITCH_GLOBAL_dirs.temp_dir);
  256. img = switch_img_read_from_file("../../images/cluecon.png", SWITCH_IMG_FMT_I420);
  257. fst_requires(img);
  258. status = switch_img_write_to_file(img, rgb_write_filename, 0);
  259. switch_img_free(&img);
  260. fst_check(status == SWITCH_STATUS_SUCCESS);
  261. unlink(rgb_write_filename);
  262. img = switch_img_read_from_file("../../images/cluecon.png", SWITCH_IMG_FMT_ARGB);
  263. fst_requires(img);
  264. status = switch_img_write_to_file(img, argb_write_filename, 0);
  265. switch_img_free(&img);
  266. fst_check(status == SWITCH_STATUS_SUCCESS);
  267. unlink(argb_write_filename);
  268. img = switch_img_read_from_file("../../images/cluecon.jpg", SWITCH_IMG_FMT_I420);
  269. fst_requires(img);
  270. status = switch_img_write_to_file(img, jpg_write_filename, 100);
  271. switch_img_free(&img);
  272. fst_check(status == SWITCH_STATUS_SUCCESS);
  273. unlink(jpg_write_filename);
  274. }
  275. FST_TEST_END()
  276. }
  277. FST_SUITE_END()
  278. }
  279. FST_CORE_END()