Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "tscanner.h"
Toshihiro Shimizu 890ddd
#include "tscannertwain.h"
Toshihiro Shimizu 890ddd
#include "texception.h"
Toshihiro Shimizu 890ddd
#include "tscannerepson.h"
Toshihiro Shimizu 890ddd
#include "tstream.h"
Toshihiro Shimizu 890ddd
#include "tconvert.h"
Toshihiro Shimizu 890ddd
#include "tfilepath.h"
Toshihiro Shimizu 890ddd
#include "tfilepath_io.h"
Toshihiro Shimizu 890ddd
#include "tenv.h"
Toshihiro Shimizu 890ddd
#include "tsystem.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
extern "C" {
Toshihiro Shimizu 890ddd
#include "../common/twain/ttwain_util.h"
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//===================================================================
Toshihiro Shimizu 890ddd
//
Toshihiro Shimizu 890ddd
// TScanParam
Toshihiro Shimizu 890ddd
//
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TScanParam::update(const TScanParam &model) {
Shinya Kitaoka 120a6e
  m_supported = model.m_supported;
Shinya Kitaoka 120a6e
  m_min       = model.m_min;
Shinya Kitaoka 120a6e
  m_max       = model.m_max;
Shinya Kitaoka 120a6e
  m_def       = model.m_def;
Shinya Kitaoka 120a6e
  m_step      = model.m_step;
Shinya Kitaoka 120a6e
  m_value     = tcrop(m_value, m_min, m_max);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//===================================================================
Toshihiro Shimizu 890ddd
//
Toshihiro Shimizu 890ddd
// TScannerParameters
Toshihiro Shimizu 890ddd
//
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 3bfa54
const std::string BlackAndWhite = "Black & White";
Shinya Kitaoka 120a6e
const std::string Graytones     = "Graytones";
Shinya Kitaoka 120a6e
const std::string Rgbcolors     = "RGB Color";
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TScannerParameters::TScannerParameters()
Shinya Kitaoka 120a6e
    : m_bw(false)
Shinya Kitaoka 120a6e
    , m_gray(false)
Shinya Kitaoka 120a6e
    , m_rgb(false)
Shinya Kitaoka 120a6e
    , m_scanType(None)
Shinya Kitaoka 120a6e
    , m_scanArea(TRectD())
Shinya Kitaoka 120a6e
    , m_cropBox(TRectD())
Shinya Kitaoka 120a6e
    , m_isPreview(false)
Shinya Kitaoka 120a6e
    , m_maxPaperSize(TDimensionD(0, 0))
Shinya Kitaoka 120a6e
    , m_paperOverflow(false)
Shinya Kitaoka 120a6e
    , m_brightness()
Shinya Kitaoka 120a6e
    , m_contrast()
Shinya Kitaoka 120a6e
    , m_threshold()
Shinya Kitaoka 120a6e
    , m_dpi()
Shinya Kitaoka 120a6e
    , m_paperFeeder()
Shinya Kitaoka 120a6e
    , m_twainVersion()
Shinya Kitaoka 120a6e
    , m_manufacturer()
Shinya Kitaoka 120a6e
    , m_prodFamily()
Shinya Kitaoka 120a6e
    , m_productName()
Shinya Kitaoka 120a6e
    , m_version()
Shinya Kitaoka 120a6e
    , m_reverseOrder(false)
Shinya Kitaoka 120a6e
    , m_validatedByCurrentScanner(false) {
Shinya Kitaoka 120a6e
  m_threshold.m_value  = 127;
Shinya Kitaoka 120a6e
  m_brightness.m_value = 127;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TScannerParameters::~TScannerParameters() {}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TScannerParameters::assign(const TScannerParameters *src) { *this = *src; }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TScannerParameters::setSupportedTypes(bool bw, bool gray, bool rgb) {
Shinya Kitaoka 120a6e
  m_bw   = bw;
Shinya Kitaoka 120a6e
  m_gray = gray;
Shinya Kitaoka 120a6e
  m_rgb  = rgb;
Shinya Kitaoka 120a6e
  if (!m_bw && !m_gray && !m_rgb) {
Shinya Kitaoka 120a6e
    m_scanType = None;
Shinya Kitaoka 120a6e
  } else {
Shinya Kitaoka 120a6e
    switch (m_scanType) {
Shinya Kitaoka 120a6e
    case BW:
Shinya Kitaoka 120a6e
      if (!m_bw) m_scanType = m_gray ? GR8 : RGB24;
Shinya Kitaoka 120a6e
      break;
Shinya Kitaoka 120a6e
    case GR8:
Shinya Kitaoka 120a6e
      if (!m_gray) m_scanType = m_rgb ? RGB24 : BW;
Shinya Kitaoka 120a6e
      break;
Shinya Kitaoka 120a6e
    case RGB24:
Shinya Kitaoka 120a6e
      if (!m_rgb) m_scanType = m_gray ? GR8 : BW;
Shinya Kitaoka 120a6e
      break;
Shinya Kitaoka 120a6e
    case None:
Shinya Kitaoka 120a6e
      if (gray)
Shinya Kitaoka 120a6e
        m_scanType = GR8;
Shinya Kitaoka 120a6e
      else {
Shinya Kitaoka 120a6e
        if (rgb)
Shinya Kitaoka 120a6e
          m_scanType = RGB24;
Shinya Kitaoka 120a6e
        else if (bw)
Shinya Kitaoka 120a6e
          m_scanType = BW;
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
      break;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
bool TScannerParameters::isSupported(ScanType scanType) const {
Shinya Kitaoka 120a6e
  switch (scanType) {
Shinya Kitaoka 120a6e
  case BW:
Shinya Kitaoka 120a6e
    return m_bw;
Shinya Kitaoka 120a6e
  case GR8:
Shinya Kitaoka 120a6e
    return m_gray;
Shinya Kitaoka 120a6e
  case RGB24:
Shinya Kitaoka 120a6e
    return m_rgb;
Shinya Kitaoka 120a6e
  default:
Shinya Kitaoka 120a6e
    assert(0);
Shinya Kitaoka 120a6e
    return false;
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TScannerParameters::cropScanArea() {
Shinya Kitaoka 120a6e
  m_paperOverflow = false;
Shinya Kitaoka 120a6e
  if (m_maxPaperSize.lx == 0 || m_maxPaperSize.ly == 0) {
Shinya Kitaoka 120a6e
    // probabilmente non e' ancora stato selezionato uno scanner e quindi non e'
Shinya Kitaoka 120a6e
    // definita una maxPaperSize
Shinya Kitaoka 120a6e
    return;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  assert(m_maxPaperSize.lx > 0 && m_maxPaperSize.ly > 0);
Shinya Kitaoka 120a6e
  if (m_scanArea.x1 > m_maxPaperSize.lx) {
Shinya Kitaoka 120a6e
    m_paperOverflow = true;
Shinya Kitaoka 120a6e
    m_scanArea.x1   = m_maxPaperSize.lx;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  if (m_scanArea.y1 > m_maxPaperSize.ly) {
Shinya Kitaoka 120a6e
    m_paperOverflow = true;
Shinya Kitaoka 120a6e
    m_scanArea.y1   = m_maxPaperSize.ly;
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TScannerParameters::setMaxPaperSize(double maxWidth, double maxHeight) {
Shinya Kitaoka 120a6e
  // assert(maxWidth>0 && maxHeight>0);
Shinya Kitaoka 120a6e
  m_maxPaperSize = TDimensionD(maxWidth, maxHeight);
Shinya Kitaoka 120a6e
  cropScanArea();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TScannerParameters::setPaperFormat(std::string paperFormat) {
Shinya Kitaoka 120a6e
  TPaperFormatManager *formatManager = TPaperFormatManager::instance();
Shinya Kitaoka 120a6e
  assert(formatManager->isValidFormat(paperFormat));
Shinya Kitaoka 120a6e
  if (!formatManager->isValidFormat(paperFormat))
Shinya Kitaoka 120a6e
    paperFormat = formatManager->getDefaultFormat();
Shinya Kitaoka 120a6e
  m_paperFormat = paperFormat;
Shinya Kitaoka 120a6e
  TDimensionD d = TPaperFormatManager::instance()->getSize(paperFormat);
Shinya Kitaoka 120a6e
  m_scanArea    = TRectD(TPointD(0, 0), d);
Shinya Kitaoka 120a6e
  cropScanArea();
Shinya Kitaoka 120a6e
  if (m_cropBox == TRectD()) m_cropBox = m_scanArea;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TScannerParameters::updatePaperFormat() {
Shinya Kitaoka 120a6e
  if (m_paperFormat == "")
Shinya Kitaoka 120a6e
    m_paperFormat = TPaperFormatManager::instance()->getDefaultFormat();
Shinya Kitaoka 120a6e
  setPaperFormat(m_paperFormat);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TScannerParameters::setScanType(ScanType scanType) {
Shinya Kitaoka 120a6e
  assert(scanType == None || scanType == BW || scanType == GR8 ||
Shinya Kitaoka 120a6e
         scanType == RGB24);
Shinya Kitaoka 120a6e
  m_scanType = scanType;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TScannerParameters::adaptToCurrentScanner() {
Shinya Kitaoka 120a6e
  try {
Shinya Kitaoka 120a6e
    if (TScanner::instance()->isDeviceSelected()) {
Shinya Kitaoka 120a6e
      TScanner::instance()->updateParameters(*this);
Shinya Kitaoka 120a6e
      m_validatedByCurrentScanner = true;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  } catch (TException &) {
Shinya Kitaoka 120a6e
    // TMessage::error("%1", e.getMessage());
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TScannerParameters::saveData(TOStream &os) const {
Shinya Kitaoka 120a6e
  std::map<std::string, std::string=""> attr;</std::string,>
Shinya Kitaoka 120a6e
  attr["fmt"] = m_paperFormat;
Shinya Kitaoka 120a6e
  os.openCloseChild("paper", attr);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (m_paperFeeder.m_value == 1.0) {
Shinya Kitaoka 120a6e
    attr.clear();
Shinya Kitaoka 120a6e
    os.openCloseChild("autoFeeder", attr);
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (m_reverseOrder) {
Shinya Kitaoka 120a6e
    attr.clear();
Shinya Kitaoka 120a6e
    os.openCloseChild("reverseOrder", attr);
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (m_scanType != None) {
Shinya Kitaoka 120a6e
    std::string scanTypeString = Rgbcolors;
Shinya Kitaoka 120a6e
    switch (m_scanType) {
Shinya Kitaoka 120a6e
    case BW:
Shinya Kitaoka 120a6e
      scanTypeString = BlackAndWhite;
Shinya Kitaoka 120a6e
      break;
Shinya Kitaoka 120a6e
    case GR8:
Shinya Kitaoka 120a6e
      scanTypeString = Graytones;
Shinya Kitaoka 120a6e
      break;
Shinya Kitaoka 120a6e
    case RGB24:
Shinya Kitaoka 120a6e
      scanTypeString = Rgbcolors;
Shinya Kitaoka 120a6e
      break;
Rozhuk Ivan 823a31
    default:
Rozhuk Ivan 823a31
      break;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
    attr.clear();
Shinya Kitaoka 120a6e
    attr["value"] = scanTypeString;
Shinya Kitaoka 120a6e
    os.openCloseChild("mode", attr);
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (m_dpi.m_supported) {
Shinya Kitaoka 120a6e
    attr.clear();
Shinya Kitaoka 120a6e
    attr["value"] = std::to_string(m_dpi.m_value);
Shinya Kitaoka 120a6e
    os.openCloseChild("dpi", attr);
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (m_brightness.m_supported) {
Shinya Kitaoka 120a6e
    attr.clear();
Shinya Kitaoka 120a6e
    attr["value"] = std::to_string(m_brightness.m_value);
Shinya Kitaoka 120a6e
    os.openCloseChild("brightness", attr);
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (m_contrast.m_supported) {
Shinya Kitaoka 120a6e
    attr.clear();
Shinya Kitaoka 120a6e
    attr["value"] = std::to_string(m_contrast.m_value);
Shinya Kitaoka 120a6e
    os.openCloseChild("contrast", attr);
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (m_threshold.m_supported) {
Shinya Kitaoka 120a6e
    attr.clear();
Shinya Kitaoka 120a6e
    attr["value"] = std::to_string(m_threshold.m_value);
Shinya Kitaoka 120a6e
    os.openCloseChild("threshold", attr);
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TScannerParameters::loadData(TIStream &is) {
Shinya Kitaoka 120a6e
  std::string tagName;
Shinya Kitaoka 120a6e
  while (is.matchTag(tagName)) {
Shinya Kitaoka 120a6e
    if (tagName == "dpi") {
Shinya Kitaoka 120a6e
      std::string s                  = is.getTagAttribute("value");
Shinya Kitaoka 120a6e
      if (isDouble(s)) m_dpi.m_value = std::stof(s);
Shinya Kitaoka 120a6e
    } else if (tagName == "brightness") {
Shinya Kitaoka 120a6e
      std::string s                         = is.getTagAttribute("value");
Shinya Kitaoka 120a6e
      if (isDouble(s)) m_brightness.m_value = std::stof(s);
Shinya Kitaoka 120a6e
    } else if (tagName == "threshold") {
Shinya Kitaoka 120a6e
      std::string s                        = is.getTagAttribute("value");
Shinya Kitaoka 120a6e
      if (isDouble(s)) m_threshold.m_value = std::stof(s);
Shinya Kitaoka 120a6e
    } else if (tagName == "contrast") {
Shinya Kitaoka 120a6e
      std::string s                       = is.getTagAttribute("value");
Shinya Kitaoka 120a6e
      if (isDouble(s)) m_contrast.m_value = std::stof(s);
Shinya Kitaoka 120a6e
    } else if (tagName == "autoFeeder") {
Shinya Kitaoka 120a6e
      m_paperFeeder.m_value = 1.0;
Shinya Kitaoka 120a6e
    } else if (tagName == "reverseOrder") {
Shinya Kitaoka 120a6e
      m_reverseOrder = true;
Shinya Kitaoka 120a6e
    } else if (tagName == "mode") {
Shinya Kitaoka 120a6e
      std::string scanTypeString = is.getTagAttribute("value");
Shinya Kitaoka 120a6e
      m_scanType                 = None;
Shinya Kitaoka 120a6e
      if (scanTypeString == BlackAndWhite)
Shinya Kitaoka 120a6e
        m_scanType = BW;
Shinya Kitaoka 120a6e
      else if (scanTypeString == Graytones)
Shinya Kitaoka 120a6e
        m_scanType = GR8;
Shinya Kitaoka 120a6e
      else if (scanTypeString == Rgbcolors)
Shinya Kitaoka 120a6e
        m_scanType = RGB24;
Shinya Kitaoka 120a6e
    } else if (tagName == "paper") {
Shinya Kitaoka 120a6e
      std::string paperFormat = is.getTagAttribute("fmt");
Shinya Kitaoka 120a6e
      if (paperFormat != "") setPaperFormat(paperFormat);
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  m_validatedByCurrentScanner = false;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//===================================================================
Toshihiro Shimizu 890ddd
//
Toshihiro Shimizu 890ddd
// TScanner
Toshihiro Shimizu 890ddd
//
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
namespace {
Toshihiro Shimizu 890ddd
TScanner *instanceTwain = 0;
Toshihiro Shimizu 890ddd
TScanner *instanceEpson = 0;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
bool TScanner::m_isTwain = true;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
namespace {
Shinya Kitaoka 120a6e
class Cleaner {
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  bool m_activated;
Shinya Kitaoka 120a6e
  Cleaner() : m_activated(false) {}
Shinya Kitaoka 120a6e
  ~Cleaner() {
Shinya Kitaoka 120a6e
    TScanner *scannerToDestroy        = 0;
Shinya Kitaoka 120a6e
    if (m_activated) scannerToDestroy = TScanner::instance();
Shinya Kitaoka 120a6e
    delete scannerToDestroy;
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Cleaner MyCleaner;
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
// #define DUMMYSCAN
Toshihiro Shimizu 890ddd
#ifdef DUMMYSCAN
Shinya Kitaoka d1f6c4
class TScannerDummy final : public TScanner {
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  TScannerDummy() {}
Shinya Kitaoka 120a6e
  ~TScannerDummy() {}
Shinya Kitaoka 120a6e
  void selectDevice() {}
Shinya Kitaoka 120a6e
  bool isDeviceAvailable() { return true; }
Shinya Kitaoka 120a6e
  void updateParameters(TScannerParameters ¶meters) {
Shinya Kitaoka 120a6e
    parameters.setSupportedTypes(false, true, true);
Shinya Kitaoka 120a6e
    parameters.setMaxPaperSize(1000., 1000.);
Shinya Kitaoka 120a6e
    parameters.enablePaperFeeder(true);
Shinya Kitaoka 120a6e
    parameters.m_brightness.update(TScanParam(0, 255, 128, 1));
Shinya Kitaoka 120a6e
    parameters.m_contrast.update(TScanParam(0, 255, 128, 1));
Shinya Kitaoka 120a6e
    parameters.m_threshold.update(TScanParam(0, 255, 128, 1));
Shinya Kitaoka 120a6e
    parameters.m_dpi.update(TScanParam(60, 1200, 100, 1));
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    setName("DummyScanner");
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  bool isTwain() const { return true; }
Shinya Kitaoka 120a6e
  void acquire(const TScannerParameters ¶m, int paperCount) { return; }
Shinya Kitaoka 120a6e
  bool isAreaSupported() { return true; }
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TScanner *TScanner::instance() {
Shinya Kitaoka 120a6e
  MyCleaner.m_activated = true;
Toshihiro Shimizu 890ddd
#ifdef DUMMYSCAN
Shinya Kitaoka 120a6e
  static TScannerDummy dummy = TScannerDummy();
Shinya Kitaoka 120a6e
  return &dummy;
Toshihiro Shimizu 890ddd
#else
Shinya Kitaoka 120a6e
  if (m_isTwain) {
Shinya Kitaoka 120a6e
    if (instanceEpson) {
Shinya Kitaoka 120a6e
      TScannerEpson *se = (TScannerEpson *)instanceEpson;
Shinya Kitaoka 120a6e
      se->closeIO();
Shinya Kitaoka 120a6e
      // delete m_instanceEpson; //e' singletone, perche' buttarlo? (vinz)
Shinya Kitaoka 120a6e
      // m_instanceEpson=0;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
    if (!instanceTwain) instanceTwain = new TScannerTwain();
Shinya Kitaoka 120a6e
  } else if (!m_isTwain) {
Shinya Kitaoka 120a6e
    if (instanceTwain) {
Shinya Kitaoka 120a6e
      // delete m_instanceTwain;  //e' singletone, perche' buttarlo? (vinz)
Shinya Kitaoka 120a6e
      // m_instanceTwain=0;
Shinya Kitaoka 120a6e
      TTWAIN_CloseAll(0);
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
    if (!instanceEpson) instanceEpson = new TScannerEpson();
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka fd19c4
  return (m_isTwain ? instanceTwain : instanceEpson);
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Shinya Kitaoka 120a6e
TScanner::TScanner() : m_paperLeft(0) {}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TScanner::~TScanner() {
Shinya Kitaoka 120a6e
  if (instanceEpson) {
Shinya Kitaoka 120a6e
    TScannerEpson *se = (TScannerEpson *)instanceEpson;
Shinya Kitaoka 120a6e
    se->closeIO();
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TScanner::addListener(TScannerListener *lst) { m_listeners.insert(lst); }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TScanner::removeListener(TScannerListener *lst) { m_listeners.erase(lst); }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TScanner::notifyImageDone(const TRasterImageP &img) {
Shinya Kitaoka 120a6e
  std::set<tscannerlistener *="">::iterator it = m_listeners.begin();</tscannerlistener>
Shinya Kitaoka 120a6e
  for (; it != m_listeners.end(); ++it) {
Shinya Kitaoka 120a6e
    (*it)->onImage(img);
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TScanner::notifyNextPaper() {
Shinya Kitaoka 120a6e
  std::set<tscannerlistener *="">::iterator it = m_listeners.begin();</tscannerlistener>
Shinya Kitaoka 120a6e
  for (; it != m_listeners.end(); ++it) {
Shinya Kitaoka 120a6e
    (*it)->onNextPaper();
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TScanner::notifyAutomaticallyNextPaper() {
Shinya Kitaoka 120a6e
  std::set<tscannerlistener *="">::iterator it = m_listeners.begin();</tscannerlistener>
Shinya Kitaoka 120a6e
  for (; it != m_listeners.end(); ++it) {
Shinya Kitaoka 120a6e
    (*it)->onAutomaticallyNextPaper();
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TScanner::notifyError() {
Shinya Kitaoka 120a6e
  std::set<tscannerlistener *="">::iterator it = m_listeners.begin();</tscannerlistener>
Shinya Kitaoka 120a6e
  for (; it != m_listeners.end(); ++it) {
Shinya Kitaoka 120a6e
    (*it)->onError();
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
/*! If one listener is set to cancel return true. */
Shinya Kitaoka 120a6e
bool TScanner::isScanningCanceled() {
Shinya Kitaoka 120a6e
  std::set<tscannerlistener *="">::iterator it = m_listeners.begin();</tscannerlistener>
Shinya Kitaoka 120a6e
  for (; it != m_listeners.end(); ++it) {
Shinya Kitaoka 120a6e
    if ((*it)->isCanceled()) return true;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  return false;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//===================================================================
Toshihiro Shimizu 890ddd
//
Toshihiro Shimizu 890ddd
// TPaperFormatManager
Toshihiro Shimizu 890ddd
//
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
namespace {
Shinya Kitaoka 120a6e
const std::pair<std::string, tdimensiond=""> defaultPaperFormat(</std::string,>
Shinya Kitaoka 120a6e
    "A4 paper", TDimensionD(210.00, 297.00));
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TPaperFormatManager::TPaperFormatManager() {
Shinya Kitaoka 120a6e
  readPaperFormats();
Shinya Kitaoka 120a6e
  // se non c'e' aggiungo il formato di default. In questo modo e' sempre
Shinya Kitaoka 120a6e
  // definito
Shinya Kitaoka 120a6e
  if (!isValidFormat(defaultPaperFormat.first))
Shinya Kitaoka 120a6e
    m_formats[defaultPaperFormat.first] = Format(defaultPaperFormat.second);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TPaperFormatManager *TPaperFormatManager::instance() {
Shinya Kitaoka 120a6e
  static TPaperFormatManager _instance;
Shinya Kitaoka 120a6e
  return &_instance;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TPaperFormatManager::getFormats(std::vector<std::string> &names) const {</std::string>
Shinya Kitaoka 120a6e
  for (FormatTable::const_iterator it = m_formats.begin();
Shinya Kitaoka 120a6e
       it != m_formats.end(); ++it)
Shinya Kitaoka 120a6e
    names.push_back(it->first);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TDimensionD TPaperFormatManager::getSize(std::string name) const {
Shinya Kitaoka 120a6e
  FormatTable::const_iterator it = m_formats.find(name);
Shinya Kitaoka 120a6e
  if (it == m_formats.end())
Shinya Kitaoka 120a6e
    return TDimensionD(0., 0.);
Shinya Kitaoka 120a6e
  else
Shinya Kitaoka 120a6e
    return it->second.m_size;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
bool TPaperFormatManager::isValidFormat(std::string name) const {
Shinya Kitaoka 120a6e
  FormatTable::const_iterator it = m_formats.find(name);
Shinya Kitaoka 120a6e
  return it != m_formats.end();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
std::string TPaperFormatManager::getDefaultFormat() const {
Shinya Kitaoka 120a6e
  return defaultPaperFormat.first;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TPaperFormatManager::readPaperFormat(const TFilePath &path) {
Shinya Kitaoka 120a6e
  if (path.getType() != "pap") return;
Shinya Kitaoka 120a6e
  Tifstream is(path);
Shinya Kitaoka 120a6e
  std::string name;
Shinya Kitaoka 120a6e
  TDimensionD size(0, 0);
Shinya Kitaoka 120a6e
  while (is) {
Shinya Kitaoka 120a6e
    char buffer[1024];
Shinya Kitaoka 120a6e
    is.getline(buffer, sizeof buffer);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    // i e' il carattere successivo alla fine della linea
Shinya Kitaoka 120a6e
    unsigned int i = 0;
Shinya Kitaoka 120a6e
    for (i = 0; i < sizeof buffer && buffer[i]; i++) {
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
    if (i > 0 && buffer[i - 1] == '\n') i--;
Shinya Kitaoka 120a6e
    while (i > 0 && buffer[i - 1] == ' ') i--;
Shinya Kitaoka 120a6e
    unsigned int j = 0;
Shinya Kitaoka 120a6e
    unsigned int k = 0;
Shinya Kitaoka 120a6e
    // j e' il carattere successivo alla fine del primo token
Shinya Kitaoka 120a6e
    for (j = 0; j < i && buffer[j] != ' '; j++) {
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    // k e' l'inizio del secondo token (se c'e', altrimenti == i)
Shinya Kitaoka 120a6e
    for (k = j; k < i && buffer[k] == ' '; k++) {
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    std::string value;
Shinya Kitaoka 120a6e
    if (k < i) value = std::string(buffer + k, i - k);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    if (buffer[0] == '#') {
Shinya Kitaoka 120a6e
      if (k < i && name == "") name = value;
Shinya Kitaoka 120a6e
    } else if (std::string(buffer).find("WIDTH") == 0) {
Shinya Kitaoka 120a6e
      if (isDouble(value)) size.lx = std::stod(value);
Shinya Kitaoka 120a6e
    } else if (std::string(buffer).find("LENGTH") == 0) {
Shinya Kitaoka 120a6e
      if (isDouble(value)) size.ly = std::stod(value);
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  if (name == "" || size.lx == 0 || size.ly == 0) {
Shinya Kitaoka 120a6e
    // TMessage::error("Error reading paper format file : %1",path);
Shinya Kitaoka 120a6e
  } else
Shinya Kitaoka 120a6e
    m_formats[name] = Format(size);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TPaperFormatManager::readPaperFormats() {
Shinya Kitaoka 120a6e
  TFilePathSet fps;
Shinya Kitaoka 120a6e
  TFilePath papDir = TEnv::getConfigDir() + "pap";
Shinya Kitaoka 120a6e
  if (!TFileStatus(papDir).isDirectory()) {
Shinya Kitaoka 120a6e
    // TMessage::error("E_CanNotReadDirectory_%1", papDir);
Shinya Kitaoka 120a6e
    return;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  try {
Shinya Kitaoka 120a6e
    fps = TSystem::readDirectory(papDir);
Shinya Kitaoka 120a6e
  } catch (TException &) {
Shinya Kitaoka 120a6e
    // TMessage::error("E_CanNotReadDirectory_%1", papDir);
Shinya Kitaoka 120a6e
    return;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  TFilePathSet::const_iterator it = fps.begin();
Shinya Kitaoka 120a6e
  for (; it != fps.end(); ++it) readPaperFormat(*it);
Toshihiro Shimizu 890ddd
}