main.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. #ifdef _WIN64
  2. #include "../tools/staticlib/ipp_m7.h"
  3. #else
  4. #include "../tools/staticlib/ipp_px.h"
  5. #endif
  6. #include <ippi.h>
  7. #include <ipps.h>
  8. #include <ippcc.h>
  9. #include "image.h"
  10. #include "iir.h"
  11. #include "resize.h"
  12. #include "alpha.h"
  13. #include "fft.h"
  14. #include "window.h"
  15. #include "pcm.h"
  16. #include "stats.h"
  17. /* naming convension stuff:
  18. nsutil_<area>_FunctionName_variant()
  19. U8 - uint8_t data
  20. destination pointer comes first
  21. */
  22. int nsutil_image_CopyFlipped_U8(uint8_t *destination_image, size_t destination_stride, const uint8_t *source_image, size_t source_stride, uint32_t width, uint32_t height)
  23. {
  24. IppiSize roi = { width, height };
  25. ippiMirror_8u_C1R(source_image, source_stride, destination_image, destination_stride, roi, ippAxsHorizontal);
  26. return 0;
  27. }
  28. int nsutil_image_Copy_U8(uint8_t *destination_image, size_t destination_stride, const uint8_t *source_image, size_t source_stride, uint32_t width, uint32_t height)
  29. {
  30. IppiSize roi = { width, height };
  31. ippiCopy_8u_C1R(source_image, source_stride, destination_image, destination_stride, roi);
  32. return 0;
  33. }
  34. int nsutil_image_Convert_RGB24_RGB32(RGB32 *destination_image, size_t destination_stride /* bytes! */, const uint8_t *source_image, size_t source_stride /* bytes! */, uint32_t width, uint32_t height)
  35. {
  36. IppiSize roi = { width, height };
  37. ippiCopy_8u_C3AC4R(source_image, source_stride, (Ipp8u *)destination_image, destination_stride, roi);
  38. return 0;
  39. }
  40. int nsutil_image_ConvertFlipped_RGB24_RGB32(RGB32 *destination_image, size_t destination_stride /* bytes! */, const uint8_t *source_image, size_t source_stride /* bytes! */, uint32_t width, uint32_t height)
  41. {
  42. #if 1
  43. IppiSize roi = { width, height };
  44. ippiCopy_8u_C3AC4R(source_image, source_stride, (Ipp8u *)destination_image + destination_stride*(height-1), -destination_stride, roi);
  45. return 0;
  46. #else
  47. IppiSize roi = { width, 1 };
  48. uint8_t *dest = (uint8_t *)destination_image;
  49. source_image += source_stride * (height-1);
  50. for (uint32_t i = 0;i != height;i++)
  51. {
  52. ippiCopy_8u_C3AC4R(source_image, source_stride, (Ipp8u *)dest, destination_stride, roi);
  53. source_image -= source_stride;
  54. dest += destination_stride;
  55. }
  56. return 0;
  57. #endif
  58. }
  59. /*
  60. struct nsutil_resize_context
  61. {
  62. int buffer_size;
  63. void *buffer;
  64. };
  65. */
  66. static inline void MakeIppiRect(IppiRect &ippi_rect, const nsutil_rect *rect)
  67. {
  68. ippi_rect.x = rect->left;
  69. ippi_rect.y = rect->top;
  70. ippi_rect.width = rect->right - rect->left;
  71. ippi_rect.height = rect->bottom - rect->top;
  72. }
  73. /*
  74. int nsutil_resize_Init_RGB(nsutil_resize_t *_context, const nsutil_rect *destination_rect, const nsutil_rect *source_rect, int resize_algorithm)
  75. {
  76. nsutil_resize_context *context = 0;
  77. if (_context)
  78. {
  79. context = (nsutil_resize_context *)(*_context);
  80. }
  81. if (!context)
  82. {
  83. context = (nsutil_resize_context *)calloc(1, sizeof(nsutil_resize_context));
  84. }
  85. IppiRect srcRoi, dstRoi;
  86. MakeIppiRect(srcRoi, source_rect);
  87. MakeIppiRect(dstRoi, destination_rect);
  88. int buffer_size=0;
  89. ippiResizeGetBufSize(srcRoi, dstRoi, 1, resize_algorithm, &buffer_size);
  90. if (buffer_size > context->buffer_size)
  91. {
  92. _aligned_free(context->buffer);
  93. context->buffer = _aligned_malloc(buffer_size, 32);
  94. context->buffer_size = buffer_size;
  95. }
  96. return 0;
  97. }
  98. int nsutil_resize_Filter_RGB(nsutil_resize_t _context, void *destination, size_t destination_stride, const nsutil_rect *destination_rect,
  99. const void *source, size_t source_stride, int source_width, int source_height, const nsutil_rect *source_rect,
  100. double dx, double dy, double x_offset, double y_offset, int resize_algorithm)
  101. {
  102. nsutil_resize_context *context = (nsutil_resize_context *)_context;
  103. IppiSize srcSize = { source_width, source_height };
  104. IppiRect srcRoi, dstRoi;
  105. MakeIppiRect(srcRoi, source_rect);
  106. MakeIppiRect(dstRoi, destination_rect);
  107. ippiResizeSqrPixel_8u_C1R((const Ipp8u *)source, srcSize, source_stride, srcRoi,
  108. (Ipp8u *)destination, destination_stride, dstRoi,
  109. dx, dy, x_offset, y_offset,
  110. resize_algorithm, (Ipp8u *)context->buffer);
  111. return 0;
  112. }
  113. */
  114. int nsutil_alpha_Premultiply_RGB32(void *image, size_t image_stride, int width, int height)
  115. {
  116. IppiSize roiSize = { width, height };
  117. ippiAlphaPremul_8u_AC4IR((Ipp8u *)image, image_stride, roiSize);
  118. return 0;
  119. }
  120. int nsutil_alpha_PremultiplyValue_RGB8(void *image, size_t image_stride, int width, int height, uint8_t alpha)
  121. {
  122. IppiSize roiSize = { width, height };
  123. ippiAlphaPremulC_8u_C1IR(alpha, (Ipp8u *)image, image_stride, roiSize);
  124. return 0;
  125. }
  126. struct nsutil_fft_struct_F32R
  127. {
  128. IppsFFTSpec_R_32f *fft_spec;
  129. Ipp8u *work_buffer;
  130. };
  131. int nsutil_fft_Create_F32R(nsutil_fft_t *fft, int order, int accuracy)
  132. {
  133. nsutil_fft_struct_F32R *ippi_fft = (nsutil_fft_struct_F32R *)calloc(1, sizeof(nsutil_fft_struct_F32R));
  134. ippsFFTInitAlloc_R_32f(&ippi_fft->fft_spec, order, IPP_FFT_NODIV_BY_ANY, (IppHintAlgorithm)accuracy);
  135. int work_buffer_size;
  136. ippsFFTGetBufSize_R_32f(ippi_fft->fft_spec, &work_buffer_size);
  137. ippi_fft->work_buffer = (Ipp8u *)_aligned_malloc(work_buffer_size, 32);
  138. *fft = ippi_fft;
  139. return 0;
  140. }
  141. int nsutil_fft_Forward_F32R_IP(nsutil_fft_t fft, float *signal)
  142. {
  143. nsutil_fft_struct_F32R *ippi_fft = (nsutil_fft_struct_F32R *)fft;
  144. ippsFFTFwd_RToPerm_32f_I(signal, ippi_fft->fft_spec, ippi_fft->work_buffer);
  145. return 0;
  146. }
  147. int nsutil_fft_Destroy_F32R(nsutil_fft_t fft)
  148. {
  149. nsutil_fft_struct_F32R *ippi_fft = (nsutil_fft_struct_F32R *)fft;
  150. ippsFFTFree_R_32f(ippi_fft->fft_spec);
  151. _aligned_free(ippi_fft->work_buffer);
  152. free(ippi_fft);
  153. return 0;
  154. }
  155. int nsutil_window_Hann_F32_IP(float *signal, size_t number_of_samples)
  156. {
  157. ippsWinHann_32f_I(signal, number_of_samples);
  158. return 0;
  159. }
  160. int nsutil_window_FillHann_F32_IP(float *window, size_t number_of_samples)
  161. {
  162. ippsSet_32f(1.0f, window, number_of_samples);
  163. ippsWinHann_32f_I(window, number_of_samples);
  164. return 0;
  165. }
  166. int nsutil_window_FillKaiser_F32_IP(float *window, float alpha, size_t number_of_samples)
  167. {
  168. ippsSet_32f(1.0f, window, number_of_samples);
  169. ippsWinKaiser_32f_I(window, number_of_samples, alpha);
  170. return 0;
  171. }
  172. int nsutil_window_Multiply_F32_IP(float *signal, const float *window, size_t number_of_samples)
  173. {
  174. ippsMul_32f_I(window, signal, number_of_samples);
  175. return 0;
  176. }
  177. int nsutil_image_Recolor_RGB32(RGB32 *destination_image, size_t destination_stride /* bytes! */, uint32_t R, uint32_t G, uint32_t B, uint32_t width, uint32_t height)
  178. {
  179. const Ipp32f twist[3][4] =
  180. {
  181. {(float)B / 65536.0f, 0, 0, 0},
  182. {0, (float)G / 65536.0f, 0, 0},
  183. {0, 0, (float)R / 65536.0f, 0}
  184. };
  185. IppiSize roiSize = { width, height };
  186. ippiColorTwist32f_8u_AC4IR((Ipp8u *)destination_image, destination_stride, roiSize, twist);
  187. return 0;
  188. }
  189. int nsutil_image_Palette_RGB32(RGB32 *destination_image, size_t destination_stride /* bytes! */, const uint8_t *source_image, size_t source_stride /* bytes! */, uint32_t width, uint32_t height, const RGB32 *palette)
  190. {
  191. IppiSize roiSize = { width, height };
  192. ippiLUTPalette_8u32u_C1R(source_image, source_stride, (Ipp32u *)destination_image, destination_stride, roiSize, (const Ipp32u *)palette, 8);
  193. return 0;
  194. }
  195. int nsutil_image_PaletteFlipped_RGB32(RGB32 *destination_image, size_t destination_stride /* bytes! */, const uint8_t *source_image, size_t source_stride /* bytes! */, uint32_t width, uint32_t height, const RGB32 *palette)
  196. {
  197. #if 1
  198. IppiSize roiSize = { width, height };
  199. ippiLUTPalette_8u32u_C1R(source_image, source_stride, (Ipp32u *) ((uint8_t *)destination_image + destination_stride*(height-1)), -destination_stride, roiSize, (const Ipp32u *)palette, 8);
  200. return 0;
  201. #else
  202. IppiSize roiSize = { width, 1 };
  203. uint8_t *dest = (uint8_t *)destination_image;
  204. source_image += source_stride * (height-1);
  205. for (uint32_t i = 0;i != height;i++)
  206. {
  207. ippiLUTPalette_8u32u_C1R(source_image, source_stride, (Ipp32u *)dest, destination_stride, roiSize, (const Ipp32u *)palette, 8);
  208. source_image -= source_stride;
  209. dest += destination_stride;
  210. }
  211. return 0;
  212. #endif
  213. }
  214. int nsutil_image_FillRectAlpha_RGB32(RGB32 *destination_image, size_t destination_stride /* bytes! */, uint32_t width, uint32_t height, RGB32 color, int alpha)
  215. {
  216. IppiSize roiSize = { width, height };
  217. if (alpha == 255)
  218. {
  219. uint8_t c[3] = {
  220. (color&0xFF0000) >> 16,
  221. (color&0xFF00) >> 8,
  222. (color&0xFF)
  223. };
  224. ippiSet_8u_AC4R(c, (uint8_t *)destination_image, destination_stride, roiSize);
  225. }
  226. else
  227. {
  228. uint8_t c[3] = {
  229. ((color&0xFF0000) * alpha / 255) >> 16,
  230. ((color&0xFF00) * alpha / 255) >> 8,
  231. ((color&0xFF) * alpha / 255)
  232. };
  233. ippiAlphaPremulC_8u_AC4IR((255-alpha), (uint8_t *)destination_image, destination_stride, roiSize);
  234. ippiAddC_8u_AC4IRSfs(c, (uint8_t *)destination_image, destination_stride, roiSize, 0);
  235. }
  236. return 0;
  237. }
  238. int nsutil_stats_RMS_F32(const float *buffer, size_t num_samples, float *rms)
  239. {
  240. ippsNorm_L2_32f(buffer, num_samples, rms);
  241. return 0;
  242. }
  243. int nsutil_image_Convert_YUV420_RGB32(RGB32 *destination_image, size_t destination_stride /* bytes! */, uint32_t width, uint32_t height, const uint8_t *planes[3], const size_t strides[3])
  244. {
  245. IppiSize roiSize = { width, height };
  246. int int_strides[3] = {strides[0], strides[1], strides[2] };
  247. ippiYUV420ToRGB_8u_P3AC4R(planes, int_strides, (Ipp8u *)destination_image, destination_stride, roiSize);
  248. return 0;
  249. }