[0001]
[0002]
[0003]
[0004]
[0005]
[0006]
[0007]
[0008]
[0009]
[0010]
[0011]
[0012]
[0013]
[0014]
[0015]
[0016]
[0017]
[0018]
[0019]
[0020]
[0021]
[0022]
[0023]
[0024]
[0025]
[0026]
[0027]
[0028]
[0029]
[0030]
[0031]
[0032]
[0033]
[0034]
[0035]
[0036]
[0037]
[0038]
[0039]
[0040]
[0041]
[0042]
[0043]
[0044]
[0045]
[0046]
[0047]
[0048]
[0049]
[0050]
[0051]
[0052]
[0053]
[0054]
[0055]
[0056]
[0057]
[0058]
[0059]
[0060]
[0061]
[0062]
[0063]
[0064]
[0065]
[0066]
[0067]
[0068]
[0069]
[0070]
[0071]
[0072]
[0073]
[0074]
[0075]
[0076]
[0077]
[0078]
[0079]
[0080]
[0081]
[0082]
[0083]
[0084]
[0085]
[0086]
[0087]
[0088]
[0089]
[0090]
[0091]
[0092]
[0093]
[0094]
[0095]
[0096]
[0097]
[0098]
[0099]
[0100]
[0101]
[0102]
[0103]
[0104]
[0105]
[0106]
[0107]
[0108]
[0109]
[0110]
[0111]
[0112]
[0113]
[0114]
[0115]
[0116]
[0117]
[0118]
[0119]
[0120]
[0121]
[0122]
[0123]
[0124]
[0125]
[0126]
[0127]
[0128]
[0129]
[0130]
[0131]
[0132]
[0133]
[0134]
[0135]
[0136]
[0137]
[0138]
[0139]
[0140]
[0141]
[0142]
[0143]
[0144]
[0145]
[0146]
[0147]
[0148]
[0149]
[0150]
[0151]
[0152]
[0153]
[0154]
[0155]
[0156]
[0157]
[0158]
[0159]
[0160]
[0161]
[0162]
[0163]
[0164]
[0165]
[0166]
[0167]
[0168]
[0169]
[0170]
[0171]
[0172]
[0173]
[0174]
[0175]
[0176]
[0177]
[0178]
[0179]
[0180]
[0181]
[0182]
[0183]
[0184]
[0185]
[0186]
[0187]
[0188]
[0189]
[0190]
[0191]
[0192]
[0193]
[0194]
[0195]
[0196]
[0197]
[0198]
[0199]
[0200]
[0201]
[0202]
[0203]
[0204]
[0205]
[0206]
[0207]
[0208]
[0209]
[0210]
[0211]
[0212]
[0213]
[0214]
[0215]
[0216]
[0217]
[0218]
[0219]
[0220]
[0221]
[0222]
[0223]
[0224]
[0225]
[0226]
[0227]
[0228]
[0229]
[0230]
[0231]
[0232]
[0233]
[0234]
[0235]
[0236]
[0237]
[0238]
[0239]
[0240]
[0241]
[0242]
[0243]
[0244]
[0245]
[0246]
[0247]
[0248]
[0249]
[0250]
[0251]
[0252]
[0253]
[0254]
[0255]
[0256]
[0257]
[0258]
[0259]
[0260]
[0261]
[0262]
[0263]
[0264]
[0265]
[0266]
[0267]
[0268]
[0269]
[0270]
[0271]
[0272]
[0273]
[0274]
[0275]
[0276]
[0277]
[0278]
[0279]
[0280]
[0281]
[0282]
[0283]
[0284]
[0285]
[0286]
[0287]
[0288]
[0289]
[0290]
[0291]
[0292]
[0293]
[0294]
[0295]
[0296]
[0297]
[0298]
[0299]
[0300]
[0301]
[0302]
[0303]
[0304]
[0305]
[0306]
[0307]
[0308]
[0309]
[0310]
[0311]
[0312]
[0313]
[0314]
[0315]
[0316]
[0317]
[0318]
[0319]
[0320]
[0321]
[0322]
[0323]
[0324]
[0325]
[0326]
[0327]
[0328]
[0329]
[0330]
[0331]
[0332]
[0333]
[0334]
[0335]
[0336]
[0337]
[0338]
[0339]
[0340]
[0341]
[0342]
[0343]
[0344]
[0345]
[0346]
[0347]
[0348]
[0349]
[0350]
[0351]
[0352]
[0353]
[0354]
[0355]
[0356]
[0357]
[0358]
[0359]
[0360]
[0361]
[0362]
[0363]
[0364]
[0365]
[0366]
[0367]
[0368]
[0369]
[0370]
[0371]
[0372]
[0373]
[0374]
[0375]
[0376]
[0377]
[0378]
[0379]
[0380]
[0381]
[0382]
[0383]
[0384]
[0385]
[0386]
[0387]
[0388]
[0389]
[0390]
[0391]
[0392]
[0393]
[0394]
[0395]
[0396]
[0397]
[0398]
[0399]
[0400]
[0401]
[0402]
[0403]
[0404]
[0405]
[0406]
[0407]
[0408]
[0409]
[0410]
[0411]
[0412]
[0413]
[0414]
[0415]
[0416]
[0417]
[0418]
[0419]
[0420]
[0421]
[0422]
[0423]
[0424]
[0425]
[0426]
[0427]
[0428]
[0429]
[0430]
[0431]
[0432]
[0433]
[0434]
[0435]
[0436]
[0437]
[0438]
[0439]
[0440]
[0441]
[0442]
[0443]
[0444]
[0445]
[0446]
[0447]
[0448]
[0449]
[0450]
[0451]
[0452]
[0453]
[0454]
[0455]
[0456]
[0457]
[0458]
[0459]
[0460]
[0461]
[0462]
[0463]
[0464]
[0465]
[0466]
[0467]
[0468]
[0469]
[0470]
[0471]
[0472]
[0473]
[0474]
[0475]
[0476]
[0477]
[0478]
[0479]
[0480]
[0481]
[0482]
[0483]
[0484]
[0485]
[0486]
[0487]
[0488]
[0489]
[0490]
[0491]
[0492]
[0493]
[0494]
[0495]
[0496]
[0497]
[0498]
[0499]
[0500]
[0501]
[0502]
[0503]
[0504]
[0505]
[0506]
[0507]
[0508]
[0509]
[0510]
[0511]
[0512]
[0513]
[0514]
[0515]
[0516]
[0517]
/*****************************************************************************/
/*
                                 PlotSpi.c

This little (and simple-minded) plotting package was developed to support the 
HyperSpi application.  It allows simple, two-colour (black on white) graphs to 
be created in memory (as pixmaps).  The bit-manipulation routines are long-
word aligned (in an attempt to make AXP systems efficient).

The function parameter 'PlotOperation' specifies whether the the pixel will be 
turned on, turned off or changed to the complement of the existing pixel 
(logical exclusive-OR).


BUILD_DETAILS
-------------
See BUILD_HYPERSPI.COM


COPYRIGHT
---------
Copyright (C) 1995-2021 Mark G.Daniel

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.


VERSION HISTORY
---------------
21-MAY-2011  MGD  maintenance
01-AUG-1997  MGD  general maintenance in line with hyperspi.c
20-JUN-1995  MGD  initial development
*/
/*****************************************************************************/

