ImageOperators.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. #ifndef _LIMUN_OPERATORST_H
  2. #define _LIMUN_OPERATORST_H
  3. // #ifdef NICE_USELIB_LIMUN_IOCOMPRESSION
  4. // #include <iocompression/gzbinstream.h>
  5. // #endif
  6. #include <core/basics/binstream.h>
  7. #include "core/image/ippwrapper.h"
  8. #include "core/image/Convert.h"
  9. #include "core/image/ImageT.h"
  10. #include "core/image/ImageT.h"
  11. #include <ostream>
  12. namespace NICE {
  13. template<class P>
  14. inline std::ostream& operator<<(std::ostream &out, const ImageT<P>& img)
  15. {
  16. for(int y=0;y<img.height();y++) {
  17. for(int x=0;x<img.width();x++) {
  18. out << img.getPixelQuick(x,y) << " ";
  19. }
  20. out << std::endl;
  21. }
  22. return out;
  23. }
  24. inline std::ostream& operator<<(std::ostream &out, const ImageT<Ipp8u>& img)
  25. {
  26. for(int y=0;y<img.height();y++) {
  27. for(int x=0;x<img.width();x++) {
  28. out << static_cast<int>(img.getPixelQuick(x,y)) << " ";
  29. }
  30. out << std::endl;
  31. }
  32. return out;
  33. }
  34. //#ifdef NICE_USELIB_ZLIB
  35. template<class P>
  36. inline obinstream& operator<<(obinstream &out, const ImageT<P>& img)
  37. {
  38. out << img.width() << img.height() << sizeof(P);
  39. for(int y=0;y<img.height();y++) {
  40. for(int x=0;x<img.width();x++) {
  41. out << img.getPixelQuick(x,y);
  42. }
  43. }
  44. return out;
  45. }
  46. template<class P>
  47. inline ibinstream& operator>>(ibinstream &in, ImageT<P>& img)
  48. {
  49. int width,height,bytedepth;
  50. in >> width >> height >> bytedepth;
  51. if(width!=img.width() || height!=img.height())
  52. img.resize(width,height);
  53. if(bytedepth!=sizeof(P))
  54. fthrow(ImageException, "wrong bitdepth while reading from file");
  55. for(int y=0;y<img.height();y++) {
  56. for(int x=0;x<img.width();x++) {
  57. P pixel;
  58. in >> pixel;
  59. img.setPixelQuick(x,y,pixel);
  60. }
  61. }
  62. return in;
  63. }
  64. //#endif
  65. template<class P>
  66. inline std::ostream& operator<<(std::ostream &out, const ColorImageT<P>& img)
  67. {
  68. for(int y=0;y<img.height();y++) {
  69. for(int x=0;x<img.width();x++) {
  70. out << "(" << img.getPixelQuick(x,y,0) << ","
  71. << img.getPixelQuick(x,y,1) << "," << img.getPixelQuick(x,y,2) << ") ";
  72. }
  73. out << std::endl;
  74. }
  75. return out;
  76. }
  77. inline std::ostream& operator<<(std::ostream &out, const ColorImageT<Ipp8u>& img)
  78. {
  79. for(int y=0;y<img.height();y++) {
  80. for(int x=0;x<img.width();x++) {
  81. out << "(" << (int)img.getPixelQuick(x,y,0) << ","
  82. << (int)img.getPixelQuick(x,y,1) << "," << (int)img.getPixelQuick(x,y,2) << ") ";
  83. }
  84. out << std::endl;
  85. }
  86. return out;
  87. }
  88. /**
  89. * @name Image Operators
  90. * \{
  91. */
  92. /**
  93. * Negates an GrayImage16s
  94. * @param a GrayImage16s to negate
  95. * @return GrayImage16s
  96. */
  97. inline GrayImage16s operator-(const GrayImage16s& a)
  98. {
  99. GrayImage16s result(a.width(),a.height());
  100. IppStatus ret = ippiMulC_C1RSfs(a.getPixelPointer(), a.getStepsize(),
  101. static_cast<Ipp16s>(-1),
  102. result.getPixelPointer(),
  103. result.getStepsize(), makeROIFullImage(a), 0); //IPP: 0 = no Scaling!
  104. if(ret!=ippStsNoErr)
  105. fthrow(ImageException, ippGetStatusString(ret));
  106. return result;
  107. }
  108. /**
  109. * Subtracts the FloatImage \c b from the FloatImage \c a and overwrite \c a
  110. * @param a source FloatImage
  111. * @param b FloatImage to subtract
  112. * @return FloatImage
  113. */
  114. inline FloatImage& operator-=(FloatImage& a, const FloatImage& b)
  115. {
  116. IppStatus ret = ippiSub_C1IR(b.getPixelPointer(), b.getStepsize(),
  117. a.getPixelPointer(), a.getStepsize(),
  118. makeROIFullImage(a));
  119. if(ret!=ippStsNoErr)
  120. fthrow(ImageException, ippGetStatusString(ret));
  121. return a;
  122. }
  123. /**
  124. * Subtracts the FloatImage \c b from the FloatImage \c a
  125. * @param a source FloatImage
  126. * @param b FloatImage to subtract
  127. * @return FloatImage
  128. */
  129. inline const FloatImage operator-(const FloatImage& a, const FloatImage& b)
  130. {
  131. FloatImage result(a.width(), a.height());
  132. IppStatus ret = ippiSub_C1R(b.getPixelPointer(), b.getStepsize(),
  133. a.getPixelPointer(), a.getStepsize(),
  134. result.getPixelPointer(), result.getStepsize(),
  135. makeROIFullImage(a));
  136. if(ret!=ippStsNoErr)
  137. fthrow(ImageException, ippGetStatusString(ret));
  138. return result;
  139. }
  140. /**
  141. * Subtracts the float constant \c c from all Pixels of the FloatImage \c a and overwrite \c a
  142. * @param a source FloatImage
  143. * @param c float constant to subtract
  144. * @return FloatImage
  145. */
  146. inline FloatImage& operator-=(FloatImage& a, Ipp32f c)
  147. {
  148. IppStatus ret = ippiSubC_C1IR(c, a.getPixelPointer(), a.getStepsize(), makeROIFullImage(a));
  149. if(ret!=ippStsNoErr)
  150. fthrow(ImageException, ippGetStatusString(ret));
  151. return a;
  152. }
  153. /**
  154. * Subtracts the float value \c c from all Pixels of the FloatImage \c a
  155. * @param a source FloatImage
  156. * @param c float value to subtract
  157. * @return FloatImage
  158. */
  159. inline const FloatImage operator-(const FloatImage& a, Ipp32f c)
  160. {
  161. FloatImage result(a.width(), a.height());
  162. IppStatus ret = ippiSubC_C1R(a.getPixelPointer(), a.getStepsize(), c,
  163. result.getPixelPointer(), result.getStepsize(), makeROIFullImage(a));
  164. if(ret!=ippStsNoErr)
  165. fthrow(ImageException, ippGetStatusString(ret));
  166. return result;
  167. }
  168. /**
  169. * Adds the FloatImage \c b to the FloatImage \c a and overwrite \c a
  170. * @param a source FloatImage
  171. * @param b FloatImage to add
  172. * @return FloatImage
  173. */
  174. inline FloatImage& operator+=(FloatImage& a, const FloatImage& b)
  175. {
  176. IppStatus ret = ippiAdd_C1IR(b.getPixelPointer(), b.getStepsize(),
  177. a.getPixelPointer(), a.getStepsize(), makeROIFullImage(a));
  178. if(ret!=ippStsNoErr)
  179. fthrow(ImageException, ippGetStatusString(ret));
  180. return a;
  181. }
  182. /**
  183. * Adds the integer constant \c c to the GrayImage16s \c a and overwrite \c a
  184. * @param a source GrayImage16s
  185. * @param c integer constant to add
  186. * @return GrayImage16s
  187. */
  188. inline GrayImage16s& operator+=(GrayImage16s& a, const Ipp16s& c)
  189. {
  190. IppStatus ret = ippiAddC_C1IRSfs(c, a.getPixelPointer(), a.getStepsize(), makeROIFullImage(a), 0);
  191. if(ret!=ippStsNoErr)
  192. fthrow(ImageException, ippGetStatusString(ret));
  193. return a;
  194. }
  195. /**
  196. * Adds the FloatImage \c b to the FloatImage \c a
  197. * @param a source FloatImage
  198. * @param b FloatImage to add
  199. * @return FloatImage
  200. */
  201. inline const FloatImage operator+(const FloatImage& a, const FloatImage& b)
  202. {
  203. FloatImage result(a.width(), a.height());
  204. IppStatus ret = ippiAdd_C1R(b.getPixelPointer(), b.getStepsize(),
  205. a.getPixelPointer(), a.getStepsize(),
  206. result.getPixelPointer(), result.getStepsize(),
  207. makeROIFullImage(a));
  208. if(ret!=ippStsNoErr)
  209. fthrow(ImageException, ippGetStatusString(ret));
  210. return result;
  211. }
  212. /**
  213. * Adds the float constant \c c to every pixel of the FloatImage \c a and overwrite \c a
  214. * @param a source FloatImage
  215. * @param c float constant to add
  216. * @return FloatImage
  217. */
  218. inline FloatImage& operator+=(FloatImage& a, Ipp32f c)
  219. {
  220. IppStatus ret = ippiAddC_C1IR(c, a.getPixelPointer(), a.getStepsize(), makeROIFullImage(a));
  221. if(ret!=ippStsNoErr)
  222. fthrow(ImageException, ippGetStatusString(ret));
  223. return a;
  224. }
  225. /**
  226. * Adds the float constant \c c to the FloatImage \c a
  227. * @param a source FloatImage
  228. * @param c float constant to add
  229. * @return FloatImage
  230. */
  231. inline const FloatImage operator+(const FloatImage& a, Ipp32f c)
  232. {
  233. FloatImage result(a.width(), a.height());
  234. IppStatus ret = ippiAddC_C1R(a.getPixelPointer(), a.getStepsize(), c,
  235. result.getPixelPointer(), result.getStepsize(),
  236. makeROIFullImage(a));
  237. if(ret!=ippStsNoErr)
  238. fthrow(ImageException, ippGetStatusString(ret));
  239. return result;
  240. }
  241. /**
  242. * Multiply the FloatImage \c b with the FloatImage \c a and overwrite \c a
  243. * @param a source FloatImage
  244. * @param b FloatImage to multiply with
  245. * @return FloatImage
  246. */
  247. inline FloatImage& operator*=(FloatImage& a, const FloatImage& b)
  248. {
  249. IppStatus ret = ippiMul_C1IR(b.getPixelPointer(), b.getStepsize(),
  250. a.getPixelPointer(), a.getStepsize(),
  251. makeROIFullImage(a));
  252. if(ret!=ippStsNoErr)
  253. fthrow(ImageException, ippGetStatusString(ret));
  254. return a;
  255. }
  256. /**
  257. * Multiply the FloatImage \c b with the FloatImage \c a
  258. * @param a source FloatImage
  259. * @param b FloatImage to multiply with
  260. * @return FloatImage
  261. */
  262. inline const FloatImage operator*(const FloatImage& a, const FloatImage& b)
  263. {
  264. FloatImage result(a.width(), a.height());
  265. IppStatus ret = ippiMul_C1R(b.getPixelPointer(), b.getStepsize(),
  266. a.getPixelPointer(), a.getStepsize(),
  267. result.getPixelPointer(), result.getStepsize(), makeROIFullImage(a));
  268. if(ret!=ippStsNoErr)
  269. fthrow(ImageException, ippGetStatusString(ret));
  270. return result;
  271. }
  272. /**
  273. * Multiply the float constant \c c with the FloatImage \c a and overwrite \c a
  274. * @param a source FloatImage
  275. * @param c float constant to multiply with
  276. * @return FloatImage
  277. */
  278. inline FloatImage& operator*=(FloatImage& a, Ipp32f c)
  279. {
  280. IppStatus ret = ippiMulC_C1IR(c, a.getPixelPointer(), a.getStepsize(), makeROIFullImage(a));
  281. if(ret!=ippStsNoErr)
  282. fthrow(ImageException, ippGetStatusString(ret));
  283. return a;
  284. }
  285. /**
  286. * Multiply the gray constant \c c with the Image \c a and overwrite \c a
  287. * @param a source Image
  288. * @param c gray constant to multiply with
  289. * @return Image
  290. */
  291. inline Image operator*=(Image& a, Ipp8u c)
  292. {
  293. IppStatus ret = ippiMulC_C1IRSfs(c, a.getPixelPointer(), a.getStepsize(), makeROIFullImage(a),0);
  294. if(ret!=ippStsNoErr)
  295. fthrow(ImageException, ippGetStatusString(ret));
  296. return a;
  297. }
  298. /**
  299. * Multiply the float constant \c c with the FloatImage \c a
  300. * @param a source FloatImage
  301. * @param c float constant to multiply with
  302. * @return FloatImage
  303. */
  304. inline const FloatImage operator*(const FloatImage& a, Ipp32f c)
  305. {
  306. FloatImage result(a.width(), a.height());
  307. IppStatus ret = ippiMulC_C1R(a.getPixelPointer(), a.getStepsize(), c,
  308. result.getPixelPointer(), result.getStepsize(), makeROIFullImage(a));
  309. if(ret!=ippStsNoErr)
  310. fthrow(ImageException, ippGetStatusString(ret));
  311. return result;
  312. }
  313. /**
  314. * Multiply the gray constant \c c with the Image \c a
  315. * @param a source Image
  316. * @param c gray constant to multiply with
  317. * @return Image
  318. */
  319. inline const Image operator*(const Image& a, Ipp8u c)
  320. {
  321. Image result(a.width(), a.height());
  322. IppStatus ret = ippiMulC_C1RSfs(a.getPixelPointer(), a.getStepsize(), c,
  323. result.getPixelPointer(), result.getStepsize(),
  324. makeROIFullImage(a),0);
  325. if(ret!=ippStsNoErr)
  326. fthrow(ImageException, ippGetStatusString(ret));
  327. return result;
  328. }
  329. /**
  330. * Divides the FloatImage \c a by FloatImage \c a and overwrite \c a
  331. * @param a source FloatImage
  332. * @param b FloatImage to divide by
  333. * @return FloatImage
  334. * @example image_operator.cpp
  335. */
  336. inline FloatImage& operator/=(FloatImage& a, const FloatImage& b)
  337. {
  338. IppStatus ret = ippiDiv_C1IR(b.getPixelPointer(), b.getStepsize(),
  339. a.getPixelPointer(), a.getStepsize(), makeROIFullImage(a));
  340. if(ret!=ippStsNoErr)
  341. fthrow(ImageException, ippGetStatusString(ret));
  342. return a;
  343. }
  344. /**
  345. * Divides the FloatImage \c a by FloatImage \c a
  346. * @param a source FloatImage
  347. * @param b FloatImage to divide by
  348. * @return FloatImage
  349. */
  350. inline const FloatImage operator/(const FloatImage& a, const FloatImage& b)
  351. {
  352. FloatImage result(a.width(), a.height());
  353. IppStatus ret = ippiDiv_C1R(b.getPixelPointer(), b.getStepsize(),
  354. a.getPixelPointer(), a.getStepsize(),
  355. result.getPixelPointer(), result.getStepsize(),
  356. makeROIFullImage(a));
  357. if(ret!=ippStsNoErr)
  358. fthrow(ImageException, ippGetStatusString(ret));
  359. return result;
  360. }
  361. /**
  362. * Divides the FloatImage \c a by the the float constant \c c and overwrite \c a
  363. * @param a source FloatImage
  364. * @param c float constant to devide by
  365. * @return FloatImage
  366. */
  367. inline FloatImage& operator/=(FloatImage& a, Ipp32f c)
  368. {
  369. IppStatus ret = ippiDivC_C1IR(c, a.getPixelPointer(), a.getStepsize(), makeROIFullImage(a));
  370. if(ret!=ippStsNoErr)
  371. fthrow(ImageException, ippGetStatusString(ret));
  372. return a;
  373. }
  374. /**
  375. * Divides the FloatImage \c a by the the float constant \c c
  376. * @param a source FloatImage
  377. * @param c float constant to devide by
  378. * @return FloatImage
  379. */
  380. inline const FloatImage operator/(const FloatImage& a, Ipp32f c)
  381. {
  382. FloatImage result(a.width(), a.height());
  383. IppStatus ret = ippiDivC_C1R(a.getPixelPointer(), a.getStepsize(), c,
  384. result.getPixelPointer(), result.getStepsize(),
  385. makeROIFullImage(a));
  386. if(ret!=ippStsNoErr)
  387. fthrow(ImageException, ippGetStatusString(ret));
  388. return result;
  389. }
  390. /**
  391. * \}
  392. */
  393. } // namespace
  394. #endif // _LIMUN_OPERATORST_H