
/* top-level program */
int	      args		  PROTO((int *argc, char **argv));
void          SetUpGraphic        PROTO((int *argc, char **argv));
void          DefineLayout        PROTO((int, char **));
int	      EventLoop		  PROTO(());
void	      CloseDisplay	  PROTO(());

/* SetUpGraphic */
char         *CheckDisplayName    PROTO((int *argc, char **argv));
Display      *OpenDisplay         PROTO((char *name, int *screen));
void          CheckColors         PROTO((Graphic *graphic, int *argc, char **argv));

/* SetUpWindow */
void          CheckGeometry       PROTO((Graphic *graphic, int *argc, char **argv));
void          TopWindow           PROTO((Graphic *graphic, Icon *icon));
void          CreateWindow        PROTO((Graphic *graphic, Window parent, int border, long events));
void	      MakeGC	    	  PROTO((Graphic *graphic));
void          MakeCursor          PROTO((Graphic *graphic, unsigned int cursor));
void          LoadFont            PROTO((Graphic *graphic, int *argc, char **argv, char *default_name));
void          SetNormalHints      PROTO((Graphic *graphic));
void          SetWMHints          PROTO((Graphic *graphic, Icon *icon));
void          NameWindow          PROTO((Graphic *graphic, char *name));
void          MapWindow           PROTO((Graphic *graphic));
void          CheckVisual         PROTO((Graphic *graphic, int *argc, char **argv));

/* X drawing utilities */
void	      DrawFrame		  PROTO((KapaGraphWidget *graph));
int           DrawObjects         PROTO((KapaGraphWidget *graph));
void          DrawLabels          PROTO((KapaGraphWidget *graph));
void          EraseLabels         PROTO((KapaGraphWidget *graph));
void          DrawLabelsRaw       PROTO((Graphic *graphic, KapaGraphWidget *graph, int color));
void	      DrawTextlines	  PROTO((KapaGraphWidget *graph));
void          DrawConnect         PROTO((KapaGraphWidget *graph, Gobjects *objects));
void          DrawHistogram       PROTO((KapaGraphWidget *graph, Gobjects *objects));
int           DrawObjectN         PROTO((KapaGraphWidget *graph, Gobjects *objects));
void          DrawPoints          PROTO((KapaGraphWidget *graph, Gobjects *objects));
void          DrawXErrors         PROTO((KapaGraphWidget *graph, Gobjects *objects));
void          DrawYErrors         PROTO((KapaGraphWidget *graph, Gobjects *objects));
void	      DrawTick		  PROTO((int fx, int fy, int dfx, int dfy, int P, double min, double max, double value, int IsLabel, int IsMajor, int naxis));
void	      AxisTickScale	  PROTO((Axis *axis, double *major, double *minor));

/* EventLoop */
int           PScommand           PROTO((int sock));
int           CheckPipe           PROTO(());
int           Reconfig            PROTO((XEvent *event));
void          Refresh             PROTO(());

/* CheckPipe */
int           PNGit               PROTO((int sock));
int           PPMit               PROTO((int sock));
int           LoadFrame           PROTO((int sock));
int           LoadObject          PROTO((int sock));
int           LoadLabels          PROTO((int sock));
int           LoadTextlines       PROTO((int sock));
int           Resize              PROTO((int sock));
int           GetLimits           PROTO((int sock));
int           SetLimits           PROTO((int sock));
int           SetSection          PROTO((int sock));
int           ListSection         PROTO((int sock));
int           MoveSection         PROTO((int sock));
int           DefineSection       PROTO((int sock));
int           SetFont             PROTO((int sock));
int           EraseCurrentPlot    PROTO((void));
int           ErasePlots          PROTO((void));
int           EraseSections       PROTO((void));
int           EraseImage          PROTO((void));
int           SetGraphData        PROTO((int sock));
int           GetGraphData        PROTO((int sock));
int           SetImageData        PROTO((int sock));
int           GetImageData        PROTO((int sock));
int           SetImageCoords      PROTO((int sock));
int           GetImageCoords      PROTO((int sock));
int           GetImageRange       PROTO((int sock));
int           SetChannel          PROTO((int sock));
int           SetColormapFromPipe PROTO((int sock));