/* standard C header files */
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <errno.h>

/***************/
/* definitions */
/***************/

#define boolean int
#define true 1
#define false 0

#define VMSok(x) ((x) & STS$M_SUCCESS)
#define VMSnok(x) !(((x) & STS$M_SUCCESS))

#ifdef __VMS
    /* vaxc$errno will be an extern int */
#   define ERROR_VALUE vaxc$errno
#else
#   define ERROR_VALUE errno
#endif

#define PLOT_ON 0
#define PLOT_OFF 1
#define PLOT_XOR 2

const int  LongBits = sizeof(unsigned long) * 8;

boolean  DoPlotDiagnostic;

int  PlotAreaX,
     PlotAreaY,
     PlotScaleX,
     PlotScaleY;

char *GraphPtr;

extern boolean  Debug;
extern int  RecordSampleRate,
            FromHour,
            SizeOfPlotX,
            SizeOfPlotY,
            SizeOfMarginX,
            SizeOfMarginY,
            XMag,
            YMag;
extern char  Utility[];

/*****************************************************************************/
/*
Allocate enough memory to accomodate the X by Y graph, pointed to by global 
storage 'GraphPtr'.  Set global storage 'PlotAreaX' and 'PlotAreaY' to the 
respective sizes of the graph (note that the actual maximum plot point of 
each of these is one less).
*/ 

