/////////////////////////////////////////////////////////////
// CINEMA 4D SDK                                           //
/////////////////////////////////////////////////////////////
// (c) MAXON Computer GmbH, all rights reserved            //
/////////////////////////////////////////////////////////////

#ifndef __X4D_FILTER_H
#define __X4D_FILTER_H

#include "x4d_macros.h"																			// defines for platform independency
#include "x4d_colors.h"																			// color spaces and pixel formats
#include "operatingsystem.h"

//----------------------------------------------------------------------------------------
// Coordinates ...
//----------------------------------------------------------------------------------------
struct RECT32																								// rectangle for 32 bit coordinates
{
	LONG	x1;
	LONG	y1;
	LONG	x2;
	LONG	y2;
};


//----------------------------------------------------------------------------------------
// Output context
//----------------------------------------------------------------------------------------
typedef	void	*DRAW_CONTEXT;																// tk: bei uns gibts kein windows oder mac !!!

//----------------------------------------------------------------------------------------
// Private and public filter settings
//----------------------------------------------------------------------------------------
typedef void	*FILTER_PARAM;																// private filter settings

#define	FLAT_SETTINGS_MAGIC	CHAR_CONST32('fflt')
struct FLAT_FILTER_SETTINGS																	// structure for filter settings (that could be saved to disk)
{
	ULONG		magic;																						// 'fflt'
	LONG		length;																						// size
	LONG		format;																						// format (0)
	LONG		reserved;																					// reserved (0)

	UCHAR		data[VARIABLE_ARRAY_SIZE];												// misc. data
};

#define	sizeof_FLAT_FILTER_SETTINGS	offsetofI(FLAT_FILTER_SETTINGS, data)	// size without the data array


//----------------------------------------------------------------------------------------
// The following structure is an argument to filter_start(). The Filter uses
// it as reference (to the bitmap) when it has to call services of the kernel.
//----------------------------------------------------------------------------------------
#define	BM_REF_MAGIC	CHAR_CONST32('bmrf')

struct BM_INFO																							// public definition
{
	ULONG		magic;																						// 'bmrf'
	LONG		length;																						// size
	LONG		format;																						// format (0)
	LONG		flags;
#define BMFLAG_NOUPDATE			1
#define BMFLAG_REPEATEFFECT	2

	LONG		image_color_space;																// color space of the image
	ULONG		image_px_format;																	// pixel format of the image
	ULONG		mask_px_format;																		// pixel format of the selection mask
	LONG		reserved1;

	LONG		preferred_tile_width;															// most efficient size for tiles
	LONG		preferred_tile_height;
	LONG		reserved2;
	LONG		reserved3;

	RECT32	image_rect;																				// coordinate space of the image bitmap
	RECT32	mask_rect;																				// (bounding) rectangle of the selected area [equal or smaller than image_rect]

};

struct FI_INFO
{
	LONG		size;
	LONG		pluginid;
};

typedef BM_INFO *BM_REF;
typedef FI_INFO *FI_REF;


//----------------------------------------------------------------------------------------
// BitmapTileGet() returns this structure for the requested bitmap tile
//----------------------------------------------------------------------------------------
#define	BM_TILE_MAGIC	CHAR_CONST32('bmtl')

struct BM_TILE
{
	ULONG		magic;																						// 'bmtl'
	LONG		length;																						// size
	LONG		format;																						// format (0)
	LONG		reserved;																					// reserved (0)

	void		*addr;																						// pointer to the bitmap data
	LONG		width;																						// width of a line in bytes
	LONG		color_space;																			// color space
	ULONG		px_format;																				// pixel format

	LONG		xmin;																							// minimum discrete x-coordinate of the bitmap
	LONG		ymin;																							// minimum discrete y-coordinate of the bitmap
	LONG		xmax;																							// maximum discrete x-coordinate of the bitmap + 1
	LONG		ymax;																							// maximum discrete y-coordinate of the bitmap + 1

	LONG		reserved4;																				// [reference of the color table] (0)
	LONG		reserved3;																				// [reference of the inverse color table] (0)
	LONG		reserved2;																				// reserved (0)
	LONG		reserved1;																				// reserved (0)

};


//----------------------------------------------------------------------------------------
// Server definitions
//----------------------------------------------------------------------------------------

// Tile modes
#define	TILE_MODE_SCRATCH			0x0000
#define	TILE_MODE_READ				0x0001
#define	TILE_MODE_WRITE				0x0002
#define	TILE_MODE_READ_WRITE	0x0003
#define	TILE_MODE_READ_SCRATCH	0x0004											// used only in the row fetcher (not supported by BfBitmapTileGet so far)

// tile types
#define	MASK_TILE_TYPE				0xFF00
#define	MASK_TILE_MODE				0x000F

#define	TILE_TYPE_MASK				0x0100
#define	TILE_TYPE_BM					0x0200