int           LoadVectorData      PROTO((int sock, KapaGraphWidget *graph, int N, char *type));

/* Section Utilities */
Section      *InitSection	  PROTO(());
void          FreeSection	  PROTO((Section *section));
void          FreeSections	  PROTO(());
Section      *AddSection	  PROTO((char *name, float x, float y, float dx, float dy));
int           DelSection	  PROTO((char *name));
int           GetSectionByName	  PROTO((char *name));
int           GetNumberOfSections PROTO(());
Section      *GetSectionByNumber  PROTO((int N));
Section      *GetActiveSection	  PROTO(());
int           SetActiveSectionByNumber PROTO((int N));
int           ListSection         PROTO((int sock));
void          SetSectionSizes     PROTO((Section *section));

KapaGraphWidget *InitGraph        PROTO(());
void          DrawGraph           PROTO((KapaGraphWidget *graph));
void          SetGraphSize        PROTO((Section *section));
void          FreeGraph           PROTO((KapaGraphWidget *graph));

void          InitLayout          PROTO((int argc, char **argv));

/* PS drawing utilities */
int           PSit                PROTO((char *filename, char *pagename, int scaleMode, int pageMode));
int           PSFrame             PROTO((KapaGraphWidget *graph, FILE *f));
int           PSObjects           PROTO((KapaGraphWidget *graph, FILE *f));
void          PSLabels            PROTO((KapaGraphWidget *graph, FILE *f));
void	      PSTextlines	  PROTO((KapaGraphWidget *graph, FILE *f));
void          PSConnect           PROTO((KapaGraphWidget *graph, Gobjects *objects, FILE *f));
void          PSHistogram         PROTO((KapaGraphWidget *graph, Gobjects *objects, FILE *f));
void          PSPoints            PROTO((KapaGraphWidget *graph, Gobjects *objects, FILE *f));
void          PSXErrors           PROTO((KapaGraphWidget *graph, Gobjects *objects, FILE *f));
void          PSYErrors           PROTO((KapaGraphWidget *graph, Gobjects *objects, FILE *f));
void	      PSTick		  PROTO((FILE *f, double fx, double fy, double dfx, double dfy, int P, double min, double max, double value, int IsLabel, int IsMajor, int naxis));
void          ClipLinePS          PROTO((double x0, double y0, double x1, double y1, double X0, double Y1, double X1, double Y0, FILE *f));
int           PSimage    	  PROTO((KapaImageWidget *image, FILE *f));
void 	      PSOverlay  	  PROTO((KapaImageWidget *image, int N, FILE *f, int extra));
void 	      PSPixmap8  	  PROTO((Graphic *graphic, KapaImageWidget *image, FILE *f));
void 	      PSPixmap16 	  PROTO((Graphic *graphic, KapaImageWidget *image, FILE *f));
void 	      PSPixmap24 	  PROTO((Graphic *graphic, KapaImageWidget *image, FILE *f));
void 	      PSPixmap32 	  PROTO((Graphic *graphic, KapaImageWidget *image, FILE *f));

/* kapa bDraw Functions */
int	      bDrawFrame	  PROTO((KapaGraphWidget *graph));
int	      bDrawObjects	  PROTO((KapaGraphWidget *graph));
void	      bDrawLabels	  PROTO((KapaGraphWidget *graph));
void	      bDrawTextlines	  PROTO((KapaGraphWidget *graph));
void	      bDrawConnect	  PROTO((KapaGraphWidget *graph, Gobjects *object));
void	      bDrawHistogram	  PROTO((KapaGraphWidget *graph, Gobjects *object));
void	      bDrawPoints	  PROTO((KapaGraphWidget *graph, Gobjects *object));
void	      bDrawXErrors	  PROTO((KapaGraphWidget *graph, Gobjects *object));
void	      bDrawYErrors	  PROTO((KapaGraphWidget *graph, Gobjects *object));
void	      bDrawTick		  PROTO((double fx, double fy, double dfx, double dfy, int P, double min, double max, double value, int IsLabel, int IsMajor, int naxis));
void	      bDrawClipLine	  PROTO((double x0, double y0, double x1, double y1, double X0, double Y1, double X1, double Y0));
bDrawBuffer  *bDrawIt		  PROTO(());
void          bDrawGraph          PROTO((KapaGraphWidget *graph));