PlotAllocate
(
int XSize,
int YSize
)
{
   int  BytesRequired;

   /*********/
   /* begin */
   /*********/

   if (Debug) fprintf (stdout, "PlotAllocate() %d,%d\n", XSize, YSize);

   PlotAreaX = XSize;
   PlotAreaY = YSize;
   /* bits in plot area, divided by bits/per byte, + rounding error allowance */
   BytesRequired = ((PlotAreaX * PlotAreaY) / 8) + 4;
   if (Debug)
      fprintf (stdout, "BytesRequired: %d long-words: %d\n",
               BytesRequired, BytesRequired/4);
   if ((GraphPtr = (char*)malloc (BytesRequired)) == NULL)
      FatalError ("MALLOC", "error allocating graph memory", ERROR_VALUE);
   memset (GraphPtr, 0, BytesRequired);
}

/*****************************************************************************/
/*
Place a box surrounding the area specified by 'XSize' and 'YSize' offset from 
0,0 by the 'XOrigin' and 'YOrigin'.  Note that this box is actually outside 
the area specified by 'XSize' and 'YSize'.  Place graduation points alongside
each axis.
*/ 

PlotAxes (int PlotOperation)

{
   int  hr, cnt, x, y,
        XSize,
        YSize,
        YGraduation;

   /*********/
   /* begin */
   /*********/

   if (Debug) fprintf (stdout, "PlotAxes()\n");

   XSize = SizeOfPlotX + XMag - 1;
   YSize = SizeOfPlotY + YMag - 1;

   PlotLineX (PlotOperation, -1, XSize+1, -1, SizeOfMarginX, SizeOfMarginY);
   PlotLineX (PlotOperation, -1, XSize+1, YSize+1,
              SizeOfMarginX, SizeOfMarginY);
   PlotLineY (PlotOperation, -1, YSize+1, -1, SizeOfMarginX, SizeOfMarginY);
   PlotLineY (PlotOperation, -1, YSize+1, XSize+1,
              SizeOfMarginX, SizeOfMarginY);

   /* place graduation along X axis */
   hr = FromHour + 1;
   x = (cnt = 1) * 60 * XMag / RecordSampleRate;
   while (x < XSize+1)
   {
      for (y = -5; y < -1; y++)
         PlotPixel (PlotOperation, x, y, SizeOfMarginX, SizeOfMarginY);
      if (hr > 23)
      {
         y = -12;
         PlotPixel (PlotOperation, x-1, y, SizeOfMarginX, SizeOfMarginY);
         PlotPixel (PlotOperation, x+1, y, SizeOfMarginX, SizeOfMarginY);
         for (; y < -1; y++)
            PlotPixel (PlotOperation, x, y, SizeOfMarginX, SizeOfMarginY);
         hr = 0;
      }
      else
      if (hr == 12)
      {
         for (y = -10; y < -1; y++)
            PlotPixel (PlotOperation, x, y, SizeOfMarginX, SizeOfMarginY);
      }
      else
      if (!(hr % 6))
      {
         for (y = -8; y < -1; y++)
            PlotPixel (PlotOperation, x, y, SizeOfMarginX, SizeOfMarginY);
      }
      hr++;
      x = ++cnt * 60 * XMag / RecordSampleRate;
   }

   if (PlotScaleY % 2)
      YGraduation = 20 * YMag;
   else
   if (PlotScaleY == 50)
      YGraduation = 20 * YMag;
   else
   if (PlotScaleY == 250)
      YGraduation = 20 * YMag;
   else
   if (PlotScaleY == 500)
      YGraduation = 20 * YMag;
   else
   if (PlotScaleY == 2500)
      YGraduation = 20 * YMag;
   else
   if (PlotScaleY == 5000)
      YGraduation = 20 * YMag;
   else
      YGraduation = 10 * YMag;

   /* place graduation along Y axis */
   for (y = YGraduation; y < YSize+1; y += YGraduation)
      for (x = -6; x < -1; x++)
         PlotPixel (PlotOperation, x, y, SizeOfMarginX, SizeOfMarginY);

   if (PlotScaleY % 2 ||
       PlotScaleY == 250 ||
       PlotScaleY == 2500) return;

   /* place intermediate graduation along Y axis */
   for (y = (YGraduation/2); y < YSize+1; y += YGraduation)
      for (x = -4; x < -1; x++)
         PlotPixel (PlotOperation, x, y, SizeOfMarginX, SizeOfMarginY);
}

