SimulatorFrameLoader.cpp 8.98 KB
Newer Older
Samuel Debionne's avatar
Samuel Debionne committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
//###########################################################################
// This file is part of LImA, a Library for Image Acquisition
//
// Copyright (C) : 2009-2011
// European Synchrotron Radiation Facility
// BP 220, Grenoble 38043
// FRANCE
//
// This is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 3 of the License, or
// (at your option) any later version.
//
// This software is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, see <http://www.gnu.org/licenses/>.
//###########################################################################

#include <unistd.h>

#include <cassert>
#include <cmath>
#include <cstring>
#include <cctype>

#include <algorithm>
#include <string>
#include <fstream>
#include <iterator>
#include <utility>
#include <map>

#if defined(WIN32)
#include <windows.h>
#include <shlwapi.h>
#pragma comment(lib, "shlwapi.lib") // This tells msvc to link with shlwapi.lib
#else
#include <glob.h>
#endif // (WIN32)

//#include <processlib/Data.h>

#include "lima/Exceptions.h"
#include "lima/SizeUtils.h"

#include "simulator/SimulatorFrameLoader.h"

using namespace lima;
using namespace lima::Simulator;

// Note: Use Boost string algo rather than this
/// Trim both sides of a string using std::isspace
static std::string &trim(std::string &&s)
{
  s.erase(s.begin(), std::find_if_not(s.begin(), s.end(), [](char c) { return std::isspace(c); }));
  s.erase(std::find_if_not(s.rbegin(), s.rend(), [](char c) { return std::isspace(c); }).base(), s.end());
  return s;
}

// Parse EDf header and add the result to headers map
static std::streamsize parseEDFHeader(std::istream &input_file, std::map<std::string, std::string> &headers)
{
  std::stringstream ss;
  std::string buffer(1024, '\0');

  input_file.read(&buffer[0], buffer.size());
  if (buffer[0] != '{') throw LIMA_EXC(CameraPlugin, Error, "Invalid EDF file");

  ss << buffer;

  // More buffer in header?
  while ((buffer.rfind('}') == std::string::npos) && !input_file.eof()) {
    input_file.read(&buffer[0], buffer.size());
    ss << buffer;
  }

  // Parse header
  while (!ss.eof()) {
    std::string line;
    std::getline(ss, line);

    // Get rid of any comment
    std::string::size_type comment = line.rfind(';');
    if (comment != std::string::npos) line.resize(comment);

    // Tokenize
    std::string::size_type token_pos = line.find('=');
    if (token_pos != std::string::npos)
      headers.insert(std::make_pair(trim(line.substr(0, token_pos)), trim(line.substr(token_pos + 1))));
  }

  return input_file.tellg();
}

static ImageType getImageType(const std::string &type)
{
  ImageType res;

  if (type == "UnsignedByte")
    res = ImageType::Bpp8;
  else if (type == "SignedByte")
    res = ImageType::Bpp8S;
  else if (type == "UnsignedShort")
    res = ImageType::Bpp16;
  else if (type == "SignedShort")
    res = ImageType::Bpp16S;
  else if (type == "UnsignedInteger")
    res = ImageType::Bpp32;
  else if (type == "SignedInteger")
    res = ImageType::Bpp32S;
  // else if (type == "Unsigned64")
  //	res = ImageType::Bpp32;
  // else if (type == "Signed64")
  //	res = ImageType::Bpp32;
  else if (type == "FloatValue")
    res = ImageType::Bpp32F;
  // else if (type == "DoubleValue")
  //	res = ImageType::Bpp32;
  else
    throw LIMA_EXC(CameraPlugin, Error, "Unsupported pixel type in EDF file format");

  return res;
}

static void findFiles(const std::string &path_pattern, std::string &folder, std::vector<std::string> &files)
{
  if (path_pattern.empty()) return;

#if defined(WIN32)
  // Extra the folder part
  folder = path_pattern;
  folder.push_back('\0');
  ;
  PathRemoveFileSpec(&folder[0]);

  WIN32_FIND_DATA FindFileData;
  HANDLE hFind = FindFirstFile(path_pattern.c_str(), &FindFileData);
  if (hFind == INVALID_HANDLE_VALUE) {
    printf("FindFirstFile failed (%d)\n", GetLastError());
    return;
  } else {
    std::string path(MAX_PATH, '\0');
    PathCombine(&path[0], folder.c_str(), FindFileData.cFileName);
    files.push_back(path);

    while (FindNextFile(hFind, &FindFileData)) {
      PathCombine(&path[0], folder.c_str(), FindFileData.cFileName);
      files.push_back(path);
    }

    FindClose(hFind);
  }
#else
  glob_t glob_result;
  glob(path_pattern.c_str(), GLOB_TILDE, NULL, &glob_result);

  for (unsigned int i = 0; i < glob_result.gl_pathc; ++i)
    files.push_back(std::string(glob_result.gl_pathv[i]));

  globfree(&glob_result);
#endif // WIN32
}

