ImageT.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. #ifdef WIN32
  77. Pixel *line = new Pixel[header.width*header.channel];
  78. file.read((char *)line,header.width*header.channel);
  79. Pixel *target = getPixelPointerY(y);
  80. Pixel *src = line;
  81. for (int x = 0; x < header.width; x++) {
  82. // // with lookup table (see firewire ColorConvert)
  83. // *target = pLutRg[*src]+pLutGg[*(src+1)]+pLutBg[*(src+2)];
  84. // *target++ += (pLutRgr[*src]+pLutGgr[*(src+1)]+pLutBgr[*(src+2)])/64;
  85. // src=src+3;
  86. *target++ = static_cast<Pixel>(*src*0.299+*(src+1)*0.587+*(src+2)*0.114);
  87. src=src+3;
  88. }
  89. delete [] line;
  90. #else
  91. Pixel line[header.width*header.channel];
  92. file.read((char *)line,header.width*header.channel);
  93. Pixel *target = getPixelPointerY(y);
  94. Pixel *src = line;
  95. for (int x = 0; x < header.width; x++) {
  96. // // with lookup table (see firewire ColorConvert)
  97. // *target = pLutRg[*src]+pLutGg[*(src+1)]+pLutBg[*(src+2)];
  98. // *target++ += (pLutRgr[*src]+pLutGgr[*(src+1)]+pLutBgr[*(src+2)])/64;
  99. // src=src+3;
  100. *target++ = static_cast<Pixel>(*src*0.299+*(src+1)*0.587+*(src+2)*0.114);
  101. src=src+3;
  102. }
  103. #endif
  104. }
  105. #endif
  106. } else {
  107. fthrow(ImageException,"Format not yet implemented.");
  108. }
  109. }
  110. template <>
  111. void ImageT<Ipp8u>::writePGM(const char* pgmFileName) const {
  112. using namespace std;
  113. ofstream file(pgmFileName, ios::binary);
  114. file << "P5" << endl << m_xsize <<" "<< m_ysize << endl << 255 << endl;
  115. for(int y = 0; y < m_ysize; y++) {
  116. file.write((char *)getPixelPointerY(y),m_xsize);
  117. }
  118. }
  119. template<>
  120. void ImageT<Ipp8u>::invert()
  121. {
  122. #ifdef NICE_USELIB_IPP
  123. int values[]={255,0,0};
  124. int levels[]={0,255,256};
  125. IppiSize ippiSize = {this->width(), this->height() };
  126. IppStatus ret = ippiLUT_Linear_C1IR(this->getPixelPointer(),
  127. this->getStepsize(),
  128. ippiSize, values, levels, 3);
  129. if(ret!=ippStsNoErr)
  130. fthrow(ImageException, ippGetStatusString(ret));
  131. #else
  132. Ipp8u* p;
  133. for(int y=0; y<this->height(); ++y)
  134. {
  135. p = this->getPixelPointerY(y);
  136. for(int x=0; x<this->width(); ++x,++p)
  137. *p = 255-*p;
  138. }
  139. #endif
  140. }
  141. } // namespace