/*
 * GraphicsDriver.h
 * Copyright 1999-2000 Y.Takagi. All Rights Reserved.
 */

#ifndef	__GRAPHICSDRIVER_H
#define	__GRAPHICSDRIVER_H

#include "JobData.h"
#include "PrintProcess.h"
#include "SpoolMetaData.h"
#include "Transport.h"

class BView;
class BBitmap;
class BMessage;
class PrinterData;
class PrinterCap;

enum {
	kGDFRotateBandBitmap = 1
};

class GraphicsDriver {
public:
	GraphicsDriver(BMessage *, PrinterData *, const PrinterCap *);
	virtual ~GraphicsDriver();
	BMessage *takeJob(BFile *spool, uint32 flags = 0);

protected:
	virtual bool startDoc();
	virtual bool startPage(int page_number);
	virtual bool nextBand(BBitmap *bitmap, BPoint *offset);
	virtual bool endPage(int page_number);
	virtual bool endDoc(bool success);

	void writeSpoolData(const void *buffer, size_t size) throw(TransportException);
	void writeSpoolString(const char *buffer, ...) throw(TransportException);
	void writeSpoolChar(char c) throw(TransportException);

	static void convert_to_rgb24(void* src, void* dst, int width, color_space cs);
	static void convert_to_gray(void* src, void* dst, int width, color_space cs);

	const JobData *getJobData() const;
	const PrinterData *getPrinterData() const;
	const PrinterCap *getPrinterCap() const;
	const SpoolMetaData *getSpoolMetaData() const;

	int getPageWidth()  const;
	int getPageHeight() const;
	int getBandWidth()  const;
	int getBandHeight() const;
	int getPixelDepth() const;

	GraphicsDriver(const GraphicsDriver &);
	GraphicsDriver &operator = (const GraphicsDriver &);

private:
	void setupData(BFile *file, long page_count);
	void setupBitmap();
	void cleanupData();
	void cleanupBitmap();
	bool printPage(PageDataList *pages);
	bool printDocument(SpoolData *spool_data);
	bool printJob(BFile *file);
	static void rgb32_to_rgb24(void* src, void* dst, int width);
	static void cmap8_to_rgb24(void* src, void* dst, int width);
	static uint8 gray(uint8 r, uint8 g, uint8 b);
	static void rgb32_to_gray(void* src, void* dst, int width);
	static void cmap8_to_gray(void* src, void* dst, int width);

	uint32           fFlags;
	BMessage         *fMsg;
	BView            *fView;
	BBitmap          *fBitmap;
	Transport        *fTransport;
	JobData          *fOrgJobData;
	JobData          *fRealJobData;
	PrinterData      *fPrinterData;
	const PrinterCap *fPrinterCap;
	SpoolMetaData    *fSpoolMetaData;

	int fPageWidth;
	int fPageHeight;
	int fBandWidth;
	int fBandHeight;
	int fPixelDepth;
	int fBandCount;
	int fInternalCopies;
};

inline const JobData *GraphicsDriver::getJobData() const
{
	return fRealJobData;
}

inline const PrinterData *GraphicsDriver::getPrinterData() const
{
	return fPrinterData;
}

inline const PrinterCap *GraphicsDriver::getPrinterCap() const
{
	return fPrinterCap;
}

inline const SpoolMetaData *GraphicsDriver::getSpoolMetaData() const
{
	return fSpoolMetaData;
}

inline int GraphicsDriver::getPageWidth() const
{
	return fPageWidth;
}

inline int GraphicsDriver::getPageHeight() const
{
	return fPageHeight;
}

inline int GraphicsDriver::getBandWidth() const
{
	return fBandWidth;
}

inline int GraphicsDriver::getBandHeight() const
{
	return fBandHeight;
}

#endif	/* __GRAPHICSDRIVER_H */
