DeprecatedConverter.tcc 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820
  1. #include "core/image/DeprecatedConverter.h"
  2. #include <iostream>
  3. namespace NICE {
  4. template<class P>
  5. ImageT<P>* DeprecatedConverter::grayToDx(const ImageT<P>& src, ImageT<P>* dst)
  6. {
  7. #ifdef NICE_USELIB_IPP
  8. ImageT<P>* result = createResultBuffer(src, dst);
  9. IppiSize ippiSize = { src.width()-2, src.height()-2 };
  10. IppStatus ret = ippiFilterSobelVert_C1R(src.getPixelPointerXY(1,1), src.getStepsize(),
  11. result->getPixelPointerXY(1,1), result->getStepsize(),
  12. ippiSize,ippMskSize3x3);
  13. if(ret!=ippStsNoErr)
  14. fthrow(ImageException, ippGetStatusString(ret));
  15. return result;
  16. #else // NICE_USELIB_IPP
  17. fthrow(ImageException,"Not yet supported without IPP.");
  18. #endif // NICE_USELIB_IPP
  19. }
  20. template<class P>
  21. ImageT<P>* DeprecatedConverter::grayToDy(const ImageT<P>& src, ImageT<P>* dst)
  22. {
  23. #ifdef NICE_USELIB_IPP
  24. ImageT<P>* result = createResultBuffer(src, dst);
  25. IppiSize ippiSize = { src.width()-2, src.height()-2 };
  26. IppStatus ret = ippiFilterSobelHoriz_C1R(src.getPixelPointerXY(1,1), src.getStepsize(),
  27. result->getPixelPointerXY(1,1), result->getStepsize(),
  28. ippiSize,ippMskSize3x3);
  29. if(ret!=ippStsNoErr)
  30. fthrow(ImageException, ippGetStatusString(ret));
  31. return result;
  32. #else // NICE_USELIB_IPP
  33. fthrow(ImageException,"Not yet supported without IPP.");
  34. #endif // NICE_USELIB_IPP
  35. }
  36. template<class P>
  37. ImageT<P>* DeprecatedConverter::resize(const ImageT<P>& src, ImageT<P>* dst, double xFactor, double yFactor, int interpolation)
  38. {
  39. #ifdef NICE_USELIB_IPP
  40. IppiRect rect;
  41. rect.x = 0;
  42. rect.y = 0;
  43. rect.width = src.width();
  44. rect.height = src.height();
  45. IppiSize dstSize;
  46. if(isZero(xFactor) && dst!=NULL) {
  47. dstSize.width = dst->width();
  48. xFactor=dst->width()/(double)src.width();
  49. } else
  50. dstSize.width = (int) ceil ((double) src.width () * xFactor);
  51. if(isZero(yFactor) && dst!=NULL) {
  52. dstSize.height = dst->height();
  53. yFactor=dst->height()/(double)src.height();
  54. } else
  55. dstSize.height = (int) ceil ((double)src.height () * yFactor);
  56. ImageT<P> * result = createResultBuffer(dstSize.width, dstSize.height, dst);
  57. IppStatus ret = ippiResize_C1R(src.getPixelPointer(), makeROIFullImage(src), src.getStepsize(), rect,
  58. result->getPixelPointer(), result->getStepsize(), dstSize,
  59. xFactor, yFactor, interpolation);
  60. if(ret!=ippStsNoErr)
  61. fthrow(ImageException, ippGetStatusString(ret));
  62. return result;
  63. #else // NICE_USELIB_IPP
  64. fthrow(ImageException,"Not yet supported without IPP.");
  65. #endif // NICE_USELIB_IPP
  66. }
  67. template<class P>
  68. ColorImageT<P>* DeprecatedConverter::resize(const ColorImageT<P>& src, ColorImageT<P>* dst, double xFactor, double yFactor, int interpolation)
  69. {
  70. #ifdef NICE_USELIB_IPP
  71. IppiRect rect;
  72. rect.x = 0;
  73. rect.y = 0;
  74. rect.width = src.width();
  75. rect.height = src.height();
  76. IppiSize dstSize;
  77. if(isZero(xFactor) && dst!=NULL) {
  78. dstSize.width = dst->width();
  79. xFactor=dst->width()/(double)src.width();
  80. } else
  81. dstSize.width = (int) ceil ((double) src.width () * xFactor);
  82. if(isZero(yFactor) && dst!=NULL) {
  83. dstSize.height = dst->height();
  84. yFactor=dst->height()/(double)src.height();
  85. } else
  86. dstSize.height = (int) ceil ((double)src.height () * yFactor);
  87. ColorImageT<P> * result = createResultBuffer(dstSize.width, dstSize.height, dst);
  88. IppStatus ret = ippiResize_C3R(src.getPixelPointer(), makeROIFullImage(src), src.getStepsize(), rect,
  89. result->getPixelPointer(), result->getStepsize(), dstSize,
  90. xFactor, yFactor, interpolation);
  91. if(ret!=ippStsNoErr)
  92. fthrow(ImageException, ippGetStatusString(ret));
  93. return result;
  94. #else // NICE_USELIB_IPP
  95. fthrow(ImageException,"Not yet supported without IPP.");
  96. #endif // NICE_USELIB_IPP
  97. }
  98. template<class P>
  99. ImageT<P>* DeprecatedConverter::mirror(const ImageT<P>& src, IppiAxis axis, ImageT<P>* dst)
  100. {
  101. #ifdef NICE_USELIB_IPP
  102. ImageT<P>* result = createResultBuffer(src, dst);
  103. IppStatus ret = ippiMirror_C1R(src.getPixelPointer(),
  104. src.getStepsize(),
  105. result->getPixelPointer(),
  106. result->getStepsize(),
  107. makeROIFullImage(src),
  108. axis);
  109. if(ret!=ippStsNoErr)
  110. fthrow(ImageException, ippGetStatusString(ret));
  111. return result;
  112. #else // NICE_USELIB_IPP
  113. fthrow(ImageException,"Not yet supported without IPP.");
  114. #endif // NICE_USELIB_IPP
  115. }
  116. template<class P>
  117. ColorImageT<P>* DeprecatedConverter::mirror(const ColorImageT<P>& src, IppiAxis axis, ColorImageT<P>* dst)
  118. {
  119. #ifdef NICE_USELIB_IPP
  120. ColorImageT<P>* result = createResultBuffer(src, dst);
  121. IppStatus ret = ippiMirror_C1R(src.getPixelPointer(),
  122. src.getStepsize(),
  123. result->getPixelPointer(),
  124. result->getStepsize(),
  125. makeROIFullImage(src),
  126. axis);
  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. template<class P1, class P2>
  135. ImageT<P2>* DeprecatedConverter::convertBitDepth(const ImageT<P1>& image,
  136. ImageT<P2>* buffer) {
  137. #ifdef NICE_USELIB_IPP
  138. ImageT<P2>* result = createResultBuffer(image, buffer);
  139. if(sizeof(P1)<sizeof(P2)) {
  140. IppStatus ret = ippiConvert_C1R(image.getPixelPointer(),
  141. image.getStepsize(),
  142. result->getPixelPointer(),
  143. result->getStepsize(),
  144. makeROIFullImage(image));
  145. if(ret!=ippStsNoErr)
  146. fthrow(ImageException, ippGetStatusString(ret));
  147. } else {
  148. IppStatus ret= ippiConvert_C1R(image.getPixelPointer(),
  149. image.getStepsize(),
  150. result->getPixelPointer(),
  151. result->getStepsize(),
  152. makeROIFullImage(image), ippRndNear);
  153. if(ret!=ippStsNoErr)
  154. fthrow(ImageException, ippGetStatusString(ret));
  155. }
  156. return result;
  157. #else // NICE_USELIB_IPP
  158. fthrow(ImageException,"Not yet supported without IPP.");
  159. #endif // NICE_USELIB_IPP
  160. }
  161. template<class P1, class P2>
  162. ColorImageT<P2>* DeprecatedConverter::convertBitDepth(const ColorImageT<P1>& image,
  163. ColorImageT<P2>* buffer) {
  164. #ifdef NICE_USELIB_IPP
  165. ColorImageT<P2>* result = createResultBuffer(image, buffer);
  166. if(sizeof(P1)<sizeof(P2)) {
  167. IppStatus ret = ippiConvert_C3R(image.getPixelPointer(),
  168. image.getStepsize(),
  169. result->getPixelPointer(),
  170. result->getStepsize(),
  171. makeROIFullImage(image));
  172. if(ret!=ippStsNoErr)
  173. fthrow(ImageException, ippGetStatusString(ret));
  174. } else {
  175. IppStatus ret = ippiConvert_C3R(image.getPixelPointer(),
  176. image.getStepsize(),
  177. result->getPixelPointer(),
  178. result->getStepsize(),
  179. makeROIFullImage(image), ippRndNear);
  180. if(ret!=ippStsNoErr) {
  181. fthrow(ImageException, ippGetStatusString(ret));
  182. }
  183. }
  184. return result;
  185. #else // NICE_USELIB_IPP
  186. fthrow(ImageException,"Not yet supported without IPP.");
  187. #endif // NICE_USELIB_IPP
  188. }
  189. template<class P>
  190. FloatImage* DeprecatedConverter::grayToFloat(const ImageT<P>& gray,
  191. FloatImage* fimage) {
  192. #ifdef NICE_USELIB_IPP
  193. FloatImage* result = createResultBuffer(gray, fimage);
  194. IppStatus ret = ippiConvert_C1R(gray.getPixelPointer(),
  195. gray.getStepsize(),
  196. result->getPixelPointer(),
  197. result->getStepsize(),
  198. makeROIFullImage(gray));
  199. if(ret!=ippStsNoErr)
  200. fthrow(ImageException, ippGetStatusString(ret));
  201. return result;
  202. #else // NICE_USELIB_IPP
  203. fthrow(ImageException,"Not yet supported without IPP.");
  204. #endif // NICE_USELIB_IPP
  205. }
  206. template<class P>
  207. ImageT<P>* DeprecatedConverter::floatToGray(const FloatImage& fimage,
  208. ImageT<P>* gray) {
  209. #ifdef NICE_USELIB_IPP
  210. ImageT<P>* result = createResultBuffer(fimage, gray);
  211. IppStatus ret = ippiConvert_C1R(fimage.getPixelPointer(),
  212. fimage.getStepsize(),
  213. result->getPixelPointer(),
  214. result->getStepsize(),
  215. makeROIFullImage(fimage),ippRndNear);
  216. if(ret!=ippStsNoErr)
  217. fthrow(ImageException, ippGetStatusString(ret));
  218. return result;
  219. #else // NICE_USELIB_IPP
  220. fthrow(ImageException,"Not yet supported without IPP.");
  221. #endif // NICE_USELIB_IPP
  222. }
  223. template<class P>
  224. ImageT<P>* DeprecatedConverter::floatToGrayScaled(const FloatImage& fimage, Ipp32f fmin, Ipp32f fmax,
  225. ImageT<P>* gray) {
  226. #ifdef NICE_USELIB_IPP
  227. ImageT<P>* result = createResultBuffer(fimage, gray);
  228. IppStatus ret = ippiScale_C1R(fimage.getPixelPointer(),
  229. fimage.getStepsize(),
  230. result->getPixelPointer(),
  231. result->getStepsize(),
  232. makeROIFullImage(fimage),
  233. fmin,
  234. fmax);
  235. if(ret!=ippStsNoErr)
  236. fthrow(ImageException, ippGetStatusString(ret));
  237. return result;
  238. #else // NICE_USELIB_IPP
  239. fthrow(ImageException,"Not yet supported without IPP.");
  240. #endif // NICE_USELIB_IPP
  241. }
  242. template<class P>
  243. ImageT<P>* DeprecatedConverter::floatToGrayScaled(const FloatImage& fimage,
  244. ImageT<P>* gray) {
  245. Ipp32f fmin, fmax;
  246. #ifdef NICE_USELIB_IPP
  247. IppStatus ret = ippiMinMax_C1R(fimage.getPixelPointer(),
  248. fimage.getStepsize(),
  249. makeROIFullImage(fimage),
  250. &fmin,
  251. &fmax);
  252. if(ret!=ippStsNoErr)
  253. fthrow(ImageException, ippGetStatusString(ret));
  254. return DeprecatedConverter::floatToGrayScaled(fimage, fmin, fmax, gray);
  255. #else // NICE_USELIB_IPP
  256. fthrow(ImageException,"Not yet supported without IPP.");
  257. #endif // NICE_USELIB_IPP
  258. }
  259. template<class P>
  260. ImageT<P>* DeprecatedConverter::rgbToGray(const ColorImageT<P>& rgb,
  261. ImageT<P>* gray) {
  262. ImageT<P>* result = createResultBuffer(rgb, gray);
  263. #ifdef NICE_USELIB_IPP
  264. // XXX rgb->gray pixel value not rounded correctly
  265. IppStatus ret = ippiRGBToGray_C3C1R(rgb.getPixelPointer(),
  266. rgb.getStepsize(),
  267. result->getPixelPointer(),
  268. result->getStepsize(),
  269. makeROIFullImage(rgb));
  270. if(ret!=ippStsNoErr)
  271. fthrow(ImageException, ippGetStatusString(ret));
  272. return result;
  273. #else // NICE_USELIB_IPP
  274. // fill rgb -> gray color transform lookup tables
  275. P *pdst;
  276. const P *psrc;
  277. if(sizeof(P)==1) {
  278. if(!pLut_initialized) {
  279. for (int i = 0; i < 256; ++i) {
  280. pLutRg[i] = static_cast<int>((0.299 *i+0.5)*(1<<12));
  281. pLutGg[i] = static_cast<int>(0.587 *i*(1<<12));
  282. pLutBg[i] = static_cast<int>(0.114 *i*(1<<12));
  283. }
  284. pLut_initialized=true;
  285. }
  286. for (int j=0; j < result->height(); ++j) {
  287. psrc=rgb.getPixelPointerY(j);
  288. pdst=result->getPixelPointerY(j);
  289. for (int i=0; i < result->width(); ++i, psrc+=3) {
  290. // using lookuptable is faster
  291. *(pdst++) = static_cast<P>((pLutRg[(int)(*psrc)]+pLutGg[(int)*(psrc+1)]+pLutBg[(int)*(psrc+2)])>>12);
  292. }
  293. }
  294. } else {
  295. for (int j=0; j < result->height(); ++j) {
  296. psrc=rgb.getPixelPointerY(j);
  297. pdst=result->getPixelPointerY(j);
  298. for (int i=0; i < result->width(); ++i, psrc+=3) {
  299. // without lookuptable (slow)
  300. *(pdst++) = static_cast<P>(*psrc*0.299+*(psrc+1)*0.587+*(psrc+2)*0.114);
  301. }
  302. }
  303. }
  304. return result;
  305. #endif // NICE_USELIB_IPP
  306. }
  307. template<class P>
  308. ColorImageT<P>* DeprecatedConverter::grayToRGB(const ImageT<P>& gray,
  309. ColorImageT<P>* rgb)
  310. {
  311. ColorImageT<P>* result = createResultBuffer(gray, rgb);
  312. #ifdef NICE_USELIB_IPP
  313. const P *src[3];
  314. src[0] = gray.getPixelPointer();
  315. src[1] = gray.getPixelPointer();
  316. src[2] = gray.getPixelPointer();
  317. IppStatus ret = ippiCopy_P3C3R(src, gray.getStepsize(),
  318. result->getPixelPointer(), result->getStepsize(),
  319. makeROIFullImage(gray));
  320. if(ret!=ippStsNoErr)
  321. fthrow(ImageException, ippGetStatusString(ret));
  322. return result;
  323. #else // NICE_USELIB_IPP
  324. P *pdst;
  325. const P *psrc;
  326. for (int j=0; j < result->height(); ++j) {
  327. psrc=gray.getPixelPointerY(j);
  328. pdst=result->getPixelPointerY(j);
  329. for (int i=0; i < result->width(); ++i,++psrc) {
  330. *(pdst++) = *psrc;
  331. *(pdst++) = *psrc;
  332. *(pdst++) = *psrc;
  333. }
  334. }
  335. return result;
  336. #endif // NICE_USELIB_IPP
  337. }
  338. template<class P>
  339. ImageT<P>* DeprecatedConverter::filterGauss3x3(const ImageT<P>& image, ImageT<P>* buffer)
  340. {
  341. #ifdef NICE_USELIB_IPP
  342. ImageT<P>* result = createResultBuffer(image, buffer);
  343. IppiSize ippiSize = { image.width()-2, image.height()-2 };
  344. IppStatus ret = ippiFilterGauss_C1R(image.getPixelPointerXY(1,1), image.getStepsize(),
  345. result->getPixelPointerXY(1,1), result->getStepsize(),
  346. ippiSize,ippMskSize3x3);
  347. if(ret!=ippStsNoErr)
  348. fthrow(ImageException, ippGetStatusString(ret));
  349. return result;
  350. #else // NICE_USELIB_IPP
  351. fthrow(ImageException,"Not yet supported without IPP.");
  352. #endif // NICE_USELIB_IPP
  353. }
  354. template<class P>
  355. ImageT<P>* DeprecatedConverter::filterGauss5x5(const ImageT<P>& image, ImageT<P>* buffer)
  356. {
  357. #ifdef NICE_USELIB_IPP
  358. ImageT<P>* result = createResultBuffer(image, buffer);
  359. IppiSize ippiSize = { image.width()-4, image.height()-4 };
  360. IppStatus ret = ippiFilterGauss_C1R(image.getPixelPointerXY(2,2), image.getStepsize(),
  361. result->getPixelPointerXY(2,2), result->getStepsize(),
  362. ippiSize,ippMskSize5x5);
  363. if(ret!=ippStsNoErr)
  364. fthrow(ImageException, ippGetStatusString(ret));
  365. return result;
  366. #else // NICE_USELIB_IPP
  367. fthrow(ImageException,"Not yet supported without IPP.");
  368. #endif // NICE_USELIB_IPP
  369. }
  370. template<class P>
  371. ImageT<P>* DeprecatedConverter::filterX(const ImageT<P>& src, const VectorT<float> &kernel, int anchor, ImageT<P>* dst)
  372. {
  373. if(anchor==-1)
  374. anchor=kernel.size()/2;
  375. #ifdef NICE_USELIB_IPP
  376. ImageT<P>* result = createResultBuffer(src, dst);
  377. IppiSize ippiSize = { src.width()-kernel.size()+1, src.height() };
  378. IppStatus ret = ippiFilterRow_C1R(src.getPixelPointerXY(kernel.size()-1-anchor,0),
  379. src.getStepsize(),
  380. result->getPixelPointerXY(anchor,0),
  381. result->getStepsize(),
  382. ippiSize,
  383. kernel.getDataPointer(),
  384. kernel.size(),
  385. anchor);
  386. if(ret!=ippStsNoErr)
  387. fthrow(ImageException, ippGetStatusString(ret));
  388. return result;
  389. #else // NICE_USELIB_IPP
  390. /*
  391. #pragma message NICE_WARNING("untested")
  392. int yend=src.height();
  393. int xend=src.width()-kernel.size()+anchor;
  394. ImageT<P> &d=*result;
  395. for(int y=0;y<yend; ++y)
  396. for(int x=anchor;x<xend; ++x) {
  397. float t=0.0;
  398. ImageT<P> &s=src(-anchor,0);
  399. for (int i=0; i<kernel.size(); ++i)
  400. t += kernel[i] * s(x+i,y);
  401. d(x,y)=t;
  402. }
  403. return result;
  404. */
  405. fthrow(ImageException,"Not yet supported without IPP.");
  406. #endif // NICE_USELIB_IPP
  407. }
  408. template<class P>
  409. ImageT<P>* DeprecatedConverter::filterY(const ImageT<P>& src, const VectorT<float> &kernel, int anchor, ImageT<P>* dst)
  410. {
  411. if(anchor==-1)
  412. anchor=kernel.size()/2;
  413. ImageT<P>* result = createResultBuffer(src, dst);
  414. #ifdef NICE_USELIB_IPP
  415. IppiSize ippiSize = { src.width(), src.height()-kernel.size()+1 };
  416. IppStatus ret = ippiFilterColumn_C1R(src.getPixelPointerXY(0,kernel.size()-1-anchor),
  417. src.getStepsize(),
  418. result->getPixelPointerXY(0,anchor),
  419. result->getStepsize(),
  420. ippiSize,
  421. kernel.getDataPointer(),
  422. kernel.size(),
  423. anchor);
  424. if(ret!=ippStsNoErr)
  425. fthrow(ImageException, ippGetStatusString(ret));
  426. return result;
  427. #else // NICE_USELIB_IPP
  428. int yend=src.height()-anchor;
  429. int xend=src.width();
  430. ImageT<P> &d=*result;
  431. for(int y=anchor;y<yend; ++y)
  432. for(int x=0;x<xend; ++x) {
  433. float t=0.0;
  434. ImageT<P> &s=src(0,-anchor);
  435. for (int i=0; i<kernel.size(); ++i)
  436. t += kernel[i] * s(x,y+i);
  437. d(x,y)=t;
  438. }
  439. return result;
  440. fthrow(ImageException,"Not yet supported without IPP.");
  441. #endif // NICE_USELIB_IPP
  442. }
  443. template<class P>
  444. ImageT<P>* DeprecatedConverter::filter(const ImageT<P>& src, const FloatImage &kernel, int anchorx, int anchory, ImageT<P>* dst)
  445. {
  446. if(anchorx==-1)
  447. anchorx=kernel.width()/2;
  448. if(anchory==-1)
  449. anchory=kernel.height()/2;
  450. ImageT<P>* result = createResultBuffer(src, dst);
  451. #ifdef NICE_USELIB_IPP
  452. IppiPoint anchor={anchorx,anchory};
  453. IppiSize ippiSize = { src.width()-kernel.width()+1, src.height()-kernel.height()+1 };
  454. if(kernel.getMemoryLayout() != GrayColorImageCommonImplementation::noAlignment && (uint)kernel.getStepsize() != sizeof(float)*kernel.width()) {
  455. FloatImage k(kernel,GrayColorImageCommonImplementation::noAlignment);
  456. IppStatus ret = ippiFilter_C1R(src.getPixelPointerXY(kernel.width()-1-anchorx,kernel.height()-1-anchory),
  457. src.getStepsize(),
  458. result->getPixelPointerXY(anchorx,anchory),
  459. result->getStepsize(),
  460. ippiSize,
  461. k.getPixelPointer(),
  462. makeROIFullImage(k),
  463. anchor);
  464. if(ret!=ippStsNoErr)
  465. fthrow(ImageException, ippGetStatusString(ret));
  466. } else {
  467. IppStatus ret = ippiFilter_C1R(src.getPixelPointerXY(kernel.width()-1-anchorx,kernel.height()-1-anchory),
  468. src.getStepsize(),
  469. result->getPixelPointerXY(anchorx,anchory),
  470. result->getStepsize(),
  471. ippiSize,
  472. kernel.getPixelPointer(),
  473. makeROIFullImage(kernel),
  474. anchor);
  475. if(ret!=ippStsNoErr)
  476. fthrow(ImageException, ippGetStatusString(ret));
  477. }
  478. #else // NICE_USELIB_IPP
  479. // int yend=src.height()-anchory;
  480. // int xend=src.width()-anchorx;
  481. // ImageT<P> &d=*result;
  482. // for(int y=anchory;y<yend; ++y)
  483. // for(int x=anchorx;x<xend; ++x) {
  484. // float t=0.0;
  485. // ImageT<P> &s=src(-anchorx,-anchory);
  486. // for (int j=0; j<kernel.height(); ++j)
  487. // for (int i=0; i<kernel.width(); ++i)
  488. // t += kernel(i,j) * s(x+i,y+j);
  489. // d(x,y)=t;
  490. // }
  491. fthrow(ImageException,"Not yet supported without IPP.");
  492. #endif // NICE_USELIB_IPP
  493. return result;
  494. }
  495. template<class P>
  496. ImageT<P>* DeprecatedConverter::And(const ImageT<P>& src0, const ImageT<P>& src1, ImageT<P>* dst)
  497. {
  498. #ifdef NICE_USELIB_IPP
  499. ImageT<P> *result = createResultBuffer(src0.width(), src0.height(), dst);
  500. IppStatus ret = ippiAnd_C1R(src0.getPixelPointer(), src0.getStepsize(),
  501. src1.getPixelPointer(), src1.getStepsize(),
  502. result->getPixelPointer(), result->getStepsize(),
  503. makeROIFullImage(src0));
  504. if(ret!=ippStsNoErr)
  505. fthrow(ImageException, ippGetStatusString(ret));
  506. return result;
  507. #else // NICE_USELIB_IPP
  508. fthrow(ImageException,"Not yet supported without IPP.");
  509. #endif // NICE_USELIB_IPP
  510. }
  511. template<class P>
  512. ColorImageT<P>* DeprecatedConverter::And(const ColorImageT<P>& src0, const ColorImageT<P>& src1, ColorImageT<P>* dst)
  513. {
  514. #ifdef NICE_USELIB_IPP
  515. ColorImageT<P> *result = createResultBuffer(src0.width(), src0.height(), dst);
  516. IppStatus ret = ippiAnd_C3R(src0.getPixelPointer(), src0.getStepsize(),
  517. src1.getPixelPointer(), src1.getStepsize(),
  518. result->getPixelPointer(), result->getStepsize(),
  519. makeROIFullImage(src0));
  520. if(ret!=ippStsNoErr)
  521. fthrow(ImageException, ippGetStatusString(ret));
  522. return result;
  523. #else // NICE_USELIB_IPP
  524. fthrow(ImageException,"Not yet supported without IPP.");
  525. #endif // NICE_USELIB_IPP
  526. }
  527. template<class P>
  528. ColorImageT<P>* DeprecatedConverter::mean(const ColorImageT<P>& src0, const ColorImageT<P>& src1, ColorImageT<P>* dst)
  529. {
  530. #ifdef NICE_USELIB_IPP
  531. ColorImageT<P> *result = createResultBuffer(src0.width(), src0.height(), dst);
  532. IppStatus ret = ippiAlphaCompC_C3R(src0.getPixelPointer(), src0.getStepsize(), 127,
  533. src1.getPixelPointer(), src1.getStepsize(), 127,
  534. result->getPixelPointer(), result->getStepsize(),
  535. makeROIFullImage(src0),ippAlphaPlus);
  536. if(ret!=ippStsNoErr)
  537. fthrow(ImageException, ippGetStatusString(ret));
  538. return result;
  539. #else // NICE_USELIB_IPP
  540. fthrow(ImageException,"Not yet supported without IPP.");
  541. #endif // NICE_USELIB_IPP
  542. }
  543. template<class P>
  544. ImageT<P>* DeprecatedConverter::mean(const ImageT<P>& src0, const ImageT<P>& src1, ImageT<P>* dst)
  545. {
  546. #ifdef NICE_USELIB_IPP
  547. ImageT<P> * result = createResultBuffer(src0.width(), src0.height(), dst);
  548. IppStatus ret = ippiAlphaCompC_C1R(src0.getPixelPointer(), src0.getStepsize(), 127,
  549. src1.getPixelPointer(), src1.getStepsize(), 127,
  550. result->getPixelPointer(), result->getStepsize(),
  551. makeROIFullImage(src0),ippAlphaPlus);
  552. if(ret!=ippStsNoErr)
  553. fthrow(ImageException, ippGetStatusString(ret));
  554. return result;
  555. #else // NICE_USELIB_IPP
  556. fthrow(ImageException,"Not yet supported without IPP.");
  557. #endif // NICE_USELIB_IPP
  558. }
  559. template <class P>
  560. VectorT<int>* DeprecatedConverter::histogramCumulative(const ImageT<P>& src, int min, int max, int levels)
  561. {
  562. VectorT<int> *histo = new VectorT<int>(levels);
  563. return DeprecatedConverter::histogramCumulative(src,histo,min,max);
  564. }
  565. template <class P>
  566. VectorT<int>* DeprecatedConverter::histogramCumulative(const ImageT<P>& src, VectorT<int> *histo, int min, int max)
  567. {
  568. DeprecatedConverter::histogram(src,histo,min,max);
  569. int *h=histo->getDataPointer();
  570. for(uint i=1;i<histo->size();i++,h++) {
  571. *(h+1) += *h;
  572. }
  573. return histo;
  574. }
  575. template <class P>
  576. VectorT<float>* DeprecatedConverter::histogramNormalized(const ImageT<P>& src, int min, int max, int levels)
  577. {
  578. VectorT<float> *histo = new VectorT<float>(levels);
  579. return DeprecatedConverter::histogramNormalized(src,histo,min,max);
  580. }
  581. template <class P>
  582. VectorT<float>* DeprecatedConverter::histogramNormalized(const ImageT<P>& src, VectorT<float> *histo, int min, int max)
  583. {
  584. VectorT<int> inthisto(histo->size());
  585. DeprecatedConverter::histogram(src,&inthisto,min,max);
  586. VectorT<int>::const_iterator it=inthisto.begin();
  587. VectorT<float>::iterator dst=histo->begin();
  588. float sum=0.0;
  589. for(;it!=inthisto.end();it++,dst++) {
  590. *dst=*it;
  591. sum+=*dst;
  592. }
  593. for(dst=histo->begin();dst!=histo->end();dst++) {
  594. *dst/=sum;
  595. }
  596. return histo;
  597. }
  598. template <class P>
  599. VectorT<int>* DeprecatedConverter::histogram(const ImageT<P>& src, int min, int max, int levels)
  600. {
  601. VectorT<int> *histo = new VectorT<int>(levels);
  602. return DeprecatedConverter::histogram(src,histo,min,max);
  603. }
  604. template <class P>
  605. VectorT<int>* DeprecatedConverter::histogram(const ImageT<P>& src, VectorT<int> *histo, int min, int max)
  606. {
  607. #ifdef NICE_USELIB_IPP
  608. int nolevels=histo->size()+1;
  609. int levels[nolevels];
  610. ippiHistogramEven_C1R(src.getPixelPointer(), src.getStepsize(), makeROIFullImage(src), histo->getDataPointer(), levels, nolevels, min, max);
  611. #else // NICE_USELIB_IPP
  612. fthrow(ImageException,"Not yet supported without IPP.");
  613. #endif // NICE_USELIB_IPP
  614. return histo;
  615. }
  616. template <class P>
  617. VectorT<int>* DeprecatedConverter::histogramCumulative(const ColorImageT<P>& src, int min, int max, int levels)
  618. {
  619. VectorT<int> *histo = new VectorT<int>(levels*3);
  620. return DeprecatedConverter::histogramCumulative(src,histo,min,max);
  621. }
  622. template <class P>
  623. VectorT<int>* DeprecatedConverter::histogramCumulative(const ColorImageT<P>& src, VectorT<int> *histo, int min, int max)
  624. {
  625. DeprecatedConverter::histogram(src,histo,min,max);
  626. int nolevels=histo->size()/3;
  627. int *h=histo->getDataPointer();
  628. for(int c=0;c<3;c++) {
  629. for(int i=1;i<nolevels;i++,h++) {
  630. *(h+1) += *h;
  631. }
  632. h++;
  633. }
  634. return histo;
  635. }
  636. template <class P>
  637. VectorT<float>* DeprecatedConverter::histogramNormalized(const ColorImageT<P>& src, int min, int max, int levels)
  638. {
  639. VectorT<float> *histo = new VectorT<float>(levels*3);
  640. return DeprecatedConverter::histogramNormalized(src,histo,min,max);
  641. }
  642. template <class P>
  643. VectorT<float>* DeprecatedConverter::histogramNormalized(const ColorImageT<P>& src, VectorT<float> *histo, int min, int max)
  644. {
  645. VectorT<int> inthisto(histo->size());
  646. DeprecatedConverter::histogram(src,&inthisto,min,max);
  647. int nolevels=histo->size()/3;
  648. int *it = inthisto.getDataPointer();
  649. float *dst = histo->getDataPointer();
  650. for(int c=0;c<3;c++) {
  651. float sum=0.0;
  652. for(int i=0;i<nolevels;i++,it++,dst++) {
  653. *dst = *it;
  654. sum += *dst;
  655. }
  656. dst=dst-nolevels;
  657. for(int i=0;i<nolevels;i++,dst++)
  658. *dst /= sum;
  659. }
  660. return histo;
  661. }
  662. template <class P>
  663. VectorT<int>* DeprecatedConverter::histogram(const ColorImageT<P>& src, int min, int max, int levels)
  664. {
  665. VectorT<int> *histo = new VectorT<int>(levels*3);
  666. return DeprecatedConverter::histogram(src,histo,min,max);
  667. }
  668. template <class P>
  669. VectorT<int>* DeprecatedConverter::histogram(const ColorImageT<P>& src, VectorT<int> *histo, int min, int max)
  670. {
  671. #ifdef NICE_USELIB_IPP
  672. int nolevels=histo->size()/3;
  673. int levels[(nolevels+1)*3];
  674. int pnolevels[3];
  675. int *phisto[3], *plevels[3], pmin[3], pmax[3];
  676. int *h=histo->getDataPointer();
  677. for(int i=0;i<3;i++) {
  678. plevels[i]=&levels[i*(nolevels+1)];
  679. phisto[i]=&h[i*nolevels];
  680. pmin[i]=min;
  681. pmax[i]=max;
  682. pnolevels[i]=nolevels+1;
  683. }
  684. ippiHistogramEven_C3R(src.getPixelPointer(), src.getStepsize(), makeROIFullImage(src), phisto, plevels, pnolevels, pmin, pmax);
  685. #else // NICE_USELIB_IPP
  686. fthrow(ImageException,"Not yet supported without IPP.");
  687. #endif // NICE_USELIB_IPP
  688. return histo;
  689. }
  690. template<class BUFFER>
  691. BUFFER* DeprecatedConverter::createResultBuffer(const int width,
  692. const int height,
  693. BUFFER* buffer) {
  694. BUFFER* result;
  695. if (buffer == NULL) {
  696. result = new BUFFER(width, height);
  697. } else if(buffer->width()==0 && buffer->height()==0) {
  698. result = buffer;
  699. result->resize(width,height);
  700. } else {
  701. result = buffer;
  702. if (result->width() != width || result->height() != height) {
  703. fthrow(ImageException,
  704. "DeprecatedConverter: size of image is not equal to (width, height).");
  705. }
  706. }
  707. return result;
  708. }
  709. template<class IMG, class BUFFER>
  710. BUFFER* DeprecatedConverter::createResultBuffer(const IMG& image, BUFFER* buffer)
  711. {
  712. BUFFER* result;
  713. if (buffer == NULL) {
  714. result = new BUFFER(image.width(), image.height());
  715. } else if(buffer->width()==0 && buffer->height()==0) {
  716. result = buffer;
  717. result->resize(image.width(),image.height());
  718. } else {
  719. result = buffer;
  720. if (result->width() != image.width()
  721. || result->height() != image.height()) {
  722. fthrow(ImageException,
  723. "DeprecatedConverter: size of image and buffer are not equal.");
  724. }
  725. }
  726. return result;
  727. }
  728. template<class P>
  729. ColorImage* DeprecatedConverter::signedImageToRGB(const ImageT<P>& image,
  730. ColorImage* colored) {
  731. //fthrow(ImageException, "signedImageToRGB() not supported for this pixel type");
  732. ColorImage *result = createResultBuffer(image, colored);
  733. P vmin;
  734. P vmax;
  735. image.minmax(vmin, vmax);
  736. const P scale = std::max(+vmax, -vmin);
  737. const Ipp8u black[3] = {0,0,0};
  738. *result = black;
  739. if (vmax == 0) {
  740. return result;
  741. }
  742. // FIXME not efficient
  743. for (int y = 0; y < image.height(); y++) {
  744. for (int x = 0; x < image.width(); x++) {
  745. const P pixel = image.getPixel(x, y);
  746. if (pixel < P(0)) {
  747. const Ipp8u newPixel = static_cast<Ipp8u>(-pixel * P(255) / scale);
  748. (*result)(x,y,0) = newPixel;
  749. } else {
  750. const Ipp8u newPixel = static_cast<Ipp8u>(pixel * P(255) / scale);
  751. (*result)(x,y,0) = newPixel;
  752. (*result)(x,y,1) = newPixel;
  753. (*result)(x,y,2) = newPixel;
  754. }
  755. }
  756. }
  757. return result;
  758. }
  759. }