# ifndef KAPA_H
# define KAPA_H

# include <X11/Xlib.h>
# include <png.h>
# include <dvo.h>

typedef struct {
  float *data1d;
  float **data2d;
  int Nx;
  int Ny;
  char *file;
  char *name;
} KiiImage;

typedef struct {
  double zero;
  double range;
  int logflux;
} KiiDisplayMode;

typedef struct {
  float x;
  float y;
  float dx;
  float dy;
  int type;
  char *text;
} KiiOverlay;

typedef enum {
  KII_OVERLAY_NONE, 
  KII_OVERLAY_TEXT, 
  KII_OVERLAY_BOX, 
  KII_OVERLAY_LINE,
  KII_OVERLAY_CIRCLE
} KiiOverlayType;

typedef enum {
  KAPA_LABEL_XM,
  KAPA_LABEL_YM,
  KAPA_LABEL_XP,
  KAPA_LABEL_YP,
  KAPA_LABEL_UL,
  KAPA_LABEL_UR,
  KAPA_LABEL_LL,
  KAPA_LABEL_LR,
} KapaLabelType;

typedef enum {
  KAPA_PS_NEWPLOT,
  KAPA_PS_NEWPAGE,
  KAPA_PS_RAWPAGE
} KapaPSmode;

typedef struct {
  char *name;
  float x;
  float y;
  float dx;
  float dy;
} KapaSection;

typedef struct {
  double xmin, xmax, ymin, ymax;
  int style, ptype, ltype, etype, ebar, color;
  double lweight, size;
  Coords coords;
  int flipeast, flipnorth;
  char axis[8], labels[8], ticks[8];
} Graphdata;

typedef struct {
  int dx, dy, ascent;
  unsigned char *bits;
} RotFont;

typedef struct {
  RotFont *font;
  char name[64];
  int size;
} FontSet;

typedef png_byte bDrawColor;

typedef struct {
  int Nx, Ny;
  bDrawColor **pixels;
} bDrawBuffer;

/* IOfuncs.c */
int KiiSendMessage (int device, char *format, ...);
int KiiSendCommand (int device, int length, char *format, ...);
int KiiSendCommandV (int device, int length, char *format, va_list argp);
int KiiScanMessage (int device, char *format, ...);
int KiiSendData (int device, char *data, int Nbytes);
char *KiiRecvData (int device);

/* KiiOpen.c */
int KiiOpen (char *kii_exec, char *name);
int KiiClose (int socket);
int KiiWait (char *sockpath);

/* KiiPicture.c */
int KiiNewPicture1D (int fd, KiiImage *image, KiiDisplayMode *mode, Coords *coords);
int KiiNewPicture2D (int fd, KiiImage *image, KiiDisplayMode *mode, Coords *coords);

/* KiiOverlay.c */
int KiiSelectOverlay (char *name, int *number);
int KiiLoadOverlay (int fd, KiiOverlay *overlay, int Noverlay, char *name);
int KiiEraseOverlay (int fd, char *name);
int KiiSaveOverlay (int fd, int celestial, char *name, char *file);
int KiiOverlayTypeByName (char *name);
char *KiiOverlayTypeByNumber (int n);

/* KiiConvert.c */
int KiiPS (int fd, int scaleMode, int pageMode, char *filename, char *pagename);
int KiiJPEG (int fd, char *filename);
int KapaPNG (int fd, char *filename);
int KapaPPM (int fd, char *filename);

/* KiiCursor.c */
int KiiCursorOn (int fd);
int KiiCursorOff (int fd);
int KiiCursorRead (int fd, double *x, double *y, char *key);

/* KapaWindow.c */
int KiiResize (int fd, int Nx, int Ny);
int KiiCenter (int fd, double x, double y, int zoom);
int KapaBox (int fd, Graphdata *graphdata);
int KapaClear (int fd, int ClearSection);
int KapaInitGraph (Graphdata *graphdata);
int KapaPrepPlot (int fd, int Npts, Graphdata *graphmode);
int KapaPlotVector (int fd, int Npts, float *values);
int KapaSetFont (int fd, char *name, int size);
int KapaSendLabel (int fd, char *string, int mode);
int KapaSendTextline (int fd, char *string, float x, float y, float angle);
int KapaSetLimits (int fd, Graphdata *graphmode);
int KapaSetSection (int fd, KapaSection *section);
int KapaSelectSection (int fd, char *name);
int KapaGetSection (int fd, char *name);

