ImageT.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. * LImUn - Library for ImageT Understanding
  3. * - libimage - An ImageT library
  4. * See file License for license information.
  5. */
  6. #include "core/image/Convert.h"
  7. #include "core/image/ImageT.h"
  8. #include "core/image/ColorImageT.h"
  9. #include <fstream>
  10. #include <sstream>
  11. namespace NICE {
  12. #ifdef NICE_USELIB_IPP
  13. template <>
  14. void ImageT<Ipp8u>::doAllocPixelIPP()
  15. {
  16. setPixelPointer(ippiMalloc_8u_C1(m_xsize, m_ysize, &m_rowStepsize));
  17. m_columnStepsize = sizeof(Ipp8u);
  18. }
  19. template <>
  20. void ImageT<Ipp16s>::doAllocPixelIPP() {
  21. setPixelPointer(ippiMalloc_16s_C1(m_xsize, m_ysize, &m_rowStepsize));
  22. m_columnStepsize = sizeof(Ipp16s);
  23. }
  24. template <>
  25. void ImageT<Ipp32f>::doAllocPixelIPP() {
  26. setPixelPointer(ippiMalloc_32f_C1(m_xsize, m_ysize, &m_rowStepsize));
  27. m_columnStepsize = sizeof(Ipp32f);
  28. }
  29. #endif //NICE_USELIB_IPP
  30. #ifdef NICE_USELIB_IPP
  31. template <>
  32. void ImageT<Ipp8u>::doFromRaw(const Pixel* raw, const int stepsize) {
  33. IppiSize roi = {width(), height()};
  34. ippiCopy_8u_C1R(raw, stepsize, getPixelPointer(), getStepsize(), roi);
  35. }
  36. template <>
  37. void ImageT<Ipp16s>::doFromRaw(const Pixel* raw, const int stepsize) {
  38. IppiSize roi = {width(), height()};
  39. ippiCopy_16s_C1R(raw, stepsize, getPixelPointer(), getStepsize(), roi);
  40. }
  41. template <>
  42. void ImageT<Ipp32f>::doFromRaw(const Pixel* raw, const int stepsize) {
  43. IppiSize roi = {width(), height()};
  44. ippiCopy_32f_C1R(raw, stepsize, getPixelPointer(), getStepsize(), roi);
  45. }
  46. #endif //NICE_USELIB_IPP
  47. template <>
  48. void ImageT<Ipp8u>::readPGM(const std::string& pgmFileName,
  49. const GrayColorImageCommonImplementation::MemoryLayout _memoryLayout) {
  50. // open the file
  51. using namespace std;
  52. ifstream file(pgmFileName.c_str(), ios::binary);
  53. // error handling
  54. if(!file.good()) {
  55. fthrow(ImageException,
  56. string("ImageT<P>::readPGM: Cannot open ") + pgmFileName);
  57. }
  58. PXMImageHeaderInfo header=getPXMHeaderInfo(file);
  59. allocPixel(header.width, header.height, _memoryLayout);
  60. file.seekg(header.datapos);
  61. if(header.format==PGM_RAW) {
  62. for(int y = 0; y < header.height; y++) {
  63. file.read((char *)(getPixelPointerY(y)),header.width*header.channel);
  64. }
  65. } else if(header.format==PPM_RAW) {
  66. #ifdef NICE_USELIB_IPP
  67. Pixel line[header.height*header.width*header.channel];
  68. file.read((char *)line,header.height*header.width*header.channel);
  69. Pixel *target = getPixelPointer();
  70. IppiSize region;
  71. region.width = header.width;
  72. region.height = header.height;
  73. ippiRGBToGray_8u_C3C1R(line, header.width*header.channel, target, header.width, region);
  74. #else
  75. for (int y = 0; y < header.height; y++) {
  76. Pixel line[header.width*header.channel];
  77. file.read((char *)line,header.width*header.channel);
  78. Pixel *target = getPixelPointerY(y);
  79. Pixel *src = line;
  80. for (int x = 0; x < header.width; x++) {
  81. // // with lookup table (see firewire ColorConvert)
  82. // *target = pLutRg[*src]+pLutGg[*(src+1)]+pLutBg[*(src+2)];
  83. // *target++ += (pLutRgr[*src]+pLutGgr[*(src+1)]+pLutBgr[*(src+2)])/64;
  84. // src=src+3;
  85. *target++ = static_cast<Pixel>(*src*0.299+*(src+1)*0.587+*(src+2)*0.114);
  86. src=src+3;
  87. }
  88. }
  89. #endif
  90. } else {
  91. fthrow(ImageException,"Format not yet implemented.");
  92. }
  93. }
  94. template <>
  95. void ImageT<Ipp8u>::writePGM(const char* pgmFileName) const {
  96. using namespace std;
  97. ofstream file(pgmFileName, ios::binary);
  98. file << "P5" << endl << m_xsize <<" "<< m_ysize << endl << 255 << endl;
  99. for(int y = 0; y < m_ysize; y++) {
  100. file.write((char *)getPixelPointerY(y),m_xsize);
  101. }
  102. }
  103. template<>
  104. void ImageT<Ipp8u>::invert()
  105. {
  106. #ifdef NICE_USELIB_IPP
  107. int values[]={255,0,0};
  108. int levels[]={0,255,256};
  109. IppiSize ippiSize = {this->width(), this->height() };
  110. IppStatus ret = ippiLUT_Linear_C1IR(this->getPixelPointer(),
  111. this->getStepsize(),
  112. ippiSize, values, levels, 3);
  113. if(ret!=ippStsNoErr)
  114. fthrow(ImageException, ippGetStatusString(ret));
  115. #else
  116. Ipp8u* p;
  117. for(int y=0; y<this->height(); ++y)
  118. {
  119. p = this->getPixelPointerY(y);
  120. for(int x=0; x<this->width(); ++x,++p)
  121. *p = 255-*p;
  122. }
  123. #endif
  124. }
  125. } // namespace