void FrameLoader::setFilePattern(const std::string &file_pattern)
{
  if (m_file_pattern != file_pattern) m_file_pattern = file_pattern;

  // Clear the file list
  m_files.clear();

  // Find the files using the new pattern
  findFiles(file_pattern, m_folder, m_files);

  if (!m_files.empty()) {
    // Get the first file
    const std::string &file = m_files.front();

    // Get the file path and extension
#if defined(WIN32)
    // std::string path(MAX_PATH, '\0');
    // PathCombine(&path[0], m_folder.c_str(), file.c_str());
    const std::string extension(PathFindExtension(file.c_str()));
#else
    // std::string path = folder + "/" + file;
    const size_t pos            = file.rfind('.');
    const std::string extension = pos == 0 ? std::string() : file.substr(pos);
#endif // WIN32

    Size size;
    ImageType image_type;

    if (extension == ".edf") {
      // Open the file and parse the header
      std::ifstream input_file(file.c_str(), std::ios::binary);
      if (!input_file.is_open()) throw LIMA_EXC(CameraPlugin, Error, "Failed to open EDF file");

      std::map<std::string, std::string> headers;
      std::streamsize data_pos = parseEDFHeader(input_file, headers);

      Point p;
      p.x  = std::stoi(headers["Dim_1"]); // C++11
      p.y  = std::stoi(headers["Dim_2"]);
      size = p;

      // Interpret header
      auto val = headers.find("DataType");
      if (val != headers.end())
        image_type = getImageType(val->second);
      else
        throw LIMA_EXC(CameraPlugin, Error, "Missing DataType header in EDF file");
    }

    m_frame_dim = FrameDim(size, image_type);

    if (m_mis_cb_act)
      // Signal LiMA core that the frame properties may have changed
      maxImageSizeChanged(size, image_type);
  } else
    throw LIMA_EXC(CameraPlugin, Error, "No file found with the given pattern");
}

void FrameLoader::prepareAcq()
{
  if (!m_files.empty()) {
    // Position the iterator
    m_it_current_file = m_files.begin();

    // Open the file
    m_current_stream->open(m_it_current_file->c_str(), std::ios::binary);
  }
}

bool FrameLoader::getNextFrame(unsigned char *ptr)
{
  if (m_it_current_file != m_files.end()) {
    Size size;
    ImageType image_type;

    const std::string &file = *m_it_current_file;

    FrameDim frame_dim;

    // Get the file extension
#if defined(WIN32)
    // std::string path(MAX_PATH, '\0');
    // PathCombine(&path[0], m_folder.c_str(), file.c_str());
    const std::string extension(PathFindExtension(file.c_str()));
#else
    // std::string path = m_folder + "/" + file;
    const size_t pos            = file.rfind('.');
    const std::string extension = pos == 0 ? std::string() : file.substr(pos);
#endif // WIN32

    if (extension == ".edf") {
      if (m_current_stream->peek() == EOF) {
        m_current_stream->close();

        // Skip to next file
        m_it_current_file++;

        if (m_it_current_file != m_files.end())
          // Open next file
          m_current_stream->open(m_it_current_file->c_str(), std::ios::binary);
        else
          throw LIMA_EXC(CameraPlugin, Error, "End of file list");
      }

      // Check if the file is still open
      if (!m_current_stream->is_open()) throw LIMA_EXC(CameraPlugin, Error, "Failed to open EDF file");

      // Parse the header
      std::map<std::string, std::string> headers;
      std::streamsize data_pos = parseEDFHeader(*m_current_stream, headers);

      // Excerpt from header:
      // DataType = UnsignedInteger;
      // Size = 1048576;
      // Dim_1 = 512;
      // Dim_2 = 512;

      Point p;
      p.x  = std::stoi(headers["Dim_1"]); // C++11
      p.y  = std::stoi(headers["Dim_2"]);
      size = p;

      // Interpret header
      auto val = headers.find("DataType");
      if (val != headers.end()) image_type = getImageType(val->second);

      frame_dim          = FrameDim(size, image_type);
      const int mem_size = frame_dim.getMemSize();

      assert(frame_dim.getMemSize() == std::stol(headers["Size"]));

      // Read the frame data
      m_current_stream->read(reinterpret_cast<char *>(ptr), mem_size);
      if (m_current_stream->fail()) throw LIMA_EXC(CameraPlugin, Error, "Failed to read data section of EDF file");
    } else
      throw LIMA_EXC(CameraPlugin, Error, "Unsupported file format");

  } else
    return false;

  return true;
}