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