image_convertice.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /*
  2. * NICE-Core - efficient algebra and computer vision methods
  3. * - libicelink - An icelink/template for new NICE libraries
  4. * See file License for license information.
  5. */
  6. /*****************************************************************************/
  7. /**
  8. * @file convertice.h
  9. * Conversion to/from ICE images.
  10. * @note
  11. * This file requires the ICE library.
  12. * However, that library is NOT defined as a dependency in the
  13. * limun image library. If you use this file, you have to use ICE, too.
  14. * This shouldn't be a problem, as this file is only useful if you are using
  15. * ICE already.
  16. */
  17. #ifndef _CONVERTICE_IMAGE_H
  18. #define _CONVERTICE_IMAGE_H
  19. #include "core/image/ImageT.h"
  20. #include "core/image/ColorImageT.h"
  21. #include "core/basics/types.h"
  22. // ICE
  23. #include <image_nonvis.h>
  24. namespace NICE
  25. {
  26. /**
  27. * For internal use.
  28. */
  29. inline unsigned char invert255(unsigned char value, bool invert)
  30. {
  31. if (invert)
  32. {
  33. return 255 - value;
  34. }
  35. else
  36. {
  37. return value;
  38. }
  39. }
  40. /**
  41. * Create a new \c NICE::Image from an \c ice::Image.
  42. * @param source The source image.
  43. * @param invertImage Invert the image? Note: ICE works with inverted images.
  44. * @param memoryLayout The memory layout for the new \c NICE::Image.
  45. * @return The new \c NICE::Image (ownership given away!)
  46. */
  47. inline NICE::Image*
  48. createGrayImage(const ice::Image& source,
  49. bool invertImage = true,
  50. NICE::GrayColorImageCommonImplementation::MemoryLayout memoryLayout
  51. = NICE::GrayColorImageCommonImplementation::ippAlignment)
  52. {
  53. // auto_ptr for exception safety
  54. auto_ptr< Image >
  55. result(new Image(source->xsize, source->ysize, memoryLayout));
  56. for (int y = 0; y < source->ysize; y++)
  57. {
  58. Image::Pixel* cursor = result->getPixelPointerY(y);
  59. for (int x = 0; x < source->xsize; x++)
  60. {
  61. // FIXME GetVal_nocheck ?!
  62. *cursor = invert255(GetVal(source, x, y), invertImage);
  63. cursor++;
  64. }
  65. }
  66. return result.release();
  67. }
  68. /**
  69. * Copy an ice::Image to a NICE::Image
  70. * @param source The source image
  71. * @param dst The destination image
  72. * @param invertImage Invert the image? Note: ICE works with inverted images.
  73. *
  74. */
  75. inline void
  76. copyGrayImage(const ice::Image& source,
  77. NICE::Image& dst,
  78. bool invertImage = true)
  79. {
  80. dst.resize(source->xsize, source->ysize);
  81. for (int y = 0; y < source->ysize; y++)
  82. {
  83. Image::Pixel* cursor = dst.getPixelPointerY(y);
  84. for (int x = 0; x < source->xsize; x++)
  85. {
  86. *cursor = invert255(GetVal(source, x, y), invertImage);
  87. cursor++;
  88. }
  89. }
  90. }
  91. /**
  92. * Create a new \c ice::Image from an \c NICE::Image.
  93. * @param source The source image.
  94. * @param invertImage Invert the image? Note: ICE works with inverted images.
  95. * @return The new \c ice::Image (ownership given away!)
  96. */
  97. inline ice::Image createIceImage(const NICE::Image& source,
  98. bool invertImage = true)
  99. {
  100. ice::Image result = ice::NewImg(source.width(), source.height(), 255);
  101. for (int y = 0; y < source.height(); y++)
  102. {
  103. const Image::Pixel* cursor = source.getPixelPointerY(y);
  104. for (int x = 0; x < source.width(); x++)
  105. {
  106. // FIXME PutVal_nocheck ?!
  107. PutVal(result, x, y, invert255(*cursor, invertImage));
  108. cursor++;
  109. }
  110. }
  111. return result;
  112. }
  113. /**
  114. * Create a new \c NICE::ColorImage from an \c ice::ImageRGB.
  115. * @param source The source image.
  116. * @param invertImage Invert the image? Note: ICE works with inverted images.
  117. * @param memoryLayout The memory layout for the new \c NICE::ColorImage.
  118. * @return The new \c NICE::ColorImage (ownership given away!)
  119. */
  120. inline NICE::ColorImage*
  121. createColorImage(const ice::ColorImage &source, // const ice::ImageRGB& source,
  122. bool invertImage = true,
  123. NICE::GrayColorImageCommonImplementation::MemoryLayout memoryLayout
  124. = NICE::GrayColorImageCommonImplementation::ippAlignment)
  125. {
  126. // auto_ptr for exception safety
  127. auto_ptr< ColorImage >
  128. //result(new ColorImage(source.xsize(), source.ysize(), memoryLayout));
  129. result(new ColorImage(source.redImage()->xsize,
  130. source.redImage()->ysize,
  131. memoryLayout));
  132. for (int y = 0; y < source.redImage()->ysize; y++)
  133. {
  134. ColorImage::Pixel* cursor = result->getPixelPointerY(y);
  135. for (int x = 0; x < source.redImage()->xsize; x++)
  136. {
  137. // FIXME GetVal_nocheck ?!
  138. *cursor = invert255(GetVal(source.redImage(), x, y), invertImage);
  139. cursor++;
  140. *cursor = invert255(GetVal(source.greenImage(), x, y), invertImage);
  141. cursor++;
  142. *cursor = invert255(GetVal(source.blueImage(), x, y), invertImage);
  143. cursor++;
  144. }
  145. }
  146. return result.release();
  147. }
  148. /**
  149. * Create a new \c ice::ImageRGB from an \c NICE::ColorImage.
  150. * @param source The source image.
  151. * @param invertImage Invert the image? Note: ICE works with inverted images.
  152. * @return The new \c ice::ImageRGB (ownership given away!)
  153. */
  154. inline ice::ColorImage* createIceImageRGB(const ice::ColorImage &source, // const NICE::ColorImage& source,
  155. bool invertImage = true)
  156. {
  157. ice::ColorImage* result = new ice::ColorImage();
  158. result->create(source.xsize, source.ysize, source.maxval);
  159. for (int y = 0; y < source.ysize; y++)
  160. {
  161. // const Image::Pixel* cursor = source.getPixelPointerY(y);
  162. for (int x = 0; x < source.xsize; x++)
  163. {
  164. // PutVal(result->redImage(), x, y, invert255(*cursor, invertImage));
  165. // cursor++;
  166. // PutVal(result->greenImage(), x, y, invert255(*cursor, invertImage));
  167. // cursor++;
  168. // PutVal(result->blueImage(), x, y, invert255(*cursor, invertImage));
  169. // cursor++;
  170. ice::ColorValue valSrc = source.getPixel(x, y);
  171. ice::ColorValue valDst(invert255(valSrc.red, invertImage),
  172. invert255(valSrc.green, invertImage),
  173. invert255(valSrc.blue, invertImage));
  174. source.setPixel(x, y, valDst);
  175. }
  176. }
  177. return result;
  178. }
  179. /**
  180. * Create a new \c NICE::FloatImage from an \c ice::ImageD.
  181. * @param source The source FloatImage.
  182. * @param memoryLayout The memory layout for the new \c NICE::FloatImage.
  183. * @return The new \c NICE::FloatImage (ownership given away!)
  184. */
  185. template<class T>
  186. inline NICE::FloatImage*
  187. createFloatImage(const ice::ImageF< T >& source)
  188. {
  189. // auto_ptr for exception safety
  190. auto_ptr< FloatImage >
  191. result(new FloatImage(source->xsize, source->ysize));
  192. for (int y = 0; y < source->ysize; y++)
  193. {
  194. FloatImage::Pixel* cursor = result->getPixelPointerY(y);
  195. for (int x = 0; x < source->xsize; x++)
  196. {
  197. // FIXME GetVal_nocheck ?!
  198. *cursor = GetValD(source, x, y);
  199. cursor++;
  200. }
  201. }
  202. return result.release();
  203. }
  204. /**
  205. * Copy an ice::ImageD to a NICE::FloatImage
  206. * @param source The source FloatImage
  207. * @param dst The destination FloatImage
  208. */
  209. inline void
  210. copyFloatImage(const ice::ImageD& source,
  211. NICE::FloatImage& dst)
  212. {
  213. dst.resize(source.xsize, source.ysize);
  214. for (int y = 0; y < source.ysize; y++)
  215. {
  216. FloatImage::Pixel* cursor = dst.getPixelPointerY(y);
  217. for (int x = 0; x < source.xsize; x++)
  218. {
  219. *cursor = GetValD(source, x, y);
  220. cursor++;
  221. }
  222. }
  223. }
  224. /**
  225. * Create a new \c ice::ImageD from an \c NICE::FloatImage.
  226. * @param source The source FloatImage.
  227. * @return The new \c ice::ImageD (ownership given away!)
  228. */
  229. inline ice::ImageD createIceImageD(const NICE::FloatImage& source
  230. )
  231. {
  232. ice::ImageD result = ice::NewImgD(source.width(), source.height());
  233. for (int y = 0; y < source.height(); y++)
  234. {
  235. const FloatImage::Pixel* cursor = source.getPixelPointerY(y);
  236. for (int x = 0; x < source.width(); x++)
  237. {
  238. // FIXME PutVal_nocheck ?!
  239. PutValD(result, x, y, *cursor);
  240. cursor++;
  241. }
  242. }
  243. return result;
  244. }
  245. }
  246. ;
  247. // namespace
  248. #endif /* _CONVERTICE_IMAGE_H */