/*****************************************************************************/
/*
*/

PlotPixel
(
int PlotOperation,
int AtX,
int AtY,
int XOrigin,
int YOrigin
)
{
   int  Bit,
        LongWord;

   /*********/
   /* begin */
   /*********/

   if (Debug)
      fprintf (stdout, "PlotPixel() %d,%d %d,%d\n",
               AtX, AtY, XOrigin, YOrigin);

   AtX += XOrigin;
   AtY += YOrigin;
   if (AtX < 0)
   {
      PlotDiagnostic ("PlotPixel() X coordinate less than zero", AtX);
      AtX = 0;
   }
   if (AtX >= PlotAreaX)
   {
      PlotDiagnostic ("PlotPixel() X coordinate out of range", AtX);
      AtX = PlotAreaX-1;
   }
   if (AtY < 0)
   {
      PlotDiagnostic ("PlotPixel() Y coordinate less than zero", AtY);
      AtY = 0;
   }
   if (AtY >= PlotAreaY || AtY < 0)
   {
      PlotDiagnostic ("PlotPixel() Y coordinate out of range", AtY);
      AtY = PlotAreaY-1;
   }
   AtY = PlotAreaY - AtY - 1;
   LongWord = (Bit = AtX + (AtY * PlotAreaX)) / LongBits;
   Bit %= LongBits;
   if (!PlotOperation)
      ((unsigned long*)GraphPtr)[LongWord] |= 1 << Bit;
   else
   if (PlotOperation == PLOT_XOR)
      ((unsigned long*)GraphPtr)[LongWord] ^= 1 << Bit;
   else
      ((unsigned long*)GraphPtr)[LongWord] &= ~(1 << Bit);
}


/*****************************************************************************/
/*
*/

PlotLineX
(
int PlotOperation,
int FromX,
int ToX,
int AtY,
int XOrigin,
int YOrigin
)
{
   int  AtX,
        Bit,
        CurrentLongWord,
        LongWord;
   unsigned long  Pixels;

   /*********/
   /* begin */
   /*********/

   if (Debug)
      fprintf (stdout, "PlotLineX() FromX: %d ToX: %d AtY: %d %d,%d\n",
               FromX, ToX, AtY, XOrigin, YOrigin);

   FromX += XOrigin;
   ToX += XOrigin;
   AtY += YOrigin;
   if (FromX < 0)
   {
      PlotDiagnostic ("PlotLineX() start X coordinate less than zero", FromX);
      FromX = 0;
   }
   if (FromX >= PlotAreaX)
   {
      PlotDiagnostic ("PlotLineX() start X coordinate out of range", FromX);
      FromX = PlotAreaX-1;
   }
   if (ToX < 0)
   {
      PlotDiagnostic ("PlotLineX() end X coordinate less than zero", ToX);
      ToX = 0;
   }
   if (ToX >= PlotAreaX)
   {
      PlotDiagnostic ("PlotLineX() end X coordinate out of range", ToX);
      ToX = PlotAreaX-1;
   }
   if (AtY < 0)
   {
      PlotDiagnostic ("PlotLineX() Y coordinate less than zero", AtY);
      AtY = 0;
   }
   if (AtY >= PlotAreaY)
   {
      PlotDiagnostic ("PlotLineX() Y coordinate out of range", AtY);
      AtY = PlotAreaY-1;
   }

   if (FromX > ToX)
   {
      int  Temp;

      Temp = FromX;
      FromX = ToX;
      ToX = Temp;
   }

   CurrentLongWord = -1;
   AtY = PlotAreaY - AtY - 1;
   for (AtX = FromX; AtX <= ToX; AtX++)
   {
      LongWord = (Bit = AtX + (AtY * PlotAreaX)) / LongBits;
      Bit %= LongBits;
      if (CurrentLongWord != LongWord)
      {
         if (CurrentLongWord != -1)
            ((unsigned long*)GraphPtr)[CurrentLongWord] = Pixels;
         Pixels = ((unsigned long*)GraphPtr)[CurrentLongWord = LongWord];
      }
      if (!PlotOperation)
         Pixels |= 1 << Bit;
      else
      if (PlotOperation == PLOT_XOR)
         Pixels ^= 1 << Bit;
      else
         Pixels &= ~(1 << Bit);
   }

   if (CurrentLongWord != -1)
   {
      if (Debug) fprintf (stdout, "storing: %d\n", CurrentLongWord);
      ((unsigned long*)GraphPtr)[CurrentLongWord] = Pixels;
   }
}


