PageRenderTime 123ms CodeModel.GetById 8ms RepoModel.GetById 0ms app.codeStats 0ms

/CODE2/74iCubMain/lib/loadcvimage.cpp

https://github.com/yianni/cappocacciaactivevision
C++ | 136 lines | 96 code | 32 blank | 8 comment | 60 complexity | 26077b0b2f06bc9fc5df35ef04c5e47c MD5 | raw file
  1. #include "loadcvimage.h"
  2. #include <sys/stat.h>
  3. LoadCVImage::LoadCVImage() {
  4. mp_cvimg = NULL;
  5. m_checkfileexistence = false;
  6. //debug = true;
  7. }
  8. LoadCVImage::~LoadCVImage() {
  9. if(mp_cvimg) delete mp_cvimg;
  10. }
  11. ///ORIGINAL
  12. //CVImage* LoadCVImage::load(const std::string& filename) {
  13. CVImage* LoadCVImage::load(const std::string& filename) {
  14. if(m_checkfileexistence) {
  15. struct stat buffer;
  16. if(stat(filename.c_str(), &buffer)) { std::cerr << "LoadCVImage::load()::ERROR::File [" << filename << "] does not exist!\n"; return NULL; }
  17. //else if(debug) { std::cout << "LoadCVImage::load()::file [" << filename << "] exists!\n"; }
  18. }
  19. IplImage* tempimg = cvLoadImage( filename.c_str(), -1);
  20. if(!tempimg) { std::cerr << "LoadCVImage::load()::ERROR::ERROR opening file [" << filename << "]\n"; return NULL; }
  21. if(mp_cvimg) {
  22. if(mp_cvimg->width != tempimg->width || mp_cvimg->height != tempimg->height || mp_cvimg->ipl->nChannels != tempimg->nChannels || mp_cvimg->ipl->depth != tempimg->depth) {
  23. delete mp_cvimg;
  24. mp_cvimg = NULL;
  25. }
  26. }
  27. if(!mp_cvimg) {
  28. int cvMatType;
  29. if(tempimg->nChannels == 3){
  30. cvMatType = CV_8UC3;
  31. }
  32. else if(tempimg->nChannels == 1) {
  33. cvMatType = CV_8UC1;
  34. }
  35. mp_cvimg = new CVImage(cvSize(tempimg->width, tempimg->height), cvMatType, 0);
  36. if(!mp_cvimg) { std::cerr << "LoadCVImage::load()::ERROR::Failed to create new CVImage\n"; return NULL; }
  37. }
  38. int datasize = (tempimg->widthStep)*(tempimg->height);
  39. memcpy(mp_cvimg->ipl->imageData, tempimg->imageData, datasize );
  40. cvReleaseImage(&tempimg);
  41. if(tempimg) { std::cerr << "LoadCVImage::load()::ERROR::Failed to release IplImage\n"; return NULL; }
  42. //if(debug) { std::cout << "LoadCVImage::load()::finished\n"; }
  43. return mp_cvimg;
  44. }
  45. CVImage* LoadCVImage::load(IplImage* InputImage) {
  46. if(m_checkfileexistence) {
  47. if(!InputImage) { std::cerr << "LoadCVImage::load()::ERROR::File does not exist!\n"; return NULL; }
  48. //else if(debug) { std::cout << "LoadCVImage::load()::file [" << filename << "] exists!\n"; }
  49. }
  50. IplImage* tempimg = cvCloneImage(InputImage);
  51. if(!tempimg) { std::cerr << "LoadCVImage::load()::ERROR::ERROR opening file ]\n"; return NULL; }
  52. if(mp_cvimg) {
  53. if(mp_cvimg->width != tempimg->width || mp_cvimg->height != tempimg->height || mp_cvimg->ipl->nChannels != tempimg->nChannels || mp_cvimg->ipl->depth != tempimg->depth) {
  54. delete mp_cvimg;
  55. mp_cvimg = NULL;
  56. }
  57. }
  58. if(!mp_cvimg) {
  59. int cvMatType;
  60. if(tempimg->nChannels == 3){
  61. cvMatType = CV_8UC3;
  62. }
  63. else if(tempimg->nChannels == 1) {
  64. cvMatType = CV_8UC1;
  65. }
  66. mp_cvimg = new CVImage(cvSize(tempimg->width, tempimg->height), cvMatType, 0);
  67. if(!mp_cvimg) { std::cerr << "LoadCVImage::load()::ERROR::Failed to create new CVImage\n"; return NULL; }
  68. }
  69. int datasize = (tempimg->widthStep)*(tempimg->height);
  70. memcpy(mp_cvimg->ipl->imageData, tempimg->imageData, datasize );
  71. cvReleaseImage(&tempimg);
  72. if(tempimg) { std::cerr << "LoadCVImage::load()::ERROR::Failed to release IplImage\n"; return NULL; }
  73. //if(debug) { std::cout << "LoadCVImage::load()::finished\n"; }
  74. return mp_cvimg;
  75. }
  76. CVImage* LoadCVImage::loadfromcamera(IplImage* image){
  77. IplImage* tempimg = cvCloneImage(image);
  78. if(!tempimg) { std::cerr << "LoadCVImage::load()::ERROR::ERROR loading tempimg\n"; return NULL; }
  79. if(mp_cvimg) {
  80. if(mp_cvimg->width != tempimg->width || mp_cvimg->height != tempimg->height || mp_cvimg->ipl->nChannels != tempimg->nChannels || mp_cvimg->ipl->depth != tempimg->depth) {
  81. delete mp_cvimg;
  82. mp_cvimg = NULL;
  83. }
  84. }
  85. if(!mp_cvimg) {
  86. int cvMatType;
  87. if(tempimg->nChannels == 3){
  88. cvMatType = CV_8UC3;
  89. }
  90. else if(tempimg->nChannels == 1) {
  91. cvMatType = CV_8UC1;
  92. }
  93. mp_cvimg = new CVImage(cvSize(tempimg->width, tempimg->height), cvMatType, 0);
  94. if(!mp_cvimg) { std::cerr << "LoadCVImage::load()::ERROR::Failed to create new CVImage\n"; return NULL; }
  95. }
  96. int datasize = (tempimg->widthStep)*(tempimg->height);
  97. memcpy(mp_cvimg->ipl->imageData, tempimg->imageData, datasize );
  98. cvReleaseImage(&tempimg);
  99. cvReleaseImage(&image);
  100. if(tempimg) { std::cerr << "LoadCVImage::load()::ERROR::Failed to release tempimg\n"; return NULL; }
  101. if(image) { std::cerr << "LoadCVImage::load()::ERROR::Failed to release image\n"; return NULL; }
  102. //if(debug) { std::cout << "LoadCVImage::load()::finished\n"; }
  103. return mp_cvimg;
  104. }