/* misc support */
int           LastEvent           PROTO((Display *display, int type, XEvent *event));
void	      FreeObjectData	  PROTO((Gobjects *object));
void	      FlushDisplay	  PROTO(());
unsigned long GetColor            PROTO((Display *display, char *name, Colormap colormap, unsigned long default_color));
void          QuitX               PROTO((Display *display, char *message));
Graphic      *GetGraphic          PROTO(());
int           GetPixelCount       PROTO((int sock));

int           Center              PROTO(());
void          SetColorScale       PROTO((Graphic *graphic, KapaImageWidget *image));
void          SetColorScale1D     PROTO((Graphic *graphic, KapaImageWidget *image));
int           SetColorScale3D     PROTO((Graphic *graphic, KapaImageWidget *image));
void          Remap               PROTO((Graphic *graphic, KapaImageWidget *image));
void          Remap8              PROTO((Graphic *graphic, KapaImageWidget *image, Picture *picture, Matrix *matrix));
void          Remap16             PROTO((Graphic *graphic, KapaImageWidget *image, Picture *picture, Matrix *matrix));
void          Remap24             PROTO((Graphic *graphic, KapaImageWidget *image, Picture *picture, Matrix *matrix));
void          Remap32             PROTO((Graphic *graphic, KapaImageWidget *image, Picture *picture, Matrix *matrix));
int           LoadPicture         PROTO((int sock));

KapaImageWidget *InitImageWidget  PROTO(());
int           InitImageChannel    PROTO((KapaImageChannel *channel));
void          FreeImage           PROTO((KapaImageWidget *image));
void          SetImageSize        PROTO((Section *section));

void          InitButtonSize      PROTO((Button *button, int width, int height, char *bitmap));
void          InitButtonFunc      PROTO((Button *button, int (*function)()));
void          DrawImage           PROTO((KapaImageWidget *image));
void          DrawButton          PROTO((Graphic *graphic, Button *button));
void          DrawBitmap          PROTO((Graphic *graphic, int x, int y, int dx, int dy, char *bitmap, int mode));
void          CrossHairs          PROTO((Graphic *graphic, Picture *image));
void          hh_hms              PROTO((char *line, double ra, double dec, char sep));

int           SetColormap         PROTO((char *name));
void          MakeColormap        PROTO((int argc, char **argv));

void          PaintOverlay        PROTO((Graphic *graphic, KapaImageWidget *image, int N));
void          PaintTickmarks      PROTO((Graphic *graphic, KapaImageWidget *image));

void          Picture_to_Image    PROTO((double *x1, double *y1, double x2, double y2, Picture *picture));
void          Screen_to_Image     PROTO((double *x1, double *y1, double x2, double y2, Picture *picture));
void          Image_to_Picture    PROTO((double *x1, double *y1, double x2, double y2, Picture *picture));
void          Image_to_Screen     PROTO((double *x1, double *y1, double x2, double y2, Picture *picture));
void          Picture_Lower 	  PROTO((int *i_start, int *j_start, Matrix *matrix, Picture *picture));
void          Picture_Upper 	  PROTO((int *i_end, int *j_end, Matrix *matrix, Picture *picture));

void          DragColorbar        PROTO((Graphic *graphic, KapaImageWidget *image, XButtonEvent *mouse_event));
void          ResetColorbar       PROTO((Graphic *graphic, double start, double slope));
void          ReorientOnButton    PROTO((Graphic *graphic, KapaImageWidget *image, XButtonEvent *mouse_event));
void          Reorient            PROTO((Graphic *graphic, KapaImageWidget *image, double X, double Y, int mode));

Button       *CheckButtons        PROTO((XButtonEvent *event, KapaImageWidget *image));
int           InButton            PROTO((XButtonEvent *event, Button *button));
int           InPicture           PROTO((XButtonEvent *event, Picture *picture));

