OptimizeCal.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. #include "OptimizeCal.h"
  2. const unsigned char icvSaturate8u_cv[] =
  3. {
  4. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  5. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  6. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  7. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  8. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  9. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  10. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  11. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  12. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  13. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  14. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  15. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  16. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  17. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  18. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  19. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  20. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
  21. 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
  22. 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
  23. 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
  24. 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
  25. 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95,
  26. 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111,
  27. 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127,
  28. 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143,
  29. 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159,
  30. 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175,
  31. 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191,
  32. 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207,
  33. 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223,
  34. 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239,
  35. 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255,
  36. 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  37. 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  38. 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  39. 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  40. 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  41. 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  42. 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  43. 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  44. 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  45. 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  46. 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  47. 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  48. 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  49. 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  50. 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  51. 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  52. 255
  53. };
  54. void FastCopy(unsigned char* pSrc, unsigned char* pDst, int nWidth)
  55. {
  56. //set a _mm256 to save the src Data Width
  57. while (nWidth > 0)
  58. {
  59. __m256i srcData = _mm256_loadu_si256((__m256i*)pSrc);
  60. __m256i src2Data = _mm256_loadu_si256((__m256i*)(pSrc + 32));
  61. __m256i* pDstData = (__m256i*)pDst;
  62. __m256i* pDst2Data = (__m256i*)(pDst + 32);
  63. _mm256_storeu_si256(pDstData, srcData);
  64. _mm256_storeu_si256(pDst2Data, src2Data);
  65. pSrc += 64;
  66. pDst += 64;
  67. nWidth -= 64;
  68. }
  69. }
  70. void distanceATS_L1_8u(unsigned char* const src, int nWidth, int nHeight, int nPitch, unsigned char* dst)
  71. {
  72. int width = nWidth, height = nHeight;
  73. int a;
  74. unsigned char lut[256];
  75. int x, y;
  76. const unsigned char* sbase = src;
  77. unsigned char* dbase = dst;
  78. int srcstep = (int)nPitch;
  79. int dststep = (int)nPitch;
  80. ////////////////////// forward scan ////////////////////////
  81. for (x = 0; x < 256; x++)
  82. lut[x] = cv::saturate_cast<uchar>(x + 1);
  83. //init first pixel to max (we're going to be skipping it)
  84. dbase[0] = (unsigned char)(sbase[0] == 0 ? 0 : 255);
  85. //first row (scan west only, skip first pixel)
  86. for (x = 1; x < width; x++)
  87. dbase[x] = (unsigned char)(sbase[x] == 0 ? 0 : lut[dbase[x - 1]]);
  88. for (y = 1; y < height; y++)
  89. {
  90. sbase += srcstep;
  91. dbase += dststep;
  92. //for left edge, scan north only
  93. a = sbase[0] == 0 ? 0 : lut[dbase[-dststep]];
  94. dbase[0] = (unsigned char)a;
  95. for (x = 1; x < width; x++)
  96. {
  97. a = sbase[x] == 0 ? 0 : lut[std::min(a, (int)dbase[x - dststep])];
  98. dbase[x] = (unsigned char)a;
  99. }
  100. }
  101. ////////////////////// backward scan ///////////////////////
  102. a = dbase[width - 1];
  103. // do last row east pixel scan here (skip bottom right pixel)
  104. for (x = width - 2; x >= 0; x--)
  105. {
  106. a = lut[a];
  107. dbase[x] = (unsigned char)(CV_CALC_MIN_8U(a, dbase[x]));
  108. }
  109. // right edge is the only error case
  110. for (y = height - 2; y >= 0; y--)
  111. {
  112. dbase -= dststep;
  113. // do right edge
  114. a = lut[dbase[width - 1 + dststep]];
  115. a = dbase[width - 1] = (unsigned char)(std::min(a,(int)dbase[width - 1]));
  116. for (x = width - 2; x >= 0; x--)
  117. {
  118. int b = dbase[x + dststep];
  119. a = lut[std::min(a, b)];
  120. a = std::min(a, (int)dbase[x]);
  121. dbase[x] = (unsigned char)(a);
  122. }
  123. }
  124. }
  125. void FastCopy_Any(const unsigned char* src_ptr, unsigned char* dst_ptr, int width)
  126. {
  127. SIMD_ALIGNED(unsigned char vin[128]);
  128. SIMD_ALIGNED(unsigned char vout[128]);
  129. memset(vin, 0, sizeof(vin)); /* for YUY2 and msan */
  130. int r = width & 63;
  131. int n = width & ~63;
  132. if (n > 0) {
  133. FastCopy((unsigned char*)src_ptr, dst_ptr, n);
  134. }
  135. memcpy(vin, src_ptr + (n >> 0) * 1, SS(r, 0) * 1);
  136. FastCopy(vin, vout, 63 + 1);
  137. memcpy(dst_ptr + n * 1, vout, r * 1);
  138. }
  139. void OptimizeCalMatAdd_8U(unsigned char* pRelated, int nRelatedWidth, int nRelatedHeight, int nRelatedPitch,
  140. unsigned char* pTarget, int nTargetWidth, int nTargetHeight, int nTargetPitch,
  141. int nLeft, int nTop,
  142. int nChannel
  143. )
  144. {
  145. int nCap = 256 / (8 * nChannel);
  146. unsigned char* pTargetPart = pTarget + nTop * nTargetPitch + nLeft;
  147. __m256i mmRelate,mmTarget;
  148. __m256i mmResult;
  149. for (int i = 0; i < nRelatedHeight; i++)
  150. {
  151. for (int wid = 0; wid < nRelatedWidth; wid+= nCap)
  152. {
  153. if (nRelatedWidth - wid < nCap)
  154. {
  155. int nlast = nRelatedWidth - wid;
  156. memcpy(mmRelate.m256i_u8, pRelated + i * nRelatedPitch + wid * nChannel, nlast * nChannel);
  157. memcpy(mmTarget.m256i_u8, pTargetPart + i * nTargetPitch + wid * nChannel, nlast * nChannel);
  158. }
  159. else
  160. {
  161. _mm256_loadu_epi8(pTargetPart + i * nTargetPitch + wid * nChannel);
  162. _mm256_loadu_epi8(pRelated + i * nRelatedPitch + wid * nChannel);
  163. }
  164. mmResult = _mm256_adds_epu8(mmRelate, mmTarget);
  165. if (nRelatedWidth - wid < nCap)
  166. {
  167. int nlast = nRelatedWidth - wid;
  168. memcpy(pTargetPart + i * nTargetPitch + wid* nChannel, mmResult.m256i_u8, nlast * nChannel);
  169. }
  170. else
  171. _mm256_storeu_epi8(pTargetPart + i * nTargetPitch + wid* nChannel, mmResult);
  172. }
  173. }
  174. }