DeprecatedConverter.cpp 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149
  1. #include "core/image/DeprecatedConverter.h"
  2. #include <math.h>
  3. #include <limits>
  4. #include <core/basics/numerictools.h>
  5. #include <sstream>
  6. #include <iostream>
  7. #include <core/basics/Log.h>
  8. using namespace std;
  9. namespace NICE {
  10. #ifndef NICE_USELIB_IPP
  11. // lookup tables for rgb -> gray colour transform
  12. bool DeprecatedConverter::pLut_initialized=false;
  13. int DeprecatedConverter::pLutRg[256];
  14. int DeprecatedConverter::pLutGg[256];
  15. int DeprecatedConverter::pLutBg[256];
  16. #endif
  17. ColorImage* DeprecatedConverter::hsvToRGB(
  18. const HSVColorImage& hsv, ColorImage* rgb)
  19. {
  20. #ifdef NICE_USELIB_IPP
  21. ColorImage* result = createResultBuffer(hsv, rgb);
  22. IppStatus ret = ippiHSVToRGB_8u_C3R(hsv.getPixelPointer(),
  23. hsv.getStepsize(),
  24. result->getPixelPointer(),
  25. result->getStepsize(),
  26. makeROIFullImage(hsv));
  27. if(ret!=ippStsNoErr)
  28. fthrow(ImageException, ippGetStatusString(ret));
  29. return result;
  30. #else // NICE_USELIB_IPP
  31. fthrow(ImageException,"Not yet supported without IPP.");
  32. #endif // NICE_USELIB_IPP
  33. }
  34. HSVColorImage* DeprecatedConverter::rgbToHSV(
  35. const ColorImage& rgb, HSVColorImage* hsv)
  36. {
  37. #ifdef NICE_USELIB_IPP
  38. HSVColorImage* result = createResultBuffer(rgb, hsv);
  39. IppStatus ret = ippiRGBToHSV_8u_C3R(rgb.getPixelPointer(),
  40. rgb.getStepsize(),
  41. result->getPixelPointer(),
  42. result->getStepsize(),
  43. makeROIFullImage(rgb));
  44. if(ret!=ippStsNoErr)
  45. fthrow(ImageException, ippGetStatusString(ret));
  46. return result;
  47. #else // NICE_USELIB_IPP
  48. fthrow(ImageException,"Not yet supported without IPP.");
  49. #endif // NICE_USELIB_IPP
  50. }
  51. HSVColorImage* DeprecatedConverter::rgbToHSVRegion(const ColorImage& rgb,
  52. HSVColorImage* hsv,
  53. const Rect rect) {
  54. #ifdef NICE_USELIB_IPP
  55. Rect roi = clipRect(rgb, rect);
  56. IppiSize ippiSize = makeROIRect(rgb, rect);
  57. preventIppBug(ippiSize, roi);
  58. HSVColorImage* result = createResultBuffer(rgb, hsv);
  59. if (ippiSize.width == 0 || ippiSize.height == 0) {
  60. return result;
  61. }
  62. Ipp8u const* src = rgb.getPixelPointerXY(roi.left, roi.top);
  63. Ipp8u* dest = result->getPixelPointerXY(roi.left, roi.top);
  64. IppStatus ret = ippiRGBToHSV_8u_C3R(src, rgb.getStepsize(),
  65. dest, result->getStepsize(),
  66. ippiSize);
  67. if(ret!=ippStsNoErr)
  68. fthrow(ImageException, ippGetStatusString(ret));
  69. return result;
  70. #else // NICE_USELIB_IPP
  71. fthrow(ImageException,"Not yet supported without IPP.");
  72. #endif // NICE_USELIB_IPP
  73. }
  74. FloatImage* DeprecatedConverter::gray16sToFloat(const GrayImage16s& gray,
  75. FloatImage* fimage)
  76. {
  77. #ifdef NICE_USELIB_IPP
  78. FloatImage* result = createResultBuffer(gray, fimage);
  79. IppStatus ret = ippiConvert_16s32f_C1R(gray.getPixelPointer(),
  80. gray.getStepsize(),
  81. result->getPixelPointer(),
  82. result->getStepsize(),
  83. makeROIFullImage(gray));
  84. if(ret!=ippStsNoErr)
  85. fthrow(ImageException, ippGetStatusString(ret));
  86. return result;
  87. #else // NICE_USELIB_IPP
  88. fthrow(ImageException,"Not yet supported without IPP.");
  89. #endif // NICE_USELIB_IPP
  90. }
  91. FloatImage* DeprecatedConverter::rgbToFloat(const ColorImage& rgb,
  92. FloatImage* fimage)
  93. {
  94. #ifdef NICE_USELIB_IPP
  95. FloatImage* result = createResultBuffer(rgb.width()*3,rgb.height(), fimage);
  96. IppStatus ret = ippiConvert_8u32f_C3R(rgb.getPixelPointer(),
  97. rgb.getStepsize(),
  98. result->getPixelPointer(),
  99. result->getStepsize(),
  100. makeROIFullImage(rgb));
  101. if(ret!=ippStsNoErr)
  102. fthrow(ImageException, ippGetStatusString(ret));
  103. return result;
  104. #else // NICE_USELIB_IPP
  105. fthrow(ImageException,"Not yet supported without IPP.");
  106. #endif // NICE_USELIB_IPP
  107. }
  108. void DeprecatedConverter::set(const unsigned char& value, Image* dst)
  109. {
  110. #ifdef NICE_USELIB_IPP
  111. IppStatus ret = ippiSet_8u_C1R(value, dst->getPixelPointer(), dst->getStepsize(), makeROIFullImage(*dst));
  112. if(ret!=ippStsNoErr)
  113. fthrow(ImageException, ippGetStatusString(ret));
  114. #else // NICE_USELIB_IPP
  115. fthrow(ImageException,"Not yet supported without IPP.");
  116. #endif // NICE_USELIB_IPP
  117. }
  118. Image* DeprecatedConverter::remap(const Image& src, const FloatImage &px, const FloatImage &py, Image* dst, int interpolation)
  119. {
  120. #ifdef NICE_USELIB_IPP
  121. Image gi (src);
  122. *(gi.getPixelPointerY(0)) = 0;
  123. Image * result = createResultBuffer(src.width(), src.height(), dst);
  124. IppStatus ret = ippiRemap_8u_C1R(gi.getPixelPointer(), makeROIFullImage(src), gi.getStepsize(), makeRectFullImage(gi),
  125. px.getPixelPointer(), px.getStepsize(), py.getPixelPointer(), py.getStepsize(),
  126. result->getPixelPointer(), result->getStepsize(), makeROIFullImage(gi), interpolation);
  127. if(ret!=ippStsNoErr)
  128. fthrow(ImageException, ippGetStatusString(ret));
  129. return result;
  130. #else // NICE_USELIB_IPP
  131. fthrow(ImageException,"Not yet supported without IPP.");
  132. #endif // NICE_USELIB_IPP
  133. }
  134. FloatImage* DeprecatedConverter::gradientStrength(const GrayImage16s& dx, const GrayImage16s& dy, FloatImage *buffer)
  135. {
  136. #ifdef NICE_USELIB_IPP
  137. if( dx.width()!=dy.width() || dx.height()!=dy.height() )
  138. fthrow(ImageException,"DeprecatedConverter::gradientStrength input images must have same size.");
  139. FloatImage *result = createResultBuffer(dx.width(), dx.height(), buffer);
  140. FloatImage tmp(dx.width(),dx.height());
  141. IppiSize roi=makeROIFullImage(*result);
  142. IppStatus ret = ippiConvert_16s32f_C1R(dx.getPixelPointer(),
  143. dx.getStepsize(),
  144. result->getPixelPointer(),
  145. result->getStepsize(),
  146. roi);
  147. if(ret!=ippStsNoErr)
  148. fthrow(ImageException, ippGetStatusString(ret));
  149. ret = ippiConvert_16s32f_C1R(dy.getPixelPointer(),
  150. dy.getStepsize(),
  151. tmp.getPixelPointer(),
  152. tmp.getStepsize(),
  153. roi);
  154. if(ret!=ippStsNoErr)
  155. fthrow(ImageException, ippGetStatusString(ret));
  156. ret = ippiSqr_32f_C1IR(result->getPixelPointer(),
  157. result->getStepsize(),
  158. roi);
  159. if(ret!=ippStsNoErr)
  160. fthrow(ImageException, ippGetStatusString(ret));
  161. ret = ippiAddSquare_32f_C1IR(tmp.getPixelPointer(),
  162. tmp.getStepsize(),
  163. result->getPixelPointer(),
  164. result->getStepsize(),
  165. roi);
  166. if(ret!=ippStsNoErr)
  167. fthrow(ImageException, ippGetStatusString(ret));
  168. ret = ippiSqrt_32f_C1IR(result->getPixelPointer(),
  169. result->getStepsize(),
  170. roi);
  171. if(ret!=ippStsNoErr)
  172. fthrow(ImageException, ippGetStatusString(ret));
  173. return result;
  174. #else // NICE_USELIB_IPP
  175. fthrow(ImageException,"Not yet supported without IPP.");
  176. #endif // NICE_USELIB_IPP
  177. }
  178. Image* DeprecatedConverter::nonMaximumSuppression(const GrayImage16s& dx, const GrayImage16s& dy, const FloatImage &mag, Image *buffer)
  179. {
  180. // #ifdef NICE_USELIB_IPP
  181. if( dx.width()!=dy.width() || dy.width() != mag.width() || dx.height()!=dy.height() || dy.height() != mag.height() )
  182. fthrow(ImageException,"DeprecatedConverter::nonMaximumSuppression input images must have same size.");
  183. int height=dx.height();
  184. int width=dx.width();
  185. Image *result = createResultBuffer(width, height, buffer);
  186. *result=0;
  187. for(int y=1;y<height-1;y++)
  188. for(int x=1;x<width-1;x++)
  189. #if 0
  190. {
  191. float m=mag.getPixelQuick(x,y);
  192. if(m==0) {
  193. result->setPixelSave(x,y,0);
  194. } else {
  195. Ipp16s gx=dx.getPixelQuick(x,y);
  196. Ipp16s gy=dy.getPixelQuick(x,y);
  197. float l1,l2,r1,r2;
  198. float mag1,mag2;
  199. float xperp = -gx/m;
  200. float yperp = gy/m;
  201. if(gx>=0) {
  202. l1 = mag.getPixelQuick(x-1,y);
  203. r1 = mag.getPixelQuick(x+1,y);
  204. if(gy>=0) {
  205. l2 = mag.getPixelQuick(x-1,y-1);
  206. r2 = mag.getPixelQuick(x+1,y+1);
  207. if (gx >= gy) { // 111
  208. // Left point
  209. mag1 = (m - l1)*xperp + (l2 - l1)*yperp;
  210. // Right point
  211. mag2 = (m - r1)*xperp + (r2 - r1)*yperp;
  212. } else { // 110
  213. // Left point
  214. mag1 = (l1 - l2)*xperp + (l1 - m)*yperp;
  215. // Right point
  216. mag2 = (r1 - r2)*xperp + (r1 - m)*yperp;
  217. }
  218. } else {
  219. l2 = mag.getPixelQuick(x-1,y+1);
  220. r2 = mag.getPixelQuick(x+1,y-1);
  221. if (gx >= -gy) { // 101
  222. // Left point
  223. mag1 = (m - l1)*xperp + (l1 - l2)*yperp;
  224. // Right point
  225. mag2 = (m - r1)*xperp + (r1 - r2)*yperp;
  226. } else { // 100
  227. // Left point
  228. mag1 = (r1 - r2)*xperp + (m - r1)*yperp;
  229. // Right point
  230. mag2 = (l1 - l2)*xperp + (m - l1)*yperp;
  231. }
  232. }
  233. } else {
  234. if (gy >= 0) {
  235. r2 = mag.getPixelQuick(x+1,y-1);
  236. l2 = mag.getPixelQuick(x-1,y+1);
  237. if (-gx >= gy) { // 011
  238. // Left point
  239. r1 = mag.getPixelQuick(x+1,y);
  240. mag1 = (r1 - m)*xperp + (r2 - r1)*yperp;
  241. // Right point
  242. l1 = mag.getPixelQuick(x-1,y);
  243. mag2 = (l1 - m)*xperp + (l2 - l1)*yperp;
  244. } else { // 010
  245. // Left point
  246. r1 = mag.getPixelQuick(x,y-1);
  247. mag1 = (r2 - r1)*xperp + (r1 - m)*yperp;
  248. // Right point
  249. l1 = mag.getPixelQuick(x,y+1);
  250. mag2 = (l2 - l1)*xperp + (l1 - m)*yperp;
  251. }
  252. } else {
  253. r2 = mag.getPixelQuick(x+1,y+1);
  254. l2 = mag.getPixelQuick(x-1,y-1);
  255. if (-gx > -gy) { // 001
  256. // Left point
  257. r1 = mag.getPixelQuick(x+1,y);
  258. mag1 = (r1 - m)*xperp + (r1 - r2)*yperp;
  259. // Right point
  260. l1 = mag.getPixelQuick(x-1,y);
  261. mag2 = (l1 - m)*xperp + (l1 - l2)*yperp;
  262. } else { // 000
  263. // Left point
  264. r1 = mag.getPixelQuick(x,y+1);
  265. mag1 = (r2 - r1)*xperp + (m - r1)*yperp;
  266. // Right point
  267. l1 = mag.getPixelQuick(x,y-1);
  268. mag2 = (l2 - l1)*xperp + (m - l1)*yperp;
  269. }
  270. }
  271. }
  272. if ((mag1 >= 0.0) || (mag2 > 0.0))
  273. result->setPixelQuick(x,y,0);
  274. else
  275. result->setPixelQuick(x,y,255);
  276. }
  277. }
  278. #else
  279. {
  280. if(x==17 && y==17) {
  281. cout << mag.getPixelQuick(x,y) << " " << dx.getPixelQuick(x,y) << " " << dy.getPixelQuick(x,y) << endl;
  282. cout << mag.getPixelQuick(x-1,y-1) << " " << mag.getPixelQuick(x,y-1) << " " << mag.getPixelQuick(x+1,y-1) << endl;
  283. cout << mag.getPixelQuick(x-1,y) << " " << mag.getPixelQuick(x,y) << " " << mag.getPixelQuick(x+1,y) << endl;
  284. cout << mag.getPixelQuick(x-1,y+1) << " " << mag.getPixelQuick(x,y+1) << " " << mag.getPixelQuick(x+1,y+1) << endl;
  285. }
  286. float m=mag.getPixelQuick(x,y);
  287. if(isZero(m)) {
  288. result->setPixelSave(x,y,0);
  289. } else {
  290. Ipp16s gx=dx.getPixelQuick(x,y);
  291. Ipp16s gy=dy.getPixelQuick(x,y);
  292. float mag1,mag2, d;
  293. if((gy<=0 && gx>-gy) || (gy>=0 && gx<-gy)) {
  294. d = abs(gy/gx);
  295. mag1 = mag.getPixelQuick(x,y+1)*(1-d) + mag.getPixelQuick(x-1,y+1)*d;
  296. mag2 = mag.getPixelQuick(x,y-1)*(1-d) + mag.getPixelQuick(x+1,y-1)*d;
  297. } else if((gx>0 && -gy>=gx) || (gx<0 && -gy<=gx)) {
  298. d = abs(gx/gy);
  299. mag1 = mag.getPixelQuick(x-1,y)*(1-d) + mag.getPixelQuick(x-1,y+1)*d;
  300. mag2 = mag.getPixelQuick(x+1,y)*(1-d) + mag.getPixelQuick(x+1,y-1)*d;
  301. } else if((gx<=0 && gx>gy) || (gx>=0 && gx<gy)) {
  302. d = abs(gx/gy);
  303. mag1 = mag.getPixelQuick(x-1,y)*(1-d) + mag.getPixelQuick(x-1,y-1)*d;
  304. mag2 = mag.getPixelQuick(x+1,y)*(1-d) + mag.getPixelQuick(x+1,y+1)*d;
  305. } else if((gy<0 && gx<=gy) || (gy>0 && gx>=gy)) {
  306. d = abs(gy/gx);
  307. mag1 = mag.getPixelQuick(x,y-1)*(1-d) + mag.getPixelQuick(x-1,y-1)*d;
  308. mag2 = mag.getPixelQuick(x,y+1)*(1-d) + mag.getPixelQuick(x+1,y+1)*d;
  309. } else {
  310. result->setPixelQuick(x,y,0);
  311. continue;
  312. }
  313. if(m>=mag1 && m>=mag2) {
  314. result->setPixelQuick(x,y,255);
  315. } else {
  316. result->setPixelQuick(x,y,0);
  317. }
  318. }
  319. }
  320. #endif
  321. return result;
  322. /*
  323. #else // NICE_USELIB_IPP
  324. fthrow(ImageException,"Not yet supported without IPP.");
  325. #endif // NICE_USELIB_IPP
  326. */
  327. }
  328. FloatImage* DeprecatedConverter::convolution(const FloatImage& src, const FloatImage &kernel, FloatImage* dst)
  329. {
  330. #ifdef NICE_USELIB_IPP
  331. FloatImage* result = createResultBuffer(abs(src.width()-kernel.width())+1,
  332. abs(src.height()-kernel.height())+1,
  333. dst);
  334. IppStatus ret = ippiConvValid_32f_C1R(src.getPixelPointer(),
  335. src.getStepsize(),
  336. makeROIFullImage(src),
  337. kernel.getPixelPointer(),
  338. kernel.getStepsize(),
  339. makeROIFullImage(kernel),
  340. result->getPixelPointer(),
  341. result->getStepsize());
  342. if(ret!=ippStsNoErr)
  343. fthrow(ImageException, ippGetStatusString(ret));
  344. return result;
  345. #else // NICE_USELIB_IPP
  346. fthrow(ImageException,"Not yet supported without IPP.");
  347. #endif // NICE_USELIB_IPP
  348. }
  349. ColorImage* DeprecatedConverter::remap(const ColorImage& src, const FloatImage &px, const FloatImage &py, ColorImage* dst, int interpolation)
  350. {
  351. #ifdef NICE_USELIB_IPP
  352. ColorImage::Pixel *cursor;
  353. ColorImage ci (src);
  354. cursor = ci.getPixelPointerY(0);
  355. *cursor = 0; cursor++;
  356. *cursor = 0; cursor++;
  357. *cursor = 0;
  358. ColorImage * result = createResultBuffer(ci.width(), ci.height(), dst);
  359. IppStatus ret = ippiRemap_8u_C3R(ci.getPixelPointer(), makeROIFullImage(ci), ci.getStepsize(), makeRectFullImage(ci),
  360. px.getPixelPointer(), px.getStepsize(), py.getPixelPointer(), py.getStepsize(),
  361. result->getPixelPointer(), result->getStepsize(), makeROIFullImage(*result), interpolation);
  362. if(ret!=ippStsNoErr)
  363. fthrow(ImageException, ippGetStatusString(ret));
  364. return result;
  365. #else // NICE_USELIB_IPP
  366. fthrow(ImageException,"Not yet supported without IPP.");
  367. #endif // NICE_USELIB_IPP
  368. }
  369. Image* DeprecatedConverter::blur(const Image& src, const int& size, Image* dst)
  370. {
  371. #ifdef NICE_USELIB_IPP
  372. Image * result = createResultBuffer(src.width(), src.height(), dst);
  373. IppiSize maskSize;
  374. maskSize.width = size;
  375. maskSize.height = size;
  376. IppiPoint anchor;
  377. anchor.x = (int)(size/2);
  378. anchor.y = (int)(size/2);
  379. IppiSize imageSize;
  380. imageSize.width = dst->width()-size;
  381. imageSize.height = dst->height()-size;
  382. IppStatus ret = ippiFilterBox_8u_C1R(src.getPixelPointerXY(anchor.x,anchor.y), src.getStepsize(),
  383. result->getPixelPointerXY(anchor.x,anchor.y), result->getStepsize(),
  384. imageSize, maskSize, anchor);
  385. if(ret!=ippStsNoErr)
  386. fthrow(ImageException, ippGetStatusString(ret));
  387. return result;
  388. #else // NICE_USELIB_IPP
  389. fthrow(ImageException,"Not yet supported without IPP.");
  390. #endif // NICE_USELIB_IPP
  391. }
  392. Image* DeprecatedConverter::median(const Image& src, const int& size, Image* dst)
  393. {
  394. #ifdef NICE_USELIB_IPP
  395. Image * result = createResultBuffer(src.width(), src.height(), dst);
  396. IppiSize maskSize;
  397. maskSize.width = size;
  398. maskSize.height = size;
  399. IppiPoint anchor;
  400. anchor.x = (int)(size/2);
  401. anchor.y = (int)(size/2);
  402. IppiSize imageSize;
  403. imageSize.width = dst->width()-size;
  404. imageSize.height = dst->height()-size;
  405. IppStatus ret = ippiFilterMedian_8u_C1R(src.getPixelPointerXY(anchor.x,anchor.y), src.getStepsize(),
  406. result->getPixelPointerXY(anchor.x,anchor.y), result->getStepsize(),
  407. imageSize, maskSize, anchor);
  408. if(ret!=ippStsNoErr)
  409. fthrow(ImageException, ippGetStatusString(ret));
  410. return result;
  411. #else // NICE_USELIB_IPP
  412. fthrow(ImageException,"Not yet supported without IPP.");
  413. #endif // NICE_USELIB_IPP
  414. }
  415. void DeprecatedConverter::thresholdIP(Image& img, int threshold)
  416. {
  417. #ifdef NICE_USELIB_IPP
  418. IppStatus ret = ippiThreshold_Val_8u_C1IR(img.getPixelPointer(), img.getStepsize(), makeROIFullImage(img), threshold, 0, ippCmpLess);
  419. if(ret!=ippStsNoErr)
  420. fthrow(ImageException, ippGetStatusString(ret));
  421. ret = ippiThreshold_Val_8u_C1IR(img.getPixelPointer(), img.getStepsize(), makeROIFullImage(img), threshold, 255, ippCmpGreater);
  422. if(ret!=ippStsNoErr)
  423. fthrow(ImageException, ippGetStatusString(ret));
  424. #else // NICE_USELIB_IPP
  425. fthrow(ImageException,"Not yet supported without IPP.");
  426. #endif // NICE_USELIB_IPP
  427. }
  428. void DeprecatedConverter::lowerThresholdIP(Image& img, int threshold, int value)
  429. {
  430. #ifdef NICE_USELIB_IPP
  431. IppStatus ret = ippiThreshold_Val_8u_C1IR(img.getPixelPointer(), img.getStepsize(), makeROIFullImage(img), threshold, value, ippCmpLess);
  432. if(ret!=ippStsNoErr)
  433. fthrow(ImageException, ippGetStatusString(ret));
  434. #else // NICE_USELIB_IPP
  435. fthrow(ImageException,"Not yet supported without IPP.");
  436. #endif // NICE_USELIB_IPP
  437. }
  438. void DeprecatedConverter::upperThresholdIP(Image& img, int threshold, int value)
  439. {
  440. #ifdef NICE_USELIB_IPP
  441. IppStatus ret = ippiThreshold_Val_8u_C1IR(img.getPixelPointer(), img.getStepsize(), makeROIFullImage(img), threshold, value, ippCmpGreater);
  442. if(ret!=ippStsNoErr)
  443. fthrow(ImageException, ippGetStatusString(ret));
  444. #else // NICE_USELIB_IPP
  445. fthrow(ImageException,"Not yet supported without IPP.");
  446. #endif // NICE_USELIB_IPP
  447. }
  448. Image* DeprecatedConverter::erode(const Image& src, Image* dst)
  449. {
  450. #ifdef NICE_USELIB_IPP
  451. IppiSize maskSize;
  452. maskSize.width = src.width()-2;
  453. maskSize.height = src.height()-2;
  454. Image * result = createResultBuffer(src.width(), src.height(), dst);
  455. IppStatus ret = ippiErode3x3_8u_C1R(src.getPixelPointerY(1)+1, src.getStepsize(),
  456. result->getPixelPointerY(1)+1, result->getStepsize(),
  457. maskSize);
  458. if(ret!=ippStsNoErr)
  459. fthrow(ImageException, ippGetStatusString(ret));
  460. return result;
  461. #else // NICE_USELIB_IPP
  462. fthrow(ImageException,"Not yet supported without IPP.");
  463. #endif // NICE_USELIB_IPP
  464. }
  465. void DeprecatedConverter::erodeIP(Image& img)
  466. {
  467. #ifdef NICE_USELIB_IPP
  468. IppiSize roiSize;
  469. roiSize.width = img.width()-2;
  470. roiSize.height = img.height()-2;
  471. IppStatus ret = ippiErode3x3_8u_C1IR(img.getPixelPointer()+img.getStepsize()+1, img.getStepsize(), roiSize);
  472. if(ret!=ippStsNoErr)
  473. fthrow(ImageException, ippGetStatusString(ret));
  474. #else // NICE_USELIB_IPP
  475. fthrow(ImageException,"Not yet supported without IPP.");
  476. #endif // NICE_USELIB_IPP
  477. }
  478. Image* DeprecatedConverter::dilate(const Image& src, Image* dst)
  479. {
  480. #ifdef NICE_USELIB_IPP
  481. IppiSize maskSize;
  482. maskSize.width = src.width()-2;
  483. maskSize.height = src.height()-2;
  484. Image * result = createResultBuffer(src.width(), src.height(), dst);
  485. IppStatus ret = ippiDilate3x3_8u_C1R(src.getPixelPointerY(1)+1, src.getStepsize(),
  486. result->getPixelPointerY(1)+1, result->getStepsize(),
  487. maskSize);
  488. if(ret!=ippStsNoErr)
  489. fthrow(ImageException, ippGetStatusString(ret));
  490. return result;
  491. #else // NICE_USELIB_IPP
  492. fthrow(ImageException,"Not yet supported without IPP.");
  493. #endif // NICE_USELIB_IPP
  494. }
  495. void DeprecatedConverter::dilateIP(Image& img)
  496. {
  497. #ifdef NICE_USELIB_IPP
  498. IppiSize roiSize;
  499. roiSize.width = img.width()-2;
  500. roiSize.height = img.height()-2;
  501. IppStatus ret = ippiDilate3x3_8u_C1IR(img.getPixelPointer()+img.getStepsize()+1, img.getStepsize(), roiSize);
  502. if(ret!=ippStsNoErr)
  503. fthrow(ImageException, ippGetStatusString(ret));
  504. #else // NICE_USELIB_IPP
  505. fthrow(ImageException,"Not yet supported without IPP.");
  506. #endif // NICE_USELIB_IPP
  507. }
  508. Image* DeprecatedConverter::absDiff(const Image& src0, const Image& src1, Image* dst)
  509. {
  510. #ifdef NICE_USELIB_IPP
  511. Image * result = createResultBuffer(src0.width(), src0.height(), dst);
  512. IppStatus ret = ippiAbsDiff_8u_C1R(src0.getPixelPointer(), src0.getStepsize(),
  513. src1.getPixelPointer(), src1.getStepsize(),
  514. result->getPixelPointer(), result->getStepsize(),
  515. makeROIFullImage(src0));
  516. if(ret!=ippStsNoErr)
  517. fthrow(ImageException, ippGetStatusString(ret));
  518. return result;
  519. #else // NICE_USELIB_IPP
  520. fthrow(ImageException,"Not yet supported without IPP.");
  521. #endif // NICE_USELIB_IPP
  522. }
  523. ColorImage* DeprecatedConverter::absDiff(const ColorImage& src0, const ColorImage& src1, ColorImage* dst)
  524. {
  525. #ifdef NICE_USELIB_IPP
  526. IppiSize ippiSize;
  527. ippiSize.width = src0.width()*3;
  528. ippiSize.height = src0.height();
  529. ColorImage * result = createResultBuffer(src0.width(), src0.height(), dst);
  530. IppStatus ret = ippiAbsDiff_8u_C1R(src0.getPixelPointer(), src0.getStepsize(),
  531. src1.getPixelPointer(), src1.getStepsize(),
  532. result->getPixelPointer(), result->getStepsize(),
  533. ippiSize);
  534. if(ret!=ippStsNoErr)
  535. fthrow(ImageException, ippGetStatusString(ret));
  536. return result;
  537. #else // NICE_USELIB_IPP
  538. fthrow(ImageException,"Not yet supported without IPP.");
  539. #endif // NICE_USELIB_IPP
  540. }
  541. Image* DeprecatedConverter::copyRect(const Image& src, const int& x0, const int& y0, const int& x1, const int& y1, Image* dst) {
  542. #ifdef NICE_USELIB_IPP
  543. int copyRectWidth = x1-x0+1;
  544. int copyRectHeight = y1-y0+1;
  545. Rect rect (x0, y0, copyRectWidth, copyRectHeight);
  546. Rect roi = clipRect(src, rect);
  547. IppiSize ippiSize = makeROIRect(src, rect);
  548. preventIppBug(ippiSize, roi);
  549. Image * result = createResultBuffer(copyRectWidth, copyRectHeight, dst);
  550. Ipp8u const* src_rect = src.getPixelPointerXY(roi.left, roi.top);
  551. IppStatus ret = ippiCopy_8u_C1R(src_rect, src.getStepsize(),
  552. result->getPixelPointer(), result->getStepsize(),
  553. ippiSize);
  554. if(ret!=ippStsNoErr)
  555. fthrow(ImageException, ippGetStatusString(ret));
  556. return result;
  557. #else // NICE_USELIB_IPP
  558. fthrow(ImageException,"Not yet supported without IPP.");
  559. #endif // NICE_USELIB_IPP
  560. }
  561. ColorImage* DeprecatedConverter::copyRect(const ColorImage& src, const int& x0, const int& y0, const int& x1, const int& y1, ColorImage* dst) {
  562. #ifdef NICE_USELIB_IPP
  563. int copyRectWidth = x1-x0+1;
  564. int copyRectHeight = y1-y0+1;
  565. Rect rect (x0, y0, copyRectWidth, copyRectHeight);
  566. Rect roi = clipRect(src, rect);
  567. IppiSize ippiSize = makeROIRect(src, rect);
  568. preventIppBug(ippiSize, roi);
  569. ColorImage * result = createResultBuffer(copyRectWidth, copyRectHeight, dst);
  570. Ipp8u const* src_rect = src.getPixelPointerXY(roi.left, roi.top);
  571. ippiCopy_8u_C3R(src_rect, src.getStepsize(),
  572. result->getPixelPointer(), result->getStepsize(),
  573. ippiSize);
  574. return result;
  575. #else // NICE_USELIB_IPP
  576. throw ImageException("Not yet supported without IPP.");
  577. #endif // NICE_USELIB_IPP
  578. }
  579. Image* DeprecatedConverter::rgbToGrayRegion(const ColorImage& rgb,
  580. Image* gray,
  581. const Rect rect) {
  582. #ifdef NICE_USELIB_IPP
  583. Rect roi = clipRect(rgb, rect);
  584. IppiSize ippiSize = makeROIRect(rgb, rect);
  585. preventIppBug(ippiSize, roi);
  586. Image* result = createResultBuffer(rgb, gray);
  587. Ipp8u const* src = rgb.getPixelPointerXY(roi.left, roi.top);
  588. Ipp8u* dest = result->getPixelPointerXY(roi.left, roi.top);
  589. if (ippiSize.width <= 0 || ippiSize.height <= 0) {
  590. return result;
  591. }
  592. IppStatus ret = ippiRGBToGray_8u_C3C1R(src, rgb.getStepsize(),
  593. dest, result->getStepsize(),
  594. ippiSize);
  595. if(ret!=ippStsNoErr) {
  596. std::stringstream s;
  597. s << ippGetStatusString(ret)
  598. << " (ippiSize: " << ippiSize.width << "x" << ippiSize.height;
  599. fthrow(ImageException, s.str());
  600. }
  601. return result;
  602. #else // NICE_USELIB_IPP
  603. fthrow(ImageException,"Not yet supported without IPP.");
  604. #endif // NICE_USELIB_IPP
  605. }
  606. ColorImage* DeprecatedConverter::floatImageToGrayRGB(const FloatImage& floatImage,
  607. ColorImage* image) {
  608. // #ifdef NICE_USELIB_IPP
  609. ColorImage* result
  610. = createResultBuffer(floatImage.width(), floatImage.height(), image);
  611. float minValue = numeric_limits<float>::infinity();
  612. float maxValue = -numeric_limits<float>::infinity();
  613. for (int y = 0; y < floatImage.height(); y++) {
  614. FloatImage::Pixel const* cursor = floatImage.getPixelPointerXY(0, y);
  615. for (int x = 0; x < floatImage.width(); x++) {
  616. float value = *cursor++;
  617. minValue = min(minValue, value);
  618. maxValue = max(maxValue, value);
  619. }
  620. }
  621. const float range = maxValue - minValue;
  622. for (int y = 0; y < floatImage.height(); y++) {
  623. FloatImage::Pixel const* cursor = floatImage.getPixelPointerXY(0, y);
  624. ColorImage::Pixel* target = result->getPixelPointerXY(0, y);
  625. for (int x = 0; x < floatImage.width(); x++) {
  626. float value = *cursor++;
  627. ColorImage::Pixel pixel
  628. = ColorImage::Pixel(round(value * 255.0 / range));
  629. *target++ = pixel;
  630. *target++ = pixel;
  631. *target++ = pixel;
  632. }
  633. }
  634. return result;
  635. // #else // NICE_USELIB_IPP
  636. // fthrow(ImageException,"Not yet supported without IPP.");
  637. // #endif // NICE_USELIB_IPP
  638. }
  639. GrayImage16s* DeprecatedConverter::grayToDxRegion(const Image& gray,
  640. GrayImage16s* dx,
  641. const Rect rect) {
  642. #ifdef NICE_USELIB_IPP
  643. Rect roi = clipRect(gray, rect);
  644. IppiSize ippiSize = makeROIRect(gray, rect);
  645. preventIppBug(ippiSize, roi);
  646. GrayImage16s* result = createResultBuffer(gray, dx);
  647. Ipp8u const* src = gray.getPixelPointerXY(roi.left, roi.top);
  648. Ipp16s* dest = result->getPixelPointerXY(roi.left, roi.top);
  649. if (ippiSize.width <= 0 || ippiSize.height <= 0) {
  650. return result;
  651. }
  652. IppStatus ret = ippiFilterSobelHoriz_8u16s_C1R(src, gray.getStepsize(),
  653. dest, result->getStepsize(),
  654. ippiSize,ippMskSize3x3);
  655. if(ret!=ippStsNoErr)
  656. fthrow(ImageException, ippGetStatusString(ret));
  657. return result;
  658. #else // NICE_USELIB_IPP
  659. fthrow(ImageException,"Not yet supported without IPP.");
  660. #endif // NICE_USELIB_IPP
  661. }
  662. GrayImage16s* DeprecatedConverter::grayToDyRegion(const Image& gray,
  663. GrayImage16s* dy,
  664. const Rect rect) {
  665. #ifdef NICE_USELIB_IPP
  666. Rect roi = clipRect(gray, rect);
  667. IppiSize ippiSize = makeROIRect(gray, rect);
  668. preventIppBug(ippiSize, roi);
  669. GrayImage16s* result = createResultBuffer(gray, dy);
  670. Ipp8u const* src = gray.getPixelPointerXY(roi.left, roi.top);
  671. Ipp16s* dest = result->getPixelPointerXY(roi.left, roi.top);
  672. if (ippiSize.width <= 0 || ippiSize.height <= 0) {
  673. return result;
  674. }
  675. IppStatus ret = ippiFilterSobelVert_8u16s_C1R(src, gray.getStepsize(),
  676. dest, result->getStepsize(),
  677. ippiSize,ippMskSize3x3);
  678. if(ret!=ippStsNoErr)
  679. fthrow(ImageException, ippGetStatusString(ret));
  680. return result;
  681. #else // NICE_USELIB_IPP
  682. fthrow(ImageException,"Not yet supported without IPP.");
  683. #endif // NICE_USELIB_IPP
  684. }
  685. Image* DeprecatedConverter::canny(GrayImage16s& srcdx, GrayImage16s &srcdy,
  686. float low, float high, Image* dst)
  687. {
  688. #ifdef NICE_USELIB_IPP
  689. Image* result = createResultBuffer(srcdx, dst);
  690. IppiSize ippiSize = { srcdx.width()-2, srcdx.height()-2 };
  691. int buffersize=0;
  692. IppStatus ret = ippiCannyGetSize(ippiSize, &buffersize);
  693. if(ret!=ippStsNoErr)
  694. fthrow(ImageException, ippGetStatusString(ret));
  695. Ipp8u buffer[buffersize];
  696. ret = ippiCanny_16s8u_C1R(srcdx.getPixelPointerXY(1,1), srcdx.getStepsize(),
  697. srcdy.getPixelPointerXY(1,1), srcdy.getStepsize(),
  698. result->getPixelPointerXY(1,1), result->getStepsize(),
  699. ippiSize,low,high,buffer);
  700. if(ret!=ippStsNoErr)
  701. fthrow(ImageException, ippGetStatusString(ret));
  702. return result;
  703. #else // NICE_USELIB_IPP
  704. fthrow(ImageException,"Not yet supported without IPP.");
  705. #endif // NICE_USELIB_IPP
  706. }
  707. GrayImage16s* DeprecatedConverter::grayToDx(const Image& src, GrayImage16s* dst)
  708. {
  709. #ifdef NICE_USELIB_IPP
  710. GrayImage16s* result = createResultBuffer(src, dst);
  711. IppiSize ippiSize = { src.width()-2, src.height()-2 };
  712. IppStatus ret = ippiFilterSobelVert_8u16s_C1R(src.getPixelPointerXY(1,1), src.getStepsize(),
  713. result->getPixelPointerXY(1,1), result->getStepsize(),
  714. ippiSize,ippMskSize3x3);
  715. if(ret!=ippStsNoErr)
  716. fthrow(ImageException, ippGetStatusString(ret));
  717. return result;
  718. #else // NICE_USELIB_IPP
  719. fthrow(ImageException,"Not yet supported without IPP.");
  720. #endif // NICE_USELIB_IPP
  721. }
  722. GrayImage16s* DeprecatedConverter::grayToDy(const Image& src, GrayImage16s* dst)
  723. {
  724. #ifdef NICE_USELIB_IPP
  725. GrayImage16s* result = createResultBuffer(src, dst);
  726. IppiSize ippiSize = { src.width()-2, src.height()-2 };
  727. IppStatus ret = ippiFilterSobelHoriz_8u16s_C1R(src.getPixelPointerXY(1,1), src.getStepsize(),
  728. result->getPixelPointerXY(1,1), result->getStepsize(),
  729. ippiSize,ippMskSize3x3);
  730. if(ret!=ippStsNoErr)
  731. fthrow(ImageException, ippGetStatusString(ret));
  732. return result;
  733. #else // NICE_USELIB_IPP
  734. fthrow(ImageException,"Not yet supported without IPP.");
  735. #endif // NICE_USELIB_IPP
  736. }
  737. Image* DeprecatedConverter::filterX(const Image& src, const VectorT<int> &kernel, int anchor, int divisor, Image* dst)
  738. {
  739. if(anchor==-1)
  740. anchor=kernel.size()/2;
  741. #ifdef NICE_USELIB_IPP
  742. Image* result = createResultBuffer(src, dst);
  743. IppiSize ippiSize = { (int)(src.width()-kernel.size()+1), (int)(src.height()) };
  744. IppStatus ret = ippiFilterRow_8u_C1R(src.getPixelPointerXY(kernel.size()-1-anchor,0),
  745. src.getStepsize(),
  746. result->getPixelPointerXY(anchor,0),
  747. result->getStepsize(),
  748. ippiSize,
  749. kernel.getDataPointer(),
  750. kernel.size(),
  751. anchor,
  752. divisor);
  753. if(ret!=ippStsNoErr)
  754. fthrow(ImageException, ippGetStatusString(ret));
  755. return result;
  756. #else // NICE_USELIB_IPP
  757. /*
  758. int yend=src.height();
  759. int xend=src.width()-anchor;
  760. Image &d=*result;
  761. VectorT<float> k(kernel.size());
  762. for (int i=0; i<kernel.size(); ++i)
  763. k[i]=static_cast<float>(kernel[i]);
  764. for(int y=0;y<yend; ++y)
  765. for(int x=anchor;x<xend; ++x) {
  766. float t=0.0;
  767. Image &s=src(-anchor,0);
  768. for (int i=0; i<kernel.size(); ++i)
  769. t += k[i] * s(x+i,y);
  770. d(x,y)=static_cast<Ipp8u>(t/divisor+0.5);
  771. }
  772. return result;
  773. */
  774. fthrow(ImageException,"Not yet supported without IPP.");
  775. #endif // NICE_USELIB_IPP
  776. }
  777. Image* DeprecatedConverter::filterY(const Image& src, const VectorT<int> &kernel, int anchor, int divisor, Image* dst)
  778. {
  779. if(anchor==-1)
  780. anchor=kernel.size()/2;
  781. #ifdef NICE_USELIB_IPP
  782. Image* result = createResultBuffer(src, dst);
  783. IppiSize ippiSize = { (int)(src.width()), (int)(src.height()-kernel.size()+1) };
  784. IppStatus ret = ippiFilterColumn_8u_C1R(src.getPixelPointerXY(0,kernel.size()-1-anchor),
  785. src.getStepsize(),
  786. result->getPixelPointerXY(0,anchor),
  787. result->getStepsize(),
  788. ippiSize,
  789. kernel.getDataPointer(),
  790. kernel.size(),
  791. anchor,
  792. divisor);
  793. if(ret!=ippStsNoErr)
  794. fthrow(ImageException, ippGetStatusString(ret));
  795. return result;
  796. #else // NICE_USELIB_IPP
  797. /*
  798. #pragma message NICE_WARNING("untested")
  799. int yend=src.height()-anchor;
  800. int xend=src.width();
  801. Image &d=*result;
  802. VectorT<float> k(kernel.size());
  803. for (int i=0; i<kernel.size(); ++i)
  804. k[i]=static_cast<float>(kernel[i]);
  805. for(int y=anchor;y<yend; ++y)
  806. for(int x=0;x<xend; ++x) {
  807. float t=0.0;
  808. Image &s=src(0,-anchor);
  809. for (int i=0; i<kernel.size(); ++i)
  810. t += k[i] * s(x,y+i);
  811. d(x,y)=static_cast<Ipp8u>(t/divisor+0.5);
  812. }
  813. return result;
  814. */
  815. fthrow(ImageException,"Not yet supported without IPP.");
  816. #endif // NICE_USELIB_IPP
  817. }
  818. Image* DeprecatedConverter::filter(const Image& src, const ImageT<int> &kernel, int anchorx, int anchory, int divisor, Image* dst)
  819. {
  820. if(anchorx==-1)
  821. anchorx=kernel.width()/2;
  822. if(anchory==-1)
  823. anchory=kernel.height()/2;
  824. Image* result = createResultBuffer(src, dst);
  825. #ifdef NICE_USELIB_IPP
  826. IppiPoint anchor={anchorx,anchory};
  827. IppiSize ippiSize = { src.width()-kernel.width()+1, src.height()-kernel.height()+1 };
  828. if(kernel.getStepsize() != kernel.width()) {
  829. ImageT<int> k(kernel,GrayColorImageCommonImplementation::noAlignment);
  830. IppStatus ret = ippiFilter_8u_C1R(src.getPixelPointerXY(kernel.width()-1-anchorx,kernel.height()-1-anchory),
  831. src.getStepsize(),
  832. result->getPixelPointerXY(anchorx,anchory),
  833. result->getStepsize(),
  834. ippiSize,
  835. k.getPixelPointer(),
  836. makeROIFullImage(k),
  837. anchor,
  838. divisor);
  839. if(ret!=ippStsNoErr)
  840. fthrow(ImageException, ippGetStatusString(ret));
  841. } else {
  842. IppStatus ret = ippiFilter_8u_C1R(src.getPixelPointerXY(kernel.width()-1-anchorx,kernel.height()-1-anchory),
  843. src.getStepsize(),
  844. result->getPixelPointerXY(anchorx,anchory),
  845. result->getStepsize(),
  846. ippiSize,
  847. kernel.getPixelPointer(),
  848. makeROIFullImage(kernel),
  849. anchor,
  850. divisor);
  851. if(ret!=ippStsNoErr)
  852. fthrow(ImageException, ippGetStatusString(ret));
  853. }
  854. #else // NICE_USELIB_IPP
  855. /*
  856. #pragma message NICE_WARNING("untested")
  857. int yend=src.height()-anchory;
  858. int xend=src.width()-anchorx;
  859. Image &d=*result;
  860. FloatImage k(kernel.width(),kernel.height());
  861. DeprecatedConverter::grayToFloat(kernel,&k);
  862. for(int y=anchory;y<yend; ++y)
  863. for(int x=anchorx;x<xend; ++x) {
  864. float t=0.0;
  865. Image &s=src(-anchorx,-anchory);
  866. for (int j=0; j<kernel.height(); ++j)
  867. for (int i=0; i<kernel.width(); ++i)
  868. t += k(i,j) * s(x+i,y+j);
  869. d(x,y)=static_cast<P>(t/divisor);
  870. }
  871. */
  872. fthrow(ImageException,"Not yet supported without IPP.");
  873. #endif // NICE_USELIB_IPP
  874. return result;
  875. }
  876. ColorImage* DeprecatedConverter::filterLowpassRegion(const ColorImage& image,
  877. ColorImage* buffer,
  878. Rect rect) {
  879. #ifdef NICE_USELIB_IPP
  880. Rect roi = clipRect(image, rect);
  881. IppiSize ippiSize = makeROIRect(image, rect);
  882. preventIppBug(ippiSize, roi);
  883. ColorImage* result
  884. = createResultBuffer(image.width(), image.height(), buffer);
  885. ColorImage::Pixel const* src = image.getPixelPointerXY(roi.left, roi.top);
  886. ColorImage::Pixel* dest = result->getPixelPointerXY(roi.left, roi.top);
  887. IppStatus ret = ippiFilterLowpass_8u_C3R(src, image.getStepsize(),
  888. dest, result->getStepsize(),
  889. ippiSize,
  890. ippMskSize3x3);
  891. if(ret!=ippStsNoErr)
  892. fthrow(ImageException, ippGetStatusString(ret));
  893. return result;
  894. #else // NICE_USELIB_IPP
  895. fthrow(ImageException,"Not yet supported without IPP.");
  896. #endif // NICE_USELIB_IPP
  897. }
  898. ColorImage* DeprecatedConverter::filterGaussRegion(const ColorImage& image,
  899. ColorImage* buffer,
  900. Rect rect) {
  901. #ifdef NICE_USELIB_IPP
  902. Rect roi = clipRect(image, rect);
  903. IppiSize ippiSize = makeROIRect(image, rect);
  904. preventIppBug(ippiSize, roi);
  905. ColorImage* result
  906. = createResultBuffer(image.width(), image.height(), buffer);
  907. ColorImage::Pixel const* src = image.getPixelPointerXY(roi.left, roi.top);
  908. ColorImage::Pixel* dest
  909. = result->getPixelPointerXY(roi.left, roi.top);
  910. IppStatus ret = ippiFilterGauss_8u_C3R(src, image.getStepsize(),
  911. dest, result->getStepsize(),
  912. ippiSize,
  913. ippMskSize3x3);
  914. if(ret!=ippStsNoErr)
  915. fthrow(ImageException, ippGetStatusString(ret));
  916. return result;
  917. #else // NICE_USELIB_IPP
  918. fthrow(ImageException,"Not yet supported without IPP.");
  919. #endif // NICE_USELIB_IPP
  920. }
  921. Image* DeprecatedConverter::filterGauss(const Image& image,
  922. float sigma,
  923. Image* buffer) {
  924. #ifdef NICE_USELIB_IPP
  925. int length=static_cast<int>(2.0*sigma*sigma-0.5);
  926. if(length<1)
  927. length=0;
  928. int resultlength=3+length*2;
  929. float kernel[resultlength];
  930. const float konst1=1.0/(sigma*sqrt(2.*M_PI));
  931. const float konst2=-.5/(sigma*sigma);
  932. float sum=konst1;
  933. int half=length+1;
  934. kernel[half]=konst1;
  935. for(int i=0;i<half;i++) {
  936. kernel[i]=kernel[resultlength-1-i]=konst1* exp(konst2*(half-i));
  937. sum+=2*kernel[i];
  938. }
  939. for(int i=0;i<resultlength;i++) {
  940. kernel[i]/=sum;
  941. }
  942. IppiSize ippiCSize = { image.width()-resultlength+1, image.height()};
  943. Image tmpimg(image.width(),image.height());
  944. tmpimg=image;
  945. IppStatus ret = ippiFilterRow_C1R(image.getPixelPointerXY(resultlength/2,0),
  946. image.getStepsize(),
  947. tmpimg.getPixelPointerXY(resultlength/2,0),
  948. tmpimg.getStepsize(),
  949. ippiCSize,
  950. kernel,
  951. resultlength,
  952. half);
  953. if(ret!=ippStsNoErr) {
  954. fthrow(ImageException, ippGetStatusString(ret));
  955. }
  956. IppiSize ippiRSize = { image.width(), image.height()-resultlength+1};
  957. Image* result = createResultBuffer(image, buffer);
  958. *result=tmpimg;
  959. ret = ippiFilterColumn_C1R(tmpimg.getPixelPointerXY(0,resultlength/2),
  960. tmpimg.getStepsize(),
  961. result->getPixelPointerXY(0,resultlength/2),
  962. result->getStepsize(),
  963. ippiRSize,
  964. kernel,
  965. resultlength,
  966. half);
  967. if(ret!=ippStsNoErr)
  968. fthrow(ImageException, ippGetStatusString(ret));
  969. return result;
  970. #else // NICE_USELIB_IPP
  971. fthrow(ImageException,"Not yet supported without IPP.");
  972. #endif // NICE_USELIB_IPP
  973. }
  974. /*
  975. ColorImage* DeprecatedConverter::filterGaussRegion(const ColorImage& image,
  976. ColorImage* buffer,
  977. Rect rect, float sigma) {
  978. #ifdef NICE_USELIB_IPP
  979. Rect roi = clipRect(image, rect);
  980. IppiSize ippiSize = makeROIRect(image, rect);
  981. preventIppBug(ippiSize, roi);
  982. ColorImage* result = createResultBuffer(image.width(), image.height(), buffer);
  983. ColorImage::Pixel const* src = image.getPixelPointerXY(roi.left, roi.top);
  984. ColorImage::Pixel* dest = result->getPixelPointerXY(roi.left, roi.top);
  985. int length=static_cast<int>(2.0*sigma*sigma-0.5);
  986. if(length<1)
  987. length=0;
  988. float base[] = {0.25, 0.5, 0.25};
  989. int resultlength=3+length*2;
  990. float *tmpv = ippsMalloc_32f(resultlength);
  991. float *kernel = ippsMalloc_32f(resultlength);
  992. copy(base, base+3, kernel);
  993. for(int i=0;i<length;i++) {
  994. ippsConv_32f(base,3,kernel,3+i*2,tmpv);
  995. copy(tmpv, tmpv+3+(i+1)*2, kernel);
  996. }
  997. ippsFree(tmpv);
  998. ippiFilterColumn_32f_C3R(src, image.getStepsize(),
  999. dest, result->getStepsize(),
  1000. ippiSize, kernel, resultlength, resultlength/2);
  1001. region.width=xsize-2*windowcenter;
  1002. IppStatus ret = ippiFilterRow_32f_C3R(tmp_image+windowcenter+(windowcenter*stepsize)/4, tmpstepsize,
  1003. ret_image+windowcenter+(windowcenter*stepsize)/4, retstepsize, region,
  1004. pkernel, windowsize, windowcenter);
  1005. if(ret!=ippStsNoErr)
  1006. fthrow(ImageException, ippGetStatusString(ret));
  1007. return result;
  1008. #else // NICE_USELIB_IPP
  1009. fthrow(ImageException,"Not yet supported.");
  1010. // #endif // NICE_USELIB_IPP
  1011. }
  1012. */
  1013. // Rect DeprecatedConverter::clipRect(const MultiChannelImageAccess& image, const Rect rect) {
  1014. // Rect result(rect);
  1015. // if (result.left < 0) {
  1016. // result.width += result.left;
  1017. // result.left = 0;
  1018. // }
  1019. // if (result.top < 0) {
  1020. // result.height += result.top;
  1021. // result.top = 0;
  1022. // }
  1023. // if (result.left >= image.width()) {
  1024. // result.left = image.width() - 1;
  1025. // result.width = 0;
  1026. // }
  1027. // if (result.top >= image.height()) {
  1028. // result.top = image.height() - 1;
  1029. // result.height = 0;
  1030. // }
  1031. // int maxWidth = image.width() - result.left;
  1032. // int maxHeight = image.height() - result.top;
  1033. // if (result.width >= maxWidth) {
  1034. // result.width = maxWidth;
  1035. // }
  1036. // if (result.height >= maxHeight) {
  1037. // result.height = maxHeight;
  1038. // }
  1039. // return result;
  1040. // }
  1041. //
  1042. // #ifdef NICE_USELIB_IPP
  1043. // IppiRect DeprecatedConverter::makeRectFullImage(const MultiChannelImageAccess& image) {
  1044. // IppiRect ippiRect;
  1045. // ippiRect.x = 0;
  1046. // ippiRect.y = 0;
  1047. // ippiRect.width = image.width();
  1048. // ippiRect.height = image.height();
  1049. // return ippiRect;
  1050. // }
  1051. //
  1052. // IppiSize DeprecatedConverter::makeROIFullImage(const MultiChannelImageAccess& image) {
  1053. // IppiSize ippiSize;
  1054. // ippiSize.width = image.width();
  1055. // ippiSize.height = image.height();
  1056. // return ippiSize;
  1057. // }
  1058. //
  1059. // IppiSize DeprecatedConverter::makeROIRect(const MultiChannelImageAccess& image, const Rect rect) {
  1060. // Rect roi = clipRect(image, rect);
  1061. // IppiSize ippiSize;
  1062. // ippiSize.width = roi.width;
  1063. // ippiSize.height = roi.height;
  1064. // return ippiSize;
  1065. // }
  1066. //
  1067. // void DeprecatedConverter::preventIppBug(IppiSize& ippiSize, Rect& clippedROI) {
  1068. // // there is a bug in IPP: if ippiSize.height == 1 the function won't return
  1069. // if (ippiSize.height == 1) {
  1070. // ippiSize.height = 2;
  1071. // if (clippedROI.top > 0) {
  1072. // clippedROI.top--;
  1073. // }
  1074. // }
  1075. // // maybe there is the same problem when ippiSize.width == 1
  1076. // if (ippiSize.width == 1) {
  1077. // ippiSize.width = 2;
  1078. // if (clippedROI.left > 0) {
  1079. // clippedROI.left--;
  1080. // }
  1081. // }
  1082. // }
  1083. // #endif // NICE_USELIB_IPP
  1084. } // namespace