/* KapaColors */
int KapaColorByName (char *name);
int KapaColormapSize ();
char *KapaColorRGBString (int N);
char *KapaColorName (int N);
png_color *KapaPNGPalette (int *Npalette);
unsigned long *KapaX11colors (Display *display, Colormap colormap, unsigned long default_color, int *Ncolors);

/* RotFont.c */
void InitRotFonts PROTO(());
int SetRotFont PROTO((char *name, int size));
char *GetRotFont PROTO((int *size));
RotFont *GetRotFontData (double *scale);
int RotStrlen PROTO((char *c));

/* DrawRotString.c */
int DrawRotText PROTO((int x, int y, char *string, int pos, double angle));
int DrawRotBitmap PROTO((int x, int y, int dx, int dy, unsigned char *bitmap, int mode, double angle, double scale));
int DrawRotTextInit (Display *display, Window window, GC gc, unsigned long fore, unsigned long back);


/* PSRotFont.c */
void PSRotText PROTO((FILE *f, int x, int y, char *string, int pos, double angle));
void PSDumpRotSegment PROTO((FILE *f, char *segment, int *Nseg));
void PSSetFont PROTO((FILE *f, char *name, int size));

/* bDrawFuncs.c */
bDrawBuffer *bDrawBufferCreate (int Nx, int Ny);
void bDrawBufferFree (bDrawBuffer *buffer);
void bDrawSetBuffer (bDrawBuffer *buffer);
void bDrawSetStyle (bDrawColor color, int lw, int lt);
void bDrawPoint (int x, int y);
void bDrawPointf (float x, float y);

void bDrawArc (double Xc, double Yc, double Xr, double Yr, double Ts, double Te);
void bDrawCircle (double Xc, double Yc, double radius);
void bDrawCircleFill (double xc, double yc, double radius);

void bDrawLine (double x1, double y1, double x2, double y2);
void bDrawLineWeight (int X1, int Y1, int X2, int Y2, int swapcoords);
void bDrawLineBresen (int X1, int Y1, int X2, int Y2, int swapcoords);
void bDrawLineHorizontal (int X1, int X2, int Y);
void bDrawLineVertical (int X, int Y1, int Y2);

void bDrawRectOpen (double x1, double y1, double x2, double y2);
void bDrawRectFill (double x1, double y1, double x2, double y2);
void bDrawTriOpen (double x1, double y1, double x2, double y2, double x3, double y3);
void bDrawTriFill (double x1, double y1, double x2, double y2, double x3, double y3);

/* bDrawRotFont.c */
int bDrawRotText (int x, int y, char *string, int pos, double angle);
int bDrawRotBitmap (int x, int y, int dx, int dy, unsigned char *bitmap, int mode, double angle, double scale);

/* define Kapa names for shared functions */
# define KapaOpen(p,n) KiiOpen(p,n)
# define KapaResize(fd,Nx,Ny) KiiResize(fd,Nx,Ny)
# define KapaCenter(fd,x,y,z) KiiResize(fd,x,y,z)
# define KapaCursorOn(fd) KiiCursorOn(fd)
# define KapaCursorOff(fd) KiiCursorOff(fd)
# define KapaCursorRead(fd,x,y,k) KiiCursorRead(fd,x,y,k)
# define KapaPS(fd,s,r,f) KiiPS(fd,s,r,f)

# define KapaClose(socket) KiiClose(socket)
# define KapaWait(sockpath) KiiWait(sockpath)
# define KapaSendCommandV(device, length, format, argp) KiiSendCommandV(device, length, format, argp)
# define KapaSendData(device, data, Nbytes) KiiSendData(device, data, Nbytes)
# define KapaRecvData(device) KiiRecvData(device)

/* these use varargs: is this safe, or should we make a stub function? */
# define KapaSendMessage KiiSendMessage
# define KapaSendCommand KiiSendCommand
# define KapaScanMessage KiiScanMessage

# endif
