Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#ifndef TSCANNER_H
Toshihiro Shimizu 890ddd
#define TSCANNER_H
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#ifdef WIN32
Toshihiro Shimizu 890ddd
#define NOMINMAX
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "trasterimage.h"
Toshihiro Shimizu 890ddd
#include <set></set>
Campbell Barton d0e335
#include <qstring></qstring>
Toshihiro Shimizu 890ddd
#undef DVAPI
Toshihiro Shimizu 890ddd
#undef DVVAR
Toshihiro Shimizu 890ddd
#ifdef TNZBASE_EXPORTS
Toshihiro Shimizu 890ddd
#define DVAPI DV_EXPORT_API
Toshihiro Shimizu 890ddd
#define DVVAR DV_EXPORT_VAR
Toshihiro Shimizu 890ddd
#else
Toshihiro Shimizu 890ddd
#define DVAPI DV_IMPORT_API
Toshihiro Shimizu 890ddd
#define DVVAR DV_IMPORT_VAR
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
// forward declaration
Toshihiro Shimizu 890ddd
class TOStream;
Toshihiro Shimizu 890ddd
class TIStream;
Toshihiro Shimizu 890ddd
class TFilePath;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
struct DVAPI TScanParam {
Toshihiro Shimizu 890ddd
	TScanParam()
Toshihiro Shimizu 890ddd
		: m_supported(false), m_min(0), m_max(0), m_def(0), m_step(0), m_value(0)
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TScanParam(float _min, float _max, float _def, float _step)
Toshihiro Shimizu 890ddd
		: m_supported(true), m_min(_min), m_max(_max), m_def(_def), m_step(1), m_value(_def)
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	bool m_supported;
Toshihiro Shimizu 890ddd
	float m_min, m_max, m_def, m_step, m_value; // questi sono float per seguire lo standard TWAIN !
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	void update(const TScanParam &model);
Toshihiro Shimizu 890ddd
	// assegna min,max,def,step e supported da model a this
Toshihiro Shimizu 890ddd
	// mantiene value a patto che sia contenuto nel nuovo range, diversamente ne fa il crop
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
class DVAPI TScannerParameters
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
public:
Toshihiro Shimizu 890ddd
	enum ScanType {
Toshihiro Shimizu 890ddd
		None,
Toshihiro Shimizu 890ddd
		BW,
Toshihiro Shimizu 890ddd
		GR8,
Toshihiro Shimizu 890ddd
		RGB24
Toshihiro Shimizu 890ddd
	};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
private:
Toshihiro Shimizu 890ddd
	// Supported scan types: Black and white, graytones, color
Toshihiro Shimizu 890ddd
	bool m_bw, m_gray, m_rgb;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// Current scan type
Toshihiro Shimizu 890ddd
	ScanType m_scanType;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	string m_paperFormat; // e.g. "A4 paper"
Toshihiro Shimizu 890ddd
	TRectD m_scanArea;	// in mm /* TWAIN preferirebbe gli inch, ma uso i mm per seguire tnz4.x*/
Toshihiro Shimizu 890ddd
	TRectD m_cropBox;	 // in mm /* TWAIN preferirebbe gli inch, ma uso i mm per seguire tnz4.x*/
Toshihiro Shimizu 890ddd
	bool m_isPreview;
Toshihiro Shimizu 890ddd
	TDimensionD m_maxPaperSize; // in mm /* TWAIN preferirebbe gli inch, ma uso i mm per seguire tnz4.x*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	bool m_paperOverflow; // vale true se la scanArea e' stata tagliata rispetto alle dimensioni della carta
Toshihiro Shimizu 890ddd
						  // per rispettare la maxPaperSize
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	bool m_validatedByCurrentScanner;
Toshihiro Shimizu 890ddd
	// vale false se bisogna ancora chiamare adaptToCurrentScanner()
Toshihiro Shimizu 890ddd
	// l'idea e' di chiamare questo metodo il meno possibile
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Toshihiro Shimizu 890ddd
	TScanParam m_brightness;
Toshihiro Shimizu 890ddd
	TScanParam m_contrast;
Toshihiro Shimizu 890ddd
	TScanParam m_threshold;
Toshihiro Shimizu 890ddd
	TScanParam m_dpi;
Toshihiro Shimizu 890ddd
	TScanParam m_paperFeeder; // value==1.0 => use paper feeder
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
private:
Toshihiro Shimizu 890ddd
	// other useful info ?!
Toshihiro Shimizu 890ddd
	string m_twainVersion;
Toshihiro Shimizu 890ddd
	string m_manufacturer;
Toshihiro Shimizu 890ddd
	string m_prodFamily;
Toshihiro Shimizu 890ddd
	string m_productName;
Toshihiro Shimizu 890ddd
	string m_version;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	bool m_reverseOrder; // if true then scan levels starting from last frame
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	void cropScanArea(); // assicura che scanArea sia dentro maxPaperSize
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Toshihiro Shimizu 890ddd
	TScannerParameters();
Toshihiro Shimizu 890ddd
	~TScannerParameters();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	void setSupportedTypes(bool bw, bool gray, bool rgb);
Toshihiro Shimizu 890ddd
	bool isSupported(ScanType) const;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	void setMaxPaperSize(double maxWidth, double maxHeight); // note: possibly update m_scanArea
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	string getPaperFormat() const { return m_paperFormat; }
Toshihiro Shimizu 890ddd
	void setPaperFormat(string paperFormat);
Toshihiro Shimizu 890ddd
	// assert(TPaperFormatManager::instance()->isValidFormat(paperFormat));
Toshihiro Shimizu 890ddd
	// updates scanArea (cropping with maxPaperSize)
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	void updatePaperFormat();
Toshihiro Shimizu 890ddd
	// if paperFormat=="" set default paper format; recompute (to be sure!) scanArea
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	bool getPaperOverflow() const { return m_paperOverflow; } // true iff paperSize > maxPaperSize
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TRectD getScanArea() const { return m_scanArea; }
Toshihiro Shimizu 890ddd
	void setCropBox(const TRectD &cropBox) { m_cropBox = cropBox * m_scanArea; }
Toshihiro Shimizu 890ddd
	TRectD getCropBox() const { return m_cropBox; }
Toshihiro Shimizu 890ddd
	void setIsPreview(bool isPreview) { m_isPreview = isPreview; };
Toshihiro Shimizu 890ddd
	bool isPreview() const { return m_isPreview; }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	bool isReverseOrder() const { return m_reverseOrder; }
Toshihiro Shimizu 890ddd
	void setReverseOrder(bool reverseOrder) { m_reverseOrder = reverseOrder; }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	bool isPaperFeederEnabled() const { return m_paperFeeder.m_value == 1.0; }
Toshihiro Shimizu 890ddd
	void enablePaperFeeder(bool on) { m_paperFeeder.m_value = on ? 1.0f : 0.0f; }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	void setScanType(ScanType scanType);
Toshihiro Shimizu 890ddd
	ScanType getScanType() const { return m_scanType; }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// se e' stato definito uno scanner aggiorna lo stato (enabled/disabled, range, etc.)
Toshihiro Shimizu 890ddd
	// dei parametri rispetto a quest'ultimo
Toshihiro Shimizu 890ddd
	void adaptToCurrentScanner();
Toshihiro Shimizu 890ddd
	void adaptToCurrentScannerIfNeeded()
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		if (!m_validatedByCurrentScanner)
Toshihiro Shimizu 890ddd
			adaptToCurrentScanner();
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	void assign(const TScannerParameters *params);
Toshihiro Shimizu 890ddd
	void saveData(TOStream &os) const;
Toshihiro Shimizu 890ddd
	void loadData(TIStream &is);
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//----------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
class TScannerListener
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
public:
Toshihiro Shimizu 890ddd
	virtual void onImage(const TRasterImageP &) = 0;
Toshihiro Shimizu 890ddd
	virtual void onError() = 0;
Toshihiro Shimizu 890ddd
	virtual void onNextPaper() = 0;
Toshihiro Shimizu 890ddd
	virtual void onAutomaticallyNextPaper() = 0;
Toshihiro Shimizu 890ddd
	virtual bool isCanceled() = 0;
Toshihiro Shimizu 890ddd
	virtual ~TScannerListener() {}
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//----------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
class DVAPI TScanner
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	std::set<tscannerlistener *=""> m_listeners;</tscannerlistener>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
protected:
Toshihiro Shimizu 890ddd
	TScanParam m_brightness, m_contrast, m_threshold, m_dpi;
Toshihiro Shimizu 890ddd
	int m_paperLeft;
Toshihiro Shimizu 890ddd
	QString m_scannerName;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Toshihiro Shimizu 890ddd
	TScanner();
Toshihiro Shimizu 890ddd
	virtual ~TScanner();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	static bool m_isTwain; //brutto, brutto :(
Toshihiro Shimizu 890ddd
	static TScanner *instance();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	virtual void selectDevice() = 0;
Toshihiro Shimizu 890ddd
	virtual bool isDeviceAvailable() = 0;
Toshihiro Shimizu 890ddd
	virtual bool isDeviceSelected() { return false; }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	virtual void updateParameters(TScannerParameters ¶meters) = 0;
Toshihiro Shimizu 890ddd
	// aggiorna i parametri 'parameters' in funzione del tipo di scanner selezionato
Toshihiro Shimizu 890ddd
	// se possibile non modifica i valori correnti, ma cambia solo quello che non e' piu' adatto
Toshihiro Shimizu 890ddd
	// abilita/disabilita i parametri "opzionali".
Toshihiro Shimizu 890ddd
	// n.b. i parametri opzionali che vengono disabilitati mantengono il loro valore
Toshihiro Shimizu 890ddd
	// (cosi' se fai save default settings dopo aver cambiato scanner non ti perdi i vecchi settaggi)
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	virtual void acquire(const TScannerParameters ¶m, int paperCount) = 0;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	void addListener(TScannerListener *);
Toshihiro Shimizu 890ddd
	void removeListener(TScannerListener *);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	void notifyImageDone(const TRasterImageP &image);
Toshihiro Shimizu 890ddd
	void notifyNextPaper();
Toshihiro Shimizu 890ddd
	void notifyAutomaticallyNextPaper();
Toshihiro Shimizu 890ddd
	void notifyError();
Toshihiro Shimizu 890ddd
	bool isScanningCanceled();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	int getPaperLeftCount() const { return m_paperLeft; }
Toshihiro Shimizu 890ddd
	void setPaperLeftCount(int count) { m_paperLeft = count; }
Toshihiro Shimizu 890ddd
	void decrementPaperLeftCount() { --m_paperLeft; }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	QString getName() const { return m_scannerName; }
Toshihiro Shimizu 890ddd
	void setName(const QString &name) { m_scannerName = name; }
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
class DVAPI TPaperFormatManager
Toshihiro Shimizu 890ddd
{ // singleton
Toshihiro Shimizu 890ddd
public:
Toshihiro Shimizu 890ddd
	class Format
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
	public:
Toshihiro Shimizu 890ddd
		TDimensionD m_size;
Toshihiro Shimizu 890ddd
		Format() : m_size(0, 0) {}
Toshihiro Shimizu 890ddd
		Format(const TDimensionD &size) : m_size(size) {}
Toshihiro Shimizu 890ddd
	};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
private:
Toshihiro Shimizu 890ddd
	typedef std::map<string, format=""> FormatTable;</string,>
Toshihiro Shimizu 890ddd
	FormatTable m_formats;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TPaperFormatManager();
Toshihiro Shimizu 890ddd
	void readPaperFormat(const TFilePath &fp);
Toshihiro Shimizu 890ddd
	void readPaperFormats();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Toshihiro Shimizu 890ddd
	static TPaperFormatManager *instance();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// resitutisce la lista dei formati
Toshihiro Shimizu 890ddd
	void getFormats(std::vector<string> &names) const;</string>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	bool isValidFormat(string name) const;
Toshihiro Shimizu 890ddd
	string getDefaultFormat() const;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// nome formato --> dimensione
Toshihiro Shimizu 890ddd
	TDimensionD getSize(string name) const;
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#endif