PageRenderTime 65ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/src/plvopencv/ImageLoader.cpp

https://github.com/NiekHoeijmakers/parlevision
C++ | 206 lines | 124 code | 40 blank | 42 comment | 26 complexity | ad6bbe6d500cea918d5781e4591c4299 MD5 | raw file
Possible License(s): GPL-3.0, LGPL-3.0
  1. /**
  2. * Copyright (C)2010 by Michel Jansen and Richard Loos
  3. * All rights reserved.
  4. *
  5. * This file is part of the plvopencv module of ParleVision.
  6. *
  7. * ParleVision is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * ParleVision is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * A copy of the GNU General Public License can be found in the root
  18. * of this software package directory in the file LICENSE.LGPL.
  19. * If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. #include "ImageLoader.h"
  22. #include <plvcore/CvMatData.h>
  23. #include <QMutexLocker>
  24. #include <QDebug>
  25. #include <opencv/cv.h>
  26. #include <opencv/highgui.h>
  27. #include <string>
  28. #include <plvcore/Pin.h>
  29. using namespace plv;
  30. using namespace plvopencv;
  31. //--- Constructor & Destructor ------------------------------------------------
  32. ImageLoader::ImageLoader()
  33. {
  34. //create the output pin
  35. m_outputPin = new OutputPin<OpenCVImage>("image_output", this );
  36. addOutputPin( m_outputPin.getPtr() );
  37. m_directory = "c:/";
  38. m_filename = "img1001.bmp";
  39. m_isReady = false;
  40. //Attempt to load the image
  41. //m_isReady = ObtainImage(m_directory, m_filename);
  42. }
  43. ImageLoader::~ImageLoader()
  44. {
  45. }
  46. //--- ImageLoader methods -----------------------------------------------------
  47. void ImageLoader::setFilename(QString filename)
  48. {
  49. m_filename.clear();
  50. m_filename = filename;
  51. qDebug() << "New filename selected:" << m_filename;
  52. //Validate the filename
  53. if(m_filename.length() >= 4 && validateExtension(m_filename))
  54. {
  55. m_isReady = ObtainImage(m_directory, m_filename);
  56. }
  57. else
  58. {
  59. qDebug() << "New filename not valid!";
  60. m_isReady = false;
  61. }
  62. emit( filenameChanged(m_filename) );
  63. }
  64. void ImageLoader::setDirectory(QString directory)
  65. {
  66. m_directory.clear();
  67. //replace all '\' characters with '/' characters
  68. m_directory = directory.replace('\\','/');
  69. qDebug() << "New directory selected:" << m_directory;
  70. //validate the directory
  71. if(m_directory.length() >= 3){
  72. m_isReady = ObtainImage(m_directory, m_filename);
  73. }
  74. else{
  75. qDebug() << "New directory not a valid directory!";
  76. m_isReady = false;
  77. }
  78. emit( directoryChanged(m_directory));
  79. }
  80. bool ImageLoader::validateExtension(QString filename) const
  81. {
  82. /**
  83. * Assumption OS does not allow an empty filename (excluding exten-
  84. * sion) to be used for files, thus is it pointless to provide only
  85. * the extension to the file.
  86. */
  87. QStringRef ref = filename.midRef(filename.length()-3,3);
  88. if(ref.compare(".sr",Qt::CaseInsensitive) == 0)
  89. {
  90. return true;
  91. }
  92. //if filename is smaller than 5 it can not be valid anymore
  93. if(filename.length() < 5) return false;
  94. ref.clear();
  95. ref = filename.midRef(filename.length()-4,4);
  96. if(ref.compare(".bmp",Qt::CaseInsensitive) == 0 ||
  97. ref.compare(".dib",Qt::CaseInsensitive) == 0 ||
  98. ref.compare(".jpg",Qt::CaseInsensitive) == 0 ||
  99. ref.compare(".jpe",Qt::CaseInsensitive) == 0 ||
  100. ref.compare(".png",Qt::CaseInsensitive) == 0 ||
  101. ref.compare(".pbm",Qt::CaseInsensitive) == 0 ||
  102. ref.compare(".pgm",Qt::CaseInsensitive) == 0 ||
  103. ref.compare(".ppm",Qt::CaseInsensitive) == 0 ||
  104. ref.compare(".ras",Qt::CaseInsensitive) == 0 ||
  105. ref.compare(".tif",Qt::CaseInsensitive) == 0 )
  106. {
  107. return true;
  108. }
  109. //if filename is smaller than 6 it can not be valid anymore
  110. if(filename.length() < 6)
  111. return false;
  112. ref.clear();
  113. ref = filename.midRef(filename.length()-5,5);
  114. if( ref.compare(".jpeg",Qt::CaseInsensitive) == 0 ||
  115. ref.compare(".tiff",Qt::CaseInsensitive) == 0 )
  116. {
  117. return true;
  118. }
  119. //none of the cases matched.
  120. return false;
  121. }
  122. bool ImageLoader::ObtainImage(QString directory, QString filename){
  123. QMutexLocker lock(&m_processMutex);
  124. //build the path
  125. QString path = directory;
  126. if(!path.endsWith('/')) path.append('/');
  127. path.append(filename);
  128. std::string c_path = path.toStdString();
  129. m_loadedImage = 0;
  130. //load the image
  131. const IplImage* image = cvLoadImage(c_path.c_str(), CV_LOAD_IMAGE_UNCHANGED );
  132. if(image != 0)
  133. {
  134. m_loadedImage = OpenCVImageFactory::instance()->getFromBuffer( image );
  135. //check if the newly loaded image is null or not.
  136. if(!m_loadedImage->isNull())
  137. {
  138. qDebug() << "New image is loaded from path: " << path;
  139. return true;
  140. }
  141. }
  142. qDebug() << "New image could not be loaded from path: " << path;
  143. return false;
  144. }
  145. //--- Inherited methods -------------------------------------------------------
  146. void ImageLoader::process()
  147. {
  148. QMutexLocker lock(&m_processMutex);
  149. //If a propper image has been loaded send it over the pipe.
  150. if(m_isReady)
  151. {
  152. m_outputPin->put( m_loadedImage.getPtr() );
  153. }
  154. }
  155. void ImageLoader::init()
  156. {
  157. ///
  158. m_isReady = ObtainImage(m_directory, m_filename);
  159. }
  160. void ImageLoader::deinit() throw() {}
  161. void ImageLoader::start() {}
  162. void ImageLoader::stop() {}
  163. bool ImageLoader::isReadyForProcessing() const
  164. {
  165. return( m_loadedImage.isNotNull() );
  166. }