/*****************************************************************************/
/*
*/

PlotLineY
(
int PlotOperation,
int FromY,
int ToY,
int AtX,
int XOrigin,
int YOrigin
)
{
   int  Bit,
        AtY,
        LongWord;

   /*********/
   /* begin */
   /*********/

   if (Debug)
      fprintf (stdout, "PlotLineY() FromY: %d ToY: %d AtX: %d %d,%d\n",
               FromY, ToY, AtX, XOrigin, YOrigin);

   FromY += YOrigin;
   ToY += YOrigin;
   AtX += XOrigin;
   if (FromY < 0)
   {
      PlotDiagnostic ("PlotLineY() start Y coordinate less than zero", FromY);
      FromY = 0;
   }
   if (FromY >= PlotAreaY)
   {
      PlotDiagnostic ("PlotLineY() start Y coordinate out of range", FromY);
      FromY = PlotAreaY-1;
   }
   if (ToY < 0)
   {
      PlotDiagnostic ("PlotLineY() end Y coordinate less than zero", ToY);
      ToY = 0;
   }
   if (ToY >= PlotAreaY)
   {
      PlotDiagnostic ("PlotLineY() end Y coordinate out of range", ToY);
      ToY = PlotAreaY-1;
   }
   if (AtX < 0)
   {
      PlotDiagnostic ("PlotLineY() X coordinate less than zero", AtX);
      AtX = 0;
   }
   if (AtX >= PlotAreaX)
   {
      PlotDiagnostic ("PlotLineY() X coordinate out of range", AtX);
      AtX = PlotAreaX-1;
   }

   if (FromY > ToY)
   {
      int  Temp;

      Temp = FromY;
      FromY = ToY;
      ToY = Temp;
   }

   FromY = PlotAreaY - FromY - 1;
   ToY = PlotAreaY - ToY - 1;
   for (AtY = FromY; AtY >= ToY; AtY--)
   {
      LongWord = (Bit = AtX + (AtY * PlotAreaX)) / LongBits;
      Bit %= LongBits;
      if (!PlotOperation)
         ((unsigned long*)GraphPtr)[LongWord] |= 1 << Bit;
      else
      if (PlotOperation == PLOT_XOR)
         ((unsigned long*)GraphPtr)[LongWord] ^= 1 << Bit;
      else
         ((unsigned long*)GraphPtr)[LongWord] &= ~(1 << Bit);
   }
}

/*****************************************************************************/
/*
*/

PlotDiagnostic
(
char *Description,
int Value
)
{
   if (!DoPlotDiagnostic) return;
   fprintf (stdout, "DIAGNOSTIC; %s: %d\n", Description, Value);
}

/*****************************************************************************/
/*
*/

FatalError
(
char *Identifier,
char *Description,
int ErrorValue
)
{
   /*********/
   /* begin */
   /*********/

#ifdef __VMS
   fprintf (stdout, "%%%s-E-%s, %s\n", Utility, Identifier, Description);
   if (ErrorValue)
      exit (ErrorValue);
   else
      exit (2 | 0x10000000);
#else
   if (ErrorValue)
      fprintf (stdout, "%s: %s: %s\n",
               Utility, Description, strerror(ErrorValue));
   else
      fprintf (stdout, "%s: %s\n", Utility, Description);
   exit (1);
#endif
}

/*****************************************************************************/