/* Button Functions */
int           greycolors	  PROTO((Graphic *graphic, KapaImageWidget *image));
int	      heat		  PROTO((Graphic *graphic, KapaImageWidget *image));
int	      rainbow		  PROTO((Graphic *graphic, KapaImageWidget *image));
int	      Recenter		  PROTO((Graphic *graphic, KapaImageWidget *image));
int	      Rescale		  PROTO((Graphic *graphic, KapaImageWidget *image));
int	      RecenterRescale	  PROTO((Graphic *graphic, KapaImageWidget *image));
int	      ToggleDEG		  PROTO((Graphic *graphic, KapaImageWidget *image));
int	      FlipImageX	  PROTO((Graphic *graphic, KapaImageWidget *image));
int	      FlipImageY	  PROTO((Graphic *graphic, KapaImageWidget *image));
int	      Overlay0		  PROTO((Graphic *graphic, KapaImageWidget *image));
int	      Overlay1		  PROTO((Graphic *graphic, KapaImageWidget *image));
int	      Overlay2		  PROTO((Graphic *graphic, KapaImageWidget *image));
int	      Overlay3		  PROTO((Graphic *graphic, KapaImageWidget *image));
int	      PSfunction	  PROTO((Graphic *graphic, KapaImageWidget *image));

/* misc functions */
void 	      SetToolbox   (int sock);
int  	      EraseOverlay (int sock);
int  	      LoadOverlay  (int sock);
int  	      SaveOverlay  (int sock);
int  	      CSaveOverlay (int sock);
int  	      JPEGit24     (int sock);

int  	      UpdatePointer (Graphic *graphic, XMotionEvent *event);
int  	      InterpretPresses (Graphic *graphic, XButtonEvent *event);
int  	      InterpretKeys (Graphic *graphic, XKeyEvent *event);
void 	      InitPipe (char *namedSocket);
void 	      EraseGraph (KapaGraphWidget *graph);
void 	      StatusBox (Graphic *graphic, KapaImageWidget *image);

void 	      CreatePicture (KapaImageWidget *image, Graphic *graphic);
void 	      CreateColorbar (KapaImageWidget *image, Graphic *graphic);
void 	      CreateZoom (Graphic *graphic, KapaImageWidget *image);
void 	      CreateWide (Graphic *graphic, KapaImageWidget *image);
void 	      UpdateStatusBox (Graphic *graphic, KapaImageWidget *image, double x, double y, double z, int mode);

// void 	      CreateZoom8  (KapaImageWidget *image, Graphic *graphic, double x, double y);
// void 	      CreateZoom16 (KapaImageWidget *image, Graphic *graphic, double x, double y);
// void 	      CreateZoom24 (KapaImageWidget *image, Graphic *graphic, double x, double y);
// void 	      CreateZoom32 (KapaImageWidget *image, Graphic *graphic, double x, double y);

int  	      GetActiveSocket ();
void 	      InvertButton (Graphic *graphic, Button *button);
void 	      bDrawOverlay (KapaImageWidget *image, int N);

/* color cube tools */
CCNode *CCNodeAlloc ();
CCNode *CCFindChild (CCNode *node, float x, float y, float z);
int CCSplitNode (CCNode *node);
int CCSplitNodeIterate (CCNode *node, int current, int max);
void CCNodeFree (CCNode *node);
CCNode *CCFindBottom (CCNode *top, float x, float y, float z);
int CCNodeExtractCounts (CCNode *node, float **values, int *nvalues, int *NVALUES);
int CCNodeDivideLimit (CCNode *node, float minValue);
int CCNodeInitCounts (CCNode *node, float value);

/* 3D color histogram */
void ColorHistogram (KapaImageWidget *image, CCNode *cube);
void CCNodeSetColorPixels (KapaImageWidget *image, CCNode *cube);
int CCNodeSetColorMap (CCNode *node, XColor *cmap, int Npixels, int *current);

int SetColorScale3D_CC (Graphic *graphic, KapaImageWidget *image);
int SetColorCubeHistogram ();