// diverse Kachel-Typen
#define	TILE_MASK_SCRATCH			0x0100												// temporary buffer [with the mask's pixel format]
#define	TILE_MASK_READ_ONLY		0x0101												// tile containing the selection mask
#define	TILE_MASK_WRITE				0x0102												// tile will be used as new selection mask
#define	TILE_MASK_READ_WRITE	0x0103												// tile contains the selection mask and will be used as new mask

#define	TILE_BM_SCRATCH				0x0200												// temporary buffer [with the image's pixel format]
#define	TILE_BM_READ_ONLY			0x0201												// tile containing the image bitmap
#define	TILE_BM_WRITE					0x0202												// tile will be used as new image bitmap
#define	TILE_BM_READ_WRITE		0x0203												// tile contains the image bitmap and will be used as new bitmap
#define	TILE_BM_READ_SCRATCH	0x0204												// used only in the row fetcher (not supported by BfBitmapTileGet so far)

// flags of GetBitmapTile()
#define TILE_REPEAT_BORDER		(1<<0)												// repeats the border for each pixel outside the bitmap
#define TILE_REPEAT_TILING		(1<<1)												// repeats the tiled image (wrap around)
#define TILE_LAST_RESULT			(1<<2)												// deliver the changed data instead of original data


// handling tiles
typedef	BM_TILE			*TYPE_BitmapTileGet(BM_REF bm, const RECT32 *src_rect, LONG width, LONG height, LONG tile_mode, LONG flags, LONG unused);
typedef	LONG				TYPE_BitmapTileDetach(BM_REF bm, BM_TILE *tile_buf, LONG flags);
typedef	LONG				TYPE_DiscardChanges(BM_REF bm);
typedef	LONG				TYPE_UpdateView(BM_REF bm);

// progress handling
typedef	LONG				PROGRESS_ID;
typedef	PROGRESS_ID	TYPE_ProgressNew(void);
typedef	LONG				TYPE_ProgressDelete(PROGRESS_ID pid);
typedef	LONG				TYPE_ProgressUpdate(PROGRESS_ID pid, LONG current_value, LONG max_value);

typedef	LONG				TYPE_display_bitmap(DRAW_CONTEXT dc, const BM_TILE *src, const RECT32 *src_rect, const RECT32 *dst_rect, LONG zero);


//----------------------------------------------------------------------------------------
// Client definitions
//----------------------------------------------------------------------------------------

// Error codes for the filter
#define	FILTER_OK					0
#define	FILTER_CANCEL			1
#define	FILTER_ABORT			2																	// filter action was stopped (TestBreak)

#define	FILTER_PARAM_ERR	-1																// invalid parameter
#define	FILTER_MEM_ERR		-2																// not enough memory
#define	FILTER_MISC_ERR		-3																// specific reason for error is unknown (e.g. intializing a class did not work)


// filter actions
typedef	LONG		TYPE_filter_start(FI_REF fi,BM_REF bm, FILTER_PARAM settings);
typedef	LONG		TYPE_filter_end(FI_REF fi,BM_REF bm, FILTER_PARAM settings);
typedef	LONG		TYPE_filter_run(FI_REF fi,BM_REF bm, FILTER_PARAM settings, Bool use_gui);

// filter settings
typedef	FILTER_PARAM					TYPE_filter_new_settings(FI_REF fi, const BaseContainer *bc);
typedef	LONG									TYPE_filter_delete_settings(FI_REF fi,FILTER_PARAM settings);
typedef	FLAT_FILTER_SETTINGS	*TYPE_filter_flatten_settings(FI_REF fi,FILTER_PARAM settings, void *(*flat_malloc)(LONG size));
typedef	FILTER_PARAM					TYPE_filter_unflatten_settings(FI_REF fi,FLAT_FILTER_SETTINGS *flat_settings);


//----------------------------------------------------------------------------------------
// function pointers
//----------------------------------------------------------------------------------------

#define BITMAPFILTER_MAGIC_V2			'bmpf'
#define BITMAPFILTER_MAGIC_V3			'bmf3'

struct BITMAP_FILTER
{
	LONG														magicid;

	// filter settings
	TYPE_filter_new_settings				*filter_new_settings;
	TYPE_filter_delete_settings			*filter_delete_settings;
	TYPE_filter_flatten_settings		*filter_flatten_settings;
	TYPE_filter_unflatten_settings	*filter_unflatten_settings;

	// filter actions
	TYPE_filter_start								*filter_start;
	TYPE_filter_end									*filter_end;
	TYPE_filter_run									*filter_run;

	// BITMAPFILTER_MAGIC_V3
	ULONG														flags;
		#define BMFI_SUPPORT_8BIT			(1<<0)
		#define BMFI_SUPPORT_16BIT		(1<<1)
		#define BMFI_SUPPORT_32BIT		(1<<2)
		#define BMFI_SUPPORT_NO_GREY	(1<<3)
		#define BMFI_SUPPORT_NO_RGB		(1<<4)
};

#endif
