/*********************************************************/
/* This source code copyright (c) 1991-2001, Aaron Giles */
/* See the Read Me file for licensing information.       */
/* Contact email: mac@aarongiles.com                     */
/*********************************************************/

#ifndef __JPEGVIEWTYPES__
#define __JPEGVIEWTYPES__

#include <Files.h>
#include <ImageCompression.h>
#include <Palettes.h>
#include <QDOffscreen.h>
#include <QuickDraw.h>
#include <Types.h>
#include <Windows.h>
#include "MarchingAnts.h"

typedef unsigned char uchar;
typedef unsigned short ushort;
typedef unsigned long ulong;

typedef struct ImageItem ImageItem, *ImagePtr, **ImageHandle;

//===================================================================================
// NestedProgress: structure for holding data necessary to do nested progress 
// procedures.
//===================================================================================

typedef struct NestedProgress
{
	ICMProgressProcRecord prog;
	short level;
	Fixed begin, end;
	char aborted;
} NestedProgress, *NestedProgressPtr, **NestedProgressHandle;

//===================================================================================
// JVPortParams: structure for holding drawing parameters that describe a port.
//===================================================================================

typedef struct JVPortParams
{
	CGrafPtr port;					// pointer to the port to draw in
	Rect outer;						// bounds of the image, in dest. coordinates
	Boolean dither;					// flag: true to dither when drawing
} JVPortParams;

//===================================================================================
// JVDrawParams: structs for holding the drawing parameters for a particular drawing 
// operation, both onscreen and offscreen.
//===================================================================================

typedef struct JVDrawParams
{
	struct JVDrawParams **next;		// handle to the next in the list
	Rect bounds;					// full-size bounds of image
	CGrafPtr dummy;					// dummy GrafPort for drawing into
	JVPortParams on;				// on-screen drawing parameters
	JVPortParams off;				// off-screen drawing parameters
	JVPortParams extra;				// extra off-screen drawing parameters
	Boolean quantize;				// quantizing flag: true to fill the histogram
	short quality;					// drawing quality to be used
	Handle privateData;				// private data for the plug-in 
	NestedProgress progress;		// nested progress procedure record
	short endprogress;				// scan line where we stop drawing progress
	RgnHandle newupdates;			// handle to new updates in the onscreen port
	PixMapPtr src;					// pointer to the source pixmap for DeviceCopy
	Rect srcrect;					// source bounds for DeviceCopy
	Rect dstrect;					// destination bounds for DeviceCopy
	Rect drawProgressRect;			// rectangle used by drawing progress
	Ptr oldbits;					// pointer to the old StdBits routine for FixBits
	CQDProcs *oldprocs;				// pointer to the old set of CQDProcs for FixBits
} JVDrawParams, *JVDrawParamsPtr, **JVDrawParamsHandle;

typedef Boolean (*idImagePtr)(uchar *theData, long theSize, short refNum, FSSpec *theSpec);
typedef OSErr (*OpenImagePtr)(ImageHandle theImage);
typedef OSErr (*DrawImagePtr)(Handle theHandle, JVDrawParamsHandle theParams);
typedef OSErr (*LoadImagePtr)(short theFile, ImageHandle theImage);
typedef OSErr (*SaveImagePtr)(FSSpec *theSpec, Handle theHandle, GWorldPtr thePreview, Rect *theRect, Handle privates);
typedef OSErr (*FixImagePtr)(ImageHandle theImage, Handle *finalData, Boolean palette);
typedef OSErr (*CloneImagePtr)(ImageHandle origImage, ImageHandle newImage);
typedef OSErr (*CloseImagePtr)(ImageHandle theImage);
typedef pascal void (*StdBitsProcPtr)(PixMap *, Rect *, Rect *, short, RgnHandle);

/*
 * sMonitorItem = linked list item describing a graphics device
 *
 */

typedef struct MonitorItem
{
	struct MonitorItem **next;		// pointer to the next monitor
	RgnHandle gray;					// this monitor's contribution to the GrayRgn
	RgnHandle screen;				// the full screen area of this monitor
	GDHandle device;				// handle to the original device record
	short oldres;					// old itable resolution
	Boolean color;					// flag: true if monitor has a greyscale CLUT
	Boolean full;					// flag: true if monitor is currently full-screen
	short depth;					// last depth of the monitor
	Rect rect;						// screen rectangle, in global coordinates
} MonitorItem, *MonitorPtr, **MonitorHandle;

/*
 * sImageFormat = list of pointers to various actions that need to be performed on
 *					images; used to simply opening/saving [ImageFormat, ImageFormatPtr]
 */

typedef struct ImageFormat
{
	OSType inType;				// the input file type associated with this guy
	OSType altInType;			// alternate input file type
	OSType outType;				// the output file type associated with this guy
	Boolean customPreviews;		// flag: true if this type has different previews
	Boolean doesColors;			// flag: true if the file type can handle quantization
	Boolean doesBanded;			// flag: true if we can save banded images
	Boolean positiveID;			// flag: true if we can make a positive ID
	Str31 formatName;			// string identifying the format
	idImagePtr id;				// identify the image as this type
	LoadImagePtr load;			// read in the data
	OpenImagePtr open;			// initialize the ImageRecord
	DrawImagePtr draw;			// draw to a destination PixMap
	FixImagePtr fix;			// convert PICT or JFIF to this type
	SaveImagePtr save;			// save the data
	CloneImagePtr clone;		// clone the image
	CloseImagePtr close;		// close the image
} ImageFormat, *ImageFormatPtr;

enum {
	kStandardQTPreview = false,
	kCustomPreview = true,
	kCanSaveQuantization = true,
	kCantSaveQuantization = false,
	kCanSaveBanded = true,
	kCantSaveBanded = false,
	kIDIsPositive = true,
	kIDIsPossible = false
};

/*
 * sImageItem = linked list item for images [ImageItem, ImagePtr, ImageHandle]
 */

struct ImageItem {
	unsigned short id;			// id to flag this as an ImageItem
	struct ImageItem **next;	// pointer to next image record
	FSSpec file;				// file specification of the original image
	Handle data;				// handle to the actual image data
	Handle comments;			// handle to the comments text, if it exists
	ImageFormatPtr format;		// pointer to the appropriate set of procs
	OSType compression;			// type of compression used
	Str63 compressionDesc;		// string description of compression
	ImageDescription desc;		// image descriptor record, for QT images
	PaletteHandle palette;		// current palette for this image
	PaletteHandle ipalette;		// custom palette for this image, if any
	PaletteHandle qpalette;		// quantized palette for this image, if any
	short npalette;				// the numbered palette of the image
	GWorldPtr gworld;			// offscreen copy of the image
	WindowPtr window;			// window record for this image
	RgnHandle update;			// cached update region
	MonitorHandle dmon;			// pointer to the deepest intersecting monitor
	MonitorHandle smon;			// pointer to the shallowest intersecting monitor
	short quality;				// display quality for this image
	ulong flags;				// set of flags describing the image
	short depth;				// depth of the image (1,2,4,8,16,24,32)
	Rect grect;					// greater rectangle of the image (in pixels)
	Rect qrect;					// QuickDraw rectangle (in 72dpi pixels)
	Rect crect;					// cropped image area, relative to grect (in pixels)
	Rect wrect;					// rectangle of this images's window (in screen pixels)
	AntsReference ants;			// reference to our marching ants
	short num;					// number appended to the end
	long dtime;					// last decompressing & display time
	Handle privateData;			// private data for the plug-in
